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
= _cupsStrAlloc(printer
->job_sheets
[0]);
1744 attr
->values
[1].string
.text
= _cupsStrAlloc(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 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
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
);
2187 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
2190 * Remove all of the subscription attributes from the job request...
2192 * TODO: Optimize this since subscription groups have to come at the
2193 * end of the request...
2196 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2200 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2201 attr
->group_tag
== IPP_TAG_ZERO
)
2204 * Free and remove this attribute...
2212 job
->attrs
->attrs
= next
;
2218 job
->attrs
->last
= prev
;
2219 job
->attrs
->current
= prev
;
2224 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2226 * See RFC 4122 for the definition of UUIDs and the format.
2230 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
2231 cupsd_job_t
*job
) /* I - Job */
2233 char uuid
[1024]; /* job-uuid string */
2234 _cups_md5_state_t md5state
; /* MD5 state */
2235 unsigned char md5sum
[16]; /* MD5 digest/sum */
2239 * First see if the job already has a job-uuid attribute; if so, return...
2242 if (ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2246 * No job-uuid attribute, so build a version 3 UUID with the local job
2247 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
2248 * the ServerName, server name and port that the client connected to,
2249 * and local job ID...
2252 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
2253 con
->serverport
, job
->id
);
2255 _cupsMD5Init(&md5state
);
2256 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
2257 _cupsMD5Finish(&md5state
, md5sum
);
2260 * Format the UUID URI using the MD5 sum and job ID.
2263 snprintf(uuid
, sizeof(uuid
),
2264 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2265 "%02x%02x%02x%02x%02x%02x",
2266 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
2267 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
2268 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
2269 md5sum
[14], md5sum
[15]);
2271 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
2276 * 'add_printer()' - Add a printer to the system.
2280 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2281 ipp_attribute_t
*uri
) /* I - URI of printer */
2283 http_status_t status
; /* Policy status */
2284 int i
; /* Looping var */
2285 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2286 username
[HTTP_MAX_URI
], /* Username portion of URI */
2287 host
[HTTP_MAX_URI
], /* Host portion of URI */
2288 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2289 int port
; /* Port portion of URI */
2290 cupsd_printer_t
*printer
; /* Printer/class */
2291 ipp_attribute_t
*attr
; /* Printer attribute */
2292 cups_file_t
*fp
; /* Script/PPD file */
2293 char line
[1024]; /* Line from file... */
2294 char srcfile
[1024], /* Source Script/PPD file */
2295 dstfile
[1024]; /* Destination Script/PPD file */
2296 int modify
; /* Non-zero if we are modifying */
2297 char newname
[IPP_MAX_NAME
]; /* New printer name */
2298 int need_restart_job
; /* Need to restart job? */
2299 int set_device_uri
, /* Did we set the device URI? */
2300 set_port_monitor
; /* Did we set the port monitor? */
2303 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2304 con
->http
.fd
, uri
->values
[0].string
.text
);
2307 * Do we have a valid URI?
2310 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2311 sizeof(scheme
), username
, sizeof(username
), host
,
2312 sizeof(host
), &port
, resource
, sizeof(resource
));
2314 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2317 * No, return an error...
2320 send_ipp_status(con
, IPP_BAD_REQUEST
,
2321 _("The printer-uri must be of the form "
2322 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2327 * Do we have a valid printer name?
2330 if (!validate_name(resource
+ 10))
2333 * No, return an error...
2336 send_ipp_status(con
, IPP_BAD_REQUEST
,
2337 _("The printer-uri \"%s\" contains invalid characters."),
2338 uri
->values
[0].string
.text
);
2343 * See if the printer already exists; if not, create a new printer...
2346 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2349 * Printer doesn't exist; see if we have a class of the same name...
2352 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2353 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
2356 * Yes, return an error...
2359 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2360 _("A class named \"%s\" already exists!"),
2366 * No, check the default policy then add the printer...
2369 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2371 send_http_error(con
, status
, NULL
);
2375 printer
= cupsdAddPrinter(resource
+ 10);
2378 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2381 * Check the default policy, then rename the implicit printer to
2382 * "AnyPrinter" or delete it...
2385 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2387 send_http_error(con
, status
, NULL
);
2391 if (ImplicitAnyClasses
)
2393 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2394 cupsdRenamePrinter(printer
, newname
);
2397 cupsdDeletePrinter(printer
, 1);
2400 * Add the printer as a new local printer...
2403 printer
= cupsdAddPrinter(resource
+ 10);
2406 else if (printer
->type
& CUPS_PRINTER_DISCOVERED
)
2409 * Check the default policy, then rename the remote printer to
2410 * "Printer@server"...
2413 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2415 send_http_error(con
, status
, NULL
);
2419 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2421 cupsdRenamePrinter(printer
, newname
);
2424 * Add the printer as a new local printer...
2427 printer
= cupsdAddPrinter(resource
+ 10);
2430 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2433 send_http_error(con
, status
, printer
);
2440 * Look for attributes and copy them over as needed...
2443 need_restart_job
= 0;
2445 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2446 IPP_TAG_TEXT
)) != NULL
)
2447 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2449 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2450 IPP_TAG_TEXT
)) != NULL
)
2451 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2455 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2456 IPP_TAG_URI
)) != NULL
)
2459 * Do we have a valid device URI?
2462 http_uri_status_t uri_status
; /* URI separation status */
2463 char old_device_uri
[1024];
2464 /* Old device URI */
2467 need_restart_job
= 1;
2469 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2470 attr
->values
[0].string
.text
,
2471 scheme
, sizeof(scheme
),
2472 username
, sizeof(username
),
2473 host
, sizeof(host
), &port
,
2474 resource
, sizeof(resource
));
2476 if (uri_status
< HTTP_URI_OK
)
2478 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2479 attr
->values
[0].string
.text
);
2480 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2481 "add_printer: httpSeparateURI returned %d", uri_status
);
2485 if (!strcmp(scheme
, "file"))
2488 * See if the administrator has enabled file devices...
2491 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2494 * File devices are disabled and the URL is not file:/dev/null...
2497 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2498 _("File device URIs have been disabled! "
2499 "To enable, see the FileDevice directive in "
2500 "\"%s/cupsd.conf\"."),
2508 * See if the backend exists and is executable...
2511 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2512 if (access(srcfile
, X_OK
))
2515 * Could not find device in list!
2518 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri scheme \"%s\"!"),
2524 if (printer
->sanitized_device_uri
)
2525 strlcpy(old_device_uri
, printer
->sanitized_device_uri
,
2526 sizeof(old_device_uri
));
2528 old_device_uri
[0] = '\0';
2530 cupsdSetDeviceURI(printer
, attr
->values
[0].string
.text
);
2532 cupsdLogMessage(CUPSD_LOG_INFO
,
2533 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2534 printer
->name
, printer
->sanitized_device_uri
,
2540 set_port_monitor
= 0;
2542 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2543 IPP_TAG_NAME
)) != NULL
)
2545 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2548 need_restart_job
= 1;
2550 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2554 for (i
= 0; i
< supported
->num_values
; i
++)
2555 if (!strcmp(supported
->values
[i
].string
.text
,
2556 attr
->values
[0].string
.text
))
2560 if (!supported
|| i
>= supported
->num_values
)
2562 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2563 attr
->values
[0].string
.text
);
2567 cupsdLogMessage(CUPSD_LOG_INFO
,
2568 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2569 printer
->name
, attr
->values
[0].string
.text
,
2570 printer
->port_monitor
? printer
->port_monitor
: "none");
2572 if (strcmp(attr
->values
[0].string
.text
, "none"))
2573 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2575 cupsdClearString(&printer
->port_monitor
);
2577 set_port_monitor
= 1;
2580 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2581 IPP_TAG_BOOLEAN
)) != NULL
)
2583 cupsdLogMessage(CUPSD_LOG_INFO
,
2584 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2585 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2587 printer
->accepting
= attr
->values
[0].boolean
;
2588 cupsdAddPrinterHistory(printer
);
2591 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2592 IPP_TAG_BOOLEAN
)) != NULL
)
2594 if (printer
->shared
&& !attr
->values
[0].boolean
)
2595 cupsdDeregisterPrinter(printer
, 1);
2597 cupsdLogMessage(CUPSD_LOG_INFO
,
2598 "Setting %s printer-is-shared to %d (was %d.)",
2599 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2601 printer
->shared
= attr
->values
[0].boolean
;
2604 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2605 IPP_TAG_ENUM
)) != NULL
)
2607 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2608 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2610 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2611 attr
->values
[0].integer
);
2615 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2616 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2618 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2619 cupsdStopPrinter(printer
, 0);
2622 need_restart_job
= 1;
2623 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2626 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2627 IPP_TAG_TEXT
)) != NULL
)
2629 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2630 sizeof(printer
->state_message
));
2631 cupsdAddPrinterHistory(printer
);
2634 if ((attr
= ippFindAttribute(con
->request
, "printer-state-reasons",
2635 IPP_TAG_KEYWORD
)) != NULL
)
2637 if (attr
->num_values
>
2638 (int)(sizeof(printer
->reasons
) / sizeof(printer
->reasons
[0])))
2640 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2641 _("Too many printer-state-reasons values (%d > %d)!"),
2643 (int)(sizeof(printer
->reasons
) /
2644 sizeof(printer
->reasons
[0])));
2648 for (i
= 0; i
< printer
->num_reasons
; i
++)
2649 _cupsStrFree(printer
->reasons
[i
]);
2651 printer
->num_reasons
= 0;
2652 for (i
= 0; i
< attr
->num_values
; i
++)
2654 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
2657 printer
->reasons
[printer
->num_reasons
] =
2658 _cupsStrAlloc(attr
->values
[i
].string
.text
);
2660 if (!strcmp(printer
->reasons
[printer
->num_reasons
], "paused") &&
2661 printer
->state
!= IPP_PRINTER_STOPPED
)
2663 cupsdLogMessage(CUPSD_LOG_INFO
,
2664 "Setting %s printer-state to %d (was %d.)",
2665 printer
->name
, IPP_PRINTER_STOPPED
, printer
->state
);
2666 cupsdStopPrinter(printer
, 0);
2669 printer
->num_reasons
++;
2672 if (PrintcapFormat
== PRINTCAP_PLIST
)
2673 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2676 set_printer_defaults(con
, printer
);
2678 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2679 IPP_TAG_KEYWORD
)) != NULL
)
2680 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2683 * See if we have all required attributes...
2686 if (!printer
->device_uri
)
2687 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2690 * See if we have an interface script or PPD file attached to the request...
2695 need_restart_job
= 1;
2697 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2699 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2702 * Yes; get the first line from it...
2706 cupsFileGets(fp
, line
, sizeof(line
));
2710 * Then see what kind of file it is...
2713 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2716 if (!strncmp(line
, "*PPD-Adobe", 10))
2719 * The new file is a PPD file, so remove any old interface script
2720 * that might be lying around...
2728 * This must be an interface script, so move the file over to the
2729 * interfaces directory and make it executable...
2732 if (copy_file(srcfile
, dstfile
))
2734 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2735 _("Unable to copy interface script - %s!"),
2740 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2741 "Copied interface script successfully!");
2742 chmod(dstfile
, 0755);
2745 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2748 if (!strncmp(line
, "*PPD-Adobe", 10))
2751 * The new file is a PPD file, so move the file over to the
2752 * ppd directory and make it readable by all...
2755 if (copy_file(srcfile
, dstfile
))
2757 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2758 _("Unable to copy PPD file - %s!"),
2763 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2764 "Copied PPD file successfully!");
2765 chmod(dstfile
, 0644);
2769 * (Re)register color profiles...
2774 apple_unregister_profiles(printer
);
2775 apple_register_profiles(printer
);
2777 #endif /* __APPLE__ */
2782 * This must be an interface script, so remove any old PPD file that
2783 * may be lying around...
2790 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2791 IPP_TAG_NAME
)) != NULL
)
2793 need_restart_job
= 1;
2795 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2798 * Raw driver, remove any existing PPD or interface script files.
2801 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2805 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2815 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2819 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2822 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2824 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2828 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2829 "Copied PPD file successfully!");
2830 chmod(dstfile
, 0644);
2834 * (Re)register color profiles...
2839 apple_unregister_profiles(printer
);
2840 apple_register_profiles(printer
);
2842 #endif /* __APPLE__ */
2847 * If we set the device URI but not the port monitor, check which port
2848 * monitor to use by default...
2851 if (set_device_uri
&& !set_port_monitor
)
2853 ppd_file_t
*ppd
; /* PPD file */
2854 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2857 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2858 sizeof(scheme
), username
, sizeof(username
), host
,
2859 sizeof(host
), &port
, resource
, sizeof(resource
));
2861 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2863 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2865 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2867 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2868 if (!strcmp(scheme
, ppdattr
->spec
))
2870 cupsdLogMessage(CUPSD_LOG_INFO
,
2871 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2872 printer
->name
, ppdattr
->value
,
2873 printer
->port_monitor
? printer
->port_monitor
2876 if (strcmp(ppdattr
->value
, "none"))
2877 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2879 cupsdClearString(&printer
->port_monitor
);
2889 * Update the printer attributes and return...
2892 cupsdSetPrinterAttrs(printer
);
2893 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2895 if (need_restart_job
&& printer
->job
)
2900 * Stop the current job and then restart it below...
2903 job
= (cupsd_job_t
*)printer
->job
;
2905 cupsdStopJob(job
, 1);
2907 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2908 job
->state_value
= IPP_JOB_PENDING
;
2911 if (need_restart_job
)
2914 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2918 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2919 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2922 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2923 printer
->name
, get_username(con
));
2927 cupsdAddPrinterHistory(printer
);
2929 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2930 "New printer \"%s\" added by \"%s\".", printer
->name
,
2933 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2934 printer
->name
, get_username(con
));
2937 con
->response
->request
.status
.status_code
= IPP_OK
;
2942 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2943 * based upon the printer state...
2947 add_printer_state_reasons(
2948 cupsd_client_t
*con
, /* I - Client connection */
2949 cupsd_printer_t
*p
) /* I - Printer info */
2951 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2952 "add_printer_state_reasons(%p[%d], %p[%s])",
2953 con
, con
->http
.fd
, p
, p
->name
);
2955 if (p
->num_reasons
== 0)
2956 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2957 "printer-state-reasons", NULL
,
2958 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2960 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2961 "printer-state-reasons", p
->num_reasons
, NULL
,
2962 (const char * const *)p
->reasons
);
2967 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2968 * the specified printer or class.
2972 add_queued_job_count(
2973 cupsd_client_t
*con
, /* I - Client connection */
2974 cupsd_printer_t
*p
) /* I - Printer or class */
2976 int count
; /* Number of jobs on destination */
2979 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2980 con
, con
->http
.fd
, p
, p
->name
);
2982 count
= cupsdGetPrinterJobCount(p
->name
);
2984 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2985 "queued-job-count", count
);
2991 * 'apple_init_profile()' - Initialize a color profile.
2996 ppd_file_t
*ppd
, /* I - PPD file */
2997 cups_array_t
*languages
, /* I - Languages in the PPD file */
2998 CMDeviceProfileInfo
*profile
, /* I - Profile record */
2999 unsigned id
, /* I - Profile ID */
3000 const char *name
, /* I - Profile name */
3001 const char *text
, /* I - Profile UI text */
3002 const char *iccfile
) /* I - ICC filename */
3004 char url
[1024]; /* URL for profile filename */
3005 CFMutableDictionaryRef dict
; /* Dictionary for name */
3006 char *language
; /* Current language */
3007 ppd_attr_t
*attr
; /* Profile attribute */
3008 CFStringRef cflang
, /* Language string */
3009 cftext
; /* Localized text */
3013 * Build the profile name dictionary...
3016 dict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3017 &kCFTypeDictionaryKeyCallBacks
,
3018 &kCFTypeDictionaryValueCallBacks
);
3020 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, text
,
3021 kCFStringEncodingUTF8
);
3025 CFDictionarySetValue(dict
, CFSTR("en"), cftext
);
3032 * Find localized names for the color profiles...
3035 cupsArraySave(ppd
->sorted_attrs
);
3037 for (language
= (char *)cupsArrayFirst(languages
);
3039 language
= (char *)cupsArrayNext(languages
))
3043 if ((attr
= _ppdLocalizedAttr(ppd
, "cupsICCProfile", name
,
3045 attr
= _ppdLocalizedAttr(ppd
, "APTiogaProfile", name
, language
);
3048 attr
= _ppdLocalizedAttr(ppd
, "ColorModel", name
, language
);
3050 if (attr
&& attr
->text
[0])
3052 cflang
= CFStringCreateWithCString(kCFAllocatorDefault
, language
,
3053 kCFStringEncodingUTF8
);
3054 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, attr
->text
,
3055 kCFStringEncodingUTF8
);
3057 if (cflang
&& cftext
)
3058 CFDictionarySetValue(dict
, cflang
, cftext
);
3068 cupsArrayRestore(ppd
->sorted_attrs
);
3072 * Fill in the profile data...
3076 httpAssembleURI(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "file", NULL
, "", 0,
3079 profile
->dataVersion
= cmDeviceProfileInfoVersion1
;
3080 profile
->profileID
= id
;
3081 profile
->profileLoc
.locType
= iccfile
? cmPathBasedProfile
: cmNoProfileBase
;
3082 profile
->profileName
= dict
;
3085 strlcpy(profile
->profileLoc
.u
.pathLoc
.path
, iccfile
,
3086 sizeof(profile
->profileLoc
.u
.pathLoc
.path
));
3091 * 'apple_register_profiles()' - Register color profiles for a printer.
3095 apple_register_profiles(
3096 cupsd_printer_t
*p
) /* I - Printer */
3098 int i
; /* Looping var */
3099 char ppdfile
[1024], /* PPD filename */
3100 iccfile
[1024], /* ICC filename */
3101 selector
[PPD_MAX_NAME
];
3102 /* Profile selection string */
3103 ppd_file_t
*ppd
; /* PPD file */
3104 ppd_attr_t
*attr
, /* Profile attributes */
3105 *profileid_attr
,/* cupsProfileID attribute */
3106 *q1_attr
, /* ColorModel (or other) qualifier */
3107 *q2_attr
, /* MediaType (or other) qualifier */
3108 *q3_attr
; /* Resolution (or other) qualifier */
3109 char q_keyword
[PPD_MAX_NAME
];
3110 /* Qualifier keyword */
3111 const char *q1_choice
, /* ColorModel (or other) choice */
3112 *q2_choice
, /* MediaType (or other) choice */
3113 *q3_choice
; /* Resolution (or other) choice */
3114 const char *profile_key
; /* Profile keyword */
3115 ppd_option_t
*cm_option
; /* Color model option */
3116 ppd_choice_t
*cm_choice
; /* Color model choice */
3117 int num_profiles
; /* Number of profiles */
3118 CMError error
; /* Last error */
3119 unsigned device_id
, /* Printer device ID */
3120 profile_id
, /* Profile ID */
3121 default_profile_id
= 0;
3122 /* Default profile ID */
3123 CFMutableDictionaryRef device_name
; /* Printer device name dictionary */
3124 CFStringRef printer_name
; /* Printer name string */
3125 CMDeviceScope scope
= /* Scope of the registration */
3127 kCFPreferencesAnyUser
,
3128 kCFPreferencesCurrentHost
3130 CMDeviceProfileArrayPtr profiles
; /* Profiles */
3131 CMDeviceProfileInfo
*profile
; /* Current profile */
3132 cups_array_t
*languages
; /* Languages array */
3136 * Make sure ColorSync is available...
3139 if (CMRegisterColorDevice
== NULL
)
3143 * Try opening the PPD file for this printer...
3146 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3147 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
3151 * See if we have any profiles...
3154 if ((attr
= ppdFindAttr(ppd
, "APTiogaProfile", NULL
)) != NULL
)
3155 profile_key
= "APTiogaProfile";
3158 attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
3159 profile_key
= "cupsICCProfile";
3162 for (num_profiles
= 0; attr
; attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3163 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3165 if (attr
->value
[0] != '/')
3166 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3169 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3171 if (access(iccfile
, 0))
3179 * If we have profiles, add them...
3182 if (num_profiles
> 0)
3184 if (profile_key
[0] == 'A')
3187 * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile
3191 if ((attr
= ppdFindAttr(ppd
, "DefaultAPTiogaProfile", NULL
)) != NULL
&&
3193 default_profile_id
= atoi(attr
->value
);
3195 q1_choice
= q2_choice
= q3_choice
= NULL
;
3200 * For CUPS PPDs, figure out the default profile selector values...
3203 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier1", NULL
)) != NULL
&&
3204 attr
->value
&& attr
->value
[0])
3206 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3207 q1_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3209 else if ((q1_attr
= ppdFindAttr(ppd
, "DefaultColorModel", NULL
)) == NULL
)
3210 q1_attr
= ppdFindAttr(ppd
, "DefaultColorSpace", NULL
);
3212 if (q1_attr
&& q1_attr
->value
&& q1_attr
->value
[0])
3213 q1_choice
= q1_attr
->value
;
3217 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier2", NULL
)) != NULL
&&
3218 attr
->value
&& attr
->value
[0])
3220 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3221 q2_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3224 q2_attr
= ppdFindAttr(ppd
, "DefaultMediaType", NULL
);
3226 if (q2_attr
&& q2_attr
->value
&& q2_attr
->value
[0])
3227 q2_choice
= q2_attr
->value
;
3231 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier3", NULL
)) != NULL
&&
3232 attr
->value
&& attr
->value
[0])
3234 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3235 q3_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3238 q3_attr
= ppdFindAttr(ppd
, "DefaultResolution", NULL
);
3240 if (q3_attr
&& q3_attr
->value
&& q3_attr
->value
[0])
3241 q3_choice
= q3_attr
->value
;
3247 * Build the array of profiles...
3249 * Note: This calloc actually requests slightly more memory than needed.
3252 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3254 cupsdLogMessage(CUPSD_LOG_ERROR
,
3255 "Unable to allocate memory for %d profiles!",
3261 profiles
->profileCount
= num_profiles
;
3262 languages
= _ppdGetLanguages(ppd
);
3264 for (profile
= profiles
->profiles
,
3265 attr
= ppdFindAttr(ppd
, profile_key
, NULL
);
3267 attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3268 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3271 * Add this profile...
3274 if (attr
->value
[0] != '/')
3275 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3278 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3280 if (access(iccfile
, 0))
3283 if (profile_key
[0] == 'c')
3285 cupsArraySave(ppd
->sorted_attrs
);
3287 if ((profileid_attr
= ppdFindAttr(ppd
, "cupsProfileID",
3288 attr
->spec
)) != NULL
&&
3289 profileid_attr
->value
&& isdigit(profileid_attr
->value
[0] & 255))
3290 profile_id
= (unsigned)strtoul(profileid_attr
->value
, NULL
, 10);
3292 profile_id
= _ppdHashName(attr
->spec
);
3294 cupsArrayRestore(ppd
->sorted_attrs
);
3297 profile_id
= atoi(attr
->spec
);
3299 apple_init_profile(ppd
, languages
, profile
, profile_id
, attr
->spec
,
3300 attr
->text
[0] ? attr
->text
: attr
->spec
, iccfile
);
3305 * See if this is the default profile...
3308 if (!default_profile_id
)
3314 snprintf(selector
, sizeof(selector
), "%s.%s.%s",
3315 q1_choice
, q2_choice
, q3_choice
);
3316 if (!strcmp(selector
, attr
->spec
))
3317 default_profile_id
= profile_id
;
3320 if (!default_profile_id
)
3322 snprintf(selector
, sizeof(selector
), "%s.%s.", q1_choice
,
3324 if (!strcmp(selector
, attr
->spec
))
3325 default_profile_id
= profile_id
;
3329 if (!default_profile_id
&& q3_choice
)
3331 snprintf(selector
, sizeof(selector
), "%s..%s", q1_choice
,
3333 if (!strcmp(selector
, attr
->spec
))
3334 default_profile_id
= profile_id
;
3337 if (!default_profile_id
)
3339 snprintf(selector
, sizeof(selector
), "%s..", q1_choice
);
3340 if (!strcmp(selector
, attr
->spec
))
3341 default_profile_id
= profile_id
;
3346 _ppdFreeLanguages(languages
);
3348 else if ((cm_option
= ppdFindOption(ppd
, "ColorModel")) != NULL
)
3351 * Extract profiles from ColorModel option...
3354 const char *profile_name
; /* Name of generic profile */
3357 num_profiles
= cm_option
->num_choices
;
3359 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3361 cupsdLogMessage(CUPSD_LOG_ERROR
,
3362 "Unable to allocate memory for %d profiles!",
3368 profiles
->profileCount
= num_profiles
;
3370 for (profile
= profiles
->profiles
, i
= cm_option
->num_choices
,
3371 cm_choice
= cm_option
->choices
;
3373 i
--, cm_choice
++, profile
++)
3375 if (!strcmp(cm_choice
->choice
, "Gray") ||
3376 !strcmp(cm_choice
->choice
, "Black"))
3377 profile_name
= "Gray";
3378 else if (!strcmp(cm_choice
->choice
, "RGB") ||
3379 !strcmp(cm_choice
->choice
, "CMY"))
3380 profile_name
= "RGB";
3381 else if (!strcmp(cm_choice
->choice
, "CMYK") ||
3382 !strcmp(cm_choice
->choice
, "KCMY"))
3383 profile_name
= "CMYK";
3385 profile_name
= "DeviceN";
3387 snprintf(selector
, sizeof(selector
), "%s..", profile_name
);
3388 profile_id
= _ppdHashName(selector
);
3390 apple_init_profile(ppd
, NULL
, profile
, profile_id
, cm_choice
->choice
,
3391 cm_choice
->text
, NULL
);
3393 if (cm_choice
->marked
)
3394 default_profile_id
= profile_id
;
3400 * Use the default colorspace...
3403 num_profiles
= 1 + ppd
->colorspace
!= PPD_CS_GRAY
;
3405 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3407 cupsdLogMessage(CUPSD_LOG_ERROR
,
3408 "Unable to allocate memory for %d profiles!",
3414 profiles
->profileCount
= num_profiles
;
3416 apple_init_profile(ppd
, NULL
, profiles
->profiles
, _ppdHashName("Gray.."),
3417 "Gray", "Gray", NULL
);
3419 switch (ppd
->colorspace
)
3423 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3424 _ppdHashName("RGB.."), "RGB", "RGB", NULL
);
3428 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3429 _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL
);
3433 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3434 _ppdHashName("DeviceN.."), "DeviceN", "DeviceN",
3443 if (num_profiles
> 0)
3446 * Make sure we have a default profile ID...
3449 if (!default_profile_id
)
3450 default_profile_id
= profiles
->profiles
[num_profiles
- 1].profileID
;
3453 * Get the device ID hash and pathelogical name dictionary.
3456 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\"",
3459 device_id
= _ppdHashName(p
->name
);
3460 device_name
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3461 &kCFTypeDictionaryKeyCallBacks
,
3462 &kCFTypeDictionaryValueCallBacks
);
3463 printer_name
= CFStringCreateWithCString(kCFAllocatorDefault
,
3464 p
->name
, kCFStringEncodingUTF8
);
3466 if (device_name
&& printer_name
)
3468 CFDictionarySetValue(device_name
, CFSTR("en"), printer_name
);
3471 * Register the device with ColorSync...
3474 error
= CMRegisterColorDevice(cmPrinterDeviceClass
, device_id
,
3475 device_name
, &scope
);
3478 * Register the profiles...
3482 error
= CMSetDeviceFactoryProfiles(cmPrinterDeviceClass
, device_id
,
3483 default_profile_id
, profiles
);
3493 cupsdLogMessage(CUPSD_LOG_ERROR
,
3494 "Unable to register ICC color profiles for \"%s\" - %d",
3495 p
->name
, (int)error
);
3497 for (profile
= profiles
->profiles
;
3499 profile
++, num_profiles
--)
3500 CFRelease(profile
->profileName
);
3505 CFRelease(printer_name
);
3508 CFRelease(device_name
);
3516 * 'apple_unregister_profiles()' - Remove color profiles for the specified
3521 apple_unregister_profiles(
3522 cupsd_printer_t
*p
) /* I - Printer */
3525 * Make sure ColorSync is available...
3528 if (CMUnregisterColorDevice
!= NULL
)
3529 CMUnregisterColorDevice(cmPrinterDeviceClass
, _ppdHashName(p
->name
));
3531 #endif /* __APPLE__ */
3534 * 'apply_printer_defaults()' - Apply printer default options to a job.
3538 apply_printer_defaults(
3539 cupsd_printer_t
*printer
, /* I - Printer */
3540 cupsd_job_t
*job
) /* I - Job */
3542 int i
, /* Looping var */
3543 num_options
; /* Number of default options */
3544 cups_option_t
*options
, /* Default options */
3545 *option
; /* Current option */
3549 * Collect all of the default options and add the missing ones to the
3553 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
3554 option
= printer
->options
;
3557 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
3559 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
3564 * Encode these options as attributes in the job object...
3567 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
3568 cupsFreeOptions(num_options
, options
);
3573 * 'authenticate_job()' - Set job authentication info.
3577 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
3578 ipp_attribute_t
*uri
) /* I - Job URI */
3580 ipp_attribute_t
*attr
, /* job-id attribute */
3581 *auth_info
; /* auth-info attribute */
3582 int jobid
; /* Job ID */
3583 cupsd_job_t
*job
; /* Current job */
3584 char scheme
[HTTP_MAX_URI
],
3585 /* Method portion of URI */
3586 username
[HTTP_MAX_URI
],
3587 /* Username portion of URI */
3589 /* Host portion of URI */
3590 resource
[HTTP_MAX_URI
];
3591 /* Resource portion of URI */
3592 int port
; /* Port portion of URI */
3595 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
3596 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
3599 * Start with "everything is OK" status...
3602 con
->response
->request
.status
.status_code
= IPP_OK
;
3605 * See if we have a job URI or a printer URI...
3608 if (!strcmp(uri
->name
, "printer-uri"))
3611 * Got a printer URI; see if we also have a job-id attribute...
3614 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3615 IPP_TAG_INTEGER
)) == NULL
)
3617 send_ipp_status(con
, IPP_BAD_REQUEST
,
3618 _("Got a printer-uri attribute but no job-id!"));
3622 jobid
= attr
->values
[0].integer
;
3627 * Got a job URI; parse it to get the job ID...
3630 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3631 sizeof(scheme
), username
, sizeof(username
), host
,
3632 sizeof(host
), &port
, resource
, sizeof(resource
));
3634 if (strncmp(resource
, "/jobs/", 6))
3640 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
3641 uri
->values
[0].string
.text
);
3645 jobid
= atoi(resource
+ 6);
3649 * See if the job exists...
3652 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3655 * Nope - return a "not found" error...
3658 send_ipp_status(con
, IPP_NOT_FOUND
,
3659 _("Job #%d does not exist!"), jobid
);
3664 * See if the job has been completed...
3667 if (job
->state_value
!= IPP_JOB_HELD
)
3670 * Return a "not-possible" error...
3673 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3674 _("Job #%d is not held for authentication!"),
3680 * See if we have already authenticated...
3683 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3685 if (!con
->username
[0] && !auth_info
)
3687 cupsd_printer_t
*printer
; /* Job destination */
3691 * No auth data. If we need to authenticate via Kerberos, send a
3692 * HTTP auth challenge, otherwise just return an IPP error...
3695 printer
= cupsdFindDest(job
->dest
);
3697 if (printer
&& printer
->num_auth_info_required
> 0 &&
3698 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3699 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3701 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3702 _("No authentication information provided!"));
3707 * See if the job is owned by the requesting user...
3710 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3712 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3717 * Save the authentication information for this job...
3720 save_auth_info(con
, job
, auth_info
);
3723 * Reset the job-hold-until value to "no-hold"...
3726 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3727 IPP_TAG_KEYWORD
)) == NULL
)
3728 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3732 attr
->value_tag
= IPP_TAG_KEYWORD
;
3733 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
3737 * Release the job and return...
3740 cupsdReleaseJob(job
);
3742 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3747 * 'cancel_all_jobs()' - Cancel all print jobs.
3751 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3752 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3754 http_status_t status
; /* Policy status */
3755 cups_ptype_t dtype
; /* Destination type */
3756 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3757 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3758 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3759 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3760 int port
; /* Port portion of URI */
3761 ipp_attribute_t
*attr
; /* Attribute in request */
3762 const char *username
; /* Username */
3763 int purge
; /* Purge? */
3764 cupsd_printer_t
*printer
; /* Printer */
3767 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3768 con
->http
.fd
, uri
->values
[0].string
.text
);
3771 * See if we have a printer URI...
3774 if (strcmp(uri
->name
, "printer-uri"))
3776 send_ipp_status(con
, IPP_BAD_REQUEST
,
3777 _("The printer-uri attribute is required!"));
3782 * Get the username (if any) for the jobs we want to cancel (only if
3783 * "my-jobs" is specified...
3786 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3787 IPP_TAG_BOOLEAN
)) != NULL
&&
3788 attr
->values
[0].boolean
)
3790 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3791 IPP_TAG_NAME
)) != NULL
)
3792 username
= attr
->values
[0].string
.text
;
3795 send_ipp_status(con
, IPP_BAD_REQUEST
,
3796 _("Missing requesting-user-name attribute!"));
3804 * Look for the "purge-jobs" attribute...
3807 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3808 IPP_TAG_BOOLEAN
)) != NULL
)
3809 purge
= attr
->values
[0].boolean
;
3814 * And if the destination is valid...
3817 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3823 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3824 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3825 hostname
, sizeof(hostname
), &port
,
3826 resource
, sizeof(resource
));
3828 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3829 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3831 send_ipp_status(con
, IPP_NOT_FOUND
,
3832 _("The printer or class was not found."));
3840 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3842 send_http_error(con
, status
, NULL
);
3847 * Cancel all jobs on all printers...
3850 cupsdCancelJobs(NULL
, username
, purge
);
3852 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3853 purge
? "purged" : "canceled", get_username(con
));
3861 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3864 send_http_error(con
, status
, printer
);
3869 * Cancel all of the jobs on the named printer...
3872 cupsdCancelJobs(printer
->name
, username
, purge
);
3874 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3875 printer
->name
, purge
? "purged" : "canceled",
3879 con
->response
->request
.status
.status_code
= IPP_OK
;
3884 * 'cancel_job()' - Cancel a print job.
3888 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3889 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3891 ipp_attribute_t
*attr
; /* Current attribute */
3892 int jobid
; /* Job ID */
3893 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3894 username
[HTTP_MAX_URI
], /* Username portion of URI */
3895 host
[HTTP_MAX_URI
], /* Host portion of URI */
3896 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3897 int port
; /* Port portion of URI */
3898 cupsd_job_t
*job
; /* Job information */
3899 cups_ptype_t dtype
; /* Destination type (printer/class) */
3900 cupsd_printer_t
*printer
; /* Printer data */
3901 int purge
; /* Purge the job? */
3904 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3905 con
->http
.fd
, uri
->values
[0].string
.text
);
3908 * See if we have a job URI or a printer URI...
3911 if (!strcmp(uri
->name
, "printer-uri"))
3914 * Got a printer URI; see if we also have a job-id attribute...
3917 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3918 IPP_TAG_INTEGER
)) == NULL
)
3920 send_ipp_status(con
, IPP_BAD_REQUEST
,
3921 _("Got a printer-uri attribute but no job-id!"));
3925 if ((jobid
= attr
->values
[0].integer
) == 0)
3928 * Find the current job on the specified printer...
3931 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3937 send_ipp_status(con
, IPP_NOT_FOUND
,
3938 _("The printer or class was not found."));
3943 * See if the printer is currently printing a job...
3947 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3951 * No, see if there are any pending jobs...
3954 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3956 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3957 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3958 !strcasecmp(job
->dest
, printer
->name
))
3965 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3967 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3968 if (job
->state_value
== IPP_JOB_STOPPED
&&
3969 !strcasecmp(job
->dest
, printer
->name
))
3976 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3987 * Got a job URI; parse it to get the job ID...
3990 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3991 sizeof(scheme
), username
, sizeof(username
), host
,
3992 sizeof(host
), &port
, resource
, sizeof(resource
));
3994 if (strncmp(resource
, "/jobs/", 6))
4000 send_ipp_status(con
, IPP_BAD_REQUEST
,
4001 _("Bad job-uri attribute \"%s\"!"),
4002 uri
->values
[0].string
.text
);
4006 jobid
= atoi(resource
+ 6);
4010 * Look for the "purge-job" attribute...
4013 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
4014 IPP_TAG_BOOLEAN
)) != NULL
)
4015 purge
= attr
->values
[0].boolean
;
4020 * See if the job exists...
4023 if ((job
= cupsdFindJob(jobid
)) == NULL
)
4026 * Nope - return a "not found" error...
4029 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
4034 * See if the job is owned by the requesting user...
4037 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
4039 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
4044 * See if the job is already completed, canceled, or aborted; if so,
4045 * we can't cancel...
4048 if (job
->state_value
>= IPP_JOB_CANCELED
&& !purge
)
4050 switch (job
->state_value
)
4052 case IPP_JOB_CANCELED
:
4053 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4054 _("Job #%d is already canceled - can\'t cancel."),
4058 case IPP_JOB_ABORTED
:
4059 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4060 _("Job #%d is already aborted - can\'t cancel."),
4065 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4066 _("Job #%d is already completed - can\'t cancel."),
4075 * Cancel the job and return...
4078 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
4082 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
4085 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
4088 con
->response
->request
.status
.status_code
= IPP_OK
;
4093 * 'cancel_subscription()' - Cancel a subscription.
4097 cancel_subscription(
4098 cupsd_client_t
*con
, /* I - Client connection */
4099 int sub_id
) /* I - Subscription ID */
4101 http_status_t status
; /* Policy status */
4102 cupsd_subscription_t
*sub
; /* Subscription */
4105 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4106 "cancel_subscription(con=%p[%d], sub_id=%d)",
4107 con
, con
->http
.fd
, sub_id
);
4110 * Is the subscription ID valid?
4113 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
4116 * Bad subscription ID...
4119 send_ipp_status(con
, IPP_NOT_FOUND
,
4120 _("notify-subscription-id %d no good!"), sub_id
);
4128 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
4130 con
, sub
->owner
)) != HTTP_OK
)
4132 send_http_error(con
, status
, sub
->dest
);
4137 * Cancel the subscription...
4140 cupsdDeleteSubscription(sub
, 1);
4142 con
->response
->request
.status
.status_code
= IPP_OK
;
4147 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
4150 static int /* O - 1 if OK, 0 if not */
4151 check_rss_recipient(
4152 const char *recipient
) /* I - Recipient URI */
4154 cupsd_subscription_t
*sub
; /* Current subscription */
4157 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
4159 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
4163 * Compare the URIs up to the first ?...
4166 const char *r1
, *r2
;
4168 for (r1
= recipient
, r2
= sub
->recipient
;
4169 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
4181 * 'check_quotas()' - Check quotas for a printer and user.
4184 static int /* O - 1 if OK, 0 if not */
4185 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
4186 cupsd_printer_t
*p
) /* I - Printer or class */
4188 int i
; /* Looping var */
4189 char username
[33]; /* Username */
4190 cupsd_quota_t
*q
; /* Quota data */
4191 #ifdef HAVE_MBR_UID_TO_UUID
4193 * Use Apple membership APIs which require that all names represent
4194 * valid user account or group records accessible by the server.
4197 uuid_t usr_uuid
; /* UUID for job requesting user */
4198 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
4199 uuid_t grp_uuid
; /* UUID for ACL group name entry */
4200 int mbr_err
; /* Error from membership function */
4201 int is_member
; /* Is this user a member? */
4204 * Use standard POSIX APIs for checking users and groups...
4207 struct passwd
*pw
; /* User password data */
4208 #endif /* HAVE_MBR_UID_TO_UUID */
4211 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
4212 con
, con
->http
.fd
, p
, p
->name
);
4215 * Figure out who is printing...
4218 strlcpy(username
, get_username(con
), sizeof(username
));
4221 * Check global active job limits for printers and users...
4224 if (MaxJobsPerPrinter
)
4227 * Check if there are too many pending jobs on this printer...
4230 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
4232 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
4241 * Check if there are too many pending jobs for this user...
4244 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
4246 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
4253 * Check against users...
4256 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
4261 #ifdef HAVE_MBR_UID_TO_UUID
4263 * Get UUID for job requesting user...
4266 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
4272 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4273 "check_quotas: UUID lookup failed for user \"%s\"",
4275 cupsdLogMessage(CUPSD_LOG_INFO
,
4276 "Denying user \"%s\" access to printer \"%s\" "
4277 "(unknown user)...",
4283 * Get UID and GID of requesting user...
4286 pw
= getpwnam(username
);
4288 #endif /* HAVE_MBR_UID_TO_UUID */
4290 for (i
= 0; i
< p
->num_users
; i
++)
4291 if (p
->users
[i
][0] == '@')
4294 * Check group membership...
4297 #ifdef HAVE_MBR_UID_TO_UUID
4298 if (p
->users
[i
][1] == '#')
4300 if (uuid_parse((char *)p
->users
[i
] + 2, grp_uuid
))
4301 uuid_clear(grp_uuid
);
4303 else if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
4307 * Invalid ACL entries are ignored for matching; just record a
4308 * warning in the log...
4311 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4312 "check_quotas: UUID lookup failed for ACL entry "
4313 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4314 cupsdLogMessage(CUPSD_LOG_WARN
,
4315 "Access control entry \"%s\" not a valid group name; "
4316 "entry ignored", p
->users
[i
]);
4319 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
4323 * At this point, there should be no errors, but check anyways...
4326 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4327 "check_quotas: group \"%s\" membership check "
4328 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
4333 * Stop if we found a match...
4340 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
4342 #endif /* HAVE_MBR_UID_TO_UUID */
4344 #ifdef HAVE_MBR_UID_TO_UUID
4347 if (p
->users
[i
][0] == '#')
4349 if (uuid_parse((char *)p
->users
[i
] + 1, usr2_uuid
))
4350 uuid_clear(usr2_uuid
);
4352 else if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
4356 * Invalid ACL entries are ignored for matching; just record a
4357 * warning in the log...
4360 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4361 "check_quotas: UUID lookup failed for ACL entry "
4362 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4363 cupsdLogMessage(CUPSD_LOG_WARN
,
4364 "Access control entry \"%s\" not a valid user name; "
4365 "entry ignored", p
->users
[i
]);
4368 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
4371 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4372 "check_quotas: User \"%s\" identity check failed "
4373 "(err=%d)", p
->users
[i
], mbr_err
);
4381 else if (!strcasecmp(username
, p
->users
[i
]))
4383 #endif /* HAVE_MBR_UID_TO_UUID */
4385 if ((i
< p
->num_users
) == p
->deny_users
)
4387 cupsdLogMessage(CUPSD_LOG_INFO
,
4388 "Denying user \"%s\" access to printer \"%s\"...",
4399 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
4402 * TODO: Define these special page count values as constants!
4405 if (q
->page_count
== -4) /* special case: unlimited user */
4407 cupsdLogMessage(CUPSD_LOG_INFO
,
4408 "User \"%s\" request approved for printer %s (%s): "
4410 username
, p
->name
, p
->info
);
4411 q
->page_count
= 0; /* allow user to print */
4414 else if (q
->page_count
== -3) /* quota exceeded */
4416 cupsdLogMessage(CUPSD_LOG_INFO
,
4417 "User \"%s\" request denied for printer %s (%s): "
4418 "quota limit exceeded.",
4419 username
, p
->name
, p
->info
);
4420 q
->page_count
= 2; /* force quota exceeded failure */
4423 else if (q
->page_count
== -2) /* quota disabled for user */
4425 cupsdLogMessage(CUPSD_LOG_INFO
,
4426 "User \"%s\" request denied for printer %s (%s): "
4427 "printing disabled for user.",
4428 username
, p
->name
, p
->info
);
4429 q
->page_count
= 2; /* force quota exceeded failure */
4432 else if (q
->page_count
== -1) /* quota access error */
4434 cupsdLogMessage(CUPSD_LOG_INFO
,
4435 "User \"%s\" request denied for printer %s (%s): "
4436 "unable to determine quota limit.",
4437 username
, p
->name
, p
->info
);
4438 q
->page_count
= 2; /* force quota exceeded failure */
4441 else if (q
->page_count
< 0) /* user not found or other error */
4443 cupsdLogMessage(CUPSD_LOG_INFO
,
4444 "User \"%s\" request denied for printer %s (%s): "
4445 "user disabled / missing quota.",
4446 username
, p
->name
, p
->info
);
4447 q
->page_count
= 2; /* force quota exceeded failure */
4450 else /* page within user limits */
4452 q
->page_count
= 0; /* allow user to print */
4457 #endif /* __APPLE__ */
4458 if (p
->k_limit
|| p
->page_limit
)
4460 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
4462 cupsdLogMessage(CUPSD_LOG_ERROR
,
4463 "Unable to allocate quota data for user \"%s\"!",
4468 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
4469 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
4471 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
4478 * If we have gotten this far, we're done!
4486 * 'copy_attribute()' - Copy a single attribute.
4489 static ipp_attribute_t
* /* O - New attribute */
4491 ipp_t
*to
, /* O - Destination request/response */
4492 ipp_attribute_t
*attr
, /* I - Attribute to copy */
4493 int quickcopy
) /* I - Do a quick copy? */
4495 int i
; /* Looping var */
4496 ipp_attribute_t
*toattr
; /* Destination attribute */
4499 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4500 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
4501 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
4504 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
4507 toattr
= ippAddSeparator(to
);
4510 case IPP_TAG_INTEGER
:
4512 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4513 attr
->name
, attr
->num_values
, NULL
);
4515 for (i
= 0; i
< attr
->num_values
; i
++)
4516 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
4519 case IPP_TAG_BOOLEAN
:
4520 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
4521 attr
->num_values
, NULL
);
4523 for (i
= 0; i
< attr
->num_values
; i
++)
4524 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
4527 case IPP_TAG_STRING
:
4530 case IPP_TAG_KEYWORD
:
4532 case IPP_TAG_URISCHEME
:
4533 case IPP_TAG_CHARSET
:
4534 case IPP_TAG_LANGUAGE
:
4535 case IPP_TAG_MIMETYPE
:
4536 toattr
= ippAddStrings(to
, attr
->group_tag
,
4537 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4538 attr
->name
, attr
->num_values
, NULL
, NULL
);
4542 for (i
= 0; i
< attr
->num_values
; i
++)
4543 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4547 for (i
= 0; i
< attr
->num_values
; i
++)
4548 toattr
->values
[i
].string
.text
=
4549 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4554 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
4555 attr
->values
[0].date
);
4558 case IPP_TAG_RESOLUTION
:
4559 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
4560 attr
->num_values
, IPP_RES_PER_INCH
,
4563 for (i
= 0; i
< attr
->num_values
; i
++)
4565 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
4566 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
4567 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
4571 case IPP_TAG_RANGE
:
4572 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
4573 attr
->num_values
, NULL
, NULL
);
4575 for (i
= 0; i
< attr
->num_values
; i
++)
4577 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
4578 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
4582 case IPP_TAG_TEXTLANG
:
4583 case IPP_TAG_NAMELANG
:
4584 toattr
= ippAddStrings(to
, attr
->group_tag
,
4585 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4586 attr
->name
, attr
->num_values
, NULL
, NULL
);
4590 for (i
= 0; i
< attr
->num_values
; i
++)
4592 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
4593 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4598 for (i
= 0; i
< attr
->num_values
; i
++)
4601 toattr
->values
[i
].string
.charset
=
4602 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
4604 toattr
->values
[i
].string
.charset
=
4605 toattr
->values
[0].string
.charset
;
4607 toattr
->values
[i
].string
.text
=
4608 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4613 case IPP_TAG_BEGIN_COLLECTION
:
4614 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
4615 attr
->num_values
, NULL
);
4617 for (i
= 0; i
< attr
->num_values
; i
++)
4619 toattr
->values
[i
].collection
= ippNew();
4620 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
4621 NULL
, IPP_TAG_ZERO
, 0);
4626 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4627 attr
->name
, attr
->num_values
, NULL
);
4629 for (i
= 0; i
< attr
->num_values
; i
++)
4631 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
4633 if (toattr
->values
[i
].unknown
.length
> 0)
4635 if ((toattr
->values
[i
].unknown
.data
=
4636 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
4637 toattr
->values
[i
].unknown
.length
= 0;
4639 memcpy(toattr
->values
[i
].unknown
.data
,
4640 attr
->values
[i
].unknown
.data
,
4641 toattr
->values
[i
].unknown
.length
);
4644 break; /* anti-compiler-warning-code */
4652 * 'copy_attrs()' - Copy attributes from one request to another.
4656 copy_attrs(ipp_t
*to
, /* I - Destination request */
4657 ipp_t
*from
, /* I - Source request */
4658 cups_array_t
*ra
, /* I - Requested attributes */
4659 ipp_tag_t group
, /* I - Group to copy */
4660 int quickcopy
) /* I - Do a quick copy? */
4662 ipp_attribute_t
*fromattr
; /* Source attribute */
4665 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4666 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4667 to
, from
, ra
, group
, quickcopy
);
4672 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4675 * Filter attributes as needed...
4678 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4679 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4682 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4683 copy_attribute(to
, fromattr
, quickcopy
);
4689 * 'copy_banner()' - Copy a banner file to the requests directory for the
4693 static int /* O - Size of banner file in kbytes */
4694 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4695 cupsd_job_t
*job
, /* I - Job information */
4696 const char *name
) /* I - Name of banner */
4698 int i
; /* Looping var */
4699 int kbytes
; /* Size of banner file in kbytes */
4700 char filename
[1024]; /* Job filename */
4701 cupsd_banner_t
*banner
; /* Pointer to banner */
4702 cups_file_t
*in
; /* Input file */
4703 cups_file_t
*out
; /* Output file */
4704 int ch
; /* Character from file */
4705 char attrname
[255], /* Name of attribute */
4706 *s
; /* Pointer into name */
4707 ipp_attribute_t
*attr
; /* Attribute */
4710 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4711 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4712 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
4713 name
? name
: "(null)");
4716 * Find the banner; return if not found or "none"...
4719 if (!name
|| !strcmp(name
, "none") ||
4720 (banner
= cupsdFindBanner(name
)) == NULL
)
4724 * Open the banner and job files...
4727 if (add_file(con
, job
, banner
->filetype
, 0))
4730 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4732 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4734 cupsdLogMessage(CUPSD_LOG_ERROR
,
4735 "Unable to create banner job file %s - %s",
4736 filename
, strerror(errno
));
4741 fchmod(cupsFileNumber(out
), 0640);
4742 fchown(cupsFileNumber(out
), RunUser
, Group
);
4745 * Try the localized banner file under the subdirectory...
4748 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4750 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4753 * Convert ll-cc to ll_CC...
4757 attrname
[3] = toupper(attrname
[3] & 255);
4758 attrname
[4] = toupper(attrname
[4] & 255);
4761 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4764 if (access(filename
, 0) && strlen(attrname
) > 2)
4767 * Wasn't able to find "ll_CC" locale file; try the non-national
4768 * localization banner directory.
4773 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4777 if (access(filename
, 0))
4780 * Use the non-localized banner file.
4783 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4786 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4790 cupsdLogMessage(CUPSD_LOG_ERROR
,
4791 "Unable to open banner template file %s - %s",
4792 filename
, strerror(errno
));
4798 * Parse the file to the end...
4801 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4805 * Get an attribute name...
4808 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4809 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4811 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4821 * Ignore { followed by stuff that is not an attribute name...
4824 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4829 * See if it is defined...
4832 if (attrname
[0] == '?')
4837 if (!strcmp(s
, "printer-name"))
4839 cupsFilePuts(out
, job
->dest
);
4842 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4845 * See if we have a leading question mark...
4848 if (attrname
[0] != '?')
4851 * Nope, write to file as-is; probably a PostScript procedure...
4854 cupsFilePrintf(out
, "{%s}", attrname
);
4861 * Output value(s)...
4864 for (i
= 0; i
< attr
->num_values
; i
++)
4867 cupsFilePutChar(out
, ',');
4869 switch (attr
->value_tag
)
4871 case IPP_TAG_INTEGER
:
4873 if (!strncmp(s
, "time-at-", 8))
4874 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
4876 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4879 case IPP_TAG_BOOLEAN
:
4880 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4883 case IPP_TAG_NOVALUE
:
4884 cupsFilePuts(out
, "novalue");
4887 case IPP_TAG_RANGE
:
4888 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4889 attr
->values
[i
].range
.upper
);
4892 case IPP_TAG_RESOLUTION
:
4893 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4894 attr
->values
[i
].resolution
.yres
,
4895 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4900 case IPP_TAG_STRING
:
4903 case IPP_TAG_KEYWORD
:
4904 case IPP_TAG_CHARSET
:
4905 case IPP_TAG_LANGUAGE
:
4906 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4909 * Need to quote strings for PS banners...
4914 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4916 if (*p
== '(' || *p
== ')' || *p
== '\\')
4918 cupsFilePutChar(out
, '\\');
4919 cupsFilePutChar(out
, *p
);
4921 else if (*p
< 32 || *p
> 126)
4922 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4924 cupsFilePutChar(out
, *p
);
4928 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4932 break; /* anti-compiler-warning-code */
4936 else if (ch
== '\\') /* Quoted char */
4938 ch
= cupsFileGetChar(in
);
4940 if (ch
!= '{') /* Only do special handling for \{ */
4941 cupsFilePutChar(out
, '\\');
4943 cupsFilePutChar(out
, ch
);
4946 cupsFilePutChar(out
, ch
);
4950 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4952 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4953 IPP_TAG_INTEGER
)) != NULL
)
4954 attr
->values
[0].integer
+= kbytes
;
4963 * 'copy_file()' - Copy a PPD file or interface script...
4966 static int /* O - 0 = success, -1 = error */
4967 copy_file(const char *from
, /* I - Source file */
4968 const char *to
) /* I - Destination file */
4970 cups_file_t
*src
, /* Source file */
4971 *dst
; /* Destination file */
4972 int bytes
; /* Bytes to read/write */
4973 char buffer
[2048]; /* Copy buffer */
4976 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4979 * Open the source and destination file for a copy...
4982 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4985 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4992 * Copy the source file to the destination...
4995 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4996 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
5004 * Close both files and return...
5009 return (cupsFileClose(dst
));
5014 * 'copy_model()' - Copy a PPD model file, substituting default values
5018 static int /* O - 0 = success, -1 = error */
5019 copy_model(cupsd_client_t
*con
, /* I - Client connection */
5020 const char *from
, /* I - Source file */
5021 const char *to
) /* I - Destination file */
5023 fd_set input
; /* select() input set */
5024 struct timeval timeout
; /* select() timeout */
5025 int maxfd
; /* Max file descriptor for select() */
5026 char tempfile
[1024]; /* Temporary PPD file */
5027 int tempfd
; /* Temporary PPD file descriptor */
5028 int temppid
; /* Process ID of cups-driverd */
5029 int temppipe
[2]; /* Temporary pipes */
5030 char *argv
[4], /* Command-line arguments */
5031 *envp
[MAX_ENV
]; /* Environment */
5032 cups_file_t
*src
, /* Source file */
5033 *dst
; /* Destination file */
5034 ppd_file_t
*ppd
; /* PPD file */
5035 int bytes
, /* Bytes from pipe */
5036 total
; /* Total bytes from pipe */
5037 char buffer
[2048]; /* Copy buffer */
5038 int i
; /* Looping var */
5039 char option
[PPD_MAX_NAME
], /* Option name */
5040 choice
[PPD_MAX_NAME
]; /* Choice name */
5041 int num_defaults
; /* Number of default options */
5042 cups_option_t
*defaults
; /* Default options */
5043 char cups_protocol
[PPD_MAX_LINE
];
5044 /* cupsProtocol attribute */
5047 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5048 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
5052 * Run cups-driverd to get the PPD file...
5055 argv
[0] = "cups-driverd";
5057 argv
[2] = (char *)from
;
5060 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5062 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
5063 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
5064 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
5068 cupsdOpenPipe(temppipe
);
5070 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5071 "copy_model: Running \"cups-driverd cat %s\"...", from
);
5073 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
5074 -1, -1, 0, DefaultProfile
, &temppid
))
5085 * Wait up to 30 seconds for the PPD file to be copied...
5090 if (temppipe
[0] > CGIPipes
[0])
5091 maxfd
= temppipe
[0] + 1;
5093 maxfd
= CGIPipes
[0] + 1;
5098 * See if we have data ready...
5102 FD_SET(temppipe
[0], &input
);
5103 FD_SET(CGIPipes
[0], &input
);
5105 timeout
.tv_sec
= 30;
5106 timeout
.tv_usec
= 0;
5108 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
5118 * We have timed out...
5124 if (FD_ISSET(temppipe
[0], &input
))
5127 * Read the PPD file from the pipe, and write it to the PPD file.
5130 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
5132 if (write(tempfd
, buffer
, bytes
) < bytes
)
5141 if (FD_ISSET(CGIPipes
[0], &input
))
5151 * No data from cups-deviced...
5154 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
5160 * Read the source file and see what page sizes are supported...
5163 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
5170 * Open the destination (if possible) and set the default options...
5175 cups_protocol
[0] = '\0';
5177 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
5180 * Read all of the default lines from the old PPD...
5183 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
5184 if (!strncmp(buffer
, "*Default", 8))
5187 * Add the default option...
5190 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5191 choice
, sizeof(choice
)))
5193 ppd_option_t
*ppdo
; /* PPD option */
5197 * Only add the default if the default hasn't already been
5198 * set and the choice exists in the new PPD...
5201 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
5202 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
5203 ppdFindChoice(ppdo
, choice
))
5204 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
5208 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
5209 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
5213 else if (ppdPageSize(ppd
, DefaultPaperSize
))
5216 * Add the default media sizes...
5219 num_defaults
= cupsAddOption("PageSize", DefaultPaperSize
,
5220 num_defaults
, &defaults
);
5221 num_defaults
= cupsAddOption("PageRegion", DefaultPaperSize
,
5222 num_defaults
, &defaults
);
5223 num_defaults
= cupsAddOption("PaperDimension", DefaultPaperSize
,
5224 num_defaults
, &defaults
);
5225 num_defaults
= cupsAddOption("ImageableArea", DefaultPaperSize
,
5226 num_defaults
, &defaults
);
5232 * Open the source file for a copy...
5235 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
5237 cupsFreeOptions(num_defaults
, defaults
);
5243 * Open the destination file for a copy...
5246 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
5248 cupsFreeOptions(num_defaults
, defaults
);
5255 * Copy the source file to the destination...
5258 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
5260 if (!strncmp(buffer
, "*Default", 8))
5263 * Check for an previous default option choice...
5266 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5267 choice
, sizeof(choice
)))
5269 const char *val
; /* Default option value */
5272 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
5275 * Substitute the previous choice...
5278 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
5283 cupsFilePrintf(dst
, "%s\n", buffer
);
5286 if (cups_protocol
[0])
5287 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
5289 cupsFreeOptions(num_defaults
, defaults
);
5292 * Close both files and return...
5299 return (cupsFileClose(dst
));
5304 * 'copy_job_attrs()' - Copy job attributes.
5308 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5309 cupsd_job_t
*job
, /* I - Job */
5310 cups_array_t
*ra
) /* I - Requested attributes array */
5312 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
5316 * Send the requested attributes for each job...
5319 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
5320 con
->servername
, con
->serverport
, "/jobs/%d",
5323 if (!ra
|| cupsArrayFind(ra
, "document-count"))
5324 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5325 "document-count", job
->num_files
);
5327 if (!ra
|| cupsArrayFind(ra
, "job-media-progress"))
5328 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5329 "job-media-progress", job
->progress
);
5331 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
5332 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5333 "job-more-info", NULL
, job_uri
);
5335 if (job
->state_value
> IPP_JOB_PROCESSING
&&
5336 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
5337 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
5338 job
->num_files
> 0);
5340 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
5341 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5342 "job-printer-up-time", time(NULL
));
5344 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
5345 add_job_state_reasons(con
, job
);
5347 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
5348 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5349 "job-uri", NULL
, job_uri
);
5351 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
5356 * 'copy_printer_attrs()' - Copy printer attributes.
5361 cupsd_client_t
*con
, /* I - Client connection */
5362 cupsd_printer_t
*printer
, /* I - Printer */
5363 cups_array_t
*ra
) /* I - Requested attributes array */
5365 char printer_uri
[HTTP_MAX_URI
];
5367 time_t curtime
; /* Current time */
5368 int i
; /* Looping var */
5369 ipp_attribute_t
*history
; /* History collection */
5373 * Copy the printer attributes to the response using requested-attributes
5374 * and document-format attributes that may be provided by the client.
5377 curtime
= time(NULL
);
5380 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
5381 printer
->recoverable
)
5382 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5383 "com.apple.print.recoverable-message", NULL
,
5384 printer
->recoverable
);
5385 #endif /* __APPLE__ */
5387 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
5388 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5389 "marker-change-time", printer
->marker_time
);
5391 if (printer
->num_printers
> 0 &&
5392 (!ra
|| cupsArrayFind(ra
, "member-uris")))
5394 ipp_attribute_t
*member_uris
; /* member-uris attribute */
5395 cupsd_printer_t
*p2
; /* Printer in class */
5396 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
5399 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
5400 IPP_TAG_URI
, "member-uris",
5401 printer
->num_printers
, NULL
,
5404 for (i
= 0; i
< printer
->num_printers
; i
++)
5406 p2
= printer
->printers
[i
];
5408 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
5409 IPP_TAG_URI
)) != NULL
)
5410 member_uris
->values
[i
].string
.text
=
5411 _cupsStrAlloc(p2_uri
->values
[0].string
.text
);
5414 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
5415 sizeof(printer_uri
), "ipp", NULL
, con
->servername
,
5417 (p2
->type
& CUPS_PRINTER_CLASS
) ?
5418 "/classes/%s" : "/printers/%s", p2
->name
);
5419 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
5425 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
5426 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
5427 "printer-alert", NULL
, printer
->alert
);
5429 if (printer
->alert_description
&&
5430 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
5431 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5432 "printer-alert-description", NULL
,
5433 printer
->alert_description
);
5435 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
5436 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
5437 ippTimeToDate(curtime
));
5440 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
5442 if (printer
->reg_name
)
5443 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5444 "printer-dns-sd-name", NULL
, printer
->reg_name
);
5446 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
5447 "printer-dns-sd-name", 0);
5449 #endif /* HAVE_DNSSD */
5451 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
5452 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5453 "printer-error-policy", NULL
, printer
->error_policy
);
5455 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
5456 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
5457 printer
->accepting
);
5459 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
5460 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
5463 if ((!ra
|| cupsArrayFind(ra
, "printer-more-info")) &&
5464 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
5466 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5467 "http", NULL
, con
->servername
, con
->serverport
,
5468 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5469 "/classes/%s" : "/printers/%s", printer
->name
);
5470 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5471 "printer-more-info", NULL
, printer_uri
);
5474 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
5475 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5476 "printer-op-policy", NULL
, printer
->op_policy
);
5478 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
5479 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
5482 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
5483 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5484 "printer-state-change-time", printer
->state_time
);
5486 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
5487 cupsArrayFind(ra
, "printer-state-history"))
5490 * Printer history is only sent if specifically requested, so that
5491 * older CUPS/IPP clients won't barf on the collection attributes.
5494 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
5495 "printer-state-history",
5496 printer
->num_history
, NULL
);
5498 for (i
= 0; i
< printer
->num_history
; i
++)
5499 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
5500 NULL
, IPP_TAG_ZERO
, 0);
5503 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
5504 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5505 "printer-state-message", NULL
, printer
->state_message
);
5507 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
5508 add_printer_state_reasons(con
, printer
);
5510 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5512 int type
; /* printer-type value */
5516 * Add the CUPS-specific printer-type attribute...
5519 type
= printer
->type
;
5521 if (printer
== DefaultPrinter
)
5522 type
|= CUPS_PRINTER_DEFAULT
;
5524 if (!printer
->accepting
)
5525 type
|= CUPS_PRINTER_REJECTING
;
5527 if (!printer
->shared
)
5528 type
|= CUPS_PRINTER_NOT_SHARED
;
5530 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
5534 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5535 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5536 "printer-up-time", curtime
);
5538 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
5539 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
5541 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5542 "ipp", NULL
, con
->servername
, con
->serverport
,
5543 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5544 "/classes/%s" : "/printers/%s", printer
->name
);
5545 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5546 "printer-uri-supported", NULL
, printer_uri
);
5547 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5551 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5552 add_queued_job_count(con
, printer
);
5554 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
5555 if (printer
->ppd_attrs
)
5556 copy_attrs(con
->response
, printer
->ppd_attrs
, ra
, IPP_TAG_ZERO
, 0);
5557 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
5562 * 'copy_subscription_attrs()' - Copy subscription attributes.
5566 copy_subscription_attrs(
5567 cupsd_client_t
*con
, /* I - Client connection */
5568 cupsd_subscription_t
*sub
, /* I - Subscription */
5569 cups_array_t
*ra
) /* I - Requested attributes array */
5571 ipp_attribute_t
*attr
; /* Current attribute */
5572 char printer_uri
[HTTP_MAX_URI
];
5574 int count
; /* Number of events */
5575 unsigned mask
; /* Current event mask */
5576 const char *name
; /* Current event name */
5580 * Copy the subscription attributes to the response using the
5581 * requested-attributes attribute that may be provided by the client.
5584 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
5586 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5589 * Simple event list...
5592 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5593 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5594 "notify-events", NULL
, name
);
5599 * Complex event list...
5602 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5603 if (sub
->mask
& mask
)
5606 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5607 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5608 "notify-events", count
, NULL
, NULL
);
5610 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5611 if (sub
->mask
& mask
)
5613 attr
->values
[count
].string
.text
=
5614 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5621 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5622 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5623 "notify-job-id", sub
->job
->id
);
5625 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
5626 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5627 "notify-lease-duration", sub
->lease
);
5629 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5631 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5632 "ipp", NULL
, con
->servername
, con
->serverport
,
5633 "/printers/%s", sub
->dest
->name
);
5634 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5635 "notify-printer-uri", NULL
, printer_uri
);
5638 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
5639 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5640 "notify-recipient-uri", NULL
, sub
->recipient
);
5641 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
5642 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5643 "notify-pull-method", NULL
, "ippget");
5645 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
5646 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5647 "notify-subscriber-user-name", NULL
, sub
->owner
);
5649 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5650 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5651 "notify-subscription-id", sub
->id
);
5653 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
5654 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5655 "notify-time-interval", sub
->interval
);
5657 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5658 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5659 sub
->user_data
, sub
->user_data_len
);
5664 * 'create_job()' - Print a file to a printer or class.
5668 create_job(cupsd_client_t
*con
, /* I - Client connection */
5669 ipp_attribute_t
*uri
) /* I - Printer URI */
5671 cupsd_printer_t
*printer
; /* Printer */
5672 cupsd_job_t
*job
; /* New job */
5675 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5676 con
->http
.fd
, uri
->values
[0].string
.text
);
5679 * Is the destination valid?
5682 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5688 send_ipp_status(con
, IPP_NOT_FOUND
,
5689 _("The printer or class was not found."));
5694 * Create the job object...
5697 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5700 job
->pending_timeout
= 1;
5703 * Save and log the job...
5706 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5707 job
->dest
, job
->username
);
5712 * 'create_requested_array()' - Create an array for the requested-attributes.
5715 static cups_array_t
* /* O - Array of attributes or NULL */
5716 create_requested_array(ipp_t
*request
) /* I - IPP request */
5718 int i
; /* Looping var */
5719 ipp_attribute_t
*requested
; /* requested-attributes attribute */
5720 cups_array_t
*ra
; /* Requested attributes array */
5721 char *value
; /* Current value */
5725 * Get the requested-attributes attribute, and return NULL if we don't
5729 if ((requested
= ippFindAttribute(request
, "requested-attributes",
5730 IPP_TAG_KEYWORD
)) == NULL
)
5734 * If the attribute contains a single "all" keyword, return NULL...
5737 if (requested
->num_values
== 1 &&
5738 !strcmp(requested
->values
[0].string
.text
, "all"))
5742 * Create an array using "strcmp" as the comparison function...
5745 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
5747 for (i
= 0; i
< requested
->num_values
; i
++)
5749 value
= requested
->values
[i
].string
.text
;
5751 if (!strcmp(value
, "job-template"))
5753 cupsArrayAdd(ra
, "copies");
5754 cupsArrayAdd(ra
, "copies-default");
5755 cupsArrayAdd(ra
, "copies-supported");
5756 cupsArrayAdd(ra
, "finishings");
5757 cupsArrayAdd(ra
, "finishings-default");
5758 cupsArrayAdd(ra
, "finishings-supported");
5759 cupsArrayAdd(ra
, "job-hold-until");
5760 cupsArrayAdd(ra
, "job-hold-until-default");
5761 cupsArrayAdd(ra
, "job-hold-until-supported");
5762 cupsArrayAdd(ra
, "job-priority");
5763 cupsArrayAdd(ra
, "job-priority-default");
5764 cupsArrayAdd(ra
, "job-priority-supported");
5765 cupsArrayAdd(ra
, "job-sheets");
5766 cupsArrayAdd(ra
, "job-sheets-default");
5767 cupsArrayAdd(ra
, "job-sheets-supported");
5768 cupsArrayAdd(ra
, "media");
5769 cupsArrayAdd(ra
, "media-default");
5770 cupsArrayAdd(ra
, "media-supported");
5771 cupsArrayAdd(ra
, "multiple-document-handling");
5772 cupsArrayAdd(ra
, "multiple-document-handling-default");
5773 cupsArrayAdd(ra
, "multiple-document-handling-supported");
5774 cupsArrayAdd(ra
, "number-up");
5775 cupsArrayAdd(ra
, "number-up-default");
5776 cupsArrayAdd(ra
, "number-up-supported");
5777 cupsArrayAdd(ra
, "orientation-requested");
5778 cupsArrayAdd(ra
, "orientation-requested-default");
5779 cupsArrayAdd(ra
, "orientation-requested-supported");
5780 cupsArrayAdd(ra
, "page-ranges");
5781 cupsArrayAdd(ra
, "page-ranges-supported");
5782 cupsArrayAdd(ra
, "printer-resolution");
5783 cupsArrayAdd(ra
, "printer-resolution-default");
5784 cupsArrayAdd(ra
, "printer-resolution-supported");
5785 cupsArrayAdd(ra
, "print-quality");
5786 cupsArrayAdd(ra
, "print-quality-default");
5787 cupsArrayAdd(ra
, "print-quality-supported");
5788 cupsArrayAdd(ra
, "sides");
5789 cupsArrayAdd(ra
, "sides-default");
5790 cupsArrayAdd(ra
, "sides-supported");
5792 else if (!strcmp(value
, "job-description"))
5794 cupsArrayAdd(ra
, "date-time-at-completed");
5795 cupsArrayAdd(ra
, "date-time-at-creation");
5796 cupsArrayAdd(ra
, "date-time-at-processing");
5797 cupsArrayAdd(ra
, "job-detailed-status-message");
5798 cupsArrayAdd(ra
, "job-document-access-errors");
5799 cupsArrayAdd(ra
, "job-id");
5800 cupsArrayAdd(ra
, "job-impressions");
5801 cupsArrayAdd(ra
, "job-impressions-completed");
5802 cupsArrayAdd(ra
, "job-k-octets");
5803 cupsArrayAdd(ra
, "job-k-octets-processed");
5804 cupsArrayAdd(ra
, "job-media-progress");
5805 cupsArrayAdd(ra
, "job-media-sheets");
5806 cupsArrayAdd(ra
, "job-media-sheets-completed");
5807 cupsArrayAdd(ra
, "job-message-from-operator");
5808 cupsArrayAdd(ra
, "job-more-info");
5809 cupsArrayAdd(ra
, "job-name");
5810 cupsArrayAdd(ra
, "job-originating-user-name");
5811 cupsArrayAdd(ra
, "job-printer-up-time");
5812 cupsArrayAdd(ra
, "job-printer-uri");
5813 cupsArrayAdd(ra
, "job-state");
5814 cupsArrayAdd(ra
, "job-state-message");
5815 cupsArrayAdd(ra
, "job-state-reasons");
5816 cupsArrayAdd(ra
, "job-uri");
5817 cupsArrayAdd(ra
, "number-of-documents");
5818 cupsArrayAdd(ra
, "number-of-intervening-jobs");
5819 cupsArrayAdd(ra
, "output-device-assigned");
5820 cupsArrayAdd(ra
, "time-at-completed");
5821 cupsArrayAdd(ra
, "time-at-creation");
5822 cupsArrayAdd(ra
, "time-at-processing");
5824 else if (!strcmp(value
, "printer-description"))
5826 cupsArrayAdd(ra
, "charset-configured");
5827 cupsArrayAdd(ra
, "charset-supported");
5828 cupsArrayAdd(ra
, "color-supported");
5829 cupsArrayAdd(ra
, "compression-supported");
5830 cupsArrayAdd(ra
, "document-format-default");
5831 cupsArrayAdd(ra
, "document-format-supported");
5832 cupsArrayAdd(ra
, "generated-natural-language-supported");
5833 cupsArrayAdd(ra
, "ipp-versions-supported");
5834 cupsArrayAdd(ra
, "job-impressions-supported");
5835 cupsArrayAdd(ra
, "job-k-octets-supported");
5836 cupsArrayAdd(ra
, "job-media-sheets-supported");
5837 cupsArrayAdd(ra
, "job-settable-attributes-supported");
5838 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
5839 cupsArrayAdd(ra
, "multiple-operation-time-out");
5840 cupsArrayAdd(ra
, "natural-language-configured");
5841 cupsArrayAdd(ra
, "notify-attributes-supported");
5842 cupsArrayAdd(ra
, "notify-lease-duration-default");
5843 cupsArrayAdd(ra
, "notify-lease-duration-supported");
5844 cupsArrayAdd(ra
, "notify-max-events-supported");
5845 cupsArrayAdd(ra
, "notify-events-default");
5846 cupsArrayAdd(ra
, "notify-events-supported");
5847 cupsArrayAdd(ra
, "notify-pull-method-supported");
5848 cupsArrayAdd(ra
, "notify-schemes-supported");
5849 cupsArrayAdd(ra
, "operations-supported");
5850 cupsArrayAdd(ra
, "pages-per-minute");
5851 cupsArrayAdd(ra
, "pages-per-minute-color");
5852 cupsArrayAdd(ra
, "pdl-override-supported");
5853 cupsArrayAdd(ra
, "printer-alert");
5854 cupsArrayAdd(ra
, "printer-alert-description");
5855 cupsArrayAdd(ra
, "printer-commands");
5856 cupsArrayAdd(ra
, "printer-current-time");
5857 cupsArrayAdd(ra
, "printer-driver-installer");
5858 cupsArrayAdd(ra
, "printer-dns-sd-name");
5859 cupsArrayAdd(ra
, "printer-info");
5860 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
5861 cupsArrayAdd(ra
, "printer-location");
5862 cupsArrayAdd(ra
, "printer-make-and-model");
5863 cupsArrayAdd(ra
, "printer-message-from-operator");
5864 cupsArrayAdd(ra
, "printer-more-info");
5865 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
5866 cupsArrayAdd(ra
, "printer-name");
5867 cupsArrayAdd(ra
, "printer-state");
5868 cupsArrayAdd(ra
, "printer-state-message");
5869 cupsArrayAdd(ra
, "printer-state-reasons");
5870 cupsArrayAdd(ra
, "printer-settable-attributes-supported");
5871 cupsArrayAdd(ra
, "printer-type");
5872 cupsArrayAdd(ra
, "printer-up-time");
5873 cupsArrayAdd(ra
, "printer-uri-supported");
5874 cupsArrayAdd(ra
, "queued-job-count");
5875 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
5876 cupsArrayAdd(ra
, "uri-authentication-supported");
5877 cupsArrayAdd(ra
, "uri-security-supported");
5879 else if (!strcmp(value
, "printer-defaults"))
5881 char *name
; /* Option name */
5884 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5886 name
= (char *)cupsArrayNext(CommonDefaults
))
5887 cupsArrayAdd(ra
, name
);
5889 else if (!strcmp(value
, "subscription-template"))
5891 cupsArrayAdd(ra
, "notify-attributes");
5892 cupsArrayAdd(ra
, "notify-charset");
5893 cupsArrayAdd(ra
, "notify-events");
5894 cupsArrayAdd(ra
, "notify-lease-duration");
5895 cupsArrayAdd(ra
, "notify-natural-language");
5896 cupsArrayAdd(ra
, "notify-pull-method");
5897 cupsArrayAdd(ra
, "notify-recipient-uri");
5898 cupsArrayAdd(ra
, "notify-time-interval");
5899 cupsArrayAdd(ra
, "notify-user-data");
5902 cupsArrayAdd(ra
, value
);
5910 * 'create_subscription()' - Create a notification subscription.
5914 create_subscription(
5915 cupsd_client_t
*con
, /* I - Client connection */
5916 ipp_attribute_t
*uri
) /* I - Printer URI */
5918 http_status_t status
; /* Policy status */
5919 int i
; /* Looping var */
5920 ipp_attribute_t
*attr
; /* Current attribute */
5921 cups_ptype_t dtype
; /* Destination type (printer/class) */
5922 char scheme
[HTTP_MAX_URI
],
5923 /* Scheme portion of URI */
5924 userpass
[HTTP_MAX_URI
],
5925 /* Username portion of URI */
5927 /* Host portion of URI */
5928 resource
[HTTP_MAX_URI
];
5929 /* Resource portion of URI */
5930 int port
; /* Port portion of URI */
5931 cupsd_printer_t
*printer
; /* Printer/class */
5932 cupsd_job_t
*job
; /* Job */
5933 int jobid
; /* Job ID */
5934 cupsd_subscription_t
*sub
; /* Subscription object */
5935 const char *username
, /* requesting-user-name or
5936 authenticated username */
5937 *recipient
, /* notify-recipient-uri */
5938 *pullmethod
; /* notify-pull-method */
5939 ipp_attribute_t
*user_data
; /* notify-user-data */
5940 int interval
, /* notify-time-interval */
5941 lease
; /* notify-lease-duration */
5942 unsigned mask
; /* notify-events */
5943 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5944 *notify_lease
; /* notify-lease-duration(-default) */
5948 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5950 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5951 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5952 attr
->value_tag
, attr
->name
);
5954 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5959 * Is the destination valid?
5962 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5963 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5964 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5966 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5967 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5968 sizeof(host
), &port
, resource
, sizeof(resource
));
5970 if (!strcmp(resource
, "/"))
5972 dtype
= (cups_ptype_t
)0;
5975 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5977 dtype
= (cups_ptype_t
)0;
5980 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5982 dtype
= CUPS_PRINTER_CLASS
;
5985 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5991 send_ipp_status(con
, IPP_NOT_FOUND
,
5992 _("The printer or class was not found."));
6002 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6005 send_http_error(con
, status
, printer
);
6009 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6011 send_http_error(con
, status
, NULL
);
6016 * Get the user that is requesting the subscription...
6019 username
= get_username(con
);
6022 * Find the first subscription group attribute; return if we have
6026 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
6027 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
6032 send_ipp_status(con
, IPP_BAD_REQUEST
,
6033 _("No subscription attributes in request!"));
6038 * Process the subscription attributes in the request...
6041 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
6049 lease
= DefaultLeaseDuration
;
6051 mask
= CUPSD_EVENT_NONE
;
6055 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
6057 notify_lease
= ippFindAttribute(printer
->attrs
,
6058 "notify-lease-duration-default",
6062 lease
= notify_lease
->values
[0].integer
;
6066 notify_events
= NULL
;
6067 notify_lease
= NULL
;
6070 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
6072 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
6073 attr
->value_tag
== IPP_TAG_URI
)
6076 * Validate the recipient scheme against the ServerBin/notifier
6080 char notifier
[1024]; /* Notifier filename */
6083 recipient
= attr
->values
[0].string
.text
;
6085 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
6086 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
6087 host
, sizeof(host
), &port
,
6088 resource
, sizeof(resource
)) < HTTP_URI_OK
)
6090 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6091 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
6092 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6093 "notify-status-code", IPP_URI_SCHEME
);
6097 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
6099 if (access(notifier
, X_OK
))
6101 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6102 _("notify-recipient-uri URI \"%s\" uses unknown "
6103 "scheme!"), recipient
);
6104 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6105 "notify-status-code", IPP_URI_SCHEME
);
6109 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
6111 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6112 _("notify-recipient-uri URI \"%s\" is already used!"),
6114 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6115 "notify-status-code", IPP_ATTRIBUTES
);
6119 else if (!strcmp(attr
->name
, "notify-pull-method") &&
6120 attr
->value_tag
== IPP_TAG_KEYWORD
)
6122 pullmethod
= attr
->values
[0].string
.text
;
6124 if (strcmp(pullmethod
, "ippget"))
6126 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6127 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
6128 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6129 "notify-status-code", IPP_ATTRIBUTES
);
6133 else if (!strcmp(attr
->name
, "notify-charset") &&
6134 attr
->value_tag
== IPP_TAG_CHARSET
&&
6135 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
6136 strcmp(attr
->values
[0].string
.text
, "utf-8"))
6138 send_ipp_status(con
, IPP_CHARSET
,
6139 _("Character set \"%s\" not supported!"),
6140 attr
->values
[0].string
.text
);
6143 else if (!strcmp(attr
->name
, "notify-natural-language") &&
6144 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
6145 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
6147 send_ipp_status(con
, IPP_CHARSET
,
6148 _("Language \"%s\" not supported!"),
6149 attr
->values
[0].string
.text
);
6152 else if (!strcmp(attr
->name
, "notify-user-data") &&
6153 attr
->value_tag
== IPP_TAG_STRING
)
6155 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
6157 send_ipp_status(con
, IPP_REQUEST_VALUE
,
6158 _("The notify-user-data value is too large "
6159 "(%d > 63 octets)!"),
6160 attr
->values
[0].unknown
.length
);
6166 else if (!strcmp(attr
->name
, "notify-events") &&
6167 attr
->value_tag
== IPP_TAG_KEYWORD
)
6168 notify_events
= attr
;
6169 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
6170 attr
->value_tag
== IPP_TAG_INTEGER
)
6171 lease
= attr
->values
[0].integer
;
6172 else if (!strcmp(attr
->name
, "notify-time-interval") &&
6173 attr
->value_tag
== IPP_TAG_INTEGER
)
6174 interval
= attr
->values
[0].integer
;
6175 else if (!strcmp(attr
->name
, "notify-job-id") &&
6176 attr
->value_tag
== IPP_TAG_INTEGER
)
6177 jobid
= attr
->values
[0].integer
;
6184 for (i
= 0; i
< notify_events
->num_values
; i
++)
6185 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
6189 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
6191 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
6192 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
6193 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
6195 if (!recipient
&& !pullmethod
)
6198 if (mask
== CUPSD_EVENT_NONE
)
6201 mask
= CUPSD_EVENT_JOB_COMPLETED
;
6203 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
6206 send_ipp_status(con
, IPP_BAD_REQUEST
,
6207 _("notify-events not specified!"));
6212 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
6214 cupsdLogMessage(CUPSD_LOG_INFO
,
6215 "create_subscription: Limiting notify-lease-duration to "
6218 lease
= MaxLeaseDuration
;
6223 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6225 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
6232 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
6235 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
6238 cupsdLogMessage(CUPSD_LOG_DEBUG
,
6239 "Added subscription %d for printer \"%s\"",
6240 sub
->id
, printer
->name
);
6242 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
6245 sub
->interval
= interval
;
6247 sub
->expire
= lease
? time(NULL
) + lease
: 0;
6249 cupsdSetString(&sub
->owner
, username
);
6253 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
6254 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
6255 sub
->user_data_len
);
6258 ippAddSeparator(con
->response
);
6259 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
6260 "notify-subscription-id", sub
->id
);
6262 con
->response
->request
.status
.status_code
= IPP_OK
;
6268 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
6273 * 'delete_printer()' - Remove a printer or class from the system.
6277 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
6278 ipp_attribute_t
*uri
) /* I - URI of printer or class */
6280 http_status_t status
; /* Policy status */
6281 cups_ptype_t dtype
; /* Destination type (printer/class) */
6282 cupsd_printer_t
*printer
; /* Printer/class */
6283 char filename
[1024]; /* Script/PPD filename */
6286 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
6287 con
->http
.fd
, uri
->values
[0].string
.text
);
6290 * Do we have a valid URI?
6293 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6299 send_ipp_status(con
, IPP_NOT_FOUND
,
6300 _("The printer or class was not found."));
6308 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6310 send_http_error(con
, status
, NULL
);
6315 * Remove old jobs...
6318 cupsdCancelJobs(printer
->name
, NULL
, 1);
6321 * Remove old subscriptions and send a "deleted printer" event...
6324 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
6325 "%s \"%s\" deleted by \"%s\".",
6326 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
6327 printer
->name
, get_username(con
));
6329 cupsdExpireSubscriptions(printer
, NULL
);
6332 * Remove any old PPD or script files...
6335 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
6339 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6343 snprintf(filename
, sizeof(filename
), "%s/%s.ipp", ServerRoot
,
6349 * Unregister color profiles...
6352 apple_unregister_profiles(printer
);
6353 #endif /* __APPLE__ */
6355 if (dtype
& CUPS_PRINTER_CLASS
)
6357 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
6358 printer
->name
, get_username(con
));
6360 cupsdDeletePrinter(printer
, 0);
6361 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6365 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
6366 printer
->name
, get_username(con
));
6368 cupsdDeletePrinter(printer
, 0);
6369 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
6372 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
6375 * Return with no errors...
6378 con
->response
->request
.status
.status_code
= IPP_OK
;
6383 * 'get_default()' - Get the default destination.
6387 get_default(cupsd_client_t
*con
) /* I - Client connection */
6389 http_status_t status
; /* Policy status */
6390 cups_array_t
*ra
; /* Requested attributes array */
6393 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
6399 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6401 send_http_error(con
, status
, NULL
);
6407 ra
= create_requested_array(con
->request
);
6409 copy_printer_attrs(con
, DefaultPrinter
, ra
);
6411 cupsArrayDelete(ra
);
6413 con
->response
->request
.status
.status_code
= IPP_OK
;
6416 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
6421 * 'get_devices()' - Get the list of available devices on the local system.
6425 get_devices(cupsd_client_t
*con
) /* I - Client connection */
6427 http_status_t status
; /* Policy status */
6428 ipp_attribute_t
*limit
, /* limit attribute */
6429 *timeout
, /* timeout attribute */
6430 *requested
, /* requested-attributes attribute */
6431 *exclude
; /* exclude-schemes attribute */
6432 char command
[1024], /* cups-deviced command */
6433 options
[1024], /* Options to pass to command */
6435 /* String for requested attributes */
6437 /* String for excluded attributes */
6440 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
6446 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6448 send_http_error(con
, status
, NULL
);
6453 * Run cups-deviced command with the given options...
6456 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6457 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
6458 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6460 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
6463 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6465 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6468 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6470 exclude_str
[0] = '\0';
6472 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
6473 snprintf(options
, sizeof(options
),
6474 "%d+%d+%d+%d+%s%s%s",
6475 con
->request
->request
.op
.request_id
,
6476 limit
? limit
->values
[0].integer
: 0,
6477 timeout
? timeout
->values
[0].integer
: 10,
6480 exclude_str
[0] ? "%20" : "", exclude_str
);
6482 if (cupsdSendCommand(con
, command
, options
, 1))
6485 * Command started successfully, don't send an IPP response here...
6488 ippDelete(con
->response
);
6489 con
->response
= NULL
;
6494 * Command failed, return "internal error" so the user knows something
6498 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6499 _("cups-deviced failed to execute."));
6505 * 'get_document()' - Get a copy of a job file.
6509 get_document(cupsd_client_t
*con
, /* I - Client connection */
6510 ipp_attribute_t
*uri
) /* I - Job URI */
6512 http_status_t status
; /* Policy status */
6513 ipp_attribute_t
*attr
; /* Current attribute */
6514 int jobid
; /* Job ID */
6515 int docnum
; /* Document number */
6516 cupsd_job_t
*job
; /* Current job */
6517 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
6518 username
[HTTP_MAX_URI
], /* Username portion of URI */
6519 host
[HTTP_MAX_URI
], /* Host portion of URI */
6520 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6521 int port
; /* Port portion of URI */
6522 char filename
[1024], /* Filename for document */
6523 format
[1024]; /* Format for document */
6526 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6527 con
->http
.fd
, uri
->values
[0].string
.text
);
6530 * See if we have a job URI or a printer URI...
6533 if (!strcmp(uri
->name
, "printer-uri"))
6536 * Got a printer URI; see if we also have a job-id attribute...
6539 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6540 IPP_TAG_INTEGER
)) == NULL
)
6542 send_ipp_status(con
, IPP_BAD_REQUEST
,
6543 _("Got a printer-uri attribute but no job-id!"));
6547 jobid
= attr
->values
[0].integer
;
6552 * Got a job URI; parse it to get the job ID...
6555 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6556 sizeof(scheme
), username
, sizeof(username
), host
,
6557 sizeof(host
), &port
, resource
, sizeof(resource
));
6559 if (strncmp(resource
, "/jobs/", 6))
6565 send_ipp_status(con
, IPP_BAD_REQUEST
,
6566 _("Bad job-uri attribute \"%s\"!"),
6567 uri
->values
[0].string
.text
);
6571 jobid
= atoi(resource
+ 6);
6575 * See if the job exists...
6578 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6581 * Nope - return a "not found" error...
6584 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6592 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6594 send_http_error(con
, status
, NULL
);
6599 * Get the document number...
6602 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6603 IPP_TAG_INTEGER
)) == NULL
)
6605 send_ipp_status(con
, IPP_BAD_REQUEST
,
6606 _("Missing document-number attribute!"));
6610 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6611 attr
->num_values
> 1)
6613 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
6618 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6620 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6622 cupsdLogMessage(CUPSD_LOG_ERROR
,
6623 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6625 send_ipp_status(con
, IPP_NOT_FOUND
,
6626 _("Unable to open document %d in job %d!"), docnum
, jobid
);
6630 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6634 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6635 job
->filetypes
[docnum
- 1]->type
);
6637 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6639 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6641 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6642 IPP_TAG_NAME
)) != NULL
)
6643 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6644 NULL
, attr
->values
[0].string
.text
);
6649 * 'get_job_attrs()' - Get job attributes.
6653 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6654 ipp_attribute_t
*uri
) /* I - Job URI */
6656 http_status_t status
; /* Policy status */
6657 ipp_attribute_t
*attr
; /* Current attribute */
6658 int jobid
; /* Job ID */
6659 cupsd_job_t
*job
; /* Current job */
6660 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
6661 username
[HTTP_MAX_URI
], /* Username portion of URI */
6662 host
[HTTP_MAX_URI
], /* Host portion of URI */
6663 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6664 int port
; /* Port portion of URI */
6665 cups_array_t
*ra
; /* Requested attributes array */
6668 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6669 con
->http
.fd
, uri
->values
[0].string
.text
);
6672 * See if we have a job URI or a printer URI...
6675 if (!strcmp(uri
->name
, "printer-uri"))
6678 * Got a printer URI; see if we also have a job-id attribute...
6681 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6682 IPP_TAG_INTEGER
)) == NULL
)
6684 send_ipp_status(con
, IPP_BAD_REQUEST
,
6685 _("Got a printer-uri attribute but no job-id!"));
6689 jobid
= attr
->values
[0].integer
;
6694 * Got a job URI; parse it to get the job ID...
6697 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6698 sizeof(scheme
), username
, sizeof(username
), host
,
6699 sizeof(host
), &port
, resource
, sizeof(resource
));
6701 if (strncmp(resource
, "/jobs/", 6))
6707 send_ipp_status(con
, IPP_BAD_REQUEST
,
6708 _("Bad job-uri attribute \"%s\"!"),
6709 uri
->values
[0].string
.text
);
6713 jobid
= atoi(resource
+ 6);
6717 * See if the job exists...
6720 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6723 * Nope - return a "not found" error...
6726 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6734 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6736 send_http_error(con
, status
, NULL
);
6741 * Copy attributes...
6746 ra
= create_requested_array(con
->request
);
6747 copy_job_attrs(con
, job
, ra
);
6748 cupsArrayDelete(ra
);
6750 con
->response
->request
.status
.status_code
= IPP_OK
;
6755 * 'get_jobs()' - Get a list of jobs for the specified printer.
6759 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6760 ipp_attribute_t
*uri
) /* I - Printer URI */
6762 http_status_t status
; /* Policy status */
6763 ipp_attribute_t
*attr
; /* Current attribute */
6764 const char *dest
; /* Destination */
6765 cups_ptype_t dtype
; /* Destination type (printer/class) */
6766 cups_ptype_t dmask
; /* Destination type mask */
6767 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6768 username
[HTTP_MAX_URI
], /* Username portion of URI */
6769 host
[HTTP_MAX_URI
], /* Host portion of URI */
6770 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6771 int port
; /* Port portion of URI */
6772 int completed
; /* Completed jobs? */
6773 int first_job_id
; /* First job ID */
6774 int limit
; /* Maximum number of jobs to return */
6775 int count
; /* Number of jobs that match */
6776 cupsd_job_t
*job
; /* Current job pointer */
6777 cupsd_printer_t
*printer
; /* Printer */
6778 cups_array_t
*list
; /* Which job list... */
6779 cups_array_t
*ra
; /* Requested attributes array */
6782 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
6783 uri
->values
[0].string
.text
);
6786 * Is the destination valid?
6789 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6790 sizeof(scheme
), username
, sizeof(username
), host
,
6791 sizeof(host
), &port
, resource
, sizeof(resource
));
6793 if (!strcmp(resource
, "/") ||
6794 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
6797 dtype
= (cups_ptype_t
)0;
6798 dmask
= (cups_ptype_t
)0;
6801 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6804 dtype
= (cups_ptype_t
)0;
6805 dmask
= CUPS_PRINTER_CLASS
;
6808 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6811 dtype
= CUPS_PRINTER_CLASS
;
6812 dmask
= CUPS_PRINTER_CLASS
;
6815 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6822 send_ipp_status(con
, IPP_NOT_FOUND
,
6823 _("The printer or class was not found."));
6828 dtype
&= CUPS_PRINTER_CLASS
;
6829 dmask
= CUPS_PRINTER_CLASS
;
6838 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6841 send_http_error(con
, status
, printer
);
6845 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6847 send_http_error(con
, status
, NULL
);
6852 * See if the "which-jobs" attribute have been specified...
6855 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6856 IPP_TAG_KEYWORD
)) != NULL
&&
6857 !strcmp(attr
->values
[0].string
.text
, "completed"))
6862 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
6867 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "printing"))
6870 list
= PrintingJobs
;
6879 * See if they want to limit the number of jobs reported...
6882 if ((attr
= ippFindAttribute(con
->request
, "limit",
6883 IPP_TAG_INTEGER
)) != NULL
)
6884 limit
= attr
->values
[0].integer
;
6888 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
6889 IPP_TAG_INTEGER
)) != NULL
)
6890 first_job_id
= attr
->values
[0].integer
;
6895 * See if we only want to see jobs for a specific user...
6898 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
6899 IPP_TAG_BOOLEAN
)) != NULL
&&
6900 attr
->values
[0].boolean
)
6901 strlcpy(username
, get_username(con
), sizeof(username
));
6905 ra
= create_requested_array(con
->request
);
6908 * OK, build a list of jobs for this printer...
6911 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6912 (limit
<= 0 || count
< limit
) && job
;
6913 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6916 * Filter out jobs that don't match...
6919 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
6921 if (!job
->dest
|| !job
->username
)
6924 if (!job
->dest
|| !job
->username
)
6927 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6928 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6930 if ((job
->dtype
& dmask
) != dtype
&&
6931 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6933 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
6936 if (job
->id
< first_job_id
)
6944 if (username
[0] && strcasecmp(username
, job
->username
))
6948 ippAddSeparator(con
->response
);
6952 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
6954 copy_job_attrs(con
, job
, ra
);
6957 cupsArrayDelete(ra
);
6959 con
->response
->request
.status
.status_code
= IPP_OK
;
6964 * 'get_notifications()' - Get events for a subscription.
6968 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6970 int i
, j
; /* Looping vars */
6971 http_status_t status
; /* Policy status */
6972 cupsd_subscription_t
*sub
; /* Subscription */
6973 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6974 *sequences
; /* notify-sequence-numbers */
6975 int min_seq
; /* Minimum sequence number */
6976 int interval
; /* Poll interval */
6979 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6983 * Get subscription attributes...
6986 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6988 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6993 send_ipp_status(con
, IPP_BAD_REQUEST
,
6994 _("Missing notify-subscription-ids attribute!"));
6999 * Are the subscription IDs valid?
7002 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
7004 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
7007 * Bad subscription ID...
7010 send_ipp_status(con
, IPP_NOT_FOUND
,
7011 _("notify-subscription-id %d no good!"),
7012 ids
->values
[i
].integer
);
7020 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7022 con
, sub
->owner
)) != HTTP_OK
)
7024 send_http_error(con
, status
, sub
->dest
);
7029 * Check the subscription type and update the interval accordingly.
7032 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
7035 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
7037 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
7043 * Tell the client to poll again in N seconds...
7047 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
7048 "notify-get-interval", interval
);
7050 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
7051 "printer-up-time", time(NULL
));
7054 * Copy the subscription event attributes to the response.
7057 con
->response
->request
.status
.status_code
=
7058 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
7060 for (i
= 0; i
< ids
->num_values
; i
++)
7063 * Get the subscription and sequence number...
7066 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
7068 if (sequences
&& i
< sequences
->num_values
)
7069 min_seq
= sequences
->values
[i
].integer
;
7074 * If we don't have any new events, nothing to do here...
7077 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
7081 * Otherwise copy all of the new events...
7084 if (sub
->first_event_id
> min_seq
)
7087 j
= min_seq
- sub
->first_event_id
;
7089 for (; j
< sub
->num_events
; j
++)
7091 ippAddSeparator(con
->response
);
7093 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
7094 IPP_TAG_EVENT_NOTIFICATION
, 0);
7101 * 'get_ppd()' - Get a named PPD from the local system.
7105 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
7106 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
7108 http_status_t status
; /* Policy status */
7109 cupsd_printer_t
*dest
; /* Destination */
7110 cups_ptype_t dtype
; /* Destination type */
7113 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
7114 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
7116 if (!strcmp(uri
->name
, "ppd-name"))
7119 * Return a PPD file from cups-driverd...
7122 char command
[1024], /* cups-driverd command */
7123 options
[1024], /* Options to pass to command */
7124 ppd_name
[1024]; /* ppd-name */
7131 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7133 send_http_error(con
, status
, NULL
);
7138 * Run cups-driverd command with the given options...
7141 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7142 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
7143 snprintf(options
, sizeof(options
), "get+%d+%s",
7144 con
->request
->request
.op
.request_id
, ppd_name
);
7146 if (cupsdSendCommand(con
, command
, options
, 0))
7149 * Command started successfully, don't send an IPP response here...
7152 ippDelete(con
->response
);
7153 con
->response
= NULL
;
7158 * Command failed, return "internal error" so the user knows something
7162 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7163 _("cups-driverd failed to execute."));
7166 else if (!strcmp(uri
->name
, "printer-uri") &&
7167 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
7169 int i
; /* Looping var */
7170 char filename
[1024]; /* PPD filename */
7177 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7179 send_http_error(con
, status
, dest
);
7184 * See if we need the PPD for a class or remote printer...
7187 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7190 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
7192 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7193 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7194 "printer-uri", NULL
, dest
->uri
);
7197 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
7199 for (i
= 0; i
< dest
->num_printers
; i
++)
7200 if (!(dest
->printers
[i
]->type
&
7201 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
7203 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7204 dest
->printers
[i
]->name
);
7206 if (!access(filename
, 0))
7210 if (i
< dest
->num_printers
)
7211 dest
= dest
->printers
[i
];
7214 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7215 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7216 "printer-uri", NULL
, dest
->printers
[0]->uri
);
7222 * Found the printer with the PPD file, now see if there is one...
7225 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
7227 send_ipp_status(con
, IPP_NOT_FOUND
,
7228 _("The PPD file \"%s\" could not be opened: %s"),
7229 uri
->values
[0].string
.text
, strerror(errno
));
7233 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
7237 con
->response
->request
.status
.status_code
= IPP_OK
;
7240 send_ipp_status(con
, IPP_NOT_FOUND
,
7241 _("The PPD file \"%s\" could not be found."),
7242 uri
->values
[0].string
.text
);
7247 * 'get_ppds()' - Get the list of PPD files on the local system.
7251 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
7253 http_status_t status
; /* Policy status */
7254 ipp_attribute_t
*limit
, /* Limit attribute */
7255 *device
, /* ppd-device-id attribute */
7256 *language
, /* ppd-natural-language attribute */
7257 *make
, /* ppd-make attribute */
7258 *model
, /* ppd-make-and-model attribute */
7259 *model_number
, /* ppd-model-number attribute */
7260 *product
, /* ppd-product attribute */
7261 *psversion
, /* ppd-psverion attribute */
7262 *type
, /* ppd-type attribute */
7263 *requested
; /* requested-attributes attribute */
7264 char command
[1024], /* cups-driverd command */
7265 options
[1024], /* Options to pass to command */
7266 device_str
[256],/* Escaped ppd-device-id string */
7268 /* Escaped ppd-natural-language */
7269 make_str
[256], /* Escaped ppd-make string */
7270 model_str
[256], /* Escaped ppd-make-and-model string */
7271 model_number_str
[256],
7272 /* ppd-model-number string */
7274 /* Escaped ppd-product string */
7276 /* Escaped ppd-psversion string */
7277 type_str
[256], /* Escaped ppd-type string */
7279 /* String for requested attributes */
7282 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
7288 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7290 send_http_error(con
, status
, NULL
);
7295 * Run cups-driverd command with the given options...
7298 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
7299 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
7300 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
7302 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
7303 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
7305 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
7307 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
7308 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
7309 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
7310 requested
= ippFindAttribute(con
->request
, "requested-attributes",
7314 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
7316 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
7319 url_encode_attr(device
, device_str
, sizeof(device_str
));
7321 device_str
[0] = '\0';
7324 url_encode_attr(language
, language_str
, sizeof(language_str
));
7326 language_str
[0] = '\0';
7329 url_encode_attr(make
, make_str
, sizeof(make_str
));
7334 url_encode_attr(model
, model_str
, sizeof(model_str
));
7336 model_str
[0] = '\0';
7339 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
7340 model_number
->values
[0].integer
);
7342 model_number_str
[0] = '\0';
7345 url_encode_attr(product
, product_str
, sizeof(product_str
));
7347 product_str
[0] = '\0';
7350 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
7352 psversion_str
[0] = '\0';
7355 url_encode_attr(type
, type_str
, sizeof(type_str
));
7359 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7360 snprintf(options
, sizeof(options
),
7361 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7362 con
->request
->request
.op
.request_id
,
7363 limit
? limit
->values
[0].integer
: 0,
7365 device
? "%20" : "", device_str
,
7366 language
? "%20" : "", language_str
,
7367 make
? "%20" : "", make_str
,
7368 model
? "%20" : "", model_str
,
7369 model_number
? "%20" : "", model_number_str
,
7370 product
? "%20" : "", product_str
,
7371 psversion
? "%20" : "", psversion_str
,
7372 type
? "%20" : "", type_str
);
7374 if (cupsdSendCommand(con
, command
, options
, 0))
7377 * Command started successfully, don't send an IPP response here...
7380 ippDelete(con
->response
);
7381 con
->response
= NULL
;
7386 * Command failed, return "internal error" so the user knows something
7390 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7391 _("cups-driverd failed to execute."));
7397 * 'get_printer_attrs()' - Get printer attributes.
7401 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
7402 ipp_attribute_t
*uri
) /* I - Printer URI */
7404 http_status_t status
; /* Policy status */
7405 cups_ptype_t dtype
; /* Destination type (printer/class) */
7406 cupsd_printer_t
*printer
; /* Printer/class */
7407 cups_array_t
*ra
; /* Requested attributes array */
7410 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
7411 con
->http
.fd
, uri
->values
[0].string
.text
);
7414 * Is the destination valid?
7417 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7423 send_ipp_status(con
, IPP_NOT_FOUND
,
7424 _("The printer or class was not found."));
7432 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7434 send_http_error(con
, status
, printer
);
7439 * Send the attributes...
7442 ra
= create_requested_array(con
->request
);
7444 copy_printer_attrs(con
, printer
, ra
);
7446 cupsArrayDelete(ra
);
7448 con
->response
->request
.status
.status_code
= IPP_OK
;
7453 * 'get_printers()' - Get a list of printers or classes.
7457 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7458 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7460 http_status_t status
; /* Policy status */
7461 ipp_attribute_t
*attr
; /* Current attribute */
7462 int limit
; /* Max number of printers to return */
7463 int count
; /* Number of printers that match */
7464 cupsd_printer_t
*printer
; /* Current printer pointer */
7465 int printer_type
, /* printer-type attribute */
7466 printer_mask
; /* printer-type-mask attribute */
7467 char *location
; /* Location string */
7468 const char *username
; /* Current user */
7469 char *first_printer_name
; /* first-printer-name attribute */
7470 cups_array_t
*ra
; /* Requested attributes array */
7473 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7474 con
->http
.fd
, type
);
7480 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7482 send_http_error(con
, status
, NULL
);
7487 * Check for printers...
7490 if (!Printers
|| !cupsArrayCount(Printers
))
7492 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7497 * See if they want to limit the number of printers reported...
7500 if ((attr
= ippFindAttribute(con
->request
, "limit",
7501 IPP_TAG_INTEGER
)) != NULL
)
7502 limit
= attr
->values
[0].integer
;
7506 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7507 IPP_TAG_NAME
)) != NULL
)
7508 first_printer_name
= attr
->values
[0].string
.text
;
7510 first_printer_name
= NULL
;
7513 * Support filtering...
7516 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7517 IPP_TAG_ENUM
)) != NULL
)
7518 printer_type
= attr
->values
[0].integer
;
7522 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7523 IPP_TAG_ENUM
)) != NULL
)
7524 printer_mask
= attr
->values
[0].integer
;
7528 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7529 IPP_TAG_TEXT
)) != NULL
)
7530 location
= attr
->values
[0].string
.text
;
7534 if (con
->username
[0])
7535 username
= con
->username
;
7536 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7537 IPP_TAG_NAME
)) != NULL
)
7538 username
= attr
->values
[0].string
.text
;
7542 ra
= create_requested_array(con
->request
);
7545 * OK, build a list of printers for this printer...
7548 if (first_printer_name
)
7550 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7551 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7554 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7557 count
< limit
&& printer
;
7558 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7560 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7561 (printer
->type
& printer_mask
) == printer_type
&&
7563 (printer
->location
&& !strcasecmp(printer
->location
, location
))))
7566 * If HideImplicitMembers is enabled, see if this printer or class
7567 * is a member of an implicit class...
7570 if (ImplicitClasses
&& HideImplicitMembers
&&
7571 printer
->in_implicit_class
)
7575 * If a username is specified, see if it is allowed or denied
7579 if (!(printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
7580 printer
->num_users
&& username
&& !user_allowed(printer
, username
))
7584 * Add the group separator as needed...
7588 ippAddSeparator(con
->response
);
7593 * Send the attributes...
7596 copy_printer_attrs(con
, printer
, ra
);
7600 cupsArrayDelete(ra
);
7602 con
->response
->request
.status
.status_code
= IPP_OK
;
7607 * 'get_subscription_attrs()' - Get subscription attributes.
7611 get_subscription_attrs(
7612 cupsd_client_t
*con
, /* I - Client connection */
7613 int sub_id
) /* I - Subscription ID */
7615 http_status_t status
; /* Policy status */
7616 cupsd_subscription_t
*sub
; /* Subscription */
7617 cups_array_t
*ra
; /* Requested attributes array */
7620 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7621 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7622 con
, con
->http
.fd
, sub_id
);
7625 * Is the subscription ID valid?
7628 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7631 * Bad subscription ID...
7634 send_ipp_status(con
, IPP_NOT_FOUND
,
7635 _("notify-subscription-id %d no good!"), sub_id
);
7643 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7645 con
, sub
->owner
)) != HTTP_OK
)
7647 send_http_error(con
, status
, sub
->dest
);
7652 * Copy the subscription attributes to the response using the
7653 * requested-attributes attribute that may be provided by the client.
7656 ra
= create_requested_array(con
->request
);
7658 copy_subscription_attrs(con
, sub
, ra
);
7660 cupsArrayDelete(ra
);
7662 con
->response
->request
.status
.status_code
= IPP_OK
;
7667 * 'get_subscriptions()' - Get subscriptions.
7671 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7672 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7674 http_status_t status
; /* Policy status */
7675 int count
; /* Number of subscriptions */
7676 int limit
; /* Limit */
7677 cupsd_subscription_t
*sub
; /* Subscription */
7678 cups_array_t
*ra
; /* Requested attributes array */
7679 ipp_attribute_t
*attr
; /* Attribute */
7680 cups_ptype_t dtype
; /* Destination type (printer/class) */
7681 char scheme
[HTTP_MAX_URI
],
7682 /* Scheme portion of URI */
7683 username
[HTTP_MAX_URI
],
7684 /* Username portion of URI */
7686 /* Host portion of URI */
7687 resource
[HTTP_MAX_URI
];
7688 /* Resource portion of URI */
7689 int port
; /* Port portion of URI */
7690 cupsd_job_t
*job
; /* Job pointer */
7691 cupsd_printer_t
*printer
; /* Printer */
7694 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7695 "get_subscriptions(con=%p[%d], uri=%s)",
7696 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
7699 * Is the destination valid?
7702 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7703 sizeof(scheme
), username
, sizeof(username
), host
,
7704 sizeof(host
), &port
, resource
, sizeof(resource
));
7706 if (!strcmp(resource
, "/") ||
7707 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7708 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7709 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7714 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7717 job
= cupsdFindJob(atoi(resource
+ 6));
7721 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
7726 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7732 send_ipp_status(con
, IPP_NOT_FOUND
,
7733 _("The printer or class was not found."));
7736 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7737 IPP_TAG_INTEGER
)) != NULL
)
7739 job
= cupsdFindJob(attr
->values
[0].integer
);
7743 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
7744 attr
->values
[0].integer
);
7755 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
7757 con
, NULL
)) != HTTP_OK
)
7759 send_http_error(con
, status
, printer
);
7764 * Copy the subscription attributes to the response using the
7765 * requested-attributes attribute that may be provided by the client.
7768 ra
= create_requested_array(con
->request
);
7770 if ((attr
= ippFindAttribute(con
->request
, "limit",
7771 IPP_TAG_INTEGER
)) != NULL
)
7772 limit
= attr
->values
[0].integer
;
7777 * See if we only want to see subscriptions for a specific user...
7780 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7781 IPP_TAG_BOOLEAN
)) != NULL
&&
7782 attr
->values
[0].boolean
)
7783 strlcpy(username
, get_username(con
), sizeof(username
));
7787 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7789 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7790 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7791 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
7793 ippAddSeparator(con
->response
);
7794 copy_subscription_attrs(con
, sub
, ra
);
7797 if (limit
&& count
>= limit
)
7801 cupsArrayDelete(ra
);
7804 con
->response
->request
.status
.status_code
= IPP_OK
;
7806 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7811 * 'get_username()' - Get the username associated with a request.
7814 static const char * /* O - Username */
7815 get_username(cupsd_client_t
*con
) /* I - Connection */
7817 ipp_attribute_t
*attr
; /* Attribute */
7820 if (con
->username
[0])
7821 return (con
->username
);
7822 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7823 IPP_TAG_NAME
)) != NULL
)
7824 return (attr
->values
[0].string
.text
);
7826 return ("anonymous");
7831 * 'hold_job()' - Hold a print job.
7835 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7836 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7838 ipp_attribute_t
*attr
, /* Current job-hold-until */
7839 *newattr
; /* New job-hold-until */
7840 int jobid
; /* Job ID */
7841 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
7842 username
[HTTP_MAX_URI
], /* Username portion of URI */
7843 host
[HTTP_MAX_URI
], /* Host portion of URI */
7844 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7845 int port
; /* Port portion of URI */
7846 cupsd_job_t
*job
; /* Job information */
7849 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
7850 uri
->values
[0].string
.text
);
7853 * See if we have a job URI or a printer URI...
7856 if (!strcmp(uri
->name
, "printer-uri"))
7859 * Got a printer URI; see if we also have a job-id attribute...
7862 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7863 IPP_TAG_INTEGER
)) == NULL
)
7865 send_ipp_status(con
, IPP_BAD_REQUEST
,
7866 _("Got a printer-uri attribute but no job-id!"));
7870 jobid
= attr
->values
[0].integer
;
7875 * Got a job URI; parse it to get the job ID...
7878 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7879 sizeof(scheme
), username
, sizeof(username
), host
,
7880 sizeof(host
), &port
, resource
, sizeof(resource
));
7882 if (strncmp(resource
, "/jobs/", 6))
7888 send_ipp_status(con
, IPP_BAD_REQUEST
,
7889 _("Bad job-uri attribute \"%s\"!"),
7890 uri
->values
[0].string
.text
);
7894 jobid
= atoi(resource
+ 6);
7898 * See if the job exists...
7901 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7904 * Nope - return a "not found" error...
7907 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7912 * See if the job is owned by the requesting user...
7915 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7917 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7922 * Hold the job and return...
7927 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
7928 "Job held by user.");
7930 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
7931 IPP_TAG_KEYWORD
)) == NULL
)
7932 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7934 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7935 IPP_TAG_KEYWORD
)) == NULL
)
7936 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7941 * Free the old hold value and copy the new one over...
7944 _cupsStrFree(attr
->values
[0].string
.text
);
7948 attr
->value_tag
= newattr
->value_tag
;
7949 attr
->values
[0].string
.text
=
7950 _cupsStrAlloc(newattr
->values
[0].string
.text
);
7954 attr
->value_tag
= IPP_TAG_KEYWORD
;
7955 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
7959 * Hold job until specified time...
7962 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
7964 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
7965 "Job job-hold-until value changed by user.");
7968 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Held by \"%s\".", username
);
7970 con
->response
->request
.status
.status_code
= IPP_OK
;
7975 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
7979 hold_new_jobs(cupsd_client_t
*con
, /* I - Connection */
7980 ipp_attribute_t
*uri
) /* I - Printer URI */
7982 http_status_t status
; /* Policy status */
7983 cups_ptype_t dtype
; /* Destination type (printer/class) */
7984 cupsd_printer_t
*printer
; /* Printer data */
7987 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_new_jobs(%p[%d], %s)", con
,
7988 con
->http
.fd
, uri
->values
[0].string
.text
);
7991 * Is the destination valid?
7994 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8000 send_ipp_status(con
, IPP_NOT_FOUND
,
8001 _("The printer or class was not found."));
8009 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8011 send_http_error(con
, status
, printer
);
8016 * Hold pending/new jobs sent to the printer...
8019 printer
->holding_new_jobs
= 1;
8021 cupsdSetPrinterReasons(printer
, "+hold-new-jobs");
8022 cupsdAddPrinterHistory(printer
);
8024 if (dtype
& CUPS_PRINTER_CLASS
)
8025 cupsdLogMessage(CUPSD_LOG_INFO
,
8026 "Class \"%s\" now holding pending/new jobs (\"%s\").",
8027 printer
->name
, get_username(con
));
8029 cupsdLogMessage(CUPSD_LOG_INFO
,
8030 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
8031 printer
->name
, get_username(con
));
8034 * Everything was ok, so return OK status...
8037 con
->response
->request
.status
.status_code
= IPP_OK
;
8042 * 'move_job()' - Move a job to a new destination.
8046 move_job(cupsd_client_t
*con
, /* I - Client connection */
8047 ipp_attribute_t
*uri
) /* I - Job URI */
8049 http_status_t status
; /* Policy status */
8050 ipp_attribute_t
*attr
; /* Current attribute */
8051 int jobid
; /* Job ID */
8052 cupsd_job_t
*job
; /* Current job */
8053 const char *src
; /* Source printer/class */
8054 cups_ptype_t stype
, /* Source type (printer or class) */
8055 dtype
; /* Destination type (printer/class) */
8056 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
8057 username
[HTTP_MAX_URI
], /* Username portion of URI */
8058 host
[HTTP_MAX_URI
], /* Host portion of URI */
8059 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8060 int port
; /* Port portion of URI */
8061 cupsd_printer_t
*sprinter
, /* Source printer */
8062 *dprinter
; /* Destination printer */
8065 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
8066 uri
->values
[0].string
.text
);
8069 * Get the new printer or class...
8072 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
8073 IPP_TAG_URI
)) == NULL
)
8076 * Need job-printer-uri...
8079 send_ipp_status(con
, IPP_BAD_REQUEST
,
8080 _("job-printer-uri attribute missing!"));
8084 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
8090 send_ipp_status(con
, IPP_NOT_FOUND
,
8091 _("The printer or class was not found."));
8096 * See if we have a job URI or a printer URI...
8099 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8100 sizeof(scheme
), username
, sizeof(username
), host
,
8101 sizeof(host
), &port
, resource
, sizeof(resource
));
8103 if (!strcmp(uri
->name
, "printer-uri"))
8106 * Got a printer URI; see if we also have a job-id attribute...
8109 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8110 IPP_TAG_INTEGER
)) == NULL
)
8116 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
8117 &sprinter
)) == NULL
)
8123 send_ipp_status(con
, IPP_NOT_FOUND
,
8124 _("The printer or class was not found."));
8133 * Otherwise, just move a single job...
8136 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
8139 * Nope - return a "not found" error...
8142 send_ipp_status(con
, IPP_NOT_FOUND
,
8143 _("Job #%d does not exist!"), attr
->values
[0].integer
);
8149 * Job found, initialize source pointers...
8160 * Got a job URI; parse it to get the job ID...
8163 if (strncmp(resource
, "/jobs/", 6))
8169 send_ipp_status(con
, IPP_BAD_REQUEST
,
8170 _("Bad job-uri attribute \"%s\"!"),
8171 uri
->values
[0].string
.text
);
8176 * See if the job exists...
8179 jobid
= atoi(resource
+ 6);
8181 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8184 * Nope - return a "not found" error...
8187 send_ipp_status(con
, IPP_NOT_FOUND
,
8188 _("Job #%d does not exist!"), jobid
);
8194 * Job found, initialize source pointers...
8203 * Check the policy of the destination printer...
8206 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
8207 job
? job
->username
: NULL
)) != HTTP_OK
)
8209 send_http_error(con
, status
, dprinter
);
8214 * Now move the job or jobs...
8220 * See if the job has been completed...
8223 if (job
->state_value
> IPP_JOB_STOPPED
)
8226 * Return a "not-possible" error...
8229 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8230 _("Job #%d is finished and cannot be altered!"),
8236 * See if the job is owned by the requesting user...
8239 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8241 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8246 * Move the job to a different printer or class...
8249 cupsdMoveJob(job
, dprinter
);
8254 * Got the source printer, now look through the jobs...
8257 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
8259 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
8262 * See if the job is pointing at the source printer or has not been
8266 if (strcasecmp(job
->dest
, src
) ||
8267 job
->state_value
> IPP_JOB_STOPPED
)
8271 * See if the job can be moved by the requesting user...
8274 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8278 * Move the job to a different printer or class...
8281 cupsdMoveJob(job
, dprinter
);
8286 * Start jobs if possible...
8292 * Return with "everything is OK" status...
8295 con
->response
->request
.status
.status_code
= IPP_OK
;
8300 * 'ppd_parse_line()' - Parse a PPD default line.
8303 static int /* O - 0 on success, -1 on failure */
8304 ppd_parse_line(const char *line
, /* I - Line */
8305 char *option
, /* O - Option name */
8306 int olen
, /* I - Size of option name */
8307 char *choice
, /* O - Choice name */
8308 int clen
) /* I - Size of choice name */
8311 * Verify this is a default option line...
8314 if (strncmp(line
, "*Default", 8))
8318 * Read the option name...
8321 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
8331 * Skip everything else up to the colon (:)...
8334 while (*line
&& *line
!= ':')
8343 * Now grab the option choice, skipping leading whitespace...
8346 while (isspace(*line
& 255))
8349 for (clen
--; isalnum(*line
& 255); line
++)
8359 * Return with no errors...
8367 * 'print_job()' - Print a file to a printer or class.
8371 print_job(cupsd_client_t
*con
, /* I - Client connection */
8372 ipp_attribute_t
*uri
) /* I - Printer URI */
8374 ipp_attribute_t
*attr
; /* Current attribute */
8375 ipp_attribute_t
*format
; /* Document-format attribute */
8376 const char *default_format
; /* document-format-default value */
8377 cupsd_job_t
*job
; /* New job */
8378 char filename
[1024]; /* Job filename */
8379 mime_type_t
*filetype
; /* Type of file */
8380 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8381 type
[MIME_MAX_TYPE
], /* Subtype of file */
8382 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8383 /* Textual name of mime type */
8384 cupsd_printer_t
*printer
; /* Printer data */
8385 struct stat fileinfo
; /* File information */
8386 int kbytes
; /* Size of file */
8387 int compression
; /* Document compression */
8390 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
8391 uri
->values
[0].string
.text
);
8394 * Validate print file attributes, for now just document-format and
8395 * compression (CUPS only supports "none" and "gzip")...
8398 compression
= CUPS_FILE_NONE
;
8400 if ((attr
= ippFindAttribute(con
->request
, "compression",
8401 IPP_TAG_KEYWORD
)) != NULL
)
8403 if (strcmp(attr
->values
[0].string
.text
, "none")
8405 && strcmp(attr
->values
[0].string
.text
, "gzip")
8406 #endif /* HAVE_LIBZ */
8409 send_ipp_status(con
, IPP_ATTRIBUTES
,
8410 _("Unsupported compression \"%s\"!"),
8411 attr
->values
[0].string
.text
);
8412 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8413 "compression", NULL
, attr
->values
[0].string
.text
);
8418 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8419 compression
= CUPS_FILE_GZIP
;
8420 #endif /* HAVE_LIBZ */
8424 * Do we have a file to print?
8429 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
8434 * Is the destination valid?
8437 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8443 send_ipp_status(con
, IPP_NOT_FOUND
,
8444 _("The printer or class was not found."));
8449 * Is it a format we support?
8452 if ((format
= ippFindAttribute(con
->request
, "document-format",
8453 IPP_TAG_MIMETYPE
)) != NULL
)
8456 * Grab format from client...
8459 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
8462 send_ipp_status(con
, IPP_BAD_REQUEST
,
8463 _("Could not scan type \"%s\"!"),
8464 format
->values
[0].string
.text
);
8468 else if ((default_format
= cupsGetOption("document-format",
8469 printer
->num_options
,
8470 printer
->options
)) != NULL
)
8473 * Use default document format...
8476 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
8478 send_ipp_status(con
, IPP_BAD_REQUEST
,
8479 _("Could not scan type \"%s\"!"),
8490 strcpy(super
, "application");
8491 strcpy(type
, "octet-stream");
8494 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8497 * Auto-type the file...
8500 ipp_attribute_t
*doc_name
; /* document-name attribute */
8503 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8505 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8506 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8507 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8511 filetype
= mimeType(MimeDatabase
, super
, type
);
8513 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8514 filetype
->super
, filetype
->type
);
8517 filetype
= mimeType(MimeDatabase
, super
, type
);
8521 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8524 * Replace the document-format attribute value with the auto-typed or
8528 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8533 _cupsStrFree(format
->values
[0].string
.text
);
8535 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8538 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8539 "document-format", NULL
, mimetype
);
8543 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8544 _("Unsupported format \'%s/%s\'!"), super
, type
);
8545 cupsdLogMessage(CUPSD_LOG_INFO
,
8546 "Hint: Do you have the raw file printing rules enabled?");
8549 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8550 "document-format", NULL
, format
->values
[0].string
.text
);
8556 * Read any embedded job ticket info from PS files...
8559 if (!strcasecmp(filetype
->super
, "application") &&
8560 (!strcasecmp(filetype
->type
, "postscript") ||
8561 !strcasecmp(filetype
->type
, "pdf")))
8562 read_job_ticket(con
);
8565 * Create the job object...
8568 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8572 * Update quota data...
8575 if (stat(con
->filename
, &fileinfo
))
8578 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8580 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8582 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8583 IPP_TAG_INTEGER
)) != NULL
)
8584 attr
->values
[0].integer
+= kbytes
;
8587 * Add the job file...
8590 if (add_file(con
, job
, filetype
, compression
))
8593 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8595 rename(con
->filename
, filename
);
8596 cupsdClearString(&con
->filename
);
8599 * See if we need to add the ending sheet...
8602 if (cupsdTimeoutJob(job
))
8606 * Log and save the job...
8609 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8610 "File of type %s/%s queued by \"%s\".",
8611 filetype
->super
, filetype
->type
, job
->username
);
8612 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8613 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8614 job
->dest
, job
->username
);
8617 * Start the job if possible...
8625 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8627 * This function only gets called when printing a single PDF or PostScript
8628 * file using the Print-Job operation. It doesn't work for Create-Job +
8629 * Send-File, since the job attributes need to be set at job creation
8630 * time for banners to work. The embedded job ticket stuff is here
8631 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8632 * options and IPP attributes which otherwise would be lost.
8634 * The format of a job ticket is simple:
8636 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8638 * %cupsJobTicket: attr1=value1
8639 * %cupsJobTicket: attr2=value2
8641 * %cupsJobTicket: attrN=valueN
8643 * Job ticket lines must appear immediately after the first line that
8644 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8645 * stops looking for job ticket info when it finds a line that does not begin
8646 * with "%cupsJobTicket:".
8648 * The maximum length of a job ticket line, including the prefix, is
8649 * 255 characters to conform with the Adobe DSC.
8651 * Read-only attributes are rejected with a notice to the error log in
8652 * case a malicious user tries anything. Since the job ticket is read
8653 * prior to attribute validation in print_job(), job ticket attributes
8654 * will go through the same validation as IPP attributes...
8658 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8660 cups_file_t
*fp
; /* File to read from */
8661 char line
[256]; /* Line data */
8662 int num_options
; /* Number of options */
8663 cups_option_t
*options
; /* Options */
8664 ipp_t
*ticket
; /* New attributes */
8665 ipp_attribute_t
*attr
, /* Current attribute */
8666 *attr2
, /* Job attribute */
8667 *prev2
; /* Previous job attribute */
8671 * First open the print file...
8674 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8676 cupsdLogMessage(CUPSD_LOG_ERROR
,
8677 "Unable to open print file for job ticket - %s",
8683 * Skip the first line...
8686 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8688 cupsdLogMessage(CUPSD_LOG_ERROR
,
8689 "Unable to read from print file for job ticket - %s",
8695 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8698 * Not a DSC-compliant file, so no job ticket info will be available...
8706 * Read job ticket info from the file...
8712 while (cupsFileGets(fp
, line
, sizeof(line
)))
8715 * Stop at the first non-ticket line...
8718 if (strncmp(line
, "%cupsJobTicket:", 15))
8722 * Add the options to the option array...
8725 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8729 * Done with the file; see if we have any options...
8734 if (num_options
== 0)
8738 * OK, convert the options to an attribute list, and apply them to
8743 cupsEncodeOptions(ticket
, num_options
, options
);
8746 * See what the user wants to change.
8749 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8751 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8754 if (!strcmp(attr
->name
, "job-originating-host-name") ||
8755 !strcmp(attr
->name
, "job-originating-user-name") ||
8756 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8757 !strcmp(attr
->name
, "job-k-octets") ||
8758 !strcmp(attr
->name
, "job-id") ||
8759 !strncmp(attr
->name
, "job-state", 9) ||
8760 !strncmp(attr
->name
, "time-at-", 8))
8761 continue; /* Read-only attrs */
8763 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8764 IPP_TAG_ZERO
)) != NULL
)
8767 * Some other value; first free the old value...
8770 if (con
->request
->attrs
== attr2
)
8772 con
->request
->attrs
= attr2
->next
;
8777 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8778 if (prev2
->next
== attr2
)
8780 prev2
->next
= attr2
->next
;
8785 if (con
->request
->last
== attr2
)
8786 con
->request
->last
= prev2
;
8788 _ippFreeAttr(attr2
);
8792 * Add new option by copying it...
8795 copy_attribute(con
->request
, attr
, 0);
8799 * Then free the attribute list and option array...
8803 cupsFreeOptions(num_options
, options
);
8808 * 'reject_jobs()' - Reject print jobs to a printer.
8812 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8813 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8815 http_status_t status
; /* Policy status */
8816 cups_ptype_t dtype
; /* Destination type (printer/class) */
8817 cupsd_printer_t
*printer
; /* Printer data */
8818 ipp_attribute_t
*attr
; /* printer-state-message text */
8821 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8822 con
->http
.fd
, uri
->values
[0].string
.text
);
8825 * Is the destination valid?
8828 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8834 send_ipp_status(con
, IPP_NOT_FOUND
,
8835 _("The printer or class was not found."));
8843 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8845 send_http_error(con
, status
, printer
);
8850 * Reject jobs sent to the printer...
8853 printer
->accepting
= 0;
8855 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8856 IPP_TAG_TEXT
)) == NULL
)
8857 strcpy(printer
->state_message
, "Rejecting Jobs");
8859 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8860 sizeof(printer
->state_message
));
8862 cupsdAddPrinterHistory(printer
);
8864 if (dtype
& CUPS_PRINTER_CLASS
)
8866 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8868 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8869 printer
->name
, get_username(con
));
8873 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8875 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8876 printer
->name
, get_username(con
));
8880 * Everything was ok, so return OK status...
8883 con
->response
->request
.status
.status_code
= IPP_OK
;
8888 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8892 release_held_new_jobs(
8893 cupsd_client_t
*con
, /* I - Connection */
8894 ipp_attribute_t
*uri
) /* I - Printer URI */
8896 http_status_t status
; /* Policy status */
8897 cups_ptype_t dtype
; /* Destination type (printer/class) */
8898 cupsd_printer_t
*printer
; /* Printer data */
8901 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_held_new_jobs(%p[%d], %s)", con
,
8902 con
->http
.fd
, uri
->values
[0].string
.text
);
8905 * Is the destination valid?
8908 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8914 send_ipp_status(con
, IPP_NOT_FOUND
,
8915 _("The printer or class was not found."));
8923 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8925 send_http_error(con
, status
, printer
);
8930 * Hold pending/new jobs sent to the printer...
8933 printer
->holding_new_jobs
= 0;
8935 cupsdSetPrinterReasons(printer
, "-hold-new-jobs");
8936 cupsdAddPrinterHistory(printer
);
8938 if (dtype
& CUPS_PRINTER_CLASS
)
8939 cupsdLogMessage(CUPSD_LOG_INFO
,
8940 "Class \"%s\" now printing pending/new jobs (\"%s\").",
8941 printer
->name
, get_username(con
));
8943 cupsdLogMessage(CUPSD_LOG_INFO
,
8944 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
8945 printer
->name
, get_username(con
));
8948 * Everything was ok, so return OK status...
8951 con
->response
->request
.status
.status_code
= IPP_OK
;
8956 * 'release_job()' - Release a held print job.
8960 release_job(cupsd_client_t
*con
, /* I - Client connection */
8961 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8963 ipp_attribute_t
*attr
; /* Current attribute */
8964 int jobid
; /* Job ID */
8965 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
8966 username
[HTTP_MAX_URI
], /* Username portion of URI */
8967 host
[HTTP_MAX_URI
], /* Host portion of URI */
8968 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8969 int port
; /* Port portion of URI */
8970 cupsd_job_t
*job
; /* Job information */
8973 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8974 con
->http
.fd
, uri
->values
[0].string
.text
);
8977 * See if we have a job URI or a printer URI...
8980 if (!strcmp(uri
->name
, "printer-uri"))
8983 * Got a printer URI; see if we also have a job-id attribute...
8986 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8987 IPP_TAG_INTEGER
)) == NULL
)
8989 send_ipp_status(con
, IPP_BAD_REQUEST
,
8990 _("Got a printer-uri attribute but no job-id!"));
8994 jobid
= attr
->values
[0].integer
;
8999 * Got a job URI; parse it to get the job ID...
9002 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9003 sizeof(scheme
), username
, sizeof(username
), host
,
9004 sizeof(host
), &port
, resource
, sizeof(resource
));
9006 if (strncmp(resource
, "/jobs/", 6))
9012 send_ipp_status(con
, IPP_BAD_REQUEST
,
9013 _("Bad job-uri attribute \"%s\"!"),
9014 uri
->values
[0].string
.text
);
9018 jobid
= atoi(resource
+ 6);
9022 * See if the job exists...
9025 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9028 * Nope - return a "not found" error...
9031 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9036 * See if job is "held"...
9039 if (job
->state_value
!= IPP_JOB_HELD
)
9042 * Nope - return a "not possible" error...
9045 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
9050 * See if the job is owned by the requesting user...
9053 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9055 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9060 * Reset the job-hold-until value to "no-hold"...
9063 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9064 IPP_TAG_KEYWORD
)) == NULL
)
9065 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9069 _cupsStrFree(attr
->values
[0].string
.text
);
9071 attr
->value_tag
= IPP_TAG_KEYWORD
;
9072 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
9074 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
9075 "Job job-hold-until value changed by user.");
9079 * Release the job and return...
9082 cupsdReleaseJob(job
);
9084 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
9085 "Job released by user.");
9087 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
9089 con
->response
->request
.status
.status_code
= IPP_OK
;
9096 * 'renew_subscription()' - Renew an existing subscription...
9101 cupsd_client_t
*con
, /* I - Client connection */
9102 int sub_id
) /* I - Subscription ID */
9104 http_status_t status
; /* Policy status */
9105 cupsd_subscription_t
*sub
; /* Subscription */
9106 ipp_attribute_t
*lease
; /* notify-lease-duration */
9109 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9110 "renew_subscription(con=%p[%d], sub_id=%d)",
9111 con
, con
->http
.fd
, sub_id
);
9114 * Is the subscription ID valid?
9117 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
9120 * Bad subscription ID...
9123 send_ipp_status(con
, IPP_NOT_FOUND
,
9124 _("notify-subscription-id %d no good!"), sub_id
);
9131 * Job subscriptions cannot be renewed...
9134 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9135 _("Job subscriptions cannot be renewed!"));
9143 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
9145 con
, sub
->owner
)) != HTTP_OK
)
9147 send_http_error(con
, status
, sub
->dest
);
9152 * Renew the subscription...
9155 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
9158 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
9160 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
9162 cupsdLogMessage(CUPSD_LOG_INFO
,
9163 "renew_subscription: Limiting notify-lease-duration to "
9166 sub
->lease
= MaxLeaseDuration
;
9169 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
9171 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
9173 con
->response
->request
.status
.status_code
= IPP_OK
;
9175 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
9176 "notify-lease-duration", sub
->lease
);
9181 * 'restart_job()' - Restart an old print job.
9185 restart_job(cupsd_client_t
*con
, /* I - Client connection */
9186 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
9188 ipp_attribute_t
*attr
; /* Current attribute */
9189 int jobid
; /* Job ID */
9190 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
9191 username
[HTTP_MAX_URI
], /* Username portion of URI */
9192 host
[HTTP_MAX_URI
], /* Host portion of URI */
9193 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9194 int port
; /* Port portion of URI */
9195 cupsd_job_t
*job
; /* Job information */
9198 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
9199 con
->http
.fd
, uri
->values
[0].string
.text
);
9202 * See if we have a job URI or a printer URI...
9205 if (!strcmp(uri
->name
, "printer-uri"))
9208 * Got a printer URI; see if we also have a job-id attribute...
9211 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9212 IPP_TAG_INTEGER
)) == NULL
)
9214 send_ipp_status(con
, IPP_BAD_REQUEST
,
9215 _("Got a printer-uri attribute but no job-id!"));
9219 jobid
= attr
->values
[0].integer
;
9224 * Got a job URI; parse it to get the job ID...
9227 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9228 sizeof(scheme
), username
, sizeof(username
), host
,
9229 sizeof(host
), &port
, resource
, sizeof(resource
));
9231 if (strncmp(resource
, "/jobs/", 6))
9237 send_ipp_status(con
, IPP_BAD_REQUEST
,
9238 _("Bad job-uri attribute \"%s\"!"),
9239 uri
->values
[0].string
.text
);
9243 jobid
= atoi(resource
+ 6);
9247 * See if the job exists...
9250 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9253 * Nope - return a "not found" error...
9256 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9261 * See if job is in any of the "completed" states...
9264 if (job
->state_value
<= IPP_JOB_PROCESSING
)
9267 * Nope - return a "not possible" error...
9270 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
9276 * See if we have retained the job files...
9281 if (!job
->attrs
|| job
->num_files
== 0)
9284 * Nope - return a "not possible" error...
9287 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9288 _("Job #%d cannot be restarted - no files!"), jobid
);
9293 * See if the job is owned by the requesting user...
9296 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9298 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9303 * Restart the job and return...
9306 cupsdRestartJob(job
);
9308 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
9310 con
->response
->request
.status
.status_code
= IPP_OK
;
9315 * 'save_auth_info()' - Save authentication information for a job.
9320 cupsd_client_t
*con
, /* I - Client connection */
9321 cupsd_job_t
*job
, /* I - Job */
9322 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
9324 int i
; /* Looping var */
9325 char filename
[1024]; /* Job authentication filename */
9326 cups_file_t
*fp
; /* Job authentication file */
9327 char line
[2048]; /* Line for file */
9328 cupsd_printer_t
*dest
; /* Destination printer/class */
9332 * This function saves the in-memory authentication information for
9333 * a job so that it can be used to authenticate with a remote host.
9334 * The information is stored in a file that is readable only by the
9335 * root user. The fields are Base-64 encoded, each on a separate line,
9336 * followed by random number (up to 1024) of newlines to limit the
9337 * amount of information that is exposed.
9339 * Because of the potential for exposing of authentication information,
9340 * this functionality is only enabled when running cupsd as root.
9342 * This caching only works for the Basic and BasicDigest authentication
9343 * types. Digest authentication cannot be cached this way, and in
9344 * the future Kerberos authentication may make all of this obsolete.
9346 * Authentication information is saved whenever an authenticated
9347 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9350 * This information is deleted after a job is completed or canceled,
9351 * so reprints may require subsequent re-authentication.
9357 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9361 * Create the authentication file and change permissions...
9364 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9365 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9367 cupsdLogMessage(CUPSD_LOG_ERROR
,
9368 "Unable to save authentication info to \"%s\" - %s",
9369 filename
, strerror(errno
));
9373 fchown(cupsFileNumber(fp
), 0, 0);
9374 fchmod(cupsFileNumber(fp
), 0400);
9376 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9379 * Write 1 to 3 auth values...
9382 cupsdClearString(&job
->auth_username
);
9383 cupsdClearString(&job
->auth_domain
);
9384 cupsdClearString(&job
->auth_password
);
9386 for (i
= 0; i
< auth_info
->num_values
; i
++)
9388 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
9389 strlen(auth_info
->values
[i
].string
.text
));
9390 cupsFilePrintf(fp
, "%s\n", line
);
9392 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9393 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s",
9394 auth_info
->values
[i
].string
.text
);
9395 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9396 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s",
9397 auth_info
->values
[i
].string
.text
);
9398 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9399 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s",
9400 auth_info
->values
[i
].string
.text
);
9403 else if (con
->username
[0])
9406 * Write the authenticated username...
9409 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
9410 cupsFilePrintf(fp
, "%s\n", line
);
9412 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", con
->username
);
9413 cupsdClearString(&job
->auth_domain
);
9416 * Write the authenticated password...
9419 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
9420 cupsFilePrintf(fp
, "%s\n", line
);
9422 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", con
->password
);
9426 * Write a random number of newlines to the end of the file...
9429 for (i
= (rand() % 1024); i
>= 0; i
--)
9430 cupsFilePutChar(fp
, '\n');
9433 * Close the file and return...
9438 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9439 if (con
->gss_have_creds
)
9440 save_krb5_creds(con
, job
);
9441 else if (job
->ccname
)
9442 cupsdClearString(&(job
->ccname
));
9443 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9447 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9449 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
9453 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
9454 cupsd_job_t
*job
) /* I - Job */
9456 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
9457 cupsdLogMessage(CUPSD_LOG_INFO
,
9458 "Sorry, your version of Kerberos does not support delegated "
9463 krb5_error_code error
; /* Kerberos error code */
9464 OM_uint32 major_status
, /* Major status code */
9465 minor_status
; /* Minor status code */
9466 krb5_principal principal
; /* Kerberos principal */
9471 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
9475 if (krb5_init_context
== NULL
)
9477 # endif /* __APPLE__ */
9479 if (!KerberosInitialized
)
9482 * Setup a Kerberos context for the scheduler to use...
9485 KerberosInitialized
= 1;
9487 if (krb5_init_context(&KerberosContext
))
9489 KerberosContext
= NULL
;
9491 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize Kerberos context");
9497 * We MUST create a file-based cache because memory-based caches are
9498 * only valid for the current process/address space.
9500 * Due to various bugs/features in different versions of Kerberos, we
9501 * need either the krb5_cc_new_unique() function or Heimdal's version
9502 * of krb5_cc_gen_new() to create a new FILE: credential cache that
9503 * can be passed to the backend. These functions create a temporary
9504 * file (typically in /tmp) containing the cached credentials, which
9505 * are removed when we have successfully printed a job.
9508 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
9509 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
9510 &(job
->ccache
))) != 0)
9511 # else /* HAVE_HEIMDAL */
9512 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
9513 &(job
->ccache
))) != 0)
9514 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
9516 cupsdLogMessage(CUPSD_LOG_ERROR
,
9517 "Unable to create new credentials cache (%d/%s)",
9518 error
, strerror(errno
));
9523 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
9525 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to parse kerberos username (%d/%s)",
9526 error
, strerror(errno
));
9527 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9532 if ((error
= krb5_cc_initialize(KerberosContext
, job
->ccache
, principal
)))
9534 cupsdLogMessage(CUPSD_LOG_ERROR
,
9535 "Unable to initialize credentials cache (%d/%s)", error
,
9537 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9538 krb5_free_principal(KerberosContext
, principal
);
9543 krb5_free_principal(KerberosContext
, principal
);
9546 * Copy the user's credentials to the new cache file...
9549 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
9552 if (GSS_ERROR(major_status
))
9554 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
9555 "Unable to import client credentials cache");
9556 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9562 * Add the KRB5CCNAME environment variable to the job so that the
9563 * backend can use the credentials when printing.
9566 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
9567 krb5_cc_get_name(KerberosContext
, job
->ccache
));
9569 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "save_krb5_creds: %s", job
->ccname
);
9570 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
9572 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9576 * 'send_document()' - Send a file to a printer or class.
9580 send_document(cupsd_client_t
*con
, /* I - Client connection */
9581 ipp_attribute_t
*uri
) /* I - Printer URI */
9583 ipp_attribute_t
*attr
; /* Current attribute */
9584 ipp_attribute_t
*format
; /* Request's document-format attribute */
9585 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9586 const char *default_format
;/* document-format-default value */
9587 int jobid
; /* Job ID number */
9588 cupsd_job_t
*job
; /* Current job */
9589 char job_uri
[HTTP_MAX_URI
],
9591 scheme
[HTTP_MAX_URI
],
9592 /* Method portion of URI */
9593 username
[HTTP_MAX_URI
],
9594 /* Username portion of URI */
9596 /* Host portion of URI */
9597 resource
[HTTP_MAX_URI
];
9598 /* Resource portion of URI */
9599 int port
; /* Port portion of URI */
9600 mime_type_t
*filetype
; /* Type of file */
9601 char super
[MIME_MAX_SUPER
],
9602 /* Supertype of file */
9603 type
[MIME_MAX_TYPE
],
9604 /* Subtype of file */
9605 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9606 /* Textual name of mime type */
9607 char filename
[1024]; /* Job filename */
9608 cupsd_printer_t
*printer
; /* Current printer */
9609 struct stat fileinfo
; /* File information */
9610 int kbytes
; /* Size of file */
9611 int compression
; /* Type of compression */
9612 int start_job
; /* Start the job? */
9615 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9616 con
->http
.fd
, uri
->values
[0].string
.text
);
9619 * See if we have a job URI or a printer URI...
9622 if (!strcmp(uri
->name
, "printer-uri"))
9625 * Got a printer URI; see if we also have a job-id attribute...
9628 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9629 IPP_TAG_INTEGER
)) == NULL
)
9631 send_ipp_status(con
, IPP_BAD_REQUEST
,
9632 _("Got a printer-uri attribute but no job-id!"));
9636 jobid
= attr
->values
[0].integer
;
9641 * Got a job URI; parse it to get the job ID...
9644 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9645 sizeof(scheme
), username
, sizeof(username
), host
,
9646 sizeof(host
), &port
, resource
, sizeof(resource
));
9648 if (strncmp(resource
, "/jobs/", 6))
9654 send_ipp_status(con
, IPP_BAD_REQUEST
,
9655 _("Bad job-uri attribute \"%s\"!"),
9656 uri
->values
[0].string
.text
);
9660 jobid
= atoi(resource
+ 6);
9664 * See if the job exists...
9667 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9670 * Nope - return a "not found" error...
9673 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9677 printer
= cupsdFindDest(job
->dest
);
9680 * See if the job is owned by the requesting user...
9683 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9685 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9690 * OK, see if the client is sending the document compressed - CUPS
9691 * only supports "none" and "gzip".
9694 compression
= CUPS_FILE_NONE
;
9696 if ((attr
= ippFindAttribute(con
->request
, "compression",
9697 IPP_TAG_KEYWORD
)) != NULL
)
9699 if (strcmp(attr
->values
[0].string
.text
, "none")
9701 && strcmp(attr
->values
[0].string
.text
, "gzip")
9702 #endif /* HAVE_LIBZ */
9705 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
9706 attr
->values
[0].string
.text
);
9707 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9708 "compression", NULL
, attr
->values
[0].string
.text
);
9713 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9714 compression
= CUPS_FILE_GZIP
;
9715 #endif /* HAVE_LIBZ */
9719 * Do we have a file to print?
9724 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
9729 * Is it a format we support?
9732 if ((format
= ippFindAttribute(con
->request
, "document-format",
9733 IPP_TAG_MIMETYPE
)) != NULL
)
9736 * Grab format from client...
9739 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
9742 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9743 format
->values
[0].string
.text
);
9747 else if ((default_format
= cupsGetOption("document-format",
9748 printer
->num_options
,
9749 printer
->options
)) != NULL
)
9752 * Use default document format...
9755 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
9757 send_ipp_status(con
, IPP_BAD_REQUEST
,
9758 _("Could not scan type \"%s\"!"),
9766 * No document format attribute? Auto-type it!
9769 strcpy(super
, "application");
9770 strcpy(type
, "octet-stream");
9773 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9776 * Auto-type the file...
9779 ipp_attribute_t
*doc_name
; /* document-name attribute */
9782 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9784 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9785 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9786 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9790 filetype
= mimeType(MimeDatabase
, super
, type
);
9792 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9793 filetype
->super
, filetype
->type
);
9796 filetype
= mimeType(MimeDatabase
, super
, type
);
9801 * Replace the document-format attribute value with the auto-typed or
9805 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9808 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9809 IPP_TAG_MIMETYPE
)) != NULL
)
9811 _cupsStrFree(jformat
->values
[0].string
.text
);
9813 jformat
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
9816 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9817 "document-format", NULL
, mimetype
);
9821 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9822 _("Unsupported format \'%s/%s\'!"), super
, type
);
9823 cupsdLogMessage(CUPSD_LOG_INFO
,
9824 "Hint: Do you have the raw file printing rules enabled?");
9827 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9828 "document-format", NULL
, format
->values
[0].string
.text
);
9833 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9835 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9838 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9839 _("Unsupported format \'%s\'!"), mimetype
);
9841 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9842 "document-format", NULL
, mimetype
);
9848 * Add the file to the job...
9853 if (add_file(con
, job
, filetype
, compression
))
9856 if (stat(con
->filename
, &fileinfo
))
9859 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9861 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9863 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
9864 IPP_TAG_INTEGER
)) != NULL
)
9865 attr
->values
[0].integer
+= kbytes
;
9867 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
9869 rename(con
->filename
, filename
);
9871 cupsdClearString(&con
->filename
);
9873 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9874 filetype
->super
, filetype
->type
, job
->username
);
9877 * Start the job if this is the last document...
9880 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9881 IPP_TAG_BOOLEAN
)) != NULL
&&
9882 attr
->values
[0].boolean
)
9885 * See if we need to add the ending sheet...
9888 if (cupsdTimeoutJob(job
))
9891 if (job
->state_value
== IPP_JOB_STOPPED
)
9893 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9894 job
->state_value
= IPP_JOB_PENDING
;
9896 else if (job
->state_value
== IPP_JOB_HELD
)
9898 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9899 IPP_TAG_KEYWORD
)) == NULL
)
9900 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9902 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9904 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9905 job
->state_value
= IPP_JOB_PENDING
;
9910 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9916 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9917 IPP_TAG_KEYWORD
)) == NULL
)
9918 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9920 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9922 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9923 job
->state_value
= IPP_JOB_HELD
;
9924 job
->hold_until
= time(NULL
) + 60;
9927 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9934 * Fill in the response info...
9937 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
9940 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9943 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9945 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9947 add_job_state_reasons(con
, job
);
9949 con
->response
->request
.status
.status_code
= IPP_OK
;
9952 * Start the job if necessary...
9961 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9966 cupsd_client_t
*con
, /* I - Client connection */
9967 http_status_t status
, /* I - HTTP status code */
9968 cupsd_printer_t
*printer
) /* I - Printer, if any */
9970 ipp_attribute_t
*uri
; /* Request URI, if any */
9973 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
9974 IPP_TAG_URI
)) == NULL
)
9975 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
9977 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
9978 "Returning HTTP %s for %s (%s) from %s",
9980 ippOpString(con
->request
->request
.op
.operation_id
),
9981 uri
? uri
->values
[0].string
.text
: "no URI",
9982 con
->http
.hostname
);
9986 int auth_type
; /* Type of authentication required */
9989 auth_type
= CUPSD_AUTH_NONE
;
9991 if (status
== HTTP_UNAUTHORIZED
&&
9992 printer
->num_auth_info_required
> 0 &&
9993 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
9995 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
9996 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
9997 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
10000 * Creating and authenticating jobs requires Kerberos...
10003 auth_type
= CUPSD_AUTH_NEGOTIATE
;
10008 * Use policy/location-defined authentication requirements...
10011 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
10012 cupsd_location_t
*auth
; /* Pointer to authentication element */
10015 if (printer
->type
& CUPS_PRINTER_CLASS
)
10016 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
10018 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
10020 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
10021 auth
->type
== CUPSD_AUTH_NONE
)
10022 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
10024 con
->request
->request
.op
.operation_id
:
10029 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
10030 auth_type
= DefaultAuthType
;
10032 auth_type
= auth
->type
;
10036 cupsdSendError(con
, status
, auth_type
);
10039 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
10041 ippDelete(con
->response
);
10042 con
->response
= NULL
;
10049 * 'send_ipp_status()' - Send a status back to the IPP client.
10053 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
10054 ipp_status_t status
, /* I - IPP status code */
10055 const char *message
,/* I - Status message */
10056 ...) /* I - Additional args as needed */
10058 va_list ap
; /* Pointer to additional args */
10059 char formatted
[1024]; /* Formatted errror message */
10062 va_start(ap
, message
);
10063 vsnprintf(formatted
, sizeof(formatted
),
10064 _cupsLangString(con
->language
, message
), ap
);
10067 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
10068 ippOpString(con
->request
->request
.op
.operation_id
),
10069 ippErrorString(status
), formatted
);
10071 con
->response
->request
.status
.status_code
= status
;
10073 if (ippFindAttribute(con
->response
, "attributes-charset",
10074 IPP_TAG_ZERO
) == NULL
)
10075 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
10076 "attributes-charset", NULL
, DefaultCharset
);
10078 if (ippFindAttribute(con
->response
, "attributes-natural-language",
10079 IPP_TAG_ZERO
) == NULL
)
10080 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
10081 "attributes-natural-language", NULL
, DefaultLanguage
);
10083 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
10084 "status-message", NULL
, formatted
);
10089 * 'set_default()' - Set the default destination...
10093 set_default(cupsd_client_t
*con
, /* I - Client connection */
10094 ipp_attribute_t
*uri
) /* I - Printer URI */
10096 http_status_t status
; /* Policy status */
10097 cups_ptype_t dtype
; /* Destination type (printer/class) */
10098 cupsd_printer_t
*printer
, /* Printer */
10099 *oldprinter
; /* Old default printer */
10102 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
10103 con
->http
.fd
, uri
->values
[0].string
.text
);
10106 * Is the destination valid?
10109 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10115 send_ipp_status(con
, IPP_NOT_FOUND
,
10116 _("The printer or class was not found."));
10124 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
10126 send_http_error(con
, status
, NULL
);
10131 * Set it as the default...
10134 oldprinter
= DefaultPrinter
;
10135 DefaultPrinter
= printer
;
10138 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
10139 "%s is no longer the default printer.", oldprinter
->name
);
10141 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
10142 "%s is now the default printer.", printer
->name
);
10144 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
10145 CUPSD_DIRTY_REMOTE
| CUPSD_DIRTY_PRINTCAP
);
10147 cupsdLogMessage(CUPSD_LOG_INFO
,
10148 "Default destination set to \"%s\" by \"%s\".",
10149 printer
->name
, get_username(con
));
10152 * Everything was ok, so return OK status...
10155 con
->response
->request
.status
.status_code
= IPP_OK
;
10160 * 'set_job_attrs()' - Set job attributes.
10164 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
10165 ipp_attribute_t
*uri
) /* I - Job URI */
10167 ipp_attribute_t
*attr
, /* Current attribute */
10168 *attr2
; /* Job attribute */
10169 int jobid
; /* Job ID */
10170 cupsd_job_t
*job
; /* Current job */
10171 char scheme
[HTTP_MAX_URI
],
10172 /* Method portion of URI */
10173 username
[HTTP_MAX_URI
],
10174 /* Username portion of URI */
10175 host
[HTTP_MAX_URI
],
10176 /* Host portion of URI */
10177 resource
[HTTP_MAX_URI
];
10178 /* Resource portion of URI */
10179 int port
; /* Port portion of URI */
10180 int event
; /* Events? */
10181 int check_jobs
; /* Check jobs? */
10184 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
10185 con
->http
.fd
, uri
->values
[0].string
.text
);
10188 * Start with "everything is OK" status...
10191 con
->response
->request
.status
.status_code
= IPP_OK
;
10194 * See if we have a job URI or a printer URI...
10197 if (!strcmp(uri
->name
, "printer-uri"))
10200 * Got a printer URI; see if we also have a job-id attribute...
10203 if ((attr
= ippFindAttribute(con
->request
, "job-id",
10204 IPP_TAG_INTEGER
)) == NULL
)
10206 send_ipp_status(con
, IPP_BAD_REQUEST
,
10207 _("Got a printer-uri attribute but no job-id!"));
10211 jobid
= attr
->values
[0].integer
;
10216 * Got a job URI; parse it to get the job ID...
10219 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
10220 sizeof(scheme
), username
, sizeof(username
), host
,
10221 sizeof(host
), &port
, resource
, sizeof(resource
));
10223 if (strncmp(resource
, "/jobs/", 6))
10229 send_ipp_status(con
, IPP_BAD_REQUEST
,
10230 _("Bad job-uri attribute \"%s\"!"),
10231 uri
->values
[0].string
.text
);
10235 jobid
= atoi(resource
+ 6);
10239 * See if the job exists...
10242 if ((job
= cupsdFindJob(jobid
)) == NULL
)
10245 * Nope - return a "not found" error...
10248 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
10253 * See if the job has been completed...
10256 if (job
->state_value
> IPP_JOB_STOPPED
)
10259 * Return a "not-possible" error...
10262 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10263 _("Job #%d is finished and cannot be altered!"), jobid
);
10268 * See if the job is owned by the requesting user...
10271 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
10273 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
10278 * See what the user wants to change.
10286 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10288 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
10291 if (!strcmp(attr
->name
, "attributes-charset") ||
10292 !strcmp(attr
->name
, "attributes-natural-language") ||
10293 !strcmp(attr
->name
, "document-compression") ||
10294 !strcmp(attr
->name
, "document-format") ||
10295 !strcmp(attr
->name
, "job-detailed-status-messages") ||
10296 !strcmp(attr
->name
, "job-document-access-errors") ||
10297 !strcmp(attr
->name
, "job-id") ||
10298 !strcmp(attr
->name
, "job-impressions-completed") ||
10299 !strcmp(attr
->name
, "job-k-octets") ||
10300 !strcmp(attr
->name
, "job-originating-host-name") ||
10301 !strcmp(attr
->name
, "job-originating-user-name") ||
10302 !strcmp(attr
->name
, "job-printer-up-time") ||
10303 !strcmp(attr
->name
, "job-printer-uri") ||
10304 !strcmp(attr
->name
, "job-sheets") ||
10305 !strcmp(attr
->name
, "job-state-message") ||
10306 !strcmp(attr
->name
, "job-state-reasons") ||
10307 !strcmp(attr
->name
, "job-uri") ||
10308 !strcmp(attr
->name
, "number-of-documents") ||
10309 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
10310 !strcmp(attr
->name
, "output-device-assigned") ||
10311 !strncmp(attr
->name
, "date-time-at-", 13) ||
10312 !strncmp(attr
->name
, "job-k-octets", 12) ||
10313 !strncmp(attr
->name
, "job-media-sheets", 16) ||
10314 !strncmp(attr
->name
, "time-at-", 8))
10320 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
10321 _("%s cannot be changed."), attr
->name
);
10323 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10324 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10329 if (!strcmp(attr
->name
, "job-priority"))
10332 * Change the job priority...
10335 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10337 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
10339 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10340 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10342 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
10344 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10345 _("Job is completed and cannot be changed."));
10348 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10350 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
10351 attr
->values
[0].integer
);
10352 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
10355 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
10356 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
10359 else if (!strcmp(attr
->name
, "job-state"))
10362 * Change the job state...
10365 if (attr
->value_tag
!= IPP_TAG_ENUM
)
10367 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
10369 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10370 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10374 switch (attr
->values
[0].integer
)
10376 case IPP_JOB_PENDING
:
10377 case IPP_JOB_HELD
:
10378 if (job
->state_value
> IPP_JOB_HELD
)
10380 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10381 _("Job state cannot be changed."));
10384 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10386 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10387 attr
->values
[0].integer
);
10389 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
10390 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
10392 event
|= CUPSD_EVENT_JOB_STATE
;
10397 case IPP_JOB_PROCESSING
:
10398 case IPP_JOB_STOPPED
:
10399 if (job
->state_value
!= attr
->values
[0].integer
)
10401 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10402 _("Job state cannot be changed."));
10407 case IPP_JOB_CANCELED
:
10408 case IPP_JOB_ABORTED
:
10409 case IPP_JOB_COMPLETED
:
10410 if (job
->state_value
> IPP_JOB_PROCESSING
)
10412 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10413 _("Job state cannot be changed."));
10416 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10418 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10419 attr
->values
[0].integer
);
10420 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
10428 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10430 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10431 IPP_TAG_ZERO
)) != NULL
)
10434 * Some other value; first free the old value...
10437 if (job
->attrs
->prev
)
10438 job
->attrs
->prev
->next
= attr2
->next
;
10440 job
->attrs
->attrs
= attr2
->next
;
10442 if (job
->attrs
->last
== attr2
)
10443 job
->attrs
->last
= job
->attrs
->prev
;
10445 _ippFreeAttr(attr2
);
10448 * Then copy the attribute...
10451 copy_attribute(job
->attrs
, attr
, 0);
10454 * See if the job-name or job-hold-until is being changed.
10457 if (!strcmp(attr
->name
, "job-hold-until"))
10459 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10460 attr
->values
[0].string
.text
);
10461 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
10463 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10464 cupsdReleaseJob(job
);
10469 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10472 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10475 * Delete the attribute...
10478 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10479 IPP_TAG_ZERO
)) != NULL
)
10481 if (job
->attrs
->prev
)
10482 job
->attrs
->prev
->next
= attr2
->next
;
10484 job
->attrs
->attrs
= attr2
->next
;
10486 if (attr2
== job
->attrs
->last
)
10487 job
->attrs
->last
= job
->attrs
->prev
;
10489 _ippFreeAttr(attr2
);
10491 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10497 * Add new option by copying it...
10500 copy_attribute(job
->attrs
, attr
, 0);
10502 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10511 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10514 * Send events as needed...
10517 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10518 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10519 cupsdFindDest(job
->dest
), job
,
10520 "Job priority changed by user.");
10522 if (event
& CUPSD_EVENT_JOB_STATE
)
10523 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10524 job
->state_value
== IPP_JOB_HELD
?
10525 "Job held by user." : "Job restarted by user.");
10527 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10528 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10529 "Job options changed by user.");
10532 * Start jobs if possible...
10541 * 'set_printer_defaults()' - Set printer default options from a request.
10545 set_printer_defaults(
10546 cupsd_client_t
*con
, /* I - Client connection */
10547 cupsd_printer_t
*printer
) /* I - Printer */
10549 int i
; /* Looping var */
10550 ipp_attribute_t
*attr
; /* Current attribute */
10551 int namelen
; /* Length of attribute name */
10552 char name
[256], /* New attribute name */
10553 value
[256]; /* String version of integer attrs */
10556 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10559 * Skip non-printer attributes...
10562 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10565 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10567 if (!strcmp(attr
->name
, "job-sheets-default"))
10570 * Only allow keywords and names...
10573 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10577 * Only allow job-sheets-default to be set when running without a
10578 * system high classification level...
10581 if (Classification
)
10584 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10586 if (attr
->num_values
> 1)
10587 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10589 cupsdSetString(&printer
->job_sheets
[1], "none");
10591 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10593 cupsdFreePrinterUsers(printer
);
10595 printer
->deny_users
= 0;
10597 if (attr
->value_tag
== IPP_TAG_NAME
&&
10598 (attr
->num_values
> 1 ||
10599 strcmp(attr
->values
[0].string
.text
, "all")))
10601 for (i
= 0; i
< attr
->num_values
; i
++)
10602 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
10605 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10607 cupsdFreePrinterUsers(printer
);
10609 printer
->deny_users
= 1;
10611 if (attr
->value_tag
== IPP_TAG_NAME
&&
10612 (attr
->num_values
> 1 ||
10613 strcmp(attr
->values
[0].string
.text
, "none")))
10615 for (i
= 0; i
< attr
->num_values
; i
++)
10616 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
10619 else if (!strcmp(attr
->name
, "job-quota-period"))
10621 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10624 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10625 attr
->values
[0].integer
);
10626 cupsdFreeQuotas(printer
);
10628 printer
->quota_period
= attr
->values
[0].integer
;
10630 else if (!strcmp(attr
->name
, "job-k-limit"))
10632 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10635 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10636 attr
->values
[0].integer
);
10637 cupsdFreeQuotas(printer
);
10639 printer
->k_limit
= attr
->values
[0].integer
;
10641 else if (!strcmp(attr
->name
, "job-page-limit"))
10643 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10646 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10647 attr
->values
[0].integer
);
10648 cupsdFreeQuotas(printer
);
10650 printer
->page_limit
= attr
->values
[0].integer
;
10652 else if (!strcmp(attr
->name
, "printer-op-policy"))
10654 cupsd_policy_t
*p
; /* Policy */
10657 if (attr
->value_tag
!= IPP_TAG_NAME
)
10660 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10662 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10663 "Setting printer-op-policy to \"%s\"...",
10664 attr
->values
[0].string
.text
);
10665 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10666 printer
->op_policy_ptr
= p
;
10670 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10671 _("Unknown printer-op-policy \"%s\"."),
10672 attr
->values
[0].string
.text
);
10676 else if (!strcmp(attr
->name
, "printer-error-policy"))
10678 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10681 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10682 strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10683 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10684 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
10686 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10687 _("Unknown printer-error-policy \"%s\"."),
10688 attr
->values
[0].string
.text
);
10692 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10693 "Setting printer-error-policy to \"%s\"...",
10694 attr
->values
[0].string
.text
);
10695 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10699 * Skip any other non-default attributes...
10702 namelen
= strlen(attr
->name
);
10703 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10704 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10708 * OK, anything else must be a user-defined default...
10711 strlcpy(name
, attr
->name
, sizeof(name
));
10712 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10714 switch (attr
->value_tag
)
10716 case IPP_TAG_DELETEATTR
:
10717 printer
->num_options
= cupsRemoveOption(name
,
10718 printer
->num_options
,
10719 &(printer
->options
));
10720 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10721 "Deleting %s", attr
->name
);
10724 case IPP_TAG_NAME
:
10725 case IPP_TAG_KEYWORD
:
10727 printer
->num_options
= cupsAddOption(name
,
10728 attr
->values
[0].string
.text
,
10729 printer
->num_options
,
10730 &(printer
->options
));
10731 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10732 "Setting %s to \"%s\"...", attr
->name
,
10733 attr
->values
[0].string
.text
);
10736 case IPP_TAG_BOOLEAN
:
10737 printer
->num_options
= cupsAddOption(name
,
10738 attr
->values
[0].boolean
?
10740 printer
->num_options
,
10741 &(printer
->options
));
10742 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10743 "Setting %s to %s...", attr
->name
,
10744 attr
->values
[0].boolean
? "true" : "false");
10747 case IPP_TAG_INTEGER
:
10748 case IPP_TAG_ENUM
:
10749 sprintf(value
, "%d", attr
->values
[0].integer
);
10750 printer
->num_options
= cupsAddOption(name
, value
,
10751 printer
->num_options
,
10752 &(printer
->options
));
10753 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10754 "Setting %s to %s...", attr
->name
, value
);
10757 case IPP_TAG_RANGE
:
10758 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10759 attr
->values
[0].range
.upper
);
10760 printer
->num_options
= cupsAddOption(name
, value
,
10761 printer
->num_options
,
10762 &(printer
->options
));
10763 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10764 "Setting %s to %s...", attr
->name
, value
);
10767 case IPP_TAG_RESOLUTION
:
10768 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10769 attr
->values
[0].resolution
.yres
,
10770 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10772 printer
->num_options
= cupsAddOption(name
, value
,
10773 printer
->num_options
,
10774 &(printer
->options
));
10775 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10776 "Setting %s to %s...", attr
->name
, value
);
10780 /* Do nothing for other values */
10788 * 'start_printer()' - Start a printer.
10792 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10793 ipp_attribute_t
*uri
) /* I - Printer URI */
10795 http_status_t status
; /* Policy status */
10796 cups_ptype_t dtype
; /* Destination type (printer/class) */
10797 cupsd_printer_t
*printer
; /* Printer data */
10800 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10801 con
->http
.fd
, uri
->values
[0].string
.text
);
10804 * Is the destination valid?
10807 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10813 send_ipp_status(con
, IPP_NOT_FOUND
,
10814 _("The printer or class was not found."));
10822 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10824 send_http_error(con
, status
, printer
);
10829 * Start the printer...
10832 printer
->state_message
[0] = '\0';
10834 cupsdStartPrinter(printer
, 1);
10836 if (dtype
& CUPS_PRINTER_CLASS
)
10837 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10838 printer
->name
, get_username(con
));
10840 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10841 printer
->name
, get_username(con
));
10846 * Everything was ok, so return OK status...
10849 con
->response
->request
.status
.status_code
= IPP_OK
;
10854 * 'stop_printer()' - Stop a printer.
10858 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10859 ipp_attribute_t
*uri
) /* I - Printer URI */
10861 http_status_t status
; /* Policy status */
10862 cups_ptype_t dtype
; /* Destination type (printer/class) */
10863 cupsd_printer_t
*printer
; /* Printer data */
10864 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10867 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10868 con
->http
.fd
, uri
->values
[0].string
.text
);
10871 * Is the destination valid?
10874 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10880 send_ipp_status(con
, IPP_NOT_FOUND
,
10881 _("The printer or class was not found."));
10889 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10891 send_http_error(con
, status
, printer
);
10896 * Stop the printer...
10899 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10900 IPP_TAG_TEXT
)) == NULL
)
10901 strcpy(printer
->state_message
, "Paused");
10904 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10905 sizeof(printer
->state_message
));
10908 cupsdStopPrinter(printer
, 1);
10910 if (dtype
& CUPS_PRINTER_CLASS
)
10911 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10912 printer
->name
, get_username(con
));
10914 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10915 printer
->name
, get_username(con
));
10918 * Everything was ok, so return OK status...
10921 con
->response
->request
.status
.status_code
= IPP_OK
;
10926 * 'url_encode_attr()' - URL-encode a string attribute.
10930 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10931 char *buffer
,/* I - String buffer */
10932 int bufsize
)/* I - Size of buffer */
10934 int i
; /* Looping var */
10935 char *bufptr
, /* Pointer into buffer */
10936 *bufend
; /* End of buffer */
10939 strlcpy(buffer
, attr
->name
, bufsize
);
10940 bufptr
= buffer
+ strlen(buffer
);
10941 bufend
= buffer
+ bufsize
- 1;
10943 for (i
= 0; i
< attr
->num_values
; i
++)
10945 if (bufptr
>= bufend
)
10953 if (bufptr
>= bufend
)
10958 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
10959 bufptr
, bufend
- bufptr
+ 1);
10961 if (bufptr
>= bufend
)
10972 * 'url_encode_string()' - URL-encode a string.
10975 static char * /* O - End of string */
10976 url_encode_string(const char *s
, /* I - String */
10977 char *buffer
, /* I - String buffer */
10978 int bufsize
) /* I - Size of buffer */
10980 char *bufptr
, /* Pointer into buffer */
10981 *bufend
; /* End of buffer */
10982 static const char *hex
= "0123456789ABCDEF";
10987 bufend
= buffer
+ bufsize
- 1;
10989 while (*s
&& bufptr
< bufend
)
10991 if (*s
== ' ' || *s
== '%')
10993 if (bufptr
>= (bufend
- 2))
10997 *bufptr
++ = hex
[(*s
>> 4) & 15];
10998 *bufptr
++ = hex
[*s
& 15];
11002 else if (*s
== '\'' || *s
== '\\')
11004 if (bufptr
>= (bufend
- 1))
11021 * 'user_allowed()' - See if a user is allowed to print to a queue.
11024 static int /* O - 0 if not allowed, 1 if allowed */
11025 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
11026 const char *username
) /* I - Username */
11028 int i
; /* Looping var */
11029 struct passwd
*pw
; /* User password data */
11030 char baseuser
[256], /* Base username */
11031 *baseptr
; /* Pointer to "@" in base username */
11034 if (p
->num_users
== 0)
11037 if (!strcmp(username
, "root"))
11040 if (strchr(username
, '@'))
11043 * Strip @REALM for username check...
11046 strlcpy(baseuser
, username
, sizeof(baseuser
));
11048 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
11051 username
= baseuser
;
11054 pw
= getpwnam(username
);
11057 for (i
= 0; i
< p
->num_users
; i
++)
11059 if (p
->users
[i
][0] == '@')
11062 * Check group membership...
11065 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
11068 else if (p
->users
[i
][0] == '#')
11074 if (cupsdCheckGroup(username
, pw
, p
->users
[i
]))
11077 else if (!strcasecmp(username
, p
->users
[i
]))
11081 return ((i
< p
->num_users
) != p
->deny_users
);
11086 * 'validate_job()' - Validate printer options and destination.
11090 validate_job(cupsd_client_t
*con
, /* I - Client connection */
11091 ipp_attribute_t
*uri
) /* I - Printer URI */
11093 http_status_t status
; /* Policy status */
11094 ipp_attribute_t
*attr
; /* Current attribute */
11095 ipp_attribute_t
*format
; /* Document-format attribute */
11096 cups_ptype_t dtype
; /* Destination type (printer/class) */
11097 char super
[MIME_MAX_SUPER
],
11098 /* Supertype of file */
11099 type
[MIME_MAX_TYPE
];
11100 /* Subtype of file */
11101 cupsd_printer_t
*printer
; /* Printer */
11104 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
11105 con
->http
.fd
, uri
->values
[0].string
.text
);
11108 * OK, see if the client is sending the document compressed - CUPS
11109 * doesn't support compression yet...
11112 if ((attr
= ippFindAttribute(con
->request
, "compression",
11113 IPP_TAG_KEYWORD
)) != NULL
&&
11114 !strcmp(attr
->values
[0].string
.text
, "none"))
11116 send_ipp_status(con
, IPP_ATTRIBUTES
,
11117 _("Unsupported compression attribute %s!"),
11118 attr
->values
[0].string
.text
);
11119 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
11120 "compression", NULL
, attr
->values
[0].string
.text
);
11125 * Is it a format we support?
11128 if ((format
= ippFindAttribute(con
->request
, "document-format",
11129 IPP_TAG_MIMETYPE
)) != NULL
)
11131 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
11134 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
11135 format
->values
[0].string
.text
);
11139 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
11140 !mimeType(MimeDatabase
, super
, type
))
11142 cupsdLogMessage(CUPSD_LOG_INFO
,
11143 "Hint: Do you have the raw file printing rules enabled?");
11144 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
11145 _("Unsupported format \"%s\"!"),
11146 format
->values
[0].string
.text
);
11147 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
11148 "document-format", NULL
, format
->values
[0].string
.text
);
11154 * Is the destination valid?
11157 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11163 send_ipp_status(con
, IPP_NOT_FOUND
,
11164 _("The printer or class was not found."));
11172 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11174 send_http_error(con
, status
, printer
);
11179 * Everything was ok, so return OK status...
11182 con
->response
->request
.status
.status_code
= IPP_OK
;
11187 * 'validate_name()' - Make sure the printer name only contains valid chars.
11190 static int /* O - 0 if name is no good, 1 if good */
11191 validate_name(const char *name
) /* I - Name to check */
11193 const char *ptr
; /* Pointer into name */
11197 * Scan the whole name...
11200 for (ptr
= name
; *ptr
; ptr
++)
11201 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
11205 * All the characters are good; validate the length, too...
11208 return ((ptr
- name
) < 128);
11213 * 'validate_user()' - Validate the user for the request.
11216 static int /* O - 1 if permitted, 0 otherwise */
11217 validate_user(cupsd_job_t
*job
, /* I - Job */
11218 cupsd_client_t
*con
, /* I - Client connection */
11219 const char *owner
, /* I - Owner of job/resource */
11220 char *username
, /* O - Authenticated username */
11221 int userlen
) /* I - Length of username */
11223 cupsd_printer_t
*printer
; /* Printer for job */
11226 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
11227 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
11229 job
->id
, con
? con
->http
.fd
: 0,
11230 owner
? owner
: "(null)", username
, userlen
);
11233 * Validate input...
11236 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11240 * Get the best authenticated username that is available.
11243 strlcpy(username
, get_username(con
), userlen
);
11246 * Check the username against the owner...
11249 printer
= cupsdFindDest(job
->dest
);
11251 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11252 con
, owner
) == HTTP_OK
);
11257 * End of "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $".