2 * "$Id: ipp.c 6689 2007-07-18 23:52:15Z 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_job_attrs() - Get job attributes.
58 * get_jobs() - Get a list of jobs for the specified printer.
59 * get_notifications() - Get events for a subscription.
60 * get_ppd() - Get a named PPD from the local system.
61 * get_ppds() - Get the list of PPD files on the local
63 * get_printer_attrs() - Get printer attributes.
64 * get_printers() - Get a list of printers.
65 * get_subscription_attrs() - Get subscription attributes.
66 * get_subscriptions() - Get subscriptions.
67 * get_username() - Get the username associated with a request.
68 * hold_job() - Hold a print job.
69 * move_job() - Move a job to a new destination.
70 * ppd_parse_line() - Parse a PPD default line.
71 * print_job() - Print a file to a printer or class.
72 * read_ps_line() - Read a line from a PS file...
73 * read_ps_job_ticket() - Reads a job ticket embedded in a PS file.
74 * reject_jobs() - Reject print jobs to a printer.
75 * release_job() - Release a held print job.
76 * restart_job() - Restart an old print job.
77 * save_auth_info() - Save authentication information for a job.
78 * save_krb5_creds() - Save Kerberos credentials for a job.
79 * send_document() - Send a file to a printer or class.
80 * send_http_error() - Send a HTTP error back to the IPP client.
81 * send_ipp_status() - Send a status back to the IPP client.
82 * set_default() - Set the default destination...
83 * set_job_attrs() - Set job attributes.
84 * set_printer_defaults() - Set printer default options from a request.
85 * start_printer() - Start a printer.
86 * stop_printer() - Stop a printer.
87 * url_encode_attr() - URL-encode a string attribute.
88 * url_encode_string() - URL-encode a string.
89 * user_allowed() - See if a user is allowed to print to a queue.
90 * validate_job() - Validate printer options and destination.
91 * validate_name() - Make sure the printer name only contains
93 * validate_user() - Validate the user for the request.
97 * Include necessary headers...
104 #endif /* HAVE_KRB5_H */
108 #endif /* HAVE_LIBPAPER */
111 # ifdef HAVE_MEMBERSHIP_H
112 # include <membership.h>
113 # endif /* HAVE_MEMBERSHIP_H */
114 # ifdef HAVE_MEMBERSHIPPRIV_H
115 # include <membershipPriv.h>
117 extern int mbr_user_name_to_uuid(const char* name
, uuid_t uu
);
118 extern int mbr_group_name_to_uuid(const char* name
, uuid_t uu
);
119 extern int mbr_check_membership_by_id(uuid_t user
, gid_t group
, int* ismember
);
120 # endif /* HAVE_MEMBERSHIPPRIV_H */
121 #endif /* __APPLE__ */
128 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
129 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
130 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
131 mime_type_t
*filetype
, int compression
);
132 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
133 mime_type_t
*filetype
);
134 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
135 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
136 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
137 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
138 static void add_printer_state_reasons(cupsd_client_t
*con
,
140 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
141 static void apply_printer_defaults(cupsd_printer_t
*printer
,
143 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
144 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
145 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
146 static void cancel_subscription(cupsd_client_t
*con
, int id
);
147 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
148 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
150 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
151 ipp_tag_t group
, int quickcopy
);
152 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
154 static int copy_file(const char *from
, const char *to
);
155 static int copy_model(cupsd_client_t
*con
, const char *from
,
157 static void copy_job_attrs(cupsd_client_t
*con
,
160 static void copy_printer_attrs(cupsd_client_t
*con
,
161 cupsd_printer_t
*printer
,
163 static void copy_subscription_attrs(cupsd_client_t
*con
,
164 cupsd_subscription_t
*sub
,
166 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
167 static cups_array_t
*create_requested_array(ipp_t
*request
);
168 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
169 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
170 static void get_default(cupsd_client_t
*con
);
171 static void get_devices(cupsd_client_t
*con
);
172 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
173 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
174 static void get_notifications(cupsd_client_t
*con
);
175 static void get_ppd(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
176 static void get_ppds(cupsd_client_t
*con
);
177 static void get_printers(cupsd_client_t
*con
, int type
);
178 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
179 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
180 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
181 static const char *get_username(cupsd_client_t
*con
);
182 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
183 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
184 static int ppd_parse_line(const char *line
, char *option
, int olen
,
185 char *choice
, int clen
);
186 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
187 static void read_ps_job_ticket(cupsd_client_t
*con
);
188 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
189 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
190 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
191 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
192 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
,
193 ipp_attribute_t
*auth_info
);
194 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
195 static void save_krb5_creds(cupsd_client_t
*con
, cupsd_job_t
*job
);
196 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
197 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
198 static void send_http_error(cupsd_client_t
*con
, http_status_t status
,
199 cupsd_printer_t
*printer
);
200 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
201 const char *message
, ...)
203 __attribute__ ((__format__ (__printf__
, 3, 4)))
204 # endif /* __GNUC__ */
206 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
207 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
208 static void set_printer_defaults(cupsd_client_t
*con
,
209 cupsd_printer_t
*printer
);
210 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
211 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
212 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
,
214 static char *url_encode_string(const char *s
, char *buffer
, int bufsize
);
215 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
216 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
217 static int validate_name(const char *name
);
218 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
,
219 const char *owner
, char *username
,
224 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
227 int /* O - 1 on success, 0 on failure */
228 cupsdProcessIPPRequest(
229 cupsd_client_t
*con
) /* I - Client connection */
231 ipp_tag_t group
; /* Current group tag */
232 ipp_attribute_t
*attr
; /* Current attribute */
233 ipp_attribute_t
*charset
; /* Character set attribute */
234 ipp_attribute_t
*language
; /* Language attribute */
235 ipp_attribute_t
*uri
; /* Printer URI attribute */
236 ipp_attribute_t
*username
; /* requesting-user-name attr */
237 int sub_id
; /* Subscription ID */
240 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
241 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
242 con
, con
->http
.fd
, con
->request
->request
.op
.operation_id
);
245 * First build an empty response message for this request...
248 con
->response
= ippNew();
250 con
->response
->request
.status
.version
[0] =
251 con
->request
->request
.op
.version
[0];
252 con
->response
->request
.status
.version
[1] =
253 con
->request
->request
.op
.version
[1];
254 con
->response
->request
.status
.request_id
=
255 con
->request
->request
.op
.request_id
;
258 * Then validate the request header and required attributes...
261 if (con
->request
->request
.any
.version
[0] != 1)
264 * Return an error, since we only support IPP 1.x.
267 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
268 "%04X %s Bad request version number %d.%d",
269 IPP_VERSION_NOT_SUPPORTED
, con
->http
.hostname
,
270 con
->request
->request
.any
.version
[0],
271 con
->request
->request
.any
.version
[1]);
273 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
274 _("Bad request version number %d.%d!"),
275 con
->request
->request
.any
.version
[0],
276 con
->request
->request
.any
.version
[1]);
278 else if (!con
->request
->attrs
)
280 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
281 "%04X %s No attributes in request",
282 IPP_BAD_REQUEST
, con
->http
.hostname
);
284 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request!"));
289 * Make sure that the attributes are provided in the correct order and
290 * don't repeat groups...
293 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
296 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
299 * Out of order; return an error...
302 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
303 "%04X %s Attribute groups are out of order",
304 IPP_BAD_REQUEST
, con
->http
.hostname
);
306 send_ipp_status(con
, IPP_BAD_REQUEST
,
307 _("Attribute groups are out of order (%x < %x)!"),
308 attr
->group_tag
, group
);
312 group
= attr
->group_tag
;
317 * Then make sure that the first three attributes are:
320 * attributes-natural-language
321 * printer-uri/job-uri
324 attr
= con
->request
->attrs
;
325 if (attr
&& !strcmp(attr
->name
, "attributes-charset") &&
326 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
334 if (attr
&& !strcmp(attr
->name
, "attributes-natural-language") &&
335 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
340 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
341 IPP_TAG_URI
)) != NULL
)
343 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
344 IPP_TAG_URI
)) != NULL
)
346 else if (con
->request
->request
.op
.operation_id
== CUPS_GET_PPD
)
347 uri
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
);
352 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
353 "attributes-charset", NULL
,
354 charset
->values
[0].string
.text
);
356 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
357 "attributes-charset", NULL
, DefaultCharset
);
360 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
361 "attributes-natural-language", NULL
,
362 language
->values
[0].string
.text
);
364 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
365 "attributes-natural-language", NULL
, DefaultLanguage
);
367 if (!charset
|| !language
||
369 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
370 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
371 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
372 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
373 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
376 * Return an error, since attributes-charset,
377 * attributes-natural-language, and printer-uri/job-uri are required
378 * for all operations.
383 cupsdLogMessage(CUPSD_LOG_ERROR
,
384 "Missing attributes-charset attribute!");
386 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
387 "%04X %s Missing attributes-charset attribute",
388 IPP_BAD_REQUEST
, con
->http
.hostname
);
393 cupsdLogMessage(CUPSD_LOG_ERROR
,
394 "Missing attributes-natural-language attribute!");
396 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
397 "%04X %s Missing attributes-natural-language attribute",
398 IPP_BAD_REQUEST
, con
->http
.hostname
);
403 cupsdLogMessage(CUPSD_LOG_ERROR
,
404 "Missing printer-uri, job-uri, or ppd-name "
407 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
408 "%04X %s Missing printer-uri, job-uri, or ppd-name "
409 "attribute", IPP_BAD_REQUEST
, con
->http
.hostname
);
412 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
414 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
415 cupsdLogMessage(CUPSD_LOG_DEBUG
,
416 "attr \"%s\": group_tag = %x, value_tag = %x",
417 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
420 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
422 send_ipp_status(con
, IPP_BAD_REQUEST
,
423 _("Missing required attributes!"));
428 * OK, all the checks pass so far; make sure requesting-user-name is
429 * not "root" from a remote host...
432 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
433 IPP_TAG_NAME
)) != NULL
)
436 * Check for root user...
439 if (!strcmp(username
->values
[0].string
.text
, "root") &&
440 strcasecmp(con
->http
.hostname
, "localhost") &&
441 strcmp(con
->username
, "root"))
444 * Remote unauthenticated user masquerading as local root...
447 _cupsStrFree(username
->values
[0].string
.text
);
448 username
->values
[0].string
.text
= _cupsStrAlloc(RemoteRoot
);
452 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
453 IPP_TAG_INTEGER
)) != NULL
)
454 sub_id
= attr
->values
[0].integer
;
459 * Then try processing the operation...
463 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
464 ippOpString(con
->request
->request
.op
.operation_id
),
465 uri
->values
[0].string
.text
);
467 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
468 ippOpString(con
->request
->request
.op
.operation_id
));
470 switch (con
->request
->request
.op
.operation_id
)
476 case IPP_VALIDATE_JOB
:
477 validate_job(con
, uri
);
480 case IPP_CREATE_JOB
:
481 create_job(con
, uri
);
484 case IPP_SEND_DOCUMENT
:
485 send_document(con
, uri
);
488 case IPP_CANCEL_JOB
:
489 cancel_job(con
, uri
);
492 case IPP_GET_JOB_ATTRIBUTES
:
493 get_job_attrs(con
, uri
);
500 case IPP_GET_PRINTER_ATTRIBUTES
:
501 get_printer_attrs(con
, uri
);
508 case IPP_RELEASE_JOB
:
509 release_job(con
, uri
);
512 case IPP_RESTART_JOB
:
513 restart_job(con
, uri
);
516 case IPP_PAUSE_PRINTER
:
517 stop_printer(con
, uri
);
520 case IPP_RESUME_PRINTER
:
521 start_printer(con
, uri
);
524 case IPP_PURGE_JOBS
:
525 cancel_all_jobs(con
, uri
);
528 case IPP_SET_JOB_ATTRIBUTES
:
529 set_job_attrs(con
, uri
);
532 case CUPS_GET_DEFAULT
:
536 case CUPS_GET_PRINTERS
:
537 get_printers(con
, 0);
540 case CUPS_GET_CLASSES
:
541 get_printers(con
, CUPS_PRINTER_CLASS
);
544 case CUPS_ADD_PRINTER
:
545 add_printer(con
, uri
);
548 case CUPS_DELETE_PRINTER
:
549 delete_printer(con
, uri
);
552 case CUPS_ADD_CLASS
:
556 case CUPS_DELETE_CLASS
:
557 delete_printer(con
, uri
);
560 case CUPS_ACCEPT_JOBS
:
561 case IPP_ENABLE_PRINTER
:
562 accept_jobs(con
, uri
);
565 case CUPS_REJECT_JOBS
:
566 case IPP_DISABLE_PRINTER
:
567 reject_jobs(con
, uri
);
570 case CUPS_SET_DEFAULT
:
571 set_default(con
, uri
);
574 case CUPS_GET_DEVICES
:
590 case CUPS_AUTHENTICATE_JOB
:
591 authenticate_job(con
, uri
);
594 case IPP_CREATE_PRINTER_SUBSCRIPTION
:
595 case IPP_CREATE_JOB_SUBSCRIPTION
:
596 create_subscription(con
, uri
);
599 case IPP_GET_SUBSCRIPTION_ATTRIBUTES
:
600 get_subscription_attrs(con
, sub_id
);
603 case IPP_GET_SUBSCRIPTIONS
:
604 get_subscriptions(con
, uri
);
607 case IPP_RENEW_SUBSCRIPTION
:
608 renew_subscription(con
, sub_id
);
611 case IPP_CANCEL_SUBSCRIPTION
:
612 cancel_subscription(con
, sub_id
);
615 case IPP_GET_NOTIFICATIONS
:
616 get_notifications(con
);
620 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
621 "%04X %s Operation %04X (%s) not supported",
622 IPP_OPERATION_NOT_SUPPORTED
, con
->http
.hostname
,
623 con
->request
->request
.op
.operation_id
,
624 ippOpString(con
->request
->request
.op
.operation_id
));
626 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
627 _("%s not supported!"),
629 con
->request
->request
.op
.operation_id
));
639 * Sending data from the scheduler...
642 cupsdLogMessage(CUPSD_LOG_DEBUG
,
643 "cupsdProcessIPPRequest: %d status_code=%x (%s)",
644 con
->http
.fd
, con
->response
->request
.status
.status_code
,
645 ippErrorString(con
->response
->request
.status
.status_code
));
647 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp", AUTH_NONE
))
649 #ifdef CUPSD_USE_CHUNKING
651 * Because older versions of CUPS (1.1.17 and older) and some IPP
652 * clients do not implement chunking properly, we cannot use
653 * chunking by default. This may become the default in future
654 * CUPS releases, or we might add a configuration directive for
658 if (con
->http
.version
== HTTP_1_1
)
660 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n") < 0)
663 if (cupsdFlushHeader(con
) < 0)
666 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
669 #endif /* CUPSD_USE_CHUNKING */
671 size_t length
; /* Length of response */
674 length
= ippLength(con
->response
);
676 if (con
->file
>= 0 && !con
->pipe_pid
)
678 struct stat fileinfo
; /* File information */
681 if (!fstat(con
->file
, &fileinfo
))
682 length
+= fileinfo
.st_size
;
685 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
686 CUPS_LLCAST length
) < 0)
689 if (cupsdFlushHeader(con
) < 0)
692 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
693 con
->http
.data_remaining
= length
;
695 if (con
->http
.data_remaining
<= INT_MAX
)
696 con
->http
._data_remaining
= con
->http
.data_remaining
;
698 con
->http
._data_remaining
= INT_MAX
;
701 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
702 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
705 * Tell the caller the response header was sent successfully...
713 * Tell the caller the response header could not be sent...
722 * Sending data from a subprocess like cups-deviced; tell the caller
723 * everything is A-OK so far...
732 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
736 cupsdTimeoutJob(cupsd_job_t
*job
) /* I - Job to timeout */
738 cupsd_printer_t
*printer
; /* Destination printer or class */
739 ipp_attribute_t
*attr
; /* job-sheets attribute */
740 int kbytes
; /* Kilobytes in banner */
743 job
->pending_timeout
= 0;
746 * See if we need to add the ending sheet...
749 printer
= cupsdFindDest(job
->dest
);
750 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
753 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
754 attr
&& attr
->num_values
> 1)
760 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Adding end banner page \"%s\".",
761 job
->id
, attr
->values
[1].string
.text
);
763 kbytes
= copy_banner(NULL
, job
, attr
->values
[1].string
.text
);
765 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
771 * 'accept_jobs()' - Accept print jobs to a printer.
775 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
776 ipp_attribute_t
*uri
) /* I - Printer or class URI */
778 http_status_t status
; /* Policy status */
779 cups_ptype_t dtype
; /* Destination type (printer/class) */
780 cupsd_printer_t
*printer
; /* Printer data */
783 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
784 con
->http
.fd
, uri
->values
[0].string
.text
);
787 * Is the destination valid?
790 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
796 send_ipp_status(con
, IPP_NOT_FOUND
,
797 _("The printer or class was not found."));
805 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
807 send_http_error(con
, status
, printer
);
812 * Accept jobs sent to the printer...
815 printer
->accepting
= 1;
816 printer
->state_message
[0] = '\0';
818 cupsdAddPrinterHistory(printer
);
820 if (dtype
& CUPS_PRINTER_CLASS
)
822 cupsdSaveAllClasses();
824 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
825 printer
->name
, get_username(con
));
829 cupsdSaveAllPrinters();
831 cupsdLogMessage(CUPSD_LOG_INFO
,
832 "Printer \"%s\" now accepting jobs (\"%s\").",
833 printer
->name
, get_username(con
));
837 * Everything was ok, so return OK status...
840 con
->response
->request
.status
.status_code
= IPP_OK
;
845 * 'add_class()' - Add a class to the system.
849 add_class(cupsd_client_t
*con
, /* I - Client connection */
850 ipp_attribute_t
*uri
) /* I - URI of class */
852 http_status_t status
; /* Policy status */
853 int i
; /* Looping var */
854 char method
[HTTP_MAX_URI
], /* Method portion of URI */
855 username
[HTTP_MAX_URI
], /* Username portion of URI */
856 host
[HTTP_MAX_URI
], /* Host portion of URI */
857 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
858 int port
; /* Port portion of URI */
859 cupsd_printer_t
*pclass
, /* Class */
860 *member
; /* Member printer/class */
861 cups_ptype_t dtype
; /* Destination type */
862 ipp_attribute_t
*attr
; /* Printer attribute */
863 int modify
; /* Non-zero if we just modified */
864 char newname
[IPP_MAX_NAME
]; /* New class name */
865 int need_restart_job
; /* Need to restart job? */
868 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
869 con
->http
.fd
, uri
->values
[0].string
.text
);
872 * Do we have a valid URI?
875 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
876 sizeof(method
), username
, sizeof(username
), host
,
877 sizeof(host
), &port
, resource
, sizeof(resource
));
880 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
883 * No, return an error...
886 send_ipp_status(con
, IPP_BAD_REQUEST
,
887 _("The printer-uri must be of the form "
888 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
893 * Do we have a valid printer name?
896 if (!validate_name(resource
+ 9))
899 * No, return an error...
902 send_ipp_status(con
, IPP_BAD_REQUEST
,
903 _("The printer-uri \"%s\" contains invalid characters."),
904 uri
->values
[0].string
.text
);
912 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
914 send_http_error(con
, status
, NULL
);
919 * See if the class already exists; if not, create a new class...
922 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
925 * Class doesn't exist; see if we have a printer of the same name...
928 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
929 !(pclass
->type
& CUPS_PRINTER_DISCOVERED
))
932 * Yes, return an error...
935 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
936 _("A printer named \"%s\" already exists!"),
942 * No, add the pclass...
945 pclass
= cupsdAddClass(resource
+ 9);
948 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
951 * Rename the implicit class to "AnyClass" or remove it...
954 if (ImplicitAnyClasses
)
956 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
957 cupsdRenamePrinter(pclass
, newname
);
960 cupsdDeletePrinter(pclass
, 1);
963 * Add the class as a new local class...
966 pclass
= cupsdAddClass(resource
+ 9);
969 else if (pclass
->type
& CUPS_PRINTER_DISCOVERED
)
972 * Rename the remote class to "Class"...
975 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
976 cupsdRenamePrinter(pclass
, newname
);
979 * Add the class as a new local class...
982 pclass
= cupsdAddClass(resource
+ 9);
989 * Look for attributes and copy them over as needed...
992 need_restart_job
= 0;
994 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
995 IPP_TAG_TEXT
)) != NULL
)
996 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
998 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
999 IPP_TAG_TEXT
)) != NULL
)
1000 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
1002 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
1003 IPP_TAG_BOOLEAN
)) != NULL
)
1005 cupsdLogMessage(CUPSD_LOG_INFO
,
1006 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
1007 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
1009 pclass
->accepting
= attr
->values
[0].boolean
;
1010 cupsdAddPrinterHistory(pclass
);
1013 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
1014 IPP_TAG_BOOLEAN
)) != NULL
)
1016 if (pclass
->shared
&& !attr
->values
[0].boolean
)
1017 cupsdDeregisterPrinter(pclass
, 1);
1019 cupsdLogMessage(CUPSD_LOG_INFO
,
1020 "Setting %s printer-is-shared to %d (was %d.)",
1021 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
1023 pclass
->shared
= attr
->values
[0].boolean
;
1026 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
1027 IPP_TAG_ENUM
)) != NULL
)
1029 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
1030 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
1032 send_ipp_status(con
, IPP_BAD_REQUEST
,
1033 _("Attempt to set %s printer-state to bad value %d!"),
1034 pclass
->name
, attr
->values
[0].integer
);
1038 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
1039 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
1041 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1042 cupsdStopPrinter(pclass
, 0);
1045 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1046 need_restart_job
= 1;
1049 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1050 IPP_TAG_TEXT
)) != NULL
)
1052 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1053 sizeof(pclass
->state_message
));
1054 cupsdAddPrinterHistory(pclass
);
1056 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1057 IPP_TAG_URI
)) != NULL
)
1060 * Clear the printer array as needed...
1063 need_restart_job
= 1;
1065 if (pclass
->num_printers
> 0)
1067 free(pclass
->printers
);
1068 pclass
->num_printers
= 0;
1072 * Add each printer or class that is listed...
1075 for (i
= 0; i
< attr
->num_values
; i
++)
1078 * Search for the printer or class URI...
1081 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1087 send_ipp_status(con
, IPP_NOT_FOUND
,
1088 _("The printer or class was not found."));
1093 * Add it to the class...
1096 cupsdAddPrinterToClass(pclass
, member
);
1100 set_printer_defaults(con
, pclass
);
1102 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1103 IPP_TAG_KEYWORD
)) != NULL
)
1104 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1107 * Update the printer class attributes and return...
1110 cupsdSetPrinterAttrs(pclass
);
1111 cupsdSaveAllClasses();
1113 if (need_restart_job
&& pclass
->job
)
1118 * Stop the current job and then restart it below...
1121 job
= (cupsd_job_t
*)pclass
->job
;
1123 cupsdStopJob(job
, 1);
1125 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1126 job
->state_value
= IPP_JOB_PENDING
;
1129 if (need_restart_job
)
1132 cupsdWritePrintcap();
1136 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1137 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1140 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1141 pclass
->name
, get_username(con
));
1145 cupsdAddPrinterHistory(pclass
);
1147 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1148 "New class \"%s\" added by \"%s\".", pclass
->name
,
1151 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1152 pclass
->name
, get_username(con
));
1155 con
->response
->request
.status
.status_code
= IPP_OK
;
1160 * 'add_file()' - Add a file to a job.
1163 static int /* O - 0 on success, -1 on error */
1164 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1165 cupsd_job_t
*job
, /* I - Job to add to */
1166 mime_type_t
*filetype
, /* I - Type of file */
1167 int compression
) /* I - Compression */
1169 mime_type_t
**filetypes
; /* New filetypes array... */
1170 int *compressions
; /* New compressions array... */
1173 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1174 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1175 "compression=%d)", con
, con
? con
->http
.fd
: -1, job
->id
,
1176 filetype
->super
, filetype
->type
, compression
);
1179 * Add the file to the job...
1182 if (job
->num_files
== 0)
1184 compressions
= (int *)malloc(sizeof(int));
1185 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1189 compressions
= (int *)realloc(job
->compressions
,
1190 (job
->num_files
+ 1) * sizeof(int));
1191 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1192 (job
->num_files
+ 1) *
1193 sizeof(mime_type_t
*));
1196 if (!compressions
|| !filetypes
)
1198 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
1201 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1202 _("Unable to allocate memory for file types!"));
1207 job
->compressions
= compressions
;
1208 job
->compressions
[job
->num_files
] = compression
;
1209 job
->filetypes
= filetypes
;
1210 job
->filetypes
[job
->num_files
] = filetype
;
1219 * 'add_job()' - Add a job to a print queue.
1222 static cupsd_job_t
* /* O - Job object */
1223 add_job(cupsd_client_t
*con
, /* I - Client connection */
1224 cupsd_printer_t
*printer
, /* I - Destination printer */
1225 mime_type_t
*filetype
) /* I - First print file type, if any */
1227 http_status_t status
; /* Policy status */
1228 ipp_attribute_t
*attr
, /* Current attribute */
1229 *auth_info
; /* auth-info attribute */
1230 const char *val
; /* Default option value */
1231 int priority
; /* Job priority */
1232 char *title
; /* Job name/title */
1233 cupsd_job_t
*job
; /* Current job */
1234 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1235 int kbytes
; /* Size of print file */
1236 int i
; /* Looping var */
1237 int lowerpagerange
; /* Page range bound */
1240 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1241 con
, con
->http
.fd
, printer
, printer
->name
,
1242 filetype
, filetype
->super
, filetype
->type
);
1245 * Check remote printing to non-shared printer...
1248 if (!printer
->shared
&&
1249 strcasecmp(con
->http
.hostname
, "localhost") &&
1250 strcasecmp(con
->http
.hostname
, ServerName
))
1252 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1253 _("The printer or class is not shared!"));
1261 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1263 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1265 send_http_error(con
, status
, printer
);
1268 else if ((printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
1269 !con
->username
[0] && !auth_info
)
1271 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1275 else if (auth_info
&& !con
->http
.tls
&&
1276 !httpAddrLocalhost(con
->http
.hostaddr
))
1279 * Require encryption of auth-info over non-local connections...
1282 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1285 #endif /* HAVE_SSL */
1288 * See if the printer is accepting jobs...
1291 if (!printer
->accepting
)
1293 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1294 _("Destination \"%s\" is not accepting jobs."),
1300 * Validate job template attributes; for now just document-format,
1301 * copies, number-up, and page-ranges...
1304 if (filetype
&& printer
->filetypes
&&
1305 !cupsArrayFind(printer
->filetypes
, filetype
))
1307 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1308 /* MIME media type string */
1311 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1314 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1315 _("Unsupported format \'%s\'!"), mimetype
);
1317 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1318 "document-format", NULL
, mimetype
);
1323 if ((attr
= ippFindAttribute(con
->request
, "copies",
1324 IPP_TAG_INTEGER
)) != NULL
)
1326 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1328 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1329 attr
->values
[0].integer
);
1330 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1331 "copies", attr
->values
[0].integer
);
1336 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1337 IPP_TAG_INTEGER
)) != NULL
)
1339 if (attr
->values
[0].integer
!= 1 &&
1340 attr
->values
[0].integer
!= 2 &&
1341 attr
->values
[0].integer
!= 4 &&
1342 attr
->values
[0].integer
!= 6 &&
1343 attr
->values
[0].integer
!= 9 &&
1344 attr
->values
[0].integer
!= 16)
1346 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1347 attr
->values
[0].integer
);
1348 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1349 "number-up", attr
->values
[0].integer
);
1354 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1355 IPP_TAG_RANGE
)) != NULL
)
1357 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1359 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1360 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1362 send_ipp_status(con
, IPP_BAD_REQUEST
,
1363 _("Bad page-ranges values %d-%d."),
1364 attr
->values
[i
].range
.lower
,
1365 attr
->values
[i
].range
.upper
);
1369 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1374 * Make sure we aren't over our limit...
1377 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1380 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1382 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1383 _("Too many active jobs."));
1387 if ((i
= check_quotas(con
, printer
)) < 0)
1389 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1394 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1399 * Create the job and set things up...
1402 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1403 IPP_TAG_INTEGER
)) != NULL
)
1404 priority
= attr
->values
[0].integer
;
1407 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1408 printer
->options
)) != NULL
)
1409 priority
= atoi(val
);
1413 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1417 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1418 IPP_TAG_NAME
)) != NULL
)
1419 title
= attr
->values
[0].string
.text
;
1421 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1422 title
= "Untitled");
1424 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1426 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1427 _("Unable to add job for destination \"%s\"!"),
1432 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
1433 CUPS_PRINTER_REMOTE
);
1434 job
->attrs
= con
->request
;
1435 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1437 add_job_uuid(con
, job
);
1438 apply_printer_defaults(printer
, job
);
1440 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1442 if (con
->username
[0])
1444 cupsdSetString(&job
->username
, con
->username
);
1447 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1451 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1452 "add_job: requesting-user-name=\"%s\"",
1453 attr
->values
[0].string
.text
);
1455 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1458 cupsdSetString(&job
->username
, "anonymous");
1461 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1462 "job-originating-user-name", NULL
, job
->username
);
1465 attr
->group_tag
= IPP_TAG_JOB
;
1466 _cupsStrFree(attr
->name
);
1467 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1470 if (con
->username
[0] || auth_info
)
1472 save_auth_info(con
, job
, auth_info
);
1475 * Remove the auth-info attribute from the attribute data...
1480 if (job
->attrs
->prev
)
1481 job
->attrs
->prev
->next
= auth_info
->next
;
1483 job
->attrs
->attrs
= auth_info
->next
;
1485 if (job
->attrs
->last
== auth_info
)
1486 job
->attrs
->last
= job
->attrs
->prev
;
1488 _ippFreeAttr(auth_info
);
1492 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1493 IPP_TAG_ZERO
)) != NULL
)
1496 * Request contains a job-originating-host-name attribute; validate it...
1499 if (attr
->value_tag
!= IPP_TAG_NAME
||
1500 attr
->num_values
!= 1 ||
1501 strcmp(con
->http
.hostname
, "localhost"))
1504 * Can't override the value if we aren't connected via localhost.
1505 * Also, we can only have 1 value and it must be a name value.
1508 switch (attr
->value_tag
)
1510 case IPP_TAG_STRING
:
1511 case IPP_TAG_TEXTLANG
:
1512 case IPP_TAG_NAMELANG
:
1515 case IPP_TAG_KEYWORD
:
1517 case IPP_TAG_URISCHEME
:
1518 case IPP_TAG_CHARSET
:
1519 case IPP_TAG_LANGUAGE
:
1520 case IPP_TAG_MIMETYPE
:
1522 * Free old strings...
1525 for (i
= 0; i
< attr
->num_values
; i
++)
1527 _cupsStrFree(attr
->values
[i
].string
.text
);
1528 attr
->values
[i
].string
.text
= NULL
;
1529 if (attr
->values
[i
].string
.charset
)
1531 _cupsStrFree(attr
->values
[i
].string
.charset
);
1532 attr
->values
[i
].string
.charset
= NULL
;
1541 * Use the default connection hostname instead...
1544 attr
->value_tag
= IPP_TAG_NAME
;
1545 attr
->num_values
= 1;
1546 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1549 attr
->group_tag
= IPP_TAG_JOB
;
1554 * No job-originating-host-name attribute, so use the hostname from
1558 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1559 "job-originating-host-name", NULL
, con
->http
.hostname
);
1562 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1564 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1565 "time-at-processing", 0);
1566 attr
->value_tag
= IPP_TAG_NOVALUE
;
1567 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1568 "time-at-completed", 0);
1569 attr
->value_tag
= IPP_TAG_NOVALUE
;
1572 * Add remaining job attributes...
1575 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1576 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1577 "job-state", IPP_JOB_STOPPED
);
1578 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1579 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1580 "job-media-sheets-completed", 0);
1581 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1583 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1586 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1587 IPP_TAG_INTEGER
)) != NULL
)
1588 attr
->values
[0].integer
= 0;
1590 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1593 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1594 IPP_TAG_KEYWORD
)) == NULL
)
1595 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1598 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1599 printer
->options
)) == NULL
)
1602 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1603 "job-hold-until", NULL
, val
);
1605 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1606 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1609 * Hold job until specified time...
1612 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1614 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1615 job
->state_value
= IPP_JOB_HELD
;
1617 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1619 job
->hold_until
= time(NULL
) + 60;
1620 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1621 job
->state_value
= IPP_JOB_HELD
;
1625 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1626 job
->state_value
= IPP_JOB_PENDING
;
1629 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1633 * Add job sheets options...
1636 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1637 IPP_TAG_ZERO
)) == NULL
)
1639 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1640 "Adding default job-sheets values \"%s,%s\"...",
1641 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1643 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1645 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1646 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1649 job
->job_sheets
= attr
;
1652 * Enforce classification level if set...
1657 cupsdLogMessage(CUPSD_LOG_INFO
,
1658 "Classification=\"%s\", ClassifyOverride=%d",
1659 Classification
? Classification
: "(null)",
1662 if (ClassifyOverride
)
1664 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1665 (attr
->num_values
== 1 ||
1666 !strcmp(attr
->values
[1].string
.text
, "none")))
1669 * Force the leading banner to have the classification on it...
1672 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1674 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1675 "job-sheets=\"%s,none\", "
1676 "job-originating-user-name=\"%s\"",
1677 job
->id
, Classification
, job
->username
);
1679 else if (attr
->num_values
== 2 &&
1680 strcmp(attr
->values
[0].string
.text
,
1681 attr
->values
[1].string
.text
) &&
1682 strcmp(attr
->values
[0].string
.text
, "none") &&
1683 strcmp(attr
->values
[1].string
.text
, "none"))
1686 * Can't put two different security markings on the same document!
1689 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1691 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1692 "job-sheets=\"%s,%s\", "
1693 "job-originating-user-name=\"%s\"",
1694 job
->id
, attr
->values
[0].string
.text
,
1695 attr
->values
[1].string
.text
, job
->username
);
1697 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1698 strcmp(attr
->values
[0].string
.text
, "none") &&
1699 (attr
->num_values
== 1 ||
1700 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1701 strcmp(attr
->values
[1].string
.text
, "none"))))
1703 if (attr
->num_values
== 1)
1704 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1705 "[Job %d] CLASSIFICATION OVERRIDDEN "
1706 "job-sheets=\"%s\", "
1707 "job-originating-user-name=\"%s\"",
1708 job
->id
, attr
->values
[0].string
.text
, job
->username
);
1710 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1711 "[Job %d] CLASSIFICATION OVERRIDDEN "
1712 "job-sheets=\"%s,%s\",fffff "
1713 "job-originating-user-name=\"%s\"",
1714 job
->id
, attr
->values
[0].string
.text
,
1715 attr
->values
[1].string
.text
, job
->username
);
1718 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1719 (attr
->num_values
== 1 ||
1720 strcmp(attr
->values
[1].string
.text
, Classification
)))
1723 * Force the banner to have the classification on it...
1726 if (attr
->num_values
> 1 &&
1727 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1729 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1730 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1734 if (attr
->num_values
== 1 ||
1735 strcmp(attr
->values
[0].string
.text
, "none"))
1736 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1738 if (attr
->num_values
> 1 &&
1739 strcmp(attr
->values
[1].string
.text
, "none"))
1740 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1743 if (attr
->num_values
> 1)
1744 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1745 "[Job %d] CLASSIFICATION FORCED "
1746 "job-sheets=\"%s,%s\", "
1747 "job-originating-user-name=\"%s\"",
1748 job
->id
, attr
->values
[0].string
.text
,
1749 attr
->values
[1].string
.text
, job
->username
);
1751 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1752 "[Job %d] CLASSIFICATION FORCED "
1753 "job-sheets=\"%s\", "
1754 "job-originating-user-name=\"%s\"",
1755 job
->id
, Classification
, job
->username
);
1760 * See if we need to add the starting sheet...
1763 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1765 cupsdLogMessage(CUPSD_LOG_INFO
,
1766 "[Job %d] Adding start banner page \"%s\".",
1767 job
->id
, attr
->values
[0].string
.text
);
1769 kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
);
1771 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1774 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1775 IPP_TAG_ZERO
)) != NULL
)
1779 * Fill in the response info...
1782 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1783 LocalPort
, job
->id
);
1785 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1788 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1790 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1792 add_job_state_reasons(con
, job
);
1794 con
->response
->request
.status
.status_code
= IPP_OK
;
1797 * Add any job subscriptions...
1800 add_job_subscriptions(con
, job
);
1803 * Set all but the first two attributes to the job attributes group...
1806 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1807 attr
->group_tag
= IPP_TAG_JOB
;
1810 * Fire the "job created" event...
1813 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1816 * Return the new job...
1824 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1825 * upon the job and printer state...
1829 add_job_state_reasons(
1830 cupsd_client_t
*con
, /* I - Client connection */
1831 cupsd_job_t
*job
) /* I - Job info */
1833 cupsd_printer_t
*dest
; /* Destination printer */
1836 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1837 con
, con
->http
.fd
, job
? job
->id
: 0);
1839 switch (job
? job
->state_value
: IPP_JOB_CANCELED
)
1841 case IPP_JOB_PENDING
:
1842 dest
= cupsdFindDest(job
->dest
);
1844 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1845 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1846 "job-state-reasons", NULL
, "printer-stopped");
1848 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1849 "job-state-reasons", NULL
, "none");
1853 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1854 IPP_TAG_KEYWORD
) != NULL
||
1855 ippFindAttribute(job
->attrs
, "job-hold-until",
1856 IPP_TAG_NAME
) != NULL
)
1857 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1858 "job-state-reasons", NULL
, "job-hold-until-specified");
1860 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1861 "job-state-reasons", NULL
, "job-incoming");
1864 case IPP_JOB_PROCESSING
:
1865 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1866 "job-state-reasons", NULL
, "job-printing");
1869 case IPP_JOB_STOPPED
:
1870 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1871 "job-state-reasons", NULL
, "job-stopped");
1874 case IPP_JOB_CANCELED
:
1875 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1876 "job-state-reasons", NULL
, "job-canceled-by-user");
1879 case IPP_JOB_ABORTED
:
1880 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1881 "job-state-reasons", NULL
, "aborted-by-system");
1884 case IPP_JOB_COMPLETED
:
1885 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1886 "job-state-reasons", NULL
, "job-completed-successfully");
1893 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1897 add_job_subscriptions(
1898 cupsd_client_t
*con
, /* I - Client connection */
1899 cupsd_job_t
*job
) /* I - Newly created job */
1901 int i
; /* Looping var */
1902 ipp_attribute_t
*prev
, /* Previous attribute */
1903 *next
, /* Next attribute */
1904 *attr
; /* Current attribute */
1905 cupsd_subscription_t
*sub
; /* Subscription object */
1906 const char *recipient
, /* notify-recipient-uri */
1907 *pullmethod
; /* notify-pull-method */
1908 ipp_attribute_t
*user_data
; /* notify-user-data */
1909 int interval
; /* notify-time-interval */
1910 unsigned mask
; /* notify-events */
1914 * Find the first subscription group attribute; return if we have
1918 for (attr
= job
->attrs
->attrs
, prev
= NULL
;
1920 prev
= attr
, attr
= attr
->next
)
1921 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1928 * Process the subscription attributes in the request...
1937 mask
= CUPSD_EVENT_NONE
;
1939 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1941 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
1942 attr
->value_tag
== IPP_TAG_URI
)
1943 recipient
= attr
->values
[0].string
.text
;
1944 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1945 attr
->value_tag
== IPP_TAG_KEYWORD
)
1946 pullmethod
= attr
->values
[0].string
.text
;
1947 else if (!strcmp(attr
->name
, "notify-charset") &&
1948 attr
->value_tag
== IPP_TAG_CHARSET
&&
1949 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
1950 strcmp(attr
->values
[0].string
.text
, "utf-8"))
1952 send_ipp_status(con
, IPP_CHARSET
,
1953 _("Character set \"%s\" not supported!"),
1954 attr
->values
[0].string
.text
);
1957 else if (!strcmp(attr
->name
, "notify-natural-language") &&
1958 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
1959 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
1961 send_ipp_status(con
, IPP_CHARSET
,
1962 _("Language \"%s\" not supported!"),
1963 attr
->values
[0].string
.text
);
1966 else if (!strcmp(attr
->name
, "notify-user-data") &&
1967 attr
->value_tag
== IPP_TAG_STRING
)
1969 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
1971 send_ipp_status(con
, IPP_REQUEST_VALUE
,
1972 _("The notify-user-data value is too large "
1973 "(%d > 63 octets)!"),
1974 attr
->values
[0].unknown
.length
);
1980 else if (!strcmp(attr
->name
, "notify-events") &&
1981 attr
->value_tag
== IPP_TAG_KEYWORD
)
1983 for (i
= 0; i
< attr
->num_values
; i
++)
1984 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
1986 else if (!strcmp(attr
->name
, "notify-lease-duration"))
1988 send_ipp_status(con
, IPP_BAD_REQUEST
,
1989 _("The notify-lease-duration attribute cannot be "
1990 "used with job subscriptions."));
1993 else if (!strcmp(attr
->name
, "notify-time-interval") &&
1994 attr
->value_tag
== IPP_TAG_INTEGER
)
1995 interval
= attr
->values
[0].integer
;
2000 if (!recipient
&& !pullmethod
)
2003 if (mask
== CUPSD_EVENT_NONE
)
2004 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2006 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
2009 sub
->interval
= interval
;
2011 cupsdSetString(&sub
->owner
, job
->username
);
2015 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2016 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2017 sub
->user_data_len
);
2020 ippAddSeparator(con
->response
);
2021 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2022 "notify-subscription-id", sub
->id
);
2028 cupsdSaveAllSubscriptions();
2031 * Remove all of the subscription attributes from the job request...
2034 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2038 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2039 attr
->group_tag
== IPP_TAG_ZERO
)
2042 * Free and remove this attribute...
2050 job
->attrs
->attrs
= next
;
2056 job
->attrs
->last
= prev
;
2057 job
->attrs
->current
= prev
;
2062 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2064 * See RFC 4122 for the definition of UUIDs and the format.
2068 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
2069 cupsd_job_t
*job
) /* I - Job */
2071 char uuid
[1024]; /* job-uuid string */
2072 _cups_md5_state_t md5state
; /* MD5 state */
2073 unsigned char md5sum
[16]; /* MD5 digest/sum */
2077 * First see if the job already has a job-uuid attribute; if so, return...
2080 if (ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2084 * No job-uuid attribute, so build a version 3 UUID with the local job
2085 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
2086 * the ServerName, server name and port that the client connected to,
2087 * and local job ID...
2090 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
2091 con
->serverport
, job
->id
);
2093 _cupsMD5Init(&md5state
);
2094 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
2095 _cupsMD5Finish(&md5state
, md5sum
);
2098 * Format the UUID URI using the MD5 sum and job ID.
2101 snprintf(uuid
, sizeof(uuid
),
2102 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2103 "%02x%02x%02x%02x%02x%02x",
2104 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
2105 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
2106 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
2107 md5sum
[14], md5sum
[15]);
2109 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
2114 * 'add_printer()' - Add a printer to the system.
2118 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2119 ipp_attribute_t
*uri
) /* I - URI of printer */
2121 http_status_t status
; /* Policy status */
2122 int i
; /* Looping var */
2123 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2124 username
[HTTP_MAX_URI
], /* Username portion of URI */
2125 host
[HTTP_MAX_URI
], /* Host portion of URI */
2126 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2127 int port
; /* Port portion of URI */
2128 cupsd_printer_t
*printer
; /* Printer/class */
2129 ipp_attribute_t
*attr
; /* Printer attribute */
2130 cups_file_t
*fp
; /* Script/PPD file */
2131 char line
[1024]; /* Line from file... */
2132 char srcfile
[1024], /* Source Script/PPD file */
2133 dstfile
[1024]; /* Destination Script/PPD file */
2134 int modify
; /* Non-zero if we are modifying */
2135 char newname
[IPP_MAX_NAME
]; /* New printer name */
2136 int need_restart_job
; /* Need to restart job? */
2137 int set_device_uri
, /* Did we set the device URI? */
2138 set_port_monitor
; /* Did we set the port monitor? */
2141 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2142 con
->http
.fd
, uri
->values
[0].string
.text
);
2145 * Do we have a valid URI?
2148 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2149 sizeof(scheme
), username
, sizeof(username
), host
,
2150 sizeof(host
), &port
, resource
, sizeof(resource
));
2152 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2155 * No, return an error...
2158 send_ipp_status(con
, IPP_BAD_REQUEST
,
2159 _("The printer-uri must be of the form "
2160 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2165 * Do we have a valid printer name?
2168 if (!validate_name(resource
+ 10))
2171 * No, return an error...
2174 send_ipp_status(con
, IPP_BAD_REQUEST
,
2175 _("The printer-uri \"%s\" contains invalid characters."),
2176 uri
->values
[0].string
.text
);
2184 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2186 send_http_error(con
, status
, NULL
);
2191 * See if the printer already exists; if not, create a new printer...
2194 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2197 * Printer doesn't exist; see if we have a class of the same name...
2200 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2201 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
2204 * Yes, return an error...
2207 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2208 _("A class named \"%s\" already exists!"),
2214 * No, add the printer...
2217 printer
= cupsdAddPrinter(resource
+ 10);
2220 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2223 * Rename the implicit printer to "AnyPrinter" or delete it...
2226 if (ImplicitAnyClasses
)
2228 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2229 cupsdRenamePrinter(printer
, newname
);
2232 cupsdDeletePrinter(printer
, 1);
2235 * Add the printer as a new local printer...
2238 printer
= cupsdAddPrinter(resource
+ 10);
2241 else if (printer
->type
& CUPS_PRINTER_DISCOVERED
)
2244 * Rename the remote printer to "Printer@server"...
2247 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2249 cupsdRenamePrinter(printer
, newname
);
2252 * Add the printer as a new local printer...
2255 printer
= cupsdAddPrinter(resource
+ 10);
2262 * Look for attributes and copy them over as needed...
2265 need_restart_job
= 0;
2267 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2268 IPP_TAG_TEXT
)) != NULL
)
2269 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2271 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2272 IPP_TAG_TEXT
)) != NULL
)
2273 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2277 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2278 IPP_TAG_URI
)) != NULL
)
2281 * Do we have a valid device URI?
2284 need_restart_job
= 1;
2286 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, scheme
,
2287 sizeof(scheme
), username
, sizeof(username
), host
,
2288 sizeof(host
), &port
, resource
, sizeof(resource
));
2290 if (!strcmp(scheme
, "file"))
2293 * See if the administrator has enabled file devices...
2296 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2299 * File devices are disabled and the URL is not file:/dev/null...
2302 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2303 _("File device URIs have been disabled! "
2304 "To enable, see the FileDevice directive in "
2305 "\"%s/cupsd.conf\"."),
2313 * See if the backend exists and is executable...
2316 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2317 if (access(srcfile
, X_OK
))
2320 * Could not find device in list!
2323 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2324 attr
->values
[0].string
.text
);
2329 cupsdLogMessage(CUPSD_LOG_INFO
,
2330 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2332 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2334 cupsdSanitizeURI(printer
->device_uri
, resource
,
2337 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2341 set_port_monitor
= 0;
2343 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2344 IPP_TAG_NAME
)) != NULL
)
2346 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2349 need_restart_job
= 1;
2351 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2353 for (i
= 0; i
< supported
->num_values
; i
++)
2354 if (!strcmp(supported
->values
[i
].string
.text
,
2355 attr
->values
[0].string
.text
))
2358 if (i
>= supported
->num_values
)
2360 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2361 attr
->values
[0].string
.text
);
2365 cupsdLogMessage(CUPSD_LOG_INFO
,
2366 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2367 printer
->name
, attr
->values
[0].string
.text
,
2368 printer
->port_monitor
? printer
->port_monitor
: "none");
2370 if (strcmp(attr
->values
[0].string
.text
, "none"))
2371 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2373 cupsdClearString(&printer
->port_monitor
);
2375 set_port_monitor
= 1;
2378 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2379 IPP_TAG_BOOLEAN
)) != NULL
)
2381 cupsdLogMessage(CUPSD_LOG_INFO
,
2382 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2383 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2385 printer
->accepting
= attr
->values
[0].boolean
;
2386 cupsdAddPrinterHistory(printer
);
2389 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2390 IPP_TAG_BOOLEAN
)) != NULL
)
2392 if (printer
->shared
&& !attr
->values
[0].boolean
)
2393 cupsdDeregisterPrinter(printer
, 1);
2395 cupsdLogMessage(CUPSD_LOG_INFO
,
2396 "Setting %s printer-is-shared to %d (was %d.)",
2397 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2399 printer
->shared
= attr
->values
[0].boolean
;
2402 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2403 IPP_TAG_ENUM
)) != NULL
)
2405 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2406 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2408 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2409 attr
->values
[0].integer
);
2413 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2414 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2416 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2417 cupsdStopPrinter(printer
, 0);
2420 need_restart_job
= 1;
2421 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2424 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2425 IPP_TAG_TEXT
)) != NULL
)
2427 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2428 sizeof(printer
->state_message
));
2429 cupsdAddPrinterHistory(printer
);
2432 set_printer_defaults(con
, printer
);
2434 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2435 IPP_TAG_KEYWORD
)) != NULL
)
2436 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2439 * See if we have all required attributes...
2442 if (!printer
->device_uri
)
2443 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2446 * See if we have an interface script or PPD file attached to the request...
2451 need_restart_job
= 1;
2453 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2455 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2458 * Yes; get the first line from it...
2462 cupsFileGets(fp
, line
, sizeof(line
));
2466 * Then see what kind of file it is...
2469 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2472 if (!strncmp(line
, "*PPD-Adobe", 10))
2475 * The new file is a PPD file, so remove any old interface script
2476 * that might be lying around...
2484 * This must be an interface script, so move the file over to the
2485 * interfaces directory and make it executable...
2488 if (copy_file(srcfile
, dstfile
))
2490 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2491 _("Unable to copy interface script - %s!"),
2497 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2498 "Copied interface script successfully!");
2499 chmod(dstfile
, 0755);
2503 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2506 if (!strncmp(line
, "*PPD-Adobe", 10))
2509 * The new file is a PPD file, so move the file over to the
2510 * ppd directory and make it readable by all...
2513 if (copy_file(srcfile
, dstfile
))
2515 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2516 _("Unable to copy PPD file - %s!"),
2522 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2523 "Copied PPD file successfully!");
2524 chmod(dstfile
, 0644);
2530 * This must be an interface script, so remove any old PPD file that
2531 * may be lying around...
2538 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2539 IPP_TAG_NAME
)) != NULL
)
2541 need_restart_job
= 1;
2543 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2546 * Raw driver, remove any existing PPD or interface script files.
2549 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2553 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2563 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2567 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2570 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2572 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2577 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2578 "Copied PPD file successfully!");
2579 chmod(dstfile
, 0644);
2585 * If we set the device URI but not the port monitor, check which port
2586 * monitor to use by default...
2589 if (set_device_uri
&& !set_port_monitor
)
2591 ppd_file_t
*ppd
; /* PPD file */
2592 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2595 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2596 sizeof(scheme
), username
, sizeof(username
), host
,
2597 sizeof(host
), &port
, resource
, sizeof(resource
));
2599 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2601 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2603 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2605 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2606 if (!strcmp(scheme
, ppdattr
->spec
))
2608 cupsdLogMessage(CUPSD_LOG_INFO
,
2609 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2610 printer
->name
, ppdattr
->value
,
2611 printer
->port_monitor
? printer
->port_monitor
2614 if (strcmp(ppdattr
->value
, "none"))
2615 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2617 cupsdClearString(&printer
->port_monitor
);
2627 * Update the printer attributes and return...
2630 cupsdSetPrinterAttrs(printer
);
2631 cupsdSaveAllPrinters();
2633 if (need_restart_job
&& printer
->job
)
2638 * Stop the current job and then restart it below...
2641 job
= (cupsd_job_t
*)printer
->job
;
2643 cupsdStopJob(job
, 1);
2645 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2646 job
->state_value
= IPP_JOB_PENDING
;
2649 if (need_restart_job
)
2652 cupsdWritePrintcap();
2656 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2657 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2660 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2661 printer
->name
, get_username(con
));
2665 cupsdAddPrinterHistory(printer
);
2667 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2668 "New printer \"%s\" added by \"%s\".", printer
->name
,
2671 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2672 printer
->name
, get_username(con
));
2675 con
->response
->request
.status
.status_code
= IPP_OK
;
2680 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2681 * based upon the printer state...
2685 add_printer_state_reasons(
2686 cupsd_client_t
*con
, /* I - Client connection */
2687 cupsd_printer_t
*p
) /* I - Printer info */
2689 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2690 "add_printer_state_reasons(%p[%d], %p[%s])",
2691 con
, con
->http
.fd
, p
, p
->name
);
2693 if (p
->num_reasons
== 0)
2694 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2695 "printer-state-reasons", NULL
,
2696 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2698 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2699 "printer-state-reasons", p
->num_reasons
, NULL
,
2700 (const char * const *)p
->reasons
);
2705 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2706 * the specified printer or class.
2710 add_queued_job_count(
2711 cupsd_client_t
*con
, /* I - Client connection */
2712 cupsd_printer_t
*p
) /* I - Printer or class */
2714 int count
; /* Number of jobs on destination */
2717 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2718 con
, con
->http
.fd
, p
, p
->name
);
2720 count
= cupsdGetPrinterJobCount(p
->name
);
2722 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2723 "queued-job-count", count
);
2728 * 'apply_printer_defaults()' - Apply printer default options to a job.
2732 apply_printer_defaults(
2733 cupsd_printer_t
*printer
, /* I - Printer */
2734 cupsd_job_t
*job
) /* I - Job */
2736 int i
, /* Looping var */
2737 num_options
; /* Number of default options */
2738 cups_option_t
*options
, /* Default options */
2739 *option
; /* Current option */
2743 * Collect all of the default options and add the missing ones to the
2747 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
2750 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2752 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2757 * Encode these options as attributes in the job object...
2760 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2761 cupsFreeOptions(num_options
, options
);
2766 * 'authenticate_job()' - Set job authentication info.
2770 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2771 ipp_attribute_t
*uri
) /* I - Job URI */
2773 ipp_attribute_t
*attr
, /* job-id attribute */
2774 *auth_info
; /* auth-info attribute */
2775 int jobid
; /* Job ID */
2776 cupsd_job_t
*job
; /* Current job */
2777 char method
[HTTP_MAX_URI
],
2778 /* Method portion of URI */
2779 username
[HTTP_MAX_URI
],
2780 /* Username portion of URI */
2782 /* Host portion of URI */
2783 resource
[HTTP_MAX_URI
];
2784 /* Resource portion of URI */
2785 int port
; /* Port portion of URI */
2788 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2789 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
2792 * Start with "everything is OK" status...
2795 con
->response
->request
.status
.status_code
= IPP_OK
;
2798 * See if we have a job URI or a printer URI...
2801 if (!strcmp(uri
->name
, "printer-uri"))
2804 * Got a printer URI; see if we also have a job-id attribute...
2807 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2808 IPP_TAG_INTEGER
)) == NULL
)
2810 send_ipp_status(con
, IPP_BAD_REQUEST
,
2811 _("Got a printer-uri attribute but no job-id!"));
2815 jobid
= attr
->values
[0].integer
;
2820 * Got a job URI; parse it to get the job ID...
2823 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2824 sizeof(method
), username
, sizeof(username
), host
,
2825 sizeof(host
), &port
, resource
, sizeof(resource
));
2827 if (strncmp(resource
, "/jobs/", 6))
2833 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
2834 uri
->values
[0].string
.text
);
2838 jobid
= atoi(resource
+ 6);
2842 * See if the job exists...
2845 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2848 * Nope - return a "not found" error...
2851 send_ipp_status(con
, IPP_NOT_FOUND
,
2852 _("Job #%d does not exist!"), jobid
);
2857 * See if the job has been completed...
2860 if (job
->state_value
!= IPP_JOB_HELD
)
2863 * Return a "not-possible" error...
2866 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2867 _("Job #%d is not held for authentication!"),
2873 * See if we have already authenticated...
2876 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
2878 if (!con
->username
[0] && !auth_info
)
2880 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
2881 _("No authentication information provided!"));
2886 * See if the job is owned by the requesting user...
2889 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
2891 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
2896 * Save the authentication information for this job...
2899 save_auth_info(con
, job
, auth_info
);
2902 * Reset the job-hold-until value to "no-hold"...
2905 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2906 IPP_TAG_KEYWORD
)) == NULL
)
2907 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2911 attr
->value_tag
= IPP_TAG_KEYWORD
;
2912 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2916 * Release the job and return...
2919 cupsdReleaseJob(job
);
2921 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Authenticated by \"%s\".", jobid
,
2927 * 'cancel_all_jobs()' - Cancel all print jobs.
2931 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
2932 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2934 http_status_t status
; /* Policy status */
2935 cups_ptype_t dtype
; /* Destination type */
2936 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2937 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2938 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
2939 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2940 int port
; /* Port portion of URI */
2941 ipp_attribute_t
*attr
; /* Attribute in request */
2942 const char *username
; /* Username */
2943 int purge
; /* Purge? */
2944 cupsd_printer_t
*printer
; /* Printer */
2947 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
2948 con
->http
.fd
, uri
->values
[0].string
.text
);
2951 * See if we have a printer URI...
2954 if (strcmp(uri
->name
, "printer-uri"))
2956 send_ipp_status(con
, IPP_BAD_REQUEST
,
2957 _("The printer-uri attribute is required!"));
2962 * Get the username (if any) for the jobs we want to cancel (only if
2963 * "my-jobs" is specified...
2966 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
2967 IPP_TAG_BOOLEAN
)) != NULL
&&
2968 attr
->values
[0].boolean
)
2970 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
2971 IPP_TAG_NAME
)) != NULL
)
2972 username
= attr
->values
[0].string
.text
;
2975 send_ipp_status(con
, IPP_BAD_REQUEST
,
2976 _("Missing requesting-user-name attribute!"));
2984 * Look for the "purge-jobs" attribute...
2987 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
2988 IPP_TAG_BOOLEAN
)) != NULL
)
2989 purge
= attr
->values
[0].boolean
;
2994 * And if the destination is valid...
2997 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3003 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3004 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3005 hostname
, sizeof(hostname
), &port
,
3006 resource
, sizeof(resource
));
3008 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3009 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3011 send_ipp_status(con
, IPP_NOT_FOUND
,
3012 _("The printer or class was not found."));
3020 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3022 send_http_error(con
, status
, NULL
);
3027 * Cancel all jobs on all printers...
3030 cupsdCancelJobs(NULL
, username
, purge
);
3032 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3033 purge
? "purged" : "canceled", get_username(con
));
3041 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3044 send_http_error(con
, status
, printer
);
3049 * Cancel all of the jobs on the named printer...
3052 cupsdCancelJobs(printer
->name
, username
, purge
);
3054 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3055 printer
->name
, purge
? "purged" : "canceled",
3059 con
->response
->request
.status
.status_code
= IPP_OK
;
3064 * 'cancel_job()' - Cancel a print job.
3068 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3069 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3071 ipp_attribute_t
*attr
; /* Current attribute */
3072 int jobid
; /* Job ID */
3073 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3074 username
[HTTP_MAX_URI
], /* Username portion of URI */
3075 host
[HTTP_MAX_URI
], /* Host portion of URI */
3076 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3077 int port
; /* Port portion of URI */
3078 cupsd_job_t
*job
; /* Job information */
3079 cups_ptype_t dtype
; /* Destination type (printer/class) */
3080 cupsd_printer_t
*printer
; /* Printer data */
3083 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3084 con
->http
.fd
, uri
->values
[0].string
.text
);
3087 * See if we have a job URI or a printer URI...
3090 if (!strcmp(uri
->name
, "printer-uri"))
3093 * Got a printer URI; see if we also have a job-id attribute...
3096 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3097 IPP_TAG_INTEGER
)) == NULL
)
3099 send_ipp_status(con
, IPP_BAD_REQUEST
,
3100 _("Got a printer-uri attribute but no job-id!"));
3104 if ((jobid
= attr
->values
[0].integer
) == 0)
3107 * Find the current job on the specified printer...
3110 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3116 send_ipp_status(con
, IPP_NOT_FOUND
,
3117 _("The printer or class was not found."));
3122 * See if the printer is currently printing a job...
3126 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3130 * No, see if there are any pending jobs...
3133 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3135 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3136 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3137 !strcasecmp(job
->dest
, printer
->name
))
3144 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3154 * Got a job URI; parse it to get the job ID...
3157 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3158 sizeof(scheme
), username
, sizeof(username
), host
,
3159 sizeof(host
), &port
, resource
, sizeof(resource
));
3161 if (strncmp(resource
, "/jobs/", 6))
3167 send_ipp_status(con
, IPP_BAD_REQUEST
,
3168 _("Bad job-uri attribute \"%s\"!"),
3169 uri
->values
[0].string
.text
);
3173 jobid
= atoi(resource
+ 6);
3177 * See if the job exists...
3180 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3183 * Nope - return a "not found" error...
3186 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3191 * See if the job is owned by the requesting user...
3194 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3196 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
3201 * See if the job is already completed, canceled, or aborted; if so,
3202 * we can't cancel...
3205 if (job
->state_value
>= IPP_JOB_CANCELED
)
3207 switch (job
->state_value
)
3209 case IPP_JOB_CANCELED
:
3210 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3211 _("Job #%d is already canceled - can\'t cancel."),
3215 case IPP_JOB_ABORTED
:
3216 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3217 _("Job #%d is already aborted - can\'t cancel."),
3222 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3223 _("Job #%d is already completed - can\'t cancel."),
3232 * Cancel the job and return...
3235 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
3238 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3241 con
->response
->request
.status
.status_code
= IPP_OK
;
3246 * 'cancel_subscription()' - Cancel a subscription.
3250 cancel_subscription(
3251 cupsd_client_t
*con
, /* I - Client connection */
3252 int sub_id
) /* I - Subscription ID */
3254 http_status_t status
; /* Policy status */
3255 cupsd_subscription_t
*sub
; /* Subscription */
3258 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3259 "cancel_subscription(con=%p[%d], sub_id=%d)",
3260 con
, con
->http
.fd
, sub_id
);
3263 * Is the subscription ID valid?
3266 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3269 * Bad subscription ID...
3272 send_ipp_status(con
, IPP_NOT_FOUND
,
3273 _("notify-subscription-id %d no good!"), sub_id
);
3281 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3283 con
, sub
->owner
)) != HTTP_OK
)
3285 send_http_error(con
, status
, sub
->dest
);
3290 * Cancel the subscription...
3293 cupsdDeleteSubscription(sub
, 1);
3295 con
->response
->request
.status
.status_code
= IPP_OK
;
3300 * 'check_quotas()' - Check quotas for a printer and user.
3303 static int /* O - 1 if OK, 0 if not */
3304 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3305 cupsd_printer_t
*p
) /* I - Printer or class */
3307 int i
; /* Looping var */
3308 char username
[33]; /* Username */
3309 cupsd_quota_t
*q
; /* Quota data */
3310 #ifdef HAVE_MBR_UID_TO_UUID
3312 * Use Apple membership APIs which require that all names represent
3313 * valid user account or group records accessible by the server.
3316 uuid_t usr_uuid
; /* UUID for job requesting user */
3317 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3318 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3319 int mbr_err
; /* Error from membership function */
3320 int is_member
; /* Is this user a member? */
3323 * Use standard POSIX APIs for checking users and groups...
3326 struct passwd
*pw
; /* User password data */
3327 #endif /* HAVE_MBR_UID_TO_UUID */
3330 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3331 con
, con
->http
.fd
, p
, p
->name
);
3341 * Figure out who is printing...
3344 strlcpy(username
, get_username(con
), sizeof(username
));
3347 * Check global active job limits for printers and users...
3350 if (MaxJobsPerPrinter
)
3353 * Check if there are too many pending jobs on this printer...
3356 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3358 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3367 * Check if there are too many pending jobs for this user...
3370 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3372 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3379 * Check against users...
3382 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3387 #ifdef HAVE_MBR_UID_TO_UUID
3389 * Get UUID for job requesting user...
3392 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3398 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3399 "check_quotas: UUID lookup failed for user \"%s\"",
3401 cupsdLogMessage(CUPSD_LOG_INFO
,
3402 "Denying user \"%s\" access to printer \"%s\" "
3403 "(unknown user)...",
3409 * Get UID and GID of requesting user...
3412 pw
= getpwnam(username
);
3414 #endif /* HAVE_MBR_UID_TO_UUID */
3416 for (i
= 0; i
< p
->num_users
; i
++)
3417 if (p
->users
[i
][0] == '@')
3420 * Check group membership...
3423 #ifdef HAVE_MBR_UID_TO_UUID
3424 if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
3428 * Invalid ACL entries are ignored for matching; just record a
3429 * warning in the log...
3432 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3433 "check_quotas: UUID lookup failed for ACL entry "
3434 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3435 cupsdLogMessage(CUPSD_LOG_WARN
,
3436 "Access control entry \"%s\" not a valid group name; "
3437 "entry ignored", p
->users
[i
]);
3441 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3445 * At this point, there should be no errors, but check anyways...
3448 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3449 "check_quotas: group \"%s\" membership check "
3450 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
3455 * Stop if we found a match...
3462 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3464 #endif /* HAVE_MBR_UID_TO_UUID */
3466 #ifdef HAVE_MBR_UID_TO_UUID
3469 if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
3473 * Invalid ACL entries are ignored for matching; just record a
3474 * warning in the log...
3477 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3478 "check_quotas: UUID lookup failed for ACL entry "
3479 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3480 cupsdLogMessage(CUPSD_LOG_WARN
,
3481 "Access control entry \"%s\" not a valid user name; "
3482 "entry ignored", p
->users
[i
]);
3486 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
3489 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3490 "check_quotas: User \"%s\" identity check failed "
3491 "(err=%d)", p
->users
[i
], mbr_err
);
3500 else if (!strcasecmp(username
, p
->users
[i
]))
3502 #endif /* HAVE_MBR_UID_TO_UUID */
3504 if ((i
< p
->num_users
) == p
->deny_users
)
3506 cupsdLogMessage(CUPSD_LOG_INFO
,
3507 "Denying user \"%s\" access to printer \"%s\"...",
3518 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
3521 * TODO: Define these special page count values as constants!
3524 if (q
->page_count
== -4) /* special case: unlimited user */
3526 cupsdLogMessage(CUPSD_LOG_INFO
,
3527 "User \"%s\" request approved for printer %s (%s): "
3529 username
, p
->name
, p
->info
);
3530 q
->page_count
= 0; /* allow user to print */
3533 else if (q
->page_count
== -3) /* quota exceeded */
3535 cupsdLogMessage(CUPSD_LOG_INFO
,
3536 "User \"%s\" request denied for printer %s (%s): "
3537 "quota limit exceeded.",
3538 username
, p
->name
, p
->info
);
3539 q
->page_count
= 2; /* force quota exceeded failure */
3542 else if (q
->page_count
== -2) /* quota disabled for user */
3544 cupsdLogMessage(CUPSD_LOG_INFO
,
3545 "User \"%s\" request denied for printer %s (%s): "
3546 "printing disabled for user.",
3547 username
, p
->name
, p
->info
);
3548 q
->page_count
= 2; /* force quota exceeded failure */
3551 else if (q
->page_count
== -1) /* quota access error */
3553 cupsdLogMessage(CUPSD_LOG_INFO
,
3554 "User \"%s\" request denied for printer %s (%s): "
3555 "unable to determine quota limit.",
3556 username
, p
->name
, p
->info
);
3557 q
->page_count
= 2; /* force quota exceeded failure */
3560 else if (q
->page_count
< 0) /* user not found or other error */
3562 cupsdLogMessage(CUPSD_LOG_INFO
,
3563 "User \"%s\" request denied for printer %s (%s): "
3564 "user disabled / missing quota.",
3565 username
, p
->name
, p
->info
);
3566 q
->page_count
= 2; /* force quota exceeded failure */
3569 else /* page within user limits */
3571 q
->page_count
= 0; /* allow user to print */
3576 #endif /* __APPLE__ */
3577 if (p
->k_limit
|| p
->page_limit
)
3579 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3581 cupsdLogMessage(CUPSD_LOG_ERROR
,
3582 "Unable to allocate quota data for user \"%s\"!",
3587 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3588 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3590 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3597 * If we have gotten this far, we're done!
3605 * 'copy_attribute()' - Copy a single attribute.
3608 static ipp_attribute_t
* /* O - New attribute */
3610 ipp_t
*to
, /* O - Destination request/response */
3611 ipp_attribute_t
*attr
, /* I - Attribute to copy */
3612 int quickcopy
) /* I - Do a quick copy? */
3614 int i
; /* Looping var */
3615 ipp_attribute_t
*toattr
; /* Destination attribute */
3618 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3619 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
3620 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
3623 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
3626 toattr
= ippAddSeparator(to
);
3629 case IPP_TAG_INTEGER
:
3631 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3632 attr
->name
, attr
->num_values
, NULL
);
3634 for (i
= 0; i
< attr
->num_values
; i
++)
3635 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
3638 case IPP_TAG_BOOLEAN
:
3639 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
3640 attr
->num_values
, NULL
);
3642 for (i
= 0; i
< attr
->num_values
; i
++)
3643 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
3646 case IPP_TAG_STRING
:
3649 case IPP_TAG_KEYWORD
:
3651 case IPP_TAG_URISCHEME
:
3652 case IPP_TAG_CHARSET
:
3653 case IPP_TAG_LANGUAGE
:
3654 case IPP_TAG_MIMETYPE
:
3655 toattr
= ippAddStrings(to
, attr
->group_tag
,
3656 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3657 attr
->name
, attr
->num_values
, NULL
, NULL
);
3661 for (i
= 0; i
< attr
->num_values
; i
++)
3662 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3666 for (i
= 0; i
< attr
->num_values
; i
++)
3667 toattr
->values
[i
].string
.text
=
3668 _cupsStrAlloc(attr
->values
[i
].string
.text
);
3673 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
3674 attr
->values
[0].date
);
3677 case IPP_TAG_RESOLUTION
:
3678 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
3679 attr
->num_values
, IPP_RES_PER_INCH
,
3682 for (i
= 0; i
< attr
->num_values
; i
++)
3684 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
3685 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
3686 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
3690 case IPP_TAG_RANGE
:
3691 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
3692 attr
->num_values
, NULL
, NULL
);
3694 for (i
= 0; i
< attr
->num_values
; i
++)
3696 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
3697 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
3701 case IPP_TAG_TEXTLANG
:
3702 case IPP_TAG_NAMELANG
:
3703 toattr
= ippAddStrings(to
, attr
->group_tag
,
3704 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3705 attr
->name
, attr
->num_values
, NULL
, NULL
);
3709 for (i
= 0; i
< attr
->num_values
; i
++)
3711 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
3712 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3717 for (i
= 0; i
< attr
->num_values
; i
++)
3720 toattr
->values
[i
].string
.charset
=
3721 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
3723 toattr
->values
[i
].string
.charset
=
3724 toattr
->values
[0].string
.charset
;
3726 toattr
->values
[i
].string
.text
=
3727 _cupsStrAlloc(attr
->values
[i
].string
.text
);
3732 case IPP_TAG_BEGIN_COLLECTION
:
3733 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
3734 attr
->num_values
, NULL
);
3736 for (i
= 0; i
< attr
->num_values
; i
++)
3738 toattr
->values
[i
].collection
= ippNew();
3739 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
3740 NULL
, IPP_TAG_ZERO
, 0);
3745 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3746 attr
->name
, attr
->num_values
, NULL
);
3748 for (i
= 0; i
< attr
->num_values
; i
++)
3750 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
3752 if (toattr
->values
[i
].unknown
.length
> 0)
3754 if ((toattr
->values
[i
].unknown
.data
=
3755 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
3756 toattr
->values
[i
].unknown
.length
= 0;
3758 memcpy(toattr
->values
[i
].unknown
.data
,
3759 attr
->values
[i
].unknown
.data
,
3760 toattr
->values
[i
].unknown
.length
);
3763 break; /* anti-compiler-warning-code */
3771 * 'copy_attrs()' - Copy attributes from one request to another.
3775 copy_attrs(ipp_t
*to
, /* I - Destination request */
3776 ipp_t
*from
, /* I - Source request */
3777 cups_array_t
*ra
, /* I - Requested attributes */
3778 ipp_tag_t group
, /* I - Group to copy */
3779 int quickcopy
) /* I - Do a quick copy? */
3781 ipp_attribute_t
*fromattr
; /* Source attribute */
3784 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3785 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3786 to
, from
, ra
, group
, quickcopy
);
3791 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3794 * Filter attributes as needed...
3797 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3798 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
3801 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
3802 copy_attribute(to
, fromattr
, quickcopy
);
3808 * 'copy_banner()' - Copy a banner file to the requests directory for the
3812 static int /* O - Size of banner file in kbytes */
3813 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
3814 cupsd_job_t
*job
, /* I - Job information */
3815 const char *name
) /* I - Name of banner */
3817 int i
; /* Looping var */
3818 int kbytes
; /* Size of banner file in kbytes */
3819 char filename
[1024]; /* Job filename */
3820 cupsd_banner_t
*banner
; /* Pointer to banner */
3821 cups_file_t
*in
; /* Input file */
3822 cups_file_t
*out
; /* Output file */
3823 int ch
; /* Character from file */
3824 char attrname
[255], /* Name of attribute */
3825 *s
; /* Pointer into name */
3826 ipp_attribute_t
*attr
; /* Attribute */
3829 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
3830 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
3831 name
? name
: "(null)");
3834 * Find the banner; return if not found or "none"...
3837 if (!name
|| !strcmp(name
, "none") ||
3838 (banner
= cupsdFindBanner(name
)) == NULL
)
3842 * Open the banner and job files...
3845 if (add_file(con
, job
, banner
->filetype
, 0))
3848 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
3850 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
3852 cupsdLogMessage(CUPSD_LOG_ERROR
,
3853 "copy_banner: Unable to create banner job file %s - %s",
3854 filename
, strerror(errno
));
3859 fchmod(cupsFileNumber(out
), 0640);
3860 fchown(cupsFileNumber(out
), RunUser
, Group
);
3863 * Try the localized banner file under the subdirectory...
3866 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
3868 if (strlen(attrname
) > 2 && attrname
[2] == '-')
3871 * Convert ll-cc to ll_CC...
3875 attrname
[3] = toupper(attrname
[3] & 255);
3876 attrname
[4] = toupper(attrname
[4] & 255);
3879 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3882 if (access(filename
, 0) && strlen(attrname
) > 2)
3885 * Wasn't able to find "ll_CC" locale file; try the non-national
3886 * localization banner directory.
3891 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3895 if (access(filename
, 0))
3898 * Use the non-localized banner file.
3901 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
3904 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
3908 cupsdLogMessage(CUPSD_LOG_ERROR
,
3909 "copy_banner: Unable to open banner template file %s - %s",
3910 filename
, strerror(errno
));
3916 * Parse the file to the end...
3919 while ((ch
= cupsFileGetChar(in
)) != EOF
)
3923 * Get an attribute name...
3926 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
3927 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
3929 else if (s
< (attrname
+ sizeof(attrname
) - 1))
3939 * Ignore { followed by stuff that is not an attribute name...
3942 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
3947 * See if it is defined...
3950 if (attrname
[0] == '?')
3955 if (!strcmp(s
, "printer-name"))
3957 cupsFilePuts(out
, job
->dest
);
3960 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
3963 * See if we have a leading question mark...
3966 if (attrname
[0] != '?')
3969 * Nope, write to file as-is; probably a PostScript procedure...
3972 cupsFilePrintf(out
, "{%s}", attrname
);
3979 * Output value(s)...
3982 for (i
= 0; i
< attr
->num_values
; i
++)
3985 cupsFilePutChar(out
, ',');
3987 switch (attr
->value_tag
)
3989 case IPP_TAG_INTEGER
:
3991 if (!strncmp(s
, "time-at-", 8))
3992 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
3994 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
3997 case IPP_TAG_BOOLEAN
:
3998 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4001 case IPP_TAG_NOVALUE
:
4002 cupsFilePuts(out
, "novalue");
4005 case IPP_TAG_RANGE
:
4006 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4007 attr
->values
[i
].range
.upper
);
4010 case IPP_TAG_RESOLUTION
:
4011 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4012 attr
->values
[i
].resolution
.yres
,
4013 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4018 case IPP_TAG_STRING
:
4021 case IPP_TAG_KEYWORD
:
4022 case IPP_TAG_CHARSET
:
4023 case IPP_TAG_LANGUAGE
:
4024 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4027 * Need to quote strings for PS banners...
4032 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4034 if (*p
== '(' || *p
== ')' || *p
== '\\')
4036 cupsFilePutChar(out
, '\\');
4037 cupsFilePutChar(out
, *p
);
4039 else if (*p
< 32 || *p
> 126)
4040 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4042 cupsFilePutChar(out
, *p
);
4046 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4050 break; /* anti-compiler-warning-code */
4054 else if (ch
== '\\') /* Quoted char */
4056 ch
= cupsFileGetChar(in
);
4058 if (ch
!= '{') /* Only do special handling for \{ */
4059 cupsFilePutChar(out
, '\\');
4061 cupsFilePutChar(out
, ch
);
4064 cupsFilePutChar(out
, ch
);
4068 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4070 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4071 IPP_TAG_INTEGER
)) != NULL
)
4072 attr
->values
[0].integer
+= kbytes
;
4081 * 'copy_file()' - Copy a PPD file or interface script...
4084 static int /* O - 0 = success, -1 = error */
4085 copy_file(const char *from
, /* I - Source file */
4086 const char *to
) /* I - Destination file */
4088 cups_file_t
*src
, /* Source file */
4089 *dst
; /* Destination file */
4090 int bytes
; /* Bytes to read/write */
4091 char buffer
[2048]; /* Copy buffer */
4094 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4097 * Open the source and destination file for a copy...
4100 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4103 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4110 * Copy the source file to the destination...
4113 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4114 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
4122 * Close both files and return...
4127 return (cupsFileClose(dst
));
4132 * 'copy_model()' - Copy a PPD model file, substituting default values
4136 static int /* O - 0 = success, -1 = error */
4137 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4138 const char *from
, /* I - Source file */
4139 const char *to
) /* I - Destination file */
4141 fd_set input
; /* select() input set */
4142 struct timeval timeout
; /* select() timeout */
4143 int maxfd
; /* Max file descriptor for select() */
4144 char tempfile
[1024]; /* Temporary PPD file */
4145 int tempfd
; /* Temporary PPD file descriptor */
4146 int temppid
; /* Process ID of cups-driverd */
4147 int temppipe
[2]; /* Temporary pipes */
4148 char *argv
[4], /* Command-line arguments */
4149 *envp
[MAX_ENV
]; /* Environment */
4150 cups_file_t
*src
, /* Source file */
4151 *dst
; /* Destination file */
4152 ppd_file_t
*ppd
; /* PPD file */
4153 int bytes
, /* Bytes from pipe */
4154 total
; /* Total bytes from pipe */
4155 char buffer
[2048]; /* Copy buffer */
4156 int i
; /* Looping var */
4157 char option
[PPD_MAX_NAME
], /* Option name */
4158 choice
[PPD_MAX_NAME
]; /* Choice name */
4159 int num_defaults
; /* Number of default options */
4160 cups_option_t
*defaults
; /* Default options */
4161 char cups_protocol
[PPD_MAX_LINE
];
4162 /* cupsProtocol attribute */
4163 int have_letter
, /* Have Letter size */
4164 have_a4
; /* Have A4 size */
4165 #ifdef HAVE_LIBPAPER
4166 char *paper_result
; /* Paper size name from libpaper */
4167 char system_paper
[64]; /* Paper size name buffer */
4168 #endif /* HAVE_LIBPAPER */
4171 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4172 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4176 * Run cups-driverd to get the PPD file...
4179 argv
[0] = "cups-driverd";
4181 argv
[2] = (char *)from
;
4184 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4186 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4187 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
4188 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4192 cupsdOpenPipe(temppipe
);
4194 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4195 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4197 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4198 -1, -1, 0, &temppid
))
4208 * Wait up to 30 seconds for the PPD file to be copied...
4213 if (temppipe
[0] > CGIPipes
[0])
4214 maxfd
= temppipe
[0] + 1;
4216 maxfd
= CGIPipes
[0] + 1;
4221 * See if we have data ready...
4227 FD_SET(temppipe
[0], &input
);
4228 FD_SET(CGIPipes
[0], &input
);
4230 timeout
.tv_sec
= 30;
4231 timeout
.tv_usec
= 0;
4233 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4243 * We have timed out...
4249 if (FD_ISSET(temppipe
[0], &input
))
4252 * Read the PPD file from the pipe, and write it to the PPD file.
4255 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4257 if (write(tempfd
, buffer
, bytes
) < bytes
)
4266 if (FD_ISSET(CGIPipes
[0], &input
))
4276 * No data from cups-deviced...
4279 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
4285 * Read the source file and see what page sizes are supported...
4288 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
4294 have_letter
= ppdPageSize(ppd
, "Letter") != NULL
;
4295 have_a4
= ppdPageSize(ppd
, "A4") != NULL
;
4298 * Open the destination (if possible) and set the default options...
4303 cups_protocol
[0] = '\0';
4305 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4308 * Read all of the default lines from the old PPD...
4311 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4312 if (!strncmp(buffer
, "*Default", 8))
4315 * Add the default option...
4318 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4319 choice
, sizeof(choice
)))
4321 ppd_option_t
*ppdo
; /* PPD option */
4325 * Only add the default if the default hasn't already been
4326 * set and the choice exists in the new PPD...
4329 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4330 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4331 ppdFindChoice(ppdo
, choice
))
4332 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4336 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4337 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4341 #ifdef HAVE_LIBPAPER
4342 else if ((paper_result
= systempapername()) != NULL
)
4345 * Set the default media sizes from the systemwide default...
4348 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
4349 system_paper
[0] = toupper(system_paper
[0] & 255);
4351 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
4352 (!strcmp(system_paper
, "A4") && have_a4
))
4354 num_defaults
= cupsAddOption("PageSize", system_paper
,
4355 num_defaults
, &defaults
);
4356 num_defaults
= cupsAddOption("PageRegion", system_paper
,
4357 num_defaults
, &defaults
);
4358 num_defaults
= cupsAddOption("PaperDimension", system_paper
,
4359 num_defaults
, &defaults
);
4360 num_defaults
= cupsAddOption("ImageableArea", system_paper
,
4361 num_defaults
, &defaults
);
4364 #endif /* HAVE_LIBPAPER */
4368 * Add the default media sizes...
4370 * Note: These values are generally not valid for large-format devices
4371 * like plotters, however it is probably safe to say that those
4372 * users will configure the media size after initially adding
4373 * the device anyways...
4376 if (!DefaultLanguage
||
4377 !strcasecmp(DefaultLanguage
, "C") ||
4378 !strcasecmp(DefaultLanguage
, "POSIX") ||
4379 !strcasecmp(DefaultLanguage
, "en") ||
4380 !strncasecmp(DefaultLanguage
, "en.", 3) ||
4381 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4382 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4383 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4386 * These are the only locales that will default to "letter" size...
4391 num_defaults
= cupsAddOption("PageSize", "Letter", num_defaults
,
4393 num_defaults
= cupsAddOption("PageRegion", "Letter", num_defaults
,
4395 num_defaults
= cupsAddOption("PaperDimension", "Letter", num_defaults
,
4397 num_defaults
= cupsAddOption("ImageableArea", "Letter", num_defaults
,
4404 * The rest default to "a4" size...
4407 num_defaults
= cupsAddOption("PageSize", "A4", num_defaults
,
4409 num_defaults
= cupsAddOption("PageRegion", "A4", num_defaults
,
4411 num_defaults
= cupsAddOption("PaperDimension", "A4", num_defaults
,
4413 num_defaults
= cupsAddOption("ImageableArea", "A4", num_defaults
,
4421 * Open the source file for a copy...
4424 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4426 cupsFreeOptions(num_defaults
, defaults
);
4432 * Open the destination file for a copy...
4435 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4437 cupsFreeOptions(num_defaults
, defaults
);
4444 * Copy the source file to the destination...
4447 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4449 if (!strncmp(buffer
, "*Default", 8))
4452 * Check for an previous default option choice...
4455 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4456 choice
, sizeof(choice
)))
4458 const char *val
; /* Default option value */
4461 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4464 * Substitute the previous choice...
4467 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4472 cupsFilePrintf(dst
, "%s\n", buffer
);
4475 if (cups_protocol
[0])
4476 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4478 cupsFreeOptions(num_defaults
, defaults
);
4481 * Close both files and return...
4488 return (cupsFileClose(dst
));
4493 * 'copy_job_attrs()' - Copy job attributes.
4497 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4498 cupsd_job_t
*job
, /* I - Job */
4499 cups_array_t
*ra
) /* I - Requested attributes array */
4501 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4505 * Send the requested attributes for each job...
4508 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4509 con
->servername
, con
->serverport
, "/jobs/%d",
4512 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
4513 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4514 "job-more-info", NULL
, job_uri
);
4516 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4517 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4518 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4519 job
->num_files
> 0);
4521 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
4522 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4523 "job-printer-up-time", time(NULL
));
4525 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4526 add_job_state_reasons(con
, job
);
4528 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4529 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4530 "job-uri", NULL
, job_uri
);
4532 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
4537 * 'copy_printer_attrs()' - Copy printer attributes.
4542 cupsd_client_t
*con
, /* I - Client connection */
4543 cupsd_printer_t
*printer
, /* I - Printer */
4544 cups_array_t
*ra
) /* I - Requested attributes array */
4546 char printer_uri
[HTTP_MAX_URI
];
4548 time_t curtime
; /* Current time */
4549 int i
; /* Looping var */
4550 ipp_attribute_t
*history
; /* History collection */
4554 * Copy the printer attributes to the response using requested-attributes
4555 * and document-format attributes that may be provided by the client.
4558 curtime
= time(NULL
);
4561 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
4562 printer
->recoverable
)
4563 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4564 "com.apple.print.recoverable-message", NULL
,
4565 printer
->recoverable
);
4566 #endif /* __APPLE__ */
4568 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4569 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
4570 "printer-alert", NULL
, printer
->alert
);
4572 if (printer
->alert_description
&&
4573 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4574 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4575 "printer-alert-description", NULL
,
4576 printer
->alert_description
);
4578 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4579 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4580 ippTimeToDate(curtime
));
4582 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4583 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4584 "printer-error-policy", NULL
, printer
->error_policy
);
4586 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4587 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
4588 printer
->accepting
);
4590 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4591 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
4594 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4595 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4596 "printer-op-policy", NULL
, printer
->op_policy
);
4598 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4599 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4602 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4603 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4604 "printer-state-change-time", printer
->state_time
);
4606 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
4607 cupsArrayFind(ra
, "printer-state-history"))
4610 * Printer history is only sent if specifically requested, so that
4611 * older CUPS/IPP clients won't barf on the collection attributes.
4614 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
4615 "printer-state-history",
4616 printer
->num_history
, NULL
);
4618 for (i
= 0; i
< printer
->num_history
; i
++)
4619 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
4620 NULL
, IPP_TAG_ZERO
, 0);
4623 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4624 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4625 "printer-state-message", NULL
, printer
->state_message
);
4627 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4628 add_printer_state_reasons(con
, printer
);
4630 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4632 int type
; /* printer-type value */
4636 * Add the CUPS-specific printer-type attribute...
4639 type
= printer
->type
;
4641 if (printer
== DefaultPrinter
)
4642 type
|= CUPS_PRINTER_DEFAULT
;
4644 if (!printer
->accepting
)
4645 type
|= CUPS_PRINTER_REJECTING
;
4647 if (!printer
->shared
)
4648 type
|= CUPS_PRINTER_NOT_SHARED
;
4650 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
4654 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4655 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4656 "printer-up-time", curtime
);
4658 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
4659 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
4662 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4663 "ipp", NULL
, con
->servername
, con
->serverport
,
4664 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4665 "/classes/%s" : "/printers/%s", printer
->name
);
4666 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4667 "printer-uri-supported", NULL
, printer_uri
);
4668 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4672 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4673 add_queued_job_count(con
, printer
);
4675 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
4676 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
4681 * 'copy_subscription_attrs()' - Copy subscription attributes.
4685 copy_subscription_attrs(
4686 cupsd_client_t
*con
, /* I - Client connection */
4687 cupsd_subscription_t
*sub
, /* I - Subscription */
4688 cups_array_t
*ra
) /* I - Requested attributes array */
4690 ipp_attribute_t
*attr
; /* Current attribute */
4691 char printer_uri
[HTTP_MAX_URI
];
4693 int count
; /* Number of events */
4694 unsigned mask
; /* Current event mask */
4695 const char *name
; /* Current event name */
4699 * Copy the subscription attributes to the response using the
4700 * requested-attributes attribute that may be provided by the client.
4703 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
4705 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
4708 * Simple event list...
4711 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
4712 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
4713 "notify-events", NULL
, name
);
4718 * Complex event list...
4721 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4722 if (sub
->mask
& mask
)
4725 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
4726 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
4727 "notify-events", count
, NULL
, NULL
);
4729 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4730 if (sub
->mask
& mask
)
4732 attr
->values
[count
].string
.text
=
4733 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
4740 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
4741 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4742 "notify-job-id", sub
->job
->id
);
4744 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
4745 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4746 "notify-lease-duration", sub
->lease
);
4748 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
4750 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4751 "ipp", NULL
, con
->servername
, con
->serverport
,
4752 "/printers/%s", sub
->dest
->name
);
4753 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4754 "notify-printer-uri", NULL
, printer_uri
);
4757 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
4758 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4759 "notify-recipient-uri", NULL
, sub
->recipient
);
4760 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
4761 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
4762 "notify-pull-method", NULL
, "ippget");
4764 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
4765 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
4766 "notify-subscriber-user-name", NULL
, sub
->owner
);
4768 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
4769 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4770 "notify-subscription-id", sub
->id
);
4772 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
4773 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4774 "notify-time-interval", sub
->interval
);
4776 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
4777 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
4778 sub
->user_data
, sub
->user_data_len
);
4783 * 'create_job()' - Print a file to a printer or class.
4787 create_job(cupsd_client_t
*con
, /* I - Client connection */
4788 ipp_attribute_t
*uri
) /* I - Printer URI */
4790 cupsd_printer_t
*printer
; /* Printer */
4791 cupsd_job_t
*job
; /* New job */
4794 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
4795 con
->http
.fd
, uri
->values
[0].string
.text
);
4798 * Is the destination valid?
4801 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
4807 send_ipp_status(con
, IPP_NOT_FOUND
,
4808 _("The printer or class was not found."));
4813 * Create the job object...
4816 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
4819 job
->pending_timeout
= 1;
4822 * Save and log the job...
4827 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
4828 job
->id
, job
->dest
, job
->username
);
4833 * 'create_requested_array()' - Create an array for the requested-attributes.
4836 static cups_array_t
* /* O - Array of attributes or NULL */
4837 create_requested_array(ipp_t
*request
) /* I - IPP request */
4839 int i
; /* Looping var */
4840 ipp_attribute_t
*requested
; /* requested-attributes attribute */
4841 cups_array_t
*ra
; /* Requested attributes array */
4842 char *value
; /* Current value */
4846 * Get the requested-attributes attribute, and return NULL if we don't
4850 if ((requested
= ippFindAttribute(request
, "requested-attributes",
4851 IPP_TAG_KEYWORD
)) == NULL
)
4855 * If the attribute contains a single "all" keyword, return NULL...
4858 if (requested
->num_values
== 1 &&
4859 !strcmp(requested
->values
[0].string
.text
, "all"))
4863 * Create an array using "strcmp" as the comparison function...
4866 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
4868 for (i
= 0; i
< requested
->num_values
; i
++)
4870 value
= requested
->values
[i
].string
.text
;
4872 if (!strcmp(value
, "job-template"))
4874 cupsArrayAdd(ra
, "copies");
4875 cupsArrayAdd(ra
, "copies-default");
4876 cupsArrayAdd(ra
, "copies-supported");
4877 cupsArrayAdd(ra
, "finishings");
4878 cupsArrayAdd(ra
, "finishings-default");
4879 cupsArrayAdd(ra
, "finishings-supported");
4880 cupsArrayAdd(ra
, "job-hold-until");
4881 cupsArrayAdd(ra
, "job-hold-until-default");
4882 cupsArrayAdd(ra
, "job-hold-until-supported");
4883 cupsArrayAdd(ra
, "job-priority");
4884 cupsArrayAdd(ra
, "job-priority-default");
4885 cupsArrayAdd(ra
, "job-priority-supported");
4886 cupsArrayAdd(ra
, "job-sheets");
4887 cupsArrayAdd(ra
, "job-sheets-default");
4888 cupsArrayAdd(ra
, "job-sheets-supported");
4889 cupsArrayAdd(ra
, "media");
4890 cupsArrayAdd(ra
, "media-default");
4891 cupsArrayAdd(ra
, "media-supported");
4892 cupsArrayAdd(ra
, "multiple-document-handling");
4893 cupsArrayAdd(ra
, "multiple-document-handling-default");
4894 cupsArrayAdd(ra
, "multiple-document-handling-supported");
4895 cupsArrayAdd(ra
, "number-up");
4896 cupsArrayAdd(ra
, "number-up-default");
4897 cupsArrayAdd(ra
, "number-up-supported");
4898 cupsArrayAdd(ra
, "orientation-requested");
4899 cupsArrayAdd(ra
, "orientation-requested-default");
4900 cupsArrayAdd(ra
, "orientation-requested-supported");
4901 cupsArrayAdd(ra
, "page-ranges");
4902 cupsArrayAdd(ra
, "page-ranges-supported");
4903 cupsArrayAdd(ra
, "printer-resolution");
4904 cupsArrayAdd(ra
, "printer-resolution-default");
4905 cupsArrayAdd(ra
, "printer-resolution-supported");
4906 cupsArrayAdd(ra
, "print-quality");
4907 cupsArrayAdd(ra
, "print-quality-default");
4908 cupsArrayAdd(ra
, "print-quality-supported");
4909 cupsArrayAdd(ra
, "sides");
4910 cupsArrayAdd(ra
, "sides-default");
4911 cupsArrayAdd(ra
, "sides-supported");
4913 else if (!strcmp(value
, "job-description"))
4915 cupsArrayAdd(ra
, "date-time-at-completed");
4916 cupsArrayAdd(ra
, "date-time-at-creation");
4917 cupsArrayAdd(ra
, "date-time-at-processing");
4918 cupsArrayAdd(ra
, "job-detailed-status-message");
4919 cupsArrayAdd(ra
, "job-document-access-errors");
4920 cupsArrayAdd(ra
, "job-id");
4921 cupsArrayAdd(ra
, "job-impressions");
4922 cupsArrayAdd(ra
, "job-impressions-completed");
4923 cupsArrayAdd(ra
, "job-k-octets");
4924 cupsArrayAdd(ra
, "job-k-octets-processed");
4925 cupsArrayAdd(ra
, "job-media-sheets");
4926 cupsArrayAdd(ra
, "job-media-sheets-completed");
4927 cupsArrayAdd(ra
, "job-message-from-operator");
4928 cupsArrayAdd(ra
, "job-more-info");
4929 cupsArrayAdd(ra
, "job-name");
4930 cupsArrayAdd(ra
, "job-originating-user-name");
4931 cupsArrayAdd(ra
, "job-printer-up-time");
4932 cupsArrayAdd(ra
, "job-printer-uri");
4933 cupsArrayAdd(ra
, "job-state");
4934 cupsArrayAdd(ra
, "job-state-message");
4935 cupsArrayAdd(ra
, "job-state-reasons");
4936 cupsArrayAdd(ra
, "job-uri");
4937 cupsArrayAdd(ra
, "number-of-documents");
4938 cupsArrayAdd(ra
, "number-of-intervening-jobs");
4939 cupsArrayAdd(ra
, "output-device-assigned");
4940 cupsArrayAdd(ra
, "time-at-completed");
4941 cupsArrayAdd(ra
, "time-at-creation");
4942 cupsArrayAdd(ra
, "time-at-processing");
4944 else if (!strcmp(value
, "printer-description"))
4946 cupsArrayAdd(ra
, "charset-configured");
4947 cupsArrayAdd(ra
, "charset-supported");
4948 cupsArrayAdd(ra
, "color-supported");
4949 cupsArrayAdd(ra
, "compression-supported");
4950 cupsArrayAdd(ra
, "document-format-default");
4951 cupsArrayAdd(ra
, "document-format-supported");
4952 cupsArrayAdd(ra
, "generated-natural-language-supported");
4953 cupsArrayAdd(ra
, "ipp-versions-supported");
4954 cupsArrayAdd(ra
, "job-impressions-supported");
4955 cupsArrayAdd(ra
, "job-k-octets-supported");
4956 cupsArrayAdd(ra
, "job-media-sheets-supported");
4957 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
4958 cupsArrayAdd(ra
, "multiple-operation-time-out");
4959 cupsArrayAdd(ra
, "natural-language-configured");
4960 cupsArrayAdd(ra
, "notify-attributes-supported");
4961 cupsArrayAdd(ra
, "notify-lease-duration-default");
4962 cupsArrayAdd(ra
, "notify-lease-duration-supported");
4963 cupsArrayAdd(ra
, "notify-max-events-supported");
4964 cupsArrayAdd(ra
, "notify-events-default");
4965 cupsArrayAdd(ra
, "notify-events-supported");
4966 cupsArrayAdd(ra
, "notify-pull-method-supported");
4967 cupsArrayAdd(ra
, "notify-schemes-supported");
4968 cupsArrayAdd(ra
, "operations-supported");
4969 cupsArrayAdd(ra
, "pages-per-minute");
4970 cupsArrayAdd(ra
, "pages-per-minute-color");
4971 cupsArrayAdd(ra
, "pdl-override-supported");
4972 cupsArrayAdd(ra
, "printer-alert");
4973 cupsArrayAdd(ra
, "printer-alert-description");
4974 cupsArrayAdd(ra
, "printer-current-time");
4975 cupsArrayAdd(ra
, "printer-driver-installer");
4976 cupsArrayAdd(ra
, "printer-info");
4977 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
4978 cupsArrayAdd(ra
, "printer-location");
4979 cupsArrayAdd(ra
, "printer-make-and-model");
4980 cupsArrayAdd(ra
, "printer-message-from-operator");
4981 cupsArrayAdd(ra
, "printer-more-info");
4982 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
4983 cupsArrayAdd(ra
, "printer-name");
4984 cupsArrayAdd(ra
, "printer-state");
4985 cupsArrayAdd(ra
, "printer-state-message");
4986 cupsArrayAdd(ra
, "printer-state-reasons");
4987 cupsArrayAdd(ra
, "printer-up-time");
4988 cupsArrayAdd(ra
, "printer-uri-supported");
4989 cupsArrayAdd(ra
, "queued-job-count");
4990 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
4991 cupsArrayAdd(ra
, "uri-authentication-supported");
4992 cupsArrayAdd(ra
, "uri-security-supported");
4994 else if (!strcmp(value
, "printer-defaults"))
4996 char *name
; /* Option name */
4999 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5001 name
= (char *)cupsArrayNext(CommonDefaults
))
5002 cupsArrayAdd(ra
, name
);
5004 else if (!strcmp(value
, "subscription-template"))
5006 cupsArrayAdd(ra
, "notify-attributes");
5007 cupsArrayAdd(ra
, "notify-charset");
5008 cupsArrayAdd(ra
, "notify-events");
5009 cupsArrayAdd(ra
, "notify-lease-duration");
5010 cupsArrayAdd(ra
, "notify-natural-language");
5011 cupsArrayAdd(ra
, "notify-pull-method");
5012 cupsArrayAdd(ra
, "notify-recipient-uri");
5013 cupsArrayAdd(ra
, "notify-time-interval");
5014 cupsArrayAdd(ra
, "notify-user-data");
5017 cupsArrayAdd(ra
, value
);
5025 * 'create_subscription()' - Create a notification subscription.
5029 create_subscription(
5030 cupsd_client_t
*con
, /* I - Client connection */
5031 ipp_attribute_t
*uri
) /* I - Printer URI */
5033 http_status_t status
; /* Policy status */
5034 int i
; /* Looping var */
5035 ipp_attribute_t
*attr
; /* Current attribute */
5036 cups_ptype_t dtype
; /* Destination type (printer/class) */
5037 char scheme
[HTTP_MAX_URI
],
5038 /* Scheme portion of URI */
5039 userpass
[HTTP_MAX_URI
],
5040 /* Username portion of URI */
5042 /* Host portion of URI */
5043 resource
[HTTP_MAX_URI
];
5044 /* Resource portion of URI */
5045 int port
; /* Port portion of URI */
5046 cupsd_printer_t
*printer
; /* Printer/class */
5047 cupsd_job_t
*job
; /* Job */
5048 int jobid
; /* Job ID */
5049 cupsd_subscription_t
*sub
; /* Subscription object */
5050 const char *username
, /* requesting-user-name or
5051 authenticated username */
5052 *recipient
, /* notify-recipient-uri */
5053 *pullmethod
; /* notify-pull-method */
5054 ipp_attribute_t
*user_data
; /* notify-user-data */
5055 int interval
, /* notify-time-interval */
5056 lease
; /* notify-lease-duration */
5057 unsigned mask
; /* notify-events */
5058 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5059 *notify_lease
; /* notify-lease-duration(-default) */
5063 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5065 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5066 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
5067 attr
->value_tag
, attr
->name
);
5069 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
5074 * Is the destination valid?
5077 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5078 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5079 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5081 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5082 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5083 sizeof(host
), &port
, resource
, sizeof(resource
));
5085 if (!strcmp(resource
, "/"))
5087 dtype
= (cups_ptype_t
)0;
5090 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5092 dtype
= (cups_ptype_t
)0;
5095 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5097 dtype
= CUPS_PRINTER_CLASS
;
5100 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5106 send_ipp_status(con
, IPP_NOT_FOUND
,
5107 _("The printer or class was not found."));
5117 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5120 send_http_error(con
, status
, printer
);
5124 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5126 send_http_error(con
, status
, NULL
);
5131 * Get the user that is requesting the subscription...
5134 username
= get_username(con
);
5137 * Find the first subscription group attribute; return if we have
5141 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5142 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5147 send_ipp_status(con
, IPP_BAD_REQUEST
,
5148 _("No subscription attributes in request!"));
5153 * Process the subscription attributes in the request...
5156 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5164 lease
= DefaultLeaseDuration
;
5166 mask
= CUPSD_EVENT_NONE
;
5170 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5172 notify_lease
= ippFindAttribute(printer
->attrs
,
5173 "notify-lease-duration-default",
5177 lease
= notify_lease
->values
[0].integer
;
5181 notify_events
= NULL
;
5182 notify_lease
= NULL
;
5185 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5187 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5188 attr
->value_tag
== IPP_TAG_URI
)
5191 * Validate the recipient scheme against the ServerBin/notifier
5195 char notifier
[1024]; /* Notifier filename */
5198 recipient
= attr
->values
[0].string
.text
;
5200 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5201 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5202 host
, sizeof(host
), &port
,
5203 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5205 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5206 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
5207 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5208 "notify-status-code", IPP_URI_SCHEME
);
5212 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5214 if (access(notifier
, X_OK
))
5216 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5217 _("notify-recipient-uri URI \"%s\" uses unknown "
5218 "scheme!"), recipient
);
5219 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5220 "notify-status-code", IPP_URI_SCHEME
);
5224 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5225 attr
->value_tag
== IPP_TAG_KEYWORD
)
5227 pullmethod
= attr
->values
[0].string
.text
;
5229 if (strcmp(pullmethod
, "ippget"))
5231 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5232 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
5233 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5234 "notify-status-code", IPP_ATTRIBUTES
);
5238 else if (!strcmp(attr
->name
, "notify-charset") &&
5239 attr
->value_tag
== IPP_TAG_CHARSET
&&
5240 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5241 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5243 send_ipp_status(con
, IPP_CHARSET
,
5244 _("Character set \"%s\" not supported!"),
5245 attr
->values
[0].string
.text
);
5248 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5249 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5250 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5252 send_ipp_status(con
, IPP_CHARSET
,
5253 _("Language \"%s\" not supported!"),
5254 attr
->values
[0].string
.text
);
5257 else if (!strcmp(attr
->name
, "notify-user-data") &&
5258 attr
->value_tag
== IPP_TAG_STRING
)
5260 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5262 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5263 _("The notify-user-data value is too large "
5264 "(%d > 63 octets)!"),
5265 attr
->values
[0].unknown
.length
);
5271 else if (!strcmp(attr
->name
, "notify-events") &&
5272 attr
->value_tag
== IPP_TAG_KEYWORD
)
5273 notify_events
= attr
;
5274 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5275 attr
->value_tag
== IPP_TAG_INTEGER
)
5276 lease
= attr
->values
[0].integer
;
5277 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5278 attr
->value_tag
== IPP_TAG_INTEGER
)
5279 interval
= attr
->values
[0].integer
;
5280 else if (!strcmp(attr
->name
, "notify-job-id") &&
5281 attr
->value_tag
== IPP_TAG_INTEGER
)
5282 jobid
= attr
->values
[0].integer
;
5289 for (i
= 0; i
< notify_events
->num_values
; i
++)
5290 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5294 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5296 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5297 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5298 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5300 if (!recipient
&& !pullmethod
)
5303 if (mask
== CUPSD_EVENT_NONE
)
5306 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5308 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5311 send_ipp_status(con
, IPP_BAD_REQUEST
,
5312 _("notify-events not specified!"));
5317 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5319 cupsdLogMessage(CUPSD_LOG_INFO
,
5320 "create_subscription: Limiting notify-lease-duration to "
5323 lease
= MaxLeaseDuration
;
5328 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5330 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
5337 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
5340 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
5343 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5344 "Added subscription %d for printer \"%s\"",
5345 sub
->id
, printer
->name
);
5347 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
5350 sub
->interval
= interval
;
5352 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5354 cupsdSetString(&sub
->owner
, username
);
5358 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5359 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5360 sub
->user_data_len
);
5363 ippAddSeparator(con
->response
);
5364 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5365 "notify-subscription-id", sub
->id
);
5367 con
->response
->request
.status
.status_code
= IPP_OK
;
5373 cupsdSaveAllSubscriptions();
5379 * 'delete_printer()' - Remove a printer or class from the system.
5383 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5384 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5386 http_status_t status
; /* Policy status */
5387 cups_ptype_t dtype
; /* Destination type (printer/class) */
5388 cupsd_printer_t
*printer
; /* Printer/class */
5389 char filename
[1024]; /* Script/PPD filename */
5392 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5393 con
->http
.fd
, uri
->values
[0].string
.text
);
5396 * Do we have a valid URI?
5399 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5405 send_ipp_status(con
, IPP_NOT_FOUND
,
5406 _("The printer or class was not found."));
5414 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5416 send_http_error(con
, status
, NULL
);
5421 * Remove old jobs...
5424 cupsdCancelJobs(printer
->name
, NULL
, 1);
5427 * Remove old subscriptions and send a "deleted printer" event...
5430 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5431 "%s \"%s\" deleted by \"%s\".",
5432 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5433 printer
->name
, get_username(con
));
5435 cupsdExpireSubscriptions(printer
, NULL
);
5438 * Remove any old PPD or script files...
5441 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5445 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5449 if (dtype
& CUPS_PRINTER_CLASS
)
5451 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5452 printer
->name
, get_username(con
));
5454 cupsdDeletePrinter(printer
, 0);
5455 cupsdSaveAllClasses();
5459 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5460 printer
->name
, get_username(con
));
5462 cupsdDeletePrinter(printer
, 0);
5463 cupsdSaveAllPrinters();
5466 cupsdWritePrintcap();
5469 * Return with no errors...
5472 con
->response
->request
.status
.status_code
= IPP_OK
;
5477 * 'get_default()' - Get the default destination.
5481 get_default(cupsd_client_t
*con
) /* I - Client connection */
5483 http_status_t status
; /* Policy status */
5484 cups_array_t
*ra
; /* Requested attributes array */
5487 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5493 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5495 send_http_error(con
, status
, NULL
);
5501 ra
= create_requested_array(con
->request
);
5503 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5505 cupsArrayDelete(ra
);
5507 con
->response
->request
.status
.status_code
= IPP_OK
;
5510 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5515 * 'get_devices()' - Get the list of available devices on the local system.
5519 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5521 http_status_t status
; /* Policy status */
5522 ipp_attribute_t
*limit
, /* Limit attribute */
5523 *requested
; /* requested-attributes attribute */
5524 char command
[1024], /* cups-deviced command */
5525 options
[1024], /* Options to pass to command */
5527 /* String for requested attributes */
5530 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5536 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5538 send_http_error(con
, status
, NULL
);
5543 * Run cups-deviced command with the given options...
5546 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5547 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5551 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5553 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5555 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5556 snprintf(options
, sizeof(options
),
5558 con
->request
->request
.op
.request_id
,
5559 limit
? limit
->values
[0].integer
: 0, (int)User
,
5562 if (cupsdSendCommand(con
, command
, options
, 1))
5565 * Command started successfully, don't send an IPP response here...
5568 ippDelete(con
->response
);
5569 con
->response
= NULL
;
5574 * Command failed, return "internal error" so the user knows something
5578 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5579 _("cups-deviced failed to execute."));
5585 * 'get_job_attrs()' - Get job attributes.
5589 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5590 ipp_attribute_t
*uri
) /* I - Job URI */
5592 http_status_t status
; /* Policy status */
5593 ipp_attribute_t
*attr
; /* Current attribute */
5594 int jobid
; /* Job ID */
5595 cupsd_job_t
*job
; /* Current job */
5596 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5597 username
[HTTP_MAX_URI
], /* Username portion of URI */
5598 host
[HTTP_MAX_URI
], /* Host portion of URI */
5599 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5600 int port
; /* Port portion of URI */
5601 cups_array_t
*ra
; /* Requested attributes array */
5604 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5605 con
->http
.fd
, uri
->values
[0].string
.text
);
5608 * See if we have a job URI or a printer URI...
5611 if (!strcmp(uri
->name
, "printer-uri"))
5614 * Got a printer URI; see if we also have a job-id attribute...
5617 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5618 IPP_TAG_INTEGER
)) == NULL
)
5620 send_ipp_status(con
, IPP_BAD_REQUEST
,
5621 _("Got a printer-uri attribute but no job-id!"));
5625 jobid
= attr
->values
[0].integer
;
5630 * Got a job URI; parse it to get the job ID...
5633 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5634 sizeof(method
), username
, sizeof(username
), host
,
5635 sizeof(host
), &port
, resource
, sizeof(resource
));
5637 if (strncmp(resource
, "/jobs/", 6))
5643 send_ipp_status(con
, IPP_BAD_REQUEST
,
5644 _("Bad job-uri attribute \"%s\"!"),
5645 uri
->values
[0].string
.text
);
5649 jobid
= atoi(resource
+ 6);
5653 * See if the job exists...
5656 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5659 * Nope - return a "not found" error...
5662 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5670 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5672 send_http_error(con
, status
, NULL
);
5677 * Copy attributes...
5682 ra
= create_requested_array(con
->request
);
5683 copy_job_attrs(con
, job
, ra
);
5684 cupsArrayDelete(ra
);
5686 con
->response
->request
.status
.status_code
= IPP_OK
;
5691 * 'get_jobs()' - Get a list of jobs for the specified printer.
5695 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
5696 ipp_attribute_t
*uri
) /* I - Printer URI */
5698 http_status_t status
; /* Policy status */
5699 ipp_attribute_t
*attr
; /* Current attribute */
5700 const char *dest
; /* Destination */
5701 cups_ptype_t dtype
; /* Destination type (printer/class) */
5702 cups_ptype_t dmask
; /* Destination type mask */
5703 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
5704 username
[HTTP_MAX_URI
], /* Username portion of URI */
5705 host
[HTTP_MAX_URI
], /* Host portion of URI */
5706 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5707 int port
; /* Port portion of URI */
5708 int completed
; /* Completed jobs? */
5709 int first_job_id
; /* First job ID */
5710 int limit
; /* Maximum number of jobs to return */
5711 int count
; /* Number of jobs that match */
5712 cupsd_job_t
*job
; /* Current job pointer */
5713 cupsd_printer_t
*printer
; /* Printer */
5714 cups_array_t
*list
; /* Which job list... */
5715 cups_array_t
*ra
; /* Requested attributes array */
5718 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
5719 uri
->values
[0].string
.text
);
5722 * Is the destination valid?
5725 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5726 sizeof(scheme
), username
, sizeof(username
), host
,
5727 sizeof(host
), &port
, resource
, sizeof(resource
));
5729 if (!strcmp(resource
, "/") ||
5730 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
5733 dtype
= (cups_ptype_t
)0;
5734 dmask
= (cups_ptype_t
)0;
5737 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5740 dtype
= (cups_ptype_t
)0;
5741 dmask
= CUPS_PRINTER_CLASS
;
5744 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5747 dtype
= CUPS_PRINTER_CLASS
;
5748 dmask
= CUPS_PRINTER_CLASS
;
5751 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
5758 send_ipp_status(con
, IPP_NOT_FOUND
,
5759 _("The printer or class was not found."));
5764 dtype
&= CUPS_PRINTER_CLASS
;
5765 dmask
= CUPS_PRINTER_CLASS
;
5774 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5777 send_http_error(con
, status
, printer
);
5781 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5783 send_http_error(con
, status
, NULL
);
5788 * See if the "which-jobs" attribute have been specified...
5791 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
5792 IPP_TAG_KEYWORD
)) != NULL
&&
5793 !strcmp(attr
->values
[0].string
.text
, "completed"))
5798 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
5810 * See if they want to limit the number of jobs reported...
5813 if ((attr
= ippFindAttribute(con
->request
, "limit",
5814 IPP_TAG_INTEGER
)) != NULL
)
5815 limit
= attr
->values
[0].integer
;
5819 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
5820 IPP_TAG_INTEGER
)) != NULL
)
5821 first_job_id
= attr
->values
[0].integer
;
5826 * See if we only want to see jobs for a specific user...
5829 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
5830 IPP_TAG_BOOLEAN
)) != NULL
&&
5831 attr
->values
[0].boolean
)
5832 strlcpy(username
, get_username(con
), sizeof(username
));
5836 ra
= create_requested_array(con
->request
);
5839 * OK, build a list of jobs for this printer...
5842 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
5843 count
< limit
&& job
;
5844 job
= (cupsd_job_t
*)cupsArrayNext(list
))
5847 * Filter out jobs that don't match...
5850 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
5852 if ((dest
&& strcmp(job
->dest
, dest
)) &&
5853 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
5855 if ((job
->dtype
& dmask
) != dtype
&&
5856 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
5858 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
5861 if (job
->id
< first_job_id
)
5869 if (username
[0] && strcasecmp(username
, job
->username
))
5873 ippAddSeparator(con
->response
);
5877 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
5879 copy_job_attrs(con
, job
, ra
);
5882 cupsArrayDelete(ra
);
5884 con
->response
->request
.status
.status_code
= IPP_OK
;
5889 * 'get_notifications()' - Get events for a subscription.
5893 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
5895 int i
, j
; /* Looping vars */
5896 http_status_t status
; /* Policy status */
5897 cupsd_subscription_t
*sub
; /* Subscription */
5898 ipp_attribute_t
*ids
, /* notify-subscription-ids */
5899 *sequences
; /* notify-sequence-numbers */
5900 int min_seq
; /* Minimum sequence number */
5901 int interval
; /* Poll interval */
5904 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
5908 * Get subscription attributes...
5911 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
5913 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
5918 send_ipp_status(con
, IPP_BAD_REQUEST
,
5919 _("Missing notify-subscription-ids attribute!"));
5924 * Are the subscription IDs valid?
5927 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
5929 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
5932 * Bad subscription ID...
5935 send_ipp_status(con
, IPP_NOT_FOUND
,
5936 _("notify-subscription-id %d no good!"),
5937 ids
->values
[i
].integer
);
5945 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5947 con
, sub
->owner
)) != HTTP_OK
)
5949 send_http_error(con
, status
, sub
->dest
);
5954 * Check the subscription type and update the interval accordingly.
5957 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
5960 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
5962 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
5968 * Tell the client to poll again in N seconds...
5972 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5973 "notify-get-interval", interval
);
5975 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5976 "printer-up-time", time(NULL
));
5979 * Copy the subscription event attributes to the response.
5982 con
->response
->request
.status
.status_code
=
5983 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
5985 for (i
= 0; i
< ids
->num_values
; i
++)
5988 * Get the subscription and sequence number...
5991 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
5993 if (sequences
&& i
< sequences
->num_values
)
5994 min_seq
= sequences
->values
[i
].integer
;
5999 * If we don't have any new events, nothing to do here...
6002 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
6006 * Otherwise copy all of the new events...
6009 if (sub
->first_event_id
> min_seq
)
6012 j
= min_seq
- sub
->first_event_id
;
6014 for (; j
< sub
->num_events
; j
++)
6016 ippAddSeparator(con
->response
);
6018 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
6019 IPP_TAG_EVENT_NOTIFICATION
, 0);
6026 * 'get_ppd()' - Get a named PPD from the local system.
6030 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
6031 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
6033 http_status_t status
; /* Policy status */
6034 cupsd_printer_t
*dest
; /* Destination */
6035 cups_ptype_t dtype
; /* Destination type */
6038 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
6039 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
6041 if (!strcmp(uri
->name
, "ppd-name"))
6044 * Return a PPD file from cups-driverd...
6047 char command
[1024], /* cups-driverd command */
6048 options
[1024], /* Options to pass to command */
6049 ppd_name
[1024]; /* ppd-name */
6056 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6058 send_http_error(con
, status
, NULL
);
6063 * Run cups-driverd command with the given options...
6066 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6067 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
6068 snprintf(options
, sizeof(options
), "get+%d+%s",
6069 con
->request
->request
.op
.request_id
, ppd_name
);
6071 if (cupsdSendCommand(con
, command
, options
, 0))
6074 * Command started successfully, don't send an IPP response here...
6077 ippDelete(con
->response
);
6078 con
->response
= NULL
;
6083 * Command failed, return "internal error" so the user knows something
6087 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6088 _("cups-driverd failed to execute."));
6091 else if (!strcmp(uri
->name
, "printer-uri") &&
6092 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
6094 int i
; /* Looping var */
6095 char filename
[1024]; /* PPD filename */
6102 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6104 send_http_error(con
, status
, NULL
);
6109 * See if we need the PPD for a class or remote printer...
6112 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6115 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
6117 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6118 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6119 "printer-uri", NULL
, dest
->uri
);
6122 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
6124 for (i
= 0; i
< dest
->num_printers
; i
++)
6125 if (!(dest
->printers
[i
]->type
&
6126 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
6128 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6129 dest
->printers
[i
]->name
);
6131 if (!access(filename
, 0))
6135 if (i
< dest
->num_printers
)
6136 dest
= dest
->printers
[i
];
6139 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6140 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6141 "printer-uri", NULL
, dest
->printers
[0]->uri
);
6147 * Found the printer with the PPD file, now see if there is one...
6150 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
6152 send_ipp_status(con
, IPP_NOT_FOUND
,
6153 _("The PPD file \"%s\" could not be opened: %s"),
6154 uri
->values
[0].string
.text
, strerror(errno
));
6158 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6162 con
->response
->request
.status
.status_code
= IPP_OK
;
6165 send_ipp_status(con
, IPP_NOT_FOUND
,
6166 _("The PPD file \"%s\" could not be found."),
6167 uri
->values
[0].string
.text
);
6172 * 'get_ppds()' - Get the list of PPD files on the local system.
6176 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
6178 http_status_t status
; /* Policy status */
6179 ipp_attribute_t
*limit
, /* Limit attribute */
6180 *device
, /* ppd-device-id attribute */
6181 *language
, /* ppd-natural-language attribute */
6182 *make
, /* ppd-make attribute */
6183 *model
, /* ppd-make-and-model attribute */
6184 *model_number
, /* ppd-model-number attribute */
6185 *product
, /* ppd-product attribute */
6186 *psversion
, /* ppd-psverion attribute */
6187 *type
, /* ppd-type attribute */
6188 *requested
; /* requested-attributes attribute */
6189 char command
[1024], /* cups-driverd command */
6190 options
[1024], /* Options to pass to command */
6191 device_str
[256],/* Escaped ppd-device-id string */
6193 /* Escaped ppd-natural-language */
6194 make_str
[256], /* Escaped ppd-make string */
6195 model_str
[256], /* Escaped ppd-make-and-model string */
6196 model_number_str
[256],
6197 /* ppd-model-number string */
6199 /* Escaped ppd-product string */
6201 /* Escaped ppd-psversion string */
6202 type_str
[256], /* Escaped ppd-type string */
6204 /* String for requested attributes */
6207 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
6213 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6215 send_http_error(con
, status
, NULL
);
6220 * Run cups-driverd command with the given options...
6223 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6224 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
6225 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
6227 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
6228 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
6230 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
6232 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
6233 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
6234 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
6235 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6239 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6241 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6244 url_encode_attr(device
, device_str
, sizeof(device_str
));
6246 device_str
[0] = '\0';
6249 url_encode_attr(language
, language_str
, sizeof(language_str
));
6251 language_str
[0] = '\0';
6254 url_encode_attr(make
, make_str
, sizeof(make_str
));
6259 url_encode_attr(model
, model_str
, sizeof(model_str
));
6261 model_str
[0] = '\0';
6264 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
6265 model_number
->values
[0].integer
);
6267 model_number_str
[0] = '\0';
6270 url_encode_attr(product
, product_str
, sizeof(product_str
));
6272 product_str
[0] = '\0';
6275 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
6277 psversion_str
[0] = '\0';
6280 url_encode_attr(type
, type_str
, sizeof(type_str
));
6284 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6285 snprintf(options
, sizeof(options
),
6286 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
6287 con
->request
->request
.op
.request_id
,
6288 limit
? limit
->values
[0].integer
: 0,
6290 device
? "%20" : "", device_str
,
6291 language
? "%20" : "", language_str
,
6292 make
? "%20" : "", make_str
,
6293 model
? "%20" : "", model_str
,
6294 model_number
? "%20" : "", model_number_str
,
6295 product
? "%20" : "", product_str
,
6296 psversion
? "%20" : "", psversion_str
,
6297 type
? "%20" : "", type_str
);
6299 if (cupsdSendCommand(con
, command
, options
, 0))
6302 * Command started successfully, don't send an IPP response here...
6305 ippDelete(con
->response
);
6306 con
->response
= NULL
;
6311 * Command failed, return "internal error" so the user knows something
6315 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6316 _("cups-driverd failed to execute."));
6322 * 'get_printer_attrs()' - Get printer attributes.
6326 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
6327 ipp_attribute_t
*uri
) /* I - Printer URI */
6329 http_status_t status
; /* Policy status */
6330 cups_ptype_t dtype
; /* Destination type (printer/class) */
6331 cupsd_printer_t
*printer
; /* Printer/class */
6332 cups_array_t
*ra
; /* Requested attributes array */
6335 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
6336 con
->http
.fd
, uri
->values
[0].string
.text
);
6339 * Is the destination valid?
6342 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6348 send_ipp_status(con
, IPP_NOT_FOUND
,
6349 _("The printer or class was not found."));
6357 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6359 send_http_error(con
, status
, printer
);
6364 * Send the attributes...
6367 ra
= create_requested_array(con
->request
);
6369 copy_printer_attrs(con
, printer
, ra
);
6371 cupsArrayDelete(ra
);
6373 con
->response
->request
.status
.status_code
= IPP_OK
;
6378 * 'get_printers()' - Get a list of printers or classes.
6382 get_printers(cupsd_client_t
*con
, /* I - Client connection */
6383 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
6385 http_status_t status
; /* Policy status */
6386 ipp_attribute_t
*attr
; /* Current attribute */
6387 int limit
; /* Max number of printers to return */
6388 int count
; /* Number of printers that match */
6389 cupsd_printer_t
*printer
; /* Current printer pointer */
6390 int printer_type
, /* printer-type attribute */
6391 printer_mask
; /* printer-type-mask attribute */
6392 char *location
; /* Location string */
6393 const char *username
; /* Current user */
6394 char *first_printer_name
; /* first-printer-name attribute */
6395 cups_array_t
*ra
; /* Requested attributes array */
6398 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
6399 con
->http
.fd
, type
);
6405 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6407 send_http_error(con
, status
, NULL
);
6412 * Check for printers...
6415 if (!Printers
|| !cupsArrayCount(Printers
))
6417 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
6422 * See if they want to limit the number of printers reported...
6425 if ((attr
= ippFindAttribute(con
->request
, "limit",
6426 IPP_TAG_INTEGER
)) != NULL
)
6427 limit
= attr
->values
[0].integer
;
6431 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
6432 IPP_TAG_NAME
)) != NULL
)
6433 first_printer_name
= attr
->values
[0].string
.text
;
6435 first_printer_name
= NULL
;
6438 * Support filtering...
6441 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
6442 IPP_TAG_ENUM
)) != NULL
)
6443 printer_type
= attr
->values
[0].integer
;
6447 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
6448 IPP_TAG_ENUM
)) != NULL
)
6449 printer_mask
= attr
->values
[0].integer
;
6453 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
6454 IPP_TAG_TEXT
)) != NULL
)
6455 location
= attr
->values
[0].string
.text
;
6459 if (con
->username
[0])
6460 username
= con
->username
;
6461 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6462 IPP_TAG_NAME
)) != NULL
)
6463 username
= attr
->values
[0].string
.text
;
6467 ra
= create_requested_array(con
->request
);
6470 * OK, build a list of printers for this printer...
6473 if (first_printer_name
)
6475 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
6476 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
6479 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
6482 count
< limit
&& printer
;
6483 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
6485 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
6486 (printer
->type
& printer_mask
) == printer_type
&&
6487 (!location
|| !printer
->location
||
6488 !strcasecmp(printer
->location
, location
)))
6491 * If HideImplicitMembers is enabled, see if this printer or class
6492 * is a member of an implicit class...
6495 if (ImplicitClasses
&& HideImplicitMembers
&&
6496 printer
->in_implicit_class
)
6500 * If a username is specified, see if it is allowed or denied
6504 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
6508 * Add the group separator as needed...
6512 ippAddSeparator(con
->response
);
6517 * Send the attributes...
6520 copy_printer_attrs(con
, printer
, ra
);
6524 cupsArrayDelete(ra
);
6526 con
->response
->request
.status
.status_code
= IPP_OK
;
6531 * 'get_subscription_attrs()' - Get subscription attributes.
6535 get_subscription_attrs(
6536 cupsd_client_t
*con
, /* I - Client connection */
6537 int sub_id
) /* I - Subscription ID */
6539 http_status_t status
; /* Policy status */
6540 cupsd_subscription_t
*sub
; /* Subscription */
6541 cups_array_t
*ra
; /* Requested attributes array */
6544 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6545 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
6546 con
, con
->http
.fd
, sub_id
);
6549 * Is the subscription ID valid?
6552 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
6555 * Bad subscription ID...
6558 send_ipp_status(con
, IPP_NOT_FOUND
,
6559 _("notify-subscription-id %d no good!"), sub_id
);
6567 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6569 con
, sub
->owner
)) != HTTP_OK
)
6571 send_http_error(con
, status
, sub
->dest
);
6576 * Copy the subscription attributes to the response using the
6577 * requested-attributes attribute that may be provided by the client.
6580 ra
= create_requested_array(con
->request
);
6582 copy_subscription_attrs(con
, sub
, ra
);
6584 cupsArrayDelete(ra
);
6586 con
->response
->request
.status
.status_code
= IPP_OK
;
6591 * 'get_subscriptions()' - Get subscriptions.
6595 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
6596 ipp_attribute_t
*uri
) /* I - Printer/job URI */
6598 http_status_t status
; /* Policy status */
6599 int count
; /* Number of subscriptions */
6600 int limit
; /* Limit */
6601 cupsd_subscription_t
*sub
; /* Subscription */
6602 cups_array_t
*ra
; /* Requested attributes array */
6603 ipp_attribute_t
*attr
; /* Attribute */
6604 cups_ptype_t dtype
; /* Destination type (printer/class) */
6605 char scheme
[HTTP_MAX_URI
],
6606 /* Scheme portion of URI */
6607 username
[HTTP_MAX_URI
],
6608 /* Username portion of URI */
6610 /* Host portion of URI */
6611 resource
[HTTP_MAX_URI
];
6612 /* Resource portion of URI */
6613 int port
; /* Port portion of URI */
6614 cupsd_job_t
*job
; /* Job pointer */
6615 cupsd_printer_t
*printer
; /* Printer */
6618 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6619 "get_subscriptions(con=%p[%d], uri=%s)",
6620 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
6623 * Is the destination valid?
6626 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6627 sizeof(scheme
), username
, sizeof(username
), host
,
6628 sizeof(host
), &port
, resource
, sizeof(resource
));
6630 if (!strcmp(resource
, "/") ||
6631 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
6632 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
6633 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
6638 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
6641 job
= cupsdFindJob(atoi(resource
+ 6));
6645 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
6650 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6656 send_ipp_status(con
, IPP_NOT_FOUND
,
6657 _("The printer or class was not found."));
6660 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
6661 IPP_TAG_INTEGER
)) != NULL
)
6663 job
= cupsdFindJob(attr
->values
[0].integer
);
6667 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
6668 attr
->values
[0].integer
);
6679 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
6681 con
, NULL
)) != HTTP_OK
)
6683 send_http_error(con
, status
, printer
);
6688 * Copy the subscription attributes to the response using the
6689 * requested-attributes attribute that may be provided by the client.
6692 ra
= create_requested_array(con
->request
);
6694 if ((attr
= ippFindAttribute(con
->request
, "limit",
6695 IPP_TAG_INTEGER
)) != NULL
)
6696 limit
= attr
->values
[0].integer
;
6701 * See if we only want to see subscriptions for a specific user...
6704 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
6705 IPP_TAG_BOOLEAN
)) != NULL
&&
6706 attr
->values
[0].boolean
)
6707 strlcpy(username
, get_username(con
), sizeof(username
));
6711 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
6713 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
6714 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
6715 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
6717 ippAddSeparator(con
->response
);
6718 copy_subscription_attrs(con
, sub
, ra
);
6721 if (limit
&& count
>= limit
)
6725 cupsArrayDelete(ra
);
6728 con
->response
->request
.status
.status_code
= IPP_OK
;
6730 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
6735 * 'get_username()' - Get the username associated with a request.
6738 static const char * /* O - Username */
6739 get_username(cupsd_client_t
*con
) /* I - Connection */
6741 ipp_attribute_t
*attr
; /* Attribute */
6744 if (con
->username
[0])
6745 return (con
->username
);
6746 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6747 IPP_TAG_NAME
)) != NULL
)
6748 return (attr
->values
[0].string
.text
);
6750 return ("anonymous");
6755 * 'hold_job()' - Hold a print job.
6759 hold_job(cupsd_client_t
*con
, /* I - Client connection */
6760 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
6762 ipp_attribute_t
*attr
, /* Current job-hold-until */
6763 *newattr
; /* New job-hold-until */
6764 int jobid
; /* Job ID */
6765 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6766 username
[HTTP_MAX_URI
], /* Username portion of URI */
6767 host
[HTTP_MAX_URI
], /* Host portion of URI */
6768 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6769 int port
; /* Port portion of URI */
6770 cupsd_job_t
*job
; /* Job information */
6773 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
6774 uri
->values
[0].string
.text
);
6777 * See if we have a job URI or a printer URI...
6780 if (!strcmp(uri
->name
, "printer-uri"))
6783 * Got a printer URI; see if we also have a job-id attribute...
6786 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6787 IPP_TAG_INTEGER
)) == NULL
)
6789 send_ipp_status(con
, IPP_BAD_REQUEST
,
6790 _("Got a printer-uri attribute but no job-id!"));
6794 jobid
= attr
->values
[0].integer
;
6799 * Got a job URI; parse it to get the job ID...
6802 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6803 sizeof(method
), username
, sizeof(username
), host
,
6804 sizeof(host
), &port
, resource
, sizeof(resource
));
6806 if (strncmp(resource
, "/jobs/", 6))
6812 send_ipp_status(con
, IPP_BAD_REQUEST
,
6813 _("Bad job-uri attribute \"%s\"!"),
6814 uri
->values
[0].string
.text
);
6818 jobid
= atoi(resource
+ 6);
6822 * See if the job exists...
6825 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6828 * Nope - return a "not found" error...
6831 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6836 * See if the job is owned by the requesting user...
6839 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6841 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
6846 * Hold the job and return...
6851 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
6852 "Job held by user.");
6854 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
6855 IPP_TAG_KEYWORD
)) == NULL
)
6856 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
6858 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
6859 IPP_TAG_KEYWORD
)) == NULL
)
6860 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
6865 * Free the old hold value and copy the new one over...
6868 _cupsStrFree(attr
->values
[0].string
.text
);
6872 attr
->value_tag
= newattr
->value_tag
;
6873 attr
->values
[0].string
.text
=
6874 _cupsStrAlloc(newattr
->values
[0].string
.text
);
6878 attr
->value_tag
= IPP_TAG_KEYWORD
;
6879 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
6883 * Hold job until specified time...
6886 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
6888 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
6889 "Job job-hold-until value changed by user.");
6892 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Held by \"%s\".", jobid
,
6895 con
->response
->request
.status
.status_code
= IPP_OK
;
6900 * 'move_job()' - Move a job to a new destination.
6904 move_job(cupsd_client_t
*con
, /* I - Client connection */
6905 ipp_attribute_t
*uri
) /* I - Job URI */
6907 http_status_t status
; /* Policy status */
6908 ipp_attribute_t
*attr
; /* Current attribute */
6909 int jobid
; /* Job ID */
6910 cupsd_job_t
*job
; /* Current job */
6911 const char *src
; /* Source printer/class */
6912 cups_ptype_t stype
, /* Source type (printer or class) */
6913 dtype
; /* Destination type (printer/class) */
6914 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6915 username
[HTTP_MAX_URI
], /* Username portion of URI */
6916 host
[HTTP_MAX_URI
], /* Host portion of URI */
6917 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6918 int port
; /* Port portion of URI */
6919 cupsd_printer_t
*sprinter
, /* Source printer */
6920 *dprinter
; /* Destination printer */
6923 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
6924 uri
->values
[0].string
.text
);
6927 * Get the new printer or class...
6930 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
6931 IPP_TAG_URI
)) == NULL
)
6934 * Need job-printer-uri...
6937 send_ipp_status(con
, IPP_BAD_REQUEST
,
6938 _("job-printer-uri attribute missing!"));
6942 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
6948 send_ipp_status(con
, IPP_NOT_FOUND
,
6949 _("The printer or class was not found."));
6957 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
6960 send_http_error(con
, status
, dprinter
);
6965 * See if we have a job URI or a printer URI...
6968 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6969 sizeof(scheme
), username
, sizeof(username
), host
,
6970 sizeof(host
), &port
, resource
, sizeof(resource
));
6972 if (!strcmp(uri
->name
, "printer-uri"))
6975 * Got a printer URI; see if we also have a job-id attribute...
6978 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6979 IPP_TAG_INTEGER
)) == NULL
)
6985 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
6986 &sprinter
)) == NULL
)
6992 send_ipp_status(con
, IPP_NOT_FOUND
,
6993 _("The printer or class was not found."));
7002 * Otherwise, just move a single job...
7005 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7008 * Nope - return a "not found" error...
7011 send_ipp_status(con
, IPP_NOT_FOUND
,
7012 _("Job #%d does not exist!"), attr
->values
[0].integer
);
7018 * Job found, initialize source pointers...
7029 * Got a job URI; parse it to get the job ID...
7032 if (strncmp(resource
, "/jobs/", 6))
7038 send_ipp_status(con
, IPP_BAD_REQUEST
,
7039 _("Bad job-uri attribute \"%s\"!"),
7040 uri
->values
[0].string
.text
);
7045 * See if the job exists...
7048 jobid
= atoi(resource
+ 6);
7050 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7053 * Nope - return a "not found" error...
7056 send_ipp_status(con
, IPP_NOT_FOUND
,
7057 _("Job #%d does not exist!"), jobid
);
7063 * Job found, initialize source pointers...
7072 * Now move the job or jobs...
7078 * See if the job has been completed...
7081 if (job
->state_value
> IPP_JOB_STOPPED
)
7084 * Return a "not-possible" error...
7087 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7088 _("Job #%d is finished and cannot be altered!"),
7094 * See if the job is owned by the requesting user...
7097 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7099 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
7104 * Move the job to a different printer or class...
7107 cupsdMoveJob(job
, dprinter
);
7112 * Got the source printer, now look through the jobs...
7115 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
7117 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
7120 * See if the job is pointing at the source printer or has not been
7124 if (strcasecmp(job
->dest
, src
) ||
7125 job
->state_value
> IPP_JOB_STOPPED
)
7129 * See if the job can be moved by the requesting user...
7132 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7136 * Move the job to a different printer or class...
7139 cupsdMoveJob(job
, dprinter
);
7144 * Start jobs if possible...
7150 * Return with "everything is OK" status...
7153 con
->response
->request
.status
.status_code
= IPP_OK
;
7158 * 'ppd_parse_line()' - Parse a PPD default line.
7161 static int /* O - 0 on success, -1 on failure */
7162 ppd_parse_line(const char *line
, /* I - Line */
7163 char *option
, /* O - Option name */
7164 int olen
, /* I - Size of option name */
7165 char *choice
, /* O - Choice name */
7166 int clen
) /* I - Size of choice name */
7169 * Verify this is a default option line...
7172 if (strncmp(line
, "*Default", 8))
7176 * Read the option name...
7179 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
7189 * Skip everything else up to the colon (:)...
7192 while (*line
&& *line
!= ':')
7201 * Now grab the option choice, skipping leading whitespace...
7204 while (isspace(*line
& 255))
7207 for (clen
--; isalnum(*line
& 255); line
++)
7217 * Return with no errors...
7225 * 'print_job()' - Print a file to a printer or class.
7229 print_job(cupsd_client_t
*con
, /* I - Client connection */
7230 ipp_attribute_t
*uri
) /* I - Printer URI */
7232 ipp_attribute_t
*attr
; /* Current attribute */
7233 ipp_attribute_t
*format
; /* Document-format attribute */
7234 const char *default_format
; /* document-format-default value */
7235 cupsd_job_t
*job
; /* New job */
7236 char filename
[1024]; /* Job filename */
7237 mime_type_t
*filetype
; /* Type of file */
7238 char super
[MIME_MAX_SUPER
], /* Supertype of file */
7239 type
[MIME_MAX_TYPE
], /* Subtype of file */
7240 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7241 /* Textual name of mime type */
7242 cupsd_printer_t
*printer
; /* Printer data */
7243 struct stat fileinfo
; /* File information */
7244 int kbytes
; /* Size of file */
7245 int compression
; /* Document compression */
7248 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
7249 uri
->values
[0].string
.text
);
7252 * Validate print file attributes, for now just document-format and
7253 * compression (CUPS only supports "none" and "gzip")...
7256 compression
= CUPS_FILE_NONE
;
7258 if ((attr
= ippFindAttribute(con
->request
, "compression",
7259 IPP_TAG_KEYWORD
)) != NULL
)
7261 if (strcmp(attr
->values
[0].string
.text
, "none")
7263 && strcmp(attr
->values
[0].string
.text
, "gzip")
7264 #endif /* HAVE_LIBZ */
7267 send_ipp_status(con
, IPP_ATTRIBUTES
,
7268 _("Unsupported compression \"%s\"!"),
7269 attr
->values
[0].string
.text
);
7270 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7271 "compression", NULL
, attr
->values
[0].string
.text
);
7276 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7277 compression
= CUPS_FILE_GZIP
;
7278 #endif /* HAVE_LIBZ */
7282 * Do we have a file to print?
7287 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7292 * Is the destination valid?
7295 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
7301 send_ipp_status(con
, IPP_NOT_FOUND
,
7302 _("The printer or class was not found."));
7307 * Is it a format we support?
7310 if ((format
= ippFindAttribute(con
->request
, "document-format",
7311 IPP_TAG_MIMETYPE
)) != NULL
)
7314 * Grab format from client...
7317 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
7320 send_ipp_status(con
, IPP_BAD_REQUEST
,
7321 _("Could not scan type \"%s\"!"),
7322 format
->values
[0].string
.text
);
7326 else if ((default_format
= cupsGetOption("document-format",
7327 printer
->num_options
,
7328 printer
->options
)) != NULL
)
7331 * Use default document format...
7334 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
7336 send_ipp_status(con
, IPP_BAD_REQUEST
,
7337 _("Could not scan type \"%s\"!"),
7348 strcpy(super
, "application");
7349 strcpy(type
, "octet-stream");
7352 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7355 * Auto-type the file...
7358 ipp_attribute_t
*doc_name
; /* document-name attribute */
7361 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
7363 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7364 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7365 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7369 filetype
= mimeType(MimeDatabase
, super
, type
);
7372 filetype
= mimeType(MimeDatabase
, super
, type
);
7376 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
7379 * Replace the document-format attribute value with the auto-typed or
7383 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7388 _cupsStrFree(format
->values
[0].string
.text
);
7390 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7393 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7394 "document-format", NULL
, mimetype
);
7398 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7399 _("Unsupported format \'%s/%s\'!"), super
, type
);
7400 cupsdLogMessage(CUPSD_LOG_INFO
,
7401 "Hint: Do you have the raw file printing rules enabled?");
7404 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7405 "document-format", NULL
, format
->values
[0].string
.text
);
7410 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: request file type is %s/%s.",
7411 filetype
->super
, filetype
->type
);
7414 * Read any embedded job ticket info from PS files...
7417 if (!strcasecmp(filetype
->super
, "application") &&
7418 !strcasecmp(filetype
->type
, "postscript"))
7419 read_ps_job_ticket(con
);
7422 * Create the job object...
7425 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
7429 * Update quota data...
7432 if (stat(con
->filename
, &fileinfo
))
7435 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7437 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7439 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7440 IPP_TAG_INTEGER
)) != NULL
)
7441 attr
->values
[0].integer
+= kbytes
;
7444 * Add the job file...
7447 if (add_file(con
, job
, filetype
, compression
))
7450 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
7452 rename(con
->filename
, filename
);
7453 cupsdClearString(&con
->filename
);
7456 * See if we need to add the ending sheet...
7459 cupsdTimeoutJob(job
);
7462 * Log and save the job...
7465 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
7466 job
->id
, job
->dest
, job
->username
);
7467 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] hold_until = %d", job
->id
,
7468 (int)job
->hold_until
);
7473 * Start the job if possible...
7481 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
7483 * This function only gets called when printing a single PostScript
7484 * file using the Print-Job operation. It doesn't work for Create-Job +
7485 * Send-File, since the job attributes need to be set at job creation
7486 * time for banners to work. The embedded PS job ticket stuff is here
7487 * only to allow the Windows printer driver for CUPS to pass in JCL
7488 * options and IPP attributes which otherwise would be lost.
7490 * The format of a PS job ticket is simple:
7492 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
7494 * %cupsJobTicket: attr1=value1
7495 * %cupsJobTicket: attr2=value2
7497 * %cupsJobTicket: attrN=valueN
7499 * Job ticket lines must appear immediately after the first line that
7500 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
7501 * looking for job ticket info when it finds a line that does not begin
7502 * with "%cupsJobTicket:".
7504 * The maximum length of a job ticket line, including the prefix, is
7505 * 255 characters to conform with the Adobe DSC.
7507 * Read-only attributes are rejected with a notice to the error log in
7508 * case a malicious user tries anything. Since the job ticket is read
7509 * prior to attribute validation in print_job(), job ticket attributes
7510 * will go through the same validation as IPP attributes...
7514 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
7516 cups_file_t
*fp
; /* File to read from */
7517 char line
[256]; /* Line data */
7518 int num_options
; /* Number of options */
7519 cups_option_t
*options
; /* Options */
7520 ipp_t
*ticket
; /* New attributes */
7521 ipp_attribute_t
*attr
, /* Current attribute */
7522 *attr2
, /* Job attribute */
7523 *prev2
; /* Previous job attribute */
7527 * First open the print file...
7530 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
7532 cupsdLogMessage(CUPSD_LOG_ERROR
,
7533 "read_ps_job_ticket: Unable to open PostScript print file "
7540 * Skip the first line...
7543 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
7545 cupsdLogMessage(CUPSD_LOG_ERROR
,
7546 "read_ps_job_ticket: Unable to read from PostScript print "
7553 if (strncmp(line
, "%!PS-Adobe-", 11))
7556 * Not a DSC-compliant file, so no job ticket info will be available...
7564 * Read job ticket info from the file...
7570 while (cupsFileGets(fp
, line
, sizeof(line
)))
7573 * Stop at the first non-ticket line...
7576 if (strncmp(line
, "%cupsJobTicket:", 15))
7580 * Add the options to the option array...
7583 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
7587 * Done with the file; see if we have any options...
7592 if (num_options
== 0)
7596 * OK, convert the options to an attribute list, and apply them to
7601 cupsEncodeOptions(ticket
, num_options
, options
);
7604 * See what the user wants to change.
7607 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
7609 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
7612 if (!strcmp(attr
->name
, "job-originating-host-name") ||
7613 !strcmp(attr
->name
, "job-originating-user-name") ||
7614 !strcmp(attr
->name
, "job-media-sheets-completed") ||
7615 !strcmp(attr
->name
, "job-k-octets") ||
7616 !strcmp(attr
->name
, "job-id") ||
7617 !strncmp(attr
->name
, "job-state", 9) ||
7618 !strncmp(attr
->name
, "time-at-", 8))
7619 continue; /* Read-only attrs */
7621 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
7622 IPP_TAG_ZERO
)) != NULL
)
7625 * Some other value; first free the old value...
7628 if (con
->request
->attrs
== attr2
)
7630 con
->request
->attrs
= attr2
->next
;
7635 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
7636 if (prev2
->next
== attr2
)
7638 prev2
->next
= attr2
->next
;
7643 if (con
->request
->last
== attr2
)
7644 con
->request
->last
= prev2
;
7646 _ippFreeAttr(attr2
);
7650 * Add new option by copying it...
7653 copy_attribute(con
->request
, attr
, 0);
7657 * Then free the attribute list and option array...
7661 cupsFreeOptions(num_options
, options
);
7666 * 'reject_jobs()' - Reject print jobs to a printer.
7670 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
7671 ipp_attribute_t
*uri
) /* I - Printer or class URI */
7673 http_status_t status
; /* Policy status */
7674 cups_ptype_t dtype
; /* Destination type (printer/class) */
7675 cupsd_printer_t
*printer
; /* Printer data */
7676 ipp_attribute_t
*attr
; /* printer-state-message text */
7679 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
7680 con
->http
.fd
, uri
->values
[0].string
.text
);
7683 * Is the destination valid?
7686 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7692 send_ipp_status(con
, IPP_NOT_FOUND
,
7693 _("The printer or class was not found."));
7701 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7703 send_http_error(con
, status
, printer
);
7708 * Reject jobs sent to the printer...
7711 printer
->accepting
= 0;
7713 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
7714 IPP_TAG_TEXT
)) == NULL
)
7715 strcpy(printer
->state_message
, "Rejecting Jobs");
7717 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
7718 sizeof(printer
->state_message
));
7720 cupsdAddPrinterHistory(printer
);
7722 if (dtype
& CUPS_PRINTER_CLASS
)
7724 cupsdSaveAllClasses();
7726 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
7727 printer
->name
, get_username(con
));
7731 cupsdSaveAllPrinters();
7733 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
7734 printer
->name
, get_username(con
));
7738 * Everything was ok, so return OK status...
7741 con
->response
->request
.status
.status_code
= IPP_OK
;
7746 * 'release_job()' - Release a held print job.
7750 release_job(cupsd_client_t
*con
, /* I - Client connection */
7751 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7753 ipp_attribute_t
*attr
; /* Current attribute */
7754 int jobid
; /* Job ID */
7755 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7756 username
[HTTP_MAX_URI
], /* Username portion of URI */
7757 host
[HTTP_MAX_URI
], /* Host portion of URI */
7758 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7759 int port
; /* Port portion of URI */
7760 cupsd_job_t
*job
; /* Job information */
7763 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
7764 con
->http
.fd
, uri
->values
[0].string
.text
);
7767 * See if we have a job URI or a printer URI...
7770 if (!strcmp(uri
->name
, "printer-uri"))
7773 * Got a printer URI; see if we also have a job-id attribute...
7776 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7777 IPP_TAG_INTEGER
)) == NULL
)
7779 send_ipp_status(con
, IPP_BAD_REQUEST
,
7780 _("Got a printer-uri attribute but no job-id!"));
7784 jobid
= attr
->values
[0].integer
;
7789 * Got a job URI; parse it to get the job ID...
7792 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7793 sizeof(method
), username
, sizeof(username
), host
,
7794 sizeof(host
), &port
, resource
, sizeof(resource
));
7796 if (strncmp(resource
, "/jobs/", 6))
7802 send_ipp_status(con
, IPP_BAD_REQUEST
,
7803 _("Bad job-uri attribute \"%s\"!"),
7804 uri
->values
[0].string
.text
);
7808 jobid
= atoi(resource
+ 6);
7812 * See if the job exists...
7815 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7818 * Nope - return a "not found" error...
7821 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7826 * See if job is "held"...
7829 if (job
->state_value
!= IPP_JOB_HELD
)
7832 * Nope - return a "not possible" error...
7835 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
7840 * See if the job is owned by the requesting user...
7843 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7845 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
7850 * Reset the job-hold-until value to "no-hold"...
7853 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7854 IPP_TAG_KEYWORD
)) == NULL
)
7855 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7859 _cupsStrFree(attr
->values
[0].string
.text
);
7861 attr
->value_tag
= IPP_TAG_KEYWORD
;
7862 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
7864 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
7865 "Job job-hold-until value changed by user.");
7869 * Release the job and return...
7872 cupsdReleaseJob(job
);
7874 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
7875 "Job released by user.");
7877 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Released by \"%s\".", jobid
,
7880 con
->response
->request
.status
.status_code
= IPP_OK
;
7885 * 'renew_subscription()' - Renew an existing subscription...
7890 cupsd_client_t
*con
, /* I - Client connection */
7891 int sub_id
) /* I - Subscription ID */
7893 http_status_t status
; /* Policy status */
7894 cupsd_subscription_t
*sub
; /* Subscription */
7895 ipp_attribute_t
*lease
; /* notify-lease-duration */
7898 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7899 "renew_subscription(con=%p[%d], sub_id=%d)",
7900 con
, con
->http
.fd
, sub_id
);
7903 * Is the subscription ID valid?
7906 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7909 * Bad subscription ID...
7912 send_ipp_status(con
, IPP_NOT_FOUND
,
7913 _("notify-subscription-id %d no good!"), sub_id
);
7920 * Job subscriptions cannot be renewed...
7923 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7924 _("Job subscriptions cannot be renewed!"));
7932 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7934 con
, sub
->owner
)) != HTTP_OK
)
7936 send_http_error(con
, status
, sub
->dest
);
7941 * Renew the subscription...
7944 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
7947 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
7949 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
7951 cupsdLogMessage(CUPSD_LOG_INFO
,
7952 "renew_subscription: Limiting notify-lease-duration to "
7955 sub
->lease
= MaxLeaseDuration
;
7958 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
7960 cupsdSaveAllSubscriptions();
7962 con
->response
->request
.status
.status_code
= IPP_OK
;
7964 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
7965 "notify-lease-duration", sub
->lease
);
7970 * 'restart_job()' - Restart an old print job.
7974 restart_job(cupsd_client_t
*con
, /* I - Client connection */
7975 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7977 ipp_attribute_t
*attr
; /* Current attribute */
7978 int jobid
; /* Job ID */
7979 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7980 username
[HTTP_MAX_URI
], /* Username portion of URI */
7981 host
[HTTP_MAX_URI
], /* Host portion of URI */
7982 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7983 int port
; /* Port portion of URI */
7984 cupsd_job_t
*job
; /* Job information */
7987 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
7988 con
->http
.fd
, uri
->values
[0].string
.text
);
7991 * See if we have a job URI or a printer URI...
7994 if (!strcmp(uri
->name
, "printer-uri"))
7997 * Got a printer URI; see if we also have a job-id attribute...
8000 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8001 IPP_TAG_INTEGER
)) == NULL
)
8003 send_ipp_status(con
, IPP_BAD_REQUEST
,
8004 _("Got a printer-uri attribute but no job-id!"));
8008 jobid
= attr
->values
[0].integer
;
8013 * Got a job URI; parse it to get the job ID...
8016 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8017 sizeof(method
), username
, sizeof(username
), host
,
8018 sizeof(host
), &port
, resource
, sizeof(resource
));
8020 if (strncmp(resource
, "/jobs/", 6))
8026 send_ipp_status(con
, IPP_BAD_REQUEST
,
8027 _("Bad job-uri attribute \"%s\"!"),
8028 uri
->values
[0].string
.text
);
8032 jobid
= atoi(resource
+ 6);
8036 * See if the job exists...
8039 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8042 * Nope - return a "not found" error...
8045 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8050 * See if job is in any of the "completed" states...
8053 if (job
->state_value
<= IPP_JOB_PROCESSING
)
8056 * Nope - return a "not possible" error...
8059 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
8065 * See if we have retained the job files...
8070 if (!job
->attrs
|| job
->num_files
== 0)
8073 * Nope - return a "not possible" error...
8076 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8077 _("Job #%d cannot be restarted - no files!"), jobid
);
8082 * See if the job is owned by the requesting user...
8085 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8087 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
8092 * Restart the job and return...
8095 cupsdRestartJob(job
);
8097 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Restarted by \"%s\".", jobid
,
8100 con
->response
->request
.status
.status_code
= IPP_OK
;
8105 * 'save_auth_info()' - Save authentication information for a job.
8110 cupsd_client_t
*con
, /* I - Client connection */
8111 cupsd_job_t
*job
, /* I - Job */
8112 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
8114 int i
; /* Looping var */
8115 char filename
[1024]; /* Job authentication filename */
8116 cups_file_t
*fp
; /* Job authentication file */
8117 char line
[2048]; /* Line for file */
8118 cupsd_printer_t
*dest
; /* Destination printer/class */
8122 * This function saves the in-memory authentication information for
8123 * a job so that it can be used to authenticate with a remote host.
8124 * The information is stored in a file that is readable only by the
8125 * root user. The fields are Base-64 encoded, each on a separate line,
8126 * followed by random number (up to 1024) of newlines to limit the
8127 * amount of information that is exposed.
8129 * Because of the potential for exposing of authentication information,
8130 * this functionality is only enabled when running cupsd as root.
8132 * This caching only works for the Basic and BasicDigest authentication
8133 * types. Digest authentication cannot be cached this way, and in
8134 * the future Kerberos authentication may make all of this obsolete.
8136 * Authentication information is saved whenever an authenticated
8137 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
8140 * This information is deleted after a job is completed or canceled,
8141 * so reprints may require subsequent re-authentication.
8147 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
8151 * Create the authentication file and change permissions...
8154 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
8155 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
8157 cupsdLogMessage(CUPSD_LOG_ERROR
,
8158 "Unable to save authentication info to \"%s\" - %s",
8159 filename
, strerror(errno
));
8163 fchown(cupsFileNumber(fp
), 0, 0);
8164 fchmod(cupsFileNumber(fp
), 0400);
8166 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
8169 * Write 1 to 3 auth values...
8172 cupsdClearString(&job
->auth_username
);
8173 cupsdClearString(&job
->auth_domain
);
8174 cupsdClearString(&job
->auth_password
);
8176 for (i
= 0; i
< auth_info
->num_values
; i
++)
8178 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
8179 strlen(auth_info
->values
[i
].string
.text
));
8180 cupsFilePrintf(fp
, "%s\n", line
);
8182 if (!strcmp(dest
->auth_info_required
[i
], "username"))
8183 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s",
8184 auth_info
->values
[i
].string
.text
);
8185 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
8186 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s",
8187 auth_info
->values
[i
].string
.text
);
8188 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
8189 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s",
8190 auth_info
->values
[i
].string
.text
);
8193 else if (con
->username
[0])
8196 * Write the authenticated username...
8199 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
8200 cupsFilePrintf(fp
, "%s\n", line
);
8202 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", con
->username
);
8203 cupsdClearString(&job
->auth_domain
);
8206 * Write the authenticated password...
8209 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
8210 cupsFilePrintf(fp
, "%s\n", line
);
8212 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", con
->password
);
8216 * Write a random number of newlines to the end of the file...
8219 for (i
= (rand() % 1024); i
>= 0; i
--)
8220 cupsFilePutChar(fp
, '\n');
8223 * Close the file and return...
8228 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8229 if (con
->gss_have_creds
)
8230 save_krb5_creds(con
, job
);
8231 else if (job
->ccname
)
8232 cupsdClearString(&(job
->ccname
));
8233 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8237 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8239 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
8243 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
8244 cupsd_job_t
*job
) /* I - Job */
8246 krb5_context krb_context
; /* Kerberos context */
8247 krb5_ccache ccache
; /* Credentials cache */
8248 OM_uint32 major_status
, /* Major status code */
8249 minor_status
; /* Minor status code */
8254 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
8258 if (krb5_init_context
== NULL
)
8260 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8261 "save_krb5_creds: GSSAPI/Kerberos framework is not "
8265 # endif /* __APPLE__ */
8268 * Setup a cached context for the job filters to use...
8271 if (krb5_init_context(&krb_context
))
8273 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize Kerberos context");
8278 * We MUST create a file-based cache because memory-based caches are
8279 * only valid for the current process/address space.
8282 # ifdef HAVE_KRB5_CC_RESOLVE
8283 if (krb5_cc_resolve(krb_context
, "FILE:", &ccache
))
8284 # elif defined(HAVE_HEIMDAL)
8285 if (krb5_cc_gen_new(krb_context
, &krb5_fcc_ops
, &ccache
))
8287 if (krb5_cc_gen_new(krb_context
, &ccache
))
8288 # endif /* HAVE_HEIMDAL */
8290 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create new credentials");
8294 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
8297 if (GSS_ERROR(major_status
))
8299 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
8300 "Unable to import client credentials cache");
8301 krb5_cc_destroy(krb_context
, ccache
);
8305 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
8306 krb5_cc_get_name(krb_context
, ccache
));
8308 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] save_krb5_creds: %s", job
->id
,
8311 krb5_cc_close(krb_context
, ccache
);
8313 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8317 * 'send_document()' - Send a file to a printer or class.
8321 send_document(cupsd_client_t
*con
, /* I - Client connection */
8322 ipp_attribute_t
*uri
) /* I - Printer URI */
8324 ipp_attribute_t
*attr
; /* Current attribute */
8325 ipp_attribute_t
*format
; /* Document-format attribute */
8326 const char *default_format
;/* document-format-default value */
8327 int jobid
; /* Job ID number */
8328 cupsd_job_t
*job
; /* Current job */
8329 char job_uri
[HTTP_MAX_URI
],
8331 method
[HTTP_MAX_URI
],
8332 /* Method portion of URI */
8333 username
[HTTP_MAX_URI
],
8334 /* Username portion of URI */
8336 /* Host portion of URI */
8337 resource
[HTTP_MAX_URI
];
8338 /* Resource portion of URI */
8339 int port
; /* Port portion of URI */
8340 mime_type_t
*filetype
; /* Type of file */
8341 char super
[MIME_MAX_SUPER
],
8342 /* Supertype of file */
8343 type
[MIME_MAX_TYPE
],
8344 /* Subtype of file */
8345 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8346 /* Textual name of mime type */
8347 char filename
[1024]; /* Job filename */
8348 cupsd_printer_t
*printer
; /* Current printer */
8349 struct stat fileinfo
; /* File information */
8350 int kbytes
; /* Size of file */
8351 int compression
; /* Type of compression */
8354 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
8355 con
->http
.fd
, uri
->values
[0].string
.text
);
8358 * See if we have a job URI or a printer URI...
8361 if (!strcmp(uri
->name
, "printer-uri"))
8364 * Got a printer URI; see if we also have a job-id attribute...
8367 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8368 IPP_TAG_INTEGER
)) == NULL
)
8370 send_ipp_status(con
, IPP_BAD_REQUEST
,
8371 _("Got a printer-uri attribute but no job-id!"));
8375 jobid
= attr
->values
[0].integer
;
8380 * Got a job URI; parse it to get the job ID...
8383 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8384 sizeof(method
), username
, sizeof(username
), host
,
8385 sizeof(host
), &port
, resource
, sizeof(resource
));
8387 if (strncmp(resource
, "/jobs/", 6))
8393 send_ipp_status(con
, IPP_BAD_REQUEST
,
8394 _("Bad job-uri attribute \"%s\"!"),
8395 uri
->values
[0].string
.text
);
8399 jobid
= atoi(resource
+ 6);
8403 * See if the job exists...
8406 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8409 * Nope - return a "not found" error...
8412 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8416 printer
= cupsdFindDest(job
->dest
);
8419 * See if the job is owned by the requesting user...
8422 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8424 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
8429 * OK, see if the client is sending the document compressed - CUPS
8430 * only supports "none" and "gzip".
8433 compression
= CUPS_FILE_NONE
;
8435 if ((attr
= ippFindAttribute(con
->request
, "compression",
8436 IPP_TAG_KEYWORD
)) != NULL
)
8438 if (strcmp(attr
->values
[0].string
.text
, "none")
8440 && strcmp(attr
->values
[0].string
.text
, "gzip")
8441 #endif /* HAVE_LIBZ */
8444 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
8445 attr
->values
[0].string
.text
);
8446 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8447 "compression", NULL
, attr
->values
[0].string
.text
);
8452 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8453 compression
= CUPS_FILE_GZIP
;
8454 #endif /* HAVE_LIBZ */
8458 * Do we have a file to print?
8463 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
8468 * Is it a format we support?
8471 if ((format
= ippFindAttribute(con
->request
, "document-format",
8472 IPP_TAG_MIMETYPE
)) != NULL
)
8475 * Grab format from client...
8478 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
8481 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
8482 format
->values
[0].string
.text
);
8486 else if ((default_format
= cupsGetOption("document-format",
8487 printer
->num_options
,
8488 printer
->options
)) != NULL
)
8491 * Use default document format...
8494 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
8496 send_ipp_status(con
, IPP_BAD_REQUEST
,
8497 _("Could not scan type \"%s\"!"),
8505 * No document format attribute? Auto-type it!
8508 strcpy(super
, "application");
8509 strcpy(type
, "octet-stream");
8512 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8515 * Auto-type the file...
8518 ipp_attribute_t
*doc_name
; /* document-name attribute */
8521 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
8523 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8524 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8525 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8529 filetype
= mimeType(MimeDatabase
, super
, type
);
8532 filetype
= mimeType(MimeDatabase
, super
, type
);
8536 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8539 * Replace the document-format attribute value with the auto-typed or
8543 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8548 _cupsStrFree(format
->values
[0].string
.text
);
8550 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8553 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8554 "document-format", NULL
, mimetype
);
8558 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8559 _("Unsupported format \'%s/%s\'!"), super
, type
);
8560 cupsdLogMessage(CUPSD_LOG_INFO
,
8561 "Hint: Do you have the raw file printing rules enabled?");
8564 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8565 "document-format", NULL
, format
->values
[0].string
.text
);
8570 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
8572 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8575 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8576 _("Unsupported format \'%s\'!"), mimetype
);
8578 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8579 "document-format", NULL
, mimetype
);
8584 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8585 "send_document: request file type is %s/%s.",
8586 filetype
->super
, filetype
->type
);
8589 * Add the file to the job...
8594 if (add_file(con
, job
, filetype
, compression
))
8597 if (stat(con
->filename
, &fileinfo
))
8600 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8602 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8604 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8605 IPP_TAG_INTEGER
)) != NULL
)
8606 attr
->values
[0].integer
+= kbytes
;
8608 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8610 rename(con
->filename
, filename
);
8612 cupsdClearString(&con
->filename
);
8614 cupsdLogMessage(CUPSD_LOG_INFO
,
8615 "File of type %s/%s queued in job #%d by \"%s\".",
8616 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
8619 * Start the job if this is the last document...
8622 if ((attr
= ippFindAttribute(con
->request
, "last-document",
8623 IPP_TAG_BOOLEAN
)) != NULL
&&
8624 attr
->values
[0].boolean
)
8627 * See if we need to add the ending sheet...
8630 cupsdTimeoutJob(job
);
8632 if (job
->state_value
== IPP_JOB_STOPPED
)
8634 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
8635 job
->state_value
= IPP_JOB_PENDING
;
8637 else if (job
->state_value
== IPP_JOB_HELD
)
8639 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8640 IPP_TAG_KEYWORD
)) == NULL
)
8641 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8643 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
8645 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
8646 job
->state_value
= IPP_JOB_PENDING
;
8653 * Start the job if possible... Since cupsdCheckJobs() can cancel a
8654 * job if it doesn't print, we need to re-find the job afterwards...
8661 job
= cupsdFindJob(jobid
);
8665 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8666 IPP_TAG_KEYWORD
)) == NULL
)
8667 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8669 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
8671 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
8672 job
->state_value
= IPP_JOB_HELD
;
8673 job
->hold_until
= time(NULL
) + 60;
8679 * Fill in the response info...
8682 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
8685 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
8688 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
8690 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
8691 job
? job
->state_value
: IPP_JOB_CANCELED
);
8692 add_job_state_reasons(con
, job
);
8694 con
->response
->request
.status
.status_code
= IPP_OK
;
8699 * 'send_http_error()' - Send a HTTP error back to the IPP client.
8704 cupsd_client_t
*con
, /* I - Client connection */
8705 http_status_t status
, /* I - HTTP status code */
8706 cupsd_printer_t
*printer
) /* I - Printer, if any */
8708 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
8709 ippOpString(con
->request
->request
.op
.operation_id
),
8710 httpStatus(status
));
8712 if (status
== HTTP_UNAUTHORIZED
&&
8713 printer
&& printer
->num_auth_info_required
> 0 &&
8714 !strcmp(printer
->auth_info_required
[0], "negotiate"))
8715 cupsdSendError(con
, status
, AUTH_NEGOTIATE
);
8717 cupsdSendError(con
, status
, AUTH_NONE
);
8719 ippDelete(con
->response
);
8720 con
->response
= NULL
;
8727 * 'send_ipp_status()' - Send a status back to the IPP client.
8731 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
8732 ipp_status_t status
, /* I - IPP status code */
8733 const char *message
, /* I - Status message */
8734 ...) /* I - Additional args as needed */
8736 va_list ap
; /* Pointer to additional args */
8737 char formatted
[1024]; /* Formatted errror message */
8740 va_start(ap
, message
);
8741 vsnprintf(formatted
, sizeof(formatted
),
8742 _cupsLangString(con
->language
, message
), ap
);
8745 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
8746 ippOpString(con
->request
->request
.op
.operation_id
),
8747 ippErrorString(status
), formatted
);
8749 con
->response
->request
.status
.status_code
= status
;
8751 if (ippFindAttribute(con
->response
, "attributes-charset",
8752 IPP_TAG_ZERO
) == NULL
)
8753 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
8754 "attributes-charset", NULL
, DefaultCharset
);
8756 if (ippFindAttribute(con
->response
, "attributes-natural-language",
8757 IPP_TAG_ZERO
) == NULL
)
8758 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
8759 "attributes-natural-language", NULL
, DefaultLanguage
);
8761 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
8762 "status-message", NULL
, formatted
);
8767 * 'set_default()' - Set the default destination...
8771 set_default(cupsd_client_t
*con
, /* I - Client connection */
8772 ipp_attribute_t
*uri
) /* I - Printer URI */
8774 http_status_t status
; /* Policy status */
8775 cups_ptype_t dtype
; /* Destination type (printer/class) */
8776 cupsd_printer_t
*printer
; /* Printer */
8779 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
8780 con
->http
.fd
, uri
->values
[0].string
.text
);
8783 * Is the destination valid?
8786 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8792 send_ipp_status(con
, IPP_NOT_FOUND
,
8793 _("The printer or class was not found."));
8801 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
8803 send_http_error(con
, status
, NULL
);
8808 * Set it as the default...
8811 DefaultPrinter
= printer
;
8813 cupsdSaveAllPrinters();
8814 cupsdSaveAllClasses();
8816 cupsdWritePrintcap();
8818 cupsdLogMessage(CUPSD_LOG_INFO
,
8819 "Default destination set to \"%s\" by \"%s\".",
8820 printer
->name
, get_username(con
));
8823 * Everything was ok, so return OK status...
8826 con
->response
->request
.status
.status_code
= IPP_OK
;
8831 * 'set_job_attrs()' - Set job attributes.
8835 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
8836 ipp_attribute_t
*uri
) /* I - Job URI */
8838 ipp_attribute_t
*attr
, /* Current attribute */
8839 *attr2
; /* Job attribute */
8840 int jobid
; /* Job ID */
8841 cupsd_job_t
*job
; /* Current job */
8842 char method
[HTTP_MAX_URI
],
8843 /* Method portion of URI */
8844 username
[HTTP_MAX_URI
],
8845 /* Username portion of URI */
8847 /* Host portion of URI */
8848 resource
[HTTP_MAX_URI
];
8849 /* Resource portion of URI */
8850 int port
; /* Port portion of URI */
8851 int event
; /* Events? */
8854 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
8855 con
->http
.fd
, uri
->values
[0].string
.text
);
8858 * Start with "everything is OK" status...
8861 con
->response
->request
.status
.status_code
= IPP_OK
;
8864 * See if we have a job URI or a printer URI...
8867 if (!strcmp(uri
->name
, "printer-uri"))
8870 * Got a printer URI; see if we also have a job-id attribute...
8873 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8874 IPP_TAG_INTEGER
)) == NULL
)
8876 send_ipp_status(con
, IPP_BAD_REQUEST
,
8877 _("Got a printer-uri attribute but no job-id!"));
8881 jobid
= attr
->values
[0].integer
;
8886 * Got a job URI; parse it to get the job ID...
8889 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8890 sizeof(method
), username
, sizeof(username
), host
,
8891 sizeof(host
), &port
, resource
, sizeof(resource
));
8893 if (strncmp(resource
, "/jobs/", 6))
8899 send_ipp_status(con
, IPP_BAD_REQUEST
,
8900 _("Bad job-uri attribute \"%s\"!"),
8901 uri
->values
[0].string
.text
);
8905 jobid
= atoi(resource
+ 6);
8909 * See if the job exists...
8912 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8915 * Nope - return a "not found" error...
8918 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8923 * See if the job has been completed...
8926 if (job
->state_value
> IPP_JOB_STOPPED
)
8929 * Return a "not-possible" error...
8932 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8933 _("Job #%d is finished and cannot be altered!"), jobid
);
8938 * See if the job is owned by the requesting user...
8941 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8943 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
8948 * See what the user wants to change.
8955 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8957 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8960 if (!strcmp(attr
->name
, "attributes-charset") ||
8961 !strcmp(attr
->name
, "attributes-natural-language") ||
8962 !strcmp(attr
->name
, "document-compression") ||
8963 !strcmp(attr
->name
, "document-format") ||
8964 !strcmp(attr
->name
, "job-detailed-status-messages") ||
8965 !strcmp(attr
->name
, "job-document-access-errors") ||
8966 !strcmp(attr
->name
, "job-id") ||
8967 !strcmp(attr
->name
, "job-k-octets") ||
8968 !strcmp(attr
->name
, "job-originating-host-name") ||
8969 !strcmp(attr
->name
, "job-originating-user-name") ||
8970 !strcmp(attr
->name
, "job-printer-up-time") ||
8971 !strcmp(attr
->name
, "job-printer-uri") ||
8972 !strcmp(attr
->name
, "job-sheets") ||
8973 !strcmp(attr
->name
, "job-state-message") ||
8974 !strcmp(attr
->name
, "job-state-reasons") ||
8975 !strcmp(attr
->name
, "job-uri") ||
8976 !strcmp(attr
->name
, "number-of-documents") ||
8977 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
8978 !strcmp(attr
->name
, "output-device-assigned") ||
8979 !strncmp(attr
->name
, "date-time-at-", 13) ||
8980 !strncmp(attr
->name
, "job-impressions", 15) ||
8981 !strncmp(attr
->name
, "job-k-octets", 12) ||
8982 !strncmp(attr
->name
, "job-media-sheets", 16) ||
8983 !strncmp(attr
->name
, "time-at-", 8))
8989 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
8990 _("%s cannot be changed."), attr
->name
);
8992 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8993 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8998 if (!strcmp(attr
->name
, "job-priority"))
9001 * Change the job priority...
9004 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9006 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
9008 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9009 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9011 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
9013 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9014 _("Job is completed and cannot be changed."));
9017 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9019 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
9020 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9023 else if (!strcmp(attr
->name
, "job-state"))
9026 * Change the job state...
9029 if (attr
->value_tag
!= IPP_TAG_ENUM
)
9031 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
9033 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9034 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9038 switch (attr
->values
[0].integer
)
9040 case IPP_JOB_PENDING
:
9042 if (job
->state_value
> IPP_JOB_HELD
)
9044 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9045 _("Job state cannot be changed."));
9048 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9050 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
9051 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
9053 event
|= CUPSD_EVENT_JOB_STATE
;
9057 case IPP_JOB_PROCESSING
:
9058 case IPP_JOB_STOPPED
:
9059 if (job
->state_value
!= attr
->values
[0].integer
)
9061 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9062 _("Job state cannot be changed."));
9067 case IPP_JOB_CANCELED
:
9068 case IPP_JOB_ABORTED
:
9069 case IPP_JOB_COMPLETED
:
9070 if (job
->state_value
> IPP_JOB_PROCESSING
)
9072 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9073 _("Job state cannot be changed."));
9076 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9077 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
9082 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
9084 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9085 IPP_TAG_ZERO
)) != NULL
)
9088 * Some other value; first free the old value...
9091 if (job
->attrs
->prev
)
9092 job
->attrs
->prev
->next
= attr2
->next
;
9094 job
->attrs
->attrs
= attr2
->next
;
9096 if (job
->attrs
->last
== attr2
)
9097 job
->attrs
->last
= job
->attrs
->prev
;
9099 _ippFreeAttr(attr2
);
9102 * Then copy the attribute...
9105 copy_attribute(job
->attrs
, attr
, 0);
9108 * See if the job-name or job-hold-until is being changed.
9111 if (!strcmp(attr
->name
, "job-hold-until"))
9113 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
9115 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
9116 cupsdReleaseJob(job
);
9120 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
9123 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
9126 * Delete the attribute...
9129 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9130 IPP_TAG_ZERO
)) != NULL
)
9132 if (job
->attrs
->prev
)
9133 job
->attrs
->prev
->next
= attr2
->next
;
9135 job
->attrs
->attrs
= attr2
->next
;
9137 if (attr2
== job
->attrs
->last
)
9138 job
->attrs
->last
= job
->attrs
->prev
;
9140 _ippFreeAttr(attr2
);
9142 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9148 * Add new option by copying it...
9151 copy_attribute(job
->attrs
, attr
, 0);
9153 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9164 * Send events as needed...
9167 if (event
& CUPSD_EVENT_JOB_STATE
)
9168 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
9169 job
->state_value
== IPP_JOB_HELD
?
9170 "Job held by user." : "Job restarted by user.");
9172 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
9173 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
9174 "Job options changed by user.");
9177 * Start jobs if possible...
9185 * 'set_printer_defaults()' - Set printer default options from a request.
9189 set_printer_defaults(
9190 cupsd_client_t
*con
, /* I - Client connection */
9191 cupsd_printer_t
*printer
) /* I - Printer */
9193 int i
; /* Looping var */
9194 ipp_attribute_t
*attr
; /* Current attribute */
9195 int namelen
; /* Length of attribute name */
9196 char name
[256], /* New attribute name */
9197 value
[256]; /* String version of integer attrs */
9200 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9203 * Skip non-printer attributes...
9206 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
9209 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
9211 if (!strcmp(attr
->name
, "job-sheets-default"))
9214 * Only allow keywords and names...
9217 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9221 * Only allow job-sheets-default to be set when running without a
9222 * system high classification level...
9228 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
9230 if (attr
->num_values
> 1)
9231 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
9233 cupsdSetString(&printer
->job_sheets
[1], "none");
9235 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
9237 cupsdFreePrinterUsers(printer
);
9239 printer
->deny_users
= 0;
9241 if (attr
->value_tag
== IPP_TAG_NAME
&&
9242 (attr
->num_values
> 1 ||
9243 strcmp(attr
->values
[0].string
.text
, "all")))
9245 for (i
= 0; i
< attr
->num_values
; i
++)
9246 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9249 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
9251 cupsdFreePrinterUsers(printer
);
9253 printer
->deny_users
= 1;
9255 if (attr
->value_tag
== IPP_TAG_NAME
&&
9256 (attr
->num_values
> 1 ||
9257 strcmp(attr
->values
[0].string
.text
, "none")))
9259 for (i
= 0; i
< attr
->num_values
; i
++)
9260 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9263 else if (!strcmp(attr
->name
, "job-quota-period"))
9265 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9268 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
9269 attr
->values
[0].integer
);
9270 cupsdFreeQuotas(printer
);
9272 printer
->quota_period
= attr
->values
[0].integer
;
9274 else if (!strcmp(attr
->name
, "job-k-limit"))
9276 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9279 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
9280 attr
->values
[0].integer
);
9281 cupsdFreeQuotas(printer
);
9283 printer
->k_limit
= attr
->values
[0].integer
;
9285 else if (!strcmp(attr
->name
, "job-page-limit"))
9287 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9290 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
9291 attr
->values
[0].integer
);
9292 cupsdFreeQuotas(printer
);
9294 printer
->page_limit
= attr
->values
[0].integer
;
9296 else if (!strcmp(attr
->name
, "printer-op-policy"))
9298 cupsd_policy_t
*p
; /* Policy */
9301 if (attr
->value_tag
!= IPP_TAG_NAME
)
9304 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
9306 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9307 "Setting printer-op-policy to \"%s\"...",
9308 attr
->values
[0].string
.text
);
9309 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
9310 printer
->op_policy_ptr
= p
;
9314 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9315 _("Unknown printer-op-policy \"%s\"."),
9316 attr
->values
[0].string
.text
);
9320 else if (!strcmp(attr
->name
, "printer-error-policy"))
9322 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9325 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
9326 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
9327 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
9329 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9330 _("Unknown printer-error-policy \"%s\"."),
9331 attr
->values
[0].string
.text
);
9335 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9336 "Setting printer-error-policy to \"%s\"...",
9337 attr
->values
[0].string
.text
);
9338 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
9342 * Skip any other non-default attributes...
9345 namelen
= strlen(attr
->name
);
9346 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
9347 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
9351 * OK, anything else must be a user-defined default...
9354 strlcpy(name
, attr
->name
, sizeof(name
));
9355 name
[namelen
- 8] = '\0'; /* Strip "-default" */
9357 switch (attr
->value_tag
)
9359 case IPP_TAG_DELETEATTR
:
9360 printer
->num_options
= cupsRemoveOption(name
,
9361 printer
->num_options
,
9362 &(printer
->options
));
9363 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9364 "Deleting %s", attr
->name
);
9368 case IPP_TAG_KEYWORD
:
9370 printer
->num_options
= cupsAddOption(name
,
9371 attr
->values
[0].string
.text
,
9372 printer
->num_options
,
9373 &(printer
->options
));
9374 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9375 "Setting %s to \"%s\"...", attr
->name
,
9376 attr
->values
[0].string
.text
);
9379 case IPP_TAG_BOOLEAN
:
9380 printer
->num_options
= cupsAddOption(name
,
9381 attr
->values
[0].boolean
?
9383 printer
->num_options
,
9384 &(printer
->options
));
9385 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9386 "Setting %s to %s...", attr
->name
,
9387 attr
->values
[0].boolean
? "true" : "false");
9390 case IPP_TAG_INTEGER
:
9392 sprintf(value
, "%d", attr
->values
[0].integer
);
9393 printer
->num_options
= cupsAddOption(name
, value
,
9394 printer
->num_options
,
9395 &(printer
->options
));
9396 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9397 "Setting %s to %s...", attr
->name
, value
);
9400 case IPP_TAG_RANGE
:
9401 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
9402 attr
->values
[0].range
.upper
);
9403 printer
->num_options
= cupsAddOption(name
, value
,
9404 printer
->num_options
,
9405 &(printer
->options
));
9406 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9407 "Setting %s to %s...", attr
->name
, value
);
9410 case IPP_TAG_RESOLUTION
:
9411 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
9412 attr
->values
[0].resolution
.yres
,
9413 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
9415 printer
->num_options
= cupsAddOption(name
, value
,
9416 printer
->num_options
,
9417 &(printer
->options
));
9418 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9419 "Setting %s to %s...", attr
->name
, value
);
9423 /* Do nothing for other values */
9431 * 'start_printer()' - Start a printer.
9435 start_printer(cupsd_client_t
*con
, /* I - Client connection */
9436 ipp_attribute_t
*uri
) /* I - Printer URI */
9438 http_status_t status
; /* Policy status */
9439 cups_ptype_t dtype
; /* Destination type (printer/class) */
9440 cupsd_printer_t
*printer
; /* Printer data */
9443 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
9444 con
->http
.fd
, uri
->values
[0].string
.text
);
9447 * Is the destination valid?
9450 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9456 send_ipp_status(con
, IPP_NOT_FOUND
,
9457 _("The printer or class was not found."));
9465 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9467 send_http_error(con
, status
, printer
);
9472 * Start the printer...
9475 printer
->state_message
[0] = '\0';
9477 cupsdStartPrinter(printer
, 1);
9479 if (dtype
& CUPS_PRINTER_CLASS
)
9480 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
9481 printer
->name
, get_username(con
));
9483 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
9484 printer
->name
, get_username(con
));
9489 * Everything was ok, so return OK status...
9492 con
->response
->request
.status
.status_code
= IPP_OK
;
9497 * 'stop_printer()' - Stop a printer.
9501 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
9502 ipp_attribute_t
*uri
) /* I - Printer URI */
9504 http_status_t status
; /* Policy status */
9505 cups_ptype_t dtype
; /* Destination type (printer/class) */
9506 cupsd_printer_t
*printer
; /* Printer data */
9507 ipp_attribute_t
*attr
; /* printer-state-message attribute */
9510 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
9511 con
->http
.fd
, uri
->values
[0].string
.text
);
9514 * Is the destination valid?
9517 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9523 send_ipp_status(con
, IPP_NOT_FOUND
,
9524 _("The printer or class was not found."));
9532 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9534 send_http_error(con
, status
, printer
);
9539 * Stop the printer...
9542 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
9543 IPP_TAG_TEXT
)) == NULL
)
9544 strcpy(printer
->state_message
, "Paused");
9547 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
9548 sizeof(printer
->state_message
));
9551 cupsdStopPrinter(printer
, 1);
9553 if (dtype
& CUPS_PRINTER_CLASS
)
9554 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
9555 printer
->name
, get_username(con
));
9557 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
9558 printer
->name
, get_username(con
));
9561 * Everything was ok, so return OK status...
9564 con
->response
->request
.status
.status_code
= IPP_OK
;
9569 * 'url_encode_attr()' - URL-encode a string attribute.
9573 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
9574 char *buffer
,/* I - String buffer */
9575 int bufsize
)/* I - Size of buffer */
9577 int i
; /* Looping var */
9578 char *bufptr
, /* Pointer into buffer */
9579 *bufend
; /* End of buffer */
9582 strlcpy(buffer
, attr
->name
, bufsize
);
9583 bufptr
= buffer
+ strlen(buffer
);
9584 bufend
= buffer
+ bufsize
- 1;
9586 for (i
= 0; i
< attr
->num_values
; i
++)
9588 if (bufptr
>= bufend
)
9596 if (bufptr
>= bufend
)
9601 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
9602 bufptr
, bufend
- bufptr
+ 1);
9604 if (bufptr
>= bufend
)
9615 * 'url_encode_string()' - URL-encode a string.
9618 static char * /* O - End of string */
9619 url_encode_string(const char *s
, /* I - String */
9620 char *buffer
, /* I - String buffer */
9621 int bufsize
) /* I - Size of buffer */
9623 char *bufptr
, /* Pointer into buffer */
9624 *bufend
; /* End of buffer */
9625 static const char *hex
= "0123456789ABCDEF";
9630 bufend
= buffer
+ bufsize
- 1;
9632 while (*s
&& bufptr
< bufend
)
9634 if (*s
== ' ' || *s
== '%')
9636 if (bufptr
>= (bufend
- 2))
9640 *bufptr
++ = hex
[(*s
>> 4) & 15];
9641 *bufptr
++ = hex
[*s
& 15];
9645 else if (*s
== '\'' || *s
== '\\')
9647 if (bufptr
>= (bufend
- 1))
9664 * 'user_allowed()' - See if a user is allowed to print to a queue.
9667 static int /* O - 0 if not allowed, 1 if allowed */
9668 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
9669 const char *username
) /* I - Username */
9671 int i
; /* Looping var */
9672 struct passwd
*pw
; /* User password data */
9675 if (p
->num_users
== 0)
9678 if (!strcmp(username
, "root"))
9681 pw
= getpwnam(username
);
9684 for (i
= 0; i
< p
->num_users
; i
++)
9686 if (p
->users
[i
][0] == '@')
9689 * Check group membership...
9692 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
9695 else if (!strcasecmp(username
, p
->users
[i
]))
9699 return ((i
< p
->num_users
) != p
->deny_users
);
9704 * 'validate_job()' - Validate printer options and destination.
9708 validate_job(cupsd_client_t
*con
, /* I - Client connection */
9709 ipp_attribute_t
*uri
) /* I - Printer URI */
9711 http_status_t status
; /* Policy status */
9712 ipp_attribute_t
*attr
; /* Current attribute */
9713 ipp_attribute_t
*format
; /* Document-format attribute */
9714 cups_ptype_t dtype
; /* Destination type (printer/class) */
9715 char super
[MIME_MAX_SUPER
],
9716 /* Supertype of file */
9717 type
[MIME_MAX_TYPE
];
9718 /* Subtype of file */
9719 cupsd_printer_t
*printer
; /* Printer */
9722 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
9723 con
->http
.fd
, uri
->values
[0].string
.text
);
9726 * OK, see if the client is sending the document compressed - CUPS
9727 * doesn't support compression yet...
9730 if ((attr
= ippFindAttribute(con
->request
, "compression",
9731 IPP_TAG_KEYWORD
)) != NULL
&&
9732 !strcmp(attr
->values
[0].string
.text
, "none"))
9734 send_ipp_status(con
, IPP_ATTRIBUTES
,
9735 _("Unsupported compression attribute %s!"),
9736 attr
->values
[0].string
.text
);
9737 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9738 "compression", NULL
, attr
->values
[0].string
.text
);
9743 * Is it a format we support?
9746 if ((format
= ippFindAttribute(con
->request
, "document-format",
9747 IPP_TAG_MIMETYPE
)) != NULL
)
9749 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
9752 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9753 format
->values
[0].string
.text
);
9757 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
9758 !mimeType(MimeDatabase
, super
, type
))
9760 cupsdLogMessage(CUPSD_LOG_INFO
,
9761 "Hint: Do you have the raw file printing rules enabled?");
9762 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9763 _("Unsupported format \"%s\"!"),
9764 format
->values
[0].string
.text
);
9765 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9766 "document-format", NULL
, format
->values
[0].string
.text
);
9772 * Is the destination valid?
9775 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9781 send_ipp_status(con
, IPP_NOT_FOUND
,
9782 _("The printer or class was not found."));
9790 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9792 send_http_error(con
, status
, printer
);
9797 * Everything was ok, so return OK status...
9800 con
->response
->request
.status
.status_code
= IPP_OK
;
9805 * 'validate_name()' - Make sure the printer name only contains valid chars.
9808 static int /* O - 0 if name is no good, 1 if good */
9809 validate_name(const char *name
) /* I - Name to check */
9811 const char *ptr
; /* Pointer into name */
9815 * Scan the whole name...
9818 for (ptr
= name
; *ptr
; ptr
++)
9819 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
9823 * All the characters are good; validate the length, too...
9826 return ((ptr
- name
) < 128);
9831 * 'validate_user()' - Validate the user for the request.
9834 static int /* O - 1 if permitted, 0 otherwise */
9835 validate_user(cupsd_job_t
*job
, /* I - Job */
9836 cupsd_client_t
*con
, /* I - Client connection */
9837 const char *owner
, /* I - Owner of job/resource */
9838 char *username
, /* O - Authenticated username */
9839 int userlen
) /* I - Length of username */
9841 cupsd_printer_t
*printer
; /* Printer for job */
9844 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9845 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
9847 job
? job
->id
: 0, con
->http
.fd
, owner
? owner
: "(null)",
9854 if (!con
|| !owner
|| !username
|| userlen
<= 0)
9858 * Get the best authenticated username that is available.
9861 strlcpy(username
, get_username(con
), userlen
);
9864 * Check the username against the owner...
9867 printer
= cupsdFindDest(job
->dest
);
9869 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
9870 con
, owner
) == HTTP_OK
);
9875 * End of "$Id: ipp.c 6689 2007-07-18 23:52:15Z mike $".