2 * "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $"
4 * IPP routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
20 * cupsdProcessIPPRequest() - Process an incoming IPP request.
21 * cupsdTimeoutJob() - Timeout a job waiting on job files.
22 * accept_jobs() - Accept print jobs to a printer.
23 * add_class() - Add a class to the system.
24 * add_file() - Add a file to a job.
25 * add_job() - Add a job to a print queue.
26 * add_job_state_reasons() - Add the "job-state-reasons" attribute based
27 * upon the job and printer state...
28 * add_job_subscriptions() - Add any subcriptions for a job.
29 * add_job_uuid() - Add job-uuid attribute to a job.
30 * add_printer() - Add a printer to the system.
31 * add_printer_state_reasons() - Add the "printer-state-reasons" attribute
32 * based upon the printer state...
33 * add_queued_job_count() - Add the "queued-job-count" attribute for
34 * apply_printer_defaults() - Apply printer default options to a job.
35 * authenticate_job() - Set job authentication info.
36 * cancel_all_jobs() - Cancel all print jobs.
37 * cancel_job() - Cancel a print job.
38 * cancel_subscription() - Cancel a subscription.
39 * check_quotas() - Check quotas for a printer and user.
40 * copy_attribute() - Copy a single attribute.
41 * copy_attrs() - Copy attributes from one request to another.
42 * copy_banner() - Copy a banner file to the requests directory
43 * for the specified job.
44 * copy_file() - Copy a PPD file or interface script...
45 * copy_model() - Copy a PPD model file, substituting default
47 * copy_job_attrs() - Copy job attributes.
48 * copy_printer_attrs() - Copy printer attributes.
49 * copy_subscription_attrs() - Copy subscription attributes.
50 * create_job() - Print a file to a printer or class.
51 * create_requested_array() - Create an array for the requested-attributes.
52 * create_subscription() - Create a notification subscription.
53 * delete_printer() - Remove a printer or class from the system.
54 * get_default() - Get the default destination.
55 * get_devices() - Get the list of available devices on the
57 * get_document() - Get a copy of a job file.
58 * get_job_attrs() - Get job attributes.
59 * get_jobs() - Get a list of jobs for the specified printer.
60 * get_notifications() - Get events for a subscription.
61 * get_ppd() - Get a named PPD from the local system.
62 * get_ppds() - Get the list of PPD files on the local
64 * get_printer_attrs() - Get printer attributes.
65 * get_printers() - Get a list of printers.
66 * get_subscription_attrs() - Get subscription attributes.
67 * get_subscriptions() - Get subscriptions.
68 * get_username() - Get the username associated with a request.
69 * hold_job() - Hold a print job.
70 * move_job() - Move a job to a new destination.
71 * ppd_parse_line() - Parse a PPD default line.
72 * print_job() - Print a file to a printer or class.
73 * read_ps_line() - Read a line from a PS file...
74 * read_ps_job_ticket() - Reads a job ticket embedded in a PS file.
75 * reject_jobs() - Reject print jobs to a printer.
76 * release_job() - Release a held print job.
77 * restart_job() - Restart an old print job.
78 * save_auth_info() - Save authentication information for a job.
79 * save_krb5_creds() - Save Kerberos credentials for a job.
80 * send_document() - Send a file to a printer or class.
81 * send_http_error() - Send a HTTP error back to the IPP client.
82 * send_ipp_status() - Send a status back to the IPP client.
83 * set_default() - Set the default destination...
84 * set_job_attrs() - Set job attributes.
85 * set_printer_defaults() - Set printer default options from a request.
86 * start_printer() - Start a printer.
87 * stop_printer() - Stop a printer.
88 * url_encode_attr() - URL-encode a string attribute.
89 * url_encode_string() - URL-encode a string.
90 * user_allowed() - See if a user is allowed to print to a queue.
91 * validate_job() - Validate printer options and destination.
92 * validate_name() - Make sure the printer name only contains
94 * validate_user() - Validate the user for the request.
98 * Include necessary headers...
105 #endif /* HAVE_LIBPAPER */
108 # ifdef HAVE_MEMBERSHIP_H
109 # include <membership.h>
110 # endif /* HAVE_MEMBERSHIP_H */
111 # ifdef HAVE_MEMBERSHIPPRIV_H
112 # include <membershipPriv.h>
114 extern int mbr_user_name_to_uuid(const char* name
, uuid_t uu
);
115 extern int mbr_group_name_to_uuid(const char* name
, uuid_t uu
);
116 extern int mbr_check_membership_by_id(uuid_t user
, gid_t group
, int* ismember
);
117 # endif /* HAVE_MEMBERSHIPPRIV_H */
118 #endif /* __APPLE__ */
125 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
126 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
127 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
128 mime_type_t
*filetype
, int compression
);
129 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
130 mime_type_t
*filetype
);
131 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
132 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
133 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
134 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
135 static void add_printer_state_reasons(cupsd_client_t
*con
,
137 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
138 static void apply_printer_defaults(cupsd_printer_t
*printer
,
140 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
141 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
142 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
143 static void cancel_subscription(cupsd_client_t
*con
, int id
);
144 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
145 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
147 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
148 ipp_tag_t group
, int quickcopy
);
149 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
151 static int copy_file(const char *from
, const char *to
);
152 static int copy_model(cupsd_client_t
*con
, const char *from
,
154 static void copy_job_attrs(cupsd_client_t
*con
,
157 static void copy_printer_attrs(cupsd_client_t
*con
,
158 cupsd_printer_t
*printer
,
160 static void copy_subscription_attrs(cupsd_client_t
*con
,
161 cupsd_subscription_t
*sub
,
163 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
164 static cups_array_t
*create_requested_array(ipp_t
*request
);
165 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
166 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
167 static void get_default(cupsd_client_t
*con
);
168 static void get_devices(cupsd_client_t
*con
);
169 static void get_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
170 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
171 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
172 static void get_notifications(cupsd_client_t
*con
);
173 static void get_ppd(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
174 static void get_ppds(cupsd_client_t
*con
);
175 static void get_printers(cupsd_client_t
*con
, int type
);
176 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
177 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
178 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
179 static const char *get_username(cupsd_client_t
*con
);
180 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
181 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
182 static int ppd_parse_line(const char *line
, char *option
, int olen
,
183 char *choice
, int clen
);
184 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
185 static void read_ps_job_ticket(cupsd_client_t
*con
);
186 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
187 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
188 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
189 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
190 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
,
191 ipp_attribute_t
*auth_info
);
192 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
193 static void save_krb5_creds(cupsd_client_t
*con
, cupsd_job_t
*job
);
194 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
195 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
196 static void send_http_error(cupsd_client_t
*con
, http_status_t status
,
197 cupsd_printer_t
*printer
);
198 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
199 const char *message
, ...)
201 __attribute__ ((__format__ (__printf__
, 3, 4)))
202 # endif /* __GNUC__ */
204 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
205 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
206 static void set_printer_defaults(cupsd_client_t
*con
,
207 cupsd_printer_t
*printer
);
208 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
209 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
210 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
,
212 static char *url_encode_string(const char *s
, char *buffer
, int bufsize
);
213 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
214 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
215 static int validate_name(const char *name
);
216 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
,
217 const char *owner
, char *username
,
222 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
225 int /* O - 1 on success, 0 on failure */
226 cupsdProcessIPPRequest(
227 cupsd_client_t
*con
) /* I - Client connection */
229 ipp_tag_t group
; /* Current group tag */
230 ipp_attribute_t
*attr
; /* Current attribute */
231 ipp_attribute_t
*charset
; /* Character set attribute */
232 ipp_attribute_t
*language
; /* Language attribute */
233 ipp_attribute_t
*uri
; /* Printer URI attribute */
234 ipp_attribute_t
*username
; /* requesting-user-name attr */
235 int sub_id
; /* Subscription ID */
238 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
239 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
240 con
, con
->http
.fd
, con
->request
->request
.op
.operation_id
);
243 * First build an empty response message for this request...
246 con
->response
= ippNew();
248 con
->response
->request
.status
.version
[0] =
249 con
->request
->request
.op
.version
[0];
250 con
->response
->request
.status
.version
[1] =
251 con
->request
->request
.op
.version
[1];
252 con
->response
->request
.status
.request_id
=
253 con
->request
->request
.op
.request_id
;
256 * Then validate the request header and required attributes...
259 if (con
->request
->request
.any
.version
[0] != 1)
262 * Return an error, since we only support IPP 1.x.
265 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
266 "%04X %s Bad request version number %d.%d",
267 IPP_VERSION_NOT_SUPPORTED
, con
->http
.hostname
,
268 con
->request
->request
.any
.version
[0],
269 con
->request
->request
.any
.version
[1]);
271 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
272 _("Bad request version number %d.%d!"),
273 con
->request
->request
.any
.version
[0],
274 con
->request
->request
.any
.version
[1]);
276 else if (!con
->request
->attrs
)
278 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
279 "%04X %s No attributes in request",
280 IPP_BAD_REQUEST
, con
->http
.hostname
);
282 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request!"));
287 * Make sure that the attributes are provided in the correct order and
288 * don't repeat groups...
291 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
294 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
297 * Out of order; return an error...
300 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
301 "%04X %s Attribute groups are out of order",
302 IPP_BAD_REQUEST
, con
->http
.hostname
);
304 send_ipp_status(con
, IPP_BAD_REQUEST
,
305 _("Attribute groups are out of order (%x < %x)!"),
306 attr
->group_tag
, group
);
310 group
= attr
->group_tag
;
315 * Then make sure that the first three attributes are:
318 * attributes-natural-language
319 * printer-uri/job-uri
322 attr
= con
->request
->attrs
;
323 if (attr
&& !strcmp(attr
->name
, "attributes-charset") &&
324 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
332 if (attr
&& !strcmp(attr
->name
, "attributes-natural-language") &&
333 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
338 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
339 IPP_TAG_URI
)) != NULL
)
341 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
342 IPP_TAG_URI
)) != NULL
)
344 else if (con
->request
->request
.op
.operation_id
== CUPS_GET_PPD
)
345 uri
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
);
350 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
351 "attributes-charset", NULL
,
352 charset
->values
[0].string
.text
);
354 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
355 "attributes-charset", NULL
, DefaultCharset
);
358 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
359 "attributes-natural-language", NULL
,
360 language
->values
[0].string
.text
);
362 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
363 "attributes-natural-language", NULL
, DefaultLanguage
);
366 strcasecmp(charset
->values
[0].string
.text
, "us-ascii") &&
367 strcasecmp(charset
->values
[0].string
.text
, "utf-8"))
370 * Bad character set...
373 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unsupported character set \"%s\"!",
374 charset
->values
[0].string
.text
);
375 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
376 "%04X %s Unsupported attributes-charset value \"%s\"",
377 IPP_CHARSET
, con
->http
.hostname
,
378 charset
->values
[0].string
.text
);
379 send_ipp_status(con
, IPP_BAD_REQUEST
,
380 _("Unsupported character set \"%s\"!"),
381 charset
->values
[0].string
.text
);
383 else if (!charset
|| !language
||
385 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
386 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
387 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
388 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
389 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
392 * Return an error, since attributes-charset,
393 * attributes-natural-language, and printer-uri/job-uri are required
394 * for all operations.
399 cupsdLogMessage(CUPSD_LOG_ERROR
,
400 "Missing attributes-charset attribute!");
402 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
403 "%04X %s Missing attributes-charset attribute",
404 IPP_BAD_REQUEST
, con
->http
.hostname
);
409 cupsdLogMessage(CUPSD_LOG_ERROR
,
410 "Missing attributes-natural-language attribute!");
412 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
413 "%04X %s Missing attributes-natural-language attribute",
414 IPP_BAD_REQUEST
, con
->http
.hostname
);
419 cupsdLogMessage(CUPSD_LOG_ERROR
,
420 "Missing printer-uri, job-uri, or ppd-name "
423 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
424 "%04X %s Missing printer-uri, job-uri, or ppd-name "
425 "attribute", IPP_BAD_REQUEST
, con
->http
.hostname
);
428 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
430 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
431 cupsdLogMessage(CUPSD_LOG_DEBUG
,
432 "attr \"%s\": group_tag = %x, value_tag = %x",
433 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
436 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
438 send_ipp_status(con
, IPP_BAD_REQUEST
,
439 _("Missing required attributes!"));
444 * OK, all the checks pass so far; make sure requesting-user-name is
445 * not "root" from a remote host...
448 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
449 IPP_TAG_NAME
)) != NULL
)
452 * Check for root user...
455 if (!strcmp(username
->values
[0].string
.text
, "root") &&
456 strcasecmp(con
->http
.hostname
, "localhost") &&
457 strcmp(con
->username
, "root"))
460 * Remote unauthenticated user masquerading as local root...
463 _cupsStrFree(username
->values
[0].string
.text
);
464 username
->values
[0].string
.text
= _cupsStrAlloc(RemoteRoot
);
468 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
469 IPP_TAG_INTEGER
)) != NULL
)
470 sub_id
= attr
->values
[0].integer
;
475 * Then try processing the operation...
479 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
480 ippOpString(con
->request
->request
.op
.operation_id
),
481 uri
->values
[0].string
.text
);
483 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
484 ippOpString(con
->request
->request
.op
.operation_id
));
486 switch (con
->request
->request
.op
.operation_id
)
492 case IPP_VALIDATE_JOB
:
493 validate_job(con
, uri
);
496 case IPP_CREATE_JOB
:
497 create_job(con
, uri
);
500 case IPP_SEND_DOCUMENT
:
501 send_document(con
, uri
);
504 case IPP_CANCEL_JOB
:
505 cancel_job(con
, uri
);
508 case IPP_GET_JOB_ATTRIBUTES
:
509 get_job_attrs(con
, uri
);
516 case IPP_GET_PRINTER_ATTRIBUTES
:
517 get_printer_attrs(con
, uri
);
524 case IPP_RELEASE_JOB
:
525 release_job(con
, uri
);
528 case IPP_RESTART_JOB
:
529 restart_job(con
, uri
);
532 case IPP_PAUSE_PRINTER
:
533 stop_printer(con
, uri
);
536 case IPP_RESUME_PRINTER
:
537 start_printer(con
, uri
);
540 case IPP_PURGE_JOBS
:
541 cancel_all_jobs(con
, uri
);
544 case IPP_SET_JOB_ATTRIBUTES
:
545 set_job_attrs(con
, uri
);
548 case CUPS_GET_DEFAULT
:
552 case CUPS_GET_PRINTERS
:
553 get_printers(con
, 0);
556 case CUPS_GET_CLASSES
:
557 get_printers(con
, CUPS_PRINTER_CLASS
);
560 case CUPS_ADD_PRINTER
:
561 add_printer(con
, uri
);
564 case CUPS_DELETE_PRINTER
:
565 delete_printer(con
, uri
);
568 case CUPS_ADD_CLASS
:
572 case CUPS_DELETE_CLASS
:
573 delete_printer(con
, uri
);
576 case CUPS_ACCEPT_JOBS
:
577 case IPP_ENABLE_PRINTER
:
578 accept_jobs(con
, uri
);
581 case CUPS_REJECT_JOBS
:
582 case IPP_DISABLE_PRINTER
:
583 reject_jobs(con
, uri
);
586 case CUPS_SET_DEFAULT
:
587 set_default(con
, uri
);
590 case CUPS_GET_DEVICES
:
594 case CUPS_GET_DOCUMENT
:
595 get_document(con
, uri
);
610 case CUPS_AUTHENTICATE_JOB
:
611 authenticate_job(con
, uri
);
614 case IPP_CREATE_PRINTER_SUBSCRIPTION
:
615 case IPP_CREATE_JOB_SUBSCRIPTION
:
616 create_subscription(con
, uri
);
619 case IPP_GET_SUBSCRIPTION_ATTRIBUTES
:
620 get_subscription_attrs(con
, sub_id
);
623 case IPP_GET_SUBSCRIPTIONS
:
624 get_subscriptions(con
, uri
);
627 case IPP_RENEW_SUBSCRIPTION
:
628 renew_subscription(con
, sub_id
);
631 case IPP_CANCEL_SUBSCRIPTION
:
632 cancel_subscription(con
, sub_id
);
635 case IPP_GET_NOTIFICATIONS
:
636 get_notifications(con
);
640 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
641 "%04X %s Operation %04X (%s) not supported",
642 IPP_OPERATION_NOT_SUPPORTED
, con
->http
.hostname
,
643 con
->request
->request
.op
.operation_id
,
644 ippOpString(con
->request
->request
.op
.operation_id
));
646 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
647 _("%s not supported!"),
649 con
->request
->request
.op
.operation_id
));
659 * Sending data from the scheduler...
662 cupsdLogMessage(CUPSD_LOG_DEBUG
,
663 "cupsdProcessIPPRequest: %d status_code=%x (%s)",
664 con
->http
.fd
, con
->response
->request
.status
.status_code
,
665 ippErrorString(con
->response
->request
.status
.status_code
));
667 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp", AUTH_NONE
))
669 #ifdef CUPSD_USE_CHUNKING
671 * Because older versions of CUPS (1.1.17 and older) and some IPP
672 * clients do not implement chunking properly, we cannot use
673 * chunking by default. This may become the default in future
674 * CUPS releases, or we might add a configuration directive for
678 if (con
->http
.version
== HTTP_1_1
)
680 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n") < 0)
683 if (cupsdFlushHeader(con
) < 0)
686 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
689 #endif /* CUPSD_USE_CHUNKING */
691 size_t length
; /* Length of response */
694 length
= ippLength(con
->response
);
696 if (con
->file
>= 0 && !con
->pipe_pid
)
698 struct stat fileinfo
; /* File information */
701 if (!fstat(con
->file
, &fileinfo
))
702 length
+= fileinfo
.st_size
;
705 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
706 CUPS_LLCAST length
) < 0)
709 if (cupsdFlushHeader(con
) < 0)
712 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
713 con
->http
.data_remaining
= length
;
715 if (con
->http
.data_remaining
<= INT_MAX
)
716 con
->http
._data_remaining
= con
->http
.data_remaining
;
718 con
->http
._data_remaining
= INT_MAX
;
721 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
722 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
725 * Tell the caller the response header was sent successfully...
733 * Tell the caller the response header could not be sent...
742 * Sending data from a subprocess like cups-deviced; tell the caller
743 * everything is A-OK so far...
752 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
756 cupsdTimeoutJob(cupsd_job_t
*job
) /* I - Job to timeout */
758 cupsd_printer_t
*printer
; /* Destination printer or class */
759 ipp_attribute_t
*attr
; /* job-sheets attribute */
760 int kbytes
; /* Kilobytes in banner */
763 job
->pending_timeout
= 0;
766 * See if we need to add the ending sheet...
769 printer
= cupsdFindDest(job
->dest
);
770 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
773 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
774 attr
&& attr
->num_values
> 1)
780 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Adding end banner page \"%s\".",
781 job
->id
, attr
->values
[1].string
.text
);
783 kbytes
= copy_banner(NULL
, job
, attr
->values
[1].string
.text
);
785 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
791 * 'accept_jobs()' - Accept print jobs to a printer.
795 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
796 ipp_attribute_t
*uri
) /* I - Printer or class URI */
798 http_status_t status
; /* Policy status */
799 cups_ptype_t dtype
; /* Destination type (printer/class) */
800 cupsd_printer_t
*printer
; /* Printer data */
803 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
804 con
->http
.fd
, uri
->values
[0].string
.text
);
807 * Is the destination valid?
810 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
816 send_ipp_status(con
, IPP_NOT_FOUND
,
817 _("The printer or class was not found."));
825 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
827 send_http_error(con
, status
, printer
);
832 * Accept jobs sent to the printer...
835 printer
->accepting
= 1;
836 printer
->state_message
[0] = '\0';
838 cupsdAddPrinterHistory(printer
);
840 if (dtype
& CUPS_PRINTER_CLASS
)
842 cupsdSaveAllClasses();
844 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
845 printer
->name
, get_username(con
));
849 cupsdSaveAllPrinters();
851 cupsdLogMessage(CUPSD_LOG_INFO
,
852 "Printer \"%s\" now accepting jobs (\"%s\").",
853 printer
->name
, get_username(con
));
857 * Everything was ok, so return OK status...
860 con
->response
->request
.status
.status_code
= IPP_OK
;
865 * 'add_class()' - Add a class to the system.
869 add_class(cupsd_client_t
*con
, /* I - Client connection */
870 ipp_attribute_t
*uri
) /* I - URI of class */
872 http_status_t status
; /* Policy status */
873 int i
; /* Looping var */
874 char method
[HTTP_MAX_URI
], /* Method portion of URI */
875 username
[HTTP_MAX_URI
], /* Username portion of URI */
876 host
[HTTP_MAX_URI
], /* Host portion of URI */
877 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
878 int port
; /* Port portion of URI */
879 cupsd_printer_t
*pclass
, /* Class */
880 *member
; /* Member printer/class */
881 cups_ptype_t dtype
; /* Destination type */
882 ipp_attribute_t
*attr
; /* Printer attribute */
883 int modify
; /* Non-zero if we just modified */
884 char newname
[IPP_MAX_NAME
]; /* New class name */
885 int need_restart_job
; /* Need to restart job? */
888 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
889 con
->http
.fd
, uri
->values
[0].string
.text
);
892 * Do we have a valid URI?
895 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
896 sizeof(method
), username
, sizeof(username
), host
,
897 sizeof(host
), &port
, resource
, sizeof(resource
));
900 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
903 * No, return an error...
906 send_ipp_status(con
, IPP_BAD_REQUEST
,
907 _("The printer-uri must be of the form "
908 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
913 * Do we have a valid printer name?
916 if (!validate_name(resource
+ 9))
919 * No, return an error...
922 send_ipp_status(con
, IPP_BAD_REQUEST
,
923 _("The printer-uri \"%s\" contains invalid characters."),
924 uri
->values
[0].string
.text
);
929 * See if the class already exists; if not, create a new class...
932 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
935 * Class doesn't exist; see if we have a printer of the same name...
938 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
939 !(pclass
->type
& CUPS_PRINTER_DISCOVERED
))
942 * Yes, return an error...
945 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
946 _("A printer named \"%s\" already exists!"),
952 * No, check the default policy and then add the class...
955 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
957 send_http_error(con
, status
, NULL
);
961 pclass
= cupsdAddClass(resource
+ 9);
964 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
967 * Check the default policy, then rename the implicit class to "AnyClass"
971 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
973 send_http_error(con
, status
, NULL
);
977 if (ImplicitAnyClasses
)
979 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
980 cupsdRenamePrinter(pclass
, newname
);
983 cupsdDeletePrinter(pclass
, 1);
986 * Add the class as a new local class...
989 pclass
= cupsdAddClass(resource
+ 9);
992 else if (pclass
->type
& CUPS_PRINTER_DISCOVERED
)
995 * Check the default policy, then rename the remote class to "Class"...
998 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
1000 send_http_error(con
, status
, NULL
);
1004 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
1005 cupsdRenamePrinter(pclass
, newname
);
1008 * Add the class as a new local class...
1011 pclass
= cupsdAddClass(resource
+ 9);
1014 else if ((status
= cupsdCheckPolicy(pclass
->op_policy_ptr
, con
,
1017 send_http_error(con
, status
, pclass
);
1024 * Look for attributes and copy them over as needed...
1027 need_restart_job
= 0;
1029 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
1030 IPP_TAG_TEXT
)) != NULL
)
1031 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
1033 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
1034 IPP_TAG_TEXT
)) != NULL
)
1035 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
1037 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
1038 IPP_TAG_BOOLEAN
)) != NULL
)
1040 cupsdLogMessage(CUPSD_LOG_INFO
,
1041 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
1042 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
1044 pclass
->accepting
= attr
->values
[0].boolean
;
1045 cupsdAddPrinterHistory(pclass
);
1048 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
1049 IPP_TAG_BOOLEAN
)) != NULL
)
1051 if (pclass
->shared
&& !attr
->values
[0].boolean
)
1052 cupsdDeregisterPrinter(pclass
, 1);
1054 cupsdLogMessage(CUPSD_LOG_INFO
,
1055 "Setting %s printer-is-shared to %d (was %d.)",
1056 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
1058 pclass
->shared
= attr
->values
[0].boolean
;
1061 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
1062 IPP_TAG_ENUM
)) != NULL
)
1064 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
1065 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
1067 send_ipp_status(con
, IPP_BAD_REQUEST
,
1068 _("Attempt to set %s printer-state to bad value %d!"),
1069 pclass
->name
, attr
->values
[0].integer
);
1073 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
1074 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
1076 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1077 cupsdStopPrinter(pclass
, 0);
1080 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1081 need_restart_job
= 1;
1084 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1085 IPP_TAG_TEXT
)) != NULL
)
1087 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1088 sizeof(pclass
->state_message
));
1089 cupsdAddPrinterHistory(pclass
);
1091 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1092 IPP_TAG_URI
)) != NULL
)
1095 * Clear the printer array as needed...
1098 need_restart_job
= 1;
1100 if (pclass
->num_printers
> 0)
1102 free(pclass
->printers
);
1103 pclass
->num_printers
= 0;
1107 * Add each printer or class that is listed...
1110 for (i
= 0; i
< attr
->num_values
; i
++)
1113 * Search for the printer or class URI...
1116 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1122 send_ipp_status(con
, IPP_NOT_FOUND
,
1123 _("The printer or class was not found."));
1128 * Add it to the class...
1131 cupsdAddPrinterToClass(pclass
, member
);
1135 set_printer_defaults(con
, pclass
);
1137 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1138 IPP_TAG_KEYWORD
)) != NULL
)
1139 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1142 * Update the printer class attributes and return...
1145 cupsdSetPrinterAttrs(pclass
);
1146 cupsdSaveAllClasses();
1148 if (need_restart_job
&& pclass
->job
)
1153 * Stop the current job and then restart it below...
1156 job
= (cupsd_job_t
*)pclass
->job
;
1158 cupsdStopJob(job
, 1);
1160 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1161 job
->state_value
= IPP_JOB_PENDING
;
1164 if (need_restart_job
)
1167 cupsdWritePrintcap();
1171 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1172 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1175 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1176 pclass
->name
, get_username(con
));
1180 cupsdAddPrinterHistory(pclass
);
1182 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1183 "New class \"%s\" added by \"%s\".", pclass
->name
,
1186 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1187 pclass
->name
, get_username(con
));
1190 con
->response
->request
.status
.status_code
= IPP_OK
;
1195 * 'add_file()' - Add a file to a job.
1198 static int /* O - 0 on success, -1 on error */
1199 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1200 cupsd_job_t
*job
, /* I - Job to add to */
1201 mime_type_t
*filetype
, /* I - Type of file */
1202 int compression
) /* I - Compression */
1204 mime_type_t
**filetypes
; /* New filetypes array... */
1205 int *compressions
; /* New compressions array... */
1208 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1209 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1210 "compression=%d)", con
, con
? con
->http
.fd
: -1, job
->id
,
1211 filetype
->super
, filetype
->type
, compression
);
1214 * Add the file to the job...
1217 if (job
->num_files
== 0)
1219 compressions
= (int *)malloc(sizeof(int));
1220 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1224 compressions
= (int *)realloc(job
->compressions
,
1225 (job
->num_files
+ 1) * sizeof(int));
1226 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1227 (job
->num_files
+ 1) *
1228 sizeof(mime_type_t
*));
1231 if (!compressions
|| !filetypes
)
1233 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
1236 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1237 _("Unable to allocate memory for file types!"));
1242 job
->compressions
= compressions
;
1243 job
->compressions
[job
->num_files
] = compression
;
1244 job
->filetypes
= filetypes
;
1245 job
->filetypes
[job
->num_files
] = filetype
;
1254 * 'add_job()' - Add a job to a print queue.
1257 static cupsd_job_t
* /* O - Job object */
1258 add_job(cupsd_client_t
*con
, /* I - Client connection */
1259 cupsd_printer_t
*printer
, /* I - Destination printer */
1260 mime_type_t
*filetype
) /* I - First print file type, if any */
1262 http_status_t status
; /* Policy status */
1263 ipp_attribute_t
*attr
, /* Current attribute */
1264 *auth_info
; /* auth-info attribute */
1265 const char *val
; /* Default option value */
1266 int priority
; /* Job priority */
1267 char *title
; /* Job name/title */
1268 cupsd_job_t
*job
; /* Current job */
1269 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1270 int kbytes
; /* Size of print file */
1271 int i
; /* Looping var */
1272 int lowerpagerange
; /* Page range bound */
1275 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1276 con
, con
->http
.fd
, printer
, printer
->name
,
1277 filetype
, filetype
->super
, filetype
->type
);
1280 * Check remote printing to non-shared printer...
1283 if (!printer
->shared
&&
1284 strcasecmp(con
->http
.hostname
, "localhost") &&
1285 strcasecmp(con
->http
.hostname
, ServerName
))
1287 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1288 _("The printer or class is not shared!"));
1296 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1298 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1300 send_http_error(con
, status
, printer
);
1303 else if ((printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
1304 !con
->username
[0] && !auth_info
)
1306 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1310 else if (auth_info
&& !con
->http
.tls
&&
1311 !httpAddrLocalhost(con
->http
.hostaddr
))
1314 * Require encryption of auth-info over non-local connections...
1317 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1320 #endif /* HAVE_SSL */
1323 * See if the printer is accepting jobs...
1326 if (!printer
->accepting
)
1328 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1329 _("Destination \"%s\" is not accepting jobs."),
1335 * Validate job template attributes; for now just document-format,
1336 * copies, number-up, and page-ranges...
1339 if (filetype
&& printer
->filetypes
&&
1340 !cupsArrayFind(printer
->filetypes
, filetype
))
1342 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1343 /* MIME media type string */
1346 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1349 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1350 _("Unsupported format \'%s\'!"), mimetype
);
1352 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1353 "document-format", NULL
, mimetype
);
1358 if ((attr
= ippFindAttribute(con
->request
, "copies",
1359 IPP_TAG_INTEGER
)) != NULL
)
1361 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1363 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1364 attr
->values
[0].integer
);
1365 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1366 "copies", attr
->values
[0].integer
);
1371 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1372 IPP_TAG_INTEGER
)) != NULL
)
1374 if (attr
->values
[0].integer
!= 1 &&
1375 attr
->values
[0].integer
!= 2 &&
1376 attr
->values
[0].integer
!= 4 &&
1377 attr
->values
[0].integer
!= 6 &&
1378 attr
->values
[0].integer
!= 9 &&
1379 attr
->values
[0].integer
!= 16)
1381 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1382 attr
->values
[0].integer
);
1383 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1384 "number-up", attr
->values
[0].integer
);
1389 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1390 IPP_TAG_RANGE
)) != NULL
)
1392 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1394 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1395 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1397 send_ipp_status(con
, IPP_BAD_REQUEST
,
1398 _("Bad page-ranges values %d-%d."),
1399 attr
->values
[i
].range
.lower
,
1400 attr
->values
[i
].range
.upper
);
1404 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1409 * Make sure we aren't over our limit...
1412 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1415 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1417 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1418 _("Too many active jobs."));
1422 if ((i
= check_quotas(con
, printer
)) < 0)
1424 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1429 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1434 * Create the job and set things up...
1437 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1438 IPP_TAG_INTEGER
)) != NULL
)
1439 priority
= attr
->values
[0].integer
;
1442 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1443 printer
->options
)) != NULL
)
1444 priority
= atoi(val
);
1448 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1452 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1453 IPP_TAG_NAME
)) != NULL
)
1454 title
= attr
->values
[0].string
.text
;
1456 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1457 title
= "Untitled");
1459 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1461 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1462 _("Unable to add job for destination \"%s\"!"),
1467 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
1468 CUPS_PRINTER_REMOTE
);
1469 job
->attrs
= con
->request
;
1470 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1472 add_job_uuid(con
, job
);
1473 apply_printer_defaults(printer
, job
);
1475 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1477 if (con
->username
[0])
1479 cupsdSetString(&job
->username
, con
->username
);
1482 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1486 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1487 "add_job: requesting-user-name=\"%s\"",
1488 attr
->values
[0].string
.text
);
1490 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1493 cupsdSetString(&job
->username
, "anonymous");
1496 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1497 "job-originating-user-name", NULL
, job
->username
);
1500 attr
->group_tag
= IPP_TAG_JOB
;
1501 _cupsStrFree(attr
->name
);
1502 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1505 if (con
->username
[0] || auth_info
)
1507 save_auth_info(con
, job
, auth_info
);
1510 * Remove the auth-info attribute from the attribute data...
1515 if (job
->attrs
->prev
)
1516 job
->attrs
->prev
->next
= auth_info
->next
;
1518 job
->attrs
->attrs
= auth_info
->next
;
1520 if (job
->attrs
->last
== auth_info
)
1521 job
->attrs
->last
= job
->attrs
->prev
;
1523 _ippFreeAttr(auth_info
);
1527 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1528 IPP_TAG_ZERO
)) != NULL
)
1531 * Request contains a job-originating-host-name attribute; validate it...
1534 if (attr
->value_tag
!= IPP_TAG_NAME
||
1535 attr
->num_values
!= 1 ||
1536 strcmp(con
->http
.hostname
, "localhost"))
1539 * Can't override the value if we aren't connected via localhost.
1540 * Also, we can only have 1 value and it must be a name value.
1543 switch (attr
->value_tag
)
1545 case IPP_TAG_STRING
:
1546 case IPP_TAG_TEXTLANG
:
1547 case IPP_TAG_NAMELANG
:
1550 case IPP_TAG_KEYWORD
:
1552 case IPP_TAG_URISCHEME
:
1553 case IPP_TAG_CHARSET
:
1554 case IPP_TAG_LANGUAGE
:
1555 case IPP_TAG_MIMETYPE
:
1557 * Free old strings...
1560 for (i
= 0; i
< attr
->num_values
; i
++)
1562 _cupsStrFree(attr
->values
[i
].string
.text
);
1563 attr
->values
[i
].string
.text
= NULL
;
1564 if (attr
->values
[i
].string
.charset
)
1566 _cupsStrFree(attr
->values
[i
].string
.charset
);
1567 attr
->values
[i
].string
.charset
= NULL
;
1576 * Use the default connection hostname instead...
1579 attr
->value_tag
= IPP_TAG_NAME
;
1580 attr
->num_values
= 1;
1581 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1584 attr
->group_tag
= IPP_TAG_JOB
;
1589 * No job-originating-host-name attribute, so use the hostname from
1593 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1594 "job-originating-host-name", NULL
, con
->http
.hostname
);
1597 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1599 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1600 "time-at-processing", 0);
1601 attr
->value_tag
= IPP_TAG_NOVALUE
;
1602 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1603 "time-at-completed", 0);
1604 attr
->value_tag
= IPP_TAG_NOVALUE
;
1607 * Add remaining job attributes...
1610 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1611 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1612 "job-state", IPP_JOB_STOPPED
);
1613 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1614 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1615 "job-media-sheets-completed", 0);
1616 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1618 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1621 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1622 IPP_TAG_INTEGER
)) != NULL
)
1623 attr
->values
[0].integer
= 0;
1625 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1628 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1629 IPP_TAG_KEYWORD
)) == NULL
)
1630 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1633 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1634 printer
->options
)) == NULL
)
1637 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1638 "job-hold-until", NULL
, val
);
1640 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1641 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1644 * Hold job until specified time...
1647 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1649 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1650 job
->state_value
= IPP_JOB_HELD
;
1652 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1654 job
->hold_until
= time(NULL
) + 60;
1655 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1656 job
->state_value
= IPP_JOB_HELD
;
1660 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1661 job
->state_value
= IPP_JOB_PENDING
;
1664 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1668 * Add job sheets options...
1671 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1672 IPP_TAG_ZERO
)) == NULL
)
1674 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1675 "Adding default job-sheets values \"%s,%s\"...",
1676 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1678 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1680 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1681 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1684 job
->job_sheets
= attr
;
1687 * Enforce classification level if set...
1692 cupsdLogMessage(CUPSD_LOG_INFO
,
1693 "Classification=\"%s\", ClassifyOverride=%d",
1694 Classification
? Classification
: "(null)",
1697 if (ClassifyOverride
)
1699 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1700 (attr
->num_values
== 1 ||
1701 !strcmp(attr
->values
[1].string
.text
, "none")))
1704 * Force the leading banner to have the classification on it...
1707 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1709 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1710 "job-sheets=\"%s,none\", "
1711 "job-originating-user-name=\"%s\"",
1712 job
->id
, Classification
, job
->username
);
1714 else if (attr
->num_values
== 2 &&
1715 strcmp(attr
->values
[0].string
.text
,
1716 attr
->values
[1].string
.text
) &&
1717 strcmp(attr
->values
[0].string
.text
, "none") &&
1718 strcmp(attr
->values
[1].string
.text
, "none"))
1721 * Can't put two different security markings on the same document!
1724 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1726 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1727 "job-sheets=\"%s,%s\", "
1728 "job-originating-user-name=\"%s\"",
1729 job
->id
, attr
->values
[0].string
.text
,
1730 attr
->values
[1].string
.text
, job
->username
);
1732 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1733 strcmp(attr
->values
[0].string
.text
, "none") &&
1734 (attr
->num_values
== 1 ||
1735 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1736 strcmp(attr
->values
[1].string
.text
, "none"))))
1738 if (attr
->num_values
== 1)
1739 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1740 "[Job %d] CLASSIFICATION OVERRIDDEN "
1741 "job-sheets=\"%s\", "
1742 "job-originating-user-name=\"%s\"",
1743 job
->id
, attr
->values
[0].string
.text
, job
->username
);
1745 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1746 "[Job %d] CLASSIFICATION OVERRIDDEN "
1747 "job-sheets=\"%s,%s\",fffff "
1748 "job-originating-user-name=\"%s\"",
1749 job
->id
, attr
->values
[0].string
.text
,
1750 attr
->values
[1].string
.text
, job
->username
);
1753 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1754 (attr
->num_values
== 1 ||
1755 strcmp(attr
->values
[1].string
.text
, Classification
)))
1758 * Force the banner to have the classification on it...
1761 if (attr
->num_values
> 1 &&
1762 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1764 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1765 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1769 if (attr
->num_values
== 1 ||
1770 strcmp(attr
->values
[0].string
.text
, "none"))
1771 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1773 if (attr
->num_values
> 1 &&
1774 strcmp(attr
->values
[1].string
.text
, "none"))
1775 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1778 if (attr
->num_values
> 1)
1779 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1780 "[Job %d] CLASSIFICATION FORCED "
1781 "job-sheets=\"%s,%s\", "
1782 "job-originating-user-name=\"%s\"",
1783 job
->id
, attr
->values
[0].string
.text
,
1784 attr
->values
[1].string
.text
, job
->username
);
1786 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1787 "[Job %d] CLASSIFICATION FORCED "
1788 "job-sheets=\"%s\", "
1789 "job-originating-user-name=\"%s\"",
1790 job
->id
, Classification
, job
->username
);
1795 * See if we need to add the starting sheet...
1798 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1800 cupsdLogMessage(CUPSD_LOG_INFO
,
1801 "[Job %d] Adding start banner page \"%s\".",
1802 job
->id
, attr
->values
[0].string
.text
);
1804 kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
);
1806 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1809 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1810 IPP_TAG_ZERO
)) != NULL
)
1814 * Fill in the response info...
1817 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1818 LocalPort
, job
->id
);
1820 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1823 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1825 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1827 add_job_state_reasons(con
, job
);
1829 con
->response
->request
.status
.status_code
= IPP_OK
;
1832 * Add any job subscriptions...
1835 add_job_subscriptions(con
, job
);
1838 * Set all but the first two attributes to the job attributes group...
1841 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1842 attr
->group_tag
= IPP_TAG_JOB
;
1845 * Fire the "job created" event...
1848 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1851 * Return the new job...
1859 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1860 * upon the job and printer state...
1864 add_job_state_reasons(
1865 cupsd_client_t
*con
, /* I - Client connection */
1866 cupsd_job_t
*job
) /* I - Job info */
1868 cupsd_printer_t
*dest
; /* Destination printer */
1871 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1872 con
, con
->http
.fd
, job
? job
->id
: 0);
1874 switch (job
? job
->state_value
: IPP_JOB_CANCELED
)
1876 case IPP_JOB_PENDING
:
1877 dest
= cupsdFindDest(job
->dest
);
1879 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1880 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1881 "job-state-reasons", NULL
, "printer-stopped");
1883 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1884 "job-state-reasons", NULL
, "none");
1888 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1889 IPP_TAG_KEYWORD
) != NULL
||
1890 ippFindAttribute(job
->attrs
, "job-hold-until",
1891 IPP_TAG_NAME
) != NULL
)
1892 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1893 "job-state-reasons", NULL
, "job-hold-until-specified");
1895 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1896 "job-state-reasons", NULL
, "job-incoming");
1899 case IPP_JOB_PROCESSING
:
1900 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1901 "job-state-reasons", NULL
, "job-printing");
1904 case IPP_JOB_STOPPED
:
1905 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1906 "job-state-reasons", NULL
, "job-stopped");
1909 case IPP_JOB_CANCELED
:
1910 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1911 "job-state-reasons", NULL
, "job-canceled-by-user");
1914 case IPP_JOB_ABORTED
:
1915 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1916 "job-state-reasons", NULL
, "aborted-by-system");
1919 case IPP_JOB_COMPLETED
:
1920 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1921 "job-state-reasons", NULL
, "job-completed-successfully");
1928 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1932 add_job_subscriptions(
1933 cupsd_client_t
*con
, /* I - Client connection */
1934 cupsd_job_t
*job
) /* I - Newly created job */
1936 int i
; /* Looping var */
1937 ipp_attribute_t
*prev
, /* Previous attribute */
1938 *next
, /* Next attribute */
1939 *attr
; /* Current attribute */
1940 cupsd_subscription_t
*sub
; /* Subscription object */
1941 const char *recipient
, /* notify-recipient-uri */
1942 *pullmethod
; /* notify-pull-method */
1943 ipp_attribute_t
*user_data
; /* notify-user-data */
1944 int interval
; /* notify-time-interval */
1945 unsigned mask
; /* notify-events */
1949 * Find the first subscription group attribute; return if we have
1953 for (attr
= job
->attrs
->attrs
, prev
= NULL
;
1955 prev
= attr
, attr
= attr
->next
)
1956 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1963 * Process the subscription attributes in the request...
1972 mask
= CUPSD_EVENT_NONE
;
1974 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1976 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
1977 attr
->value_tag
== IPP_TAG_URI
)
1978 recipient
= attr
->values
[0].string
.text
;
1979 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1980 attr
->value_tag
== IPP_TAG_KEYWORD
)
1981 pullmethod
= attr
->values
[0].string
.text
;
1982 else if (!strcmp(attr
->name
, "notify-charset") &&
1983 attr
->value_tag
== IPP_TAG_CHARSET
&&
1984 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
1985 strcmp(attr
->values
[0].string
.text
, "utf-8"))
1987 send_ipp_status(con
, IPP_CHARSET
,
1988 _("Character set \"%s\" not supported!"),
1989 attr
->values
[0].string
.text
);
1992 else if (!strcmp(attr
->name
, "notify-natural-language") &&
1993 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
1994 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
1996 send_ipp_status(con
, IPP_CHARSET
,
1997 _("Language \"%s\" not supported!"),
1998 attr
->values
[0].string
.text
);
2001 else if (!strcmp(attr
->name
, "notify-user-data") &&
2002 attr
->value_tag
== IPP_TAG_STRING
)
2004 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
2006 send_ipp_status(con
, IPP_REQUEST_VALUE
,
2007 _("The notify-user-data value is too large "
2008 "(%d > 63 octets)!"),
2009 attr
->values
[0].unknown
.length
);
2015 else if (!strcmp(attr
->name
, "notify-events") &&
2016 attr
->value_tag
== IPP_TAG_KEYWORD
)
2018 for (i
= 0; i
< attr
->num_values
; i
++)
2019 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
2021 else if (!strcmp(attr
->name
, "notify-lease-duration"))
2023 send_ipp_status(con
, IPP_BAD_REQUEST
,
2024 _("The notify-lease-duration attribute cannot be "
2025 "used with job subscriptions."));
2028 else if (!strcmp(attr
->name
, "notify-time-interval") &&
2029 attr
->value_tag
== IPP_TAG_INTEGER
)
2030 interval
= attr
->values
[0].integer
;
2035 if (!recipient
&& !pullmethod
)
2038 if (mask
== CUPSD_EVENT_NONE
)
2039 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2041 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
2044 sub
->interval
= interval
;
2046 cupsdSetString(&sub
->owner
, job
->username
);
2050 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2051 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2052 sub
->user_data_len
);
2055 ippAddSeparator(con
->response
);
2056 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2057 "notify-subscription-id", sub
->id
);
2063 cupsdSaveAllSubscriptions();
2066 * Remove all of the subscription attributes from the job request...
2069 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2073 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2074 attr
->group_tag
== IPP_TAG_ZERO
)
2077 * Free and remove this attribute...
2085 job
->attrs
->attrs
= next
;
2091 job
->attrs
->last
= prev
;
2092 job
->attrs
->current
= prev
;
2097 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2099 * See RFC 4122 for the definition of UUIDs and the format.
2103 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
2104 cupsd_job_t
*job
) /* I - Job */
2106 char uuid
[1024]; /* job-uuid string */
2107 _cups_md5_state_t md5state
; /* MD5 state */
2108 unsigned char md5sum
[16]; /* MD5 digest/sum */
2112 * First see if the job already has a job-uuid attribute; if so, return...
2115 if (ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2119 * No job-uuid attribute, so build a version 3 UUID with the local job
2120 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
2121 * the ServerName, server name and port that the client connected to,
2122 * and local job ID...
2125 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
2126 con
->serverport
, job
->id
);
2128 _cupsMD5Init(&md5state
);
2129 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
2130 _cupsMD5Finish(&md5state
, md5sum
);
2133 * Format the UUID URI using the MD5 sum and job ID.
2136 snprintf(uuid
, sizeof(uuid
),
2137 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2138 "%02x%02x%02x%02x%02x%02x",
2139 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
2140 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
2141 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
2142 md5sum
[14], md5sum
[15]);
2144 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
2149 * 'add_printer()' - Add a printer to the system.
2153 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2154 ipp_attribute_t
*uri
) /* I - URI of printer */
2156 http_status_t status
; /* Policy status */
2157 int i
; /* Looping var */
2158 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2159 username
[HTTP_MAX_URI
], /* Username portion of URI */
2160 host
[HTTP_MAX_URI
], /* Host portion of URI */
2161 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2162 int port
; /* Port portion of URI */
2163 cupsd_printer_t
*printer
; /* Printer/class */
2164 ipp_attribute_t
*attr
; /* Printer attribute */
2165 cups_file_t
*fp
; /* Script/PPD file */
2166 char line
[1024]; /* Line from file... */
2167 char srcfile
[1024], /* Source Script/PPD file */
2168 dstfile
[1024]; /* Destination Script/PPD file */
2169 int modify
; /* Non-zero if we are modifying */
2170 char newname
[IPP_MAX_NAME
]; /* New printer name */
2171 int need_restart_job
; /* Need to restart job? */
2172 int set_device_uri
, /* Did we set the device URI? */
2173 set_port_monitor
; /* Did we set the port monitor? */
2176 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2177 con
->http
.fd
, uri
->values
[0].string
.text
);
2180 * Do we have a valid URI?
2183 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2184 sizeof(scheme
), username
, sizeof(username
), host
,
2185 sizeof(host
), &port
, resource
, sizeof(resource
));
2187 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2190 * No, return an error...
2193 send_ipp_status(con
, IPP_BAD_REQUEST
,
2194 _("The printer-uri must be of the form "
2195 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2200 * Do we have a valid printer name?
2203 if (!validate_name(resource
+ 10))
2206 * No, return an error...
2209 send_ipp_status(con
, IPP_BAD_REQUEST
,
2210 _("The printer-uri \"%s\" contains invalid characters."),
2211 uri
->values
[0].string
.text
);
2216 * See if the printer already exists; if not, create a new printer...
2219 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2222 * Printer doesn't exist; see if we have a class of the same name...
2225 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2226 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
2229 * Yes, return an error...
2232 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2233 _("A class named \"%s\" already exists!"),
2239 * No, check the default policy then add the printer...
2242 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2244 send_http_error(con
, status
, NULL
);
2248 printer
= cupsdAddPrinter(resource
+ 10);
2251 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2254 * Check the default policy, then rename the implicit printer to
2255 * "AnyPrinter" or delete it...
2258 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2260 send_http_error(con
, status
, NULL
);
2264 if (ImplicitAnyClasses
)
2266 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2267 cupsdRenamePrinter(printer
, newname
);
2270 cupsdDeletePrinter(printer
, 1);
2273 * Add the printer as a new local printer...
2276 printer
= cupsdAddPrinter(resource
+ 10);
2279 else if (printer
->type
& CUPS_PRINTER_DISCOVERED
)
2282 * Check the default policy, then rename the remote printer to
2283 * "Printer@server"...
2286 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2288 send_http_error(con
, status
, NULL
);
2292 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2294 cupsdRenamePrinter(printer
, newname
);
2297 * Add the printer as a new local printer...
2300 printer
= cupsdAddPrinter(resource
+ 10);
2303 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2306 send_http_error(con
, status
, printer
);
2313 * Look for attributes and copy them over as needed...
2316 need_restart_job
= 0;
2318 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2319 IPP_TAG_TEXT
)) != NULL
)
2320 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2322 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2323 IPP_TAG_TEXT
)) != NULL
)
2324 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2328 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2329 IPP_TAG_URI
)) != NULL
)
2332 * Do we have a valid device URI?
2335 http_uri_status_t uri_status
; /* URI separation status */
2338 need_restart_job
= 1;
2340 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2341 attr
->values
[0].string
.text
,
2342 scheme
, sizeof(scheme
),
2343 username
, sizeof(username
),
2344 host
, sizeof(host
), &port
,
2345 resource
, sizeof(resource
));
2347 if (uri_status
< HTTP_URI_OK
)
2349 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2350 attr
->values
[0].string
.text
);
2351 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2352 "add_printer: httpSeparateURI returned %d", uri_status
);
2356 if (!strcmp(scheme
, "file"))
2359 * See if the administrator has enabled file devices...
2362 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2365 * File devices are disabled and the URL is not file:/dev/null...
2368 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2369 _("File device URIs have been disabled! "
2370 "To enable, see the FileDevice directive in "
2371 "\"%s/cupsd.conf\"."),
2379 * See if the backend exists and is executable...
2382 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2383 if (access(srcfile
, X_OK
))
2386 * Could not find device in list!
2389 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2390 attr
->values
[0].string
.text
);
2395 cupsdLogMessage(CUPSD_LOG_INFO
,
2396 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2398 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2400 cupsdSanitizeURI(printer
->device_uri
, resource
,
2403 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2407 set_port_monitor
= 0;
2409 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2410 IPP_TAG_NAME
)) != NULL
)
2412 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2415 need_restart_job
= 1;
2417 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2419 for (i
= 0; i
< supported
->num_values
; i
++)
2420 if (!strcmp(supported
->values
[i
].string
.text
,
2421 attr
->values
[0].string
.text
))
2424 if (i
>= supported
->num_values
)
2426 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2427 attr
->values
[0].string
.text
);
2431 cupsdLogMessage(CUPSD_LOG_INFO
,
2432 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2433 printer
->name
, attr
->values
[0].string
.text
,
2434 printer
->port_monitor
? printer
->port_monitor
: "none");
2436 if (strcmp(attr
->values
[0].string
.text
, "none"))
2437 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2439 cupsdClearString(&printer
->port_monitor
);
2441 set_port_monitor
= 1;
2444 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2445 IPP_TAG_BOOLEAN
)) != NULL
)
2447 cupsdLogMessage(CUPSD_LOG_INFO
,
2448 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2449 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2451 printer
->accepting
= attr
->values
[0].boolean
;
2452 cupsdAddPrinterHistory(printer
);
2455 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2456 IPP_TAG_BOOLEAN
)) != NULL
)
2458 if (printer
->shared
&& !attr
->values
[0].boolean
)
2459 cupsdDeregisterPrinter(printer
, 1);
2461 cupsdLogMessage(CUPSD_LOG_INFO
,
2462 "Setting %s printer-is-shared to %d (was %d.)",
2463 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2465 printer
->shared
= attr
->values
[0].boolean
;
2468 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2469 IPP_TAG_ENUM
)) != NULL
)
2471 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2472 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2474 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2475 attr
->values
[0].integer
);
2479 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2480 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2482 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2483 cupsdStopPrinter(printer
, 0);
2486 need_restart_job
= 1;
2487 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2490 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2491 IPP_TAG_TEXT
)) != NULL
)
2493 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2494 sizeof(printer
->state_message
));
2495 cupsdAddPrinterHistory(printer
);
2498 set_printer_defaults(con
, printer
);
2500 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2501 IPP_TAG_KEYWORD
)) != NULL
)
2502 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2505 * See if we have all required attributes...
2508 if (!printer
->device_uri
)
2509 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2512 * See if we have an interface script or PPD file attached to the request...
2517 need_restart_job
= 1;
2519 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2521 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2524 * Yes; get the first line from it...
2528 cupsFileGets(fp
, line
, sizeof(line
));
2532 * Then see what kind of file it is...
2535 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2538 if (!strncmp(line
, "*PPD-Adobe", 10))
2541 * The new file is a PPD file, so remove any old interface script
2542 * that might be lying around...
2550 * This must be an interface script, so move the file over to the
2551 * interfaces directory and make it executable...
2554 if (copy_file(srcfile
, dstfile
))
2556 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2557 _("Unable to copy interface script - %s!"),
2563 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2564 "Copied interface script successfully!");
2565 chmod(dstfile
, 0755);
2569 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2572 if (!strncmp(line
, "*PPD-Adobe", 10))
2575 * The new file is a PPD file, so move the file over to the
2576 * ppd directory and make it readable by all...
2579 if (copy_file(srcfile
, dstfile
))
2581 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2582 _("Unable to copy PPD file - %s!"),
2588 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2589 "Copied PPD file successfully!");
2590 chmod(dstfile
, 0644);
2596 * This must be an interface script, so remove any old PPD file that
2597 * may be lying around...
2604 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2605 IPP_TAG_NAME
)) != NULL
)
2607 need_restart_job
= 1;
2609 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2612 * Raw driver, remove any existing PPD or interface script files.
2615 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2619 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2629 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2633 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2636 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2638 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2643 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2644 "Copied PPD file successfully!");
2645 chmod(dstfile
, 0644);
2651 * If we set the device URI but not the port monitor, check which port
2652 * monitor to use by default...
2655 if (set_device_uri
&& !set_port_monitor
)
2657 ppd_file_t
*ppd
; /* PPD file */
2658 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2661 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2662 sizeof(scheme
), username
, sizeof(username
), host
,
2663 sizeof(host
), &port
, resource
, sizeof(resource
));
2665 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2667 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2669 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2671 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2672 if (!strcmp(scheme
, ppdattr
->spec
))
2674 cupsdLogMessage(CUPSD_LOG_INFO
,
2675 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2676 printer
->name
, ppdattr
->value
,
2677 printer
->port_monitor
? printer
->port_monitor
2680 if (strcmp(ppdattr
->value
, "none"))
2681 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2683 cupsdClearString(&printer
->port_monitor
);
2693 * Update the printer attributes and return...
2696 cupsdSetPrinterAttrs(printer
);
2697 cupsdSaveAllPrinters();
2699 if (need_restart_job
&& printer
->job
)
2704 * Stop the current job and then restart it below...
2707 job
= (cupsd_job_t
*)printer
->job
;
2709 cupsdStopJob(job
, 1);
2711 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2712 job
->state_value
= IPP_JOB_PENDING
;
2715 if (need_restart_job
)
2718 cupsdWritePrintcap();
2722 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2723 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2726 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2727 printer
->name
, get_username(con
));
2731 cupsdAddPrinterHistory(printer
);
2733 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2734 "New printer \"%s\" added by \"%s\".", printer
->name
,
2737 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2738 printer
->name
, get_username(con
));
2741 con
->response
->request
.status
.status_code
= IPP_OK
;
2746 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2747 * based upon the printer state...
2751 add_printer_state_reasons(
2752 cupsd_client_t
*con
, /* I - Client connection */
2753 cupsd_printer_t
*p
) /* I - Printer info */
2755 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2756 "add_printer_state_reasons(%p[%d], %p[%s])",
2757 con
, con
->http
.fd
, p
, p
->name
);
2759 if (p
->num_reasons
== 0)
2760 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2761 "printer-state-reasons", NULL
,
2762 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2764 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2765 "printer-state-reasons", p
->num_reasons
, NULL
,
2766 (const char * const *)p
->reasons
);
2771 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2772 * the specified printer or class.
2776 add_queued_job_count(
2777 cupsd_client_t
*con
, /* I - Client connection */
2778 cupsd_printer_t
*p
) /* I - Printer or class */
2780 int count
; /* Number of jobs on destination */
2783 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2784 con
, con
->http
.fd
, p
, p
->name
);
2786 count
= cupsdGetPrinterJobCount(p
->name
);
2788 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2789 "queued-job-count", count
);
2794 * 'apply_printer_defaults()' - Apply printer default options to a job.
2798 apply_printer_defaults(
2799 cupsd_printer_t
*printer
, /* I - Printer */
2800 cupsd_job_t
*job
) /* I - Job */
2802 int i
, /* Looping var */
2803 num_options
; /* Number of default options */
2804 cups_option_t
*options
, /* Default options */
2805 *option
; /* Current option */
2809 * Collect all of the default options and add the missing ones to the
2813 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
2816 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2818 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2823 * Encode these options as attributes in the job object...
2826 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2827 cupsFreeOptions(num_options
, options
);
2832 * 'authenticate_job()' - Set job authentication info.
2836 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2837 ipp_attribute_t
*uri
) /* I - Job URI */
2839 ipp_attribute_t
*attr
, /* job-id attribute */
2840 *auth_info
; /* auth-info attribute */
2841 int jobid
; /* Job ID */
2842 cupsd_job_t
*job
; /* Current job */
2843 char method
[HTTP_MAX_URI
],
2844 /* Method portion of URI */
2845 username
[HTTP_MAX_URI
],
2846 /* Username portion of URI */
2848 /* Host portion of URI */
2849 resource
[HTTP_MAX_URI
];
2850 /* Resource portion of URI */
2851 int port
; /* Port portion of URI */
2854 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2855 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
2858 * Start with "everything is OK" status...
2861 con
->response
->request
.status
.status_code
= IPP_OK
;
2864 * See if we have a job URI or a printer URI...
2867 if (!strcmp(uri
->name
, "printer-uri"))
2870 * Got a printer URI; see if we also have a job-id attribute...
2873 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2874 IPP_TAG_INTEGER
)) == NULL
)
2876 send_ipp_status(con
, IPP_BAD_REQUEST
,
2877 _("Got a printer-uri attribute but no job-id!"));
2881 jobid
= attr
->values
[0].integer
;
2886 * Got a job URI; parse it to get the job ID...
2889 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2890 sizeof(method
), username
, sizeof(username
), host
,
2891 sizeof(host
), &port
, resource
, sizeof(resource
));
2893 if (strncmp(resource
, "/jobs/", 6))
2899 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
2900 uri
->values
[0].string
.text
);
2904 jobid
= atoi(resource
+ 6);
2908 * See if the job exists...
2911 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2914 * Nope - return a "not found" error...
2917 send_ipp_status(con
, IPP_NOT_FOUND
,
2918 _("Job #%d does not exist!"), jobid
);
2923 * See if the job has been completed...
2926 if (job
->state_value
!= IPP_JOB_HELD
)
2929 * Return a "not-possible" error...
2932 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2933 _("Job #%d is not held for authentication!"),
2939 * See if we have already authenticated...
2942 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
2944 if (!con
->username
[0] && !auth_info
)
2946 cupsd_printer_t
*printer
; /* Job destination */
2950 * No auth data. If we need to authenticate via Kerberos, send a
2951 * HTTP auth challenge, otherwise just return an IPP error...
2954 printer
= cupsdFindDest(job
->dest
);
2956 if (printer
&& printer
->num_auth_info_required
> 0 &&
2957 !strcmp(printer
->auth_info_required
[0], "negotiate"))
2958 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
2960 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
2961 _("No authentication information provided!"));
2966 * See if the job is owned by the requesting user...
2969 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
2971 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
2976 * Save the authentication information for this job...
2979 save_auth_info(con
, job
, auth_info
);
2982 * Reset the job-hold-until value to "no-hold"...
2985 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2986 IPP_TAG_KEYWORD
)) == NULL
)
2987 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2991 attr
->value_tag
= IPP_TAG_KEYWORD
;
2992 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2996 * Release the job and return...
2999 cupsdReleaseJob(job
);
3001 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Authenticated by \"%s\".", jobid
,
3007 * 'cancel_all_jobs()' - Cancel all print jobs.
3011 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3012 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3014 http_status_t status
; /* Policy status */
3015 cups_ptype_t dtype
; /* Destination type */
3016 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3017 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3018 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3019 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3020 int port
; /* Port portion of URI */
3021 ipp_attribute_t
*attr
; /* Attribute in request */
3022 const char *username
; /* Username */
3023 int purge
; /* Purge? */
3024 cupsd_printer_t
*printer
; /* Printer */
3027 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3028 con
->http
.fd
, uri
->values
[0].string
.text
);
3031 * See if we have a printer URI...
3034 if (strcmp(uri
->name
, "printer-uri"))
3036 send_ipp_status(con
, IPP_BAD_REQUEST
,
3037 _("The printer-uri attribute is required!"));
3042 * Get the username (if any) for the jobs we want to cancel (only if
3043 * "my-jobs" is specified...
3046 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3047 IPP_TAG_BOOLEAN
)) != NULL
&&
3048 attr
->values
[0].boolean
)
3050 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3051 IPP_TAG_NAME
)) != NULL
)
3052 username
= attr
->values
[0].string
.text
;
3055 send_ipp_status(con
, IPP_BAD_REQUEST
,
3056 _("Missing requesting-user-name attribute!"));
3064 * Look for the "purge-jobs" attribute...
3067 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3068 IPP_TAG_BOOLEAN
)) != NULL
)
3069 purge
= attr
->values
[0].boolean
;
3074 * And if the destination is valid...
3077 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3083 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3084 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3085 hostname
, sizeof(hostname
), &port
,
3086 resource
, sizeof(resource
));
3088 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3089 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3091 send_ipp_status(con
, IPP_NOT_FOUND
,
3092 _("The printer or class was not found."));
3100 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3102 send_http_error(con
, status
, NULL
);
3107 * Cancel all jobs on all printers...
3110 cupsdCancelJobs(NULL
, username
, purge
);
3112 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3113 purge
? "purged" : "canceled", get_username(con
));
3121 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3124 send_http_error(con
, status
, printer
);
3129 * Cancel all of the jobs on the named printer...
3132 cupsdCancelJobs(printer
->name
, username
, purge
);
3134 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3135 printer
->name
, purge
? "purged" : "canceled",
3139 con
->response
->request
.status
.status_code
= IPP_OK
;
3144 * 'cancel_job()' - Cancel a print job.
3148 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3149 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3151 ipp_attribute_t
*attr
; /* Current attribute */
3152 int jobid
; /* Job ID */
3153 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3154 username
[HTTP_MAX_URI
], /* Username portion of URI */
3155 host
[HTTP_MAX_URI
], /* Host portion of URI */
3156 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3157 int port
; /* Port portion of URI */
3158 cupsd_job_t
*job
; /* Job information */
3159 cups_ptype_t dtype
; /* Destination type (printer/class) */
3160 cupsd_printer_t
*printer
; /* Printer data */
3163 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3164 con
->http
.fd
, uri
->values
[0].string
.text
);
3167 * See if we have a job URI or a printer URI...
3170 if (!strcmp(uri
->name
, "printer-uri"))
3173 * Got a printer URI; see if we also have a job-id attribute...
3176 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3177 IPP_TAG_INTEGER
)) == NULL
)
3179 send_ipp_status(con
, IPP_BAD_REQUEST
,
3180 _("Got a printer-uri attribute but no job-id!"));
3184 if ((jobid
= attr
->values
[0].integer
) == 0)
3187 * Find the current job on the specified printer...
3190 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3196 send_ipp_status(con
, IPP_NOT_FOUND
,
3197 _("The printer or class was not found."));
3202 * See if the printer is currently printing a job...
3206 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3210 * No, see if there are any pending jobs...
3213 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3215 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3216 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3217 !strcasecmp(job
->dest
, printer
->name
))
3224 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3226 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3227 if (job
->state_value
== IPP_JOB_STOPPED
&&
3228 !strcasecmp(job
->dest
, printer
->name
))
3235 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3246 * Got a job URI; parse it to get the job ID...
3249 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3250 sizeof(scheme
), username
, sizeof(username
), host
,
3251 sizeof(host
), &port
, resource
, sizeof(resource
));
3253 if (strncmp(resource
, "/jobs/", 6))
3259 send_ipp_status(con
, IPP_BAD_REQUEST
,
3260 _("Bad job-uri attribute \"%s\"!"),
3261 uri
->values
[0].string
.text
);
3265 jobid
= atoi(resource
+ 6);
3269 * See if the job exists...
3272 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3275 * Nope - return a "not found" error...
3278 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3283 * See if the job is owned by the requesting user...
3286 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3288 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3293 * See if the job is already completed, canceled, or aborted; if so,
3294 * we can't cancel...
3297 if (job
->state_value
>= IPP_JOB_CANCELED
)
3299 switch (job
->state_value
)
3301 case IPP_JOB_CANCELED
:
3302 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3303 _("Job #%d is already canceled - can\'t cancel."),
3307 case IPP_JOB_ABORTED
:
3308 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3309 _("Job #%d is already aborted - can\'t cancel."),
3314 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3315 _("Job #%d is already completed - can\'t cancel."),
3324 * Cancel the job and return...
3327 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
3330 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3333 con
->response
->request
.status
.status_code
= IPP_OK
;
3338 * 'cancel_subscription()' - Cancel a subscription.
3342 cancel_subscription(
3343 cupsd_client_t
*con
, /* I - Client connection */
3344 int sub_id
) /* I - Subscription ID */
3346 http_status_t status
; /* Policy status */
3347 cupsd_subscription_t
*sub
; /* Subscription */
3350 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3351 "cancel_subscription(con=%p[%d], sub_id=%d)",
3352 con
, con
->http
.fd
, sub_id
);
3355 * Is the subscription ID valid?
3358 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3361 * Bad subscription ID...
3364 send_ipp_status(con
, IPP_NOT_FOUND
,
3365 _("notify-subscription-id %d no good!"), sub_id
);
3373 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3375 con
, sub
->owner
)) != HTTP_OK
)
3377 send_http_error(con
, status
, sub
->dest
);
3382 * Cancel the subscription...
3385 cupsdDeleteSubscription(sub
, 1);
3387 con
->response
->request
.status
.status_code
= IPP_OK
;
3392 * 'check_quotas()' - Check quotas for a printer and user.
3395 static int /* O - 1 if OK, 0 if not */
3396 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3397 cupsd_printer_t
*p
) /* I - Printer or class */
3399 int i
; /* Looping var */
3400 char username
[33]; /* Username */
3401 cupsd_quota_t
*q
; /* Quota data */
3402 #ifdef HAVE_MBR_UID_TO_UUID
3404 * Use Apple membership APIs which require that all names represent
3405 * valid user account or group records accessible by the server.
3408 uuid_t usr_uuid
; /* UUID for job requesting user */
3409 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3410 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3411 int mbr_err
; /* Error from membership function */
3412 int is_member
; /* Is this user a member? */
3415 * Use standard POSIX APIs for checking users and groups...
3418 struct passwd
*pw
; /* User password data */
3419 #endif /* HAVE_MBR_UID_TO_UUID */
3422 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3423 con
, con
->http
.fd
, p
, p
->name
);
3433 * Figure out who is printing...
3436 strlcpy(username
, get_username(con
), sizeof(username
));
3439 * Check global active job limits for printers and users...
3442 if (MaxJobsPerPrinter
)
3445 * Check if there are too many pending jobs on this printer...
3448 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3450 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3459 * Check if there are too many pending jobs for this user...
3462 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3464 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3471 * Check against users...
3474 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3479 #ifdef HAVE_MBR_UID_TO_UUID
3481 * Get UUID for job requesting user...
3484 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3490 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3491 "check_quotas: UUID lookup failed for user \"%s\"",
3493 cupsdLogMessage(CUPSD_LOG_INFO
,
3494 "Denying user \"%s\" access to printer \"%s\" "
3495 "(unknown user)...",
3501 * Get UID and GID of requesting user...
3504 pw
= getpwnam(username
);
3506 #endif /* HAVE_MBR_UID_TO_UUID */
3508 for (i
= 0; i
< p
->num_users
; i
++)
3509 if (p
->users
[i
][0] == '@')
3512 * Check group membership...
3515 #ifdef HAVE_MBR_UID_TO_UUID
3516 if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
3520 * Invalid ACL entries are ignored for matching; just record a
3521 * warning in the log...
3524 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3525 "check_quotas: UUID lookup failed for ACL entry "
3526 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3527 cupsdLogMessage(CUPSD_LOG_WARN
,
3528 "Access control entry \"%s\" not a valid group name; "
3529 "entry ignored", p
->users
[i
]);
3533 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3537 * At this point, there should be no errors, but check anyways...
3540 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3541 "check_quotas: group \"%s\" membership check "
3542 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
3547 * Stop if we found a match...
3554 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3556 #endif /* HAVE_MBR_UID_TO_UUID */
3558 #ifdef HAVE_MBR_UID_TO_UUID
3561 if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
3565 * Invalid ACL entries are ignored for matching; just record a
3566 * warning in the log...
3569 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3570 "check_quotas: UUID lookup failed for ACL entry "
3571 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3572 cupsdLogMessage(CUPSD_LOG_WARN
,
3573 "Access control entry \"%s\" not a valid user name; "
3574 "entry ignored", p
->users
[i
]);
3578 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
3581 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3582 "check_quotas: User \"%s\" identity check failed "
3583 "(err=%d)", p
->users
[i
], mbr_err
);
3592 else if (!strcasecmp(username
, p
->users
[i
]))
3594 #endif /* HAVE_MBR_UID_TO_UUID */
3596 if ((i
< p
->num_users
) == p
->deny_users
)
3598 cupsdLogMessage(CUPSD_LOG_INFO
,
3599 "Denying user \"%s\" access to printer \"%s\"...",
3610 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
3613 * TODO: Define these special page count values as constants!
3616 if (q
->page_count
== -4) /* special case: unlimited user */
3618 cupsdLogMessage(CUPSD_LOG_INFO
,
3619 "User \"%s\" request approved for printer %s (%s): "
3621 username
, p
->name
, p
->info
);
3622 q
->page_count
= 0; /* allow user to print */
3625 else if (q
->page_count
== -3) /* quota exceeded */
3627 cupsdLogMessage(CUPSD_LOG_INFO
,
3628 "User \"%s\" request denied for printer %s (%s): "
3629 "quota limit exceeded.",
3630 username
, p
->name
, p
->info
);
3631 q
->page_count
= 2; /* force quota exceeded failure */
3634 else if (q
->page_count
== -2) /* quota disabled for user */
3636 cupsdLogMessage(CUPSD_LOG_INFO
,
3637 "User \"%s\" request denied for printer %s (%s): "
3638 "printing disabled for user.",
3639 username
, p
->name
, p
->info
);
3640 q
->page_count
= 2; /* force quota exceeded failure */
3643 else if (q
->page_count
== -1) /* quota access error */
3645 cupsdLogMessage(CUPSD_LOG_INFO
,
3646 "User \"%s\" request denied for printer %s (%s): "
3647 "unable to determine quota limit.",
3648 username
, p
->name
, p
->info
);
3649 q
->page_count
= 2; /* force quota exceeded failure */
3652 else if (q
->page_count
< 0) /* user not found or other error */
3654 cupsdLogMessage(CUPSD_LOG_INFO
,
3655 "User \"%s\" request denied for printer %s (%s): "
3656 "user disabled / missing quota.",
3657 username
, p
->name
, p
->info
);
3658 q
->page_count
= 2; /* force quota exceeded failure */
3661 else /* page within user limits */
3663 q
->page_count
= 0; /* allow user to print */
3668 #endif /* __APPLE__ */
3669 if (p
->k_limit
|| p
->page_limit
)
3671 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3673 cupsdLogMessage(CUPSD_LOG_ERROR
,
3674 "Unable to allocate quota data for user \"%s\"!",
3679 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3680 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3682 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3689 * If we have gotten this far, we're done!
3697 * 'copy_attribute()' - Copy a single attribute.
3700 static ipp_attribute_t
* /* O - New attribute */
3702 ipp_t
*to
, /* O - Destination request/response */
3703 ipp_attribute_t
*attr
, /* I - Attribute to copy */
3704 int quickcopy
) /* I - Do a quick copy? */
3706 int i
; /* Looping var */
3707 ipp_attribute_t
*toattr
; /* Destination attribute */
3710 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3711 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
3712 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
3715 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
3718 toattr
= ippAddSeparator(to
);
3721 case IPP_TAG_INTEGER
:
3723 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3724 attr
->name
, attr
->num_values
, NULL
);
3726 for (i
= 0; i
< attr
->num_values
; i
++)
3727 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
3730 case IPP_TAG_BOOLEAN
:
3731 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
3732 attr
->num_values
, NULL
);
3734 for (i
= 0; i
< attr
->num_values
; i
++)
3735 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
3738 case IPP_TAG_STRING
:
3741 case IPP_TAG_KEYWORD
:
3743 case IPP_TAG_URISCHEME
:
3744 case IPP_TAG_CHARSET
:
3745 case IPP_TAG_LANGUAGE
:
3746 case IPP_TAG_MIMETYPE
:
3747 toattr
= ippAddStrings(to
, attr
->group_tag
,
3748 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3749 attr
->name
, attr
->num_values
, NULL
, NULL
);
3753 for (i
= 0; i
< attr
->num_values
; i
++)
3754 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3758 for (i
= 0; i
< attr
->num_values
; i
++)
3759 toattr
->values
[i
].string
.text
=
3760 _cupsStrAlloc(attr
->values
[i
].string
.text
);
3765 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
3766 attr
->values
[0].date
);
3769 case IPP_TAG_RESOLUTION
:
3770 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
3771 attr
->num_values
, IPP_RES_PER_INCH
,
3774 for (i
= 0; i
< attr
->num_values
; i
++)
3776 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
3777 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
3778 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
3782 case IPP_TAG_RANGE
:
3783 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
3784 attr
->num_values
, NULL
, NULL
);
3786 for (i
= 0; i
< attr
->num_values
; i
++)
3788 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
3789 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
3793 case IPP_TAG_TEXTLANG
:
3794 case IPP_TAG_NAMELANG
:
3795 toattr
= ippAddStrings(to
, attr
->group_tag
,
3796 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3797 attr
->name
, attr
->num_values
, NULL
, NULL
);
3801 for (i
= 0; i
< attr
->num_values
; i
++)
3803 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
3804 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3809 for (i
= 0; i
< attr
->num_values
; i
++)
3812 toattr
->values
[i
].string
.charset
=
3813 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
3815 toattr
->values
[i
].string
.charset
=
3816 toattr
->values
[0].string
.charset
;
3818 toattr
->values
[i
].string
.text
=
3819 _cupsStrAlloc(attr
->values
[i
].string
.text
);
3824 case IPP_TAG_BEGIN_COLLECTION
:
3825 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
3826 attr
->num_values
, NULL
);
3828 for (i
= 0; i
< attr
->num_values
; i
++)
3830 toattr
->values
[i
].collection
= ippNew();
3831 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
3832 NULL
, IPP_TAG_ZERO
, 0);
3837 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3838 attr
->name
, attr
->num_values
, NULL
);
3840 for (i
= 0; i
< attr
->num_values
; i
++)
3842 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
3844 if (toattr
->values
[i
].unknown
.length
> 0)
3846 if ((toattr
->values
[i
].unknown
.data
=
3847 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
3848 toattr
->values
[i
].unknown
.length
= 0;
3850 memcpy(toattr
->values
[i
].unknown
.data
,
3851 attr
->values
[i
].unknown
.data
,
3852 toattr
->values
[i
].unknown
.length
);
3855 break; /* anti-compiler-warning-code */
3863 * 'copy_attrs()' - Copy attributes from one request to another.
3867 copy_attrs(ipp_t
*to
, /* I - Destination request */
3868 ipp_t
*from
, /* I - Source request */
3869 cups_array_t
*ra
, /* I - Requested attributes */
3870 ipp_tag_t group
, /* I - Group to copy */
3871 int quickcopy
) /* I - Do a quick copy? */
3873 ipp_attribute_t
*fromattr
; /* Source attribute */
3876 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3877 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3878 to
, from
, ra
, group
, quickcopy
);
3883 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3886 * Filter attributes as needed...
3889 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3890 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
3893 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
3894 copy_attribute(to
, fromattr
, quickcopy
);
3900 * 'copy_banner()' - Copy a banner file to the requests directory for the
3904 static int /* O - Size of banner file in kbytes */
3905 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
3906 cupsd_job_t
*job
, /* I - Job information */
3907 const char *name
) /* I - Name of banner */
3909 int i
; /* Looping var */
3910 int kbytes
; /* Size of banner file in kbytes */
3911 char filename
[1024]; /* Job filename */
3912 cupsd_banner_t
*banner
; /* Pointer to banner */
3913 cups_file_t
*in
; /* Input file */
3914 cups_file_t
*out
; /* Output file */
3915 int ch
; /* Character from file */
3916 char attrname
[255], /* Name of attribute */
3917 *s
; /* Pointer into name */
3918 ipp_attribute_t
*attr
; /* Attribute */
3921 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
3922 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
3923 name
? name
: "(null)");
3926 * Find the banner; return if not found or "none"...
3929 if (!name
|| !strcmp(name
, "none") ||
3930 (banner
= cupsdFindBanner(name
)) == NULL
)
3934 * Open the banner and job files...
3937 if (add_file(con
, job
, banner
->filetype
, 0))
3940 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
3942 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
3944 cupsdLogMessage(CUPSD_LOG_ERROR
,
3945 "copy_banner: Unable to create banner job file %s - %s",
3946 filename
, strerror(errno
));
3951 fchmod(cupsFileNumber(out
), 0640);
3952 fchown(cupsFileNumber(out
), RunUser
, Group
);
3955 * Try the localized banner file under the subdirectory...
3958 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
3960 if (strlen(attrname
) > 2 && attrname
[2] == '-')
3963 * Convert ll-cc to ll_CC...
3967 attrname
[3] = toupper(attrname
[3] & 255);
3968 attrname
[4] = toupper(attrname
[4] & 255);
3971 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3974 if (access(filename
, 0) && strlen(attrname
) > 2)
3977 * Wasn't able to find "ll_CC" locale file; try the non-national
3978 * localization banner directory.
3983 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3987 if (access(filename
, 0))
3990 * Use the non-localized banner file.
3993 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
3996 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4000 cupsdLogMessage(CUPSD_LOG_ERROR
,
4001 "copy_banner: Unable to open banner template file %s - %s",
4002 filename
, strerror(errno
));
4008 * Parse the file to the end...
4011 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4015 * Get an attribute name...
4018 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4019 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4021 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4031 * Ignore { followed by stuff that is not an attribute name...
4034 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4039 * See if it is defined...
4042 if (attrname
[0] == '?')
4047 if (!strcmp(s
, "printer-name"))
4049 cupsFilePuts(out
, job
->dest
);
4052 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4055 * See if we have a leading question mark...
4058 if (attrname
[0] != '?')
4061 * Nope, write to file as-is; probably a PostScript procedure...
4064 cupsFilePrintf(out
, "{%s}", attrname
);
4071 * Output value(s)...
4074 for (i
= 0; i
< attr
->num_values
; i
++)
4077 cupsFilePutChar(out
, ',');
4079 switch (attr
->value_tag
)
4081 case IPP_TAG_INTEGER
:
4083 if (!strncmp(s
, "time-at-", 8))
4084 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
4086 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4089 case IPP_TAG_BOOLEAN
:
4090 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4093 case IPP_TAG_NOVALUE
:
4094 cupsFilePuts(out
, "novalue");
4097 case IPP_TAG_RANGE
:
4098 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4099 attr
->values
[i
].range
.upper
);
4102 case IPP_TAG_RESOLUTION
:
4103 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4104 attr
->values
[i
].resolution
.yres
,
4105 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4110 case IPP_TAG_STRING
:
4113 case IPP_TAG_KEYWORD
:
4114 case IPP_TAG_CHARSET
:
4115 case IPP_TAG_LANGUAGE
:
4116 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4119 * Need to quote strings for PS banners...
4124 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4126 if (*p
== '(' || *p
== ')' || *p
== '\\')
4128 cupsFilePutChar(out
, '\\');
4129 cupsFilePutChar(out
, *p
);
4131 else if (*p
< 32 || *p
> 126)
4132 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4134 cupsFilePutChar(out
, *p
);
4138 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4142 break; /* anti-compiler-warning-code */
4146 else if (ch
== '\\') /* Quoted char */
4148 ch
= cupsFileGetChar(in
);
4150 if (ch
!= '{') /* Only do special handling for \{ */
4151 cupsFilePutChar(out
, '\\');
4153 cupsFilePutChar(out
, ch
);
4156 cupsFilePutChar(out
, ch
);
4160 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4162 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4163 IPP_TAG_INTEGER
)) != NULL
)
4164 attr
->values
[0].integer
+= kbytes
;
4173 * 'copy_file()' - Copy a PPD file or interface script...
4176 static int /* O - 0 = success, -1 = error */
4177 copy_file(const char *from
, /* I - Source file */
4178 const char *to
) /* I - Destination file */
4180 cups_file_t
*src
, /* Source file */
4181 *dst
; /* Destination file */
4182 int bytes
; /* Bytes to read/write */
4183 char buffer
[2048]; /* Copy buffer */
4186 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4189 * Open the source and destination file for a copy...
4192 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4195 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4202 * Copy the source file to the destination...
4205 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4206 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
4214 * Close both files and return...
4219 return (cupsFileClose(dst
));
4224 * 'copy_model()' - Copy a PPD model file, substituting default values
4228 static int /* O - 0 = success, -1 = error */
4229 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4230 const char *from
, /* I - Source file */
4231 const char *to
) /* I - Destination file */
4233 fd_set input
; /* select() input set */
4234 struct timeval timeout
; /* select() timeout */
4235 int maxfd
; /* Max file descriptor for select() */
4236 char tempfile
[1024]; /* Temporary PPD file */
4237 int tempfd
; /* Temporary PPD file descriptor */
4238 int temppid
; /* Process ID of cups-driverd */
4239 int temppipe
[2]; /* Temporary pipes */
4240 char *argv
[4], /* Command-line arguments */
4241 *envp
[MAX_ENV
]; /* Environment */
4242 cups_file_t
*src
, /* Source file */
4243 *dst
; /* Destination file */
4244 ppd_file_t
*ppd
; /* PPD file */
4245 int bytes
, /* Bytes from pipe */
4246 total
; /* Total bytes from pipe */
4247 char buffer
[2048]; /* Copy buffer */
4248 int i
; /* Looping var */
4249 char option
[PPD_MAX_NAME
], /* Option name */
4250 choice
[PPD_MAX_NAME
]; /* Choice name */
4251 int num_defaults
; /* Number of default options */
4252 cups_option_t
*defaults
; /* Default options */
4253 char cups_protocol
[PPD_MAX_LINE
];
4254 /* cupsProtocol attribute */
4255 int have_letter
, /* Have Letter size */
4256 have_a4
; /* Have A4 size */
4257 #ifdef HAVE_LIBPAPER
4258 char *paper_result
; /* Paper size name from libpaper */
4259 char system_paper
[64]; /* Paper size name buffer */
4260 #endif /* HAVE_LIBPAPER */
4263 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4264 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4268 * Run cups-driverd to get the PPD file...
4271 argv
[0] = "cups-driverd";
4273 argv
[2] = (char *)from
;
4276 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4278 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4279 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
4280 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4284 cupsdOpenPipe(temppipe
);
4286 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4287 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4289 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4290 -1, -1, 0, &temppid
))
4300 * Wait up to 30 seconds for the PPD file to be copied...
4305 if (temppipe
[0] > CGIPipes
[0])
4306 maxfd
= temppipe
[0] + 1;
4308 maxfd
= CGIPipes
[0] + 1;
4313 * See if we have data ready...
4319 FD_SET(temppipe
[0], &input
);
4320 FD_SET(CGIPipes
[0], &input
);
4322 timeout
.tv_sec
= 30;
4323 timeout
.tv_usec
= 0;
4325 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4335 * We have timed out...
4341 if (FD_ISSET(temppipe
[0], &input
))
4344 * Read the PPD file from the pipe, and write it to the PPD file.
4347 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4349 if (write(tempfd
, buffer
, bytes
) < bytes
)
4358 if (FD_ISSET(CGIPipes
[0], &input
))
4368 * No data from cups-deviced...
4371 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
4377 * Read the source file and see what page sizes are supported...
4380 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
4386 have_letter
= ppdPageSize(ppd
, "Letter") != NULL
;
4387 have_a4
= ppdPageSize(ppd
, "A4") != NULL
;
4390 * Open the destination (if possible) and set the default options...
4395 cups_protocol
[0] = '\0';
4397 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4400 * Read all of the default lines from the old PPD...
4403 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4404 if (!strncmp(buffer
, "*Default", 8))
4407 * Add the default option...
4410 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4411 choice
, sizeof(choice
)))
4413 ppd_option_t
*ppdo
; /* PPD option */
4417 * Only add the default if the default hasn't already been
4418 * set and the choice exists in the new PPD...
4421 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4422 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4423 ppdFindChoice(ppdo
, choice
))
4424 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4428 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4429 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4433 #ifdef HAVE_LIBPAPER
4434 else if ((paper_result
= systempapername()) != NULL
)
4437 * Set the default media sizes from the systemwide default...
4440 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
4441 system_paper
[0] = toupper(system_paper
[0] & 255);
4443 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
4444 (!strcmp(system_paper
, "A4") && have_a4
))
4446 num_defaults
= cupsAddOption("PageSize", system_paper
,
4447 num_defaults
, &defaults
);
4448 num_defaults
= cupsAddOption("PageRegion", system_paper
,
4449 num_defaults
, &defaults
);
4450 num_defaults
= cupsAddOption("PaperDimension", system_paper
,
4451 num_defaults
, &defaults
);
4452 num_defaults
= cupsAddOption("ImageableArea", system_paper
,
4453 num_defaults
, &defaults
);
4456 #endif /* HAVE_LIBPAPER */
4460 * Add the default media sizes...
4462 * Note: These values are generally not valid for large-format devices
4463 * like plotters, however it is probably safe to say that those
4464 * users will configure the media size after initially adding
4465 * the device anyways...
4468 if (!DefaultLanguage
||
4469 !strcasecmp(DefaultLanguage
, "C") ||
4470 !strcasecmp(DefaultLanguage
, "POSIX") ||
4471 !strcasecmp(DefaultLanguage
, "en") ||
4472 !strncasecmp(DefaultLanguage
, "en.", 3) ||
4473 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4474 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4475 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4478 * These are the only locales that will default to "letter" size...
4483 num_defaults
= cupsAddOption("PageSize", "Letter", num_defaults
,
4485 num_defaults
= cupsAddOption("PageRegion", "Letter", num_defaults
,
4487 num_defaults
= cupsAddOption("PaperDimension", "Letter", num_defaults
,
4489 num_defaults
= cupsAddOption("ImageableArea", "Letter", num_defaults
,
4496 * The rest default to "a4" size...
4499 num_defaults
= cupsAddOption("PageSize", "A4", num_defaults
,
4501 num_defaults
= cupsAddOption("PageRegion", "A4", num_defaults
,
4503 num_defaults
= cupsAddOption("PaperDimension", "A4", num_defaults
,
4505 num_defaults
= cupsAddOption("ImageableArea", "A4", num_defaults
,
4513 * Open the source file for a copy...
4516 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4518 cupsFreeOptions(num_defaults
, defaults
);
4524 * Open the destination file for a copy...
4527 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4529 cupsFreeOptions(num_defaults
, defaults
);
4536 * Copy the source file to the destination...
4539 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4541 if (!strncmp(buffer
, "*Default", 8))
4544 * Check for an previous default option choice...
4547 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4548 choice
, sizeof(choice
)))
4550 const char *val
; /* Default option value */
4553 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4556 * Substitute the previous choice...
4559 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4564 cupsFilePrintf(dst
, "%s\n", buffer
);
4567 if (cups_protocol
[0])
4568 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4570 cupsFreeOptions(num_defaults
, defaults
);
4573 * Close both files and return...
4580 return (cupsFileClose(dst
));
4585 * 'copy_job_attrs()' - Copy job attributes.
4589 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4590 cupsd_job_t
*job
, /* I - Job */
4591 cups_array_t
*ra
) /* I - Requested attributes array */
4593 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4597 * Send the requested attributes for each job...
4600 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4601 con
->servername
, con
->serverport
, "/jobs/%d",
4604 if (!ra
|| cupsArrayFind(ra
, "document-count"))
4605 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4606 "document-count", job
->num_files
);
4608 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
4609 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4610 "job-more-info", NULL
, job_uri
);
4612 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4613 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4614 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4615 job
->num_files
> 0);
4617 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
4618 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4619 "job-printer-up-time", time(NULL
));
4621 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4622 add_job_state_reasons(con
, job
);
4624 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4625 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4626 "job-uri", NULL
, job_uri
);
4628 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
4633 * 'copy_printer_attrs()' - Copy printer attributes.
4638 cupsd_client_t
*con
, /* I - Client connection */
4639 cupsd_printer_t
*printer
, /* I - Printer */
4640 cups_array_t
*ra
) /* I - Requested attributes array */
4642 char printer_uri
[HTTP_MAX_URI
];
4644 time_t curtime
; /* Current time */
4645 int i
; /* Looping var */
4646 ipp_attribute_t
*history
; /* History collection */
4650 * Copy the printer attributes to the response using requested-attributes
4651 * and document-format attributes that may be provided by the client.
4654 curtime
= time(NULL
);
4657 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
4658 printer
->recoverable
)
4659 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4660 "com.apple.print.recoverable-message", NULL
,
4661 printer
->recoverable
);
4662 #endif /* __APPLE__ */
4664 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4665 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
4666 "printer-alert", NULL
, printer
->alert
);
4668 if (printer
->alert_description
&&
4669 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4670 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4671 "printer-alert-description", NULL
,
4672 printer
->alert_description
);
4674 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4675 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4676 ippTimeToDate(curtime
));
4678 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4679 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4680 "printer-error-policy", NULL
, printer
->error_policy
);
4682 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4683 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
4684 printer
->accepting
);
4686 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4687 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
4690 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4691 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4692 "printer-op-policy", NULL
, printer
->op_policy
);
4694 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4695 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4698 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4699 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4700 "printer-state-change-time", printer
->state_time
);
4702 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
4703 cupsArrayFind(ra
, "printer-state-history"))
4706 * Printer history is only sent if specifically requested, so that
4707 * older CUPS/IPP clients won't barf on the collection attributes.
4710 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
4711 "printer-state-history",
4712 printer
->num_history
, NULL
);
4714 for (i
= 0; i
< printer
->num_history
; i
++)
4715 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
4716 NULL
, IPP_TAG_ZERO
, 0);
4719 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4720 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4721 "printer-state-message", NULL
, printer
->state_message
);
4723 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4724 add_printer_state_reasons(con
, printer
);
4726 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4728 int type
; /* printer-type value */
4732 * Add the CUPS-specific printer-type attribute...
4735 type
= printer
->type
;
4737 if (printer
== DefaultPrinter
)
4738 type
|= CUPS_PRINTER_DEFAULT
;
4740 if (!printer
->accepting
)
4741 type
|= CUPS_PRINTER_REJECTING
;
4743 if (!printer
->shared
)
4744 type
|= CUPS_PRINTER_NOT_SHARED
;
4746 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
4750 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4751 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4752 "printer-up-time", curtime
);
4754 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
4755 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
4758 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4759 "ipp", NULL
, con
->servername
, con
->serverport
,
4760 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4761 "/classes/%s" : "/printers/%s", printer
->name
);
4762 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4763 "printer-uri-supported", NULL
, printer_uri
);
4764 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4768 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4769 add_queued_job_count(con
, printer
);
4771 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
4772 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
4777 * 'copy_subscription_attrs()' - Copy subscription attributes.
4781 copy_subscription_attrs(
4782 cupsd_client_t
*con
, /* I - Client connection */
4783 cupsd_subscription_t
*sub
, /* I - Subscription */
4784 cups_array_t
*ra
) /* I - Requested attributes array */
4786 ipp_attribute_t
*attr
; /* Current attribute */
4787 char printer_uri
[HTTP_MAX_URI
];
4789 int count
; /* Number of events */
4790 unsigned mask
; /* Current event mask */
4791 const char *name
; /* Current event name */
4795 * Copy the subscription attributes to the response using the
4796 * requested-attributes attribute that may be provided by the client.
4799 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
4801 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
4804 * Simple event list...
4807 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
4808 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
4809 "notify-events", NULL
, name
);
4814 * Complex event list...
4817 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4818 if (sub
->mask
& mask
)
4821 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
4822 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
4823 "notify-events", count
, NULL
, NULL
);
4825 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4826 if (sub
->mask
& mask
)
4828 attr
->values
[count
].string
.text
=
4829 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
4836 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
4837 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4838 "notify-job-id", sub
->job
->id
);
4840 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
4841 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4842 "notify-lease-duration", sub
->lease
);
4844 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
4846 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4847 "ipp", NULL
, con
->servername
, con
->serverport
,
4848 "/printers/%s", sub
->dest
->name
);
4849 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4850 "notify-printer-uri", NULL
, printer_uri
);
4853 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
4854 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4855 "notify-recipient-uri", NULL
, sub
->recipient
);
4856 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
4857 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
4858 "notify-pull-method", NULL
, "ippget");
4860 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
4861 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
4862 "notify-subscriber-user-name", NULL
, sub
->owner
);
4864 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
4865 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4866 "notify-subscription-id", sub
->id
);
4868 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
4869 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4870 "notify-time-interval", sub
->interval
);
4872 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
4873 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
4874 sub
->user_data
, sub
->user_data_len
);
4879 * 'create_job()' - Print a file to a printer or class.
4883 create_job(cupsd_client_t
*con
, /* I - Client connection */
4884 ipp_attribute_t
*uri
) /* I - Printer URI */
4886 cupsd_printer_t
*printer
; /* Printer */
4887 cupsd_job_t
*job
; /* New job */
4890 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
4891 con
->http
.fd
, uri
->values
[0].string
.text
);
4894 * Is the destination valid?
4897 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
4903 send_ipp_status(con
, IPP_NOT_FOUND
,
4904 _("The printer or class was not found."));
4909 * Create the job object...
4912 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
4915 job
->pending_timeout
= 1;
4918 * Save and log the job...
4923 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
4924 job
->id
, job
->dest
, job
->username
);
4929 * 'create_requested_array()' - Create an array for the requested-attributes.
4932 static cups_array_t
* /* O - Array of attributes or NULL */
4933 create_requested_array(ipp_t
*request
) /* I - IPP request */
4935 int i
; /* Looping var */
4936 ipp_attribute_t
*requested
; /* requested-attributes attribute */
4937 cups_array_t
*ra
; /* Requested attributes array */
4938 char *value
; /* Current value */
4942 * Get the requested-attributes attribute, and return NULL if we don't
4946 if ((requested
= ippFindAttribute(request
, "requested-attributes",
4947 IPP_TAG_KEYWORD
)) == NULL
)
4951 * If the attribute contains a single "all" keyword, return NULL...
4954 if (requested
->num_values
== 1 &&
4955 !strcmp(requested
->values
[0].string
.text
, "all"))
4959 * Create an array using "strcmp" as the comparison function...
4962 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
4964 for (i
= 0; i
< requested
->num_values
; i
++)
4966 value
= requested
->values
[i
].string
.text
;
4968 if (!strcmp(value
, "job-template"))
4970 cupsArrayAdd(ra
, "copies");
4971 cupsArrayAdd(ra
, "copies-default");
4972 cupsArrayAdd(ra
, "copies-supported");
4973 cupsArrayAdd(ra
, "finishings");
4974 cupsArrayAdd(ra
, "finishings-default");
4975 cupsArrayAdd(ra
, "finishings-supported");
4976 cupsArrayAdd(ra
, "job-hold-until");
4977 cupsArrayAdd(ra
, "job-hold-until-default");
4978 cupsArrayAdd(ra
, "job-hold-until-supported");
4979 cupsArrayAdd(ra
, "job-priority");
4980 cupsArrayAdd(ra
, "job-priority-default");
4981 cupsArrayAdd(ra
, "job-priority-supported");
4982 cupsArrayAdd(ra
, "job-sheets");
4983 cupsArrayAdd(ra
, "job-sheets-default");
4984 cupsArrayAdd(ra
, "job-sheets-supported");
4985 cupsArrayAdd(ra
, "media");
4986 cupsArrayAdd(ra
, "media-default");
4987 cupsArrayAdd(ra
, "media-supported");
4988 cupsArrayAdd(ra
, "multiple-document-handling");
4989 cupsArrayAdd(ra
, "multiple-document-handling-default");
4990 cupsArrayAdd(ra
, "multiple-document-handling-supported");
4991 cupsArrayAdd(ra
, "number-up");
4992 cupsArrayAdd(ra
, "number-up-default");
4993 cupsArrayAdd(ra
, "number-up-supported");
4994 cupsArrayAdd(ra
, "orientation-requested");
4995 cupsArrayAdd(ra
, "orientation-requested-default");
4996 cupsArrayAdd(ra
, "orientation-requested-supported");
4997 cupsArrayAdd(ra
, "page-ranges");
4998 cupsArrayAdd(ra
, "page-ranges-supported");
4999 cupsArrayAdd(ra
, "printer-resolution");
5000 cupsArrayAdd(ra
, "printer-resolution-default");
5001 cupsArrayAdd(ra
, "printer-resolution-supported");
5002 cupsArrayAdd(ra
, "print-quality");
5003 cupsArrayAdd(ra
, "print-quality-default");
5004 cupsArrayAdd(ra
, "print-quality-supported");
5005 cupsArrayAdd(ra
, "sides");
5006 cupsArrayAdd(ra
, "sides-default");
5007 cupsArrayAdd(ra
, "sides-supported");
5009 else if (!strcmp(value
, "job-description"))
5011 cupsArrayAdd(ra
, "date-time-at-completed");
5012 cupsArrayAdd(ra
, "date-time-at-creation");
5013 cupsArrayAdd(ra
, "date-time-at-processing");
5014 cupsArrayAdd(ra
, "job-detailed-status-message");
5015 cupsArrayAdd(ra
, "job-document-access-errors");
5016 cupsArrayAdd(ra
, "job-id");
5017 cupsArrayAdd(ra
, "job-impressions");
5018 cupsArrayAdd(ra
, "job-impressions-completed");
5019 cupsArrayAdd(ra
, "job-k-octets");
5020 cupsArrayAdd(ra
, "job-k-octets-processed");
5021 cupsArrayAdd(ra
, "job-media-sheets");
5022 cupsArrayAdd(ra
, "job-media-sheets-completed");
5023 cupsArrayAdd(ra
, "job-message-from-operator");
5024 cupsArrayAdd(ra
, "job-more-info");
5025 cupsArrayAdd(ra
, "job-name");
5026 cupsArrayAdd(ra
, "job-originating-user-name");
5027 cupsArrayAdd(ra
, "job-printer-up-time");
5028 cupsArrayAdd(ra
, "job-printer-uri");
5029 cupsArrayAdd(ra
, "job-state");
5030 cupsArrayAdd(ra
, "job-state-message");
5031 cupsArrayAdd(ra
, "job-state-reasons");
5032 cupsArrayAdd(ra
, "job-uri");
5033 cupsArrayAdd(ra
, "number-of-documents");
5034 cupsArrayAdd(ra
, "number-of-intervening-jobs");
5035 cupsArrayAdd(ra
, "output-device-assigned");
5036 cupsArrayAdd(ra
, "time-at-completed");
5037 cupsArrayAdd(ra
, "time-at-creation");
5038 cupsArrayAdd(ra
, "time-at-processing");
5040 else if (!strcmp(value
, "printer-description"))
5042 cupsArrayAdd(ra
, "charset-configured");
5043 cupsArrayAdd(ra
, "charset-supported");
5044 cupsArrayAdd(ra
, "color-supported");
5045 cupsArrayAdd(ra
, "compression-supported");
5046 cupsArrayAdd(ra
, "document-format-default");
5047 cupsArrayAdd(ra
, "document-format-supported");
5048 cupsArrayAdd(ra
, "generated-natural-language-supported");
5049 cupsArrayAdd(ra
, "ipp-versions-supported");
5050 cupsArrayAdd(ra
, "job-impressions-supported");
5051 cupsArrayAdd(ra
, "job-k-octets-supported");
5052 cupsArrayAdd(ra
, "job-media-sheets-supported");
5053 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
5054 cupsArrayAdd(ra
, "multiple-operation-time-out");
5055 cupsArrayAdd(ra
, "natural-language-configured");
5056 cupsArrayAdd(ra
, "notify-attributes-supported");
5057 cupsArrayAdd(ra
, "notify-lease-duration-default");
5058 cupsArrayAdd(ra
, "notify-lease-duration-supported");
5059 cupsArrayAdd(ra
, "notify-max-events-supported");
5060 cupsArrayAdd(ra
, "notify-events-default");
5061 cupsArrayAdd(ra
, "notify-events-supported");
5062 cupsArrayAdd(ra
, "notify-pull-method-supported");
5063 cupsArrayAdd(ra
, "notify-schemes-supported");
5064 cupsArrayAdd(ra
, "operations-supported");
5065 cupsArrayAdd(ra
, "pages-per-minute");
5066 cupsArrayAdd(ra
, "pages-per-minute-color");
5067 cupsArrayAdd(ra
, "pdl-override-supported");
5068 cupsArrayAdd(ra
, "printer-alert");
5069 cupsArrayAdd(ra
, "printer-alert-description");
5070 cupsArrayAdd(ra
, "printer-current-time");
5071 cupsArrayAdd(ra
, "printer-driver-installer");
5072 cupsArrayAdd(ra
, "printer-info");
5073 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
5074 cupsArrayAdd(ra
, "printer-location");
5075 cupsArrayAdd(ra
, "printer-make-and-model");
5076 cupsArrayAdd(ra
, "printer-message-from-operator");
5077 cupsArrayAdd(ra
, "printer-more-info");
5078 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
5079 cupsArrayAdd(ra
, "printer-name");
5080 cupsArrayAdd(ra
, "printer-state");
5081 cupsArrayAdd(ra
, "printer-state-message");
5082 cupsArrayAdd(ra
, "printer-state-reasons");
5083 cupsArrayAdd(ra
, "printer-up-time");
5084 cupsArrayAdd(ra
, "printer-uri-supported");
5085 cupsArrayAdd(ra
, "queued-job-count");
5086 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
5087 cupsArrayAdd(ra
, "uri-authentication-supported");
5088 cupsArrayAdd(ra
, "uri-security-supported");
5090 else if (!strcmp(value
, "printer-defaults"))
5092 char *name
; /* Option name */
5095 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5097 name
= (char *)cupsArrayNext(CommonDefaults
))
5098 cupsArrayAdd(ra
, name
);
5100 else if (!strcmp(value
, "subscription-template"))
5102 cupsArrayAdd(ra
, "notify-attributes");
5103 cupsArrayAdd(ra
, "notify-charset");
5104 cupsArrayAdd(ra
, "notify-events");
5105 cupsArrayAdd(ra
, "notify-lease-duration");
5106 cupsArrayAdd(ra
, "notify-natural-language");
5107 cupsArrayAdd(ra
, "notify-pull-method");
5108 cupsArrayAdd(ra
, "notify-recipient-uri");
5109 cupsArrayAdd(ra
, "notify-time-interval");
5110 cupsArrayAdd(ra
, "notify-user-data");
5113 cupsArrayAdd(ra
, value
);
5121 * 'create_subscription()' - Create a notification subscription.
5125 create_subscription(
5126 cupsd_client_t
*con
, /* I - Client connection */
5127 ipp_attribute_t
*uri
) /* I - Printer URI */
5129 http_status_t status
; /* Policy status */
5130 int i
; /* Looping var */
5131 ipp_attribute_t
*attr
; /* Current attribute */
5132 cups_ptype_t dtype
; /* Destination type (printer/class) */
5133 char scheme
[HTTP_MAX_URI
],
5134 /* Scheme portion of URI */
5135 userpass
[HTTP_MAX_URI
],
5136 /* Username portion of URI */
5138 /* Host portion of URI */
5139 resource
[HTTP_MAX_URI
];
5140 /* Resource portion of URI */
5141 int port
; /* Port portion of URI */
5142 cupsd_printer_t
*printer
; /* Printer/class */
5143 cupsd_job_t
*job
; /* Job */
5144 int jobid
; /* Job ID */
5145 cupsd_subscription_t
*sub
; /* Subscription object */
5146 const char *username
, /* requesting-user-name or
5147 authenticated username */
5148 *recipient
, /* notify-recipient-uri */
5149 *pullmethod
; /* notify-pull-method */
5150 ipp_attribute_t
*user_data
; /* notify-user-data */
5151 int interval
, /* notify-time-interval */
5152 lease
; /* notify-lease-duration */
5153 unsigned mask
; /* notify-events */
5154 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5155 *notify_lease
; /* notify-lease-duration(-default) */
5159 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5161 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5162 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
5163 attr
->value_tag
, attr
->name
);
5165 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
5170 * Is the destination valid?
5173 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5174 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5175 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5177 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5178 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5179 sizeof(host
), &port
, resource
, sizeof(resource
));
5181 if (!strcmp(resource
, "/"))
5183 dtype
= (cups_ptype_t
)0;
5186 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5188 dtype
= (cups_ptype_t
)0;
5191 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5193 dtype
= CUPS_PRINTER_CLASS
;
5196 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5202 send_ipp_status(con
, IPP_NOT_FOUND
,
5203 _("The printer or class was not found."));
5213 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5216 send_http_error(con
, status
, printer
);
5220 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5222 send_http_error(con
, status
, NULL
);
5227 * Get the user that is requesting the subscription...
5230 username
= get_username(con
);
5233 * Find the first subscription group attribute; return if we have
5237 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5238 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5243 send_ipp_status(con
, IPP_BAD_REQUEST
,
5244 _("No subscription attributes in request!"));
5249 * Process the subscription attributes in the request...
5252 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5260 lease
= DefaultLeaseDuration
;
5262 mask
= CUPSD_EVENT_NONE
;
5266 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5268 notify_lease
= ippFindAttribute(printer
->attrs
,
5269 "notify-lease-duration-default",
5273 lease
= notify_lease
->values
[0].integer
;
5277 notify_events
= NULL
;
5278 notify_lease
= NULL
;
5281 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5283 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5284 attr
->value_tag
== IPP_TAG_URI
)
5287 * Validate the recipient scheme against the ServerBin/notifier
5291 char notifier
[1024]; /* Notifier filename */
5294 recipient
= attr
->values
[0].string
.text
;
5296 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5297 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5298 host
, sizeof(host
), &port
,
5299 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5301 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5302 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
5303 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5304 "notify-status-code", IPP_URI_SCHEME
);
5308 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5310 if (access(notifier
, X_OK
))
5312 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5313 _("notify-recipient-uri URI \"%s\" uses unknown "
5314 "scheme!"), recipient
);
5315 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5316 "notify-status-code", IPP_URI_SCHEME
);
5320 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5321 attr
->value_tag
== IPP_TAG_KEYWORD
)
5323 pullmethod
= attr
->values
[0].string
.text
;
5325 if (strcmp(pullmethod
, "ippget"))
5327 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5328 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
5329 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5330 "notify-status-code", IPP_ATTRIBUTES
);
5334 else if (!strcmp(attr
->name
, "notify-charset") &&
5335 attr
->value_tag
== IPP_TAG_CHARSET
&&
5336 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5337 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5339 send_ipp_status(con
, IPP_CHARSET
,
5340 _("Character set \"%s\" not supported!"),
5341 attr
->values
[0].string
.text
);
5344 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5345 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5346 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5348 send_ipp_status(con
, IPP_CHARSET
,
5349 _("Language \"%s\" not supported!"),
5350 attr
->values
[0].string
.text
);
5353 else if (!strcmp(attr
->name
, "notify-user-data") &&
5354 attr
->value_tag
== IPP_TAG_STRING
)
5356 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5358 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5359 _("The notify-user-data value is too large "
5360 "(%d > 63 octets)!"),
5361 attr
->values
[0].unknown
.length
);
5367 else if (!strcmp(attr
->name
, "notify-events") &&
5368 attr
->value_tag
== IPP_TAG_KEYWORD
)
5369 notify_events
= attr
;
5370 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5371 attr
->value_tag
== IPP_TAG_INTEGER
)
5372 lease
= attr
->values
[0].integer
;
5373 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5374 attr
->value_tag
== IPP_TAG_INTEGER
)
5375 interval
= attr
->values
[0].integer
;
5376 else if (!strcmp(attr
->name
, "notify-job-id") &&
5377 attr
->value_tag
== IPP_TAG_INTEGER
)
5378 jobid
= attr
->values
[0].integer
;
5385 for (i
= 0; i
< notify_events
->num_values
; i
++)
5386 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5390 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5392 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5393 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5394 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5396 if (!recipient
&& !pullmethod
)
5399 if (mask
== CUPSD_EVENT_NONE
)
5402 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5404 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5407 send_ipp_status(con
, IPP_BAD_REQUEST
,
5408 _("notify-events not specified!"));
5413 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5415 cupsdLogMessage(CUPSD_LOG_INFO
,
5416 "create_subscription: Limiting notify-lease-duration to "
5419 lease
= MaxLeaseDuration
;
5424 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5426 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
5433 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
5436 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
5439 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5440 "Added subscription %d for printer \"%s\"",
5441 sub
->id
, printer
->name
);
5443 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
5446 sub
->interval
= interval
;
5448 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5450 cupsdSetString(&sub
->owner
, username
);
5454 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5455 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5456 sub
->user_data_len
);
5459 ippAddSeparator(con
->response
);
5460 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5461 "notify-subscription-id", sub
->id
);
5463 con
->response
->request
.status
.status_code
= IPP_OK
;
5469 cupsdSaveAllSubscriptions();
5475 * 'delete_printer()' - Remove a printer or class from the system.
5479 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5480 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5482 http_status_t status
; /* Policy status */
5483 cups_ptype_t dtype
; /* Destination type (printer/class) */
5484 cupsd_printer_t
*printer
; /* Printer/class */
5485 char filename
[1024]; /* Script/PPD filename */
5488 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5489 con
->http
.fd
, uri
->values
[0].string
.text
);
5492 * Do we have a valid URI?
5495 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5501 send_ipp_status(con
, IPP_NOT_FOUND
,
5502 _("The printer or class was not found."));
5510 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5512 send_http_error(con
, status
, NULL
);
5517 * Remove old jobs...
5520 cupsdCancelJobs(printer
->name
, NULL
, 1);
5523 * Remove old subscriptions and send a "deleted printer" event...
5526 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5527 "%s \"%s\" deleted by \"%s\".",
5528 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5529 printer
->name
, get_username(con
));
5531 cupsdExpireSubscriptions(printer
, NULL
);
5534 * Remove any old PPD or script files...
5537 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5541 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5545 if (dtype
& CUPS_PRINTER_CLASS
)
5547 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5548 printer
->name
, get_username(con
));
5550 cupsdDeletePrinter(printer
, 0);
5551 cupsdSaveAllClasses();
5555 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5556 printer
->name
, get_username(con
));
5558 cupsdDeletePrinter(printer
, 0);
5559 cupsdSaveAllPrinters();
5562 cupsdWritePrintcap();
5565 * Return with no errors...
5568 con
->response
->request
.status
.status_code
= IPP_OK
;
5573 * 'get_default()' - Get the default destination.
5577 get_default(cupsd_client_t
*con
) /* I - Client connection */
5579 http_status_t status
; /* Policy status */
5580 cups_array_t
*ra
; /* Requested attributes array */
5583 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5589 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5591 send_http_error(con
, status
, NULL
);
5597 ra
= create_requested_array(con
->request
);
5599 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5601 cupsArrayDelete(ra
);
5603 con
->response
->request
.status
.status_code
= IPP_OK
;
5606 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5611 * 'get_devices()' - Get the list of available devices on the local system.
5615 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5617 http_status_t status
; /* Policy status */
5618 ipp_attribute_t
*limit
, /* Limit attribute */
5619 *requested
; /* requested-attributes attribute */
5620 char command
[1024], /* cups-deviced command */
5621 options
[1024], /* Options to pass to command */
5623 /* String for requested attributes */
5626 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5632 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5634 send_http_error(con
, status
, NULL
);
5639 * Run cups-deviced command with the given options...
5642 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5643 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5647 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5649 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5651 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5652 snprintf(options
, sizeof(options
),
5654 con
->request
->request
.op
.request_id
,
5655 limit
? limit
->values
[0].integer
: 0, (int)User
,
5658 if (cupsdSendCommand(con
, command
, options
, 1))
5661 * Command started successfully, don't send an IPP response here...
5664 ippDelete(con
->response
);
5665 con
->response
= NULL
;
5670 * Command failed, return "internal error" so the user knows something
5674 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5675 _("cups-deviced failed to execute."));
5681 * 'get_document()' - Get a copy of a job file.
5685 get_document(cupsd_client_t
*con
, /* I - Client connection */
5686 ipp_attribute_t
*uri
) /* I - Job URI */
5688 http_status_t status
; /* Policy status */
5689 ipp_attribute_t
*attr
; /* Current attribute */
5690 int jobid
; /* Job ID */
5691 int docnum
; /* Document number */
5692 cupsd_job_t
*job
; /* Current job */
5693 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5694 username
[HTTP_MAX_URI
], /* Username portion of URI */
5695 host
[HTTP_MAX_URI
], /* Host portion of URI */
5696 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5697 int port
; /* Port portion of URI */
5698 char filename
[1024], /* Filename for document */
5699 format
[1024]; /* Format for document */
5702 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
5703 con
->http
.fd
, uri
->values
[0].string
.text
);
5706 * See if we have a job URI or a printer URI...
5709 if (!strcmp(uri
->name
, "printer-uri"))
5712 * Got a printer URI; see if we also have a job-id attribute...
5715 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5716 IPP_TAG_INTEGER
)) == NULL
)
5718 send_ipp_status(con
, IPP_BAD_REQUEST
,
5719 _("Got a printer-uri attribute but no job-id!"));
5723 jobid
= attr
->values
[0].integer
;
5728 * Got a job URI; parse it to get the job ID...
5731 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5732 sizeof(method
), username
, sizeof(username
), host
,
5733 sizeof(host
), &port
, resource
, sizeof(resource
));
5735 if (strncmp(resource
, "/jobs/", 6))
5741 send_ipp_status(con
, IPP_BAD_REQUEST
,
5742 _("Bad job-uri attribute \"%s\"!"),
5743 uri
->values
[0].string
.text
);
5747 jobid
= atoi(resource
+ 6);
5751 * See if the job exists...
5754 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5757 * Nope - return a "not found" error...
5760 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5768 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5770 send_http_error(con
, status
, NULL
);
5775 * Get the document number...
5778 if ((attr
= ippFindAttribute(con
->request
, "document-number",
5779 IPP_TAG_INTEGER
)) == NULL
)
5781 send_ipp_status(con
, IPP_BAD_REQUEST
,
5782 _("Missing document-number attribute!"));
5786 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
5787 attr
->num_values
> 1)
5789 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
5794 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
5796 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
5798 cupsdLogMessage(CUPSD_LOG_ERROR
,
5799 "Unable to open document %d in job %d - %s", docnum
, jobid
,
5801 send_ipp_status(con
, IPP_NOT_FOUND
,
5802 _("Unable to open document %d in job %d!"), docnum
, jobid
);
5806 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
5810 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
5811 job
->filetypes
[docnum
- 1]->type
);
5813 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
5815 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
5817 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
5818 IPP_TAG_NAME
)) != NULL
)
5819 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
5820 NULL
, attr
->values
[0].string
.text
);
5825 * 'get_job_attrs()' - Get job attributes.
5829 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5830 ipp_attribute_t
*uri
) /* I - Job URI */
5832 http_status_t status
; /* Policy status */
5833 ipp_attribute_t
*attr
; /* Current attribute */
5834 int jobid
; /* Job ID */
5835 cupsd_job_t
*job
; /* Current job */
5836 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5837 username
[HTTP_MAX_URI
], /* Username portion of URI */
5838 host
[HTTP_MAX_URI
], /* Host portion of URI */
5839 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5840 int port
; /* Port portion of URI */
5841 cups_array_t
*ra
; /* Requested attributes array */
5844 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5845 con
->http
.fd
, uri
->values
[0].string
.text
);
5848 * See if we have a job URI or a printer URI...
5851 if (!strcmp(uri
->name
, "printer-uri"))
5854 * Got a printer URI; see if we also have a job-id attribute...
5857 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5858 IPP_TAG_INTEGER
)) == NULL
)
5860 send_ipp_status(con
, IPP_BAD_REQUEST
,
5861 _("Got a printer-uri attribute but no job-id!"));
5865 jobid
= attr
->values
[0].integer
;
5870 * Got a job URI; parse it to get the job ID...
5873 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5874 sizeof(method
), username
, sizeof(username
), host
,
5875 sizeof(host
), &port
, resource
, sizeof(resource
));
5877 if (strncmp(resource
, "/jobs/", 6))
5883 send_ipp_status(con
, IPP_BAD_REQUEST
,
5884 _("Bad job-uri attribute \"%s\"!"),
5885 uri
->values
[0].string
.text
);
5889 jobid
= atoi(resource
+ 6);
5893 * See if the job exists...
5896 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5899 * Nope - return a "not found" error...
5902 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5910 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5912 send_http_error(con
, status
, NULL
);
5917 * Copy attributes...
5922 ra
= create_requested_array(con
->request
);
5923 copy_job_attrs(con
, job
, ra
);
5924 cupsArrayDelete(ra
);
5926 con
->response
->request
.status
.status_code
= IPP_OK
;
5931 * 'get_jobs()' - Get a list of jobs for the specified printer.
5935 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
5936 ipp_attribute_t
*uri
) /* I - Printer URI */
5938 http_status_t status
; /* Policy status */
5939 ipp_attribute_t
*attr
; /* Current attribute */
5940 const char *dest
; /* Destination */
5941 cups_ptype_t dtype
; /* Destination type (printer/class) */
5942 cups_ptype_t dmask
; /* Destination type mask */
5943 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
5944 username
[HTTP_MAX_URI
], /* Username portion of URI */
5945 host
[HTTP_MAX_URI
], /* Host portion of URI */
5946 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5947 int port
; /* Port portion of URI */
5948 int completed
; /* Completed jobs? */
5949 int first_job_id
; /* First job ID */
5950 int limit
; /* Maximum number of jobs to return */
5951 int count
; /* Number of jobs that match */
5952 cupsd_job_t
*job
; /* Current job pointer */
5953 cupsd_printer_t
*printer
; /* Printer */
5954 cups_array_t
*list
; /* Which job list... */
5955 cups_array_t
*ra
; /* Requested attributes array */
5958 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
5959 uri
->values
[0].string
.text
);
5962 * Is the destination valid?
5965 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5966 sizeof(scheme
), username
, sizeof(username
), host
,
5967 sizeof(host
), &port
, resource
, sizeof(resource
));
5969 if (!strcmp(resource
, "/") ||
5970 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
5973 dtype
= (cups_ptype_t
)0;
5974 dmask
= (cups_ptype_t
)0;
5977 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5980 dtype
= (cups_ptype_t
)0;
5981 dmask
= CUPS_PRINTER_CLASS
;
5984 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5987 dtype
= CUPS_PRINTER_CLASS
;
5988 dmask
= CUPS_PRINTER_CLASS
;
5991 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
5998 send_ipp_status(con
, IPP_NOT_FOUND
,
5999 _("The printer or class was not found."));
6004 dtype
&= CUPS_PRINTER_CLASS
;
6005 dmask
= CUPS_PRINTER_CLASS
;
6014 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6017 send_http_error(con
, status
, printer
);
6021 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6023 send_http_error(con
, status
, NULL
);
6028 * See if the "which-jobs" attribute have been specified...
6031 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6032 IPP_TAG_KEYWORD
)) != NULL
&&
6033 !strcmp(attr
->values
[0].string
.text
, "completed"))
6038 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
6050 * See if they want to limit the number of jobs reported...
6053 if ((attr
= ippFindAttribute(con
->request
, "limit",
6054 IPP_TAG_INTEGER
)) != NULL
)
6055 limit
= attr
->values
[0].integer
;
6059 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
6060 IPP_TAG_INTEGER
)) != NULL
)
6061 first_job_id
= attr
->values
[0].integer
;
6066 * See if we only want to see jobs for a specific user...
6069 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
6070 IPP_TAG_BOOLEAN
)) != NULL
&&
6071 attr
->values
[0].boolean
)
6072 strlcpy(username
, get_username(con
), sizeof(username
));
6076 ra
= create_requested_array(con
->request
);
6079 * OK, build a list of jobs for this printer...
6082 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6083 count
< limit
&& job
;
6084 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6087 * Filter out jobs that don't match...
6090 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
6092 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6093 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6095 if ((job
->dtype
& dmask
) != dtype
&&
6096 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6098 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
6101 if (job
->id
< first_job_id
)
6109 if (username
[0] && strcasecmp(username
, job
->username
))
6113 ippAddSeparator(con
->response
);
6117 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
6119 copy_job_attrs(con
, job
, ra
);
6122 cupsArrayDelete(ra
);
6124 con
->response
->request
.status
.status_code
= IPP_OK
;
6129 * 'get_notifications()' - Get events for a subscription.
6133 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6135 int i
, j
; /* Looping vars */
6136 http_status_t status
; /* Policy status */
6137 cupsd_subscription_t
*sub
; /* Subscription */
6138 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6139 *sequences
; /* notify-sequence-numbers */
6140 int min_seq
; /* Minimum sequence number */
6141 int interval
; /* Poll interval */
6144 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6148 * Get subscription attributes...
6151 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6153 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6158 send_ipp_status(con
, IPP_BAD_REQUEST
,
6159 _("Missing notify-subscription-ids attribute!"));
6164 * Are the subscription IDs valid?
6167 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6169 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6172 * Bad subscription ID...
6175 send_ipp_status(con
, IPP_NOT_FOUND
,
6176 _("notify-subscription-id %d no good!"),
6177 ids
->values
[i
].integer
);
6185 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6187 con
, sub
->owner
)) != HTTP_OK
)
6189 send_http_error(con
, status
, sub
->dest
);
6194 * Check the subscription type and update the interval accordingly.
6197 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6200 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6202 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6208 * Tell the client to poll again in N seconds...
6212 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6213 "notify-get-interval", interval
);
6215 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6216 "printer-up-time", time(NULL
));
6219 * Copy the subscription event attributes to the response.
6222 con
->response
->request
.status
.status_code
=
6223 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6225 for (i
= 0; i
< ids
->num_values
; i
++)
6228 * Get the subscription and sequence number...
6231 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6233 if (sequences
&& i
< sequences
->num_values
)
6234 min_seq
= sequences
->values
[i
].integer
;
6239 * If we don't have any new events, nothing to do here...
6242 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
6246 * Otherwise copy all of the new events...
6249 if (sub
->first_event_id
> min_seq
)
6252 j
= min_seq
- sub
->first_event_id
;
6254 for (; j
< sub
->num_events
; j
++)
6256 ippAddSeparator(con
->response
);
6258 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
6259 IPP_TAG_EVENT_NOTIFICATION
, 0);
6266 * 'get_ppd()' - Get a named PPD from the local system.
6270 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
6271 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
6273 http_status_t status
; /* Policy status */
6274 cupsd_printer_t
*dest
; /* Destination */
6275 cups_ptype_t dtype
; /* Destination type */
6278 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
6279 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
6281 if (!strcmp(uri
->name
, "ppd-name"))
6284 * Return a PPD file from cups-driverd...
6287 char command
[1024], /* cups-driverd command */
6288 options
[1024], /* Options to pass to command */
6289 ppd_name
[1024]; /* ppd-name */
6296 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6298 send_http_error(con
, status
, NULL
);
6303 * Run cups-driverd command with the given options...
6306 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6307 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
6308 snprintf(options
, sizeof(options
), "get+%d+%s",
6309 con
->request
->request
.op
.request_id
, ppd_name
);
6311 if (cupsdSendCommand(con
, command
, options
, 0))
6314 * Command started successfully, don't send an IPP response here...
6317 ippDelete(con
->response
);
6318 con
->response
= NULL
;
6323 * Command failed, return "internal error" so the user knows something
6327 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6328 _("cups-driverd failed to execute."));
6331 else if (!strcmp(uri
->name
, "printer-uri") &&
6332 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
6334 int i
; /* Looping var */
6335 char filename
[1024]; /* PPD filename */
6342 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6344 send_http_error(con
, status
, dest
);
6349 * See if we need the PPD for a class or remote printer...
6352 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6355 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
6357 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6358 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6359 "printer-uri", NULL
, dest
->uri
);
6362 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
6364 for (i
= 0; i
< dest
->num_printers
; i
++)
6365 if (!(dest
->printers
[i
]->type
&
6366 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
6368 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6369 dest
->printers
[i
]->name
);
6371 if (!access(filename
, 0))
6375 if (i
< dest
->num_printers
)
6376 dest
= dest
->printers
[i
];
6379 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6380 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6381 "printer-uri", NULL
, dest
->printers
[0]->uri
);
6387 * Found the printer with the PPD file, now see if there is one...
6390 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
6392 send_ipp_status(con
, IPP_NOT_FOUND
,
6393 _("The PPD file \"%s\" could not be opened: %s"),
6394 uri
->values
[0].string
.text
, strerror(errno
));
6398 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6402 con
->response
->request
.status
.status_code
= IPP_OK
;
6405 send_ipp_status(con
, IPP_NOT_FOUND
,
6406 _("The PPD file \"%s\" could not be found."),
6407 uri
->values
[0].string
.text
);
6412 * 'get_ppds()' - Get the list of PPD files on the local system.
6416 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
6418 http_status_t status
; /* Policy status */
6419 ipp_attribute_t
*limit
, /* Limit attribute */
6420 *device
, /* ppd-device-id attribute */
6421 *language
, /* ppd-natural-language attribute */
6422 *make
, /* ppd-make attribute */
6423 *model
, /* ppd-make-and-model attribute */
6424 *model_number
, /* ppd-model-number attribute */
6425 *product
, /* ppd-product attribute */
6426 *psversion
, /* ppd-psverion attribute */
6427 *type
, /* ppd-type attribute */
6428 *requested
; /* requested-attributes attribute */
6429 char command
[1024], /* cups-driverd command */
6430 options
[1024], /* Options to pass to command */
6431 device_str
[256],/* Escaped ppd-device-id string */
6433 /* Escaped ppd-natural-language */
6434 make_str
[256], /* Escaped ppd-make string */
6435 model_str
[256], /* Escaped ppd-make-and-model string */
6436 model_number_str
[256],
6437 /* ppd-model-number string */
6439 /* Escaped ppd-product string */
6441 /* Escaped ppd-psversion string */
6442 type_str
[256], /* Escaped ppd-type string */
6444 /* String for requested attributes */
6447 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
6453 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6455 send_http_error(con
, status
, NULL
);
6460 * Run cups-driverd command with the given options...
6463 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6464 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
6465 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
6467 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
6468 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
6470 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
6472 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
6473 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
6474 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
6475 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6479 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6481 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6484 url_encode_attr(device
, device_str
, sizeof(device_str
));
6486 device_str
[0] = '\0';
6489 url_encode_attr(language
, language_str
, sizeof(language_str
));
6491 language_str
[0] = '\0';
6494 url_encode_attr(make
, make_str
, sizeof(make_str
));
6499 url_encode_attr(model
, model_str
, sizeof(model_str
));
6501 model_str
[0] = '\0';
6504 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
6505 model_number
->values
[0].integer
);
6507 model_number_str
[0] = '\0';
6510 url_encode_attr(product
, product_str
, sizeof(product_str
));
6512 product_str
[0] = '\0';
6515 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
6517 psversion_str
[0] = '\0';
6520 url_encode_attr(type
, type_str
, sizeof(type_str
));
6524 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6525 snprintf(options
, sizeof(options
),
6526 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
6527 con
->request
->request
.op
.request_id
,
6528 limit
? limit
->values
[0].integer
: 0,
6530 device
? "%20" : "", device_str
,
6531 language
? "%20" : "", language_str
,
6532 make
? "%20" : "", make_str
,
6533 model
? "%20" : "", model_str
,
6534 model_number
? "%20" : "", model_number_str
,
6535 product
? "%20" : "", product_str
,
6536 psversion
? "%20" : "", psversion_str
,
6537 type
? "%20" : "", type_str
);
6539 if (cupsdSendCommand(con
, command
, options
, 0))
6542 * Command started successfully, don't send an IPP response here...
6545 ippDelete(con
->response
);
6546 con
->response
= NULL
;
6551 * Command failed, return "internal error" so the user knows something
6555 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6556 _("cups-driverd failed to execute."));
6562 * 'get_printer_attrs()' - Get printer attributes.
6566 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
6567 ipp_attribute_t
*uri
) /* I - Printer URI */
6569 http_status_t status
; /* Policy status */
6570 cups_ptype_t dtype
; /* Destination type (printer/class) */
6571 cupsd_printer_t
*printer
; /* Printer/class */
6572 cups_array_t
*ra
; /* Requested attributes array */
6575 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
6576 con
->http
.fd
, uri
->values
[0].string
.text
);
6579 * Is the destination valid?
6582 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6588 send_ipp_status(con
, IPP_NOT_FOUND
,
6589 _("The printer or class was not found."));
6597 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6599 send_http_error(con
, status
, printer
);
6604 * Send the attributes...
6607 ra
= create_requested_array(con
->request
);
6609 copy_printer_attrs(con
, printer
, ra
);
6611 cupsArrayDelete(ra
);
6613 con
->response
->request
.status
.status_code
= IPP_OK
;
6618 * 'get_printers()' - Get a list of printers or classes.
6622 get_printers(cupsd_client_t
*con
, /* I - Client connection */
6623 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
6625 http_status_t status
; /* Policy status */
6626 ipp_attribute_t
*attr
; /* Current attribute */
6627 int limit
; /* Max number of printers to return */
6628 int count
; /* Number of printers that match */
6629 cupsd_printer_t
*printer
; /* Current printer pointer */
6630 int printer_type
, /* printer-type attribute */
6631 printer_mask
; /* printer-type-mask attribute */
6632 char *location
; /* Location string */
6633 const char *username
; /* Current user */
6634 char *first_printer_name
; /* first-printer-name attribute */
6635 cups_array_t
*ra
; /* Requested attributes array */
6638 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
6639 con
->http
.fd
, type
);
6645 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6647 send_http_error(con
, status
, NULL
);
6652 * Check for printers...
6655 if (!Printers
|| !cupsArrayCount(Printers
))
6657 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
6662 * See if they want to limit the number of printers reported...
6665 if ((attr
= ippFindAttribute(con
->request
, "limit",
6666 IPP_TAG_INTEGER
)) != NULL
)
6667 limit
= attr
->values
[0].integer
;
6671 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
6672 IPP_TAG_NAME
)) != NULL
)
6673 first_printer_name
= attr
->values
[0].string
.text
;
6675 first_printer_name
= NULL
;
6678 * Support filtering...
6681 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
6682 IPP_TAG_ENUM
)) != NULL
)
6683 printer_type
= attr
->values
[0].integer
;
6687 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
6688 IPP_TAG_ENUM
)) != NULL
)
6689 printer_mask
= attr
->values
[0].integer
;
6693 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
6694 IPP_TAG_TEXT
)) != NULL
)
6695 location
= attr
->values
[0].string
.text
;
6699 if (con
->username
[0])
6700 username
= con
->username
;
6701 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6702 IPP_TAG_NAME
)) != NULL
)
6703 username
= attr
->values
[0].string
.text
;
6707 ra
= create_requested_array(con
->request
);
6710 * OK, build a list of printers for this printer...
6713 if (first_printer_name
)
6715 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
6716 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
6719 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
6722 count
< limit
&& printer
;
6723 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
6725 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
6726 (printer
->type
& printer_mask
) == printer_type
&&
6727 (!location
|| !printer
->location
||
6728 !strcasecmp(printer
->location
, location
)))
6731 * If HideImplicitMembers is enabled, see if this printer or class
6732 * is a member of an implicit class...
6735 if (ImplicitClasses
&& HideImplicitMembers
&&
6736 printer
->in_implicit_class
)
6740 * If a username is specified, see if it is allowed or denied
6744 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
6748 * Add the group separator as needed...
6752 ippAddSeparator(con
->response
);
6757 * Send the attributes...
6760 copy_printer_attrs(con
, printer
, ra
);
6764 cupsArrayDelete(ra
);
6766 con
->response
->request
.status
.status_code
= IPP_OK
;
6771 * 'get_subscription_attrs()' - Get subscription attributes.
6775 get_subscription_attrs(
6776 cupsd_client_t
*con
, /* I - Client connection */
6777 int sub_id
) /* I - Subscription ID */
6779 http_status_t status
; /* Policy status */
6780 cupsd_subscription_t
*sub
; /* Subscription */
6781 cups_array_t
*ra
; /* Requested attributes array */
6784 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6785 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
6786 con
, con
->http
.fd
, sub_id
);
6789 * Is the subscription ID valid?
6792 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
6795 * Bad subscription ID...
6798 send_ipp_status(con
, IPP_NOT_FOUND
,
6799 _("notify-subscription-id %d no good!"), sub_id
);
6807 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6809 con
, sub
->owner
)) != HTTP_OK
)
6811 send_http_error(con
, status
, sub
->dest
);
6816 * Copy the subscription attributes to the response using the
6817 * requested-attributes attribute that may be provided by the client.
6820 ra
= create_requested_array(con
->request
);
6822 copy_subscription_attrs(con
, sub
, ra
);
6824 cupsArrayDelete(ra
);
6826 con
->response
->request
.status
.status_code
= IPP_OK
;
6831 * 'get_subscriptions()' - Get subscriptions.
6835 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
6836 ipp_attribute_t
*uri
) /* I - Printer/job URI */
6838 http_status_t status
; /* Policy status */
6839 int count
; /* Number of subscriptions */
6840 int limit
; /* Limit */
6841 cupsd_subscription_t
*sub
; /* Subscription */
6842 cups_array_t
*ra
; /* Requested attributes array */
6843 ipp_attribute_t
*attr
; /* Attribute */
6844 cups_ptype_t dtype
; /* Destination type (printer/class) */
6845 char scheme
[HTTP_MAX_URI
],
6846 /* Scheme portion of URI */
6847 username
[HTTP_MAX_URI
],
6848 /* Username portion of URI */
6850 /* Host portion of URI */
6851 resource
[HTTP_MAX_URI
];
6852 /* Resource portion of URI */
6853 int port
; /* Port portion of URI */
6854 cupsd_job_t
*job
; /* Job pointer */
6855 cupsd_printer_t
*printer
; /* Printer */
6858 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6859 "get_subscriptions(con=%p[%d], uri=%s)",
6860 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
6863 * Is the destination valid?
6866 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6867 sizeof(scheme
), username
, sizeof(username
), host
,
6868 sizeof(host
), &port
, resource
, sizeof(resource
));
6870 if (!strcmp(resource
, "/") ||
6871 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
6872 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
6873 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
6878 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
6881 job
= cupsdFindJob(atoi(resource
+ 6));
6885 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
6890 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6896 send_ipp_status(con
, IPP_NOT_FOUND
,
6897 _("The printer or class was not found."));
6900 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
6901 IPP_TAG_INTEGER
)) != NULL
)
6903 job
= cupsdFindJob(attr
->values
[0].integer
);
6907 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
6908 attr
->values
[0].integer
);
6919 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
6921 con
, NULL
)) != HTTP_OK
)
6923 send_http_error(con
, status
, printer
);
6928 * Copy the subscription attributes to the response using the
6929 * requested-attributes attribute that may be provided by the client.
6932 ra
= create_requested_array(con
->request
);
6934 if ((attr
= ippFindAttribute(con
->request
, "limit",
6935 IPP_TAG_INTEGER
)) != NULL
)
6936 limit
= attr
->values
[0].integer
;
6941 * See if we only want to see subscriptions for a specific user...
6944 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
6945 IPP_TAG_BOOLEAN
)) != NULL
&&
6946 attr
->values
[0].boolean
)
6947 strlcpy(username
, get_username(con
), sizeof(username
));
6951 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
6953 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
6954 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
6955 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
6957 ippAddSeparator(con
->response
);
6958 copy_subscription_attrs(con
, sub
, ra
);
6961 if (limit
&& count
>= limit
)
6965 cupsArrayDelete(ra
);
6968 con
->response
->request
.status
.status_code
= IPP_OK
;
6970 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
6975 * 'get_username()' - Get the username associated with a request.
6978 static const char * /* O - Username */
6979 get_username(cupsd_client_t
*con
) /* I - Connection */
6981 ipp_attribute_t
*attr
; /* Attribute */
6984 if (con
->username
[0])
6985 return (con
->username
);
6986 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6987 IPP_TAG_NAME
)) != NULL
)
6988 return (attr
->values
[0].string
.text
);
6990 return ("anonymous");
6995 * 'hold_job()' - Hold a print job.
6999 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7000 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7002 ipp_attribute_t
*attr
, /* Current job-hold-until */
7003 *newattr
; /* New job-hold-until */
7004 int jobid
; /* Job ID */
7005 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7006 username
[HTTP_MAX_URI
], /* Username portion of URI */
7007 host
[HTTP_MAX_URI
], /* Host portion of URI */
7008 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7009 int port
; /* Port portion of URI */
7010 cupsd_job_t
*job
; /* Job information */
7013 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
7014 uri
->values
[0].string
.text
);
7017 * See if we have a job URI or a printer URI...
7020 if (!strcmp(uri
->name
, "printer-uri"))
7023 * Got a printer URI; see if we also have a job-id attribute...
7026 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7027 IPP_TAG_INTEGER
)) == NULL
)
7029 send_ipp_status(con
, IPP_BAD_REQUEST
,
7030 _("Got a printer-uri attribute but no job-id!"));
7034 jobid
= attr
->values
[0].integer
;
7039 * Got a job URI; parse it to get the job ID...
7042 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7043 sizeof(method
), username
, sizeof(username
), host
,
7044 sizeof(host
), &port
, resource
, sizeof(resource
));
7046 if (strncmp(resource
, "/jobs/", 6))
7052 send_ipp_status(con
, IPP_BAD_REQUEST
,
7053 _("Bad job-uri attribute \"%s\"!"),
7054 uri
->values
[0].string
.text
);
7058 jobid
= atoi(resource
+ 6);
7062 * See if the job exists...
7065 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7068 * Nope - return a "not found" error...
7071 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7076 * See if the job is owned by the requesting user...
7079 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7081 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7086 * Hold the job and return...
7091 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
7092 "Job held by user.");
7094 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
7095 IPP_TAG_KEYWORD
)) == NULL
)
7096 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7098 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7099 IPP_TAG_KEYWORD
)) == NULL
)
7100 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7105 * Free the old hold value and copy the new one over...
7108 _cupsStrFree(attr
->values
[0].string
.text
);
7112 attr
->value_tag
= newattr
->value_tag
;
7113 attr
->values
[0].string
.text
=
7114 _cupsStrAlloc(newattr
->values
[0].string
.text
);
7118 attr
->value_tag
= IPP_TAG_KEYWORD
;
7119 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
7123 * Hold job until specified time...
7126 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
7128 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
7129 "Job job-hold-until value changed by user.");
7132 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Held by \"%s\".", jobid
,
7135 con
->response
->request
.status
.status_code
= IPP_OK
;
7140 * 'move_job()' - Move a job to a new destination.
7144 move_job(cupsd_client_t
*con
, /* I - Client connection */
7145 ipp_attribute_t
*uri
) /* I - Job URI */
7147 http_status_t status
; /* Policy status */
7148 ipp_attribute_t
*attr
; /* Current attribute */
7149 int jobid
; /* Job ID */
7150 cupsd_job_t
*job
; /* Current job */
7151 const char *src
; /* Source printer/class */
7152 cups_ptype_t stype
, /* Source type (printer or class) */
7153 dtype
; /* Destination type (printer/class) */
7154 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7155 username
[HTTP_MAX_URI
], /* Username portion of URI */
7156 host
[HTTP_MAX_URI
], /* Host portion of URI */
7157 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7158 int port
; /* Port portion of URI */
7159 cupsd_printer_t
*sprinter
, /* Source printer */
7160 *dprinter
; /* Destination printer */
7163 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
7164 uri
->values
[0].string
.text
);
7167 * Get the new printer or class...
7170 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7171 IPP_TAG_URI
)) == NULL
)
7174 * Need job-printer-uri...
7177 send_ipp_status(con
, IPP_BAD_REQUEST
,
7178 _("job-printer-uri attribute missing!"));
7182 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7188 send_ipp_status(con
, IPP_NOT_FOUND
,
7189 _("The printer or class was not found."));
7197 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
7200 send_http_error(con
, status
, dprinter
);
7205 * See if we have a job URI or a printer URI...
7208 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7209 sizeof(scheme
), username
, sizeof(username
), host
,
7210 sizeof(host
), &port
, resource
, sizeof(resource
));
7212 if (!strcmp(uri
->name
, "printer-uri"))
7215 * Got a printer URI; see if we also have a job-id attribute...
7218 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7219 IPP_TAG_INTEGER
)) == NULL
)
7225 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
7226 &sprinter
)) == NULL
)
7232 send_ipp_status(con
, IPP_NOT_FOUND
,
7233 _("The printer or class was not found."));
7242 * Otherwise, just move a single job...
7245 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7248 * Nope - return a "not found" error...
7251 send_ipp_status(con
, IPP_NOT_FOUND
,
7252 _("Job #%d does not exist!"), attr
->values
[0].integer
);
7258 * Job found, initialize source pointers...
7269 * Got a job URI; parse it to get the job ID...
7272 if (strncmp(resource
, "/jobs/", 6))
7278 send_ipp_status(con
, IPP_BAD_REQUEST
,
7279 _("Bad job-uri attribute \"%s\"!"),
7280 uri
->values
[0].string
.text
);
7285 * See if the job exists...
7288 jobid
= atoi(resource
+ 6);
7290 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7293 * Nope - return a "not found" error...
7296 send_ipp_status(con
, IPP_NOT_FOUND
,
7297 _("Job #%d does not exist!"), jobid
);
7303 * Job found, initialize source pointers...
7312 * Now move the job or jobs...
7318 * See if the job has been completed...
7321 if (job
->state_value
> IPP_JOB_STOPPED
)
7324 * Return a "not-possible" error...
7327 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7328 _("Job #%d is finished and cannot be altered!"),
7334 * See if the job is owned by the requesting user...
7337 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7339 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7344 * Move the job to a different printer or class...
7347 cupsdMoveJob(job
, dprinter
);
7352 * Got the source printer, now look through the jobs...
7355 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
7357 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
7360 * See if the job is pointing at the source printer or has not been
7364 if (strcasecmp(job
->dest
, src
) ||
7365 job
->state_value
> IPP_JOB_STOPPED
)
7369 * See if the job can be moved by the requesting user...
7372 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7376 * Move the job to a different printer or class...
7379 cupsdMoveJob(job
, dprinter
);
7384 * Start jobs if possible...
7390 * Return with "everything is OK" status...
7393 con
->response
->request
.status
.status_code
= IPP_OK
;
7398 * 'ppd_parse_line()' - Parse a PPD default line.
7401 static int /* O - 0 on success, -1 on failure */
7402 ppd_parse_line(const char *line
, /* I - Line */
7403 char *option
, /* O - Option name */
7404 int olen
, /* I - Size of option name */
7405 char *choice
, /* O - Choice name */
7406 int clen
) /* I - Size of choice name */
7409 * Verify this is a default option line...
7412 if (strncmp(line
, "*Default", 8))
7416 * Read the option name...
7419 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
7429 * Skip everything else up to the colon (:)...
7432 while (*line
&& *line
!= ':')
7441 * Now grab the option choice, skipping leading whitespace...
7444 while (isspace(*line
& 255))
7447 for (clen
--; isalnum(*line
& 255); line
++)
7457 * Return with no errors...
7465 * 'print_job()' - Print a file to a printer or class.
7469 print_job(cupsd_client_t
*con
, /* I - Client connection */
7470 ipp_attribute_t
*uri
) /* I - Printer URI */
7472 ipp_attribute_t
*attr
; /* Current attribute */
7473 ipp_attribute_t
*format
; /* Document-format attribute */
7474 const char *default_format
; /* document-format-default value */
7475 cupsd_job_t
*job
; /* New job */
7476 char filename
[1024]; /* Job filename */
7477 mime_type_t
*filetype
; /* Type of file */
7478 char super
[MIME_MAX_SUPER
], /* Supertype of file */
7479 type
[MIME_MAX_TYPE
], /* Subtype of file */
7480 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7481 /* Textual name of mime type */
7482 cupsd_printer_t
*printer
; /* Printer data */
7483 struct stat fileinfo
; /* File information */
7484 int kbytes
; /* Size of file */
7485 int compression
; /* Document compression */
7488 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
7489 uri
->values
[0].string
.text
);
7492 * Validate print file attributes, for now just document-format and
7493 * compression (CUPS only supports "none" and "gzip")...
7496 compression
= CUPS_FILE_NONE
;
7498 if ((attr
= ippFindAttribute(con
->request
, "compression",
7499 IPP_TAG_KEYWORD
)) != NULL
)
7501 if (strcmp(attr
->values
[0].string
.text
, "none")
7503 && strcmp(attr
->values
[0].string
.text
, "gzip")
7504 #endif /* HAVE_LIBZ */
7507 send_ipp_status(con
, IPP_ATTRIBUTES
,
7508 _("Unsupported compression \"%s\"!"),
7509 attr
->values
[0].string
.text
);
7510 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7511 "compression", NULL
, attr
->values
[0].string
.text
);
7516 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7517 compression
= CUPS_FILE_GZIP
;
7518 #endif /* HAVE_LIBZ */
7522 * Do we have a file to print?
7527 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7532 * Is the destination valid?
7535 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
7541 send_ipp_status(con
, IPP_NOT_FOUND
,
7542 _("The printer or class was not found."));
7547 * Is it a format we support?
7550 if ((format
= ippFindAttribute(con
->request
, "document-format",
7551 IPP_TAG_MIMETYPE
)) != NULL
)
7554 * Grab format from client...
7557 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
7560 send_ipp_status(con
, IPP_BAD_REQUEST
,
7561 _("Could not scan type \"%s\"!"),
7562 format
->values
[0].string
.text
);
7566 else if ((default_format
= cupsGetOption("document-format",
7567 printer
->num_options
,
7568 printer
->options
)) != NULL
)
7571 * Use default document format...
7574 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
7576 send_ipp_status(con
, IPP_BAD_REQUEST
,
7577 _("Could not scan type \"%s\"!"),
7588 strcpy(super
, "application");
7589 strcpy(type
, "octet-stream");
7592 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7595 * Auto-type the file...
7598 ipp_attribute_t
*doc_name
; /* document-name attribute */
7601 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
7603 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7604 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7605 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7609 filetype
= mimeType(MimeDatabase
, super
, type
);
7612 filetype
= mimeType(MimeDatabase
, super
, type
);
7616 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
7619 * Replace the document-format attribute value with the auto-typed or
7623 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7628 _cupsStrFree(format
->values
[0].string
.text
);
7630 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7633 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7634 "document-format", NULL
, mimetype
);
7638 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7639 _("Unsupported format \'%s/%s\'!"), super
, type
);
7640 cupsdLogMessage(CUPSD_LOG_INFO
,
7641 "Hint: Do you have the raw file printing rules enabled?");
7644 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7645 "document-format", NULL
, format
->values
[0].string
.text
);
7651 * Read any embedded job ticket info from PS files...
7654 if (!strcasecmp(filetype
->super
, "application") &&
7655 !strcasecmp(filetype
->type
, "postscript"))
7656 read_ps_job_ticket(con
);
7659 * Create the job object...
7662 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
7665 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Adding job file of type %s/%s.",
7666 job
->id
, filetype
->super
, filetype
->type
);
7669 * Update quota data...
7672 if (stat(con
->filename
, &fileinfo
))
7675 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7677 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7679 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7680 IPP_TAG_INTEGER
)) != NULL
)
7681 attr
->values
[0].integer
+= kbytes
;
7684 * Add the job file...
7687 if (add_file(con
, job
, filetype
, compression
))
7690 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
7692 rename(con
->filename
, filename
);
7693 cupsdClearString(&con
->filename
);
7696 * See if we need to add the ending sheet...
7699 cupsdTimeoutJob(job
);
7702 * Log and save the job...
7705 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
7706 job
->id
, job
->dest
, job
->username
);
7707 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] hold_until = %d", job
->id
,
7708 (int)job
->hold_until
);
7713 * Start the job if possible...
7721 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
7723 * This function only gets called when printing a single PostScript
7724 * file using the Print-Job operation. It doesn't work for Create-Job +
7725 * Send-File, since the job attributes need to be set at job creation
7726 * time for banners to work. The embedded PS job ticket stuff is here
7727 * only to allow the Windows printer driver for CUPS to pass in JCL
7728 * options and IPP attributes which otherwise would be lost.
7730 * The format of a PS job ticket is simple:
7732 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
7734 * %cupsJobTicket: attr1=value1
7735 * %cupsJobTicket: attr2=value2
7737 * %cupsJobTicket: attrN=valueN
7739 * Job ticket lines must appear immediately after the first line that
7740 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
7741 * looking for job ticket info when it finds a line that does not begin
7742 * with "%cupsJobTicket:".
7744 * The maximum length of a job ticket line, including the prefix, is
7745 * 255 characters to conform with the Adobe DSC.
7747 * Read-only attributes are rejected with a notice to the error log in
7748 * case a malicious user tries anything. Since the job ticket is read
7749 * prior to attribute validation in print_job(), job ticket attributes
7750 * will go through the same validation as IPP attributes...
7754 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
7756 cups_file_t
*fp
; /* File to read from */
7757 char line
[256]; /* Line data */
7758 int num_options
; /* Number of options */
7759 cups_option_t
*options
; /* Options */
7760 ipp_t
*ticket
; /* New attributes */
7761 ipp_attribute_t
*attr
, /* Current attribute */
7762 *attr2
, /* Job attribute */
7763 *prev2
; /* Previous job attribute */
7767 * First open the print file...
7770 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
7772 cupsdLogMessage(CUPSD_LOG_ERROR
,
7773 "read_ps_job_ticket: Unable to open PostScript print file "
7780 * Skip the first line...
7783 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
7785 cupsdLogMessage(CUPSD_LOG_ERROR
,
7786 "read_ps_job_ticket: Unable to read from PostScript print "
7793 if (strncmp(line
, "%!PS-Adobe-", 11))
7796 * Not a DSC-compliant file, so no job ticket info will be available...
7804 * Read job ticket info from the file...
7810 while (cupsFileGets(fp
, line
, sizeof(line
)))
7813 * Stop at the first non-ticket line...
7816 if (strncmp(line
, "%cupsJobTicket:", 15))
7820 * Add the options to the option array...
7823 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
7827 * Done with the file; see if we have any options...
7832 if (num_options
== 0)
7836 * OK, convert the options to an attribute list, and apply them to
7841 cupsEncodeOptions(ticket
, num_options
, options
);
7844 * See what the user wants to change.
7847 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
7849 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
7852 if (!strcmp(attr
->name
, "job-originating-host-name") ||
7853 !strcmp(attr
->name
, "job-originating-user-name") ||
7854 !strcmp(attr
->name
, "job-media-sheets-completed") ||
7855 !strcmp(attr
->name
, "job-k-octets") ||
7856 !strcmp(attr
->name
, "job-id") ||
7857 !strncmp(attr
->name
, "job-state", 9) ||
7858 !strncmp(attr
->name
, "time-at-", 8))
7859 continue; /* Read-only attrs */
7861 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
7862 IPP_TAG_ZERO
)) != NULL
)
7865 * Some other value; first free the old value...
7868 if (con
->request
->attrs
== attr2
)
7870 con
->request
->attrs
= attr2
->next
;
7875 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
7876 if (prev2
->next
== attr2
)
7878 prev2
->next
= attr2
->next
;
7883 if (con
->request
->last
== attr2
)
7884 con
->request
->last
= prev2
;
7886 _ippFreeAttr(attr2
);
7890 * Add new option by copying it...
7893 copy_attribute(con
->request
, attr
, 0);
7897 * Then free the attribute list and option array...
7901 cupsFreeOptions(num_options
, options
);
7906 * 'reject_jobs()' - Reject print jobs to a printer.
7910 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
7911 ipp_attribute_t
*uri
) /* I - Printer or class URI */
7913 http_status_t status
; /* Policy status */
7914 cups_ptype_t dtype
; /* Destination type (printer/class) */
7915 cupsd_printer_t
*printer
; /* Printer data */
7916 ipp_attribute_t
*attr
; /* printer-state-message text */
7919 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
7920 con
->http
.fd
, uri
->values
[0].string
.text
);
7923 * Is the destination valid?
7926 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7932 send_ipp_status(con
, IPP_NOT_FOUND
,
7933 _("The printer or class was not found."));
7941 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7943 send_http_error(con
, status
, printer
);
7948 * Reject jobs sent to the printer...
7951 printer
->accepting
= 0;
7953 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
7954 IPP_TAG_TEXT
)) == NULL
)
7955 strcpy(printer
->state_message
, "Rejecting Jobs");
7957 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
7958 sizeof(printer
->state_message
));
7960 cupsdAddPrinterHistory(printer
);
7962 if (dtype
& CUPS_PRINTER_CLASS
)
7964 cupsdSaveAllClasses();
7966 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
7967 printer
->name
, get_username(con
));
7971 cupsdSaveAllPrinters();
7973 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
7974 printer
->name
, get_username(con
));
7978 * Everything was ok, so return OK status...
7981 con
->response
->request
.status
.status_code
= IPP_OK
;
7986 * 'release_job()' - Release a held print job.
7990 release_job(cupsd_client_t
*con
, /* I - Client connection */
7991 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7993 ipp_attribute_t
*attr
; /* Current attribute */
7994 int jobid
; /* Job ID */
7995 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7996 username
[HTTP_MAX_URI
], /* Username portion of URI */
7997 host
[HTTP_MAX_URI
], /* Host portion of URI */
7998 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7999 int port
; /* Port portion of URI */
8000 cupsd_job_t
*job
; /* Job information */
8003 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8004 con
->http
.fd
, uri
->values
[0].string
.text
);
8007 * See if we have a job URI or a printer URI...
8010 if (!strcmp(uri
->name
, "printer-uri"))
8013 * Got a printer URI; see if we also have a job-id attribute...
8016 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8017 IPP_TAG_INTEGER
)) == NULL
)
8019 send_ipp_status(con
, IPP_BAD_REQUEST
,
8020 _("Got a printer-uri attribute but no job-id!"));
8024 jobid
= attr
->values
[0].integer
;
8029 * Got a job URI; parse it to get the job ID...
8032 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8033 sizeof(method
), username
, sizeof(username
), host
,
8034 sizeof(host
), &port
, resource
, sizeof(resource
));
8036 if (strncmp(resource
, "/jobs/", 6))
8042 send_ipp_status(con
, IPP_BAD_REQUEST
,
8043 _("Bad job-uri attribute \"%s\"!"),
8044 uri
->values
[0].string
.text
);
8048 jobid
= atoi(resource
+ 6);
8052 * See if the job exists...
8055 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8058 * Nope - return a "not found" error...
8061 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8066 * See if job is "held"...
8069 if (job
->state_value
!= IPP_JOB_HELD
)
8072 * Nope - return a "not possible" error...
8075 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
8080 * See if the job is owned by the requesting user...
8083 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8085 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8090 * Reset the job-hold-until value to "no-hold"...
8093 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8094 IPP_TAG_KEYWORD
)) == NULL
)
8095 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8099 _cupsStrFree(attr
->values
[0].string
.text
);
8101 attr
->value_tag
= IPP_TAG_KEYWORD
;
8102 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
8104 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
8105 "Job job-hold-until value changed by user.");
8109 * Release the job and return...
8112 cupsdReleaseJob(job
);
8114 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
8115 "Job released by user.");
8117 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Released by \"%s\".", jobid
,
8120 con
->response
->request
.status
.status_code
= IPP_OK
;
8125 * 'renew_subscription()' - Renew an existing subscription...
8130 cupsd_client_t
*con
, /* I - Client connection */
8131 int sub_id
) /* I - Subscription ID */
8133 http_status_t status
; /* Policy status */
8134 cupsd_subscription_t
*sub
; /* Subscription */
8135 ipp_attribute_t
*lease
; /* notify-lease-duration */
8138 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8139 "renew_subscription(con=%p[%d], sub_id=%d)",
8140 con
, con
->http
.fd
, sub_id
);
8143 * Is the subscription ID valid?
8146 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8149 * Bad subscription ID...
8152 send_ipp_status(con
, IPP_NOT_FOUND
,
8153 _("notify-subscription-id %d no good!"), sub_id
);
8160 * Job subscriptions cannot be renewed...
8163 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8164 _("Job subscriptions cannot be renewed!"));
8172 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8174 con
, sub
->owner
)) != HTTP_OK
)
8176 send_http_error(con
, status
, sub
->dest
);
8181 * Renew the subscription...
8184 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8187 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8189 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8191 cupsdLogMessage(CUPSD_LOG_INFO
,
8192 "renew_subscription: Limiting notify-lease-duration to "
8195 sub
->lease
= MaxLeaseDuration
;
8198 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
8200 cupsdSaveAllSubscriptions();
8202 con
->response
->request
.status
.status_code
= IPP_OK
;
8204 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
8205 "notify-lease-duration", sub
->lease
);
8210 * 'restart_job()' - Restart an old print job.
8214 restart_job(cupsd_client_t
*con
, /* I - Client connection */
8215 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8217 ipp_attribute_t
*attr
; /* Current attribute */
8218 int jobid
; /* Job ID */
8219 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8220 username
[HTTP_MAX_URI
], /* Username portion of URI */
8221 host
[HTTP_MAX_URI
], /* Host portion of URI */
8222 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8223 int port
; /* Port portion of URI */
8224 cupsd_job_t
*job
; /* Job information */
8227 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
8228 con
->http
.fd
, uri
->values
[0].string
.text
);
8231 * See if we have a job URI or a printer URI...
8234 if (!strcmp(uri
->name
, "printer-uri"))
8237 * Got a printer URI; see if we also have a job-id attribute...
8240 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8241 IPP_TAG_INTEGER
)) == NULL
)
8243 send_ipp_status(con
, IPP_BAD_REQUEST
,
8244 _("Got a printer-uri attribute but no job-id!"));
8248 jobid
= attr
->values
[0].integer
;
8253 * Got a job URI; parse it to get the job ID...
8256 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8257 sizeof(method
), username
, sizeof(username
), host
,
8258 sizeof(host
), &port
, resource
, sizeof(resource
));
8260 if (strncmp(resource
, "/jobs/", 6))
8266 send_ipp_status(con
, IPP_BAD_REQUEST
,
8267 _("Bad job-uri attribute \"%s\"!"),
8268 uri
->values
[0].string
.text
);
8272 jobid
= atoi(resource
+ 6);
8276 * See if the job exists...
8279 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8282 * Nope - return a "not found" error...
8285 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8290 * See if job is in any of the "completed" states...
8293 if (job
->state_value
<= IPP_JOB_PROCESSING
)
8296 * Nope - return a "not possible" error...
8299 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
8305 * See if we have retained the job files...
8310 if (!job
->attrs
|| job
->num_files
== 0)
8313 * Nope - return a "not possible" error...
8316 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8317 _("Job #%d cannot be restarted - no files!"), jobid
);
8322 * See if the job is owned by the requesting user...
8325 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8327 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8332 * Restart the job and return...
8335 cupsdRestartJob(job
);
8337 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Restarted by \"%s\".", jobid
,
8340 con
->response
->request
.status
.status_code
= IPP_OK
;
8345 * 'save_auth_info()' - Save authentication information for a job.
8350 cupsd_client_t
*con
, /* I - Client connection */
8351 cupsd_job_t
*job
, /* I - Job */
8352 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
8354 int i
; /* Looping var */
8355 char filename
[1024]; /* Job authentication filename */
8356 cups_file_t
*fp
; /* Job authentication file */
8357 char line
[2048]; /* Line for file */
8358 cupsd_printer_t
*dest
; /* Destination printer/class */
8362 * This function saves the in-memory authentication information for
8363 * a job so that it can be used to authenticate with a remote host.
8364 * The information is stored in a file that is readable only by the
8365 * root user. The fields are Base-64 encoded, each on a separate line,
8366 * followed by random number (up to 1024) of newlines to limit the
8367 * amount of information that is exposed.
8369 * Because of the potential for exposing of authentication information,
8370 * this functionality is only enabled when running cupsd as root.
8372 * This caching only works for the Basic and BasicDigest authentication
8373 * types. Digest authentication cannot be cached this way, and in
8374 * the future Kerberos authentication may make all of this obsolete.
8376 * Authentication information is saved whenever an authenticated
8377 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
8380 * This information is deleted after a job is completed or canceled,
8381 * so reprints may require subsequent re-authentication.
8387 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
8391 * Create the authentication file and change permissions...
8394 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
8395 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
8397 cupsdLogMessage(CUPSD_LOG_ERROR
,
8398 "Unable to save authentication info to \"%s\" - %s",
8399 filename
, strerror(errno
));
8403 fchown(cupsFileNumber(fp
), 0, 0);
8404 fchmod(cupsFileNumber(fp
), 0400);
8406 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
8409 * Write 1 to 3 auth values...
8412 cupsdClearString(&job
->auth_username
);
8413 cupsdClearString(&job
->auth_domain
);
8414 cupsdClearString(&job
->auth_password
);
8416 for (i
= 0; i
< auth_info
->num_values
; i
++)
8418 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
8419 strlen(auth_info
->values
[i
].string
.text
));
8420 cupsFilePrintf(fp
, "%s\n", line
);
8422 if (!strcmp(dest
->auth_info_required
[i
], "username"))
8423 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s",
8424 auth_info
->values
[i
].string
.text
);
8425 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
8426 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s",
8427 auth_info
->values
[i
].string
.text
);
8428 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
8429 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s",
8430 auth_info
->values
[i
].string
.text
);
8433 else if (con
->username
[0])
8436 * Write the authenticated username...
8439 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
8440 cupsFilePrintf(fp
, "%s\n", line
);
8442 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", con
->username
);
8443 cupsdClearString(&job
->auth_domain
);
8446 * Write the authenticated password...
8449 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
8450 cupsFilePrintf(fp
, "%s\n", line
);
8452 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", con
->password
);
8456 * Write a random number of newlines to the end of the file...
8459 for (i
= (rand() % 1024); i
>= 0; i
--)
8460 cupsFilePutChar(fp
, '\n');
8463 * Close the file and return...
8468 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8469 if (con
->gss_have_creds
)
8470 save_krb5_creds(con
, job
);
8471 else if (job
->ccname
)
8472 cupsdClearString(&(job
->ccname
));
8473 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8477 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8479 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
8483 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
8484 cupsd_job_t
*job
) /* I - Job */
8486 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
8487 cupsdLogMessage(CUPSD_LOG_INFO
,
8488 "Sorry, your version of Kerberos does not support delegated "
8493 krb5_error_code error
; /* Kerberos error code */
8494 OM_uint32 major_status
, /* Major status code */
8495 minor_status
; /* Minor status code */
8496 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
8497 krb5_principal principal
; /* Kerberos principal */
8498 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
8503 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
8507 if (krb5_init_context
== NULL
)
8509 # endif /* __APPLE__ */
8512 * We MUST create a file-based cache because memory-based caches are
8513 * only valid for the current process/address space.
8515 * Due to various bugs/features in different versions of Kerberos, we
8516 * need either the krb5_cc_new_unique() function or Heimdal's version
8517 * of krb5_cc_gen_new() to create a new FILE: credential cache that
8518 * can be passed to the backend. These functions create a temporary
8519 * file (typically in /tmp) containing the cached credentials, which
8520 * are removed when we have successfully printed a job.
8523 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
8524 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
8525 &(job
->ccache
))) != 0)
8526 # else /* HAVE_HEIMDAL */
8527 if ((error
= krb5_cc_gen_new(krb_context
, &krb5_fcc_ops
,
8528 &(job
->ccache
))) != 0)
8529 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
8531 cupsdLogMessage(CUPSD_LOG_ERROR
,
8532 "Unable to create new credentials cache (%d/%s)",
8533 error
, strerror(errno
));
8538 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
8540 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to parse kerberos username (%d/%s)",
8541 error
, strerror(errno
));
8542 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8547 if ((error
= krb5_cc_initialize(KerberosContext
, job
->ccache
, principal
)))
8549 cupsdLogMessage(CUPSD_LOG_ERROR
,
8550 "Unable to initialize credentials cache (%d/%s)", error
,
8552 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8553 krb5_free_principal(KerberosContext
, principal
);
8558 krb5_free_principal(KerberosContext
, principal
);
8561 * Copy the user's credentials to the new cache file...
8564 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
8567 if (GSS_ERROR(major_status
))
8569 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
8570 "Unable to import client credentials cache");
8571 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8577 * Add the KRB5CCNAME environment variable to the job so that the
8578 * backend can use the credentials when printing.
8581 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
8582 krb5_cc_get_name(KerberosContext
, job
->ccache
));
8584 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] save_krb5_creds: %s", job
->id
,
8586 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
8588 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8592 * 'send_document()' - Send a file to a printer or class.
8596 send_document(cupsd_client_t
*con
, /* I - Client connection */
8597 ipp_attribute_t
*uri
) /* I - Printer URI */
8599 ipp_attribute_t
*attr
; /* Current attribute */
8600 ipp_attribute_t
*format
; /* Document-format attribute */
8601 const char *default_format
;/* document-format-default value */
8602 int jobid
; /* Job ID number */
8603 cupsd_job_t
*job
; /* Current job */
8604 char job_uri
[HTTP_MAX_URI
],
8606 method
[HTTP_MAX_URI
],
8607 /* Method portion of URI */
8608 username
[HTTP_MAX_URI
],
8609 /* Username portion of URI */
8611 /* Host portion of URI */
8612 resource
[HTTP_MAX_URI
];
8613 /* Resource portion of URI */
8614 int port
; /* Port portion of URI */
8615 mime_type_t
*filetype
; /* Type of file */
8616 char super
[MIME_MAX_SUPER
],
8617 /* Supertype of file */
8618 type
[MIME_MAX_TYPE
],
8619 /* Subtype of file */
8620 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8621 /* Textual name of mime type */
8622 char filename
[1024]; /* Job filename */
8623 cupsd_printer_t
*printer
; /* Current printer */
8624 struct stat fileinfo
; /* File information */
8625 int kbytes
; /* Size of file */
8626 int compression
; /* Type of compression */
8629 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
8630 con
->http
.fd
, uri
->values
[0].string
.text
);
8633 * See if we have a job URI or a printer URI...
8636 if (!strcmp(uri
->name
, "printer-uri"))
8639 * Got a printer URI; see if we also have a job-id attribute...
8642 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8643 IPP_TAG_INTEGER
)) == NULL
)
8645 send_ipp_status(con
, IPP_BAD_REQUEST
,
8646 _("Got a printer-uri attribute but no job-id!"));
8650 jobid
= attr
->values
[0].integer
;
8655 * Got a job URI; parse it to get the job ID...
8658 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8659 sizeof(method
), username
, sizeof(username
), host
,
8660 sizeof(host
), &port
, resource
, sizeof(resource
));
8662 if (strncmp(resource
, "/jobs/", 6))
8668 send_ipp_status(con
, IPP_BAD_REQUEST
,
8669 _("Bad job-uri attribute \"%s\"!"),
8670 uri
->values
[0].string
.text
);
8674 jobid
= atoi(resource
+ 6);
8678 * See if the job exists...
8681 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8684 * Nope - return a "not found" error...
8687 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8691 printer
= cupsdFindDest(job
->dest
);
8694 * See if the job is owned by the requesting user...
8697 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8699 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8704 * OK, see if the client is sending the document compressed - CUPS
8705 * only supports "none" and "gzip".
8708 compression
= CUPS_FILE_NONE
;
8710 if ((attr
= ippFindAttribute(con
->request
, "compression",
8711 IPP_TAG_KEYWORD
)) != NULL
)
8713 if (strcmp(attr
->values
[0].string
.text
, "none")
8715 && strcmp(attr
->values
[0].string
.text
, "gzip")
8716 #endif /* HAVE_LIBZ */
8719 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
8720 attr
->values
[0].string
.text
);
8721 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8722 "compression", NULL
, attr
->values
[0].string
.text
);
8727 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8728 compression
= CUPS_FILE_GZIP
;
8729 #endif /* HAVE_LIBZ */
8733 * Do we have a file to print?
8738 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
8743 * Is it a format we support?
8746 if ((format
= ippFindAttribute(con
->request
, "document-format",
8747 IPP_TAG_MIMETYPE
)) != NULL
)
8750 * Grab format from client...
8753 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
8756 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
8757 format
->values
[0].string
.text
);
8761 else if ((default_format
= cupsGetOption("document-format",
8762 printer
->num_options
,
8763 printer
->options
)) != NULL
)
8766 * Use default document format...
8769 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
8771 send_ipp_status(con
, IPP_BAD_REQUEST
,
8772 _("Could not scan type \"%s\"!"),
8780 * No document format attribute? Auto-type it!
8783 strcpy(super
, "application");
8784 strcpy(type
, "octet-stream");
8787 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8790 * Auto-type the file...
8793 ipp_attribute_t
*doc_name
; /* document-name attribute */
8796 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
8798 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8799 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8800 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8804 filetype
= mimeType(MimeDatabase
, super
, type
);
8807 filetype
= mimeType(MimeDatabase
, super
, type
);
8811 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8814 * Replace the document-format attribute value with the auto-typed or
8818 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8823 _cupsStrFree(format
->values
[0].string
.text
);
8825 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8828 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8829 "document-format", NULL
, mimetype
);
8833 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8834 _("Unsupported format \'%s/%s\'!"), super
, type
);
8835 cupsdLogMessage(CUPSD_LOG_INFO
,
8836 "Hint: Do you have the raw file printing rules enabled?");
8839 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8840 "document-format", NULL
, format
->values
[0].string
.text
);
8845 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
8847 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8850 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8851 _("Unsupported format \'%s\'!"), mimetype
);
8853 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8854 "document-format", NULL
, mimetype
);
8859 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8860 "send_document: request file type is %s/%s.",
8861 filetype
->super
, filetype
->type
);
8864 * Add the file to the job...
8869 if (add_file(con
, job
, filetype
, compression
))
8872 if (stat(con
->filename
, &fileinfo
))
8875 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8877 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8879 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8880 IPP_TAG_INTEGER
)) != NULL
)
8881 attr
->values
[0].integer
+= kbytes
;
8883 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8885 rename(con
->filename
, filename
);
8887 cupsdClearString(&con
->filename
);
8889 cupsdLogMessage(CUPSD_LOG_INFO
,
8890 "File of type %s/%s queued in job #%d by \"%s\".",
8891 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
8894 * Start the job if this is the last document...
8897 if ((attr
= ippFindAttribute(con
->request
, "last-document",
8898 IPP_TAG_BOOLEAN
)) != NULL
&&
8899 attr
->values
[0].boolean
)
8902 * See if we need to add the ending sheet...
8905 cupsdTimeoutJob(job
);
8907 if (job
->state_value
== IPP_JOB_STOPPED
)
8909 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
8910 job
->state_value
= IPP_JOB_PENDING
;
8912 else if (job
->state_value
== IPP_JOB_HELD
)
8914 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8915 IPP_TAG_KEYWORD
)) == NULL
)
8916 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8918 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
8920 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
8921 job
->state_value
= IPP_JOB_PENDING
;
8928 * Start the job if possible... Since cupsdCheckJobs() can cancel a
8929 * job if it doesn't print, we need to re-find the job afterwards...
8936 job
= cupsdFindJob(jobid
);
8940 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8941 IPP_TAG_KEYWORD
)) == NULL
)
8942 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8944 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
8946 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
8947 job
->state_value
= IPP_JOB_HELD
;
8948 job
->hold_until
= time(NULL
) + 60;
8954 * Fill in the response info...
8957 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
8960 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
8963 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
8965 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
8966 job
? job
->state_value
: IPP_JOB_CANCELED
);
8967 add_job_state_reasons(con
, job
);
8969 con
->response
->request
.status
.status_code
= IPP_OK
;
8974 * 'send_http_error()' - Send a HTTP error back to the IPP client.
8979 cupsd_client_t
*con
, /* I - Client connection */
8980 http_status_t status
, /* I - HTTP status code */
8981 cupsd_printer_t
*printer
) /* I - Printer, if any */
8983 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
8984 ippOpString(con
->request
->request
.op
.operation_id
),
8985 httpStatus(status
));
8987 if (status
== HTTP_UNAUTHORIZED
&&
8988 printer
&& printer
->num_auth_info_required
> 0 &&
8989 !strcmp(printer
->auth_info_required
[0], "negotiate"))
8990 cupsdSendError(con
, status
, AUTH_NEGOTIATE
);
8993 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8994 cupsd_location_t
*auth
; /* Pointer to authentication element */
8997 if (printer
->type
& CUPS_PRINTER_CLASS
)
8998 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9000 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9002 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9003 auth
->type
== AUTH_NONE
)
9004 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
, IPP_PRINT_JOB
);
9006 cupsdSendError(con
, status
, auth
? auth
->type
: AUTH_NONE
);
9009 cupsdSendError(con
, status
, AUTH_NONE
);
9011 ippDelete(con
->response
);
9012 con
->response
= NULL
;
9019 * 'send_ipp_status()' - Send a status back to the IPP client.
9023 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9024 ipp_status_t status
, /* I - IPP status code */
9025 const char *message
, /* I - Status message */
9026 ...) /* I - Additional args as needed */
9028 va_list ap
; /* Pointer to additional args */
9029 char formatted
[1024]; /* Formatted errror message */
9032 va_start(ap
, message
);
9033 vsnprintf(formatted
, sizeof(formatted
),
9034 _cupsLangString(con
->language
, message
), ap
);
9037 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9038 ippOpString(con
->request
->request
.op
.operation_id
),
9039 ippErrorString(status
), formatted
);
9041 con
->response
->request
.status
.status_code
= status
;
9043 if (ippFindAttribute(con
->response
, "attributes-charset",
9044 IPP_TAG_ZERO
) == NULL
)
9045 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9046 "attributes-charset", NULL
, DefaultCharset
);
9048 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9049 IPP_TAG_ZERO
) == NULL
)
9050 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9051 "attributes-natural-language", NULL
, DefaultLanguage
);
9053 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9054 "status-message", NULL
, formatted
);
9059 * 'set_default()' - Set the default destination...
9063 set_default(cupsd_client_t
*con
, /* I - Client connection */
9064 ipp_attribute_t
*uri
) /* I - Printer URI */
9066 http_status_t status
; /* Policy status */
9067 cups_ptype_t dtype
; /* Destination type (printer/class) */
9068 cupsd_printer_t
*printer
; /* Printer */
9071 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9072 con
->http
.fd
, uri
->values
[0].string
.text
);
9075 * Is the destination valid?
9078 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9084 send_ipp_status(con
, IPP_NOT_FOUND
,
9085 _("The printer or class was not found."));
9093 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9095 send_http_error(con
, status
, NULL
);
9100 * Set it as the default...
9103 DefaultPrinter
= printer
;
9105 cupsdSaveAllPrinters();
9106 cupsdSaveAllClasses();
9108 cupsdWritePrintcap();
9110 cupsdLogMessage(CUPSD_LOG_INFO
,
9111 "Default destination set to \"%s\" by \"%s\".",
9112 printer
->name
, get_username(con
));
9115 * Everything was ok, so return OK status...
9118 con
->response
->request
.status
.status_code
= IPP_OK
;
9123 * 'set_job_attrs()' - Set job attributes.
9127 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9128 ipp_attribute_t
*uri
) /* I - Job URI */
9130 ipp_attribute_t
*attr
, /* Current attribute */
9131 *attr2
; /* Job attribute */
9132 int jobid
; /* Job ID */
9133 cupsd_job_t
*job
; /* Current job */
9134 char method
[HTTP_MAX_URI
],
9135 /* Method portion of URI */
9136 username
[HTTP_MAX_URI
],
9137 /* Username portion of URI */
9139 /* Host portion of URI */
9140 resource
[HTTP_MAX_URI
];
9141 /* Resource portion of URI */
9142 int port
; /* Port portion of URI */
9143 int event
; /* Events? */
9146 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
9147 con
->http
.fd
, uri
->values
[0].string
.text
);
9150 * Start with "everything is OK" status...
9153 con
->response
->request
.status
.status_code
= IPP_OK
;
9156 * See if we have a job URI or a printer URI...
9159 if (!strcmp(uri
->name
, "printer-uri"))
9162 * Got a printer URI; see if we also have a job-id attribute...
9165 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9166 IPP_TAG_INTEGER
)) == NULL
)
9168 send_ipp_status(con
, IPP_BAD_REQUEST
,
9169 _("Got a printer-uri attribute but no job-id!"));
9173 jobid
= attr
->values
[0].integer
;
9178 * Got a job URI; parse it to get the job ID...
9181 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9182 sizeof(method
), username
, sizeof(username
), host
,
9183 sizeof(host
), &port
, resource
, sizeof(resource
));
9185 if (strncmp(resource
, "/jobs/", 6))
9191 send_ipp_status(con
, IPP_BAD_REQUEST
,
9192 _("Bad job-uri attribute \"%s\"!"),
9193 uri
->values
[0].string
.text
);
9197 jobid
= atoi(resource
+ 6);
9201 * See if the job exists...
9204 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9207 * Nope - return a "not found" error...
9210 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9215 * See if the job has been completed...
9218 if (job
->state_value
> IPP_JOB_STOPPED
)
9221 * Return a "not-possible" error...
9224 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9225 _("Job #%d is finished and cannot be altered!"), jobid
);
9230 * See if the job is owned by the requesting user...
9233 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9235 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9240 * See what the user wants to change.
9247 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9249 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
9252 if (!strcmp(attr
->name
, "attributes-charset") ||
9253 !strcmp(attr
->name
, "attributes-natural-language") ||
9254 !strcmp(attr
->name
, "document-compression") ||
9255 !strcmp(attr
->name
, "document-format") ||
9256 !strcmp(attr
->name
, "job-detailed-status-messages") ||
9257 !strcmp(attr
->name
, "job-document-access-errors") ||
9258 !strcmp(attr
->name
, "job-id") ||
9259 !strcmp(attr
->name
, "job-k-octets") ||
9260 !strcmp(attr
->name
, "job-originating-host-name") ||
9261 !strcmp(attr
->name
, "job-originating-user-name") ||
9262 !strcmp(attr
->name
, "job-printer-up-time") ||
9263 !strcmp(attr
->name
, "job-printer-uri") ||
9264 !strcmp(attr
->name
, "job-sheets") ||
9265 !strcmp(attr
->name
, "job-state-message") ||
9266 !strcmp(attr
->name
, "job-state-reasons") ||
9267 !strcmp(attr
->name
, "job-uri") ||
9268 !strcmp(attr
->name
, "number-of-documents") ||
9269 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
9270 !strcmp(attr
->name
, "output-device-assigned") ||
9271 !strncmp(attr
->name
, "date-time-at-", 13) ||
9272 !strncmp(attr
->name
, "job-impressions", 15) ||
9273 !strncmp(attr
->name
, "job-k-octets", 12) ||
9274 !strncmp(attr
->name
, "job-media-sheets", 16) ||
9275 !strncmp(attr
->name
, "time-at-", 8))
9281 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
9282 _("%s cannot be changed."), attr
->name
);
9284 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9285 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9290 if (!strcmp(attr
->name
, "job-priority"))
9293 * Change the job priority...
9296 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9298 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
9300 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9301 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9303 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
9305 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9306 _("Job is completed and cannot be changed."));
9309 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9311 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
9312 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9315 else if (!strcmp(attr
->name
, "job-state"))
9318 * Change the job state...
9321 if (attr
->value_tag
!= IPP_TAG_ENUM
)
9323 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
9325 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9326 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9330 switch (attr
->values
[0].integer
)
9332 case IPP_JOB_PENDING
:
9334 if (job
->state_value
> IPP_JOB_HELD
)
9336 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9337 _("Job state cannot be changed."));
9340 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9342 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
9343 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
9345 event
|= CUPSD_EVENT_JOB_STATE
;
9349 case IPP_JOB_PROCESSING
:
9350 case IPP_JOB_STOPPED
:
9351 if (job
->state_value
!= attr
->values
[0].integer
)
9353 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9354 _("Job state cannot be changed."));
9359 case IPP_JOB_CANCELED
:
9360 case IPP_JOB_ABORTED
:
9361 case IPP_JOB_COMPLETED
:
9362 if (job
->state_value
> IPP_JOB_PROCESSING
)
9364 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9365 _("Job state cannot be changed."));
9368 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9369 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
9374 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
9376 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9377 IPP_TAG_ZERO
)) != NULL
)
9380 * Some other value; first free the old value...
9383 if (job
->attrs
->prev
)
9384 job
->attrs
->prev
->next
= attr2
->next
;
9386 job
->attrs
->attrs
= attr2
->next
;
9388 if (job
->attrs
->last
== attr2
)
9389 job
->attrs
->last
= job
->attrs
->prev
;
9391 _ippFreeAttr(attr2
);
9394 * Then copy the attribute...
9397 copy_attribute(job
->attrs
, attr
, 0);
9400 * See if the job-name or job-hold-until is being changed.
9403 if (!strcmp(attr
->name
, "job-hold-until"))
9405 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
9407 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
9408 cupsdReleaseJob(job
);
9412 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
9415 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
9418 * Delete the attribute...
9421 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9422 IPP_TAG_ZERO
)) != NULL
)
9424 if (job
->attrs
->prev
)
9425 job
->attrs
->prev
->next
= attr2
->next
;
9427 job
->attrs
->attrs
= attr2
->next
;
9429 if (attr2
== job
->attrs
->last
)
9430 job
->attrs
->last
= job
->attrs
->prev
;
9432 _ippFreeAttr(attr2
);
9434 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9440 * Add new option by copying it...
9443 copy_attribute(job
->attrs
, attr
, 0);
9445 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9456 * Send events as needed...
9459 if (event
& CUPSD_EVENT_JOB_STATE
)
9460 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
9461 job
->state_value
== IPP_JOB_HELD
?
9462 "Job held by user." : "Job restarted by user.");
9464 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
9465 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
9466 "Job options changed by user.");
9469 * Start jobs if possible...
9477 * 'set_printer_defaults()' - Set printer default options from a request.
9481 set_printer_defaults(
9482 cupsd_client_t
*con
, /* I - Client connection */
9483 cupsd_printer_t
*printer
) /* I - Printer */
9485 int i
; /* Looping var */
9486 ipp_attribute_t
*attr
; /* Current attribute */
9487 int namelen
; /* Length of attribute name */
9488 char name
[256], /* New attribute name */
9489 value
[256]; /* String version of integer attrs */
9492 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9495 * Skip non-printer attributes...
9498 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
9501 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
9503 if (!strcmp(attr
->name
, "job-sheets-default"))
9506 * Only allow keywords and names...
9509 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9513 * Only allow job-sheets-default to be set when running without a
9514 * system high classification level...
9520 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
9522 if (attr
->num_values
> 1)
9523 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
9525 cupsdSetString(&printer
->job_sheets
[1], "none");
9527 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
9529 cupsdFreePrinterUsers(printer
);
9531 printer
->deny_users
= 0;
9533 if (attr
->value_tag
== IPP_TAG_NAME
&&
9534 (attr
->num_values
> 1 ||
9535 strcmp(attr
->values
[0].string
.text
, "all")))
9537 for (i
= 0; i
< attr
->num_values
; i
++)
9538 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9541 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
9543 cupsdFreePrinterUsers(printer
);
9545 printer
->deny_users
= 1;
9547 if (attr
->value_tag
== IPP_TAG_NAME
&&
9548 (attr
->num_values
> 1 ||
9549 strcmp(attr
->values
[0].string
.text
, "none")))
9551 for (i
= 0; i
< attr
->num_values
; i
++)
9552 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9555 else if (!strcmp(attr
->name
, "job-quota-period"))
9557 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9560 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
9561 attr
->values
[0].integer
);
9562 cupsdFreeQuotas(printer
);
9564 printer
->quota_period
= attr
->values
[0].integer
;
9566 else if (!strcmp(attr
->name
, "job-k-limit"))
9568 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9571 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
9572 attr
->values
[0].integer
);
9573 cupsdFreeQuotas(printer
);
9575 printer
->k_limit
= attr
->values
[0].integer
;
9577 else if (!strcmp(attr
->name
, "job-page-limit"))
9579 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9582 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
9583 attr
->values
[0].integer
);
9584 cupsdFreeQuotas(printer
);
9586 printer
->page_limit
= attr
->values
[0].integer
;
9588 else if (!strcmp(attr
->name
, "printer-op-policy"))
9590 cupsd_policy_t
*p
; /* Policy */
9593 if (attr
->value_tag
!= IPP_TAG_NAME
)
9596 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
9598 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9599 "Setting printer-op-policy to \"%s\"...",
9600 attr
->values
[0].string
.text
);
9601 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
9602 printer
->op_policy_ptr
= p
;
9606 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9607 _("Unknown printer-op-policy \"%s\"."),
9608 attr
->values
[0].string
.text
);
9612 else if (!strcmp(attr
->name
, "printer-error-policy"))
9614 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9617 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
9618 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
9619 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
9621 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9622 _("Unknown printer-error-policy \"%s\"."),
9623 attr
->values
[0].string
.text
);
9627 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9628 "Setting printer-error-policy to \"%s\"...",
9629 attr
->values
[0].string
.text
);
9630 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
9634 * Skip any other non-default attributes...
9637 namelen
= strlen(attr
->name
);
9638 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
9639 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
9643 * OK, anything else must be a user-defined default...
9646 strlcpy(name
, attr
->name
, sizeof(name
));
9647 name
[namelen
- 8] = '\0'; /* Strip "-default" */
9649 switch (attr
->value_tag
)
9651 case IPP_TAG_DELETEATTR
:
9652 printer
->num_options
= cupsRemoveOption(name
,
9653 printer
->num_options
,
9654 &(printer
->options
));
9655 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9656 "Deleting %s", attr
->name
);
9660 case IPP_TAG_KEYWORD
:
9662 printer
->num_options
= cupsAddOption(name
,
9663 attr
->values
[0].string
.text
,
9664 printer
->num_options
,
9665 &(printer
->options
));
9666 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9667 "Setting %s to \"%s\"...", attr
->name
,
9668 attr
->values
[0].string
.text
);
9671 case IPP_TAG_BOOLEAN
:
9672 printer
->num_options
= cupsAddOption(name
,
9673 attr
->values
[0].boolean
?
9675 printer
->num_options
,
9676 &(printer
->options
));
9677 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9678 "Setting %s to %s...", attr
->name
,
9679 attr
->values
[0].boolean
? "true" : "false");
9682 case IPP_TAG_INTEGER
:
9684 sprintf(value
, "%d", attr
->values
[0].integer
);
9685 printer
->num_options
= cupsAddOption(name
, value
,
9686 printer
->num_options
,
9687 &(printer
->options
));
9688 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9689 "Setting %s to %s...", attr
->name
, value
);
9692 case IPP_TAG_RANGE
:
9693 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
9694 attr
->values
[0].range
.upper
);
9695 printer
->num_options
= cupsAddOption(name
, value
,
9696 printer
->num_options
,
9697 &(printer
->options
));
9698 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9699 "Setting %s to %s...", attr
->name
, value
);
9702 case IPP_TAG_RESOLUTION
:
9703 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
9704 attr
->values
[0].resolution
.yres
,
9705 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
9707 printer
->num_options
= cupsAddOption(name
, value
,
9708 printer
->num_options
,
9709 &(printer
->options
));
9710 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9711 "Setting %s to %s...", attr
->name
, value
);
9715 /* Do nothing for other values */
9723 * 'start_printer()' - Start a printer.
9727 start_printer(cupsd_client_t
*con
, /* I - Client connection */
9728 ipp_attribute_t
*uri
) /* I - Printer URI */
9730 http_status_t status
; /* Policy status */
9731 cups_ptype_t dtype
; /* Destination type (printer/class) */
9732 cupsd_printer_t
*printer
; /* Printer data */
9735 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
9736 con
->http
.fd
, uri
->values
[0].string
.text
);
9739 * Is the destination valid?
9742 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9748 send_ipp_status(con
, IPP_NOT_FOUND
,
9749 _("The printer or class was not found."));
9757 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9759 send_http_error(con
, status
, printer
);
9764 * Start the printer...
9767 printer
->state_message
[0] = '\0';
9769 cupsdStartPrinter(printer
, 1);
9771 if (dtype
& CUPS_PRINTER_CLASS
)
9772 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
9773 printer
->name
, get_username(con
));
9775 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
9776 printer
->name
, get_username(con
));
9781 * Everything was ok, so return OK status...
9784 con
->response
->request
.status
.status_code
= IPP_OK
;
9789 * 'stop_printer()' - Stop a printer.
9793 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
9794 ipp_attribute_t
*uri
) /* I - Printer URI */
9796 http_status_t status
; /* Policy status */
9797 cups_ptype_t dtype
; /* Destination type (printer/class) */
9798 cupsd_printer_t
*printer
; /* Printer data */
9799 ipp_attribute_t
*attr
; /* printer-state-message attribute */
9802 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
9803 con
->http
.fd
, uri
->values
[0].string
.text
);
9806 * Is the destination valid?
9809 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9815 send_ipp_status(con
, IPP_NOT_FOUND
,
9816 _("The printer or class was not found."));
9824 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9826 send_http_error(con
, status
, printer
);
9831 * Stop the printer...
9834 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
9835 IPP_TAG_TEXT
)) == NULL
)
9836 strcpy(printer
->state_message
, "Paused");
9839 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
9840 sizeof(printer
->state_message
));
9843 cupsdStopPrinter(printer
, 1);
9845 if (dtype
& CUPS_PRINTER_CLASS
)
9846 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
9847 printer
->name
, get_username(con
));
9849 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
9850 printer
->name
, get_username(con
));
9853 * Everything was ok, so return OK status...
9856 con
->response
->request
.status
.status_code
= IPP_OK
;
9861 * 'url_encode_attr()' - URL-encode a string attribute.
9865 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
9866 char *buffer
,/* I - String buffer */
9867 int bufsize
)/* I - Size of buffer */
9869 int i
; /* Looping var */
9870 char *bufptr
, /* Pointer into buffer */
9871 *bufend
; /* End of buffer */
9874 strlcpy(buffer
, attr
->name
, bufsize
);
9875 bufptr
= buffer
+ strlen(buffer
);
9876 bufend
= buffer
+ bufsize
- 1;
9878 for (i
= 0; i
< attr
->num_values
; i
++)
9880 if (bufptr
>= bufend
)
9888 if (bufptr
>= bufend
)
9893 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
9894 bufptr
, bufend
- bufptr
+ 1);
9896 if (bufptr
>= bufend
)
9907 * 'url_encode_string()' - URL-encode a string.
9910 static char * /* O - End of string */
9911 url_encode_string(const char *s
, /* I - String */
9912 char *buffer
, /* I - String buffer */
9913 int bufsize
) /* I - Size of buffer */
9915 char *bufptr
, /* Pointer into buffer */
9916 *bufend
; /* End of buffer */
9917 static const char *hex
= "0123456789ABCDEF";
9922 bufend
= buffer
+ bufsize
- 1;
9924 while (*s
&& bufptr
< bufend
)
9926 if (*s
== ' ' || *s
== '%')
9928 if (bufptr
>= (bufend
- 2))
9932 *bufptr
++ = hex
[(*s
>> 4) & 15];
9933 *bufptr
++ = hex
[*s
& 15];
9937 else if (*s
== '\'' || *s
== '\\')
9939 if (bufptr
>= (bufend
- 1))
9956 * 'user_allowed()' - See if a user is allowed to print to a queue.
9959 static int /* O - 0 if not allowed, 1 if allowed */
9960 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
9961 const char *username
) /* I - Username */
9963 int i
; /* Looping var */
9964 struct passwd
*pw
; /* User password data */
9967 if (p
->num_users
== 0)
9970 if (!strcmp(username
, "root"))
9973 pw
= getpwnam(username
);
9976 for (i
= 0; i
< p
->num_users
; i
++)
9978 if (p
->users
[i
][0] == '@')
9981 * Check group membership...
9984 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
9987 else if (!strcasecmp(username
, p
->users
[i
]))
9991 return ((i
< p
->num_users
) != p
->deny_users
);
9996 * 'validate_job()' - Validate printer options and destination.
10000 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10001 ipp_attribute_t
*uri
) /* I - Printer URI */
10003 http_status_t status
; /* Policy status */
10004 ipp_attribute_t
*attr
; /* Current attribute */
10005 ipp_attribute_t
*format
; /* Document-format attribute */
10006 cups_ptype_t dtype
; /* Destination type (printer/class) */
10007 char super
[MIME_MAX_SUPER
],
10008 /* Supertype of file */
10009 type
[MIME_MAX_TYPE
];
10010 /* Subtype of file */
10011 cupsd_printer_t
*printer
; /* Printer */
10014 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10015 con
->http
.fd
, uri
->values
[0].string
.text
);
10018 * OK, see if the client is sending the document compressed - CUPS
10019 * doesn't support compression yet...
10022 if ((attr
= ippFindAttribute(con
->request
, "compression",
10023 IPP_TAG_KEYWORD
)) != NULL
&&
10024 !strcmp(attr
->values
[0].string
.text
, "none"))
10026 send_ipp_status(con
, IPP_ATTRIBUTES
,
10027 _("Unsupported compression attribute %s!"),
10028 attr
->values
[0].string
.text
);
10029 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10030 "compression", NULL
, attr
->values
[0].string
.text
);
10035 * Is it a format we support?
10038 if ((format
= ippFindAttribute(con
->request
, "document-format",
10039 IPP_TAG_MIMETYPE
)) != NULL
)
10041 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
10044 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
10045 format
->values
[0].string
.text
);
10049 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10050 !mimeType(MimeDatabase
, super
, type
))
10052 cupsdLogMessage(CUPSD_LOG_INFO
,
10053 "Hint: Do you have the raw file printing rules enabled?");
10054 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10055 _("Unsupported format \"%s\"!"),
10056 format
->values
[0].string
.text
);
10057 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10058 "document-format", NULL
, format
->values
[0].string
.text
);
10064 * Is the destination valid?
10067 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10073 send_ipp_status(con
, IPP_NOT_FOUND
,
10074 _("The printer or class was not found."));
10082 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10084 send_http_error(con
, status
, printer
);
10089 * Everything was ok, so return OK status...
10092 con
->response
->request
.status
.status_code
= IPP_OK
;
10097 * 'validate_name()' - Make sure the printer name only contains valid chars.
10100 static int /* O - 0 if name is no good, 1 if good */
10101 validate_name(const char *name
) /* I - Name to check */
10103 const char *ptr
; /* Pointer into name */
10107 * Scan the whole name...
10110 for (ptr
= name
; *ptr
; ptr
++)
10111 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
10115 * All the characters are good; validate the length, too...
10118 return ((ptr
- name
) < 128);
10123 * 'validate_user()' - Validate the user for the request.
10126 static int /* O - 1 if permitted, 0 otherwise */
10127 validate_user(cupsd_job_t
*job
, /* I - Job */
10128 cupsd_client_t
*con
, /* I - Client connection */
10129 const char *owner
, /* I - Owner of job/resource */
10130 char *username
, /* O - Authenticated username */
10131 int userlen
) /* I - Length of username */
10133 cupsd_printer_t
*printer
; /* Printer for job */
10136 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
10137 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
10139 job
? job
->id
: 0, con
->http
.fd
, owner
? owner
: "(null)",
10140 username
, userlen
);
10143 * Validate input...
10146 if (!con
|| !owner
|| !username
|| userlen
<= 0)
10150 * Get the best authenticated username that is available.
10153 strlcpy(username
, get_username(con
), userlen
);
10156 * Check the username against the owner...
10159 printer
= cupsdFindDest(job
->dest
);
10161 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
10162 con
, owner
) == HTTP_OK
);
10167 * End of "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $".