2 * "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $"
4 * IPP routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
20 * cupsdProcessIPPRequest() - Process an incoming IPP request.
21 * cupsdTimeoutJob() - Timeout a job waiting on job files.
22 * accept_jobs() - Accept print jobs to a printer.
23 * add_class() - Add a class to the system.
24 * add_file() - Add a file to a job.
25 * add_job() - Add a job to a print queue.
26 * add_job_state_reasons() - Add the "job-state-reasons" attribute based
27 * upon the job and printer state...
28 * add_job_subscriptions() - Add any subscriptions for a job.
29 * add_job_uuid() - Add job-uuid attribute to a job.
30 * add_printer() - Add a printer to the system.
31 * add_printer_state_reasons() - Add the "printer-state-reasons" attribute
32 * based upon the printer state...
33 * add_queued_job_count() - Add the "queued-job-count" attribute for the
34 * specified printer or class.
35 * apple_init_profile() - Initialize a color profile.
36 * apple_register_profiles() - Register color profiles for a printer.
37 * apple_unregister_profiles() - Remove color profiles for the specified
39 * apply_printer_defaults() - Apply printer default options to a job.
40 * authenticate_job() - Set job authentication info.
41 * cancel_all_jobs() - Cancel all print jobs.
42 * cancel_job() - Cancel a print job.
43 * cancel_subscription() - Cancel a subscription.
44 * check_quotas() - Check quotas for a printer and user.
45 * check_rss_recipient() - Check that we do not have a duplicate RSS
47 * copy_attribute() - Copy a single attribute.
48 * copy_attrs() - Copy attributes from one request to another.
49 * copy_banner() - Copy a banner file to the requests directory
50 * for the specified job.
51 * copy_file() - Copy a PPD file or interface script...
52 * copy_model() - Copy a PPD model file, substituting default
54 * copy_job_attrs() - Copy job attributes.
55 * copy_printer_attrs() - Copy printer attributes.
56 * copy_subscription_attrs() - Copy subscription attributes.
57 * create_job() - Print a file to a printer or class.
58 * create_requested_array() - Create an array for the requested-attributes.
59 * create_subscription() - Create a notification subscription.
60 * delete_printer() - Remove a printer or class from the system.
61 * get_default() - Get the default destination.
62 * get_devices() - Get the list of available devices on the
64 * get_document() - Get a copy of a job file.
65 * get_job_attrs() - Get job attributes.
66 * get_jobs() - Get a list of jobs for the specified printer.
67 * get_notifications() - Get events for a subscription.
68 * get_ppd() - Get a named PPD from the local system.
69 * get_ppds() - Get the list of PPD files on the local
71 * get_printer_attrs() - Get printer attributes.
72 * get_printers() - Get a list of printers or classes.
73 * get_subscription_attrs() - Get subscription attributes.
74 * get_subscriptions() - Get subscriptions.
75 * get_username() - Get the username associated with a request.
76 * hold_job() - Hold a print job.
77 * move_job() - Move a job to a new destination.
78 * ppd_parse_line() - Parse a PPD default line.
79 * print_job() - Print a file to a printer or class.
80 * read_job_ticket() - Read a job ticket embedded in a print file.
81 * reject_jobs() - Reject print jobs to a printer.
82 * release_job() - Release a held print job.
83 * renew_subscription() - Renew an existing subscription...
84 * restart_job() - Restart an old print job.
85 * save_auth_info() - Save authentication information for a job.
86 * save_krb5_creds() - Save Kerberos credentials for the job.
87 * send_document() - Send a file to a printer or class.
88 * send_http_error() - Send a HTTP error back to the IPP client.
89 * send_ipp_status() - Send a status back to the IPP client.
90 * set_default() - Set the default destination...
91 * set_job_attrs() - Set job attributes.
92 * set_printer_defaults() - Set printer default options from a request.
93 * start_printer() - Start a printer.
94 * stop_printer() - Stop a printer.
95 * url_encode_attr() - URL-encode a string attribute.
96 * url_encode_string() - URL-encode a string.
97 * user_allowed() - See if a user is allowed to print to a queue.
98 * validate_job() - Validate printer options and destination.
99 * validate_name() - Make sure the printer name only contains
101 * validate_user() - Validate the user for the request.
105 * Include necessary headers...
109 #include <cups/ppd-private.h>
113 #endif /* HAVE_LIBPAPER */
116 # include <ApplicationServices/ApplicationServices.h>
117 # include <CoreFoundation/CoreFoundation.h>
118 # ifdef HAVE_MEMBERSHIP_H
119 # include <membership.h>
120 # endif /* HAVE_MEMBERSHIP_H */
121 # ifdef HAVE_MEMBERSHIPPRIV_H
122 # include <membershipPriv.h>
124 extern int mbr_user_name_to_uuid(const char* name
, uuid_t uu
);
125 extern int mbr_group_name_to_uuid(const char* name
, uuid_t uu
);
126 extern int mbr_check_membership_by_id(uuid_t user
, gid_t group
, int* ismember
);
127 # endif /* HAVE_MEMBERSHIPPRIV_H */
128 #endif /* __APPLE__ */
135 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
136 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
137 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
138 mime_type_t
*filetype
, int compression
);
139 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
140 mime_type_t
*filetype
);
141 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
142 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
143 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
144 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
145 static void add_printer_state_reasons(cupsd_client_t
*con
,
147 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
149 static void apple_init_profile(ppd_file_t
*ppd
, cups_array_t
*languages
,
150 CMDeviceProfileInfo
*profile
, unsigned id
,
151 const char *name
, const char *text
,
152 const char *iccfile
);
153 static void apple_register_profiles(cupsd_printer_t
*p
);
154 static void apple_unregister_profiles(cupsd_printer_t
*p
);
155 #endif /* __APPLE__ */
156 static void apply_printer_defaults(cupsd_printer_t
*printer
,
158 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
159 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
160 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
161 static void cancel_subscription(cupsd_client_t
*con
, int id
);
162 static int check_rss_recipient(const char *recipient
);
163 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
164 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
166 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
167 ipp_tag_t group
, int quickcopy
);
168 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
170 static int copy_file(const char *from
, const char *to
);
171 static int copy_model(cupsd_client_t
*con
, const char *from
,
173 static void copy_job_attrs(cupsd_client_t
*con
,
176 static void copy_printer_attrs(cupsd_client_t
*con
,
177 cupsd_printer_t
*printer
,
179 static void copy_subscription_attrs(cupsd_client_t
*con
,
180 cupsd_subscription_t
*sub
,
182 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
183 static cups_array_t
*create_requested_array(ipp_t
*request
);
184 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
185 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
186 static void get_default(cupsd_client_t
*con
);
187 static void get_devices(cupsd_client_t
*con
);
188 static void get_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
189 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
190 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
191 static void get_notifications(cupsd_client_t
*con
);
192 static void get_ppd(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
193 static void get_ppds(cupsd_client_t
*con
);
194 static void get_printers(cupsd_client_t
*con
, int type
);
195 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
196 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
197 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
198 static const char *get_username(cupsd_client_t
*con
);
199 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
200 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
201 static int ppd_parse_line(const char *line
, char *option
, int olen
,
202 char *choice
, int clen
);
203 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
204 static void read_job_ticket(cupsd_client_t
*con
);
205 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
206 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
207 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
208 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
209 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
,
210 ipp_attribute_t
*auth_info
);
211 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
212 static void save_krb5_creds(cupsd_client_t
*con
, cupsd_job_t
*job
);
213 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
214 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
215 static void send_http_error(cupsd_client_t
*con
, http_status_t status
,
216 cupsd_printer_t
*printer
);
217 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
218 const char *message
, ...)
220 __attribute__ ((__format__ (__printf__
, 3, 4)))
221 # endif /* __GNUC__ */
223 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
224 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
225 static void set_printer_defaults(cupsd_client_t
*con
,
226 cupsd_printer_t
*printer
);
227 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
228 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
229 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
,
231 static char *url_encode_string(const char *s
, char *buffer
, int bufsize
);
232 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
233 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
234 static int validate_name(const char *name
);
235 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
,
236 const char *owner
, char *username
,
241 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
244 int /* O - 1 on success, 0 on failure */
245 cupsdProcessIPPRequest(
246 cupsd_client_t
*con
) /* I - Client connection */
248 ipp_tag_t group
; /* Current group tag */
249 ipp_attribute_t
*attr
; /* Current attribute */
250 ipp_attribute_t
*charset
; /* Character set attribute */
251 ipp_attribute_t
*language
; /* Language attribute */
252 ipp_attribute_t
*uri
= NULL
; /* Printer or job URI attribute */
253 ipp_attribute_t
*username
; /* requesting-user-name attr */
254 int sub_id
; /* Subscription ID */
257 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
258 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
259 con
, con
->http
.fd
, con
->request
->request
.op
.operation_id
);
262 * First build an empty response message for this request...
265 con
->response
= ippNew();
267 con
->response
->request
.status
.version
[0] =
268 con
->request
->request
.op
.version
[0];
269 con
->response
->request
.status
.version
[1] =
270 con
->request
->request
.op
.version
[1];
271 con
->response
->request
.status
.request_id
=
272 con
->request
->request
.op
.request_id
;
275 * Then validate the request header and required attributes...
278 if (con
->request
->request
.any
.version
[0] != 1)
281 * Return an error, since we only support IPP 1.x.
284 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
285 "%04X %s Bad request version number %d.%d",
286 IPP_VERSION_NOT_SUPPORTED
, con
->http
.hostname
,
287 con
->request
->request
.any
.version
[0],
288 con
->request
->request
.any
.version
[1]);
290 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
291 _("Bad request version number %d.%d!"),
292 con
->request
->request
.any
.version
[0],
293 con
->request
->request
.any
.version
[1]);
295 else if (!con
->request
->attrs
)
297 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
298 "%04X %s No attributes in request",
299 IPP_BAD_REQUEST
, con
->http
.hostname
);
301 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request!"));
306 * Make sure that the attributes are provided in the correct order and
307 * don't repeat groups...
310 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
313 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
316 * Out of order; return an error...
319 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
320 "%04X %s Attribute groups are out of order",
321 IPP_BAD_REQUEST
, con
->http
.hostname
);
323 send_ipp_status(con
, IPP_BAD_REQUEST
,
324 _("Attribute groups are out of order (%x < %x)!"),
325 attr
->group_tag
, group
);
329 group
= attr
->group_tag
;
334 * Then make sure that the first three attributes are:
337 * attributes-natural-language
338 * printer-uri/job-uri
341 attr
= con
->request
->attrs
;
342 if (attr
&& !strcmp(attr
->name
, "attributes-charset") &&
343 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
351 if (attr
&& !strcmp(attr
->name
, "attributes-natural-language") &&
352 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
357 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
358 IPP_TAG_URI
)) != NULL
)
360 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
361 IPP_TAG_URI
)) != NULL
)
363 else if (con
->request
->request
.op
.operation_id
== CUPS_GET_PPD
)
364 uri
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
);
369 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
370 "attributes-charset", NULL
,
371 charset
->values
[0].string
.text
);
373 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
374 "attributes-charset", NULL
, DefaultCharset
);
377 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
378 "attributes-natural-language", NULL
,
379 language
->values
[0].string
.text
);
381 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
382 "attributes-natural-language", NULL
, DefaultLanguage
);
385 strcasecmp(charset
->values
[0].string
.text
, "us-ascii") &&
386 strcasecmp(charset
->values
[0].string
.text
, "utf-8"))
389 * Bad character set...
392 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unsupported character set \"%s\"!",
393 charset
->values
[0].string
.text
);
394 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
395 "%04X %s Unsupported attributes-charset value \"%s\"",
396 IPP_CHARSET
, con
->http
.hostname
,
397 charset
->values
[0].string
.text
);
398 send_ipp_status(con
, IPP_BAD_REQUEST
,
399 _("Unsupported character set \"%s\"!"),
400 charset
->values
[0].string
.text
);
402 else if (!charset
|| !language
||
404 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
405 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
406 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
407 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
408 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
411 * Return an error, since attributes-charset,
412 * attributes-natural-language, and printer-uri/job-uri are required
413 * for all operations.
418 cupsdLogMessage(CUPSD_LOG_ERROR
,
419 "Missing attributes-charset attribute!");
421 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
422 "%04X %s Missing attributes-charset attribute",
423 IPP_BAD_REQUEST
, con
->http
.hostname
);
428 cupsdLogMessage(CUPSD_LOG_ERROR
,
429 "Missing attributes-natural-language attribute!");
431 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
432 "%04X %s Missing attributes-natural-language attribute",
433 IPP_BAD_REQUEST
, con
->http
.hostname
);
438 cupsdLogMessage(CUPSD_LOG_ERROR
,
439 "Missing printer-uri, job-uri, or ppd-name "
442 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
443 "%04X %s Missing printer-uri, job-uri, or ppd-name "
444 "attribute", IPP_BAD_REQUEST
, con
->http
.hostname
);
447 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
449 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
450 cupsdLogMessage(CUPSD_LOG_DEBUG
,
451 "attr \"%s\": group_tag = %x, value_tag = %x",
452 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
455 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
457 send_ipp_status(con
, IPP_BAD_REQUEST
,
458 _("Missing required attributes!"));
463 * OK, all the checks pass so far; make sure requesting-user-name is
464 * not "root" from a remote host...
467 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
468 IPP_TAG_NAME
)) != NULL
)
471 * Check for root user...
474 if (!strcmp(username
->values
[0].string
.text
, "root") &&
475 strcasecmp(con
->http
.hostname
, "localhost") &&
476 strcmp(con
->username
, "root"))
479 * Remote unauthenticated user masquerading as local root...
482 _cupsStrFree(username
->values
[0].string
.text
);
483 username
->values
[0].string
.text
= _cupsStrAlloc(RemoteRoot
);
487 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
488 IPP_TAG_INTEGER
)) != NULL
)
489 sub_id
= attr
->values
[0].integer
;
494 * Then try processing the operation...
498 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
499 ippOpString(con
->request
->request
.op
.operation_id
),
500 uri
->values
[0].string
.text
);
502 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
503 ippOpString(con
->request
->request
.op
.operation_id
));
505 switch (con
->request
->request
.op
.operation_id
)
511 case IPP_VALIDATE_JOB
:
512 validate_job(con
, uri
);
515 case IPP_CREATE_JOB
:
516 create_job(con
, uri
);
519 case IPP_SEND_DOCUMENT
:
520 send_document(con
, uri
);
523 case IPP_CANCEL_JOB
:
524 cancel_job(con
, uri
);
527 case IPP_GET_JOB_ATTRIBUTES
:
528 get_job_attrs(con
, uri
);
535 case IPP_GET_PRINTER_ATTRIBUTES
:
536 get_printer_attrs(con
, uri
);
543 case IPP_RELEASE_JOB
:
544 release_job(con
, uri
);
547 case IPP_RESTART_JOB
:
548 restart_job(con
, uri
);
551 case IPP_PAUSE_PRINTER
:
552 stop_printer(con
, uri
);
555 case IPP_RESUME_PRINTER
:
556 start_printer(con
, uri
);
559 case IPP_PURGE_JOBS
:
560 cancel_all_jobs(con
, uri
);
563 case IPP_SET_JOB_ATTRIBUTES
:
564 set_job_attrs(con
, uri
);
567 case CUPS_GET_DEFAULT
:
571 case CUPS_GET_PRINTERS
:
572 get_printers(con
, 0);
575 case CUPS_GET_CLASSES
:
576 get_printers(con
, CUPS_PRINTER_CLASS
);
579 case CUPS_ADD_PRINTER
:
580 add_printer(con
, uri
);
583 case CUPS_DELETE_PRINTER
:
584 delete_printer(con
, uri
);
587 case CUPS_ADD_CLASS
:
591 case CUPS_DELETE_CLASS
:
592 delete_printer(con
, uri
);
595 case CUPS_ACCEPT_JOBS
:
596 case IPP_ENABLE_PRINTER
:
597 accept_jobs(con
, uri
);
600 case CUPS_REJECT_JOBS
:
601 case IPP_DISABLE_PRINTER
:
602 reject_jobs(con
, uri
);
605 case CUPS_SET_DEFAULT
:
606 set_default(con
, uri
);
609 case CUPS_GET_DEVICES
:
613 case CUPS_GET_DOCUMENT
:
614 get_document(con
, uri
);
629 case CUPS_AUTHENTICATE_JOB
:
630 authenticate_job(con
, uri
);
633 case IPP_CREATE_PRINTER_SUBSCRIPTION
:
634 case IPP_CREATE_JOB_SUBSCRIPTION
:
635 create_subscription(con
, uri
);
638 case IPP_GET_SUBSCRIPTION_ATTRIBUTES
:
639 get_subscription_attrs(con
, sub_id
);
642 case IPP_GET_SUBSCRIPTIONS
:
643 get_subscriptions(con
, uri
);
646 case IPP_RENEW_SUBSCRIPTION
:
647 renew_subscription(con
, sub_id
);
650 case IPP_CANCEL_SUBSCRIPTION
:
651 cancel_subscription(con
, sub_id
);
654 case IPP_GET_NOTIFICATIONS
:
655 get_notifications(con
);
659 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
660 "%04X %s Operation %04X (%s) not supported",
661 IPP_OPERATION_NOT_SUPPORTED
, con
->http
.hostname
,
662 con
->request
->request
.op
.operation_id
,
663 ippOpString(con
->request
->request
.op
.operation_id
));
665 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
666 _("%s not supported!"),
668 con
->request
->request
.op
.operation_id
));
678 * Sending data from the scheduler...
681 cupsdLogMessage(con
->response
->request
.status
.status_code
682 >= IPP_BAD_REQUEST
&&
683 con
->response
->request
.status
.status_code
684 != IPP_NOT_FOUND
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
685 "Returning IPP %s for %s (%s) from %s",
686 ippErrorString(con
->response
->request
.status
.status_code
),
687 ippOpString(con
->request
->request
.op
.operation_id
),
688 uri
? uri
->values
[0].string
.text
: "no URI",
691 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp", CUPSD_AUTH_NONE
))
693 #ifdef CUPSD_USE_CHUNKING
695 * Because older versions of CUPS (1.1.17 and older) and some IPP
696 * clients do not implement chunking properly, we cannot use
697 * chunking by default. This may become the default in future
698 * CUPS releases, or we might add a configuration directive for
702 if (con
->http
.version
== HTTP_1_1
)
704 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n") < 0)
707 if (cupsdFlushHeader(con
) < 0)
710 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
713 #endif /* CUPSD_USE_CHUNKING */
715 size_t length
; /* Length of response */
718 length
= ippLength(con
->response
);
720 if (con
->file
>= 0 && !con
->pipe_pid
)
722 struct stat fileinfo
; /* File information */
725 if (!fstat(con
->file
, &fileinfo
))
726 length
+= fileinfo
.st_size
;
729 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
730 CUPS_LLCAST length
) < 0)
733 if (cupsdFlushHeader(con
) < 0)
736 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
737 con
->http
.data_remaining
= length
;
739 if (con
->http
.data_remaining
<= INT_MAX
)
740 con
->http
._data_remaining
= con
->http
.data_remaining
;
742 con
->http
._data_remaining
= INT_MAX
;
745 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
746 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
749 * Tell the caller the response header was sent successfully...
757 * Tell the caller the response header could not be sent...
766 * Sending data from a subprocess like cups-deviced; tell the caller
767 * everything is A-OK so far...
776 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
779 int /* O - 0 on success, -1 on error */
780 cupsdTimeoutJob(cupsd_job_t
*job
) /* I - Job to timeout */
782 cupsd_printer_t
*printer
; /* Destination printer or class */
783 ipp_attribute_t
*attr
; /* job-sheets attribute */
784 int kbytes
; /* Kilobytes in banner */
787 job
->pending_timeout
= 0;
790 * See if we need to add the ending sheet...
793 printer
= cupsdFindDest(job
->dest
);
794 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
797 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
798 attr
&& attr
->num_values
> 1)
804 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding end banner page \"%s\".",
805 attr
->values
[1].string
.text
);
807 if ((kbytes
= copy_banner(NULL
, job
, attr
->values
[1].string
.text
)) < 0)
810 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
818 * 'accept_jobs()' - Accept print jobs to a printer.
822 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
823 ipp_attribute_t
*uri
) /* I - Printer or class URI */
825 http_status_t status
; /* Policy status */
826 cups_ptype_t dtype
; /* Destination type (printer/class) */
827 cupsd_printer_t
*printer
; /* Printer data */
830 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
831 con
->http
.fd
, uri
->values
[0].string
.text
);
834 * Is the destination valid?
837 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
843 send_ipp_status(con
, IPP_NOT_FOUND
,
844 _("The printer or class was not found."));
852 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
854 send_http_error(con
, status
, printer
);
859 * Accept jobs sent to the printer...
862 printer
->accepting
= 1;
863 printer
->state_message
[0] = '\0';
865 cupsdAddPrinterHistory(printer
);
867 if (dtype
& CUPS_PRINTER_CLASS
)
869 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
871 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
872 printer
->name
, get_username(con
));
876 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
878 cupsdLogMessage(CUPSD_LOG_INFO
,
879 "Printer \"%s\" now accepting jobs (\"%s\").",
880 printer
->name
, get_username(con
));
884 * Everything was ok, so return OK status...
887 con
->response
->request
.status
.status_code
= IPP_OK
;
892 * 'add_class()' - Add a class to the system.
896 add_class(cupsd_client_t
*con
, /* I - Client connection */
897 ipp_attribute_t
*uri
) /* I - URI of class */
899 http_status_t status
; /* Policy status */
900 int i
; /* Looping var */
901 char method
[HTTP_MAX_URI
], /* Method portion of URI */
902 username
[HTTP_MAX_URI
], /* Username portion of URI */
903 host
[HTTP_MAX_URI
], /* Host portion of URI */
904 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
905 int port
; /* Port portion of URI */
906 cupsd_printer_t
*pclass
, /* Class */
907 *member
; /* Member printer/class */
908 cups_ptype_t dtype
; /* Destination type */
909 ipp_attribute_t
*attr
; /* Printer attribute */
910 int modify
; /* Non-zero if we just modified */
911 char newname
[IPP_MAX_NAME
]; /* New class name */
912 int need_restart_job
; /* Need to restart job? */
915 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
916 con
->http
.fd
, uri
->values
[0].string
.text
);
919 * Do we have a valid URI?
922 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
923 sizeof(method
), username
, sizeof(username
), host
,
924 sizeof(host
), &port
, resource
, sizeof(resource
));
927 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
930 * No, return an error...
933 send_ipp_status(con
, IPP_BAD_REQUEST
,
934 _("The printer-uri must be of the form "
935 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
940 * Do we have a valid printer name?
943 if (!validate_name(resource
+ 9))
946 * No, return an error...
949 send_ipp_status(con
, IPP_BAD_REQUEST
,
950 _("The printer-uri \"%s\" contains invalid characters."),
951 uri
->values
[0].string
.text
);
956 * See if the class already exists; if not, create a new class...
959 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
962 * Class doesn't exist; see if we have a printer of the same name...
965 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
966 !(pclass
->type
& CUPS_PRINTER_DISCOVERED
))
969 * Yes, return an error...
972 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
973 _("A printer named \"%s\" already exists!"),
979 * No, check the default policy and then add the class...
982 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
984 send_http_error(con
, status
, NULL
);
988 pclass
= cupsdAddClass(resource
+ 9);
991 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
994 * Check the default policy, then rename the implicit class to "AnyClass"
998 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
1000 send_http_error(con
, status
, NULL
);
1004 if (ImplicitAnyClasses
)
1006 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
1007 cupsdRenamePrinter(pclass
, newname
);
1010 cupsdDeletePrinter(pclass
, 1);
1013 * Add the class as a new local class...
1016 pclass
= cupsdAddClass(resource
+ 9);
1019 else if (pclass
->type
& CUPS_PRINTER_DISCOVERED
)
1022 * Check the default policy, then rename the remote class to "Class"...
1025 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
1027 send_http_error(con
, status
, NULL
);
1031 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
1032 cupsdRenamePrinter(pclass
, newname
);
1035 * Add the class as a new local class...
1038 pclass
= cupsdAddClass(resource
+ 9);
1041 else if ((status
= cupsdCheckPolicy(pclass
->op_policy_ptr
, con
,
1044 send_http_error(con
, status
, pclass
);
1051 * Look for attributes and copy them over as needed...
1054 need_restart_job
= 0;
1056 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
1057 IPP_TAG_TEXT
)) != NULL
)
1058 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
1060 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
1061 IPP_TAG_TEXT
)) != NULL
)
1062 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
1064 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
1065 IPP_TAG_BOOLEAN
)) != NULL
)
1067 cupsdLogMessage(CUPSD_LOG_INFO
,
1068 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
1069 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
1071 pclass
->accepting
= attr
->values
[0].boolean
;
1072 cupsdAddPrinterHistory(pclass
);
1075 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
1076 IPP_TAG_BOOLEAN
)) != NULL
)
1078 if (pclass
->shared
&& !attr
->values
[0].boolean
)
1079 cupsdDeregisterPrinter(pclass
, 1);
1081 cupsdLogMessage(CUPSD_LOG_INFO
,
1082 "Setting %s printer-is-shared to %d (was %d.)",
1083 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
1085 pclass
->shared
= attr
->values
[0].boolean
;
1088 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
1089 IPP_TAG_ENUM
)) != NULL
)
1091 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
1092 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
1094 send_ipp_status(con
, IPP_BAD_REQUEST
,
1095 _("Attempt to set %s printer-state to bad value %d!"),
1096 pclass
->name
, attr
->values
[0].integer
);
1100 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
1101 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
1103 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1104 cupsdStopPrinter(pclass
, 0);
1107 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1108 need_restart_job
= 1;
1111 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1112 IPP_TAG_TEXT
)) != NULL
)
1114 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1115 sizeof(pclass
->state_message
));
1116 cupsdAddPrinterHistory(pclass
);
1118 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1119 IPP_TAG_URI
)) != NULL
)
1122 * Clear the printer array as needed...
1125 need_restart_job
= 1;
1127 if (pclass
->num_printers
> 0)
1129 free(pclass
->printers
);
1130 pclass
->num_printers
= 0;
1134 * Add each printer or class that is listed...
1137 for (i
= 0; i
< attr
->num_values
; i
++)
1140 * Search for the printer or class URI...
1143 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1149 send_ipp_status(con
, IPP_NOT_FOUND
,
1150 _("The printer or class was not found."));
1155 * Add it to the class...
1158 cupsdAddPrinterToClass(pclass
, member
);
1162 set_printer_defaults(con
, pclass
);
1164 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1165 IPP_TAG_KEYWORD
)) != NULL
)
1166 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1169 * Update the printer class attributes and return...
1172 cupsdSetPrinterAttrs(pclass
);
1173 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
1175 if (need_restart_job
&& pclass
->job
)
1180 * Stop the current job and then restart it below...
1183 job
= (cupsd_job_t
*)pclass
->job
;
1185 cupsdStopJob(job
, 1);
1187 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1188 job
->state_value
= IPP_JOB_PENDING
;
1191 if (need_restart_job
)
1194 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
1198 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1199 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1202 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1203 pclass
->name
, get_username(con
));
1207 cupsdAddPrinterHistory(pclass
);
1209 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1210 "New class \"%s\" added by \"%s\".", pclass
->name
,
1213 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1214 pclass
->name
, get_username(con
));
1217 con
->response
->request
.status
.status_code
= IPP_OK
;
1222 * 'add_file()' - Add a file to a job.
1225 static int /* O - 0 on success, -1 on error */
1226 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1227 cupsd_job_t
*job
, /* I - Job to add to */
1228 mime_type_t
*filetype
, /* I - Type of file */
1229 int compression
) /* I - Compression */
1231 mime_type_t
**filetypes
; /* New filetypes array... */
1232 int *compressions
; /* New compressions array... */
1235 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1236 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1237 "compression=%d)", con
, con
? con
->http
.fd
: -1, job
->id
,
1238 filetype
->super
, filetype
->type
, compression
);
1241 * Add the file to the job...
1244 if (job
->num_files
== 0)
1246 compressions
= (int *)malloc(sizeof(int));
1247 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1251 compressions
= (int *)realloc(job
->compressions
,
1252 (job
->num_files
+ 1) * sizeof(int));
1253 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1254 (job
->num_files
+ 1) *
1255 sizeof(mime_type_t
*));
1258 if (!compressions
|| !filetypes
)
1260 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
1263 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1264 _("Unable to allocate memory for file types!"));
1269 job
->compressions
= compressions
;
1270 job
->compressions
[job
->num_files
] = compression
;
1271 job
->filetypes
= filetypes
;
1272 job
->filetypes
[job
->num_files
] = filetype
;
1277 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1284 * 'add_job()' - Add a job to a print queue.
1287 static cupsd_job_t
* /* O - Job object */
1288 add_job(cupsd_client_t
*con
, /* I - Client connection */
1289 cupsd_printer_t
*printer
, /* I - Destination printer */
1290 mime_type_t
*filetype
) /* I - First print file type, if any */
1292 http_status_t status
; /* Policy status */
1293 ipp_attribute_t
*attr
, /* Current attribute */
1294 *auth_info
; /* auth-info attribute */
1295 const char *val
; /* Default option value */
1296 int priority
; /* Job priority */
1297 char *title
; /* Job name/title */
1298 cupsd_job_t
*job
; /* Current job */
1299 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1300 int kbytes
; /* Size of print file */
1301 int i
; /* Looping var */
1302 int lowerpagerange
; /* Page range bound */
1305 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1306 con
, con
->http
.fd
, printer
, printer
->name
,
1307 filetype
, filetype
? filetype
->super
: "none",
1308 filetype
? filetype
->type
: "none");
1311 * Check remote printing to non-shared printer...
1314 if (!printer
->shared
&&
1315 strcasecmp(con
->http
.hostname
, "localhost") &&
1316 strcasecmp(con
->http
.hostname
, ServerName
))
1318 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1319 _("The printer or class is not shared!"));
1327 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1329 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1331 send_http_error(con
, status
, printer
);
1334 else if (printer
->num_auth_info_required
> 0 &&
1335 strcmp(printer
->auth_info_required
[0], "none") &&
1336 !con
->username
[0] && !auth_info
)
1338 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1342 else if (auth_info
&& !con
->http
.tls
&&
1343 !httpAddrLocalhost(con
->http
.hostaddr
))
1346 * Require encryption of auth-info over non-local connections...
1349 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1352 #endif /* HAVE_SSL */
1355 * See if the printer is accepting jobs...
1358 if (!printer
->accepting
)
1360 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1361 _("Destination \"%s\" is not accepting jobs."),
1367 * Validate job template attributes; for now just document-format,
1368 * copies, number-up, and page-ranges...
1371 if (filetype
&& printer
->filetypes
&&
1372 !cupsArrayFind(printer
->filetypes
, filetype
))
1374 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1375 /* MIME media type string */
1378 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1381 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1382 _("Unsupported format \'%s\'!"), mimetype
);
1384 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1385 "document-format", NULL
, mimetype
);
1390 if ((attr
= ippFindAttribute(con
->request
, "copies",
1391 IPP_TAG_INTEGER
)) != NULL
)
1393 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1395 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1396 attr
->values
[0].integer
);
1397 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1398 "copies", attr
->values
[0].integer
);
1403 if ((attr
= ippFindAttribute(con
->request
, "job-sheets",
1404 IPP_TAG_ZERO
)) != NULL
)
1406 if (attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1407 attr
->value_tag
!= IPP_TAG_NAME
)
1409 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value type!"));
1413 if (attr
->num_values
> 2)
1415 send_ipp_status(con
, IPP_BAD_REQUEST
,
1416 _("Too many job-sheets values (%d > 2)!"),
1421 for (i
= 0; i
< attr
->num_values
; i
++)
1422 if (strcmp(attr
->values
[i
].string
.text
, "none") &&
1423 !cupsdFindBanner(attr
->values
[i
].string
.text
))
1425 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value \"%s\"!"),
1426 attr
->values
[i
].string
.text
);
1431 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1432 IPP_TAG_INTEGER
)) != NULL
)
1434 if (attr
->values
[0].integer
!= 1 &&
1435 attr
->values
[0].integer
!= 2 &&
1436 attr
->values
[0].integer
!= 4 &&
1437 attr
->values
[0].integer
!= 6 &&
1438 attr
->values
[0].integer
!= 9 &&
1439 attr
->values
[0].integer
!= 16)
1441 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1442 attr
->values
[0].integer
);
1443 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1444 "number-up", attr
->values
[0].integer
);
1449 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1450 IPP_TAG_RANGE
)) != NULL
)
1452 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1454 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1455 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1457 send_ipp_status(con
, IPP_BAD_REQUEST
,
1458 _("Bad page-ranges values %d-%d."),
1459 attr
->values
[i
].range
.lower
,
1460 attr
->values
[i
].range
.upper
);
1464 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1469 * Make sure we aren't over our limit...
1472 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1475 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1477 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1478 _("Too many active jobs."));
1482 if ((i
= check_quotas(con
, printer
)) < 0)
1484 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1489 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1494 * Create the job and set things up...
1497 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1498 IPP_TAG_INTEGER
)) != NULL
)
1499 priority
= attr
->values
[0].integer
;
1502 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1503 printer
->options
)) != NULL
)
1504 priority
= atoi(val
);
1508 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1512 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1513 IPP_TAG_NAME
)) != NULL
)
1514 title
= attr
->values
[0].string
.text
;
1516 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1517 title
= "Untitled");
1519 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1521 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1522 _("Unable to add job for destination \"%s\"!"),
1527 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
1528 CUPS_PRINTER_REMOTE
);
1529 job
->attrs
= con
->request
;
1531 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1533 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1535 add_job_uuid(con
, job
);
1536 apply_printer_defaults(printer
, job
);
1538 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1540 if (con
->username
[0])
1542 cupsdSetString(&job
->username
, con
->username
);
1545 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1549 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1550 "add_job: requesting-user-name=\"%s\"",
1551 attr
->values
[0].string
.text
);
1553 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1556 cupsdSetString(&job
->username
, "anonymous");
1559 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1560 "job-originating-user-name", NULL
, job
->username
);
1563 attr
->group_tag
= IPP_TAG_JOB
;
1564 _cupsStrFree(attr
->name
);
1565 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1568 if (con
->username
[0] || auth_info
)
1570 save_auth_info(con
, job
, auth_info
);
1573 * Remove the auth-info attribute from the attribute data...
1577 ippDeleteAttribute(job
->attrs
, auth_info
);
1580 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1581 IPP_TAG_ZERO
)) != NULL
)
1584 * Request contains a job-originating-host-name attribute; validate it...
1587 if (attr
->value_tag
!= IPP_TAG_NAME
||
1588 attr
->num_values
!= 1 ||
1589 strcmp(con
->http
.hostname
, "localhost"))
1592 * Can't override the value if we aren't connected via localhost.
1593 * Also, we can only have 1 value and it must be a name value.
1596 switch (attr
->value_tag
)
1598 case IPP_TAG_STRING
:
1599 case IPP_TAG_TEXTLANG
:
1600 case IPP_TAG_NAMELANG
:
1603 case IPP_TAG_KEYWORD
:
1605 case IPP_TAG_URISCHEME
:
1606 case IPP_TAG_CHARSET
:
1607 case IPP_TAG_LANGUAGE
:
1608 case IPP_TAG_MIMETYPE
:
1610 * Free old strings...
1613 for (i
= 0; i
< attr
->num_values
; i
++)
1615 _cupsStrFree(attr
->values
[i
].string
.text
);
1616 attr
->values
[i
].string
.text
= NULL
;
1617 if (attr
->values
[i
].string
.charset
)
1619 _cupsStrFree(attr
->values
[i
].string
.charset
);
1620 attr
->values
[i
].string
.charset
= NULL
;
1629 * Use the default connection hostname instead...
1632 attr
->value_tag
= IPP_TAG_NAME
;
1633 attr
->num_values
= 1;
1634 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1637 attr
->group_tag
= IPP_TAG_JOB
;
1642 * No job-originating-host-name attribute, so use the hostname from
1646 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1647 "job-originating-host-name", NULL
, con
->http
.hostname
);
1650 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1652 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1653 "time-at-processing", 0);
1654 attr
->value_tag
= IPP_TAG_NOVALUE
;
1655 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1656 "time-at-completed", 0);
1657 attr
->value_tag
= IPP_TAG_NOVALUE
;
1660 * Add remaining job attributes...
1663 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1664 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1665 "job-state", IPP_JOB_STOPPED
);
1666 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1667 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1668 "job-media-sheets-completed", 0);
1669 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1671 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1674 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1675 IPP_TAG_INTEGER
)) != NULL
)
1676 attr
->values
[0].integer
= 0;
1678 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", 0);
1680 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1681 IPP_TAG_KEYWORD
)) == NULL
)
1682 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1685 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1686 printer
->options
)) == NULL
)
1689 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1690 "job-hold-until", NULL
, val
);
1692 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1693 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1696 * Hold job until specified time...
1699 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1701 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1702 job
->state_value
= IPP_JOB_HELD
;
1704 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1706 job
->hold_until
= time(NULL
) + 60;
1707 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1708 job
->state_value
= IPP_JOB_HELD
;
1712 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1713 job
->state_value
= IPP_JOB_PENDING
;
1716 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1720 * Add job sheets options...
1723 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1724 IPP_TAG_ZERO
)) == NULL
)
1726 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1727 "Adding default job-sheets values \"%s,%s\"...",
1728 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1730 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1732 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1733 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1736 job
->job_sheets
= attr
;
1739 * Enforce classification level if set...
1744 cupsdLogMessage(CUPSD_LOG_INFO
,
1745 "Classification=\"%s\", ClassifyOverride=%d",
1746 Classification
? Classification
: "(null)",
1749 if (ClassifyOverride
)
1751 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1752 (attr
->num_values
== 1 ||
1753 !strcmp(attr
->values
[1].string
.text
, "none")))
1756 * Force the leading banner to have the classification on it...
1759 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1761 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1762 "job-sheets=\"%s,none\", "
1763 "job-originating-user-name=\"%s\"",
1764 Classification
, job
->username
);
1766 else if (attr
->num_values
== 2 &&
1767 strcmp(attr
->values
[0].string
.text
,
1768 attr
->values
[1].string
.text
) &&
1769 strcmp(attr
->values
[0].string
.text
, "none") &&
1770 strcmp(attr
->values
[1].string
.text
, "none"))
1773 * Can't put two different security markings on the same document!
1776 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1778 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1779 "job-sheets=\"%s,%s\", "
1780 "job-originating-user-name=\"%s\"",
1781 attr
->values
[0].string
.text
,
1782 attr
->values
[1].string
.text
, job
->username
);
1784 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1785 strcmp(attr
->values
[0].string
.text
, "none") &&
1786 (attr
->num_values
== 1 ||
1787 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1788 strcmp(attr
->values
[1].string
.text
, "none"))))
1790 if (attr
->num_values
== 1)
1791 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1792 "CLASSIFICATION OVERRIDDEN "
1793 "job-sheets=\"%s\", "
1794 "job-originating-user-name=\"%s\"",
1795 attr
->values
[0].string
.text
, job
->username
);
1797 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1798 "CLASSIFICATION OVERRIDDEN "
1799 "job-sheets=\"%s,%s\",fffff "
1800 "job-originating-user-name=\"%s\"",
1801 attr
->values
[0].string
.text
,
1802 attr
->values
[1].string
.text
, job
->username
);
1805 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1806 (attr
->num_values
== 1 ||
1807 strcmp(attr
->values
[1].string
.text
, Classification
)))
1810 * Force the banner to have the classification on it...
1813 if (attr
->num_values
> 1 &&
1814 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1816 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1817 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1821 if (attr
->num_values
== 1 ||
1822 strcmp(attr
->values
[0].string
.text
, "none"))
1823 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1825 if (attr
->num_values
> 1 &&
1826 strcmp(attr
->values
[1].string
.text
, "none"))
1827 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1830 if (attr
->num_values
> 1)
1831 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1832 "CLASSIFICATION FORCED "
1833 "job-sheets=\"%s,%s\", "
1834 "job-originating-user-name=\"%s\"",
1835 attr
->values
[0].string
.text
,
1836 attr
->values
[1].string
.text
, job
->username
);
1838 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1839 "CLASSIFICATION FORCED "
1840 "job-sheets=\"%s\", "
1841 "job-originating-user-name=\"%s\"",
1842 Classification
, job
->username
);
1847 * See if we need to add the starting sheet...
1850 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1852 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding start banner page \"%s\".",
1853 attr
->values
[0].string
.text
);
1855 if ((kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
)) < 0)
1857 cupsdDeleteJob(job
);
1861 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1864 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1865 IPP_TAG_ZERO
)) != NULL
)
1869 * Fill in the response info...
1872 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1873 LocalPort
, job
->id
);
1875 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1878 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1880 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1882 add_job_state_reasons(con
, job
);
1884 con
->response
->request
.status
.status_code
= IPP_OK
;
1887 * Add any job subscriptions...
1890 add_job_subscriptions(con
, job
);
1893 * Set all but the first two attributes to the job attributes group...
1896 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1897 attr
->group_tag
= IPP_TAG_JOB
;
1900 * Fire the "job created" event...
1903 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1906 * Return the new job...
1914 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1915 * upon the job and printer state...
1919 add_job_state_reasons(
1920 cupsd_client_t
*con
, /* I - Client connection */
1921 cupsd_job_t
*job
) /* I - Job info */
1923 cupsd_printer_t
*dest
; /* Destination printer */
1926 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1927 con
, con
->http
.fd
, job
? job
->id
: 0);
1929 switch (job
? job
->state_value
: IPP_JOB_CANCELED
)
1931 case IPP_JOB_PENDING
:
1932 dest
= cupsdFindDest(job
->dest
);
1934 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1935 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1936 "job-state-reasons", NULL
, "printer-stopped");
1938 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1939 "job-state-reasons", NULL
, "none");
1943 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1944 IPP_TAG_KEYWORD
) != NULL
||
1945 ippFindAttribute(job
->attrs
, "job-hold-until",
1946 IPP_TAG_NAME
) != NULL
)
1947 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1948 "job-state-reasons", NULL
, "job-hold-until-specified");
1950 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1951 "job-state-reasons", NULL
, "job-incoming");
1954 case IPP_JOB_PROCESSING
:
1955 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1956 "job-state-reasons", NULL
, "job-printing");
1959 case IPP_JOB_STOPPED
:
1960 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1961 "job-state-reasons", NULL
, "job-stopped");
1964 case IPP_JOB_CANCELED
:
1965 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1966 "job-state-reasons", NULL
, "job-canceled-by-user");
1969 case IPP_JOB_ABORTED
:
1970 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1971 "job-state-reasons", NULL
, "aborted-by-system");
1974 case IPP_JOB_COMPLETED
:
1975 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1976 "job-state-reasons", NULL
, "job-completed-successfully");
1983 * 'add_job_subscriptions()' - Add any subscriptions for a job.
1987 add_job_subscriptions(
1988 cupsd_client_t
*con
, /* I - Client connection */
1989 cupsd_job_t
*job
) /* I - Newly created job */
1991 int i
; /* Looping var */
1992 ipp_attribute_t
*prev
, /* Previous attribute */
1993 *next
, /* Next attribute */
1994 *attr
; /* Current attribute */
1995 cupsd_subscription_t
*sub
; /* Subscription object */
1996 const char *recipient
, /* notify-recipient-uri */
1997 *pullmethod
; /* notify-pull-method */
1998 ipp_attribute_t
*user_data
; /* notify-user-data */
1999 int interval
; /* notify-time-interval */
2000 unsigned mask
; /* notify-events */
2004 * Find the first subscription group attribute; return if we have
2008 for (attr
= job
->attrs
->attrs
; attr
; attr
= attr
->next
)
2009 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
2016 * Process the subscription attributes in the request...
2025 mask
= CUPSD_EVENT_NONE
;
2027 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
2029 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
2030 attr
->value_tag
== IPP_TAG_URI
)
2033 * Validate the recipient scheme against the ServerBin/notifier
2037 char notifier
[1024], /* Notifier filename */
2038 scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2039 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2040 host
[HTTP_MAX_URI
], /* Host portion of URI */
2041 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2042 int port
; /* Port portion of URI */
2045 recipient
= attr
->values
[0].string
.text
;
2047 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
2048 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
2049 host
, sizeof(host
), &port
,
2050 resource
, sizeof(resource
)) < HTTP_URI_OK
)
2052 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2053 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
2054 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2055 "notify-status-code", IPP_URI_SCHEME
);
2059 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
2061 if (access(notifier
, X_OK
))
2063 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2064 _("notify-recipient-uri URI \"%s\" uses unknown "
2065 "scheme!"), recipient
);
2066 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2067 "notify-status-code", IPP_URI_SCHEME
);
2071 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
2073 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2074 _("notify-recipient-uri URI \"%s\" is already used!"),
2076 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2077 "notify-status-code", IPP_ATTRIBUTES
);
2081 else if (!strcmp(attr
->name
, "notify-pull-method") &&
2082 attr
->value_tag
== IPP_TAG_KEYWORD
)
2084 pullmethod
= attr
->values
[0].string
.text
;
2086 if (strcmp(pullmethod
, "ippget"))
2088 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2089 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
2090 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2091 "notify-status-code", IPP_ATTRIBUTES
);
2095 else if (!strcmp(attr
->name
, "notify-charset") &&
2096 attr
->value_tag
== IPP_TAG_CHARSET
&&
2097 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
2098 strcmp(attr
->values
[0].string
.text
, "utf-8"))
2100 send_ipp_status(con
, IPP_CHARSET
,
2101 _("Character set \"%s\" not supported!"),
2102 attr
->values
[0].string
.text
);
2105 else if (!strcmp(attr
->name
, "notify-natural-language") &&
2106 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
2107 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
2109 send_ipp_status(con
, IPP_CHARSET
,
2110 _("Language \"%s\" not supported!"),
2111 attr
->values
[0].string
.text
);
2114 else if (!strcmp(attr
->name
, "notify-user-data") &&
2115 attr
->value_tag
== IPP_TAG_STRING
)
2117 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
2119 send_ipp_status(con
, IPP_REQUEST_VALUE
,
2120 _("The notify-user-data value is too large "
2121 "(%d > 63 octets)!"),
2122 attr
->values
[0].unknown
.length
);
2128 else if (!strcmp(attr
->name
, "notify-events") &&
2129 attr
->value_tag
== IPP_TAG_KEYWORD
)
2131 for (i
= 0; i
< attr
->num_values
; i
++)
2132 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
2134 else if (!strcmp(attr
->name
, "notify-lease-duration"))
2136 send_ipp_status(con
, IPP_BAD_REQUEST
,
2137 _("The notify-lease-duration attribute cannot be "
2138 "used with job subscriptions."));
2141 else if (!strcmp(attr
->name
, "notify-time-interval") &&
2142 attr
->value_tag
== IPP_TAG_INTEGER
)
2143 interval
= attr
->values
[0].integer
;
2148 if (!recipient
&& !pullmethod
)
2151 if (mask
== CUPSD_EVENT_NONE
)
2152 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2154 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
2157 sub
->interval
= interval
;
2159 cupsdSetString(&sub
->owner
, job
->username
);
2163 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2164 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2165 sub
->user_data_len
);
2168 ippAddSeparator(con
->response
);
2169 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2170 "notify-subscription-id", sub
->id
);
2176 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
2179 * Remove all of the subscription attributes from the job request...
2181 * TODO: Optimize this since subscription groups have to come at the
2182 * end of the request...
2185 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2189 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2190 attr
->group_tag
== IPP_TAG_ZERO
)
2193 * Free and remove this attribute...
2201 job
->attrs
->attrs
= next
;
2207 job
->attrs
->last
= prev
;
2208 job
->attrs
->current
= prev
;
2213 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2215 * See RFC 4122 for the definition of UUIDs and the format.
2219 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
2220 cupsd_job_t
*job
) /* I - Job */
2222 char uuid
[1024]; /* job-uuid string */
2223 _cups_md5_state_t md5state
; /* MD5 state */
2224 unsigned char md5sum
[16]; /* MD5 digest/sum */
2228 * First see if the job already has a job-uuid attribute; if so, return...
2231 if (ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2235 * No job-uuid attribute, so build a version 3 UUID with the local job
2236 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
2237 * the ServerName, server name and port that the client connected to,
2238 * and local job ID...
2241 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
2242 con
->serverport
, job
->id
);
2244 _cupsMD5Init(&md5state
);
2245 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
2246 _cupsMD5Finish(&md5state
, md5sum
);
2249 * Format the UUID URI using the MD5 sum and job ID.
2252 snprintf(uuid
, sizeof(uuid
),
2253 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2254 "%02x%02x%02x%02x%02x%02x",
2255 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
2256 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
2257 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
2258 md5sum
[14], md5sum
[15]);
2260 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
2265 * 'add_printer()' - Add a printer to the system.
2269 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2270 ipp_attribute_t
*uri
) /* I - URI of printer */
2272 http_status_t status
; /* Policy status */
2273 int i
; /* Looping var */
2274 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2275 username
[HTTP_MAX_URI
], /* Username portion of URI */
2276 host
[HTTP_MAX_URI
], /* Host portion of URI */
2277 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2278 int port
; /* Port portion of URI */
2279 cupsd_printer_t
*printer
; /* Printer/class */
2280 ipp_attribute_t
*attr
; /* Printer attribute */
2281 cups_file_t
*fp
; /* Script/PPD file */
2282 char line
[1024]; /* Line from file... */
2283 char srcfile
[1024], /* Source Script/PPD file */
2284 dstfile
[1024]; /* Destination Script/PPD file */
2285 int modify
; /* Non-zero if we are modifying */
2286 char newname
[IPP_MAX_NAME
]; /* New printer name */
2287 int need_restart_job
; /* Need to restart job? */
2288 int set_device_uri
, /* Did we set the device URI? */
2289 set_port_monitor
; /* Did we set the port monitor? */
2292 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2293 con
->http
.fd
, uri
->values
[0].string
.text
);
2296 * Do we have a valid URI?
2299 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2300 sizeof(scheme
), username
, sizeof(username
), host
,
2301 sizeof(host
), &port
, resource
, sizeof(resource
));
2303 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2306 * No, return an error...
2309 send_ipp_status(con
, IPP_BAD_REQUEST
,
2310 _("The printer-uri must be of the form "
2311 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2316 * Do we have a valid printer name?
2319 if (!validate_name(resource
+ 10))
2322 * No, return an error...
2325 send_ipp_status(con
, IPP_BAD_REQUEST
,
2326 _("The printer-uri \"%s\" contains invalid characters."),
2327 uri
->values
[0].string
.text
);
2332 * See if the printer already exists; if not, create a new printer...
2335 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2338 * Printer doesn't exist; see if we have a class of the same name...
2341 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2342 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
2345 * Yes, return an error...
2348 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2349 _("A class named \"%s\" already exists!"),
2355 * No, check the default policy then add the printer...
2358 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2360 send_http_error(con
, status
, NULL
);
2364 printer
= cupsdAddPrinter(resource
+ 10);
2367 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2370 * Check the default policy, then rename the implicit printer to
2371 * "AnyPrinter" or delete it...
2374 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2376 send_http_error(con
, status
, NULL
);
2380 if (ImplicitAnyClasses
)
2382 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2383 cupsdRenamePrinter(printer
, newname
);
2386 cupsdDeletePrinter(printer
, 1);
2389 * Add the printer as a new local printer...
2392 printer
= cupsdAddPrinter(resource
+ 10);
2395 else if (printer
->type
& CUPS_PRINTER_DISCOVERED
)
2398 * Check the default policy, then rename the remote printer to
2399 * "Printer@server"...
2402 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2404 send_http_error(con
, status
, NULL
);
2408 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2410 cupsdRenamePrinter(printer
, newname
);
2413 * Add the printer as a new local printer...
2416 printer
= cupsdAddPrinter(resource
+ 10);
2419 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2422 send_http_error(con
, status
, printer
);
2429 * Look for attributes and copy them over as needed...
2432 need_restart_job
= 0;
2434 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2435 IPP_TAG_TEXT
)) != NULL
)
2436 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2438 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2439 IPP_TAG_TEXT
)) != NULL
)
2440 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2444 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2445 IPP_TAG_URI
)) != NULL
)
2448 * Do we have a valid device URI?
2451 http_uri_status_t uri_status
; /* URI separation status */
2452 char old_device_uri
[1024];
2453 /* Old device URI */
2456 need_restart_job
= 1;
2458 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2459 attr
->values
[0].string
.text
,
2460 scheme
, sizeof(scheme
),
2461 username
, sizeof(username
),
2462 host
, sizeof(host
), &port
,
2463 resource
, sizeof(resource
));
2465 if (uri_status
< HTTP_URI_OK
)
2467 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2468 attr
->values
[0].string
.text
);
2469 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2470 "add_printer: httpSeparateURI returned %d", uri_status
);
2474 if (!strcmp(scheme
, "file"))
2477 * See if the administrator has enabled file devices...
2480 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2483 * File devices are disabled and the URL is not file:/dev/null...
2486 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2487 _("File device URIs have been disabled! "
2488 "To enable, see the FileDevice directive in "
2489 "\"%s/cupsd.conf\"."),
2497 * See if the backend exists and is executable...
2500 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2501 if (access(srcfile
, X_OK
))
2504 * Could not find device in list!
2507 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2508 attr
->values
[0].string
.text
);
2513 if (printer
->sanitized_device_uri
)
2514 strlcpy(old_device_uri
, printer
->sanitized_device_uri
,
2515 sizeof(old_device_uri
));
2517 old_device_uri
[0] = '\0';
2519 cupsdSetDeviceURI(printer
, attr
->values
[0].string
.text
);
2521 cupsdLogMessage(CUPSD_LOG_INFO
,
2522 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2523 printer
->name
, printer
->sanitized_device_uri
,
2529 set_port_monitor
= 0;
2531 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2532 IPP_TAG_NAME
)) != NULL
)
2534 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2537 need_restart_job
= 1;
2539 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2543 for (i
= 0; i
< supported
->num_values
; i
++)
2544 if (!strcmp(supported
->values
[i
].string
.text
,
2545 attr
->values
[0].string
.text
))
2549 if (!supported
|| i
>= supported
->num_values
)
2551 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2552 attr
->values
[0].string
.text
);
2556 cupsdLogMessage(CUPSD_LOG_INFO
,
2557 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2558 printer
->name
, attr
->values
[0].string
.text
,
2559 printer
->port_monitor
? printer
->port_monitor
: "none");
2561 if (strcmp(attr
->values
[0].string
.text
, "none"))
2562 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2564 cupsdClearString(&printer
->port_monitor
);
2566 set_port_monitor
= 1;
2569 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2570 IPP_TAG_BOOLEAN
)) != NULL
)
2572 cupsdLogMessage(CUPSD_LOG_INFO
,
2573 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2574 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2576 printer
->accepting
= attr
->values
[0].boolean
;
2577 cupsdAddPrinterHistory(printer
);
2580 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2581 IPP_TAG_BOOLEAN
)) != NULL
)
2583 if (printer
->shared
&& !attr
->values
[0].boolean
)
2584 cupsdDeregisterPrinter(printer
, 1);
2586 cupsdLogMessage(CUPSD_LOG_INFO
,
2587 "Setting %s printer-is-shared to %d (was %d.)",
2588 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2590 printer
->shared
= attr
->values
[0].boolean
;
2593 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2594 IPP_TAG_ENUM
)) != NULL
)
2596 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2597 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2599 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2600 attr
->values
[0].integer
);
2604 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2605 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2607 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2608 cupsdStopPrinter(printer
, 0);
2611 need_restart_job
= 1;
2612 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2615 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2616 IPP_TAG_TEXT
)) != NULL
)
2618 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2619 sizeof(printer
->state_message
));
2620 cupsdAddPrinterHistory(printer
);
2623 set_printer_defaults(con
, printer
);
2625 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2626 IPP_TAG_KEYWORD
)) != NULL
)
2627 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2630 * See if we have all required attributes...
2633 if (!printer
->device_uri
)
2634 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2637 * See if we have an interface script or PPD file attached to the request...
2642 need_restart_job
= 1;
2644 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2646 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2649 * Yes; get the first line from it...
2653 cupsFileGets(fp
, line
, sizeof(line
));
2657 * Then see what kind of file it is...
2660 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2663 if (!strncmp(line
, "*PPD-Adobe", 10))
2666 * The new file is a PPD file, so remove any old interface script
2667 * that might be lying around...
2675 * This must be an interface script, so move the file over to the
2676 * interfaces directory and make it executable...
2679 if (copy_file(srcfile
, dstfile
))
2681 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2682 _("Unable to copy interface script - %s!"),
2687 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2688 "Copied interface script successfully!");
2689 chmod(dstfile
, 0755);
2692 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2695 if (!strncmp(line
, "*PPD-Adobe", 10))
2698 * The new file is a PPD file, so move the file over to the
2699 * ppd directory and make it readable by all...
2702 if (copy_file(srcfile
, dstfile
))
2704 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2705 _("Unable to copy PPD file - %s!"),
2710 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2711 "Copied PPD file successfully!");
2712 chmod(dstfile
, 0644);
2716 * (Re)register color profiles...
2719 apple_unregister_profiles(printer
);
2720 apple_register_profiles(printer
);
2721 #endif /* __APPLE__ */
2726 * This must be an interface script, so remove any old PPD file that
2727 * may be lying around...
2734 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2735 IPP_TAG_NAME
)) != NULL
)
2737 need_restart_job
= 1;
2739 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2742 * Raw driver, remove any existing PPD or interface script files.
2745 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2749 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2759 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2763 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2766 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2768 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2772 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2773 "Copied PPD file successfully!");
2774 chmod(dstfile
, 0644);
2778 * (Re)register color profiles...
2781 apple_unregister_profiles(printer
);
2782 apple_register_profiles(printer
);
2783 #endif /* __APPLE__ */
2788 * If we set the device URI but not the port monitor, check which port
2789 * monitor to use by default...
2792 if (set_device_uri
&& !set_port_monitor
)
2794 ppd_file_t
*ppd
; /* PPD file */
2795 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2798 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2799 sizeof(scheme
), username
, sizeof(username
), host
,
2800 sizeof(host
), &port
, resource
, sizeof(resource
));
2802 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2804 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2806 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2808 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2809 if (!strcmp(scheme
, ppdattr
->spec
))
2811 cupsdLogMessage(CUPSD_LOG_INFO
,
2812 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2813 printer
->name
, ppdattr
->value
,
2814 printer
->port_monitor
? printer
->port_monitor
2817 if (strcmp(ppdattr
->value
, "none"))
2818 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2820 cupsdClearString(&printer
->port_monitor
);
2830 * Update the printer attributes and return...
2833 cupsdSetPrinterAttrs(printer
);
2834 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2836 if (need_restart_job
&& printer
->job
)
2841 * Stop the current job and then restart it below...
2844 job
= (cupsd_job_t
*)printer
->job
;
2846 cupsdStopJob(job
, 1);
2848 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2849 job
->state_value
= IPP_JOB_PENDING
;
2852 if (need_restart_job
)
2855 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2859 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2860 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2863 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2864 printer
->name
, get_username(con
));
2868 cupsdAddPrinterHistory(printer
);
2870 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2871 "New printer \"%s\" added by \"%s\".", printer
->name
,
2874 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2875 printer
->name
, get_username(con
));
2878 con
->response
->request
.status
.status_code
= IPP_OK
;
2883 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2884 * based upon the printer state...
2888 add_printer_state_reasons(
2889 cupsd_client_t
*con
, /* I - Client connection */
2890 cupsd_printer_t
*p
) /* I - Printer info */
2892 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2893 "add_printer_state_reasons(%p[%d], %p[%s])",
2894 con
, con
->http
.fd
, p
, p
->name
);
2896 if (p
->num_reasons
== 0)
2897 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2898 "printer-state-reasons", NULL
,
2899 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2901 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2902 "printer-state-reasons", p
->num_reasons
, NULL
,
2903 (const char * const *)p
->reasons
);
2908 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2909 * the specified printer or class.
2913 add_queued_job_count(
2914 cupsd_client_t
*con
, /* I - Client connection */
2915 cupsd_printer_t
*p
) /* I - Printer or class */
2917 int count
; /* Number of jobs on destination */
2920 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2921 con
, con
->http
.fd
, p
, p
->name
);
2923 count
= cupsdGetPrinterJobCount(p
->name
);
2925 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2926 "queued-job-count", count
);
2932 * 'apple_init_profile()' - Initialize a color profile.
2937 ppd_file_t
*ppd
, /* I - PPD file */
2938 cups_array_t
*languages
, /* I - Languages in the PPD file */
2939 CMDeviceProfileInfo
*profile
, /* I - Profile record */
2940 unsigned id
, /* I - Profile ID */
2941 const char *name
, /* I - Profile name */
2942 const char *text
, /* I - Profile UI text */
2943 const char *iccfile
) /* I - ICC filename */
2945 char url
[1024]; /* URL for profile filename */
2946 CFMutableDictionaryRef dict
; /* Dictionary for name */
2947 char *language
; /* Current language */
2948 ppd_attr_t
*attr
; /* Profile attribute */
2949 CFStringRef cflang
, /* Language string */
2950 cftext
; /* Localized text */
2954 * Build the profile name dictionary...
2957 dict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
2958 &kCFTypeDictionaryKeyCallBacks
,
2959 &kCFTypeDictionaryValueCallBacks
);
2961 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, text
,
2962 kCFStringEncodingUTF8
);
2966 CFDictionarySetValue(dict
, CFSTR("en"), cftext
);
2973 * Find localized names for the color profiles...
2976 cupsArraySave(ppd
->sorted_attrs
);
2978 for (language
= (char *)cupsArrayFirst(languages
);
2980 language
= (char *)cupsArrayNext(languages
))
2984 if ((attr
= _ppdLocalizedAttr(ppd
, "cupsICCProfile", name
,
2986 attr
= _ppdLocalizedAttr(ppd
, "APTiogaProfile", name
, language
);
2989 attr
= _ppdLocalizedAttr(ppd
, "ColorModel", name
, language
);
2991 if (attr
&& attr
->text
[0])
2993 cflang
= CFStringCreateWithCString(kCFAllocatorDefault
, language
,
2994 kCFStringEncodingUTF8
);
2995 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, attr
->text
,
2996 kCFStringEncodingUTF8
);
2998 if (cflang
&& cftext
)
2999 CFDictionarySetValue(dict
, cflang
, cftext
);
3009 cupsArrayRestore(ppd
->sorted_attrs
);
3013 * Fill in the profile data...
3017 httpAssembleURI(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "file", NULL
, "", 0,
3020 profile
->dataVersion
= cmDeviceProfileInfoVersion1
;
3021 profile
->profileID
= id
;
3022 profile
->profileLoc
.locType
= iccfile
? cmPathBasedProfile
: cmNoProfileBase
;
3023 profile
->profileName
= dict
;
3026 strlcpy(profile
->profileLoc
.u
.pathLoc
.path
, iccfile
,
3027 sizeof(profile
->profileLoc
.u
.pathLoc
.path
));
3032 * 'apple_register_profiles()' - Register color profiles for a printer.
3036 apple_register_profiles(
3037 cupsd_printer_t
*p
) /* I - Printer */
3039 int i
; /* Looping var */
3040 char ppdfile
[1024], /* PPD filename */
3041 iccfile
[1024], /* ICC filename */
3042 selector
[PPD_MAX_NAME
];
3043 /* Profile selection string */
3044 ppd_file_t
*ppd
; /* PPD file */
3045 ppd_attr_t
*attr
, /* Profile attributes */
3046 *profileid_attr
,/* cupsProfileID attribute */
3047 *q1_attr
, /* ColorModel (or other) qualifier */
3048 *q2_attr
, /* MediaType (or other) qualifier */
3049 *q3_attr
; /* Resolution (or other) qualifier */
3050 char q_keyword
[PPD_MAX_NAME
];
3051 /* Qualifier keyword */
3052 const char *q1_choice
, /* ColorModel (or other) choice */
3053 *q2_choice
, /* MediaType (or other) choice */
3054 *q3_choice
; /* Resolution (or other) choice */
3055 const char *profile_key
; /* Profile keyword */
3056 ppd_option_t
*cm_option
; /* Color model option */
3057 ppd_choice_t
*cm_choice
; /* Color model choice */
3058 int num_profiles
; /* Number of profiles */
3059 CMError error
; /* Last error */
3060 unsigned device_id
, /* Printer device ID */
3061 profile_id
, /* Profile ID */
3062 default_profile_id
= 0;
3063 /* Default profile ID */
3064 CFMutableDictionaryRef device_name
; /* Printer device name dictionary */
3065 CFStringRef printer_name
; /* Printer name string */
3066 CMDeviceScope scope
= /* Scope of the registration */
3068 kCFPreferencesAnyUser
,
3069 kCFPreferencesCurrentHost
3071 CMDeviceProfileArrayPtr profiles
; /* Profiles */
3072 CMDeviceProfileInfo
*profile
; /* Current profile */
3073 cups_array_t
*languages
; /* Languages array */
3077 * Make sure ColorSync is available...
3080 if (CMRegisterColorDevice
== NULL
)
3084 * Try opening the PPD file for this printer...
3087 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3088 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
3092 * See if we have any profiles...
3095 if ((attr
= ppdFindAttr(ppd
, "APTiogaProfile", NULL
)) != NULL
)
3096 profile_key
= "APTiogaProfile";
3099 attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
3100 profile_key
= "cupsICCProfile";
3103 for (num_profiles
= 0; attr
; attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3104 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3106 if (attr
->value
[0] != '/')
3107 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3110 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3112 if (access(iccfile
, 0))
3120 * If we have profiles, add them...
3123 if (num_profiles
> 0)
3125 if (profile_key
[0] == 'A')
3128 * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile
3132 if ((attr
= ppdFindAttr(ppd
, "DefaultAPTiogaProfile", NULL
)) != NULL
&&
3134 default_profile_id
= atoi(attr
->value
);
3136 q1_choice
= q2_choice
= q3_choice
= NULL
;
3141 * For CUPS PPDs, figure out the default profile selector values...
3144 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier1", NULL
)) != NULL
&&
3145 attr
->value
&& attr
->value
[0])
3147 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3148 q1_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3150 else if ((q1_attr
= ppdFindAttr(ppd
, "DefaultColorModel", NULL
)) == NULL
)
3151 q1_attr
= ppdFindAttr(ppd
, "DefaultColorSpace", NULL
);
3153 if (q1_attr
&& q1_attr
->value
&& q1_attr
->value
[0])
3154 q1_choice
= q1_attr
->value
;
3158 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier2", NULL
)) != NULL
&&
3159 attr
->value
&& attr
->value
[0])
3161 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3162 q2_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3165 q2_attr
= ppdFindAttr(ppd
, "DefaultMediaType", NULL
);
3167 if (q2_attr
&& q2_attr
->value
&& q2_attr
->value
[0])
3168 q2_choice
= q2_attr
->value
;
3172 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier3", NULL
)) != NULL
&&
3173 attr
->value
&& attr
->value
[0])
3175 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3176 q3_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3179 q3_attr
= ppdFindAttr(ppd
, "DefaultResolution", NULL
);
3181 if (q3_attr
&& q3_attr
->value
&& q3_attr
->value
[0])
3182 q3_choice
= q3_attr
->value
;
3188 * Build the array of profiles...
3190 * Note: This calloc actually requests slightly more memory than needed.
3193 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3195 cupsdLogMessage(CUPSD_LOG_ERROR
,
3196 "Unable to allocate memory for %d profiles!",
3202 profiles
->profileCount
= num_profiles
;
3203 languages
= _ppdGetLanguages(ppd
);
3205 for (profile
= profiles
->profiles
,
3206 attr
= ppdFindAttr(ppd
, profile_key
, NULL
);
3208 attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3209 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3212 * Add this profile...
3215 if (attr
->value
[0] != '/')
3216 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3219 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3221 if (access(iccfile
, 0))
3224 if (profile_key
[0] == 'c')
3226 cupsArraySave(ppd
->sorted_attrs
);
3228 if ((profileid_attr
= ppdFindAttr(ppd
, "cupsProfileID",
3229 attr
->spec
)) != NULL
&&
3230 profileid_attr
->value
&& isdigit(profileid_attr
->value
[0] & 255))
3231 profile_id
= (unsigned)strtoul(profileid_attr
->value
, NULL
, 10);
3233 profile_id
= _ppdHashName(attr
->spec
);
3235 cupsArrayRestore(ppd
->sorted_attrs
);
3238 profile_id
= atoi(attr
->spec
);
3240 apple_init_profile(ppd
, languages
, profile
, profile_id
, attr
->spec
,
3241 attr
->text
[0] ? attr
->text
: attr
->spec
, iccfile
);
3246 * See if this is the default profile...
3249 if (!default_profile_id
)
3255 snprintf(selector
, sizeof(selector
), "%s.%s.%s",
3256 q1_choice
, q2_choice
, q3_choice
);
3257 if (!strcmp(selector
, attr
->spec
))
3258 default_profile_id
= profile_id
;
3261 if (!default_profile_id
)
3263 snprintf(selector
, sizeof(selector
), "%s.%s.", q1_choice
,
3265 if (!strcmp(selector
, attr
->spec
))
3266 default_profile_id
= profile_id
;
3270 if (!default_profile_id
&& q3_choice
)
3272 snprintf(selector
, sizeof(selector
), "%s..%s", q1_choice
,
3274 if (!strcmp(selector
, attr
->spec
))
3275 default_profile_id
= profile_id
;
3278 if (!default_profile_id
)
3280 snprintf(selector
, sizeof(selector
), "%s..", q1_choice
);
3281 if (!strcmp(selector
, attr
->spec
))
3282 default_profile_id
= profile_id
;
3287 _ppdFreeLanguages(languages
);
3289 else if ((cm_option
= ppdFindOption(ppd
, "ColorModel")) != NULL
)
3292 * Extract profiles from ColorModel option...
3295 const char *profile_name
; /* Name of generic profile */
3298 num_profiles
= cm_option
->num_choices
;
3300 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3302 cupsdLogMessage(CUPSD_LOG_ERROR
,
3303 "Unable to allocate memory for %d profiles!",
3309 profiles
->profileCount
= num_profiles
;
3311 for (profile
= profiles
->profiles
, i
= cm_option
->num_choices
,
3312 cm_choice
= cm_option
->choices
;
3314 i
--, cm_choice
++, profile
++)
3316 if (!strcmp(cm_choice
->choice
, "Gray") ||
3317 !strcmp(cm_choice
->choice
, "Black"))
3318 profile_name
= "Gray";
3319 else if (!strcmp(cm_choice
->choice
, "RGB") ||
3320 !strcmp(cm_choice
->choice
, "CMY"))
3321 profile_name
= "RGB";
3322 else if (!strcmp(cm_choice
->choice
, "CMYK") ||
3323 !strcmp(cm_choice
->choice
, "KCMY"))
3324 profile_name
= "CMYK";
3326 profile_name
= "DeviceN";
3328 snprintf(selector
, sizeof(selector
), "%s..", profile_name
);
3329 profile_id
= _ppdHashName(selector
);
3331 apple_init_profile(ppd
, NULL
, profile
, profile_id
, cm_choice
->choice
,
3332 cm_choice
->text
, NULL
);
3334 if (cm_choice
->marked
)
3335 default_profile_id
= profile_id
;
3341 * Use the default colorspace...
3344 num_profiles
= 1 + ppd
->colorspace
!= PPD_CS_GRAY
;
3346 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3348 cupsdLogMessage(CUPSD_LOG_ERROR
,
3349 "Unable to allocate memory for %d profiles!",
3355 profiles
->profileCount
= num_profiles
;
3357 apple_init_profile(ppd
, NULL
, profiles
->profiles
, _ppdHashName("Gray.."),
3358 "Gray", "Gray", NULL
);
3360 switch (ppd
->colorspace
)
3364 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3365 _ppdHashName("RGB.."), "RGB", "RGB", NULL
);
3369 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3370 _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL
);
3374 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3375 _ppdHashName("DeviceN.."), "DeviceN", "DeviceN",
3384 if (num_profiles
> 0)
3387 * Make sure we have a default profile ID...
3390 if (!default_profile_id
)
3391 default_profile_id
= profiles
->profiles
[num_profiles
- 1].profileID
;
3394 * Get the device ID hash and pathelogical name dictionary.
3397 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\"",
3400 device_id
= _ppdHashName(p
->name
);
3401 device_name
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3402 &kCFTypeDictionaryKeyCallBacks
,
3403 &kCFTypeDictionaryValueCallBacks
);
3404 printer_name
= CFStringCreateWithCString(kCFAllocatorDefault
,
3405 p
->name
, kCFStringEncodingUTF8
);
3407 if (device_name
&& printer_name
)
3409 CFDictionarySetValue(device_name
, CFSTR("en"), printer_name
);
3412 * Register the device with ColorSync...
3415 error
= CMRegisterColorDevice(cmPrinterDeviceClass
, device_id
,
3416 device_name
, &scope
);
3419 * Register the profiles...
3423 error
= CMSetDeviceFactoryProfiles(cmPrinterDeviceClass
, device_id
,
3424 default_profile_id
, profiles
);
3434 cupsdLogMessage(CUPSD_LOG_ERROR
,
3435 "Unable to register ICC color profiles for \"%s\" - %d",
3436 p
->name
, (int)error
);
3438 for (profile
= profiles
->profiles
;
3440 profile
++, num_profiles
--)
3441 CFRelease(profile
->profileName
);
3446 CFRelease(printer_name
);
3449 CFRelease(device_name
);
3457 * 'apple_unregister_profiles()' - Remove color profiles for the specified
3462 apple_unregister_profiles(
3463 cupsd_printer_t
*p
) /* I - Printer */
3466 * Make sure ColorSync is available...
3469 if (CMUnregisterColorDevice
!= NULL
)
3470 CMUnregisterColorDevice(cmPrinterDeviceClass
, _ppdHashName(p
->name
));
3472 #endif /* __APPLE__ */
3475 * 'apply_printer_defaults()' - Apply printer default options to a job.
3479 apply_printer_defaults(
3480 cupsd_printer_t
*printer
, /* I - Printer */
3481 cupsd_job_t
*job
) /* I - Job */
3483 int i
, /* Looping var */
3484 num_options
; /* Number of default options */
3485 cups_option_t
*options
, /* Default options */
3486 *option
; /* Current option */
3490 * Collect all of the default options and add the missing ones to the
3494 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
3495 option
= printer
->options
;
3498 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
3500 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
3505 * Encode these options as attributes in the job object...
3508 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
3509 cupsFreeOptions(num_options
, options
);
3514 * 'authenticate_job()' - Set job authentication info.
3518 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
3519 ipp_attribute_t
*uri
) /* I - Job URI */
3521 ipp_attribute_t
*attr
, /* job-id attribute */
3522 *auth_info
; /* auth-info attribute */
3523 int jobid
; /* Job ID */
3524 cupsd_job_t
*job
; /* Current job */
3525 char method
[HTTP_MAX_URI
],
3526 /* Method portion of URI */
3527 username
[HTTP_MAX_URI
],
3528 /* Username portion of URI */
3530 /* Host portion of URI */
3531 resource
[HTTP_MAX_URI
];
3532 /* Resource portion of URI */
3533 int port
; /* Port portion of URI */
3536 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
3537 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
3540 * Start with "everything is OK" status...
3543 con
->response
->request
.status
.status_code
= IPP_OK
;
3546 * See if we have a job URI or a printer URI...
3549 if (!strcmp(uri
->name
, "printer-uri"))
3552 * Got a printer URI; see if we also have a job-id attribute...
3555 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3556 IPP_TAG_INTEGER
)) == NULL
)
3558 send_ipp_status(con
, IPP_BAD_REQUEST
,
3559 _("Got a printer-uri attribute but no job-id!"));
3563 jobid
= attr
->values
[0].integer
;
3568 * Got a job URI; parse it to get the job ID...
3571 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
3572 sizeof(method
), username
, sizeof(username
), host
,
3573 sizeof(host
), &port
, resource
, sizeof(resource
));
3575 if (strncmp(resource
, "/jobs/", 6))
3581 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
3582 uri
->values
[0].string
.text
);
3586 jobid
= atoi(resource
+ 6);
3590 * See if the job exists...
3593 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3596 * Nope - return a "not found" error...
3599 send_ipp_status(con
, IPP_NOT_FOUND
,
3600 _("Job #%d does not exist!"), jobid
);
3605 * See if the job has been completed...
3608 if (job
->state_value
!= IPP_JOB_HELD
)
3611 * Return a "not-possible" error...
3614 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3615 _("Job #%d is not held for authentication!"),
3621 * See if we have already authenticated...
3624 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3626 if (!con
->username
[0] && !auth_info
)
3628 cupsd_printer_t
*printer
; /* Job destination */
3632 * No auth data. If we need to authenticate via Kerberos, send a
3633 * HTTP auth challenge, otherwise just return an IPP error...
3636 printer
= cupsdFindDest(job
->dest
);
3638 if (printer
&& printer
->num_auth_info_required
> 0 &&
3639 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3640 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3642 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3643 _("No authentication information provided!"));
3648 * See if the job is owned by the requesting user...
3651 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3653 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3658 * Save the authentication information for this job...
3661 save_auth_info(con
, job
, auth_info
);
3664 * Reset the job-hold-until value to "no-hold"...
3667 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3668 IPP_TAG_KEYWORD
)) == NULL
)
3669 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3673 attr
->value_tag
= IPP_TAG_KEYWORD
;
3674 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
3678 * Release the job and return...
3681 cupsdReleaseJob(job
);
3683 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3688 * 'cancel_all_jobs()' - Cancel all print jobs.
3692 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3693 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3695 http_status_t status
; /* Policy status */
3696 cups_ptype_t dtype
; /* Destination type */
3697 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3698 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3699 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3700 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3701 int port
; /* Port portion of URI */
3702 ipp_attribute_t
*attr
; /* Attribute in request */
3703 const char *username
; /* Username */
3704 int purge
; /* Purge? */
3705 cupsd_printer_t
*printer
; /* Printer */
3708 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3709 con
->http
.fd
, uri
->values
[0].string
.text
);
3712 * See if we have a printer URI...
3715 if (strcmp(uri
->name
, "printer-uri"))
3717 send_ipp_status(con
, IPP_BAD_REQUEST
,
3718 _("The printer-uri attribute is required!"));
3723 * Get the username (if any) for the jobs we want to cancel (only if
3724 * "my-jobs" is specified...
3727 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3728 IPP_TAG_BOOLEAN
)) != NULL
&&
3729 attr
->values
[0].boolean
)
3731 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3732 IPP_TAG_NAME
)) != NULL
)
3733 username
= attr
->values
[0].string
.text
;
3736 send_ipp_status(con
, IPP_BAD_REQUEST
,
3737 _("Missing requesting-user-name attribute!"));
3745 * Look for the "purge-jobs" attribute...
3748 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3749 IPP_TAG_BOOLEAN
)) != NULL
)
3750 purge
= attr
->values
[0].boolean
;
3755 * And if the destination is valid...
3758 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3764 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3765 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3766 hostname
, sizeof(hostname
), &port
,
3767 resource
, sizeof(resource
));
3769 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3770 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3772 send_ipp_status(con
, IPP_NOT_FOUND
,
3773 _("The printer or class was not found."));
3781 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3783 send_http_error(con
, status
, NULL
);
3788 * Cancel all jobs on all printers...
3791 cupsdCancelJobs(NULL
, username
, purge
);
3793 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3794 purge
? "purged" : "canceled", get_username(con
));
3802 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3805 send_http_error(con
, status
, printer
);
3810 * Cancel all of the jobs on the named printer...
3813 cupsdCancelJobs(printer
->name
, username
, purge
);
3815 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3816 printer
->name
, purge
? "purged" : "canceled",
3820 con
->response
->request
.status
.status_code
= IPP_OK
;
3825 * 'cancel_job()' - Cancel a print job.
3829 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3830 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3832 ipp_attribute_t
*attr
; /* Current attribute */
3833 int jobid
; /* Job ID */
3834 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3835 username
[HTTP_MAX_URI
], /* Username portion of URI */
3836 host
[HTTP_MAX_URI
], /* Host portion of URI */
3837 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3838 int port
; /* Port portion of URI */
3839 cupsd_job_t
*job
; /* Job information */
3840 cups_ptype_t dtype
; /* Destination type (printer/class) */
3841 cupsd_printer_t
*printer
; /* Printer data */
3842 int purge
; /* Purge the job? */
3845 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3846 con
->http
.fd
, uri
->values
[0].string
.text
);
3849 * See if we have a job URI or a printer URI...
3852 if (!strcmp(uri
->name
, "printer-uri"))
3855 * Got a printer URI; see if we also have a job-id attribute...
3858 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3859 IPP_TAG_INTEGER
)) == NULL
)
3861 send_ipp_status(con
, IPP_BAD_REQUEST
,
3862 _("Got a printer-uri attribute but no job-id!"));
3866 if ((jobid
= attr
->values
[0].integer
) == 0)
3869 * Find the current job on the specified printer...
3872 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3878 send_ipp_status(con
, IPP_NOT_FOUND
,
3879 _("The printer or class was not found."));
3884 * See if the printer is currently printing a job...
3888 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3892 * No, see if there are any pending jobs...
3895 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3897 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3898 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3899 !strcasecmp(job
->dest
, printer
->name
))
3906 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3908 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3909 if (job
->state_value
== IPP_JOB_STOPPED
&&
3910 !strcasecmp(job
->dest
, printer
->name
))
3917 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3928 * Got a job URI; parse it to get the job ID...
3931 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3932 sizeof(scheme
), username
, sizeof(username
), host
,
3933 sizeof(host
), &port
, resource
, sizeof(resource
));
3935 if (strncmp(resource
, "/jobs/", 6))
3941 send_ipp_status(con
, IPP_BAD_REQUEST
,
3942 _("Bad job-uri attribute \"%s\"!"),
3943 uri
->values
[0].string
.text
);
3947 jobid
= atoi(resource
+ 6);
3951 * Look for the "purge-job" attribute...
3954 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3955 IPP_TAG_BOOLEAN
)) != NULL
)
3956 purge
= attr
->values
[0].boolean
;
3961 * See if the job exists...
3964 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3967 * Nope - return a "not found" error...
3970 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3975 * See if the job is owned by the requesting user...
3978 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3980 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3985 * See if the job is already completed, canceled, or aborted; if so,
3986 * we can't cancel...
3989 if (job
->state_value
>= IPP_JOB_CANCELED
&& !purge
)
3991 switch (job
->state_value
)
3993 case IPP_JOB_CANCELED
:
3994 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3995 _("Job #%d is already canceled - can\'t cancel."),
3999 case IPP_JOB_ABORTED
:
4000 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4001 _("Job #%d is already aborted - can\'t cancel."),
4006 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4007 _("Job #%d is already completed - can\'t cancel."),
4016 * Cancel the job and return...
4019 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
4023 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
4026 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
4029 con
->response
->request
.status
.status_code
= IPP_OK
;
4034 * 'cancel_subscription()' - Cancel a subscription.
4038 cancel_subscription(
4039 cupsd_client_t
*con
, /* I - Client connection */
4040 int sub_id
) /* I - Subscription ID */
4042 http_status_t status
; /* Policy status */
4043 cupsd_subscription_t
*sub
; /* Subscription */
4046 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4047 "cancel_subscription(con=%p[%d], sub_id=%d)",
4048 con
, con
->http
.fd
, sub_id
);
4051 * Is the subscription ID valid?
4054 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
4057 * Bad subscription ID...
4060 send_ipp_status(con
, IPP_NOT_FOUND
,
4061 _("notify-subscription-id %d no good!"), sub_id
);
4069 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
4071 con
, sub
->owner
)) != HTTP_OK
)
4073 send_http_error(con
, status
, sub
->dest
);
4078 * Cancel the subscription...
4081 cupsdDeleteSubscription(sub
, 1);
4083 con
->response
->request
.status
.status_code
= IPP_OK
;
4088 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
4091 static int /* O - 1 if OK, 0 if not */
4092 check_rss_recipient(
4093 const char *recipient
) /* I - Recipient URI */
4095 cupsd_subscription_t
*sub
; /* Current subscription */
4098 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
4100 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
4104 * Compare the URIs up to the first ?...
4107 const char *r1
, *r2
;
4109 for (r1
= recipient
, r2
= sub
->recipient
;
4110 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
4122 * 'check_quotas()' - Check quotas for a printer and user.
4125 static int /* O - 1 if OK, 0 if not */
4126 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
4127 cupsd_printer_t
*p
) /* I - Printer or class */
4129 int i
; /* Looping var */
4130 char username
[33]; /* Username */
4131 cupsd_quota_t
*q
; /* Quota data */
4132 #ifdef HAVE_MBR_UID_TO_UUID
4134 * Use Apple membership APIs which require that all names represent
4135 * valid user account or group records accessible by the server.
4138 uuid_t usr_uuid
; /* UUID for job requesting user */
4139 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
4140 uuid_t grp_uuid
; /* UUID for ACL group name entry */
4141 int mbr_err
; /* Error from membership function */
4142 int is_member
; /* Is this user a member? */
4145 * Use standard POSIX APIs for checking users and groups...
4148 struct passwd
*pw
; /* User password data */
4149 #endif /* HAVE_MBR_UID_TO_UUID */
4152 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
4153 con
, con
->http
.fd
, p
, p
->name
);
4156 * Figure out who is printing...
4159 strlcpy(username
, get_username(con
), sizeof(username
));
4162 * Check global active job limits for printers and users...
4165 if (MaxJobsPerPrinter
)
4168 * Check if there are too many pending jobs on this printer...
4171 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
4173 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
4182 * Check if there are too many pending jobs for this user...
4185 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
4187 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
4194 * Check against users...
4197 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
4202 #ifdef HAVE_MBR_UID_TO_UUID
4204 * Get UUID for job requesting user...
4207 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
4213 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4214 "check_quotas: UUID lookup failed for user \"%s\"",
4216 cupsdLogMessage(CUPSD_LOG_INFO
,
4217 "Denying user \"%s\" access to printer \"%s\" "
4218 "(unknown user)...",
4224 * Get UID and GID of requesting user...
4227 pw
= getpwnam(username
);
4229 #endif /* HAVE_MBR_UID_TO_UUID */
4231 for (i
= 0; i
< p
->num_users
; i
++)
4232 if (p
->users
[i
][0] == '@')
4235 * Check group membership...
4238 #ifdef HAVE_MBR_UID_TO_UUID
4239 if (p
->users
[i
][1] == '#')
4241 if (uuid_parse((char *)p
->users
[i
] + 2, grp_uuid
))
4242 uuid_clear(grp_uuid
);
4244 else if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
4248 * Invalid ACL entries are ignored for matching; just record a
4249 * warning in the log...
4252 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4253 "check_quotas: UUID lookup failed for ACL entry "
4254 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4255 cupsdLogMessage(CUPSD_LOG_WARN
,
4256 "Access control entry \"%s\" not a valid group name; "
4257 "entry ignored", p
->users
[i
]);
4260 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
4264 * At this point, there should be no errors, but check anyways...
4267 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4268 "check_quotas: group \"%s\" membership check "
4269 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
4274 * Stop if we found a match...
4281 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
4283 #endif /* HAVE_MBR_UID_TO_UUID */
4285 #ifdef HAVE_MBR_UID_TO_UUID
4288 if (p
->users
[i
][0] == '#')
4290 if (uuid_parse((char *)p
->users
[i
] + 1, usr2_uuid
))
4291 uuid_clear(usr2_uuid
);
4293 else if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
4297 * Invalid ACL entries are ignored for matching; just record a
4298 * warning in the log...
4301 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4302 "check_quotas: UUID lookup failed for ACL entry "
4303 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4304 cupsdLogMessage(CUPSD_LOG_WARN
,
4305 "Access control entry \"%s\" not a valid user name; "
4306 "entry ignored", p
->users
[i
]);
4309 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
4312 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4313 "check_quotas: User \"%s\" identity check failed "
4314 "(err=%d)", p
->users
[i
], mbr_err
);
4322 else if (!strcasecmp(username
, p
->users
[i
]))
4324 #endif /* HAVE_MBR_UID_TO_UUID */
4326 if ((i
< p
->num_users
) == p
->deny_users
)
4328 cupsdLogMessage(CUPSD_LOG_INFO
,
4329 "Denying user \"%s\" access to printer \"%s\"...",
4340 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
4343 * TODO: Define these special page count values as constants!
4346 if (q
->page_count
== -4) /* special case: unlimited user */
4348 cupsdLogMessage(CUPSD_LOG_INFO
,
4349 "User \"%s\" request approved for printer %s (%s): "
4351 username
, p
->name
, p
->info
);
4352 q
->page_count
= 0; /* allow user to print */
4355 else if (q
->page_count
== -3) /* quota exceeded */
4357 cupsdLogMessage(CUPSD_LOG_INFO
,
4358 "User \"%s\" request denied for printer %s (%s): "
4359 "quota limit exceeded.",
4360 username
, p
->name
, p
->info
);
4361 q
->page_count
= 2; /* force quota exceeded failure */
4364 else if (q
->page_count
== -2) /* quota disabled for user */
4366 cupsdLogMessage(CUPSD_LOG_INFO
,
4367 "User \"%s\" request denied for printer %s (%s): "
4368 "printing disabled for user.",
4369 username
, p
->name
, p
->info
);
4370 q
->page_count
= 2; /* force quota exceeded failure */
4373 else if (q
->page_count
== -1) /* quota access error */
4375 cupsdLogMessage(CUPSD_LOG_INFO
,
4376 "User \"%s\" request denied for printer %s (%s): "
4377 "unable to determine quota limit.",
4378 username
, p
->name
, p
->info
);
4379 q
->page_count
= 2; /* force quota exceeded failure */
4382 else if (q
->page_count
< 0) /* user not found or other error */
4384 cupsdLogMessage(CUPSD_LOG_INFO
,
4385 "User \"%s\" request denied for printer %s (%s): "
4386 "user disabled / missing quota.",
4387 username
, p
->name
, p
->info
);
4388 q
->page_count
= 2; /* force quota exceeded failure */
4391 else /* page within user limits */
4393 q
->page_count
= 0; /* allow user to print */
4398 #endif /* __APPLE__ */
4399 if (p
->k_limit
|| p
->page_limit
)
4401 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
4403 cupsdLogMessage(CUPSD_LOG_ERROR
,
4404 "Unable to allocate quota data for user \"%s\"!",
4409 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
4410 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
4412 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
4419 * If we have gotten this far, we're done!
4427 * 'copy_attribute()' - Copy a single attribute.
4430 static ipp_attribute_t
* /* O - New attribute */
4432 ipp_t
*to
, /* O - Destination request/response */
4433 ipp_attribute_t
*attr
, /* I - Attribute to copy */
4434 int quickcopy
) /* I - Do a quick copy? */
4436 int i
; /* Looping var */
4437 ipp_attribute_t
*toattr
; /* Destination attribute */
4440 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4441 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
4442 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
4445 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
4448 toattr
= ippAddSeparator(to
);
4451 case IPP_TAG_INTEGER
:
4453 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4454 attr
->name
, attr
->num_values
, NULL
);
4456 for (i
= 0; i
< attr
->num_values
; i
++)
4457 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
4460 case IPP_TAG_BOOLEAN
:
4461 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
4462 attr
->num_values
, NULL
);
4464 for (i
= 0; i
< attr
->num_values
; i
++)
4465 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
4468 case IPP_TAG_STRING
:
4471 case IPP_TAG_KEYWORD
:
4473 case IPP_TAG_URISCHEME
:
4474 case IPP_TAG_CHARSET
:
4475 case IPP_TAG_LANGUAGE
:
4476 case IPP_TAG_MIMETYPE
:
4477 toattr
= ippAddStrings(to
, attr
->group_tag
,
4478 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4479 attr
->name
, attr
->num_values
, NULL
, NULL
);
4483 for (i
= 0; i
< attr
->num_values
; i
++)
4484 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4488 for (i
= 0; i
< attr
->num_values
; i
++)
4489 toattr
->values
[i
].string
.text
=
4490 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4495 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
4496 attr
->values
[0].date
);
4499 case IPP_TAG_RESOLUTION
:
4500 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
4501 attr
->num_values
, IPP_RES_PER_INCH
,
4504 for (i
= 0; i
< attr
->num_values
; i
++)
4506 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
4507 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
4508 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
4512 case IPP_TAG_RANGE
:
4513 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
4514 attr
->num_values
, NULL
, NULL
);
4516 for (i
= 0; i
< attr
->num_values
; i
++)
4518 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
4519 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
4523 case IPP_TAG_TEXTLANG
:
4524 case IPP_TAG_NAMELANG
:
4525 toattr
= ippAddStrings(to
, attr
->group_tag
,
4526 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4527 attr
->name
, attr
->num_values
, NULL
, NULL
);
4531 for (i
= 0; i
< attr
->num_values
; i
++)
4533 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
4534 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4539 for (i
= 0; i
< attr
->num_values
; i
++)
4542 toattr
->values
[i
].string
.charset
=
4543 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
4545 toattr
->values
[i
].string
.charset
=
4546 toattr
->values
[0].string
.charset
;
4548 toattr
->values
[i
].string
.text
=
4549 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4554 case IPP_TAG_BEGIN_COLLECTION
:
4555 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
4556 attr
->num_values
, NULL
);
4558 for (i
= 0; i
< attr
->num_values
; i
++)
4560 toattr
->values
[i
].collection
= ippNew();
4561 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
4562 NULL
, IPP_TAG_ZERO
, 0);
4567 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4568 attr
->name
, attr
->num_values
, NULL
);
4570 for (i
= 0; i
< attr
->num_values
; i
++)
4572 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
4574 if (toattr
->values
[i
].unknown
.length
> 0)
4576 if ((toattr
->values
[i
].unknown
.data
=
4577 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
4578 toattr
->values
[i
].unknown
.length
= 0;
4580 memcpy(toattr
->values
[i
].unknown
.data
,
4581 attr
->values
[i
].unknown
.data
,
4582 toattr
->values
[i
].unknown
.length
);
4585 break; /* anti-compiler-warning-code */
4593 * 'copy_attrs()' - Copy attributes from one request to another.
4597 copy_attrs(ipp_t
*to
, /* I - Destination request */
4598 ipp_t
*from
, /* I - Source request */
4599 cups_array_t
*ra
, /* I - Requested attributes */
4600 ipp_tag_t group
, /* I - Group to copy */
4601 int quickcopy
) /* I - Do a quick copy? */
4603 ipp_attribute_t
*fromattr
; /* Source attribute */
4606 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4607 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4608 to
, from
, ra
, group
, quickcopy
);
4613 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4616 * Filter attributes as needed...
4619 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4620 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4623 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4624 copy_attribute(to
, fromattr
, quickcopy
);
4630 * 'copy_banner()' - Copy a banner file to the requests directory for the
4634 static int /* O - Size of banner file in kbytes */
4635 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4636 cupsd_job_t
*job
, /* I - Job information */
4637 const char *name
) /* I - Name of banner */
4639 int i
; /* Looping var */
4640 int kbytes
; /* Size of banner file in kbytes */
4641 char filename
[1024]; /* Job filename */
4642 cupsd_banner_t
*banner
; /* Pointer to banner */
4643 cups_file_t
*in
; /* Input file */
4644 cups_file_t
*out
; /* Output file */
4645 int ch
; /* Character from file */
4646 char attrname
[255], /* Name of attribute */
4647 *s
; /* Pointer into name */
4648 ipp_attribute_t
*attr
; /* Attribute */
4651 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4652 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4653 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
4654 name
? name
: "(null)");
4657 * Find the banner; return if not found or "none"...
4660 if (!name
|| !strcmp(name
, "none") ||
4661 (banner
= cupsdFindBanner(name
)) == NULL
)
4665 * Open the banner and job files...
4668 if (add_file(con
, job
, banner
->filetype
, 0))
4671 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4673 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4675 cupsdLogMessage(CUPSD_LOG_ERROR
,
4676 "Unable to create banner job file %s - %s",
4677 filename
, strerror(errno
));
4682 fchmod(cupsFileNumber(out
), 0640);
4683 fchown(cupsFileNumber(out
), RunUser
, Group
);
4686 * Try the localized banner file under the subdirectory...
4689 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4691 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4694 * Convert ll-cc to ll_CC...
4698 attrname
[3] = toupper(attrname
[3] & 255);
4699 attrname
[4] = toupper(attrname
[4] & 255);
4702 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4705 if (access(filename
, 0) && strlen(attrname
) > 2)
4708 * Wasn't able to find "ll_CC" locale file; try the non-national
4709 * localization banner directory.
4714 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4718 if (access(filename
, 0))
4721 * Use the non-localized banner file.
4724 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4727 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4731 cupsdLogMessage(CUPSD_LOG_ERROR
,
4732 "Unable to open banner template file %s - %s",
4733 filename
, strerror(errno
));
4739 * Parse the file to the end...
4742 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4746 * Get an attribute name...
4749 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4750 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4752 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4762 * Ignore { followed by stuff that is not an attribute name...
4765 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4770 * See if it is defined...
4773 if (attrname
[0] == '?')
4778 if (!strcmp(s
, "printer-name"))
4780 cupsFilePuts(out
, job
->dest
);
4783 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4786 * See if we have a leading question mark...
4789 if (attrname
[0] != '?')
4792 * Nope, write to file as-is; probably a PostScript procedure...
4795 cupsFilePrintf(out
, "{%s}", attrname
);
4802 * Output value(s)...
4805 for (i
= 0; i
< attr
->num_values
; i
++)
4808 cupsFilePutChar(out
, ',');
4810 switch (attr
->value_tag
)
4812 case IPP_TAG_INTEGER
:
4814 if (!strncmp(s
, "time-at-", 8))
4815 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
4817 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4820 case IPP_TAG_BOOLEAN
:
4821 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4824 case IPP_TAG_NOVALUE
:
4825 cupsFilePuts(out
, "novalue");
4828 case IPP_TAG_RANGE
:
4829 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4830 attr
->values
[i
].range
.upper
);
4833 case IPP_TAG_RESOLUTION
:
4834 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4835 attr
->values
[i
].resolution
.yres
,
4836 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4841 case IPP_TAG_STRING
:
4844 case IPP_TAG_KEYWORD
:
4845 case IPP_TAG_CHARSET
:
4846 case IPP_TAG_LANGUAGE
:
4847 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4850 * Need to quote strings for PS banners...
4855 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4857 if (*p
== '(' || *p
== ')' || *p
== '\\')
4859 cupsFilePutChar(out
, '\\');
4860 cupsFilePutChar(out
, *p
);
4862 else if (*p
< 32 || *p
> 126)
4863 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4865 cupsFilePutChar(out
, *p
);
4869 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4873 break; /* anti-compiler-warning-code */
4877 else if (ch
== '\\') /* Quoted char */
4879 ch
= cupsFileGetChar(in
);
4881 if (ch
!= '{') /* Only do special handling for \{ */
4882 cupsFilePutChar(out
, '\\');
4884 cupsFilePutChar(out
, ch
);
4887 cupsFilePutChar(out
, ch
);
4891 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4893 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4894 IPP_TAG_INTEGER
)) != NULL
)
4895 attr
->values
[0].integer
+= kbytes
;
4904 * 'copy_file()' - Copy a PPD file or interface script...
4907 static int /* O - 0 = success, -1 = error */
4908 copy_file(const char *from
, /* I - Source file */
4909 const char *to
) /* I - Destination file */
4911 cups_file_t
*src
, /* Source file */
4912 *dst
; /* Destination file */
4913 int bytes
; /* Bytes to read/write */
4914 char buffer
[2048]; /* Copy buffer */
4917 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4920 * Open the source and destination file for a copy...
4923 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4926 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4933 * Copy the source file to the destination...
4936 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4937 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
4945 * Close both files and return...
4950 return (cupsFileClose(dst
));
4955 * 'copy_model()' - Copy a PPD model file, substituting default values
4959 static int /* O - 0 = success, -1 = error */
4960 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4961 const char *from
, /* I - Source file */
4962 const char *to
) /* I - Destination file */
4964 fd_set input
; /* select() input set */
4965 struct timeval timeout
; /* select() timeout */
4966 int maxfd
; /* Max file descriptor for select() */
4967 char tempfile
[1024]; /* Temporary PPD file */
4968 int tempfd
; /* Temporary PPD file descriptor */
4969 int temppid
; /* Process ID of cups-driverd */
4970 int temppipe
[2]; /* Temporary pipes */
4971 char *argv
[4], /* Command-line arguments */
4972 *envp
[MAX_ENV
]; /* Environment */
4973 cups_file_t
*src
, /* Source file */
4974 *dst
; /* Destination file */
4975 ppd_file_t
*ppd
; /* PPD file */
4976 int bytes
, /* Bytes from pipe */
4977 total
; /* Total bytes from pipe */
4978 char buffer
[2048]; /* Copy buffer */
4979 int i
; /* Looping var */
4980 char option
[PPD_MAX_NAME
], /* Option name */
4981 choice
[PPD_MAX_NAME
]; /* Choice name */
4982 int num_defaults
; /* Number of default options */
4983 cups_option_t
*defaults
; /* Default options */
4984 char cups_protocol
[PPD_MAX_LINE
];
4985 /* cupsProtocol attribute */
4988 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4989 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4993 * Run cups-driverd to get the PPD file...
4996 argv
[0] = "cups-driverd";
4998 argv
[2] = (char *)from
;
5001 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5003 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
5004 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
5005 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
5009 cupsdOpenPipe(temppipe
);
5011 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5012 "copy_model: Running \"cups-driverd cat %s\"...", from
);
5014 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
5015 -1, -1, 0, DefaultProfile
, &temppid
))
5026 * Wait up to 30 seconds for the PPD file to be copied...
5031 if (temppipe
[0] > CGIPipes
[0])
5032 maxfd
= temppipe
[0] + 1;
5034 maxfd
= CGIPipes
[0] + 1;
5039 * See if we have data ready...
5043 FD_SET(temppipe
[0], &input
);
5044 FD_SET(CGIPipes
[0], &input
);
5046 timeout
.tv_sec
= 30;
5047 timeout
.tv_usec
= 0;
5049 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
5059 * We have timed out...
5065 if (FD_ISSET(temppipe
[0], &input
))
5068 * Read the PPD file from the pipe, and write it to the PPD file.
5071 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
5073 if (write(tempfd
, buffer
, bytes
) < bytes
)
5082 if (FD_ISSET(CGIPipes
[0], &input
))
5092 * No data from cups-deviced...
5095 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
5101 * Read the source file and see what page sizes are supported...
5104 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
5111 * Open the destination (if possible) and set the default options...
5116 cups_protocol
[0] = '\0';
5118 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
5121 * Read all of the default lines from the old PPD...
5124 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
5125 if (!strncmp(buffer
, "*Default", 8))
5128 * Add the default option...
5131 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5132 choice
, sizeof(choice
)))
5134 ppd_option_t
*ppdo
; /* PPD option */
5138 * Only add the default if the default hasn't already been
5139 * set and the choice exists in the new PPD...
5142 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
5143 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
5144 ppdFindChoice(ppdo
, choice
))
5145 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
5149 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
5150 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
5154 else if (ppdPageSize(ppd
, DefaultPaperSize
))
5157 * Add the default media sizes...
5160 num_defaults
= cupsAddOption("PageSize", DefaultPaperSize
,
5161 num_defaults
, &defaults
);
5162 num_defaults
= cupsAddOption("PageRegion", DefaultPaperSize
,
5163 num_defaults
, &defaults
);
5164 num_defaults
= cupsAddOption("PaperDimension", DefaultPaperSize
,
5165 num_defaults
, &defaults
);
5166 num_defaults
= cupsAddOption("ImageableArea", DefaultPaperSize
,
5167 num_defaults
, &defaults
);
5173 * Open the source file for a copy...
5176 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
5178 cupsFreeOptions(num_defaults
, defaults
);
5184 * Open the destination file for a copy...
5187 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
5189 cupsFreeOptions(num_defaults
, defaults
);
5196 * Copy the source file to the destination...
5199 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
5201 if (!strncmp(buffer
, "*Default", 8))
5204 * Check for an previous default option choice...
5207 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5208 choice
, sizeof(choice
)))
5210 const char *val
; /* Default option value */
5213 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
5216 * Substitute the previous choice...
5219 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
5224 cupsFilePrintf(dst
, "%s\n", buffer
);
5227 if (cups_protocol
[0])
5228 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
5230 cupsFreeOptions(num_defaults
, defaults
);
5233 * Close both files and return...
5240 return (cupsFileClose(dst
));
5245 * 'copy_job_attrs()' - Copy job attributes.
5249 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5250 cupsd_job_t
*job
, /* I - Job */
5251 cups_array_t
*ra
) /* I - Requested attributes array */
5253 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
5257 * Send the requested attributes for each job...
5260 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
5261 con
->servername
, con
->serverport
, "/jobs/%d",
5264 if (!ra
|| cupsArrayFind(ra
, "document-count"))
5265 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5266 "document-count", job
->num_files
);
5268 if (!ra
|| cupsArrayFind(ra
, "job-media-progress"))
5269 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5270 "job-media-progress", job
->progress
);
5272 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
5273 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5274 "job-more-info", NULL
, job_uri
);
5276 if (job
->state_value
> IPP_JOB_PROCESSING
&&
5277 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
5278 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
5279 job
->num_files
> 0);
5281 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
5282 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5283 "job-printer-up-time", time(NULL
));
5285 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
5286 add_job_state_reasons(con
, job
);
5288 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
5289 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5290 "job-uri", NULL
, job_uri
);
5292 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
5297 * 'copy_printer_attrs()' - Copy printer attributes.
5302 cupsd_client_t
*con
, /* I - Client connection */
5303 cupsd_printer_t
*printer
, /* I - Printer */
5304 cups_array_t
*ra
) /* I - Requested attributes array */
5306 char printer_uri
[HTTP_MAX_URI
];
5308 time_t curtime
; /* Current time */
5309 int i
; /* Looping var */
5310 ipp_attribute_t
*history
; /* History collection */
5314 * Copy the printer attributes to the response using requested-attributes
5315 * and document-format attributes that may be provided by the client.
5318 curtime
= time(NULL
);
5321 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
5322 printer
->recoverable
)
5323 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5324 "com.apple.print.recoverable-message", NULL
,
5325 printer
->recoverable
);
5326 #endif /* __APPLE__ */
5328 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
5329 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5330 "marker-change-time", printer
->marker_time
);
5332 if (printer
->num_printers
> 0 &&
5333 (!ra
|| cupsArrayFind(ra
, "member-uris")))
5335 ipp_attribute_t
*member_uris
; /* member-uris attribute */
5336 cupsd_printer_t
*p2
; /* Printer in class */
5337 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
5340 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
5341 IPP_TAG_URI
, "member-uris",
5342 printer
->num_printers
, NULL
,
5345 for (i
= 0; i
< printer
->num_printers
; i
++)
5347 p2
= printer
->printers
[i
];
5349 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
5350 IPP_TAG_URI
)) != NULL
)
5351 member_uris
->values
[i
].string
.text
=
5352 _cupsStrAlloc(p2_uri
->values
[0].string
.text
);
5355 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
5356 sizeof(printer_uri
), "ipp", NULL
, con
->servername
,
5358 (p2
->type
& CUPS_PRINTER_CLASS
) ?
5359 "/classes/%s" : "/printers/%s", p2
->name
);
5360 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
5366 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
5367 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
5368 "printer-alert", NULL
, printer
->alert
);
5370 if (printer
->alert_description
&&
5371 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
5372 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5373 "printer-alert-description", NULL
,
5374 printer
->alert_description
);
5376 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
5377 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
5378 ippTimeToDate(curtime
));
5381 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
5383 if (printer
->reg_name
)
5384 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5385 "printer-dns-sd-name", NULL
, printer
->reg_name
);
5387 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
5388 "printer-dns-sd-name", 0);
5390 #endif /* HAVE_DNSSD */
5392 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
5393 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5394 "printer-error-policy", NULL
, printer
->error_policy
);
5396 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
5397 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
5398 printer
->accepting
);
5400 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
5401 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
5404 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
5405 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5406 "printer-op-policy", NULL
, printer
->op_policy
);
5408 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
5409 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
5412 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
5413 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5414 "printer-state-change-time", printer
->state_time
);
5416 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
5417 cupsArrayFind(ra
, "printer-state-history"))
5420 * Printer history is only sent if specifically requested, so that
5421 * older CUPS/IPP clients won't barf on the collection attributes.
5424 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
5425 "printer-state-history",
5426 printer
->num_history
, NULL
);
5428 for (i
= 0; i
< printer
->num_history
; i
++)
5429 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
5430 NULL
, IPP_TAG_ZERO
, 0);
5433 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
5434 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5435 "printer-state-message", NULL
, printer
->state_message
);
5437 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
5438 add_printer_state_reasons(con
, printer
);
5440 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5442 int type
; /* printer-type value */
5446 * Add the CUPS-specific printer-type attribute...
5449 type
= printer
->type
;
5451 if (printer
== DefaultPrinter
)
5452 type
|= CUPS_PRINTER_DEFAULT
;
5454 if (!printer
->accepting
)
5455 type
|= CUPS_PRINTER_REJECTING
;
5457 if (!printer
->shared
)
5458 type
|= CUPS_PRINTER_NOT_SHARED
;
5460 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
5464 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5465 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5466 "printer-up-time", curtime
);
5468 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
5469 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
5472 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5473 "ipp", NULL
, con
->servername
, con
->serverport
,
5474 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5475 "/classes/%s" : "/printers/%s", printer
->name
);
5476 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5477 "printer-uri-supported", NULL
, printer_uri
);
5478 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5482 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5483 add_queued_job_count(con
, printer
);
5485 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
5486 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
5491 * 'copy_subscription_attrs()' - Copy subscription attributes.
5495 copy_subscription_attrs(
5496 cupsd_client_t
*con
, /* I - Client connection */
5497 cupsd_subscription_t
*sub
, /* I - Subscription */
5498 cups_array_t
*ra
) /* I - Requested attributes array */
5500 ipp_attribute_t
*attr
; /* Current attribute */
5501 char printer_uri
[HTTP_MAX_URI
];
5503 int count
; /* Number of events */
5504 unsigned mask
; /* Current event mask */
5505 const char *name
; /* Current event name */
5509 * Copy the subscription attributes to the response using the
5510 * requested-attributes attribute that may be provided by the client.
5513 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
5515 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5518 * Simple event list...
5521 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5522 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5523 "notify-events", NULL
, name
);
5528 * Complex event list...
5531 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5532 if (sub
->mask
& mask
)
5535 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5536 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5537 "notify-events", count
, NULL
, NULL
);
5539 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5540 if (sub
->mask
& mask
)
5542 attr
->values
[count
].string
.text
=
5543 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5550 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5551 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5552 "notify-job-id", sub
->job
->id
);
5554 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
5555 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5556 "notify-lease-duration", sub
->lease
);
5558 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5560 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5561 "ipp", NULL
, con
->servername
, con
->serverport
,
5562 "/printers/%s", sub
->dest
->name
);
5563 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5564 "notify-printer-uri", NULL
, printer_uri
);
5567 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
5568 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5569 "notify-recipient-uri", NULL
, sub
->recipient
);
5570 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
5571 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5572 "notify-pull-method", NULL
, "ippget");
5574 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
5575 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5576 "notify-subscriber-user-name", NULL
, sub
->owner
);
5578 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5579 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5580 "notify-subscription-id", sub
->id
);
5582 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
5583 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5584 "notify-time-interval", sub
->interval
);
5586 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5587 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5588 sub
->user_data
, sub
->user_data_len
);
5593 * 'create_job()' - Print a file to a printer or class.
5597 create_job(cupsd_client_t
*con
, /* I - Client connection */
5598 ipp_attribute_t
*uri
) /* I - Printer URI */
5600 cupsd_printer_t
*printer
; /* Printer */
5601 cupsd_job_t
*job
; /* New job */
5604 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5605 con
->http
.fd
, uri
->values
[0].string
.text
);
5608 * Is the destination valid?
5611 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5617 send_ipp_status(con
, IPP_NOT_FOUND
,
5618 _("The printer or class was not found."));
5623 * Create the job object...
5626 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5629 job
->pending_timeout
= 1;
5632 * Save and log the job...
5635 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5636 job
->dest
, job
->username
);
5641 * 'create_requested_array()' - Create an array for the requested-attributes.
5644 static cups_array_t
* /* O - Array of attributes or NULL */
5645 create_requested_array(ipp_t
*request
) /* I - IPP request */
5647 int i
; /* Looping var */
5648 ipp_attribute_t
*requested
; /* requested-attributes attribute */
5649 cups_array_t
*ra
; /* Requested attributes array */
5650 char *value
; /* Current value */
5654 * Get the requested-attributes attribute, and return NULL if we don't
5658 if ((requested
= ippFindAttribute(request
, "requested-attributes",
5659 IPP_TAG_KEYWORD
)) == NULL
)
5663 * If the attribute contains a single "all" keyword, return NULL...
5666 if (requested
->num_values
== 1 &&
5667 !strcmp(requested
->values
[0].string
.text
, "all"))
5671 * Create an array using "strcmp" as the comparison function...
5674 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
5676 for (i
= 0; i
< requested
->num_values
; i
++)
5678 value
= requested
->values
[i
].string
.text
;
5680 if (!strcmp(value
, "job-template"))
5682 cupsArrayAdd(ra
, "copies");
5683 cupsArrayAdd(ra
, "copies-default");
5684 cupsArrayAdd(ra
, "copies-supported");
5685 cupsArrayAdd(ra
, "finishings");
5686 cupsArrayAdd(ra
, "finishings-default");
5687 cupsArrayAdd(ra
, "finishings-supported");
5688 cupsArrayAdd(ra
, "job-hold-until");
5689 cupsArrayAdd(ra
, "job-hold-until-default");
5690 cupsArrayAdd(ra
, "job-hold-until-supported");
5691 cupsArrayAdd(ra
, "job-priority");
5692 cupsArrayAdd(ra
, "job-priority-default");
5693 cupsArrayAdd(ra
, "job-priority-supported");
5694 cupsArrayAdd(ra
, "job-sheets");
5695 cupsArrayAdd(ra
, "job-sheets-default");
5696 cupsArrayAdd(ra
, "job-sheets-supported");
5697 cupsArrayAdd(ra
, "media");
5698 cupsArrayAdd(ra
, "media-default");
5699 cupsArrayAdd(ra
, "media-supported");
5700 cupsArrayAdd(ra
, "multiple-document-handling");
5701 cupsArrayAdd(ra
, "multiple-document-handling-default");
5702 cupsArrayAdd(ra
, "multiple-document-handling-supported");
5703 cupsArrayAdd(ra
, "number-up");
5704 cupsArrayAdd(ra
, "number-up-default");
5705 cupsArrayAdd(ra
, "number-up-supported");
5706 cupsArrayAdd(ra
, "orientation-requested");
5707 cupsArrayAdd(ra
, "orientation-requested-default");
5708 cupsArrayAdd(ra
, "orientation-requested-supported");
5709 cupsArrayAdd(ra
, "page-ranges");
5710 cupsArrayAdd(ra
, "page-ranges-supported");
5711 cupsArrayAdd(ra
, "printer-resolution");
5712 cupsArrayAdd(ra
, "printer-resolution-default");
5713 cupsArrayAdd(ra
, "printer-resolution-supported");
5714 cupsArrayAdd(ra
, "print-quality");
5715 cupsArrayAdd(ra
, "print-quality-default");
5716 cupsArrayAdd(ra
, "print-quality-supported");
5717 cupsArrayAdd(ra
, "sides");
5718 cupsArrayAdd(ra
, "sides-default");
5719 cupsArrayAdd(ra
, "sides-supported");
5721 else if (!strcmp(value
, "job-description"))
5723 cupsArrayAdd(ra
, "date-time-at-completed");
5724 cupsArrayAdd(ra
, "date-time-at-creation");
5725 cupsArrayAdd(ra
, "date-time-at-processing");
5726 cupsArrayAdd(ra
, "job-detailed-status-message");
5727 cupsArrayAdd(ra
, "job-document-access-errors");
5728 cupsArrayAdd(ra
, "job-id");
5729 cupsArrayAdd(ra
, "job-impressions");
5730 cupsArrayAdd(ra
, "job-impressions-completed");
5731 cupsArrayAdd(ra
, "job-k-octets");
5732 cupsArrayAdd(ra
, "job-k-octets-processed");
5733 cupsArrayAdd(ra
, "job-media-progress");
5734 cupsArrayAdd(ra
, "job-media-sheets");
5735 cupsArrayAdd(ra
, "job-media-sheets-completed");
5736 cupsArrayAdd(ra
, "job-message-from-operator");
5737 cupsArrayAdd(ra
, "job-more-info");
5738 cupsArrayAdd(ra
, "job-name");
5739 cupsArrayAdd(ra
, "job-originating-user-name");
5740 cupsArrayAdd(ra
, "job-printer-up-time");
5741 cupsArrayAdd(ra
, "job-printer-uri");
5742 cupsArrayAdd(ra
, "job-state");
5743 cupsArrayAdd(ra
, "job-state-message");
5744 cupsArrayAdd(ra
, "job-state-reasons");
5745 cupsArrayAdd(ra
, "job-uri");
5746 cupsArrayAdd(ra
, "number-of-documents");
5747 cupsArrayAdd(ra
, "number-of-intervening-jobs");
5748 cupsArrayAdd(ra
, "output-device-assigned");
5749 cupsArrayAdd(ra
, "time-at-completed");
5750 cupsArrayAdd(ra
, "time-at-creation");
5751 cupsArrayAdd(ra
, "time-at-processing");
5753 else if (!strcmp(value
, "printer-description"))
5755 cupsArrayAdd(ra
, "charset-configured");
5756 cupsArrayAdd(ra
, "charset-supported");
5757 cupsArrayAdd(ra
, "color-supported");
5758 cupsArrayAdd(ra
, "compression-supported");
5759 cupsArrayAdd(ra
, "document-format-default");
5760 cupsArrayAdd(ra
, "document-format-supported");
5761 cupsArrayAdd(ra
, "generated-natural-language-supported");
5762 cupsArrayAdd(ra
, "ipp-versions-supported");
5763 cupsArrayAdd(ra
, "job-impressions-supported");
5764 cupsArrayAdd(ra
, "job-k-octets-supported");
5765 cupsArrayAdd(ra
, "job-media-sheets-supported");
5766 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
5767 cupsArrayAdd(ra
, "multiple-operation-time-out");
5768 cupsArrayAdd(ra
, "natural-language-configured");
5769 cupsArrayAdd(ra
, "notify-attributes-supported");
5770 cupsArrayAdd(ra
, "notify-lease-duration-default");
5771 cupsArrayAdd(ra
, "notify-lease-duration-supported");
5772 cupsArrayAdd(ra
, "notify-max-events-supported");
5773 cupsArrayAdd(ra
, "notify-events-default");
5774 cupsArrayAdd(ra
, "notify-events-supported");
5775 cupsArrayAdd(ra
, "notify-pull-method-supported");
5776 cupsArrayAdd(ra
, "notify-schemes-supported");
5777 cupsArrayAdd(ra
, "operations-supported");
5778 cupsArrayAdd(ra
, "pages-per-minute");
5779 cupsArrayAdd(ra
, "pages-per-minute-color");
5780 cupsArrayAdd(ra
, "pdl-override-supported");
5781 cupsArrayAdd(ra
, "printer-alert");
5782 cupsArrayAdd(ra
, "printer-alert-description");
5783 cupsArrayAdd(ra
, "printer-commands");
5784 cupsArrayAdd(ra
, "printer-current-time");
5785 cupsArrayAdd(ra
, "printer-driver-installer");
5786 cupsArrayAdd(ra
, "printer-dns-sd-name");
5787 cupsArrayAdd(ra
, "printer-info");
5788 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
5789 cupsArrayAdd(ra
, "printer-location");
5790 cupsArrayAdd(ra
, "printer-make-and-model");
5791 cupsArrayAdd(ra
, "printer-message-from-operator");
5792 cupsArrayAdd(ra
, "printer-more-info");
5793 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
5794 cupsArrayAdd(ra
, "printer-name");
5795 cupsArrayAdd(ra
, "printer-state");
5796 cupsArrayAdd(ra
, "printer-state-message");
5797 cupsArrayAdd(ra
, "printer-state-reasons");
5798 cupsArrayAdd(ra
, "printer-type");
5799 cupsArrayAdd(ra
, "printer-up-time");
5800 cupsArrayAdd(ra
, "printer-uri-supported");
5801 cupsArrayAdd(ra
, "queued-job-count");
5802 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
5803 cupsArrayAdd(ra
, "uri-authentication-supported");
5804 cupsArrayAdd(ra
, "uri-security-supported");
5806 else if (!strcmp(value
, "printer-defaults"))
5808 char *name
; /* Option name */
5811 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5813 name
= (char *)cupsArrayNext(CommonDefaults
))
5814 cupsArrayAdd(ra
, name
);
5816 else if (!strcmp(value
, "subscription-template"))
5818 cupsArrayAdd(ra
, "notify-attributes");
5819 cupsArrayAdd(ra
, "notify-charset");
5820 cupsArrayAdd(ra
, "notify-events");
5821 cupsArrayAdd(ra
, "notify-lease-duration");
5822 cupsArrayAdd(ra
, "notify-natural-language");
5823 cupsArrayAdd(ra
, "notify-pull-method");
5824 cupsArrayAdd(ra
, "notify-recipient-uri");
5825 cupsArrayAdd(ra
, "notify-time-interval");
5826 cupsArrayAdd(ra
, "notify-user-data");
5829 cupsArrayAdd(ra
, value
);
5837 * 'create_subscription()' - Create a notification subscription.
5841 create_subscription(
5842 cupsd_client_t
*con
, /* I - Client connection */
5843 ipp_attribute_t
*uri
) /* I - Printer URI */
5845 http_status_t status
; /* Policy status */
5846 int i
; /* Looping var */
5847 ipp_attribute_t
*attr
; /* Current attribute */
5848 cups_ptype_t dtype
; /* Destination type (printer/class) */
5849 char scheme
[HTTP_MAX_URI
],
5850 /* Scheme portion of URI */
5851 userpass
[HTTP_MAX_URI
],
5852 /* Username portion of URI */
5854 /* Host portion of URI */
5855 resource
[HTTP_MAX_URI
];
5856 /* Resource portion of URI */
5857 int port
; /* Port portion of URI */
5858 cupsd_printer_t
*printer
; /* Printer/class */
5859 cupsd_job_t
*job
; /* Job */
5860 int jobid
; /* Job ID */
5861 cupsd_subscription_t
*sub
; /* Subscription object */
5862 const char *username
, /* requesting-user-name or
5863 authenticated username */
5864 *recipient
, /* notify-recipient-uri */
5865 *pullmethod
; /* notify-pull-method */
5866 ipp_attribute_t
*user_data
; /* notify-user-data */
5867 int interval
, /* notify-time-interval */
5868 lease
; /* notify-lease-duration */
5869 unsigned mask
; /* notify-events */
5870 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5871 *notify_lease
; /* notify-lease-duration(-default) */
5875 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5877 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5878 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5879 attr
->value_tag
, attr
->name
);
5881 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5886 * Is the destination valid?
5889 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5890 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5891 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5893 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5894 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5895 sizeof(host
), &port
, resource
, sizeof(resource
));
5897 if (!strcmp(resource
, "/"))
5899 dtype
= (cups_ptype_t
)0;
5902 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5904 dtype
= (cups_ptype_t
)0;
5907 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5909 dtype
= CUPS_PRINTER_CLASS
;
5912 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5918 send_ipp_status(con
, IPP_NOT_FOUND
,
5919 _("The printer or class was not found."));
5929 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5932 send_http_error(con
, status
, printer
);
5936 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5938 send_http_error(con
, status
, NULL
);
5943 * Get the user that is requesting the subscription...
5946 username
= get_username(con
);
5949 * Find the first subscription group attribute; return if we have
5953 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5954 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5959 send_ipp_status(con
, IPP_BAD_REQUEST
,
5960 _("No subscription attributes in request!"));
5965 * Process the subscription attributes in the request...
5968 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5976 lease
= DefaultLeaseDuration
;
5978 mask
= CUPSD_EVENT_NONE
;
5982 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5984 notify_lease
= ippFindAttribute(printer
->attrs
,
5985 "notify-lease-duration-default",
5989 lease
= notify_lease
->values
[0].integer
;
5993 notify_events
= NULL
;
5994 notify_lease
= NULL
;
5997 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5999 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
6000 attr
->value_tag
== IPP_TAG_URI
)
6003 * Validate the recipient scheme against the ServerBin/notifier
6007 char notifier
[1024]; /* Notifier filename */
6010 recipient
= attr
->values
[0].string
.text
;
6012 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
6013 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
6014 host
, sizeof(host
), &port
,
6015 resource
, sizeof(resource
)) < HTTP_URI_OK
)
6017 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6018 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
6019 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6020 "notify-status-code", IPP_URI_SCHEME
);
6024 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
6026 if (access(notifier
, X_OK
))
6028 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6029 _("notify-recipient-uri URI \"%s\" uses unknown "
6030 "scheme!"), recipient
);
6031 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6032 "notify-status-code", IPP_URI_SCHEME
);
6036 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
6038 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6039 _("notify-recipient-uri URI \"%s\" is already used!"),
6041 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6042 "notify-status-code", IPP_ATTRIBUTES
);
6046 else if (!strcmp(attr
->name
, "notify-pull-method") &&
6047 attr
->value_tag
== IPP_TAG_KEYWORD
)
6049 pullmethod
= attr
->values
[0].string
.text
;
6051 if (strcmp(pullmethod
, "ippget"))
6053 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6054 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
6055 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6056 "notify-status-code", IPP_ATTRIBUTES
);
6060 else if (!strcmp(attr
->name
, "notify-charset") &&
6061 attr
->value_tag
== IPP_TAG_CHARSET
&&
6062 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
6063 strcmp(attr
->values
[0].string
.text
, "utf-8"))
6065 send_ipp_status(con
, IPP_CHARSET
,
6066 _("Character set \"%s\" not supported!"),
6067 attr
->values
[0].string
.text
);
6070 else if (!strcmp(attr
->name
, "notify-natural-language") &&
6071 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
6072 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
6074 send_ipp_status(con
, IPP_CHARSET
,
6075 _("Language \"%s\" not supported!"),
6076 attr
->values
[0].string
.text
);
6079 else if (!strcmp(attr
->name
, "notify-user-data") &&
6080 attr
->value_tag
== IPP_TAG_STRING
)
6082 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
6084 send_ipp_status(con
, IPP_REQUEST_VALUE
,
6085 _("The notify-user-data value is too large "
6086 "(%d > 63 octets)!"),
6087 attr
->values
[0].unknown
.length
);
6093 else if (!strcmp(attr
->name
, "notify-events") &&
6094 attr
->value_tag
== IPP_TAG_KEYWORD
)
6095 notify_events
= attr
;
6096 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
6097 attr
->value_tag
== IPP_TAG_INTEGER
)
6098 lease
= attr
->values
[0].integer
;
6099 else if (!strcmp(attr
->name
, "notify-time-interval") &&
6100 attr
->value_tag
== IPP_TAG_INTEGER
)
6101 interval
= attr
->values
[0].integer
;
6102 else if (!strcmp(attr
->name
, "notify-job-id") &&
6103 attr
->value_tag
== IPP_TAG_INTEGER
)
6104 jobid
= attr
->values
[0].integer
;
6111 for (i
= 0; i
< notify_events
->num_values
; i
++)
6112 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
6116 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
6118 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
6119 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
6120 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
6122 if (!recipient
&& !pullmethod
)
6125 if (mask
== CUPSD_EVENT_NONE
)
6128 mask
= CUPSD_EVENT_JOB_COMPLETED
;
6130 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
6133 send_ipp_status(con
, IPP_BAD_REQUEST
,
6134 _("notify-events not specified!"));
6139 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
6141 cupsdLogMessage(CUPSD_LOG_INFO
,
6142 "create_subscription: Limiting notify-lease-duration to "
6145 lease
= MaxLeaseDuration
;
6150 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6152 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
6159 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
6162 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
6165 cupsdLogMessage(CUPSD_LOG_DEBUG
,
6166 "Added subscription %d for printer \"%s\"",
6167 sub
->id
, printer
->name
);
6169 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
6172 sub
->interval
= interval
;
6174 sub
->expire
= lease
? time(NULL
) + lease
: 0;
6176 cupsdSetString(&sub
->owner
, username
);
6180 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
6181 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
6182 sub
->user_data_len
);
6185 ippAddSeparator(con
->response
);
6186 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
6187 "notify-subscription-id", sub
->id
);
6189 con
->response
->request
.status
.status_code
= IPP_OK
;
6195 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
6200 * 'delete_printer()' - Remove a printer or class from the system.
6204 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
6205 ipp_attribute_t
*uri
) /* I - URI of printer or class */
6207 http_status_t status
; /* Policy status */
6208 cups_ptype_t dtype
; /* Destination type (printer/class) */
6209 cupsd_printer_t
*printer
; /* Printer/class */
6210 char filename
[1024]; /* Script/PPD filename */
6213 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
6214 con
->http
.fd
, uri
->values
[0].string
.text
);
6217 * Do we have a valid URI?
6220 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6226 send_ipp_status(con
, IPP_NOT_FOUND
,
6227 _("The printer or class was not found."));
6235 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6237 send_http_error(con
, status
, NULL
);
6242 * Remove old jobs...
6245 cupsdCancelJobs(printer
->name
, NULL
, 1);
6248 * Remove old subscriptions and send a "deleted printer" event...
6251 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
6252 "%s \"%s\" deleted by \"%s\".",
6253 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
6254 printer
->name
, get_username(con
));
6256 cupsdExpireSubscriptions(printer
, NULL
);
6259 * Remove any old PPD or script files...
6262 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
6266 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6272 * Unregister color profiles...
6275 apple_unregister_profiles(printer
);
6276 #endif /* __APPLE__ */
6278 if (dtype
& CUPS_PRINTER_CLASS
)
6280 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
6281 printer
->name
, get_username(con
));
6283 cupsdDeletePrinter(printer
, 0);
6284 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6288 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
6289 printer
->name
, get_username(con
));
6291 cupsdDeletePrinter(printer
, 0);
6292 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
6295 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
6298 * Return with no errors...
6301 con
->response
->request
.status
.status_code
= IPP_OK
;
6306 * 'get_default()' - Get the default destination.
6310 get_default(cupsd_client_t
*con
) /* I - Client connection */
6312 http_status_t status
; /* Policy status */
6313 cups_array_t
*ra
; /* Requested attributes array */
6316 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
6322 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6324 send_http_error(con
, status
, NULL
);
6330 ra
= create_requested_array(con
->request
);
6332 copy_printer_attrs(con
, DefaultPrinter
, ra
);
6334 cupsArrayDelete(ra
);
6336 con
->response
->request
.status
.status_code
= IPP_OK
;
6339 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
6344 * 'get_devices()' - Get the list of available devices on the local system.
6348 get_devices(cupsd_client_t
*con
) /* I - Client connection */
6350 http_status_t status
; /* Policy status */
6351 ipp_attribute_t
*limit
, /* limit attribute */
6352 *timeout
, /* timeout attribute */
6353 *requested
, /* requested-attributes attribute */
6354 *exclude
; /* exclude-schemes attribute */
6355 char command
[1024], /* cups-deviced command */
6356 options
[1024], /* Options to pass to command */
6358 /* String for requested attributes */
6360 /* String for excluded attributes */
6363 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
6369 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6371 send_http_error(con
, status
, NULL
);
6376 * Run cups-deviced command with the given options...
6379 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6380 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
6381 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6383 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
6386 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6388 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6391 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6393 exclude_str
[0] = '\0';
6395 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
6396 snprintf(options
, sizeof(options
),
6397 "%d+%d+%d+%d+%s%s%s",
6398 con
->request
->request
.op
.request_id
,
6399 limit
? limit
->values
[0].integer
: 0,
6400 timeout
? timeout
->values
[0].integer
: 10,
6403 exclude_str
[0] ? "%20" : "", exclude_str
);
6405 if (cupsdSendCommand(con
, command
, options
, 1))
6408 * Command started successfully, don't send an IPP response here...
6411 ippDelete(con
->response
);
6412 con
->response
= NULL
;
6417 * Command failed, return "internal error" so the user knows something
6421 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6422 _("cups-deviced failed to execute."));
6428 * 'get_document()' - Get a copy of a job file.
6432 get_document(cupsd_client_t
*con
, /* I - Client connection */
6433 ipp_attribute_t
*uri
) /* I - Job URI */
6435 http_status_t status
; /* Policy status */
6436 ipp_attribute_t
*attr
; /* Current attribute */
6437 int jobid
; /* Job ID */
6438 int docnum
; /* Document number */
6439 cupsd_job_t
*job
; /* Current job */
6440 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6441 username
[HTTP_MAX_URI
], /* Username portion of URI */
6442 host
[HTTP_MAX_URI
], /* Host portion of URI */
6443 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6444 int port
; /* Port portion of URI */
6445 char filename
[1024], /* Filename for document */
6446 format
[1024]; /* Format for document */
6449 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6450 con
->http
.fd
, uri
->values
[0].string
.text
);
6453 * See if we have a job URI or a printer URI...
6456 if (!strcmp(uri
->name
, "printer-uri"))
6459 * Got a printer URI; see if we also have a job-id attribute...
6462 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6463 IPP_TAG_INTEGER
)) == NULL
)
6465 send_ipp_status(con
, IPP_BAD_REQUEST
,
6466 _("Got a printer-uri attribute but no job-id!"));
6470 jobid
= attr
->values
[0].integer
;
6475 * Got a job URI; parse it to get the job ID...
6478 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6479 sizeof(method
), username
, sizeof(username
), host
,
6480 sizeof(host
), &port
, resource
, sizeof(resource
));
6482 if (strncmp(resource
, "/jobs/", 6))
6488 send_ipp_status(con
, IPP_BAD_REQUEST
,
6489 _("Bad job-uri attribute \"%s\"!"),
6490 uri
->values
[0].string
.text
);
6494 jobid
= atoi(resource
+ 6);
6498 * See if the job exists...
6501 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6504 * Nope - return a "not found" error...
6507 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6515 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6517 send_http_error(con
, status
, NULL
);
6522 * Get the document number...
6525 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6526 IPP_TAG_INTEGER
)) == NULL
)
6528 send_ipp_status(con
, IPP_BAD_REQUEST
,
6529 _("Missing document-number attribute!"));
6533 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6534 attr
->num_values
> 1)
6536 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
6541 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6543 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6545 cupsdLogMessage(CUPSD_LOG_ERROR
,
6546 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6548 send_ipp_status(con
, IPP_NOT_FOUND
,
6549 _("Unable to open document %d in job %d!"), docnum
, jobid
);
6553 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6557 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6558 job
->filetypes
[docnum
- 1]->type
);
6560 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6562 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6564 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6565 IPP_TAG_NAME
)) != NULL
)
6566 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6567 NULL
, attr
->values
[0].string
.text
);
6572 * 'get_job_attrs()' - Get job attributes.
6576 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6577 ipp_attribute_t
*uri
) /* I - Job URI */
6579 http_status_t status
; /* Policy status */
6580 ipp_attribute_t
*attr
; /* Current attribute */
6581 int jobid
; /* Job ID */
6582 cupsd_job_t
*job
; /* Current job */
6583 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6584 username
[HTTP_MAX_URI
], /* Username portion of URI */
6585 host
[HTTP_MAX_URI
], /* Host portion of URI */
6586 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6587 int port
; /* Port portion of URI */
6588 cups_array_t
*ra
; /* Requested attributes array */
6591 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6592 con
->http
.fd
, uri
->values
[0].string
.text
);
6595 * See if we have a job URI or a printer URI...
6598 if (!strcmp(uri
->name
, "printer-uri"))
6601 * Got a printer URI; see if we also have a job-id attribute...
6604 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6605 IPP_TAG_INTEGER
)) == NULL
)
6607 send_ipp_status(con
, IPP_BAD_REQUEST
,
6608 _("Got a printer-uri attribute but no job-id!"));
6612 jobid
= attr
->values
[0].integer
;
6617 * Got a job URI; parse it to get the job ID...
6620 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6621 sizeof(method
), username
, sizeof(username
), host
,
6622 sizeof(host
), &port
, resource
, sizeof(resource
));
6624 if (strncmp(resource
, "/jobs/", 6))
6630 send_ipp_status(con
, IPP_BAD_REQUEST
,
6631 _("Bad job-uri attribute \"%s\"!"),
6632 uri
->values
[0].string
.text
);
6636 jobid
= atoi(resource
+ 6);
6640 * See if the job exists...
6643 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6646 * Nope - return a "not found" error...
6649 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6657 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6659 send_http_error(con
, status
, NULL
);
6664 * Copy attributes...
6669 ra
= create_requested_array(con
->request
);
6670 copy_job_attrs(con
, job
, ra
);
6671 cupsArrayDelete(ra
);
6673 con
->response
->request
.status
.status_code
= IPP_OK
;
6678 * 'get_jobs()' - Get a list of jobs for the specified printer.
6682 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6683 ipp_attribute_t
*uri
) /* I - Printer URI */
6685 http_status_t status
; /* Policy status */
6686 ipp_attribute_t
*attr
; /* Current attribute */
6687 const char *dest
; /* Destination */
6688 cups_ptype_t dtype
; /* Destination type (printer/class) */
6689 cups_ptype_t dmask
; /* Destination type mask */
6690 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6691 username
[HTTP_MAX_URI
], /* Username portion of URI */
6692 host
[HTTP_MAX_URI
], /* Host portion of URI */
6693 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6694 int port
; /* Port portion of URI */
6695 int completed
; /* Completed jobs? */
6696 int first_job_id
; /* First job ID */
6697 int limit
; /* Maximum number of jobs to return */
6698 int count
; /* Number of jobs that match */
6699 cupsd_job_t
*job
; /* Current job pointer */
6700 cupsd_printer_t
*printer
; /* Printer */
6701 cups_array_t
*list
; /* Which job list... */
6702 cups_array_t
*ra
; /* Requested attributes array */
6705 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
6706 uri
->values
[0].string
.text
);
6709 * Is the destination valid?
6712 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6713 sizeof(scheme
), username
, sizeof(username
), host
,
6714 sizeof(host
), &port
, resource
, sizeof(resource
));
6716 if (!strcmp(resource
, "/") ||
6717 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
6720 dtype
= (cups_ptype_t
)0;
6721 dmask
= (cups_ptype_t
)0;
6724 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6727 dtype
= (cups_ptype_t
)0;
6728 dmask
= CUPS_PRINTER_CLASS
;
6731 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6734 dtype
= CUPS_PRINTER_CLASS
;
6735 dmask
= CUPS_PRINTER_CLASS
;
6738 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6745 send_ipp_status(con
, IPP_NOT_FOUND
,
6746 _("The printer or class was not found."));
6751 dtype
&= CUPS_PRINTER_CLASS
;
6752 dmask
= CUPS_PRINTER_CLASS
;
6761 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6764 send_http_error(con
, status
, printer
);
6768 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6770 send_http_error(con
, status
, NULL
);
6775 * See if the "which-jobs" attribute have been specified...
6778 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6779 IPP_TAG_KEYWORD
)) != NULL
&&
6780 !strcmp(attr
->values
[0].string
.text
, "completed"))
6785 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
6790 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "printing"))
6793 list
= PrintingJobs
;
6802 * See if they want to limit the number of jobs reported...
6805 if ((attr
= ippFindAttribute(con
->request
, "limit",
6806 IPP_TAG_INTEGER
)) != NULL
)
6807 limit
= attr
->values
[0].integer
;
6811 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
6812 IPP_TAG_INTEGER
)) != NULL
)
6813 first_job_id
= attr
->values
[0].integer
;
6818 * See if we only want to see jobs for a specific user...
6821 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
6822 IPP_TAG_BOOLEAN
)) != NULL
&&
6823 attr
->values
[0].boolean
)
6824 strlcpy(username
, get_username(con
), sizeof(username
));
6828 ra
= create_requested_array(con
->request
);
6831 * OK, build a list of jobs for this printer...
6834 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6835 (limit
<= 0 || count
< limit
) && job
;
6836 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6839 * Filter out jobs that don't match...
6842 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
6844 if (!job
->dest
|| !job
->username
)
6847 if (!job
->dest
|| !job
->username
)
6850 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6851 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6853 if ((job
->dtype
& dmask
) != dtype
&&
6854 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6856 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
6859 if (job
->id
< first_job_id
)
6867 if (username
[0] && strcasecmp(username
, job
->username
))
6871 ippAddSeparator(con
->response
);
6875 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
6877 copy_job_attrs(con
, job
, ra
);
6880 cupsArrayDelete(ra
);
6882 con
->response
->request
.status
.status_code
= IPP_OK
;
6887 * 'get_notifications()' - Get events for a subscription.
6891 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6893 int i
, j
; /* Looping vars */
6894 http_status_t status
; /* Policy status */
6895 cupsd_subscription_t
*sub
; /* Subscription */
6896 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6897 *sequences
; /* notify-sequence-numbers */
6898 int min_seq
; /* Minimum sequence number */
6899 int interval
; /* Poll interval */
6902 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6906 * Get subscription attributes...
6909 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6911 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6916 send_ipp_status(con
, IPP_BAD_REQUEST
,
6917 _("Missing notify-subscription-ids attribute!"));
6922 * Are the subscription IDs valid?
6925 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6927 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6930 * Bad subscription ID...
6933 send_ipp_status(con
, IPP_NOT_FOUND
,
6934 _("notify-subscription-id %d no good!"),
6935 ids
->values
[i
].integer
);
6943 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6945 con
, sub
->owner
)) != HTTP_OK
)
6947 send_http_error(con
, status
, sub
->dest
);
6952 * Check the subscription type and update the interval accordingly.
6955 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6958 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6960 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6966 * Tell the client to poll again in N seconds...
6970 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6971 "notify-get-interval", interval
);
6973 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6974 "printer-up-time", time(NULL
));
6977 * Copy the subscription event attributes to the response.
6980 con
->response
->request
.status
.status_code
=
6981 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6983 for (i
= 0; i
< ids
->num_values
; i
++)
6986 * Get the subscription and sequence number...
6989 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6991 if (sequences
&& i
< sequences
->num_values
)
6992 min_seq
= sequences
->values
[i
].integer
;
6997 * If we don't have any new events, nothing to do here...
7000 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
7004 * Otherwise copy all of the new events...
7007 if (sub
->first_event_id
> min_seq
)
7010 j
= min_seq
- sub
->first_event_id
;
7012 for (; j
< sub
->num_events
; j
++)
7014 ippAddSeparator(con
->response
);
7016 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
7017 IPP_TAG_EVENT_NOTIFICATION
, 0);
7024 * 'get_ppd()' - Get a named PPD from the local system.
7028 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
7029 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
7031 http_status_t status
; /* Policy status */
7032 cupsd_printer_t
*dest
; /* Destination */
7033 cups_ptype_t dtype
; /* Destination type */
7036 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
7037 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
7039 if (!strcmp(uri
->name
, "ppd-name"))
7042 * Return a PPD file from cups-driverd...
7045 char command
[1024], /* cups-driverd command */
7046 options
[1024], /* Options to pass to command */
7047 ppd_name
[1024]; /* ppd-name */
7054 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7056 send_http_error(con
, status
, NULL
);
7061 * Run cups-driverd command with the given options...
7064 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7065 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
7066 snprintf(options
, sizeof(options
), "get+%d+%s",
7067 con
->request
->request
.op
.request_id
, ppd_name
);
7069 if (cupsdSendCommand(con
, command
, options
, 0))
7072 * Command started successfully, don't send an IPP response here...
7075 ippDelete(con
->response
);
7076 con
->response
= NULL
;
7081 * Command failed, return "internal error" so the user knows something
7085 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7086 _("cups-driverd failed to execute."));
7089 else if (!strcmp(uri
->name
, "printer-uri") &&
7090 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
7092 int i
; /* Looping var */
7093 char filename
[1024]; /* PPD filename */
7100 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7102 send_http_error(con
, status
, dest
);
7107 * See if we need the PPD for a class or remote printer...
7110 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7113 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
7115 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7116 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7117 "printer-uri", NULL
, dest
->uri
);
7120 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
7122 for (i
= 0; i
< dest
->num_printers
; i
++)
7123 if (!(dest
->printers
[i
]->type
&
7124 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
7126 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7127 dest
->printers
[i
]->name
);
7129 if (!access(filename
, 0))
7133 if (i
< dest
->num_printers
)
7134 dest
= dest
->printers
[i
];
7137 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7138 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7139 "printer-uri", NULL
, dest
->printers
[0]->uri
);
7145 * Found the printer with the PPD file, now see if there is one...
7148 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
7150 send_ipp_status(con
, IPP_NOT_FOUND
,
7151 _("The PPD file \"%s\" could not be opened: %s"),
7152 uri
->values
[0].string
.text
, strerror(errno
));
7156 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
7160 con
->response
->request
.status
.status_code
= IPP_OK
;
7163 send_ipp_status(con
, IPP_NOT_FOUND
,
7164 _("The PPD file \"%s\" could not be found."),
7165 uri
->values
[0].string
.text
);
7170 * 'get_ppds()' - Get the list of PPD files on the local system.
7174 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
7176 http_status_t status
; /* Policy status */
7177 ipp_attribute_t
*limit
, /* Limit attribute */
7178 *device
, /* ppd-device-id attribute */
7179 *language
, /* ppd-natural-language attribute */
7180 *make
, /* ppd-make attribute */
7181 *model
, /* ppd-make-and-model attribute */
7182 *model_number
, /* ppd-model-number attribute */
7183 *product
, /* ppd-product attribute */
7184 *psversion
, /* ppd-psverion attribute */
7185 *type
, /* ppd-type attribute */
7186 *requested
; /* requested-attributes attribute */
7187 char command
[1024], /* cups-driverd command */
7188 options
[1024], /* Options to pass to command */
7189 device_str
[256],/* Escaped ppd-device-id string */
7191 /* Escaped ppd-natural-language */
7192 make_str
[256], /* Escaped ppd-make string */
7193 model_str
[256], /* Escaped ppd-make-and-model string */
7194 model_number_str
[256],
7195 /* ppd-model-number string */
7197 /* Escaped ppd-product string */
7199 /* Escaped ppd-psversion string */
7200 type_str
[256], /* Escaped ppd-type string */
7202 /* String for requested attributes */
7205 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
7211 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7213 send_http_error(con
, status
, NULL
);
7218 * Run cups-driverd command with the given options...
7221 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
7222 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
7223 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
7225 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
7226 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
7228 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
7230 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
7231 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
7232 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
7233 requested
= ippFindAttribute(con
->request
, "requested-attributes",
7237 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
7239 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
7242 url_encode_attr(device
, device_str
, sizeof(device_str
));
7244 device_str
[0] = '\0';
7247 url_encode_attr(language
, language_str
, sizeof(language_str
));
7249 language_str
[0] = '\0';
7252 url_encode_attr(make
, make_str
, sizeof(make_str
));
7257 url_encode_attr(model
, model_str
, sizeof(model_str
));
7259 model_str
[0] = '\0';
7262 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
7263 model_number
->values
[0].integer
);
7265 model_number_str
[0] = '\0';
7268 url_encode_attr(product
, product_str
, sizeof(product_str
));
7270 product_str
[0] = '\0';
7273 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
7275 psversion_str
[0] = '\0';
7278 url_encode_attr(type
, type_str
, sizeof(type_str
));
7282 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7283 snprintf(options
, sizeof(options
),
7284 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7285 con
->request
->request
.op
.request_id
,
7286 limit
? limit
->values
[0].integer
: 0,
7288 device
? "%20" : "", device_str
,
7289 language
? "%20" : "", language_str
,
7290 make
? "%20" : "", make_str
,
7291 model
? "%20" : "", model_str
,
7292 model_number
? "%20" : "", model_number_str
,
7293 product
? "%20" : "", product_str
,
7294 psversion
? "%20" : "", psversion_str
,
7295 type
? "%20" : "", type_str
);
7297 if (cupsdSendCommand(con
, command
, options
, 0))
7300 * Command started successfully, don't send an IPP response here...
7303 ippDelete(con
->response
);
7304 con
->response
= NULL
;
7309 * Command failed, return "internal error" so the user knows something
7313 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7314 _("cups-driverd failed to execute."));
7320 * 'get_printer_attrs()' - Get printer attributes.
7324 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
7325 ipp_attribute_t
*uri
) /* I - Printer URI */
7327 http_status_t status
; /* Policy status */
7328 cups_ptype_t dtype
; /* Destination type (printer/class) */
7329 cupsd_printer_t
*printer
; /* Printer/class */
7330 cups_array_t
*ra
; /* Requested attributes array */
7333 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
7334 con
->http
.fd
, uri
->values
[0].string
.text
);
7337 * Is the destination valid?
7340 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7346 send_ipp_status(con
, IPP_NOT_FOUND
,
7347 _("The printer or class was not found."));
7355 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7357 send_http_error(con
, status
, printer
);
7362 * Send the attributes...
7365 ra
= create_requested_array(con
->request
);
7367 copy_printer_attrs(con
, printer
, ra
);
7369 cupsArrayDelete(ra
);
7371 con
->response
->request
.status
.status_code
= IPP_OK
;
7376 * 'get_printers()' - Get a list of printers or classes.
7380 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7381 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7383 http_status_t status
; /* Policy status */
7384 ipp_attribute_t
*attr
; /* Current attribute */
7385 int limit
; /* Max number of printers to return */
7386 int count
; /* Number of printers that match */
7387 cupsd_printer_t
*printer
; /* Current printer pointer */
7388 int printer_type
, /* printer-type attribute */
7389 printer_mask
; /* printer-type-mask attribute */
7390 char *location
; /* Location string */
7391 const char *username
; /* Current user */
7392 char *first_printer_name
; /* first-printer-name attribute */
7393 cups_array_t
*ra
; /* Requested attributes array */
7396 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7397 con
->http
.fd
, type
);
7403 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7405 send_http_error(con
, status
, NULL
);
7410 * Check for printers...
7413 if (!Printers
|| !cupsArrayCount(Printers
))
7415 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7420 * See if they want to limit the number of printers reported...
7423 if ((attr
= ippFindAttribute(con
->request
, "limit",
7424 IPP_TAG_INTEGER
)) != NULL
)
7425 limit
= attr
->values
[0].integer
;
7429 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7430 IPP_TAG_NAME
)) != NULL
)
7431 first_printer_name
= attr
->values
[0].string
.text
;
7433 first_printer_name
= NULL
;
7436 * Support filtering...
7439 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7440 IPP_TAG_ENUM
)) != NULL
)
7441 printer_type
= attr
->values
[0].integer
;
7445 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7446 IPP_TAG_ENUM
)) != NULL
)
7447 printer_mask
= attr
->values
[0].integer
;
7451 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7452 IPP_TAG_TEXT
)) != NULL
)
7453 location
= attr
->values
[0].string
.text
;
7457 if (con
->username
[0])
7458 username
= con
->username
;
7459 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7460 IPP_TAG_NAME
)) != NULL
)
7461 username
= attr
->values
[0].string
.text
;
7465 ra
= create_requested_array(con
->request
);
7468 * OK, build a list of printers for this printer...
7471 if (first_printer_name
)
7473 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7474 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7477 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7480 count
< limit
&& printer
;
7481 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7483 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7484 (printer
->type
& printer_mask
) == printer_type
&&
7486 (printer
->location
&& !strcasecmp(printer
->location
, location
))))
7489 * If HideImplicitMembers is enabled, see if this printer or class
7490 * is a member of an implicit class...
7493 if (ImplicitClasses
&& HideImplicitMembers
&&
7494 printer
->in_implicit_class
)
7498 * If a username is specified, see if it is allowed or denied
7502 if (!(printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
7503 printer
->num_users
&& username
&& !user_allowed(printer
, username
))
7507 * Add the group separator as needed...
7511 ippAddSeparator(con
->response
);
7516 * Send the attributes...
7519 copy_printer_attrs(con
, printer
, ra
);
7523 cupsArrayDelete(ra
);
7525 con
->response
->request
.status
.status_code
= IPP_OK
;
7530 * 'get_subscription_attrs()' - Get subscription attributes.
7534 get_subscription_attrs(
7535 cupsd_client_t
*con
, /* I - Client connection */
7536 int sub_id
) /* I - Subscription ID */
7538 http_status_t status
; /* Policy status */
7539 cupsd_subscription_t
*sub
; /* Subscription */
7540 cups_array_t
*ra
; /* Requested attributes array */
7543 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7544 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7545 con
, con
->http
.fd
, sub_id
);
7548 * Is the subscription ID valid?
7551 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7554 * Bad subscription ID...
7557 send_ipp_status(con
, IPP_NOT_FOUND
,
7558 _("notify-subscription-id %d no good!"), sub_id
);
7566 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7568 con
, sub
->owner
)) != HTTP_OK
)
7570 send_http_error(con
, status
, sub
->dest
);
7575 * Copy the subscription attributes to the response using the
7576 * requested-attributes attribute that may be provided by the client.
7579 ra
= create_requested_array(con
->request
);
7581 copy_subscription_attrs(con
, sub
, ra
);
7583 cupsArrayDelete(ra
);
7585 con
->response
->request
.status
.status_code
= IPP_OK
;
7590 * 'get_subscriptions()' - Get subscriptions.
7594 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7595 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7597 http_status_t status
; /* Policy status */
7598 int count
; /* Number of subscriptions */
7599 int limit
; /* Limit */
7600 cupsd_subscription_t
*sub
; /* Subscription */
7601 cups_array_t
*ra
; /* Requested attributes array */
7602 ipp_attribute_t
*attr
; /* Attribute */
7603 cups_ptype_t dtype
; /* Destination type (printer/class) */
7604 char scheme
[HTTP_MAX_URI
],
7605 /* Scheme portion of URI */
7606 username
[HTTP_MAX_URI
],
7607 /* Username portion of URI */
7609 /* Host portion of URI */
7610 resource
[HTTP_MAX_URI
];
7611 /* Resource portion of URI */
7612 int port
; /* Port portion of URI */
7613 cupsd_job_t
*job
; /* Job pointer */
7614 cupsd_printer_t
*printer
; /* Printer */
7617 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7618 "get_subscriptions(con=%p[%d], uri=%s)",
7619 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
7622 * Is the destination valid?
7625 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7626 sizeof(scheme
), username
, sizeof(username
), host
,
7627 sizeof(host
), &port
, resource
, sizeof(resource
));
7629 if (!strcmp(resource
, "/") ||
7630 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7631 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7632 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7637 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7640 job
= cupsdFindJob(atoi(resource
+ 6));
7644 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
7649 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7655 send_ipp_status(con
, IPP_NOT_FOUND
,
7656 _("The printer or class was not found."));
7659 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7660 IPP_TAG_INTEGER
)) != NULL
)
7662 job
= cupsdFindJob(attr
->values
[0].integer
);
7666 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
7667 attr
->values
[0].integer
);
7678 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
7680 con
, NULL
)) != HTTP_OK
)
7682 send_http_error(con
, status
, printer
);
7687 * Copy the subscription attributes to the response using the
7688 * requested-attributes attribute that may be provided by the client.
7691 ra
= create_requested_array(con
->request
);
7693 if ((attr
= ippFindAttribute(con
->request
, "limit",
7694 IPP_TAG_INTEGER
)) != NULL
)
7695 limit
= attr
->values
[0].integer
;
7700 * See if we only want to see subscriptions for a specific user...
7703 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7704 IPP_TAG_BOOLEAN
)) != NULL
&&
7705 attr
->values
[0].boolean
)
7706 strlcpy(username
, get_username(con
), sizeof(username
));
7710 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7712 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7713 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7714 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
7716 ippAddSeparator(con
->response
);
7717 copy_subscription_attrs(con
, sub
, ra
);
7720 if (limit
&& count
>= limit
)
7724 cupsArrayDelete(ra
);
7727 con
->response
->request
.status
.status_code
= IPP_OK
;
7729 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7734 * 'get_username()' - Get the username associated with a request.
7737 static const char * /* O - Username */
7738 get_username(cupsd_client_t
*con
) /* I - Connection */
7740 ipp_attribute_t
*attr
; /* Attribute */
7743 if (con
->username
[0])
7744 return (con
->username
);
7745 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7746 IPP_TAG_NAME
)) != NULL
)
7747 return (attr
->values
[0].string
.text
);
7749 return ("anonymous");
7754 * 'hold_job()' - Hold a print job.
7758 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7759 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7761 ipp_attribute_t
*attr
, /* Current job-hold-until */
7762 *newattr
; /* New job-hold-until */
7763 int jobid
; /* Job ID */
7764 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7765 username
[HTTP_MAX_URI
], /* Username portion of URI */
7766 host
[HTTP_MAX_URI
], /* Host portion of URI */
7767 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7768 int port
; /* Port portion of URI */
7769 cupsd_job_t
*job
; /* Job information */
7772 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
7773 uri
->values
[0].string
.text
);
7776 * See if we have a job URI or a printer URI...
7779 if (!strcmp(uri
->name
, "printer-uri"))
7782 * Got a printer URI; see if we also have a job-id attribute...
7785 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7786 IPP_TAG_INTEGER
)) == NULL
)
7788 send_ipp_status(con
, IPP_BAD_REQUEST
,
7789 _("Got a printer-uri attribute but no job-id!"));
7793 jobid
= attr
->values
[0].integer
;
7798 * Got a job URI; parse it to get the job ID...
7801 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7802 sizeof(method
), username
, sizeof(username
), host
,
7803 sizeof(host
), &port
, resource
, sizeof(resource
));
7805 if (strncmp(resource
, "/jobs/", 6))
7811 send_ipp_status(con
, IPP_BAD_REQUEST
,
7812 _("Bad job-uri attribute \"%s\"!"),
7813 uri
->values
[0].string
.text
);
7817 jobid
= atoi(resource
+ 6);
7821 * See if the job exists...
7824 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7827 * Nope - return a "not found" error...
7830 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7835 * See if the job is owned by the requesting user...
7838 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7840 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7845 * Hold the job and return...
7850 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
7851 "Job held by user.");
7853 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
7854 IPP_TAG_KEYWORD
)) == NULL
)
7855 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7857 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7858 IPP_TAG_KEYWORD
)) == NULL
)
7859 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7864 * Free the old hold value and copy the new one over...
7867 _cupsStrFree(attr
->values
[0].string
.text
);
7871 attr
->value_tag
= newattr
->value_tag
;
7872 attr
->values
[0].string
.text
=
7873 _cupsStrAlloc(newattr
->values
[0].string
.text
);
7877 attr
->value_tag
= IPP_TAG_KEYWORD
;
7878 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
7882 * Hold job until specified time...
7885 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
7887 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
7888 "Job job-hold-until value changed by user.");
7891 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Held by \"%s\".", username
);
7893 con
->response
->request
.status
.status_code
= IPP_OK
;
7898 * 'move_job()' - Move a job to a new destination.
7902 move_job(cupsd_client_t
*con
, /* I - Client connection */
7903 ipp_attribute_t
*uri
) /* I - Job URI */
7905 http_status_t status
; /* Policy status */
7906 ipp_attribute_t
*attr
; /* Current attribute */
7907 int jobid
; /* Job ID */
7908 cupsd_job_t
*job
; /* Current job */
7909 const char *src
; /* Source printer/class */
7910 cups_ptype_t stype
, /* Source type (printer or class) */
7911 dtype
; /* Destination type (printer/class) */
7912 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7913 username
[HTTP_MAX_URI
], /* Username portion of URI */
7914 host
[HTTP_MAX_URI
], /* Host portion of URI */
7915 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7916 int port
; /* Port portion of URI */
7917 cupsd_printer_t
*sprinter
, /* Source printer */
7918 *dprinter
; /* Destination printer */
7921 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
7922 uri
->values
[0].string
.text
);
7925 * Get the new printer or class...
7928 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7929 IPP_TAG_URI
)) == NULL
)
7932 * Need job-printer-uri...
7935 send_ipp_status(con
, IPP_BAD_REQUEST
,
7936 _("job-printer-uri attribute missing!"));
7940 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7946 send_ipp_status(con
, IPP_NOT_FOUND
,
7947 _("The printer or class was not found."));
7952 * See if we have a job URI or a printer URI...
7955 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7956 sizeof(scheme
), username
, sizeof(username
), host
,
7957 sizeof(host
), &port
, resource
, sizeof(resource
));
7959 if (!strcmp(uri
->name
, "printer-uri"))
7962 * Got a printer URI; see if we also have a job-id attribute...
7965 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7966 IPP_TAG_INTEGER
)) == NULL
)
7972 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
7973 &sprinter
)) == NULL
)
7979 send_ipp_status(con
, IPP_NOT_FOUND
,
7980 _("The printer or class was not found."));
7989 * Otherwise, just move a single job...
7992 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7995 * Nope - return a "not found" error...
7998 send_ipp_status(con
, IPP_NOT_FOUND
,
7999 _("Job #%d does not exist!"), attr
->values
[0].integer
);
8005 * Job found, initialize source pointers...
8016 * Got a job URI; parse it to get the job ID...
8019 if (strncmp(resource
, "/jobs/", 6))
8025 send_ipp_status(con
, IPP_BAD_REQUEST
,
8026 _("Bad job-uri attribute \"%s\"!"),
8027 uri
->values
[0].string
.text
);
8032 * See if the job exists...
8035 jobid
= atoi(resource
+ 6);
8037 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8040 * Nope - return a "not found" error...
8043 send_ipp_status(con
, IPP_NOT_FOUND
,
8044 _("Job #%d does not exist!"), jobid
);
8050 * Job found, initialize source pointers...
8059 * Check the policy of the destination printer...
8062 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
8063 job
? job
->username
: NULL
)) != HTTP_OK
)
8065 send_http_error(con
, status
, dprinter
);
8070 * Now move the job or jobs...
8076 * See if the job has been completed...
8079 if (job
->state_value
> IPP_JOB_STOPPED
)
8082 * Return a "not-possible" error...
8085 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8086 _("Job #%d is finished and cannot be altered!"),
8092 * See if the job is owned by the requesting user...
8095 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8097 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8102 * Move the job to a different printer or class...
8105 cupsdMoveJob(job
, dprinter
);
8110 * Got the source printer, now look through the jobs...
8113 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
8115 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
8118 * See if the job is pointing at the source printer or has not been
8122 if (strcasecmp(job
->dest
, src
) ||
8123 job
->state_value
> IPP_JOB_STOPPED
)
8127 * See if the job can be moved by the requesting user...
8130 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8134 * Move the job to a different printer or class...
8137 cupsdMoveJob(job
, dprinter
);
8142 * Start jobs if possible...
8148 * Return with "everything is OK" status...
8151 con
->response
->request
.status
.status_code
= IPP_OK
;
8156 * 'ppd_parse_line()' - Parse a PPD default line.
8159 static int /* O - 0 on success, -1 on failure */
8160 ppd_parse_line(const char *line
, /* I - Line */
8161 char *option
, /* O - Option name */
8162 int olen
, /* I - Size of option name */
8163 char *choice
, /* O - Choice name */
8164 int clen
) /* I - Size of choice name */
8167 * Verify this is a default option line...
8170 if (strncmp(line
, "*Default", 8))
8174 * Read the option name...
8177 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
8187 * Skip everything else up to the colon (:)...
8190 while (*line
&& *line
!= ':')
8199 * Now grab the option choice, skipping leading whitespace...
8202 while (isspace(*line
& 255))
8205 for (clen
--; isalnum(*line
& 255); line
++)
8215 * Return with no errors...
8223 * 'print_job()' - Print a file to a printer or class.
8227 print_job(cupsd_client_t
*con
, /* I - Client connection */
8228 ipp_attribute_t
*uri
) /* I - Printer URI */
8230 ipp_attribute_t
*attr
; /* Current attribute */
8231 ipp_attribute_t
*format
; /* Document-format attribute */
8232 const char *default_format
; /* document-format-default value */
8233 cupsd_job_t
*job
; /* New job */
8234 char filename
[1024]; /* Job filename */
8235 mime_type_t
*filetype
; /* Type of file */
8236 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8237 type
[MIME_MAX_TYPE
], /* Subtype of file */
8238 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8239 /* Textual name of mime type */
8240 cupsd_printer_t
*printer
; /* Printer data */
8241 struct stat fileinfo
; /* File information */
8242 int kbytes
; /* Size of file */
8243 int compression
; /* Document compression */
8246 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
8247 uri
->values
[0].string
.text
);
8250 * Validate print file attributes, for now just document-format and
8251 * compression (CUPS only supports "none" and "gzip")...
8254 compression
= CUPS_FILE_NONE
;
8256 if ((attr
= ippFindAttribute(con
->request
, "compression",
8257 IPP_TAG_KEYWORD
)) != NULL
)
8259 if (strcmp(attr
->values
[0].string
.text
, "none")
8261 && strcmp(attr
->values
[0].string
.text
, "gzip")
8262 #endif /* HAVE_LIBZ */
8265 send_ipp_status(con
, IPP_ATTRIBUTES
,
8266 _("Unsupported compression \"%s\"!"),
8267 attr
->values
[0].string
.text
);
8268 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8269 "compression", NULL
, attr
->values
[0].string
.text
);
8274 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8275 compression
= CUPS_FILE_GZIP
;
8276 #endif /* HAVE_LIBZ */
8280 * Do we have a file to print?
8285 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
8290 * Is the destination valid?
8293 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8299 send_ipp_status(con
, IPP_NOT_FOUND
,
8300 _("The printer or class was not found."));
8305 * Is it a format we support?
8308 if ((format
= ippFindAttribute(con
->request
, "document-format",
8309 IPP_TAG_MIMETYPE
)) != NULL
)
8312 * Grab format from client...
8315 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
8318 send_ipp_status(con
, IPP_BAD_REQUEST
,
8319 _("Could not scan type \"%s\"!"),
8320 format
->values
[0].string
.text
);
8324 else if ((default_format
= cupsGetOption("document-format",
8325 printer
->num_options
,
8326 printer
->options
)) != NULL
)
8329 * Use default document format...
8332 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
8334 send_ipp_status(con
, IPP_BAD_REQUEST
,
8335 _("Could not scan type \"%s\"!"),
8346 strcpy(super
, "application");
8347 strcpy(type
, "octet-stream");
8350 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8353 * Auto-type the file...
8356 ipp_attribute_t
*doc_name
; /* document-name attribute */
8359 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8361 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8362 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8363 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8367 filetype
= mimeType(MimeDatabase
, super
, type
);
8369 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8370 filetype
->super
, filetype
->type
);
8373 filetype
= mimeType(MimeDatabase
, super
, type
);
8377 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8380 * Replace the document-format attribute value with the auto-typed or
8384 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8389 _cupsStrFree(format
->values
[0].string
.text
);
8391 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8394 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8395 "document-format", NULL
, mimetype
);
8399 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8400 _("Unsupported format \'%s/%s\'!"), super
, type
);
8401 cupsdLogMessage(CUPSD_LOG_INFO
,
8402 "Hint: Do you have the raw file printing rules enabled?");
8405 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8406 "document-format", NULL
, format
->values
[0].string
.text
);
8412 * Read any embedded job ticket info from PS files...
8415 if (!strcasecmp(filetype
->super
, "application") &&
8416 (!strcasecmp(filetype
->type
, "postscript") ||
8417 !strcasecmp(filetype
->type
, "pdf")))
8418 read_job_ticket(con
);
8421 * Create the job object...
8424 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8428 * Update quota data...
8431 if (stat(con
->filename
, &fileinfo
))
8434 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8436 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8438 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8439 IPP_TAG_INTEGER
)) != NULL
)
8440 attr
->values
[0].integer
+= kbytes
;
8443 * Add the job file...
8446 if (add_file(con
, job
, filetype
, compression
))
8449 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8451 rename(con
->filename
, filename
);
8452 cupsdClearString(&con
->filename
);
8455 * See if we need to add the ending sheet...
8458 if (cupsdTimeoutJob(job
))
8462 * Log and save the job...
8465 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8466 "File of type %s/%s queued by \"%s\".",
8467 filetype
->super
, filetype
->type
, job
->username
);
8468 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8469 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8470 job
->dest
, job
->username
);
8473 * Start the job if possible...
8481 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8483 * This function only gets called when printing a single PDF or PostScript
8484 * file using the Print-Job operation. It doesn't work for Create-Job +
8485 * Send-File, since the job attributes need to be set at job creation
8486 * time for banners to work. The embedded job ticket stuff is here
8487 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8488 * options and IPP attributes which otherwise would be lost.
8490 * The format of a job ticket is simple:
8492 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8494 * %cupsJobTicket: attr1=value1
8495 * %cupsJobTicket: attr2=value2
8497 * %cupsJobTicket: attrN=valueN
8499 * Job ticket lines must appear immediately after the first line that
8500 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8501 * stops looking for job ticket info when it finds a line that does not begin
8502 * with "%cupsJobTicket:".
8504 * The maximum length of a job ticket line, including the prefix, is
8505 * 255 characters to conform with the Adobe DSC.
8507 * Read-only attributes are rejected with a notice to the error log in
8508 * case a malicious user tries anything. Since the job ticket is read
8509 * prior to attribute validation in print_job(), job ticket attributes
8510 * will go through the same validation as IPP attributes...
8514 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8516 cups_file_t
*fp
; /* File to read from */
8517 char line
[256]; /* Line data */
8518 int num_options
; /* Number of options */
8519 cups_option_t
*options
; /* Options */
8520 ipp_t
*ticket
; /* New attributes */
8521 ipp_attribute_t
*attr
, /* Current attribute */
8522 *attr2
, /* Job attribute */
8523 *prev2
; /* Previous job attribute */
8527 * First open the print file...
8530 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8532 cupsdLogMessage(CUPSD_LOG_ERROR
,
8533 "Unable to open print file for job ticket - %s",
8539 * Skip the first line...
8542 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8544 cupsdLogMessage(CUPSD_LOG_ERROR
,
8545 "Unable to read from print file for job ticket - %s",
8551 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8554 * Not a DSC-compliant file, so no job ticket info will be available...
8562 * Read job ticket info from the file...
8568 while (cupsFileGets(fp
, line
, sizeof(line
)))
8571 * Stop at the first non-ticket line...
8574 if (strncmp(line
, "%cupsJobTicket:", 15))
8578 * Add the options to the option array...
8581 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8585 * Done with the file; see if we have any options...
8590 if (num_options
== 0)
8594 * OK, convert the options to an attribute list, and apply them to
8599 cupsEncodeOptions(ticket
, num_options
, options
);
8602 * See what the user wants to change.
8605 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8607 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8610 if (!strcmp(attr
->name
, "job-originating-host-name") ||
8611 !strcmp(attr
->name
, "job-originating-user-name") ||
8612 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8613 !strcmp(attr
->name
, "job-k-octets") ||
8614 !strcmp(attr
->name
, "job-id") ||
8615 !strncmp(attr
->name
, "job-state", 9) ||
8616 !strncmp(attr
->name
, "time-at-", 8))
8617 continue; /* Read-only attrs */
8619 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8620 IPP_TAG_ZERO
)) != NULL
)
8623 * Some other value; first free the old value...
8626 if (con
->request
->attrs
== attr2
)
8628 con
->request
->attrs
= attr2
->next
;
8633 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8634 if (prev2
->next
== attr2
)
8636 prev2
->next
= attr2
->next
;
8641 if (con
->request
->last
== attr2
)
8642 con
->request
->last
= prev2
;
8644 _ippFreeAttr(attr2
);
8648 * Add new option by copying it...
8651 copy_attribute(con
->request
, attr
, 0);
8655 * Then free the attribute list and option array...
8659 cupsFreeOptions(num_options
, options
);
8664 * 'reject_jobs()' - Reject print jobs to a printer.
8668 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8669 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8671 http_status_t status
; /* Policy status */
8672 cups_ptype_t dtype
; /* Destination type (printer/class) */
8673 cupsd_printer_t
*printer
; /* Printer data */
8674 ipp_attribute_t
*attr
; /* printer-state-message text */
8677 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8678 con
->http
.fd
, uri
->values
[0].string
.text
);
8681 * Is the destination valid?
8684 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8690 send_ipp_status(con
, IPP_NOT_FOUND
,
8691 _("The printer or class was not found."));
8699 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8701 send_http_error(con
, status
, printer
);
8706 * Reject jobs sent to the printer...
8709 printer
->accepting
= 0;
8711 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8712 IPP_TAG_TEXT
)) == NULL
)
8713 strcpy(printer
->state_message
, "Rejecting Jobs");
8715 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8716 sizeof(printer
->state_message
));
8718 cupsdAddPrinterHistory(printer
);
8720 if (dtype
& CUPS_PRINTER_CLASS
)
8722 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8724 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8725 printer
->name
, get_username(con
));
8729 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8731 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8732 printer
->name
, get_username(con
));
8736 * Everything was ok, so return OK status...
8739 con
->response
->request
.status
.status_code
= IPP_OK
;
8744 * 'release_job()' - Release a held print job.
8748 release_job(cupsd_client_t
*con
, /* I - Client connection */
8749 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8751 ipp_attribute_t
*attr
; /* Current attribute */
8752 int jobid
; /* Job ID */
8753 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8754 username
[HTTP_MAX_URI
], /* Username portion of URI */
8755 host
[HTTP_MAX_URI
], /* Host portion of URI */
8756 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8757 int port
; /* Port portion of URI */
8758 cupsd_job_t
*job
; /* Job information */
8761 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8762 con
->http
.fd
, uri
->values
[0].string
.text
);
8765 * See if we have a job URI or a printer URI...
8768 if (!strcmp(uri
->name
, "printer-uri"))
8771 * Got a printer URI; see if we also have a job-id attribute...
8774 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8775 IPP_TAG_INTEGER
)) == NULL
)
8777 send_ipp_status(con
, IPP_BAD_REQUEST
,
8778 _("Got a printer-uri attribute but no job-id!"));
8782 jobid
= attr
->values
[0].integer
;
8787 * Got a job URI; parse it to get the job ID...
8790 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8791 sizeof(method
), username
, sizeof(username
), host
,
8792 sizeof(host
), &port
, resource
, sizeof(resource
));
8794 if (strncmp(resource
, "/jobs/", 6))
8800 send_ipp_status(con
, IPP_BAD_REQUEST
,
8801 _("Bad job-uri attribute \"%s\"!"),
8802 uri
->values
[0].string
.text
);
8806 jobid
= atoi(resource
+ 6);
8810 * See if the job exists...
8813 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8816 * Nope - return a "not found" error...
8819 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8824 * See if job is "held"...
8827 if (job
->state_value
!= IPP_JOB_HELD
)
8830 * Nope - return a "not possible" error...
8833 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
8838 * See if the job is owned by the requesting user...
8841 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8843 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8848 * Reset the job-hold-until value to "no-hold"...
8851 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8852 IPP_TAG_KEYWORD
)) == NULL
)
8853 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8857 _cupsStrFree(attr
->values
[0].string
.text
);
8859 attr
->value_tag
= IPP_TAG_KEYWORD
;
8860 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
8862 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
8863 "Job job-hold-until value changed by user.");
8867 * Release the job and return...
8870 cupsdReleaseJob(job
);
8872 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
8873 "Job released by user.");
8875 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
8877 con
->response
->request
.status
.status_code
= IPP_OK
;
8884 * 'renew_subscription()' - Renew an existing subscription...
8889 cupsd_client_t
*con
, /* I - Client connection */
8890 int sub_id
) /* I - Subscription ID */
8892 http_status_t status
; /* Policy status */
8893 cupsd_subscription_t
*sub
; /* Subscription */
8894 ipp_attribute_t
*lease
; /* notify-lease-duration */
8897 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8898 "renew_subscription(con=%p[%d], sub_id=%d)",
8899 con
, con
->http
.fd
, sub_id
);
8902 * Is the subscription ID valid?
8905 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8908 * Bad subscription ID...
8911 send_ipp_status(con
, IPP_NOT_FOUND
,
8912 _("notify-subscription-id %d no good!"), sub_id
);
8919 * Job subscriptions cannot be renewed...
8922 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8923 _("Job subscriptions cannot be renewed!"));
8931 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8933 con
, sub
->owner
)) != HTTP_OK
)
8935 send_http_error(con
, status
, sub
->dest
);
8940 * Renew the subscription...
8943 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8946 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8948 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8950 cupsdLogMessage(CUPSD_LOG_INFO
,
8951 "renew_subscription: Limiting notify-lease-duration to "
8954 sub
->lease
= MaxLeaseDuration
;
8957 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
8959 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
8961 con
->response
->request
.status
.status_code
= IPP_OK
;
8963 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
8964 "notify-lease-duration", sub
->lease
);
8969 * 'restart_job()' - Restart an old print job.
8973 restart_job(cupsd_client_t
*con
, /* I - Client connection */
8974 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8976 ipp_attribute_t
*attr
; /* Current attribute */
8977 int jobid
; /* Job ID */
8978 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8979 username
[HTTP_MAX_URI
], /* Username portion of URI */
8980 host
[HTTP_MAX_URI
], /* Host portion of URI */
8981 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8982 int port
; /* Port portion of URI */
8983 cupsd_job_t
*job
; /* Job information */
8986 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
8987 con
->http
.fd
, uri
->values
[0].string
.text
);
8990 * See if we have a job URI or a printer URI...
8993 if (!strcmp(uri
->name
, "printer-uri"))
8996 * Got a printer URI; see if we also have a job-id attribute...
8999 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9000 IPP_TAG_INTEGER
)) == NULL
)
9002 send_ipp_status(con
, IPP_BAD_REQUEST
,
9003 _("Got a printer-uri attribute but no job-id!"));
9007 jobid
= attr
->values
[0].integer
;
9012 * Got a job URI; parse it to get the job ID...
9015 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9016 sizeof(method
), username
, sizeof(username
), host
,
9017 sizeof(host
), &port
, resource
, sizeof(resource
));
9019 if (strncmp(resource
, "/jobs/", 6))
9025 send_ipp_status(con
, IPP_BAD_REQUEST
,
9026 _("Bad job-uri attribute \"%s\"!"),
9027 uri
->values
[0].string
.text
);
9031 jobid
= atoi(resource
+ 6);
9035 * See if the job exists...
9038 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9041 * Nope - return a "not found" error...
9044 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9049 * See if job is in any of the "completed" states...
9052 if (job
->state_value
<= IPP_JOB_PROCESSING
)
9055 * Nope - return a "not possible" error...
9058 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
9064 * See if we have retained the job files...
9069 if (!job
->attrs
|| job
->num_files
== 0)
9072 * Nope - return a "not possible" error...
9075 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9076 _("Job #%d cannot be restarted - no files!"), jobid
);
9081 * See if the job is owned by the requesting user...
9084 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9086 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9091 * Restart the job and return...
9094 cupsdRestartJob(job
);
9096 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
9098 con
->response
->request
.status
.status_code
= IPP_OK
;
9103 * 'save_auth_info()' - Save authentication information for a job.
9108 cupsd_client_t
*con
, /* I - Client connection */
9109 cupsd_job_t
*job
, /* I - Job */
9110 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
9112 int i
; /* Looping var */
9113 char filename
[1024]; /* Job authentication filename */
9114 cups_file_t
*fp
; /* Job authentication file */
9115 char line
[2048]; /* Line for file */
9116 cupsd_printer_t
*dest
; /* Destination printer/class */
9120 * This function saves the in-memory authentication information for
9121 * a job so that it can be used to authenticate with a remote host.
9122 * The information is stored in a file that is readable only by the
9123 * root user. The fields are Base-64 encoded, each on a separate line,
9124 * followed by random number (up to 1024) of newlines to limit the
9125 * amount of information that is exposed.
9127 * Because of the potential for exposing of authentication information,
9128 * this functionality is only enabled when running cupsd as root.
9130 * This caching only works for the Basic and BasicDigest authentication
9131 * types. Digest authentication cannot be cached this way, and in
9132 * the future Kerberos authentication may make all of this obsolete.
9134 * Authentication information is saved whenever an authenticated
9135 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9138 * This information is deleted after a job is completed or canceled,
9139 * so reprints may require subsequent re-authentication.
9145 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9149 * Create the authentication file and change permissions...
9152 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9153 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9155 cupsdLogMessage(CUPSD_LOG_ERROR
,
9156 "Unable to save authentication info to \"%s\" - %s",
9157 filename
, strerror(errno
));
9161 fchown(cupsFileNumber(fp
), 0, 0);
9162 fchmod(cupsFileNumber(fp
), 0400);
9164 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9167 * Write 1 to 3 auth values...
9170 cupsdClearString(&job
->auth_username
);
9171 cupsdClearString(&job
->auth_domain
);
9172 cupsdClearString(&job
->auth_password
);
9174 for (i
= 0; i
< auth_info
->num_values
; i
++)
9176 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
9177 strlen(auth_info
->values
[i
].string
.text
));
9178 cupsFilePrintf(fp
, "%s\n", line
);
9180 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9181 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s",
9182 auth_info
->values
[i
].string
.text
);
9183 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9184 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s",
9185 auth_info
->values
[i
].string
.text
);
9186 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9187 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s",
9188 auth_info
->values
[i
].string
.text
);
9191 else if (con
->username
[0])
9194 * Write the authenticated username...
9197 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
9198 cupsFilePrintf(fp
, "%s\n", line
);
9200 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", con
->username
);
9201 cupsdClearString(&job
->auth_domain
);
9204 * Write the authenticated password...
9207 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
9208 cupsFilePrintf(fp
, "%s\n", line
);
9210 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", con
->password
);
9214 * Write a random number of newlines to the end of the file...
9217 for (i
= (rand() % 1024); i
>= 0; i
--)
9218 cupsFilePutChar(fp
, '\n');
9221 * Close the file and return...
9226 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9227 if (con
->gss_have_creds
)
9228 save_krb5_creds(con
, job
);
9229 else if (job
->ccname
)
9230 cupsdClearString(&(job
->ccname
));
9231 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9235 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9237 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
9241 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
9242 cupsd_job_t
*job
) /* I - Job */
9244 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
9245 cupsdLogMessage(CUPSD_LOG_INFO
,
9246 "Sorry, your version of Kerberos does not support delegated "
9251 krb5_error_code error
; /* Kerberos error code */
9252 OM_uint32 major_status
, /* Major status code */
9253 minor_status
; /* Minor status code */
9254 krb5_principal principal
; /* Kerberos principal */
9259 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
9263 if (krb5_init_context
== NULL
)
9265 # endif /* __APPLE__ */
9267 if (!KerberosInitialized
)
9270 * Setup a Kerberos context for the scheduler to use...
9273 KerberosInitialized
= 1;
9275 if (krb5_init_context(&KerberosContext
))
9277 KerberosContext
= NULL
;
9279 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize Kerberos context");
9285 * We MUST create a file-based cache because memory-based caches are
9286 * only valid for the current process/address space.
9288 * Due to various bugs/features in different versions of Kerberos, we
9289 * need either the krb5_cc_new_unique() function or Heimdal's version
9290 * of krb5_cc_gen_new() to create a new FILE: credential cache that
9291 * can be passed to the backend. These functions create a temporary
9292 * file (typically in /tmp) containing the cached credentials, which
9293 * are removed when we have successfully printed a job.
9296 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
9297 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
9298 &(job
->ccache
))) != 0)
9299 # else /* HAVE_HEIMDAL */
9300 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
9301 &(job
->ccache
))) != 0)
9302 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
9304 cupsdLogMessage(CUPSD_LOG_ERROR
,
9305 "Unable to create new credentials cache (%d/%s)",
9306 error
, strerror(errno
));
9311 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
9313 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to parse kerberos username (%d/%s)",
9314 error
, strerror(errno
));
9315 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9320 if ((error
= krb5_cc_initialize(KerberosContext
, job
->ccache
, principal
)))
9322 cupsdLogMessage(CUPSD_LOG_ERROR
,
9323 "Unable to initialize credentials cache (%d/%s)", error
,
9325 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9326 krb5_free_principal(KerberosContext
, principal
);
9331 krb5_free_principal(KerberosContext
, principal
);
9334 * Copy the user's credentials to the new cache file...
9337 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
9340 if (GSS_ERROR(major_status
))
9342 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
9343 "Unable to import client credentials cache");
9344 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9350 * Add the KRB5CCNAME environment variable to the job so that the
9351 * backend can use the credentials when printing.
9354 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
9355 krb5_cc_get_name(KerberosContext
, job
->ccache
));
9357 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "save_krb5_creds: %s", job
->ccname
);
9358 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
9360 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9364 * 'send_document()' - Send a file to a printer or class.
9368 send_document(cupsd_client_t
*con
, /* I - Client connection */
9369 ipp_attribute_t
*uri
) /* I - Printer URI */
9371 ipp_attribute_t
*attr
; /* Current attribute */
9372 ipp_attribute_t
*format
; /* Request's document-format attribute */
9373 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9374 const char *default_format
;/* document-format-default value */
9375 int jobid
; /* Job ID number */
9376 cupsd_job_t
*job
; /* Current job */
9377 char job_uri
[HTTP_MAX_URI
],
9379 method
[HTTP_MAX_URI
],
9380 /* Method portion of URI */
9381 username
[HTTP_MAX_URI
],
9382 /* Username portion of URI */
9384 /* Host portion of URI */
9385 resource
[HTTP_MAX_URI
];
9386 /* Resource portion of URI */
9387 int port
; /* Port portion of URI */
9388 mime_type_t
*filetype
; /* Type of file */
9389 char super
[MIME_MAX_SUPER
],
9390 /* Supertype of file */
9391 type
[MIME_MAX_TYPE
],
9392 /* Subtype of file */
9393 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9394 /* Textual name of mime type */
9395 char filename
[1024]; /* Job filename */
9396 cupsd_printer_t
*printer
; /* Current printer */
9397 struct stat fileinfo
; /* File information */
9398 int kbytes
; /* Size of file */
9399 int compression
; /* Type of compression */
9400 int start_job
; /* Start the job? */
9403 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9404 con
->http
.fd
, uri
->values
[0].string
.text
);
9407 * See if we have a job URI or a printer URI...
9410 if (!strcmp(uri
->name
, "printer-uri"))
9413 * Got a printer URI; see if we also have a job-id attribute...
9416 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9417 IPP_TAG_INTEGER
)) == NULL
)
9419 send_ipp_status(con
, IPP_BAD_REQUEST
,
9420 _("Got a printer-uri attribute but no job-id!"));
9424 jobid
= attr
->values
[0].integer
;
9429 * Got a job URI; parse it to get the job ID...
9432 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9433 sizeof(method
), username
, sizeof(username
), host
,
9434 sizeof(host
), &port
, resource
, sizeof(resource
));
9436 if (strncmp(resource
, "/jobs/", 6))
9442 send_ipp_status(con
, IPP_BAD_REQUEST
,
9443 _("Bad job-uri attribute \"%s\"!"),
9444 uri
->values
[0].string
.text
);
9448 jobid
= atoi(resource
+ 6);
9452 * See if the job exists...
9455 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9458 * Nope - return a "not found" error...
9461 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9465 printer
= cupsdFindDest(job
->dest
);
9468 * See if the job is owned by the requesting user...
9471 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9473 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9478 * OK, see if the client is sending the document compressed - CUPS
9479 * only supports "none" and "gzip".
9482 compression
= CUPS_FILE_NONE
;
9484 if ((attr
= ippFindAttribute(con
->request
, "compression",
9485 IPP_TAG_KEYWORD
)) != NULL
)
9487 if (strcmp(attr
->values
[0].string
.text
, "none")
9489 && strcmp(attr
->values
[0].string
.text
, "gzip")
9490 #endif /* HAVE_LIBZ */
9493 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
9494 attr
->values
[0].string
.text
);
9495 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9496 "compression", NULL
, attr
->values
[0].string
.text
);
9501 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9502 compression
= CUPS_FILE_GZIP
;
9503 #endif /* HAVE_LIBZ */
9507 * Do we have a file to print?
9512 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
9517 * Is it a format we support?
9520 if ((format
= ippFindAttribute(con
->request
, "document-format",
9521 IPP_TAG_MIMETYPE
)) != NULL
)
9524 * Grab format from client...
9527 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
9530 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9531 format
->values
[0].string
.text
);
9535 else if ((default_format
= cupsGetOption("document-format",
9536 printer
->num_options
,
9537 printer
->options
)) != NULL
)
9540 * Use default document format...
9543 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
9545 send_ipp_status(con
, IPP_BAD_REQUEST
,
9546 _("Could not scan type \"%s\"!"),
9554 * No document format attribute? Auto-type it!
9557 strcpy(super
, "application");
9558 strcpy(type
, "octet-stream");
9561 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9564 * Auto-type the file...
9567 ipp_attribute_t
*doc_name
; /* document-name attribute */
9570 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9572 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9573 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9574 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9578 filetype
= mimeType(MimeDatabase
, super
, type
);
9580 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9581 filetype
->super
, filetype
->type
);
9584 filetype
= mimeType(MimeDatabase
, super
, type
);
9589 * Replace the document-format attribute value with the auto-typed or
9593 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9596 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9597 IPP_TAG_MIMETYPE
)) != NULL
)
9599 _cupsStrFree(jformat
->values
[0].string
.text
);
9601 jformat
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
9604 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9605 "document-format", NULL
, mimetype
);
9609 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9610 _("Unsupported format \'%s/%s\'!"), super
, type
);
9611 cupsdLogMessage(CUPSD_LOG_INFO
,
9612 "Hint: Do you have the raw file printing rules enabled?");
9615 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9616 "document-format", NULL
, format
->values
[0].string
.text
);
9621 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9623 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9626 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9627 _("Unsupported format \'%s\'!"), mimetype
);
9629 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9630 "document-format", NULL
, mimetype
);
9636 * Add the file to the job...
9641 if (add_file(con
, job
, filetype
, compression
))
9644 if (stat(con
->filename
, &fileinfo
))
9647 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9649 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9651 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
9652 IPP_TAG_INTEGER
)) != NULL
)
9653 attr
->values
[0].integer
+= kbytes
;
9655 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
9657 rename(con
->filename
, filename
);
9659 cupsdClearString(&con
->filename
);
9661 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9662 filetype
->super
, filetype
->type
, job
->username
);
9665 * Start the job if this is the last document...
9668 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9669 IPP_TAG_BOOLEAN
)) != NULL
&&
9670 attr
->values
[0].boolean
)
9673 * See if we need to add the ending sheet...
9676 if (cupsdTimeoutJob(job
))
9679 if (job
->state_value
== IPP_JOB_STOPPED
)
9681 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9682 job
->state_value
= IPP_JOB_PENDING
;
9684 else if (job
->state_value
== IPP_JOB_HELD
)
9686 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9687 IPP_TAG_KEYWORD
)) == NULL
)
9688 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9690 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9692 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9693 job
->state_value
= IPP_JOB_PENDING
;
9698 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9704 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9705 IPP_TAG_KEYWORD
)) == NULL
)
9706 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9708 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9710 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9711 job
->state_value
= IPP_JOB_HELD
;
9712 job
->hold_until
= time(NULL
) + 60;
9715 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9722 * Fill in the response info...
9725 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
9728 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9731 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9733 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9735 add_job_state_reasons(con
, job
);
9737 con
->response
->request
.status
.status_code
= IPP_OK
;
9740 * Start the job if necessary...
9749 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9754 cupsd_client_t
*con
, /* I - Client connection */
9755 http_status_t status
, /* I - HTTP status code */
9756 cupsd_printer_t
*printer
) /* I - Printer, if any */
9758 ipp_attribute_t
*uri
; /* Request URI, if any */
9761 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
9762 IPP_TAG_URI
)) == NULL
)
9763 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
9765 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
9766 "Returning HTTP %s for %s (%s) from %s",
9768 ippOpString(con
->request
->request
.op
.operation_id
),
9769 uri
? uri
->values
[0].string
.text
: "no URI",
9770 con
->http
.hostname
);
9774 int auth_type
; /* Type of authentication required */
9777 auth_type
= CUPSD_AUTH_NONE
;
9779 if (status
== HTTP_UNAUTHORIZED
&&
9780 printer
->num_auth_info_required
> 0 &&
9781 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
9783 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
9784 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
9785 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
9788 * Creating and authenticating jobs requires Kerberos...
9791 auth_type
= CUPSD_AUTH_NEGOTIATE
;
9796 * Use policy/location-defined authentication requirements...
9799 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9800 cupsd_location_t
*auth
; /* Pointer to authentication element */
9803 if (printer
->type
& CUPS_PRINTER_CLASS
)
9804 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9806 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9808 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9809 auth
->type
== CUPSD_AUTH_NONE
)
9810 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
9812 con
->request
->request
.op
.operation_id
:
9817 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
9818 auth_type
= DefaultAuthType
;
9820 auth_type
= auth
->type
;
9824 cupsdSendError(con
, status
, auth_type
);
9827 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
9829 ippDelete(con
->response
);
9830 con
->response
= NULL
;
9837 * 'send_ipp_status()' - Send a status back to the IPP client.
9841 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9842 ipp_status_t status
, /* I - IPP status code */
9843 const char *message
,/* I - Status message */
9844 ...) /* I - Additional args as needed */
9846 va_list ap
; /* Pointer to additional args */
9847 char formatted
[1024]; /* Formatted errror message */
9850 va_start(ap
, message
);
9851 vsnprintf(formatted
, sizeof(formatted
),
9852 _cupsLangString(con
->language
, message
), ap
);
9855 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9856 ippOpString(con
->request
->request
.op
.operation_id
),
9857 ippErrorString(status
), formatted
);
9859 con
->response
->request
.status
.status_code
= status
;
9861 if (ippFindAttribute(con
->response
, "attributes-charset",
9862 IPP_TAG_ZERO
) == NULL
)
9863 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9864 "attributes-charset", NULL
, DefaultCharset
);
9866 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9867 IPP_TAG_ZERO
) == NULL
)
9868 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9869 "attributes-natural-language", NULL
, DefaultLanguage
);
9871 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9872 "status-message", NULL
, formatted
);
9877 * 'set_default()' - Set the default destination...
9881 set_default(cupsd_client_t
*con
, /* I - Client connection */
9882 ipp_attribute_t
*uri
) /* I - Printer URI */
9884 http_status_t status
; /* Policy status */
9885 cups_ptype_t dtype
; /* Destination type (printer/class) */
9886 cupsd_printer_t
*printer
, /* Printer */
9887 *oldprinter
; /* Old default printer */
9890 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9891 con
->http
.fd
, uri
->values
[0].string
.text
);
9894 * Is the destination valid?
9897 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9903 send_ipp_status(con
, IPP_NOT_FOUND
,
9904 _("The printer or class was not found."));
9912 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9914 send_http_error(con
, status
, NULL
);
9919 * Set it as the default...
9922 oldprinter
= DefaultPrinter
;
9923 DefaultPrinter
= printer
;
9926 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
9927 "%s is no longer the default printer.", oldprinter
->name
);
9929 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
9930 "%s is now the default printer.", printer
->name
);
9932 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
9933 CUPSD_DIRTY_REMOTE
| CUPSD_DIRTY_PRINTCAP
);
9935 cupsdLogMessage(CUPSD_LOG_INFO
,
9936 "Default destination set to \"%s\" by \"%s\".",
9937 printer
->name
, get_username(con
));
9940 * Everything was ok, so return OK status...
9943 con
->response
->request
.status
.status_code
= IPP_OK
;
9948 * 'set_job_attrs()' - Set job attributes.
9952 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9953 ipp_attribute_t
*uri
) /* I - Job URI */
9955 ipp_attribute_t
*attr
, /* Current attribute */
9956 *attr2
; /* Job attribute */
9957 int jobid
; /* Job ID */
9958 cupsd_job_t
*job
; /* Current job */
9959 char method
[HTTP_MAX_URI
],
9960 /* Method portion of URI */
9961 username
[HTTP_MAX_URI
],
9962 /* Username portion of URI */
9964 /* Host portion of URI */
9965 resource
[HTTP_MAX_URI
];
9966 /* Resource portion of URI */
9967 int port
; /* Port portion of URI */
9968 int event
; /* Events? */
9969 int check_jobs
; /* Check jobs? */
9972 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
9973 con
->http
.fd
, uri
->values
[0].string
.text
);
9976 * Start with "everything is OK" status...
9979 con
->response
->request
.status
.status_code
= IPP_OK
;
9982 * See if we have a job URI or a printer URI...
9985 if (!strcmp(uri
->name
, "printer-uri"))
9988 * Got a printer URI; see if we also have a job-id attribute...
9991 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9992 IPP_TAG_INTEGER
)) == NULL
)
9994 send_ipp_status(con
, IPP_BAD_REQUEST
,
9995 _("Got a printer-uri attribute but no job-id!"));
9999 jobid
= attr
->values
[0].integer
;
10004 * Got a job URI; parse it to get the job ID...
10007 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
10008 sizeof(method
), username
, sizeof(username
), host
,
10009 sizeof(host
), &port
, resource
, sizeof(resource
));
10011 if (strncmp(resource
, "/jobs/", 6))
10017 send_ipp_status(con
, IPP_BAD_REQUEST
,
10018 _("Bad job-uri attribute \"%s\"!"),
10019 uri
->values
[0].string
.text
);
10023 jobid
= atoi(resource
+ 6);
10027 * See if the job exists...
10030 if ((job
= cupsdFindJob(jobid
)) == NULL
)
10033 * Nope - return a "not found" error...
10036 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
10041 * See if the job has been completed...
10044 if (job
->state_value
> IPP_JOB_STOPPED
)
10047 * Return a "not-possible" error...
10050 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10051 _("Job #%d is finished and cannot be altered!"), jobid
);
10056 * See if the job is owned by the requesting user...
10059 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
10061 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
10066 * See what the user wants to change.
10074 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10076 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
10079 if (!strcmp(attr
->name
, "attributes-charset") ||
10080 !strcmp(attr
->name
, "attributes-natural-language") ||
10081 !strcmp(attr
->name
, "document-compression") ||
10082 !strcmp(attr
->name
, "document-format") ||
10083 !strcmp(attr
->name
, "job-detailed-status-messages") ||
10084 !strcmp(attr
->name
, "job-document-access-errors") ||
10085 !strcmp(attr
->name
, "job-id") ||
10086 !strcmp(attr
->name
, "job-impressions-completed") ||
10087 !strcmp(attr
->name
, "job-k-octets") ||
10088 !strcmp(attr
->name
, "job-originating-host-name") ||
10089 !strcmp(attr
->name
, "job-originating-user-name") ||
10090 !strcmp(attr
->name
, "job-printer-up-time") ||
10091 !strcmp(attr
->name
, "job-printer-uri") ||
10092 !strcmp(attr
->name
, "job-sheets") ||
10093 !strcmp(attr
->name
, "job-state-message") ||
10094 !strcmp(attr
->name
, "job-state-reasons") ||
10095 !strcmp(attr
->name
, "job-uri") ||
10096 !strcmp(attr
->name
, "number-of-documents") ||
10097 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
10098 !strcmp(attr
->name
, "output-device-assigned") ||
10099 !strncmp(attr
->name
, "date-time-at-", 13) ||
10100 !strncmp(attr
->name
, "job-k-octets", 12) ||
10101 !strncmp(attr
->name
, "job-media-sheets", 16) ||
10102 !strncmp(attr
->name
, "time-at-", 8))
10108 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
10109 _("%s cannot be changed."), attr
->name
);
10111 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10112 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10117 if (!strcmp(attr
->name
, "job-priority"))
10120 * Change the job priority...
10123 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10125 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
10127 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10128 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10130 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
10132 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10133 _("Job is completed and cannot be changed."));
10136 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10138 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
10139 attr
->values
[0].integer
);
10140 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
10143 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
10144 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
10147 else if (!strcmp(attr
->name
, "job-state"))
10150 * Change the job state...
10153 if (attr
->value_tag
!= IPP_TAG_ENUM
)
10155 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
10157 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10158 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10162 switch (attr
->values
[0].integer
)
10164 case IPP_JOB_PENDING
:
10165 case IPP_JOB_HELD
:
10166 if (job
->state_value
> IPP_JOB_HELD
)
10168 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10169 _("Job state cannot be changed."));
10172 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10174 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10175 attr
->values
[0].integer
);
10177 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
10178 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
10180 event
|= CUPSD_EVENT_JOB_STATE
;
10185 case IPP_JOB_PROCESSING
:
10186 case IPP_JOB_STOPPED
:
10187 if (job
->state_value
!= attr
->values
[0].integer
)
10189 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10190 _("Job state cannot be changed."));
10195 case IPP_JOB_CANCELED
:
10196 case IPP_JOB_ABORTED
:
10197 case IPP_JOB_COMPLETED
:
10198 if (job
->state_value
> IPP_JOB_PROCESSING
)
10200 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10201 _("Job state cannot be changed."));
10204 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10206 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10207 attr
->values
[0].integer
);
10208 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
10216 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10218 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10219 IPP_TAG_ZERO
)) != NULL
)
10222 * Some other value; first free the old value...
10225 if (job
->attrs
->prev
)
10226 job
->attrs
->prev
->next
= attr2
->next
;
10228 job
->attrs
->attrs
= attr2
->next
;
10230 if (job
->attrs
->last
== attr2
)
10231 job
->attrs
->last
= job
->attrs
->prev
;
10233 _ippFreeAttr(attr2
);
10236 * Then copy the attribute...
10239 copy_attribute(job
->attrs
, attr
, 0);
10242 * See if the job-name or job-hold-until is being changed.
10245 if (!strcmp(attr
->name
, "job-hold-until"))
10247 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10248 attr
->values
[0].string
.text
);
10249 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
10251 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10252 cupsdReleaseJob(job
);
10257 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10260 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10263 * Delete the attribute...
10266 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10267 IPP_TAG_ZERO
)) != NULL
)
10269 if (job
->attrs
->prev
)
10270 job
->attrs
->prev
->next
= attr2
->next
;
10272 job
->attrs
->attrs
= attr2
->next
;
10274 if (attr2
== job
->attrs
->last
)
10275 job
->attrs
->last
= job
->attrs
->prev
;
10277 _ippFreeAttr(attr2
);
10279 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10285 * Add new option by copying it...
10288 copy_attribute(job
->attrs
, attr
, 0);
10290 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10299 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10302 * Send events as needed...
10305 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10306 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10307 cupsdFindDest(job
->dest
), job
,
10308 "Job priority changed by user.");
10310 if (event
& CUPSD_EVENT_JOB_STATE
)
10311 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10312 job
->state_value
== IPP_JOB_HELD
?
10313 "Job held by user." : "Job restarted by user.");
10315 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10316 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10317 "Job options changed by user.");
10320 * Start jobs if possible...
10329 * 'set_printer_defaults()' - Set printer default options from a request.
10333 set_printer_defaults(
10334 cupsd_client_t
*con
, /* I - Client connection */
10335 cupsd_printer_t
*printer
) /* I - Printer */
10337 int i
; /* Looping var */
10338 ipp_attribute_t
*attr
; /* Current attribute */
10339 int namelen
; /* Length of attribute name */
10340 char name
[256], /* New attribute name */
10341 value
[256]; /* String version of integer attrs */
10344 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10347 * Skip non-printer attributes...
10350 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10353 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10355 if (!strcmp(attr
->name
, "job-sheets-default"))
10358 * Only allow keywords and names...
10361 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10365 * Only allow job-sheets-default to be set when running without a
10366 * system high classification level...
10369 if (Classification
)
10372 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10374 if (attr
->num_values
> 1)
10375 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10377 cupsdSetString(&printer
->job_sheets
[1], "none");
10379 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10381 cupsdFreePrinterUsers(printer
);
10383 printer
->deny_users
= 0;
10385 if (attr
->value_tag
== IPP_TAG_NAME
&&
10386 (attr
->num_values
> 1 ||
10387 strcmp(attr
->values
[0].string
.text
, "all")))
10389 for (i
= 0; i
< attr
->num_values
; i
++)
10390 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
10393 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10395 cupsdFreePrinterUsers(printer
);
10397 printer
->deny_users
= 1;
10399 if (attr
->value_tag
== IPP_TAG_NAME
&&
10400 (attr
->num_values
> 1 ||
10401 strcmp(attr
->values
[0].string
.text
, "none")))
10403 for (i
= 0; i
< attr
->num_values
; i
++)
10404 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
10407 else if (!strcmp(attr
->name
, "job-quota-period"))
10409 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10412 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10413 attr
->values
[0].integer
);
10414 cupsdFreeQuotas(printer
);
10416 printer
->quota_period
= attr
->values
[0].integer
;
10418 else if (!strcmp(attr
->name
, "job-k-limit"))
10420 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10423 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10424 attr
->values
[0].integer
);
10425 cupsdFreeQuotas(printer
);
10427 printer
->k_limit
= attr
->values
[0].integer
;
10429 else if (!strcmp(attr
->name
, "job-page-limit"))
10431 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10434 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10435 attr
->values
[0].integer
);
10436 cupsdFreeQuotas(printer
);
10438 printer
->page_limit
= attr
->values
[0].integer
;
10440 else if (!strcmp(attr
->name
, "printer-op-policy"))
10442 cupsd_policy_t
*p
; /* Policy */
10445 if (attr
->value_tag
!= IPP_TAG_NAME
)
10448 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10450 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10451 "Setting printer-op-policy to \"%s\"...",
10452 attr
->values
[0].string
.text
);
10453 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10454 printer
->op_policy_ptr
= p
;
10458 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10459 _("Unknown printer-op-policy \"%s\"."),
10460 attr
->values
[0].string
.text
);
10464 else if (!strcmp(attr
->name
, "printer-error-policy"))
10466 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10469 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10470 strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10471 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10472 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
10474 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10475 _("Unknown printer-error-policy \"%s\"."),
10476 attr
->values
[0].string
.text
);
10480 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10481 "Setting printer-error-policy to \"%s\"...",
10482 attr
->values
[0].string
.text
);
10483 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10487 * Skip any other non-default attributes...
10490 namelen
= strlen(attr
->name
);
10491 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10492 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10496 * OK, anything else must be a user-defined default...
10499 strlcpy(name
, attr
->name
, sizeof(name
));
10500 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10502 switch (attr
->value_tag
)
10504 case IPP_TAG_DELETEATTR
:
10505 printer
->num_options
= cupsRemoveOption(name
,
10506 printer
->num_options
,
10507 &(printer
->options
));
10508 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10509 "Deleting %s", attr
->name
);
10512 case IPP_TAG_NAME
:
10513 case IPP_TAG_KEYWORD
:
10515 printer
->num_options
= cupsAddOption(name
,
10516 attr
->values
[0].string
.text
,
10517 printer
->num_options
,
10518 &(printer
->options
));
10519 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10520 "Setting %s to \"%s\"...", attr
->name
,
10521 attr
->values
[0].string
.text
);
10524 case IPP_TAG_BOOLEAN
:
10525 printer
->num_options
= cupsAddOption(name
,
10526 attr
->values
[0].boolean
?
10528 printer
->num_options
,
10529 &(printer
->options
));
10530 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10531 "Setting %s to %s...", attr
->name
,
10532 attr
->values
[0].boolean
? "true" : "false");
10535 case IPP_TAG_INTEGER
:
10536 case IPP_TAG_ENUM
:
10537 sprintf(value
, "%d", attr
->values
[0].integer
);
10538 printer
->num_options
= cupsAddOption(name
, value
,
10539 printer
->num_options
,
10540 &(printer
->options
));
10541 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10542 "Setting %s to %s...", attr
->name
, value
);
10545 case IPP_TAG_RANGE
:
10546 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10547 attr
->values
[0].range
.upper
);
10548 printer
->num_options
= cupsAddOption(name
, value
,
10549 printer
->num_options
,
10550 &(printer
->options
));
10551 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10552 "Setting %s to %s...", attr
->name
, value
);
10555 case IPP_TAG_RESOLUTION
:
10556 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10557 attr
->values
[0].resolution
.yres
,
10558 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10560 printer
->num_options
= cupsAddOption(name
, value
,
10561 printer
->num_options
,
10562 &(printer
->options
));
10563 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10564 "Setting %s to %s...", attr
->name
, value
);
10568 /* Do nothing for other values */
10576 * 'start_printer()' - Start a printer.
10580 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10581 ipp_attribute_t
*uri
) /* I - Printer URI */
10583 http_status_t status
; /* Policy status */
10584 cups_ptype_t dtype
; /* Destination type (printer/class) */
10585 cupsd_printer_t
*printer
; /* Printer data */
10588 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10589 con
->http
.fd
, uri
->values
[0].string
.text
);
10592 * Is the destination valid?
10595 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10601 send_ipp_status(con
, IPP_NOT_FOUND
,
10602 _("The printer or class was not found."));
10610 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10612 send_http_error(con
, status
, printer
);
10617 * Start the printer...
10620 printer
->state_message
[0] = '\0';
10622 cupsdStartPrinter(printer
, 1);
10624 if (dtype
& CUPS_PRINTER_CLASS
)
10625 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10626 printer
->name
, get_username(con
));
10628 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10629 printer
->name
, get_username(con
));
10634 * Everything was ok, so return OK status...
10637 con
->response
->request
.status
.status_code
= IPP_OK
;
10642 * 'stop_printer()' - Stop a printer.
10646 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10647 ipp_attribute_t
*uri
) /* I - Printer URI */
10649 http_status_t status
; /* Policy status */
10650 cups_ptype_t dtype
; /* Destination type (printer/class) */
10651 cupsd_printer_t
*printer
; /* Printer data */
10652 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10655 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10656 con
->http
.fd
, uri
->values
[0].string
.text
);
10659 * Is the destination valid?
10662 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10668 send_ipp_status(con
, IPP_NOT_FOUND
,
10669 _("The printer or class was not found."));
10677 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10679 send_http_error(con
, status
, printer
);
10684 * Stop the printer...
10687 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10688 IPP_TAG_TEXT
)) == NULL
)
10689 strcpy(printer
->state_message
, "Paused");
10692 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10693 sizeof(printer
->state_message
));
10696 cupsdStopPrinter(printer
, 1);
10698 if (dtype
& CUPS_PRINTER_CLASS
)
10699 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10700 printer
->name
, get_username(con
));
10702 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10703 printer
->name
, get_username(con
));
10706 * Everything was ok, so return OK status...
10709 con
->response
->request
.status
.status_code
= IPP_OK
;
10714 * 'url_encode_attr()' - URL-encode a string attribute.
10718 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10719 char *buffer
,/* I - String buffer */
10720 int bufsize
)/* I - Size of buffer */
10722 int i
; /* Looping var */
10723 char *bufptr
, /* Pointer into buffer */
10724 *bufend
; /* End of buffer */
10727 strlcpy(buffer
, attr
->name
, bufsize
);
10728 bufptr
= buffer
+ strlen(buffer
);
10729 bufend
= buffer
+ bufsize
- 1;
10731 for (i
= 0; i
< attr
->num_values
; i
++)
10733 if (bufptr
>= bufend
)
10741 if (bufptr
>= bufend
)
10746 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
10747 bufptr
, bufend
- bufptr
+ 1);
10749 if (bufptr
>= bufend
)
10760 * 'url_encode_string()' - URL-encode a string.
10763 static char * /* O - End of string */
10764 url_encode_string(const char *s
, /* I - String */
10765 char *buffer
, /* I - String buffer */
10766 int bufsize
) /* I - Size of buffer */
10768 char *bufptr
, /* Pointer into buffer */
10769 *bufend
; /* End of buffer */
10770 static const char *hex
= "0123456789ABCDEF";
10775 bufend
= buffer
+ bufsize
- 1;
10777 while (*s
&& bufptr
< bufend
)
10779 if (*s
== ' ' || *s
== '%')
10781 if (bufptr
>= (bufend
- 2))
10785 *bufptr
++ = hex
[(*s
>> 4) & 15];
10786 *bufptr
++ = hex
[*s
& 15];
10790 else if (*s
== '\'' || *s
== '\\')
10792 if (bufptr
>= (bufend
- 1))
10809 * 'user_allowed()' - See if a user is allowed to print to a queue.
10812 static int /* O - 0 if not allowed, 1 if allowed */
10813 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
10814 const char *username
) /* I - Username */
10816 int i
; /* Looping var */
10817 struct passwd
*pw
; /* User password data */
10818 char baseuser
[256], /* Base username */
10819 *baseptr
; /* Pointer to "@" in base username */
10822 if (p
->num_users
== 0)
10825 if (!strcmp(username
, "root"))
10828 if (strchr(username
, '@'))
10831 * Strip @REALM for username check...
10834 strlcpy(baseuser
, username
, sizeof(baseuser
));
10836 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
10839 username
= baseuser
;
10842 pw
= getpwnam(username
);
10845 for (i
= 0; i
< p
->num_users
; i
++)
10847 if (p
->users
[i
][0] == '@')
10850 * Check group membership...
10853 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
10856 else if (p
->users
[i
][0] == '#')
10862 if (cupsdCheckGroup(username
, pw
, p
->users
[i
]))
10865 else if (!strcasecmp(username
, p
->users
[i
]))
10869 return ((i
< p
->num_users
) != p
->deny_users
);
10874 * 'validate_job()' - Validate printer options and destination.
10878 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10879 ipp_attribute_t
*uri
) /* I - Printer URI */
10881 http_status_t status
; /* Policy status */
10882 ipp_attribute_t
*attr
; /* Current attribute */
10883 ipp_attribute_t
*format
; /* Document-format attribute */
10884 cups_ptype_t dtype
; /* Destination type (printer/class) */
10885 char super
[MIME_MAX_SUPER
],
10886 /* Supertype of file */
10887 type
[MIME_MAX_TYPE
];
10888 /* Subtype of file */
10889 cupsd_printer_t
*printer
; /* Printer */
10892 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10893 con
->http
.fd
, uri
->values
[0].string
.text
);
10896 * OK, see if the client is sending the document compressed - CUPS
10897 * doesn't support compression yet...
10900 if ((attr
= ippFindAttribute(con
->request
, "compression",
10901 IPP_TAG_KEYWORD
)) != NULL
&&
10902 !strcmp(attr
->values
[0].string
.text
, "none"))
10904 send_ipp_status(con
, IPP_ATTRIBUTES
,
10905 _("Unsupported compression attribute %s!"),
10906 attr
->values
[0].string
.text
);
10907 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10908 "compression", NULL
, attr
->values
[0].string
.text
);
10913 * Is it a format we support?
10916 if ((format
= ippFindAttribute(con
->request
, "document-format",
10917 IPP_TAG_MIMETYPE
)) != NULL
)
10919 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
10922 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
10923 format
->values
[0].string
.text
);
10927 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10928 !mimeType(MimeDatabase
, super
, type
))
10930 cupsdLogMessage(CUPSD_LOG_INFO
,
10931 "Hint: Do you have the raw file printing rules enabled?");
10932 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10933 _("Unsupported format \"%s\"!"),
10934 format
->values
[0].string
.text
);
10935 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10936 "document-format", NULL
, format
->values
[0].string
.text
);
10942 * Is the destination valid?
10945 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10951 send_ipp_status(con
, IPP_NOT_FOUND
,
10952 _("The printer or class was not found."));
10960 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10962 send_http_error(con
, status
, printer
);
10967 * Everything was ok, so return OK status...
10970 con
->response
->request
.status
.status_code
= IPP_OK
;
10975 * 'validate_name()' - Make sure the printer name only contains valid chars.
10978 static int /* O - 0 if name is no good, 1 if good */
10979 validate_name(const char *name
) /* I - Name to check */
10981 const char *ptr
; /* Pointer into name */
10985 * Scan the whole name...
10988 for (ptr
= name
; *ptr
; ptr
++)
10989 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
10993 * All the characters are good; validate the length, too...
10996 return ((ptr
- name
) < 128);
11001 * 'validate_user()' - Validate the user for the request.
11004 static int /* O - 1 if permitted, 0 otherwise */
11005 validate_user(cupsd_job_t
*job
, /* I - Job */
11006 cupsd_client_t
*con
, /* I - Client connection */
11007 const char *owner
, /* I - Owner of job/resource */
11008 char *username
, /* O - Authenticated username */
11009 int userlen
) /* I - Length of username */
11011 cupsd_printer_t
*printer
; /* Printer for job */
11014 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
11015 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
11017 job
->id
, con
? con
->http
.fd
: 0,
11018 owner
? owner
: "(null)", username
, userlen
);
11021 * Validate input...
11024 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11028 * Get the best authenticated username that is available.
11031 strlcpy(username
, get_username(con
), userlen
);
11034 * Check the username against the owner...
11037 printer
= cupsdFindDest(job
->dest
);
11039 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11040 con
, owner
) == HTTP_OK
);
11045 * End of "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $".