2 * "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $"
4 * IPP routines for the CUPS scheduler.
6 * Copyright 2007-2010 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 or selected print jobs.
42 * cancel_job() - Cancel a print job.
43 * cancel_subscription() - Cancel a subscription.
44 * check_rss_recipient() - Check that we do not have a duplicate RSS
46 * check_quotas() - Check quotas for a printer and user.
47 * close_job() - Close a multi-file job.
48 * copy_attribute() - Copy a single attribute.
49 * copy_attrs() - Copy attributes from one request to another.
50 * copy_banner() - Copy a banner file to the requests directory
51 * for the specified job.
52 * copy_file() - Copy a PPD file or interface script...
53 * copy_model() - Copy a PPD model file, substituting default
55 * copy_job_attrs() - Copy job attributes.
56 * copy_printer_attrs() - Copy printer attributes.
57 * copy_subscription_attrs() - Copy subscription attributes.
58 * create_job() - Print a file to a printer or class.
59 * create_requested_array() - Create an array for the requested-attributes.
60 * create_subscription() - Create a notification subscription.
61 * delete_printer() - Remove a printer or class from the system.
62 * get_default() - Get the default destination.
63 * get_devices() - Get the list of available devices on the
65 * get_document() - Get a copy of a job file.
66 * get_job_attrs() - Get job attributes.
67 * get_jobs() - Get a list of jobs for the specified printer.
68 * get_notifications() - Get events for a subscription.
69 * get_ppd() - Get a named PPD from the local system.
70 * get_ppds() - Get the list of PPD files on the local
72 * get_printer_attrs() - Get printer attributes.
73 * get_printer_supported() - Get printer supported values.
74 * get_printers() - Get a list of printers or classes.
75 * get_subscription_attrs() - Get subscription attributes.
76 * get_subscriptions() - Get subscriptions.
77 * get_username() - Get the username associated with a request.
78 * hold_job() - Hold a print job.
79 * hold_new_jobs() - Hold pending/new jobs on a printer or class.
80 * move_job() - Move a job to a new destination.
81 * ppd_parse_line() - Parse a PPD default line.
82 * print_job() - Print a file to a printer or class.
83 * read_job_ticket() - Read a job ticket embedded in a print file.
84 * reject_jobs() - Reject print jobs to a printer.
85 * release_held_new_jobs() - Release pending/new jobs on a printer or
87 * release_job() - Release a held print job.
88 * renew_subscription() - Renew an existing subscription...
89 * restart_job() - Restart an old print job.
90 * save_auth_info() - Save authentication information for a job.
91 * save_krb5_creds() - Save Kerberos credentials for the job.
92 * send_document() - Send a file to a printer or class.
93 * send_http_error() - Send a HTTP error back to the IPP client.
94 * send_ipp_status() - Send a status back to the IPP client.
95 * set_default() - Set the default destination...
96 * set_job_attrs() - Set job attributes.
97 * set_printer_attrs() - Set printer attributes.
98 * set_printer_defaults() - Set printer default options from a request.
99 * start_printer() - Start a printer.
100 * stop_printer() - Stop a printer.
101 * url_encode_attr() - URL-encode a string attribute.
102 * url_encode_string() - URL-encode a string.
103 * user_allowed() - See if a user is allowed to print to a queue.
104 * validate_job() - Validate printer options and destination.
105 * validate_name() - Make sure the printer name only contains
107 * validate_user() - Validate the user for the request.
111 * Include necessary headers...
115 #include <cups/ppd-private.h>
118 # include <ApplicationServices/ApplicationServices.h>
119 # include <CoreFoundation/CoreFoundation.h>
120 # ifdef HAVE_MEMBERSHIP_H
121 # include <membership.h>
122 # endif /* HAVE_MEMBERSHIP_H */
123 # ifdef HAVE_MEMBERSHIPPRIV_H
124 # include <membershipPriv.h>
126 extern int mbr_user_name_to_uuid(const char* name
, uuid_t uu
);
127 extern int mbr_group_name_to_uuid(const char* name
, uuid_t uu
);
128 extern int mbr_check_membership_by_id(uuid_t user
, gid_t group
, int* ismember
);
129 # endif /* HAVE_MEMBERSHIPPRIV_H */
130 #endif /* __APPLE__ */
137 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
138 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
139 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
140 mime_type_t
*filetype
, int compression
);
141 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
142 mime_type_t
*filetype
);
143 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
144 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
145 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
146 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
147 static void add_printer_state_reasons(cupsd_client_t
*con
,
149 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
151 static void apple_init_profile(ppd_file_t
*ppd
, cups_array_t
*languages
,
152 CMDeviceProfileInfo
*profile
, unsigned id
,
153 const char *name
, const char *text
,
154 const char *iccfile
);
155 static void apple_register_profiles(cupsd_printer_t
*p
);
156 static void apple_unregister_profiles(cupsd_printer_t
*p
);
157 #endif /* __APPLE__ */
158 static void apply_printer_defaults(cupsd_printer_t
*printer
,
160 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
161 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
162 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
163 static void cancel_subscription(cupsd_client_t
*con
, int id
);
164 static int check_rss_recipient(const char *recipient
);
165 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
166 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
168 static void close_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
169 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
170 ipp_tag_t group
, int quickcopy
);
171 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
173 static int copy_file(const char *from
, const char *to
);
174 static int copy_model(cupsd_client_t
*con
, const char *from
,
176 static void copy_job_attrs(cupsd_client_t
*con
,
179 static void copy_printer_attrs(cupsd_client_t
*con
,
180 cupsd_printer_t
*printer
,
182 static void copy_subscription_attrs(cupsd_client_t
*con
,
183 cupsd_subscription_t
*sub
,
185 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
186 static cups_array_t
*create_requested_array(ipp_t
*request
);
187 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
188 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
189 static void get_default(cupsd_client_t
*con
);
190 static void get_devices(cupsd_client_t
*con
);
191 static void get_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
192 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
193 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
194 static void get_notifications(cupsd_client_t
*con
);
195 static void get_ppd(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
196 static void get_ppds(cupsd_client_t
*con
);
197 static void get_printers(cupsd_client_t
*con
, int type
);
198 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
199 static void get_printer_supported(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
200 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
201 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
202 static const char *get_username(cupsd_client_t
*con
);
203 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
204 static void hold_new_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
205 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
206 static int ppd_parse_line(const char *line
, char *option
, int olen
,
207 char *choice
, int clen
);
208 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
209 static void read_job_ticket(cupsd_client_t
*con
);
210 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
211 static void release_held_new_jobs(cupsd_client_t
*con
,
212 ipp_attribute_t
*uri
);
213 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
214 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
215 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
216 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
,
217 ipp_attribute_t
*auth_info
);
218 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
219 static void save_krb5_creds(cupsd_client_t
*con
, cupsd_job_t
*job
);
220 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
221 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
222 static void send_http_error(cupsd_client_t
*con
, http_status_t status
,
223 cupsd_printer_t
*printer
);
224 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
225 const char *message
, ...)
227 __attribute__ ((__format__ (__printf__
, 3, 4)))
228 # endif /* __GNUC__ */
230 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
231 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
232 static void set_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
233 static void set_printer_defaults(cupsd_client_t
*con
,
234 cupsd_printer_t
*printer
);
235 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
236 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
237 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
,
239 static char *url_encode_string(const char *s
, char *buffer
, int bufsize
);
240 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
241 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
242 static int validate_name(const char *name
);
243 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
,
244 const char *owner
, char *username
,
249 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
252 int /* O - 1 on success, 0 on failure */
253 cupsdProcessIPPRequest(
254 cupsd_client_t
*con
) /* I - Client connection */
256 ipp_tag_t group
; /* Current group tag */
257 ipp_attribute_t
*attr
; /* Current attribute */
258 ipp_attribute_t
*charset
; /* Character set attribute */
259 ipp_attribute_t
*language
; /* Language attribute */
260 ipp_attribute_t
*uri
= NULL
; /* Printer or job URI attribute */
261 ipp_attribute_t
*username
; /* requesting-user-name attr */
262 int sub_id
; /* Subscription ID */
265 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
266 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
267 con
, con
->http
.fd
, con
->request
->request
.op
.operation_id
);
270 * First build an empty response message for this request...
273 con
->response
= ippNew();
275 con
->response
->request
.status
.version
[0] =
276 con
->request
->request
.op
.version
[0];
277 con
->response
->request
.status
.version
[1] =
278 con
->request
->request
.op
.version
[1];
279 con
->response
->request
.status
.request_id
=
280 con
->request
->request
.op
.request_id
;
283 * Then validate the request header and required attributes...
286 if (con
->request
->request
.any
.version
[0] != 1 &&
287 con
->request
->request
.any
.version
[0] != 2)
290 * Return an error, since we only support IPP 1.x and 2.x.
293 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
294 "%04X %s Bad request version number %d.%d",
295 IPP_VERSION_NOT_SUPPORTED
, con
->http
.hostname
,
296 con
->request
->request
.any
.version
[0],
297 con
->request
->request
.any
.version
[1]);
299 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
300 _("Bad request version number %d.%d"),
301 con
->request
->request
.any
.version
[0],
302 con
->request
->request
.any
.version
[1]);
304 else if (con
->request
->request
.any
.request_id
< 1)
307 * Return an error, since request IDs must be between 1 and 2^31-1
310 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
311 "%04X %s Bad request ID %d",
312 IPP_BAD_REQUEST
, con
->http
.hostname
,
313 con
->request
->request
.any
.request_id
);
315 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad request ID %d"),
316 con
->request
->request
.any
.request_id
);
318 else if (!con
->request
->attrs
)
320 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
321 "%04X %s No attributes in request",
322 IPP_BAD_REQUEST
, con
->http
.hostname
);
324 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request"));
329 * Make sure that the attributes are provided in the correct order and
330 * don't repeat groups...
333 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
336 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
339 * Out of order; return an error...
342 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
343 "%04X %s Attribute groups are out of order",
344 IPP_BAD_REQUEST
, con
->http
.hostname
);
346 send_ipp_status(con
, IPP_BAD_REQUEST
,
347 _("Attribute groups are out of order (%x < %x)"),
348 attr
->group_tag
, group
);
352 group
= attr
->group_tag
;
357 * Then make sure that the first three attributes are:
360 * attributes-natural-language
361 * printer-uri/job-uri
364 attr
= con
->request
->attrs
;
365 if (attr
&& attr
->name
&&
366 !strcmp(attr
->name
, "attributes-charset") &&
367 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
375 if (attr
&& attr
->name
&&
376 !strcmp(attr
->name
, "attributes-natural-language") &&
377 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
382 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
383 IPP_TAG_URI
)) != NULL
)
385 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
386 IPP_TAG_URI
)) != NULL
)
388 else if (con
->request
->request
.op
.operation_id
== CUPS_GET_PPD
)
389 uri
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
);
394 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
395 "attributes-charset", NULL
,
396 charset
->values
[0].string
.text
);
398 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
399 "attributes-charset", NULL
, "utf-8");
402 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
403 "attributes-natural-language", NULL
,
404 language
->values
[0].string
.text
);
406 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
407 "attributes-natural-language", NULL
, DefaultLanguage
);
410 strcasecmp(charset
->values
[0].string
.text
, "us-ascii") &&
411 strcasecmp(charset
->values
[0].string
.text
, "utf-8"))
414 * Bad character set...
417 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unsupported character set \"%s\"",
418 charset
->values
[0].string
.text
);
419 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
420 "%04X %s Unsupported attributes-charset value \"%s\"",
421 IPP_CHARSET
, con
->http
.hostname
,
422 charset
->values
[0].string
.text
);
423 send_ipp_status(con
, IPP_BAD_REQUEST
,
424 _("Unsupported character set \"%s\""),
425 charset
->values
[0].string
.text
);
427 else if (!charset
|| !language
||
429 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
430 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
431 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
432 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
433 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
436 * Return an error, since attributes-charset,
437 * attributes-natural-language, and printer-uri/job-uri are required
438 * for all operations.
443 cupsdLogMessage(CUPSD_LOG_ERROR
,
444 "Missing attributes-charset attribute");
446 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
447 "%04X %s Missing attributes-charset attribute",
448 IPP_BAD_REQUEST
, con
->http
.hostname
);
453 cupsdLogMessage(CUPSD_LOG_ERROR
,
454 "Missing attributes-natural-language attribute");
456 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
457 "%04X %s Missing attributes-natural-language attribute",
458 IPP_BAD_REQUEST
, con
->http
.hostname
);
463 cupsdLogMessage(CUPSD_LOG_ERROR
,
464 "Missing printer-uri, job-uri, or ppd-name "
467 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
468 "%04X %s Missing printer-uri, job-uri, or ppd-name "
469 "attribute", IPP_BAD_REQUEST
, con
->http
.hostname
);
472 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
474 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
475 cupsdLogMessage(CUPSD_LOG_DEBUG
,
476 "attr \"%s\": group_tag = %x, value_tag = %x",
477 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
480 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
482 send_ipp_status(con
, IPP_BAD_REQUEST
,
483 _("Missing required attributes"));
488 * OK, all the checks pass so far; make sure requesting-user-name is
489 * not "root" from a remote host...
492 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
493 IPP_TAG_NAME
)) != NULL
)
496 * Check for root user...
499 if (!strcmp(username
->values
[0].string
.text
, "root") &&
500 strcasecmp(con
->http
.hostname
, "localhost") &&
501 strcmp(con
->username
, "root"))
504 * Remote unauthenticated user masquerading as local root...
507 _cupsStrFree(username
->values
[0].string
.text
);
508 username
->values
[0].string
.text
= _cupsStrAlloc(RemoteRoot
);
512 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
513 IPP_TAG_INTEGER
)) != NULL
)
514 sub_id
= attr
->values
[0].integer
;
519 * Then try processing the operation...
523 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
524 ippOpString(con
->request
->request
.op
.operation_id
),
525 uri
->values
[0].string
.text
);
527 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
528 ippOpString(con
->request
->request
.op
.operation_id
));
530 switch (con
->request
->request
.op
.operation_id
)
536 case IPP_VALIDATE_JOB
:
537 validate_job(con
, uri
);
540 case IPP_CREATE_JOB
:
541 create_job(con
, uri
);
544 case IPP_SEND_DOCUMENT
:
545 send_document(con
, uri
);
548 case IPP_CANCEL_JOB
:
549 cancel_job(con
, uri
);
552 case IPP_GET_JOB_ATTRIBUTES
:
553 get_job_attrs(con
, uri
);
560 case IPP_GET_PRINTER_ATTRIBUTES
:
561 get_printer_attrs(con
, uri
);
564 case IPP_GET_PRINTER_SUPPORTED_VALUES
:
565 get_printer_supported(con
, uri
);
572 case IPP_RELEASE_JOB
:
573 release_job(con
, uri
);
576 case IPP_RESTART_JOB
:
577 restart_job(con
, uri
);
580 case IPP_PAUSE_PRINTER
:
581 stop_printer(con
, uri
);
584 case IPP_RESUME_PRINTER
:
585 start_printer(con
, uri
);
588 case IPP_PURGE_JOBS
:
589 case IPP_CANCEL_JOBS
:
590 case IPP_CANCEL_MY_JOBS
:
591 cancel_all_jobs(con
, uri
);
594 case IPP_SET_JOB_ATTRIBUTES
:
595 set_job_attrs(con
, uri
);
598 case IPP_SET_PRINTER_ATTRIBUTES
:
599 set_printer_attrs(con
, uri
);
602 case IPP_HOLD_NEW_JOBS
:
603 hold_new_jobs(con
, uri
);
606 case IPP_RELEASE_HELD_NEW_JOBS
:
607 release_held_new_jobs(con
, uri
);
614 case CUPS_GET_DEFAULT
:
618 case CUPS_GET_PRINTERS
:
619 get_printers(con
, 0);
622 case CUPS_GET_CLASSES
:
623 get_printers(con
, CUPS_PRINTER_CLASS
);
626 case CUPS_ADD_PRINTER
:
627 add_printer(con
, uri
);
630 case CUPS_DELETE_PRINTER
:
631 delete_printer(con
, uri
);
634 case CUPS_ADD_CLASS
:
638 case CUPS_DELETE_CLASS
:
639 delete_printer(con
, uri
);
642 case CUPS_ACCEPT_JOBS
:
643 case IPP_ENABLE_PRINTER
:
644 accept_jobs(con
, uri
);
647 case CUPS_REJECT_JOBS
:
648 case IPP_DISABLE_PRINTER
:
649 reject_jobs(con
, uri
);
652 case CUPS_SET_DEFAULT
:
653 set_default(con
, uri
);
656 case CUPS_GET_DEVICES
:
660 case CUPS_GET_DOCUMENT
:
661 get_document(con
, uri
);
676 case CUPS_AUTHENTICATE_JOB
:
677 authenticate_job(con
, uri
);
680 case IPP_CREATE_PRINTER_SUBSCRIPTION
:
681 case IPP_CREATE_JOB_SUBSCRIPTION
:
682 create_subscription(con
, uri
);
685 case IPP_GET_SUBSCRIPTION_ATTRIBUTES
:
686 get_subscription_attrs(con
, sub_id
);
689 case IPP_GET_SUBSCRIPTIONS
:
690 get_subscriptions(con
, uri
);
693 case IPP_RENEW_SUBSCRIPTION
:
694 renew_subscription(con
, sub_id
);
697 case IPP_CANCEL_SUBSCRIPTION
:
698 cancel_subscription(con
, sub_id
);
701 case IPP_GET_NOTIFICATIONS
:
702 get_notifications(con
);
706 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
707 "%04X %s Operation %04X (%s) not supported",
708 IPP_OPERATION_NOT_SUPPORTED
, con
->http
.hostname
,
709 con
->request
->request
.op
.operation_id
,
710 ippOpString(con
->request
->request
.op
.operation_id
));
712 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
713 _("%s not supported"),
715 con
->request
->request
.op
.operation_id
));
725 * Sending data from the scheduler...
728 cupsdLogMessage(con
->response
->request
.status
.status_code
729 >= IPP_BAD_REQUEST
&&
730 con
->response
->request
.status
.status_code
731 != IPP_NOT_FOUND
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
732 "Returning IPP %s for %s (%s) from %s",
733 ippErrorString(con
->response
->request
.status
.status_code
),
734 ippOpString(con
->request
->request
.op
.operation_id
),
735 uri
? uri
->values
[0].string
.text
: "no URI",
738 if (LogLevel
== CUPSD_LOG_DEBUG2
)
739 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
740 "cupsdProcessIPPRequest: ippLength(response)=%ld",
741 (long)ippLength(con
->response
));
743 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp", CUPSD_AUTH_NONE
))
745 #ifdef CUPSD_USE_CHUNKING
747 * Because older versions of CUPS (1.1.17 and older) and some IPP
748 * clients do not implement chunking properly, we cannot use
749 * chunking by default. This may become the default in future
750 * CUPS releases, or we might add a configuration directive for
754 if (con
->http
.version
== HTTP_1_1
)
756 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n") < 0)
759 if (cupsdFlushHeader(con
) < 0)
762 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
765 #endif /* CUPSD_USE_CHUNKING */
767 size_t length
; /* Length of response */
770 length
= ippLength(con
->response
);
772 if (con
->file
>= 0 && !con
->pipe_pid
)
774 struct stat fileinfo
; /* File information */
777 if (!fstat(con
->file
, &fileinfo
))
778 length
+= fileinfo
.st_size
;
781 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
782 CUPS_LLCAST length
) < 0)
785 if (cupsdFlushHeader(con
) < 0)
788 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
789 con
->http
.data_remaining
= length
;
791 if (con
->http
.data_remaining
<= INT_MAX
)
792 con
->http
._data_remaining
= con
->http
.data_remaining
;
794 con
->http
._data_remaining
= INT_MAX
;
797 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
798 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
801 * Tell the caller the response header was sent successfully...
809 * Tell the caller the response header could not be sent...
818 * Sending data from a subprocess like cups-deviced; tell the caller
819 * everything is A-OK so far...
828 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
831 int /* O - 0 on success, -1 on error */
832 cupsdTimeoutJob(cupsd_job_t
*job
) /* I - Job to timeout */
834 cupsd_printer_t
*printer
; /* Destination printer or class */
835 ipp_attribute_t
*attr
; /* job-sheets attribute */
836 int kbytes
; /* Kilobytes in banner */
839 job
->pending_timeout
= 0;
842 * See if we need to add the ending sheet...
845 if (!cupsdLoadJob(job
))
848 printer
= cupsdFindDest(job
->dest
);
849 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
852 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
853 attr
&& attr
->num_values
> 1)
859 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding end banner page \"%s\".",
860 attr
->values
[1].string
.text
);
862 if ((kbytes
= copy_banner(NULL
, job
, attr
->values
[1].string
.text
)) < 0)
865 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
873 * 'accept_jobs()' - Accept print jobs to a printer.
877 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
878 ipp_attribute_t
*uri
) /* I - Printer or class URI */
880 http_status_t status
; /* Policy status */
881 cups_ptype_t dtype
; /* Destination type (printer/class) */
882 cupsd_printer_t
*printer
; /* Printer data */
885 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
886 con
->http
.fd
, uri
->values
[0].string
.text
);
889 * Is the destination valid?
892 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
898 send_ipp_status(con
, IPP_NOT_FOUND
,
899 _("The printer or class was not found."));
907 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
909 send_http_error(con
, status
, printer
);
914 * Accept jobs sent to the printer...
917 printer
->accepting
= 1;
918 printer
->state_message
[0] = '\0';
920 cupsdAddPrinterHistory(printer
);
922 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
923 "Now accepting jobs.");
925 if (dtype
& CUPS_PRINTER_CLASS
)
927 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
929 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
930 printer
->name
, get_username(con
));
934 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
936 cupsdLogMessage(CUPSD_LOG_INFO
,
937 "Printer \"%s\" now accepting jobs (\"%s\").",
938 printer
->name
, get_username(con
));
942 * Everything was ok, so return OK status...
945 con
->response
->request
.status
.status_code
= IPP_OK
;
950 * 'add_class()' - Add a class to the system.
954 add_class(cupsd_client_t
*con
, /* I - Client connection */
955 ipp_attribute_t
*uri
) /* I - URI of class */
957 http_status_t status
; /* Policy status */
958 int i
; /* Looping var */
959 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
960 username
[HTTP_MAX_URI
], /* Username portion of URI */
961 host
[HTTP_MAX_URI
], /* Host portion of URI */
962 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
963 int port
; /* Port portion of URI */
964 cupsd_printer_t
*pclass
, /* Class */
965 *member
; /* Member printer/class */
966 cups_ptype_t dtype
; /* Destination type */
967 ipp_attribute_t
*attr
; /* Printer attribute */
968 int modify
; /* Non-zero if we just modified */
969 char newname
[IPP_MAX_NAME
]; /* New class name */
970 int need_restart_job
; /* Need to restart job? */
973 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
974 con
->http
.fd
, uri
->values
[0].string
.text
);
977 * Do we have a valid URI?
980 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
981 sizeof(scheme
), username
, sizeof(username
), host
,
982 sizeof(host
), &port
, resource
, sizeof(resource
));
985 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
988 * No, return an error...
991 send_ipp_status(con
, IPP_BAD_REQUEST
,
992 _("The printer-uri must be of the form "
993 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
998 * Do we have a valid printer name?
1001 if (!validate_name(resource
+ 9))
1004 * No, return an error...
1007 send_ipp_status(con
, IPP_BAD_REQUEST
,
1008 _("The printer-uri \"%s\" contains invalid characters."),
1009 uri
->values
[0].string
.text
);
1014 * See if the class already exists; if not, create a new class...
1017 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
1020 * Class doesn't exist; see if we have a printer of the same name...
1023 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
1024 !(pclass
->type
& CUPS_PRINTER_DISCOVERED
))
1027 * Yes, return an error...
1030 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1031 _("A printer named \"%s\" already exists"),
1037 * No, check the default policy and then add the class...
1040 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
1042 send_http_error(con
, status
, NULL
);
1046 pclass
= cupsdAddClass(resource
+ 9);
1049 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
1052 * Check the default policy, then rename the implicit class to "AnyClass"
1056 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
1058 send_http_error(con
, status
, NULL
);
1062 if (ImplicitAnyClasses
)
1064 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
1065 cupsdRenamePrinter(pclass
, newname
);
1068 cupsdDeletePrinter(pclass
, 1);
1071 * Add the class as a new local class...
1074 pclass
= cupsdAddClass(resource
+ 9);
1077 else if (pclass
->type
& CUPS_PRINTER_DISCOVERED
)
1080 * Check the default policy, then rename the remote class to "Class"...
1083 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
1085 send_http_error(con
, status
, NULL
);
1089 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
1090 cupsdRenamePrinter(pclass
, newname
);
1093 * Add the class as a new local class...
1096 pclass
= cupsdAddClass(resource
+ 9);
1099 else if ((status
= cupsdCheckPolicy(pclass
->op_policy_ptr
, con
,
1102 send_http_error(con
, status
, pclass
);
1109 * Look for attributes and copy them over as needed...
1112 need_restart_job
= 0;
1114 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
1115 IPP_TAG_TEXT
)) != NULL
)
1116 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
1118 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
1119 IPP_TAG_TEXT
)) != NULL
)
1120 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
1122 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
1123 IPP_TAG_BOOLEAN
)) != NULL
&&
1124 attr
->values
[0].boolean
!= pclass
->accepting
)
1126 cupsdLogMessage(CUPSD_LOG_INFO
,
1127 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
1128 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
1130 pclass
->accepting
= attr
->values
[0].boolean
;
1131 cupsdAddPrinterHistory(pclass
);
1133 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, pclass
, NULL
, "%s accepting jobs.",
1134 pclass
->accepting
? "Now" : "No longer");
1137 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
1138 IPP_TAG_BOOLEAN
)) != NULL
)
1140 if (pclass
->shared
&& !attr
->values
[0].boolean
)
1141 cupsdDeregisterPrinter(pclass
, 1);
1143 cupsdLogMessage(CUPSD_LOG_INFO
,
1144 "Setting %s printer-is-shared to %d (was %d.)",
1145 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
1147 pclass
->shared
= attr
->values
[0].boolean
;
1150 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
1151 IPP_TAG_ENUM
)) != NULL
)
1153 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
1154 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
1156 send_ipp_status(con
, IPP_BAD_REQUEST
,
1157 _("Attempt to set %s printer-state to bad value %d"),
1158 pclass
->name
, attr
->values
[0].integer
);
1162 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
1163 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
1165 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1166 cupsdStopPrinter(pclass
, 0);
1169 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1170 need_restart_job
= 1;
1173 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1174 IPP_TAG_TEXT
)) != NULL
)
1176 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1177 sizeof(pclass
->state_message
));
1178 cupsdAddPrinterHistory(pclass
);
1180 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, pclass
, NULL
, "%s",
1181 pclass
->state_message
);
1183 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1184 IPP_TAG_URI
)) != NULL
)
1187 * Clear the printer array as needed...
1190 need_restart_job
= 1;
1192 if (pclass
->num_printers
> 0)
1194 free(pclass
->printers
);
1195 pclass
->num_printers
= 0;
1199 * Add each printer or class that is listed...
1202 for (i
= 0; i
< attr
->num_values
; i
++)
1205 * Search for the printer or class URI...
1208 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1214 send_ipp_status(con
, IPP_NOT_FOUND
,
1215 _("The printer or class was not found."));
1218 else if (dtype
& CUPS_PRINTER_CLASS
)
1220 send_ipp_status(con
, IPP_BAD_REQUEST
,
1221 _("Nested classes are not allowed"));
1226 * Add it to the class...
1229 cupsdAddPrinterToClass(pclass
, member
);
1233 set_printer_defaults(con
, pclass
);
1235 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1236 IPP_TAG_KEYWORD
)) != NULL
)
1237 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1240 * Update the printer class attributes and return...
1243 cupsdSetPrinterAttrs(pclass
);
1244 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
1246 if (need_restart_job
&& pclass
->job
)
1249 * Reset the current job to a "pending" status...
1252 cupsdSetJobState(pclass
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
1253 "Job restarted because the class was modified.");
1256 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
1260 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
| CUPSD_EVENT_PRINTER_CONFIG
,
1261 pclass
, NULL
, "Class \"%s\" modified by \"%s\".",
1262 pclass
->name
, get_username(con
));
1264 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1265 pclass
->name
, get_username(con
));
1269 cupsdAddPrinterHistory(pclass
);
1271 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
| CUPSD_EVENT_PRINTER_CONFIG
,
1272 pclass
, NULL
, "New class \"%s\" added by \"%s\".",
1273 pclass
->name
, get_username(con
));
1275 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1276 pclass
->name
, get_username(con
));
1279 con
->response
->request
.status
.status_code
= IPP_OK
;
1284 * 'add_file()' - Add a file to a job.
1287 static int /* O - 0 on success, -1 on error */
1288 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1289 cupsd_job_t
*job
, /* I - Job to add to */
1290 mime_type_t
*filetype
, /* I - Type of file */
1291 int compression
) /* I - Compression */
1293 mime_type_t
**filetypes
; /* New filetypes array... */
1294 int *compressions
; /* New compressions array... */
1297 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1298 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1299 "compression=%d)", con
, con
? con
->http
.fd
: -1, job
->id
,
1300 filetype
->super
, filetype
->type
, compression
);
1303 * Add the file to the job...
1306 if (job
->num_files
== 0)
1308 compressions
= (int *)malloc(sizeof(int));
1309 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1313 compressions
= (int *)realloc(job
->compressions
,
1314 (job
->num_files
+ 1) * sizeof(int));
1315 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1316 (job
->num_files
+ 1) *
1317 sizeof(mime_type_t
*));
1320 if (!compressions
|| !filetypes
)
1322 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
1323 "Job aborted because the scheduler ran out of memory.");
1326 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1327 _("Unable to allocate memory for file types"));
1332 job
->compressions
= compressions
;
1333 job
->compressions
[job
->num_files
] = compression
;
1334 job
->filetypes
= filetypes
;
1335 job
->filetypes
[job
->num_files
] = filetype
;
1340 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1347 * 'add_job()' - Add a job to a print queue.
1350 static cupsd_job_t
* /* O - Job object */
1351 add_job(cupsd_client_t
*con
, /* I - Client connection */
1352 cupsd_printer_t
*printer
, /* I - Destination printer */
1353 mime_type_t
*filetype
) /* I - First print file type, if any */
1355 http_status_t status
; /* Policy status */
1356 ipp_attribute_t
*attr
, /* Current attribute */
1357 *auth_info
; /* auth-info attribute */
1358 const char *val
; /* Default option value */
1359 int priority
; /* Job priority */
1360 cupsd_job_t
*job
; /* Current job */
1361 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1362 int kbytes
; /* Size of print file */
1363 int i
; /* Looping var */
1364 int lowerpagerange
; /* Page range bound */
1365 const char *ppd
; /* PPD keyword for media selection */
1366 int exact
; /* Did we have an exact match? */
1367 ipp_attribute_t
*media_col
, /* media-col attribute */
1368 *media_margin
; /* media-*-margin attribute */
1369 ipp_t
*unsup_col
; /* media-col in unsupported response */
1372 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1373 con
, con
->http
.fd
, printer
, printer
->name
,
1374 filetype
, filetype
? filetype
->super
: "none",
1375 filetype
? filetype
->type
: "none");
1378 * Check remote printing to non-shared printer...
1381 if (!printer
->shared
&&
1382 strcasecmp(con
->http
.hostname
, "localhost") &&
1383 strcasecmp(con
->http
.hostname
, ServerName
))
1385 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1386 _("The printer or class is not shared"));
1394 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1396 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1398 send_http_error(con
, status
, printer
);
1401 else if (printer
->num_auth_info_required
== 1 &&
1402 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
1405 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1409 else if (auth_info
&& !con
->http
.tls
&&
1410 !httpAddrLocalhost(con
->http
.hostaddr
))
1413 * Require encryption of auth-info over non-local connections...
1416 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1419 #endif /* HAVE_SSL */
1422 * See if the printer is accepting jobs...
1425 if (!printer
->accepting
)
1427 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1428 _("Destination \"%s\" is not accepting jobs."),
1434 * Validate job template attributes; for now just document-format,
1435 * copies, number-up, and page-ranges...
1438 if (filetype
&& printer
->filetypes
&&
1439 !cupsArrayFind(printer
->filetypes
, filetype
))
1441 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1442 /* MIME media type string */
1445 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1448 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1449 _("Unsupported format \'%s\'"), mimetype
);
1451 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1452 "document-format", NULL
, mimetype
);
1457 if ((attr
= ippFindAttribute(con
->request
, "copies",
1458 IPP_TAG_INTEGER
)) != NULL
)
1460 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1462 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1463 attr
->values
[0].integer
);
1464 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1465 "copies", attr
->values
[0].integer
);
1470 if ((attr
= ippFindAttribute(con
->request
, "job-sheets",
1471 IPP_TAG_ZERO
)) != NULL
)
1473 if (attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1474 attr
->value_tag
!= IPP_TAG_NAME
)
1476 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value type"));
1480 if (attr
->num_values
> 2)
1482 send_ipp_status(con
, IPP_BAD_REQUEST
,
1483 _("Too many job-sheets values (%d > 2)"),
1488 for (i
= 0; i
< attr
->num_values
; i
++)
1489 if (strcmp(attr
->values
[i
].string
.text
, "none") &&
1490 !cupsdFindBanner(attr
->values
[i
].string
.text
))
1492 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value \"%s\""),
1493 attr
->values
[i
].string
.text
);
1498 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1499 IPP_TAG_INTEGER
)) != NULL
)
1501 if (attr
->values
[0].integer
!= 1 &&
1502 attr
->values
[0].integer
!= 2 &&
1503 attr
->values
[0].integer
!= 4 &&
1504 attr
->values
[0].integer
!= 6 &&
1505 attr
->values
[0].integer
!= 9 &&
1506 attr
->values
[0].integer
!= 16)
1508 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1509 attr
->values
[0].integer
);
1510 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1511 "number-up", attr
->values
[0].integer
);
1516 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1517 IPP_TAG_RANGE
)) != NULL
)
1519 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1521 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1522 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1524 send_ipp_status(con
, IPP_BAD_REQUEST
,
1525 _("Bad page-ranges values %d-%d."),
1526 attr
->values
[i
].range
.lower
,
1527 attr
->values
[i
].range
.upper
);
1531 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1536 * Do media selection as needed...
1539 if (!ippFindAttribute(con
->request
, "InputSlot", IPP_TAG_ZERO
) &&
1540 (ppd
= _pwgGetInputSlot(printer
->pwg
, con
->request
, NULL
)) != NULL
)
1541 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "InputSlot", NULL
,
1544 if (!ippFindAttribute(con
->request
, "MediaType", IPP_TAG_ZERO
) &&
1545 (ppd
= _pwgGetMediaType(printer
->pwg
, con
->request
, NULL
)) != NULL
)
1546 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "MediaType", NULL
,
1549 if (!ippFindAttribute(con
->request
, "PageSize", IPP_TAG_ZERO
) &&
1550 (ppd
= _pwgGetPageSize(printer
->pwg
, con
->request
, NULL
, &exact
)) != NULL
)
1552 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "PageSize", NULL
,
1556 (media_col
= ippFindAttribute(con
->request
, "media-col",
1557 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1559 send_ipp_status(con
, IPP_OK_SUBST
, _("Unsupported margins."));
1561 unsup_col
= ippNew();
1562 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1563 "media-bottom-margin",
1564 IPP_TAG_INTEGER
)) != NULL
)
1565 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1566 "media-bottom-margin", media_margin
->values
[0].integer
);
1568 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1569 "media-left-margin",
1570 IPP_TAG_INTEGER
)) != NULL
)
1571 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1572 "media-left-margin", media_margin
->values
[0].integer
);
1574 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1575 "media-right-margin",
1576 IPP_TAG_INTEGER
)) != NULL
)
1577 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1578 "media-right-margin", media_margin
->values
[0].integer
);
1580 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1582 IPP_TAG_INTEGER
)) != NULL
)
1583 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1584 "media-top-margin", media_margin
->values
[0].integer
);
1586 ippAddCollection(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, "media-col",
1588 ippDelete(unsup_col
);
1593 * Make sure we aren't over our limit...
1596 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1599 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1601 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1602 _("Too many active jobs."));
1606 if ((i
= check_quotas(con
, printer
)) < 0)
1608 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1613 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1618 * Create the job and set things up...
1621 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1622 IPP_TAG_INTEGER
)) != NULL
)
1623 priority
= attr
->values
[0].integer
;
1626 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1627 printer
->options
)) != NULL
)
1628 priority
= atoi(val
);
1632 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1636 if (!ippFindAttribute(con
->request
, "job-name", IPP_TAG_NAME
))
1637 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1640 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1642 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1643 _("Unable to add job for destination \"%s\""),
1648 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
1649 CUPS_PRINTER_REMOTE
);
1650 job
->attrs
= con
->request
;
1652 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1654 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1656 add_job_uuid(con
, job
);
1657 apply_printer_defaults(printer
, job
);
1659 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1661 if (con
->username
[0])
1663 cupsdSetString(&job
->username
, con
->username
);
1666 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1670 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1671 "add_job: requesting-user-name=\"%s\"",
1672 attr
->values
[0].string
.text
);
1674 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1677 cupsdSetString(&job
->username
, "anonymous");
1680 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1681 "job-originating-user-name", NULL
, job
->username
);
1684 attr
->group_tag
= IPP_TAG_JOB
;
1685 _cupsStrFree(attr
->name
);
1686 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1689 if (con
->username
[0] || auth_info
)
1691 save_auth_info(con
, job
, auth_info
);
1694 * Remove the auth-info attribute from the attribute data...
1698 ippDeleteAttribute(job
->attrs
, auth_info
);
1701 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1702 IPP_TAG_ZERO
)) != NULL
)
1705 * Request contains a job-originating-host-name attribute; validate it...
1708 if (attr
->value_tag
!= IPP_TAG_NAME
||
1709 attr
->num_values
!= 1 ||
1710 strcmp(con
->http
.hostname
, "localhost"))
1713 * Can't override the value if we aren't connected via localhost.
1714 * Also, we can only have 1 value and it must be a name value.
1717 switch (attr
->value_tag
)
1719 case IPP_TAG_STRING
:
1720 case IPP_TAG_TEXTLANG
:
1721 case IPP_TAG_NAMELANG
:
1724 case IPP_TAG_KEYWORD
:
1726 case IPP_TAG_URISCHEME
:
1727 case IPP_TAG_CHARSET
:
1728 case IPP_TAG_LANGUAGE
:
1729 case IPP_TAG_MIMETYPE
:
1731 * Free old strings...
1734 for (i
= 0; i
< attr
->num_values
; i
++)
1736 _cupsStrFree(attr
->values
[i
].string
.text
);
1737 attr
->values
[i
].string
.text
= NULL
;
1738 if (attr
->values
[i
].string
.charset
)
1740 _cupsStrFree(attr
->values
[i
].string
.charset
);
1741 attr
->values
[i
].string
.charset
= NULL
;
1750 * Use the default connection hostname instead...
1753 attr
->value_tag
= IPP_TAG_NAME
;
1754 attr
->num_values
= 1;
1755 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1758 attr
->group_tag
= IPP_TAG_JOB
;
1763 * No job-originating-host-name attribute, so use the hostname from
1767 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1768 "job-originating-host-name", NULL
, con
->http
.hostname
);
1771 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1773 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1774 "time-at-processing", 0);
1775 attr
->value_tag
= IPP_TAG_NOVALUE
;
1776 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1777 "time-at-completed", 0);
1778 attr
->value_tag
= IPP_TAG_NOVALUE
;
1781 * Add remaining job attributes...
1784 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1785 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1786 "job-state", IPP_JOB_STOPPED
);
1787 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1788 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1789 "job-media-sheets-completed", 0);
1790 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1793 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1794 IPP_TAG_INTEGER
)) != NULL
)
1795 attr
->values
[0].integer
= 0;
1797 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", 0);
1799 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1800 IPP_TAG_KEYWORD
)) == NULL
)
1801 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1804 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1805 printer
->options
)) == NULL
)
1808 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1809 "job-hold-until", NULL
, val
);
1811 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
1814 * Hold job until specified time...
1817 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
1819 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1820 job
->state_value
= IPP_JOB_HELD
;
1822 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1824 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
1825 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1826 job
->state_value
= IPP_JOB_HELD
;
1830 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1831 job
->state_value
= IPP_JOB_PENDING
;
1834 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1838 * Add job sheets options...
1841 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1842 IPP_TAG_ZERO
)) == NULL
)
1844 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1845 "Adding default job-sheets values \"%s,%s\"...",
1846 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1848 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1850 attr
->values
[0].string
.text
= _cupsStrRetain(printer
->job_sheets
[0]);
1851 attr
->values
[1].string
.text
= _cupsStrRetain(printer
->job_sheets
[1]);
1854 job
->job_sheets
= attr
;
1857 * Enforce classification level if set...
1862 cupsdLogMessage(CUPSD_LOG_INFO
,
1863 "Classification=\"%s\", ClassifyOverride=%d",
1864 Classification
? Classification
: "(null)",
1867 if (ClassifyOverride
)
1869 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1870 (attr
->num_values
== 1 ||
1871 !strcmp(attr
->values
[1].string
.text
, "none")))
1874 * Force the leading banner to have the classification on it...
1877 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1879 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1880 "job-sheets=\"%s,none\", "
1881 "job-originating-user-name=\"%s\"",
1882 Classification
, job
->username
);
1884 else if (attr
->num_values
== 2 &&
1885 strcmp(attr
->values
[0].string
.text
,
1886 attr
->values
[1].string
.text
) &&
1887 strcmp(attr
->values
[0].string
.text
, "none") &&
1888 strcmp(attr
->values
[1].string
.text
, "none"))
1891 * Can't put two different security markings on the same document!
1894 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1896 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1897 "job-sheets=\"%s,%s\", "
1898 "job-originating-user-name=\"%s\"",
1899 attr
->values
[0].string
.text
,
1900 attr
->values
[1].string
.text
, job
->username
);
1902 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1903 strcmp(attr
->values
[0].string
.text
, "none") &&
1904 (attr
->num_values
== 1 ||
1905 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1906 strcmp(attr
->values
[1].string
.text
, "none"))))
1908 if (attr
->num_values
== 1)
1909 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1910 "CLASSIFICATION OVERRIDDEN "
1911 "job-sheets=\"%s\", "
1912 "job-originating-user-name=\"%s\"",
1913 attr
->values
[0].string
.text
, job
->username
);
1915 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1916 "CLASSIFICATION OVERRIDDEN "
1917 "job-sheets=\"%s,%s\",fffff "
1918 "job-originating-user-name=\"%s\"",
1919 attr
->values
[0].string
.text
,
1920 attr
->values
[1].string
.text
, job
->username
);
1923 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1924 (attr
->num_values
== 1 ||
1925 strcmp(attr
->values
[1].string
.text
, Classification
)))
1928 * Force the banner to have the classification on it...
1931 if (attr
->num_values
> 1 &&
1932 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1934 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1935 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1939 if (attr
->num_values
== 1 ||
1940 strcmp(attr
->values
[0].string
.text
, "none"))
1941 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1943 if (attr
->num_values
> 1 &&
1944 strcmp(attr
->values
[1].string
.text
, "none"))
1945 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1948 if (attr
->num_values
> 1)
1949 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1950 "CLASSIFICATION FORCED "
1951 "job-sheets=\"%s,%s\", "
1952 "job-originating-user-name=\"%s\"",
1953 attr
->values
[0].string
.text
,
1954 attr
->values
[1].string
.text
, job
->username
);
1956 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1957 "CLASSIFICATION FORCED "
1958 "job-sheets=\"%s\", "
1959 "job-originating-user-name=\"%s\"",
1960 Classification
, job
->username
);
1965 * See if we need to add the starting sheet...
1968 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1970 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding start banner page \"%s\".",
1971 attr
->values
[0].string
.text
);
1973 if ((kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
)) < 0)
1975 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
1976 "Aborting job because the start banner could not be "
1981 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1984 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1985 IPP_TAG_ZERO
)) != NULL
)
1989 * Fill in the response info...
1992 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1993 LocalPort
, job
->id
);
1995 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1998 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
2000 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
2002 add_job_state_reasons(con
, job
);
2004 con
->response
->request
.status
.status_code
= IPP_OK
;
2007 * Add any job subscriptions...
2010 add_job_subscriptions(con
, job
);
2013 * Set all but the first two attributes to the job attributes group...
2016 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
2017 attr
->group_tag
= IPP_TAG_JOB
;
2020 * Fire the "job created" event...
2023 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
2026 * Return the new job...
2034 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
2035 * upon the job and printer state...
2039 add_job_state_reasons(
2040 cupsd_client_t
*con
, /* I - Client connection */
2041 cupsd_job_t
*job
) /* I - Job info */
2043 cupsd_printer_t
*dest
; /* Destination printer */
2046 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
2047 con
, con
->http
.fd
, job
? job
->id
: 0);
2049 switch (job
? job
->state_value
: IPP_JOB_CANCELED
)
2051 case IPP_JOB_PENDING
:
2052 dest
= cupsdFindDest(job
->dest
);
2054 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
2055 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2056 "job-state-reasons", NULL
, "printer-stopped");
2058 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2059 "job-state-reasons", NULL
, "none");
2063 if (ippFindAttribute(job
->attrs
, "job-hold-until",
2064 IPP_TAG_KEYWORD
) != NULL
||
2065 ippFindAttribute(job
->attrs
, "job-hold-until",
2066 IPP_TAG_NAME
) != NULL
)
2067 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2068 "job-state-reasons", NULL
, "job-hold-until-specified");
2070 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2071 "job-state-reasons", NULL
, "job-incoming");
2074 case IPP_JOB_PROCESSING
:
2075 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2076 "job-state-reasons", NULL
, "job-printing");
2079 case IPP_JOB_STOPPED
:
2080 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2081 "job-state-reasons", NULL
, "job-stopped");
2084 case IPP_JOB_CANCELED
:
2085 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2086 "job-state-reasons", NULL
, "job-canceled-by-user");
2089 case IPP_JOB_ABORTED
:
2090 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2091 "job-state-reasons", NULL
, "aborted-by-system");
2094 case IPP_JOB_COMPLETED
:
2095 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2096 "job-state-reasons", NULL
, "job-completed-successfully");
2103 * 'add_job_subscriptions()' - Add any subscriptions for a job.
2107 add_job_subscriptions(
2108 cupsd_client_t
*con
, /* I - Client connection */
2109 cupsd_job_t
*job
) /* I - Newly created job */
2111 int i
; /* Looping var */
2112 ipp_attribute_t
*prev
, /* Previous attribute */
2113 *next
, /* Next attribute */
2114 *attr
; /* Current attribute */
2115 cupsd_subscription_t
*sub
; /* Subscription object */
2116 const char *recipient
, /* notify-recipient-uri */
2117 *pullmethod
; /* notify-pull-method */
2118 ipp_attribute_t
*user_data
; /* notify-user-data */
2119 int interval
; /* notify-time-interval */
2120 unsigned mask
; /* notify-events */
2124 * Find the first subscription group attribute; return if we have
2128 for (attr
= job
->attrs
->attrs
; attr
; attr
= attr
->next
)
2129 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
2136 * Process the subscription attributes in the request...
2145 mask
= CUPSD_EVENT_NONE
;
2147 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
2149 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
2150 attr
->value_tag
== IPP_TAG_URI
)
2153 * Validate the recipient scheme against the ServerBin/notifier
2157 char notifier
[1024], /* Notifier filename */
2158 scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2159 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2160 host
[HTTP_MAX_URI
], /* Host portion of URI */
2161 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2162 int port
; /* Port portion of URI */
2165 recipient
= attr
->values
[0].string
.text
;
2167 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
2168 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
2169 host
, sizeof(host
), &port
,
2170 resource
, sizeof(resource
)) < HTTP_URI_OK
)
2172 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2173 _("Bad notify-recipient-uri URI \"%s\""), recipient
);
2174 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2175 "notify-status-code", IPP_URI_SCHEME
);
2179 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
2181 if (access(notifier
, X_OK
))
2183 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2184 _("notify-recipient-uri URI \"%s\" uses unknown "
2185 "scheme"), recipient
);
2186 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2187 "notify-status-code", IPP_URI_SCHEME
);
2191 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
2193 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2194 _("notify-recipient-uri URI \"%s\" is already used"),
2196 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2197 "notify-status-code", IPP_ATTRIBUTES
);
2201 else if (!strcmp(attr
->name
, "notify-pull-method") &&
2202 attr
->value_tag
== IPP_TAG_KEYWORD
)
2204 pullmethod
= attr
->values
[0].string
.text
;
2206 if (strcmp(pullmethod
, "ippget"))
2208 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2209 _("Bad notify-pull-method \"%s\""), pullmethod
);
2210 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2211 "notify-status-code", IPP_ATTRIBUTES
);
2215 else if (!strcmp(attr
->name
, "notify-charset") &&
2216 attr
->value_tag
== IPP_TAG_CHARSET
&&
2217 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
2218 strcmp(attr
->values
[0].string
.text
, "utf-8"))
2220 send_ipp_status(con
, IPP_CHARSET
,
2221 _("Character set \"%s\" not supported"),
2222 attr
->values
[0].string
.text
);
2225 else if (!strcmp(attr
->name
, "notify-natural-language") &&
2226 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
2227 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
2229 send_ipp_status(con
, IPP_CHARSET
,
2230 _("Language \"%s\" not supported"),
2231 attr
->values
[0].string
.text
);
2234 else if (!strcmp(attr
->name
, "notify-user-data") &&
2235 attr
->value_tag
== IPP_TAG_STRING
)
2237 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
2239 send_ipp_status(con
, IPP_REQUEST_VALUE
,
2240 _("The notify-user-data value is too large "
2241 "(%d > 63 octets)"),
2242 attr
->values
[0].unknown
.length
);
2248 else if (!strcmp(attr
->name
, "notify-events") &&
2249 attr
->value_tag
== IPP_TAG_KEYWORD
)
2251 for (i
= 0; i
< attr
->num_values
; i
++)
2252 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
2254 else if (!strcmp(attr
->name
, "notify-lease-duration"))
2256 send_ipp_status(con
, IPP_BAD_REQUEST
,
2257 _("The notify-lease-duration attribute cannot be "
2258 "used with job subscriptions."));
2261 else if (!strcmp(attr
->name
, "notify-time-interval") &&
2262 attr
->value_tag
== IPP_TAG_INTEGER
)
2263 interval
= attr
->values
[0].integer
;
2268 if (!recipient
&& !pullmethod
)
2271 if (mask
== CUPSD_EVENT_NONE
)
2272 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2274 if ((sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
,
2275 recipient
, 0)) != NULL
)
2277 sub
->interval
= interval
;
2279 cupsdSetString(&sub
->owner
, job
->username
);
2283 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2284 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2285 sub
->user_data_len
);
2288 ippAddSeparator(con
->response
);
2289 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2290 "notify-subscription-id", sub
->id
);
2292 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
2300 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
2303 * Remove all of the subscription attributes from the job request...
2305 * TODO: Optimize this since subscription groups have to come at the
2306 * end of the request...
2309 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2313 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2314 attr
->group_tag
== IPP_TAG_ZERO
)
2317 * Free and remove this attribute...
2325 job
->attrs
->attrs
= next
;
2331 job
->attrs
->last
= prev
;
2332 job
->attrs
->current
= prev
;
2337 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2339 * See RFC 4122 for the definition of UUIDs and the format.
2343 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
2344 cupsd_job_t
*job
) /* I - Job */
2346 char uuid
[1024]; /* job-uuid string */
2347 _cups_md5_state_t md5state
; /* MD5 state */
2348 unsigned char md5sum
[16]; /* MD5 digest/sum */
2352 * First see if the job already has a job-uuid attribute; if so, return...
2355 if (ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2359 * No job-uuid attribute, so build a version 3 UUID with the local job
2360 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
2361 * the ServerName, server name and port that the client connected to,
2362 * and local job ID...
2365 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
2366 con
->serverport
, job
->id
);
2368 _cupsMD5Init(&md5state
);
2369 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
2370 _cupsMD5Finish(&md5state
, md5sum
);
2373 * Format the UUID URI using the MD5 sum and job ID.
2376 snprintf(uuid
, sizeof(uuid
),
2377 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2378 "%02x%02x%02x%02x%02x%02x",
2379 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
2380 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
2381 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
2382 md5sum
[14], md5sum
[15]);
2384 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
2389 * 'add_printer()' - Add a printer to the system.
2393 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2394 ipp_attribute_t
*uri
) /* I - URI of printer */
2396 http_status_t status
; /* Policy status */
2397 int i
; /* Looping var */
2398 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2399 username
[HTTP_MAX_URI
], /* Username portion of URI */
2400 host
[HTTP_MAX_URI
], /* Host portion of URI */
2401 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2402 int port
; /* Port portion of URI */
2403 cupsd_printer_t
*printer
; /* Printer/class */
2404 ipp_attribute_t
*attr
; /* Printer attribute */
2405 cups_file_t
*fp
; /* Script/PPD file */
2406 char line
[1024]; /* Line from file... */
2407 char srcfile
[1024], /* Source Script/PPD file */
2408 dstfile
[1024]; /* Destination Script/PPD file */
2409 int modify
; /* Non-zero if we are modifying */
2410 char newname
[IPP_MAX_NAME
]; /* New printer name */
2411 int changed_driver
, /* Changed the PPD/interface script? */
2412 need_restart_job
, /* Need to restart job? */
2413 set_device_uri
, /* Did we set the device URI? */
2414 set_port_monitor
; /* Did we set the port monitor? */
2417 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2418 con
->http
.fd
, uri
->values
[0].string
.text
);
2421 * Do we have a valid URI?
2424 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2425 sizeof(scheme
), username
, sizeof(username
), host
,
2426 sizeof(host
), &port
, resource
, sizeof(resource
));
2428 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2431 * No, return an error...
2434 send_ipp_status(con
, IPP_BAD_REQUEST
,
2435 _("The printer-uri must be of the form "
2436 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2441 * Do we have a valid printer name?
2444 if (!validate_name(resource
+ 10))
2447 * No, return an error...
2450 send_ipp_status(con
, IPP_BAD_REQUEST
,
2451 _("The printer-uri \"%s\" contains invalid characters."),
2452 uri
->values
[0].string
.text
);
2457 * See if the printer already exists; if not, create a new printer...
2460 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2463 * Printer doesn't exist; see if we have a class of the same name...
2466 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2467 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
2470 * Yes, return an error...
2473 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2474 _("A class named \"%s\" already exists"),
2480 * No, check the default policy then add the printer...
2483 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2485 send_http_error(con
, status
, NULL
);
2489 printer
= cupsdAddPrinter(resource
+ 10);
2492 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2495 * Check the default policy, then rename the implicit printer to
2496 * "AnyPrinter" or delete it...
2499 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2501 send_http_error(con
, status
, NULL
);
2505 if (ImplicitAnyClasses
)
2507 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2508 cupsdRenamePrinter(printer
, newname
);
2511 cupsdDeletePrinter(printer
, 1);
2514 * Add the printer as a new local printer...
2517 printer
= cupsdAddPrinter(resource
+ 10);
2520 else if (printer
->type
& CUPS_PRINTER_DISCOVERED
)
2523 * Check the default policy, then rename the remote printer to
2524 * "Printer@server"...
2527 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2529 send_http_error(con
, status
, NULL
);
2533 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2535 cupsdRenamePrinter(printer
, newname
);
2538 * Add the printer as a new local printer...
2541 printer
= cupsdAddPrinter(resource
+ 10);
2544 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2547 send_http_error(con
, status
, printer
);
2554 * Look for attributes and copy them over as needed...
2558 need_restart_job
= 0;
2560 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2561 IPP_TAG_TEXT
)) != NULL
)
2562 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2564 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2565 IPP_TAG_TEXT
)) != NULL
)
2566 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2570 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2571 IPP_TAG_URI
)) != NULL
)
2574 * Do we have a valid device URI?
2577 http_uri_status_t uri_status
; /* URI separation status */
2578 char old_device_uri
[1024];
2579 /* Old device URI */
2582 need_restart_job
= 1;
2584 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2585 attr
->values
[0].string
.text
,
2586 scheme
, sizeof(scheme
),
2587 username
, sizeof(username
),
2588 host
, sizeof(host
), &port
,
2589 resource
, sizeof(resource
));
2591 if (uri_status
< HTTP_URI_OK
)
2593 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\""),
2594 attr
->values
[0].string
.text
);
2595 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2596 "add_printer: httpSeparateURI returned %d", uri_status
);
2600 if (!strcmp(scheme
, "file"))
2603 * See if the administrator has enabled file devices...
2606 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2609 * File devices are disabled and the URL is not file:/dev/null...
2612 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2613 _("File device URIs have been disabled! "
2614 "To enable, see the FileDevice directive in "
2615 "\"%s/cupsd.conf\"."),
2623 * See if the backend exists and is executable...
2626 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2627 if (access(srcfile
, X_OK
))
2630 * Could not find device in list!
2633 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri scheme \"%s\""),
2639 if (printer
->sanitized_device_uri
)
2640 strlcpy(old_device_uri
, printer
->sanitized_device_uri
,
2641 sizeof(old_device_uri
));
2643 old_device_uri
[0] = '\0';
2645 cupsdSetDeviceURI(printer
, attr
->values
[0].string
.text
);
2647 cupsdLogMessage(CUPSD_LOG_INFO
,
2648 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2649 printer
->name
, printer
->sanitized_device_uri
,
2655 set_port_monitor
= 0;
2657 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2658 IPP_TAG_NAME
)) != NULL
)
2660 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2663 need_restart_job
= 1;
2665 supported
= ippFindAttribute(printer
->ppd_attrs
, "port-monitor-supported",
2669 for (i
= 0; i
< supported
->num_values
; i
++)
2670 if (!strcmp(supported
->values
[i
].string
.text
,
2671 attr
->values
[0].string
.text
))
2675 if (!supported
|| i
>= supported
->num_values
)
2677 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\""),
2678 attr
->values
[0].string
.text
);
2682 cupsdLogMessage(CUPSD_LOG_INFO
,
2683 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2684 printer
->name
, attr
->values
[0].string
.text
,
2685 printer
->port_monitor
? printer
->port_monitor
: "none");
2687 if (strcmp(attr
->values
[0].string
.text
, "none"))
2688 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2690 cupsdClearString(&printer
->port_monitor
);
2692 set_port_monitor
= 1;
2695 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2696 IPP_TAG_BOOLEAN
)) != NULL
&&
2697 attr
->values
[0].boolean
!= printer
->accepting
)
2699 cupsdLogMessage(CUPSD_LOG_INFO
,
2700 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2701 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2703 printer
->accepting
= attr
->values
[0].boolean
;
2704 cupsdAddPrinterHistory(printer
);
2706 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
2707 "%s accepting jobs.",
2708 printer
->accepting
? "Now" : "No longer");
2711 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2712 IPP_TAG_BOOLEAN
)) != NULL
)
2714 if (printer
->shared
&& !attr
->values
[0].boolean
)
2715 cupsdDeregisterPrinter(printer
, 1);
2717 cupsdLogMessage(CUPSD_LOG_INFO
,
2718 "Setting %s printer-is-shared to %d (was %d.)",
2719 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2721 printer
->shared
= attr
->values
[0].boolean
;
2724 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2725 IPP_TAG_ENUM
)) != NULL
)
2727 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2728 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2730 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d"),
2731 attr
->values
[0].integer
);
2735 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2736 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2738 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2739 cupsdStopPrinter(printer
, 0);
2742 need_restart_job
= 1;
2743 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2747 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2748 IPP_TAG_TEXT
)) != NULL
)
2750 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2751 sizeof(printer
->state_message
));
2752 cupsdAddPrinterHistory(printer
);
2754 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
, "%s",
2755 printer
->state_message
);
2758 if ((attr
= ippFindAttribute(con
->request
, "printer-state-reasons",
2759 IPP_TAG_KEYWORD
)) != NULL
)
2761 if (attr
->num_values
>
2762 (int)(sizeof(printer
->reasons
) / sizeof(printer
->reasons
[0])))
2764 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2765 _("Too many printer-state-reasons values (%d > %d)"),
2767 (int)(sizeof(printer
->reasons
) /
2768 sizeof(printer
->reasons
[0])));
2772 for (i
= 0; i
< printer
->num_reasons
; i
++)
2773 _cupsStrFree(printer
->reasons
[i
]);
2775 printer
->num_reasons
= 0;
2776 for (i
= 0; i
< attr
->num_values
; i
++)
2778 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
2781 printer
->reasons
[printer
->num_reasons
] =
2782 _cupsStrRetain(attr
->values
[i
].string
.text
);
2783 printer
->num_reasons
++;
2785 if (!strcmp(attr
->values
[i
].string
.text
, "paused") &&
2786 printer
->state
!= IPP_PRINTER_STOPPED
)
2788 cupsdLogMessage(CUPSD_LOG_INFO
,
2789 "Setting %s printer-state to %d (was %d.)",
2790 printer
->name
, IPP_PRINTER_STOPPED
, printer
->state
);
2791 cupsdStopPrinter(printer
, 0);
2795 if (PrintcapFormat
== PRINTCAP_PLIST
)
2796 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2798 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
2799 "Printer \"%s\" state changed.", printer
->name
);
2802 set_printer_defaults(con
, printer
);
2804 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2805 IPP_TAG_KEYWORD
)) != NULL
)
2806 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2809 * See if we have all required attributes...
2812 if (!printer
->device_uri
)
2813 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2816 * See if we have an interface script or PPD file attached to the request...
2821 need_restart_job
= 1;
2824 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2826 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2829 * Yes; get the first line from it...
2833 cupsFileGets(fp
, line
, sizeof(line
));
2837 * Then see what kind of file it is...
2840 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2843 if (!strncmp(line
, "*PPD-Adobe", 10))
2846 * The new file is a PPD file, so remove any old interface script
2847 * that might be lying around...
2855 * This must be an interface script, so move the file over to the
2856 * interfaces directory and make it executable...
2859 if (copy_file(srcfile
, dstfile
))
2861 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2862 _("Unable to copy interface script - %s"),
2867 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2868 "Copied interface script successfully");
2869 chmod(dstfile
, 0755);
2872 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2875 if (!strncmp(line
, "*PPD-Adobe", 10))
2878 * The new file is a PPD file, so move the file over to the
2879 * ppd directory and make it readable by all...
2882 if (copy_file(srcfile
, dstfile
))
2884 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2885 _("Unable to copy PPD file - %s"),
2890 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2891 "Copied PPD file successfully");
2892 chmod(dstfile
, 0644);
2897 * This must be an interface script, so remove any old PPD file that
2898 * may be lying around...
2905 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2906 IPP_TAG_NAME
)) != NULL
)
2908 need_restart_job
= 1;
2911 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2914 * Raw driver, remove any existing PPD or interface script files.
2917 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2921 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2931 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2935 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2938 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2940 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file"));
2944 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2945 "Copied PPD file successfully");
2946 chmod(dstfile
, 0644);
2953 * If we changed the PPD/interface script, then remove the printer's cache
2957 char cache_name
[1024]; /* Cache filename for printer attrs */
2959 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.ipp2", CacheDir
,
2963 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.pwg", CacheDir
,
2969 * (Re)register color profiles...
2974 apple_unregister_profiles(printer
);
2975 apple_register_profiles(printer
);
2977 #endif /* __APPLE__ */
2981 * If we set the device URI but not the port monitor, check which port
2982 * monitor to use by default...
2985 if (set_device_uri
&& !set_port_monitor
)
2987 ppd_file_t
*ppd
; /* PPD file */
2988 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2991 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2992 sizeof(scheme
), username
, sizeof(username
), host
,
2993 sizeof(host
), &port
, resource
, sizeof(resource
));
2995 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2997 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2999 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
3001 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
3002 if (!strcmp(scheme
, ppdattr
->spec
))
3004 cupsdLogMessage(CUPSD_LOG_INFO
,
3005 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
3006 printer
->name
, ppdattr
->value
,
3007 printer
->port_monitor
? printer
->port_monitor
3010 if (strcmp(ppdattr
->value
, "none"))
3011 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
3013 cupsdClearString(&printer
->port_monitor
);
3023 * Update the printer attributes and return...
3026 cupsdSetPrinterAttrs(printer
);
3027 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3029 if (need_restart_job
&& printer
->job
)
3032 * Restart the current job...
3035 cupsdSetJobState(printer
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
3036 "Job restarted because the printer was modified.");
3039 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3043 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
| CUPSD_EVENT_PRINTER_CONFIG
,
3044 printer
, NULL
, "Printer \"%s\" modified by \"%s\".",
3045 printer
->name
, get_username(con
));
3047 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
3048 printer
->name
, get_username(con
));
3052 cupsdAddPrinterHistory(printer
);
3054 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
| CUPSD_EVENT_PRINTER_CONFIG
,
3055 printer
, NULL
, "New printer \"%s\" added by \"%s\".",
3056 printer
->name
, get_username(con
));
3058 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
3059 printer
->name
, get_username(con
));
3062 con
->response
->request
.status
.status_code
= IPP_OK
;
3067 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
3068 * based upon the printer state...
3072 add_printer_state_reasons(
3073 cupsd_client_t
*con
, /* I - Client connection */
3074 cupsd_printer_t
*p
) /* I - Printer info */
3076 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3077 "add_printer_state_reasons(%p[%d], %p[%s])",
3078 con
, con
->http
.fd
, p
, p
->name
);
3080 if (p
->num_reasons
== 0)
3081 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3082 "printer-state-reasons", NULL
, "none");
3084 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3085 "printer-state-reasons", p
->num_reasons
, NULL
,
3086 (const char * const *)p
->reasons
);
3091 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
3092 * the specified printer or class.
3096 add_queued_job_count(
3097 cupsd_client_t
*con
, /* I - Client connection */
3098 cupsd_printer_t
*p
) /* I - Printer or class */
3100 int count
; /* Number of jobs on destination */
3103 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
3104 con
, con
->http
.fd
, p
, p
->name
);
3106 count
= cupsdGetPrinterJobCount(p
->name
);
3108 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3109 "queued-job-count", count
);
3115 * 'apple_init_profile()' - Initialize a color profile.
3120 ppd_file_t
*ppd
, /* I - PPD file */
3121 cups_array_t
*languages
, /* I - Languages in the PPD file */
3122 CMDeviceProfileInfo
*profile
, /* I - Profile record */
3123 unsigned id
, /* I - Profile ID */
3124 const char *name
, /* I - Profile name */
3125 const char *text
, /* I - Profile UI text */
3126 const char *iccfile
) /* I - ICC filename */
3128 char url
[1024]; /* URL for profile filename */
3129 CFMutableDictionaryRef dict
; /* Dictionary for name */
3130 char *language
; /* Current language */
3131 ppd_attr_t
*attr
; /* Profile attribute */
3132 CFStringRef cflang
, /* Language string */
3133 cftext
; /* Localized text */
3137 * Build the profile name dictionary...
3140 dict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3141 &kCFTypeDictionaryKeyCallBacks
,
3142 &kCFTypeDictionaryValueCallBacks
);
3144 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, text
,
3145 kCFStringEncodingUTF8
);
3149 CFDictionarySetValue(dict
, CFSTR("en"), cftext
);
3156 * Find localized names for the color profiles...
3159 cupsArraySave(ppd
->sorted_attrs
);
3161 for (language
= (char *)cupsArrayFirst(languages
);
3163 language
= (char *)cupsArrayNext(languages
))
3167 if ((attr
= _ppdLocalizedAttr(ppd
, "cupsICCProfile", name
,
3169 attr
= _ppdLocalizedAttr(ppd
, "APTiogaProfile", name
, language
);
3172 attr
= _ppdLocalizedAttr(ppd
, "ColorModel", name
, language
);
3174 if (attr
&& attr
->text
[0])
3176 cflang
= CFStringCreateWithCString(kCFAllocatorDefault
, language
,
3177 kCFStringEncodingUTF8
);
3178 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, attr
->text
,
3179 kCFStringEncodingUTF8
);
3181 if (cflang
&& cftext
)
3182 CFDictionarySetValue(dict
, cflang
, cftext
);
3192 cupsArrayRestore(ppd
->sorted_attrs
);
3196 * Fill in the profile data...
3200 httpAssembleURI(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "file", NULL
, "", 0,
3203 profile
->dataVersion
= cmDeviceProfileInfoVersion1
;
3204 profile
->profileID
= id
;
3205 profile
->profileLoc
.locType
= iccfile
? cmPathBasedProfile
: cmNoProfileBase
;
3206 profile
->profileName
= dict
;
3209 strlcpy(profile
->profileLoc
.u
.pathLoc
.path
, iccfile
,
3210 sizeof(profile
->profileLoc
.u
.pathLoc
.path
));
3215 * 'apple_register_profiles()' - Register color profiles for a printer.
3219 apple_register_profiles(
3220 cupsd_printer_t
*p
) /* I - Printer */
3222 int i
; /* Looping var */
3223 char ppdfile
[1024], /* PPD filename */
3224 iccfile
[1024], /* ICC filename */
3225 selector
[PPD_MAX_NAME
];
3226 /* Profile selection string */
3227 ppd_file_t
*ppd
; /* PPD file */
3228 ppd_attr_t
*attr
, /* Profile attributes */
3229 *profileid_attr
,/* cupsProfileID attribute */
3230 *q1_attr
, /* ColorModel (or other) qualifier */
3231 *q2_attr
, /* MediaType (or other) qualifier */
3232 *q3_attr
; /* Resolution (or other) qualifier */
3233 char q_keyword
[PPD_MAX_NAME
];
3234 /* Qualifier keyword */
3235 const char *q1_choice
, /* ColorModel (or other) choice */
3236 *q2_choice
, /* MediaType (or other) choice */
3237 *q3_choice
; /* Resolution (or other) choice */
3238 const char *profile_key
; /* Profile keyword */
3239 ppd_option_t
*cm_option
; /* Color model option */
3240 ppd_choice_t
*cm_choice
; /* Color model choice */
3241 int num_profiles
; /* Number of profiles */
3242 CMError error
; /* Last error */
3243 unsigned device_id
, /* Printer device ID */
3244 profile_id
, /* Profile ID */
3245 default_profile_id
= 0;
3246 /* Default profile ID */
3247 CFMutableDictionaryRef device_name
; /* Printer device name dictionary */
3248 CFStringRef printer_name
; /* Printer name string */
3249 CMDeviceScope scope
= /* Scope of the registration */
3251 kCFPreferencesAnyUser
,
3252 kCFPreferencesCurrentHost
3254 CMDeviceProfileArrayPtr profiles
; /* Profiles */
3255 CMDeviceProfileInfo
*profile
; /* Current profile */
3256 cups_array_t
*languages
; /* Languages array */
3260 * Make sure ColorSync is available...
3263 if (CMRegisterColorDevice
== NULL
)
3267 * Try opening the PPD file for this printer...
3270 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3271 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
3275 * See if we have any profiles...
3278 if ((attr
= ppdFindAttr(ppd
, "APTiogaProfile", NULL
)) != NULL
)
3279 profile_key
= "APTiogaProfile";
3282 attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
3283 profile_key
= "cupsICCProfile";
3286 for (num_profiles
= 0; attr
; attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3287 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3289 if (attr
->value
[0] != '/')
3290 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3293 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3295 if (access(iccfile
, 0))
3303 * If we have profiles, add them...
3306 if (num_profiles
> 0)
3308 if (profile_key
[0] == 'A')
3311 * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile
3315 if ((attr
= ppdFindAttr(ppd
, "DefaultAPTiogaProfile", NULL
)) != NULL
&&
3317 default_profile_id
= atoi(attr
->value
);
3319 q1_choice
= q2_choice
= q3_choice
= NULL
;
3324 * For CUPS PPDs, figure out the default profile selector values...
3327 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier1", NULL
)) != NULL
&&
3328 attr
->value
&& attr
->value
[0])
3330 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3331 q1_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3333 else if ((q1_attr
= ppdFindAttr(ppd
, "DefaultColorModel", NULL
)) == NULL
)
3334 q1_attr
= ppdFindAttr(ppd
, "DefaultColorSpace", NULL
);
3336 if (q1_attr
&& q1_attr
->value
&& q1_attr
->value
[0])
3337 q1_choice
= q1_attr
->value
;
3341 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier2", NULL
)) != NULL
&&
3342 attr
->value
&& attr
->value
[0])
3344 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3345 q2_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3348 q2_attr
= ppdFindAttr(ppd
, "DefaultMediaType", NULL
);
3350 if (q2_attr
&& q2_attr
->value
&& q2_attr
->value
[0])
3351 q2_choice
= q2_attr
->value
;
3355 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier3", NULL
)) != NULL
&&
3356 attr
->value
&& attr
->value
[0])
3358 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3359 q3_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3362 q3_attr
= ppdFindAttr(ppd
, "DefaultResolution", NULL
);
3364 if (q3_attr
&& q3_attr
->value
&& q3_attr
->value
[0])
3365 q3_choice
= q3_attr
->value
;
3371 * Build the array of profiles...
3373 * Note: This calloc actually requests slightly more memory than needed.
3376 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3378 cupsdLogMessage(CUPSD_LOG_ERROR
,
3379 "Unable to allocate memory for %d profiles",
3385 profiles
->profileCount
= num_profiles
;
3386 languages
= _ppdGetLanguages(ppd
);
3388 for (profile
= profiles
->profiles
,
3389 attr
= ppdFindAttr(ppd
, profile_key
, NULL
);
3391 attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3392 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3395 * Add this profile...
3398 if (attr
->value
[0] != '/')
3399 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3402 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3404 if (access(iccfile
, 0))
3407 if (profile_key
[0] == 'c')
3409 cupsArraySave(ppd
->sorted_attrs
);
3411 if ((profileid_attr
= ppdFindAttr(ppd
, "cupsProfileID",
3412 attr
->spec
)) != NULL
&&
3413 profileid_attr
->value
&& isdigit(profileid_attr
->value
[0] & 255))
3414 profile_id
= (unsigned)strtoul(profileid_attr
->value
, NULL
, 10);
3416 profile_id
= _ppdHashName(attr
->spec
);
3418 cupsArrayRestore(ppd
->sorted_attrs
);
3421 profile_id
= atoi(attr
->spec
);
3423 apple_init_profile(ppd
, languages
, profile
, profile_id
, attr
->spec
,
3424 attr
->text
[0] ? attr
->text
: attr
->spec
, iccfile
);
3429 * See if this is the default profile...
3432 if (!default_profile_id
)
3438 snprintf(selector
, sizeof(selector
), "%s.%s.%s",
3439 q1_choice
, q2_choice
, q3_choice
);
3440 if (!strcmp(selector
, attr
->spec
))
3441 default_profile_id
= profile_id
;
3444 if (!default_profile_id
)
3446 snprintf(selector
, sizeof(selector
), "%s.%s.", q1_choice
,
3448 if (!strcmp(selector
, attr
->spec
))
3449 default_profile_id
= profile_id
;
3453 if (!default_profile_id
&& q3_choice
)
3455 snprintf(selector
, sizeof(selector
), "%s..%s", q1_choice
,
3457 if (!strcmp(selector
, attr
->spec
))
3458 default_profile_id
= profile_id
;
3461 if (!default_profile_id
)
3463 snprintf(selector
, sizeof(selector
), "%s..", q1_choice
);
3464 if (!strcmp(selector
, attr
->spec
))
3465 default_profile_id
= profile_id
;
3470 _ppdFreeLanguages(languages
);
3472 else if ((cm_option
= ppdFindOption(ppd
, "ColorModel")) != NULL
)
3475 * Extract profiles from ColorModel option...
3478 const char *profile_name
; /* Name of generic profile */
3481 num_profiles
= cm_option
->num_choices
;
3483 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3485 cupsdLogMessage(CUPSD_LOG_ERROR
,
3486 "Unable to allocate memory for %d profiles",
3492 profiles
->profileCount
= num_profiles
;
3494 for (profile
= profiles
->profiles
, i
= cm_option
->num_choices
,
3495 cm_choice
= cm_option
->choices
;
3497 i
--, cm_choice
++, profile
++)
3499 if (!strcmp(cm_choice
->choice
, "Gray") ||
3500 !strcmp(cm_choice
->choice
, "Black"))
3501 profile_name
= "Gray";
3502 else if (!strcmp(cm_choice
->choice
, "RGB") ||
3503 !strcmp(cm_choice
->choice
, "CMY"))
3504 profile_name
= "RGB";
3505 else if (!strcmp(cm_choice
->choice
, "CMYK") ||
3506 !strcmp(cm_choice
->choice
, "KCMY"))
3507 profile_name
= "CMYK";
3509 profile_name
= "DeviceN";
3511 snprintf(selector
, sizeof(selector
), "%s..", profile_name
);
3512 profile_id
= _ppdHashName(selector
);
3514 apple_init_profile(ppd
, NULL
, profile
, profile_id
, cm_choice
->choice
,
3515 cm_choice
->text
, NULL
);
3517 if (cm_choice
->marked
)
3518 default_profile_id
= profile_id
;
3524 * Use the default colorspace...
3527 attr
= ppdFindAttr(ppd
, "DefaultColorSpace", NULL
);
3529 num_profiles
= (attr
&& ppd
->colorspace
== PPD_CS_GRAY
) ? 1 : 2;
3531 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3533 cupsdLogMessage(CUPSD_LOG_ERROR
,
3534 "Unable to allocate memory for %d profiles",
3540 profiles
->profileCount
= num_profiles
;
3542 apple_init_profile(ppd
, NULL
, profiles
->profiles
, _ppdHashName("Gray.."),
3543 "Gray", "Gray", NULL
);
3545 switch (ppd
->colorspace
)
3549 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3550 _ppdHashName("RGB.."), "RGB", "RGB", NULL
);
3554 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3555 _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL
);
3563 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3564 _ppdHashName("DeviceN.."), "DeviceN", "DeviceN",
3570 if (num_profiles
> 0)
3573 * Make sure we have a default profile ID...
3576 if (!default_profile_id
)
3577 default_profile_id
= profiles
->profiles
[num_profiles
- 1].profileID
;
3580 * Get the device ID hash and pathelogical name dictionary.
3583 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\"",
3586 device_id
= _ppdHashName(p
->name
);
3587 device_name
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3588 &kCFTypeDictionaryKeyCallBacks
,
3589 &kCFTypeDictionaryValueCallBacks
);
3590 printer_name
= CFStringCreateWithCString(kCFAllocatorDefault
,
3591 p
->name
, kCFStringEncodingUTF8
);
3593 if (device_name
&& printer_name
)
3595 CFDictionarySetValue(device_name
, CFSTR("en"), printer_name
);
3598 * Register the device with ColorSync...
3601 error
= CMRegisterColorDevice(cmPrinterDeviceClass
, device_id
,
3602 device_name
, &scope
);
3605 * Register the profiles...
3609 error
= CMSetDeviceFactoryProfiles(cmPrinterDeviceClass
, device_id
,
3610 default_profile_id
, profiles
);
3620 cupsdLogMessage(CUPSD_LOG_ERROR
,
3621 "Unable to register ICC color profiles for \"%s\" - %d",
3622 p
->name
, (int)error
);
3624 for (profile
= profiles
->profiles
;
3626 profile
++, num_profiles
--)
3627 CFRelease(profile
->profileName
);
3632 CFRelease(printer_name
);
3635 CFRelease(device_name
);
3643 * 'apple_unregister_profiles()' - Remove color profiles for the specified
3648 apple_unregister_profiles(
3649 cupsd_printer_t
*p
) /* I - Printer */
3652 * Make sure ColorSync is available...
3655 if (CMUnregisterColorDevice
!= NULL
)
3656 CMUnregisterColorDevice(cmPrinterDeviceClass
, _ppdHashName(p
->name
));
3658 #endif /* __APPLE__ */
3661 * 'apply_printer_defaults()' - Apply printer default options to a job.
3665 apply_printer_defaults(
3666 cupsd_printer_t
*printer
, /* I - Printer */
3667 cupsd_job_t
*job
) /* I - Job */
3669 int i
, /* Looping var */
3670 num_options
; /* Number of default options */
3671 cups_option_t
*options
, /* Default options */
3672 *option
; /* Current option */
3676 * Collect all of the default options and add the missing ones to the
3680 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
3681 option
= printer
->options
;
3684 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
3686 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
3691 * Encode these options as attributes in the job object...
3694 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
3695 cupsFreeOptions(num_options
, options
);
3700 * 'authenticate_job()' - Set job authentication info.
3704 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
3705 ipp_attribute_t
*uri
) /* I - Job URI */
3707 ipp_attribute_t
*attr
, /* job-id attribute */
3708 *auth_info
; /* auth-info attribute */
3709 int jobid
; /* Job ID */
3710 cupsd_job_t
*job
; /* Current job */
3711 char scheme
[HTTP_MAX_URI
],
3712 /* Method portion of URI */
3713 username
[HTTP_MAX_URI
],
3714 /* Username portion of URI */
3716 /* Host portion of URI */
3717 resource
[HTTP_MAX_URI
];
3718 /* Resource portion of URI */
3719 int port
; /* Port portion of URI */
3722 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
3723 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
3726 * Start with "everything is OK" status...
3729 con
->response
->request
.status
.status_code
= IPP_OK
;
3732 * See if we have a job URI or a printer URI...
3735 if (!strcmp(uri
->name
, "printer-uri"))
3738 * Got a printer URI; see if we also have a job-id attribute...
3741 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3742 IPP_TAG_INTEGER
)) == NULL
)
3744 send_ipp_status(con
, IPP_BAD_REQUEST
,
3745 _("Got a printer-uri attribute but no job-id"));
3749 jobid
= attr
->values
[0].integer
;
3754 * Got a job URI; parse it to get the job ID...
3757 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3758 sizeof(scheme
), username
, sizeof(username
), host
,
3759 sizeof(host
), &port
, resource
, sizeof(resource
));
3761 if (strncmp(resource
, "/jobs/", 6))
3767 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\""),
3768 uri
->values
[0].string
.text
);
3772 jobid
= atoi(resource
+ 6);
3776 * See if the job exists...
3779 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3782 * Nope - return a "not found" error...
3785 send_ipp_status(con
, IPP_NOT_FOUND
,
3786 _("Job #%d does not exist"), jobid
);
3791 * See if the job has been completed...
3794 if (job
->state_value
!= IPP_JOB_HELD
)
3797 * Return a "not-possible" error...
3800 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3801 _("Job #%d is not held for authentication"),
3807 * See if we have already authenticated...
3810 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3812 if (!con
->username
[0] && !auth_info
)
3814 cupsd_printer_t
*printer
; /* Job destination */
3818 * No auth data. If we need to authenticate via Kerberos, send a
3819 * HTTP auth challenge, otherwise just return an IPP error...
3822 printer
= cupsdFindDest(job
->dest
);
3824 if (printer
&& printer
->num_auth_info_required
> 0 &&
3825 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3826 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3828 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3829 _("No authentication information provided"));
3834 * See if the job is owned by the requesting user...
3837 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3839 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3840 cupsdFindDest(job
->dest
));
3845 * Save the authentication information for this job...
3848 save_auth_info(con
, job
, auth_info
);
3851 * Reset the job-hold-until value to "no-hold"...
3854 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3855 IPP_TAG_KEYWORD
)) == NULL
)
3856 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3860 attr
->value_tag
= IPP_TAG_KEYWORD
;
3861 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
3865 * Release the job and return...
3868 cupsdReleaseJob(job
);
3870 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3875 * 'cancel_all_jobs()' - Cancel all or selected print jobs.
3879 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3880 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3882 int i
; /* Looping var */
3883 http_status_t status
; /* Policy status */
3884 cups_ptype_t dtype
; /* Destination type */
3885 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3886 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3887 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3888 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3889 int port
; /* Port portion of URI */
3890 ipp_attribute_t
*attr
; /* Attribute in request */
3891 const char *username
= NULL
; /* Username */
3892 cupsd_jobaction_t purge
= CUPSD_JOB_DEFAULT
;
3894 cupsd_printer_t
*printer
; /* Printer */
3895 ipp_attribute_t
*job_ids
; /* job-ids attribute */
3896 cupsd_job_t
*job
; /* Job */
3899 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3900 con
->http
.fd
, uri
->values
[0].string
.text
);
3903 * Get the jobs to cancel/purge...
3906 switch (con
->request
->request
.op
.operation_id
)
3908 case IPP_PURGE_JOBS
:
3910 * Get the username (if any) for the jobs we want to cancel (only if
3911 * "my-jobs" is specified...
3914 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3915 IPP_TAG_BOOLEAN
)) != NULL
&&
3916 attr
->values
[0].boolean
)
3918 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3919 IPP_TAG_NAME
)) != NULL
)
3920 username
= attr
->values
[0].string
.text
;
3923 send_ipp_status(con
, IPP_BAD_REQUEST
,
3924 _("Missing requesting-user-name attribute"));
3930 * Look for the "purge-jobs" attribute...
3933 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3934 IPP_TAG_BOOLEAN
)) != NULL
)
3935 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3937 purge
= CUPSD_JOB_PURGE
;
3940 case IPP_CANCEL_MY_JOBS
:
3941 if (con
->username
[0])
3942 username
= con
->username
;
3943 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3944 IPP_TAG_NAME
)) != NULL
)
3945 username
= attr
->values
[0].string
.text
;
3948 send_ipp_status(con
, IPP_BAD_REQUEST
,
3949 _("Missing requesting-user-name attribute"));
3957 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
3960 * See if we have a printer URI...
3963 if (strcmp(uri
->name
, "printer-uri"))
3965 send_ipp_status(con
, IPP_BAD_REQUEST
,
3966 _("The printer-uri attribute is required"));
3971 * And if the destination is valid...
3974 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3980 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3981 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3982 hostname
, sizeof(hostname
), &port
,
3983 resource
, sizeof(resource
));
3985 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3986 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3988 send_ipp_status(con
, IPP_NOT_FOUND
,
3989 _("The printer or class was not found."));
3997 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3999 send_http_error(con
, status
, NULL
);
4005 for (i
= 0; i
< job_ids
->num_values
; i
++)
4007 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
)
4011 if (i
< job_ids
->num_values
)
4013 send_ipp_status(con
, IPP_NOT_FOUND
, _("job-id %d not found."),
4014 job_ids
->values
[i
].integer
);
4018 for (i
= 0; i
< job_ids
->num_values
; i
++)
4020 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
4022 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
4023 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
4024 "Job canceled by user.");
4027 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
4028 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
4034 * Cancel all jobs on all printers...
4037 cupsdCancelJobs(NULL
, username
, purge
);
4039 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
4040 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
4050 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
4053 send_http_error(con
, status
, printer
);
4059 for (i
= 0; i
< job_ids
->num_values
; i
++)
4061 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
||
4062 strcasecmp(job
->dest
, printer
->name
))
4066 if (i
< job_ids
->num_values
)
4068 send_ipp_status(con
, IPP_NOT_FOUND
, _("job-id %d not found."),
4069 job_ids
->values
[i
].integer
);
4073 for (i
= 0; i
< job_ids
->num_values
; i
++)
4075 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
4077 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
4078 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
4079 "Job canceled by user.");
4082 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
4083 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
4089 * Cancel all of the jobs on the named printer...
4092 cupsdCancelJobs(printer
->name
, username
, purge
);
4094 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
4096 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
4101 con
->response
->request
.status
.status_code
= IPP_OK
;
4106 * 'cancel_job()' - Cancel a print job.
4110 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
4111 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
4113 ipp_attribute_t
*attr
; /* Current attribute */
4114 int jobid
; /* Job ID */
4115 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
4116 username
[HTTP_MAX_URI
], /* Username portion of URI */
4117 host
[HTTP_MAX_URI
], /* Host portion of URI */
4118 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
4119 int port
; /* Port portion of URI */
4120 cupsd_job_t
*job
; /* Job information */
4121 cups_ptype_t dtype
; /* Destination type (printer/class) */
4122 cupsd_printer_t
*printer
; /* Printer data */
4123 cupsd_jobaction_t purge
; /* Purge the job? */
4126 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
4127 con
->http
.fd
, uri
->values
[0].string
.text
);
4130 * See if we have a job URI or a printer URI...
4133 if (!strcmp(uri
->name
, "printer-uri"))
4136 * Got a printer URI; see if we also have a job-id attribute...
4139 if ((attr
= ippFindAttribute(con
->request
, "job-id",
4140 IPP_TAG_INTEGER
)) == NULL
)
4142 send_ipp_status(con
, IPP_BAD_REQUEST
,
4143 _("Got a printer-uri attribute but no job-id"));
4147 if ((jobid
= attr
->values
[0].integer
) == 0)
4150 * Find the current job on the specified printer...
4153 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
4159 send_ipp_status(con
, IPP_NOT_FOUND
,
4160 _("The printer or class was not found."));
4165 * See if there are any pending jobs...
4168 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
4170 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
4171 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
4172 !strcasecmp(job
->dest
, printer
->name
))
4180 * No, try stopped jobs...
4183 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
4185 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
4186 if (job
->state_value
== IPP_JOB_STOPPED
&&
4187 !strcasecmp(job
->dest
, printer
->name
))
4194 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s"),
4204 * Got a job URI; parse it to get the job ID...
4207 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
4208 sizeof(scheme
), username
, sizeof(username
), host
,
4209 sizeof(host
), &port
, resource
, sizeof(resource
));
4211 if (strncmp(resource
, "/jobs/", 6))
4217 send_ipp_status(con
, IPP_BAD_REQUEST
,
4218 _("Bad job-uri attribute \"%s\""),
4219 uri
->values
[0].string
.text
);
4223 jobid
= atoi(resource
+ 6);
4227 * Look for the "purge-job" attribute...
4230 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
4231 IPP_TAG_BOOLEAN
)) != NULL
)
4232 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
4234 purge
= CUPSD_JOB_DEFAULT
;
4237 * See if the job exists...
4240 if ((job
= cupsdFindJob(jobid
)) == NULL
)
4243 * Nope - return a "not found" error...
4246 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist"), jobid
);
4251 * See if the job is owned by the requesting user...
4254 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
4256 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
4257 cupsdFindDest(job
->dest
));
4262 * See if the job is already completed, canceled, or aborted; if so,
4263 * we can't cancel...
4266 if (job
->state_value
>= IPP_JOB_CANCELED
&& purge
!= CUPSD_JOB_PURGE
)
4268 switch (job
->state_value
)
4270 case IPP_JOB_CANCELED
:
4271 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4272 _("Job #%d is already canceled - can\'t cancel."),
4276 case IPP_JOB_ABORTED
:
4277 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4278 _("Job #%d is already aborted - can\'t cancel."),
4283 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4284 _("Job #%d is already completed - can\'t cancel."),
4293 * Cancel the job and return...
4296 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
4297 purge
== CUPSD_JOB_PURGE
? "Job purged by \"%s\"" :
4298 "Job canceled by \"%s\"",
4302 if (purge
== CUPSD_JOB_PURGE
)
4303 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
4306 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
4309 con
->response
->request
.status
.status_code
= IPP_OK
;
4314 * 'cancel_subscription()' - Cancel a subscription.
4318 cancel_subscription(
4319 cupsd_client_t
*con
, /* I - Client connection */
4320 int sub_id
) /* I - Subscription ID */
4322 http_status_t status
; /* Policy status */
4323 cupsd_subscription_t
*sub
; /* Subscription */
4326 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4327 "cancel_subscription(con=%p[%d], sub_id=%d)",
4328 con
, con
->http
.fd
, sub_id
);
4331 * Is the subscription ID valid?
4334 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
4337 * Bad subscription ID...
4340 send_ipp_status(con
, IPP_NOT_FOUND
,
4341 _("notify-subscription-id %d no good"), sub_id
);
4349 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
4351 con
, sub
->owner
)) != HTTP_OK
)
4353 send_http_error(con
, status
, sub
->dest
);
4358 * Cancel the subscription...
4361 cupsdDeleteSubscription(sub
, 1);
4363 con
->response
->request
.status
.status_code
= IPP_OK
;
4368 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
4371 static int /* O - 1 if OK, 0 if not */
4372 check_rss_recipient(
4373 const char *recipient
) /* I - Recipient URI */
4375 cupsd_subscription_t
*sub
; /* Current subscription */
4378 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
4380 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
4384 * Compare the URIs up to the first ?...
4387 const char *r1
, *r2
;
4389 for (r1
= recipient
, r2
= sub
->recipient
;
4390 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
4402 * 'check_quotas()' - Check quotas for a printer and user.
4405 static int /* O - 1 if OK, 0 if not */
4406 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
4407 cupsd_printer_t
*p
) /* I - Printer or class */
4409 int i
; /* Looping var */
4410 char username
[33]; /* Username */
4411 cupsd_quota_t
*q
; /* Quota data */
4412 #ifdef HAVE_MBR_UID_TO_UUID
4414 * Use Apple membership APIs which require that all names represent
4415 * valid user account or group records accessible by the server.
4418 uuid_t usr_uuid
; /* UUID for job requesting user */
4419 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
4420 uuid_t grp_uuid
; /* UUID for ACL group name entry */
4421 int mbr_err
; /* Error from membership function */
4422 int is_member
; /* Is this user a member? */
4425 * Use standard POSIX APIs for checking users and groups...
4428 struct passwd
*pw
; /* User password data */
4429 #endif /* HAVE_MBR_UID_TO_UUID */
4432 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
4433 con
, con
->http
.fd
, p
, p
->name
);
4436 * Figure out who is printing...
4439 strlcpy(username
, get_username(con
), sizeof(username
));
4442 * Check global active job limits for printers and users...
4445 if (MaxJobsPerPrinter
)
4448 * Check if there are too many pending jobs on this printer...
4451 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
4453 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
4462 * Check if there are too many pending jobs for this user...
4465 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
4467 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
4474 * Check against users...
4477 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
4482 #ifdef HAVE_MBR_UID_TO_UUID
4484 * Get UUID for job requesting user...
4487 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
4493 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4494 "check_quotas: UUID lookup failed for user \"%s\"",
4496 cupsdLogMessage(CUPSD_LOG_INFO
,
4497 "Denying user \"%s\" access to printer \"%s\" "
4498 "(unknown user)...",
4504 * Get UID and GID of requesting user...
4507 pw
= getpwnam(username
);
4509 #endif /* HAVE_MBR_UID_TO_UUID */
4511 for (i
= 0; i
< p
->num_users
; i
++)
4512 if (p
->users
[i
][0] == '@')
4515 * Check group membership...
4518 #ifdef HAVE_MBR_UID_TO_UUID
4519 if (p
->users
[i
][1] == '#')
4521 if (uuid_parse((char *)p
->users
[i
] + 2, grp_uuid
))
4522 uuid_clear(grp_uuid
);
4524 else if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
4528 * Invalid ACL entries are ignored for matching; just record a
4529 * warning in the log...
4532 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4533 "check_quotas: UUID lookup failed for ACL entry "
4534 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4535 cupsdLogMessage(CUPSD_LOG_WARN
,
4536 "Access control entry \"%s\" not a valid group name; "
4537 "entry ignored", p
->users
[i
]);
4540 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
4544 * At this point, there should be no errors, but check anyways...
4547 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4548 "check_quotas: group \"%s\" membership check "
4549 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
4554 * Stop if we found a match...
4561 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
4563 #endif /* HAVE_MBR_UID_TO_UUID */
4565 #ifdef HAVE_MBR_UID_TO_UUID
4568 if (p
->users
[i
][0] == '#')
4570 if (uuid_parse((char *)p
->users
[i
] + 1, usr2_uuid
))
4571 uuid_clear(usr2_uuid
);
4573 else if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
4577 * Invalid ACL entries are ignored for matching; just record a
4578 * warning in the log...
4581 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4582 "check_quotas: UUID lookup failed for ACL entry "
4583 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4584 cupsdLogMessage(CUPSD_LOG_WARN
,
4585 "Access control entry \"%s\" not a valid user name; "
4586 "entry ignored", p
->users
[i
]);
4589 if (!uuid_compare(usr_uuid
, usr2_uuid
))
4593 else if (!strcasecmp(username
, p
->users
[i
]))
4595 #endif /* HAVE_MBR_UID_TO_UUID */
4597 if ((i
< p
->num_users
) == p
->deny_users
)
4599 cupsdLogMessage(CUPSD_LOG_INFO
,
4600 "Denying user \"%s\" access to printer \"%s\"...",
4610 if (p
->k_limit
|| p
->page_limit
)
4612 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
4614 cupsdLogMessage(CUPSD_LOG_ERROR
,
4615 "Unable to allocate quota data for user \"%s\"",
4620 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
4621 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
4623 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
4630 * If we have gotten this far, we're done!
4638 * 'close_job()' - Close a multi-file job.
4642 close_job(cupsd_client_t
*con
, /* I - Client connection */
4643 ipp_attribute_t
*uri
) /* I - Printer URI */
4645 cupsd_job_t
*job
; /* Job */
4646 cupsd_printer_t
*printer
; /* Printer */
4647 ipp_attribute_t
*attr
; /* Attribute */
4648 char job_uri
[HTTP_MAX_URI
],
4650 username
[256]; /* User name */
4653 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "close_job(%p[%d], %s)", con
,
4654 con
->http
.fd
, uri
->values
[0].string
.text
);
4657 * See if we have a job URI or a printer URI...
4660 if (strcmp(uri
->name
, "printer-uri"))
4663 * job-uri is not supported by Close-Job!
4666 send_ipp_status(con
, IPP_BAD_REQUEST
,
4667 _("Close-Job doesn't support the job-uri attribute."));
4672 * Got a printer URI; see if we also have a job-id attribute...
4675 if ((attr
= ippFindAttribute(con
->request
, "job-id",
4676 IPP_TAG_INTEGER
)) == NULL
)
4678 send_ipp_status(con
, IPP_BAD_REQUEST
,
4679 _("Got a printer-uri attribute but no job-id"));
4683 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
4686 * Nope - return a "not found" error...
4689 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist"),
4690 attr
->values
[0].integer
);
4694 printer
= cupsdFindDest(job
->dest
);
4697 * See if the job is owned by the requesting user...
4700 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
4702 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
4703 cupsdFindDest(job
->dest
));
4708 * Add any ending sheet...
4711 if (cupsdTimeoutJob(job
))
4714 if (job
->state_value
== IPP_JOB_STOPPED
)
4716 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
4717 job
->state_value
= IPP_JOB_PENDING
;
4719 else if (job
->state_value
== IPP_JOB_HELD
)
4721 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
4722 IPP_TAG_KEYWORD
)) == NULL
)
4723 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
4725 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
4727 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
4728 job
->state_value
= IPP_JOB_PENDING
;
4733 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
4736 * Fill in the response info...
4739 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
4740 LocalPort
, job
->id
);
4741 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
4744 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
4746 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
4749 add_job_state_reasons(con
, job
);
4751 con
->response
->request
.status
.status_code
= IPP_OK
;
4754 * Start the job if necessary...
4762 * 'copy_attribute()' - Copy a single attribute.
4765 static ipp_attribute_t
* /* O - New attribute */
4767 ipp_t
*to
, /* O - Destination request/response */
4768 ipp_attribute_t
*attr
, /* I - Attribute to copy */
4769 int quickcopy
) /* I - Do a quick copy? */
4771 int i
; /* Looping var */
4772 ipp_attribute_t
*toattr
; /* Destination attribute */
4775 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4776 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
4777 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
4780 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
4783 toattr
= ippAddSeparator(to
);
4786 case IPP_TAG_INTEGER
:
4788 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4789 attr
->name
, attr
->num_values
, NULL
);
4791 for (i
= 0; i
< attr
->num_values
; i
++)
4792 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
4795 case IPP_TAG_BOOLEAN
:
4796 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
4797 attr
->num_values
, NULL
);
4799 for (i
= 0; i
< attr
->num_values
; i
++)
4800 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
4803 case IPP_TAG_STRING
:
4806 case IPP_TAG_KEYWORD
:
4808 case IPP_TAG_URISCHEME
:
4809 case IPP_TAG_CHARSET
:
4810 case IPP_TAG_LANGUAGE
:
4811 case IPP_TAG_MIMETYPE
:
4812 toattr
= ippAddStrings(to
, attr
->group_tag
,
4813 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4814 attr
->name
, attr
->num_values
, NULL
, NULL
);
4818 for (i
= 0; i
< attr
->num_values
; i
++)
4819 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4821 else if (attr
->value_tag
& IPP_TAG_COPY
)
4823 for (i
= 0; i
< attr
->num_values
; i
++)
4824 toattr
->values
[i
].string
.text
=
4825 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4829 for (i
= 0; i
< attr
->num_values
; i
++)
4830 toattr
->values
[i
].string
.text
=
4831 _cupsStrRetain(attr
->values
[i
].string
.text
);
4836 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
4837 attr
->values
[0].date
);
4840 case IPP_TAG_RESOLUTION
:
4841 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
4842 attr
->num_values
, IPP_RES_PER_INCH
,
4845 for (i
= 0; i
< attr
->num_values
; i
++)
4847 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
4848 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
4849 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
4853 case IPP_TAG_RANGE
:
4854 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
4855 attr
->num_values
, NULL
, NULL
);
4857 for (i
= 0; i
< attr
->num_values
; i
++)
4859 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
4860 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
4864 case IPP_TAG_TEXTLANG
:
4865 case IPP_TAG_NAMELANG
:
4866 toattr
= ippAddStrings(to
, attr
->group_tag
,
4867 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4868 attr
->name
, attr
->num_values
, NULL
, NULL
);
4872 for (i
= 0; i
< attr
->num_values
; i
++)
4874 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
4875 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4878 else if (attr
->value_tag
& IPP_TAG_COPY
)
4880 for (i
= 0; i
< attr
->num_values
; i
++)
4883 toattr
->values
[i
].string
.charset
=
4884 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
4886 toattr
->values
[i
].string
.charset
=
4887 toattr
->values
[0].string
.charset
;
4889 toattr
->values
[i
].string
.text
=
4890 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4895 for (i
= 0; i
< attr
->num_values
; i
++)
4898 toattr
->values
[i
].string
.charset
=
4899 _cupsStrRetain(attr
->values
[i
].string
.charset
);
4901 toattr
->values
[i
].string
.charset
=
4902 toattr
->values
[0].string
.charset
;
4904 toattr
->values
[i
].string
.text
=
4905 _cupsStrRetain(attr
->values
[i
].string
.text
);
4910 case IPP_TAG_BEGIN_COLLECTION
:
4911 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
4912 attr
->num_values
, NULL
);
4914 for (i
= 0; i
< attr
->num_values
; i
++)
4916 toattr
->values
[i
].collection
= attr
->values
[i
].collection
;
4917 attr
->values
[i
].collection
->use
++;
4922 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4923 attr
->name
, attr
->num_values
, NULL
);
4925 for (i
= 0; i
< attr
->num_values
; i
++)
4927 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
4929 if (toattr
->values
[i
].unknown
.length
> 0)
4931 if ((toattr
->values
[i
].unknown
.data
=
4932 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
4933 toattr
->values
[i
].unknown
.length
= 0;
4935 memcpy(toattr
->values
[i
].unknown
.data
,
4936 attr
->values
[i
].unknown
.data
,
4937 toattr
->values
[i
].unknown
.length
);
4940 break; /* anti-compiler-warning-code */
4948 * 'copy_attrs()' - Copy attributes from one request to another.
4952 copy_attrs(ipp_t
*to
, /* I - Destination request */
4953 ipp_t
*from
, /* I - Source request */
4954 cups_array_t
*ra
, /* I - Requested attributes */
4955 ipp_tag_t group
, /* I - Group to copy */
4956 int quickcopy
) /* I - Do a quick copy? */
4958 ipp_attribute_t
*fromattr
; /* Source attribute */
4961 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4962 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4963 to
, from
, ra
, group
, quickcopy
);
4968 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4971 * Filter attributes as needed...
4974 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4975 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4978 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4981 * Don't send collection attributes by default to IPP/1.x clients
4982 * since many do not support collections. Also don't send
4983 * media-col-database unless specifically requested by the client.
4986 if (fromattr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
&&
4988 (to
->request
.status
.version
[0] == 1 ||
4989 !strcmp(fromattr
->name
, "media-col-database")))
4992 copy_attribute(to
, fromattr
, quickcopy
);
4999 * 'copy_banner()' - Copy a banner file to the requests directory for the
5003 static int /* O - Size of banner file in kbytes */
5004 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
5005 cupsd_job_t
*job
, /* I - Job information */
5006 const char *name
) /* I - Name of banner */
5008 int i
; /* Looping var */
5009 int kbytes
; /* Size of banner file in kbytes */
5010 char filename
[1024]; /* Job filename */
5011 cupsd_banner_t
*banner
; /* Pointer to banner */
5012 cups_file_t
*in
; /* Input file */
5013 cups_file_t
*out
; /* Output file */
5014 int ch
; /* Character from file */
5015 char attrname
[255], /* Name of attribute */
5016 *s
; /* Pointer into name */
5017 ipp_attribute_t
*attr
; /* Attribute */
5020 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5021 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
5022 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
5023 name
? name
: "(null)");
5026 * Find the banner; return if not found or "none"...
5029 if (!name
|| !strcmp(name
, "none") ||
5030 (banner
= cupsdFindBanner(name
)) == NULL
)
5034 * Open the banner and job files...
5037 if (add_file(con
, job
, banner
->filetype
, 0))
5040 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
5042 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
5044 cupsdLogMessage(CUPSD_LOG_ERROR
,
5045 "Unable to create banner job file %s - %s",
5046 filename
, strerror(errno
));
5051 fchmod(cupsFileNumber(out
), 0640);
5052 fchown(cupsFileNumber(out
), RunUser
, Group
);
5055 * Try the localized banner file under the subdirectory...
5058 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
5060 if (strlen(attrname
) > 2 && attrname
[2] == '-')
5063 * Convert ll-cc to ll_CC...
5067 attrname
[3] = toupper(attrname
[3] & 255);
5068 attrname
[4] = toupper(attrname
[4] & 255);
5071 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
5074 if (access(filename
, 0) && strlen(attrname
) > 2)
5077 * Wasn't able to find "ll_CC" locale file; try the non-national
5078 * localization banner directory.
5083 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
5087 if (access(filename
, 0))
5090 * Use the non-localized banner file.
5093 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
5096 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
5100 cupsdLogMessage(CUPSD_LOG_ERROR
,
5101 "Unable to open banner template file %s - %s",
5102 filename
, strerror(errno
));
5108 * Parse the file to the end...
5111 while ((ch
= cupsFileGetChar(in
)) != EOF
)
5115 * Get an attribute name...
5118 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
5119 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
5121 else if (s
< (attrname
+ sizeof(attrname
) - 1))
5131 * Ignore { followed by stuff that is not an attribute name...
5134 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
5139 * See if it is defined...
5142 if (attrname
[0] == '?')
5147 if (!strcmp(s
, "printer-name"))
5149 cupsFilePuts(out
, job
->dest
);
5152 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
5155 * See if we have a leading question mark...
5158 if (attrname
[0] != '?')
5161 * Nope, write to file as-is; probably a PostScript procedure...
5164 cupsFilePrintf(out
, "{%s}", attrname
);
5171 * Output value(s)...
5174 for (i
= 0; i
< attr
->num_values
; i
++)
5177 cupsFilePutChar(out
, ',');
5179 switch (attr
->value_tag
)
5181 case IPP_TAG_INTEGER
:
5183 if (!strncmp(s
, "time-at-", 8))
5185 struct timeval tv
= { attr
->values
[i
].integer
, 0 };
5186 cupsFilePuts(out
, cupsdGetDateTime(&tv
, CUPSD_TIME_STANDARD
));
5189 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
5192 case IPP_TAG_BOOLEAN
:
5193 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
5196 case IPP_TAG_NOVALUE
:
5197 cupsFilePuts(out
, "novalue");
5200 case IPP_TAG_RANGE
:
5201 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
5202 attr
->values
[i
].range
.upper
);
5205 case IPP_TAG_RESOLUTION
:
5206 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
5207 attr
->values
[i
].resolution
.yres
,
5208 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5213 case IPP_TAG_STRING
:
5216 case IPP_TAG_KEYWORD
:
5217 case IPP_TAG_CHARSET
:
5218 case IPP_TAG_LANGUAGE
:
5219 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
5222 * Need to quote strings for PS banners...
5227 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
5229 if (*p
== '(' || *p
== ')' || *p
== '\\')
5231 cupsFilePutChar(out
, '\\');
5232 cupsFilePutChar(out
, *p
);
5234 else if (*p
< 32 || *p
> 126)
5235 cupsFilePrintf(out
, "\\%03o", *p
& 255);
5237 cupsFilePutChar(out
, *p
);
5241 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
5245 break; /* anti-compiler-warning-code */
5249 else if (ch
== '\\') /* Quoted char */
5251 ch
= cupsFileGetChar(in
);
5253 if (ch
!= '{') /* Only do special handling for \{ */
5254 cupsFilePutChar(out
, '\\');
5256 cupsFilePutChar(out
, ch
);
5259 cupsFilePutChar(out
, ch
);
5263 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
5265 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
5266 IPP_TAG_INTEGER
)) != NULL
)
5267 attr
->values
[0].integer
+= kbytes
;
5276 * 'copy_file()' - Copy a PPD file or interface script...
5279 static int /* O - 0 = success, -1 = error */
5280 copy_file(const char *from
, /* I - Source file */
5281 const char *to
) /* I - Destination file */
5283 cups_file_t
*src
, /* Source file */
5284 *dst
; /* Destination file */
5285 int bytes
; /* Bytes to read/write */
5286 char buffer
[2048]; /* Copy buffer */
5289 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
5292 * Open the source and destination file for a copy...
5295 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
5298 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
5305 * Copy the source file to the destination...
5308 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
5309 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
5317 * Close both files and return...
5322 return (cupsFileClose(dst
));
5327 * 'copy_model()' - Copy a PPD model file, substituting default values
5331 static int /* O - 0 = success, -1 = error */
5332 copy_model(cupsd_client_t
*con
, /* I - Client connection */
5333 const char *from
, /* I - Source file */
5334 const char *to
) /* I - Destination file */
5336 fd_set input
; /* select() input set */
5337 struct timeval timeout
; /* select() timeout */
5338 int maxfd
; /* Max file descriptor for select() */
5339 char tempfile
[1024]; /* Temporary PPD file */
5340 int tempfd
; /* Temporary PPD file descriptor */
5341 int temppid
; /* Process ID of cups-driverd */
5342 int temppipe
[2]; /* Temporary pipes */
5343 char *argv
[4], /* Command-line arguments */
5344 *envp
[MAX_ENV
]; /* Environment */
5345 cups_file_t
*src
, /* Source file */
5346 *dst
; /* Destination file */
5347 ppd_file_t
*ppd
; /* PPD file */
5348 int bytes
, /* Bytes from pipe */
5349 total
; /* Total bytes from pipe */
5350 char buffer
[2048]; /* Copy buffer */
5351 int i
; /* Looping var */
5352 char option
[PPD_MAX_NAME
], /* Option name */
5353 choice
[PPD_MAX_NAME
]; /* Choice name */
5354 ppd_size_t
*size
; /* Default size */
5355 int num_defaults
; /* Number of default options */
5356 cups_option_t
*defaults
; /* Default options */
5357 char cups_protocol
[PPD_MAX_LINE
];
5358 /* cupsProtocol attribute */
5361 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5362 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
5366 * Run cups-driverd to get the PPD file...
5369 argv
[0] = "cups-driverd";
5371 argv
[2] = (char *)from
;
5374 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5376 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
5377 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
5378 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
5382 cupsdOpenPipe(temppipe
);
5384 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5385 "copy_model: Running \"cups-driverd cat %s\"...", from
);
5387 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
5388 -1, -1, 0, DefaultProfile
, NULL
, &temppid
))
5399 * Wait up to 30 seconds for the PPD file to be copied...
5404 if (temppipe
[0] > CGIPipes
[0])
5405 maxfd
= temppipe
[0] + 1;
5407 maxfd
= CGIPipes
[0] + 1;
5412 * See if we have data ready...
5416 FD_SET(temppipe
[0], &input
);
5417 FD_SET(CGIPipes
[0], &input
);
5419 timeout
.tv_sec
= 30;
5420 timeout
.tv_usec
= 0;
5422 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
5432 * We have timed out...
5438 if (FD_ISSET(temppipe
[0], &input
))
5441 * Read the PPD file from the pipe, and write it to the PPD file.
5444 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
5446 if (write(tempfd
, buffer
, bytes
) < bytes
)
5455 if (FD_ISSET(CGIPipes
[0], &input
))
5465 * No data from cups-deviced...
5468 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file");
5474 * Read the source file and see what page sizes are supported...
5477 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
5484 * Open the destination (if possible) and set the default options...
5489 cups_protocol
[0] = '\0';
5491 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
5494 * Read all of the default lines from the old PPD...
5497 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
5498 if (!strncmp(buffer
, "*Default", 8))
5501 * Add the default option...
5504 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5505 choice
, sizeof(choice
)))
5507 ppd_option_t
*ppdo
; /* PPD option */
5511 * Only add the default if the default hasn't already been
5512 * set and the choice exists in the new PPD...
5515 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
5516 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
5517 ppdFindChoice(ppdo
, choice
))
5518 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
5522 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
5523 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
5527 else if ((size
= ppdPageSize(ppd
, DefaultPaperSize
)) != NULL
)
5530 * Add the default media sizes...
5533 num_defaults
= cupsAddOption("PageSize", size
->name
,
5534 num_defaults
, &defaults
);
5535 num_defaults
= cupsAddOption("PageRegion", size
->name
,
5536 num_defaults
, &defaults
);
5537 num_defaults
= cupsAddOption("PaperDimension", size
->name
,
5538 num_defaults
, &defaults
);
5539 num_defaults
= cupsAddOption("ImageableArea", size
->name
,
5540 num_defaults
, &defaults
);
5546 * Open the source file for a copy...
5549 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
5551 cupsFreeOptions(num_defaults
, defaults
);
5557 * Open the destination file for a copy...
5560 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
5562 cupsFreeOptions(num_defaults
, defaults
);
5569 * Copy the source file to the destination...
5572 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
5574 if (!strncmp(buffer
, "*Default", 8))
5577 * Check for an previous default option choice...
5580 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5581 choice
, sizeof(choice
)))
5583 const char *val
; /* Default option value */
5586 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
5589 * Substitute the previous choice...
5592 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
5597 cupsFilePrintf(dst
, "%s\n", buffer
);
5600 if (cups_protocol
[0])
5601 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
5603 cupsFreeOptions(num_defaults
, defaults
);
5606 * Close both files and return...
5613 return (cupsFileClose(dst
));
5618 * 'copy_job_attrs()' - Copy job attributes.
5622 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5623 cupsd_job_t
*job
, /* I - Job */
5624 cups_array_t
*ra
) /* I - Requested attributes array */
5626 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
5630 * Send the requested attributes for each job...
5633 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
5634 con
->servername
, con
->serverport
, "/jobs/%d",
5637 if (!ra
|| cupsArrayFind(ra
, "document-count"))
5638 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5639 "document-count", job
->num_files
);
5641 if (!ra
|| cupsArrayFind(ra
, "job-media-progress"))
5642 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5643 "job-media-progress", job
->progress
);
5645 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
5646 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5647 "job-more-info", NULL
, job_uri
);
5649 if (job
->state_value
> IPP_JOB_PROCESSING
&&
5650 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
5651 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
5652 job
->num_files
> 0);
5654 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
5655 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5656 "job-printer-up-time", time(NULL
));
5658 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
5659 add_job_state_reasons(con
, job
);
5661 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
5662 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5663 "job-uri", NULL
, job_uri
);
5665 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
5670 * 'copy_printer_attrs()' - Copy printer attributes.
5675 cupsd_client_t
*con
, /* I - Client connection */
5676 cupsd_printer_t
*printer
, /* I - Printer */
5677 cups_array_t
*ra
) /* I - Requested attributes array */
5679 char printer_uri
[HTTP_MAX_URI
];
5681 time_t curtime
; /* Current time */
5682 int i
; /* Looping var */
5683 ipp_attribute_t
*history
; /* History collection */
5687 * Copy the printer attributes to the response using requested-attributes
5688 * and document-format attributes that may be provided by the client.
5691 curtime
= time(NULL
);
5693 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
5694 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5695 "marker-change-time", printer
->marker_time
);
5697 if (printer
->num_printers
> 0 &&
5698 (!ra
|| cupsArrayFind(ra
, "member-uris")))
5700 ipp_attribute_t
*member_uris
; /* member-uris attribute */
5701 cupsd_printer_t
*p2
; /* Printer in class */
5702 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
5705 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
5706 IPP_TAG_URI
, "member-uris",
5707 printer
->num_printers
, NULL
,
5710 for (i
= 0; i
< printer
->num_printers
; i
++)
5712 p2
= printer
->printers
[i
];
5714 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
5715 IPP_TAG_URI
)) != NULL
)
5716 member_uris
->values
[i
].string
.text
=
5717 _cupsStrRetain(p2_uri
->values
[0].string
.text
);
5720 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
5721 sizeof(printer_uri
), "ipp", NULL
, con
->servername
,
5723 (p2
->type
& CUPS_PRINTER_CLASS
) ?
5724 "/classes/%s" : "/printers/%s", p2
->name
);
5725 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
5731 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
5732 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
5733 "printer-alert", NULL
, printer
->alert
);
5735 if (printer
->alert_description
&&
5736 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
5737 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5738 "printer-alert-description", NULL
,
5739 printer
->alert_description
);
5741 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
5742 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
5743 ippTimeToDate(curtime
));
5746 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
5748 if (printer
->reg_name
)
5749 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5750 "printer-dns-sd-name", NULL
, printer
->reg_name
);
5752 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
5753 "printer-dns-sd-name", 0);
5755 #endif /* HAVE_DNSSD */
5757 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
5758 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5759 "printer-error-policy", NULL
, printer
->error_policy
);
5761 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy-supported"))
5763 static const char * const errors
[] =/* printer-error-policy-supported values */
5766 "retry-current-job",
5771 if (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
))
5772 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
5773 "printer-error-policy-supported", NULL
, "retry-current-job");
5775 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
5776 "printer-error-policy-supported",
5777 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
5780 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
5781 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
5782 printer
->accepting
);
5784 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
5785 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
5788 if ((!ra
|| cupsArrayFind(ra
, "printer-more-info")) &&
5789 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
5791 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5792 "http", NULL
, con
->servername
, con
->serverport
,
5793 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5794 "/classes/%s" : "/printers/%s", printer
->name
);
5795 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5796 "printer-more-info", NULL
, printer_uri
);
5799 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
5800 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5801 "printer-op-policy", NULL
, printer
->op_policy
);
5803 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
5804 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
5807 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
5808 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5809 "printer-state-change-time", printer
->state_time
);
5811 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
5812 cupsArrayFind(ra
, "printer-state-history"))
5815 * Printer history is only sent if specifically requested, so that
5816 * older CUPS/IPP clients won't barf on the collection attributes.
5819 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
5820 "printer-state-history",
5821 printer
->num_history
, NULL
);
5823 for (i
= 0; i
< printer
->num_history
; i
++)
5824 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
5825 NULL
, IPP_TAG_ZERO
, 0);
5828 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
5829 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5830 "printer-state-message", NULL
, printer
->state_message
);
5832 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
5833 add_printer_state_reasons(con
, printer
);
5835 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5837 int type
; /* printer-type value */
5841 * Add the CUPS-specific printer-type attribute...
5844 type
= printer
->type
;
5846 if (printer
== DefaultPrinter
)
5847 type
|= CUPS_PRINTER_DEFAULT
;
5849 if (!printer
->accepting
)
5850 type
|= CUPS_PRINTER_REJECTING
;
5852 if (!printer
->shared
)
5853 type
|= CUPS_PRINTER_NOT_SHARED
;
5855 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
5859 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5860 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5861 "printer-up-time", curtime
);
5863 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
5864 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
5866 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5867 "ipp", NULL
, con
->servername
, con
->serverport
,
5868 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5869 "/classes/%s" : "/printers/%s", printer
->name
);
5870 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5871 "printer-uri-supported", NULL
, printer_uri
);
5872 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5876 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5877 add_queued_job_count(con
, printer
);
5879 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
5880 if (printer
->ppd_attrs
)
5881 copy_attrs(con
->response
, printer
->ppd_attrs
, ra
, IPP_TAG_ZERO
, 0);
5882 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
5887 * 'copy_subscription_attrs()' - Copy subscription attributes.
5891 copy_subscription_attrs(
5892 cupsd_client_t
*con
, /* I - Client connection */
5893 cupsd_subscription_t
*sub
, /* I - Subscription */
5894 cups_array_t
*ra
) /* I - Requested attributes array */
5896 ipp_attribute_t
*attr
; /* Current attribute */
5897 char printer_uri
[HTTP_MAX_URI
];
5899 int count
; /* Number of events */
5900 unsigned mask
; /* Current event mask */
5901 const char *name
; /* Current event name */
5905 * Copy the subscription attributes to the response using the
5906 * requested-attributes attribute that may be provided by the client.
5909 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
5911 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5914 * Simple event list...
5917 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5918 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5919 "notify-events", NULL
, name
);
5924 * Complex event list...
5927 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5928 if (sub
->mask
& mask
)
5931 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5932 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5933 "notify-events", count
, NULL
, NULL
);
5935 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5936 if (sub
->mask
& mask
)
5938 attr
->values
[count
].string
.text
=
5939 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5946 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5947 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5948 "notify-job-id", sub
->job
->id
);
5950 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
5951 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5952 "notify-lease-duration", sub
->lease
);
5954 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5956 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5957 "ipp", NULL
, con
->servername
, con
->serverport
,
5958 "/printers/%s", sub
->dest
->name
);
5959 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5960 "notify-printer-uri", NULL
, printer_uri
);
5963 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
5964 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5965 "notify-recipient-uri", NULL
, sub
->recipient
);
5966 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
5967 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5968 "notify-pull-method", NULL
, "ippget");
5970 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
5971 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5972 "notify-subscriber-user-name", NULL
, sub
->owner
);
5974 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5975 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5976 "notify-subscription-id", sub
->id
);
5978 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
5979 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5980 "notify-time-interval", sub
->interval
);
5982 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5983 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5984 sub
->user_data
, sub
->user_data_len
);
5989 * 'create_job()' - Print a file to a printer or class.
5993 create_job(cupsd_client_t
*con
, /* I - Client connection */
5994 ipp_attribute_t
*uri
) /* I - Printer URI */
5996 cupsd_printer_t
*printer
; /* Printer */
5997 cupsd_job_t
*job
; /* New job */
6000 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
6001 con
->http
.fd
, uri
->values
[0].string
.text
);
6004 * Is the destination valid?
6007 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
6013 send_ipp_status(con
, IPP_NOT_FOUND
,
6014 _("The printer or class was not found."));
6019 * Create the job object...
6022 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
6025 job
->pending_timeout
= 1;
6028 * Save and log the job...
6031 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
6032 job
->dest
, job
->username
);
6037 * 'create_requested_array()' - Create an array for the requested-attributes.
6040 static cups_array_t
* /* O - Array of attributes or NULL */
6041 create_requested_array(ipp_t
*request
) /* I - IPP request */
6043 int i
; /* Looping var */
6044 ipp_attribute_t
*requested
; /* requested-attributes attribute */
6045 cups_array_t
*ra
; /* Requested attributes array */
6046 char *value
; /* Current value */
6050 * Get the requested-attributes attribute, and return NULL if we don't
6054 if ((requested
= ippFindAttribute(request
, "requested-attributes",
6055 IPP_TAG_KEYWORD
)) == NULL
)
6059 * If the attribute contains a single "all" keyword, return NULL...
6062 if (requested
->num_values
== 1 &&
6063 !strcmp(requested
->values
[0].string
.text
, "all"))
6067 * Create an array using "strcmp" as the comparison function...
6070 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
6072 for (i
= 0; i
< requested
->num_values
; i
++)
6074 value
= requested
->values
[i
].string
.text
;
6076 if (!strcmp(value
, "job-template"))
6078 cupsArrayAdd(ra
, "copies");
6079 cupsArrayAdd(ra
, "copies-default");
6080 cupsArrayAdd(ra
, "copies-supported");
6081 cupsArrayAdd(ra
, "finishings");
6082 cupsArrayAdd(ra
, "finishings-default");
6083 cupsArrayAdd(ra
, "finishings-supported");
6084 cupsArrayAdd(ra
, "job-hold-until");
6085 cupsArrayAdd(ra
, "job-hold-until-default");
6086 cupsArrayAdd(ra
, "job-hold-until-supported");
6087 cupsArrayAdd(ra
, "job-priority");
6088 cupsArrayAdd(ra
, "job-priority-default");
6089 cupsArrayAdd(ra
, "job-priority-supported");
6090 cupsArrayAdd(ra
, "job-sheets");
6091 cupsArrayAdd(ra
, "job-sheets-default");
6092 cupsArrayAdd(ra
, "job-sheets-supported");
6093 cupsArrayAdd(ra
, "media");
6094 cupsArrayAdd(ra
, "media-default");
6095 cupsArrayAdd(ra
, "media-supported");
6096 cupsArrayAdd(ra
, "multiple-document-handling");
6097 cupsArrayAdd(ra
, "multiple-document-handling-default");
6098 cupsArrayAdd(ra
, "multiple-document-handling-supported");
6099 cupsArrayAdd(ra
, "number-up");
6100 cupsArrayAdd(ra
, "number-up-default");
6101 cupsArrayAdd(ra
, "number-up-supported");
6102 cupsArrayAdd(ra
, "orientation-requested");
6103 cupsArrayAdd(ra
, "orientation-requested-default");
6104 cupsArrayAdd(ra
, "orientation-requested-supported");
6105 cupsArrayAdd(ra
, "page-ranges");
6106 cupsArrayAdd(ra
, "page-ranges-supported");
6107 cupsArrayAdd(ra
, "printer-resolution");
6108 cupsArrayAdd(ra
, "printer-resolution-default");
6109 cupsArrayAdd(ra
, "printer-resolution-supported");
6110 cupsArrayAdd(ra
, "print-quality");
6111 cupsArrayAdd(ra
, "print-quality-default");
6112 cupsArrayAdd(ra
, "print-quality-supported");
6113 cupsArrayAdd(ra
, "sides");
6114 cupsArrayAdd(ra
, "sides-default");
6115 cupsArrayAdd(ra
, "sides-supported");
6117 else if (!strcmp(value
, "job-description"))
6119 cupsArrayAdd(ra
, "date-time-at-completed");
6120 cupsArrayAdd(ra
, "date-time-at-creation");
6121 cupsArrayAdd(ra
, "date-time-at-processing");
6122 cupsArrayAdd(ra
, "job-detailed-status-message");
6123 cupsArrayAdd(ra
, "job-document-access-errors");
6124 cupsArrayAdd(ra
, "job-id");
6125 cupsArrayAdd(ra
, "job-impressions");
6126 cupsArrayAdd(ra
, "job-impressions-completed");
6127 cupsArrayAdd(ra
, "job-k-octets");
6128 cupsArrayAdd(ra
, "job-k-octets-processed");
6129 cupsArrayAdd(ra
, "job-media-progress");
6130 cupsArrayAdd(ra
, "job-media-sheets");
6131 cupsArrayAdd(ra
, "job-media-sheets-completed");
6132 cupsArrayAdd(ra
, "job-message-from-operator");
6133 cupsArrayAdd(ra
, "job-more-info");
6134 cupsArrayAdd(ra
, "job-name");
6135 cupsArrayAdd(ra
, "job-originating-user-name");
6136 cupsArrayAdd(ra
, "job-printer-up-time");
6137 cupsArrayAdd(ra
, "job-printer-uri");
6138 cupsArrayAdd(ra
, "job-state");
6139 cupsArrayAdd(ra
, "job-state-message");
6140 cupsArrayAdd(ra
, "job-state-reasons");
6141 cupsArrayAdd(ra
, "job-uri");
6142 cupsArrayAdd(ra
, "number-of-documents");
6143 cupsArrayAdd(ra
, "number-of-intervening-jobs");
6144 cupsArrayAdd(ra
, "output-device-assigned");
6145 cupsArrayAdd(ra
, "time-at-completed");
6146 cupsArrayAdd(ra
, "time-at-creation");
6147 cupsArrayAdd(ra
, "time-at-processing");
6149 else if (!strcmp(value
, "printer-description"))
6151 cupsArrayAdd(ra
, "charset-configured");
6152 cupsArrayAdd(ra
, "charset-supported");
6153 cupsArrayAdd(ra
, "color-supported");
6154 cupsArrayAdd(ra
, "compression-supported");
6155 cupsArrayAdd(ra
, "document-format-default");
6156 cupsArrayAdd(ra
, "document-format-supported");
6157 cupsArrayAdd(ra
, "generated-natural-language-supported");
6158 cupsArrayAdd(ra
, "ipp-versions-supported");
6159 cupsArrayAdd(ra
, "job-impressions-supported");
6160 cupsArrayAdd(ra
, "job-k-octets-supported");
6161 cupsArrayAdd(ra
, "job-media-sheets-supported");
6162 cupsArrayAdd(ra
, "job-settable-attributes-supported");
6163 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
6164 cupsArrayAdd(ra
, "multiple-operation-time-out");
6165 cupsArrayAdd(ra
, "natural-language-configured");
6166 cupsArrayAdd(ra
, "notify-attributes-supported");
6167 cupsArrayAdd(ra
, "notify-lease-duration-default");
6168 cupsArrayAdd(ra
, "notify-lease-duration-supported");
6169 cupsArrayAdd(ra
, "notify-max-events-supported");
6170 cupsArrayAdd(ra
, "notify-events-default");
6171 cupsArrayAdd(ra
, "notify-events-supported");
6172 cupsArrayAdd(ra
, "notify-pull-method-supported");
6173 cupsArrayAdd(ra
, "notify-schemes-supported");
6174 cupsArrayAdd(ra
, "operations-supported");
6175 cupsArrayAdd(ra
, "pages-per-minute");
6176 cupsArrayAdd(ra
, "pages-per-minute-color");
6177 cupsArrayAdd(ra
, "pdl-override-supported");
6178 cupsArrayAdd(ra
, "printer-alert");
6179 cupsArrayAdd(ra
, "printer-alert-description");
6180 cupsArrayAdd(ra
, "printer-commands");
6181 cupsArrayAdd(ra
, "printer-current-time");
6182 cupsArrayAdd(ra
, "printer-driver-installer");
6183 cupsArrayAdd(ra
, "printer-dns-sd-name");
6184 cupsArrayAdd(ra
, "printer-info");
6185 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
6186 cupsArrayAdd(ra
, "printer-location");
6187 cupsArrayAdd(ra
, "printer-make-and-model");
6188 cupsArrayAdd(ra
, "printer-message-from-operator");
6189 cupsArrayAdd(ra
, "printer-more-info");
6190 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
6191 cupsArrayAdd(ra
, "printer-name");
6192 cupsArrayAdd(ra
, "printer-state");
6193 cupsArrayAdd(ra
, "printer-state-message");
6194 cupsArrayAdd(ra
, "printer-state-reasons");
6195 cupsArrayAdd(ra
, "printer-settable-attributes-supported");
6196 cupsArrayAdd(ra
, "printer-type");
6197 cupsArrayAdd(ra
, "printer-up-time");
6198 cupsArrayAdd(ra
, "printer-uri-supported");
6199 cupsArrayAdd(ra
, "queued-job-count");
6200 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
6201 cupsArrayAdd(ra
, "uri-authentication-supported");
6202 cupsArrayAdd(ra
, "uri-security-supported");
6204 else if (!strcmp(value
, "printer-defaults"))
6206 char *name
; /* Option name */
6209 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
6211 name
= (char *)cupsArrayNext(CommonDefaults
))
6212 cupsArrayAdd(ra
, name
);
6214 else if (!strcmp(value
, "subscription-template"))
6216 cupsArrayAdd(ra
, "notify-attributes");
6217 cupsArrayAdd(ra
, "notify-charset");
6218 cupsArrayAdd(ra
, "notify-events");
6219 cupsArrayAdd(ra
, "notify-lease-duration");
6220 cupsArrayAdd(ra
, "notify-natural-language");
6221 cupsArrayAdd(ra
, "notify-pull-method");
6222 cupsArrayAdd(ra
, "notify-recipient-uri");
6223 cupsArrayAdd(ra
, "notify-time-interval");
6224 cupsArrayAdd(ra
, "notify-user-data");
6227 cupsArrayAdd(ra
, value
);
6235 * 'create_subscription()' - Create a notification subscription.
6239 create_subscription(
6240 cupsd_client_t
*con
, /* I - Client connection */
6241 ipp_attribute_t
*uri
) /* I - Printer URI */
6243 http_status_t status
; /* Policy status */
6244 int i
; /* Looping var */
6245 ipp_attribute_t
*attr
; /* Current attribute */
6246 cups_ptype_t dtype
; /* Destination type (printer/class) */
6247 char scheme
[HTTP_MAX_URI
],
6248 /* Scheme portion of URI */
6249 userpass
[HTTP_MAX_URI
],
6250 /* Username portion of URI */
6252 /* Host portion of URI */
6253 resource
[HTTP_MAX_URI
];
6254 /* Resource portion of URI */
6255 int port
; /* Port portion of URI */
6256 cupsd_printer_t
*printer
; /* Printer/class */
6257 cupsd_job_t
*job
; /* Job */
6258 int jobid
; /* Job ID */
6259 cupsd_subscription_t
*sub
; /* Subscription object */
6260 const char *username
, /* requesting-user-name or
6261 authenticated username */
6262 *recipient
, /* notify-recipient-uri */
6263 *pullmethod
; /* notify-pull-method */
6264 ipp_attribute_t
*user_data
; /* notify-user-data */
6265 int interval
, /* notify-time-interval */
6266 lease
; /* notify-lease-duration */
6267 unsigned mask
; /* notify-events */
6268 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
6269 *notify_lease
; /* notify-lease-duration(-default) */
6273 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
6275 if (attr
->group_tag
!= IPP_TAG_ZERO
)
6276 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
6277 attr
->value_tag
, attr
->name
);
6279 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
6284 * Is the destination valid?
6287 cupsdLogMessage(CUPSD_LOG_DEBUG
,
6288 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
6289 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
6291 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6292 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
6293 sizeof(host
), &port
, resource
, sizeof(resource
));
6295 if (!strcmp(resource
, "/"))
6297 dtype
= (cups_ptype_t
)0;
6300 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6302 dtype
= (cups_ptype_t
)0;
6305 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6307 dtype
= CUPS_PRINTER_CLASS
;
6310 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6316 send_ipp_status(con
, IPP_NOT_FOUND
,
6317 _("The printer or class was not found."));
6327 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6330 send_http_error(con
, status
, printer
);
6334 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6336 send_http_error(con
, status
, NULL
);
6341 * Get the user that is requesting the subscription...
6344 username
= get_username(con
);
6347 * Find the first subscription group attribute; return if we have
6351 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
6352 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
6357 send_ipp_status(con
, IPP_BAD_REQUEST
,
6358 _("No subscription attributes in request"));
6363 * Process the subscription attributes in the request...
6366 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
6374 lease
= DefaultLeaseDuration
;
6376 mask
= CUPSD_EVENT_NONE
;
6380 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
6382 notify_lease
= ippFindAttribute(printer
->attrs
,
6383 "notify-lease-duration-default",
6387 lease
= notify_lease
->values
[0].integer
;
6391 notify_events
= NULL
;
6392 notify_lease
= NULL
;
6395 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
6397 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
6398 attr
->value_tag
== IPP_TAG_URI
)
6401 * Validate the recipient scheme against the ServerBin/notifier
6405 char notifier
[1024]; /* Notifier filename */
6408 recipient
= attr
->values
[0].string
.text
;
6410 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
6411 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
6412 host
, sizeof(host
), &port
,
6413 resource
, sizeof(resource
)) < HTTP_URI_OK
)
6415 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6416 _("Bad notify-recipient-uri URI \"%s\""), recipient
);
6417 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6418 "notify-status-code", IPP_URI_SCHEME
);
6422 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
6424 if (access(notifier
, X_OK
))
6426 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6427 _("notify-recipient-uri URI \"%s\" uses unknown "
6428 "scheme"), recipient
);
6429 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6430 "notify-status-code", IPP_URI_SCHEME
);
6434 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
6436 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6437 _("notify-recipient-uri URI \"%s\" is already used"),
6439 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6440 "notify-status-code", IPP_ATTRIBUTES
);
6444 else if (!strcmp(attr
->name
, "notify-pull-method") &&
6445 attr
->value_tag
== IPP_TAG_KEYWORD
)
6447 pullmethod
= attr
->values
[0].string
.text
;
6449 if (strcmp(pullmethod
, "ippget"))
6451 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6452 _("Bad notify-pull-method \"%s\""), pullmethod
);
6453 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6454 "notify-status-code", IPP_ATTRIBUTES
);
6458 else if (!strcmp(attr
->name
, "notify-charset") &&
6459 attr
->value_tag
== IPP_TAG_CHARSET
&&
6460 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
6461 strcmp(attr
->values
[0].string
.text
, "utf-8"))
6463 send_ipp_status(con
, IPP_CHARSET
,
6464 _("Character set \"%s\" not supported"),
6465 attr
->values
[0].string
.text
);
6468 else if (!strcmp(attr
->name
, "notify-natural-language") &&
6469 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
6470 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
6472 send_ipp_status(con
, IPP_CHARSET
,
6473 _("Language \"%s\" not supported"),
6474 attr
->values
[0].string
.text
);
6477 else if (!strcmp(attr
->name
, "notify-user-data") &&
6478 attr
->value_tag
== IPP_TAG_STRING
)
6480 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
6482 send_ipp_status(con
, IPP_REQUEST_VALUE
,
6483 _("The notify-user-data value is too large "
6484 "(%d > 63 octets)"),
6485 attr
->values
[0].unknown
.length
);
6491 else if (!strcmp(attr
->name
, "notify-events") &&
6492 attr
->value_tag
== IPP_TAG_KEYWORD
)
6493 notify_events
= attr
;
6494 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
6495 attr
->value_tag
== IPP_TAG_INTEGER
)
6496 lease
= attr
->values
[0].integer
;
6497 else if (!strcmp(attr
->name
, "notify-time-interval") &&
6498 attr
->value_tag
== IPP_TAG_INTEGER
)
6499 interval
= attr
->values
[0].integer
;
6500 else if (!strcmp(attr
->name
, "notify-job-id") &&
6501 attr
->value_tag
== IPP_TAG_INTEGER
)
6502 jobid
= attr
->values
[0].integer
;
6509 for (i
= 0; i
< notify_events
->num_values
; i
++)
6510 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
6514 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
6516 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
6517 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
6518 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
6520 if (!recipient
&& !pullmethod
)
6523 if (mask
== CUPSD_EVENT_NONE
)
6526 mask
= CUPSD_EVENT_JOB_COMPLETED
;
6528 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
6531 send_ipp_status(con
, IPP_BAD_REQUEST
,
6532 _("notify-events not specified"));
6537 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
6539 cupsdLogMessage(CUPSD_LOG_INFO
,
6540 "create_subscription: Limiting notify-lease-duration to "
6543 lease
= MaxLeaseDuration
;
6548 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6550 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found"), jobid
);
6557 if ((sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0)) == NULL
)
6559 send_ipp_status(con
, IPP_TOO_MANY_SUBSCRIPTIONS
,
6560 _("There are too many subscriptions."));
6565 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
6568 cupsdLogMessage(CUPSD_LOG_DEBUG
,
6569 "Added subscription %d for printer \"%s\"",
6570 sub
->id
, printer
->name
);
6572 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
6575 sub
->interval
= interval
;
6577 sub
->expire
= lease
? time(NULL
) + lease
: 0;
6579 cupsdSetString(&sub
->owner
, username
);
6583 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
6584 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
6585 sub
->user_data_len
);
6588 ippAddSeparator(con
->response
);
6589 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
6590 "notify-subscription-id", sub
->id
);
6592 con
->response
->request
.status
.status_code
= IPP_OK
;
6598 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
6603 * 'delete_printer()' - Remove a printer or class from the system.
6607 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
6608 ipp_attribute_t
*uri
) /* I - URI of printer or class */
6610 http_status_t status
; /* Policy status */
6611 cups_ptype_t dtype
; /* Destination type (printer/class) */
6612 cupsd_printer_t
*printer
; /* Printer/class */
6613 char filename
[1024]; /* Script/PPD filename */
6616 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
6617 con
->http
.fd
, uri
->values
[0].string
.text
);
6620 * Do we have a valid URI?
6623 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6629 send_ipp_status(con
, IPP_NOT_FOUND
,
6630 _("The printer or class was not found."));
6638 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6640 send_http_error(con
, status
, NULL
);
6645 * Remove old jobs...
6648 cupsdCancelJobs(printer
->name
, NULL
, 1);
6651 * Remove old subscriptions and send a "deleted printer" event...
6654 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
6655 "%s \"%s\" deleted by \"%s\".",
6656 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
6657 printer
->name
, get_username(con
));
6659 cupsdExpireSubscriptions(printer
, NULL
);
6662 * Remove any old PPD or script files...
6665 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
6669 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6673 snprintf(filename
, sizeof(filename
), "%s/%s.ipp2", CacheDir
, printer
->name
);
6676 snprintf(filename
, sizeof(filename
), "%s/%s.pwg", CacheDir
, printer
->name
);
6681 * Unregister color profiles...
6684 apple_unregister_profiles(printer
);
6685 #endif /* __APPLE__ */
6687 if (dtype
& CUPS_PRINTER_CLASS
)
6689 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
6690 printer
->name
, get_username(con
));
6692 cupsdDeletePrinter(printer
, 0);
6693 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6697 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
6698 printer
->name
, get_username(con
));
6700 if (cupsdDeletePrinter(printer
, 0))
6701 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6703 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
6706 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
6709 * Return with no errors...
6712 con
->response
->request
.status
.status_code
= IPP_OK
;
6717 * 'get_default()' - Get the default destination.
6721 get_default(cupsd_client_t
*con
) /* I - Client connection */
6723 http_status_t status
; /* Policy status */
6724 cups_array_t
*ra
; /* Requested attributes array */
6727 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
6733 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6735 send_http_error(con
, status
, NULL
);
6741 ra
= create_requested_array(con
->request
);
6743 copy_printer_attrs(con
, DefaultPrinter
, ra
);
6745 cupsArrayDelete(ra
);
6747 con
->response
->request
.status
.status_code
= IPP_OK
;
6750 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
6755 * 'get_devices()' - Get the list of available devices on the local system.
6759 get_devices(cupsd_client_t
*con
) /* I - Client connection */
6761 http_status_t status
; /* Policy status */
6762 ipp_attribute_t
*limit
, /* limit attribute */
6763 *timeout
, /* timeout attribute */
6764 *requested
, /* requested-attributes attribute */
6765 *exclude
, /* exclude-schemes attribute */
6766 *include
; /* include-schemes attribute */
6767 char command
[1024], /* cups-deviced command */
6768 options
[2048], /* Options to pass to command */
6770 /* String for requested attributes */
6772 /* String for excluded schemes */
6774 /* String for included schemes */
6777 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
6783 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6785 send_http_error(con
, status
, NULL
);
6790 * Run cups-deviced command with the given options...
6793 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6794 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
6795 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6797 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
6798 include
= ippFindAttribute(con
->request
, "include-schemes", IPP_TAG_NAME
);
6801 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6803 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6806 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6808 exclude_str
[0] = '\0';
6811 url_encode_attr(include
, include_str
, sizeof(include_str
));
6813 include_str
[0] = '\0';
6815 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
6816 snprintf(options
, sizeof(options
),
6817 "%d+%d+%d+%d+%s%s%s%s%s",
6818 con
->request
->request
.op
.request_id
,
6819 limit
? limit
->values
[0].integer
: 0,
6820 timeout
? timeout
->values
[0].integer
: 15,
6823 exclude_str
[0] ? "%20" : "", exclude_str
,
6824 include_str
[0] ? "%20" : "", include_str
);
6826 if (cupsdSendCommand(con
, command
, options
, 1))
6829 * Command started successfully, don't send an IPP response here...
6832 ippDelete(con
->response
);
6833 con
->response
= NULL
;
6838 * Command failed, return "internal error" so the user knows something
6842 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6843 _("cups-deviced failed to execute."));
6849 * 'get_document()' - Get a copy of a job file.
6853 get_document(cupsd_client_t
*con
, /* I - Client connection */
6854 ipp_attribute_t
*uri
) /* I - Job URI */
6856 http_status_t status
; /* Policy status */
6857 ipp_attribute_t
*attr
; /* Current attribute */
6858 int jobid
; /* Job ID */
6859 int docnum
; /* Document number */
6860 cupsd_job_t
*job
; /* Current job */
6861 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
6862 username
[HTTP_MAX_URI
], /* Username portion of URI */
6863 host
[HTTP_MAX_URI
], /* Host portion of URI */
6864 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6865 int port
; /* Port portion of URI */
6866 char filename
[1024], /* Filename for document */
6867 format
[1024]; /* Format for document */
6870 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6871 con
->http
.fd
, uri
->values
[0].string
.text
);
6874 * See if we have a job URI or a printer URI...
6877 if (!strcmp(uri
->name
, "printer-uri"))
6880 * Got a printer URI; see if we also have a job-id attribute...
6883 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6884 IPP_TAG_INTEGER
)) == NULL
)
6886 send_ipp_status(con
, IPP_BAD_REQUEST
,
6887 _("Got a printer-uri attribute but no job-id"));
6891 jobid
= attr
->values
[0].integer
;
6896 * Got a job URI; parse it to get the job ID...
6899 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6900 sizeof(scheme
), username
, sizeof(username
), host
,
6901 sizeof(host
), &port
, resource
, sizeof(resource
));
6903 if (strncmp(resource
, "/jobs/", 6))
6909 send_ipp_status(con
, IPP_BAD_REQUEST
,
6910 _("Bad job-uri attribute \"%s\""),
6911 uri
->values
[0].string
.text
);
6915 jobid
= atoi(resource
+ 6);
6919 * See if the job exists...
6922 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6925 * Nope - return a "not found" error...
6928 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist"), jobid
);
6936 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6938 send_http_error(con
, status
, NULL
);
6943 * Get the document number...
6946 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6947 IPP_TAG_INTEGER
)) == NULL
)
6949 send_ipp_status(con
, IPP_BAD_REQUEST
,
6950 _("Missing document-number attribute"));
6954 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6955 attr
->num_values
> 1)
6957 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
6962 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6964 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6966 cupsdLogMessage(CUPSD_LOG_ERROR
,
6967 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6969 send_ipp_status(con
, IPP_NOT_FOUND
,
6970 _("Unable to open document %d in job %d"), docnum
, jobid
);
6974 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6978 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6979 job
->filetypes
[docnum
- 1]->type
);
6981 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6983 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6985 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6986 IPP_TAG_NAME
)) != NULL
)
6987 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6988 NULL
, attr
->values
[0].string
.text
);
6993 * 'get_job_attrs()' - Get job attributes.
6997 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6998 ipp_attribute_t
*uri
) /* I - Job URI */
7000 http_status_t status
; /* Policy status */
7001 ipp_attribute_t
*attr
; /* Current attribute */
7002 int jobid
; /* Job ID */
7003 cupsd_job_t
*job
; /* Current job */
7004 cupsd_printer_t
*printer
; /* Current printer */
7005 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
7006 username
[HTTP_MAX_URI
], /* Username portion of URI */
7007 host
[HTTP_MAX_URI
], /* Host portion of URI */
7008 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7009 int port
; /* Port portion of URI */
7010 cups_array_t
*ra
; /* Requested attributes array */
7013 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
7014 con
->http
.fd
, uri
->values
[0].string
.text
);
7017 * See if we have a job URI or a printer URI...
7020 if (!strcmp(uri
->name
, "printer-uri"))
7023 * Got a printer URI; see if we also have a job-id attribute...
7026 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7027 IPP_TAG_INTEGER
)) == NULL
)
7029 send_ipp_status(con
, IPP_BAD_REQUEST
,
7030 _("Got a printer-uri attribute but no job-id"));
7034 jobid
= attr
->values
[0].integer
;
7039 * Got a job URI; parse it to get the job ID...
7042 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7043 sizeof(scheme
), username
, sizeof(username
), host
,
7044 sizeof(host
), &port
, resource
, sizeof(resource
));
7046 if (strncmp(resource
, "/jobs/", 6))
7052 send_ipp_status(con
, IPP_BAD_REQUEST
,
7053 _("Bad job-uri attribute \"%s\""),
7054 uri
->values
[0].string
.text
);
7058 jobid
= atoi(resource
+ 6);
7062 * See if the job exists...
7065 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7068 * Nope - return a "not found" error...
7071 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist"), jobid
);
7079 if ((printer
= job
->printer
) == NULL
)
7080 printer
= cupsdFindDest(job
->dest
);
7084 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
7087 send_http_error(con
, status
, printer
);
7091 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7093 send_http_error(con
, status
, NULL
);
7098 * Copy attributes...
7103 ra
= create_requested_array(con
->request
);
7104 copy_job_attrs(con
, job
, ra
);
7105 cupsArrayDelete(ra
);
7107 con
->response
->request
.status
.status_code
= IPP_OK
;
7112 * 'get_jobs()' - Get a list of jobs for the specified printer.
7116 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
7117 ipp_attribute_t
*uri
) /* I - Printer URI */
7119 http_status_t status
; /* Policy status */
7120 ipp_attribute_t
*attr
; /* Current attribute */
7121 const char *dest
; /* Destination */
7122 cups_ptype_t dtype
; /* Destination type (printer/class) */
7123 cups_ptype_t dmask
; /* Destination type mask */
7124 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7125 username
[HTTP_MAX_URI
], /* Username portion of URI */
7126 host
[HTTP_MAX_URI
], /* Host portion of URI */
7127 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7128 int port
; /* Port portion of URI */
7129 int job_comparison
; /* Job comparison */
7130 ipp_jstate_t job_state
; /* job-state value */
7131 int first_job_id
; /* First job ID */
7132 int limit
; /* Maximum number of jobs to return */
7133 int count
; /* Number of jobs that match */
7134 ipp_attribute_t
*job_ids
; /* job-ids attribute */
7135 cupsd_job_t
*job
; /* Current job pointer */
7136 cupsd_printer_t
*printer
; /* Printer */
7137 cups_array_t
*list
; /* Which job list... */
7138 cups_array_t
*ra
; /* Requested attributes array */
7141 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
7142 uri
->values
[0].string
.text
);
7145 * Is the destination valid?
7148 if (strcmp(uri
->name
, "printer-uri"))
7150 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No printer-uri in request"));
7154 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7155 sizeof(scheme
), username
, sizeof(username
), host
,
7156 sizeof(host
), &port
, resource
, sizeof(resource
));
7158 if (!strcmp(resource
, "/") || !strcmp(resource
, "/jobs"))
7161 dtype
= (cups_ptype_t
)0;
7162 dmask
= (cups_ptype_t
)0;
7165 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
7168 dtype
= (cups_ptype_t
)0;
7169 dmask
= CUPS_PRINTER_CLASS
;
7172 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
7175 dtype
= CUPS_PRINTER_CLASS
;
7176 dmask
= CUPS_PRINTER_CLASS
;
7179 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
7186 send_ipp_status(con
, IPP_NOT_FOUND
,
7187 _("The printer or class was not found."));
7192 dtype
&= CUPS_PRINTER_CLASS
;
7193 dmask
= CUPS_PRINTER_CLASS
;
7202 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
7205 send_http_error(con
, status
, printer
);
7209 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7211 send_http_error(con
, status
, NULL
);
7215 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
7218 * See if the "which-jobs" attribute have been specified...
7221 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
7222 IPP_TAG_KEYWORD
)) != NULL
&& job_ids
)
7224 send_ipp_status(con
, IPP_CONFLICT
,
7225 _("The %s attribute cannot be provided with job-ids."),
7229 else if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "not-completed"))
7231 job_comparison
= -1;
7232 job_state
= IPP_JOB_STOPPED
;
7235 else if (!strcmp(attr
->values
[0].string
.text
, "completed"))
7238 job_state
= IPP_JOB_CANCELED
;
7241 else if (!strcmp(attr
->values
[0].string
.text
, "aborted"))
7244 job_state
= IPP_JOB_ABORTED
;
7247 else if (!strcmp(attr
->values
[0].string
.text
, "all"))
7250 job_state
= IPP_JOB_PENDING
;
7253 else if (!strcmp(attr
->values
[0].string
.text
, "canceled"))
7256 job_state
= IPP_JOB_CANCELED
;
7259 else if (!strcmp(attr
->values
[0].string
.text
, "pending"))
7262 job_state
= IPP_JOB_PENDING
;
7265 else if (!strcmp(attr
->values
[0].string
.text
, "pending-held"))
7268 job_state
= IPP_JOB_HELD
;
7271 else if (!strcmp(attr
->values
[0].string
.text
, "processing"))
7274 job_state
= IPP_JOB_PROCESSING
;
7275 list
= PrintingJobs
;
7277 else if (!strcmp(attr
->values
[0].string
.text
, "processing-stopped"))
7280 job_state
= IPP_JOB_STOPPED
;
7285 send_ipp_status(con
, IPP_ATTRIBUTES
,
7286 _("The which-jobs value \"%s\" is not supported."),
7287 attr
->values
[0].string
.text
);
7288 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7289 "which-jobs", NULL
, attr
->values
[0].string
.text
);
7294 * See if they want to limit the number of jobs reported...
7297 if ((attr
= ippFindAttribute(con
->request
, "limit",
7298 IPP_TAG_INTEGER
)) != NULL
)
7302 send_ipp_status(con
, IPP_CONFLICT
,
7303 _("The %s attribute cannot be provided with job-ids."),
7308 limit
= attr
->values
[0].integer
;
7313 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
7314 IPP_TAG_INTEGER
)) != NULL
)
7318 send_ipp_status(con
, IPP_CONFLICT
,
7319 _("The %s attribute cannot be provided with job-ids."),
7324 first_job_id
= attr
->values
[0].integer
;
7330 * See if we only want to see jobs for a specific user...
7333 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
7334 IPP_TAG_BOOLEAN
)) != NULL
&& job_ids
)
7336 send_ipp_status(con
, IPP_CONFLICT
,
7337 _("The %s attribute cannot be provided with job-ids."),
7341 else if (attr
&& attr
->values
[0].boolean
)
7342 strlcpy(username
, get_username(con
), sizeof(username
));
7346 if ((ra
= create_requested_array(con
->request
)) == NULL
&&
7347 !ippFindAttribute(con
->request
, "requested-attributes", IPP_TAG_KEYWORD
))
7350 * IPP conformance - Get-Jobs has a default requested-attributes value of
7351 * "job-id" and "job-uri".
7354 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
7355 cupsArrayAdd(ra
, "job-id");
7356 cupsArrayAdd(ra
, "job-uri");
7360 * OK, build a list of jobs for this printer...
7365 int i
; /* Looping var */
7367 for (i
= 0; i
< job_ids
->num_values
; i
++)
7369 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
)
7373 if (i
< job_ids
->num_values
)
7375 send_ipp_status(con
, IPP_NOT_FOUND
, _("job-id %d not found."),
7376 job_ids
->values
[i
].integer
);
7380 for (i
= 0; i
< job_ids
->num_values
; i
++)
7382 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
7388 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d",
7394 ippAddSeparator(con
->response
);
7396 copy_job_attrs(con
, job
, ra
);
7401 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
7402 (limit
<= 0 || count
< limit
) && job
;
7403 job
= (cupsd_job_t
*)cupsArrayNext(list
))
7406 * Filter out jobs that don't match...
7409 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7410 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
7411 "state_value=%d, attrs=%p", job
->id
, job
->dest
,
7412 job
->username
, job
->state_value
, job
->attrs
);
7414 if (!job
->dest
|| !job
->username
)
7417 if (!job
->dest
|| !job
->username
)
7420 if ((dest
&& strcmp(job
->dest
, dest
)) &&
7421 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
7423 if ((job
->dtype
& dmask
) != dtype
&&
7424 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
7427 if ((job_comparison
< 0 && job
->state_value
> job_state
) ||
7428 (job_comparison
== 0 && job
->state_value
!= job_state
) ||
7429 (job_comparison
> 0 && job
->state_value
< job_state
))
7432 if (job
->id
< first_job_id
)
7439 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d",
7444 if (username
[0] && strcasecmp(username
, job
->username
))
7448 ippAddSeparator(con
->response
);
7452 copy_job_attrs(con
, job
, ra
);
7455 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count=%d", count
);
7458 cupsArrayDelete(ra
);
7460 con
->response
->request
.status
.status_code
= IPP_OK
;
7465 * 'get_notifications()' - Get events for a subscription.
7469 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
7471 int i
, j
; /* Looping vars */
7472 http_status_t status
; /* Policy status */
7473 cupsd_subscription_t
*sub
; /* Subscription */
7474 ipp_attribute_t
*ids
, /* notify-subscription-ids */
7475 *sequences
; /* notify-sequence-numbers */
7476 int min_seq
; /* Minimum sequence number */
7477 int interval
; /* Poll interval */
7480 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
7484 * Get subscription attributes...
7487 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
7489 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
7494 send_ipp_status(con
, IPP_BAD_REQUEST
,
7495 _("Missing notify-subscription-ids attribute"));
7500 * Are the subscription IDs valid?
7503 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
7505 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
7508 * Bad subscription ID...
7511 send_ipp_status(con
, IPP_NOT_FOUND
,
7512 _("notify-subscription-id %d no good"),
7513 ids
->values
[i
].integer
);
7521 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7523 con
, sub
->owner
)) != HTTP_OK
)
7525 send_http_error(con
, status
, sub
->dest
);
7530 * Check the subscription type and update the interval accordingly.
7533 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
7536 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
7538 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
7544 * Tell the client to poll again in N seconds...
7548 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
7549 "notify-get-interval", interval
);
7551 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
7552 "printer-up-time", time(NULL
));
7555 * Copy the subscription event attributes to the response.
7558 con
->response
->request
.status
.status_code
=
7559 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
7561 for (i
= 0; i
< ids
->num_values
; i
++)
7564 * Get the subscription and sequence number...
7567 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
7569 if (sequences
&& i
< sequences
->num_values
)
7570 min_seq
= sequences
->values
[i
].integer
;
7575 * If we don't have any new events, nothing to do here...
7578 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
7582 * Otherwise copy all of the new events...
7585 if (sub
->first_event_id
> min_seq
)
7588 j
= min_seq
- sub
->first_event_id
;
7590 for (; j
< sub
->num_events
; j
++)
7592 ippAddSeparator(con
->response
);
7594 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
7595 IPP_TAG_EVENT_NOTIFICATION
, 0);
7602 * 'get_ppd()' - Get a named PPD from the local system.
7606 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
7607 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
7609 http_status_t status
; /* Policy status */
7610 cupsd_printer_t
*dest
; /* Destination */
7611 cups_ptype_t dtype
; /* Destination type */
7614 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
7615 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
7617 if (!strcmp(uri
->name
, "ppd-name"))
7620 * Return a PPD file from cups-driverd...
7623 char command
[1024], /* cups-driverd command */
7624 options
[1024], /* Options to pass to command */
7625 ppd_name
[1024]; /* ppd-name */
7632 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7634 send_http_error(con
, status
, NULL
);
7639 * Run cups-driverd command with the given options...
7642 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7643 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
7644 snprintf(options
, sizeof(options
), "get+%d+%s",
7645 con
->request
->request
.op
.request_id
, ppd_name
);
7647 if (cupsdSendCommand(con
, command
, options
, 0))
7650 * Command started successfully, don't send an IPP response here...
7653 ippDelete(con
->response
);
7654 con
->response
= NULL
;
7659 * Command failed, return "internal error" so the user knows something
7663 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7664 _("cups-driverd failed to execute."));
7667 else if (!strcmp(uri
->name
, "printer-uri") &&
7668 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
7670 int i
; /* Looping var */
7671 char filename
[1024]; /* PPD filename */
7678 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7680 send_http_error(con
, status
, dest
);
7685 * See if we need the PPD for a class or remote printer...
7688 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7691 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
7693 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7694 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7695 "printer-uri", NULL
, dest
->uri
);
7698 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
7700 for (i
= 0; i
< dest
->num_printers
; i
++)
7701 if (!(dest
->printers
[i
]->type
&
7702 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
7704 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7705 dest
->printers
[i
]->name
);
7707 if (!access(filename
, 0))
7711 if (i
< dest
->num_printers
)
7712 dest
= dest
->printers
[i
];
7715 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7716 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7717 "printer-uri", NULL
, dest
->printers
[0]->uri
);
7723 * Found the printer with the PPD file, now see if there is one...
7726 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
7728 send_ipp_status(con
, IPP_NOT_FOUND
,
7729 _("The PPD file \"%s\" could not be opened: %s"),
7730 uri
->values
[0].string
.text
, strerror(errno
));
7734 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
7738 con
->response
->request
.status
.status_code
= IPP_OK
;
7741 send_ipp_status(con
, IPP_NOT_FOUND
,
7742 _("The PPD file \"%s\" could not be found."),
7743 uri
->values
[0].string
.text
);
7748 * 'get_ppds()' - Get the list of PPD files on the local system.
7752 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
7754 http_status_t status
; /* Policy status */
7755 ipp_attribute_t
*limit
, /* Limit attribute */
7756 *device
, /* ppd-device-id attribute */
7757 *language
, /* ppd-natural-language attribute */
7758 *make
, /* ppd-make attribute */
7759 *model
, /* ppd-make-and-model attribute */
7760 *model_number
, /* ppd-model-number attribute */
7761 *product
, /* ppd-product attribute */
7762 *psversion
, /* ppd-psverion attribute */
7763 *type
, /* ppd-type attribute */
7764 *requested
, /* requested-attributes attribute */
7765 *exclude
, /* exclude-schemes attribute */
7766 *include
; /* include-schemes attribute */
7767 char command
[1024], /* cups-driverd command */
7768 options
[4096], /* Options to pass to command */
7769 device_str
[256],/* Escaped ppd-device-id string */
7771 /* Escaped ppd-natural-language */
7772 make_str
[256], /* Escaped ppd-make string */
7773 model_str
[256], /* Escaped ppd-make-and-model string */
7774 model_number_str
[256],
7775 /* ppd-model-number string */
7777 /* Escaped ppd-product string */
7779 /* Escaped ppd-psversion string */
7780 type_str
[256], /* Escaped ppd-type string */
7782 /* String for requested attributes */
7784 /* String for excluded schemes */
7786 /* String for included schemes */
7789 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
7795 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7797 send_http_error(con
, status
, NULL
);
7802 * Run cups-driverd command with the given options...
7805 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
7806 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
7807 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
7809 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
7810 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
7812 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
7814 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
7815 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
7816 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
7817 requested
= ippFindAttribute(con
->request
, "requested-attributes",
7819 exclude
= ippFindAttribute(con
->request
, "exclude-schemes",
7821 include
= ippFindAttribute(con
->request
, "include-schemes",
7825 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
7827 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
7830 url_encode_attr(device
, device_str
, sizeof(device_str
));
7832 device_str
[0] = '\0';
7835 url_encode_attr(language
, language_str
, sizeof(language_str
));
7837 language_str
[0] = '\0';
7840 url_encode_attr(make
, make_str
, sizeof(make_str
));
7845 url_encode_attr(model
, model_str
, sizeof(model_str
));
7847 model_str
[0] = '\0';
7850 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
7851 model_number
->values
[0].integer
);
7853 model_number_str
[0] = '\0';
7856 url_encode_attr(product
, product_str
, sizeof(product_str
));
7858 product_str
[0] = '\0';
7861 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
7863 psversion_str
[0] = '\0';
7866 url_encode_attr(type
, type_str
, sizeof(type_str
));
7871 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
7873 exclude_str
[0] = '\0';
7876 url_encode_attr(include
, include_str
, sizeof(include_str
));
7878 include_str
[0] = '\0';
7880 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7881 snprintf(options
, sizeof(options
),
7882 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7883 con
->request
->request
.op
.request_id
,
7884 limit
? limit
->values
[0].integer
: 0,
7886 device
? "%20" : "", device_str
,
7887 language
? "%20" : "", language_str
,
7888 make
? "%20" : "", make_str
,
7889 model
? "%20" : "", model_str
,
7890 model_number
? "%20" : "", model_number_str
,
7891 product
? "%20" : "", product_str
,
7892 psversion
? "%20" : "", psversion_str
,
7893 type
? "%20" : "", type_str
,
7894 exclude_str
[0] ? "%20" : "", exclude_str
,
7895 include_str
[0] ? "%20" : "", include_str
);
7897 if (cupsdSendCommand(con
, command
, options
, 0))
7900 * Command started successfully, don't send an IPP response here...
7903 ippDelete(con
->response
);
7904 con
->response
= NULL
;
7909 * Command failed, return "internal error" so the user knows something
7913 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7914 _("cups-driverd failed to execute."));
7920 * 'get_printer_attrs()' - Get printer attributes.
7924 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
7925 ipp_attribute_t
*uri
) /* I - Printer URI */
7927 http_status_t status
; /* Policy status */
7928 cups_ptype_t dtype
; /* Destination type (printer/class) */
7929 cupsd_printer_t
*printer
; /* Printer/class */
7930 cups_array_t
*ra
; /* Requested attributes array */
7933 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
7934 con
->http
.fd
, uri
->values
[0].string
.text
);
7937 * Is the destination valid?
7940 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7946 send_ipp_status(con
, IPP_NOT_FOUND
,
7947 _("The printer or class was not found."));
7955 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7957 send_http_error(con
, status
, printer
);
7962 * Send the attributes...
7965 ra
= create_requested_array(con
->request
);
7967 copy_printer_attrs(con
, printer
, ra
);
7969 cupsArrayDelete(ra
);
7971 con
->response
->request
.status
.status_code
= IPP_OK
;
7976 * 'get_printer_supported()' - Get printer supported values.
7980 get_printer_supported(
7981 cupsd_client_t
*con
, /* I - Client connection */
7982 ipp_attribute_t
*uri
) /* I - Printer URI */
7984 http_status_t status
; /* Policy status */
7985 cups_ptype_t dtype
; /* Destination type (printer/class) */
7986 cupsd_printer_t
*printer
; /* Printer/class */
7989 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_supported(%p[%d], %s)", con
,
7990 con
->http
.fd
, uri
->values
[0].string
.text
);
7993 * Is the destination valid?
7996 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8002 send_ipp_status(con
, IPP_NOT_FOUND
,
8003 _("The printer or class was not found."));
8011 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8013 send_http_error(con
, status
, printer
);
8018 * Return a list of attributes that can be set via Set-Printer-Attributes.
8021 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
8023 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
8024 "printer-location", 0);
8026 con
->response
->request
.status
.status_code
= IPP_OK
;
8031 * 'get_printers()' - Get a list of printers or classes.
8035 get_printers(cupsd_client_t
*con
, /* I - Client connection */
8036 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
8038 http_status_t status
; /* Policy status */
8039 ipp_attribute_t
*attr
; /* Current attribute */
8040 int limit
; /* Max number of printers to return */
8041 int count
; /* Number of printers that match */
8042 cupsd_printer_t
*printer
; /* Current printer pointer */
8043 int printer_type
, /* printer-type attribute */
8044 printer_mask
; /* printer-type-mask attribute */
8045 char *location
; /* Location string */
8046 const char *username
; /* Current user */
8047 char *first_printer_name
; /* first-printer-name attribute */
8048 cups_array_t
*ra
; /* Requested attributes array */
8049 int local
; /* Local connection? */
8052 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
8053 con
->http
.fd
, type
);
8059 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
8061 send_http_error(con
, status
, NULL
);
8066 * Check for printers...
8069 if (!Printers
|| !cupsArrayCount(Printers
))
8071 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
8076 * See if they want to limit the number of printers reported...
8079 if ((attr
= ippFindAttribute(con
->request
, "limit",
8080 IPP_TAG_INTEGER
)) != NULL
)
8081 limit
= attr
->values
[0].integer
;
8085 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
8086 IPP_TAG_NAME
)) != NULL
)
8087 first_printer_name
= attr
->values
[0].string
.text
;
8089 first_printer_name
= NULL
;
8092 * Support filtering...
8095 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
8096 IPP_TAG_ENUM
)) != NULL
)
8097 printer_type
= attr
->values
[0].integer
;
8101 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
8102 IPP_TAG_ENUM
)) != NULL
)
8103 printer_mask
= attr
->values
[0].integer
;
8107 local
= httpAddrLocalhost(&(con
->clientaddr
));
8109 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
8110 IPP_TAG_TEXT
)) != NULL
)
8111 location
= attr
->values
[0].string
.text
;
8115 if (con
->username
[0])
8116 username
= con
->username
;
8117 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
8118 IPP_TAG_NAME
)) != NULL
)
8119 username
= attr
->values
[0].string
.text
;
8123 ra
= create_requested_array(con
->request
);
8126 * OK, build a list of printers for this printer...
8129 if (first_printer_name
)
8131 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
8132 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
8135 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
8138 count
< limit
&& printer
;
8139 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
8141 if (!local
&& !printer
->shared
)
8144 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
8145 (printer
->type
& printer_mask
) == printer_type
&&
8147 (printer
->location
&& !strcasecmp(printer
->location
, location
))))
8150 * If HideImplicitMembers is enabled, see if this printer or class
8151 * is a member of an implicit class...
8154 if (ImplicitClasses
&& HideImplicitMembers
&&
8155 printer
->in_implicit_class
)
8159 * If a username is specified, see if it is allowed or denied
8163 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
8167 * Add the group separator as needed...
8171 ippAddSeparator(con
->response
);
8176 * Send the attributes...
8179 copy_printer_attrs(con
, printer
, ra
);
8183 cupsArrayDelete(ra
);
8185 con
->response
->request
.status
.status_code
= IPP_OK
;
8190 * 'get_subscription_attrs()' - Get subscription attributes.
8194 get_subscription_attrs(
8195 cupsd_client_t
*con
, /* I - Client connection */
8196 int sub_id
) /* I - Subscription ID */
8198 http_status_t status
; /* Policy status */
8199 cupsd_subscription_t
*sub
; /* Subscription */
8200 cups_array_t
*ra
; /* Requested attributes array */
8203 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8204 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
8205 con
, con
->http
.fd
, sub_id
);
8208 * Is the subscription ID valid?
8211 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8214 * Bad subscription ID...
8217 send_ipp_status(con
, IPP_NOT_FOUND
,
8218 _("notify-subscription-id %d no good"), sub_id
);
8226 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8228 con
, sub
->owner
)) != HTTP_OK
)
8230 send_http_error(con
, status
, sub
->dest
);
8235 * Copy the subscription attributes to the response using the
8236 * requested-attributes attribute that may be provided by the client.
8239 ra
= create_requested_array(con
->request
);
8241 copy_subscription_attrs(con
, sub
, ra
);
8243 cupsArrayDelete(ra
);
8245 con
->response
->request
.status
.status_code
= IPP_OK
;
8250 * 'get_subscriptions()' - Get subscriptions.
8254 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
8255 ipp_attribute_t
*uri
) /* I - Printer/job URI */
8257 http_status_t status
; /* Policy status */
8258 int count
; /* Number of subscriptions */
8259 int limit
; /* Limit */
8260 cupsd_subscription_t
*sub
; /* Subscription */
8261 cups_array_t
*ra
; /* Requested attributes array */
8262 ipp_attribute_t
*attr
; /* Attribute */
8263 cups_ptype_t dtype
; /* Destination type (printer/class) */
8264 char scheme
[HTTP_MAX_URI
],
8265 /* Scheme portion of URI */
8266 username
[HTTP_MAX_URI
],
8267 /* Username portion of URI */
8269 /* Host portion of URI */
8270 resource
[HTTP_MAX_URI
];
8271 /* Resource portion of URI */
8272 int port
; /* Port portion of URI */
8273 cupsd_job_t
*job
; /* Job pointer */
8274 cupsd_printer_t
*printer
; /* Printer */
8277 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8278 "get_subscriptions(con=%p[%d], uri=%s)",
8279 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
8282 * Is the destination valid?
8285 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8286 sizeof(scheme
), username
, sizeof(username
), host
,
8287 sizeof(host
), &port
, resource
, sizeof(resource
));
8289 if (!strcmp(resource
, "/") ||
8290 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
8291 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
8292 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
8297 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
8300 job
= cupsdFindJob(atoi(resource
+ 6));
8304 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist"),
8309 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8315 send_ipp_status(con
, IPP_NOT_FOUND
,
8316 _("The printer or class was not found."));
8319 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
8320 IPP_TAG_INTEGER
)) != NULL
)
8322 job
= cupsdFindJob(attr
->values
[0].integer
);
8326 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist"),
8327 attr
->values
[0].integer
);
8338 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
8340 con
, NULL
)) != HTTP_OK
)
8342 send_http_error(con
, status
, printer
);
8347 * Copy the subscription attributes to the response using the
8348 * requested-attributes attribute that may be provided by the client.
8351 ra
= create_requested_array(con
->request
);
8353 if ((attr
= ippFindAttribute(con
->request
, "limit",
8354 IPP_TAG_INTEGER
)) != NULL
)
8355 limit
= attr
->values
[0].integer
;
8360 * See if we only want to see subscriptions for a specific user...
8363 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
8364 IPP_TAG_BOOLEAN
)) != NULL
&&
8365 attr
->values
[0].boolean
)
8366 strlcpy(username
, get_username(con
), sizeof(username
));
8370 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
8372 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
8373 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
8374 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
8376 ippAddSeparator(con
->response
);
8377 copy_subscription_attrs(con
, sub
, ra
);
8380 if (limit
&& count
>= limit
)
8384 cupsArrayDelete(ra
);
8387 con
->response
->request
.status
.status_code
= IPP_OK
;
8389 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
8394 * 'get_username()' - Get the username associated with a request.
8397 static const char * /* O - Username */
8398 get_username(cupsd_client_t
*con
) /* I - Connection */
8400 ipp_attribute_t
*attr
; /* Attribute */
8403 if (con
->username
[0])
8404 return (con
->username
);
8405 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
8406 IPP_TAG_NAME
)) != NULL
)
8407 return (attr
->values
[0].string
.text
);
8409 return ("anonymous");
8414 * 'hold_job()' - Hold a print job.
8418 hold_job(cupsd_client_t
*con
, /* I - Client connection */
8419 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8421 ipp_attribute_t
*attr
; /* Current job-hold-until */
8422 const char *when
; /* New value */
8423 int jobid
; /* Job ID */
8424 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
8425 username
[HTTP_MAX_URI
], /* Username portion of URI */
8426 host
[HTTP_MAX_URI
], /* Host portion of URI */
8427 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8428 int port
; /* Port portion of URI */
8429 cupsd_job_t
*job
; /* Job information */
8432 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
8433 uri
->values
[0].string
.text
);
8436 * See if we have a job URI or a printer URI...
8439 if (!strcmp(uri
->name
, "printer-uri"))
8442 * Got a printer URI; see if we also have a job-id attribute...
8445 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8446 IPP_TAG_INTEGER
)) == NULL
)
8448 send_ipp_status(con
, IPP_BAD_REQUEST
,
8449 _("Got a printer-uri attribute but no job-id"));
8453 jobid
= attr
->values
[0].integer
;
8458 * Got a job URI; parse it to get the job ID...
8461 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8462 sizeof(scheme
), username
, sizeof(username
), host
,
8463 sizeof(host
), &port
, resource
, sizeof(resource
));
8465 if (strncmp(resource
, "/jobs/", 6))
8471 send_ipp_status(con
, IPP_BAD_REQUEST
,
8472 _("Bad job-uri attribute \"%s\""),
8473 uri
->values
[0].string
.text
);
8477 jobid
= atoi(resource
+ 6);
8481 * See if the job exists...
8484 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8487 * Nope - return a "not found" error...
8490 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist"), jobid
);
8495 * See if the job is owned by the requesting user...
8498 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8500 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8501 cupsdFindDest(job
->dest
));
8506 * Hold the job and return...
8509 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
8510 IPP_TAG_KEYWORD
)) == NULL
)
8511 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
8515 when
= attr
->values
[0].string
.text
;
8517 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
8518 "Job job-hold-until value changed by user.");
8521 when
= "indefinite";
8523 cupsdSetJobHoldUntil(job
, when
, 1);
8524 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
, "Job held by \"%s\".",
8527 con
->response
->request
.status
.status_code
= IPP_OK
;
8532 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
8536 hold_new_jobs(cupsd_client_t
*con
, /* I - Connection */
8537 ipp_attribute_t
*uri
) /* I - Printer URI */
8539 http_status_t status
; /* Policy status */
8540 cups_ptype_t dtype
; /* Destination type (printer/class) */
8541 cupsd_printer_t
*printer
; /* Printer data */
8544 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_new_jobs(%p[%d], %s)", con
,
8545 con
->http
.fd
, uri
->values
[0].string
.text
);
8548 * Is the destination valid?
8551 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8557 send_ipp_status(con
, IPP_NOT_FOUND
,
8558 _("The printer or class was not found."));
8566 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8568 send_http_error(con
, status
, printer
);
8573 * Hold pending/new jobs sent to the printer...
8576 printer
->holding_new_jobs
= 1;
8578 cupsdSetPrinterReasons(printer
, "+hold-new-jobs");
8579 cupsdAddPrinterHistory(printer
);
8581 if (dtype
& CUPS_PRINTER_CLASS
)
8582 cupsdLogMessage(CUPSD_LOG_INFO
,
8583 "Class \"%s\" now holding pending/new jobs (\"%s\").",
8584 printer
->name
, get_username(con
));
8586 cupsdLogMessage(CUPSD_LOG_INFO
,
8587 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
8588 printer
->name
, get_username(con
));
8591 * Everything was ok, so return OK status...
8594 con
->response
->request
.status
.status_code
= IPP_OK
;
8599 * 'move_job()' - Move a job to a new destination.
8603 move_job(cupsd_client_t
*con
, /* I - Client connection */
8604 ipp_attribute_t
*uri
) /* I - Job URI */
8606 http_status_t status
; /* Policy status */
8607 ipp_attribute_t
*attr
; /* Current attribute */
8608 int jobid
; /* Job ID */
8609 cupsd_job_t
*job
; /* Current job */
8610 const char *src
; /* Source printer/class */
8611 cups_ptype_t stype
, /* Source type (printer or class) */
8612 dtype
; /* Destination type (printer/class) */
8613 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
8614 username
[HTTP_MAX_URI
], /* Username portion of URI */
8615 host
[HTTP_MAX_URI
], /* Host portion of URI */
8616 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8617 int port
; /* Port portion of URI */
8618 cupsd_printer_t
*sprinter
, /* Source printer */
8619 *dprinter
; /* Destination printer */
8622 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
8623 uri
->values
[0].string
.text
);
8626 * Get the new printer or class...
8629 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
8630 IPP_TAG_URI
)) == NULL
)
8633 * Need job-printer-uri...
8636 send_ipp_status(con
, IPP_BAD_REQUEST
,
8637 _("job-printer-uri attribute missing"));
8641 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
8647 send_ipp_status(con
, IPP_NOT_FOUND
,
8648 _("The printer or class was not found."));
8653 * See if we have a job URI or a printer URI...
8656 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8657 sizeof(scheme
), username
, sizeof(username
), host
,
8658 sizeof(host
), &port
, resource
, sizeof(resource
));
8660 if (!strcmp(uri
->name
, "printer-uri"))
8663 * Got a printer URI; see if we also have a job-id attribute...
8666 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8667 IPP_TAG_INTEGER
)) == NULL
)
8673 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
8674 &sprinter
)) == NULL
)
8680 send_ipp_status(con
, IPP_NOT_FOUND
,
8681 _("The printer or class was not found."));
8690 * Otherwise, just move a single job...
8693 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
8696 * Nope - return a "not found" error...
8699 send_ipp_status(con
, IPP_NOT_FOUND
,
8700 _("Job #%d does not exist"), attr
->values
[0].integer
);
8706 * Job found, initialize source pointers...
8717 * Got a job URI; parse it to get the job ID...
8720 if (strncmp(resource
, "/jobs/", 6))
8726 send_ipp_status(con
, IPP_BAD_REQUEST
,
8727 _("Bad job-uri attribute \"%s\""),
8728 uri
->values
[0].string
.text
);
8733 * See if the job exists...
8736 jobid
= atoi(resource
+ 6);
8738 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8741 * Nope - return a "not found" error...
8744 send_ipp_status(con
, IPP_NOT_FOUND
,
8745 _("Job #%d does not exist"), jobid
);
8751 * Job found, initialize source pointers...
8760 * Check the policy of the destination printer...
8763 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
8764 job
? job
->username
: NULL
)) != HTTP_OK
)
8766 send_http_error(con
, status
, dprinter
);
8771 * Now move the job or jobs...
8777 * See if the job has been completed...
8780 if (job
->state_value
> IPP_JOB_STOPPED
)
8783 * Return a "not-possible" error...
8786 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8787 _("Job #%d is finished and cannot be altered"),
8793 * See if the job is owned by the requesting user...
8796 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8798 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8799 cupsdFindDest(job
->dest
));
8804 * Move the job to a different printer or class...
8807 cupsdMoveJob(job
, dprinter
);
8812 * Got the source printer, now look through the jobs...
8815 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
8817 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
8820 * See if the job is pointing at the source printer or has not been
8824 if (strcasecmp(job
->dest
, src
) ||
8825 job
->state_value
> IPP_JOB_STOPPED
)
8829 * See if the job can be moved by the requesting user...
8832 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8836 * Move the job to a different printer or class...
8839 cupsdMoveJob(job
, dprinter
);
8844 * Start jobs if possible...
8850 * Return with "everything is OK" status...
8853 con
->response
->request
.status
.status_code
= IPP_OK
;
8858 * 'ppd_parse_line()' - Parse a PPD default line.
8861 static int /* O - 0 on success, -1 on failure */
8862 ppd_parse_line(const char *line
, /* I - Line */
8863 char *option
, /* O - Option name */
8864 int olen
, /* I - Size of option name */
8865 char *choice
, /* O - Choice name */
8866 int clen
) /* I - Size of choice name */
8869 * Verify this is a default option line...
8872 if (strncmp(line
, "*Default", 8))
8876 * Read the option name...
8879 for (line
+= 8, olen
--;
8880 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
8891 * Skip everything else up to the colon (:)...
8894 while (*line
&& *line
!= ':')
8903 * Now grab the option choice, skipping leading whitespace...
8906 while (isspace(*line
& 255))
8910 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
8921 * Return with no errors...
8929 * 'print_job()' - Print a file to a printer or class.
8933 print_job(cupsd_client_t
*con
, /* I - Client connection */
8934 ipp_attribute_t
*uri
) /* I - Printer URI */
8936 ipp_attribute_t
*attr
; /* Current attribute */
8937 ipp_attribute_t
*format
; /* Document-format attribute */
8938 const char *default_format
; /* document-format-default value */
8939 cupsd_job_t
*job
; /* New job */
8940 char filename
[1024]; /* Job filename */
8941 mime_type_t
*filetype
; /* Type of file */
8942 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8943 type
[MIME_MAX_TYPE
], /* Subtype of file */
8944 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8945 /* Textual name of mime type */
8946 cupsd_printer_t
*printer
; /* Printer data */
8947 struct stat fileinfo
; /* File information */
8948 int kbytes
; /* Size of file */
8949 int compression
; /* Document compression */
8952 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
8953 uri
->values
[0].string
.text
);
8956 * Validate print file attributes, for now just document-format and
8957 * compression (CUPS only supports "none" and "gzip")...
8960 compression
= CUPS_FILE_NONE
;
8962 if ((attr
= ippFindAttribute(con
->request
, "compression",
8963 IPP_TAG_KEYWORD
)) != NULL
)
8965 if (strcmp(attr
->values
[0].string
.text
, "none")
8967 && strcmp(attr
->values
[0].string
.text
, "gzip")
8968 #endif /* HAVE_LIBZ */
8971 send_ipp_status(con
, IPP_ATTRIBUTES
,
8972 _("Unsupported compression \"%s\""),
8973 attr
->values
[0].string
.text
);
8974 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8975 "compression", NULL
, attr
->values
[0].string
.text
);
8980 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8981 compression
= CUPS_FILE_GZIP
;
8982 #endif /* HAVE_LIBZ */
8986 * Do we have a file to print?
8991 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?"));
8996 * Is the destination valid?
8999 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
9005 send_ipp_status(con
, IPP_NOT_FOUND
,
9006 _("The printer or class was not found."));
9011 * Is it a format we support?
9014 if ((format
= ippFindAttribute(con
->request
, "document-format",
9015 IPP_TAG_MIMETYPE
)) != NULL
)
9018 * Grab format from client...
9021 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
9024 send_ipp_status(con
, IPP_BAD_REQUEST
,
9025 _("Could not scan type \"%s\""),
9026 format
->values
[0].string
.text
);
9030 else if ((default_format
= cupsGetOption("document-format",
9031 printer
->num_options
,
9032 printer
->options
)) != NULL
)
9035 * Use default document format...
9038 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
9040 send_ipp_status(con
, IPP_BAD_REQUEST
,
9041 _("Could not scan type \"%s\""),
9052 strcpy(super
, "application");
9053 strcpy(type
, "octet-stream");
9056 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9059 * Auto-type the file...
9062 ipp_attribute_t
*doc_name
; /* document-name attribute */
9065 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
9067 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9068 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9069 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9073 filetype
= mimeType(MimeDatabase
, super
, type
);
9075 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
9076 filetype
->super
, filetype
->type
);
9079 filetype
= mimeType(MimeDatabase
, super
, type
);
9083 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
9086 * Replace the document-format attribute value with the auto-typed or
9090 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9095 _cupsStrFree(format
->values
[0].string
.text
);
9097 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
9100 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9101 "document-format", NULL
, mimetype
);
9105 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9106 _("Unsupported format \'%s/%s\'"), super
, type
);
9107 cupsdLogMessage(CUPSD_LOG_INFO
,
9108 "Hint: Do you have the raw file printing rules enabled?");
9111 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9112 "document-format", NULL
, format
->values
[0].string
.text
);
9118 * Read any embedded job ticket info from PS files...
9121 if (!strcasecmp(filetype
->super
, "application") &&
9122 (!strcasecmp(filetype
->type
, "postscript") ||
9123 !strcasecmp(filetype
->type
, "pdf")))
9124 read_job_ticket(con
);
9127 * Create the job object...
9130 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
9134 * Update quota data...
9137 if (stat(con
->filename
, &fileinfo
))
9140 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9142 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9144 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
9145 IPP_TAG_INTEGER
)) != NULL
)
9146 attr
->values
[0].integer
+= kbytes
;
9149 * Add the job file...
9152 if (add_file(con
, job
, filetype
, compression
))
9155 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
9157 rename(con
->filename
, filename
);
9158 cupsdClearString(&con
->filename
);
9161 * See if we need to add the ending sheet...
9164 if (cupsdTimeoutJob(job
))
9168 * Log and save the job...
9171 cupsdLogJob(job
, CUPSD_LOG_INFO
,
9172 "File of type %s/%s queued by \"%s\".",
9173 filetype
->super
, filetype
->type
, job
->username
);
9174 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
9175 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
9176 job
->dest
, job
->username
);
9179 * Start the job if possible...
9187 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
9189 * This function only gets called when printing a single PDF or PostScript
9190 * file using the Print-Job operation. It doesn't work for Create-Job +
9191 * Send-File, since the job attributes need to be set at job creation
9192 * time for banners to work. The embedded job ticket stuff is here
9193 * primarily to allow the Windows printer driver for CUPS to pass in JCL
9194 * options and IPP attributes which otherwise would be lost.
9196 * The format of a job ticket is simple:
9198 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
9200 * %cupsJobTicket: attr1=value1
9201 * %cupsJobTicket: attr2=value2
9203 * %cupsJobTicket: attrN=valueN
9205 * Job ticket lines must appear immediately after the first line that
9206 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
9207 * stops looking for job ticket info when it finds a line that does not begin
9208 * with "%cupsJobTicket:".
9210 * The maximum length of a job ticket line, including the prefix, is
9211 * 255 characters to conform with the Adobe DSC.
9213 * Read-only attributes are rejected with a notice to the error log in
9214 * case a malicious user tries anything. Since the job ticket is read
9215 * prior to attribute validation in print_job(), job ticket attributes
9216 * will go through the same validation as IPP attributes...
9220 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
9222 cups_file_t
*fp
; /* File to read from */
9223 char line
[256]; /* Line data */
9224 int num_options
; /* Number of options */
9225 cups_option_t
*options
; /* Options */
9226 ipp_t
*ticket
; /* New attributes */
9227 ipp_attribute_t
*attr
, /* Current attribute */
9228 *attr2
, /* Job attribute */
9229 *prev2
; /* Previous job attribute */
9233 * First open the print file...
9236 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
9238 cupsdLogMessage(CUPSD_LOG_ERROR
,
9239 "Unable to open print file for job ticket - %s",
9245 * Skip the first line...
9248 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
9250 cupsdLogMessage(CUPSD_LOG_ERROR
,
9251 "Unable to read from print file for job ticket - %s",
9257 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
9260 * Not a DSC-compliant file, so no job ticket info will be available...
9268 * Read job ticket info from the file...
9274 while (cupsFileGets(fp
, line
, sizeof(line
)))
9277 * Stop at the first non-ticket line...
9280 if (strncmp(line
, "%cupsJobTicket:", 15))
9284 * Add the options to the option array...
9287 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
9291 * Done with the file; see if we have any options...
9296 if (num_options
== 0)
9300 * OK, convert the options to an attribute list, and apply them to
9305 cupsEncodeOptions(ticket
, num_options
, options
);
9308 * See what the user wants to change.
9311 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
9313 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
9316 if (!strcmp(attr
->name
, "job-originating-host-name") ||
9317 !strcmp(attr
->name
, "job-originating-user-name") ||
9318 !strcmp(attr
->name
, "job-media-sheets-completed") ||
9319 !strcmp(attr
->name
, "job-k-octets") ||
9320 !strcmp(attr
->name
, "job-id") ||
9321 !strncmp(attr
->name
, "job-state", 9) ||
9322 !strncmp(attr
->name
, "time-at-", 8))
9323 continue; /* Read-only attrs */
9325 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
9326 IPP_TAG_ZERO
)) != NULL
)
9329 * Some other value; first free the old value...
9332 if (con
->request
->attrs
== attr2
)
9334 con
->request
->attrs
= attr2
->next
;
9339 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
9340 if (prev2
->next
== attr2
)
9342 prev2
->next
= attr2
->next
;
9347 if (con
->request
->last
== attr2
)
9348 con
->request
->last
= prev2
;
9350 _ippFreeAttr(attr2
);
9354 * Add new option by copying it...
9357 copy_attribute(con
->request
, attr
, 0);
9361 * Then free the attribute list and option array...
9365 cupsFreeOptions(num_options
, options
);
9370 * 'reject_jobs()' - Reject print jobs to a printer.
9374 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
9375 ipp_attribute_t
*uri
) /* I - Printer or class URI */
9377 http_status_t status
; /* Policy status */
9378 cups_ptype_t dtype
; /* Destination type (printer/class) */
9379 cupsd_printer_t
*printer
; /* Printer data */
9380 ipp_attribute_t
*attr
; /* printer-state-message text */
9383 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
9384 con
->http
.fd
, uri
->values
[0].string
.text
);
9387 * Is the destination valid?
9390 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9396 send_ipp_status(con
, IPP_NOT_FOUND
,
9397 _("The printer or class was not found."));
9405 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9407 send_http_error(con
, status
, printer
);
9412 * Reject jobs sent to the printer...
9415 printer
->accepting
= 0;
9417 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
9418 IPP_TAG_TEXT
)) == NULL
)
9419 strcpy(printer
->state_message
, "Rejecting Jobs");
9421 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
9422 sizeof(printer
->state_message
));
9424 cupsdAddPrinterHistory(printer
);
9426 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
9427 "No longer accepting jobs.");
9429 if (dtype
& CUPS_PRINTER_CLASS
)
9431 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
9433 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
9434 printer
->name
, get_username(con
));
9438 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
9440 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
9441 printer
->name
, get_username(con
));
9445 * Everything was ok, so return OK status...
9448 con
->response
->request
.status
.status_code
= IPP_OK
;
9453 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
9457 release_held_new_jobs(
9458 cupsd_client_t
*con
, /* I - Connection */
9459 ipp_attribute_t
*uri
) /* I - Printer URI */
9461 http_status_t status
; /* Policy status */
9462 cups_ptype_t dtype
; /* Destination type (printer/class) */
9463 cupsd_printer_t
*printer
; /* Printer data */
9466 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_held_new_jobs(%p[%d], %s)", con
,
9467 con
->http
.fd
, uri
->values
[0].string
.text
);
9470 * Is the destination valid?
9473 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9479 send_ipp_status(con
, IPP_NOT_FOUND
,
9480 _("The printer or class was not found."));
9488 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9490 send_http_error(con
, status
, printer
);
9495 * Hold pending/new jobs sent to the printer...
9498 printer
->holding_new_jobs
= 0;
9500 cupsdSetPrinterReasons(printer
, "-hold-new-jobs");
9501 cupsdAddPrinterHistory(printer
);
9503 if (dtype
& CUPS_PRINTER_CLASS
)
9504 cupsdLogMessage(CUPSD_LOG_INFO
,
9505 "Class \"%s\" now printing pending/new jobs (\"%s\").",
9506 printer
->name
, get_username(con
));
9508 cupsdLogMessage(CUPSD_LOG_INFO
,
9509 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
9510 printer
->name
, get_username(con
));
9513 * Everything was ok, so return OK status...
9516 con
->response
->request
.status
.status_code
= IPP_OK
;
9521 * 'release_job()' - Release a held print job.
9525 release_job(cupsd_client_t
*con
, /* I - Client connection */
9526 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
9528 ipp_attribute_t
*attr
; /* Current attribute */
9529 int jobid
; /* Job ID */
9530 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
9531 username
[HTTP_MAX_URI
], /* Username portion of URI */
9532 host
[HTTP_MAX_URI
], /* Host portion of URI */
9533 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9534 int port
; /* Port portion of URI */
9535 cupsd_job_t
*job
; /* Job information */
9538 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
9539 con
->http
.fd
, uri
->values
[0].string
.text
);
9542 * See if we have a job URI or a printer URI...
9545 if (!strcmp(uri
->name
, "printer-uri"))
9548 * Got a printer URI; see if we also have a job-id attribute...
9551 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9552 IPP_TAG_INTEGER
)) == NULL
)
9554 send_ipp_status(con
, IPP_BAD_REQUEST
,
9555 _("Got a printer-uri attribute but no job-id"));
9559 jobid
= attr
->values
[0].integer
;
9564 * Got a job URI; parse it to get the job ID...
9567 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9568 sizeof(scheme
), username
, sizeof(username
), host
,
9569 sizeof(host
), &port
, resource
, sizeof(resource
));
9571 if (strncmp(resource
, "/jobs/", 6))
9577 send_ipp_status(con
, IPP_BAD_REQUEST
,
9578 _("Bad job-uri attribute \"%s\""),
9579 uri
->values
[0].string
.text
);
9583 jobid
= atoi(resource
+ 6);
9587 * See if the job exists...
9590 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9593 * Nope - return a "not found" error...
9596 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist"), jobid
);
9601 * See if job is "held"...
9604 if (job
->state_value
!= IPP_JOB_HELD
)
9607 * Nope - return a "not possible" error...
9610 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held"), jobid
);
9615 * See if the job is owned by the requesting user...
9618 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9620 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9621 cupsdFindDest(job
->dest
));
9626 * Reset the job-hold-until value to "no-hold"...
9629 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9630 IPP_TAG_KEYWORD
)) == NULL
)
9631 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9635 _cupsStrFree(attr
->values
[0].string
.text
);
9637 attr
->value_tag
= IPP_TAG_KEYWORD
;
9638 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
9640 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
9641 "Job job-hold-until value changed by user.");
9645 * Release the job and return...
9648 cupsdReleaseJob(job
);
9650 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
9651 "Job released by user.");
9653 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
9655 con
->response
->request
.status
.status_code
= IPP_OK
;
9662 * 'renew_subscription()' - Renew an existing subscription...
9667 cupsd_client_t
*con
, /* I - Client connection */
9668 int sub_id
) /* I - Subscription ID */
9670 http_status_t status
; /* Policy status */
9671 cupsd_subscription_t
*sub
; /* Subscription */
9672 ipp_attribute_t
*lease
; /* notify-lease-duration */
9675 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9676 "renew_subscription(con=%p[%d], sub_id=%d)",
9677 con
, con
->http
.fd
, sub_id
);
9680 * Is the subscription ID valid?
9683 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
9686 * Bad subscription ID...
9689 send_ipp_status(con
, IPP_NOT_FOUND
,
9690 _("notify-subscription-id %d no good"), sub_id
);
9697 * Job subscriptions cannot be renewed...
9700 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9701 _("Job subscriptions cannot be renewed"));
9709 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
9711 con
, sub
->owner
)) != HTTP_OK
)
9713 send_http_error(con
, status
, sub
->dest
);
9718 * Renew the subscription...
9721 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
9724 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
9726 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
9728 cupsdLogMessage(CUPSD_LOG_INFO
,
9729 "renew_subscription: Limiting notify-lease-duration to "
9732 sub
->lease
= MaxLeaseDuration
;
9735 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
9737 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
9739 con
->response
->request
.status
.status_code
= IPP_OK
;
9741 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
9742 "notify-lease-duration", sub
->lease
);
9747 * 'restart_job()' - Restart an old print job.
9751 restart_job(cupsd_client_t
*con
, /* I - Client connection */
9752 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
9754 ipp_attribute_t
*attr
; /* Current attribute */
9755 int jobid
; /* Job ID */
9756 cupsd_job_t
*job
; /* Job information */
9757 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
9758 username
[HTTP_MAX_URI
], /* Username portion of URI */
9759 host
[HTTP_MAX_URI
], /* Host portion of URI */
9760 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9761 int port
; /* Port portion of URI */
9764 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
9765 con
->http
.fd
, uri
->values
[0].string
.text
);
9768 * See if we have a job URI or a printer URI...
9771 if (!strcmp(uri
->name
, "printer-uri"))
9774 * Got a printer URI; see if we also have a job-id attribute...
9777 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9778 IPP_TAG_INTEGER
)) == NULL
)
9780 send_ipp_status(con
, IPP_BAD_REQUEST
,
9781 _("Got a printer-uri attribute but no job-id"));
9785 jobid
= attr
->values
[0].integer
;
9790 * Got a job URI; parse it to get the job ID...
9793 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9794 sizeof(scheme
), username
, sizeof(username
), host
,
9795 sizeof(host
), &port
, resource
, sizeof(resource
));
9797 if (strncmp(resource
, "/jobs/", 6))
9803 send_ipp_status(con
, IPP_BAD_REQUEST
,
9804 _("Bad job-uri attribute \"%s\""),
9805 uri
->values
[0].string
.text
);
9809 jobid
= atoi(resource
+ 6);
9813 * See if the job exists...
9816 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9819 * Nope - return a "not found" error...
9822 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist"), jobid
);
9827 * See if job is in any of the "completed" states...
9830 if (job
->state_value
<= IPP_JOB_PROCESSING
)
9833 * Nope - return a "not possible" error...
9836 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete"),
9842 * See if we have retained the job files...
9847 if (!job
->attrs
|| job
->num_files
== 0)
9850 * Nope - return a "not possible" error...
9853 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9854 _("Job #%d cannot be restarted - no files"), jobid
);
9859 * See if the job is owned by the requesting user...
9862 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9864 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9865 cupsdFindDest(job
->dest
));
9870 * See if the job-hold-until attribute is specified...
9873 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
9874 IPP_TAG_KEYWORD
)) == NULL
)
9875 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
9877 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
9880 * Return the job to a held state...
9883 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
9884 "Restarted by \"%s\" with job-hold-until=%s.",
9885 username
, attr
->values
[0].string
.text
);
9886 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
9888 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
,
9889 NULL
, job
, "Job restarted by user with job-hold-until=%s",
9890 attr
->values
[0].string
.text
);
9895 * Restart the job...
9898 cupsdRestartJob(job
);
9902 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
9904 con
->response
->request
.status
.status_code
= IPP_OK
;
9909 * 'save_auth_info()' - Save authentication information for a job.
9914 cupsd_client_t
*con
, /* I - Client connection */
9915 cupsd_job_t
*job
, /* I - Job */
9916 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
9918 int i
; /* Looping var */
9919 char filename
[1024]; /* Job authentication filename */
9920 cups_file_t
*fp
; /* Job authentication file */
9921 char line
[2048]; /* Line for file */
9922 cupsd_printer_t
*dest
; /* Destination printer/class */
9926 * This function saves the in-memory authentication information for
9927 * a job so that it can be used to authenticate with a remote host.
9928 * The information is stored in a file that is readable only by the
9929 * root user. The fields are Base-64 encoded, each on a separate line,
9930 * followed by random number (up to 1024) of newlines to limit the
9931 * amount of information that is exposed.
9933 * Because of the potential for exposing of authentication information,
9934 * this functionality is only enabled when running cupsd as root.
9936 * This caching only works for the Basic and BasicDigest authentication
9937 * types. Digest authentication cannot be cached this way, and in
9938 * the future Kerberos authentication may make all of this obsolete.
9940 * Authentication information is saved whenever an authenticated
9941 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9944 * This information is deleted after a job is completed or canceled,
9945 * so reprints may require subsequent re-authentication.
9951 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9955 * Create the authentication file and change permissions...
9958 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9959 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9961 cupsdLogMessage(CUPSD_LOG_ERROR
,
9962 "Unable to save authentication info to \"%s\" - %s",
9963 filename
, strerror(errno
));
9967 fchown(cupsFileNumber(fp
), 0, 0);
9968 fchmod(cupsFileNumber(fp
), 0400);
9970 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9973 * Write 1 to 3 auth values...
9976 cupsdClearString(&job
->auth_username
);
9977 cupsdClearString(&job
->auth_domain
);
9978 cupsdClearString(&job
->auth_password
);
9980 for (i
= 0; i
< auth_info
->num_values
; i
++)
9982 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
9983 strlen(auth_info
->values
[i
].string
.text
));
9984 cupsFilePrintf(fp
, "%s\n", line
);
9986 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9987 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s",
9988 auth_info
->values
[i
].string
.text
);
9989 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9990 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s",
9991 auth_info
->values
[i
].string
.text
);
9992 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9993 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s",
9994 auth_info
->values
[i
].string
.text
);
9997 else if (con
->username
[0])
10000 * Write the authenticated username...
10003 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
10004 cupsFilePrintf(fp
, "%s\n", line
);
10006 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", con
->username
);
10007 cupsdClearString(&job
->auth_domain
);
10010 * Write the authenticated password...
10013 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
10014 cupsFilePrintf(fp
, "%s\n", line
);
10016 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", con
->password
);
10020 * Write a random number of newlines to the end of the file...
10023 for (i
= (CUPS_RAND() % 1024); i
>= 0; i
--)
10024 cupsFilePutChar(fp
, '\n');
10027 * Close the file and return...
10032 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
10033 # ifdef HAVE_KRB5_IPC_CLIENT_SET_TARGET_UID
10034 if (con
->have_gss
&&
10035 (con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
|| con
->gss_creds
))
10037 if (con
->have_gss
&& con
->gss_creds
)
10038 # endif /* HAVE_KRB5_IPC_CLIENT_SET_TARGET_UID */
10039 save_krb5_creds(con
, job
);
10040 else if (job
->ccname
)
10041 cupsdClearString(&(job
->ccname
));
10042 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
10046 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
10048 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
10052 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
10053 cupsd_job_t
*job
) /* I - Job */
10056 * Get the credentials...
10059 job
->ccache
= cupsdCopyKrb5Creds(con
);
10062 * Add the KRB5CCNAME environment variable to the job so that the
10063 * backend can use the credentials when printing.
10068 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
10069 krb5_cc_get_name(KerberosContext
, job
->ccache
));
10071 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "save_krb5_creds: %s", job
->ccname
);
10074 cupsdClearString(&(job
->ccname
));
10076 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
10080 * 'send_document()' - Send a file to a printer or class.
10084 send_document(cupsd_client_t
*con
, /* I - Client connection */
10085 ipp_attribute_t
*uri
) /* I - Printer URI */
10087 ipp_attribute_t
*attr
; /* Current attribute */
10088 ipp_attribute_t
*format
; /* Request's document-format attribute */
10089 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
10090 const char *default_format
;/* document-format-default value */
10091 int jobid
; /* Job ID number */
10092 cupsd_job_t
*job
; /* Current job */
10093 char job_uri
[HTTP_MAX_URI
],
10095 scheme
[HTTP_MAX_URI
],
10096 /* Method portion of URI */
10097 username
[HTTP_MAX_URI
],
10098 /* Username portion of URI */
10099 host
[HTTP_MAX_URI
],
10100 /* Host portion of URI */
10101 resource
[HTTP_MAX_URI
];
10102 /* Resource portion of URI */
10103 int port
; /* Port portion of URI */
10104 mime_type_t
*filetype
; /* Type of file */
10105 char super
[MIME_MAX_SUPER
],
10106 /* Supertype of file */
10107 type
[MIME_MAX_TYPE
],
10108 /* Subtype of file */
10109 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
10110 /* Textual name of mime type */
10111 char filename
[1024]; /* Job filename */
10112 cupsd_printer_t
*printer
; /* Current printer */
10113 struct stat fileinfo
; /* File information */
10114 int kbytes
; /* Size of file */
10115 int compression
; /* Type of compression */
10116 int start_job
; /* Start the job? */
10119 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
10120 con
->http
.fd
, uri
->values
[0].string
.text
);
10123 * See if we have a job URI or a printer URI...
10126 if (!strcmp(uri
->name
, "printer-uri"))
10129 * Got a printer URI; see if we also have a job-id attribute...
10132 if ((attr
= ippFindAttribute(con
->request
, "job-id",
10133 IPP_TAG_INTEGER
)) == NULL
)
10135 send_ipp_status(con
, IPP_BAD_REQUEST
,
10136 _("Got a printer-uri attribute but no job-id"));
10140 jobid
= attr
->values
[0].integer
;
10145 * Got a job URI; parse it to get the job ID...
10148 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
10149 sizeof(scheme
), username
, sizeof(username
), host
,
10150 sizeof(host
), &port
, resource
, sizeof(resource
));
10152 if (strncmp(resource
, "/jobs/", 6))
10158 send_ipp_status(con
, IPP_BAD_REQUEST
,
10159 _("Bad job-uri attribute \"%s\""),
10160 uri
->values
[0].string
.text
);
10164 jobid
= atoi(resource
+ 6);
10168 * See if the job exists...
10171 if ((job
= cupsdFindJob(jobid
)) == NULL
)
10174 * Nope - return a "not found" error...
10177 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist"), jobid
);
10181 printer
= cupsdFindDest(job
->dest
);
10184 * See if the job is owned by the requesting user...
10187 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
10189 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
10190 cupsdFindDest(job
->dest
));
10195 * OK, see if the client is sending the document compressed - CUPS
10196 * only supports "none" and "gzip".
10199 compression
= CUPS_FILE_NONE
;
10201 if ((attr
= ippFindAttribute(con
->request
, "compression",
10202 IPP_TAG_KEYWORD
)) != NULL
)
10204 if (strcmp(attr
->values
[0].string
.text
, "none")
10206 && strcmp(attr
->values
[0].string
.text
, "gzip")
10207 #endif /* HAVE_LIBZ */
10210 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\""),
10211 attr
->values
[0].string
.text
);
10212 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10213 "compression", NULL
, attr
->values
[0].string
.text
);
10218 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
10219 compression
= CUPS_FILE_GZIP
;
10220 #endif /* HAVE_LIBZ */
10224 * Do we have a file to print?
10227 if (!con
->filename
)
10230 * Check for an empty request with "last-document" set to true, which is
10231 * used to close an "open" job by RFC 2911, section 3.3.2.
10234 if (job
->num_files
> 0 &&
10235 (attr
= ippFindAttribute(con
->request
, "last-document",
10236 IPP_TAG_BOOLEAN
)) != NULL
&&
10237 attr
->values
[0].boolean
)
10238 goto last_document
;
10240 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?"));
10245 * Is it a format we support?
10248 if ((format
= ippFindAttribute(con
->request
, "document-format",
10249 IPP_TAG_MIMETYPE
)) != NULL
)
10252 * Grab format from client...
10255 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
10258 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\""),
10259 format
->values
[0].string
.text
);
10263 else if ((default_format
= cupsGetOption("document-format",
10264 printer
->num_options
,
10265 printer
->options
)) != NULL
)
10268 * Use default document format...
10271 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
10273 send_ipp_status(con
, IPP_BAD_REQUEST
,
10274 _("Could not scan type \"%s\""),
10282 * No document format attribute? Auto-type it!
10285 strcpy(super
, "application");
10286 strcpy(type
, "octet-stream");
10289 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
10292 * Auto-type the file...
10295 ipp_attribute_t
*doc_name
; /* document-name attribute */
10298 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
10300 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
10301 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
10302 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
10306 filetype
= mimeType(MimeDatabase
, super
, type
);
10308 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
10309 filetype
->super
, filetype
->type
);
10312 filetype
= mimeType(MimeDatabase
, super
, type
);
10317 * Replace the document-format attribute value with the auto-typed or
10321 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
10324 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
10325 IPP_TAG_MIMETYPE
)) != NULL
)
10327 _cupsStrFree(jformat
->values
[0].string
.text
);
10329 jformat
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
10332 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
10333 "document-format", NULL
, mimetype
);
10335 else if (!filetype
)
10337 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10338 _("Unsupported format \'%s/%s\'"), super
, type
);
10339 cupsdLogMessage(CUPSD_LOG_INFO
,
10340 "Hint: Do you have the raw file printing rules enabled?");
10343 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10344 "document-format", NULL
, format
->values
[0].string
.text
);
10349 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
10351 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
10354 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10355 _("Unsupported format \'%s\'"), mimetype
);
10357 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10358 "document-format", NULL
, mimetype
);
10364 * Add the file to the job...
10369 if (add_file(con
, job
, filetype
, compression
))
10372 if (stat(con
->filename
, &fileinfo
))
10375 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
10377 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
10379 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
10380 IPP_TAG_INTEGER
)) != NULL
)
10381 attr
->values
[0].integer
+= kbytes
;
10383 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
10385 rename(con
->filename
, filename
);
10387 cupsdClearString(&con
->filename
);
10389 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
10390 filetype
->super
, filetype
->type
, job
->username
);
10393 * Start the job if this is the last document...
10398 if ((attr
= ippFindAttribute(con
->request
, "last-document",
10399 IPP_TAG_BOOLEAN
)) != NULL
&&
10400 attr
->values
[0].boolean
)
10403 * See if we need to add the ending sheet...
10406 if (cupsdTimeoutJob(job
))
10409 if (job
->state_value
== IPP_JOB_STOPPED
)
10411 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
10412 job
->state_value
= IPP_JOB_PENDING
;
10414 else if (job
->state_value
== IPP_JOB_HELD
)
10416 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
10417 IPP_TAG_KEYWORD
)) == NULL
)
10418 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
10420 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
10422 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
10423 job
->state_value
= IPP_JOB_PENDING
;
10428 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10434 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
10435 IPP_TAG_KEYWORD
)) == NULL
)
10436 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
10438 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
10440 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
10441 job
->state_value
= IPP_JOB_HELD
;
10442 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
10445 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10452 * Fill in the response info...
10455 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
10458 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
10461 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
10463 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
10465 add_job_state_reasons(con
, job
);
10467 con
->response
->request
.status
.status_code
= IPP_OK
;
10470 * Start the job if necessary...
10479 * 'send_http_error()' - Send a HTTP error back to the IPP client.
10484 cupsd_client_t
*con
, /* I - Client connection */
10485 http_status_t status
, /* I - HTTP status code */
10486 cupsd_printer_t
*printer
) /* I - Printer, if any */
10488 ipp_attribute_t
*uri
; /* Request URI, if any */
10491 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
10492 IPP_TAG_URI
)) == NULL
)
10493 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
10495 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
10496 "Returning HTTP %s for %s (%s) from %s",
10497 httpStatus(status
),
10498 ippOpString(con
->request
->request
.op
.operation_id
),
10499 uri
? uri
->values
[0].string
.text
: "no URI",
10500 con
->http
.hostname
);
10504 int auth_type
; /* Type of authentication required */
10507 auth_type
= CUPSD_AUTH_NONE
;
10509 if (status
== HTTP_UNAUTHORIZED
&&
10510 printer
->num_auth_info_required
> 0 &&
10511 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
10513 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
10514 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
10515 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
10518 * Creating and authenticating jobs requires Kerberos...
10521 auth_type
= CUPSD_AUTH_NEGOTIATE
;
10526 * Use policy/location-defined authentication requirements...
10529 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
10530 cupsd_location_t
*auth
; /* Pointer to authentication element */
10533 if (printer
->type
& CUPS_PRINTER_CLASS
)
10534 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
10536 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
10538 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
10539 auth
->type
== CUPSD_AUTH_NONE
)
10540 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
10542 con
->request
->request
.op
.operation_id
:
10547 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
10548 auth_type
= DefaultAuthType
;
10550 auth_type
= auth
->type
;
10554 cupsdSendError(con
, status
, auth_type
);
10557 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
10559 ippDelete(con
->response
);
10560 con
->response
= NULL
;
10567 * 'send_ipp_status()' - Send a status back to the IPP client.
10571 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
10572 ipp_status_t status
, /* I - IPP status code */
10573 const char *message
,/* I - Status message */
10574 ...) /* I - Additional args as needed */
10576 va_list ap
; /* Pointer to additional args */
10577 char formatted
[1024]; /* Formatted errror message */
10580 va_start(ap
, message
);
10581 vsnprintf(formatted
, sizeof(formatted
),
10582 _cupsLangString(con
->language
, message
), ap
);
10585 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
10586 ippOpString(con
->request
->request
.op
.operation_id
),
10587 ippErrorString(status
), formatted
);
10589 con
->response
->request
.status
.status_code
= status
;
10591 if (ippFindAttribute(con
->response
, "attributes-charset",
10592 IPP_TAG_ZERO
) == NULL
)
10593 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
10594 "attributes-charset", NULL
, "utf-8");
10596 if (ippFindAttribute(con
->response
, "attributes-natural-language",
10597 IPP_TAG_ZERO
) == NULL
)
10598 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
10599 "attributes-natural-language", NULL
, DefaultLanguage
);
10601 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
10602 "status-message", NULL
, formatted
);
10607 * 'set_default()' - Set the default destination...
10611 set_default(cupsd_client_t
*con
, /* I - Client connection */
10612 ipp_attribute_t
*uri
) /* I - Printer URI */
10614 http_status_t status
; /* Policy status */
10615 cups_ptype_t dtype
; /* Destination type (printer/class) */
10616 cupsd_printer_t
*printer
, /* Printer */
10617 *oldprinter
; /* Old default printer */
10620 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
10621 con
->http
.fd
, uri
->values
[0].string
.text
);
10624 * Is the destination valid?
10627 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10633 send_ipp_status(con
, IPP_NOT_FOUND
,
10634 _("The printer or class was not found."));
10642 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
10644 send_http_error(con
, status
, NULL
);
10649 * Set it as the default...
10652 oldprinter
= DefaultPrinter
;
10653 DefaultPrinter
= printer
;
10656 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
10657 "%s is no longer the default printer.", oldprinter
->name
);
10659 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
10660 "%s is now the default printer.", printer
->name
);
10662 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
10663 CUPSD_DIRTY_REMOTE
| CUPSD_DIRTY_PRINTCAP
);
10665 cupsdLogMessage(CUPSD_LOG_INFO
,
10666 "Default destination set to \"%s\" by \"%s\".",
10667 printer
->name
, get_username(con
));
10670 * Everything was ok, so return OK status...
10673 con
->response
->request
.status
.status_code
= IPP_OK
;
10678 * 'set_job_attrs()' - Set job attributes.
10682 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
10683 ipp_attribute_t
*uri
) /* I - Job URI */
10685 ipp_attribute_t
*attr
, /* Current attribute */
10686 *attr2
; /* Job attribute */
10687 int jobid
; /* Job ID */
10688 cupsd_job_t
*job
; /* Current job */
10689 char scheme
[HTTP_MAX_URI
],
10690 /* Method portion of URI */
10691 username
[HTTP_MAX_URI
],
10692 /* Username portion of URI */
10693 host
[HTTP_MAX_URI
],
10694 /* Host portion of URI */
10695 resource
[HTTP_MAX_URI
];
10696 /* Resource portion of URI */
10697 int port
; /* Port portion of URI */
10698 int event
; /* Events? */
10699 int check_jobs
; /* Check jobs? */
10702 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
10703 con
->http
.fd
, uri
->values
[0].string
.text
);
10706 * Start with "everything is OK" status...
10709 con
->response
->request
.status
.status_code
= IPP_OK
;
10712 * See if we have a job URI or a printer URI...
10715 if (!strcmp(uri
->name
, "printer-uri"))
10718 * Got a printer URI; see if we also have a job-id attribute...
10721 if ((attr
= ippFindAttribute(con
->request
, "job-id",
10722 IPP_TAG_INTEGER
)) == NULL
)
10724 send_ipp_status(con
, IPP_BAD_REQUEST
,
10725 _("Got a printer-uri attribute but no job-id"));
10729 jobid
= attr
->values
[0].integer
;
10734 * Got a job URI; parse it to get the job ID...
10737 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
10738 sizeof(scheme
), username
, sizeof(username
), host
,
10739 sizeof(host
), &port
, resource
, sizeof(resource
));
10741 if (strncmp(resource
, "/jobs/", 6))
10747 send_ipp_status(con
, IPP_BAD_REQUEST
,
10748 _("Bad job-uri attribute \"%s\""),
10749 uri
->values
[0].string
.text
);
10753 jobid
= atoi(resource
+ 6);
10757 * See if the job exists...
10760 if ((job
= cupsdFindJob(jobid
)) == NULL
)
10763 * Nope - return a "not found" error...
10766 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist"), jobid
);
10771 * See if the job has been completed...
10774 if (job
->state_value
> IPP_JOB_STOPPED
)
10777 * Return a "not-possible" error...
10780 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10781 _("Job #%d is finished and cannot be altered"), jobid
);
10786 * See if the job is owned by the requesting user...
10789 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
10791 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
10792 cupsdFindDest(job
->dest
));
10797 * See what the user wants to change.
10805 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10807 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
10810 if (!strcmp(attr
->name
, "attributes-charset") ||
10811 !strcmp(attr
->name
, "attributes-natural-language") ||
10812 !strcmp(attr
->name
, "document-compression") ||
10813 !strcmp(attr
->name
, "document-format") ||
10814 !strcmp(attr
->name
, "job-detailed-status-messages") ||
10815 !strcmp(attr
->name
, "job-document-access-errors") ||
10816 !strcmp(attr
->name
, "job-id") ||
10817 !strcmp(attr
->name
, "job-impressions-completed") ||
10818 !strcmp(attr
->name
, "job-k-octets") ||
10819 !strcmp(attr
->name
, "job-originating-host-name") ||
10820 !strcmp(attr
->name
, "job-originating-user-name") ||
10821 !strcmp(attr
->name
, "job-printer-up-time") ||
10822 !strcmp(attr
->name
, "job-printer-uri") ||
10823 !strcmp(attr
->name
, "job-sheets") ||
10824 !strcmp(attr
->name
, "job-state-message") ||
10825 !strcmp(attr
->name
, "job-state-reasons") ||
10826 !strcmp(attr
->name
, "job-uri") ||
10827 !strcmp(attr
->name
, "number-of-documents") ||
10828 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
10829 !strcmp(attr
->name
, "output-device-assigned") ||
10830 !strncmp(attr
->name
, "date-time-at-", 13) ||
10831 !strncmp(attr
->name
, "job-k-octets", 12) ||
10832 !strncmp(attr
->name
, "job-media-sheets", 16) ||
10833 !strncmp(attr
->name
, "time-at-", 8))
10839 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
10840 _("%s cannot be changed."), attr
->name
);
10842 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10843 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10848 if (!strcmp(attr
->name
, "job-priority"))
10851 * Change the job priority...
10854 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10856 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value"));
10858 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10859 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10861 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
10863 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10864 _("Job is completed and cannot be changed."));
10867 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10869 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
10870 attr
->values
[0].integer
);
10871 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
10874 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
10875 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
10878 else if (!strcmp(attr
->name
, "job-state"))
10881 * Change the job state...
10884 if (attr
->value_tag
!= IPP_TAG_ENUM
)
10886 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value"));
10888 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10889 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10893 switch (attr
->values
[0].integer
)
10895 case IPP_JOB_PENDING
:
10896 case IPP_JOB_HELD
:
10897 if (job
->state_value
> IPP_JOB_HELD
)
10899 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10900 _("Job state cannot be changed."));
10903 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10905 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10906 attr
->values
[0].integer
);
10907 cupsdSetJobState(job
, attr
->values
[0].integer
,
10909 "Job state changed by \"%s\"", username
);
10914 case IPP_JOB_PROCESSING
:
10915 case IPP_JOB_STOPPED
:
10916 if (job
->state_value
!= attr
->values
[0].integer
)
10918 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10919 _("Job state cannot be changed."));
10924 case IPP_JOB_CANCELED
:
10925 case IPP_JOB_ABORTED
:
10926 case IPP_JOB_COMPLETED
:
10927 if (job
->state_value
> IPP_JOB_PROCESSING
)
10929 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10930 _("Job state cannot be changed."));
10933 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10935 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10936 attr
->values
[0].integer
);
10937 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
,
10939 "Job state changed by \"%s\"", username
);
10946 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10948 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10949 IPP_TAG_ZERO
)) != NULL
)
10952 * Some other value; first free the old value...
10955 if (job
->attrs
->prev
)
10956 job
->attrs
->prev
->next
= attr2
->next
;
10958 job
->attrs
->attrs
= attr2
->next
;
10960 if (job
->attrs
->last
== attr2
)
10961 job
->attrs
->last
= job
->attrs
->prev
;
10963 _ippFreeAttr(attr2
);
10966 * Then copy the attribute...
10969 copy_attribute(job
->attrs
, attr
, 0);
10972 * See if the job-name or job-hold-until is being changed.
10975 if (!strcmp(attr
->name
, "job-hold-until"))
10977 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10978 attr
->values
[0].string
.text
);
10979 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
10981 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10983 cupsdReleaseJob(job
);
10987 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
,
10988 "Job held by \"%s\".", username
);
10990 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10993 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10996 * Delete the attribute...
10999 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
11000 IPP_TAG_ZERO
)) != NULL
)
11002 if (job
->attrs
->prev
)
11003 job
->attrs
->prev
->next
= attr2
->next
;
11005 job
->attrs
->attrs
= attr2
->next
;
11007 if (attr2
== job
->attrs
->last
)
11008 job
->attrs
->last
= job
->attrs
->prev
;
11010 _ippFreeAttr(attr2
);
11012 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
11018 * Add new option by copying it...
11021 copy_attribute(job
->attrs
, attr
, 0);
11023 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
11032 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
11035 * Send events as needed...
11038 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
11039 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
11040 cupsdFindDest(job
->dest
), job
,
11041 "Job priority changed by user.");
11043 if (event
& CUPSD_EVENT_JOB_STATE
)
11044 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
11045 job
->state_value
== IPP_JOB_HELD
?
11046 "Job held by user." : "Job restarted by user.");
11048 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
11049 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
11050 "Job options changed by user.");
11053 * Start jobs if possible...
11062 * 'set_printer_attrs()' - Set printer attributes.
11066 set_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
11067 ipp_attribute_t
*uri
) /* I - Printer */
11069 http_status_t status
; /* Policy status */
11070 cups_ptype_t dtype
; /* Destination type (printer/class) */
11071 cupsd_printer_t
*printer
; /* Printer/class */
11072 ipp_attribute_t
*attr
; /* Printer attribute */
11073 int changed
= 0; /* Was anything changed? */
11076 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_attrs(%p[%d], %s)", con
,
11077 con
->http
.fd
, uri
->values
[0].string
.text
);
11080 * Is the destination valid?
11083 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11089 send_ipp_status(con
, IPP_NOT_FOUND
,
11090 _("The printer or class was not found."));
11098 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11100 send_http_error(con
, status
, printer
);
11105 * Return a list of attributes that can be set via Set-Printer-Attributes.
11108 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
11109 IPP_TAG_TEXT
)) != NULL
)
11111 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
11115 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
11116 IPP_TAG_TEXT
)) != NULL
)
11118 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
11123 * Update the printer attributes and return...
11128 cupsdSetPrinterAttrs(printer
);
11129 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
11131 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG
, printer
, NULL
,
11132 "Printer \"%s\" description or location changed by \"%s\".",
11133 printer
->name
, get_username(con
));
11135 cupsdLogMessage(CUPSD_LOG_INFO
,
11136 "Printer \"%s\" description or location changed by \"%s\".",
11137 printer
->name
, get_username(con
));
11140 con
->response
->request
.status
.status_code
= IPP_OK
;
11145 * 'set_printer_defaults()' - Set printer default options from a request.
11149 set_printer_defaults(
11150 cupsd_client_t
*con
, /* I - Client connection */
11151 cupsd_printer_t
*printer
) /* I - Printer */
11153 int i
; /* Looping var */
11154 ipp_attribute_t
*attr
; /* Current attribute */
11155 int namelen
; /* Length of attribute name */
11156 char name
[256], /* New attribute name */
11157 value
[256]; /* String version of integer attrs */
11160 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
11163 * Skip non-printer attributes...
11166 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
11169 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
11171 if (!strcmp(attr
->name
, "job-sheets-default"))
11174 * Only allow keywords and names...
11177 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
11181 * Only allow job-sheets-default to be set when running without a
11182 * system high classification level...
11185 if (Classification
)
11188 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
11190 if (attr
->num_values
> 1)
11191 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
11193 cupsdSetString(&printer
->job_sheets
[1], "none");
11195 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
11197 cupsdFreePrinterUsers(printer
);
11199 printer
->deny_users
= 0;
11201 if (attr
->value_tag
== IPP_TAG_NAME
&&
11202 (attr
->num_values
> 1 ||
11203 strcmp(attr
->values
[0].string
.text
, "all")))
11205 for (i
= 0; i
< attr
->num_values
; i
++)
11206 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
11209 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
11211 cupsdFreePrinterUsers(printer
);
11213 printer
->deny_users
= 1;
11215 if (attr
->value_tag
== IPP_TAG_NAME
&&
11216 (attr
->num_values
> 1 ||
11217 strcmp(attr
->values
[0].string
.text
, "none")))
11219 for (i
= 0; i
< attr
->num_values
; i
++)
11220 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
11223 else if (!strcmp(attr
->name
, "job-quota-period"))
11225 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
11228 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
11229 attr
->values
[0].integer
);
11230 cupsdFreeQuotas(printer
);
11232 printer
->quota_period
= attr
->values
[0].integer
;
11234 else if (!strcmp(attr
->name
, "job-k-limit"))
11236 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
11239 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
11240 attr
->values
[0].integer
);
11241 cupsdFreeQuotas(printer
);
11243 printer
->k_limit
= attr
->values
[0].integer
;
11245 else if (!strcmp(attr
->name
, "job-page-limit"))
11247 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
11250 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
11251 attr
->values
[0].integer
);
11252 cupsdFreeQuotas(printer
);
11254 printer
->page_limit
= attr
->values
[0].integer
;
11256 else if (!strcmp(attr
->name
, "printer-op-policy"))
11258 cupsd_policy_t
*p
; /* Policy */
11261 if (attr
->value_tag
!= IPP_TAG_NAME
)
11264 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
11266 cupsdLogMessage(CUPSD_LOG_DEBUG
,
11267 "Setting printer-op-policy to \"%s\"...",
11268 attr
->values
[0].string
.text
);
11269 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
11270 printer
->op_policy_ptr
= p
;
11274 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
11275 _("Unknown printer-op-policy \"%s\"."),
11276 attr
->values
[0].string
.text
);
11280 else if (!strcmp(attr
->name
, "printer-error-policy"))
11282 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
11285 if (strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
11286 ((printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)) ||
11287 (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
11288 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
11289 strcmp(attr
->values
[0].string
.text
, "stop-printer"))))
11291 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
11292 _("Unknown printer-error-policy \"%s\"."),
11293 attr
->values
[0].string
.text
);
11297 cupsdLogMessage(CUPSD_LOG_DEBUG
,
11298 "Setting printer-error-policy to \"%s\"...",
11299 attr
->values
[0].string
.text
);
11300 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
11304 * Skip any other non-default attributes...
11307 namelen
= strlen(attr
->name
);
11308 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
11309 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
11313 * OK, anything else must be a user-defined default...
11316 strlcpy(name
, attr
->name
, sizeof(name
));
11317 name
[namelen
- 8] = '\0'; /* Strip "-default" */
11319 switch (attr
->value_tag
)
11321 case IPP_TAG_DELETEATTR
:
11322 printer
->num_options
= cupsRemoveOption(name
,
11323 printer
->num_options
,
11324 &(printer
->options
));
11325 cupsdLogMessage(CUPSD_LOG_DEBUG
,
11326 "Deleting %s", attr
->name
);
11329 case IPP_TAG_NAME
:
11330 case IPP_TAG_KEYWORD
:
11332 printer
->num_options
= cupsAddOption(name
,
11333 attr
->values
[0].string
.text
,
11334 printer
->num_options
,
11335 &(printer
->options
));
11336 cupsdLogMessage(CUPSD_LOG_DEBUG
,
11337 "Setting %s to \"%s\"...", attr
->name
,
11338 attr
->values
[0].string
.text
);
11341 case IPP_TAG_BOOLEAN
:
11342 printer
->num_options
= cupsAddOption(name
,
11343 attr
->values
[0].boolean
?
11345 printer
->num_options
,
11346 &(printer
->options
));
11347 cupsdLogMessage(CUPSD_LOG_DEBUG
,
11348 "Setting %s to %s...", attr
->name
,
11349 attr
->values
[0].boolean
? "true" : "false");
11352 case IPP_TAG_INTEGER
:
11353 case IPP_TAG_ENUM
:
11354 sprintf(value
, "%d", attr
->values
[0].integer
);
11355 printer
->num_options
= cupsAddOption(name
, value
,
11356 printer
->num_options
,
11357 &(printer
->options
));
11358 cupsdLogMessage(CUPSD_LOG_DEBUG
,
11359 "Setting %s to %s...", attr
->name
, value
);
11362 case IPP_TAG_RANGE
:
11363 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
11364 attr
->values
[0].range
.upper
);
11365 printer
->num_options
= cupsAddOption(name
, value
,
11366 printer
->num_options
,
11367 &(printer
->options
));
11368 cupsdLogMessage(CUPSD_LOG_DEBUG
,
11369 "Setting %s to %s...", attr
->name
, value
);
11372 case IPP_TAG_RESOLUTION
:
11373 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
11374 attr
->values
[0].resolution
.yres
,
11375 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
11377 printer
->num_options
= cupsAddOption(name
, value
,
11378 printer
->num_options
,
11379 &(printer
->options
));
11380 cupsdLogMessage(CUPSD_LOG_DEBUG
,
11381 "Setting %s to %s...", attr
->name
, value
);
11385 /* Do nothing for other values */
11393 * 'start_printer()' - Start a printer.
11397 start_printer(cupsd_client_t
*con
, /* I - Client connection */
11398 ipp_attribute_t
*uri
) /* I - Printer URI */
11400 http_status_t status
; /* Policy status */
11401 cups_ptype_t dtype
; /* Destination type (printer/class) */
11402 cupsd_printer_t
*printer
; /* Printer data */
11405 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
11406 con
->http
.fd
, uri
->values
[0].string
.text
);
11409 * Is the destination valid?
11412 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11418 send_ipp_status(con
, IPP_NOT_FOUND
,
11419 _("The printer or class was not found."));
11427 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11429 send_http_error(con
, status
, printer
);
11434 * Start the printer...
11437 printer
->state_message
[0] = '\0';
11439 cupsdStartPrinter(printer
, 1);
11441 if (dtype
& CUPS_PRINTER_CLASS
)
11442 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
11443 printer
->name
, get_username(con
));
11445 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
11446 printer
->name
, get_username(con
));
11451 * Everything was ok, so return OK status...
11454 con
->response
->request
.status
.status_code
= IPP_OK
;
11459 * 'stop_printer()' - Stop a printer.
11463 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
11464 ipp_attribute_t
*uri
) /* I - Printer URI */
11466 http_status_t status
; /* Policy status */
11467 cups_ptype_t dtype
; /* Destination type (printer/class) */
11468 cupsd_printer_t
*printer
; /* Printer data */
11469 ipp_attribute_t
*attr
; /* printer-state-message attribute */
11472 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
11473 con
->http
.fd
, uri
->values
[0].string
.text
);
11476 * Is the destination valid?
11479 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11485 send_ipp_status(con
, IPP_NOT_FOUND
,
11486 _("The printer or class was not found."));
11494 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11496 send_http_error(con
, status
, printer
);
11501 * Stop the printer...
11504 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
11505 IPP_TAG_TEXT
)) == NULL
)
11506 strcpy(printer
->state_message
, "Paused");
11509 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
11510 sizeof(printer
->state_message
));
11513 cupsdStopPrinter(printer
, 1);
11515 if (dtype
& CUPS_PRINTER_CLASS
)
11516 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
11517 printer
->name
, get_username(con
));
11519 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
11520 printer
->name
, get_username(con
));
11523 * Everything was ok, so return OK status...
11526 con
->response
->request
.status
.status_code
= IPP_OK
;
11531 * 'url_encode_attr()' - URL-encode a string attribute.
11535 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
11536 char *buffer
,/* I - String buffer */
11537 int bufsize
)/* I - Size of buffer */
11539 int i
; /* Looping var */
11540 char *bufptr
, /* Pointer into buffer */
11541 *bufend
; /* End of buffer */
11544 strlcpy(buffer
, attr
->name
, bufsize
);
11545 bufptr
= buffer
+ strlen(buffer
);
11546 bufend
= buffer
+ bufsize
- 1;
11548 for (i
= 0; i
< attr
->num_values
; i
++)
11550 if (bufptr
>= bufend
)
11558 if (bufptr
>= bufend
)
11563 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
11564 bufptr
, bufend
- bufptr
+ 1);
11566 if (bufptr
>= bufend
)
11577 * 'url_encode_string()' - URL-encode a string.
11580 static char * /* O - End of string */
11581 url_encode_string(const char *s
, /* I - String */
11582 char *buffer
, /* I - String buffer */
11583 int bufsize
) /* I - Size of buffer */
11585 char *bufptr
, /* Pointer into buffer */
11586 *bufend
; /* End of buffer */
11587 static const char *hex
= "0123456789ABCDEF";
11592 bufend
= buffer
+ bufsize
- 1;
11594 while (*s
&& bufptr
< bufend
)
11596 if (*s
== ' ' || *s
== '%' || *s
== '+')
11598 if (bufptr
>= (bufend
- 2))
11602 *bufptr
++ = hex
[(*s
>> 4) & 15];
11603 *bufptr
++ = hex
[*s
& 15];
11607 else if (*s
== '\'' || *s
== '\\')
11609 if (bufptr
>= (bufend
- 1))
11626 * 'user_allowed()' - See if a user is allowed to print to a queue.
11629 static int /* O - 0 if not allowed, 1 if allowed */
11630 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
11631 const char *username
) /* I - Username */
11633 int i
; /* Looping var */
11634 struct passwd
*pw
; /* User password data */
11635 char baseuser
[256], /* Base username */
11636 *baseptr
; /* Pointer to "@" in base username */
11639 if (p
->num_users
== 0)
11642 if (!strcmp(username
, "root"))
11645 if (strchr(username
, '@'))
11648 * Strip @REALM for username check...
11651 strlcpy(baseuser
, username
, sizeof(baseuser
));
11653 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
11656 username
= baseuser
;
11659 pw
= getpwnam(username
);
11662 for (i
= 0; i
< p
->num_users
; i
++)
11664 if (p
->users
[i
][0] == '@')
11667 * Check group membership...
11670 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
11673 else if (p
->users
[i
][0] == '#')
11679 if (cupsdCheckGroup(username
, pw
, p
->users
[i
]))
11682 else if (!strcasecmp(username
, p
->users
[i
]))
11686 return ((i
< p
->num_users
) != p
->deny_users
);
11691 * 'validate_job()' - Validate printer options and destination.
11695 validate_job(cupsd_client_t
*con
, /* I - Client connection */
11696 ipp_attribute_t
*uri
) /* I - Printer URI */
11698 http_status_t status
; /* Policy status */
11699 ipp_attribute_t
*attr
; /* Current attribute */
11700 ipp_attribute_t
*format
; /* Document-format attribute */
11701 cups_ptype_t dtype
; /* Destination type (printer/class) */
11702 char super
[MIME_MAX_SUPER
],
11703 /* Supertype of file */
11704 type
[MIME_MAX_TYPE
];
11705 /* Subtype of file */
11706 cupsd_printer_t
*printer
; /* Printer */
11709 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
11710 con
->http
.fd
, uri
->values
[0].string
.text
);
11713 * OK, see if the client is sending the document compressed - CUPS
11714 * doesn't support compression yet...
11717 if ((attr
= ippFindAttribute(con
->request
, "compression",
11718 IPP_TAG_KEYWORD
)) != NULL
&&
11719 !strcmp(attr
->values
[0].string
.text
, "none"))
11721 send_ipp_status(con
, IPP_ATTRIBUTES
,
11722 _("Unsupported compression attribute %s"),
11723 attr
->values
[0].string
.text
);
11724 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
11725 "compression", NULL
, attr
->values
[0].string
.text
);
11730 * Is it a format we support?
11733 if ((format
= ippFindAttribute(con
->request
, "document-format",
11734 IPP_TAG_MIMETYPE
)) != NULL
)
11736 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
11739 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\""),
11740 format
->values
[0].string
.text
);
11744 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
11745 !mimeType(MimeDatabase
, super
, type
))
11747 cupsdLogMessage(CUPSD_LOG_INFO
,
11748 "Hint: Do you have the raw file printing rules enabled?");
11749 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
11750 _("Unsupported format \"%s\""),
11751 format
->values
[0].string
.text
);
11752 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
11753 "document-format", NULL
, format
->values
[0].string
.text
);
11759 * Is the destination valid?
11762 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11768 send_ipp_status(con
, IPP_NOT_FOUND
,
11769 _("The printer or class was not found."));
11777 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11779 send_http_error(con
, status
, printer
);
11784 * Everything was ok, so return OK status...
11787 con
->response
->request
.status
.status_code
= IPP_OK
;
11792 * 'validate_name()' - Make sure the printer name only contains valid chars.
11795 static int /* O - 0 if name is no good, 1 if good */
11796 validate_name(const char *name
) /* I - Name to check */
11798 const char *ptr
; /* Pointer into name */
11802 * Scan the whole name...
11805 for (ptr
= name
; *ptr
; ptr
++)
11806 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
11810 * All the characters are good; validate the length, too...
11813 return ((ptr
- name
) < 128);
11818 * 'validate_user()' - Validate the user for the request.
11821 static int /* O - 1 if permitted, 0 otherwise */
11822 validate_user(cupsd_job_t
*job
, /* I - Job */
11823 cupsd_client_t
*con
, /* I - Client connection */
11824 const char *owner
, /* I - Owner of job/resource */
11825 char *username
, /* O - Authenticated username */
11826 int userlen
) /* I - Length of username */
11828 cupsd_printer_t
*printer
; /* Printer for job */
11831 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
11832 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
11834 job
->id
, con
? con
->http
.fd
: 0,
11835 owner
? owner
: "(null)", username
, userlen
);
11838 * Validate input...
11841 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11845 * Get the best authenticated username that is available.
11848 strlcpy(username
, get_username(con
), userlen
);
11851 * Check the username against the owner...
11854 printer
= cupsdFindDest(job
->dest
);
11856 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11857 con
, owner
) == HTTP_OK
);
11862 * End of "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $".