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 * Make sure ColorSync is available...
2930 if (CMRegisterColorDevice
== NULL
)
2934 * Try opening the PPD file for this printer...
2937 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
2938 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
2942 * See if we have any profiles...
2945 for (num_profiles
= 0, attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
2947 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
2948 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
2950 if (attr
->value
[0] != '/')
2951 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
2954 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
2956 if (access(iccfile
, 0))
2963 * If we have profiles, add them...
2966 if (num_profiles
> 0)
2969 * Build the array of profiles...
2971 * Note: This calloc actually requests slightly more memory than needed.
2974 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
2976 cupsdLogMessage(CUPSD_LOG_ERROR
,
2977 "Unable to allocate memory for %d profiles!",
2983 profiles
->profileCount
= num_profiles
;
2984 languages
= _ppdGetLanguages(ppd
);
2986 for (profile
= profiles
->profiles
,
2987 attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
2989 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
2990 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
2993 * Add this profile...
2996 if (attr
->value
[0] != '/')
2997 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3000 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3002 if (access(iccfile
, 0))
3005 apple_init_profile(ppd
, languages
, profile
, _ppdHashName(attr
->spec
),
3006 attr
->spec
, attr
->text
[0] ? attr
->text
: attr
->spec
,
3012 _ppdFreeLanguages(languages
);
3014 else if ((cm_option
= ppdFindOption(ppd
, "ColorModel")) != NULL
)
3017 * Extract standard grayscale, RGB, or CMYK profiles...
3020 for (num_profiles
= 0, i
= cm_option
->num_choices
,
3021 cm_choice
= cm_option
->choices
;
3024 if (!strcmp(cm_choice
->choice
, "Gray") ||
3025 !strcmp(cm_choice
->choice
, "RGB") ||
3026 !strcmp(cm_choice
->choice
, "CMYK"))
3030 * Build the array of profiles...
3032 * Note: This calloc actually requests slightly more memory than needed.
3035 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3037 cupsdLogMessage(CUPSD_LOG_ERROR
,
3038 "Unable to allocate memory for %d profiles!",
3044 profiles
->profileCount
= num_profiles
;
3046 for (profile
= profiles
->profiles
, i
= cm_option
->num_choices
,
3047 cm_choice
= cm_option
->choices
;
3050 if (!strcmp(cm_choice
->choice
, "Gray"))
3052 apple_init_profile(ppd
, NULL
, profile
, _ppdHashName("Gray.."),
3053 "Gray", "Gray", NULL
);
3056 else if (!strcmp(cm_choice
->choice
, "RGB"))
3058 apple_init_profile(ppd
, NULL
, profile
, _ppdHashName("RGB.."),
3059 "RGB", "RGB", NULL
);
3062 else if (!strcmp(cm_choice
->choice
, "CMYK"))
3064 apple_init_profile(ppd
, NULL
, profile
, _ppdHashName("CMYK.."),
3065 "CMYK", "CMYK", NULL
);
3072 * Use the default colorspace...
3077 if ((profiles
= calloc(1, sizeof(CMDeviceProfileArray
))) == NULL
)
3079 cupsdLogMessage(CUPSD_LOG_ERROR
,
3080 "Unable to allocate memory for %d profiles!",
3086 profiles
->profileCount
= 1;
3088 switch (ppd
->colorspace
)
3091 apple_init_profile(ppd
, NULL
, profiles
->profiles
,
3092 _ppdHashName("Gray.."), "Gray", "Gray", NULL
);
3095 apple_init_profile(ppd
, NULL
, profiles
->profiles
,
3096 _ppdHashName("RGB.."), "RGB", "RGB", NULL
);
3099 apple_init_profile(ppd
, NULL
, profiles
->profiles
,
3100 _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL
);
3110 if (num_profiles
> 0)
3113 * Get the device ID hash and pathelogical name dictionary.
3116 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\"",
3119 id
= _ppdHashName(p
->name
);
3120 name
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3121 &kCFTypeDictionaryKeyCallBacks
,
3122 &kCFTypeDictionaryValueCallBacks
);
3124 CFDictionarySetValue(name
, CFSTR("en"),
3125 CFStringCreateWithCString(kCFAllocatorDefault
, p
->name
,
3126 kCFStringEncodingUTF8
));
3129 * Register the device with ColorSync...
3132 error
= CMRegisterColorDevice(cmPrinterDeviceClass
, id
, name
, &scope
);
3135 * Register the profiles...
3139 error
= CMSetDeviceFactoryProfiles(cmPrinterDeviceClass
, id
,
3140 profiles
->profiles
[0].profileID
,
3148 cupsdLogMessage(CUPSD_LOG_ERROR
,
3149 "Unable to register ICC color profiles for \"%s\" - %d",
3150 p
->name
, (int)error
);
3152 for (profile
= profiles
->profiles
;
3154 profile
++, num_profiles
--)
3155 CFRelease(profile
->profileName
);
3166 * 'apple_unregister_profiles()' - Remove color profiles for the specified
3171 apple_unregister_profiles(
3172 cupsd_printer_t
*p
) /* I - Printer */
3175 * Make sure ColorSync is available...
3178 if (CMUnregisterColorDevice
!= NULL
)
3179 CMUnregisterColorDevice(cmPrinterDeviceClass
, _ppdHashName(p
->name
));
3181 #endif /* __APPLE__ */
3184 * 'apply_printer_defaults()' - Apply printer default options to a job.
3188 apply_printer_defaults(
3189 cupsd_printer_t
*printer
, /* I - Printer */
3190 cupsd_job_t
*job
) /* I - Job */
3192 int i
, /* Looping var */
3193 num_options
; /* Number of default options */
3194 cups_option_t
*options
, /* Default options */
3195 *option
; /* Current option */
3199 * Collect all of the default options and add the missing ones to the
3203 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
3206 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
3208 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
3213 * Encode these options as attributes in the job object...
3216 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
3217 cupsFreeOptions(num_options
, options
);
3222 * 'authenticate_job()' - Set job authentication info.
3226 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
3227 ipp_attribute_t
*uri
) /* I - Job URI */
3229 ipp_attribute_t
*attr
, /* job-id attribute */
3230 *auth_info
; /* auth-info attribute */
3231 int jobid
; /* Job ID */
3232 cupsd_job_t
*job
; /* Current job */
3233 char method
[HTTP_MAX_URI
],
3234 /* Method portion of URI */
3235 username
[HTTP_MAX_URI
],
3236 /* Username portion of URI */
3238 /* Host portion of URI */
3239 resource
[HTTP_MAX_URI
];
3240 /* Resource portion of URI */
3241 int port
; /* Port portion of URI */
3244 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
3245 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
3248 * Start with "everything is OK" status...
3251 con
->response
->request
.status
.status_code
= IPP_OK
;
3254 * See if we have a job URI or a printer URI...
3257 if (!strcmp(uri
->name
, "printer-uri"))
3260 * Got a printer URI; see if we also have a job-id attribute...
3263 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3264 IPP_TAG_INTEGER
)) == NULL
)
3266 send_ipp_status(con
, IPP_BAD_REQUEST
,
3267 _("Got a printer-uri attribute but no job-id!"));
3271 jobid
= attr
->values
[0].integer
;
3276 * Got a job URI; parse it to get the job ID...
3279 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
3280 sizeof(method
), username
, sizeof(username
), host
,
3281 sizeof(host
), &port
, resource
, sizeof(resource
));
3283 if (strncmp(resource
, "/jobs/", 6))
3289 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
3290 uri
->values
[0].string
.text
);
3294 jobid
= atoi(resource
+ 6);
3298 * See if the job exists...
3301 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3304 * Nope - return a "not found" error...
3307 send_ipp_status(con
, IPP_NOT_FOUND
,
3308 _("Job #%d does not exist!"), jobid
);
3313 * See if the job has been completed...
3316 if (job
->state_value
!= IPP_JOB_HELD
)
3319 * Return a "not-possible" error...
3322 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3323 _("Job #%d is not held for authentication!"),
3329 * See if we have already authenticated...
3332 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3334 if (!con
->username
[0] && !auth_info
)
3336 cupsd_printer_t
*printer
; /* Job destination */
3340 * No auth data. If we need to authenticate via Kerberos, send a
3341 * HTTP auth challenge, otherwise just return an IPP error...
3344 printer
= cupsdFindDest(job
->dest
);
3346 if (printer
&& printer
->num_auth_info_required
> 0 &&
3347 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3348 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3350 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3351 _("No authentication information provided!"));
3356 * See if the job is owned by the requesting user...
3359 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3361 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3366 * Save the authentication information for this job...
3369 save_auth_info(con
, job
, auth_info
);
3372 * Reset the job-hold-until value to "no-hold"...
3375 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3376 IPP_TAG_KEYWORD
)) == NULL
)
3377 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3381 attr
->value_tag
= IPP_TAG_KEYWORD
;
3382 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
3386 * Release the job and return...
3389 cupsdReleaseJob(job
);
3391 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Authenticated by \"%s\".", jobid
,
3397 * 'cancel_all_jobs()' - Cancel all print jobs.
3401 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3402 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3404 http_status_t status
; /* Policy status */
3405 cups_ptype_t dtype
; /* Destination type */
3406 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3407 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3408 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3409 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3410 int port
; /* Port portion of URI */
3411 ipp_attribute_t
*attr
; /* Attribute in request */
3412 const char *username
; /* Username */
3413 int purge
; /* Purge? */
3414 cupsd_printer_t
*printer
; /* Printer */
3417 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3418 con
->http
.fd
, uri
->values
[0].string
.text
);
3421 * See if we have a printer URI...
3424 if (strcmp(uri
->name
, "printer-uri"))
3426 send_ipp_status(con
, IPP_BAD_REQUEST
,
3427 _("The printer-uri attribute is required!"));
3432 * Get the username (if any) for the jobs we want to cancel (only if
3433 * "my-jobs" is specified...
3436 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3437 IPP_TAG_BOOLEAN
)) != NULL
&&
3438 attr
->values
[0].boolean
)
3440 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3441 IPP_TAG_NAME
)) != NULL
)
3442 username
= attr
->values
[0].string
.text
;
3445 send_ipp_status(con
, IPP_BAD_REQUEST
,
3446 _("Missing requesting-user-name attribute!"));
3454 * Look for the "purge-jobs" attribute...
3457 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3458 IPP_TAG_BOOLEAN
)) != NULL
)
3459 purge
= attr
->values
[0].boolean
;
3464 * And if the destination is valid...
3467 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3473 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3474 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3475 hostname
, sizeof(hostname
), &port
,
3476 resource
, sizeof(resource
));
3478 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3479 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3481 send_ipp_status(con
, IPP_NOT_FOUND
,
3482 _("The printer or class was not found."));
3490 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3492 send_http_error(con
, status
, NULL
);
3497 * Cancel all jobs on all printers...
3500 cupsdCancelJobs(NULL
, username
, purge
);
3502 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3503 purge
? "purged" : "canceled", get_username(con
));
3511 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3514 send_http_error(con
, status
, printer
);
3519 * Cancel all of the jobs on the named printer...
3522 cupsdCancelJobs(printer
->name
, username
, purge
);
3524 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3525 printer
->name
, purge
? "purged" : "canceled",
3529 con
->response
->request
.status
.status_code
= IPP_OK
;
3534 * 'cancel_job()' - Cancel a print job.
3538 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3539 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3541 ipp_attribute_t
*attr
; /* Current attribute */
3542 int jobid
; /* Job ID */
3543 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3544 username
[HTTP_MAX_URI
], /* Username portion of URI */
3545 host
[HTTP_MAX_URI
], /* Host portion of URI */
3546 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3547 int port
; /* Port portion of URI */
3548 cupsd_job_t
*job
; /* Job information */
3549 cups_ptype_t dtype
; /* Destination type (printer/class) */
3550 cupsd_printer_t
*printer
; /* Printer data */
3551 int purge
; /* Purge the job? */
3554 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3555 con
->http
.fd
, uri
->values
[0].string
.text
);
3558 * See if we have a job URI or a printer URI...
3561 if (!strcmp(uri
->name
, "printer-uri"))
3564 * Got a printer URI; see if we also have a job-id attribute...
3567 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3568 IPP_TAG_INTEGER
)) == NULL
)
3570 send_ipp_status(con
, IPP_BAD_REQUEST
,
3571 _("Got a printer-uri attribute but no job-id!"));
3575 if ((jobid
= attr
->values
[0].integer
) == 0)
3578 * Find the current job on the specified printer...
3581 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3587 send_ipp_status(con
, IPP_NOT_FOUND
,
3588 _("The printer or class was not found."));
3593 * See if the printer is currently printing a job...
3597 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3601 * No, see if there are any pending jobs...
3604 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3606 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3607 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3608 !strcasecmp(job
->dest
, printer
->name
))
3615 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3617 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3618 if (job
->state_value
== IPP_JOB_STOPPED
&&
3619 !strcasecmp(job
->dest
, printer
->name
))
3626 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3637 * Got a job URI; parse it to get the job ID...
3640 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3641 sizeof(scheme
), username
, sizeof(username
), host
,
3642 sizeof(host
), &port
, resource
, sizeof(resource
));
3644 if (strncmp(resource
, "/jobs/", 6))
3650 send_ipp_status(con
, IPP_BAD_REQUEST
,
3651 _("Bad job-uri attribute \"%s\"!"),
3652 uri
->values
[0].string
.text
);
3656 jobid
= atoi(resource
+ 6);
3660 * Look for the "purge-job" attribute...
3663 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3664 IPP_TAG_BOOLEAN
)) != NULL
)
3665 purge
= attr
->values
[0].boolean
;
3670 * See if the job exists...
3673 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3676 * Nope - return a "not found" error...
3679 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3684 * See if the job is owned by the requesting user...
3687 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3689 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3694 * See if the job is already completed, canceled, or aborted; if so,
3695 * we can't cancel...
3698 if (job
->state_value
>= IPP_JOB_CANCELED
&& !purge
)
3700 switch (job
->state_value
)
3702 case IPP_JOB_CANCELED
:
3703 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3704 _("Job #%d is already canceled - can\'t cancel."),
3708 case IPP_JOB_ABORTED
:
3709 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3710 _("Job #%d is already aborted - can\'t cancel."),
3715 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3716 _("Job #%d is already completed - can\'t cancel."),
3725 * Cancel the job and return...
3728 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
3732 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
3735 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3738 con
->response
->request
.status
.status_code
= IPP_OK
;
3743 * 'cancel_subscription()' - Cancel a subscription.
3747 cancel_subscription(
3748 cupsd_client_t
*con
, /* I - Client connection */
3749 int sub_id
) /* I - Subscription ID */
3751 http_status_t status
; /* Policy status */
3752 cupsd_subscription_t
*sub
; /* Subscription */
3755 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3756 "cancel_subscription(con=%p[%d], sub_id=%d)",
3757 con
, con
->http
.fd
, sub_id
);
3760 * Is the subscription ID valid?
3763 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3766 * Bad subscription ID...
3769 send_ipp_status(con
, IPP_NOT_FOUND
,
3770 _("notify-subscription-id %d no good!"), sub_id
);
3778 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3780 con
, sub
->owner
)) != HTTP_OK
)
3782 send_http_error(con
, status
, sub
->dest
);
3787 * Cancel the subscription...
3790 cupsdDeleteSubscription(sub
, 1);
3792 con
->response
->request
.status
.status_code
= IPP_OK
;
3797 * 'check_quotas()' - Check quotas for a printer and user.
3800 static int /* O - 1 if OK, 0 if not */
3801 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3802 cupsd_printer_t
*p
) /* I - Printer or class */
3804 int i
; /* Looping var */
3805 char username
[33]; /* Username */
3806 cupsd_quota_t
*q
; /* Quota data */
3807 #ifdef HAVE_MBR_UID_TO_UUID
3809 * Use Apple membership APIs which require that all names represent
3810 * valid user account or group records accessible by the server.
3813 uuid_t usr_uuid
; /* UUID for job requesting user */
3814 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3815 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3816 int mbr_err
; /* Error from membership function */
3817 int is_member
; /* Is this user a member? */
3820 * Use standard POSIX APIs for checking users and groups...
3823 struct passwd
*pw
; /* User password data */
3824 #endif /* HAVE_MBR_UID_TO_UUID */
3827 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3828 con
, con
->http
.fd
, p
, p
->name
);
3831 * Figure out who is printing...
3834 strlcpy(username
, get_username(con
), sizeof(username
));
3837 * Check global active job limits for printers and users...
3840 if (MaxJobsPerPrinter
)
3843 * Check if there are too many pending jobs on this printer...
3846 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3848 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3857 * Check if there are too many pending jobs for this user...
3860 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3862 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3869 * Check against users...
3872 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3877 #ifdef HAVE_MBR_UID_TO_UUID
3879 * Get UUID for job requesting user...
3882 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3888 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3889 "check_quotas: UUID lookup failed for user \"%s\"",
3891 cupsdLogMessage(CUPSD_LOG_INFO
,
3892 "Denying user \"%s\" access to printer \"%s\" "
3893 "(unknown user)...",
3899 * Get UID and GID of requesting user...
3902 pw
= getpwnam(username
);
3904 #endif /* HAVE_MBR_UID_TO_UUID */
3906 for (i
= 0; i
< p
->num_users
; i
++)
3907 if (p
->users
[i
][0] == '@')
3910 * Check group membership...
3913 #ifdef HAVE_MBR_UID_TO_UUID
3914 if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
3918 * Invalid ACL entries are ignored for matching; just record a
3919 * warning in the log...
3922 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3923 "check_quotas: UUID lookup failed for ACL entry "
3924 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3925 cupsdLogMessage(CUPSD_LOG_WARN
,
3926 "Access control entry \"%s\" not a valid group name; "
3927 "entry ignored", p
->users
[i
]);
3931 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3935 * At this point, there should be no errors, but check anyways...
3938 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3939 "check_quotas: group \"%s\" membership check "
3940 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
3945 * Stop if we found a match...
3952 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3954 #endif /* HAVE_MBR_UID_TO_UUID */
3956 #ifdef HAVE_MBR_UID_TO_UUID
3959 if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
3963 * Invalid ACL entries are ignored for matching; just record a
3964 * warning in the log...
3967 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3968 "check_quotas: UUID lookup failed for ACL entry "
3969 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3970 cupsdLogMessage(CUPSD_LOG_WARN
,
3971 "Access control entry \"%s\" not a valid user name; "
3972 "entry ignored", p
->users
[i
]);
3976 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
3979 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3980 "check_quotas: User \"%s\" identity check failed "
3981 "(err=%d)", p
->users
[i
], mbr_err
);
3990 else if (!strcasecmp(username
, p
->users
[i
]))
3992 #endif /* HAVE_MBR_UID_TO_UUID */
3994 if ((i
< p
->num_users
) == p
->deny_users
)
3996 cupsdLogMessage(CUPSD_LOG_INFO
,
3997 "Denying user \"%s\" access to printer \"%s\"...",
4008 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
4011 * TODO: Define these special page count values as constants!
4014 if (q
->page_count
== -4) /* special case: unlimited user */
4016 cupsdLogMessage(CUPSD_LOG_INFO
,
4017 "User \"%s\" request approved for printer %s (%s): "
4019 username
, p
->name
, p
->info
);
4020 q
->page_count
= 0; /* allow user to print */
4023 else if (q
->page_count
== -3) /* quota exceeded */
4025 cupsdLogMessage(CUPSD_LOG_INFO
,
4026 "User \"%s\" request denied for printer %s (%s): "
4027 "quota limit exceeded.",
4028 username
, p
->name
, p
->info
);
4029 q
->page_count
= 2; /* force quota exceeded failure */
4032 else if (q
->page_count
== -2) /* quota disabled for user */
4034 cupsdLogMessage(CUPSD_LOG_INFO
,
4035 "User \"%s\" request denied for printer %s (%s): "
4036 "printing disabled for user.",
4037 username
, p
->name
, p
->info
);
4038 q
->page_count
= 2; /* force quota exceeded failure */
4041 else if (q
->page_count
== -1) /* quota access error */
4043 cupsdLogMessage(CUPSD_LOG_INFO
,
4044 "User \"%s\" request denied for printer %s (%s): "
4045 "unable to determine quota limit.",
4046 username
, p
->name
, p
->info
);
4047 q
->page_count
= 2; /* force quota exceeded failure */
4050 else if (q
->page_count
< 0) /* user not found or other error */
4052 cupsdLogMessage(CUPSD_LOG_INFO
,
4053 "User \"%s\" request denied for printer %s (%s): "
4054 "user disabled / missing quota.",
4055 username
, p
->name
, p
->info
);
4056 q
->page_count
= 2; /* force quota exceeded failure */
4059 else /* page within user limits */
4061 q
->page_count
= 0; /* allow user to print */
4066 #endif /* __APPLE__ */
4067 if (p
->k_limit
|| p
->page_limit
)
4069 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
4071 cupsdLogMessage(CUPSD_LOG_ERROR
,
4072 "Unable to allocate quota data for user \"%s\"!",
4077 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
4078 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
4080 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
4087 * If we have gotten this far, we're done!
4095 * 'copy_attribute()' - Copy a single attribute.
4098 static ipp_attribute_t
* /* O - New attribute */
4100 ipp_t
*to
, /* O - Destination request/response */
4101 ipp_attribute_t
*attr
, /* I - Attribute to copy */
4102 int quickcopy
) /* I - Do a quick copy? */
4104 int i
; /* Looping var */
4105 ipp_attribute_t
*toattr
; /* Destination attribute */
4108 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4109 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
4110 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
4113 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
4116 toattr
= ippAddSeparator(to
);
4119 case IPP_TAG_INTEGER
:
4121 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4122 attr
->name
, attr
->num_values
, NULL
);
4124 for (i
= 0; i
< attr
->num_values
; i
++)
4125 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
4128 case IPP_TAG_BOOLEAN
:
4129 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
4130 attr
->num_values
, NULL
);
4132 for (i
= 0; i
< attr
->num_values
; i
++)
4133 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
4136 case IPP_TAG_STRING
:
4139 case IPP_TAG_KEYWORD
:
4141 case IPP_TAG_URISCHEME
:
4142 case IPP_TAG_CHARSET
:
4143 case IPP_TAG_LANGUAGE
:
4144 case IPP_TAG_MIMETYPE
:
4145 toattr
= ippAddStrings(to
, attr
->group_tag
,
4146 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4147 attr
->name
, attr
->num_values
, NULL
, NULL
);
4151 for (i
= 0; i
< attr
->num_values
; i
++)
4152 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4156 for (i
= 0; i
< attr
->num_values
; i
++)
4157 toattr
->values
[i
].string
.text
=
4158 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4163 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
4164 attr
->values
[0].date
);
4167 case IPP_TAG_RESOLUTION
:
4168 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
4169 attr
->num_values
, IPP_RES_PER_INCH
,
4172 for (i
= 0; i
< attr
->num_values
; i
++)
4174 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
4175 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
4176 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
4180 case IPP_TAG_RANGE
:
4181 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
4182 attr
->num_values
, NULL
, NULL
);
4184 for (i
= 0; i
< attr
->num_values
; i
++)
4186 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
4187 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
4191 case IPP_TAG_TEXTLANG
:
4192 case IPP_TAG_NAMELANG
:
4193 toattr
= ippAddStrings(to
, attr
->group_tag
,
4194 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4195 attr
->name
, attr
->num_values
, NULL
, NULL
);
4199 for (i
= 0; i
< attr
->num_values
; i
++)
4201 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
4202 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4207 for (i
= 0; i
< attr
->num_values
; i
++)
4210 toattr
->values
[i
].string
.charset
=
4211 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
4213 toattr
->values
[i
].string
.charset
=
4214 toattr
->values
[0].string
.charset
;
4216 toattr
->values
[i
].string
.text
=
4217 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4222 case IPP_TAG_BEGIN_COLLECTION
:
4223 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
4224 attr
->num_values
, NULL
);
4226 for (i
= 0; i
< attr
->num_values
; i
++)
4228 toattr
->values
[i
].collection
= ippNew();
4229 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
4230 NULL
, IPP_TAG_ZERO
, 0);
4235 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4236 attr
->name
, attr
->num_values
, NULL
);
4238 for (i
= 0; i
< attr
->num_values
; i
++)
4240 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
4242 if (toattr
->values
[i
].unknown
.length
> 0)
4244 if ((toattr
->values
[i
].unknown
.data
=
4245 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
4246 toattr
->values
[i
].unknown
.length
= 0;
4248 memcpy(toattr
->values
[i
].unknown
.data
,
4249 attr
->values
[i
].unknown
.data
,
4250 toattr
->values
[i
].unknown
.length
);
4253 break; /* anti-compiler-warning-code */
4261 * 'copy_attrs()' - Copy attributes from one request to another.
4265 copy_attrs(ipp_t
*to
, /* I - Destination request */
4266 ipp_t
*from
, /* I - Source request */
4267 cups_array_t
*ra
, /* I - Requested attributes */
4268 ipp_tag_t group
, /* I - Group to copy */
4269 int quickcopy
) /* I - Do a quick copy? */
4271 ipp_attribute_t
*fromattr
; /* Source attribute */
4274 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4275 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4276 to
, from
, ra
, group
, quickcopy
);
4281 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4284 * Filter attributes as needed...
4287 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4288 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4291 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4292 copy_attribute(to
, fromattr
, quickcopy
);
4298 * 'copy_banner()' - Copy a banner file to the requests directory for the
4302 static int /* O - Size of banner file in kbytes */
4303 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4304 cupsd_job_t
*job
, /* I - Job information */
4305 const char *name
) /* I - Name of banner */
4307 int i
; /* Looping var */
4308 int kbytes
; /* Size of banner file in kbytes */
4309 char filename
[1024]; /* Job filename */
4310 cupsd_banner_t
*banner
; /* Pointer to banner */
4311 cups_file_t
*in
; /* Input file */
4312 cups_file_t
*out
; /* Output file */
4313 int ch
; /* Character from file */
4314 char attrname
[255], /* Name of attribute */
4315 *s
; /* Pointer into name */
4316 ipp_attribute_t
*attr
; /* Attribute */
4319 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
4320 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
4321 name
? name
: "(null)");
4324 * Find the banner; return if not found or "none"...
4327 if (!name
|| !strcmp(name
, "none") ||
4328 (banner
= cupsdFindBanner(name
)) == NULL
)
4332 * Open the banner and job files...
4335 if (add_file(con
, job
, banner
->filetype
, 0))
4338 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4340 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4342 cupsdLogMessage(CUPSD_LOG_ERROR
,
4343 "copy_banner: Unable to create banner job file %s - %s",
4344 filename
, strerror(errno
));
4349 fchmod(cupsFileNumber(out
), 0640);
4350 fchown(cupsFileNumber(out
), RunUser
, Group
);
4353 * Try the localized banner file under the subdirectory...
4356 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4358 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4361 * Convert ll-cc to ll_CC...
4365 attrname
[3] = toupper(attrname
[3] & 255);
4366 attrname
[4] = toupper(attrname
[4] & 255);
4369 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4372 if (access(filename
, 0) && strlen(attrname
) > 2)
4375 * Wasn't able to find "ll_CC" locale file; try the non-national
4376 * localization banner directory.
4381 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4385 if (access(filename
, 0))
4388 * Use the non-localized banner file.
4391 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4394 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4398 cupsdLogMessage(CUPSD_LOG_ERROR
,
4399 "copy_banner: Unable to open banner template file %s - %s",
4400 filename
, strerror(errno
));
4406 * Parse the file to the end...
4409 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4413 * Get an attribute name...
4416 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4417 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4419 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4429 * Ignore { followed by stuff that is not an attribute name...
4432 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4437 * See if it is defined...
4440 if (attrname
[0] == '?')
4445 if (!strcmp(s
, "printer-name"))
4447 cupsFilePuts(out
, job
->dest
);
4450 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4453 * See if we have a leading question mark...
4456 if (attrname
[0] != '?')
4459 * Nope, write to file as-is; probably a PostScript procedure...
4462 cupsFilePrintf(out
, "{%s}", attrname
);
4469 * Output value(s)...
4472 for (i
= 0; i
< attr
->num_values
; i
++)
4475 cupsFilePutChar(out
, ',');
4477 switch (attr
->value_tag
)
4479 case IPP_TAG_INTEGER
:
4481 if (!strncmp(s
, "time-at-", 8))
4482 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
4484 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4487 case IPP_TAG_BOOLEAN
:
4488 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4491 case IPP_TAG_NOVALUE
:
4492 cupsFilePuts(out
, "novalue");
4495 case IPP_TAG_RANGE
:
4496 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4497 attr
->values
[i
].range
.upper
);
4500 case IPP_TAG_RESOLUTION
:
4501 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4502 attr
->values
[i
].resolution
.yres
,
4503 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4508 case IPP_TAG_STRING
:
4511 case IPP_TAG_KEYWORD
:
4512 case IPP_TAG_CHARSET
:
4513 case IPP_TAG_LANGUAGE
:
4514 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4517 * Need to quote strings for PS banners...
4522 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4524 if (*p
== '(' || *p
== ')' || *p
== '\\')
4526 cupsFilePutChar(out
, '\\');
4527 cupsFilePutChar(out
, *p
);
4529 else if (*p
< 32 || *p
> 126)
4530 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4532 cupsFilePutChar(out
, *p
);
4536 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4540 break; /* anti-compiler-warning-code */
4544 else if (ch
== '\\') /* Quoted char */
4546 ch
= cupsFileGetChar(in
);
4548 if (ch
!= '{') /* Only do special handling for \{ */
4549 cupsFilePutChar(out
, '\\');
4551 cupsFilePutChar(out
, ch
);
4554 cupsFilePutChar(out
, ch
);
4558 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4560 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4561 IPP_TAG_INTEGER
)) != NULL
)
4562 attr
->values
[0].integer
+= kbytes
;
4571 * 'copy_file()' - Copy a PPD file or interface script...
4574 static int /* O - 0 = success, -1 = error */
4575 copy_file(const char *from
, /* I - Source file */
4576 const char *to
) /* I - Destination file */
4578 cups_file_t
*src
, /* Source file */
4579 *dst
; /* Destination file */
4580 int bytes
; /* Bytes to read/write */
4581 char buffer
[2048]; /* Copy buffer */
4584 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4587 * Open the source and destination file for a copy...
4590 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4593 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4600 * Copy the source file to the destination...
4603 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4604 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
4612 * Close both files and return...
4617 return (cupsFileClose(dst
));
4622 * 'copy_model()' - Copy a PPD model file, substituting default values
4626 static int /* O - 0 = success, -1 = error */
4627 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4628 const char *from
, /* I - Source file */
4629 const char *to
) /* I - Destination file */
4631 fd_set input
; /* select() input set */
4632 struct timeval timeout
; /* select() timeout */
4633 int maxfd
; /* Max file descriptor for select() */
4634 char tempfile
[1024]; /* Temporary PPD file */
4635 int tempfd
; /* Temporary PPD file descriptor */
4636 int temppid
; /* Process ID of cups-driverd */
4637 int temppipe
[2]; /* Temporary pipes */
4638 char *argv
[4], /* Command-line arguments */
4639 *envp
[MAX_ENV
]; /* Environment */
4640 cups_file_t
*src
, /* Source file */
4641 *dst
; /* Destination file */
4642 ppd_file_t
*ppd
; /* PPD file */
4643 int bytes
, /* Bytes from pipe */
4644 total
; /* Total bytes from pipe */
4645 char buffer
[2048]; /* Copy buffer */
4646 int i
; /* Looping var */
4647 char option
[PPD_MAX_NAME
], /* Option name */
4648 choice
[PPD_MAX_NAME
]; /* Choice name */
4649 int num_defaults
; /* Number of default options */
4650 cups_option_t
*defaults
; /* Default options */
4651 char cups_protocol
[PPD_MAX_LINE
];
4652 /* cupsProtocol attribute */
4653 int have_letter
, /* Have Letter size */
4654 have_a4
; /* Have A4 size */
4655 #ifdef HAVE_LIBPAPER
4656 char *paper_result
; /* Paper size name from libpaper */
4657 char system_paper
[64]; /* Paper size name buffer */
4658 #endif /* HAVE_LIBPAPER */
4661 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4662 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4666 * Run cups-driverd to get the PPD file...
4669 argv
[0] = "cups-driverd";
4671 argv
[2] = (char *)from
;
4674 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4676 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4677 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
4678 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4682 cupsdOpenPipe(temppipe
);
4684 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4685 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4687 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4688 -1, -1, 0, DefaultProfile
, &temppid
))
4699 * Wait up to 30 seconds for the PPD file to be copied...
4704 if (temppipe
[0] > CGIPipes
[0])
4705 maxfd
= temppipe
[0] + 1;
4707 maxfd
= CGIPipes
[0] + 1;
4712 * See if we have data ready...
4718 FD_SET(temppipe
[0], &input
);
4719 FD_SET(CGIPipes
[0], &input
);
4721 timeout
.tv_sec
= 30;
4722 timeout
.tv_usec
= 0;
4724 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4734 * We have timed out...
4740 if (FD_ISSET(temppipe
[0], &input
))
4743 * Read the PPD file from the pipe, and write it to the PPD file.
4746 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4748 if (write(tempfd
, buffer
, bytes
) < bytes
)
4757 if (FD_ISSET(CGIPipes
[0], &input
))
4767 * No data from cups-deviced...
4770 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
4776 * Read the source file and see what page sizes are supported...
4779 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
4785 have_letter
= ppdPageSize(ppd
, "Letter") != NULL
;
4786 have_a4
= ppdPageSize(ppd
, "A4") != NULL
;
4789 * Open the destination (if possible) and set the default options...
4794 cups_protocol
[0] = '\0';
4796 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4799 * Read all of the default lines from the old PPD...
4802 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4803 if (!strncmp(buffer
, "*Default", 8))
4806 * Add the default option...
4809 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4810 choice
, sizeof(choice
)))
4812 ppd_option_t
*ppdo
; /* PPD option */
4816 * Only add the default if the default hasn't already been
4817 * set and the choice exists in the new PPD...
4820 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4821 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4822 ppdFindChoice(ppdo
, choice
))
4823 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4827 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4828 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4832 #ifdef HAVE_LIBPAPER
4833 else if ((paper_result
= systempapername()) != NULL
)
4836 * Set the default media sizes from the systemwide default...
4839 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
4840 system_paper
[0] = toupper(system_paper
[0] & 255);
4842 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
4843 (!strcmp(system_paper
, "A4") && have_a4
))
4845 num_defaults
= cupsAddOption("PageSize", system_paper
,
4846 num_defaults
, &defaults
);
4847 num_defaults
= cupsAddOption("PageRegion", system_paper
,
4848 num_defaults
, &defaults
);
4849 num_defaults
= cupsAddOption("PaperDimension", system_paper
,
4850 num_defaults
, &defaults
);
4851 num_defaults
= cupsAddOption("ImageableArea", system_paper
,
4852 num_defaults
, &defaults
);
4855 #endif /* HAVE_LIBPAPER */
4859 * Add the default media sizes...
4861 * Note: These values are generally not valid for large-format devices
4862 * like plotters, however it is probably safe to say that those
4863 * users will configure the media size after initially adding
4864 * the device anyways...
4867 if (!DefaultLanguage
||
4868 !strcasecmp(DefaultLanguage
, "C") ||
4869 !strcasecmp(DefaultLanguage
, "POSIX") ||
4870 !strcasecmp(DefaultLanguage
, "en") ||
4871 !strncasecmp(DefaultLanguage
, "en.", 3) ||
4872 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4873 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4874 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4877 * These are the only locales that will default to "letter" size...
4882 num_defaults
= cupsAddOption("PageSize", "Letter", num_defaults
,
4884 num_defaults
= cupsAddOption("PageRegion", "Letter", num_defaults
,
4886 num_defaults
= cupsAddOption("PaperDimension", "Letter", num_defaults
,
4888 num_defaults
= cupsAddOption("ImageableArea", "Letter", num_defaults
,
4895 * The rest default to "a4" size...
4898 num_defaults
= cupsAddOption("PageSize", "A4", num_defaults
,
4900 num_defaults
= cupsAddOption("PageRegion", "A4", num_defaults
,
4902 num_defaults
= cupsAddOption("PaperDimension", "A4", num_defaults
,
4904 num_defaults
= cupsAddOption("ImageableArea", "A4", num_defaults
,
4912 * Open the source file for a copy...
4915 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4917 cupsFreeOptions(num_defaults
, defaults
);
4923 * Open the destination file for a copy...
4926 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4928 cupsFreeOptions(num_defaults
, defaults
);
4935 * Copy the source file to the destination...
4938 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4940 if (!strncmp(buffer
, "*Default", 8))
4943 * Check for an previous default option choice...
4946 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4947 choice
, sizeof(choice
)))
4949 const char *val
; /* Default option value */
4952 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4955 * Substitute the previous choice...
4958 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4963 cupsFilePrintf(dst
, "%s\n", buffer
);
4966 if (cups_protocol
[0])
4967 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4969 cupsFreeOptions(num_defaults
, defaults
);
4972 * Close both files and return...
4979 return (cupsFileClose(dst
));
4984 * 'copy_job_attrs()' - Copy job attributes.
4988 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4989 cupsd_job_t
*job
, /* I - Job */
4990 cups_array_t
*ra
) /* I - Requested attributes array */
4992 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4996 * Send the requested attributes for each job...
4999 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
5000 con
->servername
, con
->serverport
, "/jobs/%d",
5003 if (!ra
|| cupsArrayFind(ra
, "document-count"))
5004 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5005 "document-count", job
->num_files
);
5007 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
5008 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5009 "job-more-info", NULL
, job_uri
);
5011 if (job
->state_value
> IPP_JOB_PROCESSING
&&
5012 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
5013 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
5014 job
->num_files
> 0);
5016 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
5017 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5018 "job-printer-up-time", time(NULL
));
5020 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
5021 add_job_state_reasons(con
, job
);
5023 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
5024 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5025 "job-uri", NULL
, job_uri
);
5027 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
5032 * 'copy_printer_attrs()' - Copy printer attributes.
5037 cupsd_client_t
*con
, /* I - Client connection */
5038 cupsd_printer_t
*printer
, /* I - Printer */
5039 cups_array_t
*ra
) /* I - Requested attributes array */
5041 char printer_uri
[HTTP_MAX_URI
];
5043 time_t curtime
; /* Current time */
5044 int i
; /* Looping var */
5045 ipp_attribute_t
*history
; /* History collection */
5049 * Copy the printer attributes to the response using requested-attributes
5050 * and document-format attributes that may be provided by the client.
5053 curtime
= time(NULL
);
5056 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
5057 printer
->recoverable
)
5058 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5059 "com.apple.print.recoverable-message", NULL
,
5060 printer
->recoverable
);
5061 #endif /* __APPLE__ */
5063 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
5064 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5065 "marker-change-time", printer
->marker_time
);
5067 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
5068 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
5069 "printer-alert", NULL
, printer
->alert
);
5071 if (printer
->alert_description
&&
5072 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
5073 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5074 "printer-alert-description", NULL
,
5075 printer
->alert_description
);
5077 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
5078 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
5079 ippTimeToDate(curtime
));
5082 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
5084 if (printer
->reg_name
)
5085 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5086 "printer-dns-sd-name", NULL
, printer
->reg_name
);
5088 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
5089 "printer-dns-sd-name", 0);
5091 #endif /* HAVE_DNSSD */
5093 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
5094 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5095 "printer-error-policy", NULL
, printer
->error_policy
);
5097 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
5098 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
5099 printer
->accepting
);
5101 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
5102 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
5105 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
5106 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5107 "printer-op-policy", NULL
, printer
->op_policy
);
5109 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
5110 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
5113 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
5114 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5115 "printer-state-change-time", printer
->state_time
);
5117 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
5118 cupsArrayFind(ra
, "printer-state-history"))
5121 * Printer history is only sent if specifically requested, so that
5122 * older CUPS/IPP clients won't barf on the collection attributes.
5125 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
5126 "printer-state-history",
5127 printer
->num_history
, NULL
);
5129 for (i
= 0; i
< printer
->num_history
; i
++)
5130 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
5131 NULL
, IPP_TAG_ZERO
, 0);
5134 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
5135 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5136 "printer-state-message", NULL
, printer
->state_message
);
5138 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
5139 add_printer_state_reasons(con
, printer
);
5141 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5143 int type
; /* printer-type value */
5147 * Add the CUPS-specific printer-type attribute...
5150 type
= printer
->type
;
5152 if (printer
== DefaultPrinter
)
5153 type
|= CUPS_PRINTER_DEFAULT
;
5155 if (!printer
->accepting
)
5156 type
|= CUPS_PRINTER_REJECTING
;
5158 if (!printer
->shared
)
5159 type
|= CUPS_PRINTER_NOT_SHARED
;
5161 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
5165 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5166 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5167 "printer-up-time", curtime
);
5169 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
5170 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
5173 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5174 "ipp", NULL
, con
->servername
, con
->serverport
,
5175 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5176 "/classes/%s" : "/printers/%s", printer
->name
);
5177 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5178 "printer-uri-supported", NULL
, printer_uri
);
5179 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5183 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5184 add_queued_job_count(con
, printer
);
5186 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
5187 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
5192 * 'copy_subscription_attrs()' - Copy subscription attributes.
5196 copy_subscription_attrs(
5197 cupsd_client_t
*con
, /* I - Client connection */
5198 cupsd_subscription_t
*sub
, /* I - Subscription */
5199 cups_array_t
*ra
) /* I - Requested attributes array */
5201 ipp_attribute_t
*attr
; /* Current attribute */
5202 char printer_uri
[HTTP_MAX_URI
];
5204 int count
; /* Number of events */
5205 unsigned mask
; /* Current event mask */
5206 const char *name
; /* Current event name */
5210 * Copy the subscription attributes to the response using the
5211 * requested-attributes attribute that may be provided by the client.
5214 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
5216 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5219 * Simple event list...
5222 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5223 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5224 "notify-events", NULL
, name
);
5229 * Complex event list...
5232 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5233 if (sub
->mask
& mask
)
5236 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5237 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5238 "notify-events", count
, NULL
, NULL
);
5240 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5241 if (sub
->mask
& mask
)
5243 attr
->values
[count
].string
.text
=
5244 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5251 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5252 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5253 "notify-job-id", sub
->job
->id
);
5255 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
5256 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5257 "notify-lease-duration", sub
->lease
);
5259 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5261 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5262 "ipp", NULL
, con
->servername
, con
->serverport
,
5263 "/printers/%s", sub
->dest
->name
);
5264 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5265 "notify-printer-uri", NULL
, printer_uri
);
5268 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
5269 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5270 "notify-recipient-uri", NULL
, sub
->recipient
);
5271 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
5272 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5273 "notify-pull-method", NULL
, "ippget");
5275 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
5276 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5277 "notify-subscriber-user-name", NULL
, sub
->owner
);
5279 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5280 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5281 "notify-subscription-id", sub
->id
);
5283 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
5284 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5285 "notify-time-interval", sub
->interval
);
5287 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5288 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5289 sub
->user_data
, sub
->user_data_len
);
5294 * 'create_job()' - Print a file to a printer or class.
5298 create_job(cupsd_client_t
*con
, /* I - Client connection */
5299 ipp_attribute_t
*uri
) /* I - Printer URI */
5301 cupsd_printer_t
*printer
; /* Printer */
5302 cupsd_job_t
*job
; /* New job */
5305 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5306 con
->http
.fd
, uri
->values
[0].string
.text
);
5309 * Is the destination valid?
5312 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5318 send_ipp_status(con
, IPP_NOT_FOUND
,
5319 _("The printer or class was not found."));
5324 * Create the job object...
5327 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5330 job
->pending_timeout
= 1;
5333 * Save and log the job...
5338 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
5339 job
->id
, job
->dest
, job
->username
);
5344 * 'create_requested_array()' - Create an array for the requested-attributes.
5347 static cups_array_t
* /* O - Array of attributes or NULL */
5348 create_requested_array(ipp_t
*request
) /* I - IPP request */
5350 int i
; /* Looping var */
5351 ipp_attribute_t
*requested
; /* requested-attributes attribute */
5352 cups_array_t
*ra
; /* Requested attributes array */
5353 char *value
; /* Current value */
5357 * Get the requested-attributes attribute, and return NULL if we don't
5361 if ((requested
= ippFindAttribute(request
, "requested-attributes",
5362 IPP_TAG_KEYWORD
)) == NULL
)
5366 * If the attribute contains a single "all" keyword, return NULL...
5369 if (requested
->num_values
== 1 &&
5370 !strcmp(requested
->values
[0].string
.text
, "all"))
5374 * Create an array using "strcmp" as the comparison function...
5377 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
5379 for (i
= 0; i
< requested
->num_values
; i
++)
5381 value
= requested
->values
[i
].string
.text
;
5383 if (!strcmp(value
, "job-template"))
5385 cupsArrayAdd(ra
, "copies");
5386 cupsArrayAdd(ra
, "copies-default");
5387 cupsArrayAdd(ra
, "copies-supported");
5388 cupsArrayAdd(ra
, "finishings");
5389 cupsArrayAdd(ra
, "finishings-default");
5390 cupsArrayAdd(ra
, "finishings-supported");
5391 cupsArrayAdd(ra
, "job-hold-until");
5392 cupsArrayAdd(ra
, "job-hold-until-default");
5393 cupsArrayAdd(ra
, "job-hold-until-supported");
5394 cupsArrayAdd(ra
, "job-priority");
5395 cupsArrayAdd(ra
, "job-priority-default");
5396 cupsArrayAdd(ra
, "job-priority-supported");
5397 cupsArrayAdd(ra
, "job-sheets");
5398 cupsArrayAdd(ra
, "job-sheets-default");
5399 cupsArrayAdd(ra
, "job-sheets-supported");
5400 cupsArrayAdd(ra
, "media");
5401 cupsArrayAdd(ra
, "media-default");
5402 cupsArrayAdd(ra
, "media-supported");
5403 cupsArrayAdd(ra
, "multiple-document-handling");
5404 cupsArrayAdd(ra
, "multiple-document-handling-default");
5405 cupsArrayAdd(ra
, "multiple-document-handling-supported");
5406 cupsArrayAdd(ra
, "number-up");
5407 cupsArrayAdd(ra
, "number-up-default");
5408 cupsArrayAdd(ra
, "number-up-supported");
5409 cupsArrayAdd(ra
, "orientation-requested");
5410 cupsArrayAdd(ra
, "orientation-requested-default");
5411 cupsArrayAdd(ra
, "orientation-requested-supported");
5412 cupsArrayAdd(ra
, "page-ranges");
5413 cupsArrayAdd(ra
, "page-ranges-supported");
5414 cupsArrayAdd(ra
, "printer-resolution");
5415 cupsArrayAdd(ra
, "printer-resolution-default");
5416 cupsArrayAdd(ra
, "printer-resolution-supported");
5417 cupsArrayAdd(ra
, "print-quality");
5418 cupsArrayAdd(ra
, "print-quality-default");
5419 cupsArrayAdd(ra
, "print-quality-supported");
5420 cupsArrayAdd(ra
, "sides");
5421 cupsArrayAdd(ra
, "sides-default");
5422 cupsArrayAdd(ra
, "sides-supported");
5424 else if (!strcmp(value
, "job-description"))
5426 cupsArrayAdd(ra
, "date-time-at-completed");
5427 cupsArrayAdd(ra
, "date-time-at-creation");
5428 cupsArrayAdd(ra
, "date-time-at-processing");
5429 cupsArrayAdd(ra
, "job-detailed-status-message");
5430 cupsArrayAdd(ra
, "job-document-access-errors");
5431 cupsArrayAdd(ra
, "job-id");
5432 cupsArrayAdd(ra
, "job-impressions");
5433 cupsArrayAdd(ra
, "job-impressions-completed");
5434 cupsArrayAdd(ra
, "job-k-octets");
5435 cupsArrayAdd(ra
, "job-k-octets-processed");
5436 cupsArrayAdd(ra
, "job-media-sheets");
5437 cupsArrayAdd(ra
, "job-media-sheets-completed");
5438 cupsArrayAdd(ra
, "job-message-from-operator");
5439 cupsArrayAdd(ra
, "job-more-info");
5440 cupsArrayAdd(ra
, "job-name");
5441 cupsArrayAdd(ra
, "job-originating-user-name");
5442 cupsArrayAdd(ra
, "job-printer-up-time");
5443 cupsArrayAdd(ra
, "job-printer-uri");
5444 cupsArrayAdd(ra
, "job-state");
5445 cupsArrayAdd(ra
, "job-state-message");
5446 cupsArrayAdd(ra
, "job-state-reasons");
5447 cupsArrayAdd(ra
, "job-uri");
5448 cupsArrayAdd(ra
, "number-of-documents");
5449 cupsArrayAdd(ra
, "number-of-intervening-jobs");
5450 cupsArrayAdd(ra
, "output-device-assigned");
5451 cupsArrayAdd(ra
, "time-at-completed");
5452 cupsArrayAdd(ra
, "time-at-creation");
5453 cupsArrayAdd(ra
, "time-at-processing");
5455 else if (!strcmp(value
, "printer-description"))
5457 cupsArrayAdd(ra
, "charset-configured");
5458 cupsArrayAdd(ra
, "charset-supported");
5459 cupsArrayAdd(ra
, "color-supported");
5460 cupsArrayAdd(ra
, "compression-supported");
5461 cupsArrayAdd(ra
, "document-format-default");
5462 cupsArrayAdd(ra
, "document-format-supported");
5463 cupsArrayAdd(ra
, "generated-natural-language-supported");
5464 cupsArrayAdd(ra
, "ipp-versions-supported");
5465 cupsArrayAdd(ra
, "job-impressions-supported");
5466 cupsArrayAdd(ra
, "job-k-octets-supported");
5467 cupsArrayAdd(ra
, "job-media-sheets-supported");
5468 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
5469 cupsArrayAdd(ra
, "multiple-operation-time-out");
5470 cupsArrayAdd(ra
, "natural-language-configured");
5471 cupsArrayAdd(ra
, "notify-attributes-supported");
5472 cupsArrayAdd(ra
, "notify-lease-duration-default");
5473 cupsArrayAdd(ra
, "notify-lease-duration-supported");
5474 cupsArrayAdd(ra
, "notify-max-events-supported");
5475 cupsArrayAdd(ra
, "notify-events-default");
5476 cupsArrayAdd(ra
, "notify-events-supported");
5477 cupsArrayAdd(ra
, "notify-pull-method-supported");
5478 cupsArrayAdd(ra
, "notify-schemes-supported");
5479 cupsArrayAdd(ra
, "operations-supported");
5480 cupsArrayAdd(ra
, "pages-per-minute");
5481 cupsArrayAdd(ra
, "pages-per-minute-color");
5482 cupsArrayAdd(ra
, "pdl-override-supported");
5483 cupsArrayAdd(ra
, "printer-alert");
5484 cupsArrayAdd(ra
, "printer-alert-description");
5485 cupsArrayAdd(ra
, "printer-current-time");
5486 cupsArrayAdd(ra
, "printer-driver-installer");
5487 cupsArrayAdd(ra
, "printer-info");
5488 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
5489 cupsArrayAdd(ra
, "printer-location");
5490 cupsArrayAdd(ra
, "printer-make-and-model");
5491 cupsArrayAdd(ra
, "printer-message-from-operator");
5492 cupsArrayAdd(ra
, "printer-more-info");
5493 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
5494 cupsArrayAdd(ra
, "printer-name");
5495 cupsArrayAdd(ra
, "printer-state");
5496 cupsArrayAdd(ra
, "printer-state-message");
5497 cupsArrayAdd(ra
, "printer-state-reasons");
5498 cupsArrayAdd(ra
, "printer-up-time");
5499 cupsArrayAdd(ra
, "printer-uri-supported");
5500 cupsArrayAdd(ra
, "queued-job-count");
5501 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
5502 cupsArrayAdd(ra
, "uri-authentication-supported");
5503 cupsArrayAdd(ra
, "uri-security-supported");
5505 else if (!strcmp(value
, "printer-defaults"))
5507 char *name
; /* Option name */
5510 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5512 name
= (char *)cupsArrayNext(CommonDefaults
))
5513 cupsArrayAdd(ra
, name
);
5515 else if (!strcmp(value
, "subscription-template"))
5517 cupsArrayAdd(ra
, "notify-attributes");
5518 cupsArrayAdd(ra
, "notify-charset");
5519 cupsArrayAdd(ra
, "notify-events");
5520 cupsArrayAdd(ra
, "notify-lease-duration");
5521 cupsArrayAdd(ra
, "notify-natural-language");
5522 cupsArrayAdd(ra
, "notify-pull-method");
5523 cupsArrayAdd(ra
, "notify-recipient-uri");
5524 cupsArrayAdd(ra
, "notify-time-interval");
5525 cupsArrayAdd(ra
, "notify-user-data");
5528 cupsArrayAdd(ra
, value
);
5536 * 'create_subscription()' - Create a notification subscription.
5540 create_subscription(
5541 cupsd_client_t
*con
, /* I - Client connection */
5542 ipp_attribute_t
*uri
) /* I - Printer URI */
5544 http_status_t status
; /* Policy status */
5545 int i
; /* Looping var */
5546 ipp_attribute_t
*attr
; /* Current attribute */
5547 cups_ptype_t dtype
; /* Destination type (printer/class) */
5548 char scheme
[HTTP_MAX_URI
],
5549 /* Scheme portion of URI */
5550 userpass
[HTTP_MAX_URI
],
5551 /* Username portion of URI */
5553 /* Host portion of URI */
5554 resource
[HTTP_MAX_URI
];
5555 /* Resource portion of URI */
5556 int port
; /* Port portion of URI */
5557 cupsd_printer_t
*printer
; /* Printer/class */
5558 cupsd_job_t
*job
; /* Job */
5559 int jobid
; /* Job ID */
5560 cupsd_subscription_t
*sub
; /* Subscription object */
5561 const char *username
, /* requesting-user-name or
5562 authenticated username */
5563 *recipient
, /* notify-recipient-uri */
5564 *pullmethod
; /* notify-pull-method */
5565 ipp_attribute_t
*user_data
; /* notify-user-data */
5566 int interval
, /* notify-time-interval */
5567 lease
; /* notify-lease-duration */
5568 unsigned mask
; /* notify-events */
5569 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5570 *notify_lease
; /* notify-lease-duration(-default) */
5574 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5576 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5577 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
5578 attr
->value_tag
, attr
->name
);
5580 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
5585 * Is the destination valid?
5588 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5589 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5590 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5592 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5593 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5594 sizeof(host
), &port
, resource
, sizeof(resource
));
5596 if (!strcmp(resource
, "/"))
5598 dtype
= (cups_ptype_t
)0;
5601 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5603 dtype
= (cups_ptype_t
)0;
5606 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5608 dtype
= CUPS_PRINTER_CLASS
;
5611 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5617 send_ipp_status(con
, IPP_NOT_FOUND
,
5618 _("The printer or class was not found."));
5628 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5631 send_http_error(con
, status
, printer
);
5635 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5637 send_http_error(con
, status
, NULL
);
5642 * Get the user that is requesting the subscription...
5645 username
= get_username(con
);
5648 * Find the first subscription group attribute; return if we have
5652 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5653 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5658 send_ipp_status(con
, IPP_BAD_REQUEST
,
5659 _("No subscription attributes in request!"));
5664 * Process the subscription attributes in the request...
5667 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5675 lease
= DefaultLeaseDuration
;
5677 mask
= CUPSD_EVENT_NONE
;
5681 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5683 notify_lease
= ippFindAttribute(printer
->attrs
,
5684 "notify-lease-duration-default",
5688 lease
= notify_lease
->values
[0].integer
;
5692 notify_events
= NULL
;
5693 notify_lease
= NULL
;
5696 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5698 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5699 attr
->value_tag
== IPP_TAG_URI
)
5702 * Validate the recipient scheme against the ServerBin/notifier
5706 char notifier
[1024]; /* Notifier filename */
5709 recipient
= attr
->values
[0].string
.text
;
5711 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5712 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5713 host
, sizeof(host
), &port
,
5714 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5716 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5717 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
5718 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5719 "notify-status-code", IPP_URI_SCHEME
);
5723 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5725 if (access(notifier
, X_OK
))
5727 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5728 _("notify-recipient-uri URI \"%s\" uses unknown "
5729 "scheme!"), recipient
);
5730 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5731 "notify-status-code", IPP_URI_SCHEME
);
5735 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5736 attr
->value_tag
== IPP_TAG_KEYWORD
)
5738 pullmethod
= attr
->values
[0].string
.text
;
5740 if (strcmp(pullmethod
, "ippget"))
5742 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5743 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
5744 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5745 "notify-status-code", IPP_ATTRIBUTES
);
5749 else if (!strcmp(attr
->name
, "notify-charset") &&
5750 attr
->value_tag
== IPP_TAG_CHARSET
&&
5751 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5752 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5754 send_ipp_status(con
, IPP_CHARSET
,
5755 _("Character set \"%s\" not supported!"),
5756 attr
->values
[0].string
.text
);
5759 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5760 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5761 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5763 send_ipp_status(con
, IPP_CHARSET
,
5764 _("Language \"%s\" not supported!"),
5765 attr
->values
[0].string
.text
);
5768 else if (!strcmp(attr
->name
, "notify-user-data") &&
5769 attr
->value_tag
== IPP_TAG_STRING
)
5771 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5773 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5774 _("The notify-user-data value is too large "
5775 "(%d > 63 octets)!"),
5776 attr
->values
[0].unknown
.length
);
5782 else if (!strcmp(attr
->name
, "notify-events") &&
5783 attr
->value_tag
== IPP_TAG_KEYWORD
)
5784 notify_events
= attr
;
5785 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5786 attr
->value_tag
== IPP_TAG_INTEGER
)
5787 lease
= attr
->values
[0].integer
;
5788 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5789 attr
->value_tag
== IPP_TAG_INTEGER
)
5790 interval
= attr
->values
[0].integer
;
5791 else if (!strcmp(attr
->name
, "notify-job-id") &&
5792 attr
->value_tag
== IPP_TAG_INTEGER
)
5793 jobid
= attr
->values
[0].integer
;
5800 for (i
= 0; i
< notify_events
->num_values
; i
++)
5801 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5805 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5807 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5808 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5809 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5811 if (!recipient
&& !pullmethod
)
5814 if (mask
== CUPSD_EVENT_NONE
)
5817 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5819 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5822 send_ipp_status(con
, IPP_BAD_REQUEST
,
5823 _("notify-events not specified!"));
5828 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5830 cupsdLogMessage(CUPSD_LOG_INFO
,
5831 "create_subscription: Limiting notify-lease-duration to "
5834 lease
= MaxLeaseDuration
;
5839 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5841 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
5848 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
5851 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
5854 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5855 "Added subscription %d for printer \"%s\"",
5856 sub
->id
, printer
->name
);
5858 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
5861 sub
->interval
= interval
;
5863 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5865 cupsdSetString(&sub
->owner
, username
);
5869 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5870 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5871 sub
->user_data_len
);
5874 ippAddSeparator(con
->response
);
5875 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5876 "notify-subscription-id", sub
->id
);
5878 con
->response
->request
.status
.status_code
= IPP_OK
;
5884 cupsdSaveAllSubscriptions();
5890 * 'delete_printer()' - Remove a printer or class from the system.
5894 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5895 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5897 http_status_t status
; /* Policy status */
5898 cups_ptype_t dtype
; /* Destination type (printer/class) */
5899 cupsd_printer_t
*printer
; /* Printer/class */
5900 char filename
[1024]; /* Script/PPD filename */
5903 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5904 con
->http
.fd
, uri
->values
[0].string
.text
);
5907 * Do we have a valid URI?
5910 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5916 send_ipp_status(con
, IPP_NOT_FOUND
,
5917 _("The printer or class was not found."));
5925 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5927 send_http_error(con
, status
, NULL
);
5932 * Remove old jobs...
5935 cupsdCancelJobs(printer
->name
, NULL
, 1);
5938 * Remove old subscriptions and send a "deleted printer" event...
5941 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5942 "%s \"%s\" deleted by \"%s\".",
5943 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5944 printer
->name
, get_username(con
));
5946 cupsdExpireSubscriptions(printer
, NULL
);
5949 * Remove any old PPD or script files...
5952 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5956 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5962 * Unregister color profiles...
5965 apple_unregister_profiles(printer
);
5966 #endif /* __APPLE__ */
5968 if (dtype
& CUPS_PRINTER_CLASS
)
5970 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5971 printer
->name
, get_username(con
));
5973 cupsdDeletePrinter(printer
, 0);
5974 cupsdSaveAllClasses();
5978 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5979 printer
->name
, get_username(con
));
5981 cupsdDeletePrinter(printer
, 0);
5982 cupsdSaveAllPrinters();
5985 cupsdWritePrintcap();
5988 * Return with no errors...
5991 con
->response
->request
.status
.status_code
= IPP_OK
;
5996 * 'get_default()' - Get the default destination.
6000 get_default(cupsd_client_t
*con
) /* I - Client connection */
6002 http_status_t status
; /* Policy status */
6003 cups_array_t
*ra
; /* Requested attributes array */
6006 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
6012 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6014 send_http_error(con
, status
, NULL
);
6020 ra
= create_requested_array(con
->request
);
6022 copy_printer_attrs(con
, DefaultPrinter
, ra
);
6024 cupsArrayDelete(ra
);
6026 con
->response
->request
.status
.status_code
= IPP_OK
;
6029 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
6034 * 'get_devices()' - Get the list of available devices on the local system.
6038 get_devices(cupsd_client_t
*con
) /* I - Client connection */
6040 http_status_t status
; /* Policy status */
6041 ipp_attribute_t
*limit
, /* Limit attribute */
6042 *requested
; /* requested-attributes attribute */
6043 char command
[1024], /* cups-deviced command */
6044 options
[1024], /* Options to pass to command */
6046 /* String for requested attributes */
6049 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
6055 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6057 send_http_error(con
, status
, NULL
);
6062 * Run cups-deviced command with the given options...
6065 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6066 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6070 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6072 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6074 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
6075 snprintf(options
, sizeof(options
),
6077 con
->request
->request
.op
.request_id
,
6078 limit
? limit
->values
[0].integer
: 0, (int)User
,
6081 if (cupsdSendCommand(con
, command
, options
, 1))
6084 * Command started successfully, don't send an IPP response here...
6087 ippDelete(con
->response
);
6088 con
->response
= NULL
;
6093 * Command failed, return "internal error" so the user knows something
6097 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6098 _("cups-deviced failed to execute."));
6104 * 'get_document()' - Get a copy of a job file.
6108 get_document(cupsd_client_t
*con
, /* I - Client connection */
6109 ipp_attribute_t
*uri
) /* I - Job URI */
6111 http_status_t status
; /* Policy status */
6112 ipp_attribute_t
*attr
; /* Current attribute */
6113 int jobid
; /* Job ID */
6114 int docnum
; /* Document number */
6115 cupsd_job_t
*job
; /* Current job */
6116 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6117 username
[HTTP_MAX_URI
], /* Username portion of URI */
6118 host
[HTTP_MAX_URI
], /* Host portion of URI */
6119 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6120 int port
; /* Port portion of URI */
6121 char filename
[1024], /* Filename for document */
6122 format
[1024]; /* Format for document */
6125 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6126 con
->http
.fd
, uri
->values
[0].string
.text
);
6129 * See if we have a job URI or a printer URI...
6132 if (!strcmp(uri
->name
, "printer-uri"))
6135 * Got a printer URI; see if we also have a job-id attribute...
6138 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6139 IPP_TAG_INTEGER
)) == NULL
)
6141 send_ipp_status(con
, IPP_BAD_REQUEST
,
6142 _("Got a printer-uri attribute but no job-id!"));
6146 jobid
= attr
->values
[0].integer
;
6151 * Got a job URI; parse it to get the job ID...
6154 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6155 sizeof(method
), username
, sizeof(username
), host
,
6156 sizeof(host
), &port
, resource
, sizeof(resource
));
6158 if (strncmp(resource
, "/jobs/", 6))
6164 send_ipp_status(con
, IPP_BAD_REQUEST
,
6165 _("Bad job-uri attribute \"%s\"!"),
6166 uri
->values
[0].string
.text
);
6170 jobid
= atoi(resource
+ 6);
6174 * See if the job exists...
6177 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6180 * Nope - return a "not found" error...
6183 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6191 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6193 send_http_error(con
, status
, NULL
);
6198 * Get the document number...
6201 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6202 IPP_TAG_INTEGER
)) == NULL
)
6204 send_ipp_status(con
, IPP_BAD_REQUEST
,
6205 _("Missing document-number attribute!"));
6209 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6210 attr
->num_values
> 1)
6212 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
6217 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6219 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6221 cupsdLogMessage(CUPSD_LOG_ERROR
,
6222 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6224 send_ipp_status(con
, IPP_NOT_FOUND
,
6225 _("Unable to open document %d in job %d!"), docnum
, jobid
);
6229 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6233 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6234 job
->filetypes
[docnum
- 1]->type
);
6236 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6238 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6240 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6241 IPP_TAG_NAME
)) != NULL
)
6242 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6243 NULL
, attr
->values
[0].string
.text
);
6248 * 'get_job_attrs()' - Get job attributes.
6252 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6253 ipp_attribute_t
*uri
) /* I - Job URI */
6255 http_status_t status
; /* Policy status */
6256 ipp_attribute_t
*attr
; /* Current attribute */
6257 int jobid
; /* Job ID */
6258 cupsd_job_t
*job
; /* Current job */
6259 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6260 username
[HTTP_MAX_URI
], /* Username portion of URI */
6261 host
[HTTP_MAX_URI
], /* Host portion of URI */
6262 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6263 int port
; /* Port portion of URI */
6264 cups_array_t
*ra
; /* Requested attributes array */
6267 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6268 con
->http
.fd
, uri
->values
[0].string
.text
);
6271 * See if we have a job URI or a printer URI...
6274 if (!strcmp(uri
->name
, "printer-uri"))
6277 * Got a printer URI; see if we also have a job-id attribute...
6280 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6281 IPP_TAG_INTEGER
)) == NULL
)
6283 send_ipp_status(con
, IPP_BAD_REQUEST
,
6284 _("Got a printer-uri attribute but no job-id!"));
6288 jobid
= attr
->values
[0].integer
;
6293 * Got a job URI; parse it to get the job ID...
6296 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6297 sizeof(method
), username
, sizeof(username
), host
,
6298 sizeof(host
), &port
, resource
, sizeof(resource
));
6300 if (strncmp(resource
, "/jobs/", 6))
6306 send_ipp_status(con
, IPP_BAD_REQUEST
,
6307 _("Bad job-uri attribute \"%s\"!"),
6308 uri
->values
[0].string
.text
);
6312 jobid
= atoi(resource
+ 6);
6316 * See if the job exists...
6319 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6322 * Nope - return a "not found" error...
6325 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6333 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6335 send_http_error(con
, status
, NULL
);
6340 * Copy attributes...
6345 ra
= create_requested_array(con
->request
);
6346 copy_job_attrs(con
, job
, ra
);
6347 cupsArrayDelete(ra
);
6349 con
->response
->request
.status
.status_code
= IPP_OK
;
6354 * 'get_jobs()' - Get a list of jobs for the specified printer.
6358 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6359 ipp_attribute_t
*uri
) /* I - Printer URI */
6361 http_status_t status
; /* Policy status */
6362 ipp_attribute_t
*attr
; /* Current attribute */
6363 const char *dest
; /* Destination */
6364 cups_ptype_t dtype
; /* Destination type (printer/class) */
6365 cups_ptype_t dmask
; /* Destination type mask */
6366 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6367 username
[HTTP_MAX_URI
], /* Username portion of URI */
6368 host
[HTTP_MAX_URI
], /* Host portion of URI */
6369 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6370 int port
; /* Port portion of URI */
6371 int completed
; /* Completed jobs? */
6372 int first_job_id
; /* First job ID */
6373 int limit
; /* Maximum number of jobs to return */
6374 int count
; /* Number of jobs that match */
6375 cupsd_job_t
*job
; /* Current job pointer */
6376 cupsd_printer_t
*printer
; /* Printer */
6377 cups_array_t
*list
; /* Which job list... */
6378 cups_array_t
*ra
; /* Requested attributes array */
6381 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
6382 uri
->values
[0].string
.text
);
6385 * Is the destination valid?
6388 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6389 sizeof(scheme
), username
, sizeof(username
), host
,
6390 sizeof(host
), &port
, resource
, sizeof(resource
));
6392 if (!strcmp(resource
, "/") ||
6393 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
6396 dtype
= (cups_ptype_t
)0;
6397 dmask
= (cups_ptype_t
)0;
6400 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6403 dtype
= (cups_ptype_t
)0;
6404 dmask
= CUPS_PRINTER_CLASS
;
6407 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6410 dtype
= CUPS_PRINTER_CLASS
;
6411 dmask
= CUPS_PRINTER_CLASS
;
6414 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6421 send_ipp_status(con
, IPP_NOT_FOUND
,
6422 _("The printer or class was not found."));
6427 dtype
&= CUPS_PRINTER_CLASS
;
6428 dmask
= CUPS_PRINTER_CLASS
;
6437 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6440 send_http_error(con
, status
, printer
);
6444 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6446 send_http_error(con
, status
, NULL
);
6451 * See if the "which-jobs" attribute have been specified...
6454 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6455 IPP_TAG_KEYWORD
)) != NULL
&&
6456 !strcmp(attr
->values
[0].string
.text
, "completed"))
6461 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
6473 * See if they want to limit the number of jobs reported...
6476 if ((attr
= ippFindAttribute(con
->request
, "limit",
6477 IPP_TAG_INTEGER
)) != NULL
)
6478 limit
= attr
->values
[0].integer
;
6482 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
6483 IPP_TAG_INTEGER
)) != NULL
)
6484 first_job_id
= attr
->values
[0].integer
;
6489 * See if we only want to see jobs for a specific user...
6492 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
6493 IPP_TAG_BOOLEAN
)) != NULL
&&
6494 attr
->values
[0].boolean
)
6495 strlcpy(username
, get_username(con
), sizeof(username
));
6499 ra
= create_requested_array(con
->request
);
6502 * OK, build a list of jobs for this printer...
6505 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6506 count
< limit
&& job
;
6507 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6510 * Filter out jobs that don't match...
6513 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
6515 if (!job
->dest
|| !job
->username
)
6518 if (!job
->dest
|| !job
->username
)
6521 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6522 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6524 if ((job
->dtype
& dmask
) != dtype
&&
6525 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6527 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
6530 if (job
->id
< first_job_id
)
6538 if (username
[0] && strcasecmp(username
, job
->username
))
6542 ippAddSeparator(con
->response
);
6546 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
6548 copy_job_attrs(con
, job
, ra
);
6551 cupsArrayDelete(ra
);
6553 con
->response
->request
.status
.status_code
= IPP_OK
;
6558 * 'get_notifications()' - Get events for a subscription.
6562 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6564 int i
, j
; /* Looping vars */
6565 http_status_t status
; /* Policy status */
6566 cupsd_subscription_t
*sub
; /* Subscription */
6567 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6568 *sequences
; /* notify-sequence-numbers */
6569 int min_seq
; /* Minimum sequence number */
6570 int interval
; /* Poll interval */
6573 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6577 * Get subscription attributes...
6580 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6582 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6587 send_ipp_status(con
, IPP_BAD_REQUEST
,
6588 _("Missing notify-subscription-ids attribute!"));
6593 * Are the subscription IDs valid?
6596 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6598 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6601 * Bad subscription ID...
6604 send_ipp_status(con
, IPP_NOT_FOUND
,
6605 _("notify-subscription-id %d no good!"),
6606 ids
->values
[i
].integer
);
6614 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6616 con
, sub
->owner
)) != HTTP_OK
)
6618 send_http_error(con
, status
, sub
->dest
);
6623 * Check the subscription type and update the interval accordingly.
6626 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6629 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6631 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6637 * Tell the client to poll again in N seconds...
6641 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6642 "notify-get-interval", interval
);
6644 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6645 "printer-up-time", time(NULL
));
6648 * Copy the subscription event attributes to the response.
6651 con
->response
->request
.status
.status_code
=
6652 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6654 for (i
= 0; i
< ids
->num_values
; i
++)
6657 * Get the subscription and sequence number...
6660 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6662 if (sequences
&& i
< sequences
->num_values
)
6663 min_seq
= sequences
->values
[i
].integer
;
6668 * If we don't have any new events, nothing to do here...
6671 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
6675 * Otherwise copy all of the new events...
6678 if (sub
->first_event_id
> min_seq
)
6681 j
= min_seq
- sub
->first_event_id
;
6683 for (; j
< sub
->num_events
; j
++)
6685 ippAddSeparator(con
->response
);
6687 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
6688 IPP_TAG_EVENT_NOTIFICATION
, 0);
6695 * 'get_ppd()' - Get a named PPD from the local system.
6699 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
6700 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
6702 http_status_t status
; /* Policy status */
6703 cupsd_printer_t
*dest
; /* Destination */
6704 cups_ptype_t dtype
; /* Destination type */
6707 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
6708 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
6710 if (!strcmp(uri
->name
, "ppd-name"))
6713 * Return a PPD file from cups-driverd...
6716 char command
[1024], /* cups-driverd command */
6717 options
[1024], /* Options to pass to command */
6718 ppd_name
[1024]; /* ppd-name */
6725 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6727 send_http_error(con
, status
, NULL
);
6732 * Run cups-driverd command with the given options...
6735 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6736 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
6737 snprintf(options
, sizeof(options
), "get+%d+%s",
6738 con
->request
->request
.op
.request_id
, ppd_name
);
6740 if (cupsdSendCommand(con
, command
, options
, 0))
6743 * Command started successfully, don't send an IPP response here...
6746 ippDelete(con
->response
);
6747 con
->response
= NULL
;
6752 * Command failed, return "internal error" so the user knows something
6756 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6757 _("cups-driverd failed to execute."));
6760 else if (!strcmp(uri
->name
, "printer-uri") &&
6761 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
6763 int i
; /* Looping var */
6764 char filename
[1024]; /* PPD filename */
6771 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6773 send_http_error(con
, status
, dest
);
6778 * See if we need the PPD for a class or remote printer...
6781 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6784 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
6786 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6787 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6788 "printer-uri", NULL
, dest
->uri
);
6791 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
6793 for (i
= 0; i
< dest
->num_printers
; i
++)
6794 if (!(dest
->printers
[i
]->type
&
6795 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
6797 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6798 dest
->printers
[i
]->name
);
6800 if (!access(filename
, 0))
6804 if (i
< dest
->num_printers
)
6805 dest
= dest
->printers
[i
];
6808 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6809 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6810 "printer-uri", NULL
, dest
->printers
[0]->uri
);
6816 * Found the printer with the PPD file, now see if there is one...
6819 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
6821 send_ipp_status(con
, IPP_NOT_FOUND
,
6822 _("The PPD file \"%s\" could not be opened: %s"),
6823 uri
->values
[0].string
.text
, strerror(errno
));
6827 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6831 con
->response
->request
.status
.status_code
= IPP_OK
;
6834 send_ipp_status(con
, IPP_NOT_FOUND
,
6835 _("The PPD file \"%s\" could not be found."),
6836 uri
->values
[0].string
.text
);
6841 * 'get_ppds()' - Get the list of PPD files on the local system.
6845 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
6847 http_status_t status
; /* Policy status */
6848 ipp_attribute_t
*limit
, /* Limit attribute */
6849 *device
, /* ppd-device-id attribute */
6850 *language
, /* ppd-natural-language attribute */
6851 *make
, /* ppd-make attribute */
6852 *model
, /* ppd-make-and-model attribute */
6853 *model_number
, /* ppd-model-number attribute */
6854 *product
, /* ppd-product attribute */
6855 *psversion
, /* ppd-psverion attribute */
6856 *type
, /* ppd-type attribute */
6857 *requested
; /* requested-attributes attribute */
6858 char command
[1024], /* cups-driverd command */
6859 options
[1024], /* Options to pass to command */
6860 device_str
[256],/* Escaped ppd-device-id string */
6862 /* Escaped ppd-natural-language */
6863 make_str
[256], /* Escaped ppd-make string */
6864 model_str
[256], /* Escaped ppd-make-and-model string */
6865 model_number_str
[256],
6866 /* ppd-model-number string */
6868 /* Escaped ppd-product string */
6870 /* Escaped ppd-psversion string */
6871 type_str
[256], /* Escaped ppd-type string */
6873 /* String for requested attributes */
6876 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
6882 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6884 send_http_error(con
, status
, NULL
);
6889 * Run cups-driverd command with the given options...
6892 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6893 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
6894 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
6896 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
6897 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
6899 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
6901 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
6902 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
6903 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
6904 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6908 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6910 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6913 url_encode_attr(device
, device_str
, sizeof(device_str
));
6915 device_str
[0] = '\0';
6918 url_encode_attr(language
, language_str
, sizeof(language_str
));
6920 language_str
[0] = '\0';
6923 url_encode_attr(make
, make_str
, sizeof(make_str
));
6928 url_encode_attr(model
, model_str
, sizeof(model_str
));
6930 model_str
[0] = '\0';
6933 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
6934 model_number
->values
[0].integer
);
6936 model_number_str
[0] = '\0';
6939 url_encode_attr(product
, product_str
, sizeof(product_str
));
6941 product_str
[0] = '\0';
6944 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
6946 psversion_str
[0] = '\0';
6949 url_encode_attr(type
, type_str
, sizeof(type_str
));
6953 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6954 snprintf(options
, sizeof(options
),
6955 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
6956 con
->request
->request
.op
.request_id
,
6957 limit
? limit
->values
[0].integer
: 0,
6959 device
? "%20" : "", device_str
,
6960 language
? "%20" : "", language_str
,
6961 make
? "%20" : "", make_str
,
6962 model
? "%20" : "", model_str
,
6963 model_number
? "%20" : "", model_number_str
,
6964 product
? "%20" : "", product_str
,
6965 psversion
? "%20" : "", psversion_str
,
6966 type
? "%20" : "", type_str
);
6968 if (cupsdSendCommand(con
, command
, options
, 0))
6971 * Command started successfully, don't send an IPP response here...
6974 ippDelete(con
->response
);
6975 con
->response
= NULL
;
6980 * Command failed, return "internal error" so the user knows something
6984 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6985 _("cups-driverd failed to execute."));
6991 * 'get_printer_attrs()' - Get printer attributes.
6995 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
6996 ipp_attribute_t
*uri
) /* I - Printer URI */
6998 http_status_t status
; /* Policy status */
6999 cups_ptype_t dtype
; /* Destination type (printer/class) */
7000 cupsd_printer_t
*printer
; /* Printer/class */
7001 cups_array_t
*ra
; /* Requested attributes array */
7004 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
7005 con
->http
.fd
, uri
->values
[0].string
.text
);
7008 * Is the destination valid?
7011 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7017 send_ipp_status(con
, IPP_NOT_FOUND
,
7018 _("The printer or class was not found."));
7026 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7028 send_http_error(con
, status
, printer
);
7033 * Send the attributes...
7036 ra
= create_requested_array(con
->request
);
7038 copy_printer_attrs(con
, printer
, ra
);
7040 cupsArrayDelete(ra
);
7042 con
->response
->request
.status
.status_code
= IPP_OK
;
7047 * 'get_printers()' - Get a list of printers or classes.
7051 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7052 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7054 http_status_t status
; /* Policy status */
7055 ipp_attribute_t
*attr
; /* Current attribute */
7056 int limit
; /* Max number of printers to return */
7057 int count
; /* Number of printers that match */
7058 cupsd_printer_t
*printer
; /* Current printer pointer */
7059 int printer_type
, /* printer-type attribute */
7060 printer_mask
; /* printer-type-mask attribute */
7061 char *location
; /* Location string */
7062 const char *username
; /* Current user */
7063 char *first_printer_name
; /* first-printer-name attribute */
7064 cups_array_t
*ra
; /* Requested attributes array */
7067 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7068 con
->http
.fd
, type
);
7074 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7076 send_http_error(con
, status
, NULL
);
7081 * Check for printers...
7084 if (!Printers
|| !cupsArrayCount(Printers
))
7086 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7091 * See if they want to limit the number of printers reported...
7094 if ((attr
= ippFindAttribute(con
->request
, "limit",
7095 IPP_TAG_INTEGER
)) != NULL
)
7096 limit
= attr
->values
[0].integer
;
7100 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7101 IPP_TAG_NAME
)) != NULL
)
7102 first_printer_name
= attr
->values
[0].string
.text
;
7104 first_printer_name
= NULL
;
7107 * Support filtering...
7110 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7111 IPP_TAG_ENUM
)) != NULL
)
7112 printer_type
= attr
->values
[0].integer
;
7116 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7117 IPP_TAG_ENUM
)) != NULL
)
7118 printer_mask
= attr
->values
[0].integer
;
7122 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7123 IPP_TAG_TEXT
)) != NULL
)
7124 location
= attr
->values
[0].string
.text
;
7128 if (con
->username
[0])
7129 username
= con
->username
;
7130 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7131 IPP_TAG_NAME
)) != NULL
)
7132 username
= attr
->values
[0].string
.text
;
7136 ra
= create_requested_array(con
->request
);
7139 * OK, build a list of printers for this printer...
7142 if (first_printer_name
)
7144 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7145 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7148 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7151 count
< limit
&& printer
;
7152 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7154 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7155 (printer
->type
& printer_mask
) == printer_type
&&
7156 (!location
|| !printer
->location
||
7157 !strcasecmp(printer
->location
, location
)))
7160 * If HideImplicitMembers is enabled, see if this printer or class
7161 * is a member of an implicit class...
7164 if (ImplicitClasses
&& HideImplicitMembers
&&
7165 printer
->in_implicit_class
)
7169 * If a username is specified, see if it is allowed or denied
7173 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
7177 * Add the group separator as needed...
7181 ippAddSeparator(con
->response
);
7186 * Send the attributes...
7189 copy_printer_attrs(con
, printer
, ra
);
7193 cupsArrayDelete(ra
);
7195 con
->response
->request
.status
.status_code
= IPP_OK
;
7200 * 'get_subscription_attrs()' - Get subscription attributes.
7204 get_subscription_attrs(
7205 cupsd_client_t
*con
, /* I - Client connection */
7206 int sub_id
) /* I - Subscription ID */
7208 http_status_t status
; /* Policy status */
7209 cupsd_subscription_t
*sub
; /* Subscription */
7210 cups_array_t
*ra
; /* Requested attributes array */
7213 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7214 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7215 con
, con
->http
.fd
, sub_id
);
7218 * Is the subscription ID valid?
7221 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7224 * Bad subscription ID...
7227 send_ipp_status(con
, IPP_NOT_FOUND
,
7228 _("notify-subscription-id %d no good!"), sub_id
);
7236 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7238 con
, sub
->owner
)) != HTTP_OK
)
7240 send_http_error(con
, status
, sub
->dest
);
7245 * Copy the subscription attributes to the response using the
7246 * requested-attributes attribute that may be provided by the client.
7249 ra
= create_requested_array(con
->request
);
7251 copy_subscription_attrs(con
, sub
, ra
);
7253 cupsArrayDelete(ra
);
7255 con
->response
->request
.status
.status_code
= IPP_OK
;
7260 * 'get_subscriptions()' - Get subscriptions.
7264 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7265 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7267 http_status_t status
; /* Policy status */
7268 int count
; /* Number of subscriptions */
7269 int limit
; /* Limit */
7270 cupsd_subscription_t
*sub
; /* Subscription */
7271 cups_array_t
*ra
; /* Requested attributes array */
7272 ipp_attribute_t
*attr
; /* Attribute */
7273 cups_ptype_t dtype
; /* Destination type (printer/class) */
7274 char scheme
[HTTP_MAX_URI
],
7275 /* Scheme portion of URI */
7276 username
[HTTP_MAX_URI
],
7277 /* Username portion of URI */
7279 /* Host portion of URI */
7280 resource
[HTTP_MAX_URI
];
7281 /* Resource portion of URI */
7282 int port
; /* Port portion of URI */
7283 cupsd_job_t
*job
; /* Job pointer */
7284 cupsd_printer_t
*printer
; /* Printer */
7287 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7288 "get_subscriptions(con=%p[%d], uri=%s)",
7289 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
7292 * Is the destination valid?
7295 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7296 sizeof(scheme
), username
, sizeof(username
), host
,
7297 sizeof(host
), &port
, resource
, sizeof(resource
));
7299 if (!strcmp(resource
, "/") ||
7300 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7301 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7302 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7307 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7310 job
= cupsdFindJob(atoi(resource
+ 6));
7314 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
7319 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7325 send_ipp_status(con
, IPP_NOT_FOUND
,
7326 _("The printer or class was not found."));
7329 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7330 IPP_TAG_INTEGER
)) != NULL
)
7332 job
= cupsdFindJob(attr
->values
[0].integer
);
7336 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
7337 attr
->values
[0].integer
);
7348 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
7350 con
, NULL
)) != HTTP_OK
)
7352 send_http_error(con
, status
, printer
);
7357 * Copy the subscription attributes to the response using the
7358 * requested-attributes attribute that may be provided by the client.
7361 ra
= create_requested_array(con
->request
);
7363 if ((attr
= ippFindAttribute(con
->request
, "limit",
7364 IPP_TAG_INTEGER
)) != NULL
)
7365 limit
= attr
->values
[0].integer
;
7370 * See if we only want to see subscriptions for a specific user...
7373 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7374 IPP_TAG_BOOLEAN
)) != NULL
&&
7375 attr
->values
[0].boolean
)
7376 strlcpy(username
, get_username(con
), sizeof(username
));
7380 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7382 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7383 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7384 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
7386 ippAddSeparator(con
->response
);
7387 copy_subscription_attrs(con
, sub
, ra
);
7390 if (limit
&& count
>= limit
)
7394 cupsArrayDelete(ra
);
7397 con
->response
->request
.status
.status_code
= IPP_OK
;
7399 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7404 * 'get_username()' - Get the username associated with a request.
7407 static const char * /* O - Username */
7408 get_username(cupsd_client_t
*con
) /* I - Connection */
7410 ipp_attribute_t
*attr
; /* Attribute */
7413 if (con
->username
[0])
7414 return (con
->username
);
7415 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7416 IPP_TAG_NAME
)) != NULL
)
7417 return (attr
->values
[0].string
.text
);
7419 return ("anonymous");
7424 * 'hold_job()' - Hold a print job.
7428 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7429 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7431 ipp_attribute_t
*attr
, /* Current job-hold-until */
7432 *newattr
; /* New job-hold-until */
7433 int jobid
; /* Job ID */
7434 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7435 username
[HTTP_MAX_URI
], /* Username portion of URI */
7436 host
[HTTP_MAX_URI
], /* Host portion of URI */
7437 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7438 int port
; /* Port portion of URI */
7439 cupsd_job_t
*job
; /* Job information */
7442 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
7443 uri
->values
[0].string
.text
);
7446 * See if we have a job URI or a printer URI...
7449 if (!strcmp(uri
->name
, "printer-uri"))
7452 * Got a printer URI; see if we also have a job-id attribute...
7455 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7456 IPP_TAG_INTEGER
)) == NULL
)
7458 send_ipp_status(con
, IPP_BAD_REQUEST
,
7459 _("Got a printer-uri attribute but no job-id!"));
7463 jobid
= attr
->values
[0].integer
;
7468 * Got a job URI; parse it to get the job ID...
7471 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7472 sizeof(method
), username
, sizeof(username
), host
,
7473 sizeof(host
), &port
, resource
, sizeof(resource
));
7475 if (strncmp(resource
, "/jobs/", 6))
7481 send_ipp_status(con
, IPP_BAD_REQUEST
,
7482 _("Bad job-uri attribute \"%s\"!"),
7483 uri
->values
[0].string
.text
);
7487 jobid
= atoi(resource
+ 6);
7491 * See if the job exists...
7494 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7497 * Nope - return a "not found" error...
7500 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7505 * See if the job is owned by the requesting user...
7508 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7510 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7515 * Hold the job and return...
7520 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
7521 "Job held by user.");
7523 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
7524 IPP_TAG_KEYWORD
)) == NULL
)
7525 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7527 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7528 IPP_TAG_KEYWORD
)) == NULL
)
7529 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7534 * Free the old hold value and copy the new one over...
7537 _cupsStrFree(attr
->values
[0].string
.text
);
7541 attr
->value_tag
= newattr
->value_tag
;
7542 attr
->values
[0].string
.text
=
7543 _cupsStrAlloc(newattr
->values
[0].string
.text
);
7547 attr
->value_tag
= IPP_TAG_KEYWORD
;
7548 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
7552 * Hold job until specified time...
7555 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
7557 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
7558 "Job job-hold-until value changed by user.");
7561 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Held by \"%s\".", jobid
,
7564 con
->response
->request
.status
.status_code
= IPP_OK
;
7569 * 'move_job()' - Move a job to a new destination.
7573 move_job(cupsd_client_t
*con
, /* I - Client connection */
7574 ipp_attribute_t
*uri
) /* I - Job URI */
7576 http_status_t status
; /* Policy status */
7577 ipp_attribute_t
*attr
; /* Current attribute */
7578 int jobid
; /* Job ID */
7579 cupsd_job_t
*job
; /* Current job */
7580 const char *src
; /* Source printer/class */
7581 cups_ptype_t stype
, /* Source type (printer or class) */
7582 dtype
; /* Destination type (printer/class) */
7583 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7584 username
[HTTP_MAX_URI
], /* Username portion of URI */
7585 host
[HTTP_MAX_URI
], /* Host portion of URI */
7586 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7587 int port
; /* Port portion of URI */
7588 cupsd_printer_t
*sprinter
, /* Source printer */
7589 *dprinter
; /* Destination printer */
7592 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
7593 uri
->values
[0].string
.text
);
7596 * Get the new printer or class...
7599 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7600 IPP_TAG_URI
)) == NULL
)
7603 * Need job-printer-uri...
7606 send_ipp_status(con
, IPP_BAD_REQUEST
,
7607 _("job-printer-uri attribute missing!"));
7611 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7617 send_ipp_status(con
, IPP_NOT_FOUND
,
7618 _("The printer or class was not found."));
7623 * See if we have a job URI or a printer URI...
7626 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7627 sizeof(scheme
), username
, sizeof(username
), host
,
7628 sizeof(host
), &port
, resource
, sizeof(resource
));
7630 if (!strcmp(uri
->name
, "printer-uri"))
7633 * Got a printer URI; see if we also have a job-id attribute...
7636 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7637 IPP_TAG_INTEGER
)) == NULL
)
7643 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
7644 &sprinter
)) == NULL
)
7650 send_ipp_status(con
, IPP_NOT_FOUND
,
7651 _("The printer or class was not found."));
7660 * Otherwise, just move a single job...
7663 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7666 * Nope - return a "not found" error...
7669 send_ipp_status(con
, IPP_NOT_FOUND
,
7670 _("Job #%d does not exist!"), attr
->values
[0].integer
);
7676 * Job found, initialize source pointers...
7687 * Got a job URI; parse it to get the job ID...
7690 if (strncmp(resource
, "/jobs/", 6))
7696 send_ipp_status(con
, IPP_BAD_REQUEST
,
7697 _("Bad job-uri attribute \"%s\"!"),
7698 uri
->values
[0].string
.text
);
7703 * See if the job exists...
7706 jobid
= atoi(resource
+ 6);
7708 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7711 * Nope - return a "not found" error...
7714 send_ipp_status(con
, IPP_NOT_FOUND
,
7715 _("Job #%d does not exist!"), jobid
);
7721 * Job found, initialize source pointers...
7730 * Check the policy of the destination printer...
7733 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
7734 job
? job
->username
: NULL
)) != HTTP_OK
)
7736 send_http_error(con
, status
, dprinter
);
7741 * Now move the job or jobs...
7747 * See if the job has been completed...
7750 if (job
->state_value
> IPP_JOB_STOPPED
)
7753 * Return a "not-possible" error...
7756 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7757 _("Job #%d is finished and cannot be altered!"),
7763 * See if the job is owned by the requesting user...
7766 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7768 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7773 * Move the job to a different printer or class...
7776 cupsdMoveJob(job
, dprinter
);
7781 * Got the source printer, now look through the jobs...
7784 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
7786 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
7789 * See if the job is pointing at the source printer or has not been
7793 if (strcasecmp(job
->dest
, src
) ||
7794 job
->state_value
> IPP_JOB_STOPPED
)
7798 * See if the job can be moved by the requesting user...
7801 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7805 * Move the job to a different printer or class...
7808 cupsdMoveJob(job
, dprinter
);
7813 * Start jobs if possible...
7819 * Return with "everything is OK" status...
7822 con
->response
->request
.status
.status_code
= IPP_OK
;
7827 * 'ppd_parse_line()' - Parse a PPD default line.
7830 static int /* O - 0 on success, -1 on failure */
7831 ppd_parse_line(const char *line
, /* I - Line */
7832 char *option
, /* O - Option name */
7833 int olen
, /* I - Size of option name */
7834 char *choice
, /* O - Choice name */
7835 int clen
) /* I - Size of choice name */
7838 * Verify this is a default option line...
7841 if (strncmp(line
, "*Default", 8))
7845 * Read the option name...
7848 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
7858 * Skip everything else up to the colon (:)...
7861 while (*line
&& *line
!= ':')
7870 * Now grab the option choice, skipping leading whitespace...
7873 while (isspace(*line
& 255))
7876 for (clen
--; isalnum(*line
& 255); line
++)
7886 * Return with no errors...
7894 * 'print_job()' - Print a file to a printer or class.
7898 print_job(cupsd_client_t
*con
, /* I - Client connection */
7899 ipp_attribute_t
*uri
) /* I - Printer URI */
7901 ipp_attribute_t
*attr
; /* Current attribute */
7902 ipp_attribute_t
*format
; /* Document-format attribute */
7903 const char *default_format
; /* document-format-default value */
7904 cupsd_job_t
*job
; /* New job */
7905 char filename
[1024]; /* Job filename */
7906 mime_type_t
*filetype
; /* Type of file */
7907 char super
[MIME_MAX_SUPER
], /* Supertype of file */
7908 type
[MIME_MAX_TYPE
], /* Subtype of file */
7909 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7910 /* Textual name of mime type */
7911 cupsd_printer_t
*printer
; /* Printer data */
7912 struct stat fileinfo
; /* File information */
7913 int kbytes
; /* Size of file */
7914 int compression
; /* Document compression */
7917 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
7918 uri
->values
[0].string
.text
);
7921 * Validate print file attributes, for now just document-format and
7922 * compression (CUPS only supports "none" and "gzip")...
7925 compression
= CUPS_FILE_NONE
;
7927 if ((attr
= ippFindAttribute(con
->request
, "compression",
7928 IPP_TAG_KEYWORD
)) != NULL
)
7930 if (strcmp(attr
->values
[0].string
.text
, "none")
7932 && strcmp(attr
->values
[0].string
.text
, "gzip")
7933 #endif /* HAVE_LIBZ */
7936 send_ipp_status(con
, IPP_ATTRIBUTES
,
7937 _("Unsupported compression \"%s\"!"),
7938 attr
->values
[0].string
.text
);
7939 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7940 "compression", NULL
, attr
->values
[0].string
.text
);
7945 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7946 compression
= CUPS_FILE_GZIP
;
7947 #endif /* HAVE_LIBZ */
7951 * Do we have a file to print?
7956 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7961 * Is the destination valid?
7964 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
7970 send_ipp_status(con
, IPP_NOT_FOUND
,
7971 _("The printer or class was not found."));
7976 * Is it a format we support?
7979 if ((format
= ippFindAttribute(con
->request
, "document-format",
7980 IPP_TAG_MIMETYPE
)) != NULL
)
7983 * Grab format from client...
7986 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
7989 send_ipp_status(con
, IPP_BAD_REQUEST
,
7990 _("Could not scan type \"%s\"!"),
7991 format
->values
[0].string
.text
);
7995 else if ((default_format
= cupsGetOption("document-format",
7996 printer
->num_options
,
7997 printer
->options
)) != NULL
)
8000 * Use default document format...
8003 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
8005 send_ipp_status(con
, IPP_BAD_REQUEST
,
8006 _("Could not scan type \"%s\"!"),
8017 strcpy(super
, "application");
8018 strcpy(type
, "octet-stream");
8021 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8024 * Auto-type the file...
8027 ipp_attribute_t
*doc_name
; /* document-name attribute */
8030 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
8032 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8033 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8034 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8038 filetype
= mimeType(MimeDatabase
, super
, type
);
8041 filetype
= mimeType(MimeDatabase
, super
, type
);
8045 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8048 * Replace the document-format attribute value with the auto-typed or
8052 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8057 _cupsStrFree(format
->values
[0].string
.text
);
8059 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8062 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8063 "document-format", NULL
, mimetype
);
8067 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8068 _("Unsupported format \'%s/%s\'!"), super
, type
);
8069 cupsdLogMessage(CUPSD_LOG_INFO
,
8070 "Hint: Do you have the raw file printing rules enabled?");
8073 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8074 "document-format", NULL
, format
->values
[0].string
.text
);
8080 * Read any embedded job ticket info from PS files...
8083 if (!strcasecmp(filetype
->super
, "application") &&
8084 !strcasecmp(filetype
->type
, "postscript"))
8085 read_ps_job_ticket(con
);
8088 * Create the job object...
8091 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8094 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Adding job file of type %s/%s.",
8095 job
->id
, filetype
->super
, filetype
->type
);
8098 * Update quota data...
8101 if (stat(con
->filename
, &fileinfo
))
8104 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8106 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8108 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8109 IPP_TAG_INTEGER
)) != NULL
)
8110 attr
->values
[0].integer
+= kbytes
;
8113 * Add the job file...
8116 if (add_file(con
, job
, filetype
, compression
))
8119 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8121 rename(con
->filename
, filename
);
8122 cupsdClearString(&con
->filename
);
8125 * See if we need to add the ending sheet...
8128 if (cupsdTimeoutJob(job
))
8132 * Log and save the job...
8135 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
8136 job
->id
, job
->dest
, job
->username
);
8137 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] hold_until = %d", job
->id
,
8138 (int)job
->hold_until
);
8143 * Start the job if possible...
8151 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
8153 * This function only gets called when printing a single PostScript
8154 * file using the Print-Job operation. It doesn't work for Create-Job +
8155 * Send-File, since the job attributes need to be set at job creation
8156 * time for banners to work. The embedded PS job ticket stuff is here
8157 * only to allow the Windows printer driver for CUPS to pass in JCL
8158 * options and IPP attributes which otherwise would be lost.
8160 * The format of a PS job ticket is simple:
8162 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8164 * %cupsJobTicket: attr1=value1
8165 * %cupsJobTicket: attr2=value2
8167 * %cupsJobTicket: attrN=valueN
8169 * Job ticket lines must appear immediately after the first line that
8170 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
8171 * looking for job ticket info when it finds a line that does not begin
8172 * with "%cupsJobTicket:".
8174 * The maximum length of a job ticket line, including the prefix, is
8175 * 255 characters to conform with the Adobe DSC.
8177 * Read-only attributes are rejected with a notice to the error log in
8178 * case a malicious user tries anything. Since the job ticket is read
8179 * prior to attribute validation in print_job(), job ticket attributes
8180 * will go through the same validation as IPP attributes...
8184 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8186 cups_file_t
*fp
; /* File to read from */
8187 char line
[256]; /* Line data */
8188 int num_options
; /* Number of options */
8189 cups_option_t
*options
; /* Options */
8190 ipp_t
*ticket
; /* New attributes */
8191 ipp_attribute_t
*attr
, /* Current attribute */
8192 *attr2
, /* Job attribute */
8193 *prev2
; /* Previous job attribute */
8197 * First open the print file...
8200 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8202 cupsdLogMessage(CUPSD_LOG_ERROR
,
8203 "read_ps_job_ticket: Unable to open PostScript print file "
8210 * Skip the first line...
8213 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8215 cupsdLogMessage(CUPSD_LOG_ERROR
,
8216 "read_ps_job_ticket: Unable to read from PostScript print "
8223 if (strncmp(line
, "%!PS-Adobe-", 11))
8226 * Not a DSC-compliant file, so no job ticket info will be available...
8234 * Read job ticket info from the file...
8240 while (cupsFileGets(fp
, line
, sizeof(line
)))
8243 * Stop at the first non-ticket line...
8246 if (strncmp(line
, "%cupsJobTicket:", 15))
8250 * Add the options to the option array...
8253 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8257 * Done with the file; see if we have any options...
8262 if (num_options
== 0)
8266 * OK, convert the options to an attribute list, and apply them to
8271 cupsEncodeOptions(ticket
, num_options
, options
);
8274 * See what the user wants to change.
8277 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8279 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8282 if (!strcmp(attr
->name
, "job-originating-host-name") ||
8283 !strcmp(attr
->name
, "job-originating-user-name") ||
8284 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8285 !strcmp(attr
->name
, "job-k-octets") ||
8286 !strcmp(attr
->name
, "job-id") ||
8287 !strncmp(attr
->name
, "job-state", 9) ||
8288 !strncmp(attr
->name
, "time-at-", 8))
8289 continue; /* Read-only attrs */
8291 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8292 IPP_TAG_ZERO
)) != NULL
)
8295 * Some other value; first free the old value...
8298 if (con
->request
->attrs
== attr2
)
8300 con
->request
->attrs
= attr2
->next
;
8305 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8306 if (prev2
->next
== attr2
)
8308 prev2
->next
= attr2
->next
;
8313 if (con
->request
->last
== attr2
)
8314 con
->request
->last
= prev2
;
8316 _ippFreeAttr(attr2
);
8320 * Add new option by copying it...
8323 copy_attribute(con
->request
, attr
, 0);
8327 * Then free the attribute list and option array...
8331 cupsFreeOptions(num_options
, options
);
8336 * 'reject_jobs()' - Reject print jobs to a printer.
8340 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8341 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8343 http_status_t status
; /* Policy status */
8344 cups_ptype_t dtype
; /* Destination type (printer/class) */
8345 cupsd_printer_t
*printer
; /* Printer data */
8346 ipp_attribute_t
*attr
; /* printer-state-message text */
8349 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8350 con
->http
.fd
, uri
->values
[0].string
.text
);
8353 * Is the destination valid?
8356 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8362 send_ipp_status(con
, IPP_NOT_FOUND
,
8363 _("The printer or class was not found."));
8371 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8373 send_http_error(con
, status
, printer
);
8378 * Reject jobs sent to the printer...
8381 printer
->accepting
= 0;
8383 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8384 IPP_TAG_TEXT
)) == NULL
)
8385 strcpy(printer
->state_message
, "Rejecting Jobs");
8387 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8388 sizeof(printer
->state_message
));
8390 cupsdAddPrinterHistory(printer
);
8392 if (dtype
& CUPS_PRINTER_CLASS
)
8394 cupsdSaveAllClasses();
8396 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8397 printer
->name
, get_username(con
));
8401 cupsdSaveAllPrinters();
8403 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8404 printer
->name
, get_username(con
));
8408 * Everything was ok, so return OK status...
8411 con
->response
->request
.status
.status_code
= IPP_OK
;
8416 * 'release_job()' - Release a held print job.
8420 release_job(cupsd_client_t
*con
, /* I - Client connection */
8421 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8423 ipp_attribute_t
*attr
; /* Current attribute */
8424 int jobid
; /* Job ID */
8425 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8426 username
[HTTP_MAX_URI
], /* Username portion of URI */
8427 host
[HTTP_MAX_URI
], /* Host portion of URI */
8428 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8429 int port
; /* Port portion of URI */
8430 cupsd_job_t
*job
; /* Job information */
8433 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8434 con
->http
.fd
, uri
->values
[0].string
.text
);
8437 * See if we have a job URI or a printer URI...
8440 if (!strcmp(uri
->name
, "printer-uri"))
8443 * Got a printer URI; see if we also have a job-id attribute...
8446 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8447 IPP_TAG_INTEGER
)) == NULL
)
8449 send_ipp_status(con
, IPP_BAD_REQUEST
,
8450 _("Got a printer-uri attribute but no job-id!"));
8454 jobid
= attr
->values
[0].integer
;
8459 * Got a job URI; parse it to get the job ID...
8462 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8463 sizeof(method
), username
, sizeof(username
), host
,
8464 sizeof(host
), &port
, resource
, sizeof(resource
));
8466 if (strncmp(resource
, "/jobs/", 6))
8472 send_ipp_status(con
, IPP_BAD_REQUEST
,
8473 _("Bad job-uri attribute \"%s\"!"),
8474 uri
->values
[0].string
.text
);
8478 jobid
= atoi(resource
+ 6);
8482 * See if the job exists...
8485 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8488 * Nope - return a "not found" error...
8491 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8496 * See if job is "held"...
8499 if (job
->state_value
!= IPP_JOB_HELD
)
8502 * Nope - return a "not possible" error...
8505 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
8510 * See if the job is owned by the requesting user...
8513 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8515 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8520 * Reset the job-hold-until value to "no-hold"...
8523 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8524 IPP_TAG_KEYWORD
)) == NULL
)
8525 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8529 _cupsStrFree(attr
->values
[0].string
.text
);
8531 attr
->value_tag
= IPP_TAG_KEYWORD
;
8532 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
8534 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
8535 "Job job-hold-until value changed by user.");
8539 * Release the job and return...
8542 cupsdReleaseJob(job
);
8544 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
8545 "Job released by user.");
8547 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Released by \"%s\".", jobid
,
8550 con
->response
->request
.status
.status_code
= IPP_OK
;
8555 * 'renew_subscription()' - Renew an existing subscription...
8560 cupsd_client_t
*con
, /* I - Client connection */
8561 int sub_id
) /* I - Subscription ID */
8563 http_status_t status
; /* Policy status */
8564 cupsd_subscription_t
*sub
; /* Subscription */
8565 ipp_attribute_t
*lease
; /* notify-lease-duration */
8568 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8569 "renew_subscription(con=%p[%d], sub_id=%d)",
8570 con
, con
->http
.fd
, sub_id
);
8573 * Is the subscription ID valid?
8576 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8579 * Bad subscription ID...
8582 send_ipp_status(con
, IPP_NOT_FOUND
,
8583 _("notify-subscription-id %d no good!"), sub_id
);
8590 * Job subscriptions cannot be renewed...
8593 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8594 _("Job subscriptions cannot be renewed!"));
8602 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8604 con
, sub
->owner
)) != HTTP_OK
)
8606 send_http_error(con
, status
, sub
->dest
);
8611 * Renew the subscription...
8614 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8617 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8619 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8621 cupsdLogMessage(CUPSD_LOG_INFO
,
8622 "renew_subscription: Limiting notify-lease-duration to "
8625 sub
->lease
= MaxLeaseDuration
;
8628 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
8630 cupsdSaveAllSubscriptions();
8632 con
->response
->request
.status
.status_code
= IPP_OK
;
8634 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
8635 "notify-lease-duration", sub
->lease
);
8640 * 'restart_job()' - Restart an old print job.
8644 restart_job(cupsd_client_t
*con
, /* I - Client connection */
8645 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8647 ipp_attribute_t
*attr
; /* Current attribute */
8648 int jobid
; /* Job ID */
8649 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8650 username
[HTTP_MAX_URI
], /* Username portion of URI */
8651 host
[HTTP_MAX_URI
], /* Host portion of URI */
8652 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8653 int port
; /* Port portion of URI */
8654 cupsd_job_t
*job
; /* Job information */
8657 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
8658 con
->http
.fd
, uri
->values
[0].string
.text
);
8661 * See if we have a job URI or a printer URI...
8664 if (!strcmp(uri
->name
, "printer-uri"))
8667 * Got a printer URI; see if we also have a job-id attribute...
8670 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8671 IPP_TAG_INTEGER
)) == NULL
)
8673 send_ipp_status(con
, IPP_BAD_REQUEST
,
8674 _("Got a printer-uri attribute but no job-id!"));
8678 jobid
= attr
->values
[0].integer
;
8683 * Got a job URI; parse it to get the job ID...
8686 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8687 sizeof(method
), username
, sizeof(username
), host
,
8688 sizeof(host
), &port
, resource
, sizeof(resource
));
8690 if (strncmp(resource
, "/jobs/", 6))
8696 send_ipp_status(con
, IPP_BAD_REQUEST
,
8697 _("Bad job-uri attribute \"%s\"!"),
8698 uri
->values
[0].string
.text
);
8702 jobid
= atoi(resource
+ 6);
8706 * See if the job exists...
8709 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8712 * Nope - return a "not found" error...
8715 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8720 * See if job is in any of the "completed" states...
8723 if (job
->state_value
<= IPP_JOB_PROCESSING
)
8726 * Nope - return a "not possible" error...
8729 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
8735 * See if we have retained the job files...
8740 if (!job
->attrs
|| job
->num_files
== 0)
8743 * Nope - return a "not possible" error...
8746 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8747 _("Job #%d cannot be restarted - no files!"), jobid
);
8752 * See if the job is owned by the requesting user...
8755 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8757 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8762 * Restart the job and return...
8765 cupsdRestartJob(job
);
8767 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Restarted by \"%s\".", jobid
,
8770 con
->response
->request
.status
.status_code
= IPP_OK
;
8775 * 'save_auth_info()' - Save authentication information for a job.
8780 cupsd_client_t
*con
, /* I - Client connection */
8781 cupsd_job_t
*job
, /* I - Job */
8782 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
8784 int i
; /* Looping var */
8785 char filename
[1024]; /* Job authentication filename */
8786 cups_file_t
*fp
; /* Job authentication file */
8787 char line
[2048]; /* Line for file */
8788 cupsd_printer_t
*dest
; /* Destination printer/class */
8792 * This function saves the in-memory authentication information for
8793 * a job so that it can be used to authenticate with a remote host.
8794 * The information is stored in a file that is readable only by the
8795 * root user. The fields are Base-64 encoded, each on a separate line,
8796 * followed by random number (up to 1024) of newlines to limit the
8797 * amount of information that is exposed.
8799 * Because of the potential for exposing of authentication information,
8800 * this functionality is only enabled when running cupsd as root.
8802 * This caching only works for the Basic and BasicDigest authentication
8803 * types. Digest authentication cannot be cached this way, and in
8804 * the future Kerberos authentication may make all of this obsolete.
8806 * Authentication information is saved whenever an authenticated
8807 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
8810 * This information is deleted after a job is completed or canceled,
8811 * so reprints may require subsequent re-authentication.
8817 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
8821 * Create the authentication file and change permissions...
8824 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
8825 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
8827 cupsdLogMessage(CUPSD_LOG_ERROR
,
8828 "Unable to save authentication info to \"%s\" - %s",
8829 filename
, strerror(errno
));
8833 fchown(cupsFileNumber(fp
), 0, 0);
8834 fchmod(cupsFileNumber(fp
), 0400);
8836 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
8839 * Write 1 to 3 auth values...
8842 cupsdClearString(&job
->auth_username
);
8843 cupsdClearString(&job
->auth_domain
);
8844 cupsdClearString(&job
->auth_password
);
8846 for (i
= 0; i
< auth_info
->num_values
; i
++)
8848 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
8849 strlen(auth_info
->values
[i
].string
.text
));
8850 cupsFilePrintf(fp
, "%s\n", line
);
8852 if (!strcmp(dest
->auth_info_required
[i
], "username"))
8853 cupsdSetStringf(&job
->auth_username
, "CUPSD_AUTH_USERNAME=%s",
8854 auth_info
->values
[i
].string
.text
);
8855 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
8856 cupsdSetStringf(&job
->auth_domain
, "CUPSD_AUTH_DOMAIN=%s",
8857 auth_info
->values
[i
].string
.text
);
8858 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
8859 cupsdSetStringf(&job
->auth_password
, "CUPSD_AUTH_PASSWORD=%s",
8860 auth_info
->values
[i
].string
.text
);
8863 else if (con
->username
[0])
8866 * Write the authenticated username...
8869 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
8870 cupsFilePrintf(fp
, "%s\n", line
);
8872 cupsdSetStringf(&job
->auth_username
, "CUPSD_AUTH_USERNAME=%s", con
->username
);
8873 cupsdClearString(&job
->auth_domain
);
8876 * Write the authenticated password...
8879 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
8880 cupsFilePrintf(fp
, "%s\n", line
);
8882 cupsdSetStringf(&job
->auth_password
, "CUPSD_AUTH_PASSWORD=%s", con
->password
);
8886 * Write a random number of newlines to the end of the file...
8889 for (i
= (rand() % 1024); i
>= 0; i
--)
8890 cupsFilePutChar(fp
, '\n');
8893 * Close the file and return...
8898 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8899 if (con
->gss_have_creds
)
8900 save_krb5_creds(con
, job
);
8901 else if (job
->ccname
)
8902 cupsdClearString(&(job
->ccname
));
8903 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8907 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8909 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
8913 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
8914 cupsd_job_t
*job
) /* I - Job */
8916 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
8917 cupsdLogMessage(CUPSD_LOG_INFO
,
8918 "Sorry, your version of Kerberos does not support delegated "
8923 krb5_error_code error
; /* Kerberos error code */
8924 OM_uint32 major_status
, /* Major status code */
8925 minor_status
; /* Minor status code */
8926 krb5_principal principal
; /* Kerberos principal */
8931 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
8935 if (krb5_init_context
== NULL
)
8937 # endif /* __APPLE__ */
8940 * We MUST create a file-based cache because memory-based caches are
8941 * only valid for the current process/address space.
8943 * Due to various bugs/features in different versions of Kerberos, we
8944 * need either the krb5_cc_new_unique() function or Heimdal's version
8945 * of krb5_cc_gen_new() to create a new FILE: credential cache that
8946 * can be passed to the backend. These functions create a temporary
8947 * file (typically in /tmp) containing the cached credentials, which
8948 * are removed when we have successfully printed a job.
8951 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
8952 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
8953 &(job
->ccache
))) != 0)
8954 # else /* HAVE_HEIMDAL */
8955 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
8956 &(job
->ccache
))) != 0)
8957 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
8959 cupsdLogMessage(CUPSD_LOG_ERROR
,
8960 "Unable to create new credentials cache (%d/%s)",
8961 error
, strerror(errno
));
8966 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
8968 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to parse kerberos username (%d/%s)",
8969 error
, strerror(errno
));
8970 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8975 if ((error
= krb5_cc_initialize(KerberosContext
, job
->ccache
, principal
)))
8977 cupsdLogMessage(CUPSD_LOG_ERROR
,
8978 "Unable to initialize credentials cache (%d/%s)", error
,
8980 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8981 krb5_free_principal(KerberosContext
, principal
);
8986 krb5_free_principal(KerberosContext
, principal
);
8989 * Copy the user's credentials to the new cache file...
8992 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
8995 if (GSS_ERROR(major_status
))
8997 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
8998 "Unable to import client credentials cache");
8999 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9005 * Add the KRB5CCNAME environment variable to the job so that the
9006 * backend can use the credentials when printing.
9009 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
9010 krb5_cc_get_name(KerberosContext
, job
->ccache
));
9012 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] save_krb5_creds: %s", job
->id
,
9014 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
9016 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9020 * 'send_document()' - Send a file to a printer or class.
9024 send_document(cupsd_client_t
*con
, /* I - Client connection */
9025 ipp_attribute_t
*uri
) /* I - Printer URI */
9027 ipp_attribute_t
*attr
; /* Current attribute */
9028 ipp_attribute_t
*format
; /* Document-format attribute */
9029 const char *default_format
;/* document-format-default value */
9030 int jobid
; /* Job ID number */
9031 cupsd_job_t
*job
; /* Current job */
9032 char job_uri
[HTTP_MAX_URI
],
9034 method
[HTTP_MAX_URI
],
9035 /* Method portion of URI */
9036 username
[HTTP_MAX_URI
],
9037 /* Username portion of URI */
9039 /* Host portion of URI */
9040 resource
[HTTP_MAX_URI
];
9041 /* Resource portion of URI */
9042 int port
; /* Port portion of URI */
9043 mime_type_t
*filetype
; /* Type of file */
9044 char super
[MIME_MAX_SUPER
],
9045 /* Supertype of file */
9046 type
[MIME_MAX_TYPE
],
9047 /* Subtype of file */
9048 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9049 /* Textual name of mime type */
9050 char filename
[1024]; /* Job filename */
9051 cupsd_printer_t
*printer
; /* Current printer */
9052 struct stat fileinfo
; /* File information */
9053 int kbytes
; /* Size of file */
9054 int compression
; /* Type of compression */
9057 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9058 con
->http
.fd
, uri
->values
[0].string
.text
);
9061 * See if we have a job URI or a printer URI...
9064 if (!strcmp(uri
->name
, "printer-uri"))
9067 * Got a printer URI; see if we also have a job-id attribute...
9070 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9071 IPP_TAG_INTEGER
)) == NULL
)
9073 send_ipp_status(con
, IPP_BAD_REQUEST
,
9074 _("Got a printer-uri attribute but no job-id!"));
9078 jobid
= attr
->values
[0].integer
;
9083 * Got a job URI; parse it to get the job ID...
9086 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9087 sizeof(method
), username
, sizeof(username
), host
,
9088 sizeof(host
), &port
, resource
, sizeof(resource
));
9090 if (strncmp(resource
, "/jobs/", 6))
9096 send_ipp_status(con
, IPP_BAD_REQUEST
,
9097 _("Bad job-uri attribute \"%s\"!"),
9098 uri
->values
[0].string
.text
);
9102 jobid
= atoi(resource
+ 6);
9106 * See if the job exists...
9109 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9112 * Nope - return a "not found" error...
9115 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9119 printer
= cupsdFindDest(job
->dest
);
9122 * See if the job is owned by the requesting user...
9125 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9127 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9132 * OK, see if the client is sending the document compressed - CUPS
9133 * only supports "none" and "gzip".
9136 compression
= CUPS_FILE_NONE
;
9138 if ((attr
= ippFindAttribute(con
->request
, "compression",
9139 IPP_TAG_KEYWORD
)) != NULL
)
9141 if (strcmp(attr
->values
[0].string
.text
, "none")
9143 && strcmp(attr
->values
[0].string
.text
, "gzip")
9144 #endif /* HAVE_LIBZ */
9147 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
9148 attr
->values
[0].string
.text
);
9149 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9150 "compression", NULL
, attr
->values
[0].string
.text
);
9155 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9156 compression
= CUPS_FILE_GZIP
;
9157 #endif /* HAVE_LIBZ */
9161 * Do we have a file to print?
9166 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
9171 * Is it a format we support?
9174 if ((format
= ippFindAttribute(con
->request
, "document-format",
9175 IPP_TAG_MIMETYPE
)) != NULL
)
9178 * Grab format from client...
9181 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
9184 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9185 format
->values
[0].string
.text
);
9189 else if ((default_format
= cupsGetOption("document-format",
9190 printer
->num_options
,
9191 printer
->options
)) != NULL
)
9194 * Use default document format...
9197 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
9199 send_ipp_status(con
, IPP_BAD_REQUEST
,
9200 _("Could not scan type \"%s\"!"),
9208 * No document format attribute? Auto-type it!
9211 strcpy(super
, "application");
9212 strcpy(type
, "octet-stream");
9215 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9218 * Auto-type the file...
9221 ipp_attribute_t
*doc_name
; /* document-name attribute */
9224 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
9226 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9227 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9228 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9232 filetype
= mimeType(MimeDatabase
, super
, type
);
9235 filetype
= mimeType(MimeDatabase
, super
, type
);
9239 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
9242 * Replace the document-format attribute value with the auto-typed or
9246 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9251 _cupsStrFree(format
->values
[0].string
.text
);
9253 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
9256 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9257 "document-format", NULL
, mimetype
);
9261 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9262 _("Unsupported format \'%s/%s\'!"), super
, type
);
9263 cupsdLogMessage(CUPSD_LOG_INFO
,
9264 "Hint: Do you have the raw file printing rules enabled?");
9267 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9268 "document-format", NULL
, format
->values
[0].string
.text
);
9273 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9275 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9278 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9279 _("Unsupported format \'%s\'!"), mimetype
);
9281 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9282 "document-format", NULL
, mimetype
);
9287 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9288 "send_document: request file type is %s/%s.",
9289 filetype
->super
, filetype
->type
);
9292 * Add the file to the job...
9297 if (add_file(con
, job
, filetype
, compression
))
9300 if (stat(con
->filename
, &fileinfo
))
9303 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9305 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9307 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
9308 IPP_TAG_INTEGER
)) != NULL
)
9309 attr
->values
[0].integer
+= kbytes
;
9311 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
9313 rename(con
->filename
, filename
);
9315 cupsdClearString(&con
->filename
);
9317 cupsdLogMessage(CUPSD_LOG_INFO
,
9318 "File of type %s/%s queued in job #%d by \"%s\".",
9319 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
9322 * Start the job if this is the last document...
9325 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9326 IPP_TAG_BOOLEAN
)) != NULL
&&
9327 attr
->values
[0].boolean
)
9330 * See if we need to add the ending sheet...
9333 if (cupsdTimeoutJob(job
))
9336 if (job
->state_value
== IPP_JOB_STOPPED
)
9338 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9339 job
->state_value
= IPP_JOB_PENDING
;
9341 else if (job
->state_value
== IPP_JOB_HELD
)
9343 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9344 IPP_TAG_KEYWORD
)) == NULL
)
9345 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9347 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9349 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9350 job
->state_value
= IPP_JOB_PENDING
;
9357 * Start the job if possible... Since cupsdCheckJobs() can cancel a
9358 * job if it doesn't print, we need to re-find the job afterwards...
9365 job
= cupsdFindJob(jobid
);
9369 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9370 IPP_TAG_KEYWORD
)) == NULL
)
9371 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9373 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9375 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9376 job
->state_value
= IPP_JOB_HELD
;
9377 job
->hold_until
= time(NULL
) + 60;
9383 * Fill in the response info...
9386 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
9389 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9392 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9394 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9395 job
? job
->state_value
: IPP_JOB_CANCELED
);
9396 add_job_state_reasons(con
, job
);
9398 con
->response
->request
.status
.status_code
= IPP_OK
;
9403 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9408 cupsd_client_t
*con
, /* I - Client connection */
9409 http_status_t status
, /* I - HTTP status code */
9410 cupsd_printer_t
*printer
) /* I - Printer, if any */
9412 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
9413 ippOpString(con
->request
->request
.op
.operation_id
),
9414 httpStatus(status
));
9416 if (status
== HTTP_UNAUTHORIZED
&&
9417 printer
&& printer
->num_auth_info_required
> 0 &&
9418 !strcmp(printer
->auth_info_required
[0], "negotiate"))
9419 cupsdSendError(con
, status
, CUPSD_AUTH_NEGOTIATE
);
9422 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9423 cupsd_location_t
*auth
; /* Pointer to authentication element */
9426 if (printer
->type
& CUPS_PRINTER_CLASS
)
9427 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9429 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9431 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9432 auth
->type
== CUPSD_AUTH_NONE
)
9433 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
, IPP_PRINT_JOB
);
9435 cupsdSendError(con
, status
, auth
? auth
->type
: CUPSD_AUTH_NONE
);
9438 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
9440 ippDelete(con
->response
);
9441 con
->response
= NULL
;
9448 * 'send_ipp_status()' - Send a status back to the IPP client.
9452 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9453 ipp_status_t status
, /* I - IPP status code */
9454 const char *message
, /* I - Status message */
9455 ...) /* I - Additional args as needed */
9457 va_list ap
; /* Pointer to additional args */
9458 char formatted
[1024]; /* Formatted errror message */
9461 va_start(ap
, message
);
9462 vsnprintf(formatted
, sizeof(formatted
),
9463 _cupsLangString(con
->language
, message
), ap
);
9466 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9467 ippOpString(con
->request
->request
.op
.operation_id
),
9468 ippErrorString(status
), formatted
);
9470 con
->response
->request
.status
.status_code
= status
;
9472 if (ippFindAttribute(con
->response
, "attributes-charset",
9473 IPP_TAG_ZERO
) == NULL
)
9474 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9475 "attributes-charset", NULL
, DefaultCharset
);
9477 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9478 IPP_TAG_ZERO
) == NULL
)
9479 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9480 "attributes-natural-language", NULL
, DefaultLanguage
);
9482 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9483 "status-message", NULL
, formatted
);
9488 * 'set_default()' - Set the default destination...
9492 set_default(cupsd_client_t
*con
, /* I - Client connection */
9493 ipp_attribute_t
*uri
) /* I - Printer URI */
9495 http_status_t status
; /* Policy status */
9496 cups_ptype_t dtype
; /* Destination type (printer/class) */
9497 cupsd_printer_t
*printer
; /* Printer */
9500 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9501 con
->http
.fd
, uri
->values
[0].string
.text
);
9504 * Is the destination valid?
9507 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9513 send_ipp_status(con
, IPP_NOT_FOUND
,
9514 _("The printer or class was not found."));
9522 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9524 send_http_error(con
, status
, NULL
);
9529 * Set it as the default...
9532 DefaultPrinter
= printer
;
9534 cupsdSaveAllPrinters();
9535 cupsdSaveAllClasses();
9537 cupsdWritePrintcap();
9539 cupsdLogMessage(CUPSD_LOG_INFO
,
9540 "Default destination set to \"%s\" by \"%s\".",
9541 printer
->name
, get_username(con
));
9544 * Everything was ok, so return OK status...
9547 con
->response
->request
.status
.status_code
= IPP_OK
;
9552 * 'set_job_attrs()' - Set job attributes.
9556 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9557 ipp_attribute_t
*uri
) /* I - Job URI */
9559 ipp_attribute_t
*attr
, /* Current attribute */
9560 *attr2
; /* Job attribute */
9561 int jobid
; /* Job ID */
9562 cupsd_job_t
*job
; /* Current job */
9563 char method
[HTTP_MAX_URI
],
9564 /* Method portion of URI */
9565 username
[HTTP_MAX_URI
],
9566 /* Username portion of URI */
9568 /* Host portion of URI */
9569 resource
[HTTP_MAX_URI
];
9570 /* Resource portion of URI */
9571 int port
; /* Port portion of URI */
9572 int event
; /* Events? */
9575 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
9576 con
->http
.fd
, uri
->values
[0].string
.text
);
9579 * Start with "everything is OK" status...
9582 con
->response
->request
.status
.status_code
= IPP_OK
;
9585 * See if we have a job URI or a printer URI...
9588 if (!strcmp(uri
->name
, "printer-uri"))
9591 * Got a printer URI; see if we also have a job-id attribute...
9594 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9595 IPP_TAG_INTEGER
)) == NULL
)
9597 send_ipp_status(con
, IPP_BAD_REQUEST
,
9598 _("Got a printer-uri attribute but no job-id!"));
9602 jobid
= attr
->values
[0].integer
;
9607 * Got a job URI; parse it to get the job ID...
9610 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9611 sizeof(method
), username
, sizeof(username
), host
,
9612 sizeof(host
), &port
, resource
, sizeof(resource
));
9614 if (strncmp(resource
, "/jobs/", 6))
9620 send_ipp_status(con
, IPP_BAD_REQUEST
,
9621 _("Bad job-uri attribute \"%s\"!"),
9622 uri
->values
[0].string
.text
);
9626 jobid
= atoi(resource
+ 6);
9630 * See if the job exists...
9633 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9636 * Nope - return a "not found" error...
9639 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9644 * See if the job has been completed...
9647 if (job
->state_value
> IPP_JOB_STOPPED
)
9650 * Return a "not-possible" error...
9653 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9654 _("Job #%d is finished and cannot be altered!"), jobid
);
9659 * See if the job is owned by the requesting user...
9662 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9664 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9669 * See what the user wants to change.
9676 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9678 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
9681 if (!strcmp(attr
->name
, "attributes-charset") ||
9682 !strcmp(attr
->name
, "attributes-natural-language") ||
9683 !strcmp(attr
->name
, "document-compression") ||
9684 !strcmp(attr
->name
, "document-format") ||
9685 !strcmp(attr
->name
, "job-detailed-status-messages") ||
9686 !strcmp(attr
->name
, "job-document-access-errors") ||
9687 !strcmp(attr
->name
, "job-id") ||
9688 !strcmp(attr
->name
, "job-k-octets") ||
9689 !strcmp(attr
->name
, "job-originating-host-name") ||
9690 !strcmp(attr
->name
, "job-originating-user-name") ||
9691 !strcmp(attr
->name
, "job-printer-up-time") ||
9692 !strcmp(attr
->name
, "job-printer-uri") ||
9693 !strcmp(attr
->name
, "job-sheets") ||
9694 !strcmp(attr
->name
, "job-state-message") ||
9695 !strcmp(attr
->name
, "job-state-reasons") ||
9696 !strcmp(attr
->name
, "job-uri") ||
9697 !strcmp(attr
->name
, "number-of-documents") ||
9698 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
9699 !strcmp(attr
->name
, "output-device-assigned") ||
9700 !strncmp(attr
->name
, "date-time-at-", 13) ||
9701 !strncmp(attr
->name
, "job-impressions", 15) ||
9702 !strncmp(attr
->name
, "job-k-octets", 12) ||
9703 !strncmp(attr
->name
, "job-media-sheets", 16) ||
9704 !strncmp(attr
->name
, "time-at-", 8))
9710 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
9711 _("%s cannot be changed."), attr
->name
);
9713 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9714 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9719 if (!strcmp(attr
->name
, "job-priority"))
9722 * Change the job priority...
9725 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9727 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
9729 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9730 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9732 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
9734 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9735 _("Job is completed and cannot be changed."));
9738 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9740 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
9741 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
9742 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
9745 else if (!strcmp(attr
->name
, "job-state"))
9748 * Change the job state...
9751 if (attr
->value_tag
!= IPP_TAG_ENUM
)
9753 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
9755 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9756 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9760 switch (attr
->values
[0].integer
)
9762 case IPP_JOB_PENDING
:
9764 if (job
->state_value
> IPP_JOB_HELD
)
9766 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9767 _("Job state cannot be changed."));
9770 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9772 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
9773 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
9775 event
|= CUPSD_EVENT_JOB_STATE
;
9779 case IPP_JOB_PROCESSING
:
9780 case IPP_JOB_STOPPED
:
9781 if (job
->state_value
!= attr
->values
[0].integer
)
9783 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9784 _("Job state cannot be changed."));
9789 case IPP_JOB_CANCELED
:
9790 case IPP_JOB_ABORTED
:
9791 case IPP_JOB_COMPLETED
:
9792 if (job
->state_value
> IPP_JOB_PROCESSING
)
9794 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9795 _("Job state cannot be changed."));
9798 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9799 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
9804 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
9806 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9807 IPP_TAG_ZERO
)) != NULL
)
9810 * Some other value; first free the old value...
9813 if (job
->attrs
->prev
)
9814 job
->attrs
->prev
->next
= attr2
->next
;
9816 job
->attrs
->attrs
= attr2
->next
;
9818 if (job
->attrs
->last
== attr2
)
9819 job
->attrs
->last
= job
->attrs
->prev
;
9821 _ippFreeAttr(attr2
);
9824 * Then copy the attribute...
9827 copy_attribute(job
->attrs
, attr
, 0);
9830 * See if the job-name or job-hold-until is being changed.
9833 if (!strcmp(attr
->name
, "job-hold-until"))
9835 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
9837 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
9838 cupsdReleaseJob(job
);
9842 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
9845 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
9848 * Delete the attribute...
9851 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9852 IPP_TAG_ZERO
)) != NULL
)
9854 if (job
->attrs
->prev
)
9855 job
->attrs
->prev
->next
= attr2
->next
;
9857 job
->attrs
->attrs
= attr2
->next
;
9859 if (attr2
== job
->attrs
->last
)
9860 job
->attrs
->last
= job
->attrs
->prev
;
9862 _ippFreeAttr(attr2
);
9864 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9870 * Add new option by copying it...
9873 copy_attribute(job
->attrs
, attr
, 0);
9875 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9886 * Send events as needed...
9889 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
9890 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
, job
->printer
, job
,
9891 "Job priority changed by user.");
9893 if (event
& CUPSD_EVENT_JOB_STATE
)
9894 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
9895 job
->state_value
== IPP_JOB_HELD
?
9896 "Job held by user." : "Job restarted by user.");
9898 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
9899 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
9900 "Job options changed by user.");
9903 * Start jobs if possible...
9911 * 'set_printer_defaults()' - Set printer default options from a request.
9915 set_printer_defaults(
9916 cupsd_client_t
*con
, /* I - Client connection */
9917 cupsd_printer_t
*printer
) /* I - Printer */
9919 int i
; /* Looping var */
9920 ipp_attribute_t
*attr
; /* Current attribute */
9921 int namelen
; /* Length of attribute name */
9922 char name
[256], /* New attribute name */
9923 value
[256]; /* String version of integer attrs */
9926 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9929 * Skip non-printer attributes...
9932 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
9935 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
9937 if (!strcmp(attr
->name
, "job-sheets-default"))
9940 * Only allow keywords and names...
9943 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9947 * Only allow job-sheets-default to be set when running without a
9948 * system high classification level...
9954 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
9956 if (attr
->num_values
> 1)
9957 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
9959 cupsdSetString(&printer
->job_sheets
[1], "none");
9961 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
9963 cupsdFreePrinterUsers(printer
);
9965 printer
->deny_users
= 0;
9967 if (attr
->value_tag
== IPP_TAG_NAME
&&
9968 (attr
->num_values
> 1 ||
9969 strcmp(attr
->values
[0].string
.text
, "all")))
9971 for (i
= 0; i
< attr
->num_values
; i
++)
9972 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9975 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
9977 cupsdFreePrinterUsers(printer
);
9979 printer
->deny_users
= 1;
9981 if (attr
->value_tag
== IPP_TAG_NAME
&&
9982 (attr
->num_values
> 1 ||
9983 strcmp(attr
->values
[0].string
.text
, "none")))
9985 for (i
= 0; i
< attr
->num_values
; i
++)
9986 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9989 else if (!strcmp(attr
->name
, "job-quota-period"))
9991 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9994 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
9995 attr
->values
[0].integer
);
9996 cupsdFreeQuotas(printer
);
9998 printer
->quota_period
= attr
->values
[0].integer
;
10000 else if (!strcmp(attr
->name
, "job-k-limit"))
10002 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10005 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10006 attr
->values
[0].integer
);
10007 cupsdFreeQuotas(printer
);
10009 printer
->k_limit
= attr
->values
[0].integer
;
10011 else if (!strcmp(attr
->name
, "job-page-limit"))
10013 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10016 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10017 attr
->values
[0].integer
);
10018 cupsdFreeQuotas(printer
);
10020 printer
->page_limit
= attr
->values
[0].integer
;
10022 else if (!strcmp(attr
->name
, "printer-op-policy"))
10024 cupsd_policy_t
*p
; /* Policy */
10027 if (attr
->value_tag
!= IPP_TAG_NAME
)
10030 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10032 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10033 "Setting printer-op-policy to \"%s\"...",
10034 attr
->values
[0].string
.text
);
10035 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10036 printer
->op_policy_ptr
= p
;
10040 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10041 _("Unknown printer-op-policy \"%s\"."),
10042 attr
->values
[0].string
.text
);
10046 else if (!strcmp(attr
->name
, "printer-error-policy"))
10048 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10051 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10052 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10053 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
10055 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10056 _("Unknown printer-error-policy \"%s\"."),
10057 attr
->values
[0].string
.text
);
10061 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10062 "Setting printer-error-policy to \"%s\"...",
10063 attr
->values
[0].string
.text
);
10064 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10068 * Skip any other non-default attributes...
10071 namelen
= strlen(attr
->name
);
10072 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10073 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10077 * OK, anything else must be a user-defined default...
10080 strlcpy(name
, attr
->name
, sizeof(name
));
10081 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10083 switch (attr
->value_tag
)
10085 case IPP_TAG_DELETEATTR
:
10086 printer
->num_options
= cupsRemoveOption(name
,
10087 printer
->num_options
,
10088 &(printer
->options
));
10089 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10090 "Deleting %s", attr
->name
);
10093 case IPP_TAG_NAME
:
10094 case IPP_TAG_KEYWORD
:
10096 printer
->num_options
= cupsAddOption(name
,
10097 attr
->values
[0].string
.text
,
10098 printer
->num_options
,
10099 &(printer
->options
));
10100 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10101 "Setting %s to \"%s\"...", attr
->name
,
10102 attr
->values
[0].string
.text
);
10105 case IPP_TAG_BOOLEAN
:
10106 printer
->num_options
= cupsAddOption(name
,
10107 attr
->values
[0].boolean
?
10109 printer
->num_options
,
10110 &(printer
->options
));
10111 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10112 "Setting %s to %s...", attr
->name
,
10113 attr
->values
[0].boolean
? "true" : "false");
10116 case IPP_TAG_INTEGER
:
10117 case IPP_TAG_ENUM
:
10118 sprintf(value
, "%d", attr
->values
[0].integer
);
10119 printer
->num_options
= cupsAddOption(name
, value
,
10120 printer
->num_options
,
10121 &(printer
->options
));
10122 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10123 "Setting %s to %s...", attr
->name
, value
);
10126 case IPP_TAG_RANGE
:
10127 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10128 attr
->values
[0].range
.upper
);
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
);
10136 case IPP_TAG_RESOLUTION
:
10137 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10138 attr
->values
[0].resolution
.yres
,
10139 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10141 printer
->num_options
= cupsAddOption(name
, value
,
10142 printer
->num_options
,
10143 &(printer
->options
));
10144 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10145 "Setting %s to %s...", attr
->name
, value
);
10149 /* Do nothing for other values */
10157 * 'start_printer()' - Start a printer.
10161 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10162 ipp_attribute_t
*uri
) /* I - Printer URI */
10164 http_status_t status
; /* Policy status */
10165 cups_ptype_t dtype
; /* Destination type (printer/class) */
10166 cupsd_printer_t
*printer
; /* Printer data */
10169 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10170 con
->http
.fd
, uri
->values
[0].string
.text
);
10173 * Is the destination valid?
10176 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10182 send_ipp_status(con
, IPP_NOT_FOUND
,
10183 _("The printer or class was not found."));
10191 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10193 send_http_error(con
, status
, printer
);
10198 * Start the printer...
10201 printer
->state_message
[0] = '\0';
10203 cupsdStartPrinter(printer
, 1);
10205 if (dtype
& CUPS_PRINTER_CLASS
)
10206 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10207 printer
->name
, get_username(con
));
10209 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10210 printer
->name
, get_username(con
));
10215 * Everything was ok, so return OK status...
10218 con
->response
->request
.status
.status_code
= IPP_OK
;
10223 * 'stop_printer()' - Stop a printer.
10227 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10228 ipp_attribute_t
*uri
) /* I - Printer URI */
10230 http_status_t status
; /* Policy status */
10231 cups_ptype_t dtype
; /* Destination type (printer/class) */
10232 cupsd_printer_t
*printer
; /* Printer data */
10233 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10236 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10237 con
->http
.fd
, uri
->values
[0].string
.text
);
10240 * Is the destination valid?
10243 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10249 send_ipp_status(con
, IPP_NOT_FOUND
,
10250 _("The printer or class was not found."));
10258 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10260 send_http_error(con
, status
, printer
);
10265 * Stop the printer...
10268 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10269 IPP_TAG_TEXT
)) == NULL
)
10270 strcpy(printer
->state_message
, "Paused");
10273 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10274 sizeof(printer
->state_message
));
10277 cupsdStopPrinter(printer
, 1);
10279 if (dtype
& CUPS_PRINTER_CLASS
)
10280 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10281 printer
->name
, get_username(con
));
10283 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10284 printer
->name
, get_username(con
));
10287 * Everything was ok, so return OK status...
10290 con
->response
->request
.status
.status_code
= IPP_OK
;
10295 * 'url_encode_attr()' - URL-encode a string attribute.
10299 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10300 char *buffer
,/* I - String buffer */
10301 int bufsize
)/* I - Size of buffer */
10303 int i
; /* Looping var */
10304 char *bufptr
, /* Pointer into buffer */
10305 *bufend
; /* End of buffer */
10308 strlcpy(buffer
, attr
->name
, bufsize
);
10309 bufptr
= buffer
+ strlen(buffer
);
10310 bufend
= buffer
+ bufsize
- 1;
10312 for (i
= 0; i
< attr
->num_values
; i
++)
10314 if (bufptr
>= bufend
)
10322 if (bufptr
>= bufend
)
10327 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
10328 bufptr
, bufend
- bufptr
+ 1);
10330 if (bufptr
>= bufend
)
10341 * 'url_encode_string()' - URL-encode a string.
10344 static char * /* O - End of string */
10345 url_encode_string(const char *s
, /* I - String */
10346 char *buffer
, /* I - String buffer */
10347 int bufsize
) /* I - Size of buffer */
10349 char *bufptr
, /* Pointer into buffer */
10350 *bufend
; /* End of buffer */
10351 static const char *hex
= "0123456789ABCDEF";
10356 bufend
= buffer
+ bufsize
- 1;
10358 while (*s
&& bufptr
< bufend
)
10360 if (*s
== ' ' || *s
== '%')
10362 if (bufptr
>= (bufend
- 2))
10366 *bufptr
++ = hex
[(*s
>> 4) & 15];
10367 *bufptr
++ = hex
[*s
& 15];
10371 else if (*s
== '\'' || *s
== '\\')
10373 if (bufptr
>= (bufend
- 1))
10390 * 'user_allowed()' - See if a user is allowed to print to a queue.
10393 static int /* O - 0 if not allowed, 1 if allowed */
10394 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
10395 const char *username
) /* I - Username */
10397 int i
; /* Looping var */
10398 struct passwd
*pw
; /* User password data */
10399 char baseuser
[256], /* Base username */
10400 *baseptr
; /* Pointer to "@" in base username */
10403 if (p
->num_users
== 0)
10406 if (!strcmp(username
, "root"))
10409 if (strchr(username
, '@'))
10412 * Strip @REALM for username check...
10415 strlcpy(baseuser
, username
, sizeof(baseuser
));
10417 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
10420 username
= baseuser
;
10423 pw
= getpwnam(username
);
10426 for (i
= 0; i
< p
->num_users
; i
++)
10428 if (p
->users
[i
][0] == '@')
10431 * Check group membership...
10434 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
10437 else if (!strcasecmp(username
, p
->users
[i
]))
10441 return ((i
< p
->num_users
) != p
->deny_users
);
10446 * 'validate_job()' - Validate printer options and destination.
10450 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10451 ipp_attribute_t
*uri
) /* I - Printer URI */
10453 http_status_t status
; /* Policy status */
10454 ipp_attribute_t
*attr
; /* Current attribute */
10455 ipp_attribute_t
*format
; /* Document-format attribute */
10456 cups_ptype_t dtype
; /* Destination type (printer/class) */
10457 char super
[MIME_MAX_SUPER
],
10458 /* Supertype of file */
10459 type
[MIME_MAX_TYPE
];
10460 /* Subtype of file */
10461 cupsd_printer_t
*printer
; /* Printer */
10464 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10465 con
->http
.fd
, uri
->values
[0].string
.text
);
10468 * OK, see if the client is sending the document compressed - CUPS
10469 * doesn't support compression yet...
10472 if ((attr
= ippFindAttribute(con
->request
, "compression",
10473 IPP_TAG_KEYWORD
)) != NULL
&&
10474 !strcmp(attr
->values
[0].string
.text
, "none"))
10476 send_ipp_status(con
, IPP_ATTRIBUTES
,
10477 _("Unsupported compression attribute %s!"),
10478 attr
->values
[0].string
.text
);
10479 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10480 "compression", NULL
, attr
->values
[0].string
.text
);
10485 * Is it a format we support?
10488 if ((format
= ippFindAttribute(con
->request
, "document-format",
10489 IPP_TAG_MIMETYPE
)) != NULL
)
10491 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
10494 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
10495 format
->values
[0].string
.text
);
10499 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10500 !mimeType(MimeDatabase
, super
, type
))
10502 cupsdLogMessage(CUPSD_LOG_INFO
,
10503 "Hint: Do you have the raw file printing rules enabled?");
10504 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10505 _("Unsupported format \"%s\"!"),
10506 format
->values
[0].string
.text
);
10507 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10508 "document-format", NULL
, format
->values
[0].string
.text
);
10514 * Is the destination valid?
10517 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10523 send_ipp_status(con
, IPP_NOT_FOUND
,
10524 _("The printer or class was not found."));
10532 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10534 send_http_error(con
, status
, printer
);
10539 * Everything was ok, so return OK status...
10542 con
->response
->request
.status
.status_code
= IPP_OK
;
10547 * 'validate_name()' - Make sure the printer name only contains valid chars.
10550 static int /* O - 0 if name is no good, 1 if good */
10551 validate_name(const char *name
) /* I - Name to check */
10553 const char *ptr
; /* Pointer into name */
10557 * Scan the whole name...
10560 for (ptr
= name
; *ptr
; ptr
++)
10561 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
10565 * All the characters are good; validate the length, too...
10568 return ((ptr
- name
) < 128);
10573 * 'validate_user()' - Validate the user for the request.
10576 static int /* O - 1 if permitted, 0 otherwise */
10577 validate_user(cupsd_job_t
*job
, /* I - Job */
10578 cupsd_client_t
*con
, /* I - Client connection */
10579 const char *owner
, /* I - Owner of job/resource */
10580 char *username
, /* O - Authenticated username */
10581 int userlen
) /* I - Length of username */
10583 cupsd_printer_t
*printer
; /* Printer for job */
10586 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
10587 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
10589 job
->id
, con
? con
->http
.fd
: 0,
10590 owner
? owner
: "(null)", username
, userlen
);
10593 * Validate input...
10596 if (!con
|| !owner
|| !username
|| userlen
<= 0)
10600 * Get the best authenticated username that is available.
10603 strlcpy(username
, get_username(con
), userlen
);
10606 * Check the username against the owner...
10609 printer
= cupsdFindDest(job
->dest
);
10611 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
10612 con
, owner
) == HTTP_OK
);
10617 * End of "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $".