2 * "$Id: ipp.c 7014 2007-10-10 21:57:43Z 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 subcriptions 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 * copy_attribute() - Copy a single attribute.
46 * copy_attrs() - Copy attributes from one request to another.
47 * copy_banner() - Copy a banner file to the requests directory
48 * for the specified job.
49 * copy_file() - Copy a PPD file or interface script...
50 * copy_model() - Copy a PPD model file, substituting default
52 * copy_job_attrs() - Copy job attributes.
53 * copy_printer_attrs() - Copy printer attributes.
54 * copy_subscription_attrs() - Copy subscription attributes.
55 * create_job() - Print a file to a printer or class.
56 * create_requested_array() - Create an array for the requested-attributes.
57 * create_subscription() - Create a notification subscription.
58 * delete_printer() - Remove a printer or class from the system.
59 * get_default() - Get the default destination.
60 * get_devices() - Get the list of available devices on the
62 * get_document() - Get a copy of a job file.
63 * get_job_attrs() - Get job attributes.
64 * get_jobs() - Get a list of jobs for the specified printer.
65 * get_notifications() - Get events for a subscription.
66 * get_ppd() - Get a named PPD from the local system.
67 * get_ppds() - Get the list of PPD files on the local
69 * get_printer_attrs() - Get printer attributes.
70 * get_printers() - Get a list of printers or classes.
71 * get_subscription_attrs() - Get subscription attributes.
72 * get_subscriptions() - Get subscriptions.
73 * get_username() - Get the username associated with a request.
74 * hold_job() - Hold a print job.
75 * move_job() - Move a job to a new destination.
76 * ppd_parse_line() - Parse a PPD default line.
77 * print_job() - Print a file to a printer or class.
78 * read_ps_job_ticket() - Reads a job ticket embedded in a PS file.
79 * reject_jobs() - Reject print jobs to a printer.
80 * release_job() - Release a held print job.
81 * renew_subscription() - Renew an existing subscription...
82 * restart_job() - Restart an old print job.
83 * save_auth_info() - Save authentication information for a job.
84 * save_krb5_creds() - Save Kerberos credentials for the job.
85 * send_document() - Send a file to a printer or class.
86 * send_http_error() - Send a HTTP error back to the IPP client.
87 * send_ipp_status() - Send a status back to the IPP client.
88 * set_default() - Set the default destination...
89 * set_job_attrs() - Set job attributes.
90 * set_printer_defaults() - Set printer default options from a request.
91 * start_printer() - Start a printer.
92 * stop_printer() - Stop a printer.
93 * url_encode_attr() - URL-encode a string attribute.
94 * url_encode_string() - URL-encode a string.
95 * user_allowed() - See if a user is allowed to print to a queue.
96 * validate_job() - Validate printer options and destination.
97 * validate_name() - Make sure the printer name only contains
99 * validate_user() - Validate the user for the request.
103 * Include necessary headers...
107 #include <cups/ppd-private.h>
111 #endif /* HAVE_LIBPAPER */
114 # include <ApplicationServices/ApplicationServices.h>
115 # include <CoreFoundation/CoreFoundation.h>
116 # ifdef HAVE_MEMBERSHIP_H
117 # include <membership.h>
118 # endif /* HAVE_MEMBERSHIP_H */
119 # ifdef HAVE_MEMBERSHIPPRIV_H
120 # include <membershipPriv.h>
122 extern int mbr_user_name_to_uuid(const char* name
, uuid_t uu
);
123 extern int mbr_group_name_to_uuid(const char* name
, uuid_t uu
);
124 extern int mbr_check_membership_by_id(uuid_t user
, gid_t group
, int* ismember
);
125 # endif /* HAVE_MEMBERSHIPPRIV_H */
126 #endif /* __APPLE__ */
133 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
134 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
135 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
136 mime_type_t
*filetype
, int compression
);
137 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
138 mime_type_t
*filetype
);
139 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
140 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
141 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
142 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
143 static void add_printer_state_reasons(cupsd_client_t
*con
,
145 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
147 static void apple_init_profile(ppd_file_t
*ppd
, cups_array_t
*languages
,
148 CMDeviceProfileInfo
*profile
, unsigned id
,
149 const char *name
, const char *text
,
150 const char *iccfile
);
151 static void apple_register_profiles(cupsd_printer_t
*p
);
152 static void apple_unregister_profiles(cupsd_printer_t
*p
);
153 #endif /* __APPLE__ */
154 static void apply_printer_defaults(cupsd_printer_t
*printer
,
156 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
157 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
158 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
159 static void cancel_subscription(cupsd_client_t
*con
, int id
);
160 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
161 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
163 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
164 ipp_tag_t group
, int quickcopy
);
165 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
167 static int copy_file(const char *from
, const char *to
);
168 static int copy_model(cupsd_client_t
*con
, const char *from
,
170 static void copy_job_attrs(cupsd_client_t
*con
,
173 static void copy_printer_attrs(cupsd_client_t
*con
,
174 cupsd_printer_t
*printer
,
176 static void copy_subscription_attrs(cupsd_client_t
*con
,
177 cupsd_subscription_t
*sub
,
179 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
180 static cups_array_t
*create_requested_array(ipp_t
*request
);
181 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
182 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
183 static void get_default(cupsd_client_t
*con
);
184 static void get_devices(cupsd_client_t
*con
);
185 static void get_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
186 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
187 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
188 static void get_notifications(cupsd_client_t
*con
);
189 static void get_ppd(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
190 static void get_ppds(cupsd_client_t
*con
);
191 static void get_printers(cupsd_client_t
*con
, int type
);
192 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
193 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
194 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
195 static const char *get_username(cupsd_client_t
*con
);
196 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
197 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
198 static int ppd_parse_line(const char *line
, char *option
, int olen
,
199 char *choice
, int clen
);
200 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
201 static void read_ps_job_ticket(cupsd_client_t
*con
);
202 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
203 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
204 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
205 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
206 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
,
207 ipp_attribute_t
*auth_info
);
208 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
209 static void save_krb5_creds(cupsd_client_t
*con
, cupsd_job_t
*job
);
210 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
211 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
212 static void send_http_error(cupsd_client_t
*con
, http_status_t status
,
213 cupsd_printer_t
*printer
);
214 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
215 const char *message
, ...)
217 __attribute__ ((__format__ (__printf__
, 3, 4)))
218 # endif /* __GNUC__ */
220 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
221 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
222 static void set_printer_defaults(cupsd_client_t
*con
,
223 cupsd_printer_t
*printer
);
224 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
225 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
226 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
,
228 static char *url_encode_string(const char *s
, char *buffer
, int bufsize
);
229 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
230 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
231 static int validate_name(const char *name
);
232 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
,
233 const char *owner
, char *username
,
238 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
241 int /* O - 1 on success, 0 on failure */
242 cupsdProcessIPPRequest(
243 cupsd_client_t
*con
) /* I - Client connection */
245 ipp_tag_t group
; /* Current group tag */
246 ipp_attribute_t
*attr
; /* Current attribute */
247 ipp_attribute_t
*charset
; /* Character set attribute */
248 ipp_attribute_t
*language
; /* Language attribute */
249 ipp_attribute_t
*uri
; /* Printer URI attribute */
250 ipp_attribute_t
*username
; /* requesting-user-name attr */
251 int sub_id
; /* Subscription ID */
254 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
255 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
256 con
, con
->http
.fd
, con
->request
->request
.op
.operation_id
);
259 * First build an empty response message for this request...
262 con
->response
= ippNew();
264 con
->response
->request
.status
.version
[0] =
265 con
->request
->request
.op
.version
[0];
266 con
->response
->request
.status
.version
[1] =
267 con
->request
->request
.op
.version
[1];
268 con
->response
->request
.status
.request_id
=
269 con
->request
->request
.op
.request_id
;
272 * Then validate the request header and required attributes...
275 if (con
->request
->request
.any
.version
[0] != 1)
278 * Return an error, since we only support IPP 1.x.
281 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
282 "%04X %s Bad request version number %d.%d",
283 IPP_VERSION_NOT_SUPPORTED
, con
->http
.hostname
,
284 con
->request
->request
.any
.version
[0],
285 con
->request
->request
.any
.version
[1]);
287 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
288 _("Bad request version number %d.%d!"),
289 con
->request
->request
.any
.version
[0],
290 con
->request
->request
.any
.version
[1]);
292 else if (!con
->request
->attrs
)
294 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
295 "%04X %s No attributes in request",
296 IPP_BAD_REQUEST
, con
->http
.hostname
);
298 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request!"));
303 * Make sure that the attributes are provided in the correct order and
304 * don't repeat groups...
307 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
310 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
313 * Out of order; return an error...
316 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
317 "%04X %s Attribute groups are out of order",
318 IPP_BAD_REQUEST
, con
->http
.hostname
);
320 send_ipp_status(con
, IPP_BAD_REQUEST
,
321 _("Attribute groups are out of order (%x < %x)!"),
322 attr
->group_tag
, group
);
326 group
= attr
->group_tag
;
331 * Then make sure that the first three attributes are:
334 * attributes-natural-language
335 * printer-uri/job-uri
338 attr
= con
->request
->attrs
;
339 if (attr
&& !strcmp(attr
->name
, "attributes-charset") &&
340 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
348 if (attr
&& !strcmp(attr
->name
, "attributes-natural-language") &&
349 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
354 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
355 IPP_TAG_URI
)) != NULL
)
357 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
358 IPP_TAG_URI
)) != NULL
)
360 else if (con
->request
->request
.op
.operation_id
== CUPS_GET_PPD
)
361 uri
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
);
366 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
367 "attributes-charset", NULL
,
368 charset
->values
[0].string
.text
);
370 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
371 "attributes-charset", NULL
, DefaultCharset
);
374 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
375 "attributes-natural-language", NULL
,
376 language
->values
[0].string
.text
);
378 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
379 "attributes-natural-language", NULL
, DefaultLanguage
);
382 strcasecmp(charset
->values
[0].string
.text
, "us-ascii") &&
383 strcasecmp(charset
->values
[0].string
.text
, "utf-8"))
386 * Bad character set...
389 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unsupported character set \"%s\"!",
390 charset
->values
[0].string
.text
);
391 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
392 "%04X %s Unsupported attributes-charset value \"%s\"",
393 IPP_CHARSET
, con
->http
.hostname
,
394 charset
->values
[0].string
.text
);
395 send_ipp_status(con
, IPP_BAD_REQUEST
,
396 _("Unsupported character set \"%s\"!"),
397 charset
->values
[0].string
.text
);
399 else if (!charset
|| !language
||
401 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
402 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
403 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
404 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
405 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
408 * Return an error, since attributes-charset,
409 * attributes-natural-language, and printer-uri/job-uri are required
410 * for all operations.
415 cupsdLogMessage(CUPSD_LOG_ERROR
,
416 "Missing attributes-charset attribute!");
418 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
419 "%04X %s Missing attributes-charset attribute",
420 IPP_BAD_REQUEST
, con
->http
.hostname
);
425 cupsdLogMessage(CUPSD_LOG_ERROR
,
426 "Missing attributes-natural-language attribute!");
428 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
429 "%04X %s Missing attributes-natural-language attribute",
430 IPP_BAD_REQUEST
, con
->http
.hostname
);
435 cupsdLogMessage(CUPSD_LOG_ERROR
,
436 "Missing printer-uri, job-uri, or ppd-name "
439 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
440 "%04X %s Missing printer-uri, job-uri, or ppd-name "
441 "attribute", IPP_BAD_REQUEST
, con
->http
.hostname
);
444 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
446 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
447 cupsdLogMessage(CUPSD_LOG_DEBUG
,
448 "attr \"%s\": group_tag = %x, value_tag = %x",
449 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
452 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
454 send_ipp_status(con
, IPP_BAD_REQUEST
,
455 _("Missing required attributes!"));
460 * OK, all the checks pass so far; make sure requesting-user-name is
461 * not "root" from a remote host...
464 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
465 IPP_TAG_NAME
)) != NULL
)
468 * Check for root user...
471 if (!strcmp(username
->values
[0].string
.text
, "root") &&
472 strcasecmp(con
->http
.hostname
, "localhost") &&
473 strcmp(con
->username
, "root"))
476 * Remote unauthenticated user masquerading as local root...
479 _cupsStrFree(username
->values
[0].string
.text
);
480 username
->values
[0].string
.text
= _cupsStrAlloc(RemoteRoot
);
484 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
485 IPP_TAG_INTEGER
)) != NULL
)
486 sub_id
= attr
->values
[0].integer
;
491 * Then try processing the operation...
495 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
496 ippOpString(con
->request
->request
.op
.operation_id
),
497 uri
->values
[0].string
.text
);
499 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
500 ippOpString(con
->request
->request
.op
.operation_id
));
502 switch (con
->request
->request
.op
.operation_id
)
508 case IPP_VALIDATE_JOB
:
509 validate_job(con
, uri
);
512 case IPP_CREATE_JOB
:
513 create_job(con
, uri
);
516 case IPP_SEND_DOCUMENT
:
517 send_document(con
, uri
);
520 case IPP_CANCEL_JOB
:
521 cancel_job(con
, uri
);
524 case IPP_GET_JOB_ATTRIBUTES
:
525 get_job_attrs(con
, uri
);
532 case IPP_GET_PRINTER_ATTRIBUTES
:
533 get_printer_attrs(con
, uri
);
540 case IPP_RELEASE_JOB
:
541 release_job(con
, uri
);
544 case IPP_RESTART_JOB
:
545 restart_job(con
, uri
);
548 case IPP_PAUSE_PRINTER
:
549 stop_printer(con
, uri
);
552 case IPP_RESUME_PRINTER
:
553 start_printer(con
, uri
);
556 case IPP_PURGE_JOBS
:
557 cancel_all_jobs(con
, uri
);
560 case IPP_SET_JOB_ATTRIBUTES
:
561 set_job_attrs(con
, uri
);
564 case CUPS_GET_DEFAULT
:
568 case CUPS_GET_PRINTERS
:
569 get_printers(con
, 0);
572 case CUPS_GET_CLASSES
:
573 get_printers(con
, CUPS_PRINTER_CLASS
);
576 case CUPS_ADD_PRINTER
:
577 add_printer(con
, uri
);
580 case CUPS_DELETE_PRINTER
:
581 delete_printer(con
, uri
);
584 case CUPS_ADD_CLASS
:
588 case CUPS_DELETE_CLASS
:
589 delete_printer(con
, uri
);
592 case CUPS_ACCEPT_JOBS
:
593 case IPP_ENABLE_PRINTER
:
594 accept_jobs(con
, uri
);
597 case CUPS_REJECT_JOBS
:
598 case IPP_DISABLE_PRINTER
:
599 reject_jobs(con
, uri
);
602 case CUPS_SET_DEFAULT
:
603 set_default(con
, uri
);
606 case CUPS_GET_DEVICES
:
610 case CUPS_GET_DOCUMENT
:
611 get_document(con
, uri
);
626 case CUPS_AUTHENTICATE_JOB
:
627 authenticate_job(con
, uri
);
630 case IPP_CREATE_PRINTER_SUBSCRIPTION
:
631 case IPP_CREATE_JOB_SUBSCRIPTION
:
632 create_subscription(con
, uri
);
635 case IPP_GET_SUBSCRIPTION_ATTRIBUTES
:
636 get_subscription_attrs(con
, sub_id
);
639 case IPP_GET_SUBSCRIPTIONS
:
640 get_subscriptions(con
, uri
);
643 case IPP_RENEW_SUBSCRIPTION
:
644 renew_subscription(con
, sub_id
);
647 case IPP_CANCEL_SUBSCRIPTION
:
648 cancel_subscription(con
, sub_id
);
651 case IPP_GET_NOTIFICATIONS
:
652 get_notifications(con
);
656 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
657 "%04X %s Operation %04X (%s) not supported",
658 IPP_OPERATION_NOT_SUPPORTED
, con
->http
.hostname
,
659 con
->request
->request
.op
.operation_id
,
660 ippOpString(con
->request
->request
.op
.operation_id
));
662 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
663 _("%s not supported!"),
665 con
->request
->request
.op
.operation_id
));
675 * Sending data from the scheduler...
678 cupsdLogMessage(CUPSD_LOG_DEBUG
,
679 "cupsdProcessIPPRequest: %d status_code=%x (%s)",
680 con
->http
.fd
, con
->response
->request
.status
.status_code
,
681 ippErrorString(con
->response
->request
.status
.status_code
));
683 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp", CUPSD_AUTH_NONE
))
685 #ifdef CUPSD_USE_CHUNKING
687 * Because older versions of CUPS (1.1.17 and older) and some IPP
688 * clients do not implement chunking properly, we cannot use
689 * chunking by default. This may become the default in future
690 * CUPS releases, or we might add a configuration directive for
694 if (con
->http
.version
== HTTP_1_1
)
696 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n") < 0)
699 if (cupsdFlushHeader(con
) < 0)
702 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
705 #endif /* CUPSD_USE_CHUNKING */
707 size_t length
; /* Length of response */
710 length
= ippLength(con
->response
);
712 if (con
->file
>= 0 && !con
->pipe_pid
)
714 struct stat fileinfo
; /* File information */
717 if (!fstat(con
->file
, &fileinfo
))
718 length
+= fileinfo
.st_size
;
721 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
722 CUPS_LLCAST length
) < 0)
725 if (cupsdFlushHeader(con
) < 0)
728 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
729 con
->http
.data_remaining
= length
;
731 if (con
->http
.data_remaining
<= INT_MAX
)
732 con
->http
._data_remaining
= con
->http
.data_remaining
;
734 con
->http
._data_remaining
= INT_MAX
;
737 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
738 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
741 * Tell the caller the response header was sent successfully...
749 * Tell the caller the response header could not be sent...
758 * Sending data from a subprocess like cups-deviced; tell the caller
759 * everything is A-OK so far...
768 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
771 int /* O - 0 on success, -1 on error */
772 cupsdTimeoutJob(cupsd_job_t
*job
) /* I - Job to timeout */
774 cupsd_printer_t
*printer
; /* Destination printer or class */
775 ipp_attribute_t
*attr
; /* job-sheets attribute */
776 int kbytes
; /* Kilobytes in banner */
779 job
->pending_timeout
= 0;
782 * See if we need to add the ending sheet...
785 printer
= cupsdFindDest(job
->dest
);
786 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
789 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
790 attr
&& attr
->num_values
> 1)
796 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Adding end banner page \"%s\".",
797 job
->id
, attr
->values
[1].string
.text
);
799 if ((kbytes
= copy_banner(NULL
, job
, attr
->values
[1].string
.text
)) < 0)
802 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
810 * 'accept_jobs()' - Accept print jobs to a printer.
814 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
815 ipp_attribute_t
*uri
) /* I - Printer or class URI */
817 http_status_t status
; /* Policy status */
818 cups_ptype_t dtype
; /* Destination type (printer/class) */
819 cupsd_printer_t
*printer
; /* Printer data */
822 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
823 con
->http
.fd
, uri
->values
[0].string
.text
);
826 * Is the destination valid?
829 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
835 send_ipp_status(con
, IPP_NOT_FOUND
,
836 _("The printer or class was not found."));
844 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
846 send_http_error(con
, status
, printer
);
851 * Accept jobs sent to the printer...
854 printer
->accepting
= 1;
855 printer
->state_message
[0] = '\0';
857 cupsdAddPrinterHistory(printer
);
859 if (dtype
& CUPS_PRINTER_CLASS
)
861 cupsdSaveAllClasses();
863 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
864 printer
->name
, get_username(con
));
868 cupsdSaveAllPrinters();
870 cupsdLogMessage(CUPSD_LOG_INFO
,
871 "Printer \"%s\" now accepting jobs (\"%s\").",
872 printer
->name
, get_username(con
));
876 * Everything was ok, so return OK status...
879 con
->response
->request
.status
.status_code
= IPP_OK
;
884 * 'add_class()' - Add a class to the system.
888 add_class(cupsd_client_t
*con
, /* I - Client connection */
889 ipp_attribute_t
*uri
) /* I - URI of class */
891 http_status_t status
; /* Policy status */
892 int i
; /* Looping var */
893 char method
[HTTP_MAX_URI
], /* Method portion of URI */
894 username
[HTTP_MAX_URI
], /* Username portion of URI */
895 host
[HTTP_MAX_URI
], /* Host portion of URI */
896 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
897 int port
; /* Port portion of URI */
898 cupsd_printer_t
*pclass
, /* Class */
899 *member
; /* Member printer/class */
900 cups_ptype_t dtype
; /* Destination type */
901 ipp_attribute_t
*attr
; /* Printer attribute */
902 int modify
; /* Non-zero if we just modified */
903 char newname
[IPP_MAX_NAME
]; /* New class name */
904 int need_restart_job
; /* Need to restart job? */
907 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
908 con
->http
.fd
, uri
->values
[0].string
.text
);
911 * Do we have a valid URI?
914 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
915 sizeof(method
), username
, sizeof(username
), host
,
916 sizeof(host
), &port
, resource
, sizeof(resource
));
919 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
922 * No, return an error...
925 send_ipp_status(con
, IPP_BAD_REQUEST
,
926 _("The printer-uri must be of the form "
927 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
932 * Do we have a valid printer name?
935 if (!validate_name(resource
+ 9))
938 * No, return an error...
941 send_ipp_status(con
, IPP_BAD_REQUEST
,
942 _("The printer-uri \"%s\" contains invalid characters."),
943 uri
->values
[0].string
.text
);
948 * See if the class already exists; if not, create a new class...
951 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
954 * Class doesn't exist; see if we have a printer of the same name...
957 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
958 !(pclass
->type
& CUPS_PRINTER_DISCOVERED
))
961 * Yes, return an error...
964 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
965 _("A printer named \"%s\" already exists!"),
971 * No, check the default policy and then add the class...
974 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
976 send_http_error(con
, status
, NULL
);
980 pclass
= cupsdAddClass(resource
+ 9);
983 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
986 * Check the default policy, then rename the implicit class to "AnyClass"
990 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
992 send_http_error(con
, status
, NULL
);
996 if (ImplicitAnyClasses
)
998 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
999 cupsdRenamePrinter(pclass
, newname
);
1002 cupsdDeletePrinter(pclass
, 1);
1005 * Add the class as a new local class...
1008 pclass
= cupsdAddClass(resource
+ 9);
1011 else if (pclass
->type
& CUPS_PRINTER_DISCOVERED
)
1014 * Check the default policy, then rename the remote class to "Class"...
1017 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
1019 send_http_error(con
, status
, NULL
);
1023 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
1024 cupsdRenamePrinter(pclass
, newname
);
1027 * Add the class as a new local class...
1030 pclass
= cupsdAddClass(resource
+ 9);
1033 else if ((status
= cupsdCheckPolicy(pclass
->op_policy_ptr
, con
,
1036 send_http_error(con
, status
, pclass
);
1043 * Look for attributes and copy them over as needed...
1046 need_restart_job
= 0;
1048 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
1049 IPP_TAG_TEXT
)) != NULL
)
1050 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
1052 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
1053 IPP_TAG_TEXT
)) != NULL
)
1054 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
1056 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
1057 IPP_TAG_BOOLEAN
)) != NULL
)
1059 cupsdLogMessage(CUPSD_LOG_INFO
,
1060 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
1061 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
1063 pclass
->accepting
= attr
->values
[0].boolean
;
1064 cupsdAddPrinterHistory(pclass
);
1067 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
1068 IPP_TAG_BOOLEAN
)) != NULL
)
1070 if (pclass
->shared
&& !attr
->values
[0].boolean
)
1071 cupsdDeregisterPrinter(pclass
, 1);
1073 cupsdLogMessage(CUPSD_LOG_INFO
,
1074 "Setting %s printer-is-shared to %d (was %d.)",
1075 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
1077 pclass
->shared
= attr
->values
[0].boolean
;
1080 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
1081 IPP_TAG_ENUM
)) != NULL
)
1083 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
1084 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
1086 send_ipp_status(con
, IPP_BAD_REQUEST
,
1087 _("Attempt to set %s printer-state to bad value %d!"),
1088 pclass
->name
, attr
->values
[0].integer
);
1092 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
1093 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
1095 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1096 cupsdStopPrinter(pclass
, 0);
1099 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1100 need_restart_job
= 1;
1103 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1104 IPP_TAG_TEXT
)) != NULL
)
1106 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1107 sizeof(pclass
->state_message
));
1108 cupsdAddPrinterHistory(pclass
);
1110 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1111 IPP_TAG_URI
)) != NULL
)
1114 * Clear the printer array as needed...
1117 need_restart_job
= 1;
1119 if (pclass
->num_printers
> 0)
1121 free(pclass
->printers
);
1122 pclass
->num_printers
= 0;
1126 * Add each printer or class that is listed...
1129 for (i
= 0; i
< attr
->num_values
; i
++)
1132 * Search for the printer or class URI...
1135 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1141 send_ipp_status(con
, IPP_NOT_FOUND
,
1142 _("The printer or class was not found."));
1147 * Add it to the class...
1150 cupsdAddPrinterToClass(pclass
, member
);
1154 set_printer_defaults(con
, pclass
);
1156 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1157 IPP_TAG_KEYWORD
)) != NULL
)
1158 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1161 * Update the printer class attributes and return...
1164 cupsdSetPrinterAttrs(pclass
);
1165 cupsdSaveAllClasses();
1167 if (need_restart_job
&& pclass
->job
)
1172 * Stop the current job and then restart it below...
1175 job
= (cupsd_job_t
*)pclass
->job
;
1177 cupsdStopJob(job
, 1);
1179 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1180 job
->state_value
= IPP_JOB_PENDING
;
1183 if (need_restart_job
)
1186 cupsdWritePrintcap();
1190 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1191 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1194 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1195 pclass
->name
, get_username(con
));
1199 cupsdAddPrinterHistory(pclass
);
1201 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1202 "New class \"%s\" added by \"%s\".", pclass
->name
,
1205 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1206 pclass
->name
, get_username(con
));
1209 con
->response
->request
.status
.status_code
= IPP_OK
;
1214 * 'add_file()' - Add a file to a job.
1217 static int /* O - 0 on success, -1 on error */
1218 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1219 cupsd_job_t
*job
, /* I - Job to add to */
1220 mime_type_t
*filetype
, /* I - Type of file */
1221 int compression
) /* I - Compression */
1223 mime_type_t
**filetypes
; /* New filetypes array... */
1224 int *compressions
; /* New compressions array... */
1227 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1228 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1229 "compression=%d)", con
, con
? con
->http
.fd
: -1, job
->id
,
1230 filetype
->super
, filetype
->type
, compression
);
1233 * Add the file to the job...
1236 if (job
->num_files
== 0)
1238 compressions
= (int *)malloc(sizeof(int));
1239 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1243 compressions
= (int *)realloc(job
->compressions
,
1244 (job
->num_files
+ 1) * sizeof(int));
1245 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1246 (job
->num_files
+ 1) *
1247 sizeof(mime_type_t
*));
1250 if (!compressions
|| !filetypes
)
1252 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
1255 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1256 _("Unable to allocate memory for file types!"));
1261 job
->compressions
= compressions
;
1262 job
->compressions
[job
->num_files
] = compression
;
1263 job
->filetypes
= filetypes
;
1264 job
->filetypes
[job
->num_files
] = filetype
;
1273 * 'add_job()' - Add a job to a print queue.
1276 static cupsd_job_t
* /* O - Job object */
1277 add_job(cupsd_client_t
*con
, /* I - Client connection */
1278 cupsd_printer_t
*printer
, /* I - Destination printer */
1279 mime_type_t
*filetype
) /* I - First print file type, if any */
1281 http_status_t status
; /* Policy status */
1282 ipp_attribute_t
*attr
, /* Current attribute */
1283 *auth_info
; /* auth-info attribute */
1284 const char *val
; /* Default option value */
1285 int priority
; /* Job priority */
1286 char *title
; /* Job name/title */
1287 cupsd_job_t
*job
; /* Current job */
1288 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1289 int kbytes
; /* Size of print file */
1290 int i
; /* Looping var */
1291 int lowerpagerange
; /* Page range bound */
1294 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1295 con
, con
->http
.fd
, printer
, printer
->name
,
1296 filetype
, filetype
? filetype
->super
: "none",
1297 filetype
? filetype
->type
: "none");
1300 * Check remote printing to non-shared printer...
1303 if (!printer
->shared
&&
1304 strcasecmp(con
->http
.hostname
, "localhost") &&
1305 strcasecmp(con
->http
.hostname
, ServerName
))
1307 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1308 _("The printer or class is not shared!"));
1316 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1318 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1320 send_http_error(con
, status
, printer
);
1323 else if ((printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
1324 !con
->username
[0] && !auth_info
)
1326 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1330 else if (auth_info
&& !con
->http
.tls
&&
1331 !httpAddrLocalhost(con
->http
.hostaddr
))
1334 * Require encryption of auth-info over non-local connections...
1337 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1340 #endif /* HAVE_SSL */
1343 * See if the printer is accepting jobs...
1346 if (!printer
->accepting
)
1348 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1349 _("Destination \"%s\" is not accepting jobs."),
1355 * Validate job template attributes; for now just document-format,
1356 * copies, number-up, and page-ranges...
1359 if (filetype
&& printer
->filetypes
&&
1360 !cupsArrayFind(printer
->filetypes
, filetype
))
1362 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1363 /* MIME media type string */
1366 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1369 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1370 _("Unsupported format \'%s\'!"), mimetype
);
1372 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1373 "document-format", NULL
, mimetype
);
1378 if ((attr
= ippFindAttribute(con
->request
, "copies",
1379 IPP_TAG_INTEGER
)) != NULL
)
1381 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1383 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1384 attr
->values
[0].integer
);
1385 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1386 "copies", attr
->values
[0].integer
);
1391 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1392 IPP_TAG_INTEGER
)) != NULL
)
1394 if (attr
->values
[0].integer
!= 1 &&
1395 attr
->values
[0].integer
!= 2 &&
1396 attr
->values
[0].integer
!= 4 &&
1397 attr
->values
[0].integer
!= 6 &&
1398 attr
->values
[0].integer
!= 9 &&
1399 attr
->values
[0].integer
!= 16)
1401 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1402 attr
->values
[0].integer
);
1403 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1404 "number-up", attr
->values
[0].integer
);
1409 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1410 IPP_TAG_RANGE
)) != NULL
)
1412 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1414 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1415 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1417 send_ipp_status(con
, IPP_BAD_REQUEST
,
1418 _("Bad page-ranges values %d-%d."),
1419 attr
->values
[i
].range
.lower
,
1420 attr
->values
[i
].range
.upper
);
1424 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1429 * Make sure we aren't over our limit...
1432 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1435 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1437 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1438 _("Too many active jobs."));
1442 if ((i
= check_quotas(con
, printer
)) < 0)
1444 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1449 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1454 * Create the job and set things up...
1457 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1458 IPP_TAG_INTEGER
)) != NULL
)
1459 priority
= attr
->values
[0].integer
;
1462 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1463 printer
->options
)) != NULL
)
1464 priority
= atoi(val
);
1468 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1472 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1473 IPP_TAG_NAME
)) != NULL
)
1474 title
= attr
->values
[0].string
.text
;
1476 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1477 title
= "Untitled");
1479 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1481 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1482 _("Unable to add job for destination \"%s\"!"),
1487 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
1488 CUPS_PRINTER_REMOTE
);
1489 job
->attrs
= con
->request
;
1490 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1492 add_job_uuid(con
, job
);
1493 apply_printer_defaults(printer
, job
);
1495 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1497 if (con
->username
[0])
1499 cupsdSetString(&job
->username
, con
->username
);
1502 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1506 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1507 "add_job: requesting-user-name=\"%s\"",
1508 attr
->values
[0].string
.text
);
1510 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1513 cupsdSetString(&job
->username
, "anonymous");
1516 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1517 "job-originating-user-name", NULL
, job
->username
);
1520 attr
->group_tag
= IPP_TAG_JOB
;
1521 _cupsStrFree(attr
->name
);
1522 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1525 if (con
->username
[0] || auth_info
)
1527 save_auth_info(con
, job
, auth_info
);
1530 * Remove the auth-info attribute from the attribute data...
1534 ippDeleteAttribute(job
->attrs
, auth_info
);
1537 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1538 IPP_TAG_ZERO
)) != NULL
)
1541 * Request contains a job-originating-host-name attribute; validate it...
1544 if (attr
->value_tag
!= IPP_TAG_NAME
||
1545 attr
->num_values
!= 1 ||
1546 strcmp(con
->http
.hostname
, "localhost"))
1549 * Can't override the value if we aren't connected via localhost.
1550 * Also, we can only have 1 value and it must be a name value.
1553 switch (attr
->value_tag
)
1555 case IPP_TAG_STRING
:
1556 case IPP_TAG_TEXTLANG
:
1557 case IPP_TAG_NAMELANG
:
1560 case IPP_TAG_KEYWORD
:
1562 case IPP_TAG_URISCHEME
:
1563 case IPP_TAG_CHARSET
:
1564 case IPP_TAG_LANGUAGE
:
1565 case IPP_TAG_MIMETYPE
:
1567 * Free old strings...
1570 for (i
= 0; i
< attr
->num_values
; i
++)
1572 _cupsStrFree(attr
->values
[i
].string
.text
);
1573 attr
->values
[i
].string
.text
= NULL
;
1574 if (attr
->values
[i
].string
.charset
)
1576 _cupsStrFree(attr
->values
[i
].string
.charset
);
1577 attr
->values
[i
].string
.charset
= NULL
;
1586 * Use the default connection hostname instead...
1589 attr
->value_tag
= IPP_TAG_NAME
;
1590 attr
->num_values
= 1;
1591 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1594 attr
->group_tag
= IPP_TAG_JOB
;
1599 * No job-originating-host-name attribute, so use the hostname from
1603 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1604 "job-originating-host-name", NULL
, con
->http
.hostname
);
1607 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1609 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1610 "time-at-processing", 0);
1611 attr
->value_tag
= IPP_TAG_NOVALUE
;
1612 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1613 "time-at-completed", 0);
1614 attr
->value_tag
= IPP_TAG_NOVALUE
;
1617 * Add remaining job attributes...
1620 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1621 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1622 "job-state", IPP_JOB_STOPPED
);
1623 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1624 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1625 "job-media-sheets-completed", 0);
1626 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1628 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1631 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1632 IPP_TAG_INTEGER
)) != NULL
)
1633 attr
->values
[0].integer
= 0;
1635 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1638 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1639 IPP_TAG_KEYWORD
)) == NULL
)
1640 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1643 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1644 printer
->options
)) == NULL
)
1647 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1648 "job-hold-until", NULL
, val
);
1650 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1651 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1654 * Hold job until specified time...
1657 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1659 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1660 job
->state_value
= IPP_JOB_HELD
;
1662 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1664 job
->hold_until
= time(NULL
) + 60;
1665 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1666 job
->state_value
= IPP_JOB_HELD
;
1670 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1671 job
->state_value
= IPP_JOB_PENDING
;
1674 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1678 * Add job sheets options...
1681 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1682 IPP_TAG_ZERO
)) == NULL
)
1684 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1685 "Adding default job-sheets values \"%s,%s\"...",
1686 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1688 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1690 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1691 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1694 job
->job_sheets
= attr
;
1697 * Enforce classification level if set...
1702 cupsdLogMessage(CUPSD_LOG_INFO
,
1703 "Classification=\"%s\", ClassifyOverride=%d",
1704 Classification
? Classification
: "(null)",
1707 if (ClassifyOverride
)
1709 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1710 (attr
->num_values
== 1 ||
1711 !strcmp(attr
->values
[1].string
.text
, "none")))
1714 * Force the leading banner to have the classification on it...
1717 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1719 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1720 "job-sheets=\"%s,none\", "
1721 "job-originating-user-name=\"%s\"",
1722 job
->id
, Classification
, job
->username
);
1724 else if (attr
->num_values
== 2 &&
1725 strcmp(attr
->values
[0].string
.text
,
1726 attr
->values
[1].string
.text
) &&
1727 strcmp(attr
->values
[0].string
.text
, "none") &&
1728 strcmp(attr
->values
[1].string
.text
, "none"))
1731 * Can't put two different security markings on the same document!
1734 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1736 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1737 "job-sheets=\"%s,%s\", "
1738 "job-originating-user-name=\"%s\"",
1739 job
->id
, attr
->values
[0].string
.text
,
1740 attr
->values
[1].string
.text
, job
->username
);
1742 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1743 strcmp(attr
->values
[0].string
.text
, "none") &&
1744 (attr
->num_values
== 1 ||
1745 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1746 strcmp(attr
->values
[1].string
.text
, "none"))))
1748 if (attr
->num_values
== 1)
1749 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1750 "[Job %d] CLASSIFICATION OVERRIDDEN "
1751 "job-sheets=\"%s\", "
1752 "job-originating-user-name=\"%s\"",
1753 job
->id
, attr
->values
[0].string
.text
, job
->username
);
1755 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1756 "[Job %d] CLASSIFICATION OVERRIDDEN "
1757 "job-sheets=\"%s,%s\",fffff "
1758 "job-originating-user-name=\"%s\"",
1759 job
->id
, attr
->values
[0].string
.text
,
1760 attr
->values
[1].string
.text
, job
->username
);
1763 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1764 (attr
->num_values
== 1 ||
1765 strcmp(attr
->values
[1].string
.text
, Classification
)))
1768 * Force the banner to have the classification on it...
1771 if (attr
->num_values
> 1 &&
1772 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1774 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1775 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1779 if (attr
->num_values
== 1 ||
1780 strcmp(attr
->values
[0].string
.text
, "none"))
1781 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1783 if (attr
->num_values
> 1 &&
1784 strcmp(attr
->values
[1].string
.text
, "none"))
1785 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1788 if (attr
->num_values
> 1)
1789 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1790 "[Job %d] CLASSIFICATION FORCED "
1791 "job-sheets=\"%s,%s\", "
1792 "job-originating-user-name=\"%s\"",
1793 job
->id
, attr
->values
[0].string
.text
,
1794 attr
->values
[1].string
.text
, job
->username
);
1796 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1797 "[Job %d] CLASSIFICATION FORCED "
1798 "job-sheets=\"%s\", "
1799 "job-originating-user-name=\"%s\"",
1800 job
->id
, Classification
, job
->username
);
1805 * See if we need to add the starting sheet...
1808 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1810 cupsdLogMessage(CUPSD_LOG_INFO
,
1811 "[Job %d] Adding start banner page \"%s\".",
1812 job
->id
, attr
->values
[0].string
.text
);
1814 if ((kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
)) < 0)
1817 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1820 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1821 IPP_TAG_ZERO
)) != NULL
)
1825 * Fill in the response info...
1828 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1829 LocalPort
, job
->id
);
1831 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1834 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1836 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1838 add_job_state_reasons(con
, job
);
1840 con
->response
->request
.status
.status_code
= IPP_OK
;
1843 * Add any job subscriptions...
1846 add_job_subscriptions(con
, job
);
1849 * Set all but the first two attributes to the job attributes group...
1852 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1853 attr
->group_tag
= IPP_TAG_JOB
;
1856 * Fire the "job created" event...
1859 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1862 * Return the new job...
1870 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1871 * upon the job and printer state...
1875 add_job_state_reasons(
1876 cupsd_client_t
*con
, /* I - Client connection */
1877 cupsd_job_t
*job
) /* I - Job info */
1879 cupsd_printer_t
*dest
; /* Destination printer */
1882 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1883 con
, con
->http
.fd
, job
? job
->id
: 0);
1885 switch (job
? job
->state_value
: IPP_JOB_CANCELED
)
1887 case IPP_JOB_PENDING
:
1888 dest
= cupsdFindDest(job
->dest
);
1890 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1891 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1892 "job-state-reasons", NULL
, "printer-stopped");
1894 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1895 "job-state-reasons", NULL
, "none");
1899 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1900 IPP_TAG_KEYWORD
) != NULL
||
1901 ippFindAttribute(job
->attrs
, "job-hold-until",
1902 IPP_TAG_NAME
) != NULL
)
1903 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1904 "job-state-reasons", NULL
, "job-hold-until-specified");
1906 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1907 "job-state-reasons", NULL
, "job-incoming");
1910 case IPP_JOB_PROCESSING
:
1911 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1912 "job-state-reasons", NULL
, "job-printing");
1915 case IPP_JOB_STOPPED
:
1916 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1917 "job-state-reasons", NULL
, "job-stopped");
1920 case IPP_JOB_CANCELED
:
1921 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1922 "job-state-reasons", NULL
, "job-canceled-by-user");
1925 case IPP_JOB_ABORTED
:
1926 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1927 "job-state-reasons", NULL
, "aborted-by-system");
1930 case IPP_JOB_COMPLETED
:
1931 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1932 "job-state-reasons", NULL
, "job-completed-successfully");
1939 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1943 add_job_subscriptions(
1944 cupsd_client_t
*con
, /* I - Client connection */
1945 cupsd_job_t
*job
) /* I - Newly created job */
1947 int i
; /* Looping var */
1948 ipp_attribute_t
*prev
, /* Previous attribute */
1949 *next
, /* Next attribute */
1950 *attr
; /* Current attribute */
1951 cupsd_subscription_t
*sub
; /* Subscription object */
1952 const char *recipient
, /* notify-recipient-uri */
1953 *pullmethod
; /* notify-pull-method */
1954 ipp_attribute_t
*user_data
; /* notify-user-data */
1955 int interval
; /* notify-time-interval */
1956 unsigned mask
; /* notify-events */
1960 * Find the first subscription group attribute; return if we have
1964 for (attr
= job
->attrs
->attrs
, prev
= NULL
;
1966 prev
= attr
, attr
= attr
->next
)
1967 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1974 * Process the subscription attributes in the request...
1983 mask
= CUPSD_EVENT_NONE
;
1985 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1987 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
1988 attr
->value_tag
== IPP_TAG_URI
)
1989 recipient
= attr
->values
[0].string
.text
;
1990 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1991 attr
->value_tag
== IPP_TAG_KEYWORD
)
1992 pullmethod
= attr
->values
[0].string
.text
;
1993 else if (!strcmp(attr
->name
, "notify-charset") &&
1994 attr
->value_tag
== IPP_TAG_CHARSET
&&
1995 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
1996 strcmp(attr
->values
[0].string
.text
, "utf-8"))
1998 send_ipp_status(con
, IPP_CHARSET
,
1999 _("Character set \"%s\" not supported!"),
2000 attr
->values
[0].string
.text
);
2003 else if (!strcmp(attr
->name
, "notify-natural-language") &&
2004 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
2005 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
2007 send_ipp_status(con
, IPP_CHARSET
,
2008 _("Language \"%s\" not supported!"),
2009 attr
->values
[0].string
.text
);
2012 else if (!strcmp(attr
->name
, "notify-user-data") &&
2013 attr
->value_tag
== IPP_TAG_STRING
)
2015 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
2017 send_ipp_status(con
, IPP_REQUEST_VALUE
,
2018 _("The notify-user-data value is too large "
2019 "(%d > 63 octets)!"),
2020 attr
->values
[0].unknown
.length
);
2026 else if (!strcmp(attr
->name
, "notify-events") &&
2027 attr
->value_tag
== IPP_TAG_KEYWORD
)
2029 for (i
= 0; i
< attr
->num_values
; i
++)
2030 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
2032 else if (!strcmp(attr
->name
, "notify-lease-duration"))
2034 send_ipp_status(con
, IPP_BAD_REQUEST
,
2035 _("The notify-lease-duration attribute cannot be "
2036 "used with job subscriptions."));
2039 else if (!strcmp(attr
->name
, "notify-time-interval") &&
2040 attr
->value_tag
== IPP_TAG_INTEGER
)
2041 interval
= attr
->values
[0].integer
;
2046 if (!recipient
&& !pullmethod
)
2049 if (mask
== CUPSD_EVENT_NONE
)
2050 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2052 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
2055 sub
->interval
= interval
;
2057 cupsdSetString(&sub
->owner
, job
->username
);
2061 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2062 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2063 sub
->user_data_len
);
2066 ippAddSeparator(con
->response
);
2067 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2068 "notify-subscription-id", sub
->id
);
2074 cupsdSaveAllSubscriptions();
2077 * Remove all of the subscription attributes from the job request...
2080 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2084 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2085 attr
->group_tag
== IPP_TAG_ZERO
)
2088 * Free and remove this attribute...
2096 job
->attrs
->attrs
= next
;
2102 job
->attrs
->last
= prev
;
2103 job
->attrs
->current
= prev
;
2108 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2110 * See RFC 4122 for the definition of UUIDs and the format.
2114 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
2115 cupsd_job_t
*job
) /* I - Job */
2117 char uuid
[1024]; /* job-uuid string */
2118 _cups_md5_state_t md5state
; /* MD5 state */
2119 unsigned char md5sum
[16]; /* MD5 digest/sum */
2123 * First see if the job already has a job-uuid attribute; if so, return...
2126 if (ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2130 * No job-uuid attribute, so build a version 3 UUID with the local job
2131 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
2132 * the ServerName, server name and port that the client connected to,
2133 * and local job ID...
2136 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
2137 con
->serverport
, job
->id
);
2139 _cupsMD5Init(&md5state
);
2140 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
2141 _cupsMD5Finish(&md5state
, md5sum
);
2144 * Format the UUID URI using the MD5 sum and job ID.
2147 snprintf(uuid
, sizeof(uuid
),
2148 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2149 "%02x%02x%02x%02x%02x%02x",
2150 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
2151 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
2152 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
2153 md5sum
[14], md5sum
[15]);
2155 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
2160 * 'add_printer()' - Add a printer to the system.
2164 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2165 ipp_attribute_t
*uri
) /* I - URI of printer */
2167 http_status_t status
; /* Policy status */
2168 int i
; /* Looping var */
2169 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2170 username
[HTTP_MAX_URI
], /* Username portion of URI */
2171 host
[HTTP_MAX_URI
], /* Host portion of URI */
2172 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2173 int port
; /* Port portion of URI */
2174 cupsd_printer_t
*printer
; /* Printer/class */
2175 ipp_attribute_t
*attr
; /* Printer attribute */
2176 cups_file_t
*fp
; /* Script/PPD file */
2177 char line
[1024]; /* Line from file... */
2178 char srcfile
[1024], /* Source Script/PPD file */
2179 dstfile
[1024]; /* Destination Script/PPD file */
2180 int modify
; /* Non-zero if we are modifying */
2181 char newname
[IPP_MAX_NAME
]; /* New printer name */
2182 int need_restart_job
; /* Need to restart job? */
2183 int set_device_uri
, /* Did we set the device URI? */
2184 set_port_monitor
; /* Did we set the port monitor? */
2187 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2188 con
->http
.fd
, uri
->values
[0].string
.text
);
2191 * Do we have a valid URI?
2194 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2195 sizeof(scheme
), username
, sizeof(username
), host
,
2196 sizeof(host
), &port
, resource
, sizeof(resource
));
2198 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2201 * No, return an error...
2204 send_ipp_status(con
, IPP_BAD_REQUEST
,
2205 _("The printer-uri must be of the form "
2206 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2211 * Do we have a valid printer name?
2214 if (!validate_name(resource
+ 10))
2217 * No, return an error...
2220 send_ipp_status(con
, IPP_BAD_REQUEST
,
2221 _("The printer-uri \"%s\" contains invalid characters."),
2222 uri
->values
[0].string
.text
);
2227 * See if the printer already exists; if not, create a new printer...
2230 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2233 * Printer doesn't exist; see if we have a class of the same name...
2236 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2237 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
2240 * Yes, return an error...
2243 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2244 _("A class named \"%s\" already exists!"),
2250 * No, check the default policy then add the printer...
2253 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2255 send_http_error(con
, status
, NULL
);
2259 printer
= cupsdAddPrinter(resource
+ 10);
2262 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2265 * Check the default policy, then rename the implicit printer to
2266 * "AnyPrinter" or delete it...
2269 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2271 send_http_error(con
, status
, NULL
);
2275 if (ImplicitAnyClasses
)
2277 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2278 cupsdRenamePrinter(printer
, newname
);
2281 cupsdDeletePrinter(printer
, 1);
2284 * Add the printer as a new local printer...
2287 printer
= cupsdAddPrinter(resource
+ 10);
2290 else if (printer
->type
& CUPS_PRINTER_DISCOVERED
)
2293 * Check the default policy, then rename the remote printer to
2294 * "Printer@server"...
2297 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2299 send_http_error(con
, status
, NULL
);
2303 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2305 cupsdRenamePrinter(printer
, newname
);
2308 * Add the printer as a new local printer...
2311 printer
= cupsdAddPrinter(resource
+ 10);
2314 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2317 send_http_error(con
, status
, printer
);
2324 * Look for attributes and copy them over as needed...
2327 need_restart_job
= 0;
2329 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2330 IPP_TAG_TEXT
)) != NULL
)
2331 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2333 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2334 IPP_TAG_TEXT
)) != NULL
)
2335 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2339 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2340 IPP_TAG_URI
)) != NULL
)
2343 * Do we have a valid device URI?
2346 http_uri_status_t uri_status
; /* URI separation status */
2349 need_restart_job
= 1;
2351 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2352 attr
->values
[0].string
.text
,
2353 scheme
, sizeof(scheme
),
2354 username
, sizeof(username
),
2355 host
, sizeof(host
), &port
,
2356 resource
, sizeof(resource
));
2358 if (uri_status
< HTTP_URI_OK
)
2360 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2361 attr
->values
[0].string
.text
);
2362 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2363 "add_printer: httpSeparateURI returned %d", uri_status
);
2367 if (!strcmp(scheme
, "file"))
2370 * See if the administrator has enabled file devices...
2373 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2376 * File devices are disabled and the URL is not file:/dev/null...
2379 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2380 _("File device URIs have been disabled! "
2381 "To enable, see the FileDevice directive in "
2382 "\"%s/cupsd.conf\"."),
2390 * See if the backend exists and is executable...
2393 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2394 if (access(srcfile
, X_OK
))
2397 * Could not find device in list!
2400 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2401 attr
->values
[0].string
.text
);
2406 cupsdLogMessage(CUPSD_LOG_INFO
,
2407 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2409 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2411 cupsdSanitizeURI(printer
->device_uri
, resource
,
2414 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2418 set_port_monitor
= 0;
2420 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2421 IPP_TAG_NAME
)) != NULL
)
2423 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2426 need_restart_job
= 1;
2428 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2432 for (i
= 0; i
< supported
->num_values
; i
++)
2433 if (!strcmp(supported
->values
[i
].string
.text
,
2434 attr
->values
[0].string
.text
))
2438 if (!supported
|| i
>= supported
->num_values
)
2440 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2441 attr
->values
[0].string
.text
);
2445 cupsdLogMessage(CUPSD_LOG_INFO
,
2446 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2447 printer
->name
, attr
->values
[0].string
.text
,
2448 printer
->port_monitor
? printer
->port_monitor
: "none");
2450 if (strcmp(attr
->values
[0].string
.text
, "none"))
2451 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2453 cupsdClearString(&printer
->port_monitor
);
2455 set_port_monitor
= 1;
2458 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2459 IPP_TAG_BOOLEAN
)) != NULL
)
2461 cupsdLogMessage(CUPSD_LOG_INFO
,
2462 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2463 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2465 printer
->accepting
= attr
->values
[0].boolean
;
2466 cupsdAddPrinterHistory(printer
);
2469 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2470 IPP_TAG_BOOLEAN
)) != NULL
)
2472 if (printer
->shared
&& !attr
->values
[0].boolean
)
2473 cupsdDeregisterPrinter(printer
, 1);
2475 cupsdLogMessage(CUPSD_LOG_INFO
,
2476 "Setting %s printer-is-shared to %d (was %d.)",
2477 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2479 printer
->shared
= attr
->values
[0].boolean
;
2482 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2483 IPP_TAG_ENUM
)) != NULL
)
2485 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2486 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2488 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2489 attr
->values
[0].integer
);
2493 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2494 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2496 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2497 cupsdStopPrinter(printer
, 0);
2500 need_restart_job
= 1;
2501 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2504 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2505 IPP_TAG_TEXT
)) != NULL
)
2507 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2508 sizeof(printer
->state_message
));
2509 cupsdAddPrinterHistory(printer
);
2512 set_printer_defaults(con
, printer
);
2514 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2515 IPP_TAG_KEYWORD
)) != NULL
)
2516 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2519 * See if we have all required attributes...
2522 if (!printer
->device_uri
)
2523 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2526 * See if we have an interface script or PPD file attached to the request...
2531 need_restart_job
= 1;
2533 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2535 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2538 * Yes; get the first line from it...
2542 cupsFileGets(fp
, line
, sizeof(line
));
2546 * Then see what kind of file it is...
2549 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2552 if (!strncmp(line
, "*PPD-Adobe", 10))
2555 * The new file is a PPD file, so remove any old interface script
2556 * that might be lying around...
2564 * This must be an interface script, so move the file over to the
2565 * interfaces directory and make it executable...
2568 if (copy_file(srcfile
, dstfile
))
2570 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2571 _("Unable to copy interface script - %s!"),
2576 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2577 "Copied interface script successfully!");
2578 chmod(dstfile
, 0755);
2581 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2584 if (!strncmp(line
, "*PPD-Adobe", 10))
2587 * The new file is a PPD file, so move the file over to the
2588 * ppd directory and make it readable by all...
2591 if (copy_file(srcfile
, dstfile
))
2593 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2594 _("Unable to copy PPD file - %s!"),
2599 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2600 "Copied PPD file successfully!");
2601 chmod(dstfile
, 0644);
2605 * (Re)register color profiles...
2608 apple_unregister_profiles(printer
);
2609 apple_register_profiles(printer
);
2610 #endif /* __APPLE__ */
2615 * This must be an interface script, so remove any old PPD file that
2616 * may be lying around...
2623 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2624 IPP_TAG_NAME
)) != NULL
)
2626 need_restart_job
= 1;
2628 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2631 * Raw driver, remove any existing PPD or interface script files.
2634 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2638 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2648 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2652 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2655 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2657 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2661 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2662 "Copied PPD file successfully!");
2663 chmod(dstfile
, 0644);
2667 * (Re)register color profiles...
2670 apple_unregister_profiles(printer
);
2671 apple_register_profiles(printer
);
2672 #endif /* __APPLE__ */
2677 * If we set the device URI but not the port monitor, check which port
2678 * monitor to use by default...
2681 if (set_device_uri
&& !set_port_monitor
)
2683 ppd_file_t
*ppd
; /* PPD file */
2684 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2687 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2688 sizeof(scheme
), username
, sizeof(username
), host
,
2689 sizeof(host
), &port
, resource
, sizeof(resource
));
2691 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2693 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2695 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2697 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2698 if (!strcmp(scheme
, ppdattr
->spec
))
2700 cupsdLogMessage(CUPSD_LOG_INFO
,
2701 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2702 printer
->name
, ppdattr
->value
,
2703 printer
->port_monitor
? printer
->port_monitor
2706 if (strcmp(ppdattr
->value
, "none"))
2707 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2709 cupsdClearString(&printer
->port_monitor
);
2719 * Update the printer attributes and return...
2722 cupsdSetPrinterAttrs(printer
);
2723 cupsdSaveAllPrinters();
2725 if (need_restart_job
&& printer
->job
)
2730 * Stop the current job and then restart it below...
2733 job
= (cupsd_job_t
*)printer
->job
;
2735 cupsdStopJob(job
, 1);
2737 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2738 job
->state_value
= IPP_JOB_PENDING
;
2741 if (need_restart_job
)
2744 cupsdWritePrintcap();
2748 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2749 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2752 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2753 printer
->name
, get_username(con
));
2757 cupsdAddPrinterHistory(printer
);
2759 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2760 "New printer \"%s\" added by \"%s\".", printer
->name
,
2763 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2764 printer
->name
, get_username(con
));
2767 con
->response
->request
.status
.status_code
= IPP_OK
;
2772 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2773 * based upon the printer state...
2777 add_printer_state_reasons(
2778 cupsd_client_t
*con
, /* I - Client connection */
2779 cupsd_printer_t
*p
) /* I - Printer info */
2781 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2782 "add_printer_state_reasons(%p[%d], %p[%s])",
2783 con
, con
->http
.fd
, p
, p
->name
);
2785 if (p
->num_reasons
== 0)
2786 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2787 "printer-state-reasons", NULL
,
2788 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2790 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2791 "printer-state-reasons", p
->num_reasons
, NULL
,
2792 (const char * const *)p
->reasons
);
2797 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2798 * the specified printer or class.
2802 add_queued_job_count(
2803 cupsd_client_t
*con
, /* I - Client connection */
2804 cupsd_printer_t
*p
) /* I - Printer or class */
2806 int count
; /* Number of jobs on destination */
2809 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2810 con
, con
->http
.fd
, p
, p
->name
);
2812 count
= cupsdGetPrinterJobCount(p
->name
);
2814 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2815 "queued-job-count", count
);
2821 * 'apple_init_profile()' - Initialize a color profile.
2826 ppd_file_t
*ppd
, /* I - PPD file */
2827 cups_array_t
*languages
, /* I - Languages in the PPD file */
2828 CMDeviceProfileInfo
*profile
, /* I - Profile record */
2829 unsigned id
, /* I - Profile ID */
2830 const char *name
, /* I - Profile name */
2831 const char *text
, /* I - Profile UI text */
2832 const char *iccfile
) /* I - ICC filename */
2834 char url
[1024]; /* URL for profile filename */
2835 CFMutableDictionaryRef dict
; /* Dictionary for name */
2836 char *language
; /* Current language */
2837 ppd_attr_t
*attr
; /* Profile attribute */
2841 * Build the profile name dictionary...
2844 dict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
2845 &kCFTypeDictionaryKeyCallBacks
,
2846 &kCFTypeDictionaryValueCallBacks
);
2848 CFDictionarySetValue(dict
, CFSTR("en"),
2849 CFStringCreateWithCString(kCFAllocatorDefault
,
2850 text
, kCFStringEncodingUTF8
));
2852 if (iccfile
&& languages
)
2855 * Find localized names for the color profiles...
2858 cupsArraySave(ppd
->sorted_attrs
);
2860 for (language
= (char *)cupsArrayFirst(languages
);
2862 language
= (char *)cupsArrayNext(languages
))
2864 if ((attr
= _ppdLocalizedAttr(ppd
, "cupsICCProfile", name
,
2865 language
)) != NULL
&& attr
->text
[0])
2866 CFDictionarySetValue(dict
,
2867 CFStringCreateWithCString(kCFAllocatorDefault
,
2869 kCFStringEncodingUTF8
),
2870 CFStringCreateWithCString(kCFAllocatorDefault
,
2872 kCFStringEncodingUTF8
));
2875 cupsArrayRestore(ppd
->sorted_attrs
);
2879 * Fill in the profile data...
2883 httpAssembleURI(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "file", NULL
, "", 0,
2886 profile
->dataVersion
= cmDeviceProfileInfoVersion1
;
2887 profile
->profileID
= id
;
2888 profile
->profileLoc
.locType
= iccfile
? cmPathBasedProfile
: cmNoProfileBase
;
2889 profile
->profileName
= dict
;
2892 strlcpy(profile
->profileLoc
.u
.pathLoc
.path
, iccfile
,
2893 sizeof(profile
->profileLoc
.u
.pathLoc
.path
));
2898 * 'apple_register_profiles()' - Register color profiles for a printer.
2902 apple_register_profiles(
2903 cupsd_printer_t
*p
) /* I - Printer */
2905 int i
; /* Looping var */
2906 char ppdfile
[1024], /* PPD filename */
2907 iccfile
[1024]; /* ICC filename */
2908 ppd_file_t
*ppd
; /* PPD file */
2909 ppd_attr_t
*attr
; /* cupsICCProfile attributes */
2910 ppd_option_t
*cm_option
; /* Color model option */
2911 ppd_choice_t
*cm_choice
; /* Color model choice */
2912 int num_profiles
; /* Number of profiles */
2913 CMError error
; /* Last error */
2914 unsigned id
; /* Printer device ID */
2915 CFMutableDictionaryRef name
; /* Printer device name dictionary */
2916 CMDeviceScope scope
= /* Scope of the registration */
2918 kCFPreferencesAnyUser
,
2919 kCFPreferencesCurrentHost
2921 CMDeviceProfileArrayPtr profiles
; /* Profiles */
2922 CMDeviceProfileInfo
*profile
; /* Current profile */
2923 cups_array_t
*languages
; /* Languages array */
2927 * Try opening the PPD file for this printer...
2930 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
2931 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
2935 * See if we have any profiles...
2938 for (num_profiles
= 0, attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
2940 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
2941 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
2943 if (attr
->value
[0] != '/')
2944 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
2947 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
2949 if (access(iccfile
, 0))
2956 * If we have profiles, add them...
2959 if (num_profiles
> 0)
2962 * Build the array of profiles...
2964 * Note: This calloc actually requests slightly more memory than needed.
2967 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
2969 cupsdLogMessage(CUPSD_LOG_ERROR
,
2970 "Unable to allocate memory for %d profiles!",
2976 profiles
->profileCount
= num_profiles
;
2977 languages
= _ppdGetLanguages(ppd
);
2979 for (profile
= profiles
->profiles
,
2980 attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
2982 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
2983 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
2986 * Add this profile...
2989 if (attr
->value
[0] != '/')
2990 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
2993 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
2995 if (access(iccfile
, 0))
2998 apple_init_profile(ppd
, languages
, profile
, _ppdHashName(attr
->spec
),
2999 attr
->spec
, attr
->text
[0] ? attr
->text
: attr
->spec
,
3005 _ppdFreeLanguages(languages
);
3007 else if ((cm_option
= ppdFindOption(ppd
, "ColorModel")) != NULL
)
3010 * Extract standard grayscale, RGB, or CMYK profiles...
3013 for (num_profiles
= 0, i
= cm_option
->num_choices
,
3014 cm_choice
= cm_option
->choices
;
3017 if (!strcmp(cm_choice
->choice
, "Gray") ||
3018 !strcmp(cm_choice
->choice
, "RGB") ||
3019 !strcmp(cm_choice
->choice
, "CMYK"))
3023 * Build the array of profiles...
3025 * Note: This calloc actually requests slightly more memory than needed.
3028 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3030 cupsdLogMessage(CUPSD_LOG_ERROR
,
3031 "Unable to allocate memory for %d profiles!",
3037 profiles
->profileCount
= num_profiles
;
3039 for (profile
= profiles
->profiles
, i
= cm_option
->num_choices
,
3040 cm_choice
= cm_option
->choices
;
3043 if (!strcmp(cm_choice
->choice
, "Gray"))
3045 apple_init_profile(ppd
, NULL
, profile
, _ppdHashName("Gray.."),
3046 "Gray", "Gray", NULL
);
3049 else if (!strcmp(cm_choice
->choice
, "RGB"))
3051 apple_init_profile(ppd
, NULL
, profile
, _ppdHashName("RGB.."),
3052 "RGB", "RGB", NULL
);
3055 else if (!strcmp(cm_choice
->choice
, "CMYK"))
3057 apple_init_profile(ppd
, NULL
, profile
, _ppdHashName("CMYK.."),
3058 "CMYK", "CMYK", NULL
);
3065 * Use the default colorspace...
3070 if ((profiles
= calloc(1, sizeof(CMDeviceProfileArray
))) == NULL
)
3072 cupsdLogMessage(CUPSD_LOG_ERROR
,
3073 "Unable to allocate memory for %d profiles!",
3079 profiles
->profileCount
= 1;
3081 switch (ppd
->colorspace
)
3084 apple_init_profile(ppd
, NULL
, profiles
->profiles
,
3085 _ppdHashName("Gray.."), "Gray", "Gray", NULL
);
3088 apple_init_profile(ppd
, NULL
, profiles
->profiles
,
3089 _ppdHashName("RGB.."), "RGB", "RGB", NULL
);
3092 apple_init_profile(ppd
, NULL
, profiles
->profiles
,
3093 _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL
);
3103 if (num_profiles
> 0)
3106 * Get the device ID hash and pathelogical name dictionary.
3109 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\"",
3112 id
= _ppdHashName(p
->name
);
3113 name
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3114 &kCFTypeDictionaryKeyCallBacks
,
3115 &kCFTypeDictionaryValueCallBacks
);
3117 CFDictionarySetValue(name
, CFSTR("en"),
3118 CFStringCreateWithCString(kCFAllocatorDefault
, p
->name
,
3119 kCFStringEncodingUTF8
));
3122 * Register the device with ColorSync...
3125 error
= CMRegisterColorDevice(cmPrinterDeviceClass
, id
, name
, &scope
);
3128 * Register the profiles...
3132 error
= CMSetDeviceFactoryProfiles(cmPrinterDeviceClass
, id
,
3133 profiles
->profiles
[0].profileID
,
3141 cupsdLogMessage(CUPSD_LOG_ERROR
,
3142 "Unable to register ICC color profiles for \"%s\" - %d",
3143 p
->name
, (int)error
);
3145 for (profile
= profiles
->profiles
;
3147 profile
++, num_profiles
--)
3148 CFRelease(profile
->profileName
);
3159 * 'apple_unregister_profiles()' - Remove color profiles for the specified
3164 apple_unregister_profiles(
3165 cupsd_printer_t
*p
) /* I - Printer */
3167 CMUnregisterColorDevice(cmPrinterDeviceClass
, _ppdHashName(p
->name
));
3169 #endif /* __APPLE__ */
3172 * 'apply_printer_defaults()' - Apply printer default options to a job.
3176 apply_printer_defaults(
3177 cupsd_printer_t
*printer
, /* I - Printer */
3178 cupsd_job_t
*job
) /* I - Job */
3180 int i
, /* Looping var */
3181 num_options
; /* Number of default options */
3182 cups_option_t
*options
, /* Default options */
3183 *option
; /* Current option */
3187 * Collect all of the default options and add the missing ones to the
3191 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
3194 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
3196 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
3201 * Encode these options as attributes in the job object...
3204 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
3205 cupsFreeOptions(num_options
, options
);
3210 * 'authenticate_job()' - Set job authentication info.
3214 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
3215 ipp_attribute_t
*uri
) /* I - Job URI */
3217 ipp_attribute_t
*attr
, /* job-id attribute */
3218 *auth_info
; /* auth-info attribute */
3219 int jobid
; /* Job ID */
3220 cupsd_job_t
*job
; /* Current job */
3221 char method
[HTTP_MAX_URI
],
3222 /* Method portion of URI */
3223 username
[HTTP_MAX_URI
],
3224 /* Username portion of URI */
3226 /* Host portion of URI */
3227 resource
[HTTP_MAX_URI
];
3228 /* Resource portion of URI */
3229 int port
; /* Port portion of URI */
3232 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
3233 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
3236 * Start with "everything is OK" status...
3239 con
->response
->request
.status
.status_code
= IPP_OK
;
3242 * See if we have a job URI or a printer URI...
3245 if (!strcmp(uri
->name
, "printer-uri"))
3248 * Got a printer URI; see if we also have a job-id attribute...
3251 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3252 IPP_TAG_INTEGER
)) == NULL
)
3254 send_ipp_status(con
, IPP_BAD_REQUEST
,
3255 _("Got a printer-uri attribute but no job-id!"));
3259 jobid
= attr
->values
[0].integer
;
3264 * Got a job URI; parse it to get the job ID...
3267 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
3268 sizeof(method
), username
, sizeof(username
), host
,
3269 sizeof(host
), &port
, resource
, sizeof(resource
));
3271 if (strncmp(resource
, "/jobs/", 6))
3277 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
3278 uri
->values
[0].string
.text
);
3282 jobid
= atoi(resource
+ 6);
3286 * See if the job exists...
3289 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3292 * Nope - return a "not found" error...
3295 send_ipp_status(con
, IPP_NOT_FOUND
,
3296 _("Job #%d does not exist!"), jobid
);
3301 * See if the job has been completed...
3304 if (job
->state_value
!= IPP_JOB_HELD
)
3307 * Return a "not-possible" error...
3310 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3311 _("Job #%d is not held for authentication!"),
3317 * See if we have already authenticated...
3320 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3322 if (!con
->username
[0] && !auth_info
)
3324 cupsd_printer_t
*printer
; /* Job destination */
3328 * No auth data. If we need to authenticate via Kerberos, send a
3329 * HTTP auth challenge, otherwise just return an IPP error...
3332 printer
= cupsdFindDest(job
->dest
);
3334 if (printer
&& printer
->num_auth_info_required
> 0 &&
3335 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3336 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3338 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3339 _("No authentication information provided!"));
3344 * See if the job is owned by the requesting user...
3347 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3349 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3354 * Save the authentication information for this job...
3357 save_auth_info(con
, job
, auth_info
);
3360 * Reset the job-hold-until value to "no-hold"...
3363 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3364 IPP_TAG_KEYWORD
)) == NULL
)
3365 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3369 attr
->value_tag
= IPP_TAG_KEYWORD
;
3370 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
3374 * Release the job and return...
3377 cupsdReleaseJob(job
);
3379 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Authenticated by \"%s\".", jobid
,
3385 * 'cancel_all_jobs()' - Cancel all print jobs.
3389 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3390 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3392 http_status_t status
; /* Policy status */
3393 cups_ptype_t dtype
; /* Destination type */
3394 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3395 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3396 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3397 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3398 int port
; /* Port portion of URI */
3399 ipp_attribute_t
*attr
; /* Attribute in request */
3400 const char *username
; /* Username */
3401 int purge
; /* Purge? */
3402 cupsd_printer_t
*printer
; /* Printer */
3405 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3406 con
->http
.fd
, uri
->values
[0].string
.text
);
3409 * See if we have a printer URI...
3412 if (strcmp(uri
->name
, "printer-uri"))
3414 send_ipp_status(con
, IPP_BAD_REQUEST
,
3415 _("The printer-uri attribute is required!"));
3420 * Get the username (if any) for the jobs we want to cancel (only if
3421 * "my-jobs" is specified...
3424 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3425 IPP_TAG_BOOLEAN
)) != NULL
&&
3426 attr
->values
[0].boolean
)
3428 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3429 IPP_TAG_NAME
)) != NULL
)
3430 username
= attr
->values
[0].string
.text
;
3433 send_ipp_status(con
, IPP_BAD_REQUEST
,
3434 _("Missing requesting-user-name attribute!"));
3442 * Look for the "purge-jobs" attribute...
3445 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3446 IPP_TAG_BOOLEAN
)) != NULL
)
3447 purge
= attr
->values
[0].boolean
;
3452 * And if the destination is valid...
3455 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3461 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3462 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3463 hostname
, sizeof(hostname
), &port
,
3464 resource
, sizeof(resource
));
3466 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3467 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3469 send_ipp_status(con
, IPP_NOT_FOUND
,
3470 _("The printer or class was not found."));
3478 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3480 send_http_error(con
, status
, NULL
);
3485 * Cancel all jobs on all printers...
3488 cupsdCancelJobs(NULL
, username
, purge
);
3490 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3491 purge
? "purged" : "canceled", get_username(con
));
3499 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3502 send_http_error(con
, status
, printer
);
3507 * Cancel all of the jobs on the named printer...
3510 cupsdCancelJobs(printer
->name
, username
, purge
);
3512 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3513 printer
->name
, purge
? "purged" : "canceled",
3517 con
->response
->request
.status
.status_code
= IPP_OK
;
3522 * 'cancel_job()' - Cancel a print job.
3526 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3527 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3529 ipp_attribute_t
*attr
; /* Current attribute */
3530 int jobid
; /* Job ID */
3531 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3532 username
[HTTP_MAX_URI
], /* Username portion of URI */
3533 host
[HTTP_MAX_URI
], /* Host portion of URI */
3534 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3535 int port
; /* Port portion of URI */
3536 cupsd_job_t
*job
; /* Job information */
3537 cups_ptype_t dtype
; /* Destination type (printer/class) */
3538 cupsd_printer_t
*printer
; /* Printer data */
3539 int purge
; /* Purge the job? */
3542 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3543 con
->http
.fd
, uri
->values
[0].string
.text
);
3546 * See if we have a job URI or a printer URI...
3549 if (!strcmp(uri
->name
, "printer-uri"))
3552 * Got a printer URI; see if we also have a job-id attribute...
3555 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3556 IPP_TAG_INTEGER
)) == NULL
)
3558 send_ipp_status(con
, IPP_BAD_REQUEST
,
3559 _("Got a printer-uri attribute but no job-id!"));
3563 if ((jobid
= attr
->values
[0].integer
) == 0)
3566 * Find the current job on the specified printer...
3569 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3575 send_ipp_status(con
, IPP_NOT_FOUND
,
3576 _("The printer or class was not found."));
3581 * See if the printer is currently printing a job...
3585 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3589 * No, see if there are any pending jobs...
3592 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3594 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3595 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3596 !strcasecmp(job
->dest
, printer
->name
))
3603 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3605 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3606 if (job
->state_value
== IPP_JOB_STOPPED
&&
3607 !strcasecmp(job
->dest
, printer
->name
))
3614 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3625 * Got a job URI; parse it to get the job ID...
3628 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3629 sizeof(scheme
), username
, sizeof(username
), host
,
3630 sizeof(host
), &port
, resource
, sizeof(resource
));
3632 if (strncmp(resource
, "/jobs/", 6))
3638 send_ipp_status(con
, IPP_BAD_REQUEST
,
3639 _("Bad job-uri attribute \"%s\"!"),
3640 uri
->values
[0].string
.text
);
3644 jobid
= atoi(resource
+ 6);
3648 * Look for the "purge-job" attribute...
3651 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3652 IPP_TAG_BOOLEAN
)) != NULL
)
3653 purge
= attr
->values
[0].boolean
;
3658 * See if the job exists...
3661 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3664 * Nope - return a "not found" error...
3667 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3672 * See if the job is owned by the requesting user...
3675 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3677 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3682 * See if the job is already completed, canceled, or aborted; if so,
3683 * we can't cancel...
3686 if (job
->state_value
>= IPP_JOB_CANCELED
&& !purge
)
3688 switch (job
->state_value
)
3690 case IPP_JOB_CANCELED
:
3691 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3692 _("Job #%d is already canceled - can\'t cancel."),
3696 case IPP_JOB_ABORTED
:
3697 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3698 _("Job #%d is already aborted - can\'t cancel."),
3703 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3704 _("Job #%d is already completed - can\'t cancel."),
3713 * Cancel the job and return...
3716 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
3720 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
3723 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3726 con
->response
->request
.status
.status_code
= IPP_OK
;
3731 * 'cancel_subscription()' - Cancel a subscription.
3735 cancel_subscription(
3736 cupsd_client_t
*con
, /* I - Client connection */
3737 int sub_id
) /* I - Subscription ID */
3739 http_status_t status
; /* Policy status */
3740 cupsd_subscription_t
*sub
; /* Subscription */
3743 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3744 "cancel_subscription(con=%p[%d], sub_id=%d)",
3745 con
, con
->http
.fd
, sub_id
);
3748 * Is the subscription ID valid?
3751 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3754 * Bad subscription ID...
3757 send_ipp_status(con
, IPP_NOT_FOUND
,
3758 _("notify-subscription-id %d no good!"), sub_id
);
3766 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3768 con
, sub
->owner
)) != HTTP_OK
)
3770 send_http_error(con
, status
, sub
->dest
);
3775 * Cancel the subscription...
3778 cupsdDeleteSubscription(sub
, 1);
3780 con
->response
->request
.status
.status_code
= IPP_OK
;
3785 * 'check_quotas()' - Check quotas for a printer and user.
3788 static int /* O - 1 if OK, 0 if not */
3789 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3790 cupsd_printer_t
*p
) /* I - Printer or class */
3792 int i
; /* Looping var */
3793 char username
[33]; /* Username */
3794 cupsd_quota_t
*q
; /* Quota data */
3795 #ifdef HAVE_MBR_UID_TO_UUID
3797 * Use Apple membership APIs which require that all names represent
3798 * valid user account or group records accessible by the server.
3801 uuid_t usr_uuid
; /* UUID for job requesting user */
3802 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3803 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3804 int mbr_err
; /* Error from membership function */
3805 int is_member
; /* Is this user a member? */
3808 * Use standard POSIX APIs for checking users and groups...
3811 struct passwd
*pw
; /* User password data */
3812 #endif /* HAVE_MBR_UID_TO_UUID */
3815 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3816 con
, con
->http
.fd
, p
, p
->name
);
3819 * Figure out who is printing...
3822 strlcpy(username
, get_username(con
), sizeof(username
));
3825 * Check global active job limits for printers and users...
3828 if (MaxJobsPerPrinter
)
3831 * Check if there are too many pending jobs on this printer...
3834 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3836 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3845 * Check if there are too many pending jobs for this user...
3848 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3850 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3857 * Check against users...
3860 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3865 #ifdef HAVE_MBR_UID_TO_UUID
3867 * Get UUID for job requesting user...
3870 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3876 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3877 "check_quotas: UUID lookup failed for user \"%s\"",
3879 cupsdLogMessage(CUPSD_LOG_INFO
,
3880 "Denying user \"%s\" access to printer \"%s\" "
3881 "(unknown user)...",
3887 * Get UID and GID of requesting user...
3890 pw
= getpwnam(username
);
3892 #endif /* HAVE_MBR_UID_TO_UUID */
3894 for (i
= 0; i
< p
->num_users
; i
++)
3895 if (p
->users
[i
][0] == '@')
3898 * Check group membership...
3901 #ifdef HAVE_MBR_UID_TO_UUID
3902 if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
3906 * Invalid ACL entries are ignored for matching; just record a
3907 * warning in the log...
3910 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3911 "check_quotas: UUID lookup failed for ACL entry "
3912 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3913 cupsdLogMessage(CUPSD_LOG_WARN
,
3914 "Access control entry \"%s\" not a valid group name; "
3915 "entry ignored", p
->users
[i
]);
3919 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3923 * At this point, there should be no errors, but check anyways...
3926 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3927 "check_quotas: group \"%s\" membership check "
3928 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
3933 * Stop if we found a match...
3940 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3942 #endif /* HAVE_MBR_UID_TO_UUID */
3944 #ifdef HAVE_MBR_UID_TO_UUID
3947 if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
3951 * Invalid ACL entries are ignored for matching; just record a
3952 * warning in the log...
3955 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3956 "check_quotas: UUID lookup failed for ACL entry "
3957 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3958 cupsdLogMessage(CUPSD_LOG_WARN
,
3959 "Access control entry \"%s\" not a valid user name; "
3960 "entry ignored", p
->users
[i
]);
3964 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
3967 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3968 "check_quotas: User \"%s\" identity check failed "
3969 "(err=%d)", p
->users
[i
], mbr_err
);
3978 else if (!strcasecmp(username
, p
->users
[i
]))
3980 #endif /* HAVE_MBR_UID_TO_UUID */
3982 if ((i
< p
->num_users
) == p
->deny_users
)
3984 cupsdLogMessage(CUPSD_LOG_INFO
,
3985 "Denying user \"%s\" access to printer \"%s\"...",
3996 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
3999 * TODO: Define these special page count values as constants!
4002 if (q
->page_count
== -4) /* special case: unlimited user */
4004 cupsdLogMessage(CUPSD_LOG_INFO
,
4005 "User \"%s\" request approved for printer %s (%s): "
4007 username
, p
->name
, p
->info
);
4008 q
->page_count
= 0; /* allow user to print */
4011 else if (q
->page_count
== -3) /* quota exceeded */
4013 cupsdLogMessage(CUPSD_LOG_INFO
,
4014 "User \"%s\" request denied for printer %s (%s): "
4015 "quota limit exceeded.",
4016 username
, p
->name
, p
->info
);
4017 q
->page_count
= 2; /* force quota exceeded failure */
4020 else if (q
->page_count
== -2) /* quota disabled for user */
4022 cupsdLogMessage(CUPSD_LOG_INFO
,
4023 "User \"%s\" request denied for printer %s (%s): "
4024 "printing disabled for user.",
4025 username
, p
->name
, p
->info
);
4026 q
->page_count
= 2; /* force quota exceeded failure */
4029 else if (q
->page_count
== -1) /* quota access error */
4031 cupsdLogMessage(CUPSD_LOG_INFO
,
4032 "User \"%s\" request denied for printer %s (%s): "
4033 "unable to determine quota limit.",
4034 username
, p
->name
, p
->info
);
4035 q
->page_count
= 2; /* force quota exceeded failure */
4038 else if (q
->page_count
< 0) /* user not found or other error */
4040 cupsdLogMessage(CUPSD_LOG_INFO
,
4041 "User \"%s\" request denied for printer %s (%s): "
4042 "user disabled / missing quota.",
4043 username
, p
->name
, p
->info
);
4044 q
->page_count
= 2; /* force quota exceeded failure */
4047 else /* page within user limits */
4049 q
->page_count
= 0; /* allow user to print */
4054 #endif /* __APPLE__ */
4055 if (p
->k_limit
|| p
->page_limit
)
4057 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
4059 cupsdLogMessage(CUPSD_LOG_ERROR
,
4060 "Unable to allocate quota data for user \"%s\"!",
4065 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
4066 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
4068 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
4075 * If we have gotten this far, we're done!
4083 * 'copy_attribute()' - Copy a single attribute.
4086 static ipp_attribute_t
* /* O - New attribute */
4088 ipp_t
*to
, /* O - Destination request/response */
4089 ipp_attribute_t
*attr
, /* I - Attribute to copy */
4090 int quickcopy
) /* I - Do a quick copy? */
4092 int i
; /* Looping var */
4093 ipp_attribute_t
*toattr
; /* Destination attribute */
4096 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4097 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
4098 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
4101 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
4104 toattr
= ippAddSeparator(to
);
4107 case IPP_TAG_INTEGER
:
4109 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4110 attr
->name
, attr
->num_values
, NULL
);
4112 for (i
= 0; i
< attr
->num_values
; i
++)
4113 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
4116 case IPP_TAG_BOOLEAN
:
4117 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
4118 attr
->num_values
, NULL
);
4120 for (i
= 0; i
< attr
->num_values
; i
++)
4121 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
4124 case IPP_TAG_STRING
:
4127 case IPP_TAG_KEYWORD
:
4129 case IPP_TAG_URISCHEME
:
4130 case IPP_TAG_CHARSET
:
4131 case IPP_TAG_LANGUAGE
:
4132 case IPP_TAG_MIMETYPE
:
4133 toattr
= ippAddStrings(to
, attr
->group_tag
,
4134 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4135 attr
->name
, attr
->num_values
, NULL
, NULL
);
4139 for (i
= 0; i
< attr
->num_values
; i
++)
4140 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4144 for (i
= 0; i
< attr
->num_values
; i
++)
4145 toattr
->values
[i
].string
.text
=
4146 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4151 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
4152 attr
->values
[0].date
);
4155 case IPP_TAG_RESOLUTION
:
4156 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
4157 attr
->num_values
, IPP_RES_PER_INCH
,
4160 for (i
= 0; i
< attr
->num_values
; i
++)
4162 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
4163 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
4164 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
4168 case IPP_TAG_RANGE
:
4169 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
4170 attr
->num_values
, NULL
, NULL
);
4172 for (i
= 0; i
< attr
->num_values
; i
++)
4174 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
4175 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
4179 case IPP_TAG_TEXTLANG
:
4180 case IPP_TAG_NAMELANG
:
4181 toattr
= ippAddStrings(to
, attr
->group_tag
,
4182 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4183 attr
->name
, attr
->num_values
, NULL
, NULL
);
4187 for (i
= 0; i
< attr
->num_values
; i
++)
4189 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
4190 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4195 for (i
= 0; i
< attr
->num_values
; i
++)
4198 toattr
->values
[i
].string
.charset
=
4199 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
4201 toattr
->values
[i
].string
.charset
=
4202 toattr
->values
[0].string
.charset
;
4204 toattr
->values
[i
].string
.text
=
4205 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4210 case IPP_TAG_BEGIN_COLLECTION
:
4211 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
4212 attr
->num_values
, NULL
);
4214 for (i
= 0; i
< attr
->num_values
; i
++)
4216 toattr
->values
[i
].collection
= ippNew();
4217 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
4218 NULL
, IPP_TAG_ZERO
, 0);
4223 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4224 attr
->name
, attr
->num_values
, NULL
);
4226 for (i
= 0; i
< attr
->num_values
; i
++)
4228 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
4230 if (toattr
->values
[i
].unknown
.length
> 0)
4232 if ((toattr
->values
[i
].unknown
.data
=
4233 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
4234 toattr
->values
[i
].unknown
.length
= 0;
4236 memcpy(toattr
->values
[i
].unknown
.data
,
4237 attr
->values
[i
].unknown
.data
,
4238 toattr
->values
[i
].unknown
.length
);
4241 break; /* anti-compiler-warning-code */
4249 * 'copy_attrs()' - Copy attributes from one request to another.
4253 copy_attrs(ipp_t
*to
, /* I - Destination request */
4254 ipp_t
*from
, /* I - Source request */
4255 cups_array_t
*ra
, /* I - Requested attributes */
4256 ipp_tag_t group
, /* I - Group to copy */
4257 int quickcopy
) /* I - Do a quick copy? */
4259 ipp_attribute_t
*fromattr
; /* Source attribute */
4262 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4263 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4264 to
, from
, ra
, group
, quickcopy
);
4269 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4272 * Filter attributes as needed...
4275 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4276 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4279 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4280 copy_attribute(to
, fromattr
, quickcopy
);
4286 * 'copy_banner()' - Copy a banner file to the requests directory for the
4290 static int /* O - Size of banner file in kbytes */
4291 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4292 cupsd_job_t
*job
, /* I - Job information */
4293 const char *name
) /* I - Name of banner */
4295 int i
; /* Looping var */
4296 int kbytes
; /* Size of banner file in kbytes */
4297 char filename
[1024]; /* Job filename */
4298 cupsd_banner_t
*banner
; /* Pointer to banner */
4299 cups_file_t
*in
; /* Input file */
4300 cups_file_t
*out
; /* Output file */
4301 int ch
; /* Character from file */
4302 char attrname
[255], /* Name of attribute */
4303 *s
; /* Pointer into name */
4304 ipp_attribute_t
*attr
; /* Attribute */
4307 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
4308 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
4309 name
? name
: "(null)");
4312 * Find the banner; return if not found or "none"...
4315 if (!name
|| !strcmp(name
, "none") ||
4316 (banner
= cupsdFindBanner(name
)) == NULL
)
4320 * Open the banner and job files...
4323 if (add_file(con
, job
, banner
->filetype
, 0))
4326 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4328 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4330 cupsdLogMessage(CUPSD_LOG_ERROR
,
4331 "copy_banner: Unable to create banner job file %s - %s",
4332 filename
, strerror(errno
));
4337 fchmod(cupsFileNumber(out
), 0640);
4338 fchown(cupsFileNumber(out
), RunUser
, Group
);
4341 * Try the localized banner file under the subdirectory...
4344 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4346 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4349 * Convert ll-cc to ll_CC...
4353 attrname
[3] = toupper(attrname
[3] & 255);
4354 attrname
[4] = toupper(attrname
[4] & 255);
4357 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4360 if (access(filename
, 0) && strlen(attrname
) > 2)
4363 * Wasn't able to find "ll_CC" locale file; try the non-national
4364 * localization banner directory.
4369 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4373 if (access(filename
, 0))
4376 * Use the non-localized banner file.
4379 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4382 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4386 cupsdLogMessage(CUPSD_LOG_ERROR
,
4387 "copy_banner: Unable to open banner template file %s - %s",
4388 filename
, strerror(errno
));
4394 * Parse the file to the end...
4397 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4401 * Get an attribute name...
4404 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4405 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4407 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4417 * Ignore { followed by stuff that is not an attribute name...
4420 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4425 * See if it is defined...
4428 if (attrname
[0] == '?')
4433 if (!strcmp(s
, "printer-name"))
4435 cupsFilePuts(out
, job
->dest
);
4438 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4441 * See if we have a leading question mark...
4444 if (attrname
[0] != '?')
4447 * Nope, write to file as-is; probably a PostScript procedure...
4450 cupsFilePrintf(out
, "{%s}", attrname
);
4457 * Output value(s)...
4460 for (i
= 0; i
< attr
->num_values
; i
++)
4463 cupsFilePutChar(out
, ',');
4465 switch (attr
->value_tag
)
4467 case IPP_TAG_INTEGER
:
4469 if (!strncmp(s
, "time-at-", 8))
4470 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
4472 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4475 case IPP_TAG_BOOLEAN
:
4476 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4479 case IPP_TAG_NOVALUE
:
4480 cupsFilePuts(out
, "novalue");
4483 case IPP_TAG_RANGE
:
4484 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4485 attr
->values
[i
].range
.upper
);
4488 case IPP_TAG_RESOLUTION
:
4489 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4490 attr
->values
[i
].resolution
.yres
,
4491 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4496 case IPP_TAG_STRING
:
4499 case IPP_TAG_KEYWORD
:
4500 case IPP_TAG_CHARSET
:
4501 case IPP_TAG_LANGUAGE
:
4502 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4505 * Need to quote strings for PS banners...
4510 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4512 if (*p
== '(' || *p
== ')' || *p
== '\\')
4514 cupsFilePutChar(out
, '\\');
4515 cupsFilePutChar(out
, *p
);
4517 else if (*p
< 32 || *p
> 126)
4518 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4520 cupsFilePutChar(out
, *p
);
4524 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4528 break; /* anti-compiler-warning-code */
4532 else if (ch
== '\\') /* Quoted char */
4534 ch
= cupsFileGetChar(in
);
4536 if (ch
!= '{') /* Only do special handling for \{ */
4537 cupsFilePutChar(out
, '\\');
4539 cupsFilePutChar(out
, ch
);
4542 cupsFilePutChar(out
, ch
);
4546 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4548 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4549 IPP_TAG_INTEGER
)) != NULL
)
4550 attr
->values
[0].integer
+= kbytes
;
4559 * 'copy_file()' - Copy a PPD file or interface script...
4562 static int /* O - 0 = success, -1 = error */
4563 copy_file(const char *from
, /* I - Source file */
4564 const char *to
) /* I - Destination file */
4566 cups_file_t
*src
, /* Source file */
4567 *dst
; /* Destination file */
4568 int bytes
; /* Bytes to read/write */
4569 char buffer
[2048]; /* Copy buffer */
4572 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4575 * Open the source and destination file for a copy...
4578 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4581 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4588 * Copy the source file to the destination...
4591 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4592 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
4600 * Close both files and return...
4605 return (cupsFileClose(dst
));
4610 * 'copy_model()' - Copy a PPD model file, substituting default values
4614 static int /* O - 0 = success, -1 = error */
4615 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4616 const char *from
, /* I - Source file */
4617 const char *to
) /* I - Destination file */
4619 fd_set input
; /* select() input set */
4620 struct timeval timeout
; /* select() timeout */
4621 int maxfd
; /* Max file descriptor for select() */
4622 char tempfile
[1024]; /* Temporary PPD file */
4623 int tempfd
; /* Temporary PPD file descriptor */
4624 int temppid
; /* Process ID of cups-driverd */
4625 int temppipe
[2]; /* Temporary pipes */
4626 char *argv
[4], /* Command-line arguments */
4627 *envp
[MAX_ENV
]; /* Environment */
4628 cups_file_t
*src
, /* Source file */
4629 *dst
; /* Destination file */
4630 ppd_file_t
*ppd
; /* PPD file */
4631 int bytes
, /* Bytes from pipe */
4632 total
; /* Total bytes from pipe */
4633 char buffer
[2048]; /* Copy buffer */
4634 int i
; /* Looping var */
4635 char option
[PPD_MAX_NAME
], /* Option name */
4636 choice
[PPD_MAX_NAME
]; /* Choice name */
4637 int num_defaults
; /* Number of default options */
4638 cups_option_t
*defaults
; /* Default options */
4639 char cups_protocol
[PPD_MAX_LINE
];
4640 /* cupsProtocol attribute */
4641 int have_letter
, /* Have Letter size */
4642 have_a4
; /* Have A4 size */
4643 #ifdef HAVE_LIBPAPER
4644 char *paper_result
; /* Paper size name from libpaper */
4645 char system_paper
[64]; /* Paper size name buffer */
4646 #endif /* HAVE_LIBPAPER */
4649 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4650 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4654 * Run cups-driverd to get the PPD file...
4657 argv
[0] = "cups-driverd";
4659 argv
[2] = (char *)from
;
4662 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4664 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4665 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
4666 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4670 cupsdOpenPipe(temppipe
);
4672 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4673 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4675 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4676 -1, -1, 0, DefaultProfile
, &temppid
))
4687 * Wait up to 30 seconds for the PPD file to be copied...
4692 if (temppipe
[0] > CGIPipes
[0])
4693 maxfd
= temppipe
[0] + 1;
4695 maxfd
= CGIPipes
[0] + 1;
4700 * See if we have data ready...
4706 FD_SET(temppipe
[0], &input
);
4707 FD_SET(CGIPipes
[0], &input
);
4709 timeout
.tv_sec
= 30;
4710 timeout
.tv_usec
= 0;
4712 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4722 * We have timed out...
4728 if (FD_ISSET(temppipe
[0], &input
))
4731 * Read the PPD file from the pipe, and write it to the PPD file.
4734 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4736 if (write(tempfd
, buffer
, bytes
) < bytes
)
4745 if (FD_ISSET(CGIPipes
[0], &input
))
4755 * No data from cups-deviced...
4758 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
4764 * Read the source file and see what page sizes are supported...
4767 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
4773 have_letter
= ppdPageSize(ppd
, "Letter") != NULL
;
4774 have_a4
= ppdPageSize(ppd
, "A4") != NULL
;
4777 * Open the destination (if possible) and set the default options...
4782 cups_protocol
[0] = '\0';
4784 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4787 * Read all of the default lines from the old PPD...
4790 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4791 if (!strncmp(buffer
, "*Default", 8))
4794 * Add the default option...
4797 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4798 choice
, sizeof(choice
)))
4800 ppd_option_t
*ppdo
; /* PPD option */
4804 * Only add the default if the default hasn't already been
4805 * set and the choice exists in the new PPD...
4808 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4809 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4810 ppdFindChoice(ppdo
, choice
))
4811 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4815 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4816 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4820 #ifdef HAVE_LIBPAPER
4821 else if ((paper_result
= systempapername()) != NULL
)
4824 * Set the default media sizes from the systemwide default...
4827 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
4828 system_paper
[0] = toupper(system_paper
[0] & 255);
4830 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
4831 (!strcmp(system_paper
, "A4") && have_a4
))
4833 num_defaults
= cupsAddOption("PageSize", system_paper
,
4834 num_defaults
, &defaults
);
4835 num_defaults
= cupsAddOption("PageRegion", system_paper
,
4836 num_defaults
, &defaults
);
4837 num_defaults
= cupsAddOption("PaperDimension", system_paper
,
4838 num_defaults
, &defaults
);
4839 num_defaults
= cupsAddOption("ImageableArea", system_paper
,
4840 num_defaults
, &defaults
);
4843 #endif /* HAVE_LIBPAPER */
4847 * Add the default media sizes...
4849 * Note: These values are generally not valid for large-format devices
4850 * like plotters, however it is probably safe to say that those
4851 * users will configure the media size after initially adding
4852 * the device anyways...
4855 if (!DefaultLanguage
||
4856 !strcasecmp(DefaultLanguage
, "C") ||
4857 !strcasecmp(DefaultLanguage
, "POSIX") ||
4858 !strcasecmp(DefaultLanguage
, "en") ||
4859 !strncasecmp(DefaultLanguage
, "en.", 3) ||
4860 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4861 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4862 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4865 * These are the only locales that will default to "letter" size...
4870 num_defaults
= cupsAddOption("PageSize", "Letter", num_defaults
,
4872 num_defaults
= cupsAddOption("PageRegion", "Letter", num_defaults
,
4874 num_defaults
= cupsAddOption("PaperDimension", "Letter", num_defaults
,
4876 num_defaults
= cupsAddOption("ImageableArea", "Letter", num_defaults
,
4883 * The rest default to "a4" size...
4886 num_defaults
= cupsAddOption("PageSize", "A4", num_defaults
,
4888 num_defaults
= cupsAddOption("PageRegion", "A4", num_defaults
,
4890 num_defaults
= cupsAddOption("PaperDimension", "A4", num_defaults
,
4892 num_defaults
= cupsAddOption("ImageableArea", "A4", num_defaults
,
4900 * Open the source file for a copy...
4903 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4905 cupsFreeOptions(num_defaults
, defaults
);
4911 * Open the destination file for a copy...
4914 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4916 cupsFreeOptions(num_defaults
, defaults
);
4923 * Copy the source file to the destination...
4926 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4928 if (!strncmp(buffer
, "*Default", 8))
4931 * Check for an previous default option choice...
4934 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4935 choice
, sizeof(choice
)))
4937 const char *val
; /* Default option value */
4940 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4943 * Substitute the previous choice...
4946 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4951 cupsFilePrintf(dst
, "%s\n", buffer
);
4954 if (cups_protocol
[0])
4955 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4957 cupsFreeOptions(num_defaults
, defaults
);
4960 * Close both files and return...
4967 return (cupsFileClose(dst
));
4972 * 'copy_job_attrs()' - Copy job attributes.
4976 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4977 cupsd_job_t
*job
, /* I - Job */
4978 cups_array_t
*ra
) /* I - Requested attributes array */
4980 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4984 * Send the requested attributes for each job...
4987 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4988 con
->servername
, con
->serverport
, "/jobs/%d",
4991 if (!ra
|| cupsArrayFind(ra
, "document-count"))
4992 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4993 "document-count", job
->num_files
);
4995 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
4996 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4997 "job-more-info", NULL
, job_uri
);
4999 if (job
->state_value
> IPP_JOB_PROCESSING
&&
5000 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
5001 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
5002 job
->num_files
> 0);
5004 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
5005 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5006 "job-printer-up-time", time(NULL
));
5008 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
5009 add_job_state_reasons(con
, job
);
5011 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
5012 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5013 "job-uri", NULL
, job_uri
);
5015 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
5020 * 'copy_printer_attrs()' - Copy printer attributes.
5025 cupsd_client_t
*con
, /* I - Client connection */
5026 cupsd_printer_t
*printer
, /* I - Printer */
5027 cups_array_t
*ra
) /* I - Requested attributes array */
5029 char printer_uri
[HTTP_MAX_URI
];
5031 time_t curtime
; /* Current time */
5032 int i
; /* Looping var */
5033 ipp_attribute_t
*history
; /* History collection */
5037 * Copy the printer attributes to the response using requested-attributes
5038 * and document-format attributes that may be provided by the client.
5041 curtime
= time(NULL
);
5044 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
5045 printer
->recoverable
)
5046 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5047 "com.apple.print.recoverable-message", NULL
,
5048 printer
->recoverable
);
5049 #endif /* __APPLE__ */
5051 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
5052 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5053 "marker-change-time", printer
->marker_time
);
5055 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
5056 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
5057 "printer-alert", NULL
, printer
->alert
);
5059 if (printer
->alert_description
&&
5060 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
5061 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5062 "printer-alert-description", NULL
,
5063 printer
->alert_description
);
5065 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
5066 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
5067 ippTimeToDate(curtime
));
5070 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
5072 if (printer
->reg_name
)
5073 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5074 "printer-dns-sd-name", NULL
, printer
->reg_name
);
5076 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
5077 "printer-dns-sd-name", 0);
5079 #endif /* HAVE_DNSSD */
5081 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
5082 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5083 "printer-error-policy", NULL
, printer
->error_policy
);
5085 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
5086 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
5087 printer
->accepting
);
5089 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
5090 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
5093 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
5094 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5095 "printer-op-policy", NULL
, printer
->op_policy
);
5097 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
5098 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
5101 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
5102 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5103 "printer-state-change-time", printer
->state_time
);
5105 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
5106 cupsArrayFind(ra
, "printer-state-history"))
5109 * Printer history is only sent if specifically requested, so that
5110 * older CUPS/IPP clients won't barf on the collection attributes.
5113 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
5114 "printer-state-history",
5115 printer
->num_history
, NULL
);
5117 for (i
= 0; i
< printer
->num_history
; i
++)
5118 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
5119 NULL
, IPP_TAG_ZERO
, 0);
5122 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
5123 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5124 "printer-state-message", NULL
, printer
->state_message
);
5126 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
5127 add_printer_state_reasons(con
, printer
);
5129 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5131 int type
; /* printer-type value */
5135 * Add the CUPS-specific printer-type attribute...
5138 type
= printer
->type
;
5140 if (printer
== DefaultPrinter
)
5141 type
|= CUPS_PRINTER_DEFAULT
;
5143 if (!printer
->accepting
)
5144 type
|= CUPS_PRINTER_REJECTING
;
5146 if (!printer
->shared
)
5147 type
|= CUPS_PRINTER_NOT_SHARED
;
5149 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
5153 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5154 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5155 "printer-up-time", curtime
);
5157 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
5158 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
5161 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5162 "ipp", NULL
, con
->servername
, con
->serverport
,
5163 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5164 "/classes/%s" : "/printers/%s", printer
->name
);
5165 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5166 "printer-uri-supported", NULL
, printer_uri
);
5167 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5171 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5172 add_queued_job_count(con
, printer
);
5174 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
5175 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
5180 * 'copy_subscription_attrs()' - Copy subscription attributes.
5184 copy_subscription_attrs(
5185 cupsd_client_t
*con
, /* I - Client connection */
5186 cupsd_subscription_t
*sub
, /* I - Subscription */
5187 cups_array_t
*ra
) /* I - Requested attributes array */
5189 ipp_attribute_t
*attr
; /* Current attribute */
5190 char printer_uri
[HTTP_MAX_URI
];
5192 int count
; /* Number of events */
5193 unsigned mask
; /* Current event mask */
5194 const char *name
; /* Current event name */
5198 * Copy the subscription attributes to the response using the
5199 * requested-attributes attribute that may be provided by the client.
5202 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
5204 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5207 * Simple event list...
5210 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5211 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5212 "notify-events", NULL
, name
);
5217 * Complex event list...
5220 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5221 if (sub
->mask
& mask
)
5224 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5225 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5226 "notify-events", count
, NULL
, NULL
);
5228 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5229 if (sub
->mask
& mask
)
5231 attr
->values
[count
].string
.text
=
5232 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5239 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5240 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5241 "notify-job-id", sub
->job
->id
);
5243 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
5244 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5245 "notify-lease-duration", sub
->lease
);
5247 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5249 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5250 "ipp", NULL
, con
->servername
, con
->serverport
,
5251 "/printers/%s", sub
->dest
->name
);
5252 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5253 "notify-printer-uri", NULL
, printer_uri
);
5256 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
5257 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5258 "notify-recipient-uri", NULL
, sub
->recipient
);
5259 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
5260 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5261 "notify-pull-method", NULL
, "ippget");
5263 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
5264 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5265 "notify-subscriber-user-name", NULL
, sub
->owner
);
5267 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5268 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5269 "notify-subscription-id", sub
->id
);
5271 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
5272 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5273 "notify-time-interval", sub
->interval
);
5275 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5276 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5277 sub
->user_data
, sub
->user_data_len
);
5282 * 'create_job()' - Print a file to a printer or class.
5286 create_job(cupsd_client_t
*con
, /* I - Client connection */
5287 ipp_attribute_t
*uri
) /* I - Printer URI */
5289 cupsd_printer_t
*printer
; /* Printer */
5290 cupsd_job_t
*job
; /* New job */
5293 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5294 con
->http
.fd
, uri
->values
[0].string
.text
);
5297 * Is the destination valid?
5300 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5306 send_ipp_status(con
, IPP_NOT_FOUND
,
5307 _("The printer or class was not found."));
5312 * Create the job object...
5315 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5318 job
->pending_timeout
= 1;
5321 * Save and log the job...
5326 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
5327 job
->id
, job
->dest
, job
->username
);
5332 * 'create_requested_array()' - Create an array for the requested-attributes.
5335 static cups_array_t
* /* O - Array of attributes or NULL */
5336 create_requested_array(ipp_t
*request
) /* I - IPP request */
5338 int i
; /* Looping var */
5339 ipp_attribute_t
*requested
; /* requested-attributes attribute */
5340 cups_array_t
*ra
; /* Requested attributes array */
5341 char *value
; /* Current value */
5345 * Get the requested-attributes attribute, and return NULL if we don't
5349 if ((requested
= ippFindAttribute(request
, "requested-attributes",
5350 IPP_TAG_KEYWORD
)) == NULL
)
5354 * If the attribute contains a single "all" keyword, return NULL...
5357 if (requested
->num_values
== 1 &&
5358 !strcmp(requested
->values
[0].string
.text
, "all"))
5362 * Create an array using "strcmp" as the comparison function...
5365 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
5367 for (i
= 0; i
< requested
->num_values
; i
++)
5369 value
= requested
->values
[i
].string
.text
;
5371 if (!strcmp(value
, "job-template"))
5373 cupsArrayAdd(ra
, "copies");
5374 cupsArrayAdd(ra
, "copies-default");
5375 cupsArrayAdd(ra
, "copies-supported");
5376 cupsArrayAdd(ra
, "finishings");
5377 cupsArrayAdd(ra
, "finishings-default");
5378 cupsArrayAdd(ra
, "finishings-supported");
5379 cupsArrayAdd(ra
, "job-hold-until");
5380 cupsArrayAdd(ra
, "job-hold-until-default");
5381 cupsArrayAdd(ra
, "job-hold-until-supported");
5382 cupsArrayAdd(ra
, "job-priority");
5383 cupsArrayAdd(ra
, "job-priority-default");
5384 cupsArrayAdd(ra
, "job-priority-supported");
5385 cupsArrayAdd(ra
, "job-sheets");
5386 cupsArrayAdd(ra
, "job-sheets-default");
5387 cupsArrayAdd(ra
, "job-sheets-supported");
5388 cupsArrayAdd(ra
, "media");
5389 cupsArrayAdd(ra
, "media-default");
5390 cupsArrayAdd(ra
, "media-supported");
5391 cupsArrayAdd(ra
, "multiple-document-handling");
5392 cupsArrayAdd(ra
, "multiple-document-handling-default");
5393 cupsArrayAdd(ra
, "multiple-document-handling-supported");
5394 cupsArrayAdd(ra
, "number-up");
5395 cupsArrayAdd(ra
, "number-up-default");
5396 cupsArrayAdd(ra
, "number-up-supported");
5397 cupsArrayAdd(ra
, "orientation-requested");
5398 cupsArrayAdd(ra
, "orientation-requested-default");
5399 cupsArrayAdd(ra
, "orientation-requested-supported");
5400 cupsArrayAdd(ra
, "page-ranges");
5401 cupsArrayAdd(ra
, "page-ranges-supported");
5402 cupsArrayAdd(ra
, "printer-resolution");
5403 cupsArrayAdd(ra
, "printer-resolution-default");
5404 cupsArrayAdd(ra
, "printer-resolution-supported");
5405 cupsArrayAdd(ra
, "print-quality");
5406 cupsArrayAdd(ra
, "print-quality-default");
5407 cupsArrayAdd(ra
, "print-quality-supported");
5408 cupsArrayAdd(ra
, "sides");
5409 cupsArrayAdd(ra
, "sides-default");
5410 cupsArrayAdd(ra
, "sides-supported");
5412 else if (!strcmp(value
, "job-description"))
5414 cupsArrayAdd(ra
, "date-time-at-completed");
5415 cupsArrayAdd(ra
, "date-time-at-creation");
5416 cupsArrayAdd(ra
, "date-time-at-processing");
5417 cupsArrayAdd(ra
, "job-detailed-status-message");
5418 cupsArrayAdd(ra
, "job-document-access-errors");
5419 cupsArrayAdd(ra
, "job-id");
5420 cupsArrayAdd(ra
, "job-impressions");
5421 cupsArrayAdd(ra
, "job-impressions-completed");
5422 cupsArrayAdd(ra
, "job-k-octets");
5423 cupsArrayAdd(ra
, "job-k-octets-processed");
5424 cupsArrayAdd(ra
, "job-media-sheets");
5425 cupsArrayAdd(ra
, "job-media-sheets-completed");
5426 cupsArrayAdd(ra
, "job-message-from-operator");
5427 cupsArrayAdd(ra
, "job-more-info");
5428 cupsArrayAdd(ra
, "job-name");
5429 cupsArrayAdd(ra
, "job-originating-user-name");
5430 cupsArrayAdd(ra
, "job-printer-up-time");
5431 cupsArrayAdd(ra
, "job-printer-uri");
5432 cupsArrayAdd(ra
, "job-state");
5433 cupsArrayAdd(ra
, "job-state-message");
5434 cupsArrayAdd(ra
, "job-state-reasons");
5435 cupsArrayAdd(ra
, "job-uri");
5436 cupsArrayAdd(ra
, "number-of-documents");
5437 cupsArrayAdd(ra
, "number-of-intervening-jobs");
5438 cupsArrayAdd(ra
, "output-device-assigned");
5439 cupsArrayAdd(ra
, "time-at-completed");
5440 cupsArrayAdd(ra
, "time-at-creation");
5441 cupsArrayAdd(ra
, "time-at-processing");
5443 else if (!strcmp(value
, "printer-description"))
5445 cupsArrayAdd(ra
, "charset-configured");
5446 cupsArrayAdd(ra
, "charset-supported");
5447 cupsArrayAdd(ra
, "color-supported");
5448 cupsArrayAdd(ra
, "compression-supported");
5449 cupsArrayAdd(ra
, "document-format-default");
5450 cupsArrayAdd(ra
, "document-format-supported");
5451 cupsArrayAdd(ra
, "generated-natural-language-supported");
5452 cupsArrayAdd(ra
, "ipp-versions-supported");
5453 cupsArrayAdd(ra
, "job-impressions-supported");
5454 cupsArrayAdd(ra
, "job-k-octets-supported");
5455 cupsArrayAdd(ra
, "job-media-sheets-supported");
5456 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
5457 cupsArrayAdd(ra
, "multiple-operation-time-out");
5458 cupsArrayAdd(ra
, "natural-language-configured");
5459 cupsArrayAdd(ra
, "notify-attributes-supported");
5460 cupsArrayAdd(ra
, "notify-lease-duration-default");
5461 cupsArrayAdd(ra
, "notify-lease-duration-supported");
5462 cupsArrayAdd(ra
, "notify-max-events-supported");
5463 cupsArrayAdd(ra
, "notify-events-default");
5464 cupsArrayAdd(ra
, "notify-events-supported");
5465 cupsArrayAdd(ra
, "notify-pull-method-supported");
5466 cupsArrayAdd(ra
, "notify-schemes-supported");
5467 cupsArrayAdd(ra
, "operations-supported");
5468 cupsArrayAdd(ra
, "pages-per-minute");
5469 cupsArrayAdd(ra
, "pages-per-minute-color");
5470 cupsArrayAdd(ra
, "pdl-override-supported");
5471 cupsArrayAdd(ra
, "printer-alert");
5472 cupsArrayAdd(ra
, "printer-alert-description");
5473 cupsArrayAdd(ra
, "printer-current-time");
5474 cupsArrayAdd(ra
, "printer-driver-installer");
5475 cupsArrayAdd(ra
, "printer-info");
5476 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
5477 cupsArrayAdd(ra
, "printer-location");
5478 cupsArrayAdd(ra
, "printer-make-and-model");
5479 cupsArrayAdd(ra
, "printer-message-from-operator");
5480 cupsArrayAdd(ra
, "printer-more-info");
5481 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
5482 cupsArrayAdd(ra
, "printer-name");
5483 cupsArrayAdd(ra
, "printer-state");
5484 cupsArrayAdd(ra
, "printer-state-message");
5485 cupsArrayAdd(ra
, "printer-state-reasons");
5486 cupsArrayAdd(ra
, "printer-up-time");
5487 cupsArrayAdd(ra
, "printer-uri-supported");
5488 cupsArrayAdd(ra
, "queued-job-count");
5489 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
5490 cupsArrayAdd(ra
, "uri-authentication-supported");
5491 cupsArrayAdd(ra
, "uri-security-supported");
5493 else if (!strcmp(value
, "printer-defaults"))
5495 char *name
; /* Option name */
5498 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5500 name
= (char *)cupsArrayNext(CommonDefaults
))
5501 cupsArrayAdd(ra
, name
);
5503 else if (!strcmp(value
, "subscription-template"))
5505 cupsArrayAdd(ra
, "notify-attributes");
5506 cupsArrayAdd(ra
, "notify-charset");
5507 cupsArrayAdd(ra
, "notify-events");
5508 cupsArrayAdd(ra
, "notify-lease-duration");
5509 cupsArrayAdd(ra
, "notify-natural-language");
5510 cupsArrayAdd(ra
, "notify-pull-method");
5511 cupsArrayAdd(ra
, "notify-recipient-uri");
5512 cupsArrayAdd(ra
, "notify-time-interval");
5513 cupsArrayAdd(ra
, "notify-user-data");
5516 cupsArrayAdd(ra
, value
);
5524 * 'create_subscription()' - Create a notification subscription.
5528 create_subscription(
5529 cupsd_client_t
*con
, /* I - Client connection */
5530 ipp_attribute_t
*uri
) /* I - Printer URI */
5532 http_status_t status
; /* Policy status */
5533 int i
; /* Looping var */
5534 ipp_attribute_t
*attr
; /* Current attribute */
5535 cups_ptype_t dtype
; /* Destination type (printer/class) */
5536 char scheme
[HTTP_MAX_URI
],
5537 /* Scheme portion of URI */
5538 userpass
[HTTP_MAX_URI
],
5539 /* Username portion of URI */
5541 /* Host portion of URI */
5542 resource
[HTTP_MAX_URI
];
5543 /* Resource portion of URI */
5544 int port
; /* Port portion of URI */
5545 cupsd_printer_t
*printer
; /* Printer/class */
5546 cupsd_job_t
*job
; /* Job */
5547 int jobid
; /* Job ID */
5548 cupsd_subscription_t
*sub
; /* Subscription object */
5549 const char *username
, /* requesting-user-name or
5550 authenticated username */
5551 *recipient
, /* notify-recipient-uri */
5552 *pullmethod
; /* notify-pull-method */
5553 ipp_attribute_t
*user_data
; /* notify-user-data */
5554 int interval
, /* notify-time-interval */
5555 lease
; /* notify-lease-duration */
5556 unsigned mask
; /* notify-events */
5557 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5558 *notify_lease
; /* notify-lease-duration(-default) */
5562 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5564 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5565 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
5566 attr
->value_tag
, attr
->name
);
5568 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
5573 * Is the destination valid?
5576 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5577 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5578 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5580 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5581 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5582 sizeof(host
), &port
, resource
, sizeof(resource
));
5584 if (!strcmp(resource
, "/"))
5586 dtype
= (cups_ptype_t
)0;
5589 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5591 dtype
= (cups_ptype_t
)0;
5594 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5596 dtype
= CUPS_PRINTER_CLASS
;
5599 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5605 send_ipp_status(con
, IPP_NOT_FOUND
,
5606 _("The printer or class was not found."));
5616 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5619 send_http_error(con
, status
, printer
);
5623 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5625 send_http_error(con
, status
, NULL
);
5630 * Get the user that is requesting the subscription...
5633 username
= get_username(con
);
5636 * Find the first subscription group attribute; return if we have
5640 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5641 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5646 send_ipp_status(con
, IPP_BAD_REQUEST
,
5647 _("No subscription attributes in request!"));
5652 * Process the subscription attributes in the request...
5655 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5663 lease
= DefaultLeaseDuration
;
5665 mask
= CUPSD_EVENT_NONE
;
5669 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5671 notify_lease
= ippFindAttribute(printer
->attrs
,
5672 "notify-lease-duration-default",
5676 lease
= notify_lease
->values
[0].integer
;
5680 notify_events
= NULL
;
5681 notify_lease
= NULL
;
5684 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5686 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5687 attr
->value_tag
== IPP_TAG_URI
)
5690 * Validate the recipient scheme against the ServerBin/notifier
5694 char notifier
[1024]; /* Notifier filename */
5697 recipient
= attr
->values
[0].string
.text
;
5699 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5700 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5701 host
, sizeof(host
), &port
,
5702 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5704 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5705 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
5706 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5707 "notify-status-code", IPP_URI_SCHEME
);
5711 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5713 if (access(notifier
, X_OK
))
5715 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5716 _("notify-recipient-uri URI \"%s\" uses unknown "
5717 "scheme!"), recipient
);
5718 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5719 "notify-status-code", IPP_URI_SCHEME
);
5723 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5724 attr
->value_tag
== IPP_TAG_KEYWORD
)
5726 pullmethod
= attr
->values
[0].string
.text
;
5728 if (strcmp(pullmethod
, "ippget"))
5730 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5731 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
5732 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5733 "notify-status-code", IPP_ATTRIBUTES
);
5737 else if (!strcmp(attr
->name
, "notify-charset") &&
5738 attr
->value_tag
== IPP_TAG_CHARSET
&&
5739 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5740 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5742 send_ipp_status(con
, IPP_CHARSET
,
5743 _("Character set \"%s\" not supported!"),
5744 attr
->values
[0].string
.text
);
5747 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5748 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5749 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5751 send_ipp_status(con
, IPP_CHARSET
,
5752 _("Language \"%s\" not supported!"),
5753 attr
->values
[0].string
.text
);
5756 else if (!strcmp(attr
->name
, "notify-user-data") &&
5757 attr
->value_tag
== IPP_TAG_STRING
)
5759 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5761 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5762 _("The notify-user-data value is too large "
5763 "(%d > 63 octets)!"),
5764 attr
->values
[0].unknown
.length
);
5770 else if (!strcmp(attr
->name
, "notify-events") &&
5771 attr
->value_tag
== IPP_TAG_KEYWORD
)
5772 notify_events
= attr
;
5773 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5774 attr
->value_tag
== IPP_TAG_INTEGER
)
5775 lease
= attr
->values
[0].integer
;
5776 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5777 attr
->value_tag
== IPP_TAG_INTEGER
)
5778 interval
= attr
->values
[0].integer
;
5779 else if (!strcmp(attr
->name
, "notify-job-id") &&
5780 attr
->value_tag
== IPP_TAG_INTEGER
)
5781 jobid
= attr
->values
[0].integer
;
5788 for (i
= 0; i
< notify_events
->num_values
; i
++)
5789 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5793 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5795 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5796 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5797 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5799 if (!recipient
&& !pullmethod
)
5802 if (mask
== CUPSD_EVENT_NONE
)
5805 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5807 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5810 send_ipp_status(con
, IPP_BAD_REQUEST
,
5811 _("notify-events not specified!"));
5816 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5818 cupsdLogMessage(CUPSD_LOG_INFO
,
5819 "create_subscription: Limiting notify-lease-duration to "
5822 lease
= MaxLeaseDuration
;
5827 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5829 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
5836 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
5839 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
5842 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5843 "Added subscription %d for printer \"%s\"",
5844 sub
->id
, printer
->name
);
5846 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
5849 sub
->interval
= interval
;
5851 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5853 cupsdSetString(&sub
->owner
, username
);
5857 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5858 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5859 sub
->user_data_len
);
5862 ippAddSeparator(con
->response
);
5863 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5864 "notify-subscription-id", sub
->id
);
5866 con
->response
->request
.status
.status_code
= IPP_OK
;
5872 cupsdSaveAllSubscriptions();
5878 * 'delete_printer()' - Remove a printer or class from the system.
5882 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5883 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5885 http_status_t status
; /* Policy status */
5886 cups_ptype_t dtype
; /* Destination type (printer/class) */
5887 cupsd_printer_t
*printer
; /* Printer/class */
5888 char filename
[1024]; /* Script/PPD filename */
5891 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5892 con
->http
.fd
, uri
->values
[0].string
.text
);
5895 * Do we have a valid URI?
5898 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5904 send_ipp_status(con
, IPP_NOT_FOUND
,
5905 _("The printer or class was not found."));
5913 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5915 send_http_error(con
, status
, NULL
);
5920 * Remove old jobs...
5923 cupsdCancelJobs(printer
->name
, NULL
, 1);
5926 * Remove old subscriptions and send a "deleted printer" event...
5929 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5930 "%s \"%s\" deleted by \"%s\".",
5931 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5932 printer
->name
, get_username(con
));
5934 cupsdExpireSubscriptions(printer
, NULL
);
5937 * Remove any old PPD or script files...
5940 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5944 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5950 * Unregister color profiles...
5953 apple_unregister_profiles(printer
);
5954 #endif /* __APPLE__ */
5956 if (dtype
& CUPS_PRINTER_CLASS
)
5958 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5959 printer
->name
, get_username(con
));
5961 cupsdDeletePrinter(printer
, 0);
5962 cupsdSaveAllClasses();
5966 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5967 printer
->name
, get_username(con
));
5969 cupsdDeletePrinter(printer
, 0);
5970 cupsdSaveAllPrinters();
5973 cupsdWritePrintcap();
5976 * Return with no errors...
5979 con
->response
->request
.status
.status_code
= IPP_OK
;
5984 * 'get_default()' - Get the default destination.
5988 get_default(cupsd_client_t
*con
) /* I - Client connection */
5990 http_status_t status
; /* Policy status */
5991 cups_array_t
*ra
; /* Requested attributes array */
5994 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
6000 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6002 send_http_error(con
, status
, NULL
);
6008 ra
= create_requested_array(con
->request
);
6010 copy_printer_attrs(con
, DefaultPrinter
, ra
);
6012 cupsArrayDelete(ra
);
6014 con
->response
->request
.status
.status_code
= IPP_OK
;
6017 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
6022 * 'get_devices()' - Get the list of available devices on the local system.
6026 get_devices(cupsd_client_t
*con
) /* I - Client connection */
6028 http_status_t status
; /* Policy status */
6029 ipp_attribute_t
*limit
, /* Limit attribute */
6030 *requested
; /* requested-attributes attribute */
6031 char command
[1024], /* cups-deviced command */
6032 options
[1024], /* Options to pass to command */
6034 /* String for requested attributes */
6037 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
6043 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6045 send_http_error(con
, status
, NULL
);
6050 * Run cups-deviced command with the given options...
6053 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6054 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6058 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6060 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6062 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
6063 snprintf(options
, sizeof(options
),
6065 con
->request
->request
.op
.request_id
,
6066 limit
? limit
->values
[0].integer
: 0, (int)User
,
6069 if (cupsdSendCommand(con
, command
, options
, 1))
6072 * Command started successfully, don't send an IPP response here...
6075 ippDelete(con
->response
);
6076 con
->response
= NULL
;
6081 * Command failed, return "internal error" so the user knows something
6085 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6086 _("cups-deviced failed to execute."));
6092 * 'get_document()' - Get a copy of a job file.
6096 get_document(cupsd_client_t
*con
, /* I - Client connection */
6097 ipp_attribute_t
*uri
) /* I - Job URI */
6099 http_status_t status
; /* Policy status */
6100 ipp_attribute_t
*attr
; /* Current attribute */
6101 int jobid
; /* Job ID */
6102 int docnum
; /* Document number */
6103 cupsd_job_t
*job
; /* Current job */
6104 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6105 username
[HTTP_MAX_URI
], /* Username portion of URI */
6106 host
[HTTP_MAX_URI
], /* Host portion of URI */
6107 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6108 int port
; /* Port portion of URI */
6109 char filename
[1024], /* Filename for document */
6110 format
[1024]; /* Format for document */
6113 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6114 con
->http
.fd
, uri
->values
[0].string
.text
);
6117 * See if we have a job URI or a printer URI...
6120 if (!strcmp(uri
->name
, "printer-uri"))
6123 * Got a printer URI; see if we also have a job-id attribute...
6126 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6127 IPP_TAG_INTEGER
)) == NULL
)
6129 send_ipp_status(con
, IPP_BAD_REQUEST
,
6130 _("Got a printer-uri attribute but no job-id!"));
6134 jobid
= attr
->values
[0].integer
;
6139 * Got a job URI; parse it to get the job ID...
6142 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6143 sizeof(method
), username
, sizeof(username
), host
,
6144 sizeof(host
), &port
, resource
, sizeof(resource
));
6146 if (strncmp(resource
, "/jobs/", 6))
6152 send_ipp_status(con
, IPP_BAD_REQUEST
,
6153 _("Bad job-uri attribute \"%s\"!"),
6154 uri
->values
[0].string
.text
);
6158 jobid
= atoi(resource
+ 6);
6162 * See if the job exists...
6165 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6168 * Nope - return a "not found" error...
6171 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6179 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6181 send_http_error(con
, status
, NULL
);
6186 * Get the document number...
6189 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6190 IPP_TAG_INTEGER
)) == NULL
)
6192 send_ipp_status(con
, IPP_BAD_REQUEST
,
6193 _("Missing document-number attribute!"));
6197 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6198 attr
->num_values
> 1)
6200 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
6205 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6207 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6209 cupsdLogMessage(CUPSD_LOG_ERROR
,
6210 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6212 send_ipp_status(con
, IPP_NOT_FOUND
,
6213 _("Unable to open document %d in job %d!"), docnum
, jobid
);
6217 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6221 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6222 job
->filetypes
[docnum
- 1]->type
);
6224 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6226 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6228 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6229 IPP_TAG_NAME
)) != NULL
)
6230 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6231 NULL
, attr
->values
[0].string
.text
);
6236 * 'get_job_attrs()' - Get job attributes.
6240 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6241 ipp_attribute_t
*uri
) /* I - Job URI */
6243 http_status_t status
; /* Policy status */
6244 ipp_attribute_t
*attr
; /* Current attribute */
6245 int jobid
; /* Job ID */
6246 cupsd_job_t
*job
; /* Current job */
6247 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6248 username
[HTTP_MAX_URI
], /* Username portion of URI */
6249 host
[HTTP_MAX_URI
], /* Host portion of URI */
6250 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6251 int port
; /* Port portion of URI */
6252 cups_array_t
*ra
; /* Requested attributes array */
6255 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6256 con
->http
.fd
, uri
->values
[0].string
.text
);
6259 * See if we have a job URI or a printer URI...
6262 if (!strcmp(uri
->name
, "printer-uri"))
6265 * Got a printer URI; see if we also have a job-id attribute...
6268 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6269 IPP_TAG_INTEGER
)) == NULL
)
6271 send_ipp_status(con
, IPP_BAD_REQUEST
,
6272 _("Got a printer-uri attribute but no job-id!"));
6276 jobid
= attr
->values
[0].integer
;
6281 * Got a job URI; parse it to get the job ID...
6284 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6285 sizeof(method
), username
, sizeof(username
), host
,
6286 sizeof(host
), &port
, resource
, sizeof(resource
));
6288 if (strncmp(resource
, "/jobs/", 6))
6294 send_ipp_status(con
, IPP_BAD_REQUEST
,
6295 _("Bad job-uri attribute \"%s\"!"),
6296 uri
->values
[0].string
.text
);
6300 jobid
= atoi(resource
+ 6);
6304 * See if the job exists...
6307 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6310 * Nope - return a "not found" error...
6313 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6321 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6323 send_http_error(con
, status
, NULL
);
6328 * Copy attributes...
6333 ra
= create_requested_array(con
->request
);
6334 copy_job_attrs(con
, job
, ra
);
6335 cupsArrayDelete(ra
);
6337 con
->response
->request
.status
.status_code
= IPP_OK
;
6342 * 'get_jobs()' - Get a list of jobs for the specified printer.
6346 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6347 ipp_attribute_t
*uri
) /* I - Printer URI */
6349 http_status_t status
; /* Policy status */
6350 ipp_attribute_t
*attr
; /* Current attribute */
6351 const char *dest
; /* Destination */
6352 cups_ptype_t dtype
; /* Destination type (printer/class) */
6353 cups_ptype_t dmask
; /* Destination type mask */
6354 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6355 username
[HTTP_MAX_URI
], /* Username portion of URI */
6356 host
[HTTP_MAX_URI
], /* Host portion of URI */
6357 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6358 int port
; /* Port portion of URI */
6359 int completed
; /* Completed jobs? */
6360 int first_job_id
; /* First job ID */
6361 int limit
; /* Maximum number of jobs to return */
6362 int count
; /* Number of jobs that match */
6363 cupsd_job_t
*job
; /* Current job pointer */
6364 cupsd_printer_t
*printer
; /* Printer */
6365 cups_array_t
*list
; /* Which job list... */
6366 cups_array_t
*ra
; /* Requested attributes array */
6369 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
6370 uri
->values
[0].string
.text
);
6373 * Is the destination valid?
6376 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6377 sizeof(scheme
), username
, sizeof(username
), host
,
6378 sizeof(host
), &port
, resource
, sizeof(resource
));
6380 if (!strcmp(resource
, "/") ||
6381 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
6384 dtype
= (cups_ptype_t
)0;
6385 dmask
= (cups_ptype_t
)0;
6388 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6391 dtype
= (cups_ptype_t
)0;
6392 dmask
= CUPS_PRINTER_CLASS
;
6395 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6398 dtype
= CUPS_PRINTER_CLASS
;
6399 dmask
= CUPS_PRINTER_CLASS
;
6402 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6409 send_ipp_status(con
, IPP_NOT_FOUND
,
6410 _("The printer or class was not found."));
6415 dtype
&= CUPS_PRINTER_CLASS
;
6416 dmask
= CUPS_PRINTER_CLASS
;
6425 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6428 send_http_error(con
, status
, printer
);
6432 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6434 send_http_error(con
, status
, NULL
);
6439 * See if the "which-jobs" attribute have been specified...
6442 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6443 IPP_TAG_KEYWORD
)) != NULL
&&
6444 !strcmp(attr
->values
[0].string
.text
, "completed"))
6449 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
6461 * See if they want to limit the number of jobs reported...
6464 if ((attr
= ippFindAttribute(con
->request
, "limit",
6465 IPP_TAG_INTEGER
)) != NULL
)
6466 limit
= attr
->values
[0].integer
;
6470 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
6471 IPP_TAG_INTEGER
)) != NULL
)
6472 first_job_id
= attr
->values
[0].integer
;
6477 * See if we only want to see jobs for a specific user...
6480 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
6481 IPP_TAG_BOOLEAN
)) != NULL
&&
6482 attr
->values
[0].boolean
)
6483 strlcpy(username
, get_username(con
), sizeof(username
));
6487 ra
= create_requested_array(con
->request
);
6490 * OK, build a list of jobs for this printer...
6493 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6494 count
< limit
&& job
;
6495 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6498 * Filter out jobs that don't match...
6501 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
6503 if (!job
->dest
|| !job
->username
)
6506 if (!job
->dest
|| !job
->username
)
6509 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6510 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6512 if ((job
->dtype
& dmask
) != dtype
&&
6513 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6515 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
6518 if (job
->id
< first_job_id
)
6526 if (username
[0] && strcasecmp(username
, job
->username
))
6530 ippAddSeparator(con
->response
);
6534 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
6536 copy_job_attrs(con
, job
, ra
);
6539 cupsArrayDelete(ra
);
6541 con
->response
->request
.status
.status_code
= IPP_OK
;
6546 * 'get_notifications()' - Get events for a subscription.
6550 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6552 int i
, j
; /* Looping vars */
6553 http_status_t status
; /* Policy status */
6554 cupsd_subscription_t
*sub
; /* Subscription */
6555 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6556 *sequences
; /* notify-sequence-numbers */
6557 int min_seq
; /* Minimum sequence number */
6558 int interval
; /* Poll interval */
6561 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6565 * Get subscription attributes...
6568 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6570 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6575 send_ipp_status(con
, IPP_BAD_REQUEST
,
6576 _("Missing notify-subscription-ids attribute!"));
6581 * Are the subscription IDs valid?
6584 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6586 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6589 * Bad subscription ID...
6592 send_ipp_status(con
, IPP_NOT_FOUND
,
6593 _("notify-subscription-id %d no good!"),
6594 ids
->values
[i
].integer
);
6602 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6604 con
, sub
->owner
)) != HTTP_OK
)
6606 send_http_error(con
, status
, sub
->dest
);
6611 * Check the subscription type and update the interval accordingly.
6614 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6617 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6619 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6625 * Tell the client to poll again in N seconds...
6629 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6630 "notify-get-interval", interval
);
6632 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6633 "printer-up-time", time(NULL
));
6636 * Copy the subscription event attributes to the response.
6639 con
->response
->request
.status
.status_code
=
6640 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6642 for (i
= 0; i
< ids
->num_values
; i
++)
6645 * Get the subscription and sequence number...
6648 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6650 if (sequences
&& i
< sequences
->num_values
)
6651 min_seq
= sequences
->values
[i
].integer
;
6656 * If we don't have any new events, nothing to do here...
6659 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
6663 * Otherwise copy all of the new events...
6666 if (sub
->first_event_id
> min_seq
)
6669 j
= min_seq
- sub
->first_event_id
;
6671 for (; j
< sub
->num_events
; j
++)
6673 ippAddSeparator(con
->response
);
6675 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
6676 IPP_TAG_EVENT_NOTIFICATION
, 0);
6683 * 'get_ppd()' - Get a named PPD from the local system.
6687 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
6688 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
6690 http_status_t status
; /* Policy status */
6691 cupsd_printer_t
*dest
; /* Destination */
6692 cups_ptype_t dtype
; /* Destination type */
6695 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
6696 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
6698 if (!strcmp(uri
->name
, "ppd-name"))
6701 * Return a PPD file from cups-driverd...
6704 char command
[1024], /* cups-driverd command */
6705 options
[1024], /* Options to pass to command */
6706 ppd_name
[1024]; /* ppd-name */
6713 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6715 send_http_error(con
, status
, NULL
);
6720 * Run cups-driverd command with the given options...
6723 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6724 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
6725 snprintf(options
, sizeof(options
), "get+%d+%s",
6726 con
->request
->request
.op
.request_id
, ppd_name
);
6728 if (cupsdSendCommand(con
, command
, options
, 0))
6731 * Command started successfully, don't send an IPP response here...
6734 ippDelete(con
->response
);
6735 con
->response
= NULL
;
6740 * Command failed, return "internal error" so the user knows something
6744 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6745 _("cups-driverd failed to execute."));
6748 else if (!strcmp(uri
->name
, "printer-uri") &&
6749 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
6751 int i
; /* Looping var */
6752 char filename
[1024]; /* PPD filename */
6759 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6761 send_http_error(con
, status
, dest
);
6766 * See if we need the PPD for a class or remote printer...
6769 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6772 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
6774 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6775 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6776 "printer-uri", NULL
, dest
->uri
);
6779 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
6781 for (i
= 0; i
< dest
->num_printers
; i
++)
6782 if (!(dest
->printers
[i
]->type
&
6783 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
6785 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6786 dest
->printers
[i
]->name
);
6788 if (!access(filename
, 0))
6792 if (i
< dest
->num_printers
)
6793 dest
= dest
->printers
[i
];
6796 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6797 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6798 "printer-uri", NULL
, dest
->printers
[0]->uri
);
6804 * Found the printer with the PPD file, now see if there is one...
6807 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
6809 send_ipp_status(con
, IPP_NOT_FOUND
,
6810 _("The PPD file \"%s\" could not be opened: %s"),
6811 uri
->values
[0].string
.text
, strerror(errno
));
6815 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6819 con
->response
->request
.status
.status_code
= IPP_OK
;
6822 send_ipp_status(con
, IPP_NOT_FOUND
,
6823 _("The PPD file \"%s\" could not be found."),
6824 uri
->values
[0].string
.text
);
6829 * 'get_ppds()' - Get the list of PPD files on the local system.
6833 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
6835 http_status_t status
; /* Policy status */
6836 ipp_attribute_t
*limit
, /* Limit attribute */
6837 *device
, /* ppd-device-id attribute */
6838 *language
, /* ppd-natural-language attribute */
6839 *make
, /* ppd-make attribute */
6840 *model
, /* ppd-make-and-model attribute */
6841 *model_number
, /* ppd-model-number attribute */
6842 *product
, /* ppd-product attribute */
6843 *psversion
, /* ppd-psverion attribute */
6844 *type
, /* ppd-type attribute */
6845 *requested
; /* requested-attributes attribute */
6846 char command
[1024], /* cups-driverd command */
6847 options
[1024], /* Options to pass to command */
6848 device_str
[256],/* Escaped ppd-device-id string */
6850 /* Escaped ppd-natural-language */
6851 make_str
[256], /* Escaped ppd-make string */
6852 model_str
[256], /* Escaped ppd-make-and-model string */
6853 model_number_str
[256],
6854 /* ppd-model-number string */
6856 /* Escaped ppd-product string */
6858 /* Escaped ppd-psversion string */
6859 type_str
[256], /* Escaped ppd-type string */
6861 /* String for requested attributes */
6864 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
6870 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6872 send_http_error(con
, status
, NULL
);
6877 * Run cups-driverd command with the given options...
6880 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6881 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
6882 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
6884 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
6885 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
6887 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
6889 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
6890 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
6891 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
6892 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6896 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6898 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6901 url_encode_attr(device
, device_str
, sizeof(device_str
));
6903 device_str
[0] = '\0';
6906 url_encode_attr(language
, language_str
, sizeof(language_str
));
6908 language_str
[0] = '\0';
6911 url_encode_attr(make
, make_str
, sizeof(make_str
));
6916 url_encode_attr(model
, model_str
, sizeof(model_str
));
6918 model_str
[0] = '\0';
6921 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
6922 model_number
->values
[0].integer
);
6924 model_number_str
[0] = '\0';
6927 url_encode_attr(product
, product_str
, sizeof(product_str
));
6929 product_str
[0] = '\0';
6932 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
6934 psversion_str
[0] = '\0';
6937 url_encode_attr(type
, type_str
, sizeof(type_str
));
6941 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6942 snprintf(options
, sizeof(options
),
6943 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
6944 con
->request
->request
.op
.request_id
,
6945 limit
? limit
->values
[0].integer
: 0,
6947 device
? "%20" : "", device_str
,
6948 language
? "%20" : "", language_str
,
6949 make
? "%20" : "", make_str
,
6950 model
? "%20" : "", model_str
,
6951 model_number
? "%20" : "", model_number_str
,
6952 product
? "%20" : "", product_str
,
6953 psversion
? "%20" : "", psversion_str
,
6954 type
? "%20" : "", type_str
);
6956 if (cupsdSendCommand(con
, command
, options
, 0))
6959 * Command started successfully, don't send an IPP response here...
6962 ippDelete(con
->response
);
6963 con
->response
= NULL
;
6968 * Command failed, return "internal error" so the user knows something
6972 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6973 _("cups-driverd failed to execute."));
6979 * 'get_printer_attrs()' - Get printer attributes.
6983 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
6984 ipp_attribute_t
*uri
) /* I - Printer URI */
6986 http_status_t status
; /* Policy status */
6987 cups_ptype_t dtype
; /* Destination type (printer/class) */
6988 cupsd_printer_t
*printer
; /* Printer/class */
6989 cups_array_t
*ra
; /* Requested attributes array */
6992 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
6993 con
->http
.fd
, uri
->values
[0].string
.text
);
6996 * Is the destination valid?
6999 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7005 send_ipp_status(con
, IPP_NOT_FOUND
,
7006 _("The printer or class was not found."));
7014 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7016 send_http_error(con
, status
, printer
);
7021 * Send the attributes...
7024 ra
= create_requested_array(con
->request
);
7026 copy_printer_attrs(con
, printer
, ra
);
7028 cupsArrayDelete(ra
);
7030 con
->response
->request
.status
.status_code
= IPP_OK
;
7035 * 'get_printers()' - Get a list of printers or classes.
7039 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7040 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7042 http_status_t status
; /* Policy status */
7043 ipp_attribute_t
*attr
; /* Current attribute */
7044 int limit
; /* Max number of printers to return */
7045 int count
; /* Number of printers that match */
7046 cupsd_printer_t
*printer
; /* Current printer pointer */
7047 int printer_type
, /* printer-type attribute */
7048 printer_mask
; /* printer-type-mask attribute */
7049 char *location
; /* Location string */
7050 const char *username
; /* Current user */
7051 char *first_printer_name
; /* first-printer-name attribute */
7052 cups_array_t
*ra
; /* Requested attributes array */
7055 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7056 con
->http
.fd
, type
);
7062 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7064 send_http_error(con
, status
, NULL
);
7069 * Check for printers...
7072 if (!Printers
|| !cupsArrayCount(Printers
))
7074 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7079 * See if they want to limit the number of printers reported...
7082 if ((attr
= ippFindAttribute(con
->request
, "limit",
7083 IPP_TAG_INTEGER
)) != NULL
)
7084 limit
= attr
->values
[0].integer
;
7088 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7089 IPP_TAG_NAME
)) != NULL
)
7090 first_printer_name
= attr
->values
[0].string
.text
;
7092 first_printer_name
= NULL
;
7095 * Support filtering...
7098 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7099 IPP_TAG_ENUM
)) != NULL
)
7100 printer_type
= attr
->values
[0].integer
;
7104 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7105 IPP_TAG_ENUM
)) != NULL
)
7106 printer_mask
= attr
->values
[0].integer
;
7110 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7111 IPP_TAG_TEXT
)) != NULL
)
7112 location
= attr
->values
[0].string
.text
;
7116 if (con
->username
[0])
7117 username
= con
->username
;
7118 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7119 IPP_TAG_NAME
)) != NULL
)
7120 username
= attr
->values
[0].string
.text
;
7124 ra
= create_requested_array(con
->request
);
7127 * OK, build a list of printers for this printer...
7130 if (first_printer_name
)
7132 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7133 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7136 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7139 count
< limit
&& printer
;
7140 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7142 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7143 (printer
->type
& printer_mask
) == printer_type
&&
7144 (!location
|| !printer
->location
||
7145 !strcasecmp(printer
->location
, location
)))
7148 * If HideImplicitMembers is enabled, see if this printer or class
7149 * is a member of an implicit class...
7152 if (ImplicitClasses
&& HideImplicitMembers
&&
7153 printer
->in_implicit_class
)
7157 * If a username is specified, see if it is allowed or denied
7161 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
7165 * Add the group separator as needed...
7169 ippAddSeparator(con
->response
);
7174 * Send the attributes...
7177 copy_printer_attrs(con
, printer
, ra
);
7181 cupsArrayDelete(ra
);
7183 con
->response
->request
.status
.status_code
= IPP_OK
;
7188 * 'get_subscription_attrs()' - Get subscription attributes.
7192 get_subscription_attrs(
7193 cupsd_client_t
*con
, /* I - Client connection */
7194 int sub_id
) /* I - Subscription ID */
7196 http_status_t status
; /* Policy status */
7197 cupsd_subscription_t
*sub
; /* Subscription */
7198 cups_array_t
*ra
; /* Requested attributes array */
7201 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7202 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7203 con
, con
->http
.fd
, sub_id
);
7206 * Is the subscription ID valid?
7209 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7212 * Bad subscription ID...
7215 send_ipp_status(con
, IPP_NOT_FOUND
,
7216 _("notify-subscription-id %d no good!"), sub_id
);
7224 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7226 con
, sub
->owner
)) != HTTP_OK
)
7228 send_http_error(con
, status
, sub
->dest
);
7233 * Copy the subscription attributes to the response using the
7234 * requested-attributes attribute that may be provided by the client.
7237 ra
= create_requested_array(con
->request
);
7239 copy_subscription_attrs(con
, sub
, ra
);
7241 cupsArrayDelete(ra
);
7243 con
->response
->request
.status
.status_code
= IPP_OK
;
7248 * 'get_subscriptions()' - Get subscriptions.
7252 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7253 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7255 http_status_t status
; /* Policy status */
7256 int count
; /* Number of subscriptions */
7257 int limit
; /* Limit */
7258 cupsd_subscription_t
*sub
; /* Subscription */
7259 cups_array_t
*ra
; /* Requested attributes array */
7260 ipp_attribute_t
*attr
; /* Attribute */
7261 cups_ptype_t dtype
; /* Destination type (printer/class) */
7262 char scheme
[HTTP_MAX_URI
],
7263 /* Scheme portion of URI */
7264 username
[HTTP_MAX_URI
],
7265 /* Username portion of URI */
7267 /* Host portion of URI */
7268 resource
[HTTP_MAX_URI
];
7269 /* Resource portion of URI */
7270 int port
; /* Port portion of URI */
7271 cupsd_job_t
*job
; /* Job pointer */
7272 cupsd_printer_t
*printer
; /* Printer */
7275 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7276 "get_subscriptions(con=%p[%d], uri=%s)",
7277 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
7280 * Is the destination valid?
7283 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7284 sizeof(scheme
), username
, sizeof(username
), host
,
7285 sizeof(host
), &port
, resource
, sizeof(resource
));
7287 if (!strcmp(resource
, "/") ||
7288 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7289 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7290 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7295 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7298 job
= cupsdFindJob(atoi(resource
+ 6));
7302 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
7307 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7313 send_ipp_status(con
, IPP_NOT_FOUND
,
7314 _("The printer or class was not found."));
7317 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7318 IPP_TAG_INTEGER
)) != NULL
)
7320 job
= cupsdFindJob(attr
->values
[0].integer
);
7324 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
7325 attr
->values
[0].integer
);
7336 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
7338 con
, NULL
)) != HTTP_OK
)
7340 send_http_error(con
, status
, printer
);
7345 * Copy the subscription attributes to the response using the
7346 * requested-attributes attribute that may be provided by the client.
7349 ra
= create_requested_array(con
->request
);
7351 if ((attr
= ippFindAttribute(con
->request
, "limit",
7352 IPP_TAG_INTEGER
)) != NULL
)
7353 limit
= attr
->values
[0].integer
;
7358 * See if we only want to see subscriptions for a specific user...
7361 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7362 IPP_TAG_BOOLEAN
)) != NULL
&&
7363 attr
->values
[0].boolean
)
7364 strlcpy(username
, get_username(con
), sizeof(username
));
7368 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7370 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7371 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7372 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
7374 ippAddSeparator(con
->response
);
7375 copy_subscription_attrs(con
, sub
, ra
);
7378 if (limit
&& count
>= limit
)
7382 cupsArrayDelete(ra
);
7385 con
->response
->request
.status
.status_code
= IPP_OK
;
7387 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7392 * 'get_username()' - Get the username associated with a request.
7395 static const char * /* O - Username */
7396 get_username(cupsd_client_t
*con
) /* I - Connection */
7398 ipp_attribute_t
*attr
; /* Attribute */
7401 if (con
->username
[0])
7402 return (con
->username
);
7403 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7404 IPP_TAG_NAME
)) != NULL
)
7405 return (attr
->values
[0].string
.text
);
7407 return ("anonymous");
7412 * 'hold_job()' - Hold a print job.
7416 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7417 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7419 ipp_attribute_t
*attr
, /* Current job-hold-until */
7420 *newattr
; /* New job-hold-until */
7421 int jobid
; /* Job ID */
7422 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7423 username
[HTTP_MAX_URI
], /* Username portion of URI */
7424 host
[HTTP_MAX_URI
], /* Host portion of URI */
7425 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7426 int port
; /* Port portion of URI */
7427 cupsd_job_t
*job
; /* Job information */
7430 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
7431 uri
->values
[0].string
.text
);
7434 * See if we have a job URI or a printer URI...
7437 if (!strcmp(uri
->name
, "printer-uri"))
7440 * Got a printer URI; see if we also have a job-id attribute...
7443 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7444 IPP_TAG_INTEGER
)) == NULL
)
7446 send_ipp_status(con
, IPP_BAD_REQUEST
,
7447 _("Got a printer-uri attribute but no job-id!"));
7451 jobid
= attr
->values
[0].integer
;
7456 * Got a job URI; parse it to get the job ID...
7459 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7460 sizeof(method
), username
, sizeof(username
), host
,
7461 sizeof(host
), &port
, resource
, sizeof(resource
));
7463 if (strncmp(resource
, "/jobs/", 6))
7469 send_ipp_status(con
, IPP_BAD_REQUEST
,
7470 _("Bad job-uri attribute \"%s\"!"),
7471 uri
->values
[0].string
.text
);
7475 jobid
= atoi(resource
+ 6);
7479 * See if the job exists...
7482 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7485 * Nope - return a "not found" error...
7488 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7493 * See if the job is owned by the requesting user...
7496 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7498 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7503 * Hold the job and return...
7508 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
7509 "Job held by user.");
7511 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
7512 IPP_TAG_KEYWORD
)) == NULL
)
7513 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7515 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7516 IPP_TAG_KEYWORD
)) == NULL
)
7517 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7522 * Free the old hold value and copy the new one over...
7525 _cupsStrFree(attr
->values
[0].string
.text
);
7529 attr
->value_tag
= newattr
->value_tag
;
7530 attr
->values
[0].string
.text
=
7531 _cupsStrAlloc(newattr
->values
[0].string
.text
);
7535 attr
->value_tag
= IPP_TAG_KEYWORD
;
7536 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
7540 * Hold job until specified time...
7543 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
7545 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
7546 "Job job-hold-until value changed by user.");
7549 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Held by \"%s\".", jobid
,
7552 con
->response
->request
.status
.status_code
= IPP_OK
;
7557 * 'move_job()' - Move a job to a new destination.
7561 move_job(cupsd_client_t
*con
, /* I - Client connection */
7562 ipp_attribute_t
*uri
) /* I - Job URI */
7564 http_status_t status
; /* Policy status */
7565 ipp_attribute_t
*attr
; /* Current attribute */
7566 int jobid
; /* Job ID */
7567 cupsd_job_t
*job
; /* Current job */
7568 const char *src
; /* Source printer/class */
7569 cups_ptype_t stype
, /* Source type (printer or class) */
7570 dtype
; /* Destination type (printer/class) */
7571 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7572 username
[HTTP_MAX_URI
], /* Username portion of URI */
7573 host
[HTTP_MAX_URI
], /* Host portion of URI */
7574 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7575 int port
; /* Port portion of URI */
7576 cupsd_printer_t
*sprinter
, /* Source printer */
7577 *dprinter
; /* Destination printer */
7580 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
7581 uri
->values
[0].string
.text
);
7584 * Get the new printer or class...
7587 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7588 IPP_TAG_URI
)) == NULL
)
7591 * Need job-printer-uri...
7594 send_ipp_status(con
, IPP_BAD_REQUEST
,
7595 _("job-printer-uri attribute missing!"));
7599 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7605 send_ipp_status(con
, IPP_NOT_FOUND
,
7606 _("The printer or class was not found."));
7611 * See if we have a job URI or a printer URI...
7614 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7615 sizeof(scheme
), username
, sizeof(username
), host
,
7616 sizeof(host
), &port
, resource
, sizeof(resource
));
7618 if (!strcmp(uri
->name
, "printer-uri"))
7621 * Got a printer URI; see if we also have a job-id attribute...
7624 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7625 IPP_TAG_INTEGER
)) == NULL
)
7631 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
7632 &sprinter
)) == NULL
)
7638 send_ipp_status(con
, IPP_NOT_FOUND
,
7639 _("The printer or class was not found."));
7648 * Otherwise, just move a single job...
7651 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7654 * Nope - return a "not found" error...
7657 send_ipp_status(con
, IPP_NOT_FOUND
,
7658 _("Job #%d does not exist!"), attr
->values
[0].integer
);
7664 * Job found, initialize source pointers...
7675 * Got a job URI; parse it to get the job ID...
7678 if (strncmp(resource
, "/jobs/", 6))
7684 send_ipp_status(con
, IPP_BAD_REQUEST
,
7685 _("Bad job-uri attribute \"%s\"!"),
7686 uri
->values
[0].string
.text
);
7691 * See if the job exists...
7694 jobid
= atoi(resource
+ 6);
7696 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7699 * Nope - return a "not found" error...
7702 send_ipp_status(con
, IPP_NOT_FOUND
,
7703 _("Job #%d does not exist!"), jobid
);
7709 * Job found, initialize source pointers...
7718 * Check the policy of the destination printer...
7721 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
7722 job
? job
->username
: NULL
)) != HTTP_OK
)
7724 send_http_error(con
, status
, dprinter
);
7729 * Now move the job or jobs...
7735 * See if the job has been completed...
7738 if (job
->state_value
> IPP_JOB_STOPPED
)
7741 * Return a "not-possible" error...
7744 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7745 _("Job #%d is finished and cannot be altered!"),
7751 * See if the job is owned by the requesting user...
7754 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7756 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7761 * Move the job to a different printer or class...
7764 cupsdMoveJob(job
, dprinter
);
7769 * Got the source printer, now look through the jobs...
7772 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
7774 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
7777 * See if the job is pointing at the source printer or has not been
7781 if (strcasecmp(job
->dest
, src
) ||
7782 job
->state_value
> IPP_JOB_STOPPED
)
7786 * See if the job can be moved by the requesting user...
7789 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7793 * Move the job to a different printer or class...
7796 cupsdMoveJob(job
, dprinter
);
7801 * Start jobs if possible...
7807 * Return with "everything is OK" status...
7810 con
->response
->request
.status
.status_code
= IPP_OK
;
7815 * 'ppd_parse_line()' - Parse a PPD default line.
7818 static int /* O - 0 on success, -1 on failure */
7819 ppd_parse_line(const char *line
, /* I - Line */
7820 char *option
, /* O - Option name */
7821 int olen
, /* I - Size of option name */
7822 char *choice
, /* O - Choice name */
7823 int clen
) /* I - Size of choice name */
7826 * Verify this is a default option line...
7829 if (strncmp(line
, "*Default", 8))
7833 * Read the option name...
7836 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
7846 * Skip everything else up to the colon (:)...
7849 while (*line
&& *line
!= ':')
7858 * Now grab the option choice, skipping leading whitespace...
7861 while (isspace(*line
& 255))
7864 for (clen
--; isalnum(*line
& 255); line
++)
7874 * Return with no errors...
7882 * 'print_job()' - Print a file to a printer or class.
7886 print_job(cupsd_client_t
*con
, /* I - Client connection */
7887 ipp_attribute_t
*uri
) /* I - Printer URI */
7889 ipp_attribute_t
*attr
; /* Current attribute */
7890 ipp_attribute_t
*format
; /* Document-format attribute */
7891 const char *default_format
; /* document-format-default value */
7892 cupsd_job_t
*job
; /* New job */
7893 char filename
[1024]; /* Job filename */
7894 mime_type_t
*filetype
; /* Type of file */
7895 char super
[MIME_MAX_SUPER
], /* Supertype of file */
7896 type
[MIME_MAX_TYPE
], /* Subtype of file */
7897 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7898 /* Textual name of mime type */
7899 cupsd_printer_t
*printer
; /* Printer data */
7900 struct stat fileinfo
; /* File information */
7901 int kbytes
; /* Size of file */
7902 int compression
; /* Document compression */
7905 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
7906 uri
->values
[0].string
.text
);
7909 * Validate print file attributes, for now just document-format and
7910 * compression (CUPS only supports "none" and "gzip")...
7913 compression
= CUPS_FILE_NONE
;
7915 if ((attr
= ippFindAttribute(con
->request
, "compression",
7916 IPP_TAG_KEYWORD
)) != NULL
)
7918 if (strcmp(attr
->values
[0].string
.text
, "none")
7920 && strcmp(attr
->values
[0].string
.text
, "gzip")
7921 #endif /* HAVE_LIBZ */
7924 send_ipp_status(con
, IPP_ATTRIBUTES
,
7925 _("Unsupported compression \"%s\"!"),
7926 attr
->values
[0].string
.text
);
7927 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7928 "compression", NULL
, attr
->values
[0].string
.text
);
7933 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7934 compression
= CUPS_FILE_GZIP
;
7935 #endif /* HAVE_LIBZ */
7939 * Do we have a file to print?
7944 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7949 * Is the destination valid?
7952 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
7958 send_ipp_status(con
, IPP_NOT_FOUND
,
7959 _("The printer or class was not found."));
7964 * Is it a format we support?
7967 if ((format
= ippFindAttribute(con
->request
, "document-format",
7968 IPP_TAG_MIMETYPE
)) != NULL
)
7971 * Grab format from client...
7974 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
7977 send_ipp_status(con
, IPP_BAD_REQUEST
,
7978 _("Could not scan type \"%s\"!"),
7979 format
->values
[0].string
.text
);
7983 else if ((default_format
= cupsGetOption("document-format",
7984 printer
->num_options
,
7985 printer
->options
)) != NULL
)
7988 * Use default document format...
7991 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
7993 send_ipp_status(con
, IPP_BAD_REQUEST
,
7994 _("Could not scan type \"%s\"!"),
8005 strcpy(super
, "application");
8006 strcpy(type
, "octet-stream");
8009 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8012 * Auto-type the file...
8015 ipp_attribute_t
*doc_name
; /* document-name attribute */
8018 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
8020 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8021 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8022 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8026 filetype
= mimeType(MimeDatabase
, super
, type
);
8029 filetype
= mimeType(MimeDatabase
, super
, type
);
8033 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8036 * Replace the document-format attribute value with the auto-typed or
8040 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8045 _cupsStrFree(format
->values
[0].string
.text
);
8047 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8050 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8051 "document-format", NULL
, mimetype
);
8055 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8056 _("Unsupported format \'%s/%s\'!"), super
, type
);
8057 cupsdLogMessage(CUPSD_LOG_INFO
,
8058 "Hint: Do you have the raw file printing rules enabled?");
8061 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8062 "document-format", NULL
, format
->values
[0].string
.text
);
8068 * Read any embedded job ticket info from PS files...
8071 if (!strcasecmp(filetype
->super
, "application") &&
8072 !strcasecmp(filetype
->type
, "postscript"))
8073 read_ps_job_ticket(con
);
8076 * Create the job object...
8079 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8082 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Adding job file of type %s/%s.",
8083 job
->id
, filetype
->super
, filetype
->type
);
8086 * Update quota data...
8089 if (stat(con
->filename
, &fileinfo
))
8092 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8094 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8096 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8097 IPP_TAG_INTEGER
)) != NULL
)
8098 attr
->values
[0].integer
+= kbytes
;
8101 * Add the job file...
8104 if (add_file(con
, job
, filetype
, compression
))
8107 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8109 rename(con
->filename
, filename
);
8110 cupsdClearString(&con
->filename
);
8113 * See if we need to add the ending sheet...
8116 if (cupsdTimeoutJob(job
))
8120 * Log and save the job...
8123 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
8124 job
->id
, job
->dest
, job
->username
);
8125 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] hold_until = %d", job
->id
,
8126 (int)job
->hold_until
);
8131 * Start the job if possible...
8139 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
8141 * This function only gets called when printing a single PostScript
8142 * file using the Print-Job operation. It doesn't work for Create-Job +
8143 * Send-File, since the job attributes need to be set at job creation
8144 * time for banners to work. The embedded PS job ticket stuff is here
8145 * only to allow the Windows printer driver for CUPS to pass in JCL
8146 * options and IPP attributes which otherwise would be lost.
8148 * The format of a PS job ticket is simple:
8150 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8152 * %cupsJobTicket: attr1=value1
8153 * %cupsJobTicket: attr2=value2
8155 * %cupsJobTicket: attrN=valueN
8157 * Job ticket lines must appear immediately after the first line that
8158 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
8159 * looking for job ticket info when it finds a line that does not begin
8160 * with "%cupsJobTicket:".
8162 * The maximum length of a job ticket line, including the prefix, is
8163 * 255 characters to conform with the Adobe DSC.
8165 * Read-only attributes are rejected with a notice to the error log in
8166 * case a malicious user tries anything. Since the job ticket is read
8167 * prior to attribute validation in print_job(), job ticket attributes
8168 * will go through the same validation as IPP attributes...
8172 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8174 cups_file_t
*fp
; /* File to read from */
8175 char line
[256]; /* Line data */
8176 int num_options
; /* Number of options */
8177 cups_option_t
*options
; /* Options */
8178 ipp_t
*ticket
; /* New attributes */
8179 ipp_attribute_t
*attr
, /* Current attribute */
8180 *attr2
, /* Job attribute */
8181 *prev2
; /* Previous job attribute */
8185 * First open the print file...
8188 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8190 cupsdLogMessage(CUPSD_LOG_ERROR
,
8191 "read_ps_job_ticket: Unable to open PostScript print file "
8198 * Skip the first line...
8201 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8203 cupsdLogMessage(CUPSD_LOG_ERROR
,
8204 "read_ps_job_ticket: Unable to read from PostScript print "
8211 if (strncmp(line
, "%!PS-Adobe-", 11))
8214 * Not a DSC-compliant file, so no job ticket info will be available...
8222 * Read job ticket info from the file...
8228 while (cupsFileGets(fp
, line
, sizeof(line
)))
8231 * Stop at the first non-ticket line...
8234 if (strncmp(line
, "%cupsJobTicket:", 15))
8238 * Add the options to the option array...
8241 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8245 * Done with the file; see if we have any options...
8250 if (num_options
== 0)
8254 * OK, convert the options to an attribute list, and apply them to
8259 cupsEncodeOptions(ticket
, num_options
, options
);
8262 * See what the user wants to change.
8265 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8267 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8270 if (!strcmp(attr
->name
, "job-originating-host-name") ||
8271 !strcmp(attr
->name
, "job-originating-user-name") ||
8272 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8273 !strcmp(attr
->name
, "job-k-octets") ||
8274 !strcmp(attr
->name
, "job-id") ||
8275 !strncmp(attr
->name
, "job-state", 9) ||
8276 !strncmp(attr
->name
, "time-at-", 8))
8277 continue; /* Read-only attrs */
8279 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8280 IPP_TAG_ZERO
)) != NULL
)
8283 * Some other value; first free the old value...
8286 if (con
->request
->attrs
== attr2
)
8288 con
->request
->attrs
= attr2
->next
;
8293 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8294 if (prev2
->next
== attr2
)
8296 prev2
->next
= attr2
->next
;
8301 if (con
->request
->last
== attr2
)
8302 con
->request
->last
= prev2
;
8304 _ippFreeAttr(attr2
);
8308 * Add new option by copying it...
8311 copy_attribute(con
->request
, attr
, 0);
8315 * Then free the attribute list and option array...
8319 cupsFreeOptions(num_options
, options
);
8324 * 'reject_jobs()' - Reject print jobs to a printer.
8328 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8329 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8331 http_status_t status
; /* Policy status */
8332 cups_ptype_t dtype
; /* Destination type (printer/class) */
8333 cupsd_printer_t
*printer
; /* Printer data */
8334 ipp_attribute_t
*attr
; /* printer-state-message text */
8337 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8338 con
->http
.fd
, uri
->values
[0].string
.text
);
8341 * Is the destination valid?
8344 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8350 send_ipp_status(con
, IPP_NOT_FOUND
,
8351 _("The printer or class was not found."));
8359 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8361 send_http_error(con
, status
, printer
);
8366 * Reject jobs sent to the printer...
8369 printer
->accepting
= 0;
8371 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8372 IPP_TAG_TEXT
)) == NULL
)
8373 strcpy(printer
->state_message
, "Rejecting Jobs");
8375 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8376 sizeof(printer
->state_message
));
8378 cupsdAddPrinterHistory(printer
);
8380 if (dtype
& CUPS_PRINTER_CLASS
)
8382 cupsdSaveAllClasses();
8384 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8385 printer
->name
, get_username(con
));
8389 cupsdSaveAllPrinters();
8391 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8392 printer
->name
, get_username(con
));
8396 * Everything was ok, so return OK status...
8399 con
->response
->request
.status
.status_code
= IPP_OK
;
8404 * 'release_job()' - Release a held print job.
8408 release_job(cupsd_client_t
*con
, /* I - Client connection */
8409 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8411 ipp_attribute_t
*attr
; /* Current attribute */
8412 int jobid
; /* Job ID */
8413 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8414 username
[HTTP_MAX_URI
], /* Username portion of URI */
8415 host
[HTTP_MAX_URI
], /* Host portion of URI */
8416 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8417 int port
; /* Port portion of URI */
8418 cupsd_job_t
*job
; /* Job information */
8421 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8422 con
->http
.fd
, uri
->values
[0].string
.text
);
8425 * See if we have a job URI or a printer URI...
8428 if (!strcmp(uri
->name
, "printer-uri"))
8431 * Got a printer URI; see if we also have a job-id attribute...
8434 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8435 IPP_TAG_INTEGER
)) == NULL
)
8437 send_ipp_status(con
, IPP_BAD_REQUEST
,
8438 _("Got a printer-uri attribute but no job-id!"));
8442 jobid
= attr
->values
[0].integer
;
8447 * Got a job URI; parse it to get the job ID...
8450 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8451 sizeof(method
), username
, sizeof(username
), host
,
8452 sizeof(host
), &port
, resource
, sizeof(resource
));
8454 if (strncmp(resource
, "/jobs/", 6))
8460 send_ipp_status(con
, IPP_BAD_REQUEST
,
8461 _("Bad job-uri attribute \"%s\"!"),
8462 uri
->values
[0].string
.text
);
8466 jobid
= atoi(resource
+ 6);
8470 * See if the job exists...
8473 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8476 * Nope - return a "not found" error...
8479 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8484 * See if job is "held"...
8487 if (job
->state_value
!= IPP_JOB_HELD
)
8490 * Nope - return a "not possible" error...
8493 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
8498 * See if the job is owned by the requesting user...
8501 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8503 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8508 * Reset the job-hold-until value to "no-hold"...
8511 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8512 IPP_TAG_KEYWORD
)) == NULL
)
8513 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8517 _cupsStrFree(attr
->values
[0].string
.text
);
8519 attr
->value_tag
= IPP_TAG_KEYWORD
;
8520 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
8522 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
8523 "Job job-hold-until value changed by user.");
8527 * Release the job and return...
8530 cupsdReleaseJob(job
);
8532 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
8533 "Job released by user.");
8535 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Released by \"%s\".", jobid
,
8538 con
->response
->request
.status
.status_code
= IPP_OK
;
8543 * 'renew_subscription()' - Renew an existing subscription...
8548 cupsd_client_t
*con
, /* I - Client connection */
8549 int sub_id
) /* I - Subscription ID */
8551 http_status_t status
; /* Policy status */
8552 cupsd_subscription_t
*sub
; /* Subscription */
8553 ipp_attribute_t
*lease
; /* notify-lease-duration */
8556 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8557 "renew_subscription(con=%p[%d], sub_id=%d)",
8558 con
, con
->http
.fd
, sub_id
);
8561 * Is the subscription ID valid?
8564 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8567 * Bad subscription ID...
8570 send_ipp_status(con
, IPP_NOT_FOUND
,
8571 _("notify-subscription-id %d no good!"), sub_id
);
8578 * Job subscriptions cannot be renewed...
8581 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8582 _("Job subscriptions cannot be renewed!"));
8590 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8592 con
, sub
->owner
)) != HTTP_OK
)
8594 send_http_error(con
, status
, sub
->dest
);
8599 * Renew the subscription...
8602 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8605 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8607 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8609 cupsdLogMessage(CUPSD_LOG_INFO
,
8610 "renew_subscription: Limiting notify-lease-duration to "
8613 sub
->lease
= MaxLeaseDuration
;
8616 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
8618 cupsdSaveAllSubscriptions();
8620 con
->response
->request
.status
.status_code
= IPP_OK
;
8622 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
8623 "notify-lease-duration", sub
->lease
);
8628 * 'restart_job()' - Restart an old print job.
8632 restart_job(cupsd_client_t
*con
, /* I - Client connection */
8633 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8635 ipp_attribute_t
*attr
; /* Current attribute */
8636 int jobid
; /* Job ID */
8637 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8638 username
[HTTP_MAX_URI
], /* Username portion of URI */
8639 host
[HTTP_MAX_URI
], /* Host portion of URI */
8640 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8641 int port
; /* Port portion of URI */
8642 cupsd_job_t
*job
; /* Job information */
8645 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
8646 con
->http
.fd
, uri
->values
[0].string
.text
);
8649 * See if we have a job URI or a printer URI...
8652 if (!strcmp(uri
->name
, "printer-uri"))
8655 * Got a printer URI; see if we also have a job-id attribute...
8658 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8659 IPP_TAG_INTEGER
)) == NULL
)
8661 send_ipp_status(con
, IPP_BAD_REQUEST
,
8662 _("Got a printer-uri attribute but no job-id!"));
8666 jobid
= attr
->values
[0].integer
;
8671 * Got a job URI; parse it to get the job ID...
8674 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8675 sizeof(method
), username
, sizeof(username
), host
,
8676 sizeof(host
), &port
, resource
, sizeof(resource
));
8678 if (strncmp(resource
, "/jobs/", 6))
8684 send_ipp_status(con
, IPP_BAD_REQUEST
,
8685 _("Bad job-uri attribute \"%s\"!"),
8686 uri
->values
[0].string
.text
);
8690 jobid
= atoi(resource
+ 6);
8694 * See if the job exists...
8697 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8700 * Nope - return a "not found" error...
8703 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8708 * See if job is in any of the "completed" states...
8711 if (job
->state_value
<= IPP_JOB_PROCESSING
)
8714 * Nope - return a "not possible" error...
8717 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
8723 * See if we have retained the job files...
8728 if (!job
->attrs
|| job
->num_files
== 0)
8731 * Nope - return a "not possible" error...
8734 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8735 _("Job #%d cannot be restarted - no files!"), jobid
);
8740 * See if the job is owned by the requesting user...
8743 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8745 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8750 * Restart the job and return...
8753 cupsdRestartJob(job
);
8755 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Restarted by \"%s\".", jobid
,
8758 con
->response
->request
.status
.status_code
= IPP_OK
;
8763 * 'save_auth_info()' - Save authentication information for a job.
8768 cupsd_client_t
*con
, /* I - Client connection */
8769 cupsd_job_t
*job
, /* I - Job */
8770 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
8772 int i
; /* Looping var */
8773 char filename
[1024]; /* Job authentication filename */
8774 cups_file_t
*fp
; /* Job authentication file */
8775 char line
[2048]; /* Line for file */
8776 cupsd_printer_t
*dest
; /* Destination printer/class */
8780 * This function saves the in-memory authentication information for
8781 * a job so that it can be used to authenticate with a remote host.
8782 * The information is stored in a file that is readable only by the
8783 * root user. The fields are Base-64 encoded, each on a separate line,
8784 * followed by random number (up to 1024) of newlines to limit the
8785 * amount of information that is exposed.
8787 * Because of the potential for exposing of authentication information,
8788 * this functionality is only enabled when running cupsd as root.
8790 * This caching only works for the Basic and BasicDigest authentication
8791 * types. Digest authentication cannot be cached this way, and in
8792 * the future Kerberos authentication may make all of this obsolete.
8794 * Authentication information is saved whenever an authenticated
8795 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
8798 * This information is deleted after a job is completed or canceled,
8799 * so reprints may require subsequent re-authentication.
8805 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
8809 * Create the authentication file and change permissions...
8812 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
8813 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
8815 cupsdLogMessage(CUPSD_LOG_ERROR
,
8816 "Unable to save authentication info to \"%s\" - %s",
8817 filename
, strerror(errno
));
8821 fchown(cupsFileNumber(fp
), 0, 0);
8822 fchmod(cupsFileNumber(fp
), 0400);
8824 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
8827 * Write 1 to 3 auth values...
8830 cupsdClearString(&job
->auth_username
);
8831 cupsdClearString(&job
->auth_domain
);
8832 cupsdClearString(&job
->auth_password
);
8834 for (i
= 0; i
< auth_info
->num_values
; i
++)
8836 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
8837 strlen(auth_info
->values
[i
].string
.text
));
8838 cupsFilePrintf(fp
, "%s\n", line
);
8840 if (!strcmp(dest
->auth_info_required
[i
], "username"))
8841 cupsdSetStringf(&job
->auth_username
, "CUPSD_AUTH_USERNAME=%s",
8842 auth_info
->values
[i
].string
.text
);
8843 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
8844 cupsdSetStringf(&job
->auth_domain
, "CUPSD_AUTH_DOMAIN=%s",
8845 auth_info
->values
[i
].string
.text
);
8846 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
8847 cupsdSetStringf(&job
->auth_password
, "CUPSD_AUTH_PASSWORD=%s",
8848 auth_info
->values
[i
].string
.text
);
8851 else if (con
->username
[0])
8854 * Write the authenticated username...
8857 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
8858 cupsFilePrintf(fp
, "%s\n", line
);
8860 cupsdSetStringf(&job
->auth_username
, "CUPSD_AUTH_USERNAME=%s", con
->username
);
8861 cupsdClearString(&job
->auth_domain
);
8864 * Write the authenticated password...
8867 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
8868 cupsFilePrintf(fp
, "%s\n", line
);
8870 cupsdSetStringf(&job
->auth_password
, "CUPSD_AUTH_PASSWORD=%s", con
->password
);
8874 * Write a random number of newlines to the end of the file...
8877 for (i
= (rand() % 1024); i
>= 0; i
--)
8878 cupsFilePutChar(fp
, '\n');
8881 * Close the file and return...
8886 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8887 if (con
->gss_have_creds
)
8888 save_krb5_creds(con
, job
);
8889 else if (job
->ccname
)
8890 cupsdClearString(&(job
->ccname
));
8891 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8895 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8897 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
8901 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
8902 cupsd_job_t
*job
) /* I - Job */
8904 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
8905 cupsdLogMessage(CUPSD_LOG_INFO
,
8906 "Sorry, your version of Kerberos does not support delegated "
8911 krb5_error_code error
; /* Kerberos error code */
8912 OM_uint32 major_status
, /* Major status code */
8913 minor_status
; /* Minor status code */
8914 krb5_principal principal
; /* Kerberos principal */
8919 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
8923 if (krb5_init_context
== NULL
)
8925 # endif /* __APPLE__ */
8928 * We MUST create a file-based cache because memory-based caches are
8929 * only valid for the current process/address space.
8931 * Due to various bugs/features in different versions of Kerberos, we
8932 * need either the krb5_cc_new_unique() function or Heimdal's version
8933 * of krb5_cc_gen_new() to create a new FILE: credential cache that
8934 * can be passed to the backend. These functions create a temporary
8935 * file (typically in /tmp) containing the cached credentials, which
8936 * are removed when we have successfully printed a job.
8939 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
8940 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
8941 &(job
->ccache
))) != 0)
8942 # else /* HAVE_HEIMDAL */
8943 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
8944 &(job
->ccache
))) != 0)
8945 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
8947 cupsdLogMessage(CUPSD_LOG_ERROR
,
8948 "Unable to create new credentials cache (%d/%s)",
8949 error
, strerror(errno
));
8954 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
8956 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to parse kerberos username (%d/%s)",
8957 error
, strerror(errno
));
8958 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8963 if ((error
= krb5_cc_initialize(KerberosContext
, job
->ccache
, principal
)))
8965 cupsdLogMessage(CUPSD_LOG_ERROR
,
8966 "Unable to initialize credentials cache (%d/%s)", error
,
8968 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8969 krb5_free_principal(KerberosContext
, principal
);
8974 krb5_free_principal(KerberosContext
, principal
);
8977 * Copy the user's credentials to the new cache file...
8980 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
8983 if (GSS_ERROR(major_status
))
8985 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
8986 "Unable to import client credentials cache");
8987 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8993 * Add the KRB5CCNAME environment variable to the job so that the
8994 * backend can use the credentials when printing.
8997 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
8998 krb5_cc_get_name(KerberosContext
, job
->ccache
));
9000 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] save_krb5_creds: %s", job
->id
,
9002 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
9004 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9008 * 'send_document()' - Send a file to a printer or class.
9012 send_document(cupsd_client_t
*con
, /* I - Client connection */
9013 ipp_attribute_t
*uri
) /* I - Printer URI */
9015 ipp_attribute_t
*attr
; /* Current attribute */
9016 ipp_attribute_t
*format
; /* Document-format attribute */
9017 const char *default_format
;/* document-format-default value */
9018 int jobid
; /* Job ID number */
9019 cupsd_job_t
*job
; /* Current job */
9020 char job_uri
[HTTP_MAX_URI
],
9022 method
[HTTP_MAX_URI
],
9023 /* Method portion of URI */
9024 username
[HTTP_MAX_URI
],
9025 /* Username portion of URI */
9027 /* Host portion of URI */
9028 resource
[HTTP_MAX_URI
];
9029 /* Resource portion of URI */
9030 int port
; /* Port portion of URI */
9031 mime_type_t
*filetype
; /* Type of file */
9032 char super
[MIME_MAX_SUPER
],
9033 /* Supertype of file */
9034 type
[MIME_MAX_TYPE
],
9035 /* Subtype of file */
9036 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9037 /* Textual name of mime type */
9038 char filename
[1024]; /* Job filename */
9039 cupsd_printer_t
*printer
; /* Current printer */
9040 struct stat fileinfo
; /* File information */
9041 int kbytes
; /* Size of file */
9042 int compression
; /* Type of compression */
9045 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9046 con
->http
.fd
, uri
->values
[0].string
.text
);
9049 * See if we have a job URI or a printer URI...
9052 if (!strcmp(uri
->name
, "printer-uri"))
9055 * Got a printer URI; see if we also have a job-id attribute...
9058 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9059 IPP_TAG_INTEGER
)) == NULL
)
9061 send_ipp_status(con
, IPP_BAD_REQUEST
,
9062 _("Got a printer-uri attribute but no job-id!"));
9066 jobid
= attr
->values
[0].integer
;
9071 * Got a job URI; parse it to get the job ID...
9074 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9075 sizeof(method
), username
, sizeof(username
), host
,
9076 sizeof(host
), &port
, resource
, sizeof(resource
));
9078 if (strncmp(resource
, "/jobs/", 6))
9084 send_ipp_status(con
, IPP_BAD_REQUEST
,
9085 _("Bad job-uri attribute \"%s\"!"),
9086 uri
->values
[0].string
.text
);
9090 jobid
= atoi(resource
+ 6);
9094 * See if the job exists...
9097 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9100 * Nope - return a "not found" error...
9103 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9107 printer
= cupsdFindDest(job
->dest
);
9110 * See if the job is owned by the requesting user...
9113 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9115 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9120 * OK, see if the client is sending the document compressed - CUPS
9121 * only supports "none" and "gzip".
9124 compression
= CUPS_FILE_NONE
;
9126 if ((attr
= ippFindAttribute(con
->request
, "compression",
9127 IPP_TAG_KEYWORD
)) != NULL
)
9129 if (strcmp(attr
->values
[0].string
.text
, "none")
9131 && strcmp(attr
->values
[0].string
.text
, "gzip")
9132 #endif /* HAVE_LIBZ */
9135 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
9136 attr
->values
[0].string
.text
);
9137 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9138 "compression", NULL
, attr
->values
[0].string
.text
);
9143 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9144 compression
= CUPS_FILE_GZIP
;
9145 #endif /* HAVE_LIBZ */
9149 * Do we have a file to print?
9154 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
9159 * Is it a format we support?
9162 if ((format
= ippFindAttribute(con
->request
, "document-format",
9163 IPP_TAG_MIMETYPE
)) != NULL
)
9166 * Grab format from client...
9169 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
9172 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9173 format
->values
[0].string
.text
);
9177 else if ((default_format
= cupsGetOption("document-format",
9178 printer
->num_options
,
9179 printer
->options
)) != NULL
)
9182 * Use default document format...
9185 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
9187 send_ipp_status(con
, IPP_BAD_REQUEST
,
9188 _("Could not scan type \"%s\"!"),
9196 * No document format attribute? Auto-type it!
9199 strcpy(super
, "application");
9200 strcpy(type
, "octet-stream");
9203 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9206 * Auto-type the file...
9209 ipp_attribute_t
*doc_name
; /* document-name attribute */
9212 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
9214 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9215 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9216 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9220 filetype
= mimeType(MimeDatabase
, super
, type
);
9223 filetype
= mimeType(MimeDatabase
, super
, type
);
9227 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
9230 * Replace the document-format attribute value with the auto-typed or
9234 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9239 _cupsStrFree(format
->values
[0].string
.text
);
9241 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
9244 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9245 "document-format", NULL
, mimetype
);
9249 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9250 _("Unsupported format \'%s/%s\'!"), super
, type
);
9251 cupsdLogMessage(CUPSD_LOG_INFO
,
9252 "Hint: Do you have the raw file printing rules enabled?");
9255 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9256 "document-format", NULL
, format
->values
[0].string
.text
);
9261 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9263 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9266 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9267 _("Unsupported format \'%s\'!"), mimetype
);
9269 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9270 "document-format", NULL
, mimetype
);
9275 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9276 "send_document: request file type is %s/%s.",
9277 filetype
->super
, filetype
->type
);
9280 * Add the file to the job...
9285 if (add_file(con
, job
, filetype
, compression
))
9288 if (stat(con
->filename
, &fileinfo
))
9291 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9293 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9295 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
9296 IPP_TAG_INTEGER
)) != NULL
)
9297 attr
->values
[0].integer
+= kbytes
;
9299 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
9301 rename(con
->filename
, filename
);
9303 cupsdClearString(&con
->filename
);
9305 cupsdLogMessage(CUPSD_LOG_INFO
,
9306 "File of type %s/%s queued in job #%d by \"%s\".",
9307 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
9310 * Start the job if this is the last document...
9313 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9314 IPP_TAG_BOOLEAN
)) != NULL
&&
9315 attr
->values
[0].boolean
)
9318 * See if we need to add the ending sheet...
9321 if (cupsdTimeoutJob(job
))
9324 if (job
->state_value
== IPP_JOB_STOPPED
)
9326 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9327 job
->state_value
= IPP_JOB_PENDING
;
9329 else if (job
->state_value
== IPP_JOB_HELD
)
9331 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9332 IPP_TAG_KEYWORD
)) == NULL
)
9333 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9335 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9337 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9338 job
->state_value
= IPP_JOB_PENDING
;
9345 * Start the job if possible... Since cupsdCheckJobs() can cancel a
9346 * job if it doesn't print, we need to re-find the job afterwards...
9353 job
= cupsdFindJob(jobid
);
9357 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9358 IPP_TAG_KEYWORD
)) == NULL
)
9359 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9361 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9363 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9364 job
->state_value
= IPP_JOB_HELD
;
9365 job
->hold_until
= time(NULL
) + 60;
9371 * Fill in the response info...
9374 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
9377 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9380 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9382 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9383 job
? job
->state_value
: IPP_JOB_CANCELED
);
9384 add_job_state_reasons(con
, job
);
9386 con
->response
->request
.status
.status_code
= IPP_OK
;
9391 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9396 cupsd_client_t
*con
, /* I - Client connection */
9397 http_status_t status
, /* I - HTTP status code */
9398 cupsd_printer_t
*printer
) /* I - Printer, if any */
9400 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
9401 ippOpString(con
->request
->request
.op
.operation_id
),
9402 httpStatus(status
));
9404 if (status
== HTTP_UNAUTHORIZED
&&
9405 printer
&& printer
->num_auth_info_required
> 0 &&
9406 !strcmp(printer
->auth_info_required
[0], "negotiate"))
9407 cupsdSendError(con
, status
, CUPSD_AUTH_NEGOTIATE
);
9410 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9411 cupsd_location_t
*auth
; /* Pointer to authentication element */
9414 if (printer
->type
& CUPS_PRINTER_CLASS
)
9415 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9417 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9419 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9420 auth
->type
== CUPSD_AUTH_NONE
)
9421 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
, IPP_PRINT_JOB
);
9423 cupsdSendError(con
, status
, auth
? auth
->type
: CUPSD_AUTH_NONE
);
9426 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
9428 ippDelete(con
->response
);
9429 con
->response
= NULL
;
9436 * 'send_ipp_status()' - Send a status back to the IPP client.
9440 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9441 ipp_status_t status
, /* I - IPP status code */
9442 const char *message
, /* I - Status message */
9443 ...) /* I - Additional args as needed */
9445 va_list ap
; /* Pointer to additional args */
9446 char formatted
[1024]; /* Formatted errror message */
9449 va_start(ap
, message
);
9450 vsnprintf(formatted
, sizeof(formatted
),
9451 _cupsLangString(con
->language
, message
), ap
);
9454 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9455 ippOpString(con
->request
->request
.op
.operation_id
),
9456 ippErrorString(status
), formatted
);
9458 con
->response
->request
.status
.status_code
= status
;
9460 if (ippFindAttribute(con
->response
, "attributes-charset",
9461 IPP_TAG_ZERO
) == NULL
)
9462 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9463 "attributes-charset", NULL
, DefaultCharset
);
9465 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9466 IPP_TAG_ZERO
) == NULL
)
9467 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9468 "attributes-natural-language", NULL
, DefaultLanguage
);
9470 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9471 "status-message", NULL
, formatted
);
9476 * 'set_default()' - Set the default destination...
9480 set_default(cupsd_client_t
*con
, /* I - Client connection */
9481 ipp_attribute_t
*uri
) /* I - Printer URI */
9483 http_status_t status
; /* Policy status */
9484 cups_ptype_t dtype
; /* Destination type (printer/class) */
9485 cupsd_printer_t
*printer
; /* Printer */
9488 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9489 con
->http
.fd
, uri
->values
[0].string
.text
);
9492 * Is the destination valid?
9495 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9501 send_ipp_status(con
, IPP_NOT_FOUND
,
9502 _("The printer or class was not found."));
9510 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9512 send_http_error(con
, status
, NULL
);
9517 * Set it as the default...
9520 DefaultPrinter
= printer
;
9522 cupsdSaveAllPrinters();
9523 cupsdSaveAllClasses();
9525 cupsdWritePrintcap();
9527 cupsdLogMessage(CUPSD_LOG_INFO
,
9528 "Default destination set to \"%s\" by \"%s\".",
9529 printer
->name
, get_username(con
));
9532 * Everything was ok, so return OK status...
9535 con
->response
->request
.status
.status_code
= IPP_OK
;
9540 * 'set_job_attrs()' - Set job attributes.
9544 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9545 ipp_attribute_t
*uri
) /* I - Job URI */
9547 ipp_attribute_t
*attr
, /* Current attribute */
9548 *attr2
; /* Job attribute */
9549 int jobid
; /* Job ID */
9550 cupsd_job_t
*job
; /* Current job */
9551 char method
[HTTP_MAX_URI
],
9552 /* Method portion of URI */
9553 username
[HTTP_MAX_URI
],
9554 /* Username portion of URI */
9556 /* Host portion of URI */
9557 resource
[HTTP_MAX_URI
];
9558 /* Resource portion of URI */
9559 int port
; /* Port portion of URI */
9560 int event
; /* Events? */
9563 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
9564 con
->http
.fd
, uri
->values
[0].string
.text
);
9567 * Start with "everything is OK" status...
9570 con
->response
->request
.status
.status_code
= IPP_OK
;
9573 * See if we have a job URI or a printer URI...
9576 if (!strcmp(uri
->name
, "printer-uri"))
9579 * Got a printer URI; see if we also have a job-id attribute...
9582 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9583 IPP_TAG_INTEGER
)) == NULL
)
9585 send_ipp_status(con
, IPP_BAD_REQUEST
,
9586 _("Got a printer-uri attribute but no job-id!"));
9590 jobid
= attr
->values
[0].integer
;
9595 * Got a job URI; parse it to get the job ID...
9598 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9599 sizeof(method
), username
, sizeof(username
), host
,
9600 sizeof(host
), &port
, resource
, sizeof(resource
));
9602 if (strncmp(resource
, "/jobs/", 6))
9608 send_ipp_status(con
, IPP_BAD_REQUEST
,
9609 _("Bad job-uri attribute \"%s\"!"),
9610 uri
->values
[0].string
.text
);
9614 jobid
= atoi(resource
+ 6);
9618 * See if the job exists...
9621 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9624 * Nope - return a "not found" error...
9627 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9632 * See if the job has been completed...
9635 if (job
->state_value
> IPP_JOB_STOPPED
)
9638 * Return a "not-possible" error...
9641 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9642 _("Job #%d is finished and cannot be altered!"), jobid
);
9647 * See if the job is owned by the requesting user...
9650 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9652 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9657 * See what the user wants to change.
9664 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9666 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
9669 if (!strcmp(attr
->name
, "attributes-charset") ||
9670 !strcmp(attr
->name
, "attributes-natural-language") ||
9671 !strcmp(attr
->name
, "document-compression") ||
9672 !strcmp(attr
->name
, "document-format") ||
9673 !strcmp(attr
->name
, "job-detailed-status-messages") ||
9674 !strcmp(attr
->name
, "job-document-access-errors") ||
9675 !strcmp(attr
->name
, "job-id") ||
9676 !strcmp(attr
->name
, "job-k-octets") ||
9677 !strcmp(attr
->name
, "job-originating-host-name") ||
9678 !strcmp(attr
->name
, "job-originating-user-name") ||
9679 !strcmp(attr
->name
, "job-printer-up-time") ||
9680 !strcmp(attr
->name
, "job-printer-uri") ||
9681 !strcmp(attr
->name
, "job-sheets") ||
9682 !strcmp(attr
->name
, "job-state-message") ||
9683 !strcmp(attr
->name
, "job-state-reasons") ||
9684 !strcmp(attr
->name
, "job-uri") ||
9685 !strcmp(attr
->name
, "number-of-documents") ||
9686 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
9687 !strcmp(attr
->name
, "output-device-assigned") ||
9688 !strncmp(attr
->name
, "date-time-at-", 13) ||
9689 !strncmp(attr
->name
, "job-impressions", 15) ||
9690 !strncmp(attr
->name
, "job-k-octets", 12) ||
9691 !strncmp(attr
->name
, "job-media-sheets", 16) ||
9692 !strncmp(attr
->name
, "time-at-", 8))
9698 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
9699 _("%s cannot be changed."), attr
->name
);
9701 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9702 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9707 if (!strcmp(attr
->name
, "job-priority"))
9710 * Change the job priority...
9713 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9715 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
9717 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9718 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9720 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
9722 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9723 _("Job is completed and cannot be changed."));
9726 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9728 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
9729 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
9730 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
9733 else if (!strcmp(attr
->name
, "job-state"))
9736 * Change the job state...
9739 if (attr
->value_tag
!= IPP_TAG_ENUM
)
9741 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
9743 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9744 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9748 switch (attr
->values
[0].integer
)
9750 case IPP_JOB_PENDING
:
9752 if (job
->state_value
> IPP_JOB_HELD
)
9754 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9755 _("Job state cannot be changed."));
9758 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9760 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
9761 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
9763 event
|= CUPSD_EVENT_JOB_STATE
;
9767 case IPP_JOB_PROCESSING
:
9768 case IPP_JOB_STOPPED
:
9769 if (job
->state_value
!= attr
->values
[0].integer
)
9771 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9772 _("Job state cannot be changed."));
9777 case IPP_JOB_CANCELED
:
9778 case IPP_JOB_ABORTED
:
9779 case IPP_JOB_COMPLETED
:
9780 if (job
->state_value
> IPP_JOB_PROCESSING
)
9782 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9783 _("Job state cannot be changed."));
9786 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9787 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
9792 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
9794 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9795 IPP_TAG_ZERO
)) != NULL
)
9798 * Some other value; first free the old value...
9801 if (job
->attrs
->prev
)
9802 job
->attrs
->prev
->next
= attr2
->next
;
9804 job
->attrs
->attrs
= attr2
->next
;
9806 if (job
->attrs
->last
== attr2
)
9807 job
->attrs
->last
= job
->attrs
->prev
;
9809 _ippFreeAttr(attr2
);
9812 * Then copy the attribute...
9815 copy_attribute(job
->attrs
, attr
, 0);
9818 * See if the job-name or job-hold-until is being changed.
9821 if (!strcmp(attr
->name
, "job-hold-until"))
9823 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
9825 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
9826 cupsdReleaseJob(job
);
9830 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
9833 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
9836 * Delete the attribute...
9839 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9840 IPP_TAG_ZERO
)) != NULL
)
9842 if (job
->attrs
->prev
)
9843 job
->attrs
->prev
->next
= attr2
->next
;
9845 job
->attrs
->attrs
= attr2
->next
;
9847 if (attr2
== job
->attrs
->last
)
9848 job
->attrs
->last
= job
->attrs
->prev
;
9850 _ippFreeAttr(attr2
);
9852 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9858 * Add new option by copying it...
9861 copy_attribute(job
->attrs
, attr
, 0);
9863 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9874 * Send events as needed...
9877 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
9878 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
, job
->printer
, job
,
9879 "Job priority changed by user.");
9881 if (event
& CUPSD_EVENT_JOB_STATE
)
9882 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
9883 job
->state_value
== IPP_JOB_HELD
?
9884 "Job held by user." : "Job restarted by user.");
9886 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
9887 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
9888 "Job options changed by user.");
9891 * Start jobs if possible...
9899 * 'set_printer_defaults()' - Set printer default options from a request.
9903 set_printer_defaults(
9904 cupsd_client_t
*con
, /* I - Client connection */
9905 cupsd_printer_t
*printer
) /* I - Printer */
9907 int i
; /* Looping var */
9908 ipp_attribute_t
*attr
; /* Current attribute */
9909 int namelen
; /* Length of attribute name */
9910 char name
[256], /* New attribute name */
9911 value
[256]; /* String version of integer attrs */
9914 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9917 * Skip non-printer attributes...
9920 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
9923 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
9925 if (!strcmp(attr
->name
, "job-sheets-default"))
9928 * Only allow keywords and names...
9931 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9935 * Only allow job-sheets-default to be set when running without a
9936 * system high classification level...
9942 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
9944 if (attr
->num_values
> 1)
9945 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
9947 cupsdSetString(&printer
->job_sheets
[1], "none");
9949 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
9951 cupsdFreePrinterUsers(printer
);
9953 printer
->deny_users
= 0;
9955 if (attr
->value_tag
== IPP_TAG_NAME
&&
9956 (attr
->num_values
> 1 ||
9957 strcmp(attr
->values
[0].string
.text
, "all")))
9959 for (i
= 0; i
< attr
->num_values
; i
++)
9960 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9963 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
9965 cupsdFreePrinterUsers(printer
);
9967 printer
->deny_users
= 1;
9969 if (attr
->value_tag
== IPP_TAG_NAME
&&
9970 (attr
->num_values
> 1 ||
9971 strcmp(attr
->values
[0].string
.text
, "none")))
9973 for (i
= 0; i
< attr
->num_values
; i
++)
9974 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9977 else if (!strcmp(attr
->name
, "job-quota-period"))
9979 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9982 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
9983 attr
->values
[0].integer
);
9984 cupsdFreeQuotas(printer
);
9986 printer
->quota_period
= attr
->values
[0].integer
;
9988 else if (!strcmp(attr
->name
, "job-k-limit"))
9990 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9993 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
9994 attr
->values
[0].integer
);
9995 cupsdFreeQuotas(printer
);
9997 printer
->k_limit
= attr
->values
[0].integer
;
9999 else if (!strcmp(attr
->name
, "job-page-limit"))
10001 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10004 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10005 attr
->values
[0].integer
);
10006 cupsdFreeQuotas(printer
);
10008 printer
->page_limit
= attr
->values
[0].integer
;
10010 else if (!strcmp(attr
->name
, "printer-op-policy"))
10012 cupsd_policy_t
*p
; /* Policy */
10015 if (attr
->value_tag
!= IPP_TAG_NAME
)
10018 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10020 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10021 "Setting printer-op-policy to \"%s\"...",
10022 attr
->values
[0].string
.text
);
10023 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10024 printer
->op_policy_ptr
= p
;
10028 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10029 _("Unknown printer-op-policy \"%s\"."),
10030 attr
->values
[0].string
.text
);
10034 else if (!strcmp(attr
->name
, "printer-error-policy"))
10036 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10039 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10040 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10041 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
10043 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10044 _("Unknown printer-error-policy \"%s\"."),
10045 attr
->values
[0].string
.text
);
10049 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10050 "Setting printer-error-policy to \"%s\"...",
10051 attr
->values
[0].string
.text
);
10052 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10056 * Skip any other non-default attributes...
10059 namelen
= strlen(attr
->name
);
10060 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10061 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10065 * OK, anything else must be a user-defined default...
10068 strlcpy(name
, attr
->name
, sizeof(name
));
10069 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10071 switch (attr
->value_tag
)
10073 case IPP_TAG_DELETEATTR
:
10074 printer
->num_options
= cupsRemoveOption(name
,
10075 printer
->num_options
,
10076 &(printer
->options
));
10077 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10078 "Deleting %s", attr
->name
);
10081 case IPP_TAG_NAME
:
10082 case IPP_TAG_KEYWORD
:
10084 printer
->num_options
= cupsAddOption(name
,
10085 attr
->values
[0].string
.text
,
10086 printer
->num_options
,
10087 &(printer
->options
));
10088 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10089 "Setting %s to \"%s\"...", attr
->name
,
10090 attr
->values
[0].string
.text
);
10093 case IPP_TAG_BOOLEAN
:
10094 printer
->num_options
= cupsAddOption(name
,
10095 attr
->values
[0].boolean
?
10097 printer
->num_options
,
10098 &(printer
->options
));
10099 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10100 "Setting %s to %s...", attr
->name
,
10101 attr
->values
[0].boolean
? "true" : "false");
10104 case IPP_TAG_INTEGER
:
10105 case IPP_TAG_ENUM
:
10106 sprintf(value
, "%d", attr
->values
[0].integer
);
10107 printer
->num_options
= cupsAddOption(name
, value
,
10108 printer
->num_options
,
10109 &(printer
->options
));
10110 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10111 "Setting %s to %s...", attr
->name
, value
);
10114 case IPP_TAG_RANGE
:
10115 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10116 attr
->values
[0].range
.upper
);
10117 printer
->num_options
= cupsAddOption(name
, value
,
10118 printer
->num_options
,
10119 &(printer
->options
));
10120 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10121 "Setting %s to %s...", attr
->name
, value
);
10124 case IPP_TAG_RESOLUTION
:
10125 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10126 attr
->values
[0].resolution
.yres
,
10127 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10129 printer
->num_options
= cupsAddOption(name
, value
,
10130 printer
->num_options
,
10131 &(printer
->options
));
10132 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10133 "Setting %s to %s...", attr
->name
, value
);
10137 /* Do nothing for other values */
10145 * 'start_printer()' - Start a printer.
10149 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10150 ipp_attribute_t
*uri
) /* I - Printer URI */
10152 http_status_t status
; /* Policy status */
10153 cups_ptype_t dtype
; /* Destination type (printer/class) */
10154 cupsd_printer_t
*printer
; /* Printer data */
10157 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10158 con
->http
.fd
, uri
->values
[0].string
.text
);
10161 * Is the destination valid?
10164 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10170 send_ipp_status(con
, IPP_NOT_FOUND
,
10171 _("The printer or class was not found."));
10179 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10181 send_http_error(con
, status
, printer
);
10186 * Start the printer...
10189 printer
->state_message
[0] = '\0';
10191 cupsdStartPrinter(printer
, 1);
10193 if (dtype
& CUPS_PRINTER_CLASS
)
10194 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10195 printer
->name
, get_username(con
));
10197 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10198 printer
->name
, get_username(con
));
10203 * Everything was ok, so return OK status...
10206 con
->response
->request
.status
.status_code
= IPP_OK
;
10211 * 'stop_printer()' - Stop a printer.
10215 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10216 ipp_attribute_t
*uri
) /* I - Printer URI */
10218 http_status_t status
; /* Policy status */
10219 cups_ptype_t dtype
; /* Destination type (printer/class) */
10220 cupsd_printer_t
*printer
; /* Printer data */
10221 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10224 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10225 con
->http
.fd
, uri
->values
[0].string
.text
);
10228 * Is the destination valid?
10231 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10237 send_ipp_status(con
, IPP_NOT_FOUND
,
10238 _("The printer or class was not found."));
10246 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10248 send_http_error(con
, status
, printer
);
10253 * Stop the printer...
10256 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10257 IPP_TAG_TEXT
)) == NULL
)
10258 strcpy(printer
->state_message
, "Paused");
10261 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10262 sizeof(printer
->state_message
));
10265 cupsdStopPrinter(printer
, 1);
10267 if (dtype
& CUPS_PRINTER_CLASS
)
10268 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10269 printer
->name
, get_username(con
));
10271 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10272 printer
->name
, get_username(con
));
10275 * Everything was ok, so return OK status...
10278 con
->response
->request
.status
.status_code
= IPP_OK
;
10283 * 'url_encode_attr()' - URL-encode a string attribute.
10287 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10288 char *buffer
,/* I - String buffer */
10289 int bufsize
)/* I - Size of buffer */
10291 int i
; /* Looping var */
10292 char *bufptr
, /* Pointer into buffer */
10293 *bufend
; /* End of buffer */
10296 strlcpy(buffer
, attr
->name
, bufsize
);
10297 bufptr
= buffer
+ strlen(buffer
);
10298 bufend
= buffer
+ bufsize
- 1;
10300 for (i
= 0; i
< attr
->num_values
; i
++)
10302 if (bufptr
>= bufend
)
10310 if (bufptr
>= bufend
)
10315 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
10316 bufptr
, bufend
- bufptr
+ 1);
10318 if (bufptr
>= bufend
)
10329 * 'url_encode_string()' - URL-encode a string.
10332 static char * /* O - End of string */
10333 url_encode_string(const char *s
, /* I - String */
10334 char *buffer
, /* I - String buffer */
10335 int bufsize
) /* I - Size of buffer */
10337 char *bufptr
, /* Pointer into buffer */
10338 *bufend
; /* End of buffer */
10339 static const char *hex
= "0123456789ABCDEF";
10344 bufend
= buffer
+ bufsize
- 1;
10346 while (*s
&& bufptr
< bufend
)
10348 if (*s
== ' ' || *s
== '%')
10350 if (bufptr
>= (bufend
- 2))
10354 *bufptr
++ = hex
[(*s
>> 4) & 15];
10355 *bufptr
++ = hex
[*s
& 15];
10359 else if (*s
== '\'' || *s
== '\\')
10361 if (bufptr
>= (bufend
- 1))
10378 * 'user_allowed()' - See if a user is allowed to print to a queue.
10381 static int /* O - 0 if not allowed, 1 if allowed */
10382 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
10383 const char *username
) /* I - Username */
10385 int i
; /* Looping var */
10386 struct passwd
*pw
; /* User password data */
10387 char baseuser
[256], /* Base username */
10388 *baseptr
; /* Pointer to "@" in base username */
10391 if (p
->num_users
== 0)
10394 if (!strcmp(username
, "root"))
10397 if (strchr(username
, '@'))
10400 * Strip @REALM for username check...
10403 strlcpy(baseuser
, username
, sizeof(baseuser
));
10405 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
10408 username
= baseuser
;
10411 pw
= getpwnam(username
);
10414 for (i
= 0; i
< p
->num_users
; i
++)
10416 if (p
->users
[i
][0] == '@')
10419 * Check group membership...
10422 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
10425 else if (!strcasecmp(username
, p
->users
[i
]))
10429 return ((i
< p
->num_users
) != p
->deny_users
);
10434 * 'validate_job()' - Validate printer options and destination.
10438 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10439 ipp_attribute_t
*uri
) /* I - Printer URI */
10441 http_status_t status
; /* Policy status */
10442 ipp_attribute_t
*attr
; /* Current attribute */
10443 ipp_attribute_t
*format
; /* Document-format attribute */
10444 cups_ptype_t dtype
; /* Destination type (printer/class) */
10445 char super
[MIME_MAX_SUPER
],
10446 /* Supertype of file */
10447 type
[MIME_MAX_TYPE
];
10448 /* Subtype of file */
10449 cupsd_printer_t
*printer
; /* Printer */
10452 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10453 con
->http
.fd
, uri
->values
[0].string
.text
);
10456 * OK, see if the client is sending the document compressed - CUPS
10457 * doesn't support compression yet...
10460 if ((attr
= ippFindAttribute(con
->request
, "compression",
10461 IPP_TAG_KEYWORD
)) != NULL
&&
10462 !strcmp(attr
->values
[0].string
.text
, "none"))
10464 send_ipp_status(con
, IPP_ATTRIBUTES
,
10465 _("Unsupported compression attribute %s!"),
10466 attr
->values
[0].string
.text
);
10467 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10468 "compression", NULL
, attr
->values
[0].string
.text
);
10473 * Is it a format we support?
10476 if ((format
= ippFindAttribute(con
->request
, "document-format",
10477 IPP_TAG_MIMETYPE
)) != NULL
)
10479 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
10482 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
10483 format
->values
[0].string
.text
);
10487 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10488 !mimeType(MimeDatabase
, super
, type
))
10490 cupsdLogMessage(CUPSD_LOG_INFO
,
10491 "Hint: Do you have the raw file printing rules enabled?");
10492 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10493 _("Unsupported format \"%s\"!"),
10494 format
->values
[0].string
.text
);
10495 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10496 "document-format", NULL
, format
->values
[0].string
.text
);
10502 * Is the destination valid?
10505 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10511 send_ipp_status(con
, IPP_NOT_FOUND
,
10512 _("The printer or class was not found."));
10520 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10522 send_http_error(con
, status
, printer
);
10527 * Everything was ok, so return OK status...
10530 con
->response
->request
.status
.status_code
= IPP_OK
;
10535 * 'validate_name()' - Make sure the printer name only contains valid chars.
10538 static int /* O - 0 if name is no good, 1 if good */
10539 validate_name(const char *name
) /* I - Name to check */
10541 const char *ptr
; /* Pointer into name */
10545 * Scan the whole name...
10548 for (ptr
= name
; *ptr
; ptr
++)
10549 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
10553 * All the characters are good; validate the length, too...
10556 return ((ptr
- name
) < 128);
10561 * 'validate_user()' - Validate the user for the request.
10564 static int /* O - 1 if permitted, 0 otherwise */
10565 validate_user(cupsd_job_t
*job
, /* I - Job */
10566 cupsd_client_t
*con
, /* I - Client connection */
10567 const char *owner
, /* I - Owner of job/resource */
10568 char *username
, /* O - Authenticated username */
10569 int userlen
) /* I - Length of username */
10571 cupsd_printer_t
*printer
; /* Printer for job */
10574 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
10575 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
10577 job
->id
, con
? con
->http
.fd
: 0,
10578 owner
? owner
: "(null)", username
, userlen
);
10581 * Validate input...
10584 if (!con
|| !owner
|| !username
|| userlen
<= 0)
10588 * Get the best authenticated username that is available.
10591 strlcpy(username
, get_username(con
), userlen
);
10594 * Check the username against the owner...
10597 printer
= cupsdFindDest(job
->dest
);
10599 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
10600 con
, owner
) == HTTP_OK
);
10605 * End of "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $".