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 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
34 * apply_printer_defaults() - Apply printer default options to a job.
35 * authenticate_job() - Set job authentication info.
36 * cancel_all_jobs() - Cancel all print jobs.
37 * cancel_job() - Cancel a print job.
38 * cancel_subscription() - Cancel a subscription.
39 * check_quotas() - Check quotas for a printer and user.
40 * copy_attribute() - Copy a single attribute.
41 * copy_attrs() - Copy attributes from one request to another.
42 * copy_banner() - Copy a banner file to the requests directory
43 * for the specified job.
44 * copy_file() - Copy a PPD file or interface script...
45 * copy_model() - Copy a PPD model file, substituting default
47 * copy_job_attrs() - Copy job attributes.
48 * copy_printer_attrs() - Copy printer attributes.
49 * copy_subscription_attrs() - Copy subscription attributes.
50 * create_job() - Print a file to a printer or class.
51 * create_requested_array() - Create an array for the requested-attributes.
52 * create_subscription() - Create a notification subscription.
53 * delete_printer() - Remove a printer or class from the system.
54 * get_default() - Get the default destination.
55 * get_devices() - Get the list of available devices on the
57 * get_document() - Get a copy of a job file.
58 * get_job_attrs() - Get job attributes.
59 * get_jobs() - Get a list of jobs for the specified printer.
60 * get_notifications() - Get events for a subscription.
61 * get_ppd() - Get a named PPD from the local system.
62 * get_ppds() - Get the list of PPD files on the local
64 * get_printer_attrs() - Get printer attributes.
65 * get_printers() - Get a list of printers.
66 * get_subscription_attrs() - Get subscription attributes.
67 * get_subscriptions() - Get subscriptions.
68 * get_username() - Get the username associated with a request.
69 * hold_job() - Hold a print job.
70 * move_job() - Move a job to a new destination.
71 * ppd_parse_line() - Parse a PPD default line.
72 * print_job() - Print a file to a printer or class.
73 * read_ps_line() - Read a line from a PS file...
74 * read_ps_job_ticket() - Reads a job ticket embedded in a PS file.
75 * reject_jobs() - Reject print jobs to a printer.
76 * release_job() - Release a held print job.
77 * restart_job() - Restart an old print job.
78 * save_auth_info() - Save authentication information for a job.
79 * save_krb5_creds() - Save Kerberos credentials for a job.
80 * send_document() - Send a file to a printer or class.
81 * send_http_error() - Send a HTTP error back to the IPP client.
82 * send_ipp_status() - Send a status back to the IPP client.
83 * set_default() - Set the default destination...
84 * set_job_attrs() - Set job attributes.
85 * set_printer_defaults() - Set printer default options from a request.
86 * start_printer() - Start a printer.
87 * stop_printer() - Stop a printer.
88 * url_encode_attr() - URL-encode a string attribute.
89 * url_encode_string() - URL-encode a string.
90 * user_allowed() - See if a user is allowed to print to a queue.
91 * validate_job() - Validate printer options and destination.
92 * validate_name() - Make sure the printer name only contains
94 * validate_user() - Validate the user for the request.
98 * Include necessary headers...
105 #endif /* HAVE_LIBPAPER */
108 # ifdef HAVE_MEMBERSHIP_H
109 # include <membership.h>
110 # endif /* HAVE_MEMBERSHIP_H */
111 # ifdef HAVE_MEMBERSHIPPRIV_H
112 # include <membershipPriv.h>
114 extern int mbr_user_name_to_uuid(const char* name
, uuid_t uu
);
115 extern int mbr_group_name_to_uuid(const char* name
, uuid_t uu
);
116 extern int mbr_check_membership_by_id(uuid_t user
, gid_t group
, int* ismember
);
117 # endif /* HAVE_MEMBERSHIPPRIV_H */
118 #endif /* __APPLE__ */
125 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
126 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
127 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
128 mime_type_t
*filetype
, int compression
);
129 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
130 mime_type_t
*filetype
);
131 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
132 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
133 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
134 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
135 static void add_printer_state_reasons(cupsd_client_t
*con
,
137 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
138 static void apply_printer_defaults(cupsd_printer_t
*printer
,
140 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
141 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
142 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
143 static void cancel_subscription(cupsd_client_t
*con
, int id
);
144 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
145 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
147 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
148 ipp_tag_t group
, int quickcopy
);
149 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
151 static int copy_file(const char *from
, const char *to
);
152 static int copy_model(cupsd_client_t
*con
, const char *from
,
154 static void copy_job_attrs(cupsd_client_t
*con
,
157 static void copy_printer_attrs(cupsd_client_t
*con
,
158 cupsd_printer_t
*printer
,
160 static void copy_subscription_attrs(cupsd_client_t
*con
,
161 cupsd_subscription_t
*sub
,
163 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
164 static cups_array_t
*create_requested_array(ipp_t
*request
);
165 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
166 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
167 static void get_default(cupsd_client_t
*con
);
168 static void get_devices(cupsd_client_t
*con
);
169 static void get_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
170 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
171 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
172 static void get_notifications(cupsd_client_t
*con
);
173 static void get_ppd(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
174 static void get_ppds(cupsd_client_t
*con
);
175 static void get_printers(cupsd_client_t
*con
, int type
);
176 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
177 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
178 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
179 static const char *get_username(cupsd_client_t
*con
);
180 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
181 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
182 static int ppd_parse_line(const char *line
, char *option
, int olen
,
183 char *choice
, int clen
);
184 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
185 static void read_ps_job_ticket(cupsd_client_t
*con
);
186 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
187 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
188 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
189 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
190 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
,
191 ipp_attribute_t
*auth_info
);
192 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
193 static void save_krb5_creds(cupsd_client_t
*con
, cupsd_job_t
*job
);
194 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
195 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
196 static void send_http_error(cupsd_client_t
*con
, http_status_t status
,
197 cupsd_printer_t
*printer
);
198 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
199 const char *message
, ...)
201 __attribute__ ((__format__ (__printf__
, 3, 4)))
202 # endif /* __GNUC__ */
204 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
205 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
206 static void set_printer_defaults(cupsd_client_t
*con
,
207 cupsd_printer_t
*printer
);
208 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
209 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
210 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
,
212 static char *url_encode_string(const char *s
, char *buffer
, int bufsize
);
213 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
214 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
215 static int validate_name(const char *name
);
216 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
,
217 const char *owner
, char *username
,
222 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
225 int /* O - 1 on success, 0 on failure */
226 cupsdProcessIPPRequest(
227 cupsd_client_t
*con
) /* I - Client connection */
229 ipp_tag_t group
; /* Current group tag */
230 ipp_attribute_t
*attr
; /* Current attribute */
231 ipp_attribute_t
*charset
; /* Character set attribute */
232 ipp_attribute_t
*language
; /* Language attribute */
233 ipp_attribute_t
*uri
; /* Printer URI attribute */
234 ipp_attribute_t
*username
; /* requesting-user-name attr */
235 int sub_id
; /* Subscription ID */
238 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
239 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
240 con
, con
->http
.fd
, con
->request
->request
.op
.operation_id
);
243 * First build an empty response message for this request...
246 con
->response
= ippNew();
248 con
->response
->request
.status
.version
[0] =
249 con
->request
->request
.op
.version
[0];
250 con
->response
->request
.status
.version
[1] =
251 con
->request
->request
.op
.version
[1];
252 con
->response
->request
.status
.request_id
=
253 con
->request
->request
.op
.request_id
;
256 * Then validate the request header and required attributes...
259 if (con
->request
->request
.any
.version
[0] != 1)
262 * Return an error, since we only support IPP 1.x.
265 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
266 "%04X %s Bad request version number %d.%d",
267 IPP_VERSION_NOT_SUPPORTED
, con
->http
.hostname
,
268 con
->request
->request
.any
.version
[0],
269 con
->request
->request
.any
.version
[1]);
271 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
272 _("Bad request version number %d.%d!"),
273 con
->request
->request
.any
.version
[0],
274 con
->request
->request
.any
.version
[1]);
276 else if (!con
->request
->attrs
)
278 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
279 "%04X %s No attributes in request",
280 IPP_BAD_REQUEST
, con
->http
.hostname
);
282 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request!"));
287 * Make sure that the attributes are provided in the correct order and
288 * don't repeat groups...
291 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
294 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
297 * Out of order; return an error...
300 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
301 "%04X %s Attribute groups are out of order",
302 IPP_BAD_REQUEST
, con
->http
.hostname
);
304 send_ipp_status(con
, IPP_BAD_REQUEST
,
305 _("Attribute groups are out of order (%x < %x)!"),
306 attr
->group_tag
, group
);
310 group
= attr
->group_tag
;
315 * Then make sure that the first three attributes are:
318 * attributes-natural-language
319 * printer-uri/job-uri
322 attr
= con
->request
->attrs
;
323 if (attr
&& !strcmp(attr
->name
, "attributes-charset") &&
324 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
332 if (attr
&& !strcmp(attr
->name
, "attributes-natural-language") &&
333 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
338 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
339 IPP_TAG_URI
)) != NULL
)
341 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
342 IPP_TAG_URI
)) != NULL
)
344 else if (con
->request
->request
.op
.operation_id
== CUPS_GET_PPD
)
345 uri
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
);
350 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
351 "attributes-charset", NULL
,
352 charset
->values
[0].string
.text
);
354 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
355 "attributes-charset", NULL
, DefaultCharset
);
358 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
359 "attributes-natural-language", NULL
,
360 language
->values
[0].string
.text
);
362 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
363 "attributes-natural-language", NULL
, DefaultLanguage
);
366 strcasecmp(charset
->values
[0].string
.text
, "us-ascii") &&
367 strcasecmp(charset
->values
[0].string
.text
, "utf-8"))
370 * Bad character set...
373 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unsupported character set \"%s\"!",
374 charset
->values
[0].string
.text
);
375 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
376 "%04X %s Unsupported attributes-charset value \"%s\"",
377 IPP_CHARSET
, con
->http
.hostname
,
378 charset
->values
[0].string
.text
);
379 send_ipp_status(con
, IPP_BAD_REQUEST
,
380 _("Unsupported character set \"%s\"!"),
381 charset
->values
[0].string
.text
);
383 else if (!charset
|| !language
||
385 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
386 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
387 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
388 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
389 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
392 * Return an error, since attributes-charset,
393 * attributes-natural-language, and printer-uri/job-uri are required
394 * for all operations.
399 cupsdLogMessage(CUPSD_LOG_ERROR
,
400 "Missing attributes-charset attribute!");
402 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
403 "%04X %s Missing attributes-charset attribute",
404 IPP_BAD_REQUEST
, con
->http
.hostname
);
409 cupsdLogMessage(CUPSD_LOG_ERROR
,
410 "Missing attributes-natural-language attribute!");
412 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
413 "%04X %s Missing attributes-natural-language attribute",
414 IPP_BAD_REQUEST
, con
->http
.hostname
);
419 cupsdLogMessage(CUPSD_LOG_ERROR
,
420 "Missing printer-uri, job-uri, or ppd-name "
423 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
424 "%04X %s Missing printer-uri, job-uri, or ppd-name "
425 "attribute", IPP_BAD_REQUEST
, con
->http
.hostname
);
428 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
430 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
431 cupsdLogMessage(CUPSD_LOG_DEBUG
,
432 "attr \"%s\": group_tag = %x, value_tag = %x",
433 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
436 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
438 send_ipp_status(con
, IPP_BAD_REQUEST
,
439 _("Missing required attributes!"));
444 * OK, all the checks pass so far; make sure requesting-user-name is
445 * not "root" from a remote host...
448 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
449 IPP_TAG_NAME
)) != NULL
)
452 * Check for root user...
455 if (!strcmp(username
->values
[0].string
.text
, "root") &&
456 strcasecmp(con
->http
.hostname
, "localhost") &&
457 strcmp(con
->username
, "root"))
460 * Remote unauthenticated user masquerading as local root...
463 _cupsStrFree(username
->values
[0].string
.text
);
464 username
->values
[0].string
.text
= _cupsStrAlloc(RemoteRoot
);
468 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
469 IPP_TAG_INTEGER
)) != NULL
)
470 sub_id
= attr
->values
[0].integer
;
475 * Then try processing the operation...
479 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
480 ippOpString(con
->request
->request
.op
.operation_id
),
481 uri
->values
[0].string
.text
);
483 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
484 ippOpString(con
->request
->request
.op
.operation_id
));
486 switch (con
->request
->request
.op
.operation_id
)
492 case IPP_VALIDATE_JOB
:
493 validate_job(con
, uri
);
496 case IPP_CREATE_JOB
:
497 create_job(con
, uri
);
500 case IPP_SEND_DOCUMENT
:
501 send_document(con
, uri
);
504 case IPP_CANCEL_JOB
:
505 cancel_job(con
, uri
);
508 case IPP_GET_JOB_ATTRIBUTES
:
509 get_job_attrs(con
, uri
);
516 case IPP_GET_PRINTER_ATTRIBUTES
:
517 get_printer_attrs(con
, uri
);
524 case IPP_RELEASE_JOB
:
525 release_job(con
, uri
);
528 case IPP_RESTART_JOB
:
529 restart_job(con
, uri
);
532 case IPP_PAUSE_PRINTER
:
533 stop_printer(con
, uri
);
536 case IPP_RESUME_PRINTER
:
537 start_printer(con
, uri
);
540 case IPP_PURGE_JOBS
:
541 cancel_all_jobs(con
, uri
);
544 case IPP_SET_JOB_ATTRIBUTES
:
545 set_job_attrs(con
, uri
);
548 case CUPS_GET_DEFAULT
:
552 case CUPS_GET_PRINTERS
:
553 get_printers(con
, 0);
556 case CUPS_GET_CLASSES
:
557 get_printers(con
, CUPS_PRINTER_CLASS
);
560 case CUPS_ADD_PRINTER
:
561 add_printer(con
, uri
);
564 case CUPS_DELETE_PRINTER
:
565 delete_printer(con
, uri
);
568 case CUPS_ADD_CLASS
:
572 case CUPS_DELETE_CLASS
:
573 delete_printer(con
, uri
);
576 case CUPS_ACCEPT_JOBS
:
577 case IPP_ENABLE_PRINTER
:
578 accept_jobs(con
, uri
);
581 case CUPS_REJECT_JOBS
:
582 case IPP_DISABLE_PRINTER
:
583 reject_jobs(con
, uri
);
586 case CUPS_SET_DEFAULT
:
587 set_default(con
, uri
);
590 case CUPS_GET_DEVICES
:
594 case CUPS_GET_DOCUMENT
:
595 get_document(con
, uri
);
610 case CUPS_AUTHENTICATE_JOB
:
611 authenticate_job(con
, uri
);
614 case IPP_CREATE_PRINTER_SUBSCRIPTION
:
615 case IPP_CREATE_JOB_SUBSCRIPTION
:
616 create_subscription(con
, uri
);
619 case IPP_GET_SUBSCRIPTION_ATTRIBUTES
:
620 get_subscription_attrs(con
, sub_id
);
623 case IPP_GET_SUBSCRIPTIONS
:
624 get_subscriptions(con
, uri
);
627 case IPP_RENEW_SUBSCRIPTION
:
628 renew_subscription(con
, sub_id
);
631 case IPP_CANCEL_SUBSCRIPTION
:
632 cancel_subscription(con
, sub_id
);
635 case IPP_GET_NOTIFICATIONS
:
636 get_notifications(con
);
640 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
641 "%04X %s Operation %04X (%s) not supported",
642 IPP_OPERATION_NOT_SUPPORTED
, con
->http
.hostname
,
643 con
->request
->request
.op
.operation_id
,
644 ippOpString(con
->request
->request
.op
.operation_id
));
646 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
647 _("%s not supported!"),
649 con
->request
->request
.op
.operation_id
));
659 * Sending data from the scheduler...
662 cupsdLogMessage(CUPSD_LOG_DEBUG
,
663 "cupsdProcessIPPRequest: %d status_code=%x (%s)",
664 con
->http
.fd
, con
->response
->request
.status
.status_code
,
665 ippErrorString(con
->response
->request
.status
.status_code
));
667 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp", AUTH_NONE
))
669 #ifdef CUPSD_USE_CHUNKING
671 * Because older versions of CUPS (1.1.17 and older) and some IPP
672 * clients do not implement chunking properly, we cannot use
673 * chunking by default. This may become the default in future
674 * CUPS releases, or we might add a configuration directive for
678 if (con
->http
.version
== HTTP_1_1
)
680 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n") < 0)
683 if (cupsdFlushHeader(con
) < 0)
686 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
689 #endif /* CUPSD_USE_CHUNKING */
691 size_t length
; /* Length of response */
694 length
= ippLength(con
->response
);
696 if (con
->file
>= 0 && !con
->pipe_pid
)
698 struct stat fileinfo
; /* File information */
701 if (!fstat(con
->file
, &fileinfo
))
702 length
+= fileinfo
.st_size
;
705 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
706 CUPS_LLCAST length
) < 0)
709 if (cupsdFlushHeader(con
) < 0)
712 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
713 con
->http
.data_remaining
= length
;
715 if (con
->http
.data_remaining
<= INT_MAX
)
716 con
->http
._data_remaining
= con
->http
.data_remaining
;
718 con
->http
._data_remaining
= INT_MAX
;
721 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
722 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
725 * Tell the caller the response header was sent successfully...
733 * Tell the caller the response header could not be sent...
742 * Sending data from a subprocess like cups-deviced; tell the caller
743 * everything is A-OK so far...
752 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
756 cupsdTimeoutJob(cupsd_job_t
*job
) /* I - Job to timeout */
758 cupsd_printer_t
*printer
; /* Destination printer or class */
759 ipp_attribute_t
*attr
; /* job-sheets attribute */
760 int kbytes
; /* Kilobytes in banner */
763 job
->pending_timeout
= 0;
766 * See if we need to add the ending sheet...
769 printer
= cupsdFindDest(job
->dest
);
770 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
773 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
774 attr
&& attr
->num_values
> 1)
780 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Adding end banner page \"%s\".",
781 job
->id
, attr
->values
[1].string
.text
);
783 kbytes
= copy_banner(NULL
, job
, attr
->values
[1].string
.text
);
785 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
791 * 'accept_jobs()' - Accept print jobs to a printer.
795 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
796 ipp_attribute_t
*uri
) /* I - Printer or class URI */
798 http_status_t status
; /* Policy status */
799 cups_ptype_t dtype
; /* Destination type (printer/class) */
800 cupsd_printer_t
*printer
; /* Printer data */
803 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
804 con
->http
.fd
, uri
->values
[0].string
.text
);
807 * Is the destination valid?
810 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
816 send_ipp_status(con
, IPP_NOT_FOUND
,
817 _("The printer or class was not found."));
825 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
827 send_http_error(con
, status
, printer
);
832 * Accept jobs sent to the printer...
835 printer
->accepting
= 1;
836 printer
->state_message
[0] = '\0';
838 cupsdAddPrinterHistory(printer
);
840 if (dtype
& CUPS_PRINTER_CLASS
)
842 cupsdSaveAllClasses();
844 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
845 printer
->name
, get_username(con
));
849 cupsdSaveAllPrinters();
851 cupsdLogMessage(CUPSD_LOG_INFO
,
852 "Printer \"%s\" now accepting jobs (\"%s\").",
853 printer
->name
, get_username(con
));
857 * Everything was ok, so return OK status...
860 con
->response
->request
.status
.status_code
= IPP_OK
;
865 * 'add_class()' - Add a class to the system.
869 add_class(cupsd_client_t
*con
, /* I - Client connection */
870 ipp_attribute_t
*uri
) /* I - URI of class */
872 http_status_t status
; /* Policy status */
873 int i
; /* Looping var */
874 char method
[HTTP_MAX_URI
], /* Method portion of URI */
875 username
[HTTP_MAX_URI
], /* Username portion of URI */
876 host
[HTTP_MAX_URI
], /* Host portion of URI */
877 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
878 int port
; /* Port portion of URI */
879 cupsd_printer_t
*pclass
, /* Class */
880 *member
; /* Member printer/class */
881 cups_ptype_t dtype
; /* Destination type */
882 ipp_attribute_t
*attr
; /* Printer attribute */
883 int modify
; /* Non-zero if we just modified */
884 char newname
[IPP_MAX_NAME
]; /* New class name */
885 int need_restart_job
; /* Need to restart job? */
888 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
889 con
->http
.fd
, uri
->values
[0].string
.text
);
892 * Do we have a valid URI?
895 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
896 sizeof(method
), username
, sizeof(username
), host
,
897 sizeof(host
), &port
, resource
, sizeof(resource
));
900 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
903 * No, return an error...
906 send_ipp_status(con
, IPP_BAD_REQUEST
,
907 _("The printer-uri must be of the form "
908 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
913 * Do we have a valid printer name?
916 if (!validate_name(resource
+ 9))
919 * No, return an error...
922 send_ipp_status(con
, IPP_BAD_REQUEST
,
923 _("The printer-uri \"%s\" contains invalid characters."),
924 uri
->values
[0].string
.text
);
929 * See if the class already exists; if not, create a new class...
932 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
935 * Class doesn't exist; see if we have a printer of the same name...
938 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
939 !(pclass
->type
& CUPS_PRINTER_DISCOVERED
))
942 * Yes, return an error...
945 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
946 _("A printer named \"%s\" already exists!"),
952 * No, check the default policy and then add the class...
955 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
957 send_http_error(con
, status
, NULL
);
961 pclass
= cupsdAddClass(resource
+ 9);
964 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
967 * Check the default policy, then rename the implicit class to "AnyClass"
971 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
973 send_http_error(con
, status
, NULL
);
977 if (ImplicitAnyClasses
)
979 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
980 cupsdRenamePrinter(pclass
, newname
);
983 cupsdDeletePrinter(pclass
, 1);
986 * Add the class as a new local class...
989 pclass
= cupsdAddClass(resource
+ 9);
992 else if (pclass
->type
& CUPS_PRINTER_DISCOVERED
)
995 * Check the default policy, then rename the remote class to "Class"...
998 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
1000 send_http_error(con
, status
, NULL
);
1004 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
1005 cupsdRenamePrinter(pclass
, newname
);
1008 * Add the class as a new local class...
1011 pclass
= cupsdAddClass(resource
+ 9);
1014 else if ((status
= cupsdCheckPolicy(pclass
->op_policy_ptr
, con
,
1017 send_http_error(con
, status
, pclass
);
1024 * Look for attributes and copy them over as needed...
1027 need_restart_job
= 0;
1029 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
1030 IPP_TAG_TEXT
)) != NULL
)
1031 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
1033 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
1034 IPP_TAG_TEXT
)) != NULL
)
1035 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
1037 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
1038 IPP_TAG_BOOLEAN
)) != NULL
)
1040 cupsdLogMessage(CUPSD_LOG_INFO
,
1041 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
1042 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
1044 pclass
->accepting
= attr
->values
[0].boolean
;
1045 cupsdAddPrinterHistory(pclass
);
1048 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
1049 IPP_TAG_BOOLEAN
)) != NULL
)
1051 if (pclass
->shared
&& !attr
->values
[0].boolean
)
1052 cupsdDeregisterPrinter(pclass
, 1);
1054 cupsdLogMessage(CUPSD_LOG_INFO
,
1055 "Setting %s printer-is-shared to %d (was %d.)",
1056 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
1058 pclass
->shared
= attr
->values
[0].boolean
;
1061 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
1062 IPP_TAG_ENUM
)) != NULL
)
1064 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
1065 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
1067 send_ipp_status(con
, IPP_BAD_REQUEST
,
1068 _("Attempt to set %s printer-state to bad value %d!"),
1069 pclass
->name
, attr
->values
[0].integer
);
1073 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
1074 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
1076 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1077 cupsdStopPrinter(pclass
, 0);
1080 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1081 need_restart_job
= 1;
1084 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1085 IPP_TAG_TEXT
)) != NULL
)
1087 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1088 sizeof(pclass
->state_message
));
1089 cupsdAddPrinterHistory(pclass
);
1091 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1092 IPP_TAG_URI
)) != NULL
)
1095 * Clear the printer array as needed...
1098 need_restart_job
= 1;
1100 if (pclass
->num_printers
> 0)
1102 free(pclass
->printers
);
1103 pclass
->num_printers
= 0;
1107 * Add each printer or class that is listed...
1110 for (i
= 0; i
< attr
->num_values
; i
++)
1113 * Search for the printer or class URI...
1116 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1122 send_ipp_status(con
, IPP_NOT_FOUND
,
1123 _("The printer or class was not found."));
1128 * Add it to the class...
1131 cupsdAddPrinterToClass(pclass
, member
);
1135 set_printer_defaults(con
, pclass
);
1137 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1138 IPP_TAG_KEYWORD
)) != NULL
)
1139 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1142 * Update the printer class attributes and return...
1145 cupsdSetPrinterAttrs(pclass
);
1146 cupsdSaveAllClasses();
1148 if (need_restart_job
&& pclass
->job
)
1153 * Stop the current job and then restart it below...
1156 job
= (cupsd_job_t
*)pclass
->job
;
1158 cupsdStopJob(job
, 1);
1160 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1161 job
->state_value
= IPP_JOB_PENDING
;
1164 if (need_restart_job
)
1167 cupsdWritePrintcap();
1171 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1172 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1175 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1176 pclass
->name
, get_username(con
));
1180 cupsdAddPrinterHistory(pclass
);
1182 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1183 "New class \"%s\" added by \"%s\".", pclass
->name
,
1186 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1187 pclass
->name
, get_username(con
));
1190 con
->response
->request
.status
.status_code
= IPP_OK
;
1195 * 'add_file()' - Add a file to a job.
1198 static int /* O - 0 on success, -1 on error */
1199 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1200 cupsd_job_t
*job
, /* I - Job to add to */
1201 mime_type_t
*filetype
, /* I - Type of file */
1202 int compression
) /* I - Compression */
1204 mime_type_t
**filetypes
; /* New filetypes array... */
1205 int *compressions
; /* New compressions array... */
1208 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1209 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1210 "compression=%d)", con
, con
? con
->http
.fd
: -1, job
->id
,
1211 filetype
->super
, filetype
->type
, compression
);
1214 * Add the file to the job...
1217 if (job
->num_files
== 0)
1219 compressions
= (int *)malloc(sizeof(int));
1220 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1224 compressions
= (int *)realloc(job
->compressions
,
1225 (job
->num_files
+ 1) * sizeof(int));
1226 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1227 (job
->num_files
+ 1) *
1228 sizeof(mime_type_t
*));
1231 if (!compressions
|| !filetypes
)
1233 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
1236 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1237 _("Unable to allocate memory for file types!"));
1242 job
->compressions
= compressions
;
1243 job
->compressions
[job
->num_files
] = compression
;
1244 job
->filetypes
= filetypes
;
1245 job
->filetypes
[job
->num_files
] = filetype
;
1254 * 'add_job()' - Add a job to a print queue.
1257 static cupsd_job_t
* /* O - Job object */
1258 add_job(cupsd_client_t
*con
, /* I - Client connection */
1259 cupsd_printer_t
*printer
, /* I - Destination printer */
1260 mime_type_t
*filetype
) /* I - First print file type, if any */
1262 http_status_t status
; /* Policy status */
1263 ipp_attribute_t
*attr
, /* Current attribute */
1264 *auth_info
; /* auth-info attribute */
1265 const char *val
; /* Default option value */
1266 int priority
; /* Job priority */
1267 char *title
; /* Job name/title */
1268 cupsd_job_t
*job
; /* Current job */
1269 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1270 int kbytes
; /* Size of print file */
1271 int i
; /* Looping var */
1272 int lowerpagerange
; /* Page range bound */
1275 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1276 con
, con
->http
.fd
, printer
, printer
->name
,
1277 filetype
, filetype
? filetype
->super
: "none",
1278 filetype
? filetype
->type
: "none");
1281 * Check remote printing to non-shared printer...
1284 if (!printer
->shared
&&
1285 strcasecmp(con
->http
.hostname
, "localhost") &&
1286 strcasecmp(con
->http
.hostname
, ServerName
))
1288 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1289 _("The printer or class is not shared!"));
1297 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1299 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1301 send_http_error(con
, status
, printer
);
1304 else if ((printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
1305 !con
->username
[0] && !auth_info
)
1307 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1311 else if (auth_info
&& !con
->http
.tls
&&
1312 !httpAddrLocalhost(con
->http
.hostaddr
))
1315 * Require encryption of auth-info over non-local connections...
1318 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1321 #endif /* HAVE_SSL */
1324 * See if the printer is accepting jobs...
1327 if (!printer
->accepting
)
1329 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1330 _("Destination \"%s\" is not accepting jobs."),
1336 * Validate job template attributes; for now just document-format,
1337 * copies, number-up, and page-ranges...
1340 if (filetype
&& printer
->filetypes
&&
1341 !cupsArrayFind(printer
->filetypes
, filetype
))
1343 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1344 /* MIME media type string */
1347 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1350 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1351 _("Unsupported format \'%s\'!"), mimetype
);
1353 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1354 "document-format", NULL
, mimetype
);
1359 if ((attr
= ippFindAttribute(con
->request
, "copies",
1360 IPP_TAG_INTEGER
)) != NULL
)
1362 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1364 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1365 attr
->values
[0].integer
);
1366 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1367 "copies", attr
->values
[0].integer
);
1372 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1373 IPP_TAG_INTEGER
)) != NULL
)
1375 if (attr
->values
[0].integer
!= 1 &&
1376 attr
->values
[0].integer
!= 2 &&
1377 attr
->values
[0].integer
!= 4 &&
1378 attr
->values
[0].integer
!= 6 &&
1379 attr
->values
[0].integer
!= 9 &&
1380 attr
->values
[0].integer
!= 16)
1382 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1383 attr
->values
[0].integer
);
1384 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1385 "number-up", attr
->values
[0].integer
);
1390 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1391 IPP_TAG_RANGE
)) != NULL
)
1393 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1395 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1396 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1398 send_ipp_status(con
, IPP_BAD_REQUEST
,
1399 _("Bad page-ranges values %d-%d."),
1400 attr
->values
[i
].range
.lower
,
1401 attr
->values
[i
].range
.upper
);
1405 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1410 * Make sure we aren't over our limit...
1413 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1416 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1418 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1419 _("Too many active jobs."));
1423 if ((i
= check_quotas(con
, printer
)) < 0)
1425 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1430 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1435 * Create the job and set things up...
1438 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1439 IPP_TAG_INTEGER
)) != NULL
)
1440 priority
= attr
->values
[0].integer
;
1443 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1444 printer
->options
)) != NULL
)
1445 priority
= atoi(val
);
1449 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1453 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1454 IPP_TAG_NAME
)) != NULL
)
1455 title
= attr
->values
[0].string
.text
;
1457 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1458 title
= "Untitled");
1460 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1462 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1463 _("Unable to add job for destination \"%s\"!"),
1468 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
1469 CUPS_PRINTER_REMOTE
);
1470 job
->attrs
= con
->request
;
1471 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1473 add_job_uuid(con
, job
);
1474 apply_printer_defaults(printer
, job
);
1476 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1478 if (con
->username
[0])
1480 cupsdSetString(&job
->username
, con
->username
);
1483 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1487 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1488 "add_job: requesting-user-name=\"%s\"",
1489 attr
->values
[0].string
.text
);
1491 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1494 cupsdSetString(&job
->username
, "anonymous");
1497 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1498 "job-originating-user-name", NULL
, job
->username
);
1501 attr
->group_tag
= IPP_TAG_JOB
;
1502 _cupsStrFree(attr
->name
);
1503 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1506 if (con
->username
[0] || auth_info
)
1508 save_auth_info(con
, job
, auth_info
);
1511 * Remove the auth-info attribute from the attribute data...
1515 ippDeleteAttribute(job
->attrs
, auth_info
);
1518 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1519 IPP_TAG_ZERO
)) != NULL
)
1522 * Request contains a job-originating-host-name attribute; validate it...
1525 if (attr
->value_tag
!= IPP_TAG_NAME
||
1526 attr
->num_values
!= 1 ||
1527 strcmp(con
->http
.hostname
, "localhost"))
1530 * Can't override the value if we aren't connected via localhost.
1531 * Also, we can only have 1 value and it must be a name value.
1534 switch (attr
->value_tag
)
1536 case IPP_TAG_STRING
:
1537 case IPP_TAG_TEXTLANG
:
1538 case IPP_TAG_NAMELANG
:
1541 case IPP_TAG_KEYWORD
:
1543 case IPP_TAG_URISCHEME
:
1544 case IPP_TAG_CHARSET
:
1545 case IPP_TAG_LANGUAGE
:
1546 case IPP_TAG_MIMETYPE
:
1548 * Free old strings...
1551 for (i
= 0; i
< attr
->num_values
; i
++)
1553 _cupsStrFree(attr
->values
[i
].string
.text
);
1554 attr
->values
[i
].string
.text
= NULL
;
1555 if (attr
->values
[i
].string
.charset
)
1557 _cupsStrFree(attr
->values
[i
].string
.charset
);
1558 attr
->values
[i
].string
.charset
= NULL
;
1567 * Use the default connection hostname instead...
1570 attr
->value_tag
= IPP_TAG_NAME
;
1571 attr
->num_values
= 1;
1572 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1575 attr
->group_tag
= IPP_TAG_JOB
;
1580 * No job-originating-host-name attribute, so use the hostname from
1584 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1585 "job-originating-host-name", NULL
, con
->http
.hostname
);
1588 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1590 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1591 "time-at-processing", 0);
1592 attr
->value_tag
= IPP_TAG_NOVALUE
;
1593 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1594 "time-at-completed", 0);
1595 attr
->value_tag
= IPP_TAG_NOVALUE
;
1598 * Add remaining job attributes...
1601 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1602 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1603 "job-state", IPP_JOB_STOPPED
);
1604 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1605 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1606 "job-media-sheets-completed", 0);
1607 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1609 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1612 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1613 IPP_TAG_INTEGER
)) != NULL
)
1614 attr
->values
[0].integer
= 0;
1616 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1619 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1620 IPP_TAG_KEYWORD
)) == NULL
)
1621 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1624 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1625 printer
->options
)) == NULL
)
1628 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1629 "job-hold-until", NULL
, val
);
1631 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1632 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1635 * Hold job until specified time...
1638 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1640 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1641 job
->state_value
= IPP_JOB_HELD
;
1643 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1645 job
->hold_until
= time(NULL
) + 60;
1646 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1647 job
->state_value
= IPP_JOB_HELD
;
1651 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1652 job
->state_value
= IPP_JOB_PENDING
;
1655 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1659 * Add job sheets options...
1662 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1663 IPP_TAG_ZERO
)) == NULL
)
1665 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1666 "Adding default job-sheets values \"%s,%s\"...",
1667 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1669 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1671 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1672 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1675 job
->job_sheets
= attr
;
1678 * Enforce classification level if set...
1683 cupsdLogMessage(CUPSD_LOG_INFO
,
1684 "Classification=\"%s\", ClassifyOverride=%d",
1685 Classification
? Classification
: "(null)",
1688 if (ClassifyOverride
)
1690 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1691 (attr
->num_values
== 1 ||
1692 !strcmp(attr
->values
[1].string
.text
, "none")))
1695 * Force the leading banner to have the classification on it...
1698 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1700 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1701 "job-sheets=\"%s,none\", "
1702 "job-originating-user-name=\"%s\"",
1703 job
->id
, Classification
, job
->username
);
1705 else if (attr
->num_values
== 2 &&
1706 strcmp(attr
->values
[0].string
.text
,
1707 attr
->values
[1].string
.text
) &&
1708 strcmp(attr
->values
[0].string
.text
, "none") &&
1709 strcmp(attr
->values
[1].string
.text
, "none"))
1712 * Can't put two different security markings on the same document!
1715 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1717 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1718 "job-sheets=\"%s,%s\", "
1719 "job-originating-user-name=\"%s\"",
1720 job
->id
, attr
->values
[0].string
.text
,
1721 attr
->values
[1].string
.text
, job
->username
);
1723 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1724 strcmp(attr
->values
[0].string
.text
, "none") &&
1725 (attr
->num_values
== 1 ||
1726 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1727 strcmp(attr
->values
[1].string
.text
, "none"))))
1729 if (attr
->num_values
== 1)
1730 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1731 "[Job %d] CLASSIFICATION OVERRIDDEN "
1732 "job-sheets=\"%s\", "
1733 "job-originating-user-name=\"%s\"",
1734 job
->id
, attr
->values
[0].string
.text
, job
->username
);
1736 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1737 "[Job %d] CLASSIFICATION OVERRIDDEN "
1738 "job-sheets=\"%s,%s\",fffff "
1739 "job-originating-user-name=\"%s\"",
1740 job
->id
, attr
->values
[0].string
.text
,
1741 attr
->values
[1].string
.text
, job
->username
);
1744 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1745 (attr
->num_values
== 1 ||
1746 strcmp(attr
->values
[1].string
.text
, Classification
)))
1749 * Force the banner to have the classification on it...
1752 if (attr
->num_values
> 1 &&
1753 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1755 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1756 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1760 if (attr
->num_values
== 1 ||
1761 strcmp(attr
->values
[0].string
.text
, "none"))
1762 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1764 if (attr
->num_values
> 1 &&
1765 strcmp(attr
->values
[1].string
.text
, "none"))
1766 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1769 if (attr
->num_values
> 1)
1770 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1771 "[Job %d] CLASSIFICATION FORCED "
1772 "job-sheets=\"%s,%s\", "
1773 "job-originating-user-name=\"%s\"",
1774 job
->id
, attr
->values
[0].string
.text
,
1775 attr
->values
[1].string
.text
, job
->username
);
1777 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1778 "[Job %d] CLASSIFICATION FORCED "
1779 "job-sheets=\"%s\", "
1780 "job-originating-user-name=\"%s\"",
1781 job
->id
, Classification
, job
->username
);
1786 * See if we need to add the starting sheet...
1789 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1791 cupsdLogMessage(CUPSD_LOG_INFO
,
1792 "[Job %d] Adding start banner page \"%s\".",
1793 job
->id
, attr
->values
[0].string
.text
);
1795 kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
);
1797 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1800 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1801 IPP_TAG_ZERO
)) != NULL
)
1805 * Fill in the response info...
1808 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1809 LocalPort
, job
->id
);
1811 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1814 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1816 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1818 add_job_state_reasons(con
, job
);
1820 con
->response
->request
.status
.status_code
= IPP_OK
;
1823 * Add any job subscriptions...
1826 add_job_subscriptions(con
, job
);
1829 * Set all but the first two attributes to the job attributes group...
1832 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1833 attr
->group_tag
= IPP_TAG_JOB
;
1836 * Fire the "job created" event...
1839 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1842 * Return the new job...
1850 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1851 * upon the job and printer state...
1855 add_job_state_reasons(
1856 cupsd_client_t
*con
, /* I - Client connection */
1857 cupsd_job_t
*job
) /* I - Job info */
1859 cupsd_printer_t
*dest
; /* Destination printer */
1862 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1863 con
, con
->http
.fd
, job
? job
->id
: 0);
1865 switch (job
? job
->state_value
: IPP_JOB_CANCELED
)
1867 case IPP_JOB_PENDING
:
1868 dest
= cupsdFindDest(job
->dest
);
1870 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1871 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1872 "job-state-reasons", NULL
, "printer-stopped");
1874 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1875 "job-state-reasons", NULL
, "none");
1879 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1880 IPP_TAG_KEYWORD
) != NULL
||
1881 ippFindAttribute(job
->attrs
, "job-hold-until",
1882 IPP_TAG_NAME
) != NULL
)
1883 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1884 "job-state-reasons", NULL
, "job-hold-until-specified");
1886 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1887 "job-state-reasons", NULL
, "job-incoming");
1890 case IPP_JOB_PROCESSING
:
1891 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1892 "job-state-reasons", NULL
, "job-printing");
1895 case IPP_JOB_STOPPED
:
1896 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1897 "job-state-reasons", NULL
, "job-stopped");
1900 case IPP_JOB_CANCELED
:
1901 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1902 "job-state-reasons", NULL
, "job-canceled-by-user");
1905 case IPP_JOB_ABORTED
:
1906 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1907 "job-state-reasons", NULL
, "aborted-by-system");
1910 case IPP_JOB_COMPLETED
:
1911 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1912 "job-state-reasons", NULL
, "job-completed-successfully");
1919 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1923 add_job_subscriptions(
1924 cupsd_client_t
*con
, /* I - Client connection */
1925 cupsd_job_t
*job
) /* I - Newly created job */
1927 int i
; /* Looping var */
1928 ipp_attribute_t
*prev
, /* Previous attribute */
1929 *next
, /* Next attribute */
1930 *attr
; /* Current attribute */
1931 cupsd_subscription_t
*sub
; /* Subscription object */
1932 const char *recipient
, /* notify-recipient-uri */
1933 *pullmethod
; /* notify-pull-method */
1934 ipp_attribute_t
*user_data
; /* notify-user-data */
1935 int interval
; /* notify-time-interval */
1936 unsigned mask
; /* notify-events */
1940 * Find the first subscription group attribute; return if we have
1944 for (attr
= job
->attrs
->attrs
, prev
= NULL
;
1946 prev
= attr
, attr
= attr
->next
)
1947 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1954 * Process the subscription attributes in the request...
1963 mask
= CUPSD_EVENT_NONE
;
1965 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1967 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
1968 attr
->value_tag
== IPP_TAG_URI
)
1969 recipient
= attr
->values
[0].string
.text
;
1970 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1971 attr
->value_tag
== IPP_TAG_KEYWORD
)
1972 pullmethod
= attr
->values
[0].string
.text
;
1973 else if (!strcmp(attr
->name
, "notify-charset") &&
1974 attr
->value_tag
== IPP_TAG_CHARSET
&&
1975 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
1976 strcmp(attr
->values
[0].string
.text
, "utf-8"))
1978 send_ipp_status(con
, IPP_CHARSET
,
1979 _("Character set \"%s\" not supported!"),
1980 attr
->values
[0].string
.text
);
1983 else if (!strcmp(attr
->name
, "notify-natural-language") &&
1984 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
1985 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
1987 send_ipp_status(con
, IPP_CHARSET
,
1988 _("Language \"%s\" not supported!"),
1989 attr
->values
[0].string
.text
);
1992 else if (!strcmp(attr
->name
, "notify-user-data") &&
1993 attr
->value_tag
== IPP_TAG_STRING
)
1995 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
1997 send_ipp_status(con
, IPP_REQUEST_VALUE
,
1998 _("The notify-user-data value is too large "
1999 "(%d > 63 octets)!"),
2000 attr
->values
[0].unknown
.length
);
2006 else if (!strcmp(attr
->name
, "notify-events") &&
2007 attr
->value_tag
== IPP_TAG_KEYWORD
)
2009 for (i
= 0; i
< attr
->num_values
; i
++)
2010 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
2012 else if (!strcmp(attr
->name
, "notify-lease-duration"))
2014 send_ipp_status(con
, IPP_BAD_REQUEST
,
2015 _("The notify-lease-duration attribute cannot be "
2016 "used with job subscriptions."));
2019 else if (!strcmp(attr
->name
, "notify-time-interval") &&
2020 attr
->value_tag
== IPP_TAG_INTEGER
)
2021 interval
= attr
->values
[0].integer
;
2026 if (!recipient
&& !pullmethod
)
2029 if (mask
== CUPSD_EVENT_NONE
)
2030 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2032 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
2035 sub
->interval
= interval
;
2037 cupsdSetString(&sub
->owner
, job
->username
);
2041 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2042 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2043 sub
->user_data_len
);
2046 ippAddSeparator(con
->response
);
2047 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2048 "notify-subscription-id", sub
->id
);
2054 cupsdSaveAllSubscriptions();
2057 * Remove all of the subscription attributes from the job request...
2060 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2064 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2065 attr
->group_tag
== IPP_TAG_ZERO
)
2068 * Free and remove this attribute...
2076 job
->attrs
->attrs
= next
;
2082 job
->attrs
->last
= prev
;
2083 job
->attrs
->current
= prev
;
2088 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2090 * See RFC 4122 for the definition of UUIDs and the format.
2094 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
2095 cupsd_job_t
*job
) /* I - Job */
2097 char uuid
[1024]; /* job-uuid string */
2098 _cups_md5_state_t md5state
; /* MD5 state */
2099 unsigned char md5sum
[16]; /* MD5 digest/sum */
2103 * First see if the job already has a job-uuid attribute; if so, return...
2106 if (ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2110 * No job-uuid attribute, so build a version 3 UUID with the local job
2111 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
2112 * the ServerName, server name and port that the client connected to,
2113 * and local job ID...
2116 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
2117 con
->serverport
, job
->id
);
2119 _cupsMD5Init(&md5state
);
2120 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
2121 _cupsMD5Finish(&md5state
, md5sum
);
2124 * Format the UUID URI using the MD5 sum and job ID.
2127 snprintf(uuid
, sizeof(uuid
),
2128 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2129 "%02x%02x%02x%02x%02x%02x",
2130 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
2131 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
2132 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
2133 md5sum
[14], md5sum
[15]);
2135 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
2140 * 'add_printer()' - Add a printer to the system.
2144 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2145 ipp_attribute_t
*uri
) /* I - URI of printer */
2147 http_status_t status
; /* Policy status */
2148 int i
; /* Looping var */
2149 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2150 username
[HTTP_MAX_URI
], /* Username portion of URI */
2151 host
[HTTP_MAX_URI
], /* Host portion of URI */
2152 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2153 int port
; /* Port portion of URI */
2154 cupsd_printer_t
*printer
; /* Printer/class */
2155 ipp_attribute_t
*attr
; /* Printer attribute */
2156 cups_file_t
*fp
; /* Script/PPD file */
2157 char line
[1024]; /* Line from file... */
2158 char srcfile
[1024], /* Source Script/PPD file */
2159 dstfile
[1024]; /* Destination Script/PPD file */
2160 int modify
; /* Non-zero if we are modifying */
2161 char newname
[IPP_MAX_NAME
]; /* New printer name */
2162 int need_restart_job
; /* Need to restart job? */
2163 int set_device_uri
, /* Did we set the device URI? */
2164 set_port_monitor
; /* Did we set the port monitor? */
2167 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2168 con
->http
.fd
, uri
->values
[0].string
.text
);
2171 * Do we have a valid URI?
2174 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2175 sizeof(scheme
), username
, sizeof(username
), host
,
2176 sizeof(host
), &port
, resource
, sizeof(resource
));
2178 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2181 * No, return an error...
2184 send_ipp_status(con
, IPP_BAD_REQUEST
,
2185 _("The printer-uri must be of the form "
2186 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2191 * Do we have a valid printer name?
2194 if (!validate_name(resource
+ 10))
2197 * No, return an error...
2200 send_ipp_status(con
, IPP_BAD_REQUEST
,
2201 _("The printer-uri \"%s\" contains invalid characters."),
2202 uri
->values
[0].string
.text
);
2207 * See if the printer already exists; if not, create a new printer...
2210 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2213 * Printer doesn't exist; see if we have a class of the same name...
2216 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2217 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
2220 * Yes, return an error...
2223 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2224 _("A class named \"%s\" already exists!"),
2230 * No, check the default policy then add the printer...
2233 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2235 send_http_error(con
, status
, NULL
);
2239 printer
= cupsdAddPrinter(resource
+ 10);
2242 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2245 * Check the default policy, then rename the implicit printer to
2246 * "AnyPrinter" or delete it...
2249 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2251 send_http_error(con
, status
, NULL
);
2255 if (ImplicitAnyClasses
)
2257 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2258 cupsdRenamePrinter(printer
, newname
);
2261 cupsdDeletePrinter(printer
, 1);
2264 * Add the printer as a new local printer...
2267 printer
= cupsdAddPrinter(resource
+ 10);
2270 else if (printer
->type
& CUPS_PRINTER_DISCOVERED
)
2273 * Check the default policy, then rename the remote printer to
2274 * "Printer@server"...
2277 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2279 send_http_error(con
, status
, NULL
);
2283 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2285 cupsdRenamePrinter(printer
, newname
);
2288 * Add the printer as a new local printer...
2291 printer
= cupsdAddPrinter(resource
+ 10);
2294 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2297 send_http_error(con
, status
, printer
);
2304 * Look for attributes and copy them over as needed...
2307 need_restart_job
= 0;
2309 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2310 IPP_TAG_TEXT
)) != NULL
)
2311 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2313 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2314 IPP_TAG_TEXT
)) != NULL
)
2315 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2319 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2320 IPP_TAG_URI
)) != NULL
)
2323 * Do we have a valid device URI?
2326 http_uri_status_t uri_status
; /* URI separation status */
2329 need_restart_job
= 1;
2331 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2332 attr
->values
[0].string
.text
,
2333 scheme
, sizeof(scheme
),
2334 username
, sizeof(username
),
2335 host
, sizeof(host
), &port
,
2336 resource
, sizeof(resource
));
2338 if (uri_status
< HTTP_URI_OK
)
2340 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2341 attr
->values
[0].string
.text
);
2342 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2343 "add_printer: httpSeparateURI returned %d", uri_status
);
2347 if (!strcmp(scheme
, "file"))
2350 * See if the administrator has enabled file devices...
2353 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2356 * File devices are disabled and the URL is not file:/dev/null...
2359 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2360 _("File device URIs have been disabled! "
2361 "To enable, see the FileDevice directive in "
2362 "\"%s/cupsd.conf\"."),
2370 * See if the backend exists and is executable...
2373 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2374 if (access(srcfile
, X_OK
))
2377 * Could not find device in list!
2380 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2381 attr
->values
[0].string
.text
);
2386 cupsdLogMessage(CUPSD_LOG_INFO
,
2387 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2389 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2391 cupsdSanitizeURI(printer
->device_uri
, resource
,
2394 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2398 set_port_monitor
= 0;
2400 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2401 IPP_TAG_NAME
)) != NULL
)
2403 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2406 need_restart_job
= 1;
2408 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2412 for (i
= 0; i
< supported
->num_values
; i
++)
2413 if (!strcmp(supported
->values
[i
].string
.text
,
2414 attr
->values
[0].string
.text
))
2418 if (!supported
|| i
>= supported
->num_values
)
2420 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2421 attr
->values
[0].string
.text
);
2425 cupsdLogMessage(CUPSD_LOG_INFO
,
2426 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2427 printer
->name
, attr
->values
[0].string
.text
,
2428 printer
->port_monitor
? printer
->port_monitor
: "none");
2430 if (strcmp(attr
->values
[0].string
.text
, "none"))
2431 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2433 cupsdClearString(&printer
->port_monitor
);
2435 set_port_monitor
= 1;
2438 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2439 IPP_TAG_BOOLEAN
)) != NULL
)
2441 cupsdLogMessage(CUPSD_LOG_INFO
,
2442 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2443 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2445 printer
->accepting
= attr
->values
[0].boolean
;
2446 cupsdAddPrinterHistory(printer
);
2449 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2450 IPP_TAG_BOOLEAN
)) != NULL
)
2452 if (printer
->shared
&& !attr
->values
[0].boolean
)
2453 cupsdDeregisterPrinter(printer
, 1);
2455 cupsdLogMessage(CUPSD_LOG_INFO
,
2456 "Setting %s printer-is-shared to %d (was %d.)",
2457 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2459 printer
->shared
= attr
->values
[0].boolean
;
2462 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2463 IPP_TAG_ENUM
)) != NULL
)
2465 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2466 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2468 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2469 attr
->values
[0].integer
);
2473 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2474 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2476 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2477 cupsdStopPrinter(printer
, 0);
2480 need_restart_job
= 1;
2481 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2484 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2485 IPP_TAG_TEXT
)) != NULL
)
2487 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2488 sizeof(printer
->state_message
));
2489 cupsdAddPrinterHistory(printer
);
2492 set_printer_defaults(con
, printer
);
2494 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2495 IPP_TAG_KEYWORD
)) != NULL
)
2496 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2499 * See if we have all required attributes...
2502 if (!printer
->device_uri
)
2503 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2506 * See if we have an interface script or PPD file attached to the request...
2511 need_restart_job
= 1;
2513 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2515 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2518 * Yes; get the first line from it...
2522 cupsFileGets(fp
, line
, sizeof(line
));
2526 * Then see what kind of file it is...
2529 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2532 if (!strncmp(line
, "*PPD-Adobe", 10))
2535 * The new file is a PPD file, so remove any old interface script
2536 * that might be lying around...
2544 * This must be an interface script, so move the file over to the
2545 * interfaces directory and make it executable...
2548 if (copy_file(srcfile
, dstfile
))
2550 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2551 _("Unable to copy interface script - %s!"),
2557 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2558 "Copied interface script successfully!");
2559 chmod(dstfile
, 0755);
2563 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2566 if (!strncmp(line
, "*PPD-Adobe", 10))
2569 * The new file is a PPD file, so move the file over to the
2570 * ppd directory and make it readable by all...
2573 if (copy_file(srcfile
, dstfile
))
2575 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2576 _("Unable to copy PPD file - %s!"),
2582 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2583 "Copied PPD file successfully!");
2584 chmod(dstfile
, 0644);
2590 * This must be an interface script, so remove any old PPD file that
2591 * may be lying around...
2598 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2599 IPP_TAG_NAME
)) != NULL
)
2601 need_restart_job
= 1;
2603 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2606 * Raw driver, remove any existing PPD or interface script files.
2609 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2613 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2623 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2627 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2630 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2632 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2637 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2638 "Copied PPD file successfully!");
2639 chmod(dstfile
, 0644);
2645 * If we set the device URI but not the port monitor, check which port
2646 * monitor to use by default...
2649 if (set_device_uri
&& !set_port_monitor
)
2651 ppd_file_t
*ppd
; /* PPD file */
2652 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2655 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2656 sizeof(scheme
), username
, sizeof(username
), host
,
2657 sizeof(host
), &port
, resource
, sizeof(resource
));
2659 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2661 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2663 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2665 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2666 if (!strcmp(scheme
, ppdattr
->spec
))
2668 cupsdLogMessage(CUPSD_LOG_INFO
,
2669 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2670 printer
->name
, ppdattr
->value
,
2671 printer
->port_monitor
? printer
->port_monitor
2674 if (strcmp(ppdattr
->value
, "none"))
2675 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2677 cupsdClearString(&printer
->port_monitor
);
2687 * Update the printer attributes and return...
2690 cupsdSetPrinterAttrs(printer
);
2691 cupsdSaveAllPrinters();
2693 if (need_restart_job
&& printer
->job
)
2698 * Stop the current job and then restart it below...
2701 job
= (cupsd_job_t
*)printer
->job
;
2703 cupsdStopJob(job
, 1);
2705 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2706 job
->state_value
= IPP_JOB_PENDING
;
2709 if (need_restart_job
)
2712 cupsdWritePrintcap();
2716 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2717 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2720 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2721 printer
->name
, get_username(con
));
2725 cupsdAddPrinterHistory(printer
);
2727 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2728 "New printer \"%s\" added by \"%s\".", printer
->name
,
2731 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2732 printer
->name
, get_username(con
));
2735 con
->response
->request
.status
.status_code
= IPP_OK
;
2740 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2741 * based upon the printer state...
2745 add_printer_state_reasons(
2746 cupsd_client_t
*con
, /* I - Client connection */
2747 cupsd_printer_t
*p
) /* I - Printer info */
2749 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2750 "add_printer_state_reasons(%p[%d], %p[%s])",
2751 con
, con
->http
.fd
, p
, p
->name
);
2753 if (p
->num_reasons
== 0)
2754 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2755 "printer-state-reasons", NULL
,
2756 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2758 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2759 "printer-state-reasons", p
->num_reasons
, NULL
,
2760 (const char * const *)p
->reasons
);
2765 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2766 * the specified printer or class.
2770 add_queued_job_count(
2771 cupsd_client_t
*con
, /* I - Client connection */
2772 cupsd_printer_t
*p
) /* I - Printer or class */
2774 int count
; /* Number of jobs on destination */
2777 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2778 con
, con
->http
.fd
, p
, p
->name
);
2780 count
= cupsdGetPrinterJobCount(p
->name
);
2782 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2783 "queued-job-count", count
);
2788 * 'apply_printer_defaults()' - Apply printer default options to a job.
2792 apply_printer_defaults(
2793 cupsd_printer_t
*printer
, /* I - Printer */
2794 cupsd_job_t
*job
) /* I - Job */
2796 int i
, /* Looping var */
2797 num_options
; /* Number of default options */
2798 cups_option_t
*options
, /* Default options */
2799 *option
; /* Current option */
2803 * Collect all of the default options and add the missing ones to the
2807 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
2810 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2812 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2817 * Encode these options as attributes in the job object...
2820 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2821 cupsFreeOptions(num_options
, options
);
2826 * 'authenticate_job()' - Set job authentication info.
2830 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2831 ipp_attribute_t
*uri
) /* I - Job URI */
2833 ipp_attribute_t
*attr
, /* job-id attribute */
2834 *auth_info
; /* auth-info attribute */
2835 int jobid
; /* Job ID */
2836 cupsd_job_t
*job
; /* Current job */
2837 char method
[HTTP_MAX_URI
],
2838 /* Method portion of URI */
2839 username
[HTTP_MAX_URI
],
2840 /* Username portion of URI */
2842 /* Host portion of URI */
2843 resource
[HTTP_MAX_URI
];
2844 /* Resource portion of URI */
2845 int port
; /* Port portion of URI */
2848 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2849 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
2852 * Start with "everything is OK" status...
2855 con
->response
->request
.status
.status_code
= IPP_OK
;
2858 * See if we have a job URI or a printer URI...
2861 if (!strcmp(uri
->name
, "printer-uri"))
2864 * Got a printer URI; see if we also have a job-id attribute...
2867 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2868 IPP_TAG_INTEGER
)) == NULL
)
2870 send_ipp_status(con
, IPP_BAD_REQUEST
,
2871 _("Got a printer-uri attribute but no job-id!"));
2875 jobid
= attr
->values
[0].integer
;
2880 * Got a job URI; parse it to get the job ID...
2883 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2884 sizeof(method
), username
, sizeof(username
), host
,
2885 sizeof(host
), &port
, resource
, sizeof(resource
));
2887 if (strncmp(resource
, "/jobs/", 6))
2893 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
2894 uri
->values
[0].string
.text
);
2898 jobid
= atoi(resource
+ 6);
2902 * See if the job exists...
2905 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2908 * Nope - return a "not found" error...
2911 send_ipp_status(con
, IPP_NOT_FOUND
,
2912 _("Job #%d does not exist!"), jobid
);
2917 * See if the job has been completed...
2920 if (job
->state_value
!= IPP_JOB_HELD
)
2923 * Return a "not-possible" error...
2926 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2927 _("Job #%d is not held for authentication!"),
2933 * See if we have already authenticated...
2936 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
2938 if (!con
->username
[0] && !auth_info
)
2940 cupsd_printer_t
*printer
; /* Job destination */
2944 * No auth data. If we need to authenticate via Kerberos, send a
2945 * HTTP auth challenge, otherwise just return an IPP error...
2948 printer
= cupsdFindDest(job
->dest
);
2950 if (printer
&& printer
->num_auth_info_required
> 0 &&
2951 !strcmp(printer
->auth_info_required
[0], "negotiate"))
2952 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
2954 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
2955 _("No authentication information provided!"));
2960 * See if the job is owned by the requesting user...
2963 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
2965 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
2970 * Save the authentication information for this job...
2973 save_auth_info(con
, job
, auth_info
);
2976 * Reset the job-hold-until value to "no-hold"...
2979 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2980 IPP_TAG_KEYWORD
)) == NULL
)
2981 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2985 attr
->value_tag
= IPP_TAG_KEYWORD
;
2986 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2990 * Release the job and return...
2993 cupsdReleaseJob(job
);
2995 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Authenticated by \"%s\".", jobid
,
3001 * 'cancel_all_jobs()' - Cancel all print jobs.
3005 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3006 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3008 http_status_t status
; /* Policy status */
3009 cups_ptype_t dtype
; /* Destination type */
3010 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3011 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3012 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3013 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3014 int port
; /* Port portion of URI */
3015 ipp_attribute_t
*attr
; /* Attribute in request */
3016 const char *username
; /* Username */
3017 int purge
; /* Purge? */
3018 cupsd_printer_t
*printer
; /* Printer */
3021 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3022 con
->http
.fd
, uri
->values
[0].string
.text
);
3025 * See if we have a printer URI...
3028 if (strcmp(uri
->name
, "printer-uri"))
3030 send_ipp_status(con
, IPP_BAD_REQUEST
,
3031 _("The printer-uri attribute is required!"));
3036 * Get the username (if any) for the jobs we want to cancel (only if
3037 * "my-jobs" is specified...
3040 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3041 IPP_TAG_BOOLEAN
)) != NULL
&&
3042 attr
->values
[0].boolean
)
3044 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3045 IPP_TAG_NAME
)) != NULL
)
3046 username
= attr
->values
[0].string
.text
;
3049 send_ipp_status(con
, IPP_BAD_REQUEST
,
3050 _("Missing requesting-user-name attribute!"));
3058 * Look for the "purge-jobs" attribute...
3061 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3062 IPP_TAG_BOOLEAN
)) != NULL
)
3063 purge
= attr
->values
[0].boolean
;
3068 * And if the destination is valid...
3071 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3077 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3078 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3079 hostname
, sizeof(hostname
), &port
,
3080 resource
, sizeof(resource
));
3082 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3083 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3085 send_ipp_status(con
, IPP_NOT_FOUND
,
3086 _("The printer or class was not found."));
3094 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3096 send_http_error(con
, status
, NULL
);
3101 * Cancel all jobs on all printers...
3104 cupsdCancelJobs(NULL
, username
, purge
);
3106 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3107 purge
? "purged" : "canceled", get_username(con
));
3115 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3118 send_http_error(con
, status
, printer
);
3123 * Cancel all of the jobs on the named printer...
3126 cupsdCancelJobs(printer
->name
, username
, purge
);
3128 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3129 printer
->name
, purge
? "purged" : "canceled",
3133 con
->response
->request
.status
.status_code
= IPP_OK
;
3138 * 'cancel_job()' - Cancel a print job.
3142 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3143 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3145 ipp_attribute_t
*attr
; /* Current attribute */
3146 int jobid
; /* Job ID */
3147 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3148 username
[HTTP_MAX_URI
], /* Username portion of URI */
3149 host
[HTTP_MAX_URI
], /* Host portion of URI */
3150 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3151 int port
; /* Port portion of URI */
3152 cupsd_job_t
*job
; /* Job information */
3153 cups_ptype_t dtype
; /* Destination type (printer/class) */
3154 cupsd_printer_t
*printer
; /* Printer data */
3155 int purge
; /* Purge the job? */
3158 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3159 con
->http
.fd
, uri
->values
[0].string
.text
);
3162 * See if we have a job URI or a printer URI...
3165 if (!strcmp(uri
->name
, "printer-uri"))
3168 * Got a printer URI; see if we also have a job-id attribute...
3171 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3172 IPP_TAG_INTEGER
)) == NULL
)
3174 send_ipp_status(con
, IPP_BAD_REQUEST
,
3175 _("Got a printer-uri attribute but no job-id!"));
3179 if ((jobid
= attr
->values
[0].integer
) == 0)
3182 * Find the current job on the specified printer...
3185 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3191 send_ipp_status(con
, IPP_NOT_FOUND
,
3192 _("The printer or class was not found."));
3197 * See if the printer is currently printing a job...
3201 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3205 * No, see if there are any pending jobs...
3208 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3210 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3211 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3212 !strcasecmp(job
->dest
, printer
->name
))
3219 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3221 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3222 if (job
->state_value
== IPP_JOB_STOPPED
&&
3223 !strcasecmp(job
->dest
, printer
->name
))
3230 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3241 * Got a job URI; parse it to get the job ID...
3244 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3245 sizeof(scheme
), username
, sizeof(username
), host
,
3246 sizeof(host
), &port
, resource
, sizeof(resource
));
3248 if (strncmp(resource
, "/jobs/", 6))
3254 send_ipp_status(con
, IPP_BAD_REQUEST
,
3255 _("Bad job-uri attribute \"%s\"!"),
3256 uri
->values
[0].string
.text
);
3260 jobid
= atoi(resource
+ 6);
3264 * Look for the "purge-job" attribute...
3267 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3268 IPP_TAG_BOOLEAN
)) != NULL
)
3269 purge
= attr
->values
[0].boolean
;
3274 * See if the job exists...
3277 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3280 * Nope - return a "not found" error...
3283 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3288 * See if the job is owned by the requesting user...
3291 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3293 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3298 * See if the job is already completed, canceled, or aborted; if so,
3299 * we can't cancel...
3302 if (job
->state_value
>= IPP_JOB_CANCELED
&& !purge
)
3304 switch (job
->state_value
)
3306 case IPP_JOB_CANCELED
:
3307 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3308 _("Job #%d is already canceled - can\'t cancel."),
3312 case IPP_JOB_ABORTED
:
3313 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3314 _("Job #%d is already aborted - can\'t cancel."),
3319 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3320 _("Job #%d is already completed - can\'t cancel."),
3329 * Cancel the job and return...
3332 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
3336 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
3339 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3342 con
->response
->request
.status
.status_code
= IPP_OK
;
3347 * 'cancel_subscription()' - Cancel a subscription.
3351 cancel_subscription(
3352 cupsd_client_t
*con
, /* I - Client connection */
3353 int sub_id
) /* I - Subscription ID */
3355 http_status_t status
; /* Policy status */
3356 cupsd_subscription_t
*sub
; /* Subscription */
3359 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3360 "cancel_subscription(con=%p[%d], sub_id=%d)",
3361 con
, con
->http
.fd
, sub_id
);
3364 * Is the subscription ID valid?
3367 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3370 * Bad subscription ID...
3373 send_ipp_status(con
, IPP_NOT_FOUND
,
3374 _("notify-subscription-id %d no good!"), sub_id
);
3382 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3384 con
, sub
->owner
)) != HTTP_OK
)
3386 send_http_error(con
, status
, sub
->dest
);
3391 * Cancel the subscription...
3394 cupsdDeleteSubscription(sub
, 1);
3396 con
->response
->request
.status
.status_code
= IPP_OK
;
3401 * 'check_quotas()' - Check quotas for a printer and user.
3404 static int /* O - 1 if OK, 0 if not */
3405 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3406 cupsd_printer_t
*p
) /* I - Printer or class */
3408 int i
; /* Looping var */
3409 char username
[33]; /* Username */
3410 cupsd_quota_t
*q
; /* Quota data */
3411 #ifdef HAVE_MBR_UID_TO_UUID
3413 * Use Apple membership APIs which require that all names represent
3414 * valid user account or group records accessible by the server.
3417 uuid_t usr_uuid
; /* UUID for job requesting user */
3418 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3419 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3420 int mbr_err
; /* Error from membership function */
3421 int is_member
; /* Is this user a member? */
3424 * Use standard POSIX APIs for checking users and groups...
3427 struct passwd
*pw
; /* User password data */
3428 #endif /* HAVE_MBR_UID_TO_UUID */
3431 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3432 con
, con
->http
.fd
, p
, p
->name
);
3442 * Figure out who is printing...
3445 strlcpy(username
, get_username(con
), sizeof(username
));
3448 * Check global active job limits for printers and users...
3451 if (MaxJobsPerPrinter
)
3454 * Check if there are too many pending jobs on this printer...
3457 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3459 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3468 * Check if there are too many pending jobs for this user...
3471 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3473 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3480 * Check against users...
3483 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3488 #ifdef HAVE_MBR_UID_TO_UUID
3490 * Get UUID for job requesting user...
3493 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3499 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3500 "check_quotas: UUID lookup failed for user \"%s\"",
3502 cupsdLogMessage(CUPSD_LOG_INFO
,
3503 "Denying user \"%s\" access to printer \"%s\" "
3504 "(unknown user)...",
3510 * Get UID and GID of requesting user...
3513 pw
= getpwnam(username
);
3515 #endif /* HAVE_MBR_UID_TO_UUID */
3517 for (i
= 0; i
< p
->num_users
; i
++)
3518 if (p
->users
[i
][0] == '@')
3521 * Check group membership...
3524 #ifdef HAVE_MBR_UID_TO_UUID
3525 if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
3529 * Invalid ACL entries are ignored for matching; just record a
3530 * warning in the log...
3533 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3534 "check_quotas: UUID lookup failed for ACL entry "
3535 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3536 cupsdLogMessage(CUPSD_LOG_WARN
,
3537 "Access control entry \"%s\" not a valid group name; "
3538 "entry ignored", p
->users
[i
]);
3542 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3546 * At this point, there should be no errors, but check anyways...
3549 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3550 "check_quotas: group \"%s\" membership check "
3551 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
3556 * Stop if we found a match...
3563 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3565 #endif /* HAVE_MBR_UID_TO_UUID */
3567 #ifdef HAVE_MBR_UID_TO_UUID
3570 if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
3574 * Invalid ACL entries are ignored for matching; just record a
3575 * warning in the log...
3578 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3579 "check_quotas: UUID lookup failed for ACL entry "
3580 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3581 cupsdLogMessage(CUPSD_LOG_WARN
,
3582 "Access control entry \"%s\" not a valid user name; "
3583 "entry ignored", p
->users
[i
]);
3587 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
3590 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3591 "check_quotas: User \"%s\" identity check failed "
3592 "(err=%d)", p
->users
[i
], mbr_err
);
3601 else if (!strcasecmp(username
, p
->users
[i
]))
3603 #endif /* HAVE_MBR_UID_TO_UUID */
3605 if ((i
< p
->num_users
) == p
->deny_users
)
3607 cupsdLogMessage(CUPSD_LOG_INFO
,
3608 "Denying user \"%s\" access to printer \"%s\"...",
3619 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
3622 * TODO: Define these special page count values as constants!
3625 if (q
->page_count
== -4) /* special case: unlimited user */
3627 cupsdLogMessage(CUPSD_LOG_INFO
,
3628 "User \"%s\" request approved for printer %s (%s): "
3630 username
, p
->name
, p
->info
);
3631 q
->page_count
= 0; /* allow user to print */
3634 else if (q
->page_count
== -3) /* quota exceeded */
3636 cupsdLogMessage(CUPSD_LOG_INFO
,
3637 "User \"%s\" request denied for printer %s (%s): "
3638 "quota limit exceeded.",
3639 username
, p
->name
, p
->info
);
3640 q
->page_count
= 2; /* force quota exceeded failure */
3643 else if (q
->page_count
== -2) /* quota disabled for user */
3645 cupsdLogMessage(CUPSD_LOG_INFO
,
3646 "User \"%s\" request denied for printer %s (%s): "
3647 "printing disabled for user.",
3648 username
, p
->name
, p
->info
);
3649 q
->page_count
= 2; /* force quota exceeded failure */
3652 else if (q
->page_count
== -1) /* quota access error */
3654 cupsdLogMessage(CUPSD_LOG_INFO
,
3655 "User \"%s\" request denied for printer %s (%s): "
3656 "unable to determine quota limit.",
3657 username
, p
->name
, p
->info
);
3658 q
->page_count
= 2; /* force quota exceeded failure */
3661 else if (q
->page_count
< 0) /* user not found or other error */
3663 cupsdLogMessage(CUPSD_LOG_INFO
,
3664 "User \"%s\" request denied for printer %s (%s): "
3665 "user disabled / missing quota.",
3666 username
, p
->name
, p
->info
);
3667 q
->page_count
= 2; /* force quota exceeded failure */
3670 else /* page within user limits */
3672 q
->page_count
= 0; /* allow user to print */
3677 #endif /* __APPLE__ */
3678 if (p
->k_limit
|| p
->page_limit
)
3680 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3682 cupsdLogMessage(CUPSD_LOG_ERROR
,
3683 "Unable to allocate quota data for user \"%s\"!",
3688 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3689 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3691 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3698 * If we have gotten this far, we're done!
3706 * 'copy_attribute()' - Copy a single attribute.
3709 static ipp_attribute_t
* /* O - New attribute */
3711 ipp_t
*to
, /* O - Destination request/response */
3712 ipp_attribute_t
*attr
, /* I - Attribute to copy */
3713 int quickcopy
) /* I - Do a quick copy? */
3715 int i
; /* Looping var */
3716 ipp_attribute_t
*toattr
; /* Destination attribute */
3719 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3720 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
3721 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
3724 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
3727 toattr
= ippAddSeparator(to
);
3730 case IPP_TAG_INTEGER
:
3732 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3733 attr
->name
, attr
->num_values
, NULL
);
3735 for (i
= 0; i
< attr
->num_values
; i
++)
3736 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
3739 case IPP_TAG_BOOLEAN
:
3740 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
3741 attr
->num_values
, NULL
);
3743 for (i
= 0; i
< attr
->num_values
; i
++)
3744 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
3747 case IPP_TAG_STRING
:
3750 case IPP_TAG_KEYWORD
:
3752 case IPP_TAG_URISCHEME
:
3753 case IPP_TAG_CHARSET
:
3754 case IPP_TAG_LANGUAGE
:
3755 case IPP_TAG_MIMETYPE
:
3756 toattr
= ippAddStrings(to
, attr
->group_tag
,
3757 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3758 attr
->name
, attr
->num_values
, NULL
, NULL
);
3762 for (i
= 0; i
< attr
->num_values
; i
++)
3763 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3767 for (i
= 0; i
< attr
->num_values
; i
++)
3768 toattr
->values
[i
].string
.text
=
3769 _cupsStrAlloc(attr
->values
[i
].string
.text
);
3774 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
3775 attr
->values
[0].date
);
3778 case IPP_TAG_RESOLUTION
:
3779 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
3780 attr
->num_values
, IPP_RES_PER_INCH
,
3783 for (i
= 0; i
< attr
->num_values
; i
++)
3785 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
3786 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
3787 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
3791 case IPP_TAG_RANGE
:
3792 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
3793 attr
->num_values
, NULL
, NULL
);
3795 for (i
= 0; i
< attr
->num_values
; i
++)
3797 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
3798 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
3802 case IPP_TAG_TEXTLANG
:
3803 case IPP_TAG_NAMELANG
:
3804 toattr
= ippAddStrings(to
, attr
->group_tag
,
3805 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3806 attr
->name
, attr
->num_values
, NULL
, NULL
);
3810 for (i
= 0; i
< attr
->num_values
; i
++)
3812 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
3813 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3818 for (i
= 0; i
< attr
->num_values
; i
++)
3821 toattr
->values
[i
].string
.charset
=
3822 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
3824 toattr
->values
[i
].string
.charset
=
3825 toattr
->values
[0].string
.charset
;
3827 toattr
->values
[i
].string
.text
=
3828 _cupsStrAlloc(attr
->values
[i
].string
.text
);
3833 case IPP_TAG_BEGIN_COLLECTION
:
3834 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
3835 attr
->num_values
, NULL
);
3837 for (i
= 0; i
< attr
->num_values
; i
++)
3839 toattr
->values
[i
].collection
= ippNew();
3840 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
3841 NULL
, IPP_TAG_ZERO
, 0);
3846 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3847 attr
->name
, attr
->num_values
, NULL
);
3849 for (i
= 0; i
< attr
->num_values
; i
++)
3851 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
3853 if (toattr
->values
[i
].unknown
.length
> 0)
3855 if ((toattr
->values
[i
].unknown
.data
=
3856 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
3857 toattr
->values
[i
].unknown
.length
= 0;
3859 memcpy(toattr
->values
[i
].unknown
.data
,
3860 attr
->values
[i
].unknown
.data
,
3861 toattr
->values
[i
].unknown
.length
);
3864 break; /* anti-compiler-warning-code */
3872 * 'copy_attrs()' - Copy attributes from one request to another.
3876 copy_attrs(ipp_t
*to
, /* I - Destination request */
3877 ipp_t
*from
, /* I - Source request */
3878 cups_array_t
*ra
, /* I - Requested attributes */
3879 ipp_tag_t group
, /* I - Group to copy */
3880 int quickcopy
) /* I - Do a quick copy? */
3882 ipp_attribute_t
*fromattr
; /* Source attribute */
3885 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3886 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3887 to
, from
, ra
, group
, quickcopy
);
3892 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3895 * Filter attributes as needed...
3898 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3899 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
3902 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
3903 copy_attribute(to
, fromattr
, quickcopy
);
3909 * 'copy_banner()' - Copy a banner file to the requests directory for the
3913 static int /* O - Size of banner file in kbytes */
3914 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
3915 cupsd_job_t
*job
, /* I - Job information */
3916 const char *name
) /* I - Name of banner */
3918 int i
; /* Looping var */
3919 int kbytes
; /* Size of banner file in kbytes */
3920 char filename
[1024]; /* Job filename */
3921 cupsd_banner_t
*banner
; /* Pointer to banner */
3922 cups_file_t
*in
; /* Input file */
3923 cups_file_t
*out
; /* Output file */
3924 int ch
; /* Character from file */
3925 char attrname
[255], /* Name of attribute */
3926 *s
; /* Pointer into name */
3927 ipp_attribute_t
*attr
; /* Attribute */
3930 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
3931 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
3932 name
? name
: "(null)");
3935 * Find the banner; return if not found or "none"...
3938 if (!name
|| !strcmp(name
, "none") ||
3939 (banner
= cupsdFindBanner(name
)) == NULL
)
3943 * Open the banner and job files...
3946 if (add_file(con
, job
, banner
->filetype
, 0))
3949 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
3951 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
3953 cupsdLogMessage(CUPSD_LOG_ERROR
,
3954 "copy_banner: Unable to create banner job file %s - %s",
3955 filename
, strerror(errno
));
3960 fchmod(cupsFileNumber(out
), 0640);
3961 fchown(cupsFileNumber(out
), RunUser
, Group
);
3964 * Try the localized banner file under the subdirectory...
3967 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
3969 if (strlen(attrname
) > 2 && attrname
[2] == '-')
3972 * Convert ll-cc to ll_CC...
3976 attrname
[3] = toupper(attrname
[3] & 255);
3977 attrname
[4] = toupper(attrname
[4] & 255);
3980 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3983 if (access(filename
, 0) && strlen(attrname
) > 2)
3986 * Wasn't able to find "ll_CC" locale file; try the non-national
3987 * localization banner directory.
3992 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3996 if (access(filename
, 0))
3999 * Use the non-localized banner file.
4002 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4005 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4009 cupsdLogMessage(CUPSD_LOG_ERROR
,
4010 "copy_banner: Unable to open banner template file %s - %s",
4011 filename
, strerror(errno
));
4017 * Parse the file to the end...
4020 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4024 * Get an attribute name...
4027 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4028 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4030 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4040 * Ignore { followed by stuff that is not an attribute name...
4043 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4048 * See if it is defined...
4051 if (attrname
[0] == '?')
4056 if (!strcmp(s
, "printer-name"))
4058 cupsFilePuts(out
, job
->dest
);
4061 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4064 * See if we have a leading question mark...
4067 if (attrname
[0] != '?')
4070 * Nope, write to file as-is; probably a PostScript procedure...
4073 cupsFilePrintf(out
, "{%s}", attrname
);
4080 * Output value(s)...
4083 for (i
= 0; i
< attr
->num_values
; i
++)
4086 cupsFilePutChar(out
, ',');
4088 switch (attr
->value_tag
)
4090 case IPP_TAG_INTEGER
:
4092 if (!strncmp(s
, "time-at-", 8))
4093 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
4095 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4098 case IPP_TAG_BOOLEAN
:
4099 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4102 case IPP_TAG_NOVALUE
:
4103 cupsFilePuts(out
, "novalue");
4106 case IPP_TAG_RANGE
:
4107 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4108 attr
->values
[i
].range
.upper
);
4111 case IPP_TAG_RESOLUTION
:
4112 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4113 attr
->values
[i
].resolution
.yres
,
4114 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4119 case IPP_TAG_STRING
:
4122 case IPP_TAG_KEYWORD
:
4123 case IPP_TAG_CHARSET
:
4124 case IPP_TAG_LANGUAGE
:
4125 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4128 * Need to quote strings for PS banners...
4133 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4135 if (*p
== '(' || *p
== ')' || *p
== '\\')
4137 cupsFilePutChar(out
, '\\');
4138 cupsFilePutChar(out
, *p
);
4140 else if (*p
< 32 || *p
> 126)
4141 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4143 cupsFilePutChar(out
, *p
);
4147 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4151 break; /* anti-compiler-warning-code */
4155 else if (ch
== '\\') /* Quoted char */
4157 ch
= cupsFileGetChar(in
);
4159 if (ch
!= '{') /* Only do special handling for \{ */
4160 cupsFilePutChar(out
, '\\');
4162 cupsFilePutChar(out
, ch
);
4165 cupsFilePutChar(out
, ch
);
4169 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4171 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4172 IPP_TAG_INTEGER
)) != NULL
)
4173 attr
->values
[0].integer
+= kbytes
;
4182 * 'copy_file()' - Copy a PPD file or interface script...
4185 static int /* O - 0 = success, -1 = error */
4186 copy_file(const char *from
, /* I - Source file */
4187 const char *to
) /* I - Destination file */
4189 cups_file_t
*src
, /* Source file */
4190 *dst
; /* Destination file */
4191 int bytes
; /* Bytes to read/write */
4192 char buffer
[2048]; /* Copy buffer */
4195 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4198 * Open the source and destination file for a copy...
4201 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4204 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4211 * Copy the source file to the destination...
4214 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4215 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
4223 * Close both files and return...
4228 return (cupsFileClose(dst
));
4233 * 'copy_model()' - Copy a PPD model file, substituting default values
4237 static int /* O - 0 = success, -1 = error */
4238 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4239 const char *from
, /* I - Source file */
4240 const char *to
) /* I - Destination file */
4242 fd_set input
; /* select() input set */
4243 struct timeval timeout
; /* select() timeout */
4244 int maxfd
; /* Max file descriptor for select() */
4245 char tempfile
[1024]; /* Temporary PPD file */
4246 int tempfd
; /* Temporary PPD file descriptor */
4247 int temppid
; /* Process ID of cups-driverd */
4248 int temppipe
[2]; /* Temporary pipes */
4249 char *argv
[4], /* Command-line arguments */
4250 *envp
[MAX_ENV
]; /* Environment */
4251 cups_file_t
*src
, /* Source file */
4252 *dst
; /* Destination file */
4253 ppd_file_t
*ppd
; /* PPD file */
4254 int bytes
, /* Bytes from pipe */
4255 total
; /* Total bytes from pipe */
4256 char buffer
[2048]; /* Copy buffer */
4257 int i
; /* Looping var */
4258 char option
[PPD_MAX_NAME
], /* Option name */
4259 choice
[PPD_MAX_NAME
]; /* Choice name */
4260 int num_defaults
; /* Number of default options */
4261 cups_option_t
*defaults
; /* Default options */
4262 char cups_protocol
[PPD_MAX_LINE
];
4263 /* cupsProtocol attribute */
4264 int have_letter
, /* Have Letter size */
4265 have_a4
; /* Have A4 size */
4266 #ifdef HAVE_LIBPAPER
4267 char *paper_result
; /* Paper size name from libpaper */
4268 char system_paper
[64]; /* Paper size name buffer */
4269 #endif /* HAVE_LIBPAPER */
4272 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4273 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4277 * Run cups-driverd to get the PPD file...
4280 argv
[0] = "cups-driverd";
4282 argv
[2] = (char *)from
;
4285 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4287 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4288 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
4289 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4293 cupsdOpenPipe(temppipe
);
4295 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4296 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4298 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4299 -1, -1, 0, DefaultProfile
, &temppid
))
4310 * Wait up to 30 seconds for the PPD file to be copied...
4315 if (temppipe
[0] > CGIPipes
[0])
4316 maxfd
= temppipe
[0] + 1;
4318 maxfd
= CGIPipes
[0] + 1;
4323 * See if we have data ready...
4329 FD_SET(temppipe
[0], &input
);
4330 FD_SET(CGIPipes
[0], &input
);
4332 timeout
.tv_sec
= 30;
4333 timeout
.tv_usec
= 0;
4335 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4345 * We have timed out...
4351 if (FD_ISSET(temppipe
[0], &input
))
4354 * Read the PPD file from the pipe, and write it to the PPD file.
4357 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4359 if (write(tempfd
, buffer
, bytes
) < bytes
)
4368 if (FD_ISSET(CGIPipes
[0], &input
))
4378 * No data from cups-deviced...
4381 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
4387 * Read the source file and see what page sizes are supported...
4390 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
4396 have_letter
= ppdPageSize(ppd
, "Letter") != NULL
;
4397 have_a4
= ppdPageSize(ppd
, "A4") != NULL
;
4400 * Open the destination (if possible) and set the default options...
4405 cups_protocol
[0] = '\0';
4407 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4410 * Read all of the default lines from the old PPD...
4413 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4414 if (!strncmp(buffer
, "*Default", 8))
4417 * Add the default option...
4420 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4421 choice
, sizeof(choice
)))
4423 ppd_option_t
*ppdo
; /* PPD option */
4427 * Only add the default if the default hasn't already been
4428 * set and the choice exists in the new PPD...
4431 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4432 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4433 ppdFindChoice(ppdo
, choice
))
4434 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4438 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4439 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4443 #ifdef HAVE_LIBPAPER
4444 else if ((paper_result
= systempapername()) != NULL
)
4447 * Set the default media sizes from the systemwide default...
4450 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
4451 system_paper
[0] = toupper(system_paper
[0] & 255);
4453 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
4454 (!strcmp(system_paper
, "A4") && have_a4
))
4456 num_defaults
= cupsAddOption("PageSize", system_paper
,
4457 num_defaults
, &defaults
);
4458 num_defaults
= cupsAddOption("PageRegion", system_paper
,
4459 num_defaults
, &defaults
);
4460 num_defaults
= cupsAddOption("PaperDimension", system_paper
,
4461 num_defaults
, &defaults
);
4462 num_defaults
= cupsAddOption("ImageableArea", system_paper
,
4463 num_defaults
, &defaults
);
4466 #endif /* HAVE_LIBPAPER */
4470 * Add the default media sizes...
4472 * Note: These values are generally not valid for large-format devices
4473 * like plotters, however it is probably safe to say that those
4474 * users will configure the media size after initially adding
4475 * the device anyways...
4478 if (!DefaultLanguage
||
4479 !strcasecmp(DefaultLanguage
, "C") ||
4480 !strcasecmp(DefaultLanguage
, "POSIX") ||
4481 !strcasecmp(DefaultLanguage
, "en") ||
4482 !strncasecmp(DefaultLanguage
, "en.", 3) ||
4483 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4484 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4485 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4488 * These are the only locales that will default to "letter" size...
4493 num_defaults
= cupsAddOption("PageSize", "Letter", num_defaults
,
4495 num_defaults
= cupsAddOption("PageRegion", "Letter", num_defaults
,
4497 num_defaults
= cupsAddOption("PaperDimension", "Letter", num_defaults
,
4499 num_defaults
= cupsAddOption("ImageableArea", "Letter", num_defaults
,
4506 * The rest default to "a4" size...
4509 num_defaults
= cupsAddOption("PageSize", "A4", num_defaults
,
4511 num_defaults
= cupsAddOption("PageRegion", "A4", num_defaults
,
4513 num_defaults
= cupsAddOption("PaperDimension", "A4", num_defaults
,
4515 num_defaults
= cupsAddOption("ImageableArea", "A4", num_defaults
,
4523 * Open the source file for a copy...
4526 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4528 cupsFreeOptions(num_defaults
, defaults
);
4534 * Open the destination file for a copy...
4537 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4539 cupsFreeOptions(num_defaults
, defaults
);
4546 * Copy the source file to the destination...
4549 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4551 if (!strncmp(buffer
, "*Default", 8))
4554 * Check for an previous default option choice...
4557 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4558 choice
, sizeof(choice
)))
4560 const char *val
; /* Default option value */
4563 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4566 * Substitute the previous choice...
4569 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4574 cupsFilePrintf(dst
, "%s\n", buffer
);
4577 if (cups_protocol
[0])
4578 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4580 cupsFreeOptions(num_defaults
, defaults
);
4583 * Close both files and return...
4590 return (cupsFileClose(dst
));
4595 * 'copy_job_attrs()' - Copy job attributes.
4599 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4600 cupsd_job_t
*job
, /* I - Job */
4601 cups_array_t
*ra
) /* I - Requested attributes array */
4603 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4607 * Send the requested attributes for each job...
4610 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4611 con
->servername
, con
->serverport
, "/jobs/%d",
4614 if (!ra
|| cupsArrayFind(ra
, "document-count"))
4615 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4616 "document-count", job
->num_files
);
4618 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
4619 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4620 "job-more-info", NULL
, job_uri
);
4622 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4623 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4624 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4625 job
->num_files
> 0);
4627 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
4628 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4629 "job-printer-up-time", time(NULL
));
4631 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4632 add_job_state_reasons(con
, job
);
4634 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4635 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4636 "job-uri", NULL
, job_uri
);
4638 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
4643 * 'copy_printer_attrs()' - Copy printer attributes.
4648 cupsd_client_t
*con
, /* I - Client connection */
4649 cupsd_printer_t
*printer
, /* I - Printer */
4650 cups_array_t
*ra
) /* I - Requested attributes array */
4652 char printer_uri
[HTTP_MAX_URI
];
4654 time_t curtime
; /* Current time */
4655 int i
; /* Looping var */
4656 ipp_attribute_t
*history
; /* History collection */
4660 * Copy the printer attributes to the response using requested-attributes
4661 * and document-format attributes that may be provided by the client.
4664 curtime
= time(NULL
);
4667 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
4668 printer
->recoverable
)
4669 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4670 "com.apple.print.recoverable-message", NULL
,
4671 printer
->recoverable
);
4672 #endif /* __APPLE__ */
4674 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4675 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
4676 "printer-alert", NULL
, printer
->alert
);
4678 if (printer
->alert_description
&&
4679 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4680 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4681 "printer-alert-description", NULL
,
4682 printer
->alert_description
);
4684 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4685 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4686 ippTimeToDate(curtime
));
4688 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4689 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4690 "printer-error-policy", NULL
, printer
->error_policy
);
4692 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4693 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
4694 printer
->accepting
);
4696 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4697 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
4700 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4701 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4702 "printer-op-policy", NULL
, printer
->op_policy
);
4704 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4705 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4708 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4709 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4710 "printer-state-change-time", printer
->state_time
);
4712 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
4713 cupsArrayFind(ra
, "printer-state-history"))
4716 * Printer history is only sent if specifically requested, so that
4717 * older CUPS/IPP clients won't barf on the collection attributes.
4720 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
4721 "printer-state-history",
4722 printer
->num_history
, NULL
);
4724 for (i
= 0; i
< printer
->num_history
; i
++)
4725 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
4726 NULL
, IPP_TAG_ZERO
, 0);
4729 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4730 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4731 "printer-state-message", NULL
, printer
->state_message
);
4733 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4734 add_printer_state_reasons(con
, printer
);
4736 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4738 int type
; /* printer-type value */
4742 * Add the CUPS-specific printer-type attribute...
4745 type
= printer
->type
;
4747 if (printer
== DefaultPrinter
)
4748 type
|= CUPS_PRINTER_DEFAULT
;
4750 if (!printer
->accepting
)
4751 type
|= CUPS_PRINTER_REJECTING
;
4753 if (!printer
->shared
)
4754 type
|= CUPS_PRINTER_NOT_SHARED
;
4756 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
4760 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4761 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4762 "printer-up-time", curtime
);
4764 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
4765 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
4768 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4769 "ipp", NULL
, con
->servername
, con
->serverport
,
4770 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4771 "/classes/%s" : "/printers/%s", printer
->name
);
4772 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4773 "printer-uri-supported", NULL
, printer_uri
);
4774 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4778 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4779 add_queued_job_count(con
, printer
);
4781 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
4782 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
4787 * 'copy_subscription_attrs()' - Copy subscription attributes.
4791 copy_subscription_attrs(
4792 cupsd_client_t
*con
, /* I - Client connection */
4793 cupsd_subscription_t
*sub
, /* I - Subscription */
4794 cups_array_t
*ra
) /* I - Requested attributes array */
4796 ipp_attribute_t
*attr
; /* Current attribute */
4797 char printer_uri
[HTTP_MAX_URI
];
4799 int count
; /* Number of events */
4800 unsigned mask
; /* Current event mask */
4801 const char *name
; /* Current event name */
4805 * Copy the subscription attributes to the response using the
4806 * requested-attributes attribute that may be provided by the client.
4809 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
4811 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
4814 * Simple event list...
4817 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
4818 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
4819 "notify-events", NULL
, name
);
4824 * Complex event list...
4827 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4828 if (sub
->mask
& mask
)
4831 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
4832 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
4833 "notify-events", count
, NULL
, NULL
);
4835 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4836 if (sub
->mask
& mask
)
4838 attr
->values
[count
].string
.text
=
4839 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
4846 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
4847 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4848 "notify-job-id", sub
->job
->id
);
4850 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
4851 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4852 "notify-lease-duration", sub
->lease
);
4854 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
4856 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4857 "ipp", NULL
, con
->servername
, con
->serverport
,
4858 "/printers/%s", sub
->dest
->name
);
4859 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4860 "notify-printer-uri", NULL
, printer_uri
);
4863 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
4864 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4865 "notify-recipient-uri", NULL
, sub
->recipient
);
4866 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
4867 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
4868 "notify-pull-method", NULL
, "ippget");
4870 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
4871 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
4872 "notify-subscriber-user-name", NULL
, sub
->owner
);
4874 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
4875 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4876 "notify-subscription-id", sub
->id
);
4878 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
4879 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4880 "notify-time-interval", sub
->interval
);
4882 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
4883 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
4884 sub
->user_data
, sub
->user_data_len
);
4889 * 'create_job()' - Print a file to a printer or class.
4893 create_job(cupsd_client_t
*con
, /* I - Client connection */
4894 ipp_attribute_t
*uri
) /* I - Printer URI */
4896 cupsd_printer_t
*printer
; /* Printer */
4897 cupsd_job_t
*job
; /* New job */
4900 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
4901 con
->http
.fd
, uri
->values
[0].string
.text
);
4904 * Is the destination valid?
4907 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
4913 send_ipp_status(con
, IPP_NOT_FOUND
,
4914 _("The printer or class was not found."));
4919 * Create the job object...
4922 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
4925 job
->pending_timeout
= 1;
4928 * Save and log the job...
4933 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
4934 job
->id
, job
->dest
, job
->username
);
4939 * 'create_requested_array()' - Create an array for the requested-attributes.
4942 static cups_array_t
* /* O - Array of attributes or NULL */
4943 create_requested_array(ipp_t
*request
) /* I - IPP request */
4945 int i
; /* Looping var */
4946 ipp_attribute_t
*requested
; /* requested-attributes attribute */
4947 cups_array_t
*ra
; /* Requested attributes array */
4948 char *value
; /* Current value */
4952 * Get the requested-attributes attribute, and return NULL if we don't
4956 if ((requested
= ippFindAttribute(request
, "requested-attributes",
4957 IPP_TAG_KEYWORD
)) == NULL
)
4961 * If the attribute contains a single "all" keyword, return NULL...
4964 if (requested
->num_values
== 1 &&
4965 !strcmp(requested
->values
[0].string
.text
, "all"))
4969 * Create an array using "strcmp" as the comparison function...
4972 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
4974 for (i
= 0; i
< requested
->num_values
; i
++)
4976 value
= requested
->values
[i
].string
.text
;
4978 if (!strcmp(value
, "job-template"))
4980 cupsArrayAdd(ra
, "copies");
4981 cupsArrayAdd(ra
, "copies-default");
4982 cupsArrayAdd(ra
, "copies-supported");
4983 cupsArrayAdd(ra
, "finishings");
4984 cupsArrayAdd(ra
, "finishings-default");
4985 cupsArrayAdd(ra
, "finishings-supported");
4986 cupsArrayAdd(ra
, "job-hold-until");
4987 cupsArrayAdd(ra
, "job-hold-until-default");
4988 cupsArrayAdd(ra
, "job-hold-until-supported");
4989 cupsArrayAdd(ra
, "job-priority");
4990 cupsArrayAdd(ra
, "job-priority-default");
4991 cupsArrayAdd(ra
, "job-priority-supported");
4992 cupsArrayAdd(ra
, "job-sheets");
4993 cupsArrayAdd(ra
, "job-sheets-default");
4994 cupsArrayAdd(ra
, "job-sheets-supported");
4995 cupsArrayAdd(ra
, "media");
4996 cupsArrayAdd(ra
, "media-default");
4997 cupsArrayAdd(ra
, "media-supported");
4998 cupsArrayAdd(ra
, "multiple-document-handling");
4999 cupsArrayAdd(ra
, "multiple-document-handling-default");
5000 cupsArrayAdd(ra
, "multiple-document-handling-supported");
5001 cupsArrayAdd(ra
, "number-up");
5002 cupsArrayAdd(ra
, "number-up-default");
5003 cupsArrayAdd(ra
, "number-up-supported");
5004 cupsArrayAdd(ra
, "orientation-requested");
5005 cupsArrayAdd(ra
, "orientation-requested-default");
5006 cupsArrayAdd(ra
, "orientation-requested-supported");
5007 cupsArrayAdd(ra
, "page-ranges");
5008 cupsArrayAdd(ra
, "page-ranges-supported");
5009 cupsArrayAdd(ra
, "printer-resolution");
5010 cupsArrayAdd(ra
, "printer-resolution-default");
5011 cupsArrayAdd(ra
, "printer-resolution-supported");
5012 cupsArrayAdd(ra
, "print-quality");
5013 cupsArrayAdd(ra
, "print-quality-default");
5014 cupsArrayAdd(ra
, "print-quality-supported");
5015 cupsArrayAdd(ra
, "sides");
5016 cupsArrayAdd(ra
, "sides-default");
5017 cupsArrayAdd(ra
, "sides-supported");
5019 else if (!strcmp(value
, "job-description"))
5021 cupsArrayAdd(ra
, "date-time-at-completed");
5022 cupsArrayAdd(ra
, "date-time-at-creation");
5023 cupsArrayAdd(ra
, "date-time-at-processing");
5024 cupsArrayAdd(ra
, "job-detailed-status-message");
5025 cupsArrayAdd(ra
, "job-document-access-errors");
5026 cupsArrayAdd(ra
, "job-id");
5027 cupsArrayAdd(ra
, "job-impressions");
5028 cupsArrayAdd(ra
, "job-impressions-completed");
5029 cupsArrayAdd(ra
, "job-k-octets");
5030 cupsArrayAdd(ra
, "job-k-octets-processed");
5031 cupsArrayAdd(ra
, "job-media-sheets");
5032 cupsArrayAdd(ra
, "job-media-sheets-completed");
5033 cupsArrayAdd(ra
, "job-message-from-operator");
5034 cupsArrayAdd(ra
, "job-more-info");
5035 cupsArrayAdd(ra
, "job-name");
5036 cupsArrayAdd(ra
, "job-originating-user-name");
5037 cupsArrayAdd(ra
, "job-printer-up-time");
5038 cupsArrayAdd(ra
, "job-printer-uri");
5039 cupsArrayAdd(ra
, "job-state");
5040 cupsArrayAdd(ra
, "job-state-message");
5041 cupsArrayAdd(ra
, "job-state-reasons");
5042 cupsArrayAdd(ra
, "job-uri");
5043 cupsArrayAdd(ra
, "number-of-documents");
5044 cupsArrayAdd(ra
, "number-of-intervening-jobs");
5045 cupsArrayAdd(ra
, "output-device-assigned");
5046 cupsArrayAdd(ra
, "time-at-completed");
5047 cupsArrayAdd(ra
, "time-at-creation");
5048 cupsArrayAdd(ra
, "time-at-processing");
5050 else if (!strcmp(value
, "printer-description"))
5052 cupsArrayAdd(ra
, "charset-configured");
5053 cupsArrayAdd(ra
, "charset-supported");
5054 cupsArrayAdd(ra
, "color-supported");
5055 cupsArrayAdd(ra
, "compression-supported");
5056 cupsArrayAdd(ra
, "document-format-default");
5057 cupsArrayAdd(ra
, "document-format-supported");
5058 cupsArrayAdd(ra
, "generated-natural-language-supported");
5059 cupsArrayAdd(ra
, "ipp-versions-supported");
5060 cupsArrayAdd(ra
, "job-impressions-supported");
5061 cupsArrayAdd(ra
, "job-k-octets-supported");
5062 cupsArrayAdd(ra
, "job-media-sheets-supported");
5063 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
5064 cupsArrayAdd(ra
, "multiple-operation-time-out");
5065 cupsArrayAdd(ra
, "natural-language-configured");
5066 cupsArrayAdd(ra
, "notify-attributes-supported");
5067 cupsArrayAdd(ra
, "notify-lease-duration-default");
5068 cupsArrayAdd(ra
, "notify-lease-duration-supported");
5069 cupsArrayAdd(ra
, "notify-max-events-supported");
5070 cupsArrayAdd(ra
, "notify-events-default");
5071 cupsArrayAdd(ra
, "notify-events-supported");
5072 cupsArrayAdd(ra
, "notify-pull-method-supported");
5073 cupsArrayAdd(ra
, "notify-schemes-supported");
5074 cupsArrayAdd(ra
, "operations-supported");
5075 cupsArrayAdd(ra
, "pages-per-minute");
5076 cupsArrayAdd(ra
, "pages-per-minute-color");
5077 cupsArrayAdd(ra
, "pdl-override-supported");
5078 cupsArrayAdd(ra
, "printer-alert");
5079 cupsArrayAdd(ra
, "printer-alert-description");
5080 cupsArrayAdd(ra
, "printer-current-time");
5081 cupsArrayAdd(ra
, "printer-driver-installer");
5082 cupsArrayAdd(ra
, "printer-info");
5083 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
5084 cupsArrayAdd(ra
, "printer-location");
5085 cupsArrayAdd(ra
, "printer-make-and-model");
5086 cupsArrayAdd(ra
, "printer-message-from-operator");
5087 cupsArrayAdd(ra
, "printer-more-info");
5088 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
5089 cupsArrayAdd(ra
, "printer-name");
5090 cupsArrayAdd(ra
, "printer-state");
5091 cupsArrayAdd(ra
, "printer-state-message");
5092 cupsArrayAdd(ra
, "printer-state-reasons");
5093 cupsArrayAdd(ra
, "printer-up-time");
5094 cupsArrayAdd(ra
, "printer-uri-supported");
5095 cupsArrayAdd(ra
, "queued-job-count");
5096 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
5097 cupsArrayAdd(ra
, "uri-authentication-supported");
5098 cupsArrayAdd(ra
, "uri-security-supported");
5100 else if (!strcmp(value
, "printer-defaults"))
5102 char *name
; /* Option name */
5105 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5107 name
= (char *)cupsArrayNext(CommonDefaults
))
5108 cupsArrayAdd(ra
, name
);
5110 else if (!strcmp(value
, "subscription-template"))
5112 cupsArrayAdd(ra
, "notify-attributes");
5113 cupsArrayAdd(ra
, "notify-charset");
5114 cupsArrayAdd(ra
, "notify-events");
5115 cupsArrayAdd(ra
, "notify-lease-duration");
5116 cupsArrayAdd(ra
, "notify-natural-language");
5117 cupsArrayAdd(ra
, "notify-pull-method");
5118 cupsArrayAdd(ra
, "notify-recipient-uri");
5119 cupsArrayAdd(ra
, "notify-time-interval");
5120 cupsArrayAdd(ra
, "notify-user-data");
5123 cupsArrayAdd(ra
, value
);
5131 * 'create_subscription()' - Create a notification subscription.
5135 create_subscription(
5136 cupsd_client_t
*con
, /* I - Client connection */
5137 ipp_attribute_t
*uri
) /* I - Printer URI */
5139 http_status_t status
; /* Policy status */
5140 int i
; /* Looping var */
5141 ipp_attribute_t
*attr
; /* Current attribute */
5142 cups_ptype_t dtype
; /* Destination type (printer/class) */
5143 char scheme
[HTTP_MAX_URI
],
5144 /* Scheme portion of URI */
5145 userpass
[HTTP_MAX_URI
],
5146 /* Username portion of URI */
5148 /* Host portion of URI */
5149 resource
[HTTP_MAX_URI
];
5150 /* Resource portion of URI */
5151 int port
; /* Port portion of URI */
5152 cupsd_printer_t
*printer
; /* Printer/class */
5153 cupsd_job_t
*job
; /* Job */
5154 int jobid
; /* Job ID */
5155 cupsd_subscription_t
*sub
; /* Subscription object */
5156 const char *username
, /* requesting-user-name or
5157 authenticated username */
5158 *recipient
, /* notify-recipient-uri */
5159 *pullmethod
; /* notify-pull-method */
5160 ipp_attribute_t
*user_data
; /* notify-user-data */
5161 int interval
, /* notify-time-interval */
5162 lease
; /* notify-lease-duration */
5163 unsigned mask
; /* notify-events */
5164 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5165 *notify_lease
; /* notify-lease-duration(-default) */
5169 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5171 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5172 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
5173 attr
->value_tag
, attr
->name
);
5175 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
5180 * Is the destination valid?
5183 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5184 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5185 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5187 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5188 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5189 sizeof(host
), &port
, resource
, sizeof(resource
));
5191 if (!strcmp(resource
, "/"))
5193 dtype
= (cups_ptype_t
)0;
5196 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5198 dtype
= (cups_ptype_t
)0;
5201 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5203 dtype
= CUPS_PRINTER_CLASS
;
5206 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5212 send_ipp_status(con
, IPP_NOT_FOUND
,
5213 _("The printer or class was not found."));
5223 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5226 send_http_error(con
, status
, printer
);
5230 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5232 send_http_error(con
, status
, NULL
);
5237 * Get the user that is requesting the subscription...
5240 username
= get_username(con
);
5243 * Find the first subscription group attribute; return if we have
5247 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5248 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5253 send_ipp_status(con
, IPP_BAD_REQUEST
,
5254 _("No subscription attributes in request!"));
5259 * Process the subscription attributes in the request...
5262 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5270 lease
= DefaultLeaseDuration
;
5272 mask
= CUPSD_EVENT_NONE
;
5276 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5278 notify_lease
= ippFindAttribute(printer
->attrs
,
5279 "notify-lease-duration-default",
5283 lease
= notify_lease
->values
[0].integer
;
5287 notify_events
= NULL
;
5288 notify_lease
= NULL
;
5291 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5293 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5294 attr
->value_tag
== IPP_TAG_URI
)
5297 * Validate the recipient scheme against the ServerBin/notifier
5301 char notifier
[1024]; /* Notifier filename */
5304 recipient
= attr
->values
[0].string
.text
;
5306 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5307 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5308 host
, sizeof(host
), &port
,
5309 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5311 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5312 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
5313 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5314 "notify-status-code", IPP_URI_SCHEME
);
5318 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5320 if (access(notifier
, X_OK
))
5322 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5323 _("notify-recipient-uri URI \"%s\" uses unknown "
5324 "scheme!"), recipient
);
5325 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5326 "notify-status-code", IPP_URI_SCHEME
);
5330 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5331 attr
->value_tag
== IPP_TAG_KEYWORD
)
5333 pullmethod
= attr
->values
[0].string
.text
;
5335 if (strcmp(pullmethod
, "ippget"))
5337 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5338 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
5339 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5340 "notify-status-code", IPP_ATTRIBUTES
);
5344 else if (!strcmp(attr
->name
, "notify-charset") &&
5345 attr
->value_tag
== IPP_TAG_CHARSET
&&
5346 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5347 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5349 send_ipp_status(con
, IPP_CHARSET
,
5350 _("Character set \"%s\" not supported!"),
5351 attr
->values
[0].string
.text
);
5354 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5355 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5356 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5358 send_ipp_status(con
, IPP_CHARSET
,
5359 _("Language \"%s\" not supported!"),
5360 attr
->values
[0].string
.text
);
5363 else if (!strcmp(attr
->name
, "notify-user-data") &&
5364 attr
->value_tag
== IPP_TAG_STRING
)
5366 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5368 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5369 _("The notify-user-data value is too large "
5370 "(%d > 63 octets)!"),
5371 attr
->values
[0].unknown
.length
);
5377 else if (!strcmp(attr
->name
, "notify-events") &&
5378 attr
->value_tag
== IPP_TAG_KEYWORD
)
5379 notify_events
= attr
;
5380 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5381 attr
->value_tag
== IPP_TAG_INTEGER
)
5382 lease
= attr
->values
[0].integer
;
5383 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5384 attr
->value_tag
== IPP_TAG_INTEGER
)
5385 interval
= attr
->values
[0].integer
;
5386 else if (!strcmp(attr
->name
, "notify-job-id") &&
5387 attr
->value_tag
== IPP_TAG_INTEGER
)
5388 jobid
= attr
->values
[0].integer
;
5395 for (i
= 0; i
< notify_events
->num_values
; i
++)
5396 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5400 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5402 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5403 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5404 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5406 if (!recipient
&& !pullmethod
)
5409 if (mask
== CUPSD_EVENT_NONE
)
5412 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5414 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5417 send_ipp_status(con
, IPP_BAD_REQUEST
,
5418 _("notify-events not specified!"));
5423 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5425 cupsdLogMessage(CUPSD_LOG_INFO
,
5426 "create_subscription: Limiting notify-lease-duration to "
5429 lease
= MaxLeaseDuration
;
5434 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5436 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
5443 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
5446 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
5449 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5450 "Added subscription %d for printer \"%s\"",
5451 sub
->id
, printer
->name
);
5453 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
5456 sub
->interval
= interval
;
5458 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5460 cupsdSetString(&sub
->owner
, username
);
5464 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5465 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5466 sub
->user_data_len
);
5469 ippAddSeparator(con
->response
);
5470 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5471 "notify-subscription-id", sub
->id
);
5473 con
->response
->request
.status
.status_code
= IPP_OK
;
5479 cupsdSaveAllSubscriptions();
5485 * 'delete_printer()' - Remove a printer or class from the system.
5489 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5490 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5492 http_status_t status
; /* Policy status */
5493 cups_ptype_t dtype
; /* Destination type (printer/class) */
5494 cupsd_printer_t
*printer
; /* Printer/class */
5495 char filename
[1024]; /* Script/PPD filename */
5498 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5499 con
->http
.fd
, uri
->values
[0].string
.text
);
5502 * Do we have a valid URI?
5505 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5511 send_ipp_status(con
, IPP_NOT_FOUND
,
5512 _("The printer or class was not found."));
5520 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5522 send_http_error(con
, status
, NULL
);
5527 * Remove old jobs...
5530 cupsdCancelJobs(printer
->name
, NULL
, 1);
5533 * Remove old subscriptions and send a "deleted printer" event...
5536 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5537 "%s \"%s\" deleted by \"%s\".",
5538 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5539 printer
->name
, get_username(con
));
5541 cupsdExpireSubscriptions(printer
, NULL
);
5544 * Remove any old PPD or script files...
5547 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5551 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5555 if (dtype
& CUPS_PRINTER_CLASS
)
5557 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5558 printer
->name
, get_username(con
));
5560 cupsdDeletePrinter(printer
, 0);
5561 cupsdSaveAllClasses();
5565 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5566 printer
->name
, get_username(con
));
5568 cupsdDeletePrinter(printer
, 0);
5569 cupsdSaveAllPrinters();
5572 cupsdWritePrintcap();
5575 * Return with no errors...
5578 con
->response
->request
.status
.status_code
= IPP_OK
;
5583 * 'get_default()' - Get the default destination.
5587 get_default(cupsd_client_t
*con
) /* I - Client connection */
5589 http_status_t status
; /* Policy status */
5590 cups_array_t
*ra
; /* Requested attributes array */
5593 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5599 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5601 send_http_error(con
, status
, NULL
);
5607 ra
= create_requested_array(con
->request
);
5609 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5611 cupsArrayDelete(ra
);
5613 con
->response
->request
.status
.status_code
= IPP_OK
;
5616 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5621 * 'get_devices()' - Get the list of available devices on the local system.
5625 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5627 http_status_t status
; /* Policy status */
5628 ipp_attribute_t
*limit
, /* Limit attribute */
5629 *requested
; /* requested-attributes attribute */
5630 char command
[1024], /* cups-deviced command */
5631 options
[1024], /* Options to pass to command */
5633 /* String for requested attributes */
5636 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5642 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5644 send_http_error(con
, status
, NULL
);
5649 * Run cups-deviced command with the given options...
5652 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5653 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5657 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5659 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5661 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5662 snprintf(options
, sizeof(options
),
5664 con
->request
->request
.op
.request_id
,
5665 limit
? limit
->values
[0].integer
: 0, (int)User
,
5668 if (cupsdSendCommand(con
, command
, options
, 1))
5671 * Command started successfully, don't send an IPP response here...
5674 ippDelete(con
->response
);
5675 con
->response
= NULL
;
5680 * Command failed, return "internal error" so the user knows something
5684 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5685 _("cups-deviced failed to execute."));
5691 * 'get_document()' - Get a copy of a job file.
5695 get_document(cupsd_client_t
*con
, /* I - Client connection */
5696 ipp_attribute_t
*uri
) /* I - Job URI */
5698 http_status_t status
; /* Policy status */
5699 ipp_attribute_t
*attr
; /* Current attribute */
5700 int jobid
; /* Job ID */
5701 int docnum
; /* Document number */
5702 cupsd_job_t
*job
; /* Current job */
5703 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5704 username
[HTTP_MAX_URI
], /* Username portion of URI */
5705 host
[HTTP_MAX_URI
], /* Host portion of URI */
5706 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5707 int port
; /* Port portion of URI */
5708 char filename
[1024], /* Filename for document */
5709 format
[1024]; /* Format for document */
5712 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
5713 con
->http
.fd
, uri
->values
[0].string
.text
);
5716 * See if we have a job URI or a printer URI...
5719 if (!strcmp(uri
->name
, "printer-uri"))
5722 * Got a printer URI; see if we also have a job-id attribute...
5725 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5726 IPP_TAG_INTEGER
)) == NULL
)
5728 send_ipp_status(con
, IPP_BAD_REQUEST
,
5729 _("Got a printer-uri attribute but no job-id!"));
5733 jobid
= attr
->values
[0].integer
;
5738 * Got a job URI; parse it to get the job ID...
5741 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5742 sizeof(method
), username
, sizeof(username
), host
,
5743 sizeof(host
), &port
, resource
, sizeof(resource
));
5745 if (strncmp(resource
, "/jobs/", 6))
5751 send_ipp_status(con
, IPP_BAD_REQUEST
,
5752 _("Bad job-uri attribute \"%s\"!"),
5753 uri
->values
[0].string
.text
);
5757 jobid
= atoi(resource
+ 6);
5761 * See if the job exists...
5764 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5767 * Nope - return a "not found" error...
5770 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5778 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5780 send_http_error(con
, status
, NULL
);
5785 * Get the document number...
5788 if ((attr
= ippFindAttribute(con
->request
, "document-number",
5789 IPP_TAG_INTEGER
)) == NULL
)
5791 send_ipp_status(con
, IPP_BAD_REQUEST
,
5792 _("Missing document-number attribute!"));
5796 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
5797 attr
->num_values
> 1)
5799 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
5804 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
5806 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
5808 cupsdLogMessage(CUPSD_LOG_ERROR
,
5809 "Unable to open document %d in job %d - %s", docnum
, jobid
,
5811 send_ipp_status(con
, IPP_NOT_FOUND
,
5812 _("Unable to open document %d in job %d!"), docnum
, jobid
);
5816 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
5820 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
5821 job
->filetypes
[docnum
- 1]->type
);
5823 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
5825 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
5827 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
5828 IPP_TAG_NAME
)) != NULL
)
5829 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
5830 NULL
, attr
->values
[0].string
.text
);
5835 * 'get_job_attrs()' - Get job attributes.
5839 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5840 ipp_attribute_t
*uri
) /* I - Job URI */
5842 http_status_t status
; /* Policy status */
5843 ipp_attribute_t
*attr
; /* Current attribute */
5844 int jobid
; /* Job ID */
5845 cupsd_job_t
*job
; /* Current job */
5846 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5847 username
[HTTP_MAX_URI
], /* Username portion of URI */
5848 host
[HTTP_MAX_URI
], /* Host portion of URI */
5849 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5850 int port
; /* Port portion of URI */
5851 cups_array_t
*ra
; /* Requested attributes array */
5854 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5855 con
->http
.fd
, uri
->values
[0].string
.text
);
5858 * See if we have a job URI or a printer URI...
5861 if (!strcmp(uri
->name
, "printer-uri"))
5864 * Got a printer URI; see if we also have a job-id attribute...
5867 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5868 IPP_TAG_INTEGER
)) == NULL
)
5870 send_ipp_status(con
, IPP_BAD_REQUEST
,
5871 _("Got a printer-uri attribute but no job-id!"));
5875 jobid
= attr
->values
[0].integer
;
5880 * Got a job URI; parse it to get the job ID...
5883 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5884 sizeof(method
), username
, sizeof(username
), host
,
5885 sizeof(host
), &port
, resource
, sizeof(resource
));
5887 if (strncmp(resource
, "/jobs/", 6))
5893 send_ipp_status(con
, IPP_BAD_REQUEST
,
5894 _("Bad job-uri attribute \"%s\"!"),
5895 uri
->values
[0].string
.text
);
5899 jobid
= atoi(resource
+ 6);
5903 * See if the job exists...
5906 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5909 * Nope - return a "not found" error...
5912 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5920 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5922 send_http_error(con
, status
, NULL
);
5927 * Copy attributes...
5932 ra
= create_requested_array(con
->request
);
5933 copy_job_attrs(con
, job
, ra
);
5934 cupsArrayDelete(ra
);
5936 con
->response
->request
.status
.status_code
= IPP_OK
;
5941 * 'get_jobs()' - Get a list of jobs for the specified printer.
5945 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
5946 ipp_attribute_t
*uri
) /* I - Printer URI */
5948 http_status_t status
; /* Policy status */
5949 ipp_attribute_t
*attr
; /* Current attribute */
5950 const char *dest
; /* Destination */
5951 cups_ptype_t dtype
; /* Destination type (printer/class) */
5952 cups_ptype_t dmask
; /* Destination type mask */
5953 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
5954 username
[HTTP_MAX_URI
], /* Username portion of URI */
5955 host
[HTTP_MAX_URI
], /* Host portion of URI */
5956 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5957 int port
; /* Port portion of URI */
5958 int completed
; /* Completed jobs? */
5959 int first_job_id
; /* First job ID */
5960 int limit
; /* Maximum number of jobs to return */
5961 int count
; /* Number of jobs that match */
5962 cupsd_job_t
*job
; /* Current job pointer */
5963 cupsd_printer_t
*printer
; /* Printer */
5964 cups_array_t
*list
; /* Which job list... */
5965 cups_array_t
*ra
; /* Requested attributes array */
5968 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
5969 uri
->values
[0].string
.text
);
5972 * Is the destination valid?
5975 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5976 sizeof(scheme
), username
, sizeof(username
), host
,
5977 sizeof(host
), &port
, resource
, sizeof(resource
));
5979 if (!strcmp(resource
, "/") ||
5980 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
5983 dtype
= (cups_ptype_t
)0;
5984 dmask
= (cups_ptype_t
)0;
5987 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5990 dtype
= (cups_ptype_t
)0;
5991 dmask
= CUPS_PRINTER_CLASS
;
5994 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5997 dtype
= CUPS_PRINTER_CLASS
;
5998 dmask
= CUPS_PRINTER_CLASS
;
6001 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6008 send_ipp_status(con
, IPP_NOT_FOUND
,
6009 _("The printer or class was not found."));
6014 dtype
&= CUPS_PRINTER_CLASS
;
6015 dmask
= CUPS_PRINTER_CLASS
;
6024 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6027 send_http_error(con
, status
, printer
);
6031 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6033 send_http_error(con
, status
, NULL
);
6038 * See if the "which-jobs" attribute have been specified...
6041 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6042 IPP_TAG_KEYWORD
)) != NULL
&&
6043 !strcmp(attr
->values
[0].string
.text
, "completed"))
6048 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
6060 * See if they want to limit the number of jobs reported...
6063 if ((attr
= ippFindAttribute(con
->request
, "limit",
6064 IPP_TAG_INTEGER
)) != NULL
)
6065 limit
= attr
->values
[0].integer
;
6069 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
6070 IPP_TAG_INTEGER
)) != NULL
)
6071 first_job_id
= attr
->values
[0].integer
;
6076 * See if we only want to see jobs for a specific user...
6079 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
6080 IPP_TAG_BOOLEAN
)) != NULL
&&
6081 attr
->values
[0].boolean
)
6082 strlcpy(username
, get_username(con
), sizeof(username
));
6086 ra
= create_requested_array(con
->request
);
6089 * OK, build a list of jobs for this printer...
6092 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6093 count
< limit
&& job
;
6094 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6097 * Filter out jobs that don't match...
6100 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
6102 if (!job
->dest
|| !job
->username
)
6105 if (!job
->dest
|| !job
->username
)
6108 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6109 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6111 if ((job
->dtype
& dmask
) != dtype
&&
6112 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6114 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
6117 if (job
->id
< first_job_id
)
6125 if (username
[0] && strcasecmp(username
, job
->username
))
6129 ippAddSeparator(con
->response
);
6133 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
6135 copy_job_attrs(con
, job
, ra
);
6138 cupsArrayDelete(ra
);
6140 con
->response
->request
.status
.status_code
= IPP_OK
;
6145 * 'get_notifications()' - Get events for a subscription.
6149 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6151 int i
, j
; /* Looping vars */
6152 http_status_t status
; /* Policy status */
6153 cupsd_subscription_t
*sub
; /* Subscription */
6154 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6155 *sequences
; /* notify-sequence-numbers */
6156 int min_seq
; /* Minimum sequence number */
6157 int interval
; /* Poll interval */
6160 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6164 * Get subscription attributes...
6167 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6169 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6174 send_ipp_status(con
, IPP_BAD_REQUEST
,
6175 _("Missing notify-subscription-ids attribute!"));
6180 * Are the subscription IDs valid?
6183 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6185 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6188 * Bad subscription ID...
6191 send_ipp_status(con
, IPP_NOT_FOUND
,
6192 _("notify-subscription-id %d no good!"),
6193 ids
->values
[i
].integer
);
6201 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6203 con
, sub
->owner
)) != HTTP_OK
)
6205 send_http_error(con
, status
, sub
->dest
);
6210 * Check the subscription type and update the interval accordingly.
6213 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6216 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6218 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6224 * Tell the client to poll again in N seconds...
6228 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6229 "notify-get-interval", interval
);
6231 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6232 "printer-up-time", time(NULL
));
6235 * Copy the subscription event attributes to the response.
6238 con
->response
->request
.status
.status_code
=
6239 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6241 for (i
= 0; i
< ids
->num_values
; i
++)
6244 * Get the subscription and sequence number...
6247 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6249 if (sequences
&& i
< sequences
->num_values
)
6250 min_seq
= sequences
->values
[i
].integer
;
6255 * If we don't have any new events, nothing to do here...
6258 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
6262 * Otherwise copy all of the new events...
6265 if (sub
->first_event_id
> min_seq
)
6268 j
= min_seq
- sub
->first_event_id
;
6270 for (; j
< sub
->num_events
; j
++)
6272 ippAddSeparator(con
->response
);
6274 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
6275 IPP_TAG_EVENT_NOTIFICATION
, 0);
6282 * 'get_ppd()' - Get a named PPD from the local system.
6286 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
6287 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
6289 http_status_t status
; /* Policy status */
6290 cupsd_printer_t
*dest
; /* Destination */
6291 cups_ptype_t dtype
; /* Destination type */
6294 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
6295 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
6297 if (!strcmp(uri
->name
, "ppd-name"))
6300 * Return a PPD file from cups-driverd...
6303 char command
[1024], /* cups-driverd command */
6304 options
[1024], /* Options to pass to command */
6305 ppd_name
[1024]; /* ppd-name */
6312 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6314 send_http_error(con
, status
, NULL
);
6319 * Run cups-driverd command with the given options...
6322 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6323 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
6324 snprintf(options
, sizeof(options
), "get+%d+%s",
6325 con
->request
->request
.op
.request_id
, ppd_name
);
6327 if (cupsdSendCommand(con
, command
, options
, 0))
6330 * Command started successfully, don't send an IPP response here...
6333 ippDelete(con
->response
);
6334 con
->response
= NULL
;
6339 * Command failed, return "internal error" so the user knows something
6343 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6344 _("cups-driverd failed to execute."));
6347 else if (!strcmp(uri
->name
, "printer-uri") &&
6348 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
6350 int i
; /* Looping var */
6351 char filename
[1024]; /* PPD filename */
6358 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6360 send_http_error(con
, status
, dest
);
6365 * See if we need the PPD for a class or remote printer...
6368 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6371 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
6373 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6374 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6375 "printer-uri", NULL
, dest
->uri
);
6378 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
6380 for (i
= 0; i
< dest
->num_printers
; i
++)
6381 if (!(dest
->printers
[i
]->type
&
6382 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
6384 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6385 dest
->printers
[i
]->name
);
6387 if (!access(filename
, 0))
6391 if (i
< dest
->num_printers
)
6392 dest
= dest
->printers
[i
];
6395 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6396 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6397 "printer-uri", NULL
, dest
->printers
[0]->uri
);
6403 * Found the printer with the PPD file, now see if there is one...
6406 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
6408 send_ipp_status(con
, IPP_NOT_FOUND
,
6409 _("The PPD file \"%s\" could not be opened: %s"),
6410 uri
->values
[0].string
.text
, strerror(errno
));
6414 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6418 con
->response
->request
.status
.status_code
= IPP_OK
;
6421 send_ipp_status(con
, IPP_NOT_FOUND
,
6422 _("The PPD file \"%s\" could not be found."),
6423 uri
->values
[0].string
.text
);
6428 * 'get_ppds()' - Get the list of PPD files on the local system.
6432 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
6434 http_status_t status
; /* Policy status */
6435 ipp_attribute_t
*limit
, /* Limit attribute */
6436 *device
, /* ppd-device-id attribute */
6437 *language
, /* ppd-natural-language attribute */
6438 *make
, /* ppd-make attribute */
6439 *model
, /* ppd-make-and-model attribute */
6440 *model_number
, /* ppd-model-number attribute */
6441 *product
, /* ppd-product attribute */
6442 *psversion
, /* ppd-psverion attribute */
6443 *type
, /* ppd-type attribute */
6444 *requested
; /* requested-attributes attribute */
6445 char command
[1024], /* cups-driverd command */
6446 options
[1024], /* Options to pass to command */
6447 device_str
[256],/* Escaped ppd-device-id string */
6449 /* Escaped ppd-natural-language */
6450 make_str
[256], /* Escaped ppd-make string */
6451 model_str
[256], /* Escaped ppd-make-and-model string */
6452 model_number_str
[256],
6453 /* ppd-model-number string */
6455 /* Escaped ppd-product string */
6457 /* Escaped ppd-psversion string */
6458 type_str
[256], /* Escaped ppd-type string */
6460 /* String for requested attributes */
6463 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
6469 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6471 send_http_error(con
, status
, NULL
);
6476 * Run cups-driverd command with the given options...
6479 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6480 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
6481 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
6483 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
6484 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
6486 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
6488 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
6489 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
6490 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
6491 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6495 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6497 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6500 url_encode_attr(device
, device_str
, sizeof(device_str
));
6502 device_str
[0] = '\0';
6505 url_encode_attr(language
, language_str
, sizeof(language_str
));
6507 language_str
[0] = '\0';
6510 url_encode_attr(make
, make_str
, sizeof(make_str
));
6515 url_encode_attr(model
, model_str
, sizeof(model_str
));
6517 model_str
[0] = '\0';
6520 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
6521 model_number
->values
[0].integer
);
6523 model_number_str
[0] = '\0';
6526 url_encode_attr(product
, product_str
, sizeof(product_str
));
6528 product_str
[0] = '\0';
6531 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
6533 psversion_str
[0] = '\0';
6536 url_encode_attr(type
, type_str
, sizeof(type_str
));
6540 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6541 snprintf(options
, sizeof(options
),
6542 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
6543 con
->request
->request
.op
.request_id
,
6544 limit
? limit
->values
[0].integer
: 0,
6546 device
? "%20" : "", device_str
,
6547 language
? "%20" : "", language_str
,
6548 make
? "%20" : "", make_str
,
6549 model
? "%20" : "", model_str
,
6550 model_number
? "%20" : "", model_number_str
,
6551 product
? "%20" : "", product_str
,
6552 psversion
? "%20" : "", psversion_str
,
6553 type
? "%20" : "", type_str
);
6555 if (cupsdSendCommand(con
, command
, options
, 0))
6558 * Command started successfully, don't send an IPP response here...
6561 ippDelete(con
->response
);
6562 con
->response
= NULL
;
6567 * Command failed, return "internal error" so the user knows something
6571 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6572 _("cups-driverd failed to execute."));
6578 * 'get_printer_attrs()' - Get printer attributes.
6582 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
6583 ipp_attribute_t
*uri
) /* I - Printer URI */
6585 http_status_t status
; /* Policy status */
6586 cups_ptype_t dtype
; /* Destination type (printer/class) */
6587 cupsd_printer_t
*printer
; /* Printer/class */
6588 cups_array_t
*ra
; /* Requested attributes array */
6591 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
6592 con
->http
.fd
, uri
->values
[0].string
.text
);
6595 * Is the destination valid?
6598 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6604 send_ipp_status(con
, IPP_NOT_FOUND
,
6605 _("The printer or class was not found."));
6613 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6615 send_http_error(con
, status
, printer
);
6620 * Send the attributes...
6623 ra
= create_requested_array(con
->request
);
6625 copy_printer_attrs(con
, printer
, ra
);
6627 cupsArrayDelete(ra
);
6629 con
->response
->request
.status
.status_code
= IPP_OK
;
6634 * 'get_printers()' - Get a list of printers or classes.
6638 get_printers(cupsd_client_t
*con
, /* I - Client connection */
6639 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
6641 http_status_t status
; /* Policy status */
6642 ipp_attribute_t
*attr
; /* Current attribute */
6643 int limit
; /* Max number of printers to return */
6644 int count
; /* Number of printers that match */
6645 cupsd_printer_t
*printer
; /* Current printer pointer */
6646 int printer_type
, /* printer-type attribute */
6647 printer_mask
; /* printer-type-mask attribute */
6648 char *location
; /* Location string */
6649 const char *username
; /* Current user */
6650 char *first_printer_name
; /* first-printer-name attribute */
6651 cups_array_t
*ra
; /* Requested attributes array */
6654 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
6655 con
->http
.fd
, type
);
6661 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6663 send_http_error(con
, status
, NULL
);
6668 * Check for printers...
6671 if (!Printers
|| !cupsArrayCount(Printers
))
6673 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
6678 * See if they want to limit the number of printers reported...
6681 if ((attr
= ippFindAttribute(con
->request
, "limit",
6682 IPP_TAG_INTEGER
)) != NULL
)
6683 limit
= attr
->values
[0].integer
;
6687 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
6688 IPP_TAG_NAME
)) != NULL
)
6689 first_printer_name
= attr
->values
[0].string
.text
;
6691 first_printer_name
= NULL
;
6694 * Support filtering...
6697 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
6698 IPP_TAG_ENUM
)) != NULL
)
6699 printer_type
= attr
->values
[0].integer
;
6703 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
6704 IPP_TAG_ENUM
)) != NULL
)
6705 printer_mask
= attr
->values
[0].integer
;
6709 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
6710 IPP_TAG_TEXT
)) != NULL
)
6711 location
= attr
->values
[0].string
.text
;
6715 if (con
->username
[0])
6716 username
= con
->username
;
6717 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6718 IPP_TAG_NAME
)) != NULL
)
6719 username
= attr
->values
[0].string
.text
;
6723 ra
= create_requested_array(con
->request
);
6726 * OK, build a list of printers for this printer...
6729 if (first_printer_name
)
6731 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
6732 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
6735 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
6738 count
< limit
&& printer
;
6739 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
6741 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
6742 (printer
->type
& printer_mask
) == printer_type
&&
6743 (!location
|| !printer
->location
||
6744 !strcasecmp(printer
->location
, location
)))
6747 * If HideImplicitMembers is enabled, see if this printer or class
6748 * is a member of an implicit class...
6751 if (ImplicitClasses
&& HideImplicitMembers
&&
6752 printer
->in_implicit_class
)
6756 * If a username is specified, see if it is allowed or denied
6760 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
6764 * Add the group separator as needed...
6768 ippAddSeparator(con
->response
);
6773 * Send the attributes...
6776 copy_printer_attrs(con
, printer
, ra
);
6780 cupsArrayDelete(ra
);
6782 con
->response
->request
.status
.status_code
= IPP_OK
;
6787 * 'get_subscription_attrs()' - Get subscription attributes.
6791 get_subscription_attrs(
6792 cupsd_client_t
*con
, /* I - Client connection */
6793 int sub_id
) /* I - Subscription ID */
6795 http_status_t status
; /* Policy status */
6796 cupsd_subscription_t
*sub
; /* Subscription */
6797 cups_array_t
*ra
; /* Requested attributes array */
6800 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6801 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
6802 con
, con
->http
.fd
, sub_id
);
6805 * Is the subscription ID valid?
6808 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
6811 * Bad subscription ID...
6814 send_ipp_status(con
, IPP_NOT_FOUND
,
6815 _("notify-subscription-id %d no good!"), sub_id
);
6823 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6825 con
, sub
->owner
)) != HTTP_OK
)
6827 send_http_error(con
, status
, sub
->dest
);
6832 * Copy the subscription attributes to the response using the
6833 * requested-attributes attribute that may be provided by the client.
6836 ra
= create_requested_array(con
->request
);
6838 copy_subscription_attrs(con
, sub
, ra
);
6840 cupsArrayDelete(ra
);
6842 con
->response
->request
.status
.status_code
= IPP_OK
;
6847 * 'get_subscriptions()' - Get subscriptions.
6851 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
6852 ipp_attribute_t
*uri
) /* I - Printer/job URI */
6854 http_status_t status
; /* Policy status */
6855 int count
; /* Number of subscriptions */
6856 int limit
; /* Limit */
6857 cupsd_subscription_t
*sub
; /* Subscription */
6858 cups_array_t
*ra
; /* Requested attributes array */
6859 ipp_attribute_t
*attr
; /* Attribute */
6860 cups_ptype_t dtype
; /* Destination type (printer/class) */
6861 char scheme
[HTTP_MAX_URI
],
6862 /* Scheme portion of URI */
6863 username
[HTTP_MAX_URI
],
6864 /* Username portion of URI */
6866 /* Host portion of URI */
6867 resource
[HTTP_MAX_URI
];
6868 /* Resource portion of URI */
6869 int port
; /* Port portion of URI */
6870 cupsd_job_t
*job
; /* Job pointer */
6871 cupsd_printer_t
*printer
; /* Printer */
6874 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6875 "get_subscriptions(con=%p[%d], uri=%s)",
6876 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
6879 * Is the destination valid?
6882 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6883 sizeof(scheme
), username
, sizeof(username
), host
,
6884 sizeof(host
), &port
, resource
, sizeof(resource
));
6886 if (!strcmp(resource
, "/") ||
6887 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
6888 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
6889 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
6894 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
6897 job
= cupsdFindJob(atoi(resource
+ 6));
6901 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
6906 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6912 send_ipp_status(con
, IPP_NOT_FOUND
,
6913 _("The printer or class was not found."));
6916 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
6917 IPP_TAG_INTEGER
)) != NULL
)
6919 job
= cupsdFindJob(attr
->values
[0].integer
);
6923 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
6924 attr
->values
[0].integer
);
6935 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
6937 con
, NULL
)) != HTTP_OK
)
6939 send_http_error(con
, status
, printer
);
6944 * Copy the subscription attributes to the response using the
6945 * requested-attributes attribute that may be provided by the client.
6948 ra
= create_requested_array(con
->request
);
6950 if ((attr
= ippFindAttribute(con
->request
, "limit",
6951 IPP_TAG_INTEGER
)) != NULL
)
6952 limit
= attr
->values
[0].integer
;
6957 * See if we only want to see subscriptions for a specific user...
6960 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
6961 IPP_TAG_BOOLEAN
)) != NULL
&&
6962 attr
->values
[0].boolean
)
6963 strlcpy(username
, get_username(con
), sizeof(username
));
6967 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
6969 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
6970 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
6971 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
6973 ippAddSeparator(con
->response
);
6974 copy_subscription_attrs(con
, sub
, ra
);
6977 if (limit
&& count
>= limit
)
6981 cupsArrayDelete(ra
);
6984 con
->response
->request
.status
.status_code
= IPP_OK
;
6986 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
6991 * 'get_username()' - Get the username associated with a request.
6994 static const char * /* O - Username */
6995 get_username(cupsd_client_t
*con
) /* I - Connection */
6997 ipp_attribute_t
*attr
; /* Attribute */
7000 if (con
->username
[0])
7001 return (con
->username
);
7002 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7003 IPP_TAG_NAME
)) != NULL
)
7004 return (attr
->values
[0].string
.text
);
7006 return ("anonymous");
7011 * 'hold_job()' - Hold a print job.
7015 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7016 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7018 ipp_attribute_t
*attr
, /* Current job-hold-until */
7019 *newattr
; /* New job-hold-until */
7020 int jobid
; /* Job ID */
7021 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7022 username
[HTTP_MAX_URI
], /* Username portion of URI */
7023 host
[HTTP_MAX_URI
], /* Host portion of URI */
7024 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7025 int port
; /* Port portion of URI */
7026 cupsd_job_t
*job
; /* Job information */
7029 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
7030 uri
->values
[0].string
.text
);
7033 * See if we have a job URI or a printer URI...
7036 if (!strcmp(uri
->name
, "printer-uri"))
7039 * Got a printer URI; see if we also have a job-id attribute...
7042 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7043 IPP_TAG_INTEGER
)) == NULL
)
7045 send_ipp_status(con
, IPP_BAD_REQUEST
,
7046 _("Got a printer-uri attribute but no job-id!"));
7050 jobid
= attr
->values
[0].integer
;
7055 * Got a job URI; parse it to get the job ID...
7058 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7059 sizeof(method
), username
, sizeof(username
), host
,
7060 sizeof(host
), &port
, resource
, sizeof(resource
));
7062 if (strncmp(resource
, "/jobs/", 6))
7068 send_ipp_status(con
, IPP_BAD_REQUEST
,
7069 _("Bad job-uri attribute \"%s\"!"),
7070 uri
->values
[0].string
.text
);
7074 jobid
= atoi(resource
+ 6);
7078 * See if the job exists...
7081 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7084 * Nope - return a "not found" error...
7087 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7092 * See if the job is owned by the requesting user...
7095 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7097 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7102 * Hold the job and return...
7107 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
7108 "Job held by user.");
7110 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
7111 IPP_TAG_KEYWORD
)) == NULL
)
7112 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7114 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7115 IPP_TAG_KEYWORD
)) == NULL
)
7116 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7121 * Free the old hold value and copy the new one over...
7124 _cupsStrFree(attr
->values
[0].string
.text
);
7128 attr
->value_tag
= newattr
->value_tag
;
7129 attr
->values
[0].string
.text
=
7130 _cupsStrAlloc(newattr
->values
[0].string
.text
);
7134 attr
->value_tag
= IPP_TAG_KEYWORD
;
7135 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
7139 * Hold job until specified time...
7142 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
7144 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
7145 "Job job-hold-until value changed by user.");
7148 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Held by \"%s\".", jobid
,
7151 con
->response
->request
.status
.status_code
= IPP_OK
;
7156 * 'move_job()' - Move a job to a new destination.
7160 move_job(cupsd_client_t
*con
, /* I - Client connection */
7161 ipp_attribute_t
*uri
) /* I - Job URI */
7163 http_status_t status
; /* Policy status */
7164 ipp_attribute_t
*attr
; /* Current attribute */
7165 int jobid
; /* Job ID */
7166 cupsd_job_t
*job
; /* Current job */
7167 const char *src
; /* Source printer/class */
7168 cups_ptype_t stype
, /* Source type (printer or class) */
7169 dtype
; /* Destination type (printer/class) */
7170 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7171 username
[HTTP_MAX_URI
], /* Username portion of URI */
7172 host
[HTTP_MAX_URI
], /* Host portion of URI */
7173 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7174 int port
; /* Port portion of URI */
7175 cupsd_printer_t
*sprinter
, /* Source printer */
7176 *dprinter
; /* Destination printer */
7179 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
7180 uri
->values
[0].string
.text
);
7183 * Get the new printer or class...
7186 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7187 IPP_TAG_URI
)) == NULL
)
7190 * Need job-printer-uri...
7193 send_ipp_status(con
, IPP_BAD_REQUEST
,
7194 _("job-printer-uri attribute missing!"));
7198 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7204 send_ipp_status(con
, IPP_NOT_FOUND
,
7205 _("The printer or class was not found."));
7213 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
7216 send_http_error(con
, status
, dprinter
);
7221 * See if we have a job URI or a printer URI...
7224 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7225 sizeof(scheme
), username
, sizeof(username
), host
,
7226 sizeof(host
), &port
, resource
, sizeof(resource
));
7228 if (!strcmp(uri
->name
, "printer-uri"))
7231 * Got a printer URI; see if we also have a job-id attribute...
7234 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7235 IPP_TAG_INTEGER
)) == NULL
)
7241 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
7242 &sprinter
)) == NULL
)
7248 send_ipp_status(con
, IPP_NOT_FOUND
,
7249 _("The printer or class was not found."));
7258 * Otherwise, just move a single job...
7261 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7264 * Nope - return a "not found" error...
7267 send_ipp_status(con
, IPP_NOT_FOUND
,
7268 _("Job #%d does not exist!"), attr
->values
[0].integer
);
7274 * Job found, initialize source pointers...
7285 * Got a job URI; parse it to get the job ID...
7288 if (strncmp(resource
, "/jobs/", 6))
7294 send_ipp_status(con
, IPP_BAD_REQUEST
,
7295 _("Bad job-uri attribute \"%s\"!"),
7296 uri
->values
[0].string
.text
);
7301 * See if the job exists...
7304 jobid
= atoi(resource
+ 6);
7306 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7309 * Nope - return a "not found" error...
7312 send_ipp_status(con
, IPP_NOT_FOUND
,
7313 _("Job #%d does not exist!"), jobid
);
7319 * Job found, initialize source pointers...
7328 * Now move the job or jobs...
7334 * See if the job has been completed...
7337 if (job
->state_value
> IPP_JOB_STOPPED
)
7340 * Return a "not-possible" error...
7343 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7344 _("Job #%d is finished and cannot be altered!"),
7350 * See if the job is owned by the requesting user...
7353 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7355 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7360 * Move the job to a different printer or class...
7363 cupsdMoveJob(job
, dprinter
);
7368 * Got the source printer, now look through the jobs...
7371 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
7373 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
7376 * See if the job is pointing at the source printer or has not been
7380 if (strcasecmp(job
->dest
, src
) ||
7381 job
->state_value
> IPP_JOB_STOPPED
)
7385 * See if the job can be moved by the requesting user...
7388 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7392 * Move the job to a different printer or class...
7395 cupsdMoveJob(job
, dprinter
);
7400 * Start jobs if possible...
7406 * Return with "everything is OK" status...
7409 con
->response
->request
.status
.status_code
= IPP_OK
;
7414 * 'ppd_parse_line()' - Parse a PPD default line.
7417 static int /* O - 0 on success, -1 on failure */
7418 ppd_parse_line(const char *line
, /* I - Line */
7419 char *option
, /* O - Option name */
7420 int olen
, /* I - Size of option name */
7421 char *choice
, /* O - Choice name */
7422 int clen
) /* I - Size of choice name */
7425 * Verify this is a default option line...
7428 if (strncmp(line
, "*Default", 8))
7432 * Read the option name...
7435 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
7445 * Skip everything else up to the colon (:)...
7448 while (*line
&& *line
!= ':')
7457 * Now grab the option choice, skipping leading whitespace...
7460 while (isspace(*line
& 255))
7463 for (clen
--; isalnum(*line
& 255); line
++)
7473 * Return with no errors...
7481 * 'print_job()' - Print a file to a printer or class.
7485 print_job(cupsd_client_t
*con
, /* I - Client connection */
7486 ipp_attribute_t
*uri
) /* I - Printer URI */
7488 ipp_attribute_t
*attr
; /* Current attribute */
7489 ipp_attribute_t
*format
; /* Document-format attribute */
7490 const char *default_format
; /* document-format-default value */
7491 cupsd_job_t
*job
; /* New job */
7492 char filename
[1024]; /* Job filename */
7493 mime_type_t
*filetype
; /* Type of file */
7494 char super
[MIME_MAX_SUPER
], /* Supertype of file */
7495 type
[MIME_MAX_TYPE
], /* Subtype of file */
7496 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7497 /* Textual name of mime type */
7498 cupsd_printer_t
*printer
; /* Printer data */
7499 struct stat fileinfo
; /* File information */
7500 int kbytes
; /* Size of file */
7501 int compression
; /* Document compression */
7504 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
7505 uri
->values
[0].string
.text
);
7508 * Validate print file attributes, for now just document-format and
7509 * compression (CUPS only supports "none" and "gzip")...
7512 compression
= CUPS_FILE_NONE
;
7514 if ((attr
= ippFindAttribute(con
->request
, "compression",
7515 IPP_TAG_KEYWORD
)) != NULL
)
7517 if (strcmp(attr
->values
[0].string
.text
, "none")
7519 && strcmp(attr
->values
[0].string
.text
, "gzip")
7520 #endif /* HAVE_LIBZ */
7523 send_ipp_status(con
, IPP_ATTRIBUTES
,
7524 _("Unsupported compression \"%s\"!"),
7525 attr
->values
[0].string
.text
);
7526 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7527 "compression", NULL
, attr
->values
[0].string
.text
);
7532 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7533 compression
= CUPS_FILE_GZIP
;
7534 #endif /* HAVE_LIBZ */
7538 * Do we have a file to print?
7543 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7548 * Is the destination valid?
7551 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
7557 send_ipp_status(con
, IPP_NOT_FOUND
,
7558 _("The printer or class was not found."));
7563 * Is it a format we support?
7566 if ((format
= ippFindAttribute(con
->request
, "document-format",
7567 IPP_TAG_MIMETYPE
)) != NULL
)
7570 * Grab format from client...
7573 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
7576 send_ipp_status(con
, IPP_BAD_REQUEST
,
7577 _("Could not scan type \"%s\"!"),
7578 format
->values
[0].string
.text
);
7582 else if ((default_format
= cupsGetOption("document-format",
7583 printer
->num_options
,
7584 printer
->options
)) != NULL
)
7587 * Use default document format...
7590 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
7592 send_ipp_status(con
, IPP_BAD_REQUEST
,
7593 _("Could not scan type \"%s\"!"),
7604 strcpy(super
, "application");
7605 strcpy(type
, "octet-stream");
7608 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7611 * Auto-type the file...
7614 ipp_attribute_t
*doc_name
; /* document-name attribute */
7617 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
7619 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7620 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7621 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7625 filetype
= mimeType(MimeDatabase
, super
, type
);
7628 filetype
= mimeType(MimeDatabase
, super
, type
);
7632 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
7635 * Replace the document-format attribute value with the auto-typed or
7639 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7644 _cupsStrFree(format
->values
[0].string
.text
);
7646 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7649 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7650 "document-format", NULL
, mimetype
);
7654 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7655 _("Unsupported format \'%s/%s\'!"), super
, type
);
7656 cupsdLogMessage(CUPSD_LOG_INFO
,
7657 "Hint: Do you have the raw file printing rules enabled?");
7660 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7661 "document-format", NULL
, format
->values
[0].string
.text
);
7667 * Read any embedded job ticket info from PS files...
7670 if (!strcasecmp(filetype
->super
, "application") &&
7671 !strcasecmp(filetype
->type
, "postscript"))
7672 read_ps_job_ticket(con
);
7675 * Create the job object...
7678 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
7681 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Adding job file of type %s/%s.",
7682 job
->id
, filetype
->super
, filetype
->type
);
7685 * Update quota data...
7688 if (stat(con
->filename
, &fileinfo
))
7691 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7693 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7695 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7696 IPP_TAG_INTEGER
)) != NULL
)
7697 attr
->values
[0].integer
+= kbytes
;
7700 * Add the job file...
7703 if (add_file(con
, job
, filetype
, compression
))
7706 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
7708 rename(con
->filename
, filename
);
7709 cupsdClearString(&con
->filename
);
7712 * See if we need to add the ending sheet...
7715 cupsdTimeoutJob(job
);
7718 * Log and save the job...
7721 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
7722 job
->id
, job
->dest
, job
->username
);
7723 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] hold_until = %d", job
->id
,
7724 (int)job
->hold_until
);
7729 * Start the job if possible...
7737 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
7739 * This function only gets called when printing a single PostScript
7740 * file using the Print-Job operation. It doesn't work for Create-Job +
7741 * Send-File, since the job attributes need to be set at job creation
7742 * time for banners to work. The embedded PS job ticket stuff is here
7743 * only to allow the Windows printer driver for CUPS to pass in JCL
7744 * options and IPP attributes which otherwise would be lost.
7746 * The format of a PS job ticket is simple:
7748 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
7750 * %cupsJobTicket: attr1=value1
7751 * %cupsJobTicket: attr2=value2
7753 * %cupsJobTicket: attrN=valueN
7755 * Job ticket lines must appear immediately after the first line that
7756 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
7757 * looking for job ticket info when it finds a line that does not begin
7758 * with "%cupsJobTicket:".
7760 * The maximum length of a job ticket line, including the prefix, is
7761 * 255 characters to conform with the Adobe DSC.
7763 * Read-only attributes are rejected with a notice to the error log in
7764 * case a malicious user tries anything. Since the job ticket is read
7765 * prior to attribute validation in print_job(), job ticket attributes
7766 * will go through the same validation as IPP attributes...
7770 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
7772 cups_file_t
*fp
; /* File to read from */
7773 char line
[256]; /* Line data */
7774 int num_options
; /* Number of options */
7775 cups_option_t
*options
; /* Options */
7776 ipp_t
*ticket
; /* New attributes */
7777 ipp_attribute_t
*attr
, /* Current attribute */
7778 *attr2
, /* Job attribute */
7779 *prev2
; /* Previous job attribute */
7783 * First open the print file...
7786 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
7788 cupsdLogMessage(CUPSD_LOG_ERROR
,
7789 "read_ps_job_ticket: Unable to open PostScript print file "
7796 * Skip the first line...
7799 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
7801 cupsdLogMessage(CUPSD_LOG_ERROR
,
7802 "read_ps_job_ticket: Unable to read from PostScript print "
7809 if (strncmp(line
, "%!PS-Adobe-", 11))
7812 * Not a DSC-compliant file, so no job ticket info will be available...
7820 * Read job ticket info from the file...
7826 while (cupsFileGets(fp
, line
, sizeof(line
)))
7829 * Stop at the first non-ticket line...
7832 if (strncmp(line
, "%cupsJobTicket:", 15))
7836 * Add the options to the option array...
7839 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
7843 * Done with the file; see if we have any options...
7848 if (num_options
== 0)
7852 * OK, convert the options to an attribute list, and apply them to
7857 cupsEncodeOptions(ticket
, num_options
, options
);
7860 * See what the user wants to change.
7863 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
7865 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
7868 if (!strcmp(attr
->name
, "job-originating-host-name") ||
7869 !strcmp(attr
->name
, "job-originating-user-name") ||
7870 !strcmp(attr
->name
, "job-media-sheets-completed") ||
7871 !strcmp(attr
->name
, "job-k-octets") ||
7872 !strcmp(attr
->name
, "job-id") ||
7873 !strncmp(attr
->name
, "job-state", 9) ||
7874 !strncmp(attr
->name
, "time-at-", 8))
7875 continue; /* Read-only attrs */
7877 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
7878 IPP_TAG_ZERO
)) != NULL
)
7881 * Some other value; first free the old value...
7884 if (con
->request
->attrs
== attr2
)
7886 con
->request
->attrs
= attr2
->next
;
7891 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
7892 if (prev2
->next
== attr2
)
7894 prev2
->next
= attr2
->next
;
7899 if (con
->request
->last
== attr2
)
7900 con
->request
->last
= prev2
;
7902 _ippFreeAttr(attr2
);
7906 * Add new option by copying it...
7909 copy_attribute(con
->request
, attr
, 0);
7913 * Then free the attribute list and option array...
7917 cupsFreeOptions(num_options
, options
);
7922 * 'reject_jobs()' - Reject print jobs to a printer.
7926 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
7927 ipp_attribute_t
*uri
) /* I - Printer or class URI */
7929 http_status_t status
; /* Policy status */
7930 cups_ptype_t dtype
; /* Destination type (printer/class) */
7931 cupsd_printer_t
*printer
; /* Printer data */
7932 ipp_attribute_t
*attr
; /* printer-state-message text */
7935 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
7936 con
->http
.fd
, uri
->values
[0].string
.text
);
7939 * Is the destination valid?
7942 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7948 send_ipp_status(con
, IPP_NOT_FOUND
,
7949 _("The printer or class was not found."));
7957 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7959 send_http_error(con
, status
, printer
);
7964 * Reject jobs sent to the printer...
7967 printer
->accepting
= 0;
7969 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
7970 IPP_TAG_TEXT
)) == NULL
)
7971 strcpy(printer
->state_message
, "Rejecting Jobs");
7973 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
7974 sizeof(printer
->state_message
));
7976 cupsdAddPrinterHistory(printer
);
7978 if (dtype
& CUPS_PRINTER_CLASS
)
7980 cupsdSaveAllClasses();
7982 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
7983 printer
->name
, get_username(con
));
7987 cupsdSaveAllPrinters();
7989 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
7990 printer
->name
, get_username(con
));
7994 * Everything was ok, so return OK status...
7997 con
->response
->request
.status
.status_code
= IPP_OK
;
8002 * 'release_job()' - Release a held print job.
8006 release_job(cupsd_client_t
*con
, /* I - Client connection */
8007 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8009 ipp_attribute_t
*attr
; /* Current attribute */
8010 int jobid
; /* Job ID */
8011 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8012 username
[HTTP_MAX_URI
], /* Username portion of URI */
8013 host
[HTTP_MAX_URI
], /* Host portion of URI */
8014 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8015 int port
; /* Port portion of URI */
8016 cupsd_job_t
*job
; /* Job information */
8019 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8020 con
->http
.fd
, uri
->values
[0].string
.text
);
8023 * See if we have a job URI or a printer URI...
8026 if (!strcmp(uri
->name
, "printer-uri"))
8029 * Got a printer URI; see if we also have a job-id attribute...
8032 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8033 IPP_TAG_INTEGER
)) == NULL
)
8035 send_ipp_status(con
, IPP_BAD_REQUEST
,
8036 _("Got a printer-uri attribute but no job-id!"));
8040 jobid
= attr
->values
[0].integer
;
8045 * Got a job URI; parse it to get the job ID...
8048 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8049 sizeof(method
), username
, sizeof(username
), host
,
8050 sizeof(host
), &port
, resource
, sizeof(resource
));
8052 if (strncmp(resource
, "/jobs/", 6))
8058 send_ipp_status(con
, IPP_BAD_REQUEST
,
8059 _("Bad job-uri attribute \"%s\"!"),
8060 uri
->values
[0].string
.text
);
8064 jobid
= atoi(resource
+ 6);
8068 * See if the job exists...
8071 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8074 * Nope - return a "not found" error...
8077 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8082 * See if job is "held"...
8085 if (job
->state_value
!= IPP_JOB_HELD
)
8088 * Nope - return a "not possible" error...
8091 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
8096 * See if the job is owned by the requesting user...
8099 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8101 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8106 * Reset the job-hold-until value to "no-hold"...
8109 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8110 IPP_TAG_KEYWORD
)) == NULL
)
8111 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8115 _cupsStrFree(attr
->values
[0].string
.text
);
8117 attr
->value_tag
= IPP_TAG_KEYWORD
;
8118 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
8120 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
8121 "Job job-hold-until value changed by user.");
8125 * Release the job and return...
8128 cupsdReleaseJob(job
);
8130 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
8131 "Job released by user.");
8133 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Released by \"%s\".", jobid
,
8136 con
->response
->request
.status
.status_code
= IPP_OK
;
8141 * 'renew_subscription()' - Renew an existing subscription...
8146 cupsd_client_t
*con
, /* I - Client connection */
8147 int sub_id
) /* I - Subscription ID */
8149 http_status_t status
; /* Policy status */
8150 cupsd_subscription_t
*sub
; /* Subscription */
8151 ipp_attribute_t
*lease
; /* notify-lease-duration */
8154 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8155 "renew_subscription(con=%p[%d], sub_id=%d)",
8156 con
, con
->http
.fd
, sub_id
);
8159 * Is the subscription ID valid?
8162 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8165 * Bad subscription ID...
8168 send_ipp_status(con
, IPP_NOT_FOUND
,
8169 _("notify-subscription-id %d no good!"), sub_id
);
8176 * Job subscriptions cannot be renewed...
8179 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8180 _("Job subscriptions cannot be renewed!"));
8188 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8190 con
, sub
->owner
)) != HTTP_OK
)
8192 send_http_error(con
, status
, sub
->dest
);
8197 * Renew the subscription...
8200 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8203 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8205 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8207 cupsdLogMessage(CUPSD_LOG_INFO
,
8208 "renew_subscription: Limiting notify-lease-duration to "
8211 sub
->lease
= MaxLeaseDuration
;
8214 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
8216 cupsdSaveAllSubscriptions();
8218 con
->response
->request
.status
.status_code
= IPP_OK
;
8220 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
8221 "notify-lease-duration", sub
->lease
);
8226 * 'restart_job()' - Restart an old print job.
8230 restart_job(cupsd_client_t
*con
, /* I - Client connection */
8231 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8233 ipp_attribute_t
*attr
; /* Current attribute */
8234 int jobid
; /* Job ID */
8235 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8236 username
[HTTP_MAX_URI
], /* Username portion of URI */
8237 host
[HTTP_MAX_URI
], /* Host portion of URI */
8238 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8239 int port
; /* Port portion of URI */
8240 cupsd_job_t
*job
; /* Job information */
8243 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
8244 con
->http
.fd
, uri
->values
[0].string
.text
);
8247 * See if we have a job URI or a printer URI...
8250 if (!strcmp(uri
->name
, "printer-uri"))
8253 * Got a printer URI; see if we also have a job-id attribute...
8256 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8257 IPP_TAG_INTEGER
)) == NULL
)
8259 send_ipp_status(con
, IPP_BAD_REQUEST
,
8260 _("Got a printer-uri attribute but no job-id!"));
8264 jobid
= attr
->values
[0].integer
;
8269 * Got a job URI; parse it to get the job ID...
8272 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8273 sizeof(method
), username
, sizeof(username
), host
,
8274 sizeof(host
), &port
, resource
, sizeof(resource
));
8276 if (strncmp(resource
, "/jobs/", 6))
8282 send_ipp_status(con
, IPP_BAD_REQUEST
,
8283 _("Bad job-uri attribute \"%s\"!"),
8284 uri
->values
[0].string
.text
);
8288 jobid
= atoi(resource
+ 6);
8292 * See if the job exists...
8295 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8298 * Nope - return a "not found" error...
8301 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8306 * See if job is in any of the "completed" states...
8309 if (job
->state_value
<= IPP_JOB_PROCESSING
)
8312 * Nope - return a "not possible" error...
8315 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
8321 * See if we have retained the job files...
8326 if (!job
->attrs
|| job
->num_files
== 0)
8329 * Nope - return a "not possible" error...
8332 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8333 _("Job #%d cannot be restarted - no files!"), jobid
);
8338 * See if the job is owned by the requesting user...
8341 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8343 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8348 * Restart the job and return...
8351 cupsdRestartJob(job
);
8353 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Restarted by \"%s\".", jobid
,
8356 con
->response
->request
.status
.status_code
= IPP_OK
;
8361 * 'save_auth_info()' - Save authentication information for a job.
8366 cupsd_client_t
*con
, /* I - Client connection */
8367 cupsd_job_t
*job
, /* I - Job */
8368 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
8370 int i
; /* Looping var */
8371 char filename
[1024]; /* Job authentication filename */
8372 cups_file_t
*fp
; /* Job authentication file */
8373 char line
[2048]; /* Line for file */
8374 cupsd_printer_t
*dest
; /* Destination printer/class */
8378 * This function saves the in-memory authentication information for
8379 * a job so that it can be used to authenticate with a remote host.
8380 * The information is stored in a file that is readable only by the
8381 * root user. The fields are Base-64 encoded, each on a separate line,
8382 * followed by random number (up to 1024) of newlines to limit the
8383 * amount of information that is exposed.
8385 * Because of the potential for exposing of authentication information,
8386 * this functionality is only enabled when running cupsd as root.
8388 * This caching only works for the Basic and BasicDigest authentication
8389 * types. Digest authentication cannot be cached this way, and in
8390 * the future Kerberos authentication may make all of this obsolete.
8392 * Authentication information is saved whenever an authenticated
8393 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
8396 * This information is deleted after a job is completed or canceled,
8397 * so reprints may require subsequent re-authentication.
8403 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
8407 * Create the authentication file and change permissions...
8410 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
8411 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
8413 cupsdLogMessage(CUPSD_LOG_ERROR
,
8414 "Unable to save authentication info to \"%s\" - %s",
8415 filename
, strerror(errno
));
8419 fchown(cupsFileNumber(fp
), 0, 0);
8420 fchmod(cupsFileNumber(fp
), 0400);
8422 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
8425 * Write 1 to 3 auth values...
8428 cupsdClearString(&job
->auth_username
);
8429 cupsdClearString(&job
->auth_domain
);
8430 cupsdClearString(&job
->auth_password
);
8432 for (i
= 0; i
< auth_info
->num_values
; i
++)
8434 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
8435 strlen(auth_info
->values
[i
].string
.text
));
8436 cupsFilePrintf(fp
, "%s\n", line
);
8438 if (!strcmp(dest
->auth_info_required
[i
], "username"))
8439 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s",
8440 auth_info
->values
[i
].string
.text
);
8441 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
8442 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s",
8443 auth_info
->values
[i
].string
.text
);
8444 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
8445 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s",
8446 auth_info
->values
[i
].string
.text
);
8449 else if (con
->username
[0])
8452 * Write the authenticated username...
8455 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
8456 cupsFilePrintf(fp
, "%s\n", line
);
8458 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", con
->username
);
8459 cupsdClearString(&job
->auth_domain
);
8462 * Write the authenticated password...
8465 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
8466 cupsFilePrintf(fp
, "%s\n", line
);
8468 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", con
->password
);
8472 * Write a random number of newlines to the end of the file...
8475 for (i
= (rand() % 1024); i
>= 0; i
--)
8476 cupsFilePutChar(fp
, '\n');
8479 * Close the file and return...
8484 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8485 if (con
->gss_have_creds
)
8486 save_krb5_creds(con
, job
);
8487 else if (job
->ccname
)
8488 cupsdClearString(&(job
->ccname
));
8489 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8493 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8495 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
8499 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
8500 cupsd_job_t
*job
) /* I - Job */
8502 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
8503 cupsdLogMessage(CUPSD_LOG_INFO
,
8504 "Sorry, your version of Kerberos does not support delegated "
8509 krb5_error_code error
; /* Kerberos error code */
8510 OM_uint32 major_status
, /* Major status code */
8511 minor_status
; /* Minor status code */
8512 krb5_principal principal
; /* Kerberos principal */
8517 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
8521 if (krb5_init_context
== NULL
)
8523 # endif /* __APPLE__ */
8526 * We MUST create a file-based cache because memory-based caches are
8527 * only valid for the current process/address space.
8529 * Due to various bugs/features in different versions of Kerberos, we
8530 * need either the krb5_cc_new_unique() function or Heimdal's version
8531 * of krb5_cc_gen_new() to create a new FILE: credential cache that
8532 * can be passed to the backend. These functions create a temporary
8533 * file (typically in /tmp) containing the cached credentials, which
8534 * are removed when we have successfully printed a job.
8537 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
8538 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
8539 &(job
->ccache
))) != 0)
8540 # else /* HAVE_HEIMDAL */
8541 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
8542 &(job
->ccache
))) != 0)
8543 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
8545 cupsdLogMessage(CUPSD_LOG_ERROR
,
8546 "Unable to create new credentials cache (%d/%s)",
8547 error
, strerror(errno
));
8552 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
8554 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to parse kerberos username (%d/%s)",
8555 error
, strerror(errno
));
8556 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8561 if ((error
= krb5_cc_initialize(KerberosContext
, job
->ccache
, principal
)))
8563 cupsdLogMessage(CUPSD_LOG_ERROR
,
8564 "Unable to initialize credentials cache (%d/%s)", error
,
8566 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8567 krb5_free_principal(KerberosContext
, principal
);
8572 krb5_free_principal(KerberosContext
, principal
);
8575 * Copy the user's credentials to the new cache file...
8578 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
8581 if (GSS_ERROR(major_status
))
8583 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
8584 "Unable to import client credentials cache");
8585 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8591 * Add the KRB5CCNAME environment variable to the job so that the
8592 * backend can use the credentials when printing.
8595 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
8596 krb5_cc_get_name(KerberosContext
, job
->ccache
));
8598 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] save_krb5_creds: %s", job
->id
,
8600 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
8602 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8606 * 'send_document()' - Send a file to a printer or class.
8610 send_document(cupsd_client_t
*con
, /* I - Client connection */
8611 ipp_attribute_t
*uri
) /* I - Printer URI */
8613 ipp_attribute_t
*attr
; /* Current attribute */
8614 ipp_attribute_t
*format
; /* Document-format attribute */
8615 const char *default_format
;/* document-format-default value */
8616 int jobid
; /* Job ID number */
8617 cupsd_job_t
*job
; /* Current job */
8618 char job_uri
[HTTP_MAX_URI
],
8620 method
[HTTP_MAX_URI
],
8621 /* Method portion of URI */
8622 username
[HTTP_MAX_URI
],
8623 /* Username portion of URI */
8625 /* Host portion of URI */
8626 resource
[HTTP_MAX_URI
];
8627 /* Resource portion of URI */
8628 int port
; /* Port portion of URI */
8629 mime_type_t
*filetype
; /* Type of file */
8630 char super
[MIME_MAX_SUPER
],
8631 /* Supertype of file */
8632 type
[MIME_MAX_TYPE
],
8633 /* Subtype of file */
8634 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8635 /* Textual name of mime type */
8636 char filename
[1024]; /* Job filename */
8637 cupsd_printer_t
*printer
; /* Current printer */
8638 struct stat fileinfo
; /* File information */
8639 int kbytes
; /* Size of file */
8640 int compression
; /* Type of compression */
8643 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
8644 con
->http
.fd
, uri
->values
[0].string
.text
);
8647 * See if we have a job URI or a printer URI...
8650 if (!strcmp(uri
->name
, "printer-uri"))
8653 * Got a printer URI; see if we also have a job-id attribute...
8656 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8657 IPP_TAG_INTEGER
)) == NULL
)
8659 send_ipp_status(con
, IPP_BAD_REQUEST
,
8660 _("Got a printer-uri attribute but no job-id!"));
8664 jobid
= attr
->values
[0].integer
;
8669 * Got a job URI; parse it to get the job ID...
8672 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8673 sizeof(method
), username
, sizeof(username
), host
,
8674 sizeof(host
), &port
, resource
, sizeof(resource
));
8676 if (strncmp(resource
, "/jobs/", 6))
8682 send_ipp_status(con
, IPP_BAD_REQUEST
,
8683 _("Bad job-uri attribute \"%s\"!"),
8684 uri
->values
[0].string
.text
);
8688 jobid
= atoi(resource
+ 6);
8692 * See if the job exists...
8695 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8698 * Nope - return a "not found" error...
8701 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8705 printer
= cupsdFindDest(job
->dest
);
8708 * See if the job is owned by the requesting user...
8711 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8713 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8718 * OK, see if the client is sending the document compressed - CUPS
8719 * only supports "none" and "gzip".
8722 compression
= CUPS_FILE_NONE
;
8724 if ((attr
= ippFindAttribute(con
->request
, "compression",
8725 IPP_TAG_KEYWORD
)) != NULL
)
8727 if (strcmp(attr
->values
[0].string
.text
, "none")
8729 && strcmp(attr
->values
[0].string
.text
, "gzip")
8730 #endif /* HAVE_LIBZ */
8733 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
8734 attr
->values
[0].string
.text
);
8735 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8736 "compression", NULL
, attr
->values
[0].string
.text
);
8741 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8742 compression
= CUPS_FILE_GZIP
;
8743 #endif /* HAVE_LIBZ */
8747 * Do we have a file to print?
8752 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
8757 * Is it a format we support?
8760 if ((format
= ippFindAttribute(con
->request
, "document-format",
8761 IPP_TAG_MIMETYPE
)) != NULL
)
8764 * Grab format from client...
8767 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
8770 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
8771 format
->values
[0].string
.text
);
8775 else if ((default_format
= cupsGetOption("document-format",
8776 printer
->num_options
,
8777 printer
->options
)) != NULL
)
8780 * Use default document format...
8783 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
8785 send_ipp_status(con
, IPP_BAD_REQUEST
,
8786 _("Could not scan type \"%s\"!"),
8794 * No document format attribute? Auto-type it!
8797 strcpy(super
, "application");
8798 strcpy(type
, "octet-stream");
8801 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8804 * Auto-type the file...
8807 ipp_attribute_t
*doc_name
; /* document-name attribute */
8810 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
8812 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8813 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8814 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8818 filetype
= mimeType(MimeDatabase
, super
, type
);
8821 filetype
= mimeType(MimeDatabase
, super
, type
);
8825 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8828 * Replace the document-format attribute value with the auto-typed or
8832 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8837 _cupsStrFree(format
->values
[0].string
.text
);
8839 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8842 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8843 "document-format", NULL
, mimetype
);
8847 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8848 _("Unsupported format \'%s/%s\'!"), super
, type
);
8849 cupsdLogMessage(CUPSD_LOG_INFO
,
8850 "Hint: Do you have the raw file printing rules enabled?");
8853 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8854 "document-format", NULL
, format
->values
[0].string
.text
);
8859 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
8861 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8864 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8865 _("Unsupported format \'%s\'!"), mimetype
);
8867 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8868 "document-format", NULL
, mimetype
);
8873 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8874 "send_document: request file type is %s/%s.",
8875 filetype
->super
, filetype
->type
);
8878 * Add the file to the job...
8883 if (add_file(con
, job
, filetype
, compression
))
8886 if (stat(con
->filename
, &fileinfo
))
8889 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8891 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8893 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8894 IPP_TAG_INTEGER
)) != NULL
)
8895 attr
->values
[0].integer
+= kbytes
;
8897 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8899 rename(con
->filename
, filename
);
8901 cupsdClearString(&con
->filename
);
8903 cupsdLogMessage(CUPSD_LOG_INFO
,
8904 "File of type %s/%s queued in job #%d by \"%s\".",
8905 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
8908 * Start the job if this is the last document...
8911 if ((attr
= ippFindAttribute(con
->request
, "last-document",
8912 IPP_TAG_BOOLEAN
)) != NULL
&&
8913 attr
->values
[0].boolean
)
8916 * See if we need to add the ending sheet...
8919 cupsdTimeoutJob(job
);
8921 if (job
->state_value
== IPP_JOB_STOPPED
)
8923 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
8924 job
->state_value
= IPP_JOB_PENDING
;
8926 else if (job
->state_value
== IPP_JOB_HELD
)
8928 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8929 IPP_TAG_KEYWORD
)) == NULL
)
8930 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8932 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
8934 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
8935 job
->state_value
= IPP_JOB_PENDING
;
8942 * Start the job if possible... Since cupsdCheckJobs() can cancel a
8943 * job if it doesn't print, we need to re-find the job afterwards...
8950 job
= cupsdFindJob(jobid
);
8954 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8955 IPP_TAG_KEYWORD
)) == NULL
)
8956 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8958 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
8960 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
8961 job
->state_value
= IPP_JOB_HELD
;
8962 job
->hold_until
= time(NULL
) + 60;
8968 * Fill in the response info...
8971 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
8974 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
8977 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
8979 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
8980 job
? job
->state_value
: IPP_JOB_CANCELED
);
8981 add_job_state_reasons(con
, job
);
8983 con
->response
->request
.status
.status_code
= IPP_OK
;
8988 * 'send_http_error()' - Send a HTTP error back to the IPP client.
8993 cupsd_client_t
*con
, /* I - Client connection */
8994 http_status_t status
, /* I - HTTP status code */
8995 cupsd_printer_t
*printer
) /* I - Printer, if any */
8997 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
8998 ippOpString(con
->request
->request
.op
.operation_id
),
8999 httpStatus(status
));
9001 if (status
== HTTP_UNAUTHORIZED
&&
9002 printer
&& printer
->num_auth_info_required
> 0 &&
9003 !strcmp(printer
->auth_info_required
[0], "negotiate"))
9004 cupsdSendError(con
, status
, AUTH_NEGOTIATE
);
9007 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9008 cupsd_location_t
*auth
; /* Pointer to authentication element */
9011 if (printer
->type
& CUPS_PRINTER_CLASS
)
9012 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9014 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9016 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9017 auth
->type
== AUTH_NONE
)
9018 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
, IPP_PRINT_JOB
);
9020 cupsdSendError(con
, status
, auth
? auth
->type
: AUTH_NONE
);
9023 cupsdSendError(con
, status
, AUTH_NONE
);
9025 ippDelete(con
->response
);
9026 con
->response
= NULL
;
9033 * 'send_ipp_status()' - Send a status back to the IPP client.
9037 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9038 ipp_status_t status
, /* I - IPP status code */
9039 const char *message
, /* I - Status message */
9040 ...) /* I - Additional args as needed */
9042 va_list ap
; /* Pointer to additional args */
9043 char formatted
[1024]; /* Formatted errror message */
9046 va_start(ap
, message
);
9047 vsnprintf(formatted
, sizeof(formatted
),
9048 _cupsLangString(con
->language
, message
), ap
);
9051 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9052 ippOpString(con
->request
->request
.op
.operation_id
),
9053 ippErrorString(status
), formatted
);
9055 con
->response
->request
.status
.status_code
= status
;
9057 if (ippFindAttribute(con
->response
, "attributes-charset",
9058 IPP_TAG_ZERO
) == NULL
)
9059 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9060 "attributes-charset", NULL
, DefaultCharset
);
9062 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9063 IPP_TAG_ZERO
) == NULL
)
9064 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9065 "attributes-natural-language", NULL
, DefaultLanguage
);
9067 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9068 "status-message", NULL
, formatted
);
9073 * 'set_default()' - Set the default destination...
9077 set_default(cupsd_client_t
*con
, /* I - Client connection */
9078 ipp_attribute_t
*uri
) /* I - Printer URI */
9080 http_status_t status
; /* Policy status */
9081 cups_ptype_t dtype
; /* Destination type (printer/class) */
9082 cupsd_printer_t
*printer
; /* Printer */
9085 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9086 con
->http
.fd
, uri
->values
[0].string
.text
);
9089 * Is the destination valid?
9092 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9098 send_ipp_status(con
, IPP_NOT_FOUND
,
9099 _("The printer or class was not found."));
9107 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9109 send_http_error(con
, status
, NULL
);
9114 * Set it as the default...
9117 DefaultPrinter
= printer
;
9119 cupsdSaveAllPrinters();
9120 cupsdSaveAllClasses();
9122 cupsdWritePrintcap();
9124 cupsdLogMessage(CUPSD_LOG_INFO
,
9125 "Default destination set to \"%s\" by \"%s\".",
9126 printer
->name
, get_username(con
));
9129 * Everything was ok, so return OK status...
9132 con
->response
->request
.status
.status_code
= IPP_OK
;
9137 * 'set_job_attrs()' - Set job attributes.
9141 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9142 ipp_attribute_t
*uri
) /* I - Job URI */
9144 ipp_attribute_t
*attr
, /* Current attribute */
9145 *attr2
; /* Job attribute */
9146 int jobid
; /* Job ID */
9147 cupsd_job_t
*job
; /* Current job */
9148 char method
[HTTP_MAX_URI
],
9149 /* Method portion of URI */
9150 username
[HTTP_MAX_URI
],
9151 /* Username portion of URI */
9153 /* Host portion of URI */
9154 resource
[HTTP_MAX_URI
];
9155 /* Resource portion of URI */
9156 int port
; /* Port portion of URI */
9157 int event
; /* Events? */
9160 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
9161 con
->http
.fd
, uri
->values
[0].string
.text
);
9164 * Start with "everything is OK" status...
9167 con
->response
->request
.status
.status_code
= IPP_OK
;
9170 * See if we have a job URI or a printer URI...
9173 if (!strcmp(uri
->name
, "printer-uri"))
9176 * Got a printer URI; see if we also have a job-id attribute...
9179 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9180 IPP_TAG_INTEGER
)) == NULL
)
9182 send_ipp_status(con
, IPP_BAD_REQUEST
,
9183 _("Got a printer-uri attribute but no job-id!"));
9187 jobid
= attr
->values
[0].integer
;
9192 * Got a job URI; parse it to get the job ID...
9195 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9196 sizeof(method
), username
, sizeof(username
), host
,
9197 sizeof(host
), &port
, resource
, sizeof(resource
));
9199 if (strncmp(resource
, "/jobs/", 6))
9205 send_ipp_status(con
, IPP_BAD_REQUEST
,
9206 _("Bad job-uri attribute \"%s\"!"),
9207 uri
->values
[0].string
.text
);
9211 jobid
= atoi(resource
+ 6);
9215 * See if the job exists...
9218 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9221 * Nope - return a "not found" error...
9224 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9229 * See if the job has been completed...
9232 if (job
->state_value
> IPP_JOB_STOPPED
)
9235 * Return a "not-possible" error...
9238 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9239 _("Job #%d is finished and cannot be altered!"), jobid
);
9244 * See if the job is owned by the requesting user...
9247 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9249 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9254 * See what the user wants to change.
9261 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9263 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
9266 if (!strcmp(attr
->name
, "attributes-charset") ||
9267 !strcmp(attr
->name
, "attributes-natural-language") ||
9268 !strcmp(attr
->name
, "document-compression") ||
9269 !strcmp(attr
->name
, "document-format") ||
9270 !strcmp(attr
->name
, "job-detailed-status-messages") ||
9271 !strcmp(attr
->name
, "job-document-access-errors") ||
9272 !strcmp(attr
->name
, "job-id") ||
9273 !strcmp(attr
->name
, "job-k-octets") ||
9274 !strcmp(attr
->name
, "job-originating-host-name") ||
9275 !strcmp(attr
->name
, "job-originating-user-name") ||
9276 !strcmp(attr
->name
, "job-printer-up-time") ||
9277 !strcmp(attr
->name
, "job-printer-uri") ||
9278 !strcmp(attr
->name
, "job-sheets") ||
9279 !strcmp(attr
->name
, "job-state-message") ||
9280 !strcmp(attr
->name
, "job-state-reasons") ||
9281 !strcmp(attr
->name
, "job-uri") ||
9282 !strcmp(attr
->name
, "number-of-documents") ||
9283 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
9284 !strcmp(attr
->name
, "output-device-assigned") ||
9285 !strncmp(attr
->name
, "date-time-at-", 13) ||
9286 !strncmp(attr
->name
, "job-impressions", 15) ||
9287 !strncmp(attr
->name
, "job-k-octets", 12) ||
9288 !strncmp(attr
->name
, "job-media-sheets", 16) ||
9289 !strncmp(attr
->name
, "time-at-", 8))
9295 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
9296 _("%s cannot be changed."), attr
->name
);
9298 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9299 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9304 if (!strcmp(attr
->name
, "job-priority"))
9307 * Change the job priority...
9310 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9312 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
9314 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9315 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9317 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
9319 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9320 _("Job is completed and cannot be changed."));
9323 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9325 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
9326 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9329 else if (!strcmp(attr
->name
, "job-state"))
9332 * Change the job state...
9335 if (attr
->value_tag
!= IPP_TAG_ENUM
)
9337 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
9339 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9340 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9344 switch (attr
->values
[0].integer
)
9346 case IPP_JOB_PENDING
:
9348 if (job
->state_value
> IPP_JOB_HELD
)
9350 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9351 _("Job state cannot be changed."));
9354 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9356 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
9357 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
9359 event
|= CUPSD_EVENT_JOB_STATE
;
9363 case IPP_JOB_PROCESSING
:
9364 case IPP_JOB_STOPPED
:
9365 if (job
->state_value
!= attr
->values
[0].integer
)
9367 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9368 _("Job state cannot be changed."));
9373 case IPP_JOB_CANCELED
:
9374 case IPP_JOB_ABORTED
:
9375 case IPP_JOB_COMPLETED
:
9376 if (job
->state_value
> IPP_JOB_PROCESSING
)
9378 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9379 _("Job state cannot be changed."));
9382 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9383 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
9388 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
9390 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9391 IPP_TAG_ZERO
)) != NULL
)
9394 * Some other value; first free the old value...
9397 if (job
->attrs
->prev
)
9398 job
->attrs
->prev
->next
= attr2
->next
;
9400 job
->attrs
->attrs
= attr2
->next
;
9402 if (job
->attrs
->last
== attr2
)
9403 job
->attrs
->last
= job
->attrs
->prev
;
9405 _ippFreeAttr(attr2
);
9408 * Then copy the attribute...
9411 copy_attribute(job
->attrs
, attr
, 0);
9414 * See if the job-name or job-hold-until is being changed.
9417 if (!strcmp(attr
->name
, "job-hold-until"))
9419 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
9421 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
9422 cupsdReleaseJob(job
);
9426 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
9429 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
9432 * Delete the attribute...
9435 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9436 IPP_TAG_ZERO
)) != NULL
)
9438 if (job
->attrs
->prev
)
9439 job
->attrs
->prev
->next
= attr2
->next
;
9441 job
->attrs
->attrs
= attr2
->next
;
9443 if (attr2
== job
->attrs
->last
)
9444 job
->attrs
->last
= job
->attrs
->prev
;
9446 _ippFreeAttr(attr2
);
9448 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9454 * Add new option by copying it...
9457 copy_attribute(job
->attrs
, attr
, 0);
9459 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9470 * Send events as needed...
9473 if (event
& CUPSD_EVENT_JOB_STATE
)
9474 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
9475 job
->state_value
== IPP_JOB_HELD
?
9476 "Job held by user." : "Job restarted by user.");
9478 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
9479 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
9480 "Job options changed by user.");
9483 * Start jobs if possible...
9491 * 'set_printer_defaults()' - Set printer default options from a request.
9495 set_printer_defaults(
9496 cupsd_client_t
*con
, /* I - Client connection */
9497 cupsd_printer_t
*printer
) /* I - Printer */
9499 int i
; /* Looping var */
9500 ipp_attribute_t
*attr
; /* Current attribute */
9501 int namelen
; /* Length of attribute name */
9502 char name
[256], /* New attribute name */
9503 value
[256]; /* String version of integer attrs */
9506 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9509 * Skip non-printer attributes...
9512 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
9515 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
9517 if (!strcmp(attr
->name
, "job-sheets-default"))
9520 * Only allow keywords and names...
9523 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9527 * Only allow job-sheets-default to be set when running without a
9528 * system high classification level...
9534 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
9536 if (attr
->num_values
> 1)
9537 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
9539 cupsdSetString(&printer
->job_sheets
[1], "none");
9541 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
9543 cupsdFreePrinterUsers(printer
);
9545 printer
->deny_users
= 0;
9547 if (attr
->value_tag
== IPP_TAG_NAME
&&
9548 (attr
->num_values
> 1 ||
9549 strcmp(attr
->values
[0].string
.text
, "all")))
9551 for (i
= 0; i
< attr
->num_values
; i
++)
9552 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9555 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
9557 cupsdFreePrinterUsers(printer
);
9559 printer
->deny_users
= 1;
9561 if (attr
->value_tag
== IPP_TAG_NAME
&&
9562 (attr
->num_values
> 1 ||
9563 strcmp(attr
->values
[0].string
.text
, "none")))
9565 for (i
= 0; i
< attr
->num_values
; i
++)
9566 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9569 else if (!strcmp(attr
->name
, "job-quota-period"))
9571 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9574 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
9575 attr
->values
[0].integer
);
9576 cupsdFreeQuotas(printer
);
9578 printer
->quota_period
= attr
->values
[0].integer
;
9580 else if (!strcmp(attr
->name
, "job-k-limit"))
9582 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9585 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
9586 attr
->values
[0].integer
);
9587 cupsdFreeQuotas(printer
);
9589 printer
->k_limit
= attr
->values
[0].integer
;
9591 else if (!strcmp(attr
->name
, "job-page-limit"))
9593 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9596 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
9597 attr
->values
[0].integer
);
9598 cupsdFreeQuotas(printer
);
9600 printer
->page_limit
= attr
->values
[0].integer
;
9602 else if (!strcmp(attr
->name
, "printer-op-policy"))
9604 cupsd_policy_t
*p
; /* Policy */
9607 if (attr
->value_tag
!= IPP_TAG_NAME
)
9610 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
9612 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9613 "Setting printer-op-policy to \"%s\"...",
9614 attr
->values
[0].string
.text
);
9615 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
9616 printer
->op_policy_ptr
= p
;
9620 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9621 _("Unknown printer-op-policy \"%s\"."),
9622 attr
->values
[0].string
.text
);
9626 else if (!strcmp(attr
->name
, "printer-error-policy"))
9628 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9631 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
9632 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
9633 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
9635 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9636 _("Unknown printer-error-policy \"%s\"."),
9637 attr
->values
[0].string
.text
);
9641 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9642 "Setting printer-error-policy to \"%s\"...",
9643 attr
->values
[0].string
.text
);
9644 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
9648 * Skip any other non-default attributes...
9651 namelen
= strlen(attr
->name
);
9652 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
9653 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
9657 * OK, anything else must be a user-defined default...
9660 strlcpy(name
, attr
->name
, sizeof(name
));
9661 name
[namelen
- 8] = '\0'; /* Strip "-default" */
9663 switch (attr
->value_tag
)
9665 case IPP_TAG_DELETEATTR
:
9666 printer
->num_options
= cupsRemoveOption(name
,
9667 printer
->num_options
,
9668 &(printer
->options
));
9669 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9670 "Deleting %s", attr
->name
);
9674 case IPP_TAG_KEYWORD
:
9676 printer
->num_options
= cupsAddOption(name
,
9677 attr
->values
[0].string
.text
,
9678 printer
->num_options
,
9679 &(printer
->options
));
9680 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9681 "Setting %s to \"%s\"...", attr
->name
,
9682 attr
->values
[0].string
.text
);
9685 case IPP_TAG_BOOLEAN
:
9686 printer
->num_options
= cupsAddOption(name
,
9687 attr
->values
[0].boolean
?
9689 printer
->num_options
,
9690 &(printer
->options
));
9691 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9692 "Setting %s to %s...", attr
->name
,
9693 attr
->values
[0].boolean
? "true" : "false");
9696 case IPP_TAG_INTEGER
:
9698 sprintf(value
, "%d", attr
->values
[0].integer
);
9699 printer
->num_options
= cupsAddOption(name
, value
,
9700 printer
->num_options
,
9701 &(printer
->options
));
9702 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9703 "Setting %s to %s...", attr
->name
, value
);
9706 case IPP_TAG_RANGE
:
9707 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
9708 attr
->values
[0].range
.upper
);
9709 printer
->num_options
= cupsAddOption(name
, value
,
9710 printer
->num_options
,
9711 &(printer
->options
));
9712 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9713 "Setting %s to %s...", attr
->name
, value
);
9716 case IPP_TAG_RESOLUTION
:
9717 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
9718 attr
->values
[0].resolution
.yres
,
9719 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
9721 printer
->num_options
= cupsAddOption(name
, value
,
9722 printer
->num_options
,
9723 &(printer
->options
));
9724 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9725 "Setting %s to %s...", attr
->name
, value
);
9729 /* Do nothing for other values */
9737 * 'start_printer()' - Start a printer.
9741 start_printer(cupsd_client_t
*con
, /* I - Client connection */
9742 ipp_attribute_t
*uri
) /* I - Printer URI */
9744 http_status_t status
; /* Policy status */
9745 cups_ptype_t dtype
; /* Destination type (printer/class) */
9746 cupsd_printer_t
*printer
; /* Printer data */
9749 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
9750 con
->http
.fd
, uri
->values
[0].string
.text
);
9753 * Is the destination valid?
9756 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9762 send_ipp_status(con
, IPP_NOT_FOUND
,
9763 _("The printer or class was not found."));
9771 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9773 send_http_error(con
, status
, printer
);
9778 * Start the printer...
9781 printer
->state_message
[0] = '\0';
9783 cupsdStartPrinter(printer
, 1);
9785 if (dtype
& CUPS_PRINTER_CLASS
)
9786 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
9787 printer
->name
, get_username(con
));
9789 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
9790 printer
->name
, get_username(con
));
9795 * Everything was ok, so return OK status...
9798 con
->response
->request
.status
.status_code
= IPP_OK
;
9803 * 'stop_printer()' - Stop a printer.
9807 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
9808 ipp_attribute_t
*uri
) /* I - Printer URI */
9810 http_status_t status
; /* Policy status */
9811 cups_ptype_t dtype
; /* Destination type (printer/class) */
9812 cupsd_printer_t
*printer
; /* Printer data */
9813 ipp_attribute_t
*attr
; /* printer-state-message attribute */
9816 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
9817 con
->http
.fd
, uri
->values
[0].string
.text
);
9820 * Is the destination valid?
9823 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9829 send_ipp_status(con
, IPP_NOT_FOUND
,
9830 _("The printer or class was not found."));
9838 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9840 send_http_error(con
, status
, printer
);
9845 * Stop the printer...
9848 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
9849 IPP_TAG_TEXT
)) == NULL
)
9850 strcpy(printer
->state_message
, "Paused");
9853 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
9854 sizeof(printer
->state_message
));
9857 cupsdStopPrinter(printer
, 1);
9859 if (dtype
& CUPS_PRINTER_CLASS
)
9860 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
9861 printer
->name
, get_username(con
));
9863 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
9864 printer
->name
, get_username(con
));
9867 * Everything was ok, so return OK status...
9870 con
->response
->request
.status
.status_code
= IPP_OK
;
9875 * 'url_encode_attr()' - URL-encode a string attribute.
9879 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
9880 char *buffer
,/* I - String buffer */
9881 int bufsize
)/* I - Size of buffer */
9883 int i
; /* Looping var */
9884 char *bufptr
, /* Pointer into buffer */
9885 *bufend
; /* End of buffer */
9888 strlcpy(buffer
, attr
->name
, bufsize
);
9889 bufptr
= buffer
+ strlen(buffer
);
9890 bufend
= buffer
+ bufsize
- 1;
9892 for (i
= 0; i
< attr
->num_values
; i
++)
9894 if (bufptr
>= bufend
)
9902 if (bufptr
>= bufend
)
9907 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
9908 bufptr
, bufend
- bufptr
+ 1);
9910 if (bufptr
>= bufend
)
9921 * 'url_encode_string()' - URL-encode a string.
9924 static char * /* O - End of string */
9925 url_encode_string(const char *s
, /* I - String */
9926 char *buffer
, /* I - String buffer */
9927 int bufsize
) /* I - Size of buffer */
9929 char *bufptr
, /* Pointer into buffer */
9930 *bufend
; /* End of buffer */
9931 static const char *hex
= "0123456789ABCDEF";
9936 bufend
= buffer
+ bufsize
- 1;
9938 while (*s
&& bufptr
< bufend
)
9940 if (*s
== ' ' || *s
== '%')
9942 if (bufptr
>= (bufend
- 2))
9946 *bufptr
++ = hex
[(*s
>> 4) & 15];
9947 *bufptr
++ = hex
[*s
& 15];
9951 else if (*s
== '\'' || *s
== '\\')
9953 if (bufptr
>= (bufend
- 1))
9970 * 'user_allowed()' - See if a user is allowed to print to a queue.
9973 static int /* O - 0 if not allowed, 1 if allowed */
9974 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
9975 const char *username
) /* I - Username */
9977 int i
; /* Looping var */
9978 struct passwd
*pw
; /* User password data */
9981 if (p
->num_users
== 0)
9984 if (!strcmp(username
, "root"))
9987 pw
= getpwnam(username
);
9990 for (i
= 0; i
< p
->num_users
; i
++)
9992 if (p
->users
[i
][0] == '@')
9995 * Check group membership...
9998 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
10001 else if (!strcasecmp(username
, p
->users
[i
]))
10005 return ((i
< p
->num_users
) != p
->deny_users
);
10010 * 'validate_job()' - Validate printer options and destination.
10014 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10015 ipp_attribute_t
*uri
) /* I - Printer URI */
10017 http_status_t status
; /* Policy status */
10018 ipp_attribute_t
*attr
; /* Current attribute */
10019 ipp_attribute_t
*format
; /* Document-format attribute */
10020 cups_ptype_t dtype
; /* Destination type (printer/class) */
10021 char super
[MIME_MAX_SUPER
],
10022 /* Supertype of file */
10023 type
[MIME_MAX_TYPE
];
10024 /* Subtype of file */
10025 cupsd_printer_t
*printer
; /* Printer */
10028 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10029 con
->http
.fd
, uri
->values
[0].string
.text
);
10032 * OK, see if the client is sending the document compressed - CUPS
10033 * doesn't support compression yet...
10036 if ((attr
= ippFindAttribute(con
->request
, "compression",
10037 IPP_TAG_KEYWORD
)) != NULL
&&
10038 !strcmp(attr
->values
[0].string
.text
, "none"))
10040 send_ipp_status(con
, IPP_ATTRIBUTES
,
10041 _("Unsupported compression attribute %s!"),
10042 attr
->values
[0].string
.text
);
10043 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10044 "compression", NULL
, attr
->values
[0].string
.text
);
10049 * Is it a format we support?
10052 if ((format
= ippFindAttribute(con
->request
, "document-format",
10053 IPP_TAG_MIMETYPE
)) != NULL
)
10055 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
10058 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
10059 format
->values
[0].string
.text
);
10063 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10064 !mimeType(MimeDatabase
, super
, type
))
10066 cupsdLogMessage(CUPSD_LOG_INFO
,
10067 "Hint: Do you have the raw file printing rules enabled?");
10068 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10069 _("Unsupported format \"%s\"!"),
10070 format
->values
[0].string
.text
);
10071 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10072 "document-format", NULL
, format
->values
[0].string
.text
);
10078 * Is the destination valid?
10081 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10087 send_ipp_status(con
, IPP_NOT_FOUND
,
10088 _("The printer or class was not found."));
10096 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10098 send_http_error(con
, status
, printer
);
10103 * Everything was ok, so return OK status...
10106 con
->response
->request
.status
.status_code
= IPP_OK
;
10111 * 'validate_name()' - Make sure the printer name only contains valid chars.
10114 static int /* O - 0 if name is no good, 1 if good */
10115 validate_name(const char *name
) /* I - Name to check */
10117 const char *ptr
; /* Pointer into name */
10121 * Scan the whole name...
10124 for (ptr
= name
; *ptr
; ptr
++)
10125 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
10129 * All the characters are good; validate the length, too...
10132 return ((ptr
- name
) < 128);
10137 * 'validate_user()' - Validate the user for the request.
10140 static int /* O - 1 if permitted, 0 otherwise */
10141 validate_user(cupsd_job_t
*job
, /* I - Job */
10142 cupsd_client_t
*con
, /* I - Client connection */
10143 const char *owner
, /* I - Owner of job/resource */
10144 char *username
, /* O - Authenticated username */
10145 int userlen
) /* I - Length of username */
10147 cupsd_printer_t
*printer
; /* Printer for job */
10150 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
10151 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
10153 job
? job
->id
: 0, con
->http
.fd
, owner
? owner
: "(null)",
10154 username
, userlen
);
10157 * Validate input...
10160 if (!con
|| !owner
|| !username
|| userlen
<= 0)
10164 * Get the best authenticated username that is available.
10167 strlcpy(username
, get_username(con
), userlen
);
10170 * Check the username against the owner...
10173 printer
= cupsdFindDest(job
->dest
);
10175 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
10176 con
, owner
) == HTTP_OK
);
10181 * End of "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $".