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 */
2454 need_restart_job
= 1;
2456 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2457 attr
->values
[0].string
.text
,
2458 scheme
, sizeof(scheme
),
2459 username
, sizeof(username
),
2460 host
, sizeof(host
), &port
,
2461 resource
, sizeof(resource
));
2463 if (uri_status
< HTTP_URI_OK
)
2465 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2466 attr
->values
[0].string
.text
);
2467 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2468 "add_printer: httpSeparateURI returned %d", uri_status
);
2472 if (!strcmp(scheme
, "file"))
2475 * See if the administrator has enabled file devices...
2478 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2481 * File devices are disabled and the URL is not file:/dev/null...
2484 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2485 _("File device URIs have been disabled! "
2486 "To enable, see the FileDevice directive in "
2487 "\"%s/cupsd.conf\"."),
2495 * See if the backend exists and is executable...
2498 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2499 if (access(srcfile
, X_OK
))
2502 * Could not find device in list!
2505 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2506 attr
->values
[0].string
.text
);
2511 cupsdLogMessage(CUPSD_LOG_INFO
,
2512 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2514 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2516 cupsdSanitizeURI(printer
->device_uri
, resource
,
2519 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2523 set_port_monitor
= 0;
2525 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2526 IPP_TAG_NAME
)) != NULL
)
2528 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2531 need_restart_job
= 1;
2533 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2537 for (i
= 0; i
< supported
->num_values
; i
++)
2538 if (!strcmp(supported
->values
[i
].string
.text
,
2539 attr
->values
[0].string
.text
))
2543 if (!supported
|| i
>= supported
->num_values
)
2545 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2546 attr
->values
[0].string
.text
);
2550 cupsdLogMessage(CUPSD_LOG_INFO
,
2551 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2552 printer
->name
, attr
->values
[0].string
.text
,
2553 printer
->port_monitor
? printer
->port_monitor
: "none");
2555 if (strcmp(attr
->values
[0].string
.text
, "none"))
2556 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2558 cupsdClearString(&printer
->port_monitor
);
2560 set_port_monitor
= 1;
2563 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2564 IPP_TAG_BOOLEAN
)) != NULL
)
2566 cupsdLogMessage(CUPSD_LOG_INFO
,
2567 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2568 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2570 printer
->accepting
= attr
->values
[0].boolean
;
2571 cupsdAddPrinterHistory(printer
);
2574 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2575 IPP_TAG_BOOLEAN
)) != NULL
)
2577 if (printer
->shared
&& !attr
->values
[0].boolean
)
2578 cupsdDeregisterPrinter(printer
, 1);
2580 cupsdLogMessage(CUPSD_LOG_INFO
,
2581 "Setting %s printer-is-shared to %d (was %d.)",
2582 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2584 printer
->shared
= attr
->values
[0].boolean
;
2587 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2588 IPP_TAG_ENUM
)) != NULL
)
2590 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2591 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2593 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2594 attr
->values
[0].integer
);
2598 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2599 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2601 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2602 cupsdStopPrinter(printer
, 0);
2605 need_restart_job
= 1;
2606 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2609 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2610 IPP_TAG_TEXT
)) != NULL
)
2612 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2613 sizeof(printer
->state_message
));
2614 cupsdAddPrinterHistory(printer
);
2617 set_printer_defaults(con
, printer
);
2619 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2620 IPP_TAG_KEYWORD
)) != NULL
)
2621 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2624 * See if we have all required attributes...
2627 if (!printer
->device_uri
)
2628 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2631 * See if we have an interface script or PPD file attached to the request...
2636 need_restart_job
= 1;
2638 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2640 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2643 * Yes; get the first line from it...
2647 cupsFileGets(fp
, line
, sizeof(line
));
2651 * Then see what kind of file it is...
2654 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2657 if (!strncmp(line
, "*PPD-Adobe", 10))
2660 * The new file is a PPD file, so remove any old interface script
2661 * that might be lying around...
2669 * This must be an interface script, so move the file over to the
2670 * interfaces directory and make it executable...
2673 if (copy_file(srcfile
, dstfile
))
2675 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2676 _("Unable to copy interface script - %s!"),
2681 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2682 "Copied interface script successfully!");
2683 chmod(dstfile
, 0755);
2686 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2689 if (!strncmp(line
, "*PPD-Adobe", 10))
2692 * The new file is a PPD file, so move the file over to the
2693 * ppd directory and make it readable by all...
2696 if (copy_file(srcfile
, dstfile
))
2698 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2699 _("Unable to copy PPD file - %s!"),
2704 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2705 "Copied PPD file successfully!");
2706 chmod(dstfile
, 0644);
2710 * (Re)register color profiles...
2713 apple_unregister_profiles(printer
);
2714 apple_register_profiles(printer
);
2715 #endif /* __APPLE__ */
2720 * This must be an interface script, so remove any old PPD file that
2721 * may be lying around...
2728 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2729 IPP_TAG_NAME
)) != NULL
)
2731 need_restart_job
= 1;
2733 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2736 * Raw driver, remove any existing PPD or interface script files.
2739 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2743 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2753 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2757 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2760 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2762 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2766 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2767 "Copied PPD file successfully!");
2768 chmod(dstfile
, 0644);
2772 * (Re)register color profiles...
2775 apple_unregister_profiles(printer
);
2776 apple_register_profiles(printer
);
2777 #endif /* __APPLE__ */
2782 * If we set the device URI but not the port monitor, check which port
2783 * monitor to use by default...
2786 if (set_device_uri
&& !set_port_monitor
)
2788 ppd_file_t
*ppd
; /* PPD file */
2789 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2792 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2793 sizeof(scheme
), username
, sizeof(username
), host
,
2794 sizeof(host
), &port
, resource
, sizeof(resource
));
2796 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2798 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2800 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2802 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2803 if (!strcmp(scheme
, ppdattr
->spec
))
2805 cupsdLogMessage(CUPSD_LOG_INFO
,
2806 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2807 printer
->name
, ppdattr
->value
,
2808 printer
->port_monitor
? printer
->port_monitor
2811 if (strcmp(ppdattr
->value
, "none"))
2812 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2814 cupsdClearString(&printer
->port_monitor
);
2824 * Update the printer attributes and return...
2827 cupsdSetPrinterAttrs(printer
);
2828 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2830 if (need_restart_job
&& printer
->job
)
2835 * Stop the current job and then restart it below...
2838 job
= (cupsd_job_t
*)printer
->job
;
2840 cupsdStopJob(job
, 1);
2842 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2843 job
->state_value
= IPP_JOB_PENDING
;
2846 if (need_restart_job
)
2849 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2853 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2854 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2857 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2858 printer
->name
, get_username(con
));
2862 cupsdAddPrinterHistory(printer
);
2864 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2865 "New printer \"%s\" added by \"%s\".", printer
->name
,
2868 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2869 printer
->name
, get_username(con
));
2872 con
->response
->request
.status
.status_code
= IPP_OK
;
2877 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2878 * based upon the printer state...
2882 add_printer_state_reasons(
2883 cupsd_client_t
*con
, /* I - Client connection */
2884 cupsd_printer_t
*p
) /* I - Printer info */
2886 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2887 "add_printer_state_reasons(%p[%d], %p[%s])",
2888 con
, con
->http
.fd
, p
, p
->name
);
2890 if (p
->num_reasons
== 0)
2891 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2892 "printer-state-reasons", NULL
,
2893 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2895 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2896 "printer-state-reasons", p
->num_reasons
, NULL
,
2897 (const char * const *)p
->reasons
);
2902 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2903 * the specified printer or class.
2907 add_queued_job_count(
2908 cupsd_client_t
*con
, /* I - Client connection */
2909 cupsd_printer_t
*p
) /* I - Printer or class */
2911 int count
; /* Number of jobs on destination */
2914 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2915 con
, con
->http
.fd
, p
, p
->name
);
2917 count
= cupsdGetPrinterJobCount(p
->name
);
2919 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2920 "queued-job-count", count
);
2926 * 'apple_init_profile()' - Initialize a color profile.
2931 ppd_file_t
*ppd
, /* I - PPD file */
2932 cups_array_t
*languages
, /* I - Languages in the PPD file */
2933 CMDeviceProfileInfo
*profile
, /* I - Profile record */
2934 unsigned id
, /* I - Profile ID */
2935 const char *name
, /* I - Profile name */
2936 const char *text
, /* I - Profile UI text */
2937 const char *iccfile
) /* I - ICC filename */
2939 char url
[1024]; /* URL for profile filename */
2940 CFMutableDictionaryRef dict
; /* Dictionary for name */
2941 char *language
; /* Current language */
2942 ppd_attr_t
*attr
; /* Profile attribute */
2943 CFStringRef cflang
, /* Language string */
2944 cftext
; /* Localized text */
2948 * Build the profile name dictionary...
2951 dict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
2952 &kCFTypeDictionaryKeyCallBacks
,
2953 &kCFTypeDictionaryValueCallBacks
);
2955 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, text
,
2956 kCFStringEncodingUTF8
);
2960 CFDictionarySetValue(dict
, CFSTR("en"), cftext
);
2967 * Find localized names for the color profiles...
2970 cupsArraySave(ppd
->sorted_attrs
);
2972 for (language
= (char *)cupsArrayFirst(languages
);
2974 language
= (char *)cupsArrayNext(languages
))
2978 if ((attr
= _ppdLocalizedAttr(ppd
, "cupsICCProfile", name
,
2980 attr
= _ppdLocalizedAttr(ppd
, "APTiogaProfile", name
, language
);
2983 attr
= _ppdLocalizedAttr(ppd
, "ColorModel", name
, language
);
2985 if (attr
&& attr
->text
[0])
2987 cflang
= CFStringCreateWithCString(kCFAllocatorDefault
, language
,
2988 kCFStringEncodingUTF8
);
2989 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, attr
->text
,
2990 kCFStringEncodingUTF8
);
2992 if (cflang
&& cftext
)
2993 CFDictionarySetValue(dict
, cflang
, cftext
);
3003 cupsArrayRestore(ppd
->sorted_attrs
);
3007 * Fill in the profile data...
3011 httpAssembleURI(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "file", NULL
, "", 0,
3014 profile
->dataVersion
= cmDeviceProfileInfoVersion1
;
3015 profile
->profileID
= id
;
3016 profile
->profileLoc
.locType
= iccfile
? cmPathBasedProfile
: cmNoProfileBase
;
3017 profile
->profileName
= dict
;
3020 strlcpy(profile
->profileLoc
.u
.pathLoc
.path
, iccfile
,
3021 sizeof(profile
->profileLoc
.u
.pathLoc
.path
));
3026 * 'apple_register_profiles()' - Register color profiles for a printer.
3030 apple_register_profiles(
3031 cupsd_printer_t
*p
) /* I - Printer */
3033 int i
; /* Looping var */
3034 char ppdfile
[1024], /* PPD filename */
3035 iccfile
[1024], /* ICC filename */
3036 selector
[PPD_MAX_NAME
];
3037 /* Profile selection string */
3038 ppd_file_t
*ppd
; /* PPD file */
3039 ppd_attr_t
*attr
, /* Profile attributes */
3040 *profileid_attr
,/* cupsProfileID attribute */
3041 *q1_attr
, /* ColorModel (or other) qualifier */
3042 *q2_attr
, /* MediaType (or other) qualifier */
3043 *q3_attr
; /* Resolution (or other) qualifier */
3044 char q_keyword
[PPD_MAX_NAME
];
3045 /* Qualifier keyword */
3046 const char *q1_choice
, /* ColorModel (or other) choice */
3047 *q2_choice
, /* MediaType (or other) choice */
3048 *q3_choice
; /* Resolution (or other) choice */
3049 const char *profile_key
; /* Profile keyword */
3050 ppd_option_t
*cm_option
; /* Color model option */
3051 ppd_choice_t
*cm_choice
; /* Color model choice */
3052 int num_profiles
; /* Number of profiles */
3053 CMError error
; /* Last error */
3054 unsigned device_id
, /* Printer device ID */
3055 profile_id
, /* Profile ID */
3056 default_profile_id
= 0;
3057 /* Default profile ID */
3058 CFMutableDictionaryRef device_name
; /* Printer device name dictionary */
3059 CFStringRef printer_name
; /* Printer name string */
3060 CMDeviceScope scope
= /* Scope of the registration */
3062 kCFPreferencesAnyUser
,
3063 kCFPreferencesCurrentHost
3065 CMDeviceProfileArrayPtr profiles
; /* Profiles */
3066 CMDeviceProfileInfo
*profile
; /* Current profile */
3067 cups_array_t
*languages
; /* Languages array */
3071 * Make sure ColorSync is available...
3074 if (CMRegisterColorDevice
== NULL
)
3078 * Try opening the PPD file for this printer...
3081 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3082 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
3086 * See if we have any profiles...
3089 if ((attr
= ppdFindAttr(ppd
, "APTiogaProfile", NULL
)) != NULL
)
3090 profile_key
= "APTiogaProfile";
3093 attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
3094 profile_key
= "cupsICCProfile";
3097 for (num_profiles
= 0; attr
; attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3098 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3100 if (attr
->value
[0] != '/')
3101 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3104 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3106 if (access(iccfile
, 0))
3114 * If we have profiles, add them...
3117 if (num_profiles
> 0)
3119 if (profile_key
[0] == 'A')
3122 * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile
3126 if ((attr
= ppdFindAttr(ppd
, "DefaultAPTiogaProfile", NULL
)) != NULL
&&
3128 default_profile_id
= atoi(attr
->value
);
3130 q1_choice
= q2_choice
= q3_choice
= NULL
;
3135 * For CUPS PPDs, figure out the default profile selector values...
3138 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier1", NULL
)) != NULL
&&
3139 attr
->value
&& attr
->value
[0])
3141 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3142 q1_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3144 else if ((q1_attr
= ppdFindAttr(ppd
, "DefaultColorModel", NULL
)) == NULL
)
3145 q1_attr
= ppdFindAttr(ppd
, "DefaultColorSpace", NULL
);
3147 if (q1_attr
&& q1_attr
->value
&& q1_attr
->value
[0])
3148 q1_choice
= q1_attr
->value
;
3152 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier2", NULL
)) != NULL
&&
3153 attr
->value
&& attr
->value
[0])
3155 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3156 q2_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3159 q2_attr
= ppdFindAttr(ppd
, "DefaultMediaType", NULL
);
3161 if (q2_attr
&& q2_attr
->value
&& q2_attr
->value
[0])
3162 q2_choice
= q2_attr
->value
;
3166 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier3", NULL
)) != NULL
&&
3167 attr
->value
&& attr
->value
[0])
3169 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3170 q3_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3173 q3_attr
= ppdFindAttr(ppd
, "DefaultResolution", NULL
);
3175 if (q3_attr
&& q3_attr
->value
&& q3_attr
->value
[0])
3176 q3_choice
= q3_attr
->value
;
3182 * Build the array of profiles...
3184 * Note: This calloc actually requests slightly more memory than needed.
3187 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3189 cupsdLogMessage(CUPSD_LOG_ERROR
,
3190 "Unable to allocate memory for %d profiles!",
3196 profiles
->profileCount
= num_profiles
;
3197 languages
= _ppdGetLanguages(ppd
);
3199 for (profile
= profiles
->profiles
,
3200 attr
= ppdFindAttr(ppd
, profile_key
, NULL
);
3202 attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3203 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3206 * Add this profile...
3209 if (attr
->value
[0] != '/')
3210 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3213 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3215 if (access(iccfile
, 0))
3218 if (profile_key
[0] == 'c')
3220 cupsArraySave(ppd
->sorted_attrs
);
3222 if ((profileid_attr
= ppdFindAttr(ppd
, "cupsProfileID",
3223 attr
->spec
)) != NULL
&&
3224 profileid_attr
->value
&& isdigit(profileid_attr
->value
[0] & 255))
3225 profile_id
= (unsigned)strtoul(profileid_attr
->value
, NULL
, 10);
3227 profile_id
= _ppdHashName(attr
->spec
);
3229 cupsArrayRestore(ppd
->sorted_attrs
);
3232 profile_id
= atoi(attr
->spec
);
3234 apple_init_profile(ppd
, languages
, profile
, profile_id
, attr
->spec
,
3235 attr
->text
[0] ? attr
->text
: attr
->spec
, iccfile
);
3240 * See if this is the default profile...
3243 if (!default_profile_id
)
3249 snprintf(selector
, sizeof(selector
), "%s.%s.%s",
3250 q1_choice
, q2_choice
, q3_choice
);
3251 if (!strcmp(selector
, attr
->spec
))
3252 default_profile_id
= profile_id
;
3255 if (!default_profile_id
)
3257 snprintf(selector
, sizeof(selector
), "%s.%s.", q1_choice
,
3259 if (!strcmp(selector
, attr
->spec
))
3260 default_profile_id
= profile_id
;
3264 if (!default_profile_id
&& q3_choice
)
3266 snprintf(selector
, sizeof(selector
), "%s..%s", q1_choice
,
3268 if (!strcmp(selector
, attr
->spec
))
3269 default_profile_id
= profile_id
;
3272 if (!default_profile_id
)
3274 snprintf(selector
, sizeof(selector
), "%s..", q1_choice
);
3275 if (!strcmp(selector
, attr
->spec
))
3276 default_profile_id
= profile_id
;
3281 _ppdFreeLanguages(languages
);
3283 else if ((cm_option
= ppdFindOption(ppd
, "ColorModel")) != NULL
)
3286 * Extract profiles from ColorModel option...
3289 const char *profile_name
; /* Name of generic profile */
3292 num_profiles
= cm_option
->num_choices
;
3294 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3296 cupsdLogMessage(CUPSD_LOG_ERROR
,
3297 "Unable to allocate memory for %d profiles!",
3303 profiles
->profileCount
= num_profiles
;
3305 for (profile
= profiles
->profiles
, i
= cm_option
->num_choices
,
3306 cm_choice
= cm_option
->choices
;
3308 i
--, cm_choice
++, profile
++)
3310 if (!strcmp(cm_choice
->choice
, "Gray") ||
3311 !strcmp(cm_choice
->choice
, "Black"))
3312 profile_name
= "Gray";
3313 else if (!strcmp(cm_choice
->choice
, "RGB") ||
3314 !strcmp(cm_choice
->choice
, "CMY"))
3315 profile_name
= "RGB";
3316 else if (!strcmp(cm_choice
->choice
, "CMYK") ||
3317 !strcmp(cm_choice
->choice
, "KCMY"))
3318 profile_name
= "CMYK";
3320 profile_name
= "DeviceN";
3322 snprintf(selector
, sizeof(selector
), "%s..", profile_name
);
3323 profile_id
= _ppdHashName(selector
);
3325 apple_init_profile(ppd
, NULL
, profile
, profile_id
, cm_choice
->choice
,
3326 cm_choice
->text
, NULL
);
3328 if (cm_choice
->marked
)
3329 default_profile_id
= profile_id
;
3335 * Use the default colorspace...
3338 num_profiles
= 1 + ppd
->colorspace
!= PPD_CS_GRAY
;
3340 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3342 cupsdLogMessage(CUPSD_LOG_ERROR
,
3343 "Unable to allocate memory for %d profiles!",
3349 profiles
->profileCount
= num_profiles
;
3351 apple_init_profile(ppd
, NULL
, profiles
->profiles
, _ppdHashName("Gray.."),
3352 "Gray", "Gray", NULL
);
3354 switch (ppd
->colorspace
)
3358 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3359 _ppdHashName("RGB.."), "RGB", "RGB", NULL
);
3363 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3364 _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL
);
3368 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3369 _ppdHashName("DeviceN.."), "DeviceN", "DeviceN",
3378 if (num_profiles
> 0)
3381 * Make sure we have a default profile ID...
3384 if (!default_profile_id
)
3385 default_profile_id
= profiles
->profiles
[num_profiles
- 1].profileID
;
3388 * Get the device ID hash and pathelogical name dictionary.
3391 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\"",
3394 device_id
= _ppdHashName(p
->name
);
3395 device_name
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3396 &kCFTypeDictionaryKeyCallBacks
,
3397 &kCFTypeDictionaryValueCallBacks
);
3398 printer_name
= CFStringCreateWithCString(kCFAllocatorDefault
,
3399 p
->name
, kCFStringEncodingUTF8
);
3401 if (device_name
&& printer_name
)
3403 CFDictionarySetValue(device_name
, CFSTR("en"), printer_name
);
3406 * Register the device with ColorSync...
3409 error
= CMRegisterColorDevice(cmPrinterDeviceClass
, device_id
,
3410 device_name
, &scope
);
3413 * Register the profiles...
3417 error
= CMSetDeviceFactoryProfiles(cmPrinterDeviceClass
, device_id
,
3418 default_profile_id
, profiles
);
3428 cupsdLogMessage(CUPSD_LOG_ERROR
,
3429 "Unable to register ICC color profiles for \"%s\" - %d",
3430 p
->name
, (int)error
);
3432 for (profile
= profiles
->profiles
;
3434 profile
++, num_profiles
--)
3435 CFRelease(profile
->profileName
);
3440 CFRelease(printer_name
);
3443 CFRelease(device_name
);
3451 * 'apple_unregister_profiles()' - Remove color profiles for the specified
3456 apple_unregister_profiles(
3457 cupsd_printer_t
*p
) /* I - Printer */
3460 * Make sure ColorSync is available...
3463 if (CMUnregisterColorDevice
!= NULL
)
3464 CMUnregisterColorDevice(cmPrinterDeviceClass
, _ppdHashName(p
->name
));
3466 #endif /* __APPLE__ */
3469 * 'apply_printer_defaults()' - Apply printer default options to a job.
3473 apply_printer_defaults(
3474 cupsd_printer_t
*printer
, /* I - Printer */
3475 cupsd_job_t
*job
) /* I - Job */
3477 int i
, /* Looping var */
3478 num_options
; /* Number of default options */
3479 cups_option_t
*options
, /* Default options */
3480 *option
; /* Current option */
3484 * Collect all of the default options and add the missing ones to the
3488 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
3489 option
= printer
->options
;
3492 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
3494 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
3499 * Encode these options as attributes in the job object...
3502 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
3503 cupsFreeOptions(num_options
, options
);
3508 * 'authenticate_job()' - Set job authentication info.
3512 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
3513 ipp_attribute_t
*uri
) /* I - Job URI */
3515 ipp_attribute_t
*attr
, /* job-id attribute */
3516 *auth_info
; /* auth-info attribute */
3517 int jobid
; /* Job ID */
3518 cupsd_job_t
*job
; /* Current job */
3519 char method
[HTTP_MAX_URI
],
3520 /* Method portion of URI */
3521 username
[HTTP_MAX_URI
],
3522 /* Username portion of URI */
3524 /* Host portion of URI */
3525 resource
[HTTP_MAX_URI
];
3526 /* Resource portion of URI */
3527 int port
; /* Port portion of URI */
3530 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
3531 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
3534 * Start with "everything is OK" status...
3537 con
->response
->request
.status
.status_code
= IPP_OK
;
3540 * See if we have a job URI or a printer URI...
3543 if (!strcmp(uri
->name
, "printer-uri"))
3546 * Got a printer URI; see if we also have a job-id attribute...
3549 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3550 IPP_TAG_INTEGER
)) == NULL
)
3552 send_ipp_status(con
, IPP_BAD_REQUEST
,
3553 _("Got a printer-uri attribute but no job-id!"));
3557 jobid
= attr
->values
[0].integer
;
3562 * Got a job URI; parse it to get the job ID...
3565 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
3566 sizeof(method
), username
, sizeof(username
), host
,
3567 sizeof(host
), &port
, resource
, sizeof(resource
));
3569 if (strncmp(resource
, "/jobs/", 6))
3575 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
3576 uri
->values
[0].string
.text
);
3580 jobid
= atoi(resource
+ 6);
3584 * See if the job exists...
3587 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3590 * Nope - return a "not found" error...
3593 send_ipp_status(con
, IPP_NOT_FOUND
,
3594 _("Job #%d does not exist!"), jobid
);
3599 * See if the job has been completed...
3602 if (job
->state_value
!= IPP_JOB_HELD
)
3605 * Return a "not-possible" error...
3608 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3609 _("Job #%d is not held for authentication!"),
3615 * See if we have already authenticated...
3618 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3620 if (!con
->username
[0] && !auth_info
)
3622 cupsd_printer_t
*printer
; /* Job destination */
3626 * No auth data. If we need to authenticate via Kerberos, send a
3627 * HTTP auth challenge, otherwise just return an IPP error...
3630 printer
= cupsdFindDest(job
->dest
);
3632 if (printer
&& printer
->num_auth_info_required
> 0 &&
3633 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3634 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3636 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3637 _("No authentication information provided!"));
3642 * See if the job is owned by the requesting user...
3645 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3647 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3652 * Save the authentication information for this job...
3655 save_auth_info(con
, job
, auth_info
);
3658 * Reset the job-hold-until value to "no-hold"...
3661 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3662 IPP_TAG_KEYWORD
)) == NULL
)
3663 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3667 attr
->value_tag
= IPP_TAG_KEYWORD
;
3668 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
3672 * Release the job and return...
3675 cupsdReleaseJob(job
);
3677 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3682 * 'cancel_all_jobs()' - Cancel all print jobs.
3686 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3687 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3689 http_status_t status
; /* Policy status */
3690 cups_ptype_t dtype
; /* Destination type */
3691 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3692 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3693 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3694 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3695 int port
; /* Port portion of URI */
3696 ipp_attribute_t
*attr
; /* Attribute in request */
3697 const char *username
; /* Username */
3698 int purge
; /* Purge? */
3699 cupsd_printer_t
*printer
; /* Printer */
3702 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3703 con
->http
.fd
, uri
->values
[0].string
.text
);
3706 * See if we have a printer URI...
3709 if (strcmp(uri
->name
, "printer-uri"))
3711 send_ipp_status(con
, IPP_BAD_REQUEST
,
3712 _("The printer-uri attribute is required!"));
3717 * Get the username (if any) for the jobs we want to cancel (only if
3718 * "my-jobs" is specified...
3721 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3722 IPP_TAG_BOOLEAN
)) != NULL
&&
3723 attr
->values
[0].boolean
)
3725 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3726 IPP_TAG_NAME
)) != NULL
)
3727 username
= attr
->values
[0].string
.text
;
3730 send_ipp_status(con
, IPP_BAD_REQUEST
,
3731 _("Missing requesting-user-name attribute!"));
3739 * Look for the "purge-jobs" attribute...
3742 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3743 IPP_TAG_BOOLEAN
)) != NULL
)
3744 purge
= attr
->values
[0].boolean
;
3749 * And if the destination is valid...
3752 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3758 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3759 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3760 hostname
, sizeof(hostname
), &port
,
3761 resource
, sizeof(resource
));
3763 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3764 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3766 send_ipp_status(con
, IPP_NOT_FOUND
,
3767 _("The printer or class was not found."));
3775 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3777 send_http_error(con
, status
, NULL
);
3782 * Cancel all jobs on all printers...
3785 cupsdCancelJobs(NULL
, username
, purge
);
3787 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3788 purge
? "purged" : "canceled", get_username(con
));
3796 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3799 send_http_error(con
, status
, printer
);
3804 * Cancel all of the jobs on the named printer...
3807 cupsdCancelJobs(printer
->name
, username
, purge
);
3809 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3810 printer
->name
, purge
? "purged" : "canceled",
3814 con
->response
->request
.status
.status_code
= IPP_OK
;
3819 * 'cancel_job()' - Cancel a print job.
3823 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3824 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3826 ipp_attribute_t
*attr
; /* Current attribute */
3827 int jobid
; /* Job ID */
3828 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3829 username
[HTTP_MAX_URI
], /* Username portion of URI */
3830 host
[HTTP_MAX_URI
], /* Host portion of URI */
3831 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3832 int port
; /* Port portion of URI */
3833 cupsd_job_t
*job
; /* Job information */
3834 cups_ptype_t dtype
; /* Destination type (printer/class) */
3835 cupsd_printer_t
*printer
; /* Printer data */
3836 int purge
; /* Purge the job? */
3839 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3840 con
->http
.fd
, uri
->values
[0].string
.text
);
3843 * See if we have a job URI or a printer URI...
3846 if (!strcmp(uri
->name
, "printer-uri"))
3849 * Got a printer URI; see if we also have a job-id attribute...
3852 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3853 IPP_TAG_INTEGER
)) == NULL
)
3855 send_ipp_status(con
, IPP_BAD_REQUEST
,
3856 _("Got a printer-uri attribute but no job-id!"));
3860 if ((jobid
= attr
->values
[0].integer
) == 0)
3863 * Find the current job on the specified printer...
3866 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3872 send_ipp_status(con
, IPP_NOT_FOUND
,
3873 _("The printer or class was not found."));
3878 * See if the printer is currently printing a job...
3882 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3886 * No, see if there are any pending jobs...
3889 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3891 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3892 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3893 !strcasecmp(job
->dest
, printer
->name
))
3900 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3902 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3903 if (job
->state_value
== IPP_JOB_STOPPED
&&
3904 !strcasecmp(job
->dest
, printer
->name
))
3911 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3922 * Got a job URI; parse it to get the job ID...
3925 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3926 sizeof(scheme
), username
, sizeof(username
), host
,
3927 sizeof(host
), &port
, resource
, sizeof(resource
));
3929 if (strncmp(resource
, "/jobs/", 6))
3935 send_ipp_status(con
, IPP_BAD_REQUEST
,
3936 _("Bad job-uri attribute \"%s\"!"),
3937 uri
->values
[0].string
.text
);
3941 jobid
= atoi(resource
+ 6);
3945 * Look for the "purge-job" attribute...
3948 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3949 IPP_TAG_BOOLEAN
)) != NULL
)
3950 purge
= attr
->values
[0].boolean
;
3955 * See if the job exists...
3958 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3961 * Nope - return a "not found" error...
3964 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3969 * See if the job is owned by the requesting user...
3972 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3974 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3979 * See if the job is already completed, canceled, or aborted; if so,
3980 * we can't cancel...
3983 if (job
->state_value
>= IPP_JOB_CANCELED
&& !purge
)
3985 switch (job
->state_value
)
3987 case IPP_JOB_CANCELED
:
3988 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3989 _("Job #%d is already canceled - can\'t cancel."),
3993 case IPP_JOB_ABORTED
:
3994 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3995 _("Job #%d is already aborted - can\'t cancel."),
4000 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4001 _("Job #%d is already completed - can\'t cancel."),
4010 * Cancel the job and return...
4013 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
4017 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
4020 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
4023 con
->response
->request
.status
.status_code
= IPP_OK
;
4028 * 'cancel_subscription()' - Cancel a subscription.
4032 cancel_subscription(
4033 cupsd_client_t
*con
, /* I - Client connection */
4034 int sub_id
) /* I - Subscription ID */
4036 http_status_t status
; /* Policy status */
4037 cupsd_subscription_t
*sub
; /* Subscription */
4040 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4041 "cancel_subscription(con=%p[%d], sub_id=%d)",
4042 con
, con
->http
.fd
, sub_id
);
4045 * Is the subscription ID valid?
4048 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
4051 * Bad subscription ID...
4054 send_ipp_status(con
, IPP_NOT_FOUND
,
4055 _("notify-subscription-id %d no good!"), sub_id
);
4063 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
4065 con
, sub
->owner
)) != HTTP_OK
)
4067 send_http_error(con
, status
, sub
->dest
);
4072 * Cancel the subscription...
4075 cupsdDeleteSubscription(sub
, 1);
4077 con
->response
->request
.status
.status_code
= IPP_OK
;
4082 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
4085 static int /* O - 1 if OK, 0 if not */
4086 check_rss_recipient(
4087 const char *recipient
) /* I - Recipient URI */
4089 cupsd_subscription_t
*sub
; /* Current subscription */
4092 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
4094 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
4098 * Compare the URIs up to the first ?...
4101 const char *r1
, *r2
;
4103 for (r1
= recipient
, r2
= sub
->recipient
;
4104 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
4116 * 'check_quotas()' - Check quotas for a printer and user.
4119 static int /* O - 1 if OK, 0 if not */
4120 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
4121 cupsd_printer_t
*p
) /* I - Printer or class */
4123 int i
; /* Looping var */
4124 char username
[33]; /* Username */
4125 cupsd_quota_t
*q
; /* Quota data */
4126 #ifdef HAVE_MBR_UID_TO_UUID
4128 * Use Apple membership APIs which require that all names represent
4129 * valid user account or group records accessible by the server.
4132 uuid_t usr_uuid
; /* UUID for job requesting user */
4133 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
4134 uuid_t grp_uuid
; /* UUID for ACL group name entry */
4135 int mbr_err
; /* Error from membership function */
4136 int is_member
; /* Is this user a member? */
4139 * Use standard POSIX APIs for checking users and groups...
4142 struct passwd
*pw
; /* User password data */
4143 #endif /* HAVE_MBR_UID_TO_UUID */
4146 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
4147 con
, con
->http
.fd
, p
, p
->name
);
4150 * Figure out who is printing...
4153 strlcpy(username
, get_username(con
), sizeof(username
));
4156 * Check global active job limits for printers and users...
4159 if (MaxJobsPerPrinter
)
4162 * Check if there are too many pending jobs on this printer...
4165 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
4167 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
4176 * Check if there are too many pending jobs for this user...
4179 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
4181 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
4188 * Check against users...
4191 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
4196 #ifdef HAVE_MBR_UID_TO_UUID
4198 * Get UUID for job requesting user...
4201 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
4207 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4208 "check_quotas: UUID lookup failed for user \"%s\"",
4210 cupsdLogMessage(CUPSD_LOG_INFO
,
4211 "Denying user \"%s\" access to printer \"%s\" "
4212 "(unknown user)...",
4218 * Get UID and GID of requesting user...
4221 pw
= getpwnam(username
);
4223 #endif /* HAVE_MBR_UID_TO_UUID */
4225 for (i
= 0; i
< p
->num_users
; i
++)
4226 if (p
->users
[i
][0] == '@')
4229 * Check group membership...
4232 #ifdef HAVE_MBR_UID_TO_UUID
4233 if (p
->users
[i
][1] == '#')
4235 if (uuid_parse((char *)p
->users
[i
] + 2, grp_uuid
))
4236 uuid_clear(grp_uuid
);
4238 else if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
4242 * Invalid ACL entries are ignored for matching; just record a
4243 * warning in the log...
4246 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4247 "check_quotas: UUID lookup failed for ACL entry "
4248 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4249 cupsdLogMessage(CUPSD_LOG_WARN
,
4250 "Access control entry \"%s\" not a valid group name; "
4251 "entry ignored", p
->users
[i
]);
4254 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
4258 * At this point, there should be no errors, but check anyways...
4261 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4262 "check_quotas: group \"%s\" membership check "
4263 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
4268 * Stop if we found a match...
4275 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
4277 #endif /* HAVE_MBR_UID_TO_UUID */
4279 #ifdef HAVE_MBR_UID_TO_UUID
4282 if (p
->users
[i
][0] == '#')
4284 if (uuid_parse((char *)p
->users
[i
] + 1, usr2_uuid
))
4285 uuid_clear(usr2_uuid
);
4287 else if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
4291 * Invalid ACL entries are ignored for matching; just record a
4292 * warning in the log...
4295 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4296 "check_quotas: UUID lookup failed for ACL entry "
4297 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4298 cupsdLogMessage(CUPSD_LOG_WARN
,
4299 "Access control entry \"%s\" not a valid user name; "
4300 "entry ignored", p
->users
[i
]);
4303 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
4306 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4307 "check_quotas: User \"%s\" identity check failed "
4308 "(err=%d)", p
->users
[i
], mbr_err
);
4316 else if (!strcasecmp(username
, p
->users
[i
]))
4318 #endif /* HAVE_MBR_UID_TO_UUID */
4320 if ((i
< p
->num_users
) == p
->deny_users
)
4322 cupsdLogMessage(CUPSD_LOG_INFO
,
4323 "Denying user \"%s\" access to printer \"%s\"...",
4334 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
4337 * TODO: Define these special page count values as constants!
4340 if (q
->page_count
== -4) /* special case: unlimited user */
4342 cupsdLogMessage(CUPSD_LOG_INFO
,
4343 "User \"%s\" request approved for printer %s (%s): "
4345 username
, p
->name
, p
->info
);
4346 q
->page_count
= 0; /* allow user to print */
4349 else if (q
->page_count
== -3) /* quota exceeded */
4351 cupsdLogMessage(CUPSD_LOG_INFO
,
4352 "User \"%s\" request denied for printer %s (%s): "
4353 "quota limit exceeded.",
4354 username
, p
->name
, p
->info
);
4355 q
->page_count
= 2; /* force quota exceeded failure */
4358 else if (q
->page_count
== -2) /* quota disabled for user */
4360 cupsdLogMessage(CUPSD_LOG_INFO
,
4361 "User \"%s\" request denied for printer %s (%s): "
4362 "printing disabled for user.",
4363 username
, p
->name
, p
->info
);
4364 q
->page_count
= 2; /* force quota exceeded failure */
4367 else if (q
->page_count
== -1) /* quota access error */
4369 cupsdLogMessage(CUPSD_LOG_INFO
,
4370 "User \"%s\" request denied for printer %s (%s): "
4371 "unable to determine quota limit.",
4372 username
, p
->name
, p
->info
);
4373 q
->page_count
= 2; /* force quota exceeded failure */
4376 else if (q
->page_count
< 0) /* user not found or other error */
4378 cupsdLogMessage(CUPSD_LOG_INFO
,
4379 "User \"%s\" request denied for printer %s (%s): "
4380 "user disabled / missing quota.",
4381 username
, p
->name
, p
->info
);
4382 q
->page_count
= 2; /* force quota exceeded failure */
4385 else /* page within user limits */
4387 q
->page_count
= 0; /* allow user to print */
4392 #endif /* __APPLE__ */
4393 if (p
->k_limit
|| p
->page_limit
)
4395 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
4397 cupsdLogMessage(CUPSD_LOG_ERROR
,
4398 "Unable to allocate quota data for user \"%s\"!",
4403 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
4404 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
4406 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
4413 * If we have gotten this far, we're done!
4421 * 'copy_attribute()' - Copy a single attribute.
4424 static ipp_attribute_t
* /* O - New attribute */
4426 ipp_t
*to
, /* O - Destination request/response */
4427 ipp_attribute_t
*attr
, /* I - Attribute to copy */
4428 int quickcopy
) /* I - Do a quick copy? */
4430 int i
; /* Looping var */
4431 ipp_attribute_t
*toattr
; /* Destination attribute */
4434 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4435 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
4436 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
4439 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
4442 toattr
= ippAddSeparator(to
);
4445 case IPP_TAG_INTEGER
:
4447 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4448 attr
->name
, attr
->num_values
, NULL
);
4450 for (i
= 0; i
< attr
->num_values
; i
++)
4451 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
4454 case IPP_TAG_BOOLEAN
:
4455 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
4456 attr
->num_values
, NULL
);
4458 for (i
= 0; i
< attr
->num_values
; i
++)
4459 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
4462 case IPP_TAG_STRING
:
4465 case IPP_TAG_KEYWORD
:
4467 case IPP_TAG_URISCHEME
:
4468 case IPP_TAG_CHARSET
:
4469 case IPP_TAG_LANGUAGE
:
4470 case IPP_TAG_MIMETYPE
:
4471 toattr
= ippAddStrings(to
, attr
->group_tag
,
4472 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4473 attr
->name
, attr
->num_values
, NULL
, NULL
);
4477 for (i
= 0; i
< attr
->num_values
; i
++)
4478 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4482 for (i
= 0; i
< attr
->num_values
; i
++)
4483 toattr
->values
[i
].string
.text
=
4484 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4489 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
4490 attr
->values
[0].date
);
4493 case IPP_TAG_RESOLUTION
:
4494 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
4495 attr
->num_values
, IPP_RES_PER_INCH
,
4498 for (i
= 0; i
< attr
->num_values
; i
++)
4500 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
4501 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
4502 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
4506 case IPP_TAG_RANGE
:
4507 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
4508 attr
->num_values
, NULL
, NULL
);
4510 for (i
= 0; i
< attr
->num_values
; i
++)
4512 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
4513 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
4517 case IPP_TAG_TEXTLANG
:
4518 case IPP_TAG_NAMELANG
:
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
++)
4527 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
4528 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4533 for (i
= 0; i
< attr
->num_values
; i
++)
4536 toattr
->values
[i
].string
.charset
=
4537 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
4539 toattr
->values
[i
].string
.charset
=
4540 toattr
->values
[0].string
.charset
;
4542 toattr
->values
[i
].string
.text
=
4543 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4548 case IPP_TAG_BEGIN_COLLECTION
:
4549 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
4550 attr
->num_values
, NULL
);
4552 for (i
= 0; i
< attr
->num_values
; i
++)
4554 toattr
->values
[i
].collection
= ippNew();
4555 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
4556 NULL
, IPP_TAG_ZERO
, 0);
4561 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4562 attr
->name
, attr
->num_values
, NULL
);
4564 for (i
= 0; i
< attr
->num_values
; i
++)
4566 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
4568 if (toattr
->values
[i
].unknown
.length
> 0)
4570 if ((toattr
->values
[i
].unknown
.data
=
4571 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
4572 toattr
->values
[i
].unknown
.length
= 0;
4574 memcpy(toattr
->values
[i
].unknown
.data
,
4575 attr
->values
[i
].unknown
.data
,
4576 toattr
->values
[i
].unknown
.length
);
4579 break; /* anti-compiler-warning-code */
4587 * 'copy_attrs()' - Copy attributes from one request to another.
4591 copy_attrs(ipp_t
*to
, /* I - Destination request */
4592 ipp_t
*from
, /* I - Source request */
4593 cups_array_t
*ra
, /* I - Requested attributes */
4594 ipp_tag_t group
, /* I - Group to copy */
4595 int quickcopy
) /* I - Do a quick copy? */
4597 ipp_attribute_t
*fromattr
; /* Source attribute */
4600 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4601 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4602 to
, from
, ra
, group
, quickcopy
);
4607 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4610 * Filter attributes as needed...
4613 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4614 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4617 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4618 copy_attribute(to
, fromattr
, quickcopy
);
4624 * 'copy_banner()' - Copy a banner file to the requests directory for the
4628 static int /* O - Size of banner file in kbytes */
4629 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4630 cupsd_job_t
*job
, /* I - Job information */
4631 const char *name
) /* I - Name of banner */
4633 int i
; /* Looping var */
4634 int kbytes
; /* Size of banner file in kbytes */
4635 char filename
[1024]; /* Job filename */
4636 cupsd_banner_t
*banner
; /* Pointer to banner */
4637 cups_file_t
*in
; /* Input file */
4638 cups_file_t
*out
; /* Output file */
4639 int ch
; /* Character from file */
4640 char attrname
[255], /* Name of attribute */
4641 *s
; /* Pointer into name */
4642 ipp_attribute_t
*attr
; /* Attribute */
4645 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4646 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4647 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
4648 name
? name
: "(null)");
4651 * Find the banner; return if not found or "none"...
4654 if (!name
|| !strcmp(name
, "none") ||
4655 (banner
= cupsdFindBanner(name
)) == NULL
)
4659 * Open the banner and job files...
4662 if (add_file(con
, job
, banner
->filetype
, 0))
4665 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4667 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4669 cupsdLogMessage(CUPSD_LOG_ERROR
,
4670 "Unable to create banner job file %s - %s",
4671 filename
, strerror(errno
));
4676 fchmod(cupsFileNumber(out
), 0640);
4677 fchown(cupsFileNumber(out
), RunUser
, Group
);
4680 * Try the localized banner file under the subdirectory...
4683 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4685 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4688 * Convert ll-cc to ll_CC...
4692 attrname
[3] = toupper(attrname
[3] & 255);
4693 attrname
[4] = toupper(attrname
[4] & 255);
4696 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4699 if (access(filename
, 0) && strlen(attrname
) > 2)
4702 * Wasn't able to find "ll_CC" locale file; try the non-national
4703 * localization banner directory.
4708 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4712 if (access(filename
, 0))
4715 * Use the non-localized banner file.
4718 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4721 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4725 cupsdLogMessage(CUPSD_LOG_ERROR
,
4726 "Unable to open banner template file %s - %s",
4727 filename
, strerror(errno
));
4733 * Parse the file to the end...
4736 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4740 * Get an attribute name...
4743 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4744 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4746 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4756 * Ignore { followed by stuff that is not an attribute name...
4759 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4764 * See if it is defined...
4767 if (attrname
[0] == '?')
4772 if (!strcmp(s
, "printer-name"))
4774 cupsFilePuts(out
, job
->dest
);
4777 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4780 * See if we have a leading question mark...
4783 if (attrname
[0] != '?')
4786 * Nope, write to file as-is; probably a PostScript procedure...
4789 cupsFilePrintf(out
, "{%s}", attrname
);
4796 * Output value(s)...
4799 for (i
= 0; i
< attr
->num_values
; i
++)
4802 cupsFilePutChar(out
, ',');
4804 switch (attr
->value_tag
)
4806 case IPP_TAG_INTEGER
:
4808 if (!strncmp(s
, "time-at-", 8))
4809 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
4811 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4814 case IPP_TAG_BOOLEAN
:
4815 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4818 case IPP_TAG_NOVALUE
:
4819 cupsFilePuts(out
, "novalue");
4822 case IPP_TAG_RANGE
:
4823 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4824 attr
->values
[i
].range
.upper
);
4827 case IPP_TAG_RESOLUTION
:
4828 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4829 attr
->values
[i
].resolution
.yres
,
4830 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4835 case IPP_TAG_STRING
:
4838 case IPP_TAG_KEYWORD
:
4839 case IPP_TAG_CHARSET
:
4840 case IPP_TAG_LANGUAGE
:
4841 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4844 * Need to quote strings for PS banners...
4849 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4851 if (*p
== '(' || *p
== ')' || *p
== '\\')
4853 cupsFilePutChar(out
, '\\');
4854 cupsFilePutChar(out
, *p
);
4856 else if (*p
< 32 || *p
> 126)
4857 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4859 cupsFilePutChar(out
, *p
);
4863 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4867 break; /* anti-compiler-warning-code */
4871 else if (ch
== '\\') /* Quoted char */
4873 ch
= cupsFileGetChar(in
);
4875 if (ch
!= '{') /* Only do special handling for \{ */
4876 cupsFilePutChar(out
, '\\');
4878 cupsFilePutChar(out
, ch
);
4881 cupsFilePutChar(out
, ch
);
4885 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4887 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4888 IPP_TAG_INTEGER
)) != NULL
)
4889 attr
->values
[0].integer
+= kbytes
;
4898 * 'copy_file()' - Copy a PPD file or interface script...
4901 static int /* O - 0 = success, -1 = error */
4902 copy_file(const char *from
, /* I - Source file */
4903 const char *to
) /* I - Destination file */
4905 cups_file_t
*src
, /* Source file */
4906 *dst
; /* Destination file */
4907 int bytes
; /* Bytes to read/write */
4908 char buffer
[2048]; /* Copy buffer */
4911 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4914 * Open the source and destination file for a copy...
4917 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4920 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4927 * Copy the source file to the destination...
4930 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4931 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
4939 * Close both files and return...
4944 return (cupsFileClose(dst
));
4949 * 'copy_model()' - Copy a PPD model file, substituting default values
4953 static int /* O - 0 = success, -1 = error */
4954 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4955 const char *from
, /* I - Source file */
4956 const char *to
) /* I - Destination file */
4958 fd_set input
; /* select() input set */
4959 struct timeval timeout
; /* select() timeout */
4960 int maxfd
; /* Max file descriptor for select() */
4961 char tempfile
[1024]; /* Temporary PPD file */
4962 int tempfd
; /* Temporary PPD file descriptor */
4963 int temppid
; /* Process ID of cups-driverd */
4964 int temppipe
[2]; /* Temporary pipes */
4965 char *argv
[4], /* Command-line arguments */
4966 *envp
[MAX_ENV
]; /* Environment */
4967 cups_file_t
*src
, /* Source file */
4968 *dst
; /* Destination file */
4969 ppd_file_t
*ppd
; /* PPD file */
4970 int bytes
, /* Bytes from pipe */
4971 total
; /* Total bytes from pipe */
4972 char buffer
[2048]; /* Copy buffer */
4973 int i
; /* Looping var */
4974 char option
[PPD_MAX_NAME
], /* Option name */
4975 choice
[PPD_MAX_NAME
]; /* Choice name */
4976 int num_defaults
; /* Number of default options */
4977 cups_option_t
*defaults
; /* Default options */
4978 char cups_protocol
[PPD_MAX_LINE
];
4979 /* cupsProtocol attribute */
4982 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4983 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4987 * Run cups-driverd to get the PPD file...
4990 argv
[0] = "cups-driverd";
4992 argv
[2] = (char *)from
;
4995 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4997 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4998 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
4999 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
5003 cupsdOpenPipe(temppipe
);
5005 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5006 "copy_model: Running \"cups-driverd cat %s\"...", from
);
5008 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
5009 -1, -1, 0, DefaultProfile
, &temppid
))
5020 * Wait up to 30 seconds for the PPD file to be copied...
5025 if (temppipe
[0] > CGIPipes
[0])
5026 maxfd
= temppipe
[0] + 1;
5028 maxfd
= CGIPipes
[0] + 1;
5033 * See if we have data ready...
5037 FD_SET(temppipe
[0], &input
);
5038 FD_SET(CGIPipes
[0], &input
);
5040 timeout
.tv_sec
= 30;
5041 timeout
.tv_usec
= 0;
5043 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
5053 * We have timed out...
5059 if (FD_ISSET(temppipe
[0], &input
))
5062 * Read the PPD file from the pipe, and write it to the PPD file.
5065 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
5067 if (write(tempfd
, buffer
, bytes
) < bytes
)
5076 if (FD_ISSET(CGIPipes
[0], &input
))
5086 * No data from cups-deviced...
5089 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
5095 * Read the source file and see what page sizes are supported...
5098 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
5105 * Open the destination (if possible) and set the default options...
5110 cups_protocol
[0] = '\0';
5112 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
5115 * Read all of the default lines from the old PPD...
5118 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
5119 if (!strncmp(buffer
, "*Default", 8))
5122 * Add the default option...
5125 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5126 choice
, sizeof(choice
)))
5128 ppd_option_t
*ppdo
; /* PPD option */
5132 * Only add the default if the default hasn't already been
5133 * set and the choice exists in the new PPD...
5136 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
5137 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
5138 ppdFindChoice(ppdo
, choice
))
5139 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
5143 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
5144 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
5148 else if (ppdPageSize(ppd
, DefaultPaperSize
))
5151 * Add the default media sizes...
5154 num_defaults
= cupsAddOption("PageSize", DefaultPaperSize
,
5155 num_defaults
, &defaults
);
5156 num_defaults
= cupsAddOption("PageRegion", DefaultPaperSize
,
5157 num_defaults
, &defaults
);
5158 num_defaults
= cupsAddOption("PaperDimension", DefaultPaperSize
,
5159 num_defaults
, &defaults
);
5160 num_defaults
= cupsAddOption("ImageableArea", DefaultPaperSize
,
5161 num_defaults
, &defaults
);
5167 * Open the source file for a copy...
5170 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
5172 cupsFreeOptions(num_defaults
, defaults
);
5178 * Open the destination file for a copy...
5181 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
5183 cupsFreeOptions(num_defaults
, defaults
);
5190 * Copy the source file to the destination...
5193 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
5195 if (!strncmp(buffer
, "*Default", 8))
5198 * Check for an previous default option choice...
5201 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5202 choice
, sizeof(choice
)))
5204 const char *val
; /* Default option value */
5207 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
5210 * Substitute the previous choice...
5213 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
5218 cupsFilePrintf(dst
, "%s\n", buffer
);
5221 if (cups_protocol
[0])
5222 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
5224 cupsFreeOptions(num_defaults
, defaults
);
5227 * Close both files and return...
5234 return (cupsFileClose(dst
));
5239 * 'copy_job_attrs()' - Copy job attributes.
5243 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5244 cupsd_job_t
*job
, /* I - Job */
5245 cups_array_t
*ra
) /* I - Requested attributes array */
5247 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
5251 * Send the requested attributes for each job...
5254 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
5255 con
->servername
, con
->serverport
, "/jobs/%d",
5258 if (!ra
|| cupsArrayFind(ra
, "document-count"))
5259 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5260 "document-count", job
->num_files
);
5262 if (!ra
|| cupsArrayFind(ra
, "job-media-progress"))
5263 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5264 "job-media-progress", job
->progress
);
5266 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
5267 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5268 "job-more-info", NULL
, job_uri
);
5270 if (job
->state_value
> IPP_JOB_PROCESSING
&&
5271 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
5272 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
5273 job
->num_files
> 0);
5275 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
5276 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5277 "job-printer-up-time", time(NULL
));
5279 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
5280 add_job_state_reasons(con
, job
);
5282 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
5283 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5284 "job-uri", NULL
, job_uri
);
5286 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
5291 * 'copy_printer_attrs()' - Copy printer attributes.
5296 cupsd_client_t
*con
, /* I - Client connection */
5297 cupsd_printer_t
*printer
, /* I - Printer */
5298 cups_array_t
*ra
) /* I - Requested attributes array */
5300 char printer_uri
[HTTP_MAX_URI
];
5302 time_t curtime
; /* Current time */
5303 int i
; /* Looping var */
5304 ipp_attribute_t
*history
; /* History collection */
5308 * Copy the printer attributes to the response using requested-attributes
5309 * and document-format attributes that may be provided by the client.
5312 curtime
= time(NULL
);
5315 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
5316 printer
->recoverable
)
5317 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5318 "com.apple.print.recoverable-message", NULL
,
5319 printer
->recoverable
);
5320 #endif /* __APPLE__ */
5322 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
5323 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5324 "marker-change-time", printer
->marker_time
);
5326 if (printer
->num_printers
> 0 &&
5327 (!ra
|| cupsArrayFind(ra
, "member-uris")))
5329 ipp_attribute_t
*member_uris
; /* member-uris attribute */
5330 cupsd_printer_t
*p2
; /* Printer in class */
5331 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
5334 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
5335 IPP_TAG_URI
, "member-uris",
5336 printer
->num_printers
, NULL
,
5339 for (i
= 0; i
< printer
->num_printers
; i
++)
5341 p2
= printer
->printers
[i
];
5343 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
5344 IPP_TAG_URI
)) != NULL
)
5345 member_uris
->values
[i
].string
.text
=
5346 _cupsStrAlloc(p2_uri
->values
[0].string
.text
);
5349 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
5350 sizeof(printer_uri
), "ipp", NULL
, con
->servername
,
5352 (p2
->type
& CUPS_PRINTER_CLASS
) ?
5353 "/classes/%s" : "/printers/%s", p2
->name
);
5354 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
5360 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
5361 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
5362 "printer-alert", NULL
, printer
->alert
);
5364 if (printer
->alert_description
&&
5365 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
5366 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5367 "printer-alert-description", NULL
,
5368 printer
->alert_description
);
5370 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
5371 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
5372 ippTimeToDate(curtime
));
5375 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
5377 if (printer
->reg_name
)
5378 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5379 "printer-dns-sd-name", NULL
, printer
->reg_name
);
5381 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
5382 "printer-dns-sd-name", 0);
5384 #endif /* HAVE_DNSSD */
5386 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
5387 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5388 "printer-error-policy", NULL
, printer
->error_policy
);
5390 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
5391 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
5392 printer
->accepting
);
5394 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
5395 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
5398 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
5399 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5400 "printer-op-policy", NULL
, printer
->op_policy
);
5402 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
5403 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
5406 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
5407 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5408 "printer-state-change-time", printer
->state_time
);
5410 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
5411 cupsArrayFind(ra
, "printer-state-history"))
5414 * Printer history is only sent if specifically requested, so that
5415 * older CUPS/IPP clients won't barf on the collection attributes.
5418 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
5419 "printer-state-history",
5420 printer
->num_history
, NULL
);
5422 for (i
= 0; i
< printer
->num_history
; i
++)
5423 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
5424 NULL
, IPP_TAG_ZERO
, 0);
5427 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
5428 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5429 "printer-state-message", NULL
, printer
->state_message
);
5431 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
5432 add_printer_state_reasons(con
, printer
);
5434 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5436 int type
; /* printer-type value */
5440 * Add the CUPS-specific printer-type attribute...
5443 type
= printer
->type
;
5445 if (printer
== DefaultPrinter
)
5446 type
|= CUPS_PRINTER_DEFAULT
;
5448 if (!printer
->accepting
)
5449 type
|= CUPS_PRINTER_REJECTING
;
5451 if (!printer
->shared
)
5452 type
|= CUPS_PRINTER_NOT_SHARED
;
5454 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
5458 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5459 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5460 "printer-up-time", curtime
);
5462 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
5463 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
5466 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5467 "ipp", NULL
, con
->servername
, con
->serverport
,
5468 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5469 "/classes/%s" : "/printers/%s", printer
->name
);
5470 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5471 "printer-uri-supported", NULL
, printer_uri
);
5472 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5476 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5477 add_queued_job_count(con
, printer
);
5479 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
5480 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
5485 * 'copy_subscription_attrs()' - Copy subscription attributes.
5489 copy_subscription_attrs(
5490 cupsd_client_t
*con
, /* I - Client connection */
5491 cupsd_subscription_t
*sub
, /* I - Subscription */
5492 cups_array_t
*ra
) /* I - Requested attributes array */
5494 ipp_attribute_t
*attr
; /* Current attribute */
5495 char printer_uri
[HTTP_MAX_URI
];
5497 int count
; /* Number of events */
5498 unsigned mask
; /* Current event mask */
5499 const char *name
; /* Current event name */
5503 * Copy the subscription attributes to the response using the
5504 * requested-attributes attribute that may be provided by the client.
5507 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
5509 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5512 * Simple event list...
5515 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5516 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5517 "notify-events", NULL
, name
);
5522 * Complex event list...
5525 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5526 if (sub
->mask
& mask
)
5529 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5530 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5531 "notify-events", count
, NULL
, NULL
);
5533 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5534 if (sub
->mask
& mask
)
5536 attr
->values
[count
].string
.text
=
5537 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5544 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5545 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5546 "notify-job-id", sub
->job
->id
);
5548 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
5549 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5550 "notify-lease-duration", sub
->lease
);
5552 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5554 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5555 "ipp", NULL
, con
->servername
, con
->serverport
,
5556 "/printers/%s", sub
->dest
->name
);
5557 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5558 "notify-printer-uri", NULL
, printer_uri
);
5561 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
5562 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5563 "notify-recipient-uri", NULL
, sub
->recipient
);
5564 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
5565 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5566 "notify-pull-method", NULL
, "ippget");
5568 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
5569 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5570 "notify-subscriber-user-name", NULL
, sub
->owner
);
5572 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5573 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5574 "notify-subscription-id", sub
->id
);
5576 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
5577 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5578 "notify-time-interval", sub
->interval
);
5580 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5581 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5582 sub
->user_data
, sub
->user_data_len
);
5587 * 'create_job()' - Print a file to a printer or class.
5591 create_job(cupsd_client_t
*con
, /* I - Client connection */
5592 ipp_attribute_t
*uri
) /* I - Printer URI */
5594 cupsd_printer_t
*printer
; /* Printer */
5595 cupsd_job_t
*job
; /* New job */
5598 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5599 con
->http
.fd
, uri
->values
[0].string
.text
);
5602 * Is the destination valid?
5605 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5611 send_ipp_status(con
, IPP_NOT_FOUND
,
5612 _("The printer or class was not found."));
5617 * Create the job object...
5620 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5623 job
->pending_timeout
= 1;
5626 * Save and log the job...
5629 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5630 job
->dest
, job
->username
);
5635 * 'create_requested_array()' - Create an array for the requested-attributes.
5638 static cups_array_t
* /* O - Array of attributes or NULL */
5639 create_requested_array(ipp_t
*request
) /* I - IPP request */
5641 int i
; /* Looping var */
5642 ipp_attribute_t
*requested
; /* requested-attributes attribute */
5643 cups_array_t
*ra
; /* Requested attributes array */
5644 char *value
; /* Current value */
5648 * Get the requested-attributes attribute, and return NULL if we don't
5652 if ((requested
= ippFindAttribute(request
, "requested-attributes",
5653 IPP_TAG_KEYWORD
)) == NULL
)
5657 * If the attribute contains a single "all" keyword, return NULL...
5660 if (requested
->num_values
== 1 &&
5661 !strcmp(requested
->values
[0].string
.text
, "all"))
5665 * Create an array using "strcmp" as the comparison function...
5668 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
5670 for (i
= 0; i
< requested
->num_values
; i
++)
5672 value
= requested
->values
[i
].string
.text
;
5674 if (!strcmp(value
, "job-template"))
5676 cupsArrayAdd(ra
, "copies");
5677 cupsArrayAdd(ra
, "copies-default");
5678 cupsArrayAdd(ra
, "copies-supported");
5679 cupsArrayAdd(ra
, "finishings");
5680 cupsArrayAdd(ra
, "finishings-default");
5681 cupsArrayAdd(ra
, "finishings-supported");
5682 cupsArrayAdd(ra
, "job-hold-until");
5683 cupsArrayAdd(ra
, "job-hold-until-default");
5684 cupsArrayAdd(ra
, "job-hold-until-supported");
5685 cupsArrayAdd(ra
, "job-priority");
5686 cupsArrayAdd(ra
, "job-priority-default");
5687 cupsArrayAdd(ra
, "job-priority-supported");
5688 cupsArrayAdd(ra
, "job-sheets");
5689 cupsArrayAdd(ra
, "job-sheets-default");
5690 cupsArrayAdd(ra
, "job-sheets-supported");
5691 cupsArrayAdd(ra
, "media");
5692 cupsArrayAdd(ra
, "media-default");
5693 cupsArrayAdd(ra
, "media-supported");
5694 cupsArrayAdd(ra
, "multiple-document-handling");
5695 cupsArrayAdd(ra
, "multiple-document-handling-default");
5696 cupsArrayAdd(ra
, "multiple-document-handling-supported");
5697 cupsArrayAdd(ra
, "number-up");
5698 cupsArrayAdd(ra
, "number-up-default");
5699 cupsArrayAdd(ra
, "number-up-supported");
5700 cupsArrayAdd(ra
, "orientation-requested");
5701 cupsArrayAdd(ra
, "orientation-requested-default");
5702 cupsArrayAdd(ra
, "orientation-requested-supported");
5703 cupsArrayAdd(ra
, "page-ranges");
5704 cupsArrayAdd(ra
, "page-ranges-supported");
5705 cupsArrayAdd(ra
, "printer-resolution");
5706 cupsArrayAdd(ra
, "printer-resolution-default");
5707 cupsArrayAdd(ra
, "printer-resolution-supported");
5708 cupsArrayAdd(ra
, "print-quality");
5709 cupsArrayAdd(ra
, "print-quality-default");
5710 cupsArrayAdd(ra
, "print-quality-supported");
5711 cupsArrayAdd(ra
, "sides");
5712 cupsArrayAdd(ra
, "sides-default");
5713 cupsArrayAdd(ra
, "sides-supported");
5715 else if (!strcmp(value
, "job-description"))
5717 cupsArrayAdd(ra
, "date-time-at-completed");
5718 cupsArrayAdd(ra
, "date-time-at-creation");
5719 cupsArrayAdd(ra
, "date-time-at-processing");
5720 cupsArrayAdd(ra
, "job-detailed-status-message");
5721 cupsArrayAdd(ra
, "job-document-access-errors");
5722 cupsArrayAdd(ra
, "job-id");
5723 cupsArrayAdd(ra
, "job-impressions");
5724 cupsArrayAdd(ra
, "job-impressions-completed");
5725 cupsArrayAdd(ra
, "job-k-octets");
5726 cupsArrayAdd(ra
, "job-k-octets-processed");
5727 cupsArrayAdd(ra
, "job-media-progress");
5728 cupsArrayAdd(ra
, "job-media-sheets");
5729 cupsArrayAdd(ra
, "job-media-sheets-completed");
5730 cupsArrayAdd(ra
, "job-message-from-operator");
5731 cupsArrayAdd(ra
, "job-more-info");
5732 cupsArrayAdd(ra
, "job-name");
5733 cupsArrayAdd(ra
, "job-originating-user-name");
5734 cupsArrayAdd(ra
, "job-printer-up-time");
5735 cupsArrayAdd(ra
, "job-printer-uri");
5736 cupsArrayAdd(ra
, "job-state");
5737 cupsArrayAdd(ra
, "job-state-message");
5738 cupsArrayAdd(ra
, "job-state-reasons");
5739 cupsArrayAdd(ra
, "job-uri");
5740 cupsArrayAdd(ra
, "number-of-documents");
5741 cupsArrayAdd(ra
, "number-of-intervening-jobs");
5742 cupsArrayAdd(ra
, "output-device-assigned");
5743 cupsArrayAdd(ra
, "time-at-completed");
5744 cupsArrayAdd(ra
, "time-at-creation");
5745 cupsArrayAdd(ra
, "time-at-processing");
5747 else if (!strcmp(value
, "printer-description"))
5749 cupsArrayAdd(ra
, "charset-configured");
5750 cupsArrayAdd(ra
, "charset-supported");
5751 cupsArrayAdd(ra
, "color-supported");
5752 cupsArrayAdd(ra
, "compression-supported");
5753 cupsArrayAdd(ra
, "document-format-default");
5754 cupsArrayAdd(ra
, "document-format-supported");
5755 cupsArrayAdd(ra
, "generated-natural-language-supported");
5756 cupsArrayAdd(ra
, "ipp-versions-supported");
5757 cupsArrayAdd(ra
, "job-impressions-supported");
5758 cupsArrayAdd(ra
, "job-k-octets-supported");
5759 cupsArrayAdd(ra
, "job-media-sheets-supported");
5760 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
5761 cupsArrayAdd(ra
, "multiple-operation-time-out");
5762 cupsArrayAdd(ra
, "natural-language-configured");
5763 cupsArrayAdd(ra
, "notify-attributes-supported");
5764 cupsArrayAdd(ra
, "notify-lease-duration-default");
5765 cupsArrayAdd(ra
, "notify-lease-duration-supported");
5766 cupsArrayAdd(ra
, "notify-max-events-supported");
5767 cupsArrayAdd(ra
, "notify-events-default");
5768 cupsArrayAdd(ra
, "notify-events-supported");
5769 cupsArrayAdd(ra
, "notify-pull-method-supported");
5770 cupsArrayAdd(ra
, "notify-schemes-supported");
5771 cupsArrayAdd(ra
, "operations-supported");
5772 cupsArrayAdd(ra
, "pages-per-minute");
5773 cupsArrayAdd(ra
, "pages-per-minute-color");
5774 cupsArrayAdd(ra
, "pdl-override-supported");
5775 cupsArrayAdd(ra
, "printer-alert");
5776 cupsArrayAdd(ra
, "printer-alert-description");
5777 cupsArrayAdd(ra
, "printer-commands");
5778 cupsArrayAdd(ra
, "printer-current-time");
5779 cupsArrayAdd(ra
, "printer-driver-installer");
5780 cupsArrayAdd(ra
, "printer-dns-sd-name");
5781 cupsArrayAdd(ra
, "printer-info");
5782 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
5783 cupsArrayAdd(ra
, "printer-location");
5784 cupsArrayAdd(ra
, "printer-make-and-model");
5785 cupsArrayAdd(ra
, "printer-message-from-operator");
5786 cupsArrayAdd(ra
, "printer-more-info");
5787 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
5788 cupsArrayAdd(ra
, "printer-name");
5789 cupsArrayAdd(ra
, "printer-state");
5790 cupsArrayAdd(ra
, "printer-state-message");
5791 cupsArrayAdd(ra
, "printer-state-reasons");
5792 cupsArrayAdd(ra
, "printer-type");
5793 cupsArrayAdd(ra
, "printer-up-time");
5794 cupsArrayAdd(ra
, "printer-uri-supported");
5795 cupsArrayAdd(ra
, "queued-job-count");
5796 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
5797 cupsArrayAdd(ra
, "uri-authentication-supported");
5798 cupsArrayAdd(ra
, "uri-security-supported");
5800 else if (!strcmp(value
, "printer-defaults"))
5802 char *name
; /* Option name */
5805 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5807 name
= (char *)cupsArrayNext(CommonDefaults
))
5808 cupsArrayAdd(ra
, name
);
5810 else if (!strcmp(value
, "subscription-template"))
5812 cupsArrayAdd(ra
, "notify-attributes");
5813 cupsArrayAdd(ra
, "notify-charset");
5814 cupsArrayAdd(ra
, "notify-events");
5815 cupsArrayAdd(ra
, "notify-lease-duration");
5816 cupsArrayAdd(ra
, "notify-natural-language");
5817 cupsArrayAdd(ra
, "notify-pull-method");
5818 cupsArrayAdd(ra
, "notify-recipient-uri");
5819 cupsArrayAdd(ra
, "notify-time-interval");
5820 cupsArrayAdd(ra
, "notify-user-data");
5823 cupsArrayAdd(ra
, value
);
5831 * 'create_subscription()' - Create a notification subscription.
5835 create_subscription(
5836 cupsd_client_t
*con
, /* I - Client connection */
5837 ipp_attribute_t
*uri
) /* I - Printer URI */
5839 http_status_t status
; /* Policy status */
5840 int i
; /* Looping var */
5841 ipp_attribute_t
*attr
; /* Current attribute */
5842 cups_ptype_t dtype
; /* Destination type (printer/class) */
5843 char scheme
[HTTP_MAX_URI
],
5844 /* Scheme portion of URI */
5845 userpass
[HTTP_MAX_URI
],
5846 /* Username portion of URI */
5848 /* Host portion of URI */
5849 resource
[HTTP_MAX_URI
];
5850 /* Resource portion of URI */
5851 int port
; /* Port portion of URI */
5852 cupsd_printer_t
*printer
; /* Printer/class */
5853 cupsd_job_t
*job
; /* Job */
5854 int jobid
; /* Job ID */
5855 cupsd_subscription_t
*sub
; /* Subscription object */
5856 const char *username
, /* requesting-user-name or
5857 authenticated username */
5858 *recipient
, /* notify-recipient-uri */
5859 *pullmethod
; /* notify-pull-method */
5860 ipp_attribute_t
*user_data
; /* notify-user-data */
5861 int interval
, /* notify-time-interval */
5862 lease
; /* notify-lease-duration */
5863 unsigned mask
; /* notify-events */
5864 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5865 *notify_lease
; /* notify-lease-duration(-default) */
5869 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5871 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5872 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5873 attr
->value_tag
, attr
->name
);
5875 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5880 * Is the destination valid?
5883 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5884 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5885 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5887 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5888 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5889 sizeof(host
), &port
, resource
, sizeof(resource
));
5891 if (!strcmp(resource
, "/"))
5893 dtype
= (cups_ptype_t
)0;
5896 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5898 dtype
= (cups_ptype_t
)0;
5901 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5903 dtype
= CUPS_PRINTER_CLASS
;
5906 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5912 send_ipp_status(con
, IPP_NOT_FOUND
,
5913 _("The printer or class was not found."));
5923 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5926 send_http_error(con
, status
, printer
);
5930 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5932 send_http_error(con
, status
, NULL
);
5937 * Get the user that is requesting the subscription...
5940 username
= get_username(con
);
5943 * Find the first subscription group attribute; return if we have
5947 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5948 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5953 send_ipp_status(con
, IPP_BAD_REQUEST
,
5954 _("No subscription attributes in request!"));
5959 * Process the subscription attributes in the request...
5962 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5970 lease
= DefaultLeaseDuration
;
5972 mask
= CUPSD_EVENT_NONE
;
5976 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5978 notify_lease
= ippFindAttribute(printer
->attrs
,
5979 "notify-lease-duration-default",
5983 lease
= notify_lease
->values
[0].integer
;
5987 notify_events
= NULL
;
5988 notify_lease
= NULL
;
5991 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5993 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5994 attr
->value_tag
== IPP_TAG_URI
)
5997 * Validate the recipient scheme against the ServerBin/notifier
6001 char notifier
[1024]; /* Notifier filename */
6004 recipient
= attr
->values
[0].string
.text
;
6006 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
6007 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
6008 host
, sizeof(host
), &port
,
6009 resource
, sizeof(resource
)) < HTTP_URI_OK
)
6011 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6012 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
6013 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6014 "notify-status-code", IPP_URI_SCHEME
);
6018 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
6020 if (access(notifier
, X_OK
))
6022 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6023 _("notify-recipient-uri URI \"%s\" uses unknown "
6024 "scheme!"), recipient
);
6025 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6026 "notify-status-code", IPP_URI_SCHEME
);
6030 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
6032 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6033 _("notify-recipient-uri URI \"%s\" is already used!"),
6035 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6036 "notify-status-code", IPP_ATTRIBUTES
);
6040 else if (!strcmp(attr
->name
, "notify-pull-method") &&
6041 attr
->value_tag
== IPP_TAG_KEYWORD
)
6043 pullmethod
= attr
->values
[0].string
.text
;
6045 if (strcmp(pullmethod
, "ippget"))
6047 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6048 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
6049 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6050 "notify-status-code", IPP_ATTRIBUTES
);
6054 else if (!strcmp(attr
->name
, "notify-charset") &&
6055 attr
->value_tag
== IPP_TAG_CHARSET
&&
6056 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
6057 strcmp(attr
->values
[0].string
.text
, "utf-8"))
6059 send_ipp_status(con
, IPP_CHARSET
,
6060 _("Character set \"%s\" not supported!"),
6061 attr
->values
[0].string
.text
);
6064 else if (!strcmp(attr
->name
, "notify-natural-language") &&
6065 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
6066 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
6068 send_ipp_status(con
, IPP_CHARSET
,
6069 _("Language \"%s\" not supported!"),
6070 attr
->values
[0].string
.text
);
6073 else if (!strcmp(attr
->name
, "notify-user-data") &&
6074 attr
->value_tag
== IPP_TAG_STRING
)
6076 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
6078 send_ipp_status(con
, IPP_REQUEST_VALUE
,
6079 _("The notify-user-data value is too large "
6080 "(%d > 63 octets)!"),
6081 attr
->values
[0].unknown
.length
);
6087 else if (!strcmp(attr
->name
, "notify-events") &&
6088 attr
->value_tag
== IPP_TAG_KEYWORD
)
6089 notify_events
= attr
;
6090 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
6091 attr
->value_tag
== IPP_TAG_INTEGER
)
6092 lease
= attr
->values
[0].integer
;
6093 else if (!strcmp(attr
->name
, "notify-time-interval") &&
6094 attr
->value_tag
== IPP_TAG_INTEGER
)
6095 interval
= attr
->values
[0].integer
;
6096 else if (!strcmp(attr
->name
, "notify-job-id") &&
6097 attr
->value_tag
== IPP_TAG_INTEGER
)
6098 jobid
= attr
->values
[0].integer
;
6105 for (i
= 0; i
< notify_events
->num_values
; i
++)
6106 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
6110 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
6112 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
6113 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
6114 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
6116 if (!recipient
&& !pullmethod
)
6119 if (mask
== CUPSD_EVENT_NONE
)
6122 mask
= CUPSD_EVENT_JOB_COMPLETED
;
6124 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
6127 send_ipp_status(con
, IPP_BAD_REQUEST
,
6128 _("notify-events not specified!"));
6133 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
6135 cupsdLogMessage(CUPSD_LOG_INFO
,
6136 "create_subscription: Limiting notify-lease-duration to "
6139 lease
= MaxLeaseDuration
;
6144 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6146 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
6153 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
6156 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
6159 cupsdLogMessage(CUPSD_LOG_DEBUG
,
6160 "Added subscription %d for printer \"%s\"",
6161 sub
->id
, printer
->name
);
6163 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
6166 sub
->interval
= interval
;
6168 sub
->expire
= lease
? time(NULL
) + lease
: 0;
6170 cupsdSetString(&sub
->owner
, username
);
6174 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
6175 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
6176 sub
->user_data_len
);
6179 ippAddSeparator(con
->response
);
6180 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
6181 "notify-subscription-id", sub
->id
);
6183 con
->response
->request
.status
.status_code
= IPP_OK
;
6189 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
6194 * 'delete_printer()' - Remove a printer or class from the system.
6198 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
6199 ipp_attribute_t
*uri
) /* I - URI of printer or class */
6201 http_status_t status
; /* Policy status */
6202 cups_ptype_t dtype
; /* Destination type (printer/class) */
6203 cupsd_printer_t
*printer
; /* Printer/class */
6204 char filename
[1024]; /* Script/PPD filename */
6207 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
6208 con
->http
.fd
, uri
->values
[0].string
.text
);
6211 * Do we have a valid URI?
6214 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6220 send_ipp_status(con
, IPP_NOT_FOUND
,
6221 _("The printer or class was not found."));
6229 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6231 send_http_error(con
, status
, NULL
);
6236 * Remove old jobs...
6239 cupsdCancelJobs(printer
->name
, NULL
, 1);
6242 * Remove old subscriptions and send a "deleted printer" event...
6245 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
6246 "%s \"%s\" deleted by \"%s\".",
6247 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
6248 printer
->name
, get_username(con
));
6250 cupsdExpireSubscriptions(printer
, NULL
);
6253 * Remove any old PPD or script files...
6256 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
6260 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6266 * Unregister color profiles...
6269 apple_unregister_profiles(printer
);
6270 #endif /* __APPLE__ */
6272 if (dtype
& CUPS_PRINTER_CLASS
)
6274 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
6275 printer
->name
, get_username(con
));
6277 cupsdDeletePrinter(printer
, 0);
6278 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6282 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
6283 printer
->name
, get_username(con
));
6285 cupsdDeletePrinter(printer
, 0);
6286 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
6289 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
6292 * Return with no errors...
6295 con
->response
->request
.status
.status_code
= IPP_OK
;
6300 * 'get_default()' - Get the default destination.
6304 get_default(cupsd_client_t
*con
) /* I - Client connection */
6306 http_status_t status
; /* Policy status */
6307 cups_array_t
*ra
; /* Requested attributes array */
6310 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
6316 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6318 send_http_error(con
, status
, NULL
);
6324 ra
= create_requested_array(con
->request
);
6326 copy_printer_attrs(con
, DefaultPrinter
, ra
);
6328 cupsArrayDelete(ra
);
6330 con
->response
->request
.status
.status_code
= IPP_OK
;
6333 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
6338 * 'get_devices()' - Get the list of available devices on the local system.
6342 get_devices(cupsd_client_t
*con
) /* I - Client connection */
6344 http_status_t status
; /* Policy status */
6345 ipp_attribute_t
*limit
, /* limit attribute */
6346 *timeout
, /* timeout attribute */
6347 *requested
, /* requested-attributes attribute */
6348 *exclude
; /* exclude-schemes attribute */
6349 char command
[1024], /* cups-deviced command */
6350 options
[1024], /* Options to pass to command */
6352 /* String for requested attributes */
6354 /* String for excluded attributes */
6357 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
6363 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6365 send_http_error(con
, status
, NULL
);
6370 * Run cups-deviced command with the given options...
6373 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6374 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
6375 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6377 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
6380 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6382 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6385 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6387 exclude_str
[0] = '\0';
6389 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
6390 snprintf(options
, sizeof(options
),
6391 "%d+%d+%d+%d+%s%s%s",
6392 con
->request
->request
.op
.request_id
,
6393 limit
? limit
->values
[0].integer
: 0,
6394 timeout
? timeout
->values
[0].integer
: 10,
6397 exclude_str
[0] ? "%20" : "", exclude_str
);
6399 if (cupsdSendCommand(con
, command
, options
, 1))
6402 * Command started successfully, don't send an IPP response here...
6405 ippDelete(con
->response
);
6406 con
->response
= NULL
;
6411 * Command failed, return "internal error" so the user knows something
6415 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6416 _("cups-deviced failed to execute."));
6422 * 'get_document()' - Get a copy of a job file.
6426 get_document(cupsd_client_t
*con
, /* I - Client connection */
6427 ipp_attribute_t
*uri
) /* I - Job URI */
6429 http_status_t status
; /* Policy status */
6430 ipp_attribute_t
*attr
; /* Current attribute */
6431 int jobid
; /* Job ID */
6432 int docnum
; /* Document number */
6433 cupsd_job_t
*job
; /* Current job */
6434 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6435 username
[HTTP_MAX_URI
], /* Username portion of URI */
6436 host
[HTTP_MAX_URI
], /* Host portion of URI */
6437 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6438 int port
; /* Port portion of URI */
6439 char filename
[1024], /* Filename for document */
6440 format
[1024]; /* Format for document */
6443 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6444 con
->http
.fd
, uri
->values
[0].string
.text
);
6447 * See if we have a job URI or a printer URI...
6450 if (!strcmp(uri
->name
, "printer-uri"))
6453 * Got a printer URI; see if we also have a job-id attribute...
6456 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6457 IPP_TAG_INTEGER
)) == NULL
)
6459 send_ipp_status(con
, IPP_BAD_REQUEST
,
6460 _("Got a printer-uri attribute but no job-id!"));
6464 jobid
= attr
->values
[0].integer
;
6469 * Got a job URI; parse it to get the job ID...
6472 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6473 sizeof(method
), username
, sizeof(username
), host
,
6474 sizeof(host
), &port
, resource
, sizeof(resource
));
6476 if (strncmp(resource
, "/jobs/", 6))
6482 send_ipp_status(con
, IPP_BAD_REQUEST
,
6483 _("Bad job-uri attribute \"%s\"!"),
6484 uri
->values
[0].string
.text
);
6488 jobid
= atoi(resource
+ 6);
6492 * See if the job exists...
6495 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6498 * Nope - return a "not found" error...
6501 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6509 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6511 send_http_error(con
, status
, NULL
);
6516 * Get the document number...
6519 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6520 IPP_TAG_INTEGER
)) == NULL
)
6522 send_ipp_status(con
, IPP_BAD_REQUEST
,
6523 _("Missing document-number attribute!"));
6527 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6528 attr
->num_values
> 1)
6530 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
6535 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6537 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6539 cupsdLogMessage(CUPSD_LOG_ERROR
,
6540 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6542 send_ipp_status(con
, IPP_NOT_FOUND
,
6543 _("Unable to open document %d in job %d!"), docnum
, jobid
);
6547 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6551 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6552 job
->filetypes
[docnum
- 1]->type
);
6554 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6556 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6558 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6559 IPP_TAG_NAME
)) != NULL
)
6560 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6561 NULL
, attr
->values
[0].string
.text
);
6566 * 'get_job_attrs()' - Get job attributes.
6570 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6571 ipp_attribute_t
*uri
) /* I - Job URI */
6573 http_status_t status
; /* Policy status */
6574 ipp_attribute_t
*attr
; /* Current attribute */
6575 int jobid
; /* Job ID */
6576 cupsd_job_t
*job
; /* Current job */
6577 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6578 username
[HTTP_MAX_URI
], /* Username portion of URI */
6579 host
[HTTP_MAX_URI
], /* Host portion of URI */
6580 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6581 int port
; /* Port portion of URI */
6582 cups_array_t
*ra
; /* Requested attributes array */
6585 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6586 con
->http
.fd
, uri
->values
[0].string
.text
);
6589 * See if we have a job URI or a printer URI...
6592 if (!strcmp(uri
->name
, "printer-uri"))
6595 * Got a printer URI; see if we also have a job-id attribute...
6598 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6599 IPP_TAG_INTEGER
)) == NULL
)
6601 send_ipp_status(con
, IPP_BAD_REQUEST
,
6602 _("Got a printer-uri attribute but no job-id!"));
6606 jobid
= attr
->values
[0].integer
;
6611 * Got a job URI; parse it to get the job ID...
6614 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6615 sizeof(method
), username
, sizeof(username
), host
,
6616 sizeof(host
), &port
, resource
, sizeof(resource
));
6618 if (strncmp(resource
, "/jobs/", 6))
6624 send_ipp_status(con
, IPP_BAD_REQUEST
,
6625 _("Bad job-uri attribute \"%s\"!"),
6626 uri
->values
[0].string
.text
);
6630 jobid
= atoi(resource
+ 6);
6634 * See if the job exists...
6637 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6640 * Nope - return a "not found" error...
6643 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6651 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6653 send_http_error(con
, status
, NULL
);
6658 * Copy attributes...
6663 ra
= create_requested_array(con
->request
);
6664 copy_job_attrs(con
, job
, ra
);
6665 cupsArrayDelete(ra
);
6667 con
->response
->request
.status
.status_code
= IPP_OK
;
6672 * 'get_jobs()' - Get a list of jobs for the specified printer.
6676 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6677 ipp_attribute_t
*uri
) /* I - Printer URI */
6679 http_status_t status
; /* Policy status */
6680 ipp_attribute_t
*attr
; /* Current attribute */
6681 const char *dest
; /* Destination */
6682 cups_ptype_t dtype
; /* Destination type (printer/class) */
6683 cups_ptype_t dmask
; /* Destination type mask */
6684 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6685 username
[HTTP_MAX_URI
], /* Username portion of URI */
6686 host
[HTTP_MAX_URI
], /* Host portion of URI */
6687 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6688 int port
; /* Port portion of URI */
6689 int completed
; /* Completed jobs? */
6690 int first_job_id
; /* First job ID */
6691 int limit
; /* Maximum number of jobs to return */
6692 int count
; /* Number of jobs that match */
6693 cupsd_job_t
*job
; /* Current job pointer */
6694 cupsd_printer_t
*printer
; /* Printer */
6695 cups_array_t
*list
; /* Which job list... */
6696 cups_array_t
*ra
; /* Requested attributes array */
6699 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
6700 uri
->values
[0].string
.text
);
6703 * Is the destination valid?
6706 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6707 sizeof(scheme
), username
, sizeof(username
), host
,
6708 sizeof(host
), &port
, resource
, sizeof(resource
));
6710 if (!strcmp(resource
, "/") ||
6711 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
6714 dtype
= (cups_ptype_t
)0;
6715 dmask
= (cups_ptype_t
)0;
6718 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6721 dtype
= (cups_ptype_t
)0;
6722 dmask
= CUPS_PRINTER_CLASS
;
6725 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6728 dtype
= CUPS_PRINTER_CLASS
;
6729 dmask
= CUPS_PRINTER_CLASS
;
6732 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6739 send_ipp_status(con
, IPP_NOT_FOUND
,
6740 _("The printer or class was not found."));
6745 dtype
&= CUPS_PRINTER_CLASS
;
6746 dmask
= CUPS_PRINTER_CLASS
;
6755 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6758 send_http_error(con
, status
, printer
);
6762 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6764 send_http_error(con
, status
, NULL
);
6769 * See if the "which-jobs" attribute have been specified...
6772 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6773 IPP_TAG_KEYWORD
)) != NULL
&&
6774 !strcmp(attr
->values
[0].string
.text
, "completed"))
6779 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
6784 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "printing"))
6787 list
= PrintingJobs
;
6796 * See if they want to limit the number of jobs reported...
6799 if ((attr
= ippFindAttribute(con
->request
, "limit",
6800 IPP_TAG_INTEGER
)) != NULL
)
6801 limit
= attr
->values
[0].integer
;
6805 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
6806 IPP_TAG_INTEGER
)) != NULL
)
6807 first_job_id
= attr
->values
[0].integer
;
6812 * See if we only want to see jobs for a specific user...
6815 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
6816 IPP_TAG_BOOLEAN
)) != NULL
&&
6817 attr
->values
[0].boolean
)
6818 strlcpy(username
, get_username(con
), sizeof(username
));
6822 ra
= create_requested_array(con
->request
);
6825 * OK, build a list of jobs for this printer...
6828 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6829 (limit
<= 0 || count
< limit
) && job
;
6830 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6833 * Filter out jobs that don't match...
6836 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
6838 if (!job
->dest
|| !job
->username
)
6841 if (!job
->dest
|| !job
->username
)
6844 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6845 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6847 if ((job
->dtype
& dmask
) != dtype
&&
6848 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6850 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
6853 if (job
->id
< first_job_id
)
6861 if (username
[0] && strcasecmp(username
, job
->username
))
6865 ippAddSeparator(con
->response
);
6869 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
6871 copy_job_attrs(con
, job
, ra
);
6874 cupsArrayDelete(ra
);
6876 con
->response
->request
.status
.status_code
= IPP_OK
;
6881 * 'get_notifications()' - Get events for a subscription.
6885 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6887 int i
, j
; /* Looping vars */
6888 http_status_t status
; /* Policy status */
6889 cupsd_subscription_t
*sub
; /* Subscription */
6890 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6891 *sequences
; /* notify-sequence-numbers */
6892 int min_seq
; /* Minimum sequence number */
6893 int interval
; /* Poll interval */
6896 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6900 * Get subscription attributes...
6903 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6905 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6910 send_ipp_status(con
, IPP_BAD_REQUEST
,
6911 _("Missing notify-subscription-ids attribute!"));
6916 * Are the subscription IDs valid?
6919 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6921 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6924 * Bad subscription ID...
6927 send_ipp_status(con
, IPP_NOT_FOUND
,
6928 _("notify-subscription-id %d no good!"),
6929 ids
->values
[i
].integer
);
6937 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6939 con
, sub
->owner
)) != HTTP_OK
)
6941 send_http_error(con
, status
, sub
->dest
);
6946 * Check the subscription type and update the interval accordingly.
6949 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6952 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6954 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6960 * Tell the client to poll again in N seconds...
6964 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6965 "notify-get-interval", interval
);
6967 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6968 "printer-up-time", time(NULL
));
6971 * Copy the subscription event attributes to the response.
6974 con
->response
->request
.status
.status_code
=
6975 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6977 for (i
= 0; i
< ids
->num_values
; i
++)
6980 * Get the subscription and sequence number...
6983 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6985 if (sequences
&& i
< sequences
->num_values
)
6986 min_seq
= sequences
->values
[i
].integer
;
6991 * If we don't have any new events, nothing to do here...
6994 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
6998 * Otherwise copy all of the new events...
7001 if (sub
->first_event_id
> min_seq
)
7004 j
= min_seq
- sub
->first_event_id
;
7006 for (; j
< sub
->num_events
; j
++)
7008 ippAddSeparator(con
->response
);
7010 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
7011 IPP_TAG_EVENT_NOTIFICATION
, 0);
7018 * 'get_ppd()' - Get a named PPD from the local system.
7022 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
7023 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
7025 http_status_t status
; /* Policy status */
7026 cupsd_printer_t
*dest
; /* Destination */
7027 cups_ptype_t dtype
; /* Destination type */
7030 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
7031 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
7033 if (!strcmp(uri
->name
, "ppd-name"))
7036 * Return a PPD file from cups-driverd...
7039 char command
[1024], /* cups-driverd command */
7040 options
[1024], /* Options to pass to command */
7041 ppd_name
[1024]; /* ppd-name */
7048 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7050 send_http_error(con
, status
, NULL
);
7055 * Run cups-driverd command with the given options...
7058 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7059 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
7060 snprintf(options
, sizeof(options
), "get+%d+%s",
7061 con
->request
->request
.op
.request_id
, ppd_name
);
7063 if (cupsdSendCommand(con
, command
, options
, 0))
7066 * Command started successfully, don't send an IPP response here...
7069 ippDelete(con
->response
);
7070 con
->response
= NULL
;
7075 * Command failed, return "internal error" so the user knows something
7079 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7080 _("cups-driverd failed to execute."));
7083 else if (!strcmp(uri
->name
, "printer-uri") &&
7084 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
7086 int i
; /* Looping var */
7087 char filename
[1024]; /* PPD filename */
7094 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7096 send_http_error(con
, status
, dest
);
7101 * See if we need the PPD for a class or remote printer...
7104 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7107 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
7109 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7110 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7111 "printer-uri", NULL
, dest
->uri
);
7114 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
7116 for (i
= 0; i
< dest
->num_printers
; i
++)
7117 if (!(dest
->printers
[i
]->type
&
7118 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
7120 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7121 dest
->printers
[i
]->name
);
7123 if (!access(filename
, 0))
7127 if (i
< dest
->num_printers
)
7128 dest
= dest
->printers
[i
];
7131 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7132 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7133 "printer-uri", NULL
, dest
->printers
[0]->uri
);
7139 * Found the printer with the PPD file, now see if there is one...
7142 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
7144 send_ipp_status(con
, IPP_NOT_FOUND
,
7145 _("The PPD file \"%s\" could not be opened: %s"),
7146 uri
->values
[0].string
.text
, strerror(errno
));
7150 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
7154 con
->response
->request
.status
.status_code
= IPP_OK
;
7157 send_ipp_status(con
, IPP_NOT_FOUND
,
7158 _("The PPD file \"%s\" could not be found."),
7159 uri
->values
[0].string
.text
);
7164 * 'get_ppds()' - Get the list of PPD files on the local system.
7168 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
7170 http_status_t status
; /* Policy status */
7171 ipp_attribute_t
*limit
, /* Limit attribute */
7172 *device
, /* ppd-device-id attribute */
7173 *language
, /* ppd-natural-language attribute */
7174 *make
, /* ppd-make attribute */
7175 *model
, /* ppd-make-and-model attribute */
7176 *model_number
, /* ppd-model-number attribute */
7177 *product
, /* ppd-product attribute */
7178 *psversion
, /* ppd-psverion attribute */
7179 *type
, /* ppd-type attribute */
7180 *requested
; /* requested-attributes attribute */
7181 char command
[1024], /* cups-driverd command */
7182 options
[1024], /* Options to pass to command */
7183 device_str
[256],/* Escaped ppd-device-id string */
7185 /* Escaped ppd-natural-language */
7186 make_str
[256], /* Escaped ppd-make string */
7187 model_str
[256], /* Escaped ppd-make-and-model string */
7188 model_number_str
[256],
7189 /* ppd-model-number string */
7191 /* Escaped ppd-product string */
7193 /* Escaped ppd-psversion string */
7194 type_str
[256], /* Escaped ppd-type string */
7196 /* String for requested attributes */
7199 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
7205 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7207 send_http_error(con
, status
, NULL
);
7212 * Run cups-driverd command with the given options...
7215 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
7216 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
7217 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
7219 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
7220 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
7222 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
7224 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
7225 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
7226 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
7227 requested
= ippFindAttribute(con
->request
, "requested-attributes",
7231 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
7233 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
7236 url_encode_attr(device
, device_str
, sizeof(device_str
));
7238 device_str
[0] = '\0';
7241 url_encode_attr(language
, language_str
, sizeof(language_str
));
7243 language_str
[0] = '\0';
7246 url_encode_attr(make
, make_str
, sizeof(make_str
));
7251 url_encode_attr(model
, model_str
, sizeof(model_str
));
7253 model_str
[0] = '\0';
7256 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
7257 model_number
->values
[0].integer
);
7259 model_number_str
[0] = '\0';
7262 url_encode_attr(product
, product_str
, sizeof(product_str
));
7264 product_str
[0] = '\0';
7267 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
7269 psversion_str
[0] = '\0';
7272 url_encode_attr(type
, type_str
, sizeof(type_str
));
7276 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7277 snprintf(options
, sizeof(options
),
7278 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7279 con
->request
->request
.op
.request_id
,
7280 limit
? limit
->values
[0].integer
: 0,
7282 device
? "%20" : "", device_str
,
7283 language
? "%20" : "", language_str
,
7284 make
? "%20" : "", make_str
,
7285 model
? "%20" : "", model_str
,
7286 model_number
? "%20" : "", model_number_str
,
7287 product
? "%20" : "", product_str
,
7288 psversion
? "%20" : "", psversion_str
,
7289 type
? "%20" : "", type_str
);
7291 if (cupsdSendCommand(con
, command
, options
, 0))
7294 * Command started successfully, don't send an IPP response here...
7297 ippDelete(con
->response
);
7298 con
->response
= NULL
;
7303 * Command failed, return "internal error" so the user knows something
7307 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7308 _("cups-driverd failed to execute."));
7314 * 'get_printer_attrs()' - Get printer attributes.
7318 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
7319 ipp_attribute_t
*uri
) /* I - Printer URI */
7321 http_status_t status
; /* Policy status */
7322 cups_ptype_t dtype
; /* Destination type (printer/class) */
7323 cupsd_printer_t
*printer
; /* Printer/class */
7324 cups_array_t
*ra
; /* Requested attributes array */
7327 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
7328 con
->http
.fd
, uri
->values
[0].string
.text
);
7331 * Is the destination valid?
7334 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7340 send_ipp_status(con
, IPP_NOT_FOUND
,
7341 _("The printer or class was not found."));
7349 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7351 send_http_error(con
, status
, printer
);
7356 * Send the attributes...
7359 ra
= create_requested_array(con
->request
);
7361 copy_printer_attrs(con
, printer
, ra
);
7363 cupsArrayDelete(ra
);
7365 con
->response
->request
.status
.status_code
= IPP_OK
;
7370 * 'get_printers()' - Get a list of printers or classes.
7374 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7375 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7377 http_status_t status
; /* Policy status */
7378 ipp_attribute_t
*attr
; /* Current attribute */
7379 int limit
; /* Max number of printers to return */
7380 int count
; /* Number of printers that match */
7381 cupsd_printer_t
*printer
; /* Current printer pointer */
7382 int printer_type
, /* printer-type attribute */
7383 printer_mask
; /* printer-type-mask attribute */
7384 char *location
; /* Location string */
7385 const char *username
; /* Current user */
7386 char *first_printer_name
; /* first-printer-name attribute */
7387 cups_array_t
*ra
; /* Requested attributes array */
7390 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7391 con
->http
.fd
, type
);
7397 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7399 send_http_error(con
, status
, NULL
);
7404 * Check for printers...
7407 if (!Printers
|| !cupsArrayCount(Printers
))
7409 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7414 * See if they want to limit the number of printers reported...
7417 if ((attr
= ippFindAttribute(con
->request
, "limit",
7418 IPP_TAG_INTEGER
)) != NULL
)
7419 limit
= attr
->values
[0].integer
;
7423 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7424 IPP_TAG_NAME
)) != NULL
)
7425 first_printer_name
= attr
->values
[0].string
.text
;
7427 first_printer_name
= NULL
;
7430 * Support filtering...
7433 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7434 IPP_TAG_ENUM
)) != NULL
)
7435 printer_type
= attr
->values
[0].integer
;
7439 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7440 IPP_TAG_ENUM
)) != NULL
)
7441 printer_mask
= attr
->values
[0].integer
;
7445 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7446 IPP_TAG_TEXT
)) != NULL
)
7447 location
= attr
->values
[0].string
.text
;
7451 if (con
->username
[0])
7452 username
= con
->username
;
7453 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7454 IPP_TAG_NAME
)) != NULL
)
7455 username
= attr
->values
[0].string
.text
;
7459 ra
= create_requested_array(con
->request
);
7462 * OK, build a list of printers for this printer...
7465 if (first_printer_name
)
7467 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7468 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7471 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7474 count
< limit
&& printer
;
7475 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7477 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7478 (printer
->type
& printer_mask
) == printer_type
&&
7480 (printer
->location
&& !strcasecmp(printer
->location
, location
))))
7483 * If HideImplicitMembers is enabled, see if this printer or class
7484 * is a member of an implicit class...
7487 if (ImplicitClasses
&& HideImplicitMembers
&&
7488 printer
->in_implicit_class
)
7492 * If a username is specified, see if it is allowed or denied
7496 if (!(printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
7497 printer
->num_users
&& username
&& !user_allowed(printer
, username
))
7501 * Add the group separator as needed...
7505 ippAddSeparator(con
->response
);
7510 * Send the attributes...
7513 copy_printer_attrs(con
, printer
, ra
);
7517 cupsArrayDelete(ra
);
7519 con
->response
->request
.status
.status_code
= IPP_OK
;
7524 * 'get_subscription_attrs()' - Get subscription attributes.
7528 get_subscription_attrs(
7529 cupsd_client_t
*con
, /* I - Client connection */
7530 int sub_id
) /* I - Subscription ID */
7532 http_status_t status
; /* Policy status */
7533 cupsd_subscription_t
*sub
; /* Subscription */
7534 cups_array_t
*ra
; /* Requested attributes array */
7537 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7538 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7539 con
, con
->http
.fd
, sub_id
);
7542 * Is the subscription ID valid?
7545 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7548 * Bad subscription ID...
7551 send_ipp_status(con
, IPP_NOT_FOUND
,
7552 _("notify-subscription-id %d no good!"), sub_id
);
7560 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7562 con
, sub
->owner
)) != HTTP_OK
)
7564 send_http_error(con
, status
, sub
->dest
);
7569 * Copy the subscription attributes to the response using the
7570 * requested-attributes attribute that may be provided by the client.
7573 ra
= create_requested_array(con
->request
);
7575 copy_subscription_attrs(con
, sub
, ra
);
7577 cupsArrayDelete(ra
);
7579 con
->response
->request
.status
.status_code
= IPP_OK
;
7584 * 'get_subscriptions()' - Get subscriptions.
7588 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7589 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7591 http_status_t status
; /* Policy status */
7592 int count
; /* Number of subscriptions */
7593 int limit
; /* Limit */
7594 cupsd_subscription_t
*sub
; /* Subscription */
7595 cups_array_t
*ra
; /* Requested attributes array */
7596 ipp_attribute_t
*attr
; /* Attribute */
7597 cups_ptype_t dtype
; /* Destination type (printer/class) */
7598 char scheme
[HTTP_MAX_URI
],
7599 /* Scheme portion of URI */
7600 username
[HTTP_MAX_URI
],
7601 /* Username portion of URI */
7603 /* Host portion of URI */
7604 resource
[HTTP_MAX_URI
];
7605 /* Resource portion of URI */
7606 int port
; /* Port portion of URI */
7607 cupsd_job_t
*job
; /* Job pointer */
7608 cupsd_printer_t
*printer
; /* Printer */
7611 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7612 "get_subscriptions(con=%p[%d], uri=%s)",
7613 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
7616 * Is the destination valid?
7619 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7620 sizeof(scheme
), username
, sizeof(username
), host
,
7621 sizeof(host
), &port
, resource
, sizeof(resource
));
7623 if (!strcmp(resource
, "/") ||
7624 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7625 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7626 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7631 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7634 job
= cupsdFindJob(atoi(resource
+ 6));
7638 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
7643 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7649 send_ipp_status(con
, IPP_NOT_FOUND
,
7650 _("The printer or class was not found."));
7653 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7654 IPP_TAG_INTEGER
)) != NULL
)
7656 job
= cupsdFindJob(attr
->values
[0].integer
);
7660 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
7661 attr
->values
[0].integer
);
7672 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
7674 con
, NULL
)) != HTTP_OK
)
7676 send_http_error(con
, status
, printer
);
7681 * Copy the subscription attributes to the response using the
7682 * requested-attributes attribute that may be provided by the client.
7685 ra
= create_requested_array(con
->request
);
7687 if ((attr
= ippFindAttribute(con
->request
, "limit",
7688 IPP_TAG_INTEGER
)) != NULL
)
7689 limit
= attr
->values
[0].integer
;
7694 * See if we only want to see subscriptions for a specific user...
7697 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7698 IPP_TAG_BOOLEAN
)) != NULL
&&
7699 attr
->values
[0].boolean
)
7700 strlcpy(username
, get_username(con
), sizeof(username
));
7704 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7706 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7707 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7708 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
7710 ippAddSeparator(con
->response
);
7711 copy_subscription_attrs(con
, sub
, ra
);
7714 if (limit
&& count
>= limit
)
7718 cupsArrayDelete(ra
);
7721 con
->response
->request
.status
.status_code
= IPP_OK
;
7723 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7728 * 'get_username()' - Get the username associated with a request.
7731 static const char * /* O - Username */
7732 get_username(cupsd_client_t
*con
) /* I - Connection */
7734 ipp_attribute_t
*attr
; /* Attribute */
7737 if (con
->username
[0])
7738 return (con
->username
);
7739 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7740 IPP_TAG_NAME
)) != NULL
)
7741 return (attr
->values
[0].string
.text
);
7743 return ("anonymous");
7748 * 'hold_job()' - Hold a print job.
7752 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7753 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7755 ipp_attribute_t
*attr
, /* Current job-hold-until */
7756 *newattr
; /* New job-hold-until */
7757 int jobid
; /* Job ID */
7758 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7759 username
[HTTP_MAX_URI
], /* Username portion of URI */
7760 host
[HTTP_MAX_URI
], /* Host portion of URI */
7761 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7762 int port
; /* Port portion of URI */
7763 cupsd_job_t
*job
; /* Job information */
7766 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
7767 uri
->values
[0].string
.text
);
7770 * See if we have a job URI or a printer URI...
7773 if (!strcmp(uri
->name
, "printer-uri"))
7776 * Got a printer URI; see if we also have a job-id attribute...
7779 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7780 IPP_TAG_INTEGER
)) == NULL
)
7782 send_ipp_status(con
, IPP_BAD_REQUEST
,
7783 _("Got a printer-uri attribute but no job-id!"));
7787 jobid
= attr
->values
[0].integer
;
7792 * Got a job URI; parse it to get the job ID...
7795 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7796 sizeof(method
), username
, sizeof(username
), host
,
7797 sizeof(host
), &port
, resource
, sizeof(resource
));
7799 if (strncmp(resource
, "/jobs/", 6))
7805 send_ipp_status(con
, IPP_BAD_REQUEST
,
7806 _("Bad job-uri attribute \"%s\"!"),
7807 uri
->values
[0].string
.text
);
7811 jobid
= atoi(resource
+ 6);
7815 * See if the job exists...
7818 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7821 * Nope - return a "not found" error...
7824 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7829 * See if the job is owned by the requesting user...
7832 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7834 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7839 * Hold the job and return...
7844 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
7845 "Job held by user.");
7847 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
7848 IPP_TAG_KEYWORD
)) == NULL
)
7849 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7851 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7852 IPP_TAG_KEYWORD
)) == NULL
)
7853 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7858 * Free the old hold value and copy the new one over...
7861 _cupsStrFree(attr
->values
[0].string
.text
);
7865 attr
->value_tag
= newattr
->value_tag
;
7866 attr
->values
[0].string
.text
=
7867 _cupsStrAlloc(newattr
->values
[0].string
.text
);
7871 attr
->value_tag
= IPP_TAG_KEYWORD
;
7872 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
7876 * Hold job until specified time...
7879 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
7881 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
7882 "Job job-hold-until value changed by user.");
7885 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Held by \"%s\".", username
);
7887 con
->response
->request
.status
.status_code
= IPP_OK
;
7892 * 'move_job()' - Move a job to a new destination.
7896 move_job(cupsd_client_t
*con
, /* I - Client connection */
7897 ipp_attribute_t
*uri
) /* I - Job URI */
7899 http_status_t status
; /* Policy status */
7900 ipp_attribute_t
*attr
; /* Current attribute */
7901 int jobid
; /* Job ID */
7902 cupsd_job_t
*job
; /* Current job */
7903 const char *src
; /* Source printer/class */
7904 cups_ptype_t stype
, /* Source type (printer or class) */
7905 dtype
; /* Destination type (printer/class) */
7906 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7907 username
[HTTP_MAX_URI
], /* Username portion of URI */
7908 host
[HTTP_MAX_URI
], /* Host portion of URI */
7909 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7910 int port
; /* Port portion of URI */
7911 cupsd_printer_t
*sprinter
, /* Source printer */
7912 *dprinter
; /* Destination printer */
7915 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
7916 uri
->values
[0].string
.text
);
7919 * Get the new printer or class...
7922 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7923 IPP_TAG_URI
)) == NULL
)
7926 * Need job-printer-uri...
7929 send_ipp_status(con
, IPP_BAD_REQUEST
,
7930 _("job-printer-uri attribute missing!"));
7934 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7940 send_ipp_status(con
, IPP_NOT_FOUND
,
7941 _("The printer or class was not found."));
7946 * See if we have a job URI or a printer URI...
7949 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7950 sizeof(scheme
), username
, sizeof(username
), host
,
7951 sizeof(host
), &port
, resource
, sizeof(resource
));
7953 if (!strcmp(uri
->name
, "printer-uri"))
7956 * Got a printer URI; see if we also have a job-id attribute...
7959 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7960 IPP_TAG_INTEGER
)) == NULL
)
7966 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
7967 &sprinter
)) == NULL
)
7973 send_ipp_status(con
, IPP_NOT_FOUND
,
7974 _("The printer or class was not found."));
7983 * Otherwise, just move a single job...
7986 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7989 * Nope - return a "not found" error...
7992 send_ipp_status(con
, IPP_NOT_FOUND
,
7993 _("Job #%d does not exist!"), attr
->values
[0].integer
);
7999 * Job found, initialize source pointers...
8010 * Got a job URI; parse it to get the job ID...
8013 if (strncmp(resource
, "/jobs/", 6))
8019 send_ipp_status(con
, IPP_BAD_REQUEST
,
8020 _("Bad job-uri attribute \"%s\"!"),
8021 uri
->values
[0].string
.text
);
8026 * See if the job exists...
8029 jobid
= atoi(resource
+ 6);
8031 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8034 * Nope - return a "not found" error...
8037 send_ipp_status(con
, IPP_NOT_FOUND
,
8038 _("Job #%d does not exist!"), jobid
);
8044 * Job found, initialize source pointers...
8053 * Check the policy of the destination printer...
8056 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
8057 job
? job
->username
: NULL
)) != HTTP_OK
)
8059 send_http_error(con
, status
, dprinter
);
8064 * Now move the job or jobs...
8070 * See if the job has been completed...
8073 if (job
->state_value
> IPP_JOB_STOPPED
)
8076 * Return a "not-possible" error...
8079 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8080 _("Job #%d is finished and cannot be altered!"),
8086 * See if the job is owned by the requesting user...
8089 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8091 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8096 * Move the job to a different printer or class...
8099 cupsdMoveJob(job
, dprinter
);
8104 * Got the source printer, now look through the jobs...
8107 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
8109 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
8112 * See if the job is pointing at the source printer or has not been
8116 if (strcasecmp(job
->dest
, src
) ||
8117 job
->state_value
> IPP_JOB_STOPPED
)
8121 * See if the job can be moved by the requesting user...
8124 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8128 * Move the job to a different printer or class...
8131 cupsdMoveJob(job
, dprinter
);
8136 * Start jobs if possible...
8142 * Return with "everything is OK" status...
8145 con
->response
->request
.status
.status_code
= IPP_OK
;
8150 * 'ppd_parse_line()' - Parse a PPD default line.
8153 static int /* O - 0 on success, -1 on failure */
8154 ppd_parse_line(const char *line
, /* I - Line */
8155 char *option
, /* O - Option name */
8156 int olen
, /* I - Size of option name */
8157 char *choice
, /* O - Choice name */
8158 int clen
) /* I - Size of choice name */
8161 * Verify this is a default option line...
8164 if (strncmp(line
, "*Default", 8))
8168 * Read the option name...
8171 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
8181 * Skip everything else up to the colon (:)...
8184 while (*line
&& *line
!= ':')
8193 * Now grab the option choice, skipping leading whitespace...
8196 while (isspace(*line
& 255))
8199 for (clen
--; isalnum(*line
& 255); line
++)
8209 * Return with no errors...
8217 * 'print_job()' - Print a file to a printer or class.
8221 print_job(cupsd_client_t
*con
, /* I - Client connection */
8222 ipp_attribute_t
*uri
) /* I - Printer URI */
8224 ipp_attribute_t
*attr
; /* Current attribute */
8225 ipp_attribute_t
*format
; /* Document-format attribute */
8226 const char *default_format
; /* document-format-default value */
8227 cupsd_job_t
*job
; /* New job */
8228 char filename
[1024]; /* Job filename */
8229 mime_type_t
*filetype
; /* Type of file */
8230 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8231 type
[MIME_MAX_TYPE
], /* Subtype of file */
8232 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8233 /* Textual name of mime type */
8234 cupsd_printer_t
*printer
; /* Printer data */
8235 struct stat fileinfo
; /* File information */
8236 int kbytes
; /* Size of file */
8237 int compression
; /* Document compression */
8240 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
8241 uri
->values
[0].string
.text
);
8244 * Validate print file attributes, for now just document-format and
8245 * compression (CUPS only supports "none" and "gzip")...
8248 compression
= CUPS_FILE_NONE
;
8250 if ((attr
= ippFindAttribute(con
->request
, "compression",
8251 IPP_TAG_KEYWORD
)) != NULL
)
8253 if (strcmp(attr
->values
[0].string
.text
, "none")
8255 && strcmp(attr
->values
[0].string
.text
, "gzip")
8256 #endif /* HAVE_LIBZ */
8259 send_ipp_status(con
, IPP_ATTRIBUTES
,
8260 _("Unsupported compression \"%s\"!"),
8261 attr
->values
[0].string
.text
);
8262 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8263 "compression", NULL
, attr
->values
[0].string
.text
);
8268 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8269 compression
= CUPS_FILE_GZIP
;
8270 #endif /* HAVE_LIBZ */
8274 * Do we have a file to print?
8279 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
8284 * Is the destination valid?
8287 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8293 send_ipp_status(con
, IPP_NOT_FOUND
,
8294 _("The printer or class was not found."));
8299 * Is it a format we support?
8302 if ((format
= ippFindAttribute(con
->request
, "document-format",
8303 IPP_TAG_MIMETYPE
)) != NULL
)
8306 * Grab format from client...
8309 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
8312 send_ipp_status(con
, IPP_BAD_REQUEST
,
8313 _("Could not scan type \"%s\"!"),
8314 format
->values
[0].string
.text
);
8318 else if ((default_format
= cupsGetOption("document-format",
8319 printer
->num_options
,
8320 printer
->options
)) != NULL
)
8323 * Use default document format...
8326 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
8328 send_ipp_status(con
, IPP_BAD_REQUEST
,
8329 _("Could not scan type \"%s\"!"),
8340 strcpy(super
, "application");
8341 strcpy(type
, "octet-stream");
8344 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8347 * Auto-type the file...
8350 ipp_attribute_t
*doc_name
; /* document-name attribute */
8353 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8355 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8356 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8357 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8361 filetype
= mimeType(MimeDatabase
, super
, type
);
8363 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8364 filetype
->super
, filetype
->type
);
8367 filetype
= mimeType(MimeDatabase
, super
, type
);
8371 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8374 * Replace the document-format attribute value with the auto-typed or
8378 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8383 _cupsStrFree(format
->values
[0].string
.text
);
8385 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8388 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8389 "document-format", NULL
, mimetype
);
8393 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8394 _("Unsupported format \'%s/%s\'!"), super
, type
);
8395 cupsdLogMessage(CUPSD_LOG_INFO
,
8396 "Hint: Do you have the raw file printing rules enabled?");
8399 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8400 "document-format", NULL
, format
->values
[0].string
.text
);
8406 * Read any embedded job ticket info from PS files...
8409 if (!strcasecmp(filetype
->super
, "application") &&
8410 (!strcasecmp(filetype
->type
, "postscript") ||
8411 !strcasecmp(filetype
->type
, "pdf")))
8412 read_job_ticket(con
);
8415 * Create the job object...
8418 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8422 * Update quota data...
8425 if (stat(con
->filename
, &fileinfo
))
8428 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8430 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8432 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8433 IPP_TAG_INTEGER
)) != NULL
)
8434 attr
->values
[0].integer
+= kbytes
;
8437 * Add the job file...
8440 if (add_file(con
, job
, filetype
, compression
))
8443 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8445 rename(con
->filename
, filename
);
8446 cupsdClearString(&con
->filename
);
8449 * See if we need to add the ending sheet...
8452 if (cupsdTimeoutJob(job
))
8456 * Log and save the job...
8459 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8460 "File of type %s/%s queued by \"%s\".",
8461 filetype
->super
, filetype
->type
, job
->username
);
8462 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8463 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8464 job
->dest
, job
->username
);
8467 * Start the job if possible...
8475 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8477 * This function only gets called when printing a single PDF or PostScript
8478 * file using the Print-Job operation. It doesn't work for Create-Job +
8479 * Send-File, since the job attributes need to be set at job creation
8480 * time for banners to work. The embedded job ticket stuff is here
8481 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8482 * options and IPP attributes which otherwise would be lost.
8484 * The format of a job ticket is simple:
8486 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8488 * %cupsJobTicket: attr1=value1
8489 * %cupsJobTicket: attr2=value2
8491 * %cupsJobTicket: attrN=valueN
8493 * Job ticket lines must appear immediately after the first line that
8494 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8495 * stops looking for job ticket info when it finds a line that does not begin
8496 * with "%cupsJobTicket:".
8498 * The maximum length of a job ticket line, including the prefix, is
8499 * 255 characters to conform with the Adobe DSC.
8501 * Read-only attributes are rejected with a notice to the error log in
8502 * case a malicious user tries anything. Since the job ticket is read
8503 * prior to attribute validation in print_job(), job ticket attributes
8504 * will go through the same validation as IPP attributes...
8508 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8510 cups_file_t
*fp
; /* File to read from */
8511 char line
[256]; /* Line data */
8512 int num_options
; /* Number of options */
8513 cups_option_t
*options
; /* Options */
8514 ipp_t
*ticket
; /* New attributes */
8515 ipp_attribute_t
*attr
, /* Current attribute */
8516 *attr2
, /* Job attribute */
8517 *prev2
; /* Previous job attribute */
8521 * First open the print file...
8524 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8526 cupsdLogMessage(CUPSD_LOG_ERROR
,
8527 "Unable to open print file for job ticket - %s",
8533 * Skip the first line...
8536 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8538 cupsdLogMessage(CUPSD_LOG_ERROR
,
8539 "Unable to read from print file for job ticket - %s",
8545 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8548 * Not a DSC-compliant file, so no job ticket info will be available...
8556 * Read job ticket info from the file...
8562 while (cupsFileGets(fp
, line
, sizeof(line
)))
8565 * Stop at the first non-ticket line...
8568 if (strncmp(line
, "%cupsJobTicket:", 15))
8572 * Add the options to the option array...
8575 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8579 * Done with the file; see if we have any options...
8584 if (num_options
== 0)
8588 * OK, convert the options to an attribute list, and apply them to
8593 cupsEncodeOptions(ticket
, num_options
, options
);
8596 * See what the user wants to change.
8599 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8601 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8604 if (!strcmp(attr
->name
, "job-originating-host-name") ||
8605 !strcmp(attr
->name
, "job-originating-user-name") ||
8606 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8607 !strcmp(attr
->name
, "job-k-octets") ||
8608 !strcmp(attr
->name
, "job-id") ||
8609 !strncmp(attr
->name
, "job-state", 9) ||
8610 !strncmp(attr
->name
, "time-at-", 8))
8611 continue; /* Read-only attrs */
8613 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8614 IPP_TAG_ZERO
)) != NULL
)
8617 * Some other value; first free the old value...
8620 if (con
->request
->attrs
== attr2
)
8622 con
->request
->attrs
= attr2
->next
;
8627 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8628 if (prev2
->next
== attr2
)
8630 prev2
->next
= attr2
->next
;
8635 if (con
->request
->last
== attr2
)
8636 con
->request
->last
= prev2
;
8638 _ippFreeAttr(attr2
);
8642 * Add new option by copying it...
8645 copy_attribute(con
->request
, attr
, 0);
8649 * Then free the attribute list and option array...
8653 cupsFreeOptions(num_options
, options
);
8658 * 'reject_jobs()' - Reject print jobs to a printer.
8662 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8663 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8665 http_status_t status
; /* Policy status */
8666 cups_ptype_t dtype
; /* Destination type (printer/class) */
8667 cupsd_printer_t
*printer
; /* Printer data */
8668 ipp_attribute_t
*attr
; /* printer-state-message text */
8671 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8672 con
->http
.fd
, uri
->values
[0].string
.text
);
8675 * Is the destination valid?
8678 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8684 send_ipp_status(con
, IPP_NOT_FOUND
,
8685 _("The printer or class was not found."));
8693 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8695 send_http_error(con
, status
, printer
);
8700 * Reject jobs sent to the printer...
8703 printer
->accepting
= 0;
8705 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8706 IPP_TAG_TEXT
)) == NULL
)
8707 strcpy(printer
->state_message
, "Rejecting Jobs");
8709 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8710 sizeof(printer
->state_message
));
8712 cupsdAddPrinterHistory(printer
);
8714 if (dtype
& CUPS_PRINTER_CLASS
)
8716 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8718 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8719 printer
->name
, get_username(con
));
8723 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8725 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8726 printer
->name
, get_username(con
));
8730 * Everything was ok, so return OK status...
8733 con
->response
->request
.status
.status_code
= IPP_OK
;
8738 * 'release_job()' - Release a held print job.
8742 release_job(cupsd_client_t
*con
, /* I - Client connection */
8743 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8745 ipp_attribute_t
*attr
; /* Current attribute */
8746 int jobid
; /* Job ID */
8747 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8748 username
[HTTP_MAX_URI
], /* Username portion of URI */
8749 host
[HTTP_MAX_URI
], /* Host portion of URI */
8750 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8751 int port
; /* Port portion of URI */
8752 cupsd_job_t
*job
; /* Job information */
8755 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8756 con
->http
.fd
, uri
->values
[0].string
.text
);
8759 * See if we have a job URI or a printer URI...
8762 if (!strcmp(uri
->name
, "printer-uri"))
8765 * Got a printer URI; see if we also have a job-id attribute...
8768 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8769 IPP_TAG_INTEGER
)) == NULL
)
8771 send_ipp_status(con
, IPP_BAD_REQUEST
,
8772 _("Got a printer-uri attribute but no job-id!"));
8776 jobid
= attr
->values
[0].integer
;
8781 * Got a job URI; parse it to get the job ID...
8784 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8785 sizeof(method
), username
, sizeof(username
), host
,
8786 sizeof(host
), &port
, resource
, sizeof(resource
));
8788 if (strncmp(resource
, "/jobs/", 6))
8794 send_ipp_status(con
, IPP_BAD_REQUEST
,
8795 _("Bad job-uri attribute \"%s\"!"),
8796 uri
->values
[0].string
.text
);
8800 jobid
= atoi(resource
+ 6);
8804 * See if the job exists...
8807 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8810 * Nope - return a "not found" error...
8813 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8818 * See if job is "held"...
8821 if (job
->state_value
!= IPP_JOB_HELD
)
8824 * Nope - return a "not possible" error...
8827 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
8832 * See if the job is owned by the requesting user...
8835 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8837 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8842 * Reset the job-hold-until value to "no-hold"...
8845 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8846 IPP_TAG_KEYWORD
)) == NULL
)
8847 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8851 _cupsStrFree(attr
->values
[0].string
.text
);
8853 attr
->value_tag
= IPP_TAG_KEYWORD
;
8854 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
8856 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
8857 "Job job-hold-until value changed by user.");
8861 * Release the job and return...
8864 cupsdReleaseJob(job
);
8866 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
8867 "Job released by user.");
8869 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
8871 con
->response
->request
.status
.status_code
= IPP_OK
;
8878 * 'renew_subscription()' - Renew an existing subscription...
8883 cupsd_client_t
*con
, /* I - Client connection */
8884 int sub_id
) /* I - Subscription ID */
8886 http_status_t status
; /* Policy status */
8887 cupsd_subscription_t
*sub
; /* Subscription */
8888 ipp_attribute_t
*lease
; /* notify-lease-duration */
8891 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8892 "renew_subscription(con=%p[%d], sub_id=%d)",
8893 con
, con
->http
.fd
, sub_id
);
8896 * Is the subscription ID valid?
8899 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8902 * Bad subscription ID...
8905 send_ipp_status(con
, IPP_NOT_FOUND
,
8906 _("notify-subscription-id %d no good!"), sub_id
);
8913 * Job subscriptions cannot be renewed...
8916 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8917 _("Job subscriptions cannot be renewed!"));
8925 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8927 con
, sub
->owner
)) != HTTP_OK
)
8929 send_http_error(con
, status
, sub
->dest
);
8934 * Renew the subscription...
8937 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8940 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8942 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8944 cupsdLogMessage(CUPSD_LOG_INFO
,
8945 "renew_subscription: Limiting notify-lease-duration to "
8948 sub
->lease
= MaxLeaseDuration
;
8951 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
8953 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
8955 con
->response
->request
.status
.status_code
= IPP_OK
;
8957 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
8958 "notify-lease-duration", sub
->lease
);
8963 * 'restart_job()' - Restart an old print job.
8967 restart_job(cupsd_client_t
*con
, /* I - Client connection */
8968 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8970 ipp_attribute_t
*attr
; /* Current attribute */
8971 int jobid
; /* Job ID */
8972 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8973 username
[HTTP_MAX_URI
], /* Username portion of URI */
8974 host
[HTTP_MAX_URI
], /* Host portion of URI */
8975 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8976 int port
; /* Port portion of URI */
8977 cupsd_job_t
*job
; /* Job information */
8980 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
8981 con
->http
.fd
, uri
->values
[0].string
.text
);
8984 * See if we have a job URI or a printer URI...
8987 if (!strcmp(uri
->name
, "printer-uri"))
8990 * Got a printer URI; see if we also have a job-id attribute...
8993 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8994 IPP_TAG_INTEGER
)) == NULL
)
8996 send_ipp_status(con
, IPP_BAD_REQUEST
,
8997 _("Got a printer-uri attribute but no job-id!"));
9001 jobid
= attr
->values
[0].integer
;
9006 * Got a job URI; parse it to get the job ID...
9009 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9010 sizeof(method
), username
, sizeof(username
), host
,
9011 sizeof(host
), &port
, resource
, sizeof(resource
));
9013 if (strncmp(resource
, "/jobs/", 6))
9019 send_ipp_status(con
, IPP_BAD_REQUEST
,
9020 _("Bad job-uri attribute \"%s\"!"),
9021 uri
->values
[0].string
.text
);
9025 jobid
= atoi(resource
+ 6);
9029 * See if the job exists...
9032 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9035 * Nope - return a "not found" error...
9038 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9043 * See if job is in any of the "completed" states...
9046 if (job
->state_value
<= IPP_JOB_PROCESSING
)
9049 * Nope - return a "not possible" error...
9052 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
9058 * See if we have retained the job files...
9063 if (!job
->attrs
|| job
->num_files
== 0)
9066 * Nope - return a "not possible" error...
9069 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9070 _("Job #%d cannot be restarted - no files!"), jobid
);
9075 * See if the job is owned by the requesting user...
9078 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9080 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9085 * Restart the job and return...
9088 cupsdRestartJob(job
);
9090 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
9092 con
->response
->request
.status
.status_code
= IPP_OK
;
9097 * 'save_auth_info()' - Save authentication information for a job.
9102 cupsd_client_t
*con
, /* I - Client connection */
9103 cupsd_job_t
*job
, /* I - Job */
9104 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
9106 int i
; /* Looping var */
9107 char filename
[1024]; /* Job authentication filename */
9108 cups_file_t
*fp
; /* Job authentication file */
9109 char line
[2048]; /* Line for file */
9110 cupsd_printer_t
*dest
; /* Destination printer/class */
9114 * This function saves the in-memory authentication information for
9115 * a job so that it can be used to authenticate with a remote host.
9116 * The information is stored in a file that is readable only by the
9117 * root user. The fields are Base-64 encoded, each on a separate line,
9118 * followed by random number (up to 1024) of newlines to limit the
9119 * amount of information that is exposed.
9121 * Because of the potential for exposing of authentication information,
9122 * this functionality is only enabled when running cupsd as root.
9124 * This caching only works for the Basic and BasicDigest authentication
9125 * types. Digest authentication cannot be cached this way, and in
9126 * the future Kerberos authentication may make all of this obsolete.
9128 * Authentication information is saved whenever an authenticated
9129 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9132 * This information is deleted after a job is completed or canceled,
9133 * so reprints may require subsequent re-authentication.
9139 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9143 * Create the authentication file and change permissions...
9146 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9147 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9149 cupsdLogMessage(CUPSD_LOG_ERROR
,
9150 "Unable to save authentication info to \"%s\" - %s",
9151 filename
, strerror(errno
));
9155 fchown(cupsFileNumber(fp
), 0, 0);
9156 fchmod(cupsFileNumber(fp
), 0400);
9158 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9161 * Write 1 to 3 auth values...
9164 cupsdClearString(&job
->auth_username
);
9165 cupsdClearString(&job
->auth_domain
);
9166 cupsdClearString(&job
->auth_password
);
9168 for (i
= 0; i
< auth_info
->num_values
; i
++)
9170 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
9171 strlen(auth_info
->values
[i
].string
.text
));
9172 cupsFilePrintf(fp
, "%s\n", line
);
9174 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9175 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s",
9176 auth_info
->values
[i
].string
.text
);
9177 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9178 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s",
9179 auth_info
->values
[i
].string
.text
);
9180 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9181 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s",
9182 auth_info
->values
[i
].string
.text
);
9185 else if (con
->username
[0])
9188 * Write the authenticated username...
9191 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
9192 cupsFilePrintf(fp
, "%s\n", line
);
9194 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", con
->username
);
9195 cupsdClearString(&job
->auth_domain
);
9198 * Write the authenticated password...
9201 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
9202 cupsFilePrintf(fp
, "%s\n", line
);
9204 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", con
->password
);
9208 * Write a random number of newlines to the end of the file...
9211 for (i
= (rand() % 1024); i
>= 0; i
--)
9212 cupsFilePutChar(fp
, '\n');
9215 * Close the file and return...
9220 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9221 if (con
->gss_have_creds
)
9222 save_krb5_creds(con
, job
);
9223 else if (job
->ccname
)
9224 cupsdClearString(&(job
->ccname
));
9225 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9229 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9231 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
9235 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
9236 cupsd_job_t
*job
) /* I - Job */
9238 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
9239 cupsdLogMessage(CUPSD_LOG_INFO
,
9240 "Sorry, your version of Kerberos does not support delegated "
9245 krb5_error_code error
; /* Kerberos error code */
9246 OM_uint32 major_status
, /* Major status code */
9247 minor_status
; /* Minor status code */
9248 krb5_principal principal
; /* Kerberos principal */
9253 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
9257 if (krb5_init_context
== NULL
)
9259 # endif /* __APPLE__ */
9262 * We MUST create a file-based cache because memory-based caches are
9263 * only valid for the current process/address space.
9265 * Due to various bugs/features in different versions of Kerberos, we
9266 * need either the krb5_cc_new_unique() function or Heimdal's version
9267 * of krb5_cc_gen_new() to create a new FILE: credential cache that
9268 * can be passed to the backend. These functions create a temporary
9269 * file (typically in /tmp) containing the cached credentials, which
9270 * are removed when we have successfully printed a job.
9273 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
9274 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
9275 &(job
->ccache
))) != 0)
9276 # else /* HAVE_HEIMDAL */
9277 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
9278 &(job
->ccache
))) != 0)
9279 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
9281 cupsdLogMessage(CUPSD_LOG_ERROR
,
9282 "Unable to create new credentials cache (%d/%s)",
9283 error
, strerror(errno
));
9288 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
9290 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to parse kerberos username (%d/%s)",
9291 error
, strerror(errno
));
9292 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9297 if ((error
= krb5_cc_initialize(KerberosContext
, job
->ccache
, principal
)))
9299 cupsdLogMessage(CUPSD_LOG_ERROR
,
9300 "Unable to initialize credentials cache (%d/%s)", error
,
9302 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9303 krb5_free_principal(KerberosContext
, principal
);
9308 krb5_free_principal(KerberosContext
, principal
);
9311 * Copy the user's credentials to the new cache file...
9314 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
9317 if (GSS_ERROR(major_status
))
9319 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
9320 "Unable to import client credentials cache");
9321 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9327 * Add the KRB5CCNAME environment variable to the job so that the
9328 * backend can use the credentials when printing.
9331 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
9332 krb5_cc_get_name(KerberosContext
, job
->ccache
));
9334 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "save_krb5_creds: %s", job
->ccname
);
9335 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
9337 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9341 * 'send_document()' - Send a file to a printer or class.
9345 send_document(cupsd_client_t
*con
, /* I - Client connection */
9346 ipp_attribute_t
*uri
) /* I - Printer URI */
9348 ipp_attribute_t
*attr
; /* Current attribute */
9349 ipp_attribute_t
*format
; /* Request's document-format attribute */
9350 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9351 const char *default_format
;/* document-format-default value */
9352 int jobid
; /* Job ID number */
9353 cupsd_job_t
*job
; /* Current job */
9354 char job_uri
[HTTP_MAX_URI
],
9356 method
[HTTP_MAX_URI
],
9357 /* Method portion of URI */
9358 username
[HTTP_MAX_URI
],
9359 /* Username portion of URI */
9361 /* Host portion of URI */
9362 resource
[HTTP_MAX_URI
];
9363 /* Resource portion of URI */
9364 int port
; /* Port portion of URI */
9365 mime_type_t
*filetype
; /* Type of file */
9366 char super
[MIME_MAX_SUPER
],
9367 /* Supertype of file */
9368 type
[MIME_MAX_TYPE
],
9369 /* Subtype of file */
9370 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9371 /* Textual name of mime type */
9372 char filename
[1024]; /* Job filename */
9373 cupsd_printer_t
*printer
; /* Current printer */
9374 struct stat fileinfo
; /* File information */
9375 int kbytes
; /* Size of file */
9376 int compression
; /* Type of compression */
9377 int start_job
; /* Start the job? */
9380 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9381 con
->http
.fd
, uri
->values
[0].string
.text
);
9384 * See if we have a job URI or a printer URI...
9387 if (!strcmp(uri
->name
, "printer-uri"))
9390 * Got a printer URI; see if we also have a job-id attribute...
9393 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9394 IPP_TAG_INTEGER
)) == NULL
)
9396 send_ipp_status(con
, IPP_BAD_REQUEST
,
9397 _("Got a printer-uri attribute but no job-id!"));
9401 jobid
= attr
->values
[0].integer
;
9406 * Got a job URI; parse it to get the job ID...
9409 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9410 sizeof(method
), username
, sizeof(username
), host
,
9411 sizeof(host
), &port
, resource
, sizeof(resource
));
9413 if (strncmp(resource
, "/jobs/", 6))
9419 send_ipp_status(con
, IPP_BAD_REQUEST
,
9420 _("Bad job-uri attribute \"%s\"!"),
9421 uri
->values
[0].string
.text
);
9425 jobid
= atoi(resource
+ 6);
9429 * See if the job exists...
9432 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9435 * Nope - return a "not found" error...
9438 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9442 printer
= cupsdFindDest(job
->dest
);
9445 * See if the job is owned by the requesting user...
9448 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9450 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9455 * OK, see if the client is sending the document compressed - CUPS
9456 * only supports "none" and "gzip".
9459 compression
= CUPS_FILE_NONE
;
9461 if ((attr
= ippFindAttribute(con
->request
, "compression",
9462 IPP_TAG_KEYWORD
)) != NULL
)
9464 if (strcmp(attr
->values
[0].string
.text
, "none")
9466 && strcmp(attr
->values
[0].string
.text
, "gzip")
9467 #endif /* HAVE_LIBZ */
9470 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
9471 attr
->values
[0].string
.text
);
9472 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9473 "compression", NULL
, attr
->values
[0].string
.text
);
9478 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9479 compression
= CUPS_FILE_GZIP
;
9480 #endif /* HAVE_LIBZ */
9484 * Do we have a file to print?
9489 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
9494 * Is it a format we support?
9497 if ((format
= ippFindAttribute(con
->request
, "document-format",
9498 IPP_TAG_MIMETYPE
)) != NULL
)
9501 * Grab format from client...
9504 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
9507 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9508 format
->values
[0].string
.text
);
9512 else if ((default_format
= cupsGetOption("document-format",
9513 printer
->num_options
,
9514 printer
->options
)) != NULL
)
9517 * Use default document format...
9520 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
9522 send_ipp_status(con
, IPP_BAD_REQUEST
,
9523 _("Could not scan type \"%s\"!"),
9531 * No document format attribute? Auto-type it!
9534 strcpy(super
, "application");
9535 strcpy(type
, "octet-stream");
9538 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9541 * Auto-type the file...
9544 ipp_attribute_t
*doc_name
; /* document-name attribute */
9547 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9549 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9550 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9551 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9555 filetype
= mimeType(MimeDatabase
, super
, type
);
9557 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9558 filetype
->super
, filetype
->type
);
9561 filetype
= mimeType(MimeDatabase
, super
, type
);
9566 * Replace the document-format attribute value with the auto-typed or
9570 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9573 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9574 IPP_TAG_MIMETYPE
)) != NULL
)
9576 _cupsStrFree(jformat
->values
[0].string
.text
);
9578 jformat
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
9581 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9582 "document-format", NULL
, mimetype
);
9586 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9587 _("Unsupported format \'%s/%s\'!"), super
, type
);
9588 cupsdLogMessage(CUPSD_LOG_INFO
,
9589 "Hint: Do you have the raw file printing rules enabled?");
9592 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9593 "document-format", NULL
, format
->values
[0].string
.text
);
9598 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9600 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9603 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9604 _("Unsupported format \'%s\'!"), mimetype
);
9606 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9607 "document-format", NULL
, mimetype
);
9613 * Add the file to the job...
9618 if (add_file(con
, job
, filetype
, compression
))
9621 if (stat(con
->filename
, &fileinfo
))
9624 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9626 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9628 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
9629 IPP_TAG_INTEGER
)) != NULL
)
9630 attr
->values
[0].integer
+= kbytes
;
9632 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
9634 rename(con
->filename
, filename
);
9636 cupsdClearString(&con
->filename
);
9638 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9639 filetype
->super
, filetype
->type
, job
->username
);
9642 * Start the job if this is the last document...
9645 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9646 IPP_TAG_BOOLEAN
)) != NULL
&&
9647 attr
->values
[0].boolean
)
9650 * See if we need to add the ending sheet...
9653 if (cupsdTimeoutJob(job
))
9656 if (job
->state_value
== IPP_JOB_STOPPED
)
9658 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9659 job
->state_value
= IPP_JOB_PENDING
;
9661 else if (job
->state_value
== IPP_JOB_HELD
)
9663 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9664 IPP_TAG_KEYWORD
)) == NULL
)
9665 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9667 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9669 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9670 job
->state_value
= IPP_JOB_PENDING
;
9675 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9681 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9682 IPP_TAG_KEYWORD
)) == NULL
)
9683 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9685 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9687 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9688 job
->state_value
= IPP_JOB_HELD
;
9689 job
->hold_until
= time(NULL
) + 60;
9692 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9699 * Fill in the response info...
9702 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
9705 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9708 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9710 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9712 add_job_state_reasons(con
, job
);
9714 con
->response
->request
.status
.status_code
= IPP_OK
;
9717 * Start the job if necessary...
9726 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9731 cupsd_client_t
*con
, /* I - Client connection */
9732 http_status_t status
, /* I - HTTP status code */
9733 cupsd_printer_t
*printer
) /* I - Printer, if any */
9735 ipp_attribute_t
*uri
; /* Request URI, if any */
9738 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
9739 IPP_TAG_URI
)) == NULL
)
9740 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
9742 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
9743 "Returning HTTP %s for %s (%s) from %s",
9745 ippOpString(con
->request
->request
.op
.operation_id
),
9746 uri
? uri
->values
[0].string
.text
: "no URI",
9747 con
->http
.hostname
);
9751 int auth_type
; /* Type of authentication required */
9754 auth_type
= CUPSD_AUTH_NONE
;
9756 if (status
== HTTP_UNAUTHORIZED
&&
9757 printer
->num_auth_info_required
> 0 &&
9758 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
9760 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
9761 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
9762 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
9765 * Creating and authenticating jobs requires Kerberos...
9768 auth_type
= CUPSD_AUTH_NEGOTIATE
;
9773 * Use policy/location-defined authentication requirements...
9776 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9777 cupsd_location_t
*auth
; /* Pointer to authentication element */
9780 if (printer
->type
& CUPS_PRINTER_CLASS
)
9781 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9783 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9785 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9786 auth
->type
== CUPSD_AUTH_NONE
)
9787 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
9789 con
->request
->request
.op
.operation_id
:
9794 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
9795 auth_type
= DefaultAuthType
;
9797 auth_type
= auth
->type
;
9801 cupsdSendError(con
, status
, auth_type
);
9804 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
9806 ippDelete(con
->response
);
9807 con
->response
= NULL
;
9814 * 'send_ipp_status()' - Send a status back to the IPP client.
9818 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9819 ipp_status_t status
, /* I - IPP status code */
9820 const char *message
,/* I - Status message */
9821 ...) /* I - Additional args as needed */
9823 va_list ap
; /* Pointer to additional args */
9824 char formatted
[1024]; /* Formatted errror message */
9827 va_start(ap
, message
);
9828 vsnprintf(formatted
, sizeof(formatted
),
9829 _cupsLangString(con
->language
, message
), ap
);
9832 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9833 ippOpString(con
->request
->request
.op
.operation_id
),
9834 ippErrorString(status
), formatted
);
9836 con
->response
->request
.status
.status_code
= status
;
9838 if (ippFindAttribute(con
->response
, "attributes-charset",
9839 IPP_TAG_ZERO
) == NULL
)
9840 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9841 "attributes-charset", NULL
, DefaultCharset
);
9843 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9844 IPP_TAG_ZERO
) == NULL
)
9845 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9846 "attributes-natural-language", NULL
, DefaultLanguage
);
9848 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9849 "status-message", NULL
, formatted
);
9854 * 'set_default()' - Set the default destination...
9858 set_default(cupsd_client_t
*con
, /* I - Client connection */
9859 ipp_attribute_t
*uri
) /* I - Printer URI */
9861 http_status_t status
; /* Policy status */
9862 cups_ptype_t dtype
; /* Destination type (printer/class) */
9863 cupsd_printer_t
*printer
, /* Printer */
9864 *oldprinter
; /* Old default printer */
9867 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9868 con
->http
.fd
, uri
->values
[0].string
.text
);
9871 * Is the destination valid?
9874 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9880 send_ipp_status(con
, IPP_NOT_FOUND
,
9881 _("The printer or class was not found."));
9889 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9891 send_http_error(con
, status
, NULL
);
9896 * Set it as the default...
9899 oldprinter
= DefaultPrinter
;
9900 DefaultPrinter
= printer
;
9903 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
9904 "%s is no longer the default printer.", oldprinter
->name
);
9906 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
9907 "%s is now the default printer.", printer
->name
);
9909 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
9910 CUPSD_DIRTY_REMOTE
| CUPSD_DIRTY_PRINTCAP
);
9912 cupsdLogMessage(CUPSD_LOG_INFO
,
9913 "Default destination set to \"%s\" by \"%s\".",
9914 printer
->name
, get_username(con
));
9917 * Everything was ok, so return OK status...
9920 con
->response
->request
.status
.status_code
= IPP_OK
;
9925 * 'set_job_attrs()' - Set job attributes.
9929 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9930 ipp_attribute_t
*uri
) /* I - Job URI */
9932 ipp_attribute_t
*attr
, /* Current attribute */
9933 *attr2
; /* Job attribute */
9934 int jobid
; /* Job ID */
9935 cupsd_job_t
*job
; /* Current job */
9936 char method
[HTTP_MAX_URI
],
9937 /* Method portion of URI */
9938 username
[HTTP_MAX_URI
],
9939 /* Username portion of URI */
9941 /* Host portion of URI */
9942 resource
[HTTP_MAX_URI
];
9943 /* Resource portion of URI */
9944 int port
; /* Port portion of URI */
9945 int event
; /* Events? */
9946 int check_jobs
; /* Check jobs? */
9949 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
9950 con
->http
.fd
, uri
->values
[0].string
.text
);
9953 * Start with "everything is OK" status...
9956 con
->response
->request
.status
.status_code
= IPP_OK
;
9959 * See if we have a job URI or a printer URI...
9962 if (!strcmp(uri
->name
, "printer-uri"))
9965 * Got a printer URI; see if we also have a job-id attribute...
9968 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9969 IPP_TAG_INTEGER
)) == NULL
)
9971 send_ipp_status(con
, IPP_BAD_REQUEST
,
9972 _("Got a printer-uri attribute but no job-id!"));
9976 jobid
= attr
->values
[0].integer
;
9981 * Got a job URI; parse it to get the job ID...
9984 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9985 sizeof(method
), username
, sizeof(username
), host
,
9986 sizeof(host
), &port
, resource
, sizeof(resource
));
9988 if (strncmp(resource
, "/jobs/", 6))
9994 send_ipp_status(con
, IPP_BAD_REQUEST
,
9995 _("Bad job-uri attribute \"%s\"!"),
9996 uri
->values
[0].string
.text
);
10000 jobid
= atoi(resource
+ 6);
10004 * See if the job exists...
10007 if ((job
= cupsdFindJob(jobid
)) == NULL
)
10010 * Nope - return a "not found" error...
10013 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
10018 * See if the job has been completed...
10021 if (job
->state_value
> IPP_JOB_STOPPED
)
10024 * Return a "not-possible" error...
10027 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10028 _("Job #%d is finished and cannot be altered!"), jobid
);
10033 * See if the job is owned by the requesting user...
10036 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
10038 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
10043 * See what the user wants to change.
10051 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10053 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
10056 if (!strcmp(attr
->name
, "attributes-charset") ||
10057 !strcmp(attr
->name
, "attributes-natural-language") ||
10058 !strcmp(attr
->name
, "document-compression") ||
10059 !strcmp(attr
->name
, "document-format") ||
10060 !strcmp(attr
->name
, "job-detailed-status-messages") ||
10061 !strcmp(attr
->name
, "job-document-access-errors") ||
10062 !strcmp(attr
->name
, "job-id") ||
10063 !strcmp(attr
->name
, "job-impressions-completed") ||
10064 !strcmp(attr
->name
, "job-k-octets") ||
10065 !strcmp(attr
->name
, "job-originating-host-name") ||
10066 !strcmp(attr
->name
, "job-originating-user-name") ||
10067 !strcmp(attr
->name
, "job-printer-up-time") ||
10068 !strcmp(attr
->name
, "job-printer-uri") ||
10069 !strcmp(attr
->name
, "job-sheets") ||
10070 !strcmp(attr
->name
, "job-state-message") ||
10071 !strcmp(attr
->name
, "job-state-reasons") ||
10072 !strcmp(attr
->name
, "job-uri") ||
10073 !strcmp(attr
->name
, "number-of-documents") ||
10074 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
10075 !strcmp(attr
->name
, "output-device-assigned") ||
10076 !strncmp(attr
->name
, "date-time-at-", 13) ||
10077 !strncmp(attr
->name
, "job-k-octets", 12) ||
10078 !strncmp(attr
->name
, "job-media-sheets", 16) ||
10079 !strncmp(attr
->name
, "time-at-", 8))
10085 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
10086 _("%s cannot be changed."), attr
->name
);
10088 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10089 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10094 if (!strcmp(attr
->name
, "job-priority"))
10097 * Change the job priority...
10100 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10102 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
10104 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10105 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10107 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
10109 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10110 _("Job is completed and cannot be changed."));
10113 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10115 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
10116 attr
->values
[0].integer
);
10117 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
10120 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
10121 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
10124 else if (!strcmp(attr
->name
, "job-state"))
10127 * Change the job state...
10130 if (attr
->value_tag
!= IPP_TAG_ENUM
)
10132 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
10134 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10135 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10139 switch (attr
->values
[0].integer
)
10141 case IPP_JOB_PENDING
:
10142 case IPP_JOB_HELD
:
10143 if (job
->state_value
> IPP_JOB_HELD
)
10145 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10146 _("Job state cannot be changed."));
10149 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10151 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10152 attr
->values
[0].integer
);
10154 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
10155 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
10157 event
|= CUPSD_EVENT_JOB_STATE
;
10162 case IPP_JOB_PROCESSING
:
10163 case IPP_JOB_STOPPED
:
10164 if (job
->state_value
!= attr
->values
[0].integer
)
10166 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10167 _("Job state cannot be changed."));
10172 case IPP_JOB_CANCELED
:
10173 case IPP_JOB_ABORTED
:
10174 case IPP_JOB_COMPLETED
:
10175 if (job
->state_value
> IPP_JOB_PROCESSING
)
10177 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10178 _("Job state cannot be changed."));
10181 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10183 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10184 attr
->values
[0].integer
);
10185 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
10193 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10195 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10196 IPP_TAG_ZERO
)) != NULL
)
10199 * Some other value; first free the old value...
10202 if (job
->attrs
->prev
)
10203 job
->attrs
->prev
->next
= attr2
->next
;
10205 job
->attrs
->attrs
= attr2
->next
;
10207 if (job
->attrs
->last
== attr2
)
10208 job
->attrs
->last
= job
->attrs
->prev
;
10210 _ippFreeAttr(attr2
);
10213 * Then copy the attribute...
10216 copy_attribute(job
->attrs
, attr
, 0);
10219 * See if the job-name or job-hold-until is being changed.
10222 if (!strcmp(attr
->name
, "job-hold-until"))
10224 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10225 attr
->values
[0].string
.text
);
10226 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
10228 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10229 cupsdReleaseJob(job
);
10234 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10237 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10240 * Delete the attribute...
10243 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10244 IPP_TAG_ZERO
)) != NULL
)
10246 if (job
->attrs
->prev
)
10247 job
->attrs
->prev
->next
= attr2
->next
;
10249 job
->attrs
->attrs
= attr2
->next
;
10251 if (attr2
== job
->attrs
->last
)
10252 job
->attrs
->last
= job
->attrs
->prev
;
10254 _ippFreeAttr(attr2
);
10256 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10262 * Add new option by copying it...
10265 copy_attribute(job
->attrs
, attr
, 0);
10267 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10276 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10279 * Send events as needed...
10282 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10283 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10284 cupsdFindDest(job
->dest
), job
,
10285 "Job priority changed by user.");
10287 if (event
& CUPSD_EVENT_JOB_STATE
)
10288 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10289 job
->state_value
== IPP_JOB_HELD
?
10290 "Job held by user." : "Job restarted by user.");
10292 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10293 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10294 "Job options changed by user.");
10297 * Start jobs if possible...
10306 * 'set_printer_defaults()' - Set printer default options from a request.
10310 set_printer_defaults(
10311 cupsd_client_t
*con
, /* I - Client connection */
10312 cupsd_printer_t
*printer
) /* I - Printer */
10314 int i
; /* Looping var */
10315 ipp_attribute_t
*attr
; /* Current attribute */
10316 int namelen
; /* Length of attribute name */
10317 char name
[256], /* New attribute name */
10318 value
[256]; /* String version of integer attrs */
10321 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10324 * Skip non-printer attributes...
10327 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10330 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10332 if (!strcmp(attr
->name
, "job-sheets-default"))
10335 * Only allow keywords and names...
10338 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10342 * Only allow job-sheets-default to be set when running without a
10343 * system high classification level...
10346 if (Classification
)
10349 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10351 if (attr
->num_values
> 1)
10352 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10354 cupsdSetString(&printer
->job_sheets
[1], "none");
10356 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10358 cupsdFreePrinterUsers(printer
);
10360 printer
->deny_users
= 0;
10362 if (attr
->value_tag
== IPP_TAG_NAME
&&
10363 (attr
->num_values
> 1 ||
10364 strcmp(attr
->values
[0].string
.text
, "all")))
10366 for (i
= 0; i
< attr
->num_values
; i
++)
10367 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
10370 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10372 cupsdFreePrinterUsers(printer
);
10374 printer
->deny_users
= 1;
10376 if (attr
->value_tag
== IPP_TAG_NAME
&&
10377 (attr
->num_values
> 1 ||
10378 strcmp(attr
->values
[0].string
.text
, "none")))
10380 for (i
= 0; i
< attr
->num_values
; i
++)
10381 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
10384 else if (!strcmp(attr
->name
, "job-quota-period"))
10386 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10389 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10390 attr
->values
[0].integer
);
10391 cupsdFreeQuotas(printer
);
10393 printer
->quota_period
= attr
->values
[0].integer
;
10395 else if (!strcmp(attr
->name
, "job-k-limit"))
10397 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10400 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10401 attr
->values
[0].integer
);
10402 cupsdFreeQuotas(printer
);
10404 printer
->k_limit
= attr
->values
[0].integer
;
10406 else if (!strcmp(attr
->name
, "job-page-limit"))
10408 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10411 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10412 attr
->values
[0].integer
);
10413 cupsdFreeQuotas(printer
);
10415 printer
->page_limit
= attr
->values
[0].integer
;
10417 else if (!strcmp(attr
->name
, "printer-op-policy"))
10419 cupsd_policy_t
*p
; /* Policy */
10422 if (attr
->value_tag
!= IPP_TAG_NAME
)
10425 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10427 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10428 "Setting printer-op-policy to \"%s\"...",
10429 attr
->values
[0].string
.text
);
10430 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10431 printer
->op_policy_ptr
= p
;
10435 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10436 _("Unknown printer-op-policy \"%s\"."),
10437 attr
->values
[0].string
.text
);
10441 else if (!strcmp(attr
->name
, "printer-error-policy"))
10443 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10446 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10447 strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10448 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10449 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
10451 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10452 _("Unknown printer-error-policy \"%s\"."),
10453 attr
->values
[0].string
.text
);
10457 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10458 "Setting printer-error-policy to \"%s\"...",
10459 attr
->values
[0].string
.text
);
10460 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10464 * Skip any other non-default attributes...
10467 namelen
= strlen(attr
->name
);
10468 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10469 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10473 * OK, anything else must be a user-defined default...
10476 strlcpy(name
, attr
->name
, sizeof(name
));
10477 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10479 switch (attr
->value_tag
)
10481 case IPP_TAG_DELETEATTR
:
10482 printer
->num_options
= cupsRemoveOption(name
,
10483 printer
->num_options
,
10484 &(printer
->options
));
10485 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10486 "Deleting %s", attr
->name
);
10489 case IPP_TAG_NAME
:
10490 case IPP_TAG_KEYWORD
:
10492 printer
->num_options
= cupsAddOption(name
,
10493 attr
->values
[0].string
.text
,
10494 printer
->num_options
,
10495 &(printer
->options
));
10496 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10497 "Setting %s to \"%s\"...", attr
->name
,
10498 attr
->values
[0].string
.text
);
10501 case IPP_TAG_BOOLEAN
:
10502 printer
->num_options
= cupsAddOption(name
,
10503 attr
->values
[0].boolean
?
10505 printer
->num_options
,
10506 &(printer
->options
));
10507 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10508 "Setting %s to %s...", attr
->name
,
10509 attr
->values
[0].boolean
? "true" : "false");
10512 case IPP_TAG_INTEGER
:
10513 case IPP_TAG_ENUM
:
10514 sprintf(value
, "%d", attr
->values
[0].integer
);
10515 printer
->num_options
= cupsAddOption(name
, value
,
10516 printer
->num_options
,
10517 &(printer
->options
));
10518 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10519 "Setting %s to %s...", attr
->name
, value
);
10522 case IPP_TAG_RANGE
:
10523 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10524 attr
->values
[0].range
.upper
);
10525 printer
->num_options
= cupsAddOption(name
, value
,
10526 printer
->num_options
,
10527 &(printer
->options
));
10528 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10529 "Setting %s to %s...", attr
->name
, value
);
10532 case IPP_TAG_RESOLUTION
:
10533 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10534 attr
->values
[0].resolution
.yres
,
10535 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10537 printer
->num_options
= cupsAddOption(name
, value
,
10538 printer
->num_options
,
10539 &(printer
->options
));
10540 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10541 "Setting %s to %s...", attr
->name
, value
);
10545 /* Do nothing for other values */
10553 * 'start_printer()' - Start a printer.
10557 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10558 ipp_attribute_t
*uri
) /* I - Printer URI */
10560 http_status_t status
; /* Policy status */
10561 cups_ptype_t dtype
; /* Destination type (printer/class) */
10562 cupsd_printer_t
*printer
; /* Printer data */
10565 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10566 con
->http
.fd
, uri
->values
[0].string
.text
);
10569 * Is the destination valid?
10572 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10578 send_ipp_status(con
, IPP_NOT_FOUND
,
10579 _("The printer or class was not found."));
10587 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10589 send_http_error(con
, status
, printer
);
10594 * Start the printer...
10597 printer
->state_message
[0] = '\0';
10599 cupsdStartPrinter(printer
, 1);
10601 if (dtype
& CUPS_PRINTER_CLASS
)
10602 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10603 printer
->name
, get_username(con
));
10605 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10606 printer
->name
, get_username(con
));
10611 * Everything was ok, so return OK status...
10614 con
->response
->request
.status
.status_code
= IPP_OK
;
10619 * 'stop_printer()' - Stop a printer.
10623 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10624 ipp_attribute_t
*uri
) /* I - Printer URI */
10626 http_status_t status
; /* Policy status */
10627 cups_ptype_t dtype
; /* Destination type (printer/class) */
10628 cupsd_printer_t
*printer
; /* Printer data */
10629 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10632 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_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 * Stop the printer...
10664 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10665 IPP_TAG_TEXT
)) == NULL
)
10666 strcpy(printer
->state_message
, "Paused");
10669 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10670 sizeof(printer
->state_message
));
10673 cupsdStopPrinter(printer
, 1);
10675 if (dtype
& CUPS_PRINTER_CLASS
)
10676 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10677 printer
->name
, get_username(con
));
10679 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10680 printer
->name
, get_username(con
));
10683 * Everything was ok, so return OK status...
10686 con
->response
->request
.status
.status_code
= IPP_OK
;
10691 * 'url_encode_attr()' - URL-encode a string attribute.
10695 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10696 char *buffer
,/* I - String buffer */
10697 int bufsize
)/* I - Size of buffer */
10699 int i
; /* Looping var */
10700 char *bufptr
, /* Pointer into buffer */
10701 *bufend
; /* End of buffer */
10704 strlcpy(buffer
, attr
->name
, bufsize
);
10705 bufptr
= buffer
+ strlen(buffer
);
10706 bufend
= buffer
+ bufsize
- 1;
10708 for (i
= 0; i
< attr
->num_values
; i
++)
10710 if (bufptr
>= bufend
)
10718 if (bufptr
>= bufend
)
10723 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
10724 bufptr
, bufend
- bufptr
+ 1);
10726 if (bufptr
>= bufend
)
10737 * 'url_encode_string()' - URL-encode a string.
10740 static char * /* O - End of string */
10741 url_encode_string(const char *s
, /* I - String */
10742 char *buffer
, /* I - String buffer */
10743 int bufsize
) /* I - Size of buffer */
10745 char *bufptr
, /* Pointer into buffer */
10746 *bufend
; /* End of buffer */
10747 static const char *hex
= "0123456789ABCDEF";
10752 bufend
= buffer
+ bufsize
- 1;
10754 while (*s
&& bufptr
< bufend
)
10756 if (*s
== ' ' || *s
== '%')
10758 if (bufptr
>= (bufend
- 2))
10762 *bufptr
++ = hex
[(*s
>> 4) & 15];
10763 *bufptr
++ = hex
[*s
& 15];
10767 else if (*s
== '\'' || *s
== '\\')
10769 if (bufptr
>= (bufend
- 1))
10786 * 'user_allowed()' - See if a user is allowed to print to a queue.
10789 static int /* O - 0 if not allowed, 1 if allowed */
10790 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
10791 const char *username
) /* I - Username */
10793 int i
; /* Looping var */
10794 struct passwd
*pw
; /* User password data */
10795 char baseuser
[256], /* Base username */
10796 *baseptr
; /* Pointer to "@" in base username */
10799 if (p
->num_users
== 0)
10802 if (!strcmp(username
, "root"))
10805 if (strchr(username
, '@'))
10808 * Strip @REALM for username check...
10811 strlcpy(baseuser
, username
, sizeof(baseuser
));
10813 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
10816 username
= baseuser
;
10819 pw
= getpwnam(username
);
10822 for (i
= 0; i
< p
->num_users
; i
++)
10824 if (p
->users
[i
][0] == '@')
10827 * Check group membership...
10830 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
10833 else if (p
->users
[i
][0] == '#')
10839 if (cupsdCheckGroup(username
, pw
, p
->users
[i
]))
10842 else if (!strcasecmp(username
, p
->users
[i
]))
10846 return ((i
< p
->num_users
) != p
->deny_users
);
10851 * 'validate_job()' - Validate printer options and destination.
10855 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10856 ipp_attribute_t
*uri
) /* I - Printer URI */
10858 http_status_t status
; /* Policy status */
10859 ipp_attribute_t
*attr
; /* Current attribute */
10860 ipp_attribute_t
*format
; /* Document-format attribute */
10861 cups_ptype_t dtype
; /* Destination type (printer/class) */
10862 char super
[MIME_MAX_SUPER
],
10863 /* Supertype of file */
10864 type
[MIME_MAX_TYPE
];
10865 /* Subtype of file */
10866 cupsd_printer_t
*printer
; /* Printer */
10869 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10870 con
->http
.fd
, uri
->values
[0].string
.text
);
10873 * OK, see if the client is sending the document compressed - CUPS
10874 * doesn't support compression yet...
10877 if ((attr
= ippFindAttribute(con
->request
, "compression",
10878 IPP_TAG_KEYWORD
)) != NULL
&&
10879 !strcmp(attr
->values
[0].string
.text
, "none"))
10881 send_ipp_status(con
, IPP_ATTRIBUTES
,
10882 _("Unsupported compression attribute %s!"),
10883 attr
->values
[0].string
.text
);
10884 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10885 "compression", NULL
, attr
->values
[0].string
.text
);
10890 * Is it a format we support?
10893 if ((format
= ippFindAttribute(con
->request
, "document-format",
10894 IPP_TAG_MIMETYPE
)) != NULL
)
10896 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
10899 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
10900 format
->values
[0].string
.text
);
10904 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10905 !mimeType(MimeDatabase
, super
, type
))
10907 cupsdLogMessage(CUPSD_LOG_INFO
,
10908 "Hint: Do you have the raw file printing rules enabled?");
10909 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10910 _("Unsupported format \"%s\"!"),
10911 format
->values
[0].string
.text
);
10912 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10913 "document-format", NULL
, format
->values
[0].string
.text
);
10919 * Is the destination valid?
10922 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10928 send_ipp_status(con
, IPP_NOT_FOUND
,
10929 _("The printer or class was not found."));
10937 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10939 send_http_error(con
, status
, printer
);
10944 * Everything was ok, so return OK status...
10947 con
->response
->request
.status
.status_code
= IPP_OK
;
10952 * 'validate_name()' - Make sure the printer name only contains valid chars.
10955 static int /* O - 0 if name is no good, 1 if good */
10956 validate_name(const char *name
) /* I - Name to check */
10958 const char *ptr
; /* Pointer into name */
10962 * Scan the whole name...
10965 for (ptr
= name
; *ptr
; ptr
++)
10966 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
10970 * All the characters are good; validate the length, too...
10973 return ((ptr
- name
) < 128);
10978 * 'validate_user()' - Validate the user for the request.
10981 static int /* O - 1 if permitted, 0 otherwise */
10982 validate_user(cupsd_job_t
*job
, /* I - Job */
10983 cupsd_client_t
*con
, /* I - Client connection */
10984 const char *owner
, /* I - Owner of job/resource */
10985 char *username
, /* O - Authenticated username */
10986 int userlen
) /* I - Length of username */
10988 cupsd_printer_t
*printer
; /* Printer for job */
10991 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
10992 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
10994 job
->id
, con
? con
->http
.fd
: 0,
10995 owner
? owner
: "(null)", username
, userlen
);
10998 * Validate input...
11001 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11005 * Get the best authenticated username that is available.
11008 strlcpy(username
, get_username(con
), userlen
);
11011 * Check the username against the owner...
11014 printer
= cupsdFindDest(job
->dest
);
11016 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11017 con
, owner
) == HTTP_OK
);
11022 * End of "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $".