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...
2761 apple_unregister_profiles(printer
);
2762 apple_register_profiles(printer
);
2763 #endif /* __APPLE__ */
2768 * This must be an interface script, so remove any old PPD file that
2769 * may be lying around...
2776 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2777 IPP_TAG_NAME
)) != NULL
)
2779 need_restart_job
= 1;
2781 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2784 * Raw driver, remove any existing PPD or interface script files.
2787 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2791 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2801 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2805 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2808 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2810 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2814 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2815 "Copied PPD file successfully!");
2816 chmod(dstfile
, 0644);
2820 * (Re)register color profiles...
2823 apple_unregister_profiles(printer
);
2824 apple_register_profiles(printer
);
2825 #endif /* __APPLE__ */
2830 * If we set the device URI but not the port monitor, check which port
2831 * monitor to use by default...
2834 if (set_device_uri
&& !set_port_monitor
)
2836 ppd_file_t
*ppd
; /* PPD file */
2837 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2840 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2841 sizeof(scheme
), username
, sizeof(username
), host
,
2842 sizeof(host
), &port
, resource
, sizeof(resource
));
2844 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2846 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2848 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2850 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2851 if (!strcmp(scheme
, ppdattr
->spec
))
2853 cupsdLogMessage(CUPSD_LOG_INFO
,
2854 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2855 printer
->name
, ppdattr
->value
,
2856 printer
->port_monitor
? printer
->port_monitor
2859 if (strcmp(ppdattr
->value
, "none"))
2860 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2862 cupsdClearString(&printer
->port_monitor
);
2872 * Update the printer attributes and return...
2875 cupsdSetPrinterAttrs(printer
);
2876 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2878 if (need_restart_job
&& printer
->job
)
2883 * Stop the current job and then restart it below...
2886 job
= (cupsd_job_t
*)printer
->job
;
2888 cupsdStopJob(job
, 1);
2890 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2891 job
->state_value
= IPP_JOB_PENDING
;
2894 if (need_restart_job
)
2897 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2901 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2902 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2905 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2906 printer
->name
, get_username(con
));
2910 cupsdAddPrinterHistory(printer
);
2912 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2913 "New printer \"%s\" added by \"%s\".", printer
->name
,
2916 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2917 printer
->name
, get_username(con
));
2920 con
->response
->request
.status
.status_code
= IPP_OK
;
2925 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2926 * based upon the printer state...
2930 add_printer_state_reasons(
2931 cupsd_client_t
*con
, /* I - Client connection */
2932 cupsd_printer_t
*p
) /* I - Printer info */
2934 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2935 "add_printer_state_reasons(%p[%d], %p[%s])",
2936 con
, con
->http
.fd
, p
, p
->name
);
2938 if (p
->num_reasons
== 0)
2939 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2940 "printer-state-reasons", NULL
,
2941 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2943 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2944 "printer-state-reasons", p
->num_reasons
, NULL
,
2945 (const char * const *)p
->reasons
);
2950 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2951 * the specified printer or class.
2955 add_queued_job_count(
2956 cupsd_client_t
*con
, /* I - Client connection */
2957 cupsd_printer_t
*p
) /* I - Printer or class */
2959 int count
; /* Number of jobs on destination */
2962 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2963 con
, con
->http
.fd
, p
, p
->name
);
2965 count
= cupsdGetPrinterJobCount(p
->name
);
2967 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2968 "queued-job-count", count
);
2974 * 'apple_init_profile()' - Initialize a color profile.
2979 ppd_file_t
*ppd
, /* I - PPD file */
2980 cups_array_t
*languages
, /* I - Languages in the PPD file */
2981 CMDeviceProfileInfo
*profile
, /* I - Profile record */
2982 unsigned id
, /* I - Profile ID */
2983 const char *name
, /* I - Profile name */
2984 const char *text
, /* I - Profile UI text */
2985 const char *iccfile
) /* I - ICC filename */
2987 char url
[1024]; /* URL for profile filename */
2988 CFMutableDictionaryRef dict
; /* Dictionary for name */
2989 char *language
; /* Current language */
2990 ppd_attr_t
*attr
; /* Profile attribute */
2991 CFStringRef cflang
, /* Language string */
2992 cftext
; /* Localized text */
2996 * Build the profile name dictionary...
2999 dict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3000 &kCFTypeDictionaryKeyCallBacks
,
3001 &kCFTypeDictionaryValueCallBacks
);
3003 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, text
,
3004 kCFStringEncodingUTF8
);
3008 CFDictionarySetValue(dict
, CFSTR("en"), cftext
);
3015 * Find localized names for the color profiles...
3018 cupsArraySave(ppd
->sorted_attrs
);
3020 for (language
= (char *)cupsArrayFirst(languages
);
3022 language
= (char *)cupsArrayNext(languages
))
3026 if ((attr
= _ppdLocalizedAttr(ppd
, "cupsICCProfile", name
,
3028 attr
= _ppdLocalizedAttr(ppd
, "APTiogaProfile", name
, language
);
3031 attr
= _ppdLocalizedAttr(ppd
, "ColorModel", name
, language
);
3033 if (attr
&& attr
->text
[0])
3035 cflang
= CFStringCreateWithCString(kCFAllocatorDefault
, language
,
3036 kCFStringEncodingUTF8
);
3037 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, attr
->text
,
3038 kCFStringEncodingUTF8
);
3040 if (cflang
&& cftext
)
3041 CFDictionarySetValue(dict
, cflang
, cftext
);
3051 cupsArrayRestore(ppd
->sorted_attrs
);
3055 * Fill in the profile data...
3059 httpAssembleURI(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "file", NULL
, "", 0,
3062 profile
->dataVersion
= cmDeviceProfileInfoVersion1
;
3063 profile
->profileID
= id
;
3064 profile
->profileLoc
.locType
= iccfile
? cmPathBasedProfile
: cmNoProfileBase
;
3065 profile
->profileName
= dict
;
3068 strlcpy(profile
->profileLoc
.u
.pathLoc
.path
, iccfile
,
3069 sizeof(profile
->profileLoc
.u
.pathLoc
.path
));
3074 * 'apple_register_profiles()' - Register color profiles for a printer.
3078 apple_register_profiles(
3079 cupsd_printer_t
*p
) /* I - Printer */
3081 int i
; /* Looping var */
3082 char ppdfile
[1024], /* PPD filename */
3083 iccfile
[1024], /* ICC filename */
3084 selector
[PPD_MAX_NAME
];
3085 /* Profile selection string */
3086 ppd_file_t
*ppd
; /* PPD file */
3087 ppd_attr_t
*attr
, /* Profile attributes */
3088 *profileid_attr
,/* cupsProfileID attribute */
3089 *q1_attr
, /* ColorModel (or other) qualifier */
3090 *q2_attr
, /* MediaType (or other) qualifier */
3091 *q3_attr
; /* Resolution (or other) qualifier */
3092 char q_keyword
[PPD_MAX_NAME
];
3093 /* Qualifier keyword */
3094 const char *q1_choice
, /* ColorModel (or other) choice */
3095 *q2_choice
, /* MediaType (or other) choice */
3096 *q3_choice
; /* Resolution (or other) choice */
3097 const char *profile_key
; /* Profile keyword */
3098 ppd_option_t
*cm_option
; /* Color model option */
3099 ppd_choice_t
*cm_choice
; /* Color model choice */
3100 int num_profiles
; /* Number of profiles */
3101 CMError error
; /* Last error */
3102 unsigned device_id
, /* Printer device ID */
3103 profile_id
, /* Profile ID */
3104 default_profile_id
= 0;
3105 /* Default profile ID */
3106 CFMutableDictionaryRef device_name
; /* Printer device name dictionary */
3107 CFStringRef printer_name
; /* Printer name string */
3108 CMDeviceScope scope
= /* Scope of the registration */
3110 kCFPreferencesAnyUser
,
3111 kCFPreferencesCurrentHost
3113 CMDeviceProfileArrayPtr profiles
; /* Profiles */
3114 CMDeviceProfileInfo
*profile
; /* Current profile */
3115 cups_array_t
*languages
; /* Languages array */
3119 * Make sure ColorSync is available...
3122 if (CMRegisterColorDevice
== NULL
)
3126 * Try opening the PPD file for this printer...
3129 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3130 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
3134 * See if we have any profiles...
3137 if ((attr
= ppdFindAttr(ppd
, "APTiogaProfile", NULL
)) != NULL
)
3138 profile_key
= "APTiogaProfile";
3141 attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
3142 profile_key
= "cupsICCProfile";
3145 for (num_profiles
= 0; attr
; attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3146 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3148 if (attr
->value
[0] != '/')
3149 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3152 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3154 if (access(iccfile
, 0))
3162 * If we have profiles, add them...
3165 if (num_profiles
> 0)
3167 if (profile_key
[0] == 'A')
3170 * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile
3174 if ((attr
= ppdFindAttr(ppd
, "DefaultAPTiogaProfile", NULL
)) != NULL
&&
3176 default_profile_id
= atoi(attr
->value
);
3178 q1_choice
= q2_choice
= q3_choice
= NULL
;
3183 * For CUPS PPDs, figure out the default profile selector values...
3186 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier1", NULL
)) != NULL
&&
3187 attr
->value
&& attr
->value
[0])
3189 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3190 q1_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3192 else if ((q1_attr
= ppdFindAttr(ppd
, "DefaultColorModel", NULL
)) == NULL
)
3193 q1_attr
= ppdFindAttr(ppd
, "DefaultColorSpace", NULL
);
3195 if (q1_attr
&& q1_attr
->value
&& q1_attr
->value
[0])
3196 q1_choice
= q1_attr
->value
;
3200 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier2", NULL
)) != NULL
&&
3201 attr
->value
&& attr
->value
[0])
3203 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3204 q2_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3207 q2_attr
= ppdFindAttr(ppd
, "DefaultMediaType", NULL
);
3209 if (q2_attr
&& q2_attr
->value
&& q2_attr
->value
[0])
3210 q2_choice
= q2_attr
->value
;
3214 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier3", NULL
)) != NULL
&&
3215 attr
->value
&& attr
->value
[0])
3217 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3218 q3_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3221 q3_attr
= ppdFindAttr(ppd
, "DefaultResolution", NULL
);
3223 if (q3_attr
&& q3_attr
->value
&& q3_attr
->value
[0])
3224 q3_choice
= q3_attr
->value
;
3230 * Build the array of profiles...
3232 * Note: This calloc actually requests slightly more memory than needed.
3235 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3237 cupsdLogMessage(CUPSD_LOG_ERROR
,
3238 "Unable to allocate memory for %d profiles!",
3244 profiles
->profileCount
= num_profiles
;
3245 languages
= _ppdGetLanguages(ppd
);
3247 for (profile
= profiles
->profiles
,
3248 attr
= ppdFindAttr(ppd
, profile_key
, NULL
);
3250 attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3251 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3254 * Add this profile...
3257 if (attr
->value
[0] != '/')
3258 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3261 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3263 if (access(iccfile
, 0))
3266 if (profile_key
[0] == 'c')
3268 cupsArraySave(ppd
->sorted_attrs
);
3270 if ((profileid_attr
= ppdFindAttr(ppd
, "cupsProfileID",
3271 attr
->spec
)) != NULL
&&
3272 profileid_attr
->value
&& isdigit(profileid_attr
->value
[0] & 255))
3273 profile_id
= (unsigned)strtoul(profileid_attr
->value
, NULL
, 10);
3275 profile_id
= _ppdHashName(attr
->spec
);
3277 cupsArrayRestore(ppd
->sorted_attrs
);
3280 profile_id
= atoi(attr
->spec
);
3282 apple_init_profile(ppd
, languages
, profile
, profile_id
, attr
->spec
,
3283 attr
->text
[0] ? attr
->text
: attr
->spec
, iccfile
);
3288 * See if this is the default profile...
3291 if (!default_profile_id
)
3297 snprintf(selector
, sizeof(selector
), "%s.%s.%s",
3298 q1_choice
, q2_choice
, q3_choice
);
3299 if (!strcmp(selector
, attr
->spec
))
3300 default_profile_id
= profile_id
;
3303 if (!default_profile_id
)
3305 snprintf(selector
, sizeof(selector
), "%s.%s.", q1_choice
,
3307 if (!strcmp(selector
, attr
->spec
))
3308 default_profile_id
= profile_id
;
3312 if (!default_profile_id
&& q3_choice
)
3314 snprintf(selector
, sizeof(selector
), "%s..%s", q1_choice
,
3316 if (!strcmp(selector
, attr
->spec
))
3317 default_profile_id
= profile_id
;
3320 if (!default_profile_id
)
3322 snprintf(selector
, sizeof(selector
), "%s..", q1_choice
);
3323 if (!strcmp(selector
, attr
->spec
))
3324 default_profile_id
= profile_id
;
3329 _ppdFreeLanguages(languages
);
3331 else if ((cm_option
= ppdFindOption(ppd
, "ColorModel")) != NULL
)
3334 * Extract profiles from ColorModel option...
3337 const char *profile_name
; /* Name of generic profile */
3340 num_profiles
= cm_option
->num_choices
;
3342 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3344 cupsdLogMessage(CUPSD_LOG_ERROR
,
3345 "Unable to allocate memory for %d profiles!",
3351 profiles
->profileCount
= num_profiles
;
3353 for (profile
= profiles
->profiles
, i
= cm_option
->num_choices
,
3354 cm_choice
= cm_option
->choices
;
3356 i
--, cm_choice
++, profile
++)
3358 if (!strcmp(cm_choice
->choice
, "Gray") ||
3359 !strcmp(cm_choice
->choice
, "Black"))
3360 profile_name
= "Gray";
3361 else if (!strcmp(cm_choice
->choice
, "RGB") ||
3362 !strcmp(cm_choice
->choice
, "CMY"))
3363 profile_name
= "RGB";
3364 else if (!strcmp(cm_choice
->choice
, "CMYK") ||
3365 !strcmp(cm_choice
->choice
, "KCMY"))
3366 profile_name
= "CMYK";
3368 profile_name
= "DeviceN";
3370 snprintf(selector
, sizeof(selector
), "%s..", profile_name
);
3371 profile_id
= _ppdHashName(selector
);
3373 apple_init_profile(ppd
, NULL
, profile
, profile_id
, cm_choice
->choice
,
3374 cm_choice
->text
, NULL
);
3376 if (cm_choice
->marked
)
3377 default_profile_id
= profile_id
;
3383 * Use the default colorspace...
3386 num_profiles
= 1 + ppd
->colorspace
!= PPD_CS_GRAY
;
3388 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3390 cupsdLogMessage(CUPSD_LOG_ERROR
,
3391 "Unable to allocate memory for %d profiles!",
3397 profiles
->profileCount
= num_profiles
;
3399 apple_init_profile(ppd
, NULL
, profiles
->profiles
, _ppdHashName("Gray.."),
3400 "Gray", "Gray", NULL
);
3402 switch (ppd
->colorspace
)
3406 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3407 _ppdHashName("RGB.."), "RGB", "RGB", NULL
);
3411 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3412 _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL
);
3416 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3417 _ppdHashName("DeviceN.."), "DeviceN", "DeviceN",
3426 if (num_profiles
> 0)
3429 * Make sure we have a default profile ID...
3432 if (!default_profile_id
)
3433 default_profile_id
= profiles
->profiles
[num_profiles
- 1].profileID
;
3436 * Get the device ID hash and pathelogical name dictionary.
3439 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\"",
3442 device_id
= _ppdHashName(p
->name
);
3443 device_name
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3444 &kCFTypeDictionaryKeyCallBacks
,
3445 &kCFTypeDictionaryValueCallBacks
);
3446 printer_name
= CFStringCreateWithCString(kCFAllocatorDefault
,
3447 p
->name
, kCFStringEncodingUTF8
);
3449 if (device_name
&& printer_name
)
3451 CFDictionarySetValue(device_name
, CFSTR("en"), printer_name
);
3454 * Register the device with ColorSync...
3457 error
= CMRegisterColorDevice(cmPrinterDeviceClass
, device_id
,
3458 device_name
, &scope
);
3461 * Register the profiles...
3465 error
= CMSetDeviceFactoryProfiles(cmPrinterDeviceClass
, device_id
,
3466 default_profile_id
, profiles
);
3476 cupsdLogMessage(CUPSD_LOG_ERROR
,
3477 "Unable to register ICC color profiles for \"%s\" - %d",
3478 p
->name
, (int)error
);
3480 for (profile
= profiles
->profiles
;
3482 profile
++, num_profiles
--)
3483 CFRelease(profile
->profileName
);
3488 CFRelease(printer_name
);
3491 CFRelease(device_name
);
3499 * 'apple_unregister_profiles()' - Remove color profiles for the specified
3504 apple_unregister_profiles(
3505 cupsd_printer_t
*p
) /* I - Printer */
3508 * Make sure ColorSync is available...
3511 if (CMUnregisterColorDevice
!= NULL
)
3512 CMUnregisterColorDevice(cmPrinterDeviceClass
, _ppdHashName(p
->name
));
3514 #endif /* __APPLE__ */
3517 * 'apply_printer_defaults()' - Apply printer default options to a job.
3521 apply_printer_defaults(
3522 cupsd_printer_t
*printer
, /* I - Printer */
3523 cupsd_job_t
*job
) /* I - Job */
3525 int i
, /* Looping var */
3526 num_options
; /* Number of default options */
3527 cups_option_t
*options
, /* Default options */
3528 *option
; /* Current option */
3532 * Collect all of the default options and add the missing ones to the
3536 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
3537 option
= printer
->options
;
3540 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
3542 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
3547 * Encode these options as attributes in the job object...
3550 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
3551 cupsFreeOptions(num_options
, options
);
3556 * 'authenticate_job()' - Set job authentication info.
3560 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
3561 ipp_attribute_t
*uri
) /* I - Job URI */
3563 ipp_attribute_t
*attr
, /* job-id attribute */
3564 *auth_info
; /* auth-info attribute */
3565 int jobid
; /* Job ID */
3566 cupsd_job_t
*job
; /* Current job */
3567 char method
[HTTP_MAX_URI
],
3568 /* Method portion of URI */
3569 username
[HTTP_MAX_URI
],
3570 /* Username portion of URI */
3572 /* Host portion of URI */
3573 resource
[HTTP_MAX_URI
];
3574 /* Resource portion of URI */
3575 int port
; /* Port portion of URI */
3578 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
3579 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
3582 * Start with "everything is OK" status...
3585 con
->response
->request
.status
.status_code
= IPP_OK
;
3588 * See if we have a job URI or a printer URI...
3591 if (!strcmp(uri
->name
, "printer-uri"))
3594 * Got a printer URI; see if we also have a job-id attribute...
3597 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3598 IPP_TAG_INTEGER
)) == NULL
)
3600 send_ipp_status(con
, IPP_BAD_REQUEST
,
3601 _("Got a printer-uri attribute but no job-id!"));
3605 jobid
= attr
->values
[0].integer
;
3610 * Got a job URI; parse it to get the job ID...
3613 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
3614 sizeof(method
), username
, sizeof(username
), host
,
3615 sizeof(host
), &port
, resource
, sizeof(resource
));
3617 if (strncmp(resource
, "/jobs/", 6))
3623 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
3624 uri
->values
[0].string
.text
);
3628 jobid
= atoi(resource
+ 6);
3632 * See if the job exists...
3635 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3638 * Nope - return a "not found" error...
3641 send_ipp_status(con
, IPP_NOT_FOUND
,
3642 _("Job #%d does not exist!"), jobid
);
3647 * See if the job has been completed...
3650 if (job
->state_value
!= IPP_JOB_HELD
)
3653 * Return a "not-possible" error...
3656 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3657 _("Job #%d is not held for authentication!"),
3663 * See if we have already authenticated...
3666 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3668 if (!con
->username
[0] && !auth_info
)
3670 cupsd_printer_t
*printer
; /* Job destination */
3674 * No auth data. If we need to authenticate via Kerberos, send a
3675 * HTTP auth challenge, otherwise just return an IPP error...
3678 printer
= cupsdFindDest(job
->dest
);
3680 if (printer
&& printer
->num_auth_info_required
> 0 &&
3681 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3682 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3684 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3685 _("No authentication information provided!"));
3690 * See if the job is owned by the requesting user...
3693 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3695 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3700 * Save the authentication information for this job...
3703 save_auth_info(con
, job
, auth_info
);
3706 * Reset the job-hold-until value to "no-hold"...
3709 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3710 IPP_TAG_KEYWORD
)) == NULL
)
3711 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3715 attr
->value_tag
= IPP_TAG_KEYWORD
;
3716 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
3720 * Release the job and return...
3723 cupsdReleaseJob(job
);
3725 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3730 * 'cancel_all_jobs()' - Cancel all print jobs.
3734 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3735 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3737 http_status_t status
; /* Policy status */
3738 cups_ptype_t dtype
; /* Destination type */
3739 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3740 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3741 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3742 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3743 int port
; /* Port portion of URI */
3744 ipp_attribute_t
*attr
; /* Attribute in request */
3745 const char *username
; /* Username */
3746 int purge
; /* Purge? */
3747 cupsd_printer_t
*printer
; /* Printer */
3750 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3751 con
->http
.fd
, uri
->values
[0].string
.text
);
3754 * See if we have a printer URI...
3757 if (strcmp(uri
->name
, "printer-uri"))
3759 send_ipp_status(con
, IPP_BAD_REQUEST
,
3760 _("The printer-uri attribute is required!"));
3765 * Get the username (if any) for the jobs we want to cancel (only if
3766 * "my-jobs" is specified...
3769 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3770 IPP_TAG_BOOLEAN
)) != NULL
&&
3771 attr
->values
[0].boolean
)
3773 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3774 IPP_TAG_NAME
)) != NULL
)
3775 username
= attr
->values
[0].string
.text
;
3778 send_ipp_status(con
, IPP_BAD_REQUEST
,
3779 _("Missing requesting-user-name attribute!"));
3787 * Look for the "purge-jobs" attribute...
3790 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3791 IPP_TAG_BOOLEAN
)) != NULL
)
3792 purge
= attr
->values
[0].boolean
;
3797 * And if the destination is valid...
3800 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3806 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3807 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3808 hostname
, sizeof(hostname
), &port
,
3809 resource
, sizeof(resource
));
3811 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3812 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3814 send_ipp_status(con
, IPP_NOT_FOUND
,
3815 _("The printer or class was not found."));
3823 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3825 send_http_error(con
, status
, NULL
);
3830 * Cancel all jobs on all printers...
3833 cupsdCancelJobs(NULL
, username
, purge
);
3835 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3836 purge
? "purged" : "canceled", get_username(con
));
3844 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3847 send_http_error(con
, status
, printer
);
3852 * Cancel all of the jobs on the named printer...
3855 cupsdCancelJobs(printer
->name
, username
, purge
);
3857 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3858 printer
->name
, purge
? "purged" : "canceled",
3862 con
->response
->request
.status
.status_code
= IPP_OK
;
3867 * 'cancel_job()' - Cancel a print job.
3871 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3872 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3874 ipp_attribute_t
*attr
; /* Current attribute */
3875 int jobid
; /* Job ID */
3876 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3877 username
[HTTP_MAX_URI
], /* Username portion of URI */
3878 host
[HTTP_MAX_URI
], /* Host portion of URI */
3879 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3880 int port
; /* Port portion of URI */
3881 cupsd_job_t
*job
; /* Job information */
3882 cups_ptype_t dtype
; /* Destination type (printer/class) */
3883 cupsd_printer_t
*printer
; /* Printer data */
3884 int purge
; /* Purge the job? */
3887 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3888 con
->http
.fd
, uri
->values
[0].string
.text
);
3891 * See if we have a job URI or a printer URI...
3894 if (!strcmp(uri
->name
, "printer-uri"))
3897 * Got a printer URI; see if we also have a job-id attribute...
3900 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3901 IPP_TAG_INTEGER
)) == NULL
)
3903 send_ipp_status(con
, IPP_BAD_REQUEST
,
3904 _("Got a printer-uri attribute but no job-id!"));
3908 if ((jobid
= attr
->values
[0].integer
) == 0)
3911 * Find the current job on the specified printer...
3914 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3920 send_ipp_status(con
, IPP_NOT_FOUND
,
3921 _("The printer or class was not found."));
3926 * See if the printer is currently printing a job...
3930 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3934 * No, see if there are any pending jobs...
3937 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3939 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3940 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3941 !strcasecmp(job
->dest
, printer
->name
))
3948 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3950 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3951 if (job
->state_value
== IPP_JOB_STOPPED
&&
3952 !strcasecmp(job
->dest
, printer
->name
))
3959 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3970 * Got a job URI; parse it to get the job ID...
3973 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3974 sizeof(scheme
), username
, sizeof(username
), host
,
3975 sizeof(host
), &port
, resource
, sizeof(resource
));
3977 if (strncmp(resource
, "/jobs/", 6))
3983 send_ipp_status(con
, IPP_BAD_REQUEST
,
3984 _("Bad job-uri attribute \"%s\"!"),
3985 uri
->values
[0].string
.text
);
3989 jobid
= atoi(resource
+ 6);
3993 * Look for the "purge-job" attribute...
3996 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3997 IPP_TAG_BOOLEAN
)) != NULL
)
3998 purge
= attr
->values
[0].boolean
;
4003 * See if the job exists...
4006 if ((job
= cupsdFindJob(jobid
)) == NULL
)
4009 * Nope - return a "not found" error...
4012 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
4017 * See if the job is owned by the requesting user...
4020 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
4022 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
4027 * See if the job is already completed, canceled, or aborted; if so,
4028 * we can't cancel...
4031 if (job
->state_value
>= IPP_JOB_CANCELED
&& !purge
)
4033 switch (job
->state_value
)
4035 case IPP_JOB_CANCELED
:
4036 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4037 _("Job #%d is already canceled - can\'t cancel."),
4041 case IPP_JOB_ABORTED
:
4042 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4043 _("Job #%d is already aborted - can\'t cancel."),
4048 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4049 _("Job #%d is already completed - can\'t cancel."),
4058 * Cancel the job and return...
4061 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
4065 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
4068 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
4071 con
->response
->request
.status
.status_code
= IPP_OK
;
4076 * 'cancel_subscription()' - Cancel a subscription.
4080 cancel_subscription(
4081 cupsd_client_t
*con
, /* I - Client connection */
4082 int sub_id
) /* I - Subscription ID */
4084 http_status_t status
; /* Policy status */
4085 cupsd_subscription_t
*sub
; /* Subscription */
4088 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4089 "cancel_subscription(con=%p[%d], sub_id=%d)",
4090 con
, con
->http
.fd
, sub_id
);
4093 * Is the subscription ID valid?
4096 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
4099 * Bad subscription ID...
4102 send_ipp_status(con
, IPP_NOT_FOUND
,
4103 _("notify-subscription-id %d no good!"), sub_id
);
4111 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
4113 con
, sub
->owner
)) != HTTP_OK
)
4115 send_http_error(con
, status
, sub
->dest
);
4120 * Cancel the subscription...
4123 cupsdDeleteSubscription(sub
, 1);
4125 con
->response
->request
.status
.status_code
= IPP_OK
;
4130 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
4133 static int /* O - 1 if OK, 0 if not */
4134 check_rss_recipient(
4135 const char *recipient
) /* I - Recipient URI */
4137 cupsd_subscription_t
*sub
; /* Current subscription */
4140 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
4142 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
4146 * Compare the URIs up to the first ?...
4149 const char *r1
, *r2
;
4151 for (r1
= recipient
, r2
= sub
->recipient
;
4152 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
4164 * 'check_quotas()' - Check quotas for a printer and user.
4167 static int /* O - 1 if OK, 0 if not */
4168 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
4169 cupsd_printer_t
*p
) /* I - Printer or class */
4171 int i
; /* Looping var */
4172 char username
[33]; /* Username */
4173 cupsd_quota_t
*q
; /* Quota data */
4174 #ifdef HAVE_MBR_UID_TO_UUID
4176 * Use Apple membership APIs which require that all names represent
4177 * valid user account or group records accessible by the server.
4180 uuid_t usr_uuid
; /* UUID for job requesting user */
4181 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
4182 uuid_t grp_uuid
; /* UUID for ACL group name entry */
4183 int mbr_err
; /* Error from membership function */
4184 int is_member
; /* Is this user a member? */
4187 * Use standard POSIX APIs for checking users and groups...
4190 struct passwd
*pw
; /* User password data */
4191 #endif /* HAVE_MBR_UID_TO_UUID */
4194 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
4195 con
, con
->http
.fd
, p
, p
->name
);
4198 * Figure out who is printing...
4201 strlcpy(username
, get_username(con
), sizeof(username
));
4204 * Check global active job limits for printers and users...
4207 if (MaxJobsPerPrinter
)
4210 * Check if there are too many pending jobs on this printer...
4213 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
4215 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
4224 * Check if there are too many pending jobs for this user...
4227 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
4229 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
4236 * Check against users...
4239 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
4244 #ifdef HAVE_MBR_UID_TO_UUID
4246 * Get UUID for job requesting user...
4249 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
4255 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4256 "check_quotas: UUID lookup failed for user \"%s\"",
4258 cupsdLogMessage(CUPSD_LOG_INFO
,
4259 "Denying user \"%s\" access to printer \"%s\" "
4260 "(unknown user)...",
4266 * Get UID and GID of requesting user...
4269 pw
= getpwnam(username
);
4271 #endif /* HAVE_MBR_UID_TO_UUID */
4273 for (i
= 0; i
< p
->num_users
; i
++)
4274 if (p
->users
[i
][0] == '@')
4277 * Check group membership...
4280 #ifdef HAVE_MBR_UID_TO_UUID
4281 if (p
->users
[i
][1] == '#')
4283 if (uuid_parse((char *)p
->users
[i
] + 2, grp_uuid
))
4284 uuid_clear(grp_uuid
);
4286 else if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
4290 * Invalid ACL entries are ignored for matching; just record a
4291 * warning in the log...
4294 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4295 "check_quotas: UUID lookup failed for ACL entry "
4296 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4297 cupsdLogMessage(CUPSD_LOG_WARN
,
4298 "Access control entry \"%s\" not a valid group name; "
4299 "entry ignored", p
->users
[i
]);
4302 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
4306 * At this point, there should be no errors, but check anyways...
4309 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4310 "check_quotas: group \"%s\" membership check "
4311 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
4316 * Stop if we found a match...
4323 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
4325 #endif /* HAVE_MBR_UID_TO_UUID */
4327 #ifdef HAVE_MBR_UID_TO_UUID
4330 if (p
->users
[i
][0] == '#')
4332 if (uuid_parse((char *)p
->users
[i
] + 1, usr2_uuid
))
4333 uuid_clear(usr2_uuid
);
4335 else if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
4339 * Invalid ACL entries are ignored for matching; just record a
4340 * warning in the log...
4343 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4344 "check_quotas: UUID lookup failed for ACL entry "
4345 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4346 cupsdLogMessage(CUPSD_LOG_WARN
,
4347 "Access control entry \"%s\" not a valid user name; "
4348 "entry ignored", p
->users
[i
]);
4351 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
4354 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4355 "check_quotas: User \"%s\" identity check failed "
4356 "(err=%d)", p
->users
[i
], mbr_err
);
4364 else if (!strcasecmp(username
, p
->users
[i
]))
4366 #endif /* HAVE_MBR_UID_TO_UUID */
4368 if ((i
< p
->num_users
) == p
->deny_users
)
4370 cupsdLogMessage(CUPSD_LOG_INFO
,
4371 "Denying user \"%s\" access to printer \"%s\"...",
4382 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
4385 * TODO: Define these special page count values as constants!
4388 if (q
->page_count
== -4) /* special case: unlimited user */
4390 cupsdLogMessage(CUPSD_LOG_INFO
,
4391 "User \"%s\" request approved for printer %s (%s): "
4393 username
, p
->name
, p
->info
);
4394 q
->page_count
= 0; /* allow user to print */
4397 else if (q
->page_count
== -3) /* quota exceeded */
4399 cupsdLogMessage(CUPSD_LOG_INFO
,
4400 "User \"%s\" request denied for printer %s (%s): "
4401 "quota limit exceeded.",
4402 username
, p
->name
, p
->info
);
4403 q
->page_count
= 2; /* force quota exceeded failure */
4406 else if (q
->page_count
== -2) /* quota disabled for user */
4408 cupsdLogMessage(CUPSD_LOG_INFO
,
4409 "User \"%s\" request denied for printer %s (%s): "
4410 "printing disabled for user.",
4411 username
, p
->name
, p
->info
);
4412 q
->page_count
= 2; /* force quota exceeded failure */
4415 else if (q
->page_count
== -1) /* quota access error */
4417 cupsdLogMessage(CUPSD_LOG_INFO
,
4418 "User \"%s\" request denied for printer %s (%s): "
4419 "unable to determine quota limit.",
4420 username
, p
->name
, p
->info
);
4421 q
->page_count
= 2; /* force quota exceeded failure */
4424 else if (q
->page_count
< 0) /* user not found or other error */
4426 cupsdLogMessage(CUPSD_LOG_INFO
,
4427 "User \"%s\" request denied for printer %s (%s): "
4428 "user disabled / missing quota.",
4429 username
, p
->name
, p
->info
);
4430 q
->page_count
= 2; /* force quota exceeded failure */
4433 else /* page within user limits */
4435 q
->page_count
= 0; /* allow user to print */
4440 #endif /* __APPLE__ */
4441 if (p
->k_limit
|| p
->page_limit
)
4443 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
4445 cupsdLogMessage(CUPSD_LOG_ERROR
,
4446 "Unable to allocate quota data for user \"%s\"!",
4451 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
4452 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
4454 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
4461 * If we have gotten this far, we're done!
4469 * 'copy_attribute()' - Copy a single attribute.
4472 static ipp_attribute_t
* /* O - New attribute */
4474 ipp_t
*to
, /* O - Destination request/response */
4475 ipp_attribute_t
*attr
, /* I - Attribute to copy */
4476 int quickcopy
) /* I - Do a quick copy? */
4478 int i
; /* Looping var */
4479 ipp_attribute_t
*toattr
; /* Destination attribute */
4482 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4483 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
4484 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
4487 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
4490 toattr
= ippAddSeparator(to
);
4493 case IPP_TAG_INTEGER
:
4495 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4496 attr
->name
, attr
->num_values
, NULL
);
4498 for (i
= 0; i
< attr
->num_values
; i
++)
4499 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
4502 case IPP_TAG_BOOLEAN
:
4503 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
4504 attr
->num_values
, NULL
);
4506 for (i
= 0; i
< attr
->num_values
; i
++)
4507 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
4510 case IPP_TAG_STRING
:
4513 case IPP_TAG_KEYWORD
:
4515 case IPP_TAG_URISCHEME
:
4516 case IPP_TAG_CHARSET
:
4517 case IPP_TAG_LANGUAGE
:
4518 case IPP_TAG_MIMETYPE
:
4519 toattr
= ippAddStrings(to
, attr
->group_tag
,
4520 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4521 attr
->name
, attr
->num_values
, NULL
, NULL
);
4525 for (i
= 0; i
< attr
->num_values
; i
++)
4526 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4530 for (i
= 0; i
< attr
->num_values
; i
++)
4531 toattr
->values
[i
].string
.text
=
4532 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4537 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
4538 attr
->values
[0].date
);
4541 case IPP_TAG_RESOLUTION
:
4542 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
4543 attr
->num_values
, IPP_RES_PER_INCH
,
4546 for (i
= 0; i
< attr
->num_values
; i
++)
4548 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
4549 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
4550 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
4554 case IPP_TAG_RANGE
:
4555 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
4556 attr
->num_values
, NULL
, NULL
);
4558 for (i
= 0; i
< attr
->num_values
; i
++)
4560 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
4561 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
4565 case IPP_TAG_TEXTLANG
:
4566 case IPP_TAG_NAMELANG
:
4567 toattr
= ippAddStrings(to
, attr
->group_tag
,
4568 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4569 attr
->name
, attr
->num_values
, NULL
, NULL
);
4573 for (i
= 0; i
< attr
->num_values
; i
++)
4575 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
4576 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4581 for (i
= 0; i
< attr
->num_values
; i
++)
4584 toattr
->values
[i
].string
.charset
=
4585 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
4587 toattr
->values
[i
].string
.charset
=
4588 toattr
->values
[0].string
.charset
;
4590 toattr
->values
[i
].string
.text
=
4591 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4596 case IPP_TAG_BEGIN_COLLECTION
:
4597 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
4598 attr
->num_values
, NULL
);
4600 for (i
= 0; i
< attr
->num_values
; i
++)
4602 toattr
->values
[i
].collection
= ippNew();
4603 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
4604 NULL
, IPP_TAG_ZERO
, 0);
4609 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4610 attr
->name
, attr
->num_values
, NULL
);
4612 for (i
= 0; i
< attr
->num_values
; i
++)
4614 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
4616 if (toattr
->values
[i
].unknown
.length
> 0)
4618 if ((toattr
->values
[i
].unknown
.data
=
4619 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
4620 toattr
->values
[i
].unknown
.length
= 0;
4622 memcpy(toattr
->values
[i
].unknown
.data
,
4623 attr
->values
[i
].unknown
.data
,
4624 toattr
->values
[i
].unknown
.length
);
4627 break; /* anti-compiler-warning-code */
4635 * 'copy_attrs()' - Copy attributes from one request to another.
4639 copy_attrs(ipp_t
*to
, /* I - Destination request */
4640 ipp_t
*from
, /* I - Source request */
4641 cups_array_t
*ra
, /* I - Requested attributes */
4642 ipp_tag_t group
, /* I - Group to copy */
4643 int quickcopy
) /* I - Do a quick copy? */
4645 ipp_attribute_t
*fromattr
; /* Source attribute */
4648 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4649 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4650 to
, from
, ra
, group
, quickcopy
);
4655 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4658 * Filter attributes as needed...
4661 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4662 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4665 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4666 copy_attribute(to
, fromattr
, quickcopy
);
4672 * 'copy_banner()' - Copy a banner file to the requests directory for the
4676 static int /* O - Size of banner file in kbytes */
4677 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4678 cupsd_job_t
*job
, /* I - Job information */
4679 const char *name
) /* I - Name of banner */
4681 int i
; /* Looping var */
4682 int kbytes
; /* Size of banner file in kbytes */
4683 char filename
[1024]; /* Job filename */
4684 cupsd_banner_t
*banner
; /* Pointer to banner */
4685 cups_file_t
*in
; /* Input file */
4686 cups_file_t
*out
; /* Output file */
4687 int ch
; /* Character from file */
4688 char attrname
[255], /* Name of attribute */
4689 *s
; /* Pointer into name */
4690 ipp_attribute_t
*attr
; /* Attribute */
4693 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4694 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4695 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
4696 name
? name
: "(null)");
4699 * Find the banner; return if not found or "none"...
4702 if (!name
|| !strcmp(name
, "none") ||
4703 (banner
= cupsdFindBanner(name
)) == NULL
)
4707 * Open the banner and job files...
4710 if (add_file(con
, job
, banner
->filetype
, 0))
4713 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4715 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4717 cupsdLogMessage(CUPSD_LOG_ERROR
,
4718 "Unable to create banner job file %s - %s",
4719 filename
, strerror(errno
));
4724 fchmod(cupsFileNumber(out
), 0640);
4725 fchown(cupsFileNumber(out
), RunUser
, Group
);
4728 * Try the localized banner file under the subdirectory...
4731 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4733 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4736 * Convert ll-cc to ll_CC...
4740 attrname
[3] = toupper(attrname
[3] & 255);
4741 attrname
[4] = toupper(attrname
[4] & 255);
4744 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4747 if (access(filename
, 0) && strlen(attrname
) > 2)
4750 * Wasn't able to find "ll_CC" locale file; try the non-national
4751 * localization banner directory.
4756 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4760 if (access(filename
, 0))
4763 * Use the non-localized banner file.
4766 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4769 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4773 cupsdLogMessage(CUPSD_LOG_ERROR
,
4774 "Unable to open banner template file %s - %s",
4775 filename
, strerror(errno
));
4781 * Parse the file to the end...
4784 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4788 * Get an attribute name...
4791 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4792 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4794 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4804 * Ignore { followed by stuff that is not an attribute name...
4807 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4812 * See if it is defined...
4815 if (attrname
[0] == '?')
4820 if (!strcmp(s
, "printer-name"))
4822 cupsFilePuts(out
, job
->dest
);
4825 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4828 * See if we have a leading question mark...
4831 if (attrname
[0] != '?')
4834 * Nope, write to file as-is; probably a PostScript procedure...
4837 cupsFilePrintf(out
, "{%s}", attrname
);
4844 * Output value(s)...
4847 for (i
= 0; i
< attr
->num_values
; i
++)
4850 cupsFilePutChar(out
, ',');
4852 switch (attr
->value_tag
)
4854 case IPP_TAG_INTEGER
:
4856 if (!strncmp(s
, "time-at-", 8))
4857 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
4859 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4862 case IPP_TAG_BOOLEAN
:
4863 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4866 case IPP_TAG_NOVALUE
:
4867 cupsFilePuts(out
, "novalue");
4870 case IPP_TAG_RANGE
:
4871 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4872 attr
->values
[i
].range
.upper
);
4875 case IPP_TAG_RESOLUTION
:
4876 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4877 attr
->values
[i
].resolution
.yres
,
4878 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4883 case IPP_TAG_STRING
:
4886 case IPP_TAG_KEYWORD
:
4887 case IPP_TAG_CHARSET
:
4888 case IPP_TAG_LANGUAGE
:
4889 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4892 * Need to quote strings for PS banners...
4897 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4899 if (*p
== '(' || *p
== ')' || *p
== '\\')
4901 cupsFilePutChar(out
, '\\');
4902 cupsFilePutChar(out
, *p
);
4904 else if (*p
< 32 || *p
> 126)
4905 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4907 cupsFilePutChar(out
, *p
);
4911 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4915 break; /* anti-compiler-warning-code */
4919 else if (ch
== '\\') /* Quoted char */
4921 ch
= cupsFileGetChar(in
);
4923 if (ch
!= '{') /* Only do special handling for \{ */
4924 cupsFilePutChar(out
, '\\');
4926 cupsFilePutChar(out
, ch
);
4929 cupsFilePutChar(out
, ch
);
4933 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4935 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4936 IPP_TAG_INTEGER
)) != NULL
)
4937 attr
->values
[0].integer
+= kbytes
;
4946 * 'copy_file()' - Copy a PPD file or interface script...
4949 static int /* O - 0 = success, -1 = error */
4950 copy_file(const char *from
, /* I - Source file */
4951 const char *to
) /* I - Destination file */
4953 cups_file_t
*src
, /* Source file */
4954 *dst
; /* Destination file */
4955 int bytes
; /* Bytes to read/write */
4956 char buffer
[2048]; /* Copy buffer */
4959 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4962 * Open the source and destination file for a copy...
4965 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4968 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4975 * Copy the source file to the destination...
4978 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4979 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
4987 * Close both files and return...
4992 return (cupsFileClose(dst
));
4997 * 'copy_model()' - Copy a PPD model file, substituting default values
5001 static int /* O - 0 = success, -1 = error */
5002 copy_model(cupsd_client_t
*con
, /* I - Client connection */
5003 const char *from
, /* I - Source file */
5004 const char *to
) /* I - Destination file */
5006 fd_set input
; /* select() input set */
5007 struct timeval timeout
; /* select() timeout */
5008 int maxfd
; /* Max file descriptor for select() */
5009 char tempfile
[1024]; /* Temporary PPD file */
5010 int tempfd
; /* Temporary PPD file descriptor */
5011 int temppid
; /* Process ID of cups-driverd */
5012 int temppipe
[2]; /* Temporary pipes */
5013 char *argv
[4], /* Command-line arguments */
5014 *envp
[MAX_ENV
]; /* Environment */
5015 cups_file_t
*src
, /* Source file */
5016 *dst
; /* Destination file */
5017 ppd_file_t
*ppd
; /* PPD file */
5018 int bytes
, /* Bytes from pipe */
5019 total
; /* Total bytes from pipe */
5020 char buffer
[2048]; /* Copy buffer */
5021 int i
; /* Looping var */
5022 char option
[PPD_MAX_NAME
], /* Option name */
5023 choice
[PPD_MAX_NAME
]; /* Choice name */
5024 int num_defaults
; /* Number of default options */
5025 cups_option_t
*defaults
; /* Default options */
5026 char cups_protocol
[PPD_MAX_LINE
];
5027 /* cupsProtocol attribute */
5030 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5031 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
5035 * Run cups-driverd to get the PPD file...
5038 argv
[0] = "cups-driverd";
5040 argv
[2] = (char *)from
;
5043 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5045 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
5046 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
5047 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
5051 cupsdOpenPipe(temppipe
);
5053 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5054 "copy_model: Running \"cups-driverd cat %s\"...", from
);
5056 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
5057 -1, -1, 0, DefaultProfile
, &temppid
))
5068 * Wait up to 30 seconds for the PPD file to be copied...
5073 if (temppipe
[0] > CGIPipes
[0])
5074 maxfd
= temppipe
[0] + 1;
5076 maxfd
= CGIPipes
[0] + 1;
5081 * See if we have data ready...
5085 FD_SET(temppipe
[0], &input
);
5086 FD_SET(CGIPipes
[0], &input
);
5088 timeout
.tv_sec
= 30;
5089 timeout
.tv_usec
= 0;
5091 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
5101 * We have timed out...
5107 if (FD_ISSET(temppipe
[0], &input
))
5110 * Read the PPD file from the pipe, and write it to the PPD file.
5113 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
5115 if (write(tempfd
, buffer
, bytes
) < bytes
)
5124 if (FD_ISSET(CGIPipes
[0], &input
))
5134 * No data from cups-deviced...
5137 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
5143 * Read the source file and see what page sizes are supported...
5146 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
5153 * Open the destination (if possible) and set the default options...
5158 cups_protocol
[0] = '\0';
5160 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
5163 * Read all of the default lines from the old PPD...
5166 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
5167 if (!strncmp(buffer
, "*Default", 8))
5170 * Add the default option...
5173 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5174 choice
, sizeof(choice
)))
5176 ppd_option_t
*ppdo
; /* PPD option */
5180 * Only add the default if the default hasn't already been
5181 * set and the choice exists in the new PPD...
5184 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
5185 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
5186 ppdFindChoice(ppdo
, choice
))
5187 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
5191 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
5192 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
5196 else if (ppdPageSize(ppd
, DefaultPaperSize
))
5199 * Add the default media sizes...
5202 num_defaults
= cupsAddOption("PageSize", DefaultPaperSize
,
5203 num_defaults
, &defaults
);
5204 num_defaults
= cupsAddOption("PageRegion", DefaultPaperSize
,
5205 num_defaults
, &defaults
);
5206 num_defaults
= cupsAddOption("PaperDimension", DefaultPaperSize
,
5207 num_defaults
, &defaults
);
5208 num_defaults
= cupsAddOption("ImageableArea", DefaultPaperSize
,
5209 num_defaults
, &defaults
);
5215 * Open the source file for a copy...
5218 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
5220 cupsFreeOptions(num_defaults
, defaults
);
5226 * Open the destination file for a copy...
5229 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
5231 cupsFreeOptions(num_defaults
, defaults
);
5238 * Copy the source file to the destination...
5241 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
5243 if (!strncmp(buffer
, "*Default", 8))
5246 * Check for an previous default option choice...
5249 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5250 choice
, sizeof(choice
)))
5252 const char *val
; /* Default option value */
5255 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
5258 * Substitute the previous choice...
5261 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
5266 cupsFilePrintf(dst
, "%s\n", buffer
);
5269 if (cups_protocol
[0])
5270 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
5272 cupsFreeOptions(num_defaults
, defaults
);
5275 * Close both files and return...
5282 return (cupsFileClose(dst
));
5287 * 'copy_job_attrs()' - Copy job attributes.
5291 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5292 cupsd_job_t
*job
, /* I - Job */
5293 cups_array_t
*ra
) /* I - Requested attributes array */
5295 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
5299 * Send the requested attributes for each job...
5302 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
5303 con
->servername
, con
->serverport
, "/jobs/%d",
5306 if (!ra
|| cupsArrayFind(ra
, "document-count"))
5307 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5308 "document-count", job
->num_files
);
5310 if (!ra
|| cupsArrayFind(ra
, "job-media-progress"))
5311 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5312 "job-media-progress", job
->progress
);
5314 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
5315 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5316 "job-more-info", NULL
, job_uri
);
5318 if (job
->state_value
> IPP_JOB_PROCESSING
&&
5319 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
5320 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
5321 job
->num_files
> 0);
5323 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
5324 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5325 "job-printer-up-time", time(NULL
));
5327 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
5328 add_job_state_reasons(con
, job
);
5330 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
5331 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5332 "job-uri", NULL
, job_uri
);
5334 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
5339 * 'copy_printer_attrs()' - Copy printer attributes.
5344 cupsd_client_t
*con
, /* I - Client connection */
5345 cupsd_printer_t
*printer
, /* I - Printer */
5346 cups_array_t
*ra
) /* I - Requested attributes array */
5348 char printer_uri
[HTTP_MAX_URI
];
5350 time_t curtime
; /* Current time */
5351 int i
; /* Looping var */
5352 ipp_attribute_t
*history
; /* History collection */
5356 * Copy the printer attributes to the response using requested-attributes
5357 * and document-format attributes that may be provided by the client.
5360 curtime
= time(NULL
);
5363 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
5364 printer
->recoverable
)
5365 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5366 "com.apple.print.recoverable-message", NULL
,
5367 printer
->recoverable
);
5368 #endif /* __APPLE__ */
5370 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
5371 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5372 "marker-change-time", printer
->marker_time
);
5374 if (printer
->num_printers
> 0 &&
5375 (!ra
|| cupsArrayFind(ra
, "member-uris")))
5377 ipp_attribute_t
*member_uris
; /* member-uris attribute */
5378 cupsd_printer_t
*p2
; /* Printer in class */
5379 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
5382 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
5383 IPP_TAG_URI
, "member-uris",
5384 printer
->num_printers
, NULL
,
5387 for (i
= 0; i
< printer
->num_printers
; i
++)
5389 p2
= printer
->printers
[i
];
5391 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
5392 IPP_TAG_URI
)) != NULL
)
5393 member_uris
->values
[i
].string
.text
=
5394 _cupsStrAlloc(p2_uri
->values
[0].string
.text
);
5397 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
5398 sizeof(printer_uri
), "ipp", NULL
, con
->servername
,
5400 (p2
->type
& CUPS_PRINTER_CLASS
) ?
5401 "/classes/%s" : "/printers/%s", p2
->name
);
5402 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
5408 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
5409 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
5410 "printer-alert", NULL
, printer
->alert
);
5412 if (printer
->alert_description
&&
5413 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
5414 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5415 "printer-alert-description", NULL
,
5416 printer
->alert_description
);
5418 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
5419 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
5420 ippTimeToDate(curtime
));
5423 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
5425 if (printer
->reg_name
)
5426 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5427 "printer-dns-sd-name", NULL
, printer
->reg_name
);
5429 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
5430 "printer-dns-sd-name", 0);
5432 #endif /* HAVE_DNSSD */
5434 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
5435 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5436 "printer-error-policy", NULL
, printer
->error_policy
);
5438 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
5439 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
5440 printer
->accepting
);
5442 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
5443 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
5446 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
5447 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5448 "printer-op-policy", NULL
, printer
->op_policy
);
5450 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
5451 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
5454 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
5455 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5456 "printer-state-change-time", printer
->state_time
);
5458 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
5459 cupsArrayFind(ra
, "printer-state-history"))
5462 * Printer history is only sent if specifically requested, so that
5463 * older CUPS/IPP clients won't barf on the collection attributes.
5466 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
5467 "printer-state-history",
5468 printer
->num_history
, NULL
);
5470 for (i
= 0; i
< printer
->num_history
; i
++)
5471 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
5472 NULL
, IPP_TAG_ZERO
, 0);
5475 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
5476 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5477 "printer-state-message", NULL
, printer
->state_message
);
5479 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
5480 add_printer_state_reasons(con
, printer
);
5482 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5484 int type
; /* printer-type value */
5488 * Add the CUPS-specific printer-type attribute...
5491 type
= printer
->type
;
5493 if (printer
== DefaultPrinter
)
5494 type
|= CUPS_PRINTER_DEFAULT
;
5496 if (!printer
->accepting
)
5497 type
|= CUPS_PRINTER_REJECTING
;
5499 if (!printer
->shared
)
5500 type
|= CUPS_PRINTER_NOT_SHARED
;
5502 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
5506 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5507 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5508 "printer-up-time", curtime
);
5510 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
5511 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
5514 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5515 "ipp", NULL
, con
->servername
, con
->serverport
,
5516 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5517 "/classes/%s" : "/printers/%s", printer
->name
);
5518 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5519 "printer-uri-supported", NULL
, printer_uri
);
5520 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5524 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5525 add_queued_job_count(con
, printer
);
5527 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
5528 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
5533 * 'copy_subscription_attrs()' - Copy subscription attributes.
5537 copy_subscription_attrs(
5538 cupsd_client_t
*con
, /* I - Client connection */
5539 cupsd_subscription_t
*sub
, /* I - Subscription */
5540 cups_array_t
*ra
) /* I - Requested attributes array */
5542 ipp_attribute_t
*attr
; /* Current attribute */
5543 char printer_uri
[HTTP_MAX_URI
];
5545 int count
; /* Number of events */
5546 unsigned mask
; /* Current event mask */
5547 const char *name
; /* Current event name */
5551 * Copy the subscription attributes to the response using the
5552 * requested-attributes attribute that may be provided by the client.
5555 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
5557 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5560 * Simple event list...
5563 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5564 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5565 "notify-events", NULL
, name
);
5570 * Complex event list...
5573 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5574 if (sub
->mask
& mask
)
5577 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5578 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5579 "notify-events", count
, NULL
, NULL
);
5581 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5582 if (sub
->mask
& mask
)
5584 attr
->values
[count
].string
.text
=
5585 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5592 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5593 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5594 "notify-job-id", sub
->job
->id
);
5596 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
5597 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5598 "notify-lease-duration", sub
->lease
);
5600 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5602 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5603 "ipp", NULL
, con
->servername
, con
->serverport
,
5604 "/printers/%s", sub
->dest
->name
);
5605 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5606 "notify-printer-uri", NULL
, printer_uri
);
5609 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
5610 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5611 "notify-recipient-uri", NULL
, sub
->recipient
);
5612 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
5613 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5614 "notify-pull-method", NULL
, "ippget");
5616 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
5617 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5618 "notify-subscriber-user-name", NULL
, sub
->owner
);
5620 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5621 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5622 "notify-subscription-id", sub
->id
);
5624 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
5625 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5626 "notify-time-interval", sub
->interval
);
5628 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5629 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5630 sub
->user_data
, sub
->user_data_len
);
5635 * 'create_job()' - Print a file to a printer or class.
5639 create_job(cupsd_client_t
*con
, /* I - Client connection */
5640 ipp_attribute_t
*uri
) /* I - Printer URI */
5642 cupsd_printer_t
*printer
; /* Printer */
5643 cupsd_job_t
*job
; /* New job */
5646 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5647 con
->http
.fd
, uri
->values
[0].string
.text
);
5650 * Is the destination valid?
5653 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5659 send_ipp_status(con
, IPP_NOT_FOUND
,
5660 _("The printer or class was not found."));
5665 * Create the job object...
5668 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5671 job
->pending_timeout
= 1;
5674 * Save and log the job...
5677 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5678 job
->dest
, job
->username
);
5683 * 'create_requested_array()' - Create an array for the requested-attributes.
5686 static cups_array_t
* /* O - Array of attributes or NULL */
5687 create_requested_array(ipp_t
*request
) /* I - IPP request */
5689 int i
; /* Looping var */
5690 ipp_attribute_t
*requested
; /* requested-attributes attribute */
5691 cups_array_t
*ra
; /* Requested attributes array */
5692 char *value
; /* Current value */
5696 * Get the requested-attributes attribute, and return NULL if we don't
5700 if ((requested
= ippFindAttribute(request
, "requested-attributes",
5701 IPP_TAG_KEYWORD
)) == NULL
)
5705 * If the attribute contains a single "all" keyword, return NULL...
5708 if (requested
->num_values
== 1 &&
5709 !strcmp(requested
->values
[0].string
.text
, "all"))
5713 * Create an array using "strcmp" as the comparison function...
5716 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
5718 for (i
= 0; i
< requested
->num_values
; i
++)
5720 value
= requested
->values
[i
].string
.text
;
5722 if (!strcmp(value
, "job-template"))
5724 cupsArrayAdd(ra
, "copies");
5725 cupsArrayAdd(ra
, "copies-default");
5726 cupsArrayAdd(ra
, "copies-supported");
5727 cupsArrayAdd(ra
, "finishings");
5728 cupsArrayAdd(ra
, "finishings-default");
5729 cupsArrayAdd(ra
, "finishings-supported");
5730 cupsArrayAdd(ra
, "job-hold-until");
5731 cupsArrayAdd(ra
, "job-hold-until-default");
5732 cupsArrayAdd(ra
, "job-hold-until-supported");
5733 cupsArrayAdd(ra
, "job-priority");
5734 cupsArrayAdd(ra
, "job-priority-default");
5735 cupsArrayAdd(ra
, "job-priority-supported");
5736 cupsArrayAdd(ra
, "job-sheets");
5737 cupsArrayAdd(ra
, "job-sheets-default");
5738 cupsArrayAdd(ra
, "job-sheets-supported");
5739 cupsArrayAdd(ra
, "media");
5740 cupsArrayAdd(ra
, "media-default");
5741 cupsArrayAdd(ra
, "media-supported");
5742 cupsArrayAdd(ra
, "multiple-document-handling");
5743 cupsArrayAdd(ra
, "multiple-document-handling-default");
5744 cupsArrayAdd(ra
, "multiple-document-handling-supported");
5745 cupsArrayAdd(ra
, "number-up");
5746 cupsArrayAdd(ra
, "number-up-default");
5747 cupsArrayAdd(ra
, "number-up-supported");
5748 cupsArrayAdd(ra
, "orientation-requested");
5749 cupsArrayAdd(ra
, "orientation-requested-default");
5750 cupsArrayAdd(ra
, "orientation-requested-supported");
5751 cupsArrayAdd(ra
, "page-ranges");
5752 cupsArrayAdd(ra
, "page-ranges-supported");
5753 cupsArrayAdd(ra
, "printer-resolution");
5754 cupsArrayAdd(ra
, "printer-resolution-default");
5755 cupsArrayAdd(ra
, "printer-resolution-supported");
5756 cupsArrayAdd(ra
, "print-quality");
5757 cupsArrayAdd(ra
, "print-quality-default");
5758 cupsArrayAdd(ra
, "print-quality-supported");
5759 cupsArrayAdd(ra
, "sides");
5760 cupsArrayAdd(ra
, "sides-default");
5761 cupsArrayAdd(ra
, "sides-supported");
5763 else if (!strcmp(value
, "job-description"))
5765 cupsArrayAdd(ra
, "date-time-at-completed");
5766 cupsArrayAdd(ra
, "date-time-at-creation");
5767 cupsArrayAdd(ra
, "date-time-at-processing");
5768 cupsArrayAdd(ra
, "job-detailed-status-message");
5769 cupsArrayAdd(ra
, "job-document-access-errors");
5770 cupsArrayAdd(ra
, "job-id");
5771 cupsArrayAdd(ra
, "job-impressions");
5772 cupsArrayAdd(ra
, "job-impressions-completed");
5773 cupsArrayAdd(ra
, "job-k-octets");
5774 cupsArrayAdd(ra
, "job-k-octets-processed");
5775 cupsArrayAdd(ra
, "job-media-progress");
5776 cupsArrayAdd(ra
, "job-media-sheets");
5777 cupsArrayAdd(ra
, "job-media-sheets-completed");
5778 cupsArrayAdd(ra
, "job-message-from-operator");
5779 cupsArrayAdd(ra
, "job-more-info");
5780 cupsArrayAdd(ra
, "job-name");
5781 cupsArrayAdd(ra
, "job-originating-user-name");
5782 cupsArrayAdd(ra
, "job-printer-up-time");
5783 cupsArrayAdd(ra
, "job-printer-uri");
5784 cupsArrayAdd(ra
, "job-state");
5785 cupsArrayAdd(ra
, "job-state-message");
5786 cupsArrayAdd(ra
, "job-state-reasons");
5787 cupsArrayAdd(ra
, "job-uri");
5788 cupsArrayAdd(ra
, "number-of-documents");
5789 cupsArrayAdd(ra
, "number-of-intervening-jobs");
5790 cupsArrayAdd(ra
, "output-device-assigned");
5791 cupsArrayAdd(ra
, "time-at-completed");
5792 cupsArrayAdd(ra
, "time-at-creation");
5793 cupsArrayAdd(ra
, "time-at-processing");
5795 else if (!strcmp(value
, "printer-description"))
5797 cupsArrayAdd(ra
, "charset-configured");
5798 cupsArrayAdd(ra
, "charset-supported");
5799 cupsArrayAdd(ra
, "color-supported");
5800 cupsArrayAdd(ra
, "compression-supported");
5801 cupsArrayAdd(ra
, "document-format-default");
5802 cupsArrayAdd(ra
, "document-format-supported");
5803 cupsArrayAdd(ra
, "generated-natural-language-supported");
5804 cupsArrayAdd(ra
, "ipp-versions-supported");
5805 cupsArrayAdd(ra
, "job-impressions-supported");
5806 cupsArrayAdd(ra
, "job-k-octets-supported");
5807 cupsArrayAdd(ra
, "job-media-sheets-supported");
5808 cupsArrayAdd(ra
, "job-settable-attributes-supported");
5809 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
5810 cupsArrayAdd(ra
, "multiple-operation-time-out");
5811 cupsArrayAdd(ra
, "natural-language-configured");
5812 cupsArrayAdd(ra
, "notify-attributes-supported");
5813 cupsArrayAdd(ra
, "notify-lease-duration-default");
5814 cupsArrayAdd(ra
, "notify-lease-duration-supported");
5815 cupsArrayAdd(ra
, "notify-max-events-supported");
5816 cupsArrayAdd(ra
, "notify-events-default");
5817 cupsArrayAdd(ra
, "notify-events-supported");
5818 cupsArrayAdd(ra
, "notify-pull-method-supported");
5819 cupsArrayAdd(ra
, "notify-schemes-supported");
5820 cupsArrayAdd(ra
, "operations-supported");
5821 cupsArrayAdd(ra
, "pages-per-minute");
5822 cupsArrayAdd(ra
, "pages-per-minute-color");
5823 cupsArrayAdd(ra
, "pdl-override-supported");
5824 cupsArrayAdd(ra
, "printer-alert");
5825 cupsArrayAdd(ra
, "printer-alert-description");
5826 cupsArrayAdd(ra
, "printer-commands");
5827 cupsArrayAdd(ra
, "printer-current-time");
5828 cupsArrayAdd(ra
, "printer-driver-installer");
5829 cupsArrayAdd(ra
, "printer-dns-sd-name");
5830 cupsArrayAdd(ra
, "printer-info");
5831 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
5832 cupsArrayAdd(ra
, "printer-location");
5833 cupsArrayAdd(ra
, "printer-make-and-model");
5834 cupsArrayAdd(ra
, "printer-message-from-operator");
5835 cupsArrayAdd(ra
, "printer-more-info");
5836 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
5837 cupsArrayAdd(ra
, "printer-name");
5838 cupsArrayAdd(ra
, "printer-state");
5839 cupsArrayAdd(ra
, "printer-state-message");
5840 cupsArrayAdd(ra
, "printer-state-reasons");
5841 cupsArrayAdd(ra
, "printer-settable-attributes-supported");
5842 cupsArrayAdd(ra
, "printer-type");
5843 cupsArrayAdd(ra
, "printer-up-time");
5844 cupsArrayAdd(ra
, "printer-uri-supported");
5845 cupsArrayAdd(ra
, "queued-job-count");
5846 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
5847 cupsArrayAdd(ra
, "uri-authentication-supported");
5848 cupsArrayAdd(ra
, "uri-security-supported");
5850 else if (!strcmp(value
, "printer-defaults"))
5852 char *name
; /* Option name */
5855 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5857 name
= (char *)cupsArrayNext(CommonDefaults
))
5858 cupsArrayAdd(ra
, name
);
5860 else if (!strcmp(value
, "subscription-template"))
5862 cupsArrayAdd(ra
, "notify-attributes");
5863 cupsArrayAdd(ra
, "notify-charset");
5864 cupsArrayAdd(ra
, "notify-events");
5865 cupsArrayAdd(ra
, "notify-lease-duration");
5866 cupsArrayAdd(ra
, "notify-natural-language");
5867 cupsArrayAdd(ra
, "notify-pull-method");
5868 cupsArrayAdd(ra
, "notify-recipient-uri");
5869 cupsArrayAdd(ra
, "notify-time-interval");
5870 cupsArrayAdd(ra
, "notify-user-data");
5873 cupsArrayAdd(ra
, value
);
5881 * 'create_subscription()' - Create a notification subscription.
5885 create_subscription(
5886 cupsd_client_t
*con
, /* I - Client connection */
5887 ipp_attribute_t
*uri
) /* I - Printer URI */
5889 http_status_t status
; /* Policy status */
5890 int i
; /* Looping var */
5891 ipp_attribute_t
*attr
; /* Current attribute */
5892 cups_ptype_t dtype
; /* Destination type (printer/class) */
5893 char scheme
[HTTP_MAX_URI
],
5894 /* Scheme portion of URI */
5895 userpass
[HTTP_MAX_URI
],
5896 /* Username portion of URI */
5898 /* Host portion of URI */
5899 resource
[HTTP_MAX_URI
];
5900 /* Resource portion of URI */
5901 int port
; /* Port portion of URI */
5902 cupsd_printer_t
*printer
; /* Printer/class */
5903 cupsd_job_t
*job
; /* Job */
5904 int jobid
; /* Job ID */
5905 cupsd_subscription_t
*sub
; /* Subscription object */
5906 const char *username
, /* requesting-user-name or
5907 authenticated username */
5908 *recipient
, /* notify-recipient-uri */
5909 *pullmethod
; /* notify-pull-method */
5910 ipp_attribute_t
*user_data
; /* notify-user-data */
5911 int interval
, /* notify-time-interval */
5912 lease
; /* notify-lease-duration */
5913 unsigned mask
; /* notify-events */
5914 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5915 *notify_lease
; /* notify-lease-duration(-default) */
5919 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5921 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5922 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5923 attr
->value_tag
, attr
->name
);
5925 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5930 * Is the destination valid?
5933 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5934 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5935 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5937 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5938 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5939 sizeof(host
), &port
, resource
, sizeof(resource
));
5941 if (!strcmp(resource
, "/"))
5943 dtype
= (cups_ptype_t
)0;
5946 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5948 dtype
= (cups_ptype_t
)0;
5951 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5953 dtype
= CUPS_PRINTER_CLASS
;
5956 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5962 send_ipp_status(con
, IPP_NOT_FOUND
,
5963 _("The printer or class was not found."));
5973 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5976 send_http_error(con
, status
, printer
);
5980 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5982 send_http_error(con
, status
, NULL
);
5987 * Get the user that is requesting the subscription...
5990 username
= get_username(con
);
5993 * Find the first subscription group attribute; return if we have
5997 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5998 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
6003 send_ipp_status(con
, IPP_BAD_REQUEST
,
6004 _("No subscription attributes in request!"));
6009 * Process the subscription attributes in the request...
6012 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
6020 lease
= DefaultLeaseDuration
;
6022 mask
= CUPSD_EVENT_NONE
;
6026 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
6028 notify_lease
= ippFindAttribute(printer
->attrs
,
6029 "notify-lease-duration-default",
6033 lease
= notify_lease
->values
[0].integer
;
6037 notify_events
= NULL
;
6038 notify_lease
= NULL
;
6041 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
6043 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
6044 attr
->value_tag
== IPP_TAG_URI
)
6047 * Validate the recipient scheme against the ServerBin/notifier
6051 char notifier
[1024]; /* Notifier filename */
6054 recipient
= attr
->values
[0].string
.text
;
6056 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
6057 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
6058 host
, sizeof(host
), &port
,
6059 resource
, sizeof(resource
)) < HTTP_URI_OK
)
6061 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6062 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
6063 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6064 "notify-status-code", IPP_URI_SCHEME
);
6068 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
6070 if (access(notifier
, X_OK
))
6072 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6073 _("notify-recipient-uri URI \"%s\" uses unknown "
6074 "scheme!"), recipient
);
6075 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6076 "notify-status-code", IPP_URI_SCHEME
);
6080 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
6082 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6083 _("notify-recipient-uri URI \"%s\" is already used!"),
6085 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6086 "notify-status-code", IPP_ATTRIBUTES
);
6090 else if (!strcmp(attr
->name
, "notify-pull-method") &&
6091 attr
->value_tag
== IPP_TAG_KEYWORD
)
6093 pullmethod
= attr
->values
[0].string
.text
;
6095 if (strcmp(pullmethod
, "ippget"))
6097 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6098 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
6099 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6100 "notify-status-code", IPP_ATTRIBUTES
);
6104 else if (!strcmp(attr
->name
, "notify-charset") &&
6105 attr
->value_tag
== IPP_TAG_CHARSET
&&
6106 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
6107 strcmp(attr
->values
[0].string
.text
, "utf-8"))
6109 send_ipp_status(con
, IPP_CHARSET
,
6110 _("Character set \"%s\" not supported!"),
6111 attr
->values
[0].string
.text
);
6114 else if (!strcmp(attr
->name
, "notify-natural-language") &&
6115 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
6116 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
6118 send_ipp_status(con
, IPP_CHARSET
,
6119 _("Language \"%s\" not supported!"),
6120 attr
->values
[0].string
.text
);
6123 else if (!strcmp(attr
->name
, "notify-user-data") &&
6124 attr
->value_tag
== IPP_TAG_STRING
)
6126 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
6128 send_ipp_status(con
, IPP_REQUEST_VALUE
,
6129 _("The notify-user-data value is too large "
6130 "(%d > 63 octets)!"),
6131 attr
->values
[0].unknown
.length
);
6137 else if (!strcmp(attr
->name
, "notify-events") &&
6138 attr
->value_tag
== IPP_TAG_KEYWORD
)
6139 notify_events
= attr
;
6140 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
6141 attr
->value_tag
== IPP_TAG_INTEGER
)
6142 lease
= attr
->values
[0].integer
;
6143 else if (!strcmp(attr
->name
, "notify-time-interval") &&
6144 attr
->value_tag
== IPP_TAG_INTEGER
)
6145 interval
= attr
->values
[0].integer
;
6146 else if (!strcmp(attr
->name
, "notify-job-id") &&
6147 attr
->value_tag
== IPP_TAG_INTEGER
)
6148 jobid
= attr
->values
[0].integer
;
6155 for (i
= 0; i
< notify_events
->num_values
; i
++)
6156 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
6160 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
6162 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
6163 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
6164 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
6166 if (!recipient
&& !pullmethod
)
6169 if (mask
== CUPSD_EVENT_NONE
)
6172 mask
= CUPSD_EVENT_JOB_COMPLETED
;
6174 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
6177 send_ipp_status(con
, IPP_BAD_REQUEST
,
6178 _("notify-events not specified!"));
6183 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
6185 cupsdLogMessage(CUPSD_LOG_INFO
,
6186 "create_subscription: Limiting notify-lease-duration to "
6189 lease
= MaxLeaseDuration
;
6194 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6196 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
6203 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
6206 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
6209 cupsdLogMessage(CUPSD_LOG_DEBUG
,
6210 "Added subscription %d for printer \"%s\"",
6211 sub
->id
, printer
->name
);
6213 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
6216 sub
->interval
= interval
;
6218 sub
->expire
= lease
? time(NULL
) + lease
: 0;
6220 cupsdSetString(&sub
->owner
, username
);
6224 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
6225 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
6226 sub
->user_data_len
);
6229 ippAddSeparator(con
->response
);
6230 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
6231 "notify-subscription-id", sub
->id
);
6233 con
->response
->request
.status
.status_code
= IPP_OK
;
6239 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
6244 * 'delete_printer()' - Remove a printer or class from the system.
6248 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
6249 ipp_attribute_t
*uri
) /* I - URI of printer or class */
6251 http_status_t status
; /* Policy status */
6252 cups_ptype_t dtype
; /* Destination type (printer/class) */
6253 cupsd_printer_t
*printer
; /* Printer/class */
6254 char filename
[1024]; /* Script/PPD filename */
6257 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
6258 con
->http
.fd
, uri
->values
[0].string
.text
);
6261 * Do we have a valid URI?
6264 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6270 send_ipp_status(con
, IPP_NOT_FOUND
,
6271 _("The printer or class was not found."));
6279 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6281 send_http_error(con
, status
, NULL
);
6286 * Remove old jobs...
6289 cupsdCancelJobs(printer
->name
, NULL
, 1);
6292 * Remove old subscriptions and send a "deleted printer" event...
6295 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
6296 "%s \"%s\" deleted by \"%s\".",
6297 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
6298 printer
->name
, get_username(con
));
6300 cupsdExpireSubscriptions(printer
, NULL
);
6303 * Remove any old PPD or script files...
6306 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
6310 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6316 * Unregister color profiles...
6319 apple_unregister_profiles(printer
);
6320 #endif /* __APPLE__ */
6322 if (dtype
& CUPS_PRINTER_CLASS
)
6324 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
6325 printer
->name
, get_username(con
));
6327 cupsdDeletePrinter(printer
, 0);
6328 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6332 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
6333 printer
->name
, get_username(con
));
6335 cupsdDeletePrinter(printer
, 0);
6336 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
6339 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
6342 * Return with no errors...
6345 con
->response
->request
.status
.status_code
= IPP_OK
;
6350 * 'get_default()' - Get the default destination.
6354 get_default(cupsd_client_t
*con
) /* I - Client connection */
6356 http_status_t status
; /* Policy status */
6357 cups_array_t
*ra
; /* Requested attributes array */
6360 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
6366 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6368 send_http_error(con
, status
, NULL
);
6374 ra
= create_requested_array(con
->request
);
6376 copy_printer_attrs(con
, DefaultPrinter
, ra
);
6378 cupsArrayDelete(ra
);
6380 con
->response
->request
.status
.status_code
= IPP_OK
;
6383 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
6388 * 'get_devices()' - Get the list of available devices on the local system.
6392 get_devices(cupsd_client_t
*con
) /* I - Client connection */
6394 http_status_t status
; /* Policy status */
6395 ipp_attribute_t
*limit
, /* limit attribute */
6396 *timeout
, /* timeout attribute */
6397 *requested
, /* requested-attributes attribute */
6398 *exclude
; /* exclude-schemes attribute */
6399 char command
[1024], /* cups-deviced command */
6400 options
[1024], /* Options to pass to command */
6402 /* String for requested attributes */
6404 /* String for excluded attributes */
6407 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
6413 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6415 send_http_error(con
, status
, NULL
);
6420 * Run cups-deviced command with the given options...
6423 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6424 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
6425 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6427 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
6430 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6432 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6435 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6437 exclude_str
[0] = '\0';
6439 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
6440 snprintf(options
, sizeof(options
),
6441 "%d+%d+%d+%d+%s%s%s",
6442 con
->request
->request
.op
.request_id
,
6443 limit
? limit
->values
[0].integer
: 0,
6444 timeout
? timeout
->values
[0].integer
: 10,
6447 exclude_str
[0] ? "%20" : "", exclude_str
);
6449 if (cupsdSendCommand(con
, command
, options
, 1))
6452 * Command started successfully, don't send an IPP response here...
6455 ippDelete(con
->response
);
6456 con
->response
= NULL
;
6461 * Command failed, return "internal error" so the user knows something
6465 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6466 _("cups-deviced failed to execute."));
6472 * 'get_document()' - Get a copy of a job file.
6476 get_document(cupsd_client_t
*con
, /* I - Client connection */
6477 ipp_attribute_t
*uri
) /* I - Job URI */
6479 http_status_t status
; /* Policy status */
6480 ipp_attribute_t
*attr
; /* Current attribute */
6481 int jobid
; /* Job ID */
6482 int docnum
; /* Document number */
6483 cupsd_job_t
*job
; /* Current job */
6484 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6485 username
[HTTP_MAX_URI
], /* Username portion of URI */
6486 host
[HTTP_MAX_URI
], /* Host portion of URI */
6487 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6488 int port
; /* Port portion of URI */
6489 char filename
[1024], /* Filename for document */
6490 format
[1024]; /* Format for document */
6493 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6494 con
->http
.fd
, uri
->values
[0].string
.text
);
6497 * See if we have a job URI or a printer URI...
6500 if (!strcmp(uri
->name
, "printer-uri"))
6503 * Got a printer URI; see if we also have a job-id attribute...
6506 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6507 IPP_TAG_INTEGER
)) == NULL
)
6509 send_ipp_status(con
, IPP_BAD_REQUEST
,
6510 _("Got a printer-uri attribute but no job-id!"));
6514 jobid
= attr
->values
[0].integer
;
6519 * Got a job URI; parse it to get the job ID...
6522 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6523 sizeof(method
), username
, sizeof(username
), host
,
6524 sizeof(host
), &port
, resource
, sizeof(resource
));
6526 if (strncmp(resource
, "/jobs/", 6))
6532 send_ipp_status(con
, IPP_BAD_REQUEST
,
6533 _("Bad job-uri attribute \"%s\"!"),
6534 uri
->values
[0].string
.text
);
6538 jobid
= atoi(resource
+ 6);
6542 * See if the job exists...
6545 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6548 * Nope - return a "not found" error...
6551 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6559 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6561 send_http_error(con
, status
, NULL
);
6566 * Get the document number...
6569 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6570 IPP_TAG_INTEGER
)) == NULL
)
6572 send_ipp_status(con
, IPP_BAD_REQUEST
,
6573 _("Missing document-number attribute!"));
6577 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6578 attr
->num_values
> 1)
6580 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
6585 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6587 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6589 cupsdLogMessage(CUPSD_LOG_ERROR
,
6590 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6592 send_ipp_status(con
, IPP_NOT_FOUND
,
6593 _("Unable to open document %d in job %d!"), docnum
, jobid
);
6597 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6601 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6602 job
->filetypes
[docnum
- 1]->type
);
6604 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6606 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6608 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6609 IPP_TAG_NAME
)) != NULL
)
6610 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6611 NULL
, attr
->values
[0].string
.text
);
6616 * 'get_job_attrs()' - Get job attributes.
6620 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6621 ipp_attribute_t
*uri
) /* I - Job URI */
6623 http_status_t status
; /* Policy status */
6624 ipp_attribute_t
*attr
; /* Current attribute */
6625 int jobid
; /* Job ID */
6626 cupsd_job_t
*job
; /* Current job */
6627 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6628 username
[HTTP_MAX_URI
], /* Username portion of URI */
6629 host
[HTTP_MAX_URI
], /* Host portion of URI */
6630 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6631 int port
; /* Port portion of URI */
6632 cups_array_t
*ra
; /* Requested attributes array */
6635 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6636 con
->http
.fd
, uri
->values
[0].string
.text
);
6639 * See if we have a job URI or a printer URI...
6642 if (!strcmp(uri
->name
, "printer-uri"))
6645 * Got a printer URI; see if we also have a job-id attribute...
6648 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6649 IPP_TAG_INTEGER
)) == NULL
)
6651 send_ipp_status(con
, IPP_BAD_REQUEST
,
6652 _("Got a printer-uri attribute but no job-id!"));
6656 jobid
= attr
->values
[0].integer
;
6661 * Got a job URI; parse it to get the job ID...
6664 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6665 sizeof(method
), username
, sizeof(username
), host
,
6666 sizeof(host
), &port
, resource
, sizeof(resource
));
6668 if (strncmp(resource
, "/jobs/", 6))
6674 send_ipp_status(con
, IPP_BAD_REQUEST
,
6675 _("Bad job-uri attribute \"%s\"!"),
6676 uri
->values
[0].string
.text
);
6680 jobid
= atoi(resource
+ 6);
6684 * See if the job exists...
6687 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6690 * Nope - return a "not found" error...
6693 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6701 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6703 send_http_error(con
, status
, NULL
);
6708 * Copy attributes...
6713 ra
= create_requested_array(con
->request
);
6714 copy_job_attrs(con
, job
, ra
);
6715 cupsArrayDelete(ra
);
6717 con
->response
->request
.status
.status_code
= IPP_OK
;
6722 * 'get_jobs()' - Get a list of jobs for the specified printer.
6726 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6727 ipp_attribute_t
*uri
) /* I - Printer URI */
6729 http_status_t status
; /* Policy status */
6730 ipp_attribute_t
*attr
; /* Current attribute */
6731 const char *dest
; /* Destination */
6732 cups_ptype_t dtype
; /* Destination type (printer/class) */
6733 cups_ptype_t dmask
; /* Destination type mask */
6734 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6735 username
[HTTP_MAX_URI
], /* Username portion of URI */
6736 host
[HTTP_MAX_URI
], /* Host portion of URI */
6737 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6738 int port
; /* Port portion of URI */
6739 int completed
; /* Completed jobs? */
6740 int first_job_id
; /* First job ID */
6741 int limit
; /* Maximum number of jobs to return */
6742 int count
; /* Number of jobs that match */
6743 cupsd_job_t
*job
; /* Current job pointer */
6744 cupsd_printer_t
*printer
; /* Printer */
6745 cups_array_t
*list
; /* Which job list... */
6746 cups_array_t
*ra
; /* Requested attributes array */
6749 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
6750 uri
->values
[0].string
.text
);
6753 * Is the destination valid?
6756 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6757 sizeof(scheme
), username
, sizeof(username
), host
,
6758 sizeof(host
), &port
, resource
, sizeof(resource
));
6760 if (!strcmp(resource
, "/") ||
6761 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
6764 dtype
= (cups_ptype_t
)0;
6765 dmask
= (cups_ptype_t
)0;
6768 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6771 dtype
= (cups_ptype_t
)0;
6772 dmask
= CUPS_PRINTER_CLASS
;
6775 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6778 dtype
= CUPS_PRINTER_CLASS
;
6779 dmask
= CUPS_PRINTER_CLASS
;
6782 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6789 send_ipp_status(con
, IPP_NOT_FOUND
,
6790 _("The printer or class was not found."));
6795 dtype
&= CUPS_PRINTER_CLASS
;
6796 dmask
= CUPS_PRINTER_CLASS
;
6805 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6808 send_http_error(con
, status
, printer
);
6812 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6814 send_http_error(con
, status
, NULL
);
6819 * See if the "which-jobs" attribute have been specified...
6822 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6823 IPP_TAG_KEYWORD
)) != NULL
&&
6824 !strcmp(attr
->values
[0].string
.text
, "completed"))
6829 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
6834 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "printing"))
6837 list
= PrintingJobs
;
6846 * See if they want to limit the number of jobs reported...
6849 if ((attr
= ippFindAttribute(con
->request
, "limit",
6850 IPP_TAG_INTEGER
)) != NULL
)
6851 limit
= attr
->values
[0].integer
;
6855 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
6856 IPP_TAG_INTEGER
)) != NULL
)
6857 first_job_id
= attr
->values
[0].integer
;
6862 * See if we only want to see jobs for a specific user...
6865 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
6866 IPP_TAG_BOOLEAN
)) != NULL
&&
6867 attr
->values
[0].boolean
)
6868 strlcpy(username
, get_username(con
), sizeof(username
));
6872 ra
= create_requested_array(con
->request
);
6875 * OK, build a list of jobs for this printer...
6878 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6879 (limit
<= 0 || count
< limit
) && job
;
6880 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6883 * Filter out jobs that don't match...
6886 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
6888 if (!job
->dest
|| !job
->username
)
6891 if (!job
->dest
|| !job
->username
)
6894 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6895 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6897 if ((job
->dtype
& dmask
) != dtype
&&
6898 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6900 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
6903 if (job
->id
< first_job_id
)
6911 if (username
[0] && strcasecmp(username
, job
->username
))
6915 ippAddSeparator(con
->response
);
6919 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
6921 copy_job_attrs(con
, job
, ra
);
6924 cupsArrayDelete(ra
);
6926 con
->response
->request
.status
.status_code
= IPP_OK
;
6931 * 'get_notifications()' - Get events for a subscription.
6935 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6937 int i
, j
; /* Looping vars */
6938 http_status_t status
; /* Policy status */
6939 cupsd_subscription_t
*sub
; /* Subscription */
6940 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6941 *sequences
; /* notify-sequence-numbers */
6942 int min_seq
; /* Minimum sequence number */
6943 int interval
; /* Poll interval */
6946 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6950 * Get subscription attributes...
6953 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6955 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6960 send_ipp_status(con
, IPP_BAD_REQUEST
,
6961 _("Missing notify-subscription-ids attribute!"));
6966 * Are the subscription IDs valid?
6969 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6971 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6974 * Bad subscription ID...
6977 send_ipp_status(con
, IPP_NOT_FOUND
,
6978 _("notify-subscription-id %d no good!"),
6979 ids
->values
[i
].integer
);
6987 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6989 con
, sub
->owner
)) != HTTP_OK
)
6991 send_http_error(con
, status
, sub
->dest
);
6996 * Check the subscription type and update the interval accordingly.
6999 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
7002 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
7004 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
7010 * Tell the client to poll again in N seconds...
7014 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
7015 "notify-get-interval", interval
);
7017 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
7018 "printer-up-time", time(NULL
));
7021 * Copy the subscription event attributes to the response.
7024 con
->response
->request
.status
.status_code
=
7025 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
7027 for (i
= 0; i
< ids
->num_values
; i
++)
7030 * Get the subscription and sequence number...
7033 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
7035 if (sequences
&& i
< sequences
->num_values
)
7036 min_seq
= sequences
->values
[i
].integer
;
7041 * If we don't have any new events, nothing to do here...
7044 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
7048 * Otherwise copy all of the new events...
7051 if (sub
->first_event_id
> min_seq
)
7054 j
= min_seq
- sub
->first_event_id
;
7056 for (; j
< sub
->num_events
; j
++)
7058 ippAddSeparator(con
->response
);
7060 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
7061 IPP_TAG_EVENT_NOTIFICATION
, 0);
7068 * 'get_ppd()' - Get a named PPD from the local system.
7072 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
7073 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
7075 http_status_t status
; /* Policy status */
7076 cupsd_printer_t
*dest
; /* Destination */
7077 cups_ptype_t dtype
; /* Destination type */
7080 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
7081 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
7083 if (!strcmp(uri
->name
, "ppd-name"))
7086 * Return a PPD file from cups-driverd...
7089 char command
[1024], /* cups-driverd command */
7090 options
[1024], /* Options to pass to command */
7091 ppd_name
[1024]; /* ppd-name */
7098 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7100 send_http_error(con
, status
, NULL
);
7105 * Run cups-driverd command with the given options...
7108 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7109 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
7110 snprintf(options
, sizeof(options
), "get+%d+%s",
7111 con
->request
->request
.op
.request_id
, ppd_name
);
7113 if (cupsdSendCommand(con
, command
, options
, 0))
7116 * Command started successfully, don't send an IPP response here...
7119 ippDelete(con
->response
);
7120 con
->response
= NULL
;
7125 * Command failed, return "internal error" so the user knows something
7129 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7130 _("cups-driverd failed to execute."));
7133 else if (!strcmp(uri
->name
, "printer-uri") &&
7134 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
7136 int i
; /* Looping var */
7137 char filename
[1024]; /* PPD filename */
7144 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7146 send_http_error(con
, status
, dest
);
7151 * See if we need the PPD for a class or remote printer...
7154 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7157 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
7159 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7160 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7161 "printer-uri", NULL
, dest
->uri
);
7164 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
7166 for (i
= 0; i
< dest
->num_printers
; i
++)
7167 if (!(dest
->printers
[i
]->type
&
7168 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
7170 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7171 dest
->printers
[i
]->name
);
7173 if (!access(filename
, 0))
7177 if (i
< dest
->num_printers
)
7178 dest
= dest
->printers
[i
];
7181 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7182 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7183 "printer-uri", NULL
, dest
->printers
[0]->uri
);
7189 * Found the printer with the PPD file, now see if there is one...
7192 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
7194 send_ipp_status(con
, IPP_NOT_FOUND
,
7195 _("The PPD file \"%s\" could not be opened: %s"),
7196 uri
->values
[0].string
.text
, strerror(errno
));
7200 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
7204 con
->response
->request
.status
.status_code
= IPP_OK
;
7207 send_ipp_status(con
, IPP_NOT_FOUND
,
7208 _("The PPD file \"%s\" could not be found."),
7209 uri
->values
[0].string
.text
);
7214 * 'get_ppds()' - Get the list of PPD files on the local system.
7218 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
7220 http_status_t status
; /* Policy status */
7221 ipp_attribute_t
*limit
, /* Limit attribute */
7222 *device
, /* ppd-device-id attribute */
7223 *language
, /* ppd-natural-language attribute */
7224 *make
, /* ppd-make attribute */
7225 *model
, /* ppd-make-and-model attribute */
7226 *model_number
, /* ppd-model-number attribute */
7227 *product
, /* ppd-product attribute */
7228 *psversion
, /* ppd-psverion attribute */
7229 *type
, /* ppd-type attribute */
7230 *requested
; /* requested-attributes attribute */
7231 char command
[1024], /* cups-driverd command */
7232 options
[1024], /* Options to pass to command */
7233 device_str
[256],/* Escaped ppd-device-id string */
7235 /* Escaped ppd-natural-language */
7236 make_str
[256], /* Escaped ppd-make string */
7237 model_str
[256], /* Escaped ppd-make-and-model string */
7238 model_number_str
[256],
7239 /* ppd-model-number string */
7241 /* Escaped ppd-product string */
7243 /* Escaped ppd-psversion string */
7244 type_str
[256], /* Escaped ppd-type string */
7246 /* String for requested attributes */
7249 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
7255 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7257 send_http_error(con
, status
, NULL
);
7262 * Run cups-driverd command with the given options...
7265 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
7266 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
7267 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
7269 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
7270 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
7272 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
7274 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
7275 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
7276 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
7277 requested
= ippFindAttribute(con
->request
, "requested-attributes",
7281 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
7283 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
7286 url_encode_attr(device
, device_str
, sizeof(device_str
));
7288 device_str
[0] = '\0';
7291 url_encode_attr(language
, language_str
, sizeof(language_str
));
7293 language_str
[0] = '\0';
7296 url_encode_attr(make
, make_str
, sizeof(make_str
));
7301 url_encode_attr(model
, model_str
, sizeof(model_str
));
7303 model_str
[0] = '\0';
7306 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
7307 model_number
->values
[0].integer
);
7309 model_number_str
[0] = '\0';
7312 url_encode_attr(product
, product_str
, sizeof(product_str
));
7314 product_str
[0] = '\0';
7317 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
7319 psversion_str
[0] = '\0';
7322 url_encode_attr(type
, type_str
, sizeof(type_str
));
7326 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7327 snprintf(options
, sizeof(options
),
7328 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7329 con
->request
->request
.op
.request_id
,
7330 limit
? limit
->values
[0].integer
: 0,
7332 device
? "%20" : "", device_str
,
7333 language
? "%20" : "", language_str
,
7334 make
? "%20" : "", make_str
,
7335 model
? "%20" : "", model_str
,
7336 model_number
? "%20" : "", model_number_str
,
7337 product
? "%20" : "", product_str
,
7338 psversion
? "%20" : "", psversion_str
,
7339 type
? "%20" : "", type_str
);
7341 if (cupsdSendCommand(con
, command
, options
, 0))
7344 * Command started successfully, don't send an IPP response here...
7347 ippDelete(con
->response
);
7348 con
->response
= NULL
;
7353 * Command failed, return "internal error" so the user knows something
7357 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7358 _("cups-driverd failed to execute."));
7364 * 'get_printer_attrs()' - Get printer attributes.
7368 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
7369 ipp_attribute_t
*uri
) /* I - Printer URI */
7371 http_status_t status
; /* Policy status */
7372 cups_ptype_t dtype
; /* Destination type (printer/class) */
7373 cupsd_printer_t
*printer
; /* Printer/class */
7374 cups_array_t
*ra
; /* Requested attributes array */
7377 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
7378 con
->http
.fd
, uri
->values
[0].string
.text
);
7381 * Is the destination valid?
7384 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7390 send_ipp_status(con
, IPP_NOT_FOUND
,
7391 _("The printer or class was not found."));
7399 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7401 send_http_error(con
, status
, printer
);
7406 * Send the attributes...
7409 ra
= create_requested_array(con
->request
);
7411 copy_printer_attrs(con
, printer
, ra
);
7413 cupsArrayDelete(ra
);
7415 con
->response
->request
.status
.status_code
= IPP_OK
;
7420 * 'get_printers()' - Get a list of printers or classes.
7424 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7425 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7427 http_status_t status
; /* Policy status */
7428 ipp_attribute_t
*attr
; /* Current attribute */
7429 int limit
; /* Max number of printers to return */
7430 int count
; /* Number of printers that match */
7431 cupsd_printer_t
*printer
; /* Current printer pointer */
7432 int printer_type
, /* printer-type attribute */
7433 printer_mask
; /* printer-type-mask attribute */
7434 char *location
; /* Location string */
7435 const char *username
; /* Current user */
7436 char *first_printer_name
; /* first-printer-name attribute */
7437 cups_array_t
*ra
; /* Requested attributes array */
7440 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7441 con
->http
.fd
, type
);
7447 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7449 send_http_error(con
, status
, NULL
);
7454 * Check for printers...
7457 if (!Printers
|| !cupsArrayCount(Printers
))
7459 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7464 * See if they want to limit the number of printers reported...
7467 if ((attr
= ippFindAttribute(con
->request
, "limit",
7468 IPP_TAG_INTEGER
)) != NULL
)
7469 limit
= attr
->values
[0].integer
;
7473 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7474 IPP_TAG_NAME
)) != NULL
)
7475 first_printer_name
= attr
->values
[0].string
.text
;
7477 first_printer_name
= NULL
;
7480 * Support filtering...
7483 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7484 IPP_TAG_ENUM
)) != NULL
)
7485 printer_type
= attr
->values
[0].integer
;
7489 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7490 IPP_TAG_ENUM
)) != NULL
)
7491 printer_mask
= attr
->values
[0].integer
;
7495 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7496 IPP_TAG_TEXT
)) != NULL
)
7497 location
= attr
->values
[0].string
.text
;
7501 if (con
->username
[0])
7502 username
= con
->username
;
7503 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7504 IPP_TAG_NAME
)) != NULL
)
7505 username
= attr
->values
[0].string
.text
;
7509 ra
= create_requested_array(con
->request
);
7512 * OK, build a list of printers for this printer...
7515 if (first_printer_name
)
7517 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7518 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7521 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7524 count
< limit
&& printer
;
7525 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7527 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7528 (printer
->type
& printer_mask
) == printer_type
&&
7530 (printer
->location
&& !strcasecmp(printer
->location
, location
))))
7533 * If HideImplicitMembers is enabled, see if this printer or class
7534 * is a member of an implicit class...
7537 if (ImplicitClasses
&& HideImplicitMembers
&&
7538 printer
->in_implicit_class
)
7542 * If a username is specified, see if it is allowed or denied
7546 if (!(printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
7547 printer
->num_users
&& username
&& !user_allowed(printer
, username
))
7551 * Add the group separator as needed...
7555 ippAddSeparator(con
->response
);
7560 * Send the attributes...
7563 copy_printer_attrs(con
, printer
, ra
);
7567 cupsArrayDelete(ra
);
7569 con
->response
->request
.status
.status_code
= IPP_OK
;
7574 * 'get_subscription_attrs()' - Get subscription attributes.
7578 get_subscription_attrs(
7579 cupsd_client_t
*con
, /* I - Client connection */
7580 int sub_id
) /* I - Subscription ID */
7582 http_status_t status
; /* Policy status */
7583 cupsd_subscription_t
*sub
; /* Subscription */
7584 cups_array_t
*ra
; /* Requested attributes array */
7587 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7588 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7589 con
, con
->http
.fd
, sub_id
);
7592 * Is the subscription ID valid?
7595 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7598 * Bad subscription ID...
7601 send_ipp_status(con
, IPP_NOT_FOUND
,
7602 _("notify-subscription-id %d no good!"), sub_id
);
7610 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7612 con
, sub
->owner
)) != HTTP_OK
)
7614 send_http_error(con
, status
, sub
->dest
);
7619 * Copy the subscription attributes to the response using the
7620 * requested-attributes attribute that may be provided by the client.
7623 ra
= create_requested_array(con
->request
);
7625 copy_subscription_attrs(con
, sub
, ra
);
7627 cupsArrayDelete(ra
);
7629 con
->response
->request
.status
.status_code
= IPP_OK
;
7634 * 'get_subscriptions()' - Get subscriptions.
7638 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7639 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7641 http_status_t status
; /* Policy status */
7642 int count
; /* Number of subscriptions */
7643 int limit
; /* Limit */
7644 cupsd_subscription_t
*sub
; /* Subscription */
7645 cups_array_t
*ra
; /* Requested attributes array */
7646 ipp_attribute_t
*attr
; /* Attribute */
7647 cups_ptype_t dtype
; /* Destination type (printer/class) */
7648 char scheme
[HTTP_MAX_URI
],
7649 /* Scheme portion of URI */
7650 username
[HTTP_MAX_URI
],
7651 /* Username portion of URI */
7653 /* Host portion of URI */
7654 resource
[HTTP_MAX_URI
];
7655 /* Resource portion of URI */
7656 int port
; /* Port portion of URI */
7657 cupsd_job_t
*job
; /* Job pointer */
7658 cupsd_printer_t
*printer
; /* Printer */
7661 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7662 "get_subscriptions(con=%p[%d], uri=%s)",
7663 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
7666 * Is the destination valid?
7669 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7670 sizeof(scheme
), username
, sizeof(username
), host
,
7671 sizeof(host
), &port
, resource
, sizeof(resource
));
7673 if (!strcmp(resource
, "/") ||
7674 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7675 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7676 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7681 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7684 job
= cupsdFindJob(atoi(resource
+ 6));
7688 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
7693 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7699 send_ipp_status(con
, IPP_NOT_FOUND
,
7700 _("The printer or class was not found."));
7703 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7704 IPP_TAG_INTEGER
)) != NULL
)
7706 job
= cupsdFindJob(attr
->values
[0].integer
);
7710 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
7711 attr
->values
[0].integer
);
7722 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
7724 con
, NULL
)) != HTTP_OK
)
7726 send_http_error(con
, status
, printer
);
7731 * Copy the subscription attributes to the response using the
7732 * requested-attributes attribute that may be provided by the client.
7735 ra
= create_requested_array(con
->request
);
7737 if ((attr
= ippFindAttribute(con
->request
, "limit",
7738 IPP_TAG_INTEGER
)) != NULL
)
7739 limit
= attr
->values
[0].integer
;
7744 * See if we only want to see subscriptions for a specific user...
7747 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7748 IPP_TAG_BOOLEAN
)) != NULL
&&
7749 attr
->values
[0].boolean
)
7750 strlcpy(username
, get_username(con
), sizeof(username
));
7754 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7756 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7757 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7758 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
7760 ippAddSeparator(con
->response
);
7761 copy_subscription_attrs(con
, sub
, ra
);
7764 if (limit
&& count
>= limit
)
7768 cupsArrayDelete(ra
);
7771 con
->response
->request
.status
.status_code
= IPP_OK
;
7773 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7778 * 'get_username()' - Get the username associated with a request.
7781 static const char * /* O - Username */
7782 get_username(cupsd_client_t
*con
) /* I - Connection */
7784 ipp_attribute_t
*attr
; /* Attribute */
7787 if (con
->username
[0])
7788 return (con
->username
);
7789 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7790 IPP_TAG_NAME
)) != NULL
)
7791 return (attr
->values
[0].string
.text
);
7793 return ("anonymous");
7798 * 'hold_job()' - Hold a print job.
7802 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7803 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7805 ipp_attribute_t
*attr
, /* Current job-hold-until */
7806 *newattr
; /* New job-hold-until */
7807 int jobid
; /* Job ID */
7808 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7809 username
[HTTP_MAX_URI
], /* Username portion of URI */
7810 host
[HTTP_MAX_URI
], /* Host portion of URI */
7811 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7812 int port
; /* Port portion of URI */
7813 cupsd_job_t
*job
; /* Job information */
7816 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
7817 uri
->values
[0].string
.text
);
7820 * See if we have a job URI or a printer URI...
7823 if (!strcmp(uri
->name
, "printer-uri"))
7826 * Got a printer URI; see if we also have a job-id attribute...
7829 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7830 IPP_TAG_INTEGER
)) == NULL
)
7832 send_ipp_status(con
, IPP_BAD_REQUEST
,
7833 _("Got a printer-uri attribute but no job-id!"));
7837 jobid
= attr
->values
[0].integer
;
7842 * Got a job URI; parse it to get the job ID...
7845 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7846 sizeof(method
), username
, sizeof(username
), host
,
7847 sizeof(host
), &port
, resource
, sizeof(resource
));
7849 if (strncmp(resource
, "/jobs/", 6))
7855 send_ipp_status(con
, IPP_BAD_REQUEST
,
7856 _("Bad job-uri attribute \"%s\"!"),
7857 uri
->values
[0].string
.text
);
7861 jobid
= atoi(resource
+ 6);
7865 * See if the job exists...
7868 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7871 * Nope - return a "not found" error...
7874 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7879 * See if the job is owned by the requesting user...
7882 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7884 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7889 * Hold the job and return...
7894 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
7895 "Job held by user.");
7897 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
7898 IPP_TAG_KEYWORD
)) == NULL
)
7899 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7901 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7902 IPP_TAG_KEYWORD
)) == NULL
)
7903 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7908 * Free the old hold value and copy the new one over...
7911 _cupsStrFree(attr
->values
[0].string
.text
);
7915 attr
->value_tag
= newattr
->value_tag
;
7916 attr
->values
[0].string
.text
=
7917 _cupsStrAlloc(newattr
->values
[0].string
.text
);
7921 attr
->value_tag
= IPP_TAG_KEYWORD
;
7922 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
7926 * Hold job until specified time...
7929 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
7931 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
7932 "Job job-hold-until value changed by user.");
7935 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Held by \"%s\".", username
);
7937 con
->response
->request
.status
.status_code
= IPP_OK
;
7942 * 'move_job()' - Move a job to a new destination.
7946 move_job(cupsd_client_t
*con
, /* I - Client connection */
7947 ipp_attribute_t
*uri
) /* I - Job URI */
7949 http_status_t status
; /* Policy status */
7950 ipp_attribute_t
*attr
; /* Current attribute */
7951 int jobid
; /* Job ID */
7952 cupsd_job_t
*job
; /* Current job */
7953 const char *src
; /* Source printer/class */
7954 cups_ptype_t stype
, /* Source type (printer or class) */
7955 dtype
; /* Destination type (printer/class) */
7956 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7957 username
[HTTP_MAX_URI
], /* Username portion of URI */
7958 host
[HTTP_MAX_URI
], /* Host portion of URI */
7959 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7960 int port
; /* Port portion of URI */
7961 cupsd_printer_t
*sprinter
, /* Source printer */
7962 *dprinter
; /* Destination printer */
7965 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
7966 uri
->values
[0].string
.text
);
7969 * Get the new printer or class...
7972 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7973 IPP_TAG_URI
)) == NULL
)
7976 * Need job-printer-uri...
7979 send_ipp_status(con
, IPP_BAD_REQUEST
,
7980 _("job-printer-uri attribute missing!"));
7984 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7990 send_ipp_status(con
, IPP_NOT_FOUND
,
7991 _("The printer or class was not found."));
7996 * See if we have a job URI or a printer URI...
7999 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8000 sizeof(scheme
), username
, sizeof(username
), host
,
8001 sizeof(host
), &port
, resource
, sizeof(resource
));
8003 if (!strcmp(uri
->name
, "printer-uri"))
8006 * Got a printer URI; see if we also have a job-id attribute...
8009 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8010 IPP_TAG_INTEGER
)) == NULL
)
8016 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
8017 &sprinter
)) == NULL
)
8023 send_ipp_status(con
, IPP_NOT_FOUND
,
8024 _("The printer or class was not found."));
8033 * Otherwise, just move a single job...
8036 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
8039 * Nope - return a "not found" error...
8042 send_ipp_status(con
, IPP_NOT_FOUND
,
8043 _("Job #%d does not exist!"), attr
->values
[0].integer
);
8049 * Job found, initialize source pointers...
8060 * Got a job URI; parse it to get the job ID...
8063 if (strncmp(resource
, "/jobs/", 6))
8069 send_ipp_status(con
, IPP_BAD_REQUEST
,
8070 _("Bad job-uri attribute \"%s\"!"),
8071 uri
->values
[0].string
.text
);
8076 * See if the job exists...
8079 jobid
= atoi(resource
+ 6);
8081 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8084 * Nope - return a "not found" error...
8087 send_ipp_status(con
, IPP_NOT_FOUND
,
8088 _("Job #%d does not exist!"), jobid
);
8094 * Job found, initialize source pointers...
8103 * Check the policy of the destination printer...
8106 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
8107 job
? job
->username
: NULL
)) != HTTP_OK
)
8109 send_http_error(con
, status
, dprinter
);
8114 * Now move the job or jobs...
8120 * See if the job has been completed...
8123 if (job
->state_value
> IPP_JOB_STOPPED
)
8126 * Return a "not-possible" error...
8129 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8130 _("Job #%d is finished and cannot be altered!"),
8136 * See if the job is owned by the requesting user...
8139 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8141 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8146 * Move the job to a different printer or class...
8149 cupsdMoveJob(job
, dprinter
);
8154 * Got the source printer, now look through the jobs...
8157 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
8159 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
8162 * See if the job is pointing at the source printer or has not been
8166 if (strcasecmp(job
->dest
, src
) ||
8167 job
->state_value
> IPP_JOB_STOPPED
)
8171 * See if the job can be moved by the requesting user...
8174 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8178 * Move the job to a different printer or class...
8181 cupsdMoveJob(job
, dprinter
);
8186 * Start jobs if possible...
8192 * Return with "everything is OK" status...
8195 con
->response
->request
.status
.status_code
= IPP_OK
;
8200 * 'ppd_parse_line()' - Parse a PPD default line.
8203 static int /* O - 0 on success, -1 on failure */
8204 ppd_parse_line(const char *line
, /* I - Line */
8205 char *option
, /* O - Option name */
8206 int olen
, /* I - Size of option name */
8207 char *choice
, /* O - Choice name */
8208 int clen
) /* I - Size of choice name */
8211 * Verify this is a default option line...
8214 if (strncmp(line
, "*Default", 8))
8218 * Read the option name...
8221 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
8231 * Skip everything else up to the colon (:)...
8234 while (*line
&& *line
!= ':')
8243 * Now grab the option choice, skipping leading whitespace...
8246 while (isspace(*line
& 255))
8249 for (clen
--; isalnum(*line
& 255); line
++)
8259 * Return with no errors...
8267 * 'print_job()' - Print a file to a printer or class.
8271 print_job(cupsd_client_t
*con
, /* I - Client connection */
8272 ipp_attribute_t
*uri
) /* I - Printer URI */
8274 ipp_attribute_t
*attr
; /* Current attribute */
8275 ipp_attribute_t
*format
; /* Document-format attribute */
8276 const char *default_format
; /* document-format-default value */
8277 cupsd_job_t
*job
; /* New job */
8278 char filename
[1024]; /* Job filename */
8279 mime_type_t
*filetype
; /* Type of file */
8280 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8281 type
[MIME_MAX_TYPE
], /* Subtype of file */
8282 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8283 /* Textual name of mime type */
8284 cupsd_printer_t
*printer
; /* Printer data */
8285 struct stat fileinfo
; /* File information */
8286 int kbytes
; /* Size of file */
8287 int compression
; /* Document compression */
8290 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
8291 uri
->values
[0].string
.text
);
8294 * Validate print file attributes, for now just document-format and
8295 * compression (CUPS only supports "none" and "gzip")...
8298 compression
= CUPS_FILE_NONE
;
8300 if ((attr
= ippFindAttribute(con
->request
, "compression",
8301 IPP_TAG_KEYWORD
)) != NULL
)
8303 if (strcmp(attr
->values
[0].string
.text
, "none")
8305 && strcmp(attr
->values
[0].string
.text
, "gzip")
8306 #endif /* HAVE_LIBZ */
8309 send_ipp_status(con
, IPP_ATTRIBUTES
,
8310 _("Unsupported compression \"%s\"!"),
8311 attr
->values
[0].string
.text
);
8312 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8313 "compression", NULL
, attr
->values
[0].string
.text
);
8318 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8319 compression
= CUPS_FILE_GZIP
;
8320 #endif /* HAVE_LIBZ */
8324 * Do we have a file to print?
8329 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
8334 * Is the destination valid?
8337 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8343 send_ipp_status(con
, IPP_NOT_FOUND
,
8344 _("The printer or class was not found."));
8349 * Is it a format we support?
8352 if ((format
= ippFindAttribute(con
->request
, "document-format",
8353 IPP_TAG_MIMETYPE
)) != NULL
)
8356 * Grab format from client...
8359 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
8362 send_ipp_status(con
, IPP_BAD_REQUEST
,
8363 _("Could not scan type \"%s\"!"),
8364 format
->values
[0].string
.text
);
8368 else if ((default_format
= cupsGetOption("document-format",
8369 printer
->num_options
,
8370 printer
->options
)) != NULL
)
8373 * Use default document format...
8376 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
8378 send_ipp_status(con
, IPP_BAD_REQUEST
,
8379 _("Could not scan type \"%s\"!"),
8390 strcpy(super
, "application");
8391 strcpy(type
, "octet-stream");
8394 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8397 * Auto-type the file...
8400 ipp_attribute_t
*doc_name
; /* document-name attribute */
8403 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8405 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8406 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8407 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8411 filetype
= mimeType(MimeDatabase
, super
, type
);
8413 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8414 filetype
->super
, filetype
->type
);
8417 filetype
= mimeType(MimeDatabase
, super
, type
);
8421 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8424 * Replace the document-format attribute value with the auto-typed or
8428 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8433 _cupsStrFree(format
->values
[0].string
.text
);
8435 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8438 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8439 "document-format", NULL
, mimetype
);
8443 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8444 _("Unsupported format \'%s/%s\'!"), super
, type
);
8445 cupsdLogMessage(CUPSD_LOG_INFO
,
8446 "Hint: Do you have the raw file printing rules enabled?");
8449 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8450 "document-format", NULL
, format
->values
[0].string
.text
);
8456 * Read any embedded job ticket info from PS files...
8459 if (!strcasecmp(filetype
->super
, "application") &&
8460 (!strcasecmp(filetype
->type
, "postscript") ||
8461 !strcasecmp(filetype
->type
, "pdf")))
8462 read_job_ticket(con
);
8465 * Create the job object...
8468 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8472 * Update quota data...
8475 if (stat(con
->filename
, &fileinfo
))
8478 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8480 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8482 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8483 IPP_TAG_INTEGER
)) != NULL
)
8484 attr
->values
[0].integer
+= kbytes
;
8487 * Add the job file...
8490 if (add_file(con
, job
, filetype
, compression
))
8493 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8495 rename(con
->filename
, filename
);
8496 cupsdClearString(&con
->filename
);
8499 * See if we need to add the ending sheet...
8502 if (cupsdTimeoutJob(job
))
8506 * Log and save the job...
8509 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8510 "File of type %s/%s queued by \"%s\".",
8511 filetype
->super
, filetype
->type
, job
->username
);
8512 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8513 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8514 job
->dest
, job
->username
);
8517 * Start the job if possible...
8525 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8527 * This function only gets called when printing a single PDF or PostScript
8528 * file using the Print-Job operation. It doesn't work for Create-Job +
8529 * Send-File, since the job attributes need to be set at job creation
8530 * time for banners to work. The embedded job ticket stuff is here
8531 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8532 * options and IPP attributes which otherwise would be lost.
8534 * The format of a job ticket is simple:
8536 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8538 * %cupsJobTicket: attr1=value1
8539 * %cupsJobTicket: attr2=value2
8541 * %cupsJobTicket: attrN=valueN
8543 * Job ticket lines must appear immediately after the first line that
8544 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8545 * stops looking for job ticket info when it finds a line that does not begin
8546 * with "%cupsJobTicket:".
8548 * The maximum length of a job ticket line, including the prefix, is
8549 * 255 characters to conform with the Adobe DSC.
8551 * Read-only attributes are rejected with a notice to the error log in
8552 * case a malicious user tries anything. Since the job ticket is read
8553 * prior to attribute validation in print_job(), job ticket attributes
8554 * will go through the same validation as IPP attributes...
8558 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8560 cups_file_t
*fp
; /* File to read from */
8561 char line
[256]; /* Line data */
8562 int num_options
; /* Number of options */
8563 cups_option_t
*options
; /* Options */
8564 ipp_t
*ticket
; /* New attributes */
8565 ipp_attribute_t
*attr
, /* Current attribute */
8566 *attr2
, /* Job attribute */
8567 *prev2
; /* Previous job attribute */
8571 * First open the print file...
8574 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8576 cupsdLogMessage(CUPSD_LOG_ERROR
,
8577 "Unable to open print file for job ticket - %s",
8583 * Skip the first line...
8586 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8588 cupsdLogMessage(CUPSD_LOG_ERROR
,
8589 "Unable to read from print file for job ticket - %s",
8595 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8598 * Not a DSC-compliant file, so no job ticket info will be available...
8606 * Read job ticket info from the file...
8612 while (cupsFileGets(fp
, line
, sizeof(line
)))
8615 * Stop at the first non-ticket line...
8618 if (strncmp(line
, "%cupsJobTicket:", 15))
8622 * Add the options to the option array...
8625 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8629 * Done with the file; see if we have any options...
8634 if (num_options
== 0)
8638 * OK, convert the options to an attribute list, and apply them to
8643 cupsEncodeOptions(ticket
, num_options
, options
);
8646 * See what the user wants to change.
8649 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8651 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8654 if (!strcmp(attr
->name
, "job-originating-host-name") ||
8655 !strcmp(attr
->name
, "job-originating-user-name") ||
8656 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8657 !strcmp(attr
->name
, "job-k-octets") ||
8658 !strcmp(attr
->name
, "job-id") ||
8659 !strncmp(attr
->name
, "job-state", 9) ||
8660 !strncmp(attr
->name
, "time-at-", 8))
8661 continue; /* Read-only attrs */
8663 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8664 IPP_TAG_ZERO
)) != NULL
)
8667 * Some other value; first free the old value...
8670 if (con
->request
->attrs
== attr2
)
8672 con
->request
->attrs
= attr2
->next
;
8677 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8678 if (prev2
->next
== attr2
)
8680 prev2
->next
= attr2
->next
;
8685 if (con
->request
->last
== attr2
)
8686 con
->request
->last
= prev2
;
8688 _ippFreeAttr(attr2
);
8692 * Add new option by copying it...
8695 copy_attribute(con
->request
, attr
, 0);
8699 * Then free the attribute list and option array...
8703 cupsFreeOptions(num_options
, options
);
8708 * 'reject_jobs()' - Reject print jobs to a printer.
8712 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8713 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8715 http_status_t status
; /* Policy status */
8716 cups_ptype_t dtype
; /* Destination type (printer/class) */
8717 cupsd_printer_t
*printer
; /* Printer data */
8718 ipp_attribute_t
*attr
; /* printer-state-message text */
8721 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8722 con
->http
.fd
, uri
->values
[0].string
.text
);
8725 * Is the destination valid?
8728 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8734 send_ipp_status(con
, IPP_NOT_FOUND
,
8735 _("The printer or class was not found."));
8743 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8745 send_http_error(con
, status
, printer
);
8750 * Reject jobs sent to the printer...
8753 printer
->accepting
= 0;
8755 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8756 IPP_TAG_TEXT
)) == NULL
)
8757 strcpy(printer
->state_message
, "Rejecting Jobs");
8759 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8760 sizeof(printer
->state_message
));
8762 cupsdAddPrinterHistory(printer
);
8764 if (dtype
& CUPS_PRINTER_CLASS
)
8766 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8768 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8769 printer
->name
, get_username(con
));
8773 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8775 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8776 printer
->name
, get_username(con
));
8780 * Everything was ok, so return OK status...
8783 con
->response
->request
.status
.status_code
= IPP_OK
;
8788 * 'release_job()' - Release a held print job.
8792 release_job(cupsd_client_t
*con
, /* I - Client connection */
8793 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8795 ipp_attribute_t
*attr
; /* Current attribute */
8796 int jobid
; /* Job ID */
8797 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8798 username
[HTTP_MAX_URI
], /* Username portion of URI */
8799 host
[HTTP_MAX_URI
], /* Host portion of URI */
8800 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8801 int port
; /* Port portion of URI */
8802 cupsd_job_t
*job
; /* Job information */
8805 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8806 con
->http
.fd
, uri
->values
[0].string
.text
);
8809 * See if we have a job URI or a printer URI...
8812 if (!strcmp(uri
->name
, "printer-uri"))
8815 * Got a printer URI; see if we also have a job-id attribute...
8818 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8819 IPP_TAG_INTEGER
)) == NULL
)
8821 send_ipp_status(con
, IPP_BAD_REQUEST
,
8822 _("Got a printer-uri attribute but no job-id!"));
8826 jobid
= attr
->values
[0].integer
;
8831 * Got a job URI; parse it to get the job ID...
8834 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8835 sizeof(method
), username
, sizeof(username
), host
,
8836 sizeof(host
), &port
, resource
, sizeof(resource
));
8838 if (strncmp(resource
, "/jobs/", 6))
8844 send_ipp_status(con
, IPP_BAD_REQUEST
,
8845 _("Bad job-uri attribute \"%s\"!"),
8846 uri
->values
[0].string
.text
);
8850 jobid
= atoi(resource
+ 6);
8854 * See if the job exists...
8857 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8860 * Nope - return a "not found" error...
8863 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8868 * See if job is "held"...
8871 if (job
->state_value
!= IPP_JOB_HELD
)
8874 * Nope - return a "not possible" error...
8877 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
8882 * See if the job is owned by the requesting user...
8885 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8887 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8892 * Reset the job-hold-until value to "no-hold"...
8895 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8896 IPP_TAG_KEYWORD
)) == NULL
)
8897 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8901 _cupsStrFree(attr
->values
[0].string
.text
);
8903 attr
->value_tag
= IPP_TAG_KEYWORD
;
8904 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
8906 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
8907 "Job job-hold-until value changed by user.");
8911 * Release the job and return...
8914 cupsdReleaseJob(job
);
8916 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
8917 "Job released by user.");
8919 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
8921 con
->response
->request
.status
.status_code
= IPP_OK
;
8928 * 'renew_subscription()' - Renew an existing subscription...
8933 cupsd_client_t
*con
, /* I - Client connection */
8934 int sub_id
) /* I - Subscription ID */
8936 http_status_t status
; /* Policy status */
8937 cupsd_subscription_t
*sub
; /* Subscription */
8938 ipp_attribute_t
*lease
; /* notify-lease-duration */
8941 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8942 "renew_subscription(con=%p[%d], sub_id=%d)",
8943 con
, con
->http
.fd
, sub_id
);
8946 * Is the subscription ID valid?
8949 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8952 * Bad subscription ID...
8955 send_ipp_status(con
, IPP_NOT_FOUND
,
8956 _("notify-subscription-id %d no good!"), sub_id
);
8963 * Job subscriptions cannot be renewed...
8966 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8967 _("Job subscriptions cannot be renewed!"));
8975 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8977 con
, sub
->owner
)) != HTTP_OK
)
8979 send_http_error(con
, status
, sub
->dest
);
8984 * Renew the subscription...
8987 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8990 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8992 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8994 cupsdLogMessage(CUPSD_LOG_INFO
,
8995 "renew_subscription: Limiting notify-lease-duration to "
8998 sub
->lease
= MaxLeaseDuration
;
9001 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
9003 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
9005 con
->response
->request
.status
.status_code
= IPP_OK
;
9007 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
9008 "notify-lease-duration", sub
->lease
);
9013 * 'restart_job()' - Restart an old print job.
9017 restart_job(cupsd_client_t
*con
, /* I - Client connection */
9018 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
9020 ipp_attribute_t
*attr
; /* Current attribute */
9021 int jobid
; /* Job ID */
9022 char method
[HTTP_MAX_URI
], /* Method portion of URI */
9023 username
[HTTP_MAX_URI
], /* Username portion of URI */
9024 host
[HTTP_MAX_URI
], /* Host portion of URI */
9025 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9026 int port
; /* Port portion of URI */
9027 cupsd_job_t
*job
; /* Job information */
9030 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
9031 con
->http
.fd
, uri
->values
[0].string
.text
);
9034 * See if we have a job URI or a printer URI...
9037 if (!strcmp(uri
->name
, "printer-uri"))
9040 * Got a printer URI; see if we also have a job-id attribute...
9043 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9044 IPP_TAG_INTEGER
)) == NULL
)
9046 send_ipp_status(con
, IPP_BAD_REQUEST
,
9047 _("Got a printer-uri attribute but no job-id!"));
9051 jobid
= attr
->values
[0].integer
;
9056 * Got a job URI; parse it to get the job ID...
9059 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9060 sizeof(method
), username
, sizeof(username
), host
,
9061 sizeof(host
), &port
, resource
, sizeof(resource
));
9063 if (strncmp(resource
, "/jobs/", 6))
9069 send_ipp_status(con
, IPP_BAD_REQUEST
,
9070 _("Bad job-uri attribute \"%s\"!"),
9071 uri
->values
[0].string
.text
);
9075 jobid
= atoi(resource
+ 6);
9079 * See if the job exists...
9082 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9085 * Nope - return a "not found" error...
9088 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9093 * See if job is in any of the "completed" states...
9096 if (job
->state_value
<= IPP_JOB_PROCESSING
)
9099 * Nope - return a "not possible" error...
9102 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
9108 * See if we have retained the job files...
9113 if (!job
->attrs
|| job
->num_files
== 0)
9116 * Nope - return a "not possible" error...
9119 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9120 _("Job #%d cannot be restarted - no files!"), jobid
);
9125 * See if the job is owned by the requesting user...
9128 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9130 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9135 * Restart the job and return...
9138 cupsdRestartJob(job
);
9140 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
9142 con
->response
->request
.status
.status_code
= IPP_OK
;
9147 * 'save_auth_info()' - Save authentication information for a job.
9152 cupsd_client_t
*con
, /* I - Client connection */
9153 cupsd_job_t
*job
, /* I - Job */
9154 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
9156 int i
; /* Looping var */
9157 char filename
[1024]; /* Job authentication filename */
9158 cups_file_t
*fp
; /* Job authentication file */
9159 char line
[2048]; /* Line for file */
9160 cupsd_printer_t
*dest
; /* Destination printer/class */
9164 * This function saves the in-memory authentication information for
9165 * a job so that it can be used to authenticate with a remote host.
9166 * The information is stored in a file that is readable only by the
9167 * root user. The fields are Base-64 encoded, each on a separate line,
9168 * followed by random number (up to 1024) of newlines to limit the
9169 * amount of information that is exposed.
9171 * Because of the potential for exposing of authentication information,
9172 * this functionality is only enabled when running cupsd as root.
9174 * This caching only works for the Basic and BasicDigest authentication
9175 * types. Digest authentication cannot be cached this way, and in
9176 * the future Kerberos authentication may make all of this obsolete.
9178 * Authentication information is saved whenever an authenticated
9179 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9182 * This information is deleted after a job is completed or canceled,
9183 * so reprints may require subsequent re-authentication.
9189 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9193 * Create the authentication file and change permissions...
9196 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9197 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9199 cupsdLogMessage(CUPSD_LOG_ERROR
,
9200 "Unable to save authentication info to \"%s\" - %s",
9201 filename
, strerror(errno
));
9205 fchown(cupsFileNumber(fp
), 0, 0);
9206 fchmod(cupsFileNumber(fp
), 0400);
9208 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9211 * Write 1 to 3 auth values...
9214 cupsdClearString(&job
->auth_username
);
9215 cupsdClearString(&job
->auth_domain
);
9216 cupsdClearString(&job
->auth_password
);
9218 for (i
= 0; i
< auth_info
->num_values
; i
++)
9220 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
9221 strlen(auth_info
->values
[i
].string
.text
));
9222 cupsFilePrintf(fp
, "%s\n", line
);
9224 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9225 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s",
9226 auth_info
->values
[i
].string
.text
);
9227 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9228 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s",
9229 auth_info
->values
[i
].string
.text
);
9230 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9231 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s",
9232 auth_info
->values
[i
].string
.text
);
9235 else if (con
->username
[0])
9238 * Write the authenticated username...
9241 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
9242 cupsFilePrintf(fp
, "%s\n", line
);
9244 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", con
->username
);
9245 cupsdClearString(&job
->auth_domain
);
9248 * Write the authenticated password...
9251 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
9252 cupsFilePrintf(fp
, "%s\n", line
);
9254 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", con
->password
);
9258 * Write a random number of newlines to the end of the file...
9261 for (i
= (rand() % 1024); i
>= 0; i
--)
9262 cupsFilePutChar(fp
, '\n');
9265 * Close the file and return...
9270 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9271 if (con
->gss_have_creds
)
9272 save_krb5_creds(con
, job
);
9273 else if (job
->ccname
)
9274 cupsdClearString(&(job
->ccname
));
9275 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9279 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9281 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
9285 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
9286 cupsd_job_t
*job
) /* I - Job */
9288 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
9289 cupsdLogMessage(CUPSD_LOG_INFO
,
9290 "Sorry, your version of Kerberos does not support delegated "
9295 krb5_error_code error
; /* Kerberos error code */
9296 OM_uint32 major_status
, /* Major status code */
9297 minor_status
; /* Minor status code */
9298 krb5_principal principal
; /* Kerberos principal */
9303 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
9307 if (krb5_init_context
== NULL
)
9309 # endif /* __APPLE__ */
9311 if (!KerberosInitialized
)
9314 * Setup a Kerberos context for the scheduler to use...
9317 KerberosInitialized
= 1;
9319 if (krb5_init_context(&KerberosContext
))
9321 KerberosContext
= NULL
;
9323 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize Kerberos context");
9329 * We MUST create a file-based cache because memory-based caches are
9330 * only valid for the current process/address space.
9332 * Due to various bugs/features in different versions of Kerberos, we
9333 * need either the krb5_cc_new_unique() function or Heimdal's version
9334 * of krb5_cc_gen_new() to create a new FILE: credential cache that
9335 * can be passed to the backend. These functions create a temporary
9336 * file (typically in /tmp) containing the cached credentials, which
9337 * are removed when we have successfully printed a job.
9340 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
9341 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
9342 &(job
->ccache
))) != 0)
9343 # else /* HAVE_HEIMDAL */
9344 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
9345 &(job
->ccache
))) != 0)
9346 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
9348 cupsdLogMessage(CUPSD_LOG_ERROR
,
9349 "Unable to create new credentials cache (%d/%s)",
9350 error
, strerror(errno
));
9355 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
9357 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to parse kerberos username (%d/%s)",
9358 error
, strerror(errno
));
9359 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9364 if ((error
= krb5_cc_initialize(KerberosContext
, job
->ccache
, principal
)))
9366 cupsdLogMessage(CUPSD_LOG_ERROR
,
9367 "Unable to initialize credentials cache (%d/%s)", error
,
9369 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9370 krb5_free_principal(KerberosContext
, principal
);
9375 krb5_free_principal(KerberosContext
, principal
);
9378 * Copy the user's credentials to the new cache file...
9381 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
9384 if (GSS_ERROR(major_status
))
9386 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
9387 "Unable to import client credentials cache");
9388 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9394 * Add the KRB5CCNAME environment variable to the job so that the
9395 * backend can use the credentials when printing.
9398 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
9399 krb5_cc_get_name(KerberosContext
, job
->ccache
));
9401 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "save_krb5_creds: %s", job
->ccname
);
9402 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
9404 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9408 * 'send_document()' - Send a file to a printer or class.
9412 send_document(cupsd_client_t
*con
, /* I - Client connection */
9413 ipp_attribute_t
*uri
) /* I - Printer URI */
9415 ipp_attribute_t
*attr
; /* Current attribute */
9416 ipp_attribute_t
*format
; /* Request's document-format attribute */
9417 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9418 const char *default_format
;/* document-format-default value */
9419 int jobid
; /* Job ID number */
9420 cupsd_job_t
*job
; /* Current job */
9421 char job_uri
[HTTP_MAX_URI
],
9423 method
[HTTP_MAX_URI
],
9424 /* Method portion of URI */
9425 username
[HTTP_MAX_URI
],
9426 /* Username portion of URI */
9428 /* Host portion of URI */
9429 resource
[HTTP_MAX_URI
];
9430 /* Resource portion of URI */
9431 int port
; /* Port portion of URI */
9432 mime_type_t
*filetype
; /* Type of file */
9433 char super
[MIME_MAX_SUPER
],
9434 /* Supertype of file */
9435 type
[MIME_MAX_TYPE
],
9436 /* Subtype of file */
9437 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9438 /* Textual name of mime type */
9439 char filename
[1024]; /* Job filename */
9440 cupsd_printer_t
*printer
; /* Current printer */
9441 struct stat fileinfo
; /* File information */
9442 int kbytes
; /* Size of file */
9443 int compression
; /* Type of compression */
9444 int start_job
; /* Start the job? */
9447 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9448 con
->http
.fd
, uri
->values
[0].string
.text
);
9451 * See if we have a job URI or a printer URI...
9454 if (!strcmp(uri
->name
, "printer-uri"))
9457 * Got a printer URI; see if we also have a job-id attribute...
9460 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9461 IPP_TAG_INTEGER
)) == NULL
)
9463 send_ipp_status(con
, IPP_BAD_REQUEST
,
9464 _("Got a printer-uri attribute but no job-id!"));
9468 jobid
= attr
->values
[0].integer
;
9473 * Got a job URI; parse it to get the job ID...
9476 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9477 sizeof(method
), username
, sizeof(username
), host
,
9478 sizeof(host
), &port
, resource
, sizeof(resource
));
9480 if (strncmp(resource
, "/jobs/", 6))
9486 send_ipp_status(con
, IPP_BAD_REQUEST
,
9487 _("Bad job-uri attribute \"%s\"!"),
9488 uri
->values
[0].string
.text
);
9492 jobid
= atoi(resource
+ 6);
9496 * See if the job exists...
9499 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9502 * Nope - return a "not found" error...
9505 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9509 printer
= cupsdFindDest(job
->dest
);
9512 * See if the job is owned by the requesting user...
9515 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9517 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9522 * OK, see if the client is sending the document compressed - CUPS
9523 * only supports "none" and "gzip".
9526 compression
= CUPS_FILE_NONE
;
9528 if ((attr
= ippFindAttribute(con
->request
, "compression",
9529 IPP_TAG_KEYWORD
)) != NULL
)
9531 if (strcmp(attr
->values
[0].string
.text
, "none")
9533 && strcmp(attr
->values
[0].string
.text
, "gzip")
9534 #endif /* HAVE_LIBZ */
9537 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
9538 attr
->values
[0].string
.text
);
9539 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9540 "compression", NULL
, attr
->values
[0].string
.text
);
9545 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9546 compression
= CUPS_FILE_GZIP
;
9547 #endif /* HAVE_LIBZ */
9551 * Do we have a file to print?
9556 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
9561 * Is it a format we support?
9564 if ((format
= ippFindAttribute(con
->request
, "document-format",
9565 IPP_TAG_MIMETYPE
)) != NULL
)
9568 * Grab format from client...
9571 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
9574 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9575 format
->values
[0].string
.text
);
9579 else if ((default_format
= cupsGetOption("document-format",
9580 printer
->num_options
,
9581 printer
->options
)) != NULL
)
9584 * Use default document format...
9587 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
9589 send_ipp_status(con
, IPP_BAD_REQUEST
,
9590 _("Could not scan type \"%s\"!"),
9598 * No document format attribute? Auto-type it!
9601 strcpy(super
, "application");
9602 strcpy(type
, "octet-stream");
9605 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9608 * Auto-type the file...
9611 ipp_attribute_t
*doc_name
; /* document-name attribute */
9614 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9616 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9617 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9618 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9622 filetype
= mimeType(MimeDatabase
, super
, type
);
9624 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9625 filetype
->super
, filetype
->type
);
9628 filetype
= mimeType(MimeDatabase
, super
, type
);
9633 * Replace the document-format attribute value with the auto-typed or
9637 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9640 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9641 IPP_TAG_MIMETYPE
)) != NULL
)
9643 _cupsStrFree(jformat
->values
[0].string
.text
);
9645 jformat
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
9648 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9649 "document-format", NULL
, mimetype
);
9653 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9654 _("Unsupported format \'%s/%s\'!"), super
, type
);
9655 cupsdLogMessage(CUPSD_LOG_INFO
,
9656 "Hint: Do you have the raw file printing rules enabled?");
9659 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9660 "document-format", NULL
, format
->values
[0].string
.text
);
9665 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9667 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9670 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9671 _("Unsupported format \'%s\'!"), mimetype
);
9673 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9674 "document-format", NULL
, mimetype
);
9680 * Add the file to the job...
9685 if (add_file(con
, job
, filetype
, compression
))
9688 if (stat(con
->filename
, &fileinfo
))
9691 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9693 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9695 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
9696 IPP_TAG_INTEGER
)) != NULL
)
9697 attr
->values
[0].integer
+= kbytes
;
9699 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
9701 rename(con
->filename
, filename
);
9703 cupsdClearString(&con
->filename
);
9705 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9706 filetype
->super
, filetype
->type
, job
->username
);
9709 * Start the job if this is the last document...
9712 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9713 IPP_TAG_BOOLEAN
)) != NULL
&&
9714 attr
->values
[0].boolean
)
9717 * See if we need to add the ending sheet...
9720 if (cupsdTimeoutJob(job
))
9723 if (job
->state_value
== IPP_JOB_STOPPED
)
9725 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9726 job
->state_value
= IPP_JOB_PENDING
;
9728 else if (job
->state_value
== IPP_JOB_HELD
)
9730 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9731 IPP_TAG_KEYWORD
)) == NULL
)
9732 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9734 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9736 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9737 job
->state_value
= IPP_JOB_PENDING
;
9742 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9748 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9749 IPP_TAG_KEYWORD
)) == NULL
)
9750 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9752 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9754 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9755 job
->state_value
= IPP_JOB_HELD
;
9756 job
->hold_until
= time(NULL
) + 60;
9759 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9766 * Fill in the response info...
9769 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
9772 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9775 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9777 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9779 add_job_state_reasons(con
, job
);
9781 con
->response
->request
.status
.status_code
= IPP_OK
;
9784 * Start the job if necessary...
9793 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9798 cupsd_client_t
*con
, /* I - Client connection */
9799 http_status_t status
, /* I - HTTP status code */
9800 cupsd_printer_t
*printer
) /* I - Printer, if any */
9802 ipp_attribute_t
*uri
; /* Request URI, if any */
9805 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
9806 IPP_TAG_URI
)) == NULL
)
9807 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
9809 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
9810 "Returning HTTP %s for %s (%s) from %s",
9812 ippOpString(con
->request
->request
.op
.operation_id
),
9813 uri
? uri
->values
[0].string
.text
: "no URI",
9814 con
->http
.hostname
);
9818 int auth_type
; /* Type of authentication required */
9821 auth_type
= CUPSD_AUTH_NONE
;
9823 if (status
== HTTP_UNAUTHORIZED
&&
9824 printer
->num_auth_info_required
> 0 &&
9825 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
9827 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
9828 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
9829 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
9832 * Creating and authenticating jobs requires Kerberos...
9835 auth_type
= CUPSD_AUTH_NEGOTIATE
;
9840 * Use policy/location-defined authentication requirements...
9843 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9844 cupsd_location_t
*auth
; /* Pointer to authentication element */
9847 if (printer
->type
& CUPS_PRINTER_CLASS
)
9848 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9850 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9852 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9853 auth
->type
== CUPSD_AUTH_NONE
)
9854 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
9856 con
->request
->request
.op
.operation_id
:
9861 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
9862 auth_type
= DefaultAuthType
;
9864 auth_type
= auth
->type
;
9868 cupsdSendError(con
, status
, auth_type
);
9871 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
9873 ippDelete(con
->response
);
9874 con
->response
= NULL
;
9881 * 'send_ipp_status()' - Send a status back to the IPP client.
9885 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9886 ipp_status_t status
, /* I - IPP status code */
9887 const char *message
,/* I - Status message */
9888 ...) /* I - Additional args as needed */
9890 va_list ap
; /* Pointer to additional args */
9891 char formatted
[1024]; /* Formatted errror message */
9894 va_start(ap
, message
);
9895 vsnprintf(formatted
, sizeof(formatted
),
9896 _cupsLangString(con
->language
, message
), ap
);
9899 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9900 ippOpString(con
->request
->request
.op
.operation_id
),
9901 ippErrorString(status
), formatted
);
9903 con
->response
->request
.status
.status_code
= status
;
9905 if (ippFindAttribute(con
->response
, "attributes-charset",
9906 IPP_TAG_ZERO
) == NULL
)
9907 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9908 "attributes-charset", NULL
, DefaultCharset
);
9910 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9911 IPP_TAG_ZERO
) == NULL
)
9912 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9913 "attributes-natural-language", NULL
, DefaultLanguage
);
9915 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9916 "status-message", NULL
, formatted
);
9921 * 'set_default()' - Set the default destination...
9925 set_default(cupsd_client_t
*con
, /* I - Client connection */
9926 ipp_attribute_t
*uri
) /* I - Printer URI */
9928 http_status_t status
; /* Policy status */
9929 cups_ptype_t dtype
; /* Destination type (printer/class) */
9930 cupsd_printer_t
*printer
, /* Printer */
9931 *oldprinter
; /* Old default printer */
9934 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9935 con
->http
.fd
, uri
->values
[0].string
.text
);
9938 * Is the destination valid?
9941 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9947 send_ipp_status(con
, IPP_NOT_FOUND
,
9948 _("The printer or class was not found."));
9956 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9958 send_http_error(con
, status
, NULL
);
9963 * Set it as the default...
9966 oldprinter
= DefaultPrinter
;
9967 DefaultPrinter
= printer
;
9970 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
9971 "%s is no longer the default printer.", oldprinter
->name
);
9973 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
9974 "%s is now the default printer.", printer
->name
);
9976 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
9977 CUPSD_DIRTY_REMOTE
| CUPSD_DIRTY_PRINTCAP
);
9979 cupsdLogMessage(CUPSD_LOG_INFO
,
9980 "Default destination set to \"%s\" by \"%s\".",
9981 printer
->name
, get_username(con
));
9984 * Everything was ok, so return OK status...
9987 con
->response
->request
.status
.status_code
= IPP_OK
;
9992 * 'set_job_attrs()' - Set job attributes.
9996 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9997 ipp_attribute_t
*uri
) /* I - Job URI */
9999 ipp_attribute_t
*attr
, /* Current attribute */
10000 *attr2
; /* Job attribute */
10001 int jobid
; /* Job ID */
10002 cupsd_job_t
*job
; /* Current job */
10003 char method
[HTTP_MAX_URI
],
10004 /* Method portion of URI */
10005 username
[HTTP_MAX_URI
],
10006 /* Username portion of URI */
10007 host
[HTTP_MAX_URI
],
10008 /* Host portion of URI */
10009 resource
[HTTP_MAX_URI
];
10010 /* Resource portion of URI */
10011 int port
; /* Port portion of URI */
10012 int event
; /* Events? */
10013 int check_jobs
; /* Check jobs? */
10016 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
10017 con
->http
.fd
, uri
->values
[0].string
.text
);
10020 * Start with "everything is OK" status...
10023 con
->response
->request
.status
.status_code
= IPP_OK
;
10026 * See if we have a job URI or a printer URI...
10029 if (!strcmp(uri
->name
, "printer-uri"))
10032 * Got a printer URI; see if we also have a job-id attribute...
10035 if ((attr
= ippFindAttribute(con
->request
, "job-id",
10036 IPP_TAG_INTEGER
)) == NULL
)
10038 send_ipp_status(con
, IPP_BAD_REQUEST
,
10039 _("Got a printer-uri attribute but no job-id!"));
10043 jobid
= attr
->values
[0].integer
;
10048 * Got a job URI; parse it to get the job ID...
10051 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
10052 sizeof(method
), username
, sizeof(username
), host
,
10053 sizeof(host
), &port
, resource
, sizeof(resource
));
10055 if (strncmp(resource
, "/jobs/", 6))
10061 send_ipp_status(con
, IPP_BAD_REQUEST
,
10062 _("Bad job-uri attribute \"%s\"!"),
10063 uri
->values
[0].string
.text
);
10067 jobid
= atoi(resource
+ 6);
10071 * See if the job exists...
10074 if ((job
= cupsdFindJob(jobid
)) == NULL
)
10077 * Nope - return a "not found" error...
10080 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
10085 * See if the job has been completed...
10088 if (job
->state_value
> IPP_JOB_STOPPED
)
10091 * Return a "not-possible" error...
10094 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10095 _("Job #%d is finished and cannot be altered!"), jobid
);
10100 * See if the job is owned by the requesting user...
10103 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
10105 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
10110 * See what the user wants to change.
10118 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10120 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
10123 if (!strcmp(attr
->name
, "attributes-charset") ||
10124 !strcmp(attr
->name
, "attributes-natural-language") ||
10125 !strcmp(attr
->name
, "document-compression") ||
10126 !strcmp(attr
->name
, "document-format") ||
10127 !strcmp(attr
->name
, "job-detailed-status-messages") ||
10128 !strcmp(attr
->name
, "job-document-access-errors") ||
10129 !strcmp(attr
->name
, "job-id") ||
10130 !strcmp(attr
->name
, "job-impressions-completed") ||
10131 !strcmp(attr
->name
, "job-k-octets") ||
10132 !strcmp(attr
->name
, "job-originating-host-name") ||
10133 !strcmp(attr
->name
, "job-originating-user-name") ||
10134 !strcmp(attr
->name
, "job-printer-up-time") ||
10135 !strcmp(attr
->name
, "job-printer-uri") ||
10136 !strcmp(attr
->name
, "job-sheets") ||
10137 !strcmp(attr
->name
, "job-state-message") ||
10138 !strcmp(attr
->name
, "job-state-reasons") ||
10139 !strcmp(attr
->name
, "job-uri") ||
10140 !strcmp(attr
->name
, "number-of-documents") ||
10141 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
10142 !strcmp(attr
->name
, "output-device-assigned") ||
10143 !strncmp(attr
->name
, "date-time-at-", 13) ||
10144 !strncmp(attr
->name
, "job-k-octets", 12) ||
10145 !strncmp(attr
->name
, "job-media-sheets", 16) ||
10146 !strncmp(attr
->name
, "time-at-", 8))
10152 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
10153 _("%s cannot be changed."), attr
->name
);
10155 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10156 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10161 if (!strcmp(attr
->name
, "job-priority"))
10164 * Change the job priority...
10167 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10169 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
10171 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10172 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10174 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
10176 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10177 _("Job is completed and cannot be changed."));
10180 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10182 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
10183 attr
->values
[0].integer
);
10184 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
10187 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
10188 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
10191 else if (!strcmp(attr
->name
, "job-state"))
10194 * Change the job state...
10197 if (attr
->value_tag
!= IPP_TAG_ENUM
)
10199 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
10201 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10202 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10206 switch (attr
->values
[0].integer
)
10208 case IPP_JOB_PENDING
:
10209 case IPP_JOB_HELD
:
10210 if (job
->state_value
> IPP_JOB_HELD
)
10212 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10213 _("Job state cannot be changed."));
10216 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10218 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10219 attr
->values
[0].integer
);
10221 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
10222 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
10224 event
|= CUPSD_EVENT_JOB_STATE
;
10229 case IPP_JOB_PROCESSING
:
10230 case IPP_JOB_STOPPED
:
10231 if (job
->state_value
!= attr
->values
[0].integer
)
10233 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10234 _("Job state cannot be changed."));
10239 case IPP_JOB_CANCELED
:
10240 case IPP_JOB_ABORTED
:
10241 case IPP_JOB_COMPLETED
:
10242 if (job
->state_value
> IPP_JOB_PROCESSING
)
10244 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10245 _("Job state cannot be changed."));
10248 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10250 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10251 attr
->values
[0].integer
);
10252 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
10260 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10262 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10263 IPP_TAG_ZERO
)) != NULL
)
10266 * Some other value; first free the old value...
10269 if (job
->attrs
->prev
)
10270 job
->attrs
->prev
->next
= attr2
->next
;
10272 job
->attrs
->attrs
= attr2
->next
;
10274 if (job
->attrs
->last
== attr2
)
10275 job
->attrs
->last
= job
->attrs
->prev
;
10277 _ippFreeAttr(attr2
);
10280 * Then copy the attribute...
10283 copy_attribute(job
->attrs
, attr
, 0);
10286 * See if the job-name or job-hold-until is being changed.
10289 if (!strcmp(attr
->name
, "job-hold-until"))
10291 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10292 attr
->values
[0].string
.text
);
10293 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
10295 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10296 cupsdReleaseJob(job
);
10301 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10304 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10307 * Delete the attribute...
10310 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10311 IPP_TAG_ZERO
)) != NULL
)
10313 if (job
->attrs
->prev
)
10314 job
->attrs
->prev
->next
= attr2
->next
;
10316 job
->attrs
->attrs
= attr2
->next
;
10318 if (attr2
== job
->attrs
->last
)
10319 job
->attrs
->last
= job
->attrs
->prev
;
10321 _ippFreeAttr(attr2
);
10323 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10329 * Add new option by copying it...
10332 copy_attribute(job
->attrs
, attr
, 0);
10334 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10343 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10346 * Send events as needed...
10349 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10350 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10351 cupsdFindDest(job
->dest
), job
,
10352 "Job priority changed by user.");
10354 if (event
& CUPSD_EVENT_JOB_STATE
)
10355 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10356 job
->state_value
== IPP_JOB_HELD
?
10357 "Job held by user." : "Job restarted by user.");
10359 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10360 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10361 "Job options changed by user.");
10364 * Start jobs if possible...
10373 * 'set_printer_defaults()' - Set printer default options from a request.
10377 set_printer_defaults(
10378 cupsd_client_t
*con
, /* I - Client connection */
10379 cupsd_printer_t
*printer
) /* I - Printer */
10381 int i
; /* Looping var */
10382 ipp_attribute_t
*attr
; /* Current attribute */
10383 int namelen
; /* Length of attribute name */
10384 char name
[256], /* New attribute name */
10385 value
[256]; /* String version of integer attrs */
10388 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10391 * Skip non-printer attributes...
10394 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10397 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10399 if (!strcmp(attr
->name
, "job-sheets-default"))
10402 * Only allow keywords and names...
10405 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10409 * Only allow job-sheets-default to be set when running without a
10410 * system high classification level...
10413 if (Classification
)
10416 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10418 if (attr
->num_values
> 1)
10419 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10421 cupsdSetString(&printer
->job_sheets
[1], "none");
10423 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10425 cupsdFreePrinterUsers(printer
);
10427 printer
->deny_users
= 0;
10429 if (attr
->value_tag
== IPP_TAG_NAME
&&
10430 (attr
->num_values
> 1 ||
10431 strcmp(attr
->values
[0].string
.text
, "all")))
10433 for (i
= 0; i
< attr
->num_values
; i
++)
10434 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
10437 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10439 cupsdFreePrinterUsers(printer
);
10441 printer
->deny_users
= 1;
10443 if (attr
->value_tag
== IPP_TAG_NAME
&&
10444 (attr
->num_values
> 1 ||
10445 strcmp(attr
->values
[0].string
.text
, "none")))
10447 for (i
= 0; i
< attr
->num_values
; i
++)
10448 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
10451 else if (!strcmp(attr
->name
, "job-quota-period"))
10453 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10456 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10457 attr
->values
[0].integer
);
10458 cupsdFreeQuotas(printer
);
10460 printer
->quota_period
= attr
->values
[0].integer
;
10462 else if (!strcmp(attr
->name
, "job-k-limit"))
10464 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10467 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10468 attr
->values
[0].integer
);
10469 cupsdFreeQuotas(printer
);
10471 printer
->k_limit
= attr
->values
[0].integer
;
10473 else if (!strcmp(attr
->name
, "job-page-limit"))
10475 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10478 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10479 attr
->values
[0].integer
);
10480 cupsdFreeQuotas(printer
);
10482 printer
->page_limit
= attr
->values
[0].integer
;
10484 else if (!strcmp(attr
->name
, "printer-op-policy"))
10486 cupsd_policy_t
*p
; /* Policy */
10489 if (attr
->value_tag
!= IPP_TAG_NAME
)
10492 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10494 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10495 "Setting printer-op-policy to \"%s\"...",
10496 attr
->values
[0].string
.text
);
10497 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10498 printer
->op_policy_ptr
= p
;
10502 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10503 _("Unknown printer-op-policy \"%s\"."),
10504 attr
->values
[0].string
.text
);
10508 else if (!strcmp(attr
->name
, "printer-error-policy"))
10510 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10513 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10514 strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10515 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10516 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
10518 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10519 _("Unknown printer-error-policy \"%s\"."),
10520 attr
->values
[0].string
.text
);
10524 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10525 "Setting printer-error-policy to \"%s\"...",
10526 attr
->values
[0].string
.text
);
10527 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10531 * Skip any other non-default attributes...
10534 namelen
= strlen(attr
->name
);
10535 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10536 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10540 * OK, anything else must be a user-defined default...
10543 strlcpy(name
, attr
->name
, sizeof(name
));
10544 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10546 switch (attr
->value_tag
)
10548 case IPP_TAG_DELETEATTR
:
10549 printer
->num_options
= cupsRemoveOption(name
,
10550 printer
->num_options
,
10551 &(printer
->options
));
10552 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10553 "Deleting %s", attr
->name
);
10556 case IPP_TAG_NAME
:
10557 case IPP_TAG_KEYWORD
:
10559 printer
->num_options
= cupsAddOption(name
,
10560 attr
->values
[0].string
.text
,
10561 printer
->num_options
,
10562 &(printer
->options
));
10563 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10564 "Setting %s to \"%s\"...", attr
->name
,
10565 attr
->values
[0].string
.text
);
10568 case IPP_TAG_BOOLEAN
:
10569 printer
->num_options
= cupsAddOption(name
,
10570 attr
->values
[0].boolean
?
10572 printer
->num_options
,
10573 &(printer
->options
));
10574 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10575 "Setting %s to %s...", attr
->name
,
10576 attr
->values
[0].boolean
? "true" : "false");
10579 case IPP_TAG_INTEGER
:
10580 case IPP_TAG_ENUM
:
10581 sprintf(value
, "%d", attr
->values
[0].integer
);
10582 printer
->num_options
= cupsAddOption(name
, value
,
10583 printer
->num_options
,
10584 &(printer
->options
));
10585 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10586 "Setting %s to %s...", attr
->name
, value
);
10589 case IPP_TAG_RANGE
:
10590 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10591 attr
->values
[0].range
.upper
);
10592 printer
->num_options
= cupsAddOption(name
, value
,
10593 printer
->num_options
,
10594 &(printer
->options
));
10595 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10596 "Setting %s to %s...", attr
->name
, value
);
10599 case IPP_TAG_RESOLUTION
:
10600 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10601 attr
->values
[0].resolution
.yres
,
10602 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10604 printer
->num_options
= cupsAddOption(name
, value
,
10605 printer
->num_options
,
10606 &(printer
->options
));
10607 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10608 "Setting %s to %s...", attr
->name
, value
);
10612 /* Do nothing for other values */
10620 * 'start_printer()' - Start a printer.
10624 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10625 ipp_attribute_t
*uri
) /* I - Printer URI */
10627 http_status_t status
; /* Policy status */
10628 cups_ptype_t dtype
; /* Destination type (printer/class) */
10629 cupsd_printer_t
*printer
; /* Printer data */
10632 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10633 con
->http
.fd
, uri
->values
[0].string
.text
);
10636 * Is the destination valid?
10639 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10645 send_ipp_status(con
, IPP_NOT_FOUND
,
10646 _("The printer or class was not found."));
10654 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10656 send_http_error(con
, status
, printer
);
10661 * Start the printer...
10664 printer
->state_message
[0] = '\0';
10666 cupsdStartPrinter(printer
, 1);
10668 if (dtype
& CUPS_PRINTER_CLASS
)
10669 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10670 printer
->name
, get_username(con
));
10672 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10673 printer
->name
, get_username(con
));
10678 * Everything was ok, so return OK status...
10681 con
->response
->request
.status
.status_code
= IPP_OK
;
10686 * 'stop_printer()' - Stop a printer.
10690 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10691 ipp_attribute_t
*uri
) /* I - Printer URI */
10693 http_status_t status
; /* Policy status */
10694 cups_ptype_t dtype
; /* Destination type (printer/class) */
10695 cupsd_printer_t
*printer
; /* Printer data */
10696 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10699 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10700 con
->http
.fd
, uri
->values
[0].string
.text
);
10703 * Is the destination valid?
10706 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10712 send_ipp_status(con
, IPP_NOT_FOUND
,
10713 _("The printer or class was not found."));
10721 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10723 send_http_error(con
, status
, printer
);
10728 * Stop the printer...
10731 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10732 IPP_TAG_TEXT
)) == NULL
)
10733 strcpy(printer
->state_message
, "Paused");
10736 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10737 sizeof(printer
->state_message
));
10740 cupsdStopPrinter(printer
, 1);
10742 if (dtype
& CUPS_PRINTER_CLASS
)
10743 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10744 printer
->name
, get_username(con
));
10746 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10747 printer
->name
, get_username(con
));
10750 * Everything was ok, so return OK status...
10753 con
->response
->request
.status
.status_code
= IPP_OK
;
10758 * 'url_encode_attr()' - URL-encode a string attribute.
10762 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10763 char *buffer
,/* I - String buffer */
10764 int bufsize
)/* I - Size of buffer */
10766 int i
; /* Looping var */
10767 char *bufptr
, /* Pointer into buffer */
10768 *bufend
; /* End of buffer */
10771 strlcpy(buffer
, attr
->name
, bufsize
);
10772 bufptr
= buffer
+ strlen(buffer
);
10773 bufend
= buffer
+ bufsize
- 1;
10775 for (i
= 0; i
< attr
->num_values
; i
++)
10777 if (bufptr
>= bufend
)
10785 if (bufptr
>= bufend
)
10790 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
10791 bufptr
, bufend
- bufptr
+ 1);
10793 if (bufptr
>= bufend
)
10804 * 'url_encode_string()' - URL-encode a string.
10807 static char * /* O - End of string */
10808 url_encode_string(const char *s
, /* I - String */
10809 char *buffer
, /* I - String buffer */
10810 int bufsize
) /* I - Size of buffer */
10812 char *bufptr
, /* Pointer into buffer */
10813 *bufend
; /* End of buffer */
10814 static const char *hex
= "0123456789ABCDEF";
10819 bufend
= buffer
+ bufsize
- 1;
10821 while (*s
&& bufptr
< bufend
)
10823 if (*s
== ' ' || *s
== '%')
10825 if (bufptr
>= (bufend
- 2))
10829 *bufptr
++ = hex
[(*s
>> 4) & 15];
10830 *bufptr
++ = hex
[*s
& 15];
10834 else if (*s
== '\'' || *s
== '\\')
10836 if (bufptr
>= (bufend
- 1))
10853 * 'user_allowed()' - See if a user is allowed to print to a queue.
10856 static int /* O - 0 if not allowed, 1 if allowed */
10857 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
10858 const char *username
) /* I - Username */
10860 int i
; /* Looping var */
10861 struct passwd
*pw
; /* User password data */
10862 char baseuser
[256], /* Base username */
10863 *baseptr
; /* Pointer to "@" in base username */
10866 if (p
->num_users
== 0)
10869 if (!strcmp(username
, "root"))
10872 if (strchr(username
, '@'))
10875 * Strip @REALM for username check...
10878 strlcpy(baseuser
, username
, sizeof(baseuser
));
10880 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
10883 username
= baseuser
;
10886 pw
= getpwnam(username
);
10889 for (i
= 0; i
< p
->num_users
; i
++)
10891 if (p
->users
[i
][0] == '@')
10894 * Check group membership...
10897 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
10900 else if (p
->users
[i
][0] == '#')
10906 if (cupsdCheckGroup(username
, pw
, p
->users
[i
]))
10909 else if (!strcasecmp(username
, p
->users
[i
]))
10913 return ((i
< p
->num_users
) != p
->deny_users
);
10918 * 'validate_job()' - Validate printer options and destination.
10922 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10923 ipp_attribute_t
*uri
) /* I - Printer URI */
10925 http_status_t status
; /* Policy status */
10926 ipp_attribute_t
*attr
; /* Current attribute */
10927 ipp_attribute_t
*format
; /* Document-format attribute */
10928 cups_ptype_t dtype
; /* Destination type (printer/class) */
10929 char super
[MIME_MAX_SUPER
],
10930 /* Supertype of file */
10931 type
[MIME_MAX_TYPE
];
10932 /* Subtype of file */
10933 cupsd_printer_t
*printer
; /* Printer */
10936 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10937 con
->http
.fd
, uri
->values
[0].string
.text
);
10940 * OK, see if the client is sending the document compressed - CUPS
10941 * doesn't support compression yet...
10944 if ((attr
= ippFindAttribute(con
->request
, "compression",
10945 IPP_TAG_KEYWORD
)) != NULL
&&
10946 !strcmp(attr
->values
[0].string
.text
, "none"))
10948 send_ipp_status(con
, IPP_ATTRIBUTES
,
10949 _("Unsupported compression attribute %s!"),
10950 attr
->values
[0].string
.text
);
10951 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10952 "compression", NULL
, attr
->values
[0].string
.text
);
10957 * Is it a format we support?
10960 if ((format
= ippFindAttribute(con
->request
, "document-format",
10961 IPP_TAG_MIMETYPE
)) != NULL
)
10963 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
10966 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
10967 format
->values
[0].string
.text
);
10971 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10972 !mimeType(MimeDatabase
, super
, type
))
10974 cupsdLogMessage(CUPSD_LOG_INFO
,
10975 "Hint: Do you have the raw file printing rules enabled?");
10976 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10977 _("Unsupported format \"%s\"!"),
10978 format
->values
[0].string
.text
);
10979 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10980 "document-format", NULL
, format
->values
[0].string
.text
);
10986 * Is the destination valid?
10989 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10995 send_ipp_status(con
, IPP_NOT_FOUND
,
10996 _("The printer or class was not found."));
11004 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11006 send_http_error(con
, status
, printer
);
11011 * Everything was ok, so return OK status...
11014 con
->response
->request
.status
.status_code
= IPP_OK
;
11019 * 'validate_name()' - Make sure the printer name only contains valid chars.
11022 static int /* O - 0 if name is no good, 1 if good */
11023 validate_name(const char *name
) /* I - Name to check */
11025 const char *ptr
; /* Pointer into name */
11029 * Scan the whole name...
11032 for (ptr
= name
; *ptr
; ptr
++)
11033 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
11037 * All the characters are good; validate the length, too...
11040 return ((ptr
- name
) < 128);
11045 * 'validate_user()' - Validate the user for the request.
11048 static int /* O - 1 if permitted, 0 otherwise */
11049 validate_user(cupsd_job_t
*job
, /* I - Job */
11050 cupsd_client_t
*con
, /* I - Client connection */
11051 const char *owner
, /* I - Owner of job/resource */
11052 char *username
, /* O - Authenticated username */
11053 int userlen
) /* I - Length of username */
11055 cupsd_printer_t
*printer
; /* Printer for job */
11058 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
11059 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
11061 job
->id
, con
? con
->http
.fd
: 0,
11062 owner
? owner
: "(null)", username
, userlen
);
11065 * Validate input...
11068 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11072 * Get the best authenticated username that is available.
11075 strlcpy(username
, get_username(con
), userlen
);
11078 * Check the username against the owner...
11081 printer
= cupsdFindDest(job
->dest
);
11083 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11084 con
, owner
) == HTTP_OK
);
11089 * End of "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $".