2 * "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $"
4 * IPP routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
20 * cupsdProcessIPPRequest() - Process an incoming IPP request.
21 * cupsdTimeoutJob() - Timeout a job waiting on job files.
22 * accept_jobs() - Accept print jobs to a printer.
23 * add_class() - Add a class to the system.
24 * add_file() - Add a file to a job.
25 * add_job() - Add a job to a print queue.
26 * add_job_state_reasons() - Add the "job-state-reasons" attribute based
27 * upon the job and printer state...
28 * add_job_subscriptions() - Add any subcriptions for a job.
29 * add_job_uuid() - Add job-uuid attribute to a job.
30 * add_printer() - Add a printer to the system.
31 * add_printer_state_reasons() - Add the "printer-state-reasons" attribute
32 * based upon the printer state...
33 * add_queued_job_count() - Add the "queued-job-count" attribute for
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", CUPSD_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.
755 int /* O - 0 on success, -1 on error */
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 if ((kbytes
= copy_banner(NULL
, job
, attr
->values
[1].string
.text
)) < 0)
786 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
794 * 'accept_jobs()' - Accept print jobs to a printer.
798 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
799 ipp_attribute_t
*uri
) /* I - Printer or class URI */
801 http_status_t status
; /* Policy status */
802 cups_ptype_t dtype
; /* Destination type (printer/class) */
803 cupsd_printer_t
*printer
; /* Printer data */
806 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
807 con
->http
.fd
, uri
->values
[0].string
.text
);
810 * Is the destination valid?
813 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
819 send_ipp_status(con
, IPP_NOT_FOUND
,
820 _("The printer or class was not found."));
828 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
830 send_http_error(con
, status
, printer
);
835 * Accept jobs sent to the printer...
838 printer
->accepting
= 1;
839 printer
->state_message
[0] = '\0';
841 cupsdAddPrinterHistory(printer
);
843 if (dtype
& CUPS_PRINTER_CLASS
)
845 cupsdSaveAllClasses();
847 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
848 printer
->name
, get_username(con
));
852 cupsdSaveAllPrinters();
854 cupsdLogMessage(CUPSD_LOG_INFO
,
855 "Printer \"%s\" now accepting jobs (\"%s\").",
856 printer
->name
, get_username(con
));
860 * Everything was ok, so return OK status...
863 con
->response
->request
.status
.status_code
= IPP_OK
;
868 * 'add_class()' - Add a class to the system.
872 add_class(cupsd_client_t
*con
, /* I - Client connection */
873 ipp_attribute_t
*uri
) /* I - URI of class */
875 http_status_t status
; /* Policy status */
876 int i
; /* Looping var */
877 char method
[HTTP_MAX_URI
], /* Method portion of URI */
878 username
[HTTP_MAX_URI
], /* Username portion of URI */
879 host
[HTTP_MAX_URI
], /* Host portion of URI */
880 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
881 int port
; /* Port portion of URI */
882 cupsd_printer_t
*pclass
, /* Class */
883 *member
; /* Member printer/class */
884 cups_ptype_t dtype
; /* Destination type */
885 ipp_attribute_t
*attr
; /* Printer attribute */
886 int modify
; /* Non-zero if we just modified */
887 char newname
[IPP_MAX_NAME
]; /* New class name */
888 int need_restart_job
; /* Need to restart job? */
891 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
892 con
->http
.fd
, uri
->values
[0].string
.text
);
895 * Do we have a valid URI?
898 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
899 sizeof(method
), username
, sizeof(username
), host
,
900 sizeof(host
), &port
, resource
, sizeof(resource
));
903 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
906 * No, return an error...
909 send_ipp_status(con
, IPP_BAD_REQUEST
,
910 _("The printer-uri must be of the form "
911 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
916 * Do we have a valid printer name?
919 if (!validate_name(resource
+ 9))
922 * No, return an error...
925 send_ipp_status(con
, IPP_BAD_REQUEST
,
926 _("The printer-uri \"%s\" contains invalid characters."),
927 uri
->values
[0].string
.text
);
932 * See if the class already exists; if not, create a new class...
935 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
938 * Class doesn't exist; see if we have a printer of the same name...
941 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
942 !(pclass
->type
& CUPS_PRINTER_DISCOVERED
))
945 * Yes, return an error...
948 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
949 _("A printer named \"%s\" already exists!"),
955 * No, check the default policy and then add the class...
958 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
960 send_http_error(con
, status
, NULL
);
964 pclass
= cupsdAddClass(resource
+ 9);
967 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
970 * Check the default policy, then rename the implicit class to "AnyClass"
974 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
976 send_http_error(con
, status
, NULL
);
980 if (ImplicitAnyClasses
)
982 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
983 cupsdRenamePrinter(pclass
, newname
);
986 cupsdDeletePrinter(pclass
, 1);
989 * Add the class as a new local class...
992 pclass
= cupsdAddClass(resource
+ 9);
995 else if (pclass
->type
& CUPS_PRINTER_DISCOVERED
)
998 * Check the default policy, then rename the remote class to "Class"...
1001 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
1003 send_http_error(con
, status
, NULL
);
1007 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
1008 cupsdRenamePrinter(pclass
, newname
);
1011 * Add the class as a new local class...
1014 pclass
= cupsdAddClass(resource
+ 9);
1017 else if ((status
= cupsdCheckPolicy(pclass
->op_policy_ptr
, con
,
1020 send_http_error(con
, status
, pclass
);
1027 * Look for attributes and copy them over as needed...
1030 need_restart_job
= 0;
1032 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
1033 IPP_TAG_TEXT
)) != NULL
)
1034 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
1036 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
1037 IPP_TAG_TEXT
)) != NULL
)
1038 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
1040 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
1041 IPP_TAG_BOOLEAN
)) != NULL
)
1043 cupsdLogMessage(CUPSD_LOG_INFO
,
1044 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
1045 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
1047 pclass
->accepting
= attr
->values
[0].boolean
;
1048 cupsdAddPrinterHistory(pclass
);
1051 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
1052 IPP_TAG_BOOLEAN
)) != NULL
)
1054 if (pclass
->shared
&& !attr
->values
[0].boolean
)
1055 cupsdDeregisterPrinter(pclass
, 1);
1057 cupsdLogMessage(CUPSD_LOG_INFO
,
1058 "Setting %s printer-is-shared to %d (was %d.)",
1059 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
1061 pclass
->shared
= attr
->values
[0].boolean
;
1064 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
1065 IPP_TAG_ENUM
)) != NULL
)
1067 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
1068 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
1070 send_ipp_status(con
, IPP_BAD_REQUEST
,
1071 _("Attempt to set %s printer-state to bad value %d!"),
1072 pclass
->name
, attr
->values
[0].integer
);
1076 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
1077 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
1079 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1080 cupsdStopPrinter(pclass
, 0);
1083 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1084 need_restart_job
= 1;
1087 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1088 IPP_TAG_TEXT
)) != NULL
)
1090 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1091 sizeof(pclass
->state_message
));
1092 cupsdAddPrinterHistory(pclass
);
1094 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1095 IPP_TAG_URI
)) != NULL
)
1098 * Clear the printer array as needed...
1101 need_restart_job
= 1;
1103 if (pclass
->num_printers
> 0)
1105 free(pclass
->printers
);
1106 pclass
->num_printers
= 0;
1110 * Add each printer or class that is listed...
1113 for (i
= 0; i
< attr
->num_values
; i
++)
1116 * Search for the printer or class URI...
1119 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1125 send_ipp_status(con
, IPP_NOT_FOUND
,
1126 _("The printer or class was not found."));
1131 * Add it to the class...
1134 cupsdAddPrinterToClass(pclass
, member
);
1138 set_printer_defaults(con
, pclass
);
1140 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1141 IPP_TAG_KEYWORD
)) != NULL
)
1142 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1145 * Update the printer class attributes and return...
1148 cupsdSetPrinterAttrs(pclass
);
1149 cupsdSaveAllClasses();
1151 if (need_restart_job
&& pclass
->job
)
1156 * Stop the current job and then restart it below...
1159 job
= (cupsd_job_t
*)pclass
->job
;
1161 cupsdStopJob(job
, 1);
1163 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1164 job
->state_value
= IPP_JOB_PENDING
;
1167 if (need_restart_job
)
1170 cupsdWritePrintcap();
1174 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1175 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1178 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1179 pclass
->name
, get_username(con
));
1183 cupsdAddPrinterHistory(pclass
);
1185 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1186 "New class \"%s\" added by \"%s\".", pclass
->name
,
1189 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1190 pclass
->name
, get_username(con
));
1193 con
->response
->request
.status
.status_code
= IPP_OK
;
1198 * 'add_file()' - Add a file to a job.
1201 static int /* O - 0 on success, -1 on error */
1202 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1203 cupsd_job_t
*job
, /* I - Job to add to */
1204 mime_type_t
*filetype
, /* I - Type of file */
1205 int compression
) /* I - Compression */
1207 mime_type_t
**filetypes
; /* New filetypes array... */
1208 int *compressions
; /* New compressions array... */
1211 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1212 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1213 "compression=%d)", con
, con
? con
->http
.fd
: -1, job
->id
,
1214 filetype
->super
, filetype
->type
, compression
);
1217 * Add the file to the job...
1220 if (job
->num_files
== 0)
1222 compressions
= (int *)malloc(sizeof(int));
1223 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1227 compressions
= (int *)realloc(job
->compressions
,
1228 (job
->num_files
+ 1) * sizeof(int));
1229 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1230 (job
->num_files
+ 1) *
1231 sizeof(mime_type_t
*));
1234 if (!compressions
|| !filetypes
)
1236 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
1239 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1240 _("Unable to allocate memory for file types!"));
1245 job
->compressions
= compressions
;
1246 job
->compressions
[job
->num_files
] = compression
;
1247 job
->filetypes
= filetypes
;
1248 job
->filetypes
[job
->num_files
] = filetype
;
1257 * 'add_job()' - Add a job to a print queue.
1260 static cupsd_job_t
* /* O - Job object */
1261 add_job(cupsd_client_t
*con
, /* I - Client connection */
1262 cupsd_printer_t
*printer
, /* I - Destination printer */
1263 mime_type_t
*filetype
) /* I - First print file type, if any */
1265 http_status_t status
; /* Policy status */
1266 ipp_attribute_t
*attr
, /* Current attribute */
1267 *auth_info
; /* auth-info attribute */
1268 const char *val
; /* Default option value */
1269 int priority
; /* Job priority */
1270 char *title
; /* Job name/title */
1271 cupsd_job_t
*job
; /* Current job */
1272 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1273 int kbytes
; /* Size of print file */
1274 int i
; /* Looping var */
1275 int lowerpagerange
; /* Page range bound */
1278 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1279 con
, con
->http
.fd
, printer
, printer
->name
,
1280 filetype
, filetype
? filetype
->super
: "none",
1281 filetype
? filetype
->type
: "none");
1284 * Check remote printing to non-shared printer...
1287 if (!printer
->shared
&&
1288 strcasecmp(con
->http
.hostname
, "localhost") &&
1289 strcasecmp(con
->http
.hostname
, ServerName
))
1291 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1292 _("The printer or class is not shared!"));
1300 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1302 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1304 send_http_error(con
, status
, printer
);
1307 else if ((printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
1308 !con
->username
[0] && !auth_info
)
1310 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1314 else if (auth_info
&& !con
->http
.tls
&&
1315 !httpAddrLocalhost(con
->http
.hostaddr
))
1318 * Require encryption of auth-info over non-local connections...
1321 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1324 #endif /* HAVE_SSL */
1327 * See if the printer is accepting jobs...
1330 if (!printer
->accepting
)
1332 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1333 _("Destination \"%s\" is not accepting jobs."),
1339 * Validate job template attributes; for now just document-format,
1340 * copies, number-up, and page-ranges...
1343 if (filetype
&& printer
->filetypes
&&
1344 !cupsArrayFind(printer
->filetypes
, filetype
))
1346 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1347 /* MIME media type string */
1350 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1353 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1354 _("Unsupported format \'%s\'!"), mimetype
);
1356 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1357 "document-format", NULL
, mimetype
);
1362 if ((attr
= ippFindAttribute(con
->request
, "copies",
1363 IPP_TAG_INTEGER
)) != NULL
)
1365 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1367 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1368 attr
->values
[0].integer
);
1369 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1370 "copies", attr
->values
[0].integer
);
1375 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1376 IPP_TAG_INTEGER
)) != NULL
)
1378 if (attr
->values
[0].integer
!= 1 &&
1379 attr
->values
[0].integer
!= 2 &&
1380 attr
->values
[0].integer
!= 4 &&
1381 attr
->values
[0].integer
!= 6 &&
1382 attr
->values
[0].integer
!= 9 &&
1383 attr
->values
[0].integer
!= 16)
1385 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1386 attr
->values
[0].integer
);
1387 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1388 "number-up", attr
->values
[0].integer
);
1393 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1394 IPP_TAG_RANGE
)) != NULL
)
1396 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1398 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1399 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1401 send_ipp_status(con
, IPP_BAD_REQUEST
,
1402 _("Bad page-ranges values %d-%d."),
1403 attr
->values
[i
].range
.lower
,
1404 attr
->values
[i
].range
.upper
);
1408 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1413 * Make sure we aren't over our limit...
1416 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1419 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1421 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1422 _("Too many active jobs."));
1426 if ((i
= check_quotas(con
, printer
)) < 0)
1428 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1433 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1438 * Create the job and set things up...
1441 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1442 IPP_TAG_INTEGER
)) != NULL
)
1443 priority
= attr
->values
[0].integer
;
1446 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1447 printer
->options
)) != NULL
)
1448 priority
= atoi(val
);
1452 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1456 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1457 IPP_TAG_NAME
)) != NULL
)
1458 title
= attr
->values
[0].string
.text
;
1460 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1461 title
= "Untitled");
1463 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1465 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1466 _("Unable to add job for destination \"%s\"!"),
1471 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
1472 CUPS_PRINTER_REMOTE
);
1473 job
->attrs
= con
->request
;
1474 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1476 add_job_uuid(con
, job
);
1477 apply_printer_defaults(printer
, job
);
1479 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1481 if (con
->username
[0])
1483 cupsdSetString(&job
->username
, con
->username
);
1486 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1490 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1491 "add_job: requesting-user-name=\"%s\"",
1492 attr
->values
[0].string
.text
);
1494 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1497 cupsdSetString(&job
->username
, "anonymous");
1500 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1501 "job-originating-user-name", NULL
, job
->username
);
1504 attr
->group_tag
= IPP_TAG_JOB
;
1505 _cupsStrFree(attr
->name
);
1506 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1509 if (con
->username
[0] || auth_info
)
1511 save_auth_info(con
, job
, auth_info
);
1514 * Remove the auth-info attribute from the attribute data...
1518 ippDeleteAttribute(job
->attrs
, auth_info
);
1521 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1522 IPP_TAG_ZERO
)) != NULL
)
1525 * Request contains a job-originating-host-name attribute; validate it...
1528 if (attr
->value_tag
!= IPP_TAG_NAME
||
1529 attr
->num_values
!= 1 ||
1530 strcmp(con
->http
.hostname
, "localhost"))
1533 * Can't override the value if we aren't connected via localhost.
1534 * Also, we can only have 1 value and it must be a name value.
1537 switch (attr
->value_tag
)
1539 case IPP_TAG_STRING
:
1540 case IPP_TAG_TEXTLANG
:
1541 case IPP_TAG_NAMELANG
:
1544 case IPP_TAG_KEYWORD
:
1546 case IPP_TAG_URISCHEME
:
1547 case IPP_TAG_CHARSET
:
1548 case IPP_TAG_LANGUAGE
:
1549 case IPP_TAG_MIMETYPE
:
1551 * Free old strings...
1554 for (i
= 0; i
< attr
->num_values
; i
++)
1556 _cupsStrFree(attr
->values
[i
].string
.text
);
1557 attr
->values
[i
].string
.text
= NULL
;
1558 if (attr
->values
[i
].string
.charset
)
1560 _cupsStrFree(attr
->values
[i
].string
.charset
);
1561 attr
->values
[i
].string
.charset
= NULL
;
1570 * Use the default connection hostname instead...
1573 attr
->value_tag
= IPP_TAG_NAME
;
1574 attr
->num_values
= 1;
1575 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1578 attr
->group_tag
= IPP_TAG_JOB
;
1583 * No job-originating-host-name attribute, so use the hostname from
1587 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1588 "job-originating-host-name", NULL
, con
->http
.hostname
);
1591 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1593 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1594 "time-at-processing", 0);
1595 attr
->value_tag
= IPP_TAG_NOVALUE
;
1596 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1597 "time-at-completed", 0);
1598 attr
->value_tag
= IPP_TAG_NOVALUE
;
1601 * Add remaining job attributes...
1604 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1605 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1606 "job-state", IPP_JOB_STOPPED
);
1607 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1608 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1609 "job-media-sheets-completed", 0);
1610 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1612 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1615 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1616 IPP_TAG_INTEGER
)) != NULL
)
1617 attr
->values
[0].integer
= 0;
1619 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1622 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1623 IPP_TAG_KEYWORD
)) == NULL
)
1624 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1627 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1628 printer
->options
)) == NULL
)
1631 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1632 "job-hold-until", NULL
, val
);
1634 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1635 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1638 * Hold job until specified time...
1641 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1643 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1644 job
->state_value
= IPP_JOB_HELD
;
1646 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1648 job
->hold_until
= time(NULL
) + 60;
1649 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1650 job
->state_value
= IPP_JOB_HELD
;
1654 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1655 job
->state_value
= IPP_JOB_PENDING
;
1658 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1662 * Add job sheets options...
1665 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1666 IPP_TAG_ZERO
)) == NULL
)
1668 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1669 "Adding default job-sheets values \"%s,%s\"...",
1670 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1672 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1674 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1675 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1678 job
->job_sheets
= attr
;
1681 * Enforce classification level if set...
1686 cupsdLogMessage(CUPSD_LOG_INFO
,
1687 "Classification=\"%s\", ClassifyOverride=%d",
1688 Classification
? Classification
: "(null)",
1691 if (ClassifyOverride
)
1693 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1694 (attr
->num_values
== 1 ||
1695 !strcmp(attr
->values
[1].string
.text
, "none")))
1698 * Force the leading banner to have the classification on it...
1701 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1703 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1704 "job-sheets=\"%s,none\", "
1705 "job-originating-user-name=\"%s\"",
1706 job
->id
, Classification
, job
->username
);
1708 else if (attr
->num_values
== 2 &&
1709 strcmp(attr
->values
[0].string
.text
,
1710 attr
->values
[1].string
.text
) &&
1711 strcmp(attr
->values
[0].string
.text
, "none") &&
1712 strcmp(attr
->values
[1].string
.text
, "none"))
1715 * Can't put two different security markings on the same document!
1718 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1720 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1721 "job-sheets=\"%s,%s\", "
1722 "job-originating-user-name=\"%s\"",
1723 job
->id
, attr
->values
[0].string
.text
,
1724 attr
->values
[1].string
.text
, job
->username
);
1726 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1727 strcmp(attr
->values
[0].string
.text
, "none") &&
1728 (attr
->num_values
== 1 ||
1729 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1730 strcmp(attr
->values
[1].string
.text
, "none"))))
1732 if (attr
->num_values
== 1)
1733 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1734 "[Job %d] CLASSIFICATION OVERRIDDEN "
1735 "job-sheets=\"%s\", "
1736 "job-originating-user-name=\"%s\"",
1737 job
->id
, attr
->values
[0].string
.text
, job
->username
);
1739 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1740 "[Job %d] CLASSIFICATION OVERRIDDEN "
1741 "job-sheets=\"%s,%s\",fffff "
1742 "job-originating-user-name=\"%s\"",
1743 job
->id
, attr
->values
[0].string
.text
,
1744 attr
->values
[1].string
.text
, job
->username
);
1747 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1748 (attr
->num_values
== 1 ||
1749 strcmp(attr
->values
[1].string
.text
, Classification
)))
1752 * Force the banner to have the classification on it...
1755 if (attr
->num_values
> 1 &&
1756 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1758 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1759 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1763 if (attr
->num_values
== 1 ||
1764 strcmp(attr
->values
[0].string
.text
, "none"))
1765 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1767 if (attr
->num_values
> 1 &&
1768 strcmp(attr
->values
[1].string
.text
, "none"))
1769 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1772 if (attr
->num_values
> 1)
1773 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1774 "[Job %d] CLASSIFICATION FORCED "
1775 "job-sheets=\"%s,%s\", "
1776 "job-originating-user-name=\"%s\"",
1777 job
->id
, attr
->values
[0].string
.text
,
1778 attr
->values
[1].string
.text
, job
->username
);
1780 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1781 "[Job %d] CLASSIFICATION FORCED "
1782 "job-sheets=\"%s\", "
1783 "job-originating-user-name=\"%s\"",
1784 job
->id
, Classification
, job
->username
);
1789 * See if we need to add the starting sheet...
1792 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1794 cupsdLogMessage(CUPSD_LOG_INFO
,
1795 "[Job %d] Adding start banner page \"%s\".",
1796 job
->id
, attr
->values
[0].string
.text
);
1798 if ((kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
)) < 0)
1801 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1804 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1805 IPP_TAG_ZERO
)) != NULL
)
1809 * Fill in the response info...
1812 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1813 LocalPort
, job
->id
);
1815 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1818 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1820 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1822 add_job_state_reasons(con
, job
);
1824 con
->response
->request
.status
.status_code
= IPP_OK
;
1827 * Add any job subscriptions...
1830 add_job_subscriptions(con
, job
);
1833 * Set all but the first two attributes to the job attributes group...
1836 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1837 attr
->group_tag
= IPP_TAG_JOB
;
1840 * Fire the "job created" event...
1843 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1846 * Return the new job...
1854 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1855 * upon the job and printer state...
1859 add_job_state_reasons(
1860 cupsd_client_t
*con
, /* I - Client connection */
1861 cupsd_job_t
*job
) /* I - Job info */
1863 cupsd_printer_t
*dest
; /* Destination printer */
1866 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1867 con
, con
->http
.fd
, job
? job
->id
: 0);
1869 switch (job
? job
->state_value
: IPP_JOB_CANCELED
)
1871 case IPP_JOB_PENDING
:
1872 dest
= cupsdFindDest(job
->dest
);
1874 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1875 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1876 "job-state-reasons", NULL
, "printer-stopped");
1878 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1879 "job-state-reasons", NULL
, "none");
1883 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1884 IPP_TAG_KEYWORD
) != NULL
||
1885 ippFindAttribute(job
->attrs
, "job-hold-until",
1886 IPP_TAG_NAME
) != NULL
)
1887 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1888 "job-state-reasons", NULL
, "job-hold-until-specified");
1890 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1891 "job-state-reasons", NULL
, "job-incoming");
1894 case IPP_JOB_PROCESSING
:
1895 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1896 "job-state-reasons", NULL
, "job-printing");
1899 case IPP_JOB_STOPPED
:
1900 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1901 "job-state-reasons", NULL
, "job-stopped");
1904 case IPP_JOB_CANCELED
:
1905 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1906 "job-state-reasons", NULL
, "job-canceled-by-user");
1909 case IPP_JOB_ABORTED
:
1910 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1911 "job-state-reasons", NULL
, "aborted-by-system");
1914 case IPP_JOB_COMPLETED
:
1915 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1916 "job-state-reasons", NULL
, "job-completed-successfully");
1923 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1927 add_job_subscriptions(
1928 cupsd_client_t
*con
, /* I - Client connection */
1929 cupsd_job_t
*job
) /* I - Newly created job */
1931 int i
; /* Looping var */
1932 ipp_attribute_t
*prev
, /* Previous attribute */
1933 *next
, /* Next attribute */
1934 *attr
; /* Current attribute */
1935 cupsd_subscription_t
*sub
; /* Subscription object */
1936 const char *recipient
, /* notify-recipient-uri */
1937 *pullmethod
; /* notify-pull-method */
1938 ipp_attribute_t
*user_data
; /* notify-user-data */
1939 int interval
; /* notify-time-interval */
1940 unsigned mask
; /* notify-events */
1944 * Find the first subscription group attribute; return if we have
1948 for (attr
= job
->attrs
->attrs
, prev
= NULL
;
1950 prev
= attr
, attr
= attr
->next
)
1951 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1958 * Process the subscription attributes in the request...
1967 mask
= CUPSD_EVENT_NONE
;
1969 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1971 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
1972 attr
->value_tag
== IPP_TAG_URI
)
1973 recipient
= attr
->values
[0].string
.text
;
1974 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1975 attr
->value_tag
== IPP_TAG_KEYWORD
)
1976 pullmethod
= attr
->values
[0].string
.text
;
1977 else if (!strcmp(attr
->name
, "notify-charset") &&
1978 attr
->value_tag
== IPP_TAG_CHARSET
&&
1979 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
1980 strcmp(attr
->values
[0].string
.text
, "utf-8"))
1982 send_ipp_status(con
, IPP_CHARSET
,
1983 _("Character set \"%s\" not supported!"),
1984 attr
->values
[0].string
.text
);
1987 else if (!strcmp(attr
->name
, "notify-natural-language") &&
1988 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
1989 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
1991 send_ipp_status(con
, IPP_CHARSET
,
1992 _("Language \"%s\" not supported!"),
1993 attr
->values
[0].string
.text
);
1996 else if (!strcmp(attr
->name
, "notify-user-data") &&
1997 attr
->value_tag
== IPP_TAG_STRING
)
1999 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
2001 send_ipp_status(con
, IPP_REQUEST_VALUE
,
2002 _("The notify-user-data value is too large "
2003 "(%d > 63 octets)!"),
2004 attr
->values
[0].unknown
.length
);
2010 else if (!strcmp(attr
->name
, "notify-events") &&
2011 attr
->value_tag
== IPP_TAG_KEYWORD
)
2013 for (i
= 0; i
< attr
->num_values
; i
++)
2014 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
2016 else if (!strcmp(attr
->name
, "notify-lease-duration"))
2018 send_ipp_status(con
, IPP_BAD_REQUEST
,
2019 _("The notify-lease-duration attribute cannot be "
2020 "used with job subscriptions."));
2023 else if (!strcmp(attr
->name
, "notify-time-interval") &&
2024 attr
->value_tag
== IPP_TAG_INTEGER
)
2025 interval
= attr
->values
[0].integer
;
2030 if (!recipient
&& !pullmethod
)
2033 if (mask
== CUPSD_EVENT_NONE
)
2034 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2036 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
2039 sub
->interval
= interval
;
2041 cupsdSetString(&sub
->owner
, job
->username
);
2045 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2046 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2047 sub
->user_data_len
);
2050 ippAddSeparator(con
->response
);
2051 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2052 "notify-subscription-id", sub
->id
);
2058 cupsdSaveAllSubscriptions();
2061 * Remove all of the subscription attributes from the job request...
2064 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2068 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2069 attr
->group_tag
== IPP_TAG_ZERO
)
2072 * Free and remove this attribute...
2080 job
->attrs
->attrs
= next
;
2086 job
->attrs
->last
= prev
;
2087 job
->attrs
->current
= prev
;
2092 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2094 * See RFC 4122 for the definition of UUIDs and the format.
2098 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
2099 cupsd_job_t
*job
) /* I - Job */
2101 char uuid
[1024]; /* job-uuid string */
2102 _cups_md5_state_t md5state
; /* MD5 state */
2103 unsigned char md5sum
[16]; /* MD5 digest/sum */
2107 * First see if the job already has a job-uuid attribute; if so, return...
2110 if (ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2114 * No job-uuid attribute, so build a version 3 UUID with the local job
2115 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
2116 * the ServerName, server name and port that the client connected to,
2117 * and local job ID...
2120 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
2121 con
->serverport
, job
->id
);
2123 _cupsMD5Init(&md5state
);
2124 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
2125 _cupsMD5Finish(&md5state
, md5sum
);
2128 * Format the UUID URI using the MD5 sum and job ID.
2131 snprintf(uuid
, sizeof(uuid
),
2132 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2133 "%02x%02x%02x%02x%02x%02x",
2134 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
2135 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
2136 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
2137 md5sum
[14], md5sum
[15]);
2139 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
2144 * 'add_printer()' - Add a printer to the system.
2148 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2149 ipp_attribute_t
*uri
) /* I - URI of printer */
2151 http_status_t status
; /* Policy status */
2152 int i
; /* Looping var */
2153 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2154 username
[HTTP_MAX_URI
], /* Username portion of URI */
2155 host
[HTTP_MAX_URI
], /* Host portion of URI */
2156 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2157 int port
; /* Port portion of URI */
2158 cupsd_printer_t
*printer
; /* Printer/class */
2159 ipp_attribute_t
*attr
; /* Printer attribute */
2160 cups_file_t
*fp
; /* Script/PPD file */
2161 char line
[1024]; /* Line from file... */
2162 char srcfile
[1024], /* Source Script/PPD file */
2163 dstfile
[1024]; /* Destination Script/PPD file */
2164 int modify
; /* Non-zero if we are modifying */
2165 char newname
[IPP_MAX_NAME
]; /* New printer name */
2166 int need_restart_job
; /* Need to restart job? */
2167 int set_device_uri
, /* Did we set the device URI? */
2168 set_port_monitor
; /* Did we set the port monitor? */
2171 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2172 con
->http
.fd
, uri
->values
[0].string
.text
);
2175 * Do we have a valid URI?
2178 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2179 sizeof(scheme
), username
, sizeof(username
), host
,
2180 sizeof(host
), &port
, resource
, sizeof(resource
));
2182 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2185 * No, return an error...
2188 send_ipp_status(con
, IPP_BAD_REQUEST
,
2189 _("The printer-uri must be of the form "
2190 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2195 * Do we have a valid printer name?
2198 if (!validate_name(resource
+ 10))
2201 * No, return an error...
2204 send_ipp_status(con
, IPP_BAD_REQUEST
,
2205 _("The printer-uri \"%s\" contains invalid characters."),
2206 uri
->values
[0].string
.text
);
2211 * See if the printer already exists; if not, create a new printer...
2214 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2217 * Printer doesn't exist; see if we have a class of the same name...
2220 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2221 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
2224 * Yes, return an error...
2227 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2228 _("A class named \"%s\" already exists!"),
2234 * No, check the default policy then add the printer...
2237 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2239 send_http_error(con
, status
, NULL
);
2243 printer
= cupsdAddPrinter(resource
+ 10);
2246 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2249 * Check the default policy, then rename the implicit printer to
2250 * "AnyPrinter" or delete it...
2253 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2255 send_http_error(con
, status
, NULL
);
2259 if (ImplicitAnyClasses
)
2261 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2262 cupsdRenamePrinter(printer
, newname
);
2265 cupsdDeletePrinter(printer
, 1);
2268 * Add the printer as a new local printer...
2271 printer
= cupsdAddPrinter(resource
+ 10);
2274 else if (printer
->type
& CUPS_PRINTER_DISCOVERED
)
2277 * Check the default policy, then rename the remote printer to
2278 * "Printer@server"...
2281 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2283 send_http_error(con
, status
, NULL
);
2287 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2289 cupsdRenamePrinter(printer
, newname
);
2292 * Add the printer as a new local printer...
2295 printer
= cupsdAddPrinter(resource
+ 10);
2298 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2301 send_http_error(con
, status
, printer
);
2308 * Look for attributes and copy them over as needed...
2311 need_restart_job
= 0;
2313 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2314 IPP_TAG_TEXT
)) != NULL
)
2315 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2317 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2318 IPP_TAG_TEXT
)) != NULL
)
2319 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2323 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2324 IPP_TAG_URI
)) != NULL
)
2327 * Do we have a valid device URI?
2330 http_uri_status_t uri_status
; /* URI separation status */
2333 need_restart_job
= 1;
2335 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2336 attr
->values
[0].string
.text
,
2337 scheme
, sizeof(scheme
),
2338 username
, sizeof(username
),
2339 host
, sizeof(host
), &port
,
2340 resource
, sizeof(resource
));
2342 if (uri_status
< HTTP_URI_OK
)
2344 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2345 attr
->values
[0].string
.text
);
2346 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2347 "add_printer: httpSeparateURI returned %d", uri_status
);
2351 if (!strcmp(scheme
, "file"))
2354 * See if the administrator has enabled file devices...
2357 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2360 * File devices are disabled and the URL is not file:/dev/null...
2363 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2364 _("File device URIs have been disabled! "
2365 "To enable, see the FileDevice directive in "
2366 "\"%s/cupsd.conf\"."),
2374 * See if the backend exists and is executable...
2377 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2378 if (access(srcfile
, X_OK
))
2381 * Could not find device in list!
2384 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2385 attr
->values
[0].string
.text
);
2390 cupsdLogMessage(CUPSD_LOG_INFO
,
2391 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2393 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2395 cupsdSanitizeURI(printer
->device_uri
, resource
,
2398 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2402 set_port_monitor
= 0;
2404 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2405 IPP_TAG_NAME
)) != NULL
)
2407 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2410 need_restart_job
= 1;
2412 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2416 for (i
= 0; i
< supported
->num_values
; i
++)
2417 if (!strcmp(supported
->values
[i
].string
.text
,
2418 attr
->values
[0].string
.text
))
2422 if (!supported
|| i
>= supported
->num_values
)
2424 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2425 attr
->values
[0].string
.text
);
2429 cupsdLogMessage(CUPSD_LOG_INFO
,
2430 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2431 printer
->name
, attr
->values
[0].string
.text
,
2432 printer
->port_monitor
? printer
->port_monitor
: "none");
2434 if (strcmp(attr
->values
[0].string
.text
, "none"))
2435 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2437 cupsdClearString(&printer
->port_monitor
);
2439 set_port_monitor
= 1;
2442 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2443 IPP_TAG_BOOLEAN
)) != NULL
)
2445 cupsdLogMessage(CUPSD_LOG_INFO
,
2446 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2447 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2449 printer
->accepting
= attr
->values
[0].boolean
;
2450 cupsdAddPrinterHistory(printer
);
2453 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2454 IPP_TAG_BOOLEAN
)) != NULL
)
2456 if (printer
->shared
&& !attr
->values
[0].boolean
)
2457 cupsdDeregisterPrinter(printer
, 1);
2459 cupsdLogMessage(CUPSD_LOG_INFO
,
2460 "Setting %s printer-is-shared to %d (was %d.)",
2461 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2463 printer
->shared
= attr
->values
[0].boolean
;
2466 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2467 IPP_TAG_ENUM
)) != NULL
)
2469 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2470 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2472 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2473 attr
->values
[0].integer
);
2477 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2478 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2480 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2481 cupsdStopPrinter(printer
, 0);
2484 need_restart_job
= 1;
2485 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2488 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2489 IPP_TAG_TEXT
)) != NULL
)
2491 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2492 sizeof(printer
->state_message
));
2493 cupsdAddPrinterHistory(printer
);
2496 set_printer_defaults(con
, printer
);
2498 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2499 IPP_TAG_KEYWORD
)) != NULL
)
2500 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2503 * See if we have all required attributes...
2506 if (!printer
->device_uri
)
2507 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2510 * See if we have an interface script or PPD file attached to the request...
2515 need_restart_job
= 1;
2517 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2519 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2522 * Yes; get the first line from it...
2526 cupsFileGets(fp
, line
, sizeof(line
));
2530 * Then see what kind of file it is...
2533 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2536 if (!strncmp(line
, "*PPD-Adobe", 10))
2539 * The new file is a PPD file, so remove any old interface script
2540 * that might be lying around...
2548 * This must be an interface script, so move the file over to the
2549 * interfaces directory and make it executable...
2552 if (copy_file(srcfile
, dstfile
))
2554 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2555 _("Unable to copy interface script - %s!"),
2561 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2562 "Copied interface script successfully!");
2563 chmod(dstfile
, 0755);
2567 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2570 if (!strncmp(line
, "*PPD-Adobe", 10))
2573 * The new file is a PPD file, so move the file over to the
2574 * ppd directory and make it readable by all...
2577 if (copy_file(srcfile
, dstfile
))
2579 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2580 _("Unable to copy PPD file - %s!"),
2586 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2587 "Copied PPD file successfully!");
2588 chmod(dstfile
, 0644);
2594 * This must be an interface script, so remove any old PPD file that
2595 * may be lying around...
2602 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2603 IPP_TAG_NAME
)) != NULL
)
2605 need_restart_job
= 1;
2607 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2610 * Raw driver, remove any existing PPD or interface script files.
2613 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2617 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2627 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2631 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2634 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2636 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2641 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2642 "Copied PPD file successfully!");
2643 chmod(dstfile
, 0644);
2649 * If we set the device URI but not the port monitor, check which port
2650 * monitor to use by default...
2653 if (set_device_uri
&& !set_port_monitor
)
2655 ppd_file_t
*ppd
; /* PPD file */
2656 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2659 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2660 sizeof(scheme
), username
, sizeof(username
), host
,
2661 sizeof(host
), &port
, resource
, sizeof(resource
));
2663 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2665 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2667 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2669 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2670 if (!strcmp(scheme
, ppdattr
->spec
))
2672 cupsdLogMessage(CUPSD_LOG_INFO
,
2673 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2674 printer
->name
, ppdattr
->value
,
2675 printer
->port_monitor
? printer
->port_monitor
2678 if (strcmp(ppdattr
->value
, "none"))
2679 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2681 cupsdClearString(&printer
->port_monitor
);
2691 * Update the printer attributes and return...
2694 cupsdSetPrinterAttrs(printer
);
2695 cupsdSaveAllPrinters();
2697 if (need_restart_job
&& printer
->job
)
2702 * Stop the current job and then restart it below...
2705 job
= (cupsd_job_t
*)printer
->job
;
2707 cupsdStopJob(job
, 1);
2709 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2710 job
->state_value
= IPP_JOB_PENDING
;
2713 if (need_restart_job
)
2716 cupsdWritePrintcap();
2720 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2721 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2724 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2725 printer
->name
, get_username(con
));
2729 cupsdAddPrinterHistory(printer
);
2731 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2732 "New printer \"%s\" added by \"%s\".", printer
->name
,
2735 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2736 printer
->name
, get_username(con
));
2739 con
->response
->request
.status
.status_code
= IPP_OK
;
2744 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2745 * based upon the printer state...
2749 add_printer_state_reasons(
2750 cupsd_client_t
*con
, /* I - Client connection */
2751 cupsd_printer_t
*p
) /* I - Printer info */
2753 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2754 "add_printer_state_reasons(%p[%d], %p[%s])",
2755 con
, con
->http
.fd
, p
, p
->name
);
2757 if (p
->num_reasons
== 0)
2758 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2759 "printer-state-reasons", NULL
,
2760 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2762 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2763 "printer-state-reasons", p
->num_reasons
, NULL
,
2764 (const char * const *)p
->reasons
);
2769 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2770 * the specified printer or class.
2774 add_queued_job_count(
2775 cupsd_client_t
*con
, /* I - Client connection */
2776 cupsd_printer_t
*p
) /* I - Printer or class */
2778 int count
; /* Number of jobs on destination */
2781 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2782 con
, con
->http
.fd
, p
, p
->name
);
2784 count
= cupsdGetPrinterJobCount(p
->name
);
2786 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2787 "queued-job-count", count
);
2792 * 'apply_printer_defaults()' - Apply printer default options to a job.
2796 apply_printer_defaults(
2797 cupsd_printer_t
*printer
, /* I - Printer */
2798 cupsd_job_t
*job
) /* I - Job */
2800 int i
, /* Looping var */
2801 num_options
; /* Number of default options */
2802 cups_option_t
*options
, /* Default options */
2803 *option
; /* Current option */
2807 * Collect all of the default options and add the missing ones to the
2811 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
2814 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2816 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2821 * Encode these options as attributes in the job object...
2824 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2825 cupsFreeOptions(num_options
, options
);
2830 * 'authenticate_job()' - Set job authentication info.
2834 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2835 ipp_attribute_t
*uri
) /* I - Job URI */
2837 ipp_attribute_t
*attr
, /* job-id attribute */
2838 *auth_info
; /* auth-info attribute */
2839 int jobid
; /* Job ID */
2840 cupsd_job_t
*job
; /* Current job */
2841 char method
[HTTP_MAX_URI
],
2842 /* Method portion of URI */
2843 username
[HTTP_MAX_URI
],
2844 /* Username portion of URI */
2846 /* Host portion of URI */
2847 resource
[HTTP_MAX_URI
];
2848 /* Resource portion of URI */
2849 int port
; /* Port portion of URI */
2852 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2853 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
2856 * Start with "everything is OK" status...
2859 con
->response
->request
.status
.status_code
= IPP_OK
;
2862 * See if we have a job URI or a printer URI...
2865 if (!strcmp(uri
->name
, "printer-uri"))
2868 * Got a printer URI; see if we also have a job-id attribute...
2871 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2872 IPP_TAG_INTEGER
)) == NULL
)
2874 send_ipp_status(con
, IPP_BAD_REQUEST
,
2875 _("Got a printer-uri attribute but no job-id!"));
2879 jobid
= attr
->values
[0].integer
;
2884 * Got a job URI; parse it to get the job ID...
2887 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2888 sizeof(method
), username
, sizeof(username
), host
,
2889 sizeof(host
), &port
, resource
, sizeof(resource
));
2891 if (strncmp(resource
, "/jobs/", 6))
2897 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
2898 uri
->values
[0].string
.text
);
2902 jobid
= atoi(resource
+ 6);
2906 * See if the job exists...
2909 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2912 * Nope - return a "not found" error...
2915 send_ipp_status(con
, IPP_NOT_FOUND
,
2916 _("Job #%d does not exist!"), jobid
);
2921 * See if the job has been completed...
2924 if (job
->state_value
!= IPP_JOB_HELD
)
2927 * Return a "not-possible" error...
2930 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2931 _("Job #%d is not held for authentication!"),
2937 * See if we have already authenticated...
2940 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
2942 if (!con
->username
[0] && !auth_info
)
2944 cupsd_printer_t
*printer
; /* Job destination */
2948 * No auth data. If we need to authenticate via Kerberos, send a
2949 * HTTP auth challenge, otherwise just return an IPP error...
2952 printer
= cupsdFindDest(job
->dest
);
2954 if (printer
&& printer
->num_auth_info_required
> 0 &&
2955 !strcmp(printer
->auth_info_required
[0], "negotiate"))
2956 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
2958 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
2959 _("No authentication information provided!"));
2964 * See if the job is owned by the requesting user...
2967 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
2969 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
2974 * Save the authentication information for this job...
2977 save_auth_info(con
, job
, auth_info
);
2980 * Reset the job-hold-until value to "no-hold"...
2983 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2984 IPP_TAG_KEYWORD
)) == NULL
)
2985 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2989 attr
->value_tag
= IPP_TAG_KEYWORD
;
2990 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2994 * Release the job and return...
2997 cupsdReleaseJob(job
);
2999 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Authenticated by \"%s\".", jobid
,
3005 * 'cancel_all_jobs()' - Cancel all print jobs.
3009 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3010 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3012 http_status_t status
; /* Policy status */
3013 cups_ptype_t dtype
; /* Destination type */
3014 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3015 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3016 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3017 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3018 int port
; /* Port portion of URI */
3019 ipp_attribute_t
*attr
; /* Attribute in request */
3020 const char *username
; /* Username */
3021 int purge
; /* Purge? */
3022 cupsd_printer_t
*printer
; /* Printer */
3025 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3026 con
->http
.fd
, uri
->values
[0].string
.text
);
3029 * See if we have a printer URI...
3032 if (strcmp(uri
->name
, "printer-uri"))
3034 send_ipp_status(con
, IPP_BAD_REQUEST
,
3035 _("The printer-uri attribute is required!"));
3040 * Get the username (if any) for the jobs we want to cancel (only if
3041 * "my-jobs" is specified...
3044 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3045 IPP_TAG_BOOLEAN
)) != NULL
&&
3046 attr
->values
[0].boolean
)
3048 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3049 IPP_TAG_NAME
)) != NULL
)
3050 username
= attr
->values
[0].string
.text
;
3053 send_ipp_status(con
, IPP_BAD_REQUEST
,
3054 _("Missing requesting-user-name attribute!"));
3062 * Look for the "purge-jobs" attribute...
3065 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3066 IPP_TAG_BOOLEAN
)) != NULL
)
3067 purge
= attr
->values
[0].boolean
;
3072 * And if the destination is valid...
3075 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3081 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3082 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3083 hostname
, sizeof(hostname
), &port
,
3084 resource
, sizeof(resource
));
3086 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3087 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3089 send_ipp_status(con
, IPP_NOT_FOUND
,
3090 _("The printer or class was not found."));
3098 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3100 send_http_error(con
, status
, NULL
);
3105 * Cancel all jobs on all printers...
3108 cupsdCancelJobs(NULL
, username
, purge
);
3110 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3111 purge
? "purged" : "canceled", get_username(con
));
3119 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3122 send_http_error(con
, status
, printer
);
3127 * Cancel all of the jobs on the named printer...
3130 cupsdCancelJobs(printer
->name
, username
, purge
);
3132 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3133 printer
->name
, purge
? "purged" : "canceled",
3137 con
->response
->request
.status
.status_code
= IPP_OK
;
3142 * 'cancel_job()' - Cancel a print job.
3146 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3147 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3149 ipp_attribute_t
*attr
; /* Current attribute */
3150 int jobid
; /* Job ID */
3151 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3152 username
[HTTP_MAX_URI
], /* Username portion of URI */
3153 host
[HTTP_MAX_URI
], /* Host portion of URI */
3154 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3155 int port
; /* Port portion of URI */
3156 cupsd_job_t
*job
; /* Job information */
3157 cups_ptype_t dtype
; /* Destination type (printer/class) */
3158 cupsd_printer_t
*printer
; /* Printer data */
3159 int purge
; /* Purge the job? */
3162 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3163 con
->http
.fd
, uri
->values
[0].string
.text
);
3166 * See if we have a job URI or a printer URI...
3169 if (!strcmp(uri
->name
, "printer-uri"))
3172 * Got a printer URI; see if we also have a job-id attribute...
3175 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3176 IPP_TAG_INTEGER
)) == NULL
)
3178 send_ipp_status(con
, IPP_BAD_REQUEST
,
3179 _("Got a printer-uri attribute but no job-id!"));
3183 if ((jobid
= attr
->values
[0].integer
) == 0)
3186 * Find the current job on the specified printer...
3189 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3195 send_ipp_status(con
, IPP_NOT_FOUND
,
3196 _("The printer or class was not found."));
3201 * See if the printer is currently printing a job...
3205 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3209 * No, see if there are any pending jobs...
3212 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3214 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3215 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3216 !strcasecmp(job
->dest
, printer
->name
))
3223 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3225 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3226 if (job
->state_value
== IPP_JOB_STOPPED
&&
3227 !strcasecmp(job
->dest
, printer
->name
))
3234 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3245 * Got a job URI; parse it to get the job ID...
3248 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3249 sizeof(scheme
), username
, sizeof(username
), host
,
3250 sizeof(host
), &port
, resource
, sizeof(resource
));
3252 if (strncmp(resource
, "/jobs/", 6))
3258 send_ipp_status(con
, IPP_BAD_REQUEST
,
3259 _("Bad job-uri attribute \"%s\"!"),
3260 uri
->values
[0].string
.text
);
3264 jobid
= atoi(resource
+ 6);
3268 * Look for the "purge-job" attribute...
3271 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3272 IPP_TAG_BOOLEAN
)) != NULL
)
3273 purge
= attr
->values
[0].boolean
;
3278 * See if the job exists...
3281 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3284 * Nope - return a "not found" error...
3287 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3292 * See if the job is owned by the requesting user...
3295 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3297 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3302 * See if the job is already completed, canceled, or aborted; if so,
3303 * we can't cancel...
3306 if (job
->state_value
>= IPP_JOB_CANCELED
&& !purge
)
3308 switch (job
->state_value
)
3310 case IPP_JOB_CANCELED
:
3311 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3312 _("Job #%d is already canceled - can\'t cancel."),
3316 case IPP_JOB_ABORTED
:
3317 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3318 _("Job #%d is already aborted - can\'t cancel."),
3323 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3324 _("Job #%d is already completed - can\'t cancel."),
3333 * Cancel the job and return...
3336 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
3340 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
3343 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3346 con
->response
->request
.status
.status_code
= IPP_OK
;
3351 * 'cancel_subscription()' - Cancel a subscription.
3355 cancel_subscription(
3356 cupsd_client_t
*con
, /* I - Client connection */
3357 int sub_id
) /* I - Subscription ID */
3359 http_status_t status
; /* Policy status */
3360 cupsd_subscription_t
*sub
; /* Subscription */
3363 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3364 "cancel_subscription(con=%p[%d], sub_id=%d)",
3365 con
, con
->http
.fd
, sub_id
);
3368 * Is the subscription ID valid?
3371 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3374 * Bad subscription ID...
3377 send_ipp_status(con
, IPP_NOT_FOUND
,
3378 _("notify-subscription-id %d no good!"), sub_id
);
3386 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3388 con
, sub
->owner
)) != HTTP_OK
)
3390 send_http_error(con
, status
, sub
->dest
);
3395 * Cancel the subscription...
3398 cupsdDeleteSubscription(sub
, 1);
3400 con
->response
->request
.status
.status_code
= IPP_OK
;
3405 * 'check_quotas()' - Check quotas for a printer and user.
3408 static int /* O - 1 if OK, 0 if not */
3409 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3410 cupsd_printer_t
*p
) /* I - Printer or class */
3412 int i
; /* Looping var */
3413 char username
[33]; /* Username */
3414 cupsd_quota_t
*q
; /* Quota data */
3415 #ifdef HAVE_MBR_UID_TO_UUID
3417 * Use Apple membership APIs which require that all names represent
3418 * valid user account or group records accessible by the server.
3421 uuid_t usr_uuid
; /* UUID for job requesting user */
3422 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3423 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3424 int mbr_err
; /* Error from membership function */
3425 int is_member
; /* Is this user a member? */
3428 * Use standard POSIX APIs for checking users and groups...
3431 struct passwd
*pw
; /* User password data */
3432 #endif /* HAVE_MBR_UID_TO_UUID */
3435 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3436 con
, con
->http
.fd
, p
, p
->name
);
3439 * Figure out who is printing...
3442 strlcpy(username
, get_username(con
), sizeof(username
));
3445 * Check global active job limits for printers and users...
3448 if (MaxJobsPerPrinter
)
3451 * Check if there are too many pending jobs on this printer...
3454 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3456 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3465 * Check if there are too many pending jobs for this user...
3468 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3470 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3477 * Check against users...
3480 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3485 #ifdef HAVE_MBR_UID_TO_UUID
3487 * Get UUID for job requesting user...
3490 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3496 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3497 "check_quotas: UUID lookup failed for user \"%s\"",
3499 cupsdLogMessage(CUPSD_LOG_INFO
,
3500 "Denying user \"%s\" access to printer \"%s\" "
3501 "(unknown user)...",
3507 * Get UID and GID of requesting user...
3510 pw
= getpwnam(username
);
3512 #endif /* HAVE_MBR_UID_TO_UUID */
3514 for (i
= 0; i
< p
->num_users
; i
++)
3515 if (p
->users
[i
][0] == '@')
3518 * Check group membership...
3521 #ifdef HAVE_MBR_UID_TO_UUID
3522 if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
3526 * Invalid ACL entries are ignored for matching; just record a
3527 * warning in the log...
3530 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3531 "check_quotas: UUID lookup failed for ACL entry "
3532 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3533 cupsdLogMessage(CUPSD_LOG_WARN
,
3534 "Access control entry \"%s\" not a valid group name; "
3535 "entry ignored", p
->users
[i
]);
3539 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3543 * At this point, there should be no errors, but check anyways...
3546 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3547 "check_quotas: group \"%s\" membership check "
3548 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
3553 * Stop if we found a match...
3560 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3562 #endif /* HAVE_MBR_UID_TO_UUID */
3564 #ifdef HAVE_MBR_UID_TO_UUID
3567 if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
3571 * Invalid ACL entries are ignored for matching; just record a
3572 * warning in the log...
3575 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3576 "check_quotas: UUID lookup failed for ACL entry "
3577 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3578 cupsdLogMessage(CUPSD_LOG_WARN
,
3579 "Access control entry \"%s\" not a valid user name; "
3580 "entry ignored", p
->users
[i
]);
3584 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
3587 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3588 "check_quotas: User \"%s\" identity check failed "
3589 "(err=%d)", p
->users
[i
], mbr_err
);
3598 else if (!strcasecmp(username
, p
->users
[i
]))
3600 #endif /* HAVE_MBR_UID_TO_UUID */
3602 if ((i
< p
->num_users
) == p
->deny_users
)
3604 cupsdLogMessage(CUPSD_LOG_INFO
,
3605 "Denying user \"%s\" access to printer \"%s\"...",
3616 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
3619 * TODO: Define these special page count values as constants!
3622 if (q
->page_count
== -4) /* special case: unlimited user */
3624 cupsdLogMessage(CUPSD_LOG_INFO
,
3625 "User \"%s\" request approved for printer %s (%s): "
3627 username
, p
->name
, p
->info
);
3628 q
->page_count
= 0; /* allow user to print */
3631 else if (q
->page_count
== -3) /* quota exceeded */
3633 cupsdLogMessage(CUPSD_LOG_INFO
,
3634 "User \"%s\" request denied for printer %s (%s): "
3635 "quota limit exceeded.",
3636 username
, p
->name
, p
->info
);
3637 q
->page_count
= 2; /* force quota exceeded failure */
3640 else if (q
->page_count
== -2) /* quota disabled for user */
3642 cupsdLogMessage(CUPSD_LOG_INFO
,
3643 "User \"%s\" request denied for printer %s (%s): "
3644 "printing disabled for user.",
3645 username
, p
->name
, p
->info
);
3646 q
->page_count
= 2; /* force quota exceeded failure */
3649 else if (q
->page_count
== -1) /* quota access error */
3651 cupsdLogMessage(CUPSD_LOG_INFO
,
3652 "User \"%s\" request denied for printer %s (%s): "
3653 "unable to determine quota limit.",
3654 username
, p
->name
, p
->info
);
3655 q
->page_count
= 2; /* force quota exceeded failure */
3658 else if (q
->page_count
< 0) /* user not found or other error */
3660 cupsdLogMessage(CUPSD_LOG_INFO
,
3661 "User \"%s\" request denied for printer %s (%s): "
3662 "user disabled / missing quota.",
3663 username
, p
->name
, p
->info
);
3664 q
->page_count
= 2; /* force quota exceeded failure */
3667 else /* page within user limits */
3669 q
->page_count
= 0; /* allow user to print */
3674 #endif /* __APPLE__ */
3675 if (p
->k_limit
|| p
->page_limit
)
3677 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3679 cupsdLogMessage(CUPSD_LOG_ERROR
,
3680 "Unable to allocate quota data for user \"%s\"!",
3685 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3686 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3688 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3695 * If we have gotten this far, we're done!
3703 * 'copy_attribute()' - Copy a single attribute.
3706 static ipp_attribute_t
* /* O - New attribute */
3708 ipp_t
*to
, /* O - Destination request/response */
3709 ipp_attribute_t
*attr
, /* I - Attribute to copy */
3710 int quickcopy
) /* I - Do a quick copy? */
3712 int i
; /* Looping var */
3713 ipp_attribute_t
*toattr
; /* Destination attribute */
3716 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3717 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
3718 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
3721 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
3724 toattr
= ippAddSeparator(to
);
3727 case IPP_TAG_INTEGER
:
3729 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3730 attr
->name
, attr
->num_values
, NULL
);
3732 for (i
= 0; i
< attr
->num_values
; i
++)
3733 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
3736 case IPP_TAG_BOOLEAN
:
3737 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
3738 attr
->num_values
, NULL
);
3740 for (i
= 0; i
< attr
->num_values
; i
++)
3741 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
3744 case IPP_TAG_STRING
:
3747 case IPP_TAG_KEYWORD
:
3749 case IPP_TAG_URISCHEME
:
3750 case IPP_TAG_CHARSET
:
3751 case IPP_TAG_LANGUAGE
:
3752 case IPP_TAG_MIMETYPE
:
3753 toattr
= ippAddStrings(to
, attr
->group_tag
,
3754 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3755 attr
->name
, attr
->num_values
, NULL
, NULL
);
3759 for (i
= 0; i
< attr
->num_values
; i
++)
3760 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3764 for (i
= 0; i
< attr
->num_values
; i
++)
3765 toattr
->values
[i
].string
.text
=
3766 _cupsStrAlloc(attr
->values
[i
].string
.text
);
3771 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
3772 attr
->values
[0].date
);
3775 case IPP_TAG_RESOLUTION
:
3776 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
3777 attr
->num_values
, IPP_RES_PER_INCH
,
3780 for (i
= 0; i
< attr
->num_values
; i
++)
3782 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
3783 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
3784 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
3788 case IPP_TAG_RANGE
:
3789 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
3790 attr
->num_values
, NULL
, NULL
);
3792 for (i
= 0; i
< attr
->num_values
; i
++)
3794 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
3795 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
3799 case IPP_TAG_TEXTLANG
:
3800 case IPP_TAG_NAMELANG
:
3801 toattr
= ippAddStrings(to
, attr
->group_tag
,
3802 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3803 attr
->name
, attr
->num_values
, NULL
, NULL
);
3807 for (i
= 0; i
< attr
->num_values
; i
++)
3809 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
3810 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3815 for (i
= 0; i
< attr
->num_values
; i
++)
3818 toattr
->values
[i
].string
.charset
=
3819 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
3821 toattr
->values
[i
].string
.charset
=
3822 toattr
->values
[0].string
.charset
;
3824 toattr
->values
[i
].string
.text
=
3825 _cupsStrAlloc(attr
->values
[i
].string
.text
);
3830 case IPP_TAG_BEGIN_COLLECTION
:
3831 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
3832 attr
->num_values
, NULL
);
3834 for (i
= 0; i
< attr
->num_values
; i
++)
3836 toattr
->values
[i
].collection
= ippNew();
3837 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
3838 NULL
, IPP_TAG_ZERO
, 0);
3843 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3844 attr
->name
, attr
->num_values
, NULL
);
3846 for (i
= 0; i
< attr
->num_values
; i
++)
3848 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
3850 if (toattr
->values
[i
].unknown
.length
> 0)
3852 if ((toattr
->values
[i
].unknown
.data
=
3853 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
3854 toattr
->values
[i
].unknown
.length
= 0;
3856 memcpy(toattr
->values
[i
].unknown
.data
,
3857 attr
->values
[i
].unknown
.data
,
3858 toattr
->values
[i
].unknown
.length
);
3861 break; /* anti-compiler-warning-code */
3869 * 'copy_attrs()' - Copy attributes from one request to another.
3873 copy_attrs(ipp_t
*to
, /* I - Destination request */
3874 ipp_t
*from
, /* I - Source request */
3875 cups_array_t
*ra
, /* I - Requested attributes */
3876 ipp_tag_t group
, /* I - Group to copy */
3877 int quickcopy
) /* I - Do a quick copy? */
3879 ipp_attribute_t
*fromattr
; /* Source attribute */
3882 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3883 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3884 to
, from
, ra
, group
, quickcopy
);
3889 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3892 * Filter attributes as needed...
3895 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3896 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
3899 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
3900 copy_attribute(to
, fromattr
, quickcopy
);
3906 * 'copy_banner()' - Copy a banner file to the requests directory for the
3910 static int /* O - Size of banner file in kbytes */
3911 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
3912 cupsd_job_t
*job
, /* I - Job information */
3913 const char *name
) /* I - Name of banner */
3915 int i
; /* Looping var */
3916 int kbytes
; /* Size of banner file in kbytes */
3917 char filename
[1024]; /* Job filename */
3918 cupsd_banner_t
*banner
; /* Pointer to banner */
3919 cups_file_t
*in
; /* Input file */
3920 cups_file_t
*out
; /* Output file */
3921 int ch
; /* Character from file */
3922 char attrname
[255], /* Name of attribute */
3923 *s
; /* Pointer into name */
3924 ipp_attribute_t
*attr
; /* Attribute */
3927 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
3928 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
3929 name
? name
: "(null)");
3932 * Find the banner; return if not found or "none"...
3935 if (!name
|| !strcmp(name
, "none") ||
3936 (banner
= cupsdFindBanner(name
)) == NULL
)
3940 * Open the banner and job files...
3943 if (add_file(con
, job
, banner
->filetype
, 0))
3946 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
3948 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
3950 cupsdLogMessage(CUPSD_LOG_ERROR
,
3951 "copy_banner: Unable to create banner job file %s - %s",
3952 filename
, strerror(errno
));
3957 fchmod(cupsFileNumber(out
), 0640);
3958 fchown(cupsFileNumber(out
), RunUser
, Group
);
3961 * Try the localized banner file under the subdirectory...
3964 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
3966 if (strlen(attrname
) > 2 && attrname
[2] == '-')
3969 * Convert ll-cc to ll_CC...
3973 attrname
[3] = toupper(attrname
[3] & 255);
3974 attrname
[4] = toupper(attrname
[4] & 255);
3977 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3980 if (access(filename
, 0) && strlen(attrname
) > 2)
3983 * Wasn't able to find "ll_CC" locale file; try the non-national
3984 * localization banner directory.
3989 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3993 if (access(filename
, 0))
3996 * Use the non-localized banner file.
3999 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4002 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4006 cupsdLogMessage(CUPSD_LOG_ERROR
,
4007 "copy_banner: Unable to open banner template file %s - %s",
4008 filename
, strerror(errno
));
4014 * Parse the file to the end...
4017 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4021 * Get an attribute name...
4024 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4025 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4027 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4037 * Ignore { followed by stuff that is not an attribute name...
4040 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4045 * See if it is defined...
4048 if (attrname
[0] == '?')
4053 if (!strcmp(s
, "printer-name"))
4055 cupsFilePuts(out
, job
->dest
);
4058 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4061 * See if we have a leading question mark...
4064 if (attrname
[0] != '?')
4067 * Nope, write to file as-is; probably a PostScript procedure...
4070 cupsFilePrintf(out
, "{%s}", attrname
);
4077 * Output value(s)...
4080 for (i
= 0; i
< attr
->num_values
; i
++)
4083 cupsFilePutChar(out
, ',');
4085 switch (attr
->value_tag
)
4087 case IPP_TAG_INTEGER
:
4089 if (!strncmp(s
, "time-at-", 8))
4090 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
4092 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4095 case IPP_TAG_BOOLEAN
:
4096 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4099 case IPP_TAG_NOVALUE
:
4100 cupsFilePuts(out
, "novalue");
4103 case IPP_TAG_RANGE
:
4104 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4105 attr
->values
[i
].range
.upper
);
4108 case IPP_TAG_RESOLUTION
:
4109 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4110 attr
->values
[i
].resolution
.yres
,
4111 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4116 case IPP_TAG_STRING
:
4119 case IPP_TAG_KEYWORD
:
4120 case IPP_TAG_CHARSET
:
4121 case IPP_TAG_LANGUAGE
:
4122 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4125 * Need to quote strings for PS banners...
4130 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4132 if (*p
== '(' || *p
== ')' || *p
== '\\')
4134 cupsFilePutChar(out
, '\\');
4135 cupsFilePutChar(out
, *p
);
4137 else if (*p
< 32 || *p
> 126)
4138 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4140 cupsFilePutChar(out
, *p
);
4144 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4148 break; /* anti-compiler-warning-code */
4152 else if (ch
== '\\') /* Quoted char */
4154 ch
= cupsFileGetChar(in
);
4156 if (ch
!= '{') /* Only do special handling for \{ */
4157 cupsFilePutChar(out
, '\\');
4159 cupsFilePutChar(out
, ch
);
4162 cupsFilePutChar(out
, ch
);
4166 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4168 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4169 IPP_TAG_INTEGER
)) != NULL
)
4170 attr
->values
[0].integer
+= kbytes
;
4179 * 'copy_file()' - Copy a PPD file or interface script...
4182 static int /* O - 0 = success, -1 = error */
4183 copy_file(const char *from
, /* I - Source file */
4184 const char *to
) /* I - Destination file */
4186 cups_file_t
*src
, /* Source file */
4187 *dst
; /* Destination file */
4188 int bytes
; /* Bytes to read/write */
4189 char buffer
[2048]; /* Copy buffer */
4192 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4195 * Open the source and destination file for a copy...
4198 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4201 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4208 * Copy the source file to the destination...
4211 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4212 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
4220 * Close both files and return...
4225 return (cupsFileClose(dst
));
4230 * 'copy_model()' - Copy a PPD model file, substituting default values
4234 static int /* O - 0 = success, -1 = error */
4235 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4236 const char *from
, /* I - Source file */
4237 const char *to
) /* I - Destination file */
4239 fd_set input
; /* select() input set */
4240 struct timeval timeout
; /* select() timeout */
4241 int maxfd
; /* Max file descriptor for select() */
4242 char tempfile
[1024]; /* Temporary PPD file */
4243 int tempfd
; /* Temporary PPD file descriptor */
4244 int temppid
; /* Process ID of cups-driverd */
4245 int temppipe
[2]; /* Temporary pipes */
4246 char *argv
[4], /* Command-line arguments */
4247 *envp
[MAX_ENV
]; /* Environment */
4248 cups_file_t
*src
, /* Source file */
4249 *dst
; /* Destination file */
4250 ppd_file_t
*ppd
; /* PPD file */
4251 int bytes
, /* Bytes from pipe */
4252 total
; /* Total bytes from pipe */
4253 char buffer
[2048]; /* Copy buffer */
4254 int i
; /* Looping var */
4255 char option
[PPD_MAX_NAME
], /* Option name */
4256 choice
[PPD_MAX_NAME
]; /* Choice name */
4257 int num_defaults
; /* Number of default options */
4258 cups_option_t
*defaults
; /* Default options */
4259 char cups_protocol
[PPD_MAX_LINE
];
4260 /* cupsProtocol attribute */
4261 int have_letter
, /* Have Letter size */
4262 have_a4
; /* Have A4 size */
4263 #ifdef HAVE_LIBPAPER
4264 char *paper_result
; /* Paper size name from libpaper */
4265 char system_paper
[64]; /* Paper size name buffer */
4266 #endif /* HAVE_LIBPAPER */
4269 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4270 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4274 * Run cups-driverd to get the PPD file...
4277 argv
[0] = "cups-driverd";
4279 argv
[2] = (char *)from
;
4282 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4284 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4285 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
4286 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4290 cupsdOpenPipe(temppipe
);
4292 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4293 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4295 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4296 -1, -1, 0, DefaultProfile
, &temppid
))
4307 * Wait up to 30 seconds for the PPD file to be copied...
4312 if (temppipe
[0] > CGIPipes
[0])
4313 maxfd
= temppipe
[0] + 1;
4315 maxfd
= CGIPipes
[0] + 1;
4320 * See if we have data ready...
4326 FD_SET(temppipe
[0], &input
);
4327 FD_SET(CGIPipes
[0], &input
);
4329 timeout
.tv_sec
= 30;
4330 timeout
.tv_usec
= 0;
4332 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4342 * We have timed out...
4348 if (FD_ISSET(temppipe
[0], &input
))
4351 * Read the PPD file from the pipe, and write it to the PPD file.
4354 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4356 if (write(tempfd
, buffer
, bytes
) < bytes
)
4365 if (FD_ISSET(CGIPipes
[0], &input
))
4375 * No data from cups-deviced...
4378 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
4384 * Read the source file and see what page sizes are supported...
4387 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
4393 have_letter
= ppdPageSize(ppd
, "Letter") != NULL
;
4394 have_a4
= ppdPageSize(ppd
, "A4") != NULL
;
4397 * Open the destination (if possible) and set the default options...
4402 cups_protocol
[0] = '\0';
4404 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4407 * Read all of the default lines from the old PPD...
4410 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4411 if (!strncmp(buffer
, "*Default", 8))
4414 * Add the default option...
4417 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4418 choice
, sizeof(choice
)))
4420 ppd_option_t
*ppdo
; /* PPD option */
4424 * Only add the default if the default hasn't already been
4425 * set and the choice exists in the new PPD...
4428 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4429 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4430 ppdFindChoice(ppdo
, choice
))
4431 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4435 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4436 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4440 #ifdef HAVE_LIBPAPER
4441 else if ((paper_result
= systempapername()) != NULL
)
4444 * Set the default media sizes from the systemwide default...
4447 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
4448 system_paper
[0] = toupper(system_paper
[0] & 255);
4450 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
4451 (!strcmp(system_paper
, "A4") && have_a4
))
4453 num_defaults
= cupsAddOption("PageSize", system_paper
,
4454 num_defaults
, &defaults
);
4455 num_defaults
= cupsAddOption("PageRegion", system_paper
,
4456 num_defaults
, &defaults
);
4457 num_defaults
= cupsAddOption("PaperDimension", system_paper
,
4458 num_defaults
, &defaults
);
4459 num_defaults
= cupsAddOption("ImageableArea", system_paper
,
4460 num_defaults
, &defaults
);
4463 #endif /* HAVE_LIBPAPER */
4467 * Add the default media sizes...
4469 * Note: These values are generally not valid for large-format devices
4470 * like plotters, however it is probably safe to say that those
4471 * users will configure the media size after initially adding
4472 * the device anyways...
4475 if (!DefaultLanguage
||
4476 !strcasecmp(DefaultLanguage
, "C") ||
4477 !strcasecmp(DefaultLanguage
, "POSIX") ||
4478 !strcasecmp(DefaultLanguage
, "en") ||
4479 !strncasecmp(DefaultLanguage
, "en.", 3) ||
4480 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4481 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4482 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4485 * These are the only locales that will default to "letter" size...
4490 num_defaults
= cupsAddOption("PageSize", "Letter", num_defaults
,
4492 num_defaults
= cupsAddOption("PageRegion", "Letter", num_defaults
,
4494 num_defaults
= cupsAddOption("PaperDimension", "Letter", num_defaults
,
4496 num_defaults
= cupsAddOption("ImageableArea", "Letter", num_defaults
,
4503 * The rest default to "a4" size...
4506 num_defaults
= cupsAddOption("PageSize", "A4", num_defaults
,
4508 num_defaults
= cupsAddOption("PageRegion", "A4", num_defaults
,
4510 num_defaults
= cupsAddOption("PaperDimension", "A4", num_defaults
,
4512 num_defaults
= cupsAddOption("ImageableArea", "A4", num_defaults
,
4520 * Open the source file for a copy...
4523 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4525 cupsFreeOptions(num_defaults
, defaults
);
4531 * Open the destination file for a copy...
4534 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4536 cupsFreeOptions(num_defaults
, defaults
);
4543 * Copy the source file to the destination...
4546 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4548 if (!strncmp(buffer
, "*Default", 8))
4551 * Check for an previous default option choice...
4554 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4555 choice
, sizeof(choice
)))
4557 const char *val
; /* Default option value */
4560 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4563 * Substitute the previous choice...
4566 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4571 cupsFilePrintf(dst
, "%s\n", buffer
);
4574 if (cups_protocol
[0])
4575 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4577 cupsFreeOptions(num_defaults
, defaults
);
4580 * Close both files and return...
4587 return (cupsFileClose(dst
));
4592 * 'copy_job_attrs()' - Copy job attributes.
4596 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4597 cupsd_job_t
*job
, /* I - Job */
4598 cups_array_t
*ra
) /* I - Requested attributes array */
4600 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4604 * Send the requested attributes for each job...
4607 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4608 con
->servername
, con
->serverport
, "/jobs/%d",
4611 if (!ra
|| cupsArrayFind(ra
, "document-count"))
4612 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4613 "document-count", job
->num_files
);
4615 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
4616 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4617 "job-more-info", NULL
, job_uri
);
4619 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4620 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4621 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4622 job
->num_files
> 0);
4624 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
4625 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4626 "job-printer-up-time", time(NULL
));
4628 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4629 add_job_state_reasons(con
, job
);
4631 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4632 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4633 "job-uri", NULL
, job_uri
);
4635 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
4640 * 'copy_printer_attrs()' - Copy printer attributes.
4645 cupsd_client_t
*con
, /* I - Client connection */
4646 cupsd_printer_t
*printer
, /* I - Printer */
4647 cups_array_t
*ra
) /* I - Requested attributes array */
4649 char printer_uri
[HTTP_MAX_URI
];
4651 time_t curtime
; /* Current time */
4652 int i
; /* Looping var */
4653 ipp_attribute_t
*history
; /* History collection */
4657 * Copy the printer attributes to the response using requested-attributes
4658 * and document-format attributes that may be provided by the client.
4661 curtime
= time(NULL
);
4664 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
4665 printer
->recoverable
)
4666 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4667 "com.apple.print.recoverable-message", NULL
,
4668 printer
->recoverable
);
4669 #endif /* __APPLE__ */
4671 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4672 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
4673 "printer-alert", NULL
, printer
->alert
);
4675 if (printer
->alert_description
&&
4676 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4677 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4678 "printer-alert-description", NULL
,
4679 printer
->alert_description
);
4681 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4682 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4683 ippTimeToDate(curtime
));
4685 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4686 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4687 "printer-error-policy", NULL
, printer
->error_policy
);
4689 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4690 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
4691 printer
->accepting
);
4693 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4694 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
4697 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4698 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4699 "printer-op-policy", NULL
, printer
->op_policy
);
4701 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4702 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4705 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4706 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4707 "printer-state-change-time", printer
->state_time
);
4709 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
4710 cupsArrayFind(ra
, "printer-state-history"))
4713 * Printer history is only sent if specifically requested, so that
4714 * older CUPS/IPP clients won't barf on the collection attributes.
4717 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
4718 "printer-state-history",
4719 printer
->num_history
, NULL
);
4721 for (i
= 0; i
< printer
->num_history
; i
++)
4722 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
4723 NULL
, IPP_TAG_ZERO
, 0);
4726 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4727 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4728 "printer-state-message", NULL
, printer
->state_message
);
4730 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4731 add_printer_state_reasons(con
, printer
);
4733 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4735 int type
; /* printer-type value */
4739 * Add the CUPS-specific printer-type attribute...
4742 type
= printer
->type
;
4744 if (printer
== DefaultPrinter
)
4745 type
|= CUPS_PRINTER_DEFAULT
;
4747 if (!printer
->accepting
)
4748 type
|= CUPS_PRINTER_REJECTING
;
4750 if (!printer
->shared
)
4751 type
|= CUPS_PRINTER_NOT_SHARED
;
4753 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
4757 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4758 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4759 "printer-up-time", curtime
);
4761 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
4762 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
4765 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4766 "ipp", NULL
, con
->servername
, con
->serverport
,
4767 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4768 "/classes/%s" : "/printers/%s", printer
->name
);
4769 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4770 "printer-uri-supported", NULL
, printer_uri
);
4771 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4775 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4776 add_queued_job_count(con
, printer
);
4778 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
4779 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
4784 * 'copy_subscription_attrs()' - Copy subscription attributes.
4788 copy_subscription_attrs(
4789 cupsd_client_t
*con
, /* I - Client connection */
4790 cupsd_subscription_t
*sub
, /* I - Subscription */
4791 cups_array_t
*ra
) /* I - Requested attributes array */
4793 ipp_attribute_t
*attr
; /* Current attribute */
4794 char printer_uri
[HTTP_MAX_URI
];
4796 int count
; /* Number of events */
4797 unsigned mask
; /* Current event mask */
4798 const char *name
; /* Current event name */
4802 * Copy the subscription attributes to the response using the
4803 * requested-attributes attribute that may be provided by the client.
4806 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
4808 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
4811 * Simple event list...
4814 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
4815 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
4816 "notify-events", NULL
, name
);
4821 * Complex event list...
4824 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4825 if (sub
->mask
& mask
)
4828 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
4829 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
4830 "notify-events", count
, NULL
, NULL
);
4832 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4833 if (sub
->mask
& mask
)
4835 attr
->values
[count
].string
.text
=
4836 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
4843 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
4844 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4845 "notify-job-id", sub
->job
->id
);
4847 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
4848 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4849 "notify-lease-duration", sub
->lease
);
4851 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
4853 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4854 "ipp", NULL
, con
->servername
, con
->serverport
,
4855 "/printers/%s", sub
->dest
->name
);
4856 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4857 "notify-printer-uri", NULL
, printer_uri
);
4860 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
4861 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4862 "notify-recipient-uri", NULL
, sub
->recipient
);
4863 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
4864 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
4865 "notify-pull-method", NULL
, "ippget");
4867 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
4868 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
4869 "notify-subscriber-user-name", NULL
, sub
->owner
);
4871 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
4872 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4873 "notify-subscription-id", sub
->id
);
4875 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
4876 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4877 "notify-time-interval", sub
->interval
);
4879 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
4880 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
4881 sub
->user_data
, sub
->user_data_len
);
4886 * 'create_job()' - Print a file to a printer or class.
4890 create_job(cupsd_client_t
*con
, /* I - Client connection */
4891 ipp_attribute_t
*uri
) /* I - Printer URI */
4893 cupsd_printer_t
*printer
; /* Printer */
4894 cupsd_job_t
*job
; /* New job */
4897 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
4898 con
->http
.fd
, uri
->values
[0].string
.text
);
4901 * Is the destination valid?
4904 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
4910 send_ipp_status(con
, IPP_NOT_FOUND
,
4911 _("The printer or class was not found."));
4916 * Create the job object...
4919 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
4922 job
->pending_timeout
= 1;
4925 * Save and log the job...
4930 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
4931 job
->id
, job
->dest
, job
->username
);
4936 * 'create_requested_array()' - Create an array for the requested-attributes.
4939 static cups_array_t
* /* O - Array of attributes or NULL */
4940 create_requested_array(ipp_t
*request
) /* I - IPP request */
4942 int i
; /* Looping var */
4943 ipp_attribute_t
*requested
; /* requested-attributes attribute */
4944 cups_array_t
*ra
; /* Requested attributes array */
4945 char *value
; /* Current value */
4949 * Get the requested-attributes attribute, and return NULL if we don't
4953 if ((requested
= ippFindAttribute(request
, "requested-attributes",
4954 IPP_TAG_KEYWORD
)) == NULL
)
4958 * If the attribute contains a single "all" keyword, return NULL...
4961 if (requested
->num_values
== 1 &&
4962 !strcmp(requested
->values
[0].string
.text
, "all"))
4966 * Create an array using "strcmp" as the comparison function...
4969 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
4971 for (i
= 0; i
< requested
->num_values
; i
++)
4973 value
= requested
->values
[i
].string
.text
;
4975 if (!strcmp(value
, "job-template"))
4977 cupsArrayAdd(ra
, "copies");
4978 cupsArrayAdd(ra
, "copies-default");
4979 cupsArrayAdd(ra
, "copies-supported");
4980 cupsArrayAdd(ra
, "finishings");
4981 cupsArrayAdd(ra
, "finishings-default");
4982 cupsArrayAdd(ra
, "finishings-supported");
4983 cupsArrayAdd(ra
, "job-hold-until");
4984 cupsArrayAdd(ra
, "job-hold-until-default");
4985 cupsArrayAdd(ra
, "job-hold-until-supported");
4986 cupsArrayAdd(ra
, "job-priority");
4987 cupsArrayAdd(ra
, "job-priority-default");
4988 cupsArrayAdd(ra
, "job-priority-supported");
4989 cupsArrayAdd(ra
, "job-sheets");
4990 cupsArrayAdd(ra
, "job-sheets-default");
4991 cupsArrayAdd(ra
, "job-sheets-supported");
4992 cupsArrayAdd(ra
, "media");
4993 cupsArrayAdd(ra
, "media-default");
4994 cupsArrayAdd(ra
, "media-supported");
4995 cupsArrayAdd(ra
, "multiple-document-handling");
4996 cupsArrayAdd(ra
, "multiple-document-handling-default");
4997 cupsArrayAdd(ra
, "multiple-document-handling-supported");
4998 cupsArrayAdd(ra
, "number-up");
4999 cupsArrayAdd(ra
, "number-up-default");
5000 cupsArrayAdd(ra
, "number-up-supported");
5001 cupsArrayAdd(ra
, "orientation-requested");
5002 cupsArrayAdd(ra
, "orientation-requested-default");
5003 cupsArrayAdd(ra
, "orientation-requested-supported");
5004 cupsArrayAdd(ra
, "page-ranges");
5005 cupsArrayAdd(ra
, "page-ranges-supported");
5006 cupsArrayAdd(ra
, "printer-resolution");
5007 cupsArrayAdd(ra
, "printer-resolution-default");
5008 cupsArrayAdd(ra
, "printer-resolution-supported");
5009 cupsArrayAdd(ra
, "print-quality");
5010 cupsArrayAdd(ra
, "print-quality-default");
5011 cupsArrayAdd(ra
, "print-quality-supported");
5012 cupsArrayAdd(ra
, "sides");
5013 cupsArrayAdd(ra
, "sides-default");
5014 cupsArrayAdd(ra
, "sides-supported");
5016 else if (!strcmp(value
, "job-description"))
5018 cupsArrayAdd(ra
, "date-time-at-completed");
5019 cupsArrayAdd(ra
, "date-time-at-creation");
5020 cupsArrayAdd(ra
, "date-time-at-processing");
5021 cupsArrayAdd(ra
, "job-detailed-status-message");
5022 cupsArrayAdd(ra
, "job-document-access-errors");
5023 cupsArrayAdd(ra
, "job-id");
5024 cupsArrayAdd(ra
, "job-impressions");
5025 cupsArrayAdd(ra
, "job-impressions-completed");
5026 cupsArrayAdd(ra
, "job-k-octets");
5027 cupsArrayAdd(ra
, "job-k-octets-processed");
5028 cupsArrayAdd(ra
, "job-media-sheets");
5029 cupsArrayAdd(ra
, "job-media-sheets-completed");
5030 cupsArrayAdd(ra
, "job-message-from-operator");
5031 cupsArrayAdd(ra
, "job-more-info");
5032 cupsArrayAdd(ra
, "job-name");
5033 cupsArrayAdd(ra
, "job-originating-user-name");
5034 cupsArrayAdd(ra
, "job-printer-up-time");
5035 cupsArrayAdd(ra
, "job-printer-uri");
5036 cupsArrayAdd(ra
, "job-state");
5037 cupsArrayAdd(ra
, "job-state-message");
5038 cupsArrayAdd(ra
, "job-state-reasons");
5039 cupsArrayAdd(ra
, "job-uri");
5040 cupsArrayAdd(ra
, "number-of-documents");
5041 cupsArrayAdd(ra
, "number-of-intervening-jobs");
5042 cupsArrayAdd(ra
, "output-device-assigned");
5043 cupsArrayAdd(ra
, "time-at-completed");
5044 cupsArrayAdd(ra
, "time-at-creation");
5045 cupsArrayAdd(ra
, "time-at-processing");
5047 else if (!strcmp(value
, "printer-description"))
5049 cupsArrayAdd(ra
, "charset-configured");
5050 cupsArrayAdd(ra
, "charset-supported");
5051 cupsArrayAdd(ra
, "color-supported");
5052 cupsArrayAdd(ra
, "compression-supported");
5053 cupsArrayAdd(ra
, "document-format-default");
5054 cupsArrayAdd(ra
, "document-format-supported");
5055 cupsArrayAdd(ra
, "generated-natural-language-supported");
5056 cupsArrayAdd(ra
, "ipp-versions-supported");
5057 cupsArrayAdd(ra
, "job-impressions-supported");
5058 cupsArrayAdd(ra
, "job-k-octets-supported");
5059 cupsArrayAdd(ra
, "job-media-sheets-supported");
5060 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
5061 cupsArrayAdd(ra
, "multiple-operation-time-out");
5062 cupsArrayAdd(ra
, "natural-language-configured");
5063 cupsArrayAdd(ra
, "notify-attributes-supported");
5064 cupsArrayAdd(ra
, "notify-lease-duration-default");
5065 cupsArrayAdd(ra
, "notify-lease-duration-supported");
5066 cupsArrayAdd(ra
, "notify-max-events-supported");
5067 cupsArrayAdd(ra
, "notify-events-default");
5068 cupsArrayAdd(ra
, "notify-events-supported");
5069 cupsArrayAdd(ra
, "notify-pull-method-supported");
5070 cupsArrayAdd(ra
, "notify-schemes-supported");
5071 cupsArrayAdd(ra
, "operations-supported");
5072 cupsArrayAdd(ra
, "pages-per-minute");
5073 cupsArrayAdd(ra
, "pages-per-minute-color");
5074 cupsArrayAdd(ra
, "pdl-override-supported");
5075 cupsArrayAdd(ra
, "printer-alert");
5076 cupsArrayAdd(ra
, "printer-alert-description");
5077 cupsArrayAdd(ra
, "printer-current-time");
5078 cupsArrayAdd(ra
, "printer-driver-installer");
5079 cupsArrayAdd(ra
, "printer-info");
5080 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
5081 cupsArrayAdd(ra
, "printer-location");
5082 cupsArrayAdd(ra
, "printer-make-and-model");
5083 cupsArrayAdd(ra
, "printer-message-from-operator");
5084 cupsArrayAdd(ra
, "printer-more-info");
5085 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
5086 cupsArrayAdd(ra
, "printer-name");
5087 cupsArrayAdd(ra
, "printer-state");
5088 cupsArrayAdd(ra
, "printer-state-message");
5089 cupsArrayAdd(ra
, "printer-state-reasons");
5090 cupsArrayAdd(ra
, "printer-up-time");
5091 cupsArrayAdd(ra
, "printer-uri-supported");
5092 cupsArrayAdd(ra
, "queued-job-count");
5093 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
5094 cupsArrayAdd(ra
, "uri-authentication-supported");
5095 cupsArrayAdd(ra
, "uri-security-supported");
5097 else if (!strcmp(value
, "printer-defaults"))
5099 char *name
; /* Option name */
5102 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5104 name
= (char *)cupsArrayNext(CommonDefaults
))
5105 cupsArrayAdd(ra
, name
);
5107 else if (!strcmp(value
, "subscription-template"))
5109 cupsArrayAdd(ra
, "notify-attributes");
5110 cupsArrayAdd(ra
, "notify-charset");
5111 cupsArrayAdd(ra
, "notify-events");
5112 cupsArrayAdd(ra
, "notify-lease-duration");
5113 cupsArrayAdd(ra
, "notify-natural-language");
5114 cupsArrayAdd(ra
, "notify-pull-method");
5115 cupsArrayAdd(ra
, "notify-recipient-uri");
5116 cupsArrayAdd(ra
, "notify-time-interval");
5117 cupsArrayAdd(ra
, "notify-user-data");
5120 cupsArrayAdd(ra
, value
);
5128 * 'create_subscription()' - Create a notification subscription.
5132 create_subscription(
5133 cupsd_client_t
*con
, /* I - Client connection */
5134 ipp_attribute_t
*uri
) /* I - Printer URI */
5136 http_status_t status
; /* Policy status */
5137 int i
; /* Looping var */
5138 ipp_attribute_t
*attr
; /* Current attribute */
5139 cups_ptype_t dtype
; /* Destination type (printer/class) */
5140 char scheme
[HTTP_MAX_URI
],
5141 /* Scheme portion of URI */
5142 userpass
[HTTP_MAX_URI
],
5143 /* Username portion of URI */
5145 /* Host portion of URI */
5146 resource
[HTTP_MAX_URI
];
5147 /* Resource portion of URI */
5148 int port
; /* Port portion of URI */
5149 cupsd_printer_t
*printer
; /* Printer/class */
5150 cupsd_job_t
*job
; /* Job */
5151 int jobid
; /* Job ID */
5152 cupsd_subscription_t
*sub
; /* Subscription object */
5153 const char *username
, /* requesting-user-name or
5154 authenticated username */
5155 *recipient
, /* notify-recipient-uri */
5156 *pullmethod
; /* notify-pull-method */
5157 ipp_attribute_t
*user_data
; /* notify-user-data */
5158 int interval
, /* notify-time-interval */
5159 lease
; /* notify-lease-duration */
5160 unsigned mask
; /* notify-events */
5161 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5162 *notify_lease
; /* notify-lease-duration(-default) */
5166 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5168 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5169 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
5170 attr
->value_tag
, attr
->name
);
5172 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
5177 * Is the destination valid?
5180 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5181 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5182 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5184 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5185 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5186 sizeof(host
), &port
, resource
, sizeof(resource
));
5188 if (!strcmp(resource
, "/"))
5190 dtype
= (cups_ptype_t
)0;
5193 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5195 dtype
= (cups_ptype_t
)0;
5198 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5200 dtype
= CUPS_PRINTER_CLASS
;
5203 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5209 send_ipp_status(con
, IPP_NOT_FOUND
,
5210 _("The printer or class was not found."));
5220 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5223 send_http_error(con
, status
, printer
);
5227 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5229 send_http_error(con
, status
, NULL
);
5234 * Get the user that is requesting the subscription...
5237 username
= get_username(con
);
5240 * Find the first subscription group attribute; return if we have
5244 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5245 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5250 send_ipp_status(con
, IPP_BAD_REQUEST
,
5251 _("No subscription attributes in request!"));
5256 * Process the subscription attributes in the request...
5259 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5267 lease
= DefaultLeaseDuration
;
5269 mask
= CUPSD_EVENT_NONE
;
5273 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5275 notify_lease
= ippFindAttribute(printer
->attrs
,
5276 "notify-lease-duration-default",
5280 lease
= notify_lease
->values
[0].integer
;
5284 notify_events
= NULL
;
5285 notify_lease
= NULL
;
5288 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5290 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5291 attr
->value_tag
== IPP_TAG_URI
)
5294 * Validate the recipient scheme against the ServerBin/notifier
5298 char notifier
[1024]; /* Notifier filename */
5301 recipient
= attr
->values
[0].string
.text
;
5303 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5304 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5305 host
, sizeof(host
), &port
,
5306 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5308 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5309 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
5310 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5311 "notify-status-code", IPP_URI_SCHEME
);
5315 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5317 if (access(notifier
, X_OK
))
5319 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5320 _("notify-recipient-uri URI \"%s\" uses unknown "
5321 "scheme!"), recipient
);
5322 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5323 "notify-status-code", IPP_URI_SCHEME
);
5327 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5328 attr
->value_tag
== IPP_TAG_KEYWORD
)
5330 pullmethod
= attr
->values
[0].string
.text
;
5332 if (strcmp(pullmethod
, "ippget"))
5334 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5335 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
5336 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5337 "notify-status-code", IPP_ATTRIBUTES
);
5341 else if (!strcmp(attr
->name
, "notify-charset") &&
5342 attr
->value_tag
== IPP_TAG_CHARSET
&&
5343 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5344 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5346 send_ipp_status(con
, IPP_CHARSET
,
5347 _("Character set \"%s\" not supported!"),
5348 attr
->values
[0].string
.text
);
5351 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5352 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5353 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5355 send_ipp_status(con
, IPP_CHARSET
,
5356 _("Language \"%s\" not supported!"),
5357 attr
->values
[0].string
.text
);
5360 else if (!strcmp(attr
->name
, "notify-user-data") &&
5361 attr
->value_tag
== IPP_TAG_STRING
)
5363 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5365 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5366 _("The notify-user-data value is too large "
5367 "(%d > 63 octets)!"),
5368 attr
->values
[0].unknown
.length
);
5374 else if (!strcmp(attr
->name
, "notify-events") &&
5375 attr
->value_tag
== IPP_TAG_KEYWORD
)
5376 notify_events
= attr
;
5377 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5378 attr
->value_tag
== IPP_TAG_INTEGER
)
5379 lease
= attr
->values
[0].integer
;
5380 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5381 attr
->value_tag
== IPP_TAG_INTEGER
)
5382 interval
= attr
->values
[0].integer
;
5383 else if (!strcmp(attr
->name
, "notify-job-id") &&
5384 attr
->value_tag
== IPP_TAG_INTEGER
)
5385 jobid
= attr
->values
[0].integer
;
5392 for (i
= 0; i
< notify_events
->num_values
; i
++)
5393 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5397 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5399 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5400 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5401 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5403 if (!recipient
&& !pullmethod
)
5406 if (mask
== CUPSD_EVENT_NONE
)
5409 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5411 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5414 send_ipp_status(con
, IPP_BAD_REQUEST
,
5415 _("notify-events not specified!"));
5420 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5422 cupsdLogMessage(CUPSD_LOG_INFO
,
5423 "create_subscription: Limiting notify-lease-duration to "
5426 lease
= MaxLeaseDuration
;
5431 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5433 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
5440 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
5443 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
5446 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5447 "Added subscription %d for printer \"%s\"",
5448 sub
->id
, printer
->name
);
5450 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
5453 sub
->interval
= interval
;
5455 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5457 cupsdSetString(&sub
->owner
, username
);
5461 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5462 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5463 sub
->user_data_len
);
5466 ippAddSeparator(con
->response
);
5467 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5468 "notify-subscription-id", sub
->id
);
5470 con
->response
->request
.status
.status_code
= IPP_OK
;
5476 cupsdSaveAllSubscriptions();
5482 * 'delete_printer()' - Remove a printer or class from the system.
5486 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5487 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5489 http_status_t status
; /* Policy status */
5490 cups_ptype_t dtype
; /* Destination type (printer/class) */
5491 cupsd_printer_t
*printer
; /* Printer/class */
5492 char filename
[1024]; /* Script/PPD filename */
5495 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5496 con
->http
.fd
, uri
->values
[0].string
.text
);
5499 * Do we have a valid URI?
5502 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5508 send_ipp_status(con
, IPP_NOT_FOUND
,
5509 _("The printer or class was not found."));
5517 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5519 send_http_error(con
, status
, NULL
);
5524 * Remove old jobs...
5527 cupsdCancelJobs(printer
->name
, NULL
, 1);
5530 * Remove old subscriptions and send a "deleted printer" event...
5533 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5534 "%s \"%s\" deleted by \"%s\".",
5535 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5536 printer
->name
, get_username(con
));
5538 cupsdExpireSubscriptions(printer
, NULL
);
5541 * Remove any old PPD or script files...
5544 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5548 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5552 if (dtype
& CUPS_PRINTER_CLASS
)
5554 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5555 printer
->name
, get_username(con
));
5557 cupsdDeletePrinter(printer
, 0);
5558 cupsdSaveAllClasses();
5562 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5563 printer
->name
, get_username(con
));
5565 cupsdDeletePrinter(printer
, 0);
5566 cupsdSaveAllPrinters();
5569 cupsdWritePrintcap();
5572 * Return with no errors...
5575 con
->response
->request
.status
.status_code
= IPP_OK
;
5580 * 'get_default()' - Get the default destination.
5584 get_default(cupsd_client_t
*con
) /* I - Client connection */
5586 http_status_t status
; /* Policy status */
5587 cups_array_t
*ra
; /* Requested attributes array */
5590 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5596 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5598 send_http_error(con
, status
, NULL
);
5604 ra
= create_requested_array(con
->request
);
5606 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5608 cupsArrayDelete(ra
);
5610 con
->response
->request
.status
.status_code
= IPP_OK
;
5613 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5618 * 'get_devices()' - Get the list of available devices on the local system.
5622 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5624 http_status_t status
; /* Policy status */
5625 ipp_attribute_t
*limit
, /* Limit attribute */
5626 *requested
; /* requested-attributes attribute */
5627 char command
[1024], /* cups-deviced command */
5628 options
[1024], /* Options to pass to command */
5630 /* String for requested attributes */
5633 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5639 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5641 send_http_error(con
, status
, NULL
);
5646 * Run cups-deviced command with the given options...
5649 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5650 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5654 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5656 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5658 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5659 snprintf(options
, sizeof(options
),
5661 con
->request
->request
.op
.request_id
,
5662 limit
? limit
->values
[0].integer
: 0, (int)User
,
5665 if (cupsdSendCommand(con
, command
, options
, 1))
5668 * Command started successfully, don't send an IPP response here...
5671 ippDelete(con
->response
);
5672 con
->response
= NULL
;
5677 * Command failed, return "internal error" so the user knows something
5681 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5682 _("cups-deviced failed to execute."));
5688 * 'get_document()' - Get a copy of a job file.
5692 get_document(cupsd_client_t
*con
, /* I - Client connection */
5693 ipp_attribute_t
*uri
) /* I - Job URI */
5695 http_status_t status
; /* Policy status */
5696 ipp_attribute_t
*attr
; /* Current attribute */
5697 int jobid
; /* Job ID */
5698 int docnum
; /* Document number */
5699 cupsd_job_t
*job
; /* Current job */
5700 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5701 username
[HTTP_MAX_URI
], /* Username portion of URI */
5702 host
[HTTP_MAX_URI
], /* Host portion of URI */
5703 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5704 int port
; /* Port portion of URI */
5705 char filename
[1024], /* Filename for document */
5706 format
[1024]; /* Format for document */
5709 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
5710 con
->http
.fd
, uri
->values
[0].string
.text
);
5713 * See if we have a job URI or a printer URI...
5716 if (!strcmp(uri
->name
, "printer-uri"))
5719 * Got a printer URI; see if we also have a job-id attribute...
5722 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5723 IPP_TAG_INTEGER
)) == NULL
)
5725 send_ipp_status(con
, IPP_BAD_REQUEST
,
5726 _("Got a printer-uri attribute but no job-id!"));
5730 jobid
= attr
->values
[0].integer
;
5735 * Got a job URI; parse it to get the job ID...
5738 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5739 sizeof(method
), username
, sizeof(username
), host
,
5740 sizeof(host
), &port
, resource
, sizeof(resource
));
5742 if (strncmp(resource
, "/jobs/", 6))
5748 send_ipp_status(con
, IPP_BAD_REQUEST
,
5749 _("Bad job-uri attribute \"%s\"!"),
5750 uri
->values
[0].string
.text
);
5754 jobid
= atoi(resource
+ 6);
5758 * See if the job exists...
5761 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5764 * Nope - return a "not found" error...
5767 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5775 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5777 send_http_error(con
, status
, NULL
);
5782 * Get the document number...
5785 if ((attr
= ippFindAttribute(con
->request
, "document-number",
5786 IPP_TAG_INTEGER
)) == NULL
)
5788 send_ipp_status(con
, IPP_BAD_REQUEST
,
5789 _("Missing document-number attribute!"));
5793 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
5794 attr
->num_values
> 1)
5796 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
5801 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
5803 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
5805 cupsdLogMessage(CUPSD_LOG_ERROR
,
5806 "Unable to open document %d in job %d - %s", docnum
, jobid
,
5808 send_ipp_status(con
, IPP_NOT_FOUND
,
5809 _("Unable to open document %d in job %d!"), docnum
, jobid
);
5813 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
5817 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
5818 job
->filetypes
[docnum
- 1]->type
);
5820 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
5822 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
5824 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
5825 IPP_TAG_NAME
)) != NULL
)
5826 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
5827 NULL
, attr
->values
[0].string
.text
);
5832 * 'get_job_attrs()' - Get job attributes.
5836 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5837 ipp_attribute_t
*uri
) /* I - Job URI */
5839 http_status_t status
; /* Policy status */
5840 ipp_attribute_t
*attr
; /* Current attribute */
5841 int jobid
; /* Job ID */
5842 cupsd_job_t
*job
; /* Current job */
5843 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5844 username
[HTTP_MAX_URI
], /* Username portion of URI */
5845 host
[HTTP_MAX_URI
], /* Host portion of URI */
5846 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5847 int port
; /* Port portion of URI */
5848 cups_array_t
*ra
; /* Requested attributes array */
5851 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5852 con
->http
.fd
, uri
->values
[0].string
.text
);
5855 * See if we have a job URI or a printer URI...
5858 if (!strcmp(uri
->name
, "printer-uri"))
5861 * Got a printer URI; see if we also have a job-id attribute...
5864 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5865 IPP_TAG_INTEGER
)) == NULL
)
5867 send_ipp_status(con
, IPP_BAD_REQUEST
,
5868 _("Got a printer-uri attribute but no job-id!"));
5872 jobid
= attr
->values
[0].integer
;
5877 * Got a job URI; parse it to get the job ID...
5880 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5881 sizeof(method
), username
, sizeof(username
), host
,
5882 sizeof(host
), &port
, resource
, sizeof(resource
));
5884 if (strncmp(resource
, "/jobs/", 6))
5890 send_ipp_status(con
, IPP_BAD_REQUEST
,
5891 _("Bad job-uri attribute \"%s\"!"),
5892 uri
->values
[0].string
.text
);
5896 jobid
= atoi(resource
+ 6);
5900 * See if the job exists...
5903 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5906 * Nope - return a "not found" error...
5909 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5917 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5919 send_http_error(con
, status
, NULL
);
5924 * Copy attributes...
5929 ra
= create_requested_array(con
->request
);
5930 copy_job_attrs(con
, job
, ra
);
5931 cupsArrayDelete(ra
);
5933 con
->response
->request
.status
.status_code
= IPP_OK
;
5938 * 'get_jobs()' - Get a list of jobs for the specified printer.
5942 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
5943 ipp_attribute_t
*uri
) /* I - Printer URI */
5945 http_status_t status
; /* Policy status */
5946 ipp_attribute_t
*attr
; /* Current attribute */
5947 const char *dest
; /* Destination */
5948 cups_ptype_t dtype
; /* Destination type (printer/class) */
5949 cups_ptype_t dmask
; /* Destination type mask */
5950 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
5951 username
[HTTP_MAX_URI
], /* Username portion of URI */
5952 host
[HTTP_MAX_URI
], /* Host portion of URI */
5953 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5954 int port
; /* Port portion of URI */
5955 int completed
; /* Completed jobs? */
5956 int first_job_id
; /* First job ID */
5957 int limit
; /* Maximum number of jobs to return */
5958 int count
; /* Number of jobs that match */
5959 cupsd_job_t
*job
; /* Current job pointer */
5960 cupsd_printer_t
*printer
; /* Printer */
5961 cups_array_t
*list
; /* Which job list... */
5962 cups_array_t
*ra
; /* Requested attributes array */
5965 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
5966 uri
->values
[0].string
.text
);
5969 * Is the destination valid?
5972 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5973 sizeof(scheme
), username
, sizeof(username
), host
,
5974 sizeof(host
), &port
, resource
, sizeof(resource
));
5976 if (!strcmp(resource
, "/") ||
5977 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
5980 dtype
= (cups_ptype_t
)0;
5981 dmask
= (cups_ptype_t
)0;
5984 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5987 dtype
= (cups_ptype_t
)0;
5988 dmask
= CUPS_PRINTER_CLASS
;
5991 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5994 dtype
= CUPS_PRINTER_CLASS
;
5995 dmask
= CUPS_PRINTER_CLASS
;
5998 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6005 send_ipp_status(con
, IPP_NOT_FOUND
,
6006 _("The printer or class was not found."));
6011 dtype
&= CUPS_PRINTER_CLASS
;
6012 dmask
= CUPS_PRINTER_CLASS
;
6021 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6024 send_http_error(con
, status
, printer
);
6028 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6030 send_http_error(con
, status
, NULL
);
6035 * See if the "which-jobs" attribute have been specified...
6038 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6039 IPP_TAG_KEYWORD
)) != NULL
&&
6040 !strcmp(attr
->values
[0].string
.text
, "completed"))
6045 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
6057 * See if they want to limit the number of jobs reported...
6060 if ((attr
= ippFindAttribute(con
->request
, "limit",
6061 IPP_TAG_INTEGER
)) != NULL
)
6062 limit
= attr
->values
[0].integer
;
6066 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
6067 IPP_TAG_INTEGER
)) != NULL
)
6068 first_job_id
= attr
->values
[0].integer
;
6073 * See if we only want to see jobs for a specific user...
6076 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
6077 IPP_TAG_BOOLEAN
)) != NULL
&&
6078 attr
->values
[0].boolean
)
6079 strlcpy(username
, get_username(con
), sizeof(username
));
6083 ra
= create_requested_array(con
->request
);
6086 * OK, build a list of jobs for this printer...
6089 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6090 count
< limit
&& job
;
6091 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6094 * Filter out jobs that don't match...
6097 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
6099 if (!job
->dest
|| !job
->username
)
6102 if (!job
->dest
|| !job
->username
)
6105 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6106 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6108 if ((job
->dtype
& dmask
) != dtype
&&
6109 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6111 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
6114 if (job
->id
< first_job_id
)
6122 if (username
[0] && strcasecmp(username
, job
->username
))
6126 ippAddSeparator(con
->response
);
6130 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
6132 copy_job_attrs(con
, job
, ra
);
6135 cupsArrayDelete(ra
);
6137 con
->response
->request
.status
.status_code
= IPP_OK
;
6142 * 'get_notifications()' - Get events for a subscription.
6146 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6148 int i
, j
; /* Looping vars */
6149 http_status_t status
; /* Policy status */
6150 cupsd_subscription_t
*sub
; /* Subscription */
6151 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6152 *sequences
; /* notify-sequence-numbers */
6153 int min_seq
; /* Minimum sequence number */
6154 int interval
; /* Poll interval */
6157 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6161 * Get subscription attributes...
6164 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6166 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6171 send_ipp_status(con
, IPP_BAD_REQUEST
,
6172 _("Missing notify-subscription-ids attribute!"));
6177 * Are the subscription IDs valid?
6180 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6182 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6185 * Bad subscription ID...
6188 send_ipp_status(con
, IPP_NOT_FOUND
,
6189 _("notify-subscription-id %d no good!"),
6190 ids
->values
[i
].integer
);
6198 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6200 con
, sub
->owner
)) != HTTP_OK
)
6202 send_http_error(con
, status
, sub
->dest
);
6207 * Check the subscription type and update the interval accordingly.
6210 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6213 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6215 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6221 * Tell the client to poll again in N seconds...
6225 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6226 "notify-get-interval", interval
);
6228 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6229 "printer-up-time", time(NULL
));
6232 * Copy the subscription event attributes to the response.
6235 con
->response
->request
.status
.status_code
=
6236 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6238 for (i
= 0; i
< ids
->num_values
; i
++)
6241 * Get the subscription and sequence number...
6244 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6246 if (sequences
&& i
< sequences
->num_values
)
6247 min_seq
= sequences
->values
[i
].integer
;
6252 * If we don't have any new events, nothing to do here...
6255 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
6259 * Otherwise copy all of the new events...
6262 if (sub
->first_event_id
> min_seq
)
6265 j
= min_seq
- sub
->first_event_id
;
6267 for (; j
< sub
->num_events
; j
++)
6269 ippAddSeparator(con
->response
);
6271 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
6272 IPP_TAG_EVENT_NOTIFICATION
, 0);
6279 * 'get_ppd()' - Get a named PPD from the local system.
6283 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
6284 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
6286 http_status_t status
; /* Policy status */
6287 cupsd_printer_t
*dest
; /* Destination */
6288 cups_ptype_t dtype
; /* Destination type */
6291 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
6292 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
6294 if (!strcmp(uri
->name
, "ppd-name"))
6297 * Return a PPD file from cups-driverd...
6300 char command
[1024], /* cups-driverd command */
6301 options
[1024], /* Options to pass to command */
6302 ppd_name
[1024]; /* ppd-name */
6309 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6311 send_http_error(con
, status
, NULL
);
6316 * Run cups-driverd command with the given options...
6319 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6320 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
6321 snprintf(options
, sizeof(options
), "get+%d+%s",
6322 con
->request
->request
.op
.request_id
, ppd_name
);
6324 if (cupsdSendCommand(con
, command
, options
, 0))
6327 * Command started successfully, don't send an IPP response here...
6330 ippDelete(con
->response
);
6331 con
->response
= NULL
;
6336 * Command failed, return "internal error" so the user knows something
6340 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6341 _("cups-driverd failed to execute."));
6344 else if (!strcmp(uri
->name
, "printer-uri") &&
6345 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
6347 int i
; /* Looping var */
6348 char filename
[1024]; /* PPD filename */
6355 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6357 send_http_error(con
, status
, dest
);
6362 * See if we need the PPD for a class or remote printer...
6365 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6368 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
6370 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6371 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6372 "printer-uri", NULL
, dest
->uri
);
6375 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
6377 for (i
= 0; i
< dest
->num_printers
; i
++)
6378 if (!(dest
->printers
[i
]->type
&
6379 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
6381 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6382 dest
->printers
[i
]->name
);
6384 if (!access(filename
, 0))
6388 if (i
< dest
->num_printers
)
6389 dest
= dest
->printers
[i
];
6392 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6393 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6394 "printer-uri", NULL
, dest
->printers
[0]->uri
);
6400 * Found the printer with the PPD file, now see if there is one...
6403 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
6405 send_ipp_status(con
, IPP_NOT_FOUND
,
6406 _("The PPD file \"%s\" could not be opened: %s"),
6407 uri
->values
[0].string
.text
, strerror(errno
));
6411 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6415 con
->response
->request
.status
.status_code
= IPP_OK
;
6418 send_ipp_status(con
, IPP_NOT_FOUND
,
6419 _("The PPD file \"%s\" could not be found."),
6420 uri
->values
[0].string
.text
);
6425 * 'get_ppds()' - Get the list of PPD files on the local system.
6429 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
6431 http_status_t status
; /* Policy status */
6432 ipp_attribute_t
*limit
, /* Limit attribute */
6433 *device
, /* ppd-device-id attribute */
6434 *language
, /* ppd-natural-language attribute */
6435 *make
, /* ppd-make attribute */
6436 *model
, /* ppd-make-and-model attribute */
6437 *model_number
, /* ppd-model-number attribute */
6438 *product
, /* ppd-product attribute */
6439 *psversion
, /* ppd-psverion attribute */
6440 *type
, /* ppd-type attribute */
6441 *requested
; /* requested-attributes attribute */
6442 char command
[1024], /* cups-driverd command */
6443 options
[1024], /* Options to pass to command */
6444 device_str
[256],/* Escaped ppd-device-id string */
6446 /* Escaped ppd-natural-language */
6447 make_str
[256], /* Escaped ppd-make string */
6448 model_str
[256], /* Escaped ppd-make-and-model string */
6449 model_number_str
[256],
6450 /* ppd-model-number string */
6452 /* Escaped ppd-product string */
6454 /* Escaped ppd-psversion string */
6455 type_str
[256], /* Escaped ppd-type string */
6457 /* String for requested attributes */
6460 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
6466 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6468 send_http_error(con
, status
, NULL
);
6473 * Run cups-driverd command with the given options...
6476 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6477 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
6478 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
6480 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
6481 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
6483 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
6485 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
6486 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
6487 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
6488 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6492 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6494 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6497 url_encode_attr(device
, device_str
, sizeof(device_str
));
6499 device_str
[0] = '\0';
6502 url_encode_attr(language
, language_str
, sizeof(language_str
));
6504 language_str
[0] = '\0';
6507 url_encode_attr(make
, make_str
, sizeof(make_str
));
6512 url_encode_attr(model
, model_str
, sizeof(model_str
));
6514 model_str
[0] = '\0';
6517 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
6518 model_number
->values
[0].integer
);
6520 model_number_str
[0] = '\0';
6523 url_encode_attr(product
, product_str
, sizeof(product_str
));
6525 product_str
[0] = '\0';
6528 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
6530 psversion_str
[0] = '\0';
6533 url_encode_attr(type
, type_str
, sizeof(type_str
));
6537 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6538 snprintf(options
, sizeof(options
),
6539 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
6540 con
->request
->request
.op
.request_id
,
6541 limit
? limit
->values
[0].integer
: 0,
6543 device
? "%20" : "", device_str
,
6544 language
? "%20" : "", language_str
,
6545 make
? "%20" : "", make_str
,
6546 model
? "%20" : "", model_str
,
6547 model_number
? "%20" : "", model_number_str
,
6548 product
? "%20" : "", product_str
,
6549 psversion
? "%20" : "", psversion_str
,
6550 type
? "%20" : "", type_str
);
6552 if (cupsdSendCommand(con
, command
, options
, 0))
6555 * Command started successfully, don't send an IPP response here...
6558 ippDelete(con
->response
);
6559 con
->response
= NULL
;
6564 * Command failed, return "internal error" so the user knows something
6568 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6569 _("cups-driverd failed to execute."));
6575 * 'get_printer_attrs()' - Get printer attributes.
6579 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
6580 ipp_attribute_t
*uri
) /* I - Printer URI */
6582 http_status_t status
; /* Policy status */
6583 cups_ptype_t dtype
; /* Destination type (printer/class) */
6584 cupsd_printer_t
*printer
; /* Printer/class */
6585 cups_array_t
*ra
; /* Requested attributes array */
6588 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
6589 con
->http
.fd
, uri
->values
[0].string
.text
);
6592 * Is the destination valid?
6595 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6601 send_ipp_status(con
, IPP_NOT_FOUND
,
6602 _("The printer or class was not found."));
6610 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6612 send_http_error(con
, status
, printer
);
6617 * Send the attributes...
6620 ra
= create_requested_array(con
->request
);
6622 copy_printer_attrs(con
, printer
, ra
);
6624 cupsArrayDelete(ra
);
6626 con
->response
->request
.status
.status_code
= IPP_OK
;
6631 * 'get_printers()' - Get a list of printers or classes.
6635 get_printers(cupsd_client_t
*con
, /* I - Client connection */
6636 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
6638 http_status_t status
; /* Policy status */
6639 ipp_attribute_t
*attr
; /* Current attribute */
6640 int limit
; /* Max number of printers to return */
6641 int count
; /* Number of printers that match */
6642 cupsd_printer_t
*printer
; /* Current printer pointer */
6643 int printer_type
, /* printer-type attribute */
6644 printer_mask
; /* printer-type-mask attribute */
6645 char *location
; /* Location string */
6646 const char *username
; /* Current user */
6647 char *first_printer_name
; /* first-printer-name attribute */
6648 cups_array_t
*ra
; /* Requested attributes array */
6651 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
6652 con
->http
.fd
, type
);
6658 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6660 send_http_error(con
, status
, NULL
);
6665 * Check for printers...
6668 if (!Printers
|| !cupsArrayCount(Printers
))
6670 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
6675 * See if they want to limit the number of printers reported...
6678 if ((attr
= ippFindAttribute(con
->request
, "limit",
6679 IPP_TAG_INTEGER
)) != NULL
)
6680 limit
= attr
->values
[0].integer
;
6684 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
6685 IPP_TAG_NAME
)) != NULL
)
6686 first_printer_name
= attr
->values
[0].string
.text
;
6688 first_printer_name
= NULL
;
6691 * Support filtering...
6694 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
6695 IPP_TAG_ENUM
)) != NULL
)
6696 printer_type
= attr
->values
[0].integer
;
6700 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
6701 IPP_TAG_ENUM
)) != NULL
)
6702 printer_mask
= attr
->values
[0].integer
;
6706 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
6707 IPP_TAG_TEXT
)) != NULL
)
6708 location
= attr
->values
[0].string
.text
;
6712 if (con
->username
[0])
6713 username
= con
->username
;
6714 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6715 IPP_TAG_NAME
)) != NULL
)
6716 username
= attr
->values
[0].string
.text
;
6720 ra
= create_requested_array(con
->request
);
6723 * OK, build a list of printers for this printer...
6726 if (first_printer_name
)
6728 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
6729 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
6732 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
6735 count
< limit
&& printer
;
6736 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
6738 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
6739 (printer
->type
& printer_mask
) == printer_type
&&
6740 (!location
|| !printer
->location
||
6741 !strcasecmp(printer
->location
, location
)))
6744 * If HideImplicitMembers is enabled, see if this printer or class
6745 * is a member of an implicit class...
6748 if (ImplicitClasses
&& HideImplicitMembers
&&
6749 printer
->in_implicit_class
)
6753 * If a username is specified, see if it is allowed or denied
6757 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
6761 * Add the group separator as needed...
6765 ippAddSeparator(con
->response
);
6770 * Send the attributes...
6773 copy_printer_attrs(con
, printer
, ra
);
6777 cupsArrayDelete(ra
);
6779 con
->response
->request
.status
.status_code
= IPP_OK
;
6784 * 'get_subscription_attrs()' - Get subscription attributes.
6788 get_subscription_attrs(
6789 cupsd_client_t
*con
, /* I - Client connection */
6790 int sub_id
) /* I - Subscription ID */
6792 http_status_t status
; /* Policy status */
6793 cupsd_subscription_t
*sub
; /* Subscription */
6794 cups_array_t
*ra
; /* Requested attributes array */
6797 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6798 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
6799 con
, con
->http
.fd
, sub_id
);
6802 * Is the subscription ID valid?
6805 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
6808 * Bad subscription ID...
6811 send_ipp_status(con
, IPP_NOT_FOUND
,
6812 _("notify-subscription-id %d no good!"), sub_id
);
6820 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6822 con
, sub
->owner
)) != HTTP_OK
)
6824 send_http_error(con
, status
, sub
->dest
);
6829 * Copy the subscription attributes to the response using the
6830 * requested-attributes attribute that may be provided by the client.
6833 ra
= create_requested_array(con
->request
);
6835 copy_subscription_attrs(con
, sub
, ra
);
6837 cupsArrayDelete(ra
);
6839 con
->response
->request
.status
.status_code
= IPP_OK
;
6844 * 'get_subscriptions()' - Get subscriptions.
6848 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
6849 ipp_attribute_t
*uri
) /* I - Printer/job URI */
6851 http_status_t status
; /* Policy status */
6852 int count
; /* Number of subscriptions */
6853 int limit
; /* Limit */
6854 cupsd_subscription_t
*sub
; /* Subscription */
6855 cups_array_t
*ra
; /* Requested attributes array */
6856 ipp_attribute_t
*attr
; /* Attribute */
6857 cups_ptype_t dtype
; /* Destination type (printer/class) */
6858 char scheme
[HTTP_MAX_URI
],
6859 /* Scheme portion of URI */
6860 username
[HTTP_MAX_URI
],
6861 /* Username portion of URI */
6863 /* Host portion of URI */
6864 resource
[HTTP_MAX_URI
];
6865 /* Resource portion of URI */
6866 int port
; /* Port portion of URI */
6867 cupsd_job_t
*job
; /* Job pointer */
6868 cupsd_printer_t
*printer
; /* Printer */
6871 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6872 "get_subscriptions(con=%p[%d], uri=%s)",
6873 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
6876 * Is the destination valid?
6879 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6880 sizeof(scheme
), username
, sizeof(username
), host
,
6881 sizeof(host
), &port
, resource
, sizeof(resource
));
6883 if (!strcmp(resource
, "/") ||
6884 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
6885 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
6886 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
6891 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
6894 job
= cupsdFindJob(atoi(resource
+ 6));
6898 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
6903 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6909 send_ipp_status(con
, IPP_NOT_FOUND
,
6910 _("The printer or class was not found."));
6913 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
6914 IPP_TAG_INTEGER
)) != NULL
)
6916 job
= cupsdFindJob(attr
->values
[0].integer
);
6920 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
6921 attr
->values
[0].integer
);
6932 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
6934 con
, NULL
)) != HTTP_OK
)
6936 send_http_error(con
, status
, printer
);
6941 * Copy the subscription attributes to the response using the
6942 * requested-attributes attribute that may be provided by the client.
6945 ra
= create_requested_array(con
->request
);
6947 if ((attr
= ippFindAttribute(con
->request
, "limit",
6948 IPP_TAG_INTEGER
)) != NULL
)
6949 limit
= attr
->values
[0].integer
;
6954 * See if we only want to see subscriptions for a specific user...
6957 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
6958 IPP_TAG_BOOLEAN
)) != NULL
&&
6959 attr
->values
[0].boolean
)
6960 strlcpy(username
, get_username(con
), sizeof(username
));
6964 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
6966 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
6967 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
6968 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
6970 ippAddSeparator(con
->response
);
6971 copy_subscription_attrs(con
, sub
, ra
);
6974 if (limit
&& count
>= limit
)
6978 cupsArrayDelete(ra
);
6981 con
->response
->request
.status
.status_code
= IPP_OK
;
6983 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
6988 * 'get_username()' - Get the username associated with a request.
6991 static const char * /* O - Username */
6992 get_username(cupsd_client_t
*con
) /* I - Connection */
6994 ipp_attribute_t
*attr
; /* Attribute */
6997 if (con
->username
[0])
6998 return (con
->username
);
6999 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7000 IPP_TAG_NAME
)) != NULL
)
7001 return (attr
->values
[0].string
.text
);
7003 return ("anonymous");
7008 * 'hold_job()' - Hold a print job.
7012 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7013 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7015 ipp_attribute_t
*attr
, /* Current job-hold-until */
7016 *newattr
; /* New job-hold-until */
7017 int jobid
; /* Job ID */
7018 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7019 username
[HTTP_MAX_URI
], /* Username portion of URI */
7020 host
[HTTP_MAX_URI
], /* Host portion of URI */
7021 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7022 int port
; /* Port portion of URI */
7023 cupsd_job_t
*job
; /* Job information */
7026 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
7027 uri
->values
[0].string
.text
);
7030 * See if we have a job URI or a printer URI...
7033 if (!strcmp(uri
->name
, "printer-uri"))
7036 * Got a printer URI; see if we also have a job-id attribute...
7039 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7040 IPP_TAG_INTEGER
)) == NULL
)
7042 send_ipp_status(con
, IPP_BAD_REQUEST
,
7043 _("Got a printer-uri attribute but no job-id!"));
7047 jobid
= attr
->values
[0].integer
;
7052 * Got a job URI; parse it to get the job ID...
7055 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7056 sizeof(method
), username
, sizeof(username
), host
,
7057 sizeof(host
), &port
, resource
, sizeof(resource
));
7059 if (strncmp(resource
, "/jobs/", 6))
7065 send_ipp_status(con
, IPP_BAD_REQUEST
,
7066 _("Bad job-uri attribute \"%s\"!"),
7067 uri
->values
[0].string
.text
);
7071 jobid
= atoi(resource
+ 6);
7075 * See if the job exists...
7078 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7081 * Nope - return a "not found" error...
7084 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7089 * See if the job is owned by the requesting user...
7092 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7094 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7099 * Hold the job and return...
7104 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
7105 "Job held by user.");
7107 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
7108 IPP_TAG_KEYWORD
)) == NULL
)
7109 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7111 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7112 IPP_TAG_KEYWORD
)) == NULL
)
7113 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7118 * Free the old hold value and copy the new one over...
7121 _cupsStrFree(attr
->values
[0].string
.text
);
7125 attr
->value_tag
= newattr
->value_tag
;
7126 attr
->values
[0].string
.text
=
7127 _cupsStrAlloc(newattr
->values
[0].string
.text
);
7131 attr
->value_tag
= IPP_TAG_KEYWORD
;
7132 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
7136 * Hold job until specified time...
7139 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
7141 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
7142 "Job job-hold-until value changed by user.");
7145 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Held by \"%s\".", jobid
,
7148 con
->response
->request
.status
.status_code
= IPP_OK
;
7153 * 'move_job()' - Move a job to a new destination.
7157 move_job(cupsd_client_t
*con
, /* I - Client connection */
7158 ipp_attribute_t
*uri
) /* I - Job URI */
7160 http_status_t status
; /* Policy status */
7161 ipp_attribute_t
*attr
; /* Current attribute */
7162 int jobid
; /* Job ID */
7163 cupsd_job_t
*job
; /* Current job */
7164 const char *src
; /* Source printer/class */
7165 cups_ptype_t stype
, /* Source type (printer or class) */
7166 dtype
; /* Destination type (printer/class) */
7167 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7168 username
[HTTP_MAX_URI
], /* Username portion of URI */
7169 host
[HTTP_MAX_URI
], /* Host portion of URI */
7170 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7171 int port
; /* Port portion of URI */
7172 cupsd_printer_t
*sprinter
, /* Source printer */
7173 *dprinter
; /* Destination printer */
7176 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
7177 uri
->values
[0].string
.text
);
7180 * Get the new printer or class...
7183 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7184 IPP_TAG_URI
)) == NULL
)
7187 * Need job-printer-uri...
7190 send_ipp_status(con
, IPP_BAD_REQUEST
,
7191 _("job-printer-uri attribute missing!"));
7195 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7201 send_ipp_status(con
, IPP_NOT_FOUND
,
7202 _("The printer or class was not found."));
7210 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
7213 send_http_error(con
, status
, dprinter
);
7218 * See if we have a job URI or a printer URI...
7221 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7222 sizeof(scheme
), username
, sizeof(username
), host
,
7223 sizeof(host
), &port
, resource
, sizeof(resource
));
7225 if (!strcmp(uri
->name
, "printer-uri"))
7228 * Got a printer URI; see if we also have a job-id attribute...
7231 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7232 IPP_TAG_INTEGER
)) == NULL
)
7238 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
7239 &sprinter
)) == NULL
)
7245 send_ipp_status(con
, IPP_NOT_FOUND
,
7246 _("The printer or class was not found."));
7255 * Otherwise, just move a single job...
7258 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7261 * Nope - return a "not found" error...
7264 send_ipp_status(con
, IPP_NOT_FOUND
,
7265 _("Job #%d does not exist!"), attr
->values
[0].integer
);
7271 * Job found, initialize source pointers...
7282 * Got a job URI; parse it to get the job ID...
7285 if (strncmp(resource
, "/jobs/", 6))
7291 send_ipp_status(con
, IPP_BAD_REQUEST
,
7292 _("Bad job-uri attribute \"%s\"!"),
7293 uri
->values
[0].string
.text
);
7298 * See if the job exists...
7301 jobid
= atoi(resource
+ 6);
7303 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7306 * Nope - return a "not found" error...
7309 send_ipp_status(con
, IPP_NOT_FOUND
,
7310 _("Job #%d does not exist!"), jobid
);
7316 * Job found, initialize source pointers...
7325 * Now move the job or jobs...
7331 * See if the job has been completed...
7334 if (job
->state_value
> IPP_JOB_STOPPED
)
7337 * Return a "not-possible" error...
7340 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7341 _("Job #%d is finished and cannot be altered!"),
7347 * See if the job is owned by the requesting user...
7350 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7352 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7357 * Move the job to a different printer or class...
7360 cupsdMoveJob(job
, dprinter
);
7365 * Got the source printer, now look through the jobs...
7368 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
7370 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
7373 * See if the job is pointing at the source printer or has not been
7377 if (strcasecmp(job
->dest
, src
) ||
7378 job
->state_value
> IPP_JOB_STOPPED
)
7382 * See if the job can be moved by the requesting user...
7385 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7389 * Move the job to a different printer or class...
7392 cupsdMoveJob(job
, dprinter
);
7397 * Start jobs if possible...
7403 * Return with "everything is OK" status...
7406 con
->response
->request
.status
.status_code
= IPP_OK
;
7411 * 'ppd_parse_line()' - Parse a PPD default line.
7414 static int /* O - 0 on success, -1 on failure */
7415 ppd_parse_line(const char *line
, /* I - Line */
7416 char *option
, /* O - Option name */
7417 int olen
, /* I - Size of option name */
7418 char *choice
, /* O - Choice name */
7419 int clen
) /* I - Size of choice name */
7422 * Verify this is a default option line...
7425 if (strncmp(line
, "*Default", 8))
7429 * Read the option name...
7432 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
7442 * Skip everything else up to the colon (:)...
7445 while (*line
&& *line
!= ':')
7454 * Now grab the option choice, skipping leading whitespace...
7457 while (isspace(*line
& 255))
7460 for (clen
--; isalnum(*line
& 255); line
++)
7470 * Return with no errors...
7478 * 'print_job()' - Print a file to a printer or class.
7482 print_job(cupsd_client_t
*con
, /* I - Client connection */
7483 ipp_attribute_t
*uri
) /* I - Printer URI */
7485 ipp_attribute_t
*attr
; /* Current attribute */
7486 ipp_attribute_t
*format
; /* Document-format attribute */
7487 const char *default_format
; /* document-format-default value */
7488 cupsd_job_t
*job
; /* New job */
7489 char filename
[1024]; /* Job filename */
7490 mime_type_t
*filetype
; /* Type of file */
7491 char super
[MIME_MAX_SUPER
], /* Supertype of file */
7492 type
[MIME_MAX_TYPE
], /* Subtype of file */
7493 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7494 /* Textual name of mime type */
7495 cupsd_printer_t
*printer
; /* Printer data */
7496 struct stat fileinfo
; /* File information */
7497 int kbytes
; /* Size of file */
7498 int compression
; /* Document compression */
7501 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
7502 uri
->values
[0].string
.text
);
7505 * Validate print file attributes, for now just document-format and
7506 * compression (CUPS only supports "none" and "gzip")...
7509 compression
= CUPS_FILE_NONE
;
7511 if ((attr
= ippFindAttribute(con
->request
, "compression",
7512 IPP_TAG_KEYWORD
)) != NULL
)
7514 if (strcmp(attr
->values
[0].string
.text
, "none")
7516 && strcmp(attr
->values
[0].string
.text
, "gzip")
7517 #endif /* HAVE_LIBZ */
7520 send_ipp_status(con
, IPP_ATTRIBUTES
,
7521 _("Unsupported compression \"%s\"!"),
7522 attr
->values
[0].string
.text
);
7523 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7524 "compression", NULL
, attr
->values
[0].string
.text
);
7529 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7530 compression
= CUPS_FILE_GZIP
;
7531 #endif /* HAVE_LIBZ */
7535 * Do we have a file to print?
7540 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7545 * Is the destination valid?
7548 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
7554 send_ipp_status(con
, IPP_NOT_FOUND
,
7555 _("The printer or class was not found."));
7560 * Is it a format we support?
7563 if ((format
= ippFindAttribute(con
->request
, "document-format",
7564 IPP_TAG_MIMETYPE
)) != NULL
)
7567 * Grab format from client...
7570 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
7573 send_ipp_status(con
, IPP_BAD_REQUEST
,
7574 _("Could not scan type \"%s\"!"),
7575 format
->values
[0].string
.text
);
7579 else if ((default_format
= cupsGetOption("document-format",
7580 printer
->num_options
,
7581 printer
->options
)) != NULL
)
7584 * Use default document format...
7587 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
7589 send_ipp_status(con
, IPP_BAD_REQUEST
,
7590 _("Could not scan type \"%s\"!"),
7601 strcpy(super
, "application");
7602 strcpy(type
, "octet-stream");
7605 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7608 * Auto-type the file...
7611 ipp_attribute_t
*doc_name
; /* document-name attribute */
7614 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
7616 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7617 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7618 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7622 filetype
= mimeType(MimeDatabase
, super
, type
);
7625 filetype
= mimeType(MimeDatabase
, super
, type
);
7629 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
7632 * Replace the document-format attribute value with the auto-typed or
7636 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7641 _cupsStrFree(format
->values
[0].string
.text
);
7643 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7646 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7647 "document-format", NULL
, mimetype
);
7651 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7652 _("Unsupported format \'%s/%s\'!"), super
, type
);
7653 cupsdLogMessage(CUPSD_LOG_INFO
,
7654 "Hint: Do you have the raw file printing rules enabled?");
7657 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7658 "document-format", NULL
, format
->values
[0].string
.text
);
7664 * Read any embedded job ticket info from PS files...
7667 if (!strcasecmp(filetype
->super
, "application") &&
7668 !strcasecmp(filetype
->type
, "postscript"))
7669 read_ps_job_ticket(con
);
7672 * Create the job object...
7675 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
7678 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Adding job file of type %s/%s.",
7679 job
->id
, filetype
->super
, filetype
->type
);
7682 * Update quota data...
7685 if (stat(con
->filename
, &fileinfo
))
7688 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7690 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7692 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7693 IPP_TAG_INTEGER
)) != NULL
)
7694 attr
->values
[0].integer
+= kbytes
;
7697 * Add the job file...
7700 if (add_file(con
, job
, filetype
, compression
))
7703 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
7705 rename(con
->filename
, filename
);
7706 cupsdClearString(&con
->filename
);
7709 * See if we need to add the ending sheet...
7712 if (cupsdTimeoutJob(job
))
7716 * Log and save the job...
7719 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
7720 job
->id
, job
->dest
, job
->username
);
7721 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] hold_until = %d", job
->id
,
7722 (int)job
->hold_until
);
7727 * Start the job if possible...
7735 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
7737 * This function only gets called when printing a single PostScript
7738 * file using the Print-Job operation. It doesn't work for Create-Job +
7739 * Send-File, since the job attributes need to be set at job creation
7740 * time for banners to work. The embedded PS job ticket stuff is here
7741 * only to allow the Windows printer driver for CUPS to pass in JCL
7742 * options and IPP attributes which otherwise would be lost.
7744 * The format of a PS job ticket is simple:
7746 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
7748 * %cupsJobTicket: attr1=value1
7749 * %cupsJobTicket: attr2=value2
7751 * %cupsJobTicket: attrN=valueN
7753 * Job ticket lines must appear immediately after the first line that
7754 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
7755 * looking for job ticket info when it finds a line that does not begin
7756 * with "%cupsJobTicket:".
7758 * The maximum length of a job ticket line, including the prefix, is
7759 * 255 characters to conform with the Adobe DSC.
7761 * Read-only attributes are rejected with a notice to the error log in
7762 * case a malicious user tries anything. Since the job ticket is read
7763 * prior to attribute validation in print_job(), job ticket attributes
7764 * will go through the same validation as IPP attributes...
7768 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
7770 cups_file_t
*fp
; /* File to read from */
7771 char line
[256]; /* Line data */
7772 int num_options
; /* Number of options */
7773 cups_option_t
*options
; /* Options */
7774 ipp_t
*ticket
; /* New attributes */
7775 ipp_attribute_t
*attr
, /* Current attribute */
7776 *attr2
, /* Job attribute */
7777 *prev2
; /* Previous job attribute */
7781 * First open the print file...
7784 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
7786 cupsdLogMessage(CUPSD_LOG_ERROR
,
7787 "read_ps_job_ticket: Unable to open PostScript print file "
7794 * Skip the first line...
7797 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
7799 cupsdLogMessage(CUPSD_LOG_ERROR
,
7800 "read_ps_job_ticket: Unable to read from PostScript print "
7807 if (strncmp(line
, "%!PS-Adobe-", 11))
7810 * Not a DSC-compliant file, so no job ticket info will be available...
7818 * Read job ticket info from the file...
7824 while (cupsFileGets(fp
, line
, sizeof(line
)))
7827 * Stop at the first non-ticket line...
7830 if (strncmp(line
, "%cupsJobTicket:", 15))
7834 * Add the options to the option array...
7837 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
7841 * Done with the file; see if we have any options...
7846 if (num_options
== 0)
7850 * OK, convert the options to an attribute list, and apply them to
7855 cupsEncodeOptions(ticket
, num_options
, options
);
7858 * See what the user wants to change.
7861 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
7863 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
7866 if (!strcmp(attr
->name
, "job-originating-host-name") ||
7867 !strcmp(attr
->name
, "job-originating-user-name") ||
7868 !strcmp(attr
->name
, "job-media-sheets-completed") ||
7869 !strcmp(attr
->name
, "job-k-octets") ||
7870 !strcmp(attr
->name
, "job-id") ||
7871 !strncmp(attr
->name
, "job-state", 9) ||
7872 !strncmp(attr
->name
, "time-at-", 8))
7873 continue; /* Read-only attrs */
7875 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
7876 IPP_TAG_ZERO
)) != NULL
)
7879 * Some other value; first free the old value...
7882 if (con
->request
->attrs
== attr2
)
7884 con
->request
->attrs
= attr2
->next
;
7889 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
7890 if (prev2
->next
== attr2
)
7892 prev2
->next
= attr2
->next
;
7897 if (con
->request
->last
== attr2
)
7898 con
->request
->last
= prev2
;
7900 _ippFreeAttr(attr2
);
7904 * Add new option by copying it...
7907 copy_attribute(con
->request
, attr
, 0);
7911 * Then free the attribute list and option array...
7915 cupsFreeOptions(num_options
, options
);
7920 * 'reject_jobs()' - Reject print jobs to a printer.
7924 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
7925 ipp_attribute_t
*uri
) /* I - Printer or class URI */
7927 http_status_t status
; /* Policy status */
7928 cups_ptype_t dtype
; /* Destination type (printer/class) */
7929 cupsd_printer_t
*printer
; /* Printer data */
7930 ipp_attribute_t
*attr
; /* printer-state-message text */
7933 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
7934 con
->http
.fd
, uri
->values
[0].string
.text
);
7937 * Is the destination valid?
7940 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7946 send_ipp_status(con
, IPP_NOT_FOUND
,
7947 _("The printer or class was not found."));
7955 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7957 send_http_error(con
, status
, printer
);
7962 * Reject jobs sent to the printer...
7965 printer
->accepting
= 0;
7967 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
7968 IPP_TAG_TEXT
)) == NULL
)
7969 strcpy(printer
->state_message
, "Rejecting Jobs");
7971 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
7972 sizeof(printer
->state_message
));
7974 cupsdAddPrinterHistory(printer
);
7976 if (dtype
& CUPS_PRINTER_CLASS
)
7978 cupsdSaveAllClasses();
7980 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
7981 printer
->name
, get_username(con
));
7985 cupsdSaveAllPrinters();
7987 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
7988 printer
->name
, get_username(con
));
7992 * Everything was ok, so return OK status...
7995 con
->response
->request
.status
.status_code
= IPP_OK
;
8000 * 'release_job()' - Release a held print job.
8004 release_job(cupsd_client_t
*con
, /* I - Client connection */
8005 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8007 ipp_attribute_t
*attr
; /* Current attribute */
8008 int jobid
; /* Job ID */
8009 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8010 username
[HTTP_MAX_URI
], /* Username portion of URI */
8011 host
[HTTP_MAX_URI
], /* Host portion of URI */
8012 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8013 int port
; /* Port portion of URI */
8014 cupsd_job_t
*job
; /* Job information */
8017 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8018 con
->http
.fd
, uri
->values
[0].string
.text
);
8021 * See if we have a job URI or a printer URI...
8024 if (!strcmp(uri
->name
, "printer-uri"))
8027 * Got a printer URI; see if we also have a job-id attribute...
8030 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8031 IPP_TAG_INTEGER
)) == NULL
)
8033 send_ipp_status(con
, IPP_BAD_REQUEST
,
8034 _("Got a printer-uri attribute but no job-id!"));
8038 jobid
= attr
->values
[0].integer
;
8043 * Got a job URI; parse it to get the job ID...
8046 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8047 sizeof(method
), username
, sizeof(username
), host
,
8048 sizeof(host
), &port
, resource
, sizeof(resource
));
8050 if (strncmp(resource
, "/jobs/", 6))
8056 send_ipp_status(con
, IPP_BAD_REQUEST
,
8057 _("Bad job-uri attribute \"%s\"!"),
8058 uri
->values
[0].string
.text
);
8062 jobid
= atoi(resource
+ 6);
8066 * See if the job exists...
8069 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8072 * Nope - return a "not found" error...
8075 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8080 * See if job is "held"...
8083 if (job
->state_value
!= IPP_JOB_HELD
)
8086 * Nope - return a "not possible" error...
8089 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
8094 * See if the job is owned by the requesting user...
8097 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8099 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8104 * Reset the job-hold-until value to "no-hold"...
8107 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8108 IPP_TAG_KEYWORD
)) == NULL
)
8109 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8113 _cupsStrFree(attr
->values
[0].string
.text
);
8115 attr
->value_tag
= IPP_TAG_KEYWORD
;
8116 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
8118 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
8119 "Job job-hold-until value changed by user.");
8123 * Release the job and return...
8126 cupsdReleaseJob(job
);
8128 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
8129 "Job released by user.");
8131 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Released by \"%s\".", jobid
,
8134 con
->response
->request
.status
.status_code
= IPP_OK
;
8139 * 'renew_subscription()' - Renew an existing subscription...
8144 cupsd_client_t
*con
, /* I - Client connection */
8145 int sub_id
) /* I - Subscription ID */
8147 http_status_t status
; /* Policy status */
8148 cupsd_subscription_t
*sub
; /* Subscription */
8149 ipp_attribute_t
*lease
; /* notify-lease-duration */
8152 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8153 "renew_subscription(con=%p[%d], sub_id=%d)",
8154 con
, con
->http
.fd
, sub_id
);
8157 * Is the subscription ID valid?
8160 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8163 * Bad subscription ID...
8166 send_ipp_status(con
, IPP_NOT_FOUND
,
8167 _("notify-subscription-id %d no good!"), sub_id
);
8174 * Job subscriptions cannot be renewed...
8177 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8178 _("Job subscriptions cannot be renewed!"));
8186 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8188 con
, sub
->owner
)) != HTTP_OK
)
8190 send_http_error(con
, status
, sub
->dest
);
8195 * Renew the subscription...
8198 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8201 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8203 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8205 cupsdLogMessage(CUPSD_LOG_INFO
,
8206 "renew_subscription: Limiting notify-lease-duration to "
8209 sub
->lease
= MaxLeaseDuration
;
8212 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
8214 cupsdSaveAllSubscriptions();
8216 con
->response
->request
.status
.status_code
= IPP_OK
;
8218 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
8219 "notify-lease-duration", sub
->lease
);
8224 * 'restart_job()' - Restart an old print job.
8228 restart_job(cupsd_client_t
*con
, /* I - Client connection */
8229 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8231 ipp_attribute_t
*attr
; /* Current attribute */
8232 int jobid
; /* Job ID */
8233 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8234 username
[HTTP_MAX_URI
], /* Username portion of URI */
8235 host
[HTTP_MAX_URI
], /* Host portion of URI */
8236 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8237 int port
; /* Port portion of URI */
8238 cupsd_job_t
*job
; /* Job information */
8241 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
8242 con
->http
.fd
, uri
->values
[0].string
.text
);
8245 * See if we have a job URI or a printer URI...
8248 if (!strcmp(uri
->name
, "printer-uri"))
8251 * Got a printer URI; see if we also have a job-id attribute...
8254 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8255 IPP_TAG_INTEGER
)) == NULL
)
8257 send_ipp_status(con
, IPP_BAD_REQUEST
,
8258 _("Got a printer-uri attribute but no job-id!"));
8262 jobid
= attr
->values
[0].integer
;
8267 * Got a job URI; parse it to get the job ID...
8270 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8271 sizeof(method
), username
, sizeof(username
), host
,
8272 sizeof(host
), &port
, resource
, sizeof(resource
));
8274 if (strncmp(resource
, "/jobs/", 6))
8280 send_ipp_status(con
, IPP_BAD_REQUEST
,
8281 _("Bad job-uri attribute \"%s\"!"),
8282 uri
->values
[0].string
.text
);
8286 jobid
= atoi(resource
+ 6);
8290 * See if the job exists...
8293 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8296 * Nope - return a "not found" error...
8299 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8304 * See if job is in any of the "completed" states...
8307 if (job
->state_value
<= IPP_JOB_PROCESSING
)
8310 * Nope - return a "not possible" error...
8313 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
8319 * See if we have retained the job files...
8324 if (!job
->attrs
|| job
->num_files
== 0)
8327 * Nope - return a "not possible" error...
8330 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8331 _("Job #%d cannot be restarted - no files!"), jobid
);
8336 * See if the job is owned by the requesting user...
8339 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8341 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8346 * Restart the job and return...
8349 cupsdRestartJob(job
);
8351 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Restarted by \"%s\".", jobid
,
8354 con
->response
->request
.status
.status_code
= IPP_OK
;
8359 * 'save_auth_info()' - Save authentication information for a job.
8364 cupsd_client_t
*con
, /* I - Client connection */
8365 cupsd_job_t
*job
, /* I - Job */
8366 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
8368 int i
; /* Looping var */
8369 char filename
[1024]; /* Job authentication filename */
8370 cups_file_t
*fp
; /* Job authentication file */
8371 char line
[2048]; /* Line for file */
8372 cupsd_printer_t
*dest
; /* Destination printer/class */
8376 * This function saves the in-memory authentication information for
8377 * a job so that it can be used to authenticate with a remote host.
8378 * The information is stored in a file that is readable only by the
8379 * root user. The fields are Base-64 encoded, each on a separate line,
8380 * followed by random number (up to 1024) of newlines to limit the
8381 * amount of information that is exposed.
8383 * Because of the potential for exposing of authentication information,
8384 * this functionality is only enabled when running cupsd as root.
8386 * This caching only works for the Basic and BasicDigest authentication
8387 * types. Digest authentication cannot be cached this way, and in
8388 * the future Kerberos authentication may make all of this obsolete.
8390 * Authentication information is saved whenever an authenticated
8391 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
8394 * This information is deleted after a job is completed or canceled,
8395 * so reprints may require subsequent re-authentication.
8401 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
8405 * Create the authentication file and change permissions...
8408 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
8409 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
8411 cupsdLogMessage(CUPSD_LOG_ERROR
,
8412 "Unable to save authentication info to \"%s\" - %s",
8413 filename
, strerror(errno
));
8417 fchown(cupsFileNumber(fp
), 0, 0);
8418 fchmod(cupsFileNumber(fp
), 0400);
8420 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
8423 * Write 1 to 3 auth values...
8426 cupsdClearString(&job
->auth_username
);
8427 cupsdClearString(&job
->auth_domain
);
8428 cupsdClearString(&job
->auth_password
);
8430 for (i
= 0; i
< auth_info
->num_values
; i
++)
8432 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
8433 strlen(auth_info
->values
[i
].string
.text
));
8434 cupsFilePrintf(fp
, "%s\n", line
);
8436 if (!strcmp(dest
->auth_info_required
[i
], "username"))
8437 cupsdSetStringf(&job
->auth_username
, "CUPSD_AUTH_USERNAME=%s",
8438 auth_info
->values
[i
].string
.text
);
8439 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
8440 cupsdSetStringf(&job
->auth_domain
, "CUPSD_AUTH_DOMAIN=%s",
8441 auth_info
->values
[i
].string
.text
);
8442 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
8443 cupsdSetStringf(&job
->auth_password
, "CUPSD_AUTH_PASSWORD=%s",
8444 auth_info
->values
[i
].string
.text
);
8447 else if (con
->username
[0])
8450 * Write the authenticated username...
8453 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
8454 cupsFilePrintf(fp
, "%s\n", line
);
8456 cupsdSetStringf(&job
->auth_username
, "CUPSD_AUTH_USERNAME=%s", con
->username
);
8457 cupsdClearString(&job
->auth_domain
);
8460 * Write the authenticated password...
8463 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
8464 cupsFilePrintf(fp
, "%s\n", line
);
8466 cupsdSetStringf(&job
->auth_password
, "CUPSD_AUTH_PASSWORD=%s", con
->password
);
8470 * Write a random number of newlines to the end of the file...
8473 for (i
= (rand() % 1024); i
>= 0; i
--)
8474 cupsFilePutChar(fp
, '\n');
8477 * Close the file and return...
8482 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8483 if (con
->gss_have_creds
)
8484 save_krb5_creds(con
, job
);
8485 else if (job
->ccname
)
8486 cupsdClearString(&(job
->ccname
));
8487 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8491 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8493 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
8497 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
8498 cupsd_job_t
*job
) /* I - Job */
8500 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
8501 cupsdLogMessage(CUPSD_LOG_INFO
,
8502 "Sorry, your version of Kerberos does not support delegated "
8507 krb5_error_code error
; /* Kerberos error code */
8508 OM_uint32 major_status
, /* Major status code */
8509 minor_status
; /* Minor status code */
8510 krb5_principal principal
; /* Kerberos principal */
8515 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
8519 if (krb5_init_context
== NULL
)
8521 # endif /* __APPLE__ */
8524 * We MUST create a file-based cache because memory-based caches are
8525 * only valid for the current process/address space.
8527 * Due to various bugs/features in different versions of Kerberos, we
8528 * need either the krb5_cc_new_unique() function or Heimdal's version
8529 * of krb5_cc_gen_new() to create a new FILE: credential cache that
8530 * can be passed to the backend. These functions create a temporary
8531 * file (typically in /tmp) containing the cached credentials, which
8532 * are removed when we have successfully printed a job.
8535 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
8536 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
8537 &(job
->ccache
))) != 0)
8538 # else /* HAVE_HEIMDAL */
8539 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
8540 &(job
->ccache
))) != 0)
8541 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
8543 cupsdLogMessage(CUPSD_LOG_ERROR
,
8544 "Unable to create new credentials cache (%d/%s)",
8545 error
, strerror(errno
));
8550 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
8552 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to parse kerberos username (%d/%s)",
8553 error
, strerror(errno
));
8554 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8559 if ((error
= krb5_cc_initialize(KerberosContext
, job
->ccache
, principal
)))
8561 cupsdLogMessage(CUPSD_LOG_ERROR
,
8562 "Unable to initialize credentials cache (%d/%s)", error
,
8564 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8565 krb5_free_principal(KerberosContext
, principal
);
8570 krb5_free_principal(KerberosContext
, principal
);
8573 * Copy the user's credentials to the new cache file...
8576 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
8579 if (GSS_ERROR(major_status
))
8581 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
8582 "Unable to import client credentials cache");
8583 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8589 * Add the KRB5CCNAME environment variable to the job so that the
8590 * backend can use the credentials when printing.
8593 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
8594 krb5_cc_get_name(KerberosContext
, job
->ccache
));
8596 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] save_krb5_creds: %s", job
->id
,
8598 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
8600 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8604 * 'send_document()' - Send a file to a printer or class.
8608 send_document(cupsd_client_t
*con
, /* I - Client connection */
8609 ipp_attribute_t
*uri
) /* I - Printer URI */
8611 ipp_attribute_t
*attr
; /* Current attribute */
8612 ipp_attribute_t
*format
; /* Document-format attribute */
8613 const char *default_format
;/* document-format-default value */
8614 int jobid
; /* Job ID number */
8615 cupsd_job_t
*job
; /* Current job */
8616 char job_uri
[HTTP_MAX_URI
],
8618 method
[HTTP_MAX_URI
],
8619 /* Method portion of URI */
8620 username
[HTTP_MAX_URI
],
8621 /* Username portion of URI */
8623 /* Host portion of URI */
8624 resource
[HTTP_MAX_URI
];
8625 /* Resource portion of URI */
8626 int port
; /* Port portion of URI */
8627 mime_type_t
*filetype
; /* Type of file */
8628 char super
[MIME_MAX_SUPER
],
8629 /* Supertype of file */
8630 type
[MIME_MAX_TYPE
],
8631 /* Subtype of file */
8632 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8633 /* Textual name of mime type */
8634 char filename
[1024]; /* Job filename */
8635 cupsd_printer_t
*printer
; /* Current printer */
8636 struct stat fileinfo
; /* File information */
8637 int kbytes
; /* Size of file */
8638 int compression
; /* Type of compression */
8641 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
8642 con
->http
.fd
, uri
->values
[0].string
.text
);
8645 * See if we have a job URI or a printer URI...
8648 if (!strcmp(uri
->name
, "printer-uri"))
8651 * Got a printer URI; see if we also have a job-id attribute...
8654 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8655 IPP_TAG_INTEGER
)) == NULL
)
8657 send_ipp_status(con
, IPP_BAD_REQUEST
,
8658 _("Got a printer-uri attribute but no job-id!"));
8662 jobid
= attr
->values
[0].integer
;
8667 * Got a job URI; parse it to get the job ID...
8670 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8671 sizeof(method
), username
, sizeof(username
), host
,
8672 sizeof(host
), &port
, resource
, sizeof(resource
));
8674 if (strncmp(resource
, "/jobs/", 6))
8680 send_ipp_status(con
, IPP_BAD_REQUEST
,
8681 _("Bad job-uri attribute \"%s\"!"),
8682 uri
->values
[0].string
.text
);
8686 jobid
= atoi(resource
+ 6);
8690 * See if the job exists...
8693 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8696 * Nope - return a "not found" error...
8699 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8703 printer
= cupsdFindDest(job
->dest
);
8706 * See if the job is owned by the requesting user...
8709 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8711 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8716 * OK, see if the client is sending the document compressed - CUPS
8717 * only supports "none" and "gzip".
8720 compression
= CUPS_FILE_NONE
;
8722 if ((attr
= ippFindAttribute(con
->request
, "compression",
8723 IPP_TAG_KEYWORD
)) != NULL
)
8725 if (strcmp(attr
->values
[0].string
.text
, "none")
8727 && strcmp(attr
->values
[0].string
.text
, "gzip")
8728 #endif /* HAVE_LIBZ */
8731 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
8732 attr
->values
[0].string
.text
);
8733 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8734 "compression", NULL
, attr
->values
[0].string
.text
);
8739 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8740 compression
= CUPS_FILE_GZIP
;
8741 #endif /* HAVE_LIBZ */
8745 * Do we have a file to print?
8750 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
8755 * Is it a format we support?
8758 if ((format
= ippFindAttribute(con
->request
, "document-format",
8759 IPP_TAG_MIMETYPE
)) != NULL
)
8762 * Grab format from client...
8765 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
8768 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
8769 format
->values
[0].string
.text
);
8773 else if ((default_format
= cupsGetOption("document-format",
8774 printer
->num_options
,
8775 printer
->options
)) != NULL
)
8778 * Use default document format...
8781 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
8783 send_ipp_status(con
, IPP_BAD_REQUEST
,
8784 _("Could not scan type \"%s\"!"),
8792 * No document format attribute? Auto-type it!
8795 strcpy(super
, "application");
8796 strcpy(type
, "octet-stream");
8799 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8802 * Auto-type the file...
8805 ipp_attribute_t
*doc_name
; /* document-name attribute */
8808 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
8810 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8811 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8812 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8816 filetype
= mimeType(MimeDatabase
, super
, type
);
8819 filetype
= mimeType(MimeDatabase
, super
, type
);
8823 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8826 * Replace the document-format attribute value with the auto-typed or
8830 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8835 _cupsStrFree(format
->values
[0].string
.text
);
8837 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8840 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8841 "document-format", NULL
, mimetype
);
8845 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8846 _("Unsupported format \'%s/%s\'!"), super
, type
);
8847 cupsdLogMessage(CUPSD_LOG_INFO
,
8848 "Hint: Do you have the raw file printing rules enabled?");
8851 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8852 "document-format", NULL
, format
->values
[0].string
.text
);
8857 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
8859 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8862 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8863 _("Unsupported format \'%s\'!"), mimetype
);
8865 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8866 "document-format", NULL
, mimetype
);
8871 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8872 "send_document: request file type is %s/%s.",
8873 filetype
->super
, filetype
->type
);
8876 * Add the file to the job...
8881 if (add_file(con
, job
, filetype
, compression
))
8884 if (stat(con
->filename
, &fileinfo
))
8887 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8889 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8891 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8892 IPP_TAG_INTEGER
)) != NULL
)
8893 attr
->values
[0].integer
+= kbytes
;
8895 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8897 rename(con
->filename
, filename
);
8899 cupsdClearString(&con
->filename
);
8901 cupsdLogMessage(CUPSD_LOG_INFO
,
8902 "File of type %s/%s queued in job #%d by \"%s\".",
8903 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
8906 * Start the job if this is the last document...
8909 if ((attr
= ippFindAttribute(con
->request
, "last-document",
8910 IPP_TAG_BOOLEAN
)) != NULL
&&
8911 attr
->values
[0].boolean
)
8914 * See if we need to add the ending sheet...
8917 if (cupsdTimeoutJob(job
))
8920 if (job
->state_value
== IPP_JOB_STOPPED
)
8922 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
8923 job
->state_value
= IPP_JOB_PENDING
;
8925 else if (job
->state_value
== IPP_JOB_HELD
)
8927 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8928 IPP_TAG_KEYWORD
)) == NULL
)
8929 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8931 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
8933 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
8934 job
->state_value
= IPP_JOB_PENDING
;
8941 * Start the job if possible... Since cupsdCheckJobs() can cancel a
8942 * job if it doesn't print, we need to re-find the job afterwards...
8949 job
= cupsdFindJob(jobid
);
8953 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8954 IPP_TAG_KEYWORD
)) == NULL
)
8955 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8957 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
8959 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
8960 job
->state_value
= IPP_JOB_HELD
;
8961 job
->hold_until
= time(NULL
) + 60;
8967 * Fill in the response info...
8970 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
8973 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
8976 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
8978 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
8979 job
? job
->state_value
: IPP_JOB_CANCELED
);
8980 add_job_state_reasons(con
, job
);
8982 con
->response
->request
.status
.status_code
= IPP_OK
;
8987 * 'send_http_error()' - Send a HTTP error back to the IPP client.
8992 cupsd_client_t
*con
, /* I - Client connection */
8993 http_status_t status
, /* I - HTTP status code */
8994 cupsd_printer_t
*printer
) /* I - Printer, if any */
8996 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
8997 ippOpString(con
->request
->request
.op
.operation_id
),
8998 httpStatus(status
));
9000 if (status
== HTTP_UNAUTHORIZED
&&
9001 printer
&& printer
->num_auth_info_required
> 0 &&
9002 !strcmp(printer
->auth_info_required
[0], "negotiate"))
9003 cupsdSendError(con
, status
, CUPSD_AUTH_NEGOTIATE
);
9006 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9007 cupsd_location_t
*auth
; /* Pointer to authentication element */
9010 if (printer
->type
& CUPS_PRINTER_CLASS
)
9011 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9013 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9015 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9016 auth
->type
== CUPSD_AUTH_NONE
)
9017 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
, IPP_PRINT_JOB
);
9019 cupsdSendError(con
, status
, auth
? auth
->type
: CUPSD_AUTH_NONE
);
9022 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
9024 ippDelete(con
->response
);
9025 con
->response
= NULL
;
9032 * 'send_ipp_status()' - Send a status back to the IPP client.
9036 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9037 ipp_status_t status
, /* I - IPP status code */
9038 const char *message
, /* I - Status message */
9039 ...) /* I - Additional args as needed */
9041 va_list ap
; /* Pointer to additional args */
9042 char formatted
[1024]; /* Formatted errror message */
9045 va_start(ap
, message
);
9046 vsnprintf(formatted
, sizeof(formatted
),
9047 _cupsLangString(con
->language
, message
), ap
);
9050 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9051 ippOpString(con
->request
->request
.op
.operation_id
),
9052 ippErrorString(status
), formatted
);
9054 con
->response
->request
.status
.status_code
= status
;
9056 if (ippFindAttribute(con
->response
, "attributes-charset",
9057 IPP_TAG_ZERO
) == NULL
)
9058 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9059 "attributes-charset", NULL
, DefaultCharset
);
9061 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9062 IPP_TAG_ZERO
) == NULL
)
9063 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9064 "attributes-natural-language", NULL
, DefaultLanguage
);
9066 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9067 "status-message", NULL
, formatted
);
9072 * 'set_default()' - Set the default destination...
9076 set_default(cupsd_client_t
*con
, /* I - Client connection */
9077 ipp_attribute_t
*uri
) /* I - Printer URI */
9079 http_status_t status
; /* Policy status */
9080 cups_ptype_t dtype
; /* Destination type (printer/class) */
9081 cupsd_printer_t
*printer
; /* Printer */
9084 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9085 con
->http
.fd
, uri
->values
[0].string
.text
);
9088 * Is the destination valid?
9091 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9097 send_ipp_status(con
, IPP_NOT_FOUND
,
9098 _("The printer or class was not found."));
9106 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9108 send_http_error(con
, status
, NULL
);
9113 * Set it as the default...
9116 DefaultPrinter
= printer
;
9118 cupsdSaveAllPrinters();
9119 cupsdSaveAllClasses();
9121 cupsdWritePrintcap();
9123 cupsdLogMessage(CUPSD_LOG_INFO
,
9124 "Default destination set to \"%s\" by \"%s\".",
9125 printer
->name
, get_username(con
));
9128 * Everything was ok, so return OK status...
9131 con
->response
->request
.status
.status_code
= IPP_OK
;
9136 * 'set_job_attrs()' - Set job attributes.
9140 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9141 ipp_attribute_t
*uri
) /* I - Job URI */
9143 ipp_attribute_t
*attr
, /* Current attribute */
9144 *attr2
; /* Job attribute */
9145 int jobid
; /* Job ID */
9146 cupsd_job_t
*job
; /* Current job */
9147 char method
[HTTP_MAX_URI
],
9148 /* Method portion of URI */
9149 username
[HTTP_MAX_URI
],
9150 /* Username portion of URI */
9152 /* Host portion of URI */
9153 resource
[HTTP_MAX_URI
];
9154 /* Resource portion of URI */
9155 int port
; /* Port portion of URI */
9156 int event
; /* Events? */
9159 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
9160 con
->http
.fd
, uri
->values
[0].string
.text
);
9163 * Start with "everything is OK" status...
9166 con
->response
->request
.status
.status_code
= IPP_OK
;
9169 * See if we have a job URI or a printer URI...
9172 if (!strcmp(uri
->name
, "printer-uri"))
9175 * Got a printer URI; see if we also have a job-id attribute...
9178 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9179 IPP_TAG_INTEGER
)) == NULL
)
9181 send_ipp_status(con
, IPP_BAD_REQUEST
,
9182 _("Got a printer-uri attribute but no job-id!"));
9186 jobid
= attr
->values
[0].integer
;
9191 * Got a job URI; parse it to get the job ID...
9194 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9195 sizeof(method
), username
, sizeof(username
), host
,
9196 sizeof(host
), &port
, resource
, sizeof(resource
));
9198 if (strncmp(resource
, "/jobs/", 6))
9204 send_ipp_status(con
, IPP_BAD_REQUEST
,
9205 _("Bad job-uri attribute \"%s\"!"),
9206 uri
->values
[0].string
.text
);
9210 jobid
= atoi(resource
+ 6);
9214 * See if the job exists...
9217 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9220 * Nope - return a "not found" error...
9223 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9228 * See if the job has been completed...
9231 if (job
->state_value
> IPP_JOB_STOPPED
)
9234 * Return a "not-possible" error...
9237 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9238 _("Job #%d is finished and cannot be altered!"), jobid
);
9243 * See if the job is owned by the requesting user...
9246 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9248 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9253 * See what the user wants to change.
9260 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9262 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
9265 if (!strcmp(attr
->name
, "attributes-charset") ||
9266 !strcmp(attr
->name
, "attributes-natural-language") ||
9267 !strcmp(attr
->name
, "document-compression") ||
9268 !strcmp(attr
->name
, "document-format") ||
9269 !strcmp(attr
->name
, "job-detailed-status-messages") ||
9270 !strcmp(attr
->name
, "job-document-access-errors") ||
9271 !strcmp(attr
->name
, "job-id") ||
9272 !strcmp(attr
->name
, "job-k-octets") ||
9273 !strcmp(attr
->name
, "job-originating-host-name") ||
9274 !strcmp(attr
->name
, "job-originating-user-name") ||
9275 !strcmp(attr
->name
, "job-printer-up-time") ||
9276 !strcmp(attr
->name
, "job-printer-uri") ||
9277 !strcmp(attr
->name
, "job-sheets") ||
9278 !strcmp(attr
->name
, "job-state-message") ||
9279 !strcmp(attr
->name
, "job-state-reasons") ||
9280 !strcmp(attr
->name
, "job-uri") ||
9281 !strcmp(attr
->name
, "number-of-documents") ||
9282 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
9283 !strcmp(attr
->name
, "output-device-assigned") ||
9284 !strncmp(attr
->name
, "date-time-at-", 13) ||
9285 !strncmp(attr
->name
, "job-impressions", 15) ||
9286 !strncmp(attr
->name
, "job-k-octets", 12) ||
9287 !strncmp(attr
->name
, "job-media-sheets", 16) ||
9288 !strncmp(attr
->name
, "time-at-", 8))
9294 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
9295 _("%s cannot be changed."), attr
->name
);
9297 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9298 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9303 if (!strcmp(attr
->name
, "job-priority"))
9306 * Change the job priority...
9309 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9311 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
9313 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9314 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9316 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
9318 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9319 _("Job is completed and cannot be changed."));
9322 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9324 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
9325 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9328 else if (!strcmp(attr
->name
, "job-state"))
9331 * Change the job state...
9334 if (attr
->value_tag
!= IPP_TAG_ENUM
)
9336 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
9338 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9339 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9343 switch (attr
->values
[0].integer
)
9345 case IPP_JOB_PENDING
:
9347 if (job
->state_value
> IPP_JOB_HELD
)
9349 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9350 _("Job state cannot be changed."));
9353 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9355 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
9356 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
9358 event
|= CUPSD_EVENT_JOB_STATE
;
9362 case IPP_JOB_PROCESSING
:
9363 case IPP_JOB_STOPPED
:
9364 if (job
->state_value
!= attr
->values
[0].integer
)
9366 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9367 _("Job state cannot be changed."));
9372 case IPP_JOB_CANCELED
:
9373 case IPP_JOB_ABORTED
:
9374 case IPP_JOB_COMPLETED
:
9375 if (job
->state_value
> IPP_JOB_PROCESSING
)
9377 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9378 _("Job state cannot be changed."));
9381 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9382 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
9387 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
9389 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9390 IPP_TAG_ZERO
)) != NULL
)
9393 * Some other value; first free the old value...
9396 if (job
->attrs
->prev
)
9397 job
->attrs
->prev
->next
= attr2
->next
;
9399 job
->attrs
->attrs
= attr2
->next
;
9401 if (job
->attrs
->last
== attr2
)
9402 job
->attrs
->last
= job
->attrs
->prev
;
9404 _ippFreeAttr(attr2
);
9407 * Then copy the attribute...
9410 copy_attribute(job
->attrs
, attr
, 0);
9413 * See if the job-name or job-hold-until is being changed.
9416 if (!strcmp(attr
->name
, "job-hold-until"))
9418 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
9420 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
9421 cupsdReleaseJob(job
);
9425 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
9428 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
9431 * Delete the attribute...
9434 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9435 IPP_TAG_ZERO
)) != NULL
)
9437 if (job
->attrs
->prev
)
9438 job
->attrs
->prev
->next
= attr2
->next
;
9440 job
->attrs
->attrs
= attr2
->next
;
9442 if (attr2
== job
->attrs
->last
)
9443 job
->attrs
->last
= job
->attrs
->prev
;
9445 _ippFreeAttr(attr2
);
9447 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9453 * Add new option by copying it...
9456 copy_attribute(job
->attrs
, attr
, 0);
9458 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9469 * Send events as needed...
9472 if (event
& CUPSD_EVENT_JOB_STATE
)
9473 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
9474 job
->state_value
== IPP_JOB_HELD
?
9475 "Job held by user." : "Job restarted by user.");
9477 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
9478 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
9479 "Job options changed by user.");
9482 * Start jobs if possible...
9490 * 'set_printer_defaults()' - Set printer default options from a request.
9494 set_printer_defaults(
9495 cupsd_client_t
*con
, /* I - Client connection */
9496 cupsd_printer_t
*printer
) /* I - Printer */
9498 int i
; /* Looping var */
9499 ipp_attribute_t
*attr
; /* Current attribute */
9500 int namelen
; /* Length of attribute name */
9501 char name
[256], /* New attribute name */
9502 value
[256]; /* String version of integer attrs */
9505 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9508 * Skip non-printer attributes...
9511 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
9514 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
9516 if (!strcmp(attr
->name
, "job-sheets-default"))
9519 * Only allow keywords and names...
9522 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9526 * Only allow job-sheets-default to be set when running without a
9527 * system high classification level...
9533 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
9535 if (attr
->num_values
> 1)
9536 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
9538 cupsdSetString(&printer
->job_sheets
[1], "none");
9540 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
9542 cupsdFreePrinterUsers(printer
);
9544 printer
->deny_users
= 0;
9546 if (attr
->value_tag
== IPP_TAG_NAME
&&
9547 (attr
->num_values
> 1 ||
9548 strcmp(attr
->values
[0].string
.text
, "all")))
9550 for (i
= 0; i
< attr
->num_values
; i
++)
9551 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9554 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
9556 cupsdFreePrinterUsers(printer
);
9558 printer
->deny_users
= 1;
9560 if (attr
->value_tag
== IPP_TAG_NAME
&&
9561 (attr
->num_values
> 1 ||
9562 strcmp(attr
->values
[0].string
.text
, "none")))
9564 for (i
= 0; i
< attr
->num_values
; i
++)
9565 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9568 else if (!strcmp(attr
->name
, "job-quota-period"))
9570 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9573 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
9574 attr
->values
[0].integer
);
9575 cupsdFreeQuotas(printer
);
9577 printer
->quota_period
= attr
->values
[0].integer
;
9579 else if (!strcmp(attr
->name
, "job-k-limit"))
9581 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9584 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
9585 attr
->values
[0].integer
);
9586 cupsdFreeQuotas(printer
);
9588 printer
->k_limit
= attr
->values
[0].integer
;
9590 else if (!strcmp(attr
->name
, "job-page-limit"))
9592 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9595 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
9596 attr
->values
[0].integer
);
9597 cupsdFreeQuotas(printer
);
9599 printer
->page_limit
= attr
->values
[0].integer
;
9601 else if (!strcmp(attr
->name
, "printer-op-policy"))
9603 cupsd_policy_t
*p
; /* Policy */
9606 if (attr
->value_tag
!= IPP_TAG_NAME
)
9609 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
9611 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9612 "Setting printer-op-policy to \"%s\"...",
9613 attr
->values
[0].string
.text
);
9614 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
9615 printer
->op_policy_ptr
= p
;
9619 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9620 _("Unknown printer-op-policy \"%s\"."),
9621 attr
->values
[0].string
.text
);
9625 else if (!strcmp(attr
->name
, "printer-error-policy"))
9627 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9630 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
9631 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
9632 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
9634 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9635 _("Unknown printer-error-policy \"%s\"."),
9636 attr
->values
[0].string
.text
);
9640 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9641 "Setting printer-error-policy to \"%s\"...",
9642 attr
->values
[0].string
.text
);
9643 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
9647 * Skip any other non-default attributes...
9650 namelen
= strlen(attr
->name
);
9651 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
9652 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
9656 * OK, anything else must be a user-defined default...
9659 strlcpy(name
, attr
->name
, sizeof(name
));
9660 name
[namelen
- 8] = '\0'; /* Strip "-default" */
9662 switch (attr
->value_tag
)
9664 case IPP_TAG_DELETEATTR
:
9665 printer
->num_options
= cupsRemoveOption(name
,
9666 printer
->num_options
,
9667 &(printer
->options
));
9668 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9669 "Deleting %s", attr
->name
);
9673 case IPP_TAG_KEYWORD
:
9675 printer
->num_options
= cupsAddOption(name
,
9676 attr
->values
[0].string
.text
,
9677 printer
->num_options
,
9678 &(printer
->options
));
9679 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9680 "Setting %s to \"%s\"...", attr
->name
,
9681 attr
->values
[0].string
.text
);
9684 case IPP_TAG_BOOLEAN
:
9685 printer
->num_options
= cupsAddOption(name
,
9686 attr
->values
[0].boolean
?
9688 printer
->num_options
,
9689 &(printer
->options
));
9690 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9691 "Setting %s to %s...", attr
->name
,
9692 attr
->values
[0].boolean
? "true" : "false");
9695 case IPP_TAG_INTEGER
:
9697 sprintf(value
, "%d", attr
->values
[0].integer
);
9698 printer
->num_options
= cupsAddOption(name
, value
,
9699 printer
->num_options
,
9700 &(printer
->options
));
9701 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9702 "Setting %s to %s...", attr
->name
, value
);
9705 case IPP_TAG_RANGE
:
9706 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
9707 attr
->values
[0].range
.upper
);
9708 printer
->num_options
= cupsAddOption(name
, value
,
9709 printer
->num_options
,
9710 &(printer
->options
));
9711 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9712 "Setting %s to %s...", attr
->name
, value
);
9715 case IPP_TAG_RESOLUTION
:
9716 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
9717 attr
->values
[0].resolution
.yres
,
9718 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
9720 printer
->num_options
= cupsAddOption(name
, value
,
9721 printer
->num_options
,
9722 &(printer
->options
));
9723 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9724 "Setting %s to %s...", attr
->name
, value
);
9728 /* Do nothing for other values */
9736 * 'start_printer()' - Start a printer.
9740 start_printer(cupsd_client_t
*con
, /* I - Client connection */
9741 ipp_attribute_t
*uri
) /* I - Printer URI */
9743 http_status_t status
; /* Policy status */
9744 cups_ptype_t dtype
; /* Destination type (printer/class) */
9745 cupsd_printer_t
*printer
; /* Printer data */
9748 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
9749 con
->http
.fd
, uri
->values
[0].string
.text
);
9752 * Is the destination valid?
9755 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9761 send_ipp_status(con
, IPP_NOT_FOUND
,
9762 _("The printer or class was not found."));
9770 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9772 send_http_error(con
, status
, printer
);
9777 * Start the printer...
9780 printer
->state_message
[0] = '\0';
9782 cupsdStartPrinter(printer
, 1);
9784 if (dtype
& CUPS_PRINTER_CLASS
)
9785 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
9786 printer
->name
, get_username(con
));
9788 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
9789 printer
->name
, get_username(con
));
9794 * Everything was ok, so return OK status...
9797 con
->response
->request
.status
.status_code
= IPP_OK
;
9802 * 'stop_printer()' - Stop a printer.
9806 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
9807 ipp_attribute_t
*uri
) /* I - Printer URI */
9809 http_status_t status
; /* Policy status */
9810 cups_ptype_t dtype
; /* Destination type (printer/class) */
9811 cupsd_printer_t
*printer
; /* Printer data */
9812 ipp_attribute_t
*attr
; /* printer-state-message attribute */
9815 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
9816 con
->http
.fd
, uri
->values
[0].string
.text
);
9819 * Is the destination valid?
9822 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9828 send_ipp_status(con
, IPP_NOT_FOUND
,
9829 _("The printer or class was not found."));
9837 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9839 send_http_error(con
, status
, printer
);
9844 * Stop the printer...
9847 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
9848 IPP_TAG_TEXT
)) == NULL
)
9849 strcpy(printer
->state_message
, "Paused");
9852 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
9853 sizeof(printer
->state_message
));
9856 cupsdStopPrinter(printer
, 1);
9858 if (dtype
& CUPS_PRINTER_CLASS
)
9859 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
9860 printer
->name
, get_username(con
));
9862 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
9863 printer
->name
, get_username(con
));
9866 * Everything was ok, so return OK status...
9869 con
->response
->request
.status
.status_code
= IPP_OK
;
9874 * 'url_encode_attr()' - URL-encode a string attribute.
9878 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
9879 char *buffer
,/* I - String buffer */
9880 int bufsize
)/* I - Size of buffer */
9882 int i
; /* Looping var */
9883 char *bufptr
, /* Pointer into buffer */
9884 *bufend
; /* End of buffer */
9887 strlcpy(buffer
, attr
->name
, bufsize
);
9888 bufptr
= buffer
+ strlen(buffer
);
9889 bufend
= buffer
+ bufsize
- 1;
9891 for (i
= 0; i
< attr
->num_values
; i
++)
9893 if (bufptr
>= bufend
)
9901 if (bufptr
>= bufend
)
9906 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
9907 bufptr
, bufend
- bufptr
+ 1);
9909 if (bufptr
>= bufend
)
9920 * 'url_encode_string()' - URL-encode a string.
9923 static char * /* O - End of string */
9924 url_encode_string(const char *s
, /* I - String */
9925 char *buffer
, /* I - String buffer */
9926 int bufsize
) /* I - Size of buffer */
9928 char *bufptr
, /* Pointer into buffer */
9929 *bufend
; /* End of buffer */
9930 static const char *hex
= "0123456789ABCDEF";
9935 bufend
= buffer
+ bufsize
- 1;
9937 while (*s
&& bufptr
< bufend
)
9939 if (*s
== ' ' || *s
== '%')
9941 if (bufptr
>= (bufend
- 2))
9945 *bufptr
++ = hex
[(*s
>> 4) & 15];
9946 *bufptr
++ = hex
[*s
& 15];
9950 else if (*s
== '\'' || *s
== '\\')
9952 if (bufptr
>= (bufend
- 1))
9969 * 'user_allowed()' - See if a user is allowed to print to a queue.
9972 static int /* O - 0 if not allowed, 1 if allowed */
9973 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
9974 const char *username
) /* I - Username */
9976 int i
; /* Looping var */
9977 struct passwd
*pw
; /* User password data */
9978 char baseuser
[256], /* Base username */
9979 *baseptr
; /* Pointer to "@" in base username */
9982 if (p
->num_users
== 0)
9985 if (!strcmp(username
, "root"))
9988 if (strchr(username
, '@'))
9991 * Strip @REALM for username check...
9994 strlcpy(baseuser
, username
, sizeof(baseuser
));
9996 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
9999 username
= baseuser
;
10002 pw
= getpwnam(username
);
10005 for (i
= 0; i
< p
->num_users
; i
++)
10007 if (p
->users
[i
][0] == '@')
10010 * Check group membership...
10013 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
10016 else if (!strcasecmp(username
, p
->users
[i
]))
10020 return ((i
< p
->num_users
) != p
->deny_users
);
10025 * 'validate_job()' - Validate printer options and destination.
10029 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10030 ipp_attribute_t
*uri
) /* I - Printer URI */
10032 http_status_t status
; /* Policy status */
10033 ipp_attribute_t
*attr
; /* Current attribute */
10034 ipp_attribute_t
*format
; /* Document-format attribute */
10035 cups_ptype_t dtype
; /* Destination type (printer/class) */
10036 char super
[MIME_MAX_SUPER
],
10037 /* Supertype of file */
10038 type
[MIME_MAX_TYPE
];
10039 /* Subtype of file */
10040 cupsd_printer_t
*printer
; /* Printer */
10043 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10044 con
->http
.fd
, uri
->values
[0].string
.text
);
10047 * OK, see if the client is sending the document compressed - CUPS
10048 * doesn't support compression yet...
10051 if ((attr
= ippFindAttribute(con
->request
, "compression",
10052 IPP_TAG_KEYWORD
)) != NULL
&&
10053 !strcmp(attr
->values
[0].string
.text
, "none"))
10055 send_ipp_status(con
, IPP_ATTRIBUTES
,
10056 _("Unsupported compression attribute %s!"),
10057 attr
->values
[0].string
.text
);
10058 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10059 "compression", NULL
, attr
->values
[0].string
.text
);
10064 * Is it a format we support?
10067 if ((format
= ippFindAttribute(con
->request
, "document-format",
10068 IPP_TAG_MIMETYPE
)) != NULL
)
10070 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
10073 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
10074 format
->values
[0].string
.text
);
10078 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10079 !mimeType(MimeDatabase
, super
, type
))
10081 cupsdLogMessage(CUPSD_LOG_INFO
,
10082 "Hint: Do you have the raw file printing rules enabled?");
10083 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10084 _("Unsupported format \"%s\"!"),
10085 format
->values
[0].string
.text
);
10086 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10087 "document-format", NULL
, format
->values
[0].string
.text
);
10093 * Is the destination valid?
10096 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10102 send_ipp_status(con
, IPP_NOT_FOUND
,
10103 _("The printer or class was not found."));
10111 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10113 send_http_error(con
, status
, printer
);
10118 * Everything was ok, so return OK status...
10121 con
->response
->request
.status
.status_code
= IPP_OK
;
10126 * 'validate_name()' - Make sure the printer name only contains valid chars.
10129 static int /* O - 0 if name is no good, 1 if good */
10130 validate_name(const char *name
) /* I - Name to check */
10132 const char *ptr
; /* Pointer into name */
10136 * Scan the whole name...
10139 for (ptr
= name
; *ptr
; ptr
++)
10140 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
10144 * All the characters are good; validate the length, too...
10147 return ((ptr
- name
) < 128);
10152 * 'validate_user()' - Validate the user for the request.
10155 static int /* O - 1 if permitted, 0 otherwise */
10156 validate_user(cupsd_job_t
*job
, /* I - Job */
10157 cupsd_client_t
*con
, /* I - Client connection */
10158 const char *owner
, /* I - Owner of job/resource */
10159 char *username
, /* O - Authenticated username */
10160 int userlen
) /* I - Length of username */
10162 cupsd_printer_t
*printer
; /* Printer for job */
10165 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
10166 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
10168 job
->id
, con
? con
->http
.fd
: 0,
10169 owner
? owner
: "(null)", username
, userlen
);
10172 * Validate input...
10175 if (!con
|| !owner
|| !username
|| userlen
<= 0)
10179 * Get the best authenticated username that is available.
10182 strlcpy(username
, get_username(con
), userlen
);
10185 * Check the username against the owner...
10188 printer
= cupsdFindDest(job
->dest
);
10190 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
10191 con
, owner
) == HTTP_OK
);
10196 * End of "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $".