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 scheme \"%s\"!"),
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 if ((attr
= ippFindAttribute(con
->request
, "printer-state-reasons",
2624 IPP_TAG_KEYWORD
)) != NULL
)
2626 if (attr
->num_values
>
2627 (int)(sizeof(printer
->reasons
) / sizeof(printer
->reasons
[0])))
2629 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2630 _("Too many printer-state-reasons values (%d > %d)!"),
2632 (int)(sizeof(printer
->reasons
) /
2633 sizeof(printer
->reasons
[0])));
2637 for (i
= 0; i
< printer
->num_reasons
; i
++)
2638 _cupsStrFree(printer
->reasons
[i
]);
2640 printer
->num_reasons
= 0;
2641 for (i
= 0; i
< attr
->num_values
; i
++)
2643 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
2646 printer
->reasons
[printer
->num_reasons
] =
2647 _cupsStrAlloc(attr
->values
[i
].string
.text
);
2649 if (!strcmp(printer
->reasons
[printer
->num_reasons
], "paused") &&
2650 printer
->state
!= IPP_PRINTER_STOPPED
)
2652 cupsdLogMessage(CUPSD_LOG_INFO
,
2653 "Setting %s printer-state to %d (was %d.)",
2654 printer
->name
, IPP_PRINTER_STOPPED
, printer
->state
);
2655 cupsdStopPrinter(printer
, 0);
2658 printer
->num_reasons
++;
2661 if (PrintcapFormat
== PRINTCAP_PLIST
)
2662 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2665 set_printer_defaults(con
, printer
);
2667 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2668 IPP_TAG_KEYWORD
)) != NULL
)
2669 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2672 * See if we have all required attributes...
2675 if (!printer
->device_uri
)
2676 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2679 * See if we have an interface script or PPD file attached to the request...
2684 need_restart_job
= 1;
2686 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2688 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2691 * Yes; get the first line from it...
2695 cupsFileGets(fp
, line
, sizeof(line
));
2699 * Then see what kind of file it is...
2702 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2705 if (!strncmp(line
, "*PPD-Adobe", 10))
2708 * The new file is a PPD file, so remove any old interface script
2709 * that might be lying around...
2717 * This must be an interface script, so move the file over to the
2718 * interfaces directory and make it executable...
2721 if (copy_file(srcfile
, dstfile
))
2723 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2724 _("Unable to copy interface script - %s!"),
2729 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2730 "Copied interface script successfully!");
2731 chmod(dstfile
, 0755);
2734 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2737 if (!strncmp(line
, "*PPD-Adobe", 10))
2740 * The new file is a PPD file, so move the file over to the
2741 * ppd directory and make it readable by all...
2744 if (copy_file(srcfile
, dstfile
))
2746 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2747 _("Unable to copy PPD file - %s!"),
2752 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2753 "Copied PPD file successfully!");
2754 chmod(dstfile
, 0644);
2758 * (Re)register color profiles...
2763 apple_unregister_profiles(printer
);
2764 apple_register_profiles(printer
);
2766 #endif /* __APPLE__ */
2771 * This must be an interface script, so remove any old PPD file that
2772 * may be lying around...
2779 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2780 IPP_TAG_NAME
)) != NULL
)
2782 need_restart_job
= 1;
2784 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2787 * Raw driver, remove any existing PPD or interface script files.
2790 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2794 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2804 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2808 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2811 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2813 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2817 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2818 "Copied PPD file successfully!");
2819 chmod(dstfile
, 0644);
2823 * (Re)register color profiles...
2828 apple_unregister_profiles(printer
);
2829 apple_register_profiles(printer
);
2831 #endif /* __APPLE__ */
2836 * If we set the device URI but not the port monitor, check which port
2837 * monitor to use by default...
2840 if (set_device_uri
&& !set_port_monitor
)
2842 ppd_file_t
*ppd
; /* PPD file */
2843 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2846 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2847 sizeof(scheme
), username
, sizeof(username
), host
,
2848 sizeof(host
), &port
, resource
, sizeof(resource
));
2850 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2852 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2854 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2856 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2857 if (!strcmp(scheme
, ppdattr
->spec
))
2859 cupsdLogMessage(CUPSD_LOG_INFO
,
2860 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2861 printer
->name
, ppdattr
->value
,
2862 printer
->port_monitor
? printer
->port_monitor
2865 if (strcmp(ppdattr
->value
, "none"))
2866 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2868 cupsdClearString(&printer
->port_monitor
);
2878 * Update the printer attributes and return...
2881 cupsdSetPrinterAttrs(printer
);
2882 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2884 if (need_restart_job
&& printer
->job
)
2889 * Stop the current job and then restart it below...
2892 job
= (cupsd_job_t
*)printer
->job
;
2894 cupsdStopJob(job
, 1);
2896 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2897 job
->state_value
= IPP_JOB_PENDING
;
2900 if (need_restart_job
)
2903 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2907 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2908 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2911 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2912 printer
->name
, get_username(con
));
2916 cupsdAddPrinterHistory(printer
);
2918 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2919 "New printer \"%s\" added by \"%s\".", printer
->name
,
2922 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2923 printer
->name
, get_username(con
));
2926 con
->response
->request
.status
.status_code
= IPP_OK
;
2931 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2932 * based upon the printer state...
2936 add_printer_state_reasons(
2937 cupsd_client_t
*con
, /* I - Client connection */
2938 cupsd_printer_t
*p
) /* I - Printer info */
2940 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2941 "add_printer_state_reasons(%p[%d], %p[%s])",
2942 con
, con
->http
.fd
, p
, p
->name
);
2944 if (p
->num_reasons
== 0)
2945 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2946 "printer-state-reasons", NULL
,
2947 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2949 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2950 "printer-state-reasons", p
->num_reasons
, NULL
,
2951 (const char * const *)p
->reasons
);
2956 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2957 * the specified printer or class.
2961 add_queued_job_count(
2962 cupsd_client_t
*con
, /* I - Client connection */
2963 cupsd_printer_t
*p
) /* I - Printer or class */
2965 int count
; /* Number of jobs on destination */
2968 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2969 con
, con
->http
.fd
, p
, p
->name
);
2971 count
= cupsdGetPrinterJobCount(p
->name
);
2973 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2974 "queued-job-count", count
);
2980 * 'apple_init_profile()' - Initialize a color profile.
2985 ppd_file_t
*ppd
, /* I - PPD file */
2986 cups_array_t
*languages
, /* I - Languages in the PPD file */
2987 CMDeviceProfileInfo
*profile
, /* I - Profile record */
2988 unsigned id
, /* I - Profile ID */
2989 const char *name
, /* I - Profile name */
2990 const char *text
, /* I - Profile UI text */
2991 const char *iccfile
) /* I - ICC filename */
2993 char url
[1024]; /* URL for profile filename */
2994 CFMutableDictionaryRef dict
; /* Dictionary for name */
2995 char *language
; /* Current language */
2996 ppd_attr_t
*attr
; /* Profile attribute */
2997 CFStringRef cflang
, /* Language string */
2998 cftext
; /* Localized text */
3002 * Build the profile name dictionary...
3005 dict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3006 &kCFTypeDictionaryKeyCallBacks
,
3007 &kCFTypeDictionaryValueCallBacks
);
3009 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, text
,
3010 kCFStringEncodingUTF8
);
3014 CFDictionarySetValue(dict
, CFSTR("en"), cftext
);
3021 * Find localized names for the color profiles...
3024 cupsArraySave(ppd
->sorted_attrs
);
3026 for (language
= (char *)cupsArrayFirst(languages
);
3028 language
= (char *)cupsArrayNext(languages
))
3032 if ((attr
= _ppdLocalizedAttr(ppd
, "cupsICCProfile", name
,
3034 attr
= _ppdLocalizedAttr(ppd
, "APTiogaProfile", name
, language
);
3037 attr
= _ppdLocalizedAttr(ppd
, "ColorModel", name
, language
);
3039 if (attr
&& attr
->text
[0])
3041 cflang
= CFStringCreateWithCString(kCFAllocatorDefault
, language
,
3042 kCFStringEncodingUTF8
);
3043 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, attr
->text
,
3044 kCFStringEncodingUTF8
);
3046 if (cflang
&& cftext
)
3047 CFDictionarySetValue(dict
, cflang
, cftext
);
3057 cupsArrayRestore(ppd
->sorted_attrs
);
3061 * Fill in the profile data...
3065 httpAssembleURI(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "file", NULL
, "", 0,
3068 profile
->dataVersion
= cmDeviceProfileInfoVersion1
;
3069 profile
->profileID
= id
;
3070 profile
->profileLoc
.locType
= iccfile
? cmPathBasedProfile
: cmNoProfileBase
;
3071 profile
->profileName
= dict
;
3074 strlcpy(profile
->profileLoc
.u
.pathLoc
.path
, iccfile
,
3075 sizeof(profile
->profileLoc
.u
.pathLoc
.path
));
3080 * 'apple_register_profiles()' - Register color profiles for a printer.
3084 apple_register_profiles(
3085 cupsd_printer_t
*p
) /* I - Printer */
3087 int i
; /* Looping var */
3088 char ppdfile
[1024], /* PPD filename */
3089 iccfile
[1024], /* ICC filename */
3090 selector
[PPD_MAX_NAME
];
3091 /* Profile selection string */
3092 ppd_file_t
*ppd
; /* PPD file */
3093 ppd_attr_t
*attr
, /* Profile attributes */
3094 *profileid_attr
,/* cupsProfileID attribute */
3095 *q1_attr
, /* ColorModel (or other) qualifier */
3096 *q2_attr
, /* MediaType (or other) qualifier */
3097 *q3_attr
; /* Resolution (or other) qualifier */
3098 char q_keyword
[PPD_MAX_NAME
];
3099 /* Qualifier keyword */
3100 const char *q1_choice
, /* ColorModel (or other) choice */
3101 *q2_choice
, /* MediaType (or other) choice */
3102 *q3_choice
; /* Resolution (or other) choice */
3103 const char *profile_key
; /* Profile keyword */
3104 ppd_option_t
*cm_option
; /* Color model option */
3105 ppd_choice_t
*cm_choice
; /* Color model choice */
3106 int num_profiles
; /* Number of profiles */
3107 CMError error
; /* Last error */
3108 unsigned device_id
, /* Printer device ID */
3109 profile_id
, /* Profile ID */
3110 default_profile_id
= 0;
3111 /* Default profile ID */
3112 CFMutableDictionaryRef device_name
; /* Printer device name dictionary */
3113 CFStringRef printer_name
; /* Printer name string */
3114 CMDeviceScope scope
= /* Scope of the registration */
3116 kCFPreferencesAnyUser
,
3117 kCFPreferencesCurrentHost
3119 CMDeviceProfileArrayPtr profiles
; /* Profiles */
3120 CMDeviceProfileInfo
*profile
; /* Current profile */
3121 cups_array_t
*languages
; /* Languages array */
3125 * Make sure ColorSync is available...
3128 if (CMRegisterColorDevice
== NULL
)
3132 * Try opening the PPD file for this printer...
3135 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3136 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
3140 * See if we have any profiles...
3143 if ((attr
= ppdFindAttr(ppd
, "APTiogaProfile", NULL
)) != NULL
)
3144 profile_key
= "APTiogaProfile";
3147 attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
3148 profile_key
= "cupsICCProfile";
3151 for (num_profiles
= 0; attr
; attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3152 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3154 if (attr
->value
[0] != '/')
3155 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3158 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3160 if (access(iccfile
, 0))
3168 * If we have profiles, add them...
3171 if (num_profiles
> 0)
3173 if (profile_key
[0] == 'A')
3176 * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile
3180 if ((attr
= ppdFindAttr(ppd
, "DefaultAPTiogaProfile", NULL
)) != NULL
&&
3182 default_profile_id
= atoi(attr
->value
);
3184 q1_choice
= q2_choice
= q3_choice
= NULL
;
3189 * For CUPS PPDs, figure out the default profile selector values...
3192 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier1", NULL
)) != NULL
&&
3193 attr
->value
&& attr
->value
[0])
3195 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3196 q1_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3198 else if ((q1_attr
= ppdFindAttr(ppd
, "DefaultColorModel", NULL
)) == NULL
)
3199 q1_attr
= ppdFindAttr(ppd
, "DefaultColorSpace", NULL
);
3201 if (q1_attr
&& q1_attr
->value
&& q1_attr
->value
[0])
3202 q1_choice
= q1_attr
->value
;
3206 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier2", NULL
)) != NULL
&&
3207 attr
->value
&& attr
->value
[0])
3209 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3210 q2_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3213 q2_attr
= ppdFindAttr(ppd
, "DefaultMediaType", NULL
);
3215 if (q2_attr
&& q2_attr
->value
&& q2_attr
->value
[0])
3216 q2_choice
= q2_attr
->value
;
3220 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier3", NULL
)) != NULL
&&
3221 attr
->value
&& attr
->value
[0])
3223 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3224 q3_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3227 q3_attr
= ppdFindAttr(ppd
, "DefaultResolution", NULL
);
3229 if (q3_attr
&& q3_attr
->value
&& q3_attr
->value
[0])
3230 q3_choice
= q3_attr
->value
;
3236 * Build the array of profiles...
3238 * Note: This calloc actually requests slightly more memory than needed.
3241 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3243 cupsdLogMessage(CUPSD_LOG_ERROR
,
3244 "Unable to allocate memory for %d profiles!",
3250 profiles
->profileCount
= num_profiles
;
3251 languages
= _ppdGetLanguages(ppd
);
3253 for (profile
= profiles
->profiles
,
3254 attr
= ppdFindAttr(ppd
, profile_key
, NULL
);
3256 attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3257 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3260 * Add this profile...
3263 if (attr
->value
[0] != '/')
3264 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3267 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3269 if (access(iccfile
, 0))
3272 if (profile_key
[0] == 'c')
3274 cupsArraySave(ppd
->sorted_attrs
);
3276 if ((profileid_attr
= ppdFindAttr(ppd
, "cupsProfileID",
3277 attr
->spec
)) != NULL
&&
3278 profileid_attr
->value
&& isdigit(profileid_attr
->value
[0] & 255))
3279 profile_id
= (unsigned)strtoul(profileid_attr
->value
, NULL
, 10);
3281 profile_id
= _ppdHashName(attr
->spec
);
3283 cupsArrayRestore(ppd
->sorted_attrs
);
3286 profile_id
= atoi(attr
->spec
);
3288 apple_init_profile(ppd
, languages
, profile
, profile_id
, attr
->spec
,
3289 attr
->text
[0] ? attr
->text
: attr
->spec
, iccfile
);
3294 * See if this is the default profile...
3297 if (!default_profile_id
)
3303 snprintf(selector
, sizeof(selector
), "%s.%s.%s",
3304 q1_choice
, q2_choice
, q3_choice
);
3305 if (!strcmp(selector
, attr
->spec
))
3306 default_profile_id
= profile_id
;
3309 if (!default_profile_id
)
3311 snprintf(selector
, sizeof(selector
), "%s.%s.", q1_choice
,
3313 if (!strcmp(selector
, attr
->spec
))
3314 default_profile_id
= profile_id
;
3318 if (!default_profile_id
&& q3_choice
)
3320 snprintf(selector
, sizeof(selector
), "%s..%s", q1_choice
,
3322 if (!strcmp(selector
, attr
->spec
))
3323 default_profile_id
= profile_id
;
3326 if (!default_profile_id
)
3328 snprintf(selector
, sizeof(selector
), "%s..", q1_choice
);
3329 if (!strcmp(selector
, attr
->spec
))
3330 default_profile_id
= profile_id
;
3335 _ppdFreeLanguages(languages
);
3337 else if ((cm_option
= ppdFindOption(ppd
, "ColorModel")) != NULL
)
3340 * Extract profiles from ColorModel option...
3343 const char *profile_name
; /* Name of generic profile */
3346 num_profiles
= cm_option
->num_choices
;
3348 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3350 cupsdLogMessage(CUPSD_LOG_ERROR
,
3351 "Unable to allocate memory for %d profiles!",
3357 profiles
->profileCount
= num_profiles
;
3359 for (profile
= profiles
->profiles
, i
= cm_option
->num_choices
,
3360 cm_choice
= cm_option
->choices
;
3362 i
--, cm_choice
++, profile
++)
3364 if (!strcmp(cm_choice
->choice
, "Gray") ||
3365 !strcmp(cm_choice
->choice
, "Black"))
3366 profile_name
= "Gray";
3367 else if (!strcmp(cm_choice
->choice
, "RGB") ||
3368 !strcmp(cm_choice
->choice
, "CMY"))
3369 profile_name
= "RGB";
3370 else if (!strcmp(cm_choice
->choice
, "CMYK") ||
3371 !strcmp(cm_choice
->choice
, "KCMY"))
3372 profile_name
= "CMYK";
3374 profile_name
= "DeviceN";
3376 snprintf(selector
, sizeof(selector
), "%s..", profile_name
);
3377 profile_id
= _ppdHashName(selector
);
3379 apple_init_profile(ppd
, NULL
, profile
, profile_id
, cm_choice
->choice
,
3380 cm_choice
->text
, NULL
);
3382 if (cm_choice
->marked
)
3383 default_profile_id
= profile_id
;
3389 * Use the default colorspace...
3392 num_profiles
= 1 + ppd
->colorspace
!= PPD_CS_GRAY
;
3394 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3396 cupsdLogMessage(CUPSD_LOG_ERROR
,
3397 "Unable to allocate memory for %d profiles!",
3403 profiles
->profileCount
= num_profiles
;
3405 apple_init_profile(ppd
, NULL
, profiles
->profiles
, _ppdHashName("Gray.."),
3406 "Gray", "Gray", NULL
);
3408 switch (ppd
->colorspace
)
3412 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3413 _ppdHashName("RGB.."), "RGB", "RGB", NULL
);
3417 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3418 _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL
);
3422 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3423 _ppdHashName("DeviceN.."), "DeviceN", "DeviceN",
3432 if (num_profiles
> 0)
3435 * Make sure we have a default profile ID...
3438 if (!default_profile_id
)
3439 default_profile_id
= profiles
->profiles
[num_profiles
- 1].profileID
;
3442 * Get the device ID hash and pathelogical name dictionary.
3445 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\"",
3448 device_id
= _ppdHashName(p
->name
);
3449 device_name
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3450 &kCFTypeDictionaryKeyCallBacks
,
3451 &kCFTypeDictionaryValueCallBacks
);
3452 printer_name
= CFStringCreateWithCString(kCFAllocatorDefault
,
3453 p
->name
, kCFStringEncodingUTF8
);
3455 if (device_name
&& printer_name
)
3457 CFDictionarySetValue(device_name
, CFSTR("en"), printer_name
);
3460 * Register the device with ColorSync...
3463 error
= CMRegisterColorDevice(cmPrinterDeviceClass
, device_id
,
3464 device_name
, &scope
);
3467 * Register the profiles...
3471 error
= CMSetDeviceFactoryProfiles(cmPrinterDeviceClass
, device_id
,
3472 default_profile_id
, profiles
);
3482 cupsdLogMessage(CUPSD_LOG_ERROR
,
3483 "Unable to register ICC color profiles for \"%s\" - %d",
3484 p
->name
, (int)error
);
3486 for (profile
= profiles
->profiles
;
3488 profile
++, num_profiles
--)
3489 CFRelease(profile
->profileName
);
3494 CFRelease(printer_name
);
3497 CFRelease(device_name
);
3505 * 'apple_unregister_profiles()' - Remove color profiles for the specified
3510 apple_unregister_profiles(
3511 cupsd_printer_t
*p
) /* I - Printer */
3514 * Make sure ColorSync is available...
3517 if (CMUnregisterColorDevice
!= NULL
)
3518 CMUnregisterColorDevice(cmPrinterDeviceClass
, _ppdHashName(p
->name
));
3520 #endif /* __APPLE__ */
3523 * 'apply_printer_defaults()' - Apply printer default options to a job.
3527 apply_printer_defaults(
3528 cupsd_printer_t
*printer
, /* I - Printer */
3529 cupsd_job_t
*job
) /* I - Job */
3531 int i
, /* Looping var */
3532 num_options
; /* Number of default options */
3533 cups_option_t
*options
, /* Default options */
3534 *option
; /* Current option */
3538 * Collect all of the default options and add the missing ones to the
3542 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
3543 option
= printer
->options
;
3546 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
3548 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
3553 * Encode these options as attributes in the job object...
3556 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
3557 cupsFreeOptions(num_options
, options
);
3562 * 'authenticate_job()' - Set job authentication info.
3566 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
3567 ipp_attribute_t
*uri
) /* I - Job URI */
3569 ipp_attribute_t
*attr
, /* job-id attribute */
3570 *auth_info
; /* auth-info attribute */
3571 int jobid
; /* Job ID */
3572 cupsd_job_t
*job
; /* Current job */
3573 char method
[HTTP_MAX_URI
],
3574 /* Method portion of URI */
3575 username
[HTTP_MAX_URI
],
3576 /* Username portion of URI */
3578 /* Host portion of URI */
3579 resource
[HTTP_MAX_URI
];
3580 /* Resource portion of URI */
3581 int port
; /* Port portion of URI */
3584 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
3585 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
3588 * Start with "everything is OK" status...
3591 con
->response
->request
.status
.status_code
= IPP_OK
;
3594 * See if we have a job URI or a printer URI...
3597 if (!strcmp(uri
->name
, "printer-uri"))
3600 * Got a printer URI; see if we also have a job-id attribute...
3603 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3604 IPP_TAG_INTEGER
)) == NULL
)
3606 send_ipp_status(con
, IPP_BAD_REQUEST
,
3607 _("Got a printer-uri attribute but no job-id!"));
3611 jobid
= attr
->values
[0].integer
;
3616 * Got a job URI; parse it to get the job ID...
3619 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
3620 sizeof(method
), username
, sizeof(username
), host
,
3621 sizeof(host
), &port
, resource
, sizeof(resource
));
3623 if (strncmp(resource
, "/jobs/", 6))
3629 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
3630 uri
->values
[0].string
.text
);
3634 jobid
= atoi(resource
+ 6);
3638 * See if the job exists...
3641 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3644 * Nope - return a "not found" error...
3647 send_ipp_status(con
, IPP_NOT_FOUND
,
3648 _("Job #%d does not exist!"), jobid
);
3653 * See if the job has been completed...
3656 if (job
->state_value
!= IPP_JOB_HELD
)
3659 * Return a "not-possible" error...
3662 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3663 _("Job #%d is not held for authentication!"),
3669 * See if we have already authenticated...
3672 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3674 if (!con
->username
[0] && !auth_info
)
3676 cupsd_printer_t
*printer
; /* Job destination */
3680 * No auth data. If we need to authenticate via Kerberos, send a
3681 * HTTP auth challenge, otherwise just return an IPP error...
3684 printer
= cupsdFindDest(job
->dest
);
3686 if (printer
&& printer
->num_auth_info_required
> 0 &&
3687 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3688 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3690 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3691 _("No authentication information provided!"));
3696 * See if the job is owned by the requesting user...
3699 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3701 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3706 * Save the authentication information for this job...
3709 save_auth_info(con
, job
, auth_info
);
3712 * Reset the job-hold-until value to "no-hold"...
3715 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3716 IPP_TAG_KEYWORD
)) == NULL
)
3717 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3721 attr
->value_tag
= IPP_TAG_KEYWORD
;
3722 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
3726 * Release the job and return...
3729 cupsdReleaseJob(job
);
3731 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3736 * 'cancel_all_jobs()' - Cancel all print jobs.
3740 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3741 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3743 http_status_t status
; /* Policy status */
3744 cups_ptype_t dtype
; /* Destination type */
3745 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3746 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3747 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3748 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3749 int port
; /* Port portion of URI */
3750 ipp_attribute_t
*attr
; /* Attribute in request */
3751 const char *username
; /* Username */
3752 int purge
; /* Purge? */
3753 cupsd_printer_t
*printer
; /* Printer */
3756 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3757 con
->http
.fd
, uri
->values
[0].string
.text
);
3760 * See if we have a printer URI...
3763 if (strcmp(uri
->name
, "printer-uri"))
3765 send_ipp_status(con
, IPP_BAD_REQUEST
,
3766 _("The printer-uri attribute is required!"));
3771 * Get the username (if any) for the jobs we want to cancel (only if
3772 * "my-jobs" is specified...
3775 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3776 IPP_TAG_BOOLEAN
)) != NULL
&&
3777 attr
->values
[0].boolean
)
3779 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3780 IPP_TAG_NAME
)) != NULL
)
3781 username
= attr
->values
[0].string
.text
;
3784 send_ipp_status(con
, IPP_BAD_REQUEST
,
3785 _("Missing requesting-user-name attribute!"));
3793 * Look for the "purge-jobs" attribute...
3796 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3797 IPP_TAG_BOOLEAN
)) != NULL
)
3798 purge
= attr
->values
[0].boolean
;
3803 * And if the destination is valid...
3806 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3812 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3813 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3814 hostname
, sizeof(hostname
), &port
,
3815 resource
, sizeof(resource
));
3817 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3818 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3820 send_ipp_status(con
, IPP_NOT_FOUND
,
3821 _("The printer or class was not found."));
3829 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3831 send_http_error(con
, status
, NULL
);
3836 * Cancel all jobs on all printers...
3839 cupsdCancelJobs(NULL
, username
, purge
);
3841 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3842 purge
? "purged" : "canceled", get_username(con
));
3850 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3853 send_http_error(con
, status
, printer
);
3858 * Cancel all of the jobs on the named printer...
3861 cupsdCancelJobs(printer
->name
, username
, purge
);
3863 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3864 printer
->name
, purge
? "purged" : "canceled",
3868 con
->response
->request
.status
.status_code
= IPP_OK
;
3873 * 'cancel_job()' - Cancel a print job.
3877 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3878 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3880 ipp_attribute_t
*attr
; /* Current attribute */
3881 int jobid
; /* Job ID */
3882 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3883 username
[HTTP_MAX_URI
], /* Username portion of URI */
3884 host
[HTTP_MAX_URI
], /* Host portion of URI */
3885 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3886 int port
; /* Port portion of URI */
3887 cupsd_job_t
*job
; /* Job information */
3888 cups_ptype_t dtype
; /* Destination type (printer/class) */
3889 cupsd_printer_t
*printer
; /* Printer data */
3890 int purge
; /* Purge the job? */
3893 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3894 con
->http
.fd
, uri
->values
[0].string
.text
);
3897 * See if we have a job URI or a printer URI...
3900 if (!strcmp(uri
->name
, "printer-uri"))
3903 * Got a printer URI; see if we also have a job-id attribute...
3906 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3907 IPP_TAG_INTEGER
)) == NULL
)
3909 send_ipp_status(con
, IPP_BAD_REQUEST
,
3910 _("Got a printer-uri attribute but no job-id!"));
3914 if ((jobid
= attr
->values
[0].integer
) == 0)
3917 * Find the current job on the specified printer...
3920 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3926 send_ipp_status(con
, IPP_NOT_FOUND
,
3927 _("The printer or class was not found."));
3932 * See if the printer is currently printing a job...
3936 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3940 * No, see if there are any pending jobs...
3943 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3945 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3946 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3947 !strcasecmp(job
->dest
, printer
->name
))
3954 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3956 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3957 if (job
->state_value
== IPP_JOB_STOPPED
&&
3958 !strcasecmp(job
->dest
, printer
->name
))
3965 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3976 * Got a job URI; parse it to get the job ID...
3979 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3980 sizeof(scheme
), username
, sizeof(username
), host
,
3981 sizeof(host
), &port
, resource
, sizeof(resource
));
3983 if (strncmp(resource
, "/jobs/", 6))
3989 send_ipp_status(con
, IPP_BAD_REQUEST
,
3990 _("Bad job-uri attribute \"%s\"!"),
3991 uri
->values
[0].string
.text
);
3995 jobid
= atoi(resource
+ 6);
3999 * Look for the "purge-job" attribute...
4002 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
4003 IPP_TAG_BOOLEAN
)) != NULL
)
4004 purge
= attr
->values
[0].boolean
;
4009 * See if the job exists...
4012 if ((job
= cupsdFindJob(jobid
)) == NULL
)
4015 * Nope - return a "not found" error...
4018 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
4023 * See if the job is owned by the requesting user...
4026 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
4028 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
4033 * See if the job is already completed, canceled, or aborted; if so,
4034 * we can't cancel...
4037 if (job
->state_value
>= IPP_JOB_CANCELED
&& !purge
)
4039 switch (job
->state_value
)
4041 case IPP_JOB_CANCELED
:
4042 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4043 _("Job #%d is already canceled - can\'t cancel."),
4047 case IPP_JOB_ABORTED
:
4048 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4049 _("Job #%d is already aborted - can\'t cancel."),
4054 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4055 _("Job #%d is already completed - can\'t cancel."),
4064 * Cancel the job and return...
4067 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
4071 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
4074 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
4077 con
->response
->request
.status
.status_code
= IPP_OK
;
4082 * 'cancel_subscription()' - Cancel a subscription.
4086 cancel_subscription(
4087 cupsd_client_t
*con
, /* I - Client connection */
4088 int sub_id
) /* I - Subscription ID */
4090 http_status_t status
; /* Policy status */
4091 cupsd_subscription_t
*sub
; /* Subscription */
4094 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4095 "cancel_subscription(con=%p[%d], sub_id=%d)",
4096 con
, con
->http
.fd
, sub_id
);
4099 * Is the subscription ID valid?
4102 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
4105 * Bad subscription ID...
4108 send_ipp_status(con
, IPP_NOT_FOUND
,
4109 _("notify-subscription-id %d no good!"), sub_id
);
4117 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
4119 con
, sub
->owner
)) != HTTP_OK
)
4121 send_http_error(con
, status
, sub
->dest
);
4126 * Cancel the subscription...
4129 cupsdDeleteSubscription(sub
, 1);
4131 con
->response
->request
.status
.status_code
= IPP_OK
;
4136 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
4139 static int /* O - 1 if OK, 0 if not */
4140 check_rss_recipient(
4141 const char *recipient
) /* I - Recipient URI */
4143 cupsd_subscription_t
*sub
; /* Current subscription */
4146 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
4148 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
4152 * Compare the URIs up to the first ?...
4155 const char *r1
, *r2
;
4157 for (r1
= recipient
, r2
= sub
->recipient
;
4158 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
4170 * 'check_quotas()' - Check quotas for a printer and user.
4173 static int /* O - 1 if OK, 0 if not */
4174 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
4175 cupsd_printer_t
*p
) /* I - Printer or class */
4177 int i
; /* Looping var */
4178 char username
[33]; /* Username */
4179 cupsd_quota_t
*q
; /* Quota data */
4180 #ifdef HAVE_MBR_UID_TO_UUID
4182 * Use Apple membership APIs which require that all names represent
4183 * valid user account or group records accessible by the server.
4186 uuid_t usr_uuid
; /* UUID for job requesting user */
4187 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
4188 uuid_t grp_uuid
; /* UUID for ACL group name entry */
4189 int mbr_err
; /* Error from membership function */
4190 int is_member
; /* Is this user a member? */
4193 * Use standard POSIX APIs for checking users and groups...
4196 struct passwd
*pw
; /* User password data */
4197 #endif /* HAVE_MBR_UID_TO_UUID */
4200 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
4201 con
, con
->http
.fd
, p
, p
->name
);
4204 * Figure out who is printing...
4207 strlcpy(username
, get_username(con
), sizeof(username
));
4210 * Check global active job limits for printers and users...
4213 if (MaxJobsPerPrinter
)
4216 * Check if there are too many pending jobs on this printer...
4219 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
4221 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
4230 * Check if there are too many pending jobs for this user...
4233 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
4235 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
4242 * Check against users...
4245 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
4250 #ifdef HAVE_MBR_UID_TO_UUID
4252 * Get UUID for job requesting user...
4255 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
4261 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4262 "check_quotas: UUID lookup failed for user \"%s\"",
4264 cupsdLogMessage(CUPSD_LOG_INFO
,
4265 "Denying user \"%s\" access to printer \"%s\" "
4266 "(unknown user)...",
4272 * Get UID and GID of requesting user...
4275 pw
= getpwnam(username
);
4277 #endif /* HAVE_MBR_UID_TO_UUID */
4279 for (i
= 0; i
< p
->num_users
; i
++)
4280 if (p
->users
[i
][0] == '@')
4283 * Check group membership...
4286 #ifdef HAVE_MBR_UID_TO_UUID
4287 if (p
->users
[i
][1] == '#')
4289 if (uuid_parse((char *)p
->users
[i
] + 2, grp_uuid
))
4290 uuid_clear(grp_uuid
);
4292 else if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
4296 * Invalid ACL entries are ignored for matching; just record a
4297 * warning in the log...
4300 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4301 "check_quotas: UUID lookup failed for ACL entry "
4302 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4303 cupsdLogMessage(CUPSD_LOG_WARN
,
4304 "Access control entry \"%s\" not a valid group name; "
4305 "entry ignored", p
->users
[i
]);
4308 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
4312 * At this point, there should be no errors, but check anyways...
4315 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4316 "check_quotas: group \"%s\" membership check "
4317 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
4322 * Stop if we found a match...
4329 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
4331 #endif /* HAVE_MBR_UID_TO_UUID */
4333 #ifdef HAVE_MBR_UID_TO_UUID
4336 if (p
->users
[i
][0] == '#')
4338 if (uuid_parse((char *)p
->users
[i
] + 1, usr2_uuid
))
4339 uuid_clear(usr2_uuid
);
4341 else if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
4345 * Invalid ACL entries are ignored for matching; just record a
4346 * warning in the log...
4349 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4350 "check_quotas: UUID lookup failed for ACL entry "
4351 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4352 cupsdLogMessage(CUPSD_LOG_WARN
,
4353 "Access control entry \"%s\" not a valid user name; "
4354 "entry ignored", p
->users
[i
]);
4357 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
4360 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4361 "check_quotas: User \"%s\" identity check failed "
4362 "(err=%d)", p
->users
[i
], mbr_err
);
4370 else if (!strcasecmp(username
, p
->users
[i
]))
4372 #endif /* HAVE_MBR_UID_TO_UUID */
4374 if ((i
< p
->num_users
) == p
->deny_users
)
4376 cupsdLogMessage(CUPSD_LOG_INFO
,
4377 "Denying user \"%s\" access to printer \"%s\"...",
4388 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
4391 * TODO: Define these special page count values as constants!
4394 if (q
->page_count
== -4) /* special case: unlimited user */
4396 cupsdLogMessage(CUPSD_LOG_INFO
,
4397 "User \"%s\" request approved for printer %s (%s): "
4399 username
, p
->name
, p
->info
);
4400 q
->page_count
= 0; /* allow user to print */
4403 else if (q
->page_count
== -3) /* quota exceeded */
4405 cupsdLogMessage(CUPSD_LOG_INFO
,
4406 "User \"%s\" request denied for printer %s (%s): "
4407 "quota limit exceeded.",
4408 username
, p
->name
, p
->info
);
4409 q
->page_count
= 2; /* force quota exceeded failure */
4412 else if (q
->page_count
== -2) /* quota disabled for user */
4414 cupsdLogMessage(CUPSD_LOG_INFO
,
4415 "User \"%s\" request denied for printer %s (%s): "
4416 "printing disabled for user.",
4417 username
, p
->name
, p
->info
);
4418 q
->page_count
= 2; /* force quota exceeded failure */
4421 else if (q
->page_count
== -1) /* quota access error */
4423 cupsdLogMessage(CUPSD_LOG_INFO
,
4424 "User \"%s\" request denied for printer %s (%s): "
4425 "unable to determine quota limit.",
4426 username
, p
->name
, p
->info
);
4427 q
->page_count
= 2; /* force quota exceeded failure */
4430 else if (q
->page_count
< 0) /* user not found or other error */
4432 cupsdLogMessage(CUPSD_LOG_INFO
,
4433 "User \"%s\" request denied for printer %s (%s): "
4434 "user disabled / missing quota.",
4435 username
, p
->name
, p
->info
);
4436 q
->page_count
= 2; /* force quota exceeded failure */
4439 else /* page within user limits */
4441 q
->page_count
= 0; /* allow user to print */
4446 #endif /* __APPLE__ */
4447 if (p
->k_limit
|| p
->page_limit
)
4449 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
4451 cupsdLogMessage(CUPSD_LOG_ERROR
,
4452 "Unable to allocate quota data for user \"%s\"!",
4457 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
4458 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
4460 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
4467 * If we have gotten this far, we're done!
4475 * 'copy_attribute()' - Copy a single attribute.
4478 static ipp_attribute_t
* /* O - New attribute */
4480 ipp_t
*to
, /* O - Destination request/response */
4481 ipp_attribute_t
*attr
, /* I - Attribute to copy */
4482 int quickcopy
) /* I - Do a quick copy? */
4484 int i
; /* Looping var */
4485 ipp_attribute_t
*toattr
; /* Destination attribute */
4488 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4489 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
4490 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
4493 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
4496 toattr
= ippAddSeparator(to
);
4499 case IPP_TAG_INTEGER
:
4501 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4502 attr
->name
, attr
->num_values
, NULL
);
4504 for (i
= 0; i
< attr
->num_values
; i
++)
4505 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
4508 case IPP_TAG_BOOLEAN
:
4509 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
4510 attr
->num_values
, NULL
);
4512 for (i
= 0; i
< attr
->num_values
; i
++)
4513 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
4516 case IPP_TAG_STRING
:
4519 case IPP_TAG_KEYWORD
:
4521 case IPP_TAG_URISCHEME
:
4522 case IPP_TAG_CHARSET
:
4523 case IPP_TAG_LANGUAGE
:
4524 case IPP_TAG_MIMETYPE
:
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
++)
4532 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4536 for (i
= 0; i
< attr
->num_values
; i
++)
4537 toattr
->values
[i
].string
.text
=
4538 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4543 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
4544 attr
->values
[0].date
);
4547 case IPP_TAG_RESOLUTION
:
4548 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
4549 attr
->num_values
, IPP_RES_PER_INCH
,
4552 for (i
= 0; i
< attr
->num_values
; i
++)
4554 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
4555 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
4556 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
4560 case IPP_TAG_RANGE
:
4561 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
4562 attr
->num_values
, NULL
, NULL
);
4564 for (i
= 0; i
< attr
->num_values
; i
++)
4566 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
4567 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
4571 case IPP_TAG_TEXTLANG
:
4572 case IPP_TAG_NAMELANG
:
4573 toattr
= ippAddStrings(to
, attr
->group_tag
,
4574 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4575 attr
->name
, attr
->num_values
, NULL
, NULL
);
4579 for (i
= 0; i
< attr
->num_values
; i
++)
4581 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
4582 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4587 for (i
= 0; i
< attr
->num_values
; i
++)
4590 toattr
->values
[i
].string
.charset
=
4591 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
4593 toattr
->values
[i
].string
.charset
=
4594 toattr
->values
[0].string
.charset
;
4596 toattr
->values
[i
].string
.text
=
4597 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4602 case IPP_TAG_BEGIN_COLLECTION
:
4603 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
4604 attr
->num_values
, NULL
);
4606 for (i
= 0; i
< attr
->num_values
; i
++)
4608 toattr
->values
[i
].collection
= ippNew();
4609 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
4610 NULL
, IPP_TAG_ZERO
, 0);
4615 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4616 attr
->name
, attr
->num_values
, NULL
);
4618 for (i
= 0; i
< attr
->num_values
; i
++)
4620 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
4622 if (toattr
->values
[i
].unknown
.length
> 0)
4624 if ((toattr
->values
[i
].unknown
.data
=
4625 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
4626 toattr
->values
[i
].unknown
.length
= 0;
4628 memcpy(toattr
->values
[i
].unknown
.data
,
4629 attr
->values
[i
].unknown
.data
,
4630 toattr
->values
[i
].unknown
.length
);
4633 break; /* anti-compiler-warning-code */
4641 * 'copy_attrs()' - Copy attributes from one request to another.
4645 copy_attrs(ipp_t
*to
, /* I - Destination request */
4646 ipp_t
*from
, /* I - Source request */
4647 cups_array_t
*ra
, /* I - Requested attributes */
4648 ipp_tag_t group
, /* I - Group to copy */
4649 int quickcopy
) /* I - Do a quick copy? */
4651 ipp_attribute_t
*fromattr
; /* Source attribute */
4654 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4655 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4656 to
, from
, ra
, group
, quickcopy
);
4661 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4664 * Filter attributes as needed...
4667 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4668 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4671 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4672 copy_attribute(to
, fromattr
, quickcopy
);
4678 * 'copy_banner()' - Copy a banner file to the requests directory for the
4682 static int /* O - Size of banner file in kbytes */
4683 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4684 cupsd_job_t
*job
, /* I - Job information */
4685 const char *name
) /* I - Name of banner */
4687 int i
; /* Looping var */
4688 int kbytes
; /* Size of banner file in kbytes */
4689 char filename
[1024]; /* Job filename */
4690 cupsd_banner_t
*banner
; /* Pointer to banner */
4691 cups_file_t
*in
; /* Input file */
4692 cups_file_t
*out
; /* Output file */
4693 int ch
; /* Character from file */
4694 char attrname
[255], /* Name of attribute */
4695 *s
; /* Pointer into name */
4696 ipp_attribute_t
*attr
; /* Attribute */
4699 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4700 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4701 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
4702 name
? name
: "(null)");
4705 * Find the banner; return if not found or "none"...
4708 if (!name
|| !strcmp(name
, "none") ||
4709 (banner
= cupsdFindBanner(name
)) == NULL
)
4713 * Open the banner and job files...
4716 if (add_file(con
, job
, banner
->filetype
, 0))
4719 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4721 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4723 cupsdLogMessage(CUPSD_LOG_ERROR
,
4724 "Unable to create banner job file %s - %s",
4725 filename
, strerror(errno
));
4730 fchmod(cupsFileNumber(out
), 0640);
4731 fchown(cupsFileNumber(out
), RunUser
, Group
);
4734 * Try the localized banner file under the subdirectory...
4737 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4739 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4742 * Convert ll-cc to ll_CC...
4746 attrname
[3] = toupper(attrname
[3] & 255);
4747 attrname
[4] = toupper(attrname
[4] & 255);
4750 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4753 if (access(filename
, 0) && strlen(attrname
) > 2)
4756 * Wasn't able to find "ll_CC" locale file; try the non-national
4757 * localization banner directory.
4762 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4766 if (access(filename
, 0))
4769 * Use the non-localized banner file.
4772 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4775 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4779 cupsdLogMessage(CUPSD_LOG_ERROR
,
4780 "Unable to open banner template file %s - %s",
4781 filename
, strerror(errno
));
4787 * Parse the file to the end...
4790 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4794 * Get an attribute name...
4797 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4798 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4800 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4810 * Ignore { followed by stuff that is not an attribute name...
4813 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4818 * See if it is defined...
4821 if (attrname
[0] == '?')
4826 if (!strcmp(s
, "printer-name"))
4828 cupsFilePuts(out
, job
->dest
);
4831 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4834 * See if we have a leading question mark...
4837 if (attrname
[0] != '?')
4840 * Nope, write to file as-is; probably a PostScript procedure...
4843 cupsFilePrintf(out
, "{%s}", attrname
);
4850 * Output value(s)...
4853 for (i
= 0; i
< attr
->num_values
; i
++)
4856 cupsFilePutChar(out
, ',');
4858 switch (attr
->value_tag
)
4860 case IPP_TAG_INTEGER
:
4862 if (!strncmp(s
, "time-at-", 8))
4863 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
4865 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4868 case IPP_TAG_BOOLEAN
:
4869 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4872 case IPP_TAG_NOVALUE
:
4873 cupsFilePuts(out
, "novalue");
4876 case IPP_TAG_RANGE
:
4877 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4878 attr
->values
[i
].range
.upper
);
4881 case IPP_TAG_RESOLUTION
:
4882 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4883 attr
->values
[i
].resolution
.yres
,
4884 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4889 case IPP_TAG_STRING
:
4892 case IPP_TAG_KEYWORD
:
4893 case IPP_TAG_CHARSET
:
4894 case IPP_TAG_LANGUAGE
:
4895 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4898 * Need to quote strings for PS banners...
4903 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4905 if (*p
== '(' || *p
== ')' || *p
== '\\')
4907 cupsFilePutChar(out
, '\\');
4908 cupsFilePutChar(out
, *p
);
4910 else if (*p
< 32 || *p
> 126)
4911 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4913 cupsFilePutChar(out
, *p
);
4917 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4921 break; /* anti-compiler-warning-code */
4925 else if (ch
== '\\') /* Quoted char */
4927 ch
= cupsFileGetChar(in
);
4929 if (ch
!= '{') /* Only do special handling for \{ */
4930 cupsFilePutChar(out
, '\\');
4932 cupsFilePutChar(out
, ch
);
4935 cupsFilePutChar(out
, ch
);
4939 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4941 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4942 IPP_TAG_INTEGER
)) != NULL
)
4943 attr
->values
[0].integer
+= kbytes
;
4952 * 'copy_file()' - Copy a PPD file or interface script...
4955 static int /* O - 0 = success, -1 = error */
4956 copy_file(const char *from
, /* I - Source file */
4957 const char *to
) /* I - Destination file */
4959 cups_file_t
*src
, /* Source file */
4960 *dst
; /* Destination file */
4961 int bytes
; /* Bytes to read/write */
4962 char buffer
[2048]; /* Copy buffer */
4965 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4968 * Open the source and destination file for a copy...
4971 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4974 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4981 * Copy the source file to the destination...
4984 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4985 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
4993 * Close both files and return...
4998 return (cupsFileClose(dst
));
5003 * 'copy_model()' - Copy a PPD model file, substituting default values
5007 static int /* O - 0 = success, -1 = error */
5008 copy_model(cupsd_client_t
*con
, /* I - Client connection */
5009 const char *from
, /* I - Source file */
5010 const char *to
) /* I - Destination file */
5012 fd_set input
; /* select() input set */
5013 struct timeval timeout
; /* select() timeout */
5014 int maxfd
; /* Max file descriptor for select() */
5015 char tempfile
[1024]; /* Temporary PPD file */
5016 int tempfd
; /* Temporary PPD file descriptor */
5017 int temppid
; /* Process ID of cups-driverd */
5018 int temppipe
[2]; /* Temporary pipes */
5019 char *argv
[4], /* Command-line arguments */
5020 *envp
[MAX_ENV
]; /* Environment */
5021 cups_file_t
*src
, /* Source file */
5022 *dst
; /* Destination file */
5023 ppd_file_t
*ppd
; /* PPD file */
5024 int bytes
, /* Bytes from pipe */
5025 total
; /* Total bytes from pipe */
5026 char buffer
[2048]; /* Copy buffer */
5027 int i
; /* Looping var */
5028 char option
[PPD_MAX_NAME
], /* Option name */
5029 choice
[PPD_MAX_NAME
]; /* Choice name */
5030 int num_defaults
; /* Number of default options */
5031 cups_option_t
*defaults
; /* Default options */
5032 char cups_protocol
[PPD_MAX_LINE
];
5033 /* cupsProtocol attribute */
5036 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5037 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
5041 * Run cups-driverd to get the PPD file...
5044 argv
[0] = "cups-driverd";
5046 argv
[2] = (char *)from
;
5049 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5051 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
5052 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
5053 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
5057 cupsdOpenPipe(temppipe
);
5059 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5060 "copy_model: Running \"cups-driverd cat %s\"...", from
);
5062 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
5063 -1, -1, 0, DefaultProfile
, &temppid
))
5074 * Wait up to 30 seconds for the PPD file to be copied...
5079 if (temppipe
[0] > CGIPipes
[0])
5080 maxfd
= temppipe
[0] + 1;
5082 maxfd
= CGIPipes
[0] + 1;
5087 * See if we have data ready...
5091 FD_SET(temppipe
[0], &input
);
5092 FD_SET(CGIPipes
[0], &input
);
5094 timeout
.tv_sec
= 30;
5095 timeout
.tv_usec
= 0;
5097 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
5107 * We have timed out...
5113 if (FD_ISSET(temppipe
[0], &input
))
5116 * Read the PPD file from the pipe, and write it to the PPD file.
5119 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
5121 if (write(tempfd
, buffer
, bytes
) < bytes
)
5130 if (FD_ISSET(CGIPipes
[0], &input
))
5140 * No data from cups-deviced...
5143 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
5149 * Read the source file and see what page sizes are supported...
5152 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
5159 * Open the destination (if possible) and set the default options...
5164 cups_protocol
[0] = '\0';
5166 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
5169 * Read all of the default lines from the old PPD...
5172 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
5173 if (!strncmp(buffer
, "*Default", 8))
5176 * Add the default option...
5179 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5180 choice
, sizeof(choice
)))
5182 ppd_option_t
*ppdo
; /* PPD option */
5186 * Only add the default if the default hasn't already been
5187 * set and the choice exists in the new PPD...
5190 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
5191 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
5192 ppdFindChoice(ppdo
, choice
))
5193 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
5197 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
5198 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
5202 else if (ppdPageSize(ppd
, DefaultPaperSize
))
5205 * Add the default media sizes...
5208 num_defaults
= cupsAddOption("PageSize", DefaultPaperSize
,
5209 num_defaults
, &defaults
);
5210 num_defaults
= cupsAddOption("PageRegion", DefaultPaperSize
,
5211 num_defaults
, &defaults
);
5212 num_defaults
= cupsAddOption("PaperDimension", DefaultPaperSize
,
5213 num_defaults
, &defaults
);
5214 num_defaults
= cupsAddOption("ImageableArea", DefaultPaperSize
,
5215 num_defaults
, &defaults
);
5221 * Open the source file for a copy...
5224 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
5226 cupsFreeOptions(num_defaults
, defaults
);
5232 * Open the destination file for a copy...
5235 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
5237 cupsFreeOptions(num_defaults
, defaults
);
5244 * Copy the source file to the destination...
5247 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
5249 if (!strncmp(buffer
, "*Default", 8))
5252 * Check for an previous default option choice...
5255 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5256 choice
, sizeof(choice
)))
5258 const char *val
; /* Default option value */
5261 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
5264 * Substitute the previous choice...
5267 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
5272 cupsFilePrintf(dst
, "%s\n", buffer
);
5275 if (cups_protocol
[0])
5276 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
5278 cupsFreeOptions(num_defaults
, defaults
);
5281 * Close both files and return...
5288 return (cupsFileClose(dst
));
5293 * 'copy_job_attrs()' - Copy job attributes.
5297 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5298 cupsd_job_t
*job
, /* I - Job */
5299 cups_array_t
*ra
) /* I - Requested attributes array */
5301 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
5305 * Send the requested attributes for each job...
5308 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
5309 con
->servername
, con
->serverport
, "/jobs/%d",
5312 if (!ra
|| cupsArrayFind(ra
, "document-count"))
5313 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5314 "document-count", job
->num_files
);
5316 if (!ra
|| cupsArrayFind(ra
, "job-media-progress"))
5317 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5318 "job-media-progress", job
->progress
);
5320 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
5321 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5322 "job-more-info", NULL
, job_uri
);
5324 if (job
->state_value
> IPP_JOB_PROCESSING
&&
5325 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
5326 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
5327 job
->num_files
> 0);
5329 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
5330 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5331 "job-printer-up-time", time(NULL
));
5333 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
5334 add_job_state_reasons(con
, job
);
5336 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
5337 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5338 "job-uri", NULL
, job_uri
);
5340 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
5345 * 'copy_printer_attrs()' - Copy printer attributes.
5350 cupsd_client_t
*con
, /* I - Client connection */
5351 cupsd_printer_t
*printer
, /* I - Printer */
5352 cups_array_t
*ra
) /* I - Requested attributes array */
5354 char printer_uri
[HTTP_MAX_URI
];
5356 time_t curtime
; /* Current time */
5357 int i
; /* Looping var */
5358 ipp_attribute_t
*history
; /* History collection */
5362 * Copy the printer attributes to the response using requested-attributes
5363 * and document-format attributes that may be provided by the client.
5366 curtime
= time(NULL
);
5369 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
5370 printer
->recoverable
)
5371 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5372 "com.apple.print.recoverable-message", NULL
,
5373 printer
->recoverable
);
5374 #endif /* __APPLE__ */
5376 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
5377 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5378 "marker-change-time", printer
->marker_time
);
5380 if (printer
->num_printers
> 0 &&
5381 (!ra
|| cupsArrayFind(ra
, "member-uris")))
5383 ipp_attribute_t
*member_uris
; /* member-uris attribute */
5384 cupsd_printer_t
*p2
; /* Printer in class */
5385 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
5388 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
5389 IPP_TAG_URI
, "member-uris",
5390 printer
->num_printers
, NULL
,
5393 for (i
= 0; i
< printer
->num_printers
; i
++)
5395 p2
= printer
->printers
[i
];
5397 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
5398 IPP_TAG_URI
)) != NULL
)
5399 member_uris
->values
[i
].string
.text
=
5400 _cupsStrAlloc(p2_uri
->values
[0].string
.text
);
5403 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
5404 sizeof(printer_uri
), "ipp", NULL
, con
->servername
,
5406 (p2
->type
& CUPS_PRINTER_CLASS
) ?
5407 "/classes/%s" : "/printers/%s", p2
->name
);
5408 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
5414 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
5415 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
5416 "printer-alert", NULL
, printer
->alert
);
5418 if (printer
->alert_description
&&
5419 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
5420 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5421 "printer-alert-description", NULL
,
5422 printer
->alert_description
);
5424 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
5425 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
5426 ippTimeToDate(curtime
));
5429 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
5431 if (printer
->reg_name
)
5432 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5433 "printer-dns-sd-name", NULL
, printer
->reg_name
);
5435 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
5436 "printer-dns-sd-name", 0);
5438 #endif /* HAVE_DNSSD */
5440 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
5441 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5442 "printer-error-policy", NULL
, printer
->error_policy
);
5444 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
5445 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
5446 printer
->accepting
);
5448 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
5449 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
5452 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
5453 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5454 "printer-op-policy", NULL
, printer
->op_policy
);
5456 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
5457 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
5460 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
5461 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5462 "printer-state-change-time", printer
->state_time
);
5464 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
5465 cupsArrayFind(ra
, "printer-state-history"))
5468 * Printer history is only sent if specifically requested, so that
5469 * older CUPS/IPP clients won't barf on the collection attributes.
5472 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
5473 "printer-state-history",
5474 printer
->num_history
, NULL
);
5476 for (i
= 0; i
< printer
->num_history
; i
++)
5477 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
5478 NULL
, IPP_TAG_ZERO
, 0);
5481 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
5482 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5483 "printer-state-message", NULL
, printer
->state_message
);
5485 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
5486 add_printer_state_reasons(con
, printer
);
5488 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5490 int type
; /* printer-type value */
5494 * Add the CUPS-specific printer-type attribute...
5497 type
= printer
->type
;
5499 if (printer
== DefaultPrinter
)
5500 type
|= CUPS_PRINTER_DEFAULT
;
5502 if (!printer
->accepting
)
5503 type
|= CUPS_PRINTER_REJECTING
;
5505 if (!printer
->shared
)
5506 type
|= CUPS_PRINTER_NOT_SHARED
;
5508 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
5512 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5513 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5514 "printer-up-time", curtime
);
5516 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
5517 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
5520 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5521 "ipp", NULL
, con
->servername
, con
->serverport
,
5522 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5523 "/classes/%s" : "/printers/%s", printer
->name
);
5524 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5525 "printer-uri-supported", NULL
, printer_uri
);
5526 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5530 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5531 add_queued_job_count(con
, printer
);
5533 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
5534 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
5539 * 'copy_subscription_attrs()' - Copy subscription attributes.
5543 copy_subscription_attrs(
5544 cupsd_client_t
*con
, /* I - Client connection */
5545 cupsd_subscription_t
*sub
, /* I - Subscription */
5546 cups_array_t
*ra
) /* I - Requested attributes array */
5548 ipp_attribute_t
*attr
; /* Current attribute */
5549 char printer_uri
[HTTP_MAX_URI
];
5551 int count
; /* Number of events */
5552 unsigned mask
; /* Current event mask */
5553 const char *name
; /* Current event name */
5557 * Copy the subscription attributes to the response using the
5558 * requested-attributes attribute that may be provided by the client.
5561 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
5563 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5566 * Simple event list...
5569 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5570 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5571 "notify-events", NULL
, name
);
5576 * Complex event list...
5579 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5580 if (sub
->mask
& mask
)
5583 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5584 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5585 "notify-events", count
, NULL
, NULL
);
5587 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5588 if (sub
->mask
& mask
)
5590 attr
->values
[count
].string
.text
=
5591 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5598 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5599 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5600 "notify-job-id", sub
->job
->id
);
5602 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
5603 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5604 "notify-lease-duration", sub
->lease
);
5606 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5608 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5609 "ipp", NULL
, con
->servername
, con
->serverport
,
5610 "/printers/%s", sub
->dest
->name
);
5611 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5612 "notify-printer-uri", NULL
, printer_uri
);
5615 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
5616 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5617 "notify-recipient-uri", NULL
, sub
->recipient
);
5618 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
5619 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5620 "notify-pull-method", NULL
, "ippget");
5622 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
5623 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5624 "notify-subscriber-user-name", NULL
, sub
->owner
);
5626 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5627 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5628 "notify-subscription-id", sub
->id
);
5630 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
5631 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5632 "notify-time-interval", sub
->interval
);
5634 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5635 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5636 sub
->user_data
, sub
->user_data_len
);
5641 * 'create_job()' - Print a file to a printer or class.
5645 create_job(cupsd_client_t
*con
, /* I - Client connection */
5646 ipp_attribute_t
*uri
) /* I - Printer URI */
5648 cupsd_printer_t
*printer
; /* Printer */
5649 cupsd_job_t
*job
; /* New job */
5652 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5653 con
->http
.fd
, uri
->values
[0].string
.text
);
5656 * Is the destination valid?
5659 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5665 send_ipp_status(con
, IPP_NOT_FOUND
,
5666 _("The printer or class was not found."));
5671 * Create the job object...
5674 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5677 job
->pending_timeout
= 1;
5680 * Save and log the job...
5683 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5684 job
->dest
, job
->username
);
5689 * 'create_requested_array()' - Create an array for the requested-attributes.
5692 static cups_array_t
* /* O - Array of attributes or NULL */
5693 create_requested_array(ipp_t
*request
) /* I - IPP request */
5695 int i
; /* Looping var */
5696 ipp_attribute_t
*requested
; /* requested-attributes attribute */
5697 cups_array_t
*ra
; /* Requested attributes array */
5698 char *value
; /* Current value */
5702 * Get the requested-attributes attribute, and return NULL if we don't
5706 if ((requested
= ippFindAttribute(request
, "requested-attributes",
5707 IPP_TAG_KEYWORD
)) == NULL
)
5711 * If the attribute contains a single "all" keyword, return NULL...
5714 if (requested
->num_values
== 1 &&
5715 !strcmp(requested
->values
[0].string
.text
, "all"))
5719 * Create an array using "strcmp" as the comparison function...
5722 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
5724 for (i
= 0; i
< requested
->num_values
; i
++)
5726 value
= requested
->values
[i
].string
.text
;
5728 if (!strcmp(value
, "job-template"))
5730 cupsArrayAdd(ra
, "copies");
5731 cupsArrayAdd(ra
, "copies-default");
5732 cupsArrayAdd(ra
, "copies-supported");
5733 cupsArrayAdd(ra
, "finishings");
5734 cupsArrayAdd(ra
, "finishings-default");
5735 cupsArrayAdd(ra
, "finishings-supported");
5736 cupsArrayAdd(ra
, "job-hold-until");
5737 cupsArrayAdd(ra
, "job-hold-until-default");
5738 cupsArrayAdd(ra
, "job-hold-until-supported");
5739 cupsArrayAdd(ra
, "job-priority");
5740 cupsArrayAdd(ra
, "job-priority-default");
5741 cupsArrayAdd(ra
, "job-priority-supported");
5742 cupsArrayAdd(ra
, "job-sheets");
5743 cupsArrayAdd(ra
, "job-sheets-default");
5744 cupsArrayAdd(ra
, "job-sheets-supported");
5745 cupsArrayAdd(ra
, "media");
5746 cupsArrayAdd(ra
, "media-default");
5747 cupsArrayAdd(ra
, "media-supported");
5748 cupsArrayAdd(ra
, "multiple-document-handling");
5749 cupsArrayAdd(ra
, "multiple-document-handling-default");
5750 cupsArrayAdd(ra
, "multiple-document-handling-supported");
5751 cupsArrayAdd(ra
, "number-up");
5752 cupsArrayAdd(ra
, "number-up-default");
5753 cupsArrayAdd(ra
, "number-up-supported");
5754 cupsArrayAdd(ra
, "orientation-requested");
5755 cupsArrayAdd(ra
, "orientation-requested-default");
5756 cupsArrayAdd(ra
, "orientation-requested-supported");
5757 cupsArrayAdd(ra
, "page-ranges");
5758 cupsArrayAdd(ra
, "page-ranges-supported");
5759 cupsArrayAdd(ra
, "printer-resolution");
5760 cupsArrayAdd(ra
, "printer-resolution-default");
5761 cupsArrayAdd(ra
, "printer-resolution-supported");
5762 cupsArrayAdd(ra
, "print-quality");
5763 cupsArrayAdd(ra
, "print-quality-default");
5764 cupsArrayAdd(ra
, "print-quality-supported");
5765 cupsArrayAdd(ra
, "sides");
5766 cupsArrayAdd(ra
, "sides-default");
5767 cupsArrayAdd(ra
, "sides-supported");
5769 else if (!strcmp(value
, "job-description"))
5771 cupsArrayAdd(ra
, "date-time-at-completed");
5772 cupsArrayAdd(ra
, "date-time-at-creation");
5773 cupsArrayAdd(ra
, "date-time-at-processing");
5774 cupsArrayAdd(ra
, "job-detailed-status-message");
5775 cupsArrayAdd(ra
, "job-document-access-errors");
5776 cupsArrayAdd(ra
, "job-id");
5777 cupsArrayAdd(ra
, "job-impressions");
5778 cupsArrayAdd(ra
, "job-impressions-completed");
5779 cupsArrayAdd(ra
, "job-k-octets");
5780 cupsArrayAdd(ra
, "job-k-octets-processed");
5781 cupsArrayAdd(ra
, "job-media-progress");
5782 cupsArrayAdd(ra
, "job-media-sheets");
5783 cupsArrayAdd(ra
, "job-media-sheets-completed");
5784 cupsArrayAdd(ra
, "job-message-from-operator");
5785 cupsArrayAdd(ra
, "job-more-info");
5786 cupsArrayAdd(ra
, "job-name");
5787 cupsArrayAdd(ra
, "job-originating-user-name");
5788 cupsArrayAdd(ra
, "job-printer-up-time");
5789 cupsArrayAdd(ra
, "job-printer-uri");
5790 cupsArrayAdd(ra
, "job-state");
5791 cupsArrayAdd(ra
, "job-state-message");
5792 cupsArrayAdd(ra
, "job-state-reasons");
5793 cupsArrayAdd(ra
, "job-uri");
5794 cupsArrayAdd(ra
, "number-of-documents");
5795 cupsArrayAdd(ra
, "number-of-intervening-jobs");
5796 cupsArrayAdd(ra
, "output-device-assigned");
5797 cupsArrayAdd(ra
, "time-at-completed");
5798 cupsArrayAdd(ra
, "time-at-creation");
5799 cupsArrayAdd(ra
, "time-at-processing");
5801 else if (!strcmp(value
, "printer-description"))
5803 cupsArrayAdd(ra
, "charset-configured");
5804 cupsArrayAdd(ra
, "charset-supported");
5805 cupsArrayAdd(ra
, "color-supported");
5806 cupsArrayAdd(ra
, "compression-supported");
5807 cupsArrayAdd(ra
, "document-format-default");
5808 cupsArrayAdd(ra
, "document-format-supported");
5809 cupsArrayAdd(ra
, "generated-natural-language-supported");
5810 cupsArrayAdd(ra
, "ipp-versions-supported");
5811 cupsArrayAdd(ra
, "job-impressions-supported");
5812 cupsArrayAdd(ra
, "job-k-octets-supported");
5813 cupsArrayAdd(ra
, "job-media-sheets-supported");
5814 cupsArrayAdd(ra
, "job-settable-attributes-supported");
5815 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
5816 cupsArrayAdd(ra
, "multiple-operation-time-out");
5817 cupsArrayAdd(ra
, "natural-language-configured");
5818 cupsArrayAdd(ra
, "notify-attributes-supported");
5819 cupsArrayAdd(ra
, "notify-lease-duration-default");
5820 cupsArrayAdd(ra
, "notify-lease-duration-supported");
5821 cupsArrayAdd(ra
, "notify-max-events-supported");
5822 cupsArrayAdd(ra
, "notify-events-default");
5823 cupsArrayAdd(ra
, "notify-events-supported");
5824 cupsArrayAdd(ra
, "notify-pull-method-supported");
5825 cupsArrayAdd(ra
, "notify-schemes-supported");
5826 cupsArrayAdd(ra
, "operations-supported");
5827 cupsArrayAdd(ra
, "pages-per-minute");
5828 cupsArrayAdd(ra
, "pages-per-minute-color");
5829 cupsArrayAdd(ra
, "pdl-override-supported");
5830 cupsArrayAdd(ra
, "printer-alert");
5831 cupsArrayAdd(ra
, "printer-alert-description");
5832 cupsArrayAdd(ra
, "printer-commands");
5833 cupsArrayAdd(ra
, "printer-current-time");
5834 cupsArrayAdd(ra
, "printer-driver-installer");
5835 cupsArrayAdd(ra
, "printer-dns-sd-name");
5836 cupsArrayAdd(ra
, "printer-info");
5837 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
5838 cupsArrayAdd(ra
, "printer-location");
5839 cupsArrayAdd(ra
, "printer-make-and-model");
5840 cupsArrayAdd(ra
, "printer-message-from-operator");
5841 cupsArrayAdd(ra
, "printer-more-info");
5842 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
5843 cupsArrayAdd(ra
, "printer-name");
5844 cupsArrayAdd(ra
, "printer-state");
5845 cupsArrayAdd(ra
, "printer-state-message");
5846 cupsArrayAdd(ra
, "printer-state-reasons");
5847 cupsArrayAdd(ra
, "printer-settable-attributes-supported");
5848 cupsArrayAdd(ra
, "printer-type");
5849 cupsArrayAdd(ra
, "printer-up-time");
5850 cupsArrayAdd(ra
, "printer-uri-supported");
5851 cupsArrayAdd(ra
, "queued-job-count");
5852 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
5853 cupsArrayAdd(ra
, "uri-authentication-supported");
5854 cupsArrayAdd(ra
, "uri-security-supported");
5856 else if (!strcmp(value
, "printer-defaults"))
5858 char *name
; /* Option name */
5861 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5863 name
= (char *)cupsArrayNext(CommonDefaults
))
5864 cupsArrayAdd(ra
, name
);
5866 else if (!strcmp(value
, "subscription-template"))
5868 cupsArrayAdd(ra
, "notify-attributes");
5869 cupsArrayAdd(ra
, "notify-charset");
5870 cupsArrayAdd(ra
, "notify-events");
5871 cupsArrayAdd(ra
, "notify-lease-duration");
5872 cupsArrayAdd(ra
, "notify-natural-language");
5873 cupsArrayAdd(ra
, "notify-pull-method");
5874 cupsArrayAdd(ra
, "notify-recipient-uri");
5875 cupsArrayAdd(ra
, "notify-time-interval");
5876 cupsArrayAdd(ra
, "notify-user-data");
5879 cupsArrayAdd(ra
, value
);
5887 * 'create_subscription()' - Create a notification subscription.
5891 create_subscription(
5892 cupsd_client_t
*con
, /* I - Client connection */
5893 ipp_attribute_t
*uri
) /* I - Printer URI */
5895 http_status_t status
; /* Policy status */
5896 int i
; /* Looping var */
5897 ipp_attribute_t
*attr
; /* Current attribute */
5898 cups_ptype_t dtype
; /* Destination type (printer/class) */
5899 char scheme
[HTTP_MAX_URI
],
5900 /* Scheme portion of URI */
5901 userpass
[HTTP_MAX_URI
],
5902 /* Username portion of URI */
5904 /* Host portion of URI */
5905 resource
[HTTP_MAX_URI
];
5906 /* Resource portion of URI */
5907 int port
; /* Port portion of URI */
5908 cupsd_printer_t
*printer
; /* Printer/class */
5909 cupsd_job_t
*job
; /* Job */
5910 int jobid
; /* Job ID */
5911 cupsd_subscription_t
*sub
; /* Subscription object */
5912 const char *username
, /* requesting-user-name or
5913 authenticated username */
5914 *recipient
, /* notify-recipient-uri */
5915 *pullmethod
; /* notify-pull-method */
5916 ipp_attribute_t
*user_data
; /* notify-user-data */
5917 int interval
, /* notify-time-interval */
5918 lease
; /* notify-lease-duration */
5919 unsigned mask
; /* notify-events */
5920 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5921 *notify_lease
; /* notify-lease-duration(-default) */
5925 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5927 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5928 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5929 attr
->value_tag
, attr
->name
);
5931 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5936 * Is the destination valid?
5939 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5940 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5941 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5943 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5944 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5945 sizeof(host
), &port
, resource
, sizeof(resource
));
5947 if (!strcmp(resource
, "/"))
5949 dtype
= (cups_ptype_t
)0;
5952 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5954 dtype
= (cups_ptype_t
)0;
5957 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5959 dtype
= CUPS_PRINTER_CLASS
;
5962 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5968 send_ipp_status(con
, IPP_NOT_FOUND
,
5969 _("The printer or class was not found."));
5979 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5982 send_http_error(con
, status
, printer
);
5986 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5988 send_http_error(con
, status
, NULL
);
5993 * Get the user that is requesting the subscription...
5996 username
= get_username(con
);
5999 * Find the first subscription group attribute; return if we have
6003 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
6004 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
6009 send_ipp_status(con
, IPP_BAD_REQUEST
,
6010 _("No subscription attributes in request!"));
6015 * Process the subscription attributes in the request...
6018 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
6026 lease
= DefaultLeaseDuration
;
6028 mask
= CUPSD_EVENT_NONE
;
6032 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
6034 notify_lease
= ippFindAttribute(printer
->attrs
,
6035 "notify-lease-duration-default",
6039 lease
= notify_lease
->values
[0].integer
;
6043 notify_events
= NULL
;
6044 notify_lease
= NULL
;
6047 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
6049 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
6050 attr
->value_tag
== IPP_TAG_URI
)
6053 * Validate the recipient scheme against the ServerBin/notifier
6057 char notifier
[1024]; /* Notifier filename */
6060 recipient
= attr
->values
[0].string
.text
;
6062 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
6063 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
6064 host
, sizeof(host
), &port
,
6065 resource
, sizeof(resource
)) < HTTP_URI_OK
)
6067 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6068 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
6069 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6070 "notify-status-code", IPP_URI_SCHEME
);
6074 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
6076 if (access(notifier
, X_OK
))
6078 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6079 _("notify-recipient-uri URI \"%s\" uses unknown "
6080 "scheme!"), recipient
);
6081 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6082 "notify-status-code", IPP_URI_SCHEME
);
6086 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
6088 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6089 _("notify-recipient-uri URI \"%s\" is already used!"),
6091 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6092 "notify-status-code", IPP_ATTRIBUTES
);
6096 else if (!strcmp(attr
->name
, "notify-pull-method") &&
6097 attr
->value_tag
== IPP_TAG_KEYWORD
)
6099 pullmethod
= attr
->values
[0].string
.text
;
6101 if (strcmp(pullmethod
, "ippget"))
6103 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6104 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
6105 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6106 "notify-status-code", IPP_ATTRIBUTES
);
6110 else if (!strcmp(attr
->name
, "notify-charset") &&
6111 attr
->value_tag
== IPP_TAG_CHARSET
&&
6112 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
6113 strcmp(attr
->values
[0].string
.text
, "utf-8"))
6115 send_ipp_status(con
, IPP_CHARSET
,
6116 _("Character set \"%s\" not supported!"),
6117 attr
->values
[0].string
.text
);
6120 else if (!strcmp(attr
->name
, "notify-natural-language") &&
6121 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
6122 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
6124 send_ipp_status(con
, IPP_CHARSET
,
6125 _("Language \"%s\" not supported!"),
6126 attr
->values
[0].string
.text
);
6129 else if (!strcmp(attr
->name
, "notify-user-data") &&
6130 attr
->value_tag
== IPP_TAG_STRING
)
6132 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
6134 send_ipp_status(con
, IPP_REQUEST_VALUE
,
6135 _("The notify-user-data value is too large "
6136 "(%d > 63 octets)!"),
6137 attr
->values
[0].unknown
.length
);
6143 else if (!strcmp(attr
->name
, "notify-events") &&
6144 attr
->value_tag
== IPP_TAG_KEYWORD
)
6145 notify_events
= attr
;
6146 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
6147 attr
->value_tag
== IPP_TAG_INTEGER
)
6148 lease
= attr
->values
[0].integer
;
6149 else if (!strcmp(attr
->name
, "notify-time-interval") &&
6150 attr
->value_tag
== IPP_TAG_INTEGER
)
6151 interval
= attr
->values
[0].integer
;
6152 else if (!strcmp(attr
->name
, "notify-job-id") &&
6153 attr
->value_tag
== IPP_TAG_INTEGER
)
6154 jobid
= attr
->values
[0].integer
;
6161 for (i
= 0; i
< notify_events
->num_values
; i
++)
6162 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
6166 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
6168 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
6169 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
6170 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
6172 if (!recipient
&& !pullmethod
)
6175 if (mask
== CUPSD_EVENT_NONE
)
6178 mask
= CUPSD_EVENT_JOB_COMPLETED
;
6180 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
6183 send_ipp_status(con
, IPP_BAD_REQUEST
,
6184 _("notify-events not specified!"));
6189 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
6191 cupsdLogMessage(CUPSD_LOG_INFO
,
6192 "create_subscription: Limiting notify-lease-duration to "
6195 lease
= MaxLeaseDuration
;
6200 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6202 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
6209 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
6212 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
6215 cupsdLogMessage(CUPSD_LOG_DEBUG
,
6216 "Added subscription %d for printer \"%s\"",
6217 sub
->id
, printer
->name
);
6219 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
6222 sub
->interval
= interval
;
6224 sub
->expire
= lease
? time(NULL
) + lease
: 0;
6226 cupsdSetString(&sub
->owner
, username
);
6230 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
6231 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
6232 sub
->user_data_len
);
6235 ippAddSeparator(con
->response
);
6236 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
6237 "notify-subscription-id", sub
->id
);
6239 con
->response
->request
.status
.status_code
= IPP_OK
;
6245 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
6250 * 'delete_printer()' - Remove a printer or class from the system.
6254 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
6255 ipp_attribute_t
*uri
) /* I - URI of printer or class */
6257 http_status_t status
; /* Policy status */
6258 cups_ptype_t dtype
; /* Destination type (printer/class) */
6259 cupsd_printer_t
*printer
; /* Printer/class */
6260 char filename
[1024]; /* Script/PPD filename */
6263 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
6264 con
->http
.fd
, uri
->values
[0].string
.text
);
6267 * Do we have a valid URI?
6270 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6276 send_ipp_status(con
, IPP_NOT_FOUND
,
6277 _("The printer or class was not found."));
6285 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6287 send_http_error(con
, status
, NULL
);
6292 * Remove old jobs...
6295 cupsdCancelJobs(printer
->name
, NULL
, 1);
6298 * Remove old subscriptions and send a "deleted printer" event...
6301 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
6302 "%s \"%s\" deleted by \"%s\".",
6303 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
6304 printer
->name
, get_username(con
));
6306 cupsdExpireSubscriptions(printer
, NULL
);
6309 * Remove any old PPD or script files...
6312 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
6316 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6322 * Unregister color profiles...
6325 apple_unregister_profiles(printer
);
6326 #endif /* __APPLE__ */
6328 if (dtype
& CUPS_PRINTER_CLASS
)
6330 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
6331 printer
->name
, get_username(con
));
6333 cupsdDeletePrinter(printer
, 0);
6334 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6338 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
6339 printer
->name
, get_username(con
));
6341 cupsdDeletePrinter(printer
, 0);
6342 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
6345 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
6348 * Return with no errors...
6351 con
->response
->request
.status
.status_code
= IPP_OK
;
6356 * 'get_default()' - Get the default destination.
6360 get_default(cupsd_client_t
*con
) /* I - Client connection */
6362 http_status_t status
; /* Policy status */
6363 cups_array_t
*ra
; /* Requested attributes array */
6366 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
6372 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6374 send_http_error(con
, status
, NULL
);
6380 ra
= create_requested_array(con
->request
);
6382 copy_printer_attrs(con
, DefaultPrinter
, ra
);
6384 cupsArrayDelete(ra
);
6386 con
->response
->request
.status
.status_code
= IPP_OK
;
6389 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
6394 * 'get_devices()' - Get the list of available devices on the local system.
6398 get_devices(cupsd_client_t
*con
) /* I - Client connection */
6400 http_status_t status
; /* Policy status */
6401 ipp_attribute_t
*limit
, /* limit attribute */
6402 *timeout
, /* timeout attribute */
6403 *requested
, /* requested-attributes attribute */
6404 *exclude
; /* exclude-schemes attribute */
6405 char command
[1024], /* cups-deviced command */
6406 options
[1024], /* Options to pass to command */
6408 /* String for requested attributes */
6410 /* String for excluded attributes */
6413 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
6419 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6421 send_http_error(con
, status
, NULL
);
6426 * Run cups-deviced command with the given options...
6429 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6430 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
6431 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6433 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
6436 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6438 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6441 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6443 exclude_str
[0] = '\0';
6445 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
6446 snprintf(options
, sizeof(options
),
6447 "%d+%d+%d+%d+%s%s%s",
6448 con
->request
->request
.op
.request_id
,
6449 limit
? limit
->values
[0].integer
: 0,
6450 timeout
? timeout
->values
[0].integer
: 10,
6453 exclude_str
[0] ? "%20" : "", exclude_str
);
6455 if (cupsdSendCommand(con
, command
, options
, 1))
6458 * Command started successfully, don't send an IPP response here...
6461 ippDelete(con
->response
);
6462 con
->response
= NULL
;
6467 * Command failed, return "internal error" so the user knows something
6471 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6472 _("cups-deviced failed to execute."));
6478 * 'get_document()' - Get a copy of a job file.
6482 get_document(cupsd_client_t
*con
, /* I - Client connection */
6483 ipp_attribute_t
*uri
) /* I - Job URI */
6485 http_status_t status
; /* Policy status */
6486 ipp_attribute_t
*attr
; /* Current attribute */
6487 int jobid
; /* Job ID */
6488 int docnum
; /* Document number */
6489 cupsd_job_t
*job
; /* Current job */
6490 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6491 username
[HTTP_MAX_URI
], /* Username portion of URI */
6492 host
[HTTP_MAX_URI
], /* Host portion of URI */
6493 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6494 int port
; /* Port portion of URI */
6495 char filename
[1024], /* Filename for document */
6496 format
[1024]; /* Format for document */
6499 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6500 con
->http
.fd
, uri
->values
[0].string
.text
);
6503 * See if we have a job URI or a printer URI...
6506 if (!strcmp(uri
->name
, "printer-uri"))
6509 * Got a printer URI; see if we also have a job-id attribute...
6512 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6513 IPP_TAG_INTEGER
)) == NULL
)
6515 send_ipp_status(con
, IPP_BAD_REQUEST
,
6516 _("Got a printer-uri attribute but no job-id!"));
6520 jobid
= attr
->values
[0].integer
;
6525 * Got a job URI; parse it to get the job ID...
6528 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6529 sizeof(method
), username
, sizeof(username
), host
,
6530 sizeof(host
), &port
, resource
, sizeof(resource
));
6532 if (strncmp(resource
, "/jobs/", 6))
6538 send_ipp_status(con
, IPP_BAD_REQUEST
,
6539 _("Bad job-uri attribute \"%s\"!"),
6540 uri
->values
[0].string
.text
);
6544 jobid
= atoi(resource
+ 6);
6548 * See if the job exists...
6551 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6554 * Nope - return a "not found" error...
6557 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6565 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6567 send_http_error(con
, status
, NULL
);
6572 * Get the document number...
6575 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6576 IPP_TAG_INTEGER
)) == NULL
)
6578 send_ipp_status(con
, IPP_BAD_REQUEST
,
6579 _("Missing document-number attribute!"));
6583 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6584 attr
->num_values
> 1)
6586 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
6591 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6593 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6595 cupsdLogMessage(CUPSD_LOG_ERROR
,
6596 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6598 send_ipp_status(con
, IPP_NOT_FOUND
,
6599 _("Unable to open document %d in job %d!"), docnum
, jobid
);
6603 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6607 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6608 job
->filetypes
[docnum
- 1]->type
);
6610 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6612 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6614 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6615 IPP_TAG_NAME
)) != NULL
)
6616 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6617 NULL
, attr
->values
[0].string
.text
);
6622 * 'get_job_attrs()' - Get job attributes.
6626 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6627 ipp_attribute_t
*uri
) /* I - Job URI */
6629 http_status_t status
; /* Policy status */
6630 ipp_attribute_t
*attr
; /* Current attribute */
6631 int jobid
; /* Job ID */
6632 cupsd_job_t
*job
; /* Current job */
6633 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6634 username
[HTTP_MAX_URI
], /* Username portion of URI */
6635 host
[HTTP_MAX_URI
], /* Host portion of URI */
6636 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6637 int port
; /* Port portion of URI */
6638 cups_array_t
*ra
; /* Requested attributes array */
6641 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6642 con
->http
.fd
, uri
->values
[0].string
.text
);
6645 * See if we have a job URI or a printer URI...
6648 if (!strcmp(uri
->name
, "printer-uri"))
6651 * Got a printer URI; see if we also have a job-id attribute...
6654 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6655 IPP_TAG_INTEGER
)) == NULL
)
6657 send_ipp_status(con
, IPP_BAD_REQUEST
,
6658 _("Got a printer-uri attribute but no job-id!"));
6662 jobid
= attr
->values
[0].integer
;
6667 * Got a job URI; parse it to get the job ID...
6670 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6671 sizeof(method
), username
, sizeof(username
), host
,
6672 sizeof(host
), &port
, resource
, sizeof(resource
));
6674 if (strncmp(resource
, "/jobs/", 6))
6680 send_ipp_status(con
, IPP_BAD_REQUEST
,
6681 _("Bad job-uri attribute \"%s\"!"),
6682 uri
->values
[0].string
.text
);
6686 jobid
= atoi(resource
+ 6);
6690 * See if the job exists...
6693 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6696 * Nope - return a "not found" error...
6699 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6707 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6709 send_http_error(con
, status
, NULL
);
6714 * Copy attributes...
6719 ra
= create_requested_array(con
->request
);
6720 copy_job_attrs(con
, job
, ra
);
6721 cupsArrayDelete(ra
);
6723 con
->response
->request
.status
.status_code
= IPP_OK
;
6728 * 'get_jobs()' - Get a list of jobs for the specified printer.
6732 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6733 ipp_attribute_t
*uri
) /* I - Printer URI */
6735 http_status_t status
; /* Policy status */
6736 ipp_attribute_t
*attr
; /* Current attribute */
6737 const char *dest
; /* Destination */
6738 cups_ptype_t dtype
; /* Destination type (printer/class) */
6739 cups_ptype_t dmask
; /* Destination type mask */
6740 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6741 username
[HTTP_MAX_URI
], /* Username portion of URI */
6742 host
[HTTP_MAX_URI
], /* Host portion of URI */
6743 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6744 int port
; /* Port portion of URI */
6745 int completed
; /* Completed jobs? */
6746 int first_job_id
; /* First job ID */
6747 int limit
; /* Maximum number of jobs to return */
6748 int count
; /* Number of jobs that match */
6749 cupsd_job_t
*job
; /* Current job pointer */
6750 cupsd_printer_t
*printer
; /* Printer */
6751 cups_array_t
*list
; /* Which job list... */
6752 cups_array_t
*ra
; /* Requested attributes array */
6755 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
6756 uri
->values
[0].string
.text
);
6759 * Is the destination valid?
6762 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6763 sizeof(scheme
), username
, sizeof(username
), host
,
6764 sizeof(host
), &port
, resource
, sizeof(resource
));
6766 if (!strcmp(resource
, "/") ||
6767 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
6770 dtype
= (cups_ptype_t
)0;
6771 dmask
= (cups_ptype_t
)0;
6774 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6777 dtype
= (cups_ptype_t
)0;
6778 dmask
= CUPS_PRINTER_CLASS
;
6781 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6784 dtype
= CUPS_PRINTER_CLASS
;
6785 dmask
= CUPS_PRINTER_CLASS
;
6788 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6795 send_ipp_status(con
, IPP_NOT_FOUND
,
6796 _("The printer or class was not found."));
6801 dtype
&= CUPS_PRINTER_CLASS
;
6802 dmask
= CUPS_PRINTER_CLASS
;
6811 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6814 send_http_error(con
, status
, printer
);
6818 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6820 send_http_error(con
, status
, NULL
);
6825 * See if the "which-jobs" attribute have been specified...
6828 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6829 IPP_TAG_KEYWORD
)) != NULL
&&
6830 !strcmp(attr
->values
[0].string
.text
, "completed"))
6835 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
6840 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "printing"))
6843 list
= PrintingJobs
;
6852 * See if they want to limit the number of jobs reported...
6855 if ((attr
= ippFindAttribute(con
->request
, "limit",
6856 IPP_TAG_INTEGER
)) != NULL
)
6857 limit
= attr
->values
[0].integer
;
6861 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
6862 IPP_TAG_INTEGER
)) != NULL
)
6863 first_job_id
= attr
->values
[0].integer
;
6868 * See if we only want to see jobs for a specific user...
6871 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
6872 IPP_TAG_BOOLEAN
)) != NULL
&&
6873 attr
->values
[0].boolean
)
6874 strlcpy(username
, get_username(con
), sizeof(username
));
6878 ra
= create_requested_array(con
->request
);
6881 * OK, build a list of jobs for this printer...
6884 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6885 (limit
<= 0 || count
< limit
) && job
;
6886 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6889 * Filter out jobs that don't match...
6892 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
6894 if (!job
->dest
|| !job
->username
)
6897 if (!job
->dest
|| !job
->username
)
6900 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6901 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6903 if ((job
->dtype
& dmask
) != dtype
&&
6904 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6906 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
6909 if (job
->id
< first_job_id
)
6917 if (username
[0] && strcasecmp(username
, job
->username
))
6921 ippAddSeparator(con
->response
);
6925 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
6927 copy_job_attrs(con
, job
, ra
);
6930 cupsArrayDelete(ra
);
6932 con
->response
->request
.status
.status_code
= IPP_OK
;
6937 * 'get_notifications()' - Get events for a subscription.
6941 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6943 int i
, j
; /* Looping vars */
6944 http_status_t status
; /* Policy status */
6945 cupsd_subscription_t
*sub
; /* Subscription */
6946 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6947 *sequences
; /* notify-sequence-numbers */
6948 int min_seq
; /* Minimum sequence number */
6949 int interval
; /* Poll interval */
6952 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6956 * Get subscription attributes...
6959 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6961 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6966 send_ipp_status(con
, IPP_BAD_REQUEST
,
6967 _("Missing notify-subscription-ids attribute!"));
6972 * Are the subscription IDs valid?
6975 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6977 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6980 * Bad subscription ID...
6983 send_ipp_status(con
, IPP_NOT_FOUND
,
6984 _("notify-subscription-id %d no good!"),
6985 ids
->values
[i
].integer
);
6993 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6995 con
, sub
->owner
)) != HTTP_OK
)
6997 send_http_error(con
, status
, sub
->dest
);
7002 * Check the subscription type and update the interval accordingly.
7005 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
7008 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
7010 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
7016 * Tell the client to poll again in N seconds...
7020 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
7021 "notify-get-interval", interval
);
7023 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
7024 "printer-up-time", time(NULL
));
7027 * Copy the subscription event attributes to the response.
7030 con
->response
->request
.status
.status_code
=
7031 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
7033 for (i
= 0; i
< ids
->num_values
; i
++)
7036 * Get the subscription and sequence number...
7039 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
7041 if (sequences
&& i
< sequences
->num_values
)
7042 min_seq
= sequences
->values
[i
].integer
;
7047 * If we don't have any new events, nothing to do here...
7050 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
7054 * Otherwise copy all of the new events...
7057 if (sub
->first_event_id
> min_seq
)
7060 j
= min_seq
- sub
->first_event_id
;
7062 for (; j
< sub
->num_events
; j
++)
7064 ippAddSeparator(con
->response
);
7066 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
7067 IPP_TAG_EVENT_NOTIFICATION
, 0);
7074 * 'get_ppd()' - Get a named PPD from the local system.
7078 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
7079 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
7081 http_status_t status
; /* Policy status */
7082 cupsd_printer_t
*dest
; /* Destination */
7083 cups_ptype_t dtype
; /* Destination type */
7086 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
7087 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
7089 if (!strcmp(uri
->name
, "ppd-name"))
7092 * Return a PPD file from cups-driverd...
7095 char command
[1024], /* cups-driverd command */
7096 options
[1024], /* Options to pass to command */
7097 ppd_name
[1024]; /* ppd-name */
7104 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7106 send_http_error(con
, status
, NULL
);
7111 * Run cups-driverd command with the given options...
7114 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7115 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
7116 snprintf(options
, sizeof(options
), "get+%d+%s",
7117 con
->request
->request
.op
.request_id
, ppd_name
);
7119 if (cupsdSendCommand(con
, command
, options
, 0))
7122 * Command started successfully, don't send an IPP response here...
7125 ippDelete(con
->response
);
7126 con
->response
= NULL
;
7131 * Command failed, return "internal error" so the user knows something
7135 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7136 _("cups-driverd failed to execute."));
7139 else if (!strcmp(uri
->name
, "printer-uri") &&
7140 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
7142 int i
; /* Looping var */
7143 char filename
[1024]; /* PPD filename */
7150 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7152 send_http_error(con
, status
, dest
);
7157 * See if we need the PPD for a class or remote printer...
7160 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7163 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
7165 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7166 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7167 "printer-uri", NULL
, dest
->uri
);
7170 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
7172 for (i
= 0; i
< dest
->num_printers
; i
++)
7173 if (!(dest
->printers
[i
]->type
&
7174 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
7176 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7177 dest
->printers
[i
]->name
);
7179 if (!access(filename
, 0))
7183 if (i
< dest
->num_printers
)
7184 dest
= dest
->printers
[i
];
7187 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7188 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7189 "printer-uri", NULL
, dest
->printers
[0]->uri
);
7195 * Found the printer with the PPD file, now see if there is one...
7198 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
7200 send_ipp_status(con
, IPP_NOT_FOUND
,
7201 _("The PPD file \"%s\" could not be opened: %s"),
7202 uri
->values
[0].string
.text
, strerror(errno
));
7206 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
7210 con
->response
->request
.status
.status_code
= IPP_OK
;
7213 send_ipp_status(con
, IPP_NOT_FOUND
,
7214 _("The PPD file \"%s\" could not be found."),
7215 uri
->values
[0].string
.text
);
7220 * 'get_ppds()' - Get the list of PPD files on the local system.
7224 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
7226 http_status_t status
; /* Policy status */
7227 ipp_attribute_t
*limit
, /* Limit attribute */
7228 *device
, /* ppd-device-id attribute */
7229 *language
, /* ppd-natural-language attribute */
7230 *make
, /* ppd-make attribute */
7231 *model
, /* ppd-make-and-model attribute */
7232 *model_number
, /* ppd-model-number attribute */
7233 *product
, /* ppd-product attribute */
7234 *psversion
, /* ppd-psverion attribute */
7235 *type
, /* ppd-type attribute */
7236 *requested
; /* requested-attributes attribute */
7237 char command
[1024], /* cups-driverd command */
7238 options
[1024], /* Options to pass to command */
7239 device_str
[256],/* Escaped ppd-device-id string */
7241 /* Escaped ppd-natural-language */
7242 make_str
[256], /* Escaped ppd-make string */
7243 model_str
[256], /* Escaped ppd-make-and-model string */
7244 model_number_str
[256],
7245 /* ppd-model-number string */
7247 /* Escaped ppd-product string */
7249 /* Escaped ppd-psversion string */
7250 type_str
[256], /* Escaped ppd-type string */
7252 /* String for requested attributes */
7255 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
7261 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7263 send_http_error(con
, status
, NULL
);
7268 * Run cups-driverd command with the given options...
7271 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
7272 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
7273 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
7275 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
7276 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
7278 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
7280 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
7281 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
7282 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
7283 requested
= ippFindAttribute(con
->request
, "requested-attributes",
7287 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
7289 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
7292 url_encode_attr(device
, device_str
, sizeof(device_str
));
7294 device_str
[0] = '\0';
7297 url_encode_attr(language
, language_str
, sizeof(language_str
));
7299 language_str
[0] = '\0';
7302 url_encode_attr(make
, make_str
, sizeof(make_str
));
7307 url_encode_attr(model
, model_str
, sizeof(model_str
));
7309 model_str
[0] = '\0';
7312 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
7313 model_number
->values
[0].integer
);
7315 model_number_str
[0] = '\0';
7318 url_encode_attr(product
, product_str
, sizeof(product_str
));
7320 product_str
[0] = '\0';
7323 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
7325 psversion_str
[0] = '\0';
7328 url_encode_attr(type
, type_str
, sizeof(type_str
));
7332 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7333 snprintf(options
, sizeof(options
),
7334 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7335 con
->request
->request
.op
.request_id
,
7336 limit
? limit
->values
[0].integer
: 0,
7338 device
? "%20" : "", device_str
,
7339 language
? "%20" : "", language_str
,
7340 make
? "%20" : "", make_str
,
7341 model
? "%20" : "", model_str
,
7342 model_number
? "%20" : "", model_number_str
,
7343 product
? "%20" : "", product_str
,
7344 psversion
? "%20" : "", psversion_str
,
7345 type
? "%20" : "", type_str
);
7347 if (cupsdSendCommand(con
, command
, options
, 0))
7350 * Command started successfully, don't send an IPP response here...
7353 ippDelete(con
->response
);
7354 con
->response
= NULL
;
7359 * Command failed, return "internal error" so the user knows something
7363 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7364 _("cups-driverd failed to execute."));
7370 * 'get_printer_attrs()' - Get printer attributes.
7374 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
7375 ipp_attribute_t
*uri
) /* I - Printer URI */
7377 http_status_t status
; /* Policy status */
7378 cups_ptype_t dtype
; /* Destination type (printer/class) */
7379 cupsd_printer_t
*printer
; /* Printer/class */
7380 cups_array_t
*ra
; /* Requested attributes array */
7383 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
7384 con
->http
.fd
, uri
->values
[0].string
.text
);
7387 * Is the destination valid?
7390 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7396 send_ipp_status(con
, IPP_NOT_FOUND
,
7397 _("The printer or class was not found."));
7405 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7407 send_http_error(con
, status
, printer
);
7412 * Send the attributes...
7415 ra
= create_requested_array(con
->request
);
7417 copy_printer_attrs(con
, printer
, ra
);
7419 cupsArrayDelete(ra
);
7421 con
->response
->request
.status
.status_code
= IPP_OK
;
7426 * 'get_printers()' - Get a list of printers or classes.
7430 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7431 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7433 http_status_t status
; /* Policy status */
7434 ipp_attribute_t
*attr
; /* Current attribute */
7435 int limit
; /* Max number of printers to return */
7436 int count
; /* Number of printers that match */
7437 cupsd_printer_t
*printer
; /* Current printer pointer */
7438 int printer_type
, /* printer-type attribute */
7439 printer_mask
; /* printer-type-mask attribute */
7440 char *location
; /* Location string */
7441 const char *username
; /* Current user */
7442 char *first_printer_name
; /* first-printer-name attribute */
7443 cups_array_t
*ra
; /* Requested attributes array */
7446 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7447 con
->http
.fd
, type
);
7453 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7455 send_http_error(con
, status
, NULL
);
7460 * Check for printers...
7463 if (!Printers
|| !cupsArrayCount(Printers
))
7465 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7470 * See if they want to limit the number of printers reported...
7473 if ((attr
= ippFindAttribute(con
->request
, "limit",
7474 IPP_TAG_INTEGER
)) != NULL
)
7475 limit
= attr
->values
[0].integer
;
7479 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7480 IPP_TAG_NAME
)) != NULL
)
7481 first_printer_name
= attr
->values
[0].string
.text
;
7483 first_printer_name
= NULL
;
7486 * Support filtering...
7489 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7490 IPP_TAG_ENUM
)) != NULL
)
7491 printer_type
= attr
->values
[0].integer
;
7495 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7496 IPP_TAG_ENUM
)) != NULL
)
7497 printer_mask
= attr
->values
[0].integer
;
7501 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7502 IPP_TAG_TEXT
)) != NULL
)
7503 location
= attr
->values
[0].string
.text
;
7507 if (con
->username
[0])
7508 username
= con
->username
;
7509 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7510 IPP_TAG_NAME
)) != NULL
)
7511 username
= attr
->values
[0].string
.text
;
7515 ra
= create_requested_array(con
->request
);
7518 * OK, build a list of printers for this printer...
7521 if (first_printer_name
)
7523 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7524 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7527 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7530 count
< limit
&& printer
;
7531 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7533 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7534 (printer
->type
& printer_mask
) == printer_type
&&
7536 (printer
->location
&& !strcasecmp(printer
->location
, location
))))
7539 * If HideImplicitMembers is enabled, see if this printer or class
7540 * is a member of an implicit class...
7543 if (ImplicitClasses
&& HideImplicitMembers
&&
7544 printer
->in_implicit_class
)
7548 * If a username is specified, see if it is allowed or denied
7552 if (!(printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
7553 printer
->num_users
&& username
&& !user_allowed(printer
, username
))
7557 * Add the group separator as needed...
7561 ippAddSeparator(con
->response
);
7566 * Send the attributes...
7569 copy_printer_attrs(con
, printer
, ra
);
7573 cupsArrayDelete(ra
);
7575 con
->response
->request
.status
.status_code
= IPP_OK
;
7580 * 'get_subscription_attrs()' - Get subscription attributes.
7584 get_subscription_attrs(
7585 cupsd_client_t
*con
, /* I - Client connection */
7586 int sub_id
) /* I - Subscription ID */
7588 http_status_t status
; /* Policy status */
7589 cupsd_subscription_t
*sub
; /* Subscription */
7590 cups_array_t
*ra
; /* Requested attributes array */
7593 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7594 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7595 con
, con
->http
.fd
, sub_id
);
7598 * Is the subscription ID valid?
7601 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7604 * Bad subscription ID...
7607 send_ipp_status(con
, IPP_NOT_FOUND
,
7608 _("notify-subscription-id %d no good!"), sub_id
);
7616 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7618 con
, sub
->owner
)) != HTTP_OK
)
7620 send_http_error(con
, status
, sub
->dest
);
7625 * Copy the subscription attributes to the response using the
7626 * requested-attributes attribute that may be provided by the client.
7629 ra
= create_requested_array(con
->request
);
7631 copy_subscription_attrs(con
, sub
, ra
);
7633 cupsArrayDelete(ra
);
7635 con
->response
->request
.status
.status_code
= IPP_OK
;
7640 * 'get_subscriptions()' - Get subscriptions.
7644 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7645 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7647 http_status_t status
; /* Policy status */
7648 int count
; /* Number of subscriptions */
7649 int limit
; /* Limit */
7650 cupsd_subscription_t
*sub
; /* Subscription */
7651 cups_array_t
*ra
; /* Requested attributes array */
7652 ipp_attribute_t
*attr
; /* Attribute */
7653 cups_ptype_t dtype
; /* Destination type (printer/class) */
7654 char scheme
[HTTP_MAX_URI
],
7655 /* Scheme portion of URI */
7656 username
[HTTP_MAX_URI
],
7657 /* Username portion of URI */
7659 /* Host portion of URI */
7660 resource
[HTTP_MAX_URI
];
7661 /* Resource portion of URI */
7662 int port
; /* Port portion of URI */
7663 cupsd_job_t
*job
; /* Job pointer */
7664 cupsd_printer_t
*printer
; /* Printer */
7667 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7668 "get_subscriptions(con=%p[%d], uri=%s)",
7669 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
7672 * Is the destination valid?
7675 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7676 sizeof(scheme
), username
, sizeof(username
), host
,
7677 sizeof(host
), &port
, resource
, sizeof(resource
));
7679 if (!strcmp(resource
, "/") ||
7680 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7681 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7682 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7687 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7690 job
= cupsdFindJob(atoi(resource
+ 6));
7694 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
7699 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7705 send_ipp_status(con
, IPP_NOT_FOUND
,
7706 _("The printer or class was not found."));
7709 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7710 IPP_TAG_INTEGER
)) != NULL
)
7712 job
= cupsdFindJob(attr
->values
[0].integer
);
7716 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
7717 attr
->values
[0].integer
);
7728 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
7730 con
, NULL
)) != HTTP_OK
)
7732 send_http_error(con
, status
, printer
);
7737 * Copy the subscription attributes to the response using the
7738 * requested-attributes attribute that may be provided by the client.
7741 ra
= create_requested_array(con
->request
);
7743 if ((attr
= ippFindAttribute(con
->request
, "limit",
7744 IPP_TAG_INTEGER
)) != NULL
)
7745 limit
= attr
->values
[0].integer
;
7750 * See if we only want to see subscriptions for a specific user...
7753 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7754 IPP_TAG_BOOLEAN
)) != NULL
&&
7755 attr
->values
[0].boolean
)
7756 strlcpy(username
, get_username(con
), sizeof(username
));
7760 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7762 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7763 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7764 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
7766 ippAddSeparator(con
->response
);
7767 copy_subscription_attrs(con
, sub
, ra
);
7770 if (limit
&& count
>= limit
)
7774 cupsArrayDelete(ra
);
7777 con
->response
->request
.status
.status_code
= IPP_OK
;
7779 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7784 * 'get_username()' - Get the username associated with a request.
7787 static const char * /* O - Username */
7788 get_username(cupsd_client_t
*con
) /* I - Connection */
7790 ipp_attribute_t
*attr
; /* Attribute */
7793 if (con
->username
[0])
7794 return (con
->username
);
7795 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7796 IPP_TAG_NAME
)) != NULL
)
7797 return (attr
->values
[0].string
.text
);
7799 return ("anonymous");
7804 * 'hold_job()' - Hold a print job.
7808 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7809 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7811 ipp_attribute_t
*attr
, /* Current job-hold-until */
7812 *newattr
; /* New job-hold-until */
7813 int jobid
; /* Job ID */
7814 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7815 username
[HTTP_MAX_URI
], /* Username portion of URI */
7816 host
[HTTP_MAX_URI
], /* Host portion of URI */
7817 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7818 int port
; /* Port portion of URI */
7819 cupsd_job_t
*job
; /* Job information */
7822 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
7823 uri
->values
[0].string
.text
);
7826 * See if we have a job URI or a printer URI...
7829 if (!strcmp(uri
->name
, "printer-uri"))
7832 * Got a printer URI; see if we also have a job-id attribute...
7835 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7836 IPP_TAG_INTEGER
)) == NULL
)
7838 send_ipp_status(con
, IPP_BAD_REQUEST
,
7839 _("Got a printer-uri attribute but no job-id!"));
7843 jobid
= attr
->values
[0].integer
;
7848 * Got a job URI; parse it to get the job ID...
7851 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7852 sizeof(method
), username
, sizeof(username
), host
,
7853 sizeof(host
), &port
, resource
, sizeof(resource
));
7855 if (strncmp(resource
, "/jobs/", 6))
7861 send_ipp_status(con
, IPP_BAD_REQUEST
,
7862 _("Bad job-uri attribute \"%s\"!"),
7863 uri
->values
[0].string
.text
);
7867 jobid
= atoi(resource
+ 6);
7871 * See if the job exists...
7874 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7877 * Nope - return a "not found" error...
7880 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7885 * See if the job is owned by the requesting user...
7888 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7890 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7895 * Hold the job and return...
7900 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
7901 "Job held by user.");
7903 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
7904 IPP_TAG_KEYWORD
)) == NULL
)
7905 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7907 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7908 IPP_TAG_KEYWORD
)) == NULL
)
7909 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7914 * Free the old hold value and copy the new one over...
7917 _cupsStrFree(attr
->values
[0].string
.text
);
7921 attr
->value_tag
= newattr
->value_tag
;
7922 attr
->values
[0].string
.text
=
7923 _cupsStrAlloc(newattr
->values
[0].string
.text
);
7927 attr
->value_tag
= IPP_TAG_KEYWORD
;
7928 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
7932 * Hold job until specified time...
7935 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
7937 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
7938 "Job job-hold-until value changed by user.");
7941 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Held by \"%s\".", username
);
7943 con
->response
->request
.status
.status_code
= IPP_OK
;
7948 * 'move_job()' - Move a job to a new destination.
7952 move_job(cupsd_client_t
*con
, /* I - Client connection */
7953 ipp_attribute_t
*uri
) /* I - Job URI */
7955 http_status_t status
; /* Policy status */
7956 ipp_attribute_t
*attr
; /* Current attribute */
7957 int jobid
; /* Job ID */
7958 cupsd_job_t
*job
; /* Current job */
7959 const char *src
; /* Source printer/class */
7960 cups_ptype_t stype
, /* Source type (printer or class) */
7961 dtype
; /* Destination type (printer/class) */
7962 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7963 username
[HTTP_MAX_URI
], /* Username portion of URI */
7964 host
[HTTP_MAX_URI
], /* Host portion of URI */
7965 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7966 int port
; /* Port portion of URI */
7967 cupsd_printer_t
*sprinter
, /* Source printer */
7968 *dprinter
; /* Destination printer */
7971 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
7972 uri
->values
[0].string
.text
);
7975 * Get the new printer or class...
7978 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7979 IPP_TAG_URI
)) == NULL
)
7982 * Need job-printer-uri...
7985 send_ipp_status(con
, IPP_BAD_REQUEST
,
7986 _("job-printer-uri attribute missing!"));
7990 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7996 send_ipp_status(con
, IPP_NOT_FOUND
,
7997 _("The printer or class was not found."));
8002 * See if we have a job URI or a printer URI...
8005 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8006 sizeof(scheme
), username
, sizeof(username
), host
,
8007 sizeof(host
), &port
, resource
, sizeof(resource
));
8009 if (!strcmp(uri
->name
, "printer-uri"))
8012 * Got a printer URI; see if we also have a job-id attribute...
8015 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8016 IPP_TAG_INTEGER
)) == NULL
)
8022 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
8023 &sprinter
)) == NULL
)
8029 send_ipp_status(con
, IPP_NOT_FOUND
,
8030 _("The printer or class was not found."));
8039 * Otherwise, just move a single job...
8042 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
8045 * Nope - return a "not found" error...
8048 send_ipp_status(con
, IPP_NOT_FOUND
,
8049 _("Job #%d does not exist!"), attr
->values
[0].integer
);
8055 * Job found, initialize source pointers...
8066 * Got a job URI; parse it to get the job ID...
8069 if (strncmp(resource
, "/jobs/", 6))
8075 send_ipp_status(con
, IPP_BAD_REQUEST
,
8076 _("Bad job-uri attribute \"%s\"!"),
8077 uri
->values
[0].string
.text
);
8082 * See if the job exists...
8085 jobid
= atoi(resource
+ 6);
8087 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8090 * Nope - return a "not found" error...
8093 send_ipp_status(con
, IPP_NOT_FOUND
,
8094 _("Job #%d does not exist!"), jobid
);
8100 * Job found, initialize source pointers...
8109 * Check the policy of the destination printer...
8112 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
8113 job
? job
->username
: NULL
)) != HTTP_OK
)
8115 send_http_error(con
, status
, dprinter
);
8120 * Now move the job or jobs...
8126 * See if the job has been completed...
8129 if (job
->state_value
> IPP_JOB_STOPPED
)
8132 * Return a "not-possible" error...
8135 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8136 _("Job #%d is finished and cannot be altered!"),
8142 * See if the job is owned by the requesting user...
8145 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8147 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8152 * Move the job to a different printer or class...
8155 cupsdMoveJob(job
, dprinter
);
8160 * Got the source printer, now look through the jobs...
8163 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
8165 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
8168 * See if the job is pointing at the source printer or has not been
8172 if (strcasecmp(job
->dest
, src
) ||
8173 job
->state_value
> IPP_JOB_STOPPED
)
8177 * See if the job can be moved by the requesting user...
8180 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8184 * Move the job to a different printer or class...
8187 cupsdMoveJob(job
, dprinter
);
8192 * Start jobs if possible...
8198 * Return with "everything is OK" status...
8201 con
->response
->request
.status
.status_code
= IPP_OK
;
8206 * 'ppd_parse_line()' - Parse a PPD default line.
8209 static int /* O - 0 on success, -1 on failure */
8210 ppd_parse_line(const char *line
, /* I - Line */
8211 char *option
, /* O - Option name */
8212 int olen
, /* I - Size of option name */
8213 char *choice
, /* O - Choice name */
8214 int clen
) /* I - Size of choice name */
8217 * Verify this is a default option line...
8220 if (strncmp(line
, "*Default", 8))
8224 * Read the option name...
8227 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
8237 * Skip everything else up to the colon (:)...
8240 while (*line
&& *line
!= ':')
8249 * Now grab the option choice, skipping leading whitespace...
8252 while (isspace(*line
& 255))
8255 for (clen
--; isalnum(*line
& 255); line
++)
8265 * Return with no errors...
8273 * 'print_job()' - Print a file to a printer or class.
8277 print_job(cupsd_client_t
*con
, /* I - Client connection */
8278 ipp_attribute_t
*uri
) /* I - Printer URI */
8280 ipp_attribute_t
*attr
; /* Current attribute */
8281 ipp_attribute_t
*format
; /* Document-format attribute */
8282 const char *default_format
; /* document-format-default value */
8283 cupsd_job_t
*job
; /* New job */
8284 char filename
[1024]; /* Job filename */
8285 mime_type_t
*filetype
; /* Type of file */
8286 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8287 type
[MIME_MAX_TYPE
], /* Subtype of file */
8288 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8289 /* Textual name of mime type */
8290 cupsd_printer_t
*printer
; /* Printer data */
8291 struct stat fileinfo
; /* File information */
8292 int kbytes
; /* Size of file */
8293 int compression
; /* Document compression */
8296 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
8297 uri
->values
[0].string
.text
);
8300 * Validate print file attributes, for now just document-format and
8301 * compression (CUPS only supports "none" and "gzip")...
8304 compression
= CUPS_FILE_NONE
;
8306 if ((attr
= ippFindAttribute(con
->request
, "compression",
8307 IPP_TAG_KEYWORD
)) != NULL
)
8309 if (strcmp(attr
->values
[0].string
.text
, "none")
8311 && strcmp(attr
->values
[0].string
.text
, "gzip")
8312 #endif /* HAVE_LIBZ */
8315 send_ipp_status(con
, IPP_ATTRIBUTES
,
8316 _("Unsupported compression \"%s\"!"),
8317 attr
->values
[0].string
.text
);
8318 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8319 "compression", NULL
, attr
->values
[0].string
.text
);
8324 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8325 compression
= CUPS_FILE_GZIP
;
8326 #endif /* HAVE_LIBZ */
8330 * Do we have a file to print?
8335 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
8340 * Is the destination valid?
8343 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8349 send_ipp_status(con
, IPP_NOT_FOUND
,
8350 _("The printer or class was not found."));
8355 * Is it a format we support?
8358 if ((format
= ippFindAttribute(con
->request
, "document-format",
8359 IPP_TAG_MIMETYPE
)) != NULL
)
8362 * Grab format from client...
8365 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
8368 send_ipp_status(con
, IPP_BAD_REQUEST
,
8369 _("Could not scan type \"%s\"!"),
8370 format
->values
[0].string
.text
);
8374 else if ((default_format
= cupsGetOption("document-format",
8375 printer
->num_options
,
8376 printer
->options
)) != NULL
)
8379 * Use default document format...
8382 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
8384 send_ipp_status(con
, IPP_BAD_REQUEST
,
8385 _("Could not scan type \"%s\"!"),
8396 strcpy(super
, "application");
8397 strcpy(type
, "octet-stream");
8400 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8403 * Auto-type the file...
8406 ipp_attribute_t
*doc_name
; /* document-name attribute */
8409 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8411 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8412 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8413 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8417 filetype
= mimeType(MimeDatabase
, super
, type
);
8419 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8420 filetype
->super
, filetype
->type
);
8423 filetype
= mimeType(MimeDatabase
, super
, type
);
8427 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8430 * Replace the document-format attribute value with the auto-typed or
8434 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8439 _cupsStrFree(format
->values
[0].string
.text
);
8441 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8444 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8445 "document-format", NULL
, mimetype
);
8449 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8450 _("Unsupported format \'%s/%s\'!"), super
, type
);
8451 cupsdLogMessage(CUPSD_LOG_INFO
,
8452 "Hint: Do you have the raw file printing rules enabled?");
8455 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8456 "document-format", NULL
, format
->values
[0].string
.text
);
8462 * Read any embedded job ticket info from PS files...
8465 if (!strcasecmp(filetype
->super
, "application") &&
8466 (!strcasecmp(filetype
->type
, "postscript") ||
8467 !strcasecmp(filetype
->type
, "pdf")))
8468 read_job_ticket(con
);
8471 * Create the job object...
8474 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8478 * Update quota data...
8481 if (stat(con
->filename
, &fileinfo
))
8484 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8486 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8488 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8489 IPP_TAG_INTEGER
)) != NULL
)
8490 attr
->values
[0].integer
+= kbytes
;
8493 * Add the job file...
8496 if (add_file(con
, job
, filetype
, compression
))
8499 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8501 rename(con
->filename
, filename
);
8502 cupsdClearString(&con
->filename
);
8505 * See if we need to add the ending sheet...
8508 if (cupsdTimeoutJob(job
))
8512 * Log and save the job...
8515 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8516 "File of type %s/%s queued by \"%s\".",
8517 filetype
->super
, filetype
->type
, job
->username
);
8518 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8519 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8520 job
->dest
, job
->username
);
8523 * Start the job if possible...
8531 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8533 * This function only gets called when printing a single PDF or PostScript
8534 * file using the Print-Job operation. It doesn't work for Create-Job +
8535 * Send-File, since the job attributes need to be set at job creation
8536 * time for banners to work. The embedded job ticket stuff is here
8537 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8538 * options and IPP attributes which otherwise would be lost.
8540 * The format of a job ticket is simple:
8542 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8544 * %cupsJobTicket: attr1=value1
8545 * %cupsJobTicket: attr2=value2
8547 * %cupsJobTicket: attrN=valueN
8549 * Job ticket lines must appear immediately after the first line that
8550 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8551 * stops looking for job ticket info when it finds a line that does not begin
8552 * with "%cupsJobTicket:".
8554 * The maximum length of a job ticket line, including the prefix, is
8555 * 255 characters to conform with the Adobe DSC.
8557 * Read-only attributes are rejected with a notice to the error log in
8558 * case a malicious user tries anything. Since the job ticket is read
8559 * prior to attribute validation in print_job(), job ticket attributes
8560 * will go through the same validation as IPP attributes...
8564 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8566 cups_file_t
*fp
; /* File to read from */
8567 char line
[256]; /* Line data */
8568 int num_options
; /* Number of options */
8569 cups_option_t
*options
; /* Options */
8570 ipp_t
*ticket
; /* New attributes */
8571 ipp_attribute_t
*attr
, /* Current attribute */
8572 *attr2
, /* Job attribute */
8573 *prev2
; /* Previous job attribute */
8577 * First open the print file...
8580 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8582 cupsdLogMessage(CUPSD_LOG_ERROR
,
8583 "Unable to open print file for job ticket - %s",
8589 * Skip the first line...
8592 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8594 cupsdLogMessage(CUPSD_LOG_ERROR
,
8595 "Unable to read from print file for job ticket - %s",
8601 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8604 * Not a DSC-compliant file, so no job ticket info will be available...
8612 * Read job ticket info from the file...
8618 while (cupsFileGets(fp
, line
, sizeof(line
)))
8621 * Stop at the first non-ticket line...
8624 if (strncmp(line
, "%cupsJobTicket:", 15))
8628 * Add the options to the option array...
8631 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8635 * Done with the file; see if we have any options...
8640 if (num_options
== 0)
8644 * OK, convert the options to an attribute list, and apply them to
8649 cupsEncodeOptions(ticket
, num_options
, options
);
8652 * See what the user wants to change.
8655 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8657 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8660 if (!strcmp(attr
->name
, "job-originating-host-name") ||
8661 !strcmp(attr
->name
, "job-originating-user-name") ||
8662 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8663 !strcmp(attr
->name
, "job-k-octets") ||
8664 !strcmp(attr
->name
, "job-id") ||
8665 !strncmp(attr
->name
, "job-state", 9) ||
8666 !strncmp(attr
->name
, "time-at-", 8))
8667 continue; /* Read-only attrs */
8669 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8670 IPP_TAG_ZERO
)) != NULL
)
8673 * Some other value; first free the old value...
8676 if (con
->request
->attrs
== attr2
)
8678 con
->request
->attrs
= attr2
->next
;
8683 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8684 if (prev2
->next
== attr2
)
8686 prev2
->next
= attr2
->next
;
8691 if (con
->request
->last
== attr2
)
8692 con
->request
->last
= prev2
;
8694 _ippFreeAttr(attr2
);
8698 * Add new option by copying it...
8701 copy_attribute(con
->request
, attr
, 0);
8705 * Then free the attribute list and option array...
8709 cupsFreeOptions(num_options
, options
);
8714 * 'reject_jobs()' - Reject print jobs to a printer.
8718 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8719 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8721 http_status_t status
; /* Policy status */
8722 cups_ptype_t dtype
; /* Destination type (printer/class) */
8723 cupsd_printer_t
*printer
; /* Printer data */
8724 ipp_attribute_t
*attr
; /* printer-state-message text */
8727 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8728 con
->http
.fd
, uri
->values
[0].string
.text
);
8731 * Is the destination valid?
8734 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8740 send_ipp_status(con
, IPP_NOT_FOUND
,
8741 _("The printer or class was not found."));
8749 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8751 send_http_error(con
, status
, printer
);
8756 * Reject jobs sent to the printer...
8759 printer
->accepting
= 0;
8761 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8762 IPP_TAG_TEXT
)) == NULL
)
8763 strcpy(printer
->state_message
, "Rejecting Jobs");
8765 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8766 sizeof(printer
->state_message
));
8768 cupsdAddPrinterHistory(printer
);
8770 if (dtype
& CUPS_PRINTER_CLASS
)
8772 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8774 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8775 printer
->name
, get_username(con
));
8779 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8781 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8782 printer
->name
, get_username(con
));
8786 * Everything was ok, so return OK status...
8789 con
->response
->request
.status
.status_code
= IPP_OK
;
8794 * 'release_job()' - Release a held print job.
8798 release_job(cupsd_client_t
*con
, /* I - Client connection */
8799 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8801 ipp_attribute_t
*attr
; /* Current attribute */
8802 int jobid
; /* Job ID */
8803 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8804 username
[HTTP_MAX_URI
], /* Username portion of URI */
8805 host
[HTTP_MAX_URI
], /* Host portion of URI */
8806 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8807 int port
; /* Port portion of URI */
8808 cupsd_job_t
*job
; /* Job information */
8811 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8812 con
->http
.fd
, uri
->values
[0].string
.text
);
8815 * See if we have a job URI or a printer URI...
8818 if (!strcmp(uri
->name
, "printer-uri"))
8821 * Got a printer URI; see if we also have a job-id attribute...
8824 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8825 IPP_TAG_INTEGER
)) == NULL
)
8827 send_ipp_status(con
, IPP_BAD_REQUEST
,
8828 _("Got a printer-uri attribute but no job-id!"));
8832 jobid
= attr
->values
[0].integer
;
8837 * Got a job URI; parse it to get the job ID...
8840 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8841 sizeof(method
), username
, sizeof(username
), host
,
8842 sizeof(host
), &port
, resource
, sizeof(resource
));
8844 if (strncmp(resource
, "/jobs/", 6))
8850 send_ipp_status(con
, IPP_BAD_REQUEST
,
8851 _("Bad job-uri attribute \"%s\"!"),
8852 uri
->values
[0].string
.text
);
8856 jobid
= atoi(resource
+ 6);
8860 * See if the job exists...
8863 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8866 * Nope - return a "not found" error...
8869 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8874 * See if job is "held"...
8877 if (job
->state_value
!= IPP_JOB_HELD
)
8880 * Nope - return a "not possible" error...
8883 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
8888 * See if the job is owned by the requesting user...
8891 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8893 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8898 * Reset the job-hold-until value to "no-hold"...
8901 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8902 IPP_TAG_KEYWORD
)) == NULL
)
8903 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8907 _cupsStrFree(attr
->values
[0].string
.text
);
8909 attr
->value_tag
= IPP_TAG_KEYWORD
;
8910 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
8912 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
8913 "Job job-hold-until value changed by user.");
8917 * Release the job and return...
8920 cupsdReleaseJob(job
);
8922 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
8923 "Job released by user.");
8925 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
8927 con
->response
->request
.status
.status_code
= IPP_OK
;
8934 * 'renew_subscription()' - Renew an existing subscription...
8939 cupsd_client_t
*con
, /* I - Client connection */
8940 int sub_id
) /* I - Subscription ID */
8942 http_status_t status
; /* Policy status */
8943 cupsd_subscription_t
*sub
; /* Subscription */
8944 ipp_attribute_t
*lease
; /* notify-lease-duration */
8947 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8948 "renew_subscription(con=%p[%d], sub_id=%d)",
8949 con
, con
->http
.fd
, sub_id
);
8952 * Is the subscription ID valid?
8955 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8958 * Bad subscription ID...
8961 send_ipp_status(con
, IPP_NOT_FOUND
,
8962 _("notify-subscription-id %d no good!"), sub_id
);
8969 * Job subscriptions cannot be renewed...
8972 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8973 _("Job subscriptions cannot be renewed!"));
8981 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8983 con
, sub
->owner
)) != HTTP_OK
)
8985 send_http_error(con
, status
, sub
->dest
);
8990 * Renew the subscription...
8993 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8996 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8998 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
9000 cupsdLogMessage(CUPSD_LOG_INFO
,
9001 "renew_subscription: Limiting notify-lease-duration to "
9004 sub
->lease
= MaxLeaseDuration
;
9007 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
9009 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
9011 con
->response
->request
.status
.status_code
= IPP_OK
;
9013 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
9014 "notify-lease-duration", sub
->lease
);
9019 * 'restart_job()' - Restart an old print job.
9023 restart_job(cupsd_client_t
*con
, /* I - Client connection */
9024 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
9026 ipp_attribute_t
*attr
; /* Current attribute */
9027 int jobid
; /* Job ID */
9028 char method
[HTTP_MAX_URI
], /* Method portion of URI */
9029 username
[HTTP_MAX_URI
], /* Username portion of URI */
9030 host
[HTTP_MAX_URI
], /* Host portion of URI */
9031 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9032 int port
; /* Port portion of URI */
9033 cupsd_job_t
*job
; /* Job information */
9036 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
9037 con
->http
.fd
, uri
->values
[0].string
.text
);
9040 * See if we have a job URI or a printer URI...
9043 if (!strcmp(uri
->name
, "printer-uri"))
9046 * Got a printer URI; see if we also have a job-id attribute...
9049 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9050 IPP_TAG_INTEGER
)) == NULL
)
9052 send_ipp_status(con
, IPP_BAD_REQUEST
,
9053 _("Got a printer-uri attribute but no job-id!"));
9057 jobid
= attr
->values
[0].integer
;
9062 * Got a job URI; parse it to get the job ID...
9065 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9066 sizeof(method
), username
, sizeof(username
), host
,
9067 sizeof(host
), &port
, resource
, sizeof(resource
));
9069 if (strncmp(resource
, "/jobs/", 6))
9075 send_ipp_status(con
, IPP_BAD_REQUEST
,
9076 _("Bad job-uri attribute \"%s\"!"),
9077 uri
->values
[0].string
.text
);
9081 jobid
= atoi(resource
+ 6);
9085 * See if the job exists...
9088 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9091 * Nope - return a "not found" error...
9094 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9099 * See if job is in any of the "completed" states...
9102 if (job
->state_value
<= IPP_JOB_PROCESSING
)
9105 * Nope - return a "not possible" error...
9108 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
9114 * See if we have retained the job files...
9119 if (!job
->attrs
|| job
->num_files
== 0)
9122 * Nope - return a "not possible" error...
9125 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9126 _("Job #%d cannot be restarted - no files!"), jobid
);
9131 * See if the job is owned by the requesting user...
9134 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9136 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9141 * Restart the job and return...
9144 cupsdRestartJob(job
);
9146 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
9148 con
->response
->request
.status
.status_code
= IPP_OK
;
9153 * 'save_auth_info()' - Save authentication information for a job.
9158 cupsd_client_t
*con
, /* I - Client connection */
9159 cupsd_job_t
*job
, /* I - Job */
9160 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
9162 int i
; /* Looping var */
9163 char filename
[1024]; /* Job authentication filename */
9164 cups_file_t
*fp
; /* Job authentication file */
9165 char line
[2048]; /* Line for file */
9166 cupsd_printer_t
*dest
; /* Destination printer/class */
9170 * This function saves the in-memory authentication information for
9171 * a job so that it can be used to authenticate with a remote host.
9172 * The information is stored in a file that is readable only by the
9173 * root user. The fields are Base-64 encoded, each on a separate line,
9174 * followed by random number (up to 1024) of newlines to limit the
9175 * amount of information that is exposed.
9177 * Because of the potential for exposing of authentication information,
9178 * this functionality is only enabled when running cupsd as root.
9180 * This caching only works for the Basic and BasicDigest authentication
9181 * types. Digest authentication cannot be cached this way, and in
9182 * the future Kerberos authentication may make all of this obsolete.
9184 * Authentication information is saved whenever an authenticated
9185 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9188 * This information is deleted after a job is completed or canceled,
9189 * so reprints may require subsequent re-authentication.
9195 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9199 * Create the authentication file and change permissions...
9202 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9203 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9205 cupsdLogMessage(CUPSD_LOG_ERROR
,
9206 "Unable to save authentication info to \"%s\" - %s",
9207 filename
, strerror(errno
));
9211 fchown(cupsFileNumber(fp
), 0, 0);
9212 fchmod(cupsFileNumber(fp
), 0400);
9214 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9217 * Write 1 to 3 auth values...
9220 cupsdClearString(&job
->auth_username
);
9221 cupsdClearString(&job
->auth_domain
);
9222 cupsdClearString(&job
->auth_password
);
9224 for (i
= 0; i
< auth_info
->num_values
; i
++)
9226 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
9227 strlen(auth_info
->values
[i
].string
.text
));
9228 cupsFilePrintf(fp
, "%s\n", line
);
9230 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9231 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s",
9232 auth_info
->values
[i
].string
.text
);
9233 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9234 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s",
9235 auth_info
->values
[i
].string
.text
);
9236 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9237 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s",
9238 auth_info
->values
[i
].string
.text
);
9241 else if (con
->username
[0])
9244 * Write the authenticated username...
9247 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
9248 cupsFilePrintf(fp
, "%s\n", line
);
9250 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", con
->username
);
9251 cupsdClearString(&job
->auth_domain
);
9254 * Write the authenticated password...
9257 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
9258 cupsFilePrintf(fp
, "%s\n", line
);
9260 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", con
->password
);
9264 * Write a random number of newlines to the end of the file...
9267 for (i
= (rand() % 1024); i
>= 0; i
--)
9268 cupsFilePutChar(fp
, '\n');
9271 * Close the file and return...
9276 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9277 if (con
->gss_have_creds
)
9278 save_krb5_creds(con
, job
);
9279 else if (job
->ccname
)
9280 cupsdClearString(&(job
->ccname
));
9281 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9285 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9287 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
9291 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
9292 cupsd_job_t
*job
) /* I - Job */
9294 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
9295 cupsdLogMessage(CUPSD_LOG_INFO
,
9296 "Sorry, your version of Kerberos does not support delegated "
9301 krb5_error_code error
; /* Kerberos error code */
9302 OM_uint32 major_status
, /* Major status code */
9303 minor_status
; /* Minor status code */
9304 krb5_principal principal
; /* Kerberos principal */
9309 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
9313 if (krb5_init_context
== NULL
)
9315 # endif /* __APPLE__ */
9317 if (!KerberosInitialized
)
9320 * Setup a Kerberos context for the scheduler to use...
9323 KerberosInitialized
= 1;
9325 if (krb5_init_context(&KerberosContext
))
9327 KerberosContext
= NULL
;
9329 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize Kerberos context");
9335 * We MUST create a file-based cache because memory-based caches are
9336 * only valid for the current process/address space.
9338 * Due to various bugs/features in different versions of Kerberos, we
9339 * need either the krb5_cc_new_unique() function or Heimdal's version
9340 * of krb5_cc_gen_new() to create a new FILE: credential cache that
9341 * can be passed to the backend. These functions create a temporary
9342 * file (typically in /tmp) containing the cached credentials, which
9343 * are removed when we have successfully printed a job.
9346 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
9347 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
9348 &(job
->ccache
))) != 0)
9349 # else /* HAVE_HEIMDAL */
9350 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
9351 &(job
->ccache
))) != 0)
9352 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
9354 cupsdLogMessage(CUPSD_LOG_ERROR
,
9355 "Unable to create new credentials cache (%d/%s)",
9356 error
, strerror(errno
));
9361 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
9363 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to parse kerberos username (%d/%s)",
9364 error
, strerror(errno
));
9365 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9370 if ((error
= krb5_cc_initialize(KerberosContext
, job
->ccache
, principal
)))
9372 cupsdLogMessage(CUPSD_LOG_ERROR
,
9373 "Unable to initialize credentials cache (%d/%s)", error
,
9375 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9376 krb5_free_principal(KerberosContext
, principal
);
9381 krb5_free_principal(KerberosContext
, principal
);
9384 * Copy the user's credentials to the new cache file...
9387 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
9390 if (GSS_ERROR(major_status
))
9392 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
9393 "Unable to import client credentials cache");
9394 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9400 * Add the KRB5CCNAME environment variable to the job so that the
9401 * backend can use the credentials when printing.
9404 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
9405 krb5_cc_get_name(KerberosContext
, job
->ccache
));
9407 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "save_krb5_creds: %s", job
->ccname
);
9408 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
9410 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9414 * 'send_document()' - Send a file to a printer or class.
9418 send_document(cupsd_client_t
*con
, /* I - Client connection */
9419 ipp_attribute_t
*uri
) /* I - Printer URI */
9421 ipp_attribute_t
*attr
; /* Current attribute */
9422 ipp_attribute_t
*format
; /* Request's document-format attribute */
9423 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9424 const char *default_format
;/* document-format-default value */
9425 int jobid
; /* Job ID number */
9426 cupsd_job_t
*job
; /* Current job */
9427 char job_uri
[HTTP_MAX_URI
],
9429 method
[HTTP_MAX_URI
],
9430 /* Method portion of URI */
9431 username
[HTTP_MAX_URI
],
9432 /* Username portion of URI */
9434 /* Host portion of URI */
9435 resource
[HTTP_MAX_URI
];
9436 /* Resource portion of URI */
9437 int port
; /* Port portion of URI */
9438 mime_type_t
*filetype
; /* Type of file */
9439 char super
[MIME_MAX_SUPER
],
9440 /* Supertype of file */
9441 type
[MIME_MAX_TYPE
],
9442 /* Subtype of file */
9443 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9444 /* Textual name of mime type */
9445 char filename
[1024]; /* Job filename */
9446 cupsd_printer_t
*printer
; /* Current printer */
9447 struct stat fileinfo
; /* File information */
9448 int kbytes
; /* Size of file */
9449 int compression
; /* Type of compression */
9450 int start_job
; /* Start the job? */
9453 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9454 con
->http
.fd
, uri
->values
[0].string
.text
);
9457 * See if we have a job URI or a printer URI...
9460 if (!strcmp(uri
->name
, "printer-uri"))
9463 * Got a printer URI; see if we also have a job-id attribute...
9466 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9467 IPP_TAG_INTEGER
)) == NULL
)
9469 send_ipp_status(con
, IPP_BAD_REQUEST
,
9470 _("Got a printer-uri attribute but no job-id!"));
9474 jobid
= attr
->values
[0].integer
;
9479 * Got a job URI; parse it to get the job ID...
9482 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9483 sizeof(method
), username
, sizeof(username
), host
,
9484 sizeof(host
), &port
, resource
, sizeof(resource
));
9486 if (strncmp(resource
, "/jobs/", 6))
9492 send_ipp_status(con
, IPP_BAD_REQUEST
,
9493 _("Bad job-uri attribute \"%s\"!"),
9494 uri
->values
[0].string
.text
);
9498 jobid
= atoi(resource
+ 6);
9502 * See if the job exists...
9505 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9508 * Nope - return a "not found" error...
9511 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9515 printer
= cupsdFindDest(job
->dest
);
9518 * See if the job is owned by the requesting user...
9521 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9523 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9528 * OK, see if the client is sending the document compressed - CUPS
9529 * only supports "none" and "gzip".
9532 compression
= CUPS_FILE_NONE
;
9534 if ((attr
= ippFindAttribute(con
->request
, "compression",
9535 IPP_TAG_KEYWORD
)) != NULL
)
9537 if (strcmp(attr
->values
[0].string
.text
, "none")
9539 && strcmp(attr
->values
[0].string
.text
, "gzip")
9540 #endif /* HAVE_LIBZ */
9543 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
9544 attr
->values
[0].string
.text
);
9545 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9546 "compression", NULL
, attr
->values
[0].string
.text
);
9551 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9552 compression
= CUPS_FILE_GZIP
;
9553 #endif /* HAVE_LIBZ */
9557 * Do we have a file to print?
9562 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
9567 * Is it a format we support?
9570 if ((format
= ippFindAttribute(con
->request
, "document-format",
9571 IPP_TAG_MIMETYPE
)) != NULL
)
9574 * Grab format from client...
9577 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
9580 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9581 format
->values
[0].string
.text
);
9585 else if ((default_format
= cupsGetOption("document-format",
9586 printer
->num_options
,
9587 printer
->options
)) != NULL
)
9590 * Use default document format...
9593 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
9595 send_ipp_status(con
, IPP_BAD_REQUEST
,
9596 _("Could not scan type \"%s\"!"),
9604 * No document format attribute? Auto-type it!
9607 strcpy(super
, "application");
9608 strcpy(type
, "octet-stream");
9611 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9614 * Auto-type the file...
9617 ipp_attribute_t
*doc_name
; /* document-name attribute */
9620 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9622 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9623 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9624 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9628 filetype
= mimeType(MimeDatabase
, super
, type
);
9630 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9631 filetype
->super
, filetype
->type
);
9634 filetype
= mimeType(MimeDatabase
, super
, type
);
9639 * Replace the document-format attribute value with the auto-typed or
9643 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9646 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9647 IPP_TAG_MIMETYPE
)) != NULL
)
9649 _cupsStrFree(jformat
->values
[0].string
.text
);
9651 jformat
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
9654 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9655 "document-format", NULL
, mimetype
);
9659 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9660 _("Unsupported format \'%s/%s\'!"), super
, type
);
9661 cupsdLogMessage(CUPSD_LOG_INFO
,
9662 "Hint: Do you have the raw file printing rules enabled?");
9665 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9666 "document-format", NULL
, format
->values
[0].string
.text
);
9671 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9673 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9676 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9677 _("Unsupported format \'%s\'!"), mimetype
);
9679 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9680 "document-format", NULL
, mimetype
);
9686 * Add the file to the job...
9691 if (add_file(con
, job
, filetype
, compression
))
9694 if (stat(con
->filename
, &fileinfo
))
9697 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9699 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9701 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
9702 IPP_TAG_INTEGER
)) != NULL
)
9703 attr
->values
[0].integer
+= kbytes
;
9705 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
9707 rename(con
->filename
, filename
);
9709 cupsdClearString(&con
->filename
);
9711 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9712 filetype
->super
, filetype
->type
, job
->username
);
9715 * Start the job if this is the last document...
9718 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9719 IPP_TAG_BOOLEAN
)) != NULL
&&
9720 attr
->values
[0].boolean
)
9723 * See if we need to add the ending sheet...
9726 if (cupsdTimeoutJob(job
))
9729 if (job
->state_value
== IPP_JOB_STOPPED
)
9731 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9732 job
->state_value
= IPP_JOB_PENDING
;
9734 else if (job
->state_value
== IPP_JOB_HELD
)
9736 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9737 IPP_TAG_KEYWORD
)) == NULL
)
9738 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9740 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9742 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9743 job
->state_value
= IPP_JOB_PENDING
;
9748 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9754 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9755 IPP_TAG_KEYWORD
)) == NULL
)
9756 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9758 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9760 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9761 job
->state_value
= IPP_JOB_HELD
;
9762 job
->hold_until
= time(NULL
) + 60;
9765 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9772 * Fill in the response info...
9775 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
9778 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9781 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9783 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9785 add_job_state_reasons(con
, job
);
9787 con
->response
->request
.status
.status_code
= IPP_OK
;
9790 * Start the job if necessary...
9799 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9804 cupsd_client_t
*con
, /* I - Client connection */
9805 http_status_t status
, /* I - HTTP status code */
9806 cupsd_printer_t
*printer
) /* I - Printer, if any */
9808 ipp_attribute_t
*uri
; /* Request URI, if any */
9811 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
9812 IPP_TAG_URI
)) == NULL
)
9813 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
9815 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
9816 "Returning HTTP %s for %s (%s) from %s",
9818 ippOpString(con
->request
->request
.op
.operation_id
),
9819 uri
? uri
->values
[0].string
.text
: "no URI",
9820 con
->http
.hostname
);
9824 int auth_type
; /* Type of authentication required */
9827 auth_type
= CUPSD_AUTH_NONE
;
9829 if (status
== HTTP_UNAUTHORIZED
&&
9830 printer
->num_auth_info_required
> 0 &&
9831 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
9833 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
9834 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
9835 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
9838 * Creating and authenticating jobs requires Kerberos...
9841 auth_type
= CUPSD_AUTH_NEGOTIATE
;
9846 * Use policy/location-defined authentication requirements...
9849 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9850 cupsd_location_t
*auth
; /* Pointer to authentication element */
9853 if (printer
->type
& CUPS_PRINTER_CLASS
)
9854 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9856 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9858 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9859 auth
->type
== CUPSD_AUTH_NONE
)
9860 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
9862 con
->request
->request
.op
.operation_id
:
9867 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
9868 auth_type
= DefaultAuthType
;
9870 auth_type
= auth
->type
;
9874 cupsdSendError(con
, status
, auth_type
);
9877 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
9879 ippDelete(con
->response
);
9880 con
->response
= NULL
;
9887 * 'send_ipp_status()' - Send a status back to the IPP client.
9891 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9892 ipp_status_t status
, /* I - IPP status code */
9893 const char *message
,/* I - Status message */
9894 ...) /* I - Additional args as needed */
9896 va_list ap
; /* Pointer to additional args */
9897 char formatted
[1024]; /* Formatted errror message */
9900 va_start(ap
, message
);
9901 vsnprintf(formatted
, sizeof(formatted
),
9902 _cupsLangString(con
->language
, message
), ap
);
9905 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9906 ippOpString(con
->request
->request
.op
.operation_id
),
9907 ippErrorString(status
), formatted
);
9909 con
->response
->request
.status
.status_code
= status
;
9911 if (ippFindAttribute(con
->response
, "attributes-charset",
9912 IPP_TAG_ZERO
) == NULL
)
9913 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9914 "attributes-charset", NULL
, DefaultCharset
);
9916 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9917 IPP_TAG_ZERO
) == NULL
)
9918 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9919 "attributes-natural-language", NULL
, DefaultLanguage
);
9921 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9922 "status-message", NULL
, formatted
);
9927 * 'set_default()' - Set the default destination...
9931 set_default(cupsd_client_t
*con
, /* I - Client connection */
9932 ipp_attribute_t
*uri
) /* I - Printer URI */
9934 http_status_t status
; /* Policy status */
9935 cups_ptype_t dtype
; /* Destination type (printer/class) */
9936 cupsd_printer_t
*printer
, /* Printer */
9937 *oldprinter
; /* Old default printer */
9940 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9941 con
->http
.fd
, uri
->values
[0].string
.text
);
9944 * Is the destination valid?
9947 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9953 send_ipp_status(con
, IPP_NOT_FOUND
,
9954 _("The printer or class was not found."));
9962 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9964 send_http_error(con
, status
, NULL
);
9969 * Set it as the default...
9972 oldprinter
= DefaultPrinter
;
9973 DefaultPrinter
= printer
;
9976 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
9977 "%s is no longer the default printer.", oldprinter
->name
);
9979 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
9980 "%s is now the default printer.", printer
->name
);
9982 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
9983 CUPSD_DIRTY_REMOTE
| CUPSD_DIRTY_PRINTCAP
);
9985 cupsdLogMessage(CUPSD_LOG_INFO
,
9986 "Default destination set to \"%s\" by \"%s\".",
9987 printer
->name
, get_username(con
));
9990 * Everything was ok, so return OK status...
9993 con
->response
->request
.status
.status_code
= IPP_OK
;
9998 * 'set_job_attrs()' - Set job attributes.
10002 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
10003 ipp_attribute_t
*uri
) /* I - Job URI */
10005 ipp_attribute_t
*attr
, /* Current attribute */
10006 *attr2
; /* Job attribute */
10007 int jobid
; /* Job ID */
10008 cupsd_job_t
*job
; /* Current job */
10009 char method
[HTTP_MAX_URI
],
10010 /* Method portion of URI */
10011 username
[HTTP_MAX_URI
],
10012 /* Username portion of URI */
10013 host
[HTTP_MAX_URI
],
10014 /* Host portion of URI */
10015 resource
[HTTP_MAX_URI
];
10016 /* Resource portion of URI */
10017 int port
; /* Port portion of URI */
10018 int event
; /* Events? */
10019 int check_jobs
; /* Check jobs? */
10022 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
10023 con
->http
.fd
, uri
->values
[0].string
.text
);
10026 * Start with "everything is OK" status...
10029 con
->response
->request
.status
.status_code
= IPP_OK
;
10032 * See if we have a job URI or a printer URI...
10035 if (!strcmp(uri
->name
, "printer-uri"))
10038 * Got a printer URI; see if we also have a job-id attribute...
10041 if ((attr
= ippFindAttribute(con
->request
, "job-id",
10042 IPP_TAG_INTEGER
)) == NULL
)
10044 send_ipp_status(con
, IPP_BAD_REQUEST
,
10045 _("Got a printer-uri attribute but no job-id!"));
10049 jobid
= attr
->values
[0].integer
;
10054 * Got a job URI; parse it to get the job ID...
10057 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
10058 sizeof(method
), username
, sizeof(username
), host
,
10059 sizeof(host
), &port
, resource
, sizeof(resource
));
10061 if (strncmp(resource
, "/jobs/", 6))
10067 send_ipp_status(con
, IPP_BAD_REQUEST
,
10068 _("Bad job-uri attribute \"%s\"!"),
10069 uri
->values
[0].string
.text
);
10073 jobid
= atoi(resource
+ 6);
10077 * See if the job exists...
10080 if ((job
= cupsdFindJob(jobid
)) == NULL
)
10083 * Nope - return a "not found" error...
10086 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
10091 * See if the job has been completed...
10094 if (job
->state_value
> IPP_JOB_STOPPED
)
10097 * Return a "not-possible" error...
10100 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10101 _("Job #%d is finished and cannot be altered!"), jobid
);
10106 * See if the job is owned by the requesting user...
10109 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
10111 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
10116 * See what the user wants to change.
10124 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10126 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
10129 if (!strcmp(attr
->name
, "attributes-charset") ||
10130 !strcmp(attr
->name
, "attributes-natural-language") ||
10131 !strcmp(attr
->name
, "document-compression") ||
10132 !strcmp(attr
->name
, "document-format") ||
10133 !strcmp(attr
->name
, "job-detailed-status-messages") ||
10134 !strcmp(attr
->name
, "job-document-access-errors") ||
10135 !strcmp(attr
->name
, "job-id") ||
10136 !strcmp(attr
->name
, "job-impressions-completed") ||
10137 !strcmp(attr
->name
, "job-k-octets") ||
10138 !strcmp(attr
->name
, "job-originating-host-name") ||
10139 !strcmp(attr
->name
, "job-originating-user-name") ||
10140 !strcmp(attr
->name
, "job-printer-up-time") ||
10141 !strcmp(attr
->name
, "job-printer-uri") ||
10142 !strcmp(attr
->name
, "job-sheets") ||
10143 !strcmp(attr
->name
, "job-state-message") ||
10144 !strcmp(attr
->name
, "job-state-reasons") ||
10145 !strcmp(attr
->name
, "job-uri") ||
10146 !strcmp(attr
->name
, "number-of-documents") ||
10147 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
10148 !strcmp(attr
->name
, "output-device-assigned") ||
10149 !strncmp(attr
->name
, "date-time-at-", 13) ||
10150 !strncmp(attr
->name
, "job-k-octets", 12) ||
10151 !strncmp(attr
->name
, "job-media-sheets", 16) ||
10152 !strncmp(attr
->name
, "time-at-", 8))
10158 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
10159 _("%s cannot be changed."), attr
->name
);
10161 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10162 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10167 if (!strcmp(attr
->name
, "job-priority"))
10170 * Change the job priority...
10173 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10175 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
10177 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10178 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10180 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
10182 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10183 _("Job is completed and cannot be changed."));
10186 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10188 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
10189 attr
->values
[0].integer
);
10190 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
10193 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
10194 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
10197 else if (!strcmp(attr
->name
, "job-state"))
10200 * Change the job state...
10203 if (attr
->value_tag
!= IPP_TAG_ENUM
)
10205 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
10207 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10208 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10212 switch (attr
->values
[0].integer
)
10214 case IPP_JOB_PENDING
:
10215 case IPP_JOB_HELD
:
10216 if (job
->state_value
> IPP_JOB_HELD
)
10218 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10219 _("Job state cannot be changed."));
10222 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10224 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10225 attr
->values
[0].integer
);
10227 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
10228 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
10230 event
|= CUPSD_EVENT_JOB_STATE
;
10235 case IPP_JOB_PROCESSING
:
10236 case IPP_JOB_STOPPED
:
10237 if (job
->state_value
!= attr
->values
[0].integer
)
10239 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10240 _("Job state cannot be changed."));
10245 case IPP_JOB_CANCELED
:
10246 case IPP_JOB_ABORTED
:
10247 case IPP_JOB_COMPLETED
:
10248 if (job
->state_value
> IPP_JOB_PROCESSING
)
10250 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10251 _("Job state cannot be changed."));
10254 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10256 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10257 attr
->values
[0].integer
);
10258 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
10266 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10268 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10269 IPP_TAG_ZERO
)) != NULL
)
10272 * Some other value; first free the old value...
10275 if (job
->attrs
->prev
)
10276 job
->attrs
->prev
->next
= attr2
->next
;
10278 job
->attrs
->attrs
= attr2
->next
;
10280 if (job
->attrs
->last
== attr2
)
10281 job
->attrs
->last
= job
->attrs
->prev
;
10283 _ippFreeAttr(attr2
);
10286 * Then copy the attribute...
10289 copy_attribute(job
->attrs
, attr
, 0);
10292 * See if the job-name or job-hold-until is being changed.
10295 if (!strcmp(attr
->name
, "job-hold-until"))
10297 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10298 attr
->values
[0].string
.text
);
10299 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
10301 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10302 cupsdReleaseJob(job
);
10307 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10310 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10313 * Delete the attribute...
10316 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10317 IPP_TAG_ZERO
)) != NULL
)
10319 if (job
->attrs
->prev
)
10320 job
->attrs
->prev
->next
= attr2
->next
;
10322 job
->attrs
->attrs
= attr2
->next
;
10324 if (attr2
== job
->attrs
->last
)
10325 job
->attrs
->last
= job
->attrs
->prev
;
10327 _ippFreeAttr(attr2
);
10329 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10335 * Add new option by copying it...
10338 copy_attribute(job
->attrs
, attr
, 0);
10340 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10349 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10352 * Send events as needed...
10355 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10356 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10357 cupsdFindDest(job
->dest
), job
,
10358 "Job priority changed by user.");
10360 if (event
& CUPSD_EVENT_JOB_STATE
)
10361 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10362 job
->state_value
== IPP_JOB_HELD
?
10363 "Job held by user." : "Job restarted by user.");
10365 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10366 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10367 "Job options changed by user.");
10370 * Start jobs if possible...
10379 * 'set_printer_defaults()' - Set printer default options from a request.
10383 set_printer_defaults(
10384 cupsd_client_t
*con
, /* I - Client connection */
10385 cupsd_printer_t
*printer
) /* I - Printer */
10387 int i
; /* Looping var */
10388 ipp_attribute_t
*attr
; /* Current attribute */
10389 int namelen
; /* Length of attribute name */
10390 char name
[256], /* New attribute name */
10391 value
[256]; /* String version of integer attrs */
10394 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10397 * Skip non-printer attributes...
10400 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10403 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10405 if (!strcmp(attr
->name
, "job-sheets-default"))
10408 * Only allow keywords and names...
10411 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10415 * Only allow job-sheets-default to be set when running without a
10416 * system high classification level...
10419 if (Classification
)
10422 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10424 if (attr
->num_values
> 1)
10425 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10427 cupsdSetString(&printer
->job_sheets
[1], "none");
10429 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10431 cupsdFreePrinterUsers(printer
);
10433 printer
->deny_users
= 0;
10435 if (attr
->value_tag
== IPP_TAG_NAME
&&
10436 (attr
->num_values
> 1 ||
10437 strcmp(attr
->values
[0].string
.text
, "all")))
10439 for (i
= 0; i
< attr
->num_values
; i
++)
10440 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
10443 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10445 cupsdFreePrinterUsers(printer
);
10447 printer
->deny_users
= 1;
10449 if (attr
->value_tag
== IPP_TAG_NAME
&&
10450 (attr
->num_values
> 1 ||
10451 strcmp(attr
->values
[0].string
.text
, "none")))
10453 for (i
= 0; i
< attr
->num_values
; i
++)
10454 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
10457 else if (!strcmp(attr
->name
, "job-quota-period"))
10459 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10462 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10463 attr
->values
[0].integer
);
10464 cupsdFreeQuotas(printer
);
10466 printer
->quota_period
= attr
->values
[0].integer
;
10468 else if (!strcmp(attr
->name
, "job-k-limit"))
10470 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10473 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10474 attr
->values
[0].integer
);
10475 cupsdFreeQuotas(printer
);
10477 printer
->k_limit
= attr
->values
[0].integer
;
10479 else if (!strcmp(attr
->name
, "job-page-limit"))
10481 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10484 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10485 attr
->values
[0].integer
);
10486 cupsdFreeQuotas(printer
);
10488 printer
->page_limit
= attr
->values
[0].integer
;
10490 else if (!strcmp(attr
->name
, "printer-op-policy"))
10492 cupsd_policy_t
*p
; /* Policy */
10495 if (attr
->value_tag
!= IPP_TAG_NAME
)
10498 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10500 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10501 "Setting printer-op-policy to \"%s\"...",
10502 attr
->values
[0].string
.text
);
10503 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10504 printer
->op_policy_ptr
= p
;
10508 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10509 _("Unknown printer-op-policy \"%s\"."),
10510 attr
->values
[0].string
.text
);
10514 else if (!strcmp(attr
->name
, "printer-error-policy"))
10516 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10519 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10520 strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10521 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10522 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
10524 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10525 _("Unknown printer-error-policy \"%s\"."),
10526 attr
->values
[0].string
.text
);
10530 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10531 "Setting printer-error-policy to \"%s\"...",
10532 attr
->values
[0].string
.text
);
10533 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10537 * Skip any other non-default attributes...
10540 namelen
= strlen(attr
->name
);
10541 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10542 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10546 * OK, anything else must be a user-defined default...
10549 strlcpy(name
, attr
->name
, sizeof(name
));
10550 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10552 switch (attr
->value_tag
)
10554 case IPP_TAG_DELETEATTR
:
10555 printer
->num_options
= cupsRemoveOption(name
,
10556 printer
->num_options
,
10557 &(printer
->options
));
10558 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10559 "Deleting %s", attr
->name
);
10562 case IPP_TAG_NAME
:
10563 case IPP_TAG_KEYWORD
:
10565 printer
->num_options
= cupsAddOption(name
,
10566 attr
->values
[0].string
.text
,
10567 printer
->num_options
,
10568 &(printer
->options
));
10569 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10570 "Setting %s to \"%s\"...", attr
->name
,
10571 attr
->values
[0].string
.text
);
10574 case IPP_TAG_BOOLEAN
:
10575 printer
->num_options
= cupsAddOption(name
,
10576 attr
->values
[0].boolean
?
10578 printer
->num_options
,
10579 &(printer
->options
));
10580 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10581 "Setting %s to %s...", attr
->name
,
10582 attr
->values
[0].boolean
? "true" : "false");
10585 case IPP_TAG_INTEGER
:
10586 case IPP_TAG_ENUM
:
10587 sprintf(value
, "%d", attr
->values
[0].integer
);
10588 printer
->num_options
= cupsAddOption(name
, value
,
10589 printer
->num_options
,
10590 &(printer
->options
));
10591 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10592 "Setting %s to %s...", attr
->name
, value
);
10595 case IPP_TAG_RANGE
:
10596 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10597 attr
->values
[0].range
.upper
);
10598 printer
->num_options
= cupsAddOption(name
, value
,
10599 printer
->num_options
,
10600 &(printer
->options
));
10601 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10602 "Setting %s to %s...", attr
->name
, value
);
10605 case IPP_TAG_RESOLUTION
:
10606 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10607 attr
->values
[0].resolution
.yres
,
10608 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10610 printer
->num_options
= cupsAddOption(name
, value
,
10611 printer
->num_options
,
10612 &(printer
->options
));
10613 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10614 "Setting %s to %s...", attr
->name
, value
);
10618 /* Do nothing for other values */
10626 * 'start_printer()' - Start a printer.
10630 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10631 ipp_attribute_t
*uri
) /* I - Printer URI */
10633 http_status_t status
; /* Policy status */
10634 cups_ptype_t dtype
; /* Destination type (printer/class) */
10635 cupsd_printer_t
*printer
; /* Printer data */
10638 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10639 con
->http
.fd
, uri
->values
[0].string
.text
);
10642 * Is the destination valid?
10645 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10651 send_ipp_status(con
, IPP_NOT_FOUND
,
10652 _("The printer or class was not found."));
10660 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10662 send_http_error(con
, status
, printer
);
10667 * Start the printer...
10670 printer
->state_message
[0] = '\0';
10672 cupsdStartPrinter(printer
, 1);
10674 if (dtype
& CUPS_PRINTER_CLASS
)
10675 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10676 printer
->name
, get_username(con
));
10678 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10679 printer
->name
, get_username(con
));
10684 * Everything was ok, so return OK status...
10687 con
->response
->request
.status
.status_code
= IPP_OK
;
10692 * 'stop_printer()' - Stop a printer.
10696 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10697 ipp_attribute_t
*uri
) /* I - Printer URI */
10699 http_status_t status
; /* Policy status */
10700 cups_ptype_t dtype
; /* Destination type (printer/class) */
10701 cupsd_printer_t
*printer
; /* Printer data */
10702 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10705 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10706 con
->http
.fd
, uri
->values
[0].string
.text
);
10709 * Is the destination valid?
10712 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10718 send_ipp_status(con
, IPP_NOT_FOUND
,
10719 _("The printer or class was not found."));
10727 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10729 send_http_error(con
, status
, printer
);
10734 * Stop the printer...
10737 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10738 IPP_TAG_TEXT
)) == NULL
)
10739 strcpy(printer
->state_message
, "Paused");
10742 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10743 sizeof(printer
->state_message
));
10746 cupsdStopPrinter(printer
, 1);
10748 if (dtype
& CUPS_PRINTER_CLASS
)
10749 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10750 printer
->name
, get_username(con
));
10752 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10753 printer
->name
, get_username(con
));
10756 * Everything was ok, so return OK status...
10759 con
->response
->request
.status
.status_code
= IPP_OK
;
10764 * 'url_encode_attr()' - URL-encode a string attribute.
10768 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10769 char *buffer
,/* I - String buffer */
10770 int bufsize
)/* I - Size of buffer */
10772 int i
; /* Looping var */
10773 char *bufptr
, /* Pointer into buffer */
10774 *bufend
; /* End of buffer */
10777 strlcpy(buffer
, attr
->name
, bufsize
);
10778 bufptr
= buffer
+ strlen(buffer
);
10779 bufend
= buffer
+ bufsize
- 1;
10781 for (i
= 0; i
< attr
->num_values
; i
++)
10783 if (bufptr
>= bufend
)
10791 if (bufptr
>= bufend
)
10796 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
10797 bufptr
, bufend
- bufptr
+ 1);
10799 if (bufptr
>= bufend
)
10810 * 'url_encode_string()' - URL-encode a string.
10813 static char * /* O - End of string */
10814 url_encode_string(const char *s
, /* I - String */
10815 char *buffer
, /* I - String buffer */
10816 int bufsize
) /* I - Size of buffer */
10818 char *bufptr
, /* Pointer into buffer */
10819 *bufend
; /* End of buffer */
10820 static const char *hex
= "0123456789ABCDEF";
10825 bufend
= buffer
+ bufsize
- 1;
10827 while (*s
&& bufptr
< bufend
)
10829 if (*s
== ' ' || *s
== '%')
10831 if (bufptr
>= (bufend
- 2))
10835 *bufptr
++ = hex
[(*s
>> 4) & 15];
10836 *bufptr
++ = hex
[*s
& 15];
10840 else if (*s
== '\'' || *s
== '\\')
10842 if (bufptr
>= (bufend
- 1))
10859 * 'user_allowed()' - See if a user is allowed to print to a queue.
10862 static int /* O - 0 if not allowed, 1 if allowed */
10863 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
10864 const char *username
) /* I - Username */
10866 int i
; /* Looping var */
10867 struct passwd
*pw
; /* User password data */
10868 char baseuser
[256], /* Base username */
10869 *baseptr
; /* Pointer to "@" in base username */
10872 if (p
->num_users
== 0)
10875 if (!strcmp(username
, "root"))
10878 if (strchr(username
, '@'))
10881 * Strip @REALM for username check...
10884 strlcpy(baseuser
, username
, sizeof(baseuser
));
10886 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
10889 username
= baseuser
;
10892 pw
= getpwnam(username
);
10895 for (i
= 0; i
< p
->num_users
; i
++)
10897 if (p
->users
[i
][0] == '@')
10900 * Check group membership...
10903 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
10906 else if (p
->users
[i
][0] == '#')
10912 if (cupsdCheckGroup(username
, pw
, p
->users
[i
]))
10915 else if (!strcasecmp(username
, p
->users
[i
]))
10919 return ((i
< p
->num_users
) != p
->deny_users
);
10924 * 'validate_job()' - Validate printer options and destination.
10928 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10929 ipp_attribute_t
*uri
) /* I - Printer URI */
10931 http_status_t status
; /* Policy status */
10932 ipp_attribute_t
*attr
; /* Current attribute */
10933 ipp_attribute_t
*format
; /* Document-format attribute */
10934 cups_ptype_t dtype
; /* Destination type (printer/class) */
10935 char super
[MIME_MAX_SUPER
],
10936 /* Supertype of file */
10937 type
[MIME_MAX_TYPE
];
10938 /* Subtype of file */
10939 cupsd_printer_t
*printer
; /* Printer */
10942 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10943 con
->http
.fd
, uri
->values
[0].string
.text
);
10946 * OK, see if the client is sending the document compressed - CUPS
10947 * doesn't support compression yet...
10950 if ((attr
= ippFindAttribute(con
->request
, "compression",
10951 IPP_TAG_KEYWORD
)) != NULL
&&
10952 !strcmp(attr
->values
[0].string
.text
, "none"))
10954 send_ipp_status(con
, IPP_ATTRIBUTES
,
10955 _("Unsupported compression attribute %s!"),
10956 attr
->values
[0].string
.text
);
10957 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10958 "compression", NULL
, attr
->values
[0].string
.text
);
10963 * Is it a format we support?
10966 if ((format
= ippFindAttribute(con
->request
, "document-format",
10967 IPP_TAG_MIMETYPE
)) != NULL
)
10969 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
10972 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
10973 format
->values
[0].string
.text
);
10977 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10978 !mimeType(MimeDatabase
, super
, type
))
10980 cupsdLogMessage(CUPSD_LOG_INFO
,
10981 "Hint: Do you have the raw file printing rules enabled?");
10982 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10983 _("Unsupported format \"%s\"!"),
10984 format
->values
[0].string
.text
);
10985 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10986 "document-format", NULL
, format
->values
[0].string
.text
);
10992 * Is the destination valid?
10995 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11001 send_ipp_status(con
, IPP_NOT_FOUND
,
11002 _("The printer or class was not found."));
11010 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11012 send_http_error(con
, status
, printer
);
11017 * Everything was ok, so return OK status...
11020 con
->response
->request
.status
.status_code
= IPP_OK
;
11025 * 'validate_name()' - Make sure the printer name only contains valid chars.
11028 static int /* O - 0 if name is no good, 1 if good */
11029 validate_name(const char *name
) /* I - Name to check */
11031 const char *ptr
; /* Pointer into name */
11035 * Scan the whole name...
11038 for (ptr
= name
; *ptr
; ptr
++)
11039 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
11043 * All the characters are good; validate the length, too...
11046 return ((ptr
- name
) < 128);
11051 * 'validate_user()' - Validate the user for the request.
11054 static int /* O - 1 if permitted, 0 otherwise */
11055 validate_user(cupsd_job_t
*job
, /* I - Job */
11056 cupsd_client_t
*con
, /* I - Client connection */
11057 const char *owner
, /* I - Owner of job/resource */
11058 char *username
, /* O - Authenticated username */
11059 int userlen
) /* I - Length of username */
11061 cupsd_printer_t
*printer
; /* Printer for job */
11064 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
11065 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
11067 job
->id
, con
? con
->http
.fd
: 0,
11068 owner
? owner
: "(null)", username
, userlen
);
11071 * Validate input...
11074 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11078 * Get the best authenticated username that is available.
11081 strlcpy(username
, get_username(con
), userlen
);
11084 * Check the username against the owner...
11087 printer
= cupsdFindDest(job
->dest
);
11089 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11090 con
, owner
) == HTTP_OK
);
11095 * End of "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $".