2 * "$Id: ipp.c 6508 2007-05-03 20:07:14Z mike $"
4 * IPP routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 * This file contains Kerberos support code, copyright 2006 by
11 * These coded instructions, statements, and computer programs are the
12 * property of Easy Software Products and are protected by Federal
13 * copyright law. Distribution and use rights are outlined in the file
14 * "LICENSE.txt" which should have been included with this file. If this
15 * file is missing or damaged please contact Easy Software Products
18 * Attn: CUPS Licensing Information
19 * Easy Software Products
20 * 44141 Airport View Drive, Suite 204
21 * Hollywood, Maryland 20636 USA
23 * Voice: (301) 373-9600
24 * EMail: cups-info@cups.org
25 * WWW: http://www.cups.org
29 * cupsdProcessIPPRequest() - Process an incoming IPP request...
30 * accept_jobs() - Accept print jobs to a printer.
31 * add_class() - Add a class to the system.
32 * add_file() - Add a file to a job.
33 * add_job() - Add a job to a print queue.
34 * add_job_state_reasons() - Add the "job-state-reasons" attribute based
35 * upon the job and printer state...
36 * add_job_subscriptions() - Add any subcriptions for a job.
37 * add_job_uuid() - Add job-uuid attribute to a job.
38 * add_printer() - Add a printer to the system.
39 * add_printer_state_reasons() - Add the "printer-state-reasons" attribute
40 * based upon the printer state...
41 * add_queued_job_count() - Add the "queued-job-count" attribute for
42 * apply_printer_defaults() - Apply printer default options to a job.
43 * authenticate_job() - Set job authentication info.
44 * cancel_all_jobs() - Cancel all print jobs.
45 * cancel_job() - Cancel a print job.
46 * cancel_subscription() - Cancel a subscription.
47 * check_quotas() - Check quotas for a printer and user.
48 * copy_attribute() - Copy a single attribute.
49 * copy_attrs() - Copy attributes from one request to another.
50 * copy_banner() - Copy a banner file to the requests directory
51 * for the specified job.
52 * copy_file() - Copy a PPD file or interface script...
53 * copy_model() - Copy a PPD model file, substituting default
55 * copy_job_attrs() - Copy job attributes.
56 * copy_printer_attrs() - Copy printer attributes.
57 * copy_subscription_attrs() - Copy subscription attributes.
58 * create_job() - Print a file to a printer or class.
59 * create_requested_array() - Create an array for the requested-attributes.
60 * create_subscription() - Create a notification subscription.
61 * delete_printer() - Remove a printer or class from the system.
62 * get_default() - Get the default destination.
63 * get_devices() - Get the list of available devices on the
65 * get_job_attrs() - Get job attributes.
66 * get_jobs() - Get a list of jobs for the specified printer.
67 * get_notifications() - Get events for a subscription.
68 * get_ppd() - Get a named PPD from the local system.
69 * get_ppds() - Get the list of PPD files on the local
71 * get_printer_attrs() - Get printer attributes.
72 * get_printers() - Get a list of printers.
73 * get_subscription_attrs() - Get subscription attributes.
74 * get_subscriptions() - Get subscriptions.
75 * get_username() - Get the username associated with a request.
76 * hold_job() - Hold a print job.
77 * move_job() - Move a job to a new destination.
78 * ppd_parse_line() - Parse a PPD default line.
79 * print_job() - Print a file to a printer or class.
80 * read_ps_line() - Read a line from a PS file...
81 * read_ps_job_ticket() - Reads a job ticket embedded in a PS file.
82 * reject_jobs() - Reject print jobs to a printer.
83 * release_job() - Release a held print job.
84 * restart_job() - Restart an old print job.
85 * save_auth_info() - Save authentication information for a job.
86 * save_krb5_creds() - Save Kerberos credentials for a job.
87 * send_document() - Send a file to a printer or class.
88 * send_http_error() - Send a HTTP error back to the IPP client.
89 * send_ipp_status() - Send a status back to the IPP client.
90 * set_default() - Set the default destination...
91 * set_job_attrs() - Set job attributes.
92 * set_printer_defaults() - Set printer default options from a request.
93 * start_printer() - Start a printer.
94 * stop_printer() - Stop a printer.
95 * url_encode_attr() - URL-encode a string attribute.
96 * url_encode_string() - URL-encode a string.
97 * user_allowed() - See if a user is allowed to print to a queue.
98 * validate_job() - Validate printer options and destination.
99 * validate_name() - Make sure the printer name only contains
101 * validate_user() - Validate the user for the request.
105 * Include necessary headers...
112 #endif /* HAVE_KRB5_H */
116 #endif /* HAVE_LIBPAPER */
119 # ifdef HAVE_MEMBERSHIP_H
120 # include <membership.h>
121 # endif /* HAVE_MEMBERSHIP_H */
122 # ifdef HAVE_MEMBERSHIPPRIV_H
123 # include <membershipPriv.h>
125 extern int mbr_user_name_to_uuid(const char* name
, uuid_t uu
);
126 extern int mbr_group_name_to_uuid(const char* name
, uuid_t uu
);
127 extern int mbr_check_membership_by_id(uuid_t user
, gid_t group
, int* ismember
);
128 # endif /* HAVE_MEMBERSHIPPRIV_H */
129 #endif /* __APPLE__ */
136 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
137 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
138 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
139 mime_type_t
*filetype
, int compression
);
140 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
141 mime_type_t
*filetype
);
142 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
143 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
144 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
145 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
146 static void add_printer_state_reasons(cupsd_client_t
*con
,
148 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
149 static void apply_printer_defaults(cupsd_printer_t
*printer
,
151 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
152 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
153 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
154 static void cancel_subscription(cupsd_client_t
*con
, int id
);
155 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
156 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
158 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
159 ipp_tag_t group
, int quickcopy
);
160 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
162 static int copy_file(const char *from
, const char *to
);
163 static int copy_model(cupsd_client_t
*con
, const char *from
,
165 static void copy_job_attrs(cupsd_client_t
*con
,
168 static void copy_printer_attrs(cupsd_client_t
*con
,
169 cupsd_printer_t
*printer
,
171 static void copy_subscription_attrs(cupsd_client_t
*con
,
172 cupsd_subscription_t
*sub
,
174 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
175 static cups_array_t
*create_requested_array(ipp_t
*request
);
176 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
177 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
178 static void get_default(cupsd_client_t
*con
);
179 static void get_devices(cupsd_client_t
*con
);
180 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
181 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
182 static void get_notifications(cupsd_client_t
*con
);
183 static void get_ppd(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
184 static void get_ppds(cupsd_client_t
*con
);
185 static void get_printers(cupsd_client_t
*con
, int type
);
186 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
187 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
188 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
189 static const char *get_username(cupsd_client_t
*con
);
190 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
191 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
192 static int ppd_parse_line(const char *line
, char *option
, int olen
,
193 char *choice
, int clen
);
194 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
195 static void read_ps_job_ticket(cupsd_client_t
*con
);
196 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
197 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
198 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
199 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
200 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
,
201 ipp_attribute_t
*auth_info
);
202 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
203 static void save_krb5_creds(cupsd_client_t
*con
, cupsd_job_t
*job
);
204 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
205 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
206 static void send_http_error(cupsd_client_t
*con
, http_status_t status
,
207 cupsd_printer_t
*printer
);
208 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
209 const char *message
, ...)
211 __attribute__ ((__format__ (__printf__
, 3, 4)))
212 # endif /* __GNUC__ */
214 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
215 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
216 static void set_printer_defaults(cupsd_client_t
*con
,
217 cupsd_printer_t
*printer
);
218 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
219 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
220 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
,
222 static char *url_encode_string(const char *s
, char *buffer
, int bufsize
);
223 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
224 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
225 static int validate_name(const char *name
);
226 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
,
227 const char *owner
, char *username
,
232 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request...
235 int /* O - 1 on success, 0 on failure */
236 cupsdProcessIPPRequest(
237 cupsd_client_t
*con
) /* I - Client connection */
239 ipp_tag_t group
; /* Current group tag */
240 ipp_attribute_t
*attr
; /* Current attribute */
241 ipp_attribute_t
*charset
; /* Character set attribute */
242 ipp_attribute_t
*language
; /* Language attribute */
243 ipp_attribute_t
*uri
; /* Printer URI attribute */
244 ipp_attribute_t
*username
; /* requesting-user-name attr */
245 int sub_id
; /* Subscription ID */
248 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
249 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
250 con
, con
->http
.fd
, con
->request
->request
.op
.operation_id
);
253 * First build an empty response message for this request...
256 con
->response
= ippNew();
258 con
->response
->request
.status
.version
[0] = con
->request
->request
.op
.version
[0];
259 con
->response
->request
.status
.version
[1] = con
->request
->request
.op
.version
[1];
260 con
->response
->request
.status
.request_id
= con
->request
->request
.op
.request_id
;
263 * Then validate the request header and required attributes...
266 if (con
->request
->request
.any
.version
[0] != 1)
269 * Return an error, since we only support IPP 1.x.
272 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
273 "%04X %s Bad request version number %d.%d",
274 IPP_VERSION_NOT_SUPPORTED
, con
->http
.hostname
,
275 con
->request
->request
.any
.version
[0],
276 con
->request
->request
.any
.version
[1]);
278 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
279 _("Bad request version number %d.%d!"),
280 con
->request
->request
.any
.version
[0],
281 con
->request
->request
.any
.version
[1]);
283 else if (!con
->request
->attrs
)
285 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
286 "%04X %s No attributes in request",
287 IPP_BAD_REQUEST
, con
->http
.hostname
);
289 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request!"));
294 * Make sure that the attributes are provided in the correct order and
295 * don't repeat groups...
298 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
301 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
304 * Out of order; return an error...
307 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
308 "%04X %s Attribute groups are out of order",
309 IPP_BAD_REQUEST
, con
->http
.hostname
);
311 send_ipp_status(con
, IPP_BAD_REQUEST
,
312 _("Attribute groups are out of order (%x < %x)!"),
313 attr
->group_tag
, group
);
317 group
= attr
->group_tag
;
322 * Then make sure that the first three attributes are:
325 * attributes-natural-language
326 * printer-uri/job-uri
329 attr
= con
->request
->attrs
;
330 if (attr
&& !strcmp(attr
->name
, "attributes-charset") &&
331 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
339 if (attr
&& !strcmp(attr
->name
, "attributes-natural-language") &&
340 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
345 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
346 IPP_TAG_URI
)) != NULL
)
348 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
349 IPP_TAG_URI
)) != NULL
)
351 else if (con
->request
->request
.op
.operation_id
== CUPS_GET_PPD
)
352 uri
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
);
357 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
358 "attributes-charset", NULL
, charset
->values
[0].string
.text
);
360 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
361 "attributes-charset", NULL
, DefaultCharset
);
364 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
365 "attributes-natural-language", NULL
,
366 language
->values
[0].string
.text
);
368 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
369 "attributes-natural-language", NULL
, DefaultLanguage
);
371 if (!charset
|| !language
||
373 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
374 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
375 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
376 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
377 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
380 * Return an error, since attributes-charset,
381 * attributes-natural-language, and printer-uri/job-uri are required
382 * for all operations.
387 cupsdLogMessage(CUPSD_LOG_ERROR
,
388 "Missing attributes-charset attribute!");
390 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
391 "%04X %s Missing attributes-charset attribute",
392 IPP_BAD_REQUEST
, con
->http
.hostname
);
397 cupsdLogMessage(CUPSD_LOG_ERROR
,
398 "Missing attributes-natural-language attribute!");
400 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
401 "%04X %s Missing attributes-natural-language attribute",
402 IPP_BAD_REQUEST
, con
->http
.hostname
);
407 cupsdLogMessage(CUPSD_LOG_ERROR
,
408 "Missing printer-uri, job-uri, or ppd-name "
411 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
412 "%04X %s Missing printer-uri, job-uri, or ppd-name "
413 "attribute", IPP_BAD_REQUEST
, con
->http
.hostname
);
416 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
418 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
419 cupsdLogMessage(CUPSD_LOG_DEBUG
,
420 "attr \"%s\": group_tag = %x, value_tag = %x",
421 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
424 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
426 send_ipp_status(con
, IPP_BAD_REQUEST
,
427 _("Missing required attributes!"));
432 * OK, all the checks pass so far; make sure requesting-user-name is
433 * not "root" from a remote host...
436 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
437 IPP_TAG_NAME
)) != NULL
)
440 * Check for root user...
443 if (!strcmp(username
->values
[0].string
.text
, "root") &&
444 strcasecmp(con
->http
.hostname
, "localhost") &&
445 strcmp(con
->username
, "root"))
448 * Remote unauthenticated user masquerading as local root...
451 _cupsStrFree(username
->values
[0].string
.text
);
452 username
->values
[0].string
.text
= _cupsStrAlloc(RemoteRoot
);
456 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
457 IPP_TAG_INTEGER
)) != NULL
)
458 sub_id
= attr
->values
[0].integer
;
463 * Then try processing the operation...
467 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
468 ippOpString(con
->request
->request
.op
.operation_id
),
469 uri
->values
[0].string
.text
);
471 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
472 ippOpString(con
->request
->request
.op
.operation_id
));
474 switch (con
->request
->request
.op
.operation_id
)
480 case IPP_VALIDATE_JOB
:
481 validate_job(con
, uri
);
484 case IPP_CREATE_JOB
:
485 create_job(con
, uri
);
488 case IPP_SEND_DOCUMENT
:
489 send_document(con
, uri
);
492 case IPP_CANCEL_JOB
:
493 cancel_job(con
, uri
);
496 case IPP_GET_JOB_ATTRIBUTES
:
497 get_job_attrs(con
, uri
);
504 case IPP_GET_PRINTER_ATTRIBUTES
:
505 get_printer_attrs(con
, uri
);
512 case IPP_RELEASE_JOB
:
513 release_job(con
, uri
);
516 case IPP_RESTART_JOB
:
517 restart_job(con
, uri
);
520 case IPP_PAUSE_PRINTER
:
521 stop_printer(con
, uri
);
524 case IPP_RESUME_PRINTER
:
525 start_printer(con
, uri
);
528 case IPP_PURGE_JOBS
:
529 cancel_all_jobs(con
, uri
);
532 case IPP_SET_JOB_ATTRIBUTES
:
533 set_job_attrs(con
, uri
);
536 case CUPS_GET_DEFAULT
:
540 case CUPS_GET_PRINTERS
:
541 get_printers(con
, 0);
544 case CUPS_GET_CLASSES
:
545 get_printers(con
, CUPS_PRINTER_CLASS
);
548 case CUPS_ADD_PRINTER
:
549 add_printer(con
, uri
);
552 case CUPS_DELETE_PRINTER
:
553 delete_printer(con
, uri
);
556 case CUPS_ADD_CLASS
:
560 case CUPS_DELETE_CLASS
:
561 delete_printer(con
, uri
);
564 case CUPS_ACCEPT_JOBS
:
565 case IPP_ENABLE_PRINTER
:
566 accept_jobs(con
, uri
);
569 case CUPS_REJECT_JOBS
:
570 case IPP_DISABLE_PRINTER
:
571 reject_jobs(con
, uri
);
574 case CUPS_SET_DEFAULT
:
575 set_default(con
, uri
);
578 case CUPS_GET_DEVICES
:
594 case CUPS_AUTHENTICATE_JOB
:
595 authenticate_job(con
, uri
);
598 case IPP_CREATE_PRINTER_SUBSCRIPTION
:
599 case IPP_CREATE_JOB_SUBSCRIPTION
:
600 create_subscription(con
, uri
);
603 case IPP_GET_SUBSCRIPTION_ATTRIBUTES
:
604 get_subscription_attrs(con
, sub_id
);
607 case IPP_GET_SUBSCRIPTIONS
:
608 get_subscriptions(con
, uri
);
611 case IPP_RENEW_SUBSCRIPTION
:
612 renew_subscription(con
, sub_id
);
615 case IPP_CANCEL_SUBSCRIPTION
:
616 cancel_subscription(con
, sub_id
);
619 case IPP_GET_NOTIFICATIONS
:
620 get_notifications(con
);
624 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
625 "%04X %s Operation %04X (%s) not supported",
626 IPP_OPERATION_NOT_SUPPORTED
, con
->http
.hostname
,
627 con
->request
->request
.op
.operation_id
,
628 ippOpString(con
->request
->request
.op
.operation_id
));
630 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
631 _("%s not supported!"),
632 ippOpString(con
->request
->request
.op
.operation_id
));
642 * Sending data from the scheduler...
645 cupsdLogMessage(CUPSD_LOG_DEBUG
,
646 "cupsdProcessIPPRequest: %d status_code=%x (%s)",
647 con
->http
.fd
, con
->response
->request
.status
.status_code
,
648 ippErrorString(con
->response
->request
.status
.status_code
));
650 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp", AUTH_NONE
))
652 #ifdef CUPSD_USE_CHUNKING
654 * Because older versions of CUPS (1.1.17 and older) and some IPP
655 * clients do not implement chunking properly, we cannot use
656 * chunking by default. This may become the default in future
657 * CUPS releases, or we might add a configuration directive for
661 if (con
->http
.version
== HTTP_1_1
)
663 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n") < 0)
666 if (cupsdFlushHeader(con
) < 0)
669 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
672 #endif /* CUPSD_USE_CHUNKING */
674 size_t length
; /* Length of response */
677 length
= ippLength(con
->response
);
679 if (con
->file
>= 0 && !con
->pipe_pid
)
681 struct stat fileinfo
; /* File information */
684 if (!fstat(con
->file
, &fileinfo
))
685 length
+= fileinfo
.st_size
;
688 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
689 CUPS_LLCAST length
) < 0)
692 if (cupsdFlushHeader(con
) < 0)
695 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
696 con
->http
.data_remaining
= length
;
698 if (con
->http
.data_remaining
<= INT_MAX
)
699 con
->http
._data_remaining
= con
->http
.data_remaining
;
701 con
->http
._data_remaining
= INT_MAX
;
704 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
705 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
708 * Tell the caller the response header was sent successfully...
716 * Tell the caller the response header could not be sent...
725 * Sending data from a subprocess like cups-deviced; tell the caller
726 * everything is A-OK so far...
735 * 'accept_jobs()' - Accept print jobs to a printer.
739 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
740 ipp_attribute_t
*uri
) /* I - Printer or class URI */
742 http_status_t status
; /* Policy status */
743 cups_ptype_t dtype
; /* Destination type (printer or class) */
744 cupsd_printer_t
*printer
; /* Printer data */
747 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
748 con
->http
.fd
, uri
->values
[0].string
.text
);
751 * Is the destination valid?
754 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
760 send_ipp_status(con
, IPP_NOT_FOUND
,
761 _("The printer or class was not found."));
769 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
771 send_http_error(con
, status
, printer
);
776 * Accept jobs sent to the printer...
779 printer
->accepting
= 1;
780 printer
->state_message
[0] = '\0';
782 cupsdAddPrinterHistory(printer
);
784 if (dtype
& CUPS_PRINTER_CLASS
)
786 cupsdSaveAllClasses();
788 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
789 printer
->name
, get_username(con
));
793 cupsdSaveAllPrinters();
795 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" now accepting jobs (\"%s\").",
796 printer
->name
, get_username(con
));
800 * Everything was ok, so return OK status...
803 con
->response
->request
.status
.status_code
= IPP_OK
;
808 * 'add_class()' - Add a class to the system.
812 add_class(cupsd_client_t
*con
, /* I - Client connection */
813 ipp_attribute_t
*uri
) /* I - URI of class */
815 http_status_t status
; /* Policy status */
816 int i
; /* Looping var */
817 char method
[HTTP_MAX_URI
], /* Method portion of URI */
818 username
[HTTP_MAX_URI
], /* Username portion of URI */
819 host
[HTTP_MAX_URI
], /* Host portion of URI */
820 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
821 int port
; /* Port portion of URI */
822 cupsd_printer_t
*pclass
, /* Class */
823 *member
; /* Member printer/class */
824 cups_ptype_t dtype
; /* Destination type */
825 ipp_attribute_t
*attr
; /* Printer attribute */
826 int modify
; /* Non-zero if we just modified */
827 char newname
[IPP_MAX_NAME
]; /* New class name */
828 int need_restart_job
; /* Need to restart job? */
831 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
832 con
->http
.fd
, uri
->values
[0].string
.text
);
835 * Do we have a valid URI?
838 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
839 sizeof(method
), username
, sizeof(username
), host
,
840 sizeof(host
), &port
, resource
, sizeof(resource
));
843 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
846 * No, return an error...
849 send_ipp_status(con
, IPP_BAD_REQUEST
,
850 _("The printer-uri must be of the form "
851 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
856 * Do we have a valid printer name?
859 if (!validate_name(resource
+ 9))
862 * No, return an error...
865 send_ipp_status(con
, IPP_BAD_REQUEST
,
866 _("The printer-uri \"%s\" contains invalid characters."),
867 uri
->values
[0].string
.text
);
875 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
877 send_http_error(con
, status
, NULL
);
882 * See if the class already exists; if not, create a new class...
885 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
888 * Class doesn't exist; see if we have a printer of the same name...
891 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
892 !(pclass
->type
& CUPS_PRINTER_REMOTE
))
895 * Yes, return an error...
898 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
899 _("A printer named \"%s\" already exists!"),
905 * No, add the pclass...
908 pclass
= cupsdAddClass(resource
+ 9);
911 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
914 * Rename the implicit class to "AnyClass" or remove it...
917 if (ImplicitAnyClasses
)
919 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
920 cupsdRenamePrinter(pclass
, newname
);
923 cupsdDeletePrinter(pclass
, 1);
926 * Add the class as a new local class...
929 pclass
= cupsdAddClass(resource
+ 9);
932 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
935 * Rename the remote class to "Class"...
938 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
939 cupsdRenamePrinter(pclass
, newname
);
942 * Add the class as a new local class...
945 pclass
= cupsdAddClass(resource
+ 9);
952 * Look for attributes and copy them over as needed...
955 need_restart_job
= 0;
957 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
958 IPP_TAG_TEXT
)) != NULL
)
959 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
961 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
962 IPP_TAG_TEXT
)) != NULL
)
963 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
965 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
966 IPP_TAG_BOOLEAN
)) != NULL
)
968 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-is-accepting-jobs to %d (was %d.)",
969 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
971 pclass
->accepting
= attr
->values
[0].boolean
;
972 cupsdAddPrinterHistory(pclass
);
975 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
976 IPP_TAG_BOOLEAN
)) != NULL
)
978 if (pclass
->shared
&& !attr
->values
[0].boolean
)
979 cupsdDeregisterPrinter(pclass
, 1);
981 cupsdLogMessage(CUPSD_LOG_INFO
,
982 "Setting %s printer-is-shared to %d (was %d.)",
983 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
985 pclass
->shared
= attr
->values
[0].boolean
;
988 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
989 IPP_TAG_ENUM
)) != NULL
)
991 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
992 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
994 send_ipp_status(con
, IPP_BAD_REQUEST
,
995 _("Attempt to set %s printer-state to bad value %d!"),
996 pclass
->name
, attr
->values
[0].integer
);
1000 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)", pclass
->name
,
1001 attr
->values
[0].integer
, pclass
->state
);
1003 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1004 cupsdStopPrinter(pclass
, 0);
1007 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1008 need_restart_job
= 1;
1011 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1012 IPP_TAG_TEXT
)) != NULL
)
1014 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1015 sizeof(pclass
->state_message
));
1016 cupsdAddPrinterHistory(pclass
);
1018 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1019 IPP_TAG_URI
)) != NULL
)
1022 * Clear the printer array as needed...
1025 need_restart_job
= 1;
1027 if (pclass
->num_printers
> 0)
1029 free(pclass
->printers
);
1030 pclass
->num_printers
= 0;
1034 * Add each printer or class that is listed...
1037 for (i
= 0; i
< attr
->num_values
; i
++)
1040 * Search for the printer or class URI...
1043 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1049 send_ipp_status(con
, IPP_NOT_FOUND
,
1050 _("The printer or class was not found."));
1055 * Add it to the class...
1058 cupsdAddPrinterToClass(pclass
, member
);
1062 set_printer_defaults(con
, pclass
);
1065 * Update the printer class attributes and return...
1068 cupsdSetPrinterAttrs(pclass
);
1069 cupsdSaveAllClasses();
1071 if (need_restart_job
&& pclass
->job
)
1076 * Stop the current job and then restart it below...
1079 job
= (cupsd_job_t
*)pclass
->job
;
1081 cupsdStopJob(job
, 1);
1083 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1084 job
->state_value
= IPP_JOB_PENDING
;
1087 if (need_restart_job
)
1090 cupsdWritePrintcap();
1094 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1095 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1098 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1099 pclass
->name
, get_username(con
));
1103 cupsdAddPrinterHistory(pclass
);
1105 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1106 "New class \"%s\" added by \"%s\".", pclass
->name
,
1109 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1110 pclass
->name
, get_username(con
));
1113 con
->response
->request
.status
.status_code
= IPP_OK
;
1118 * 'add_file()' - Add a file to a job.
1121 static int /* O - 0 on success, -1 on error */
1122 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1123 cupsd_job_t
*job
, /* I - Job to add to */
1124 mime_type_t
*filetype
, /* I - Type of file */
1125 int compression
) /* I - Compression */
1127 mime_type_t
**filetypes
; /* New filetypes array... */
1128 int *compressions
; /* New compressions array... */
1131 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1132 "add_file(con=%p[%d], job=%d, filetype=%s/%s, compression=%d)",
1133 con
, con
->http
.fd
, job
->id
, filetype
->super
, filetype
->type
,
1137 * Add the file to the job...
1140 if (job
->num_files
== 0)
1142 compressions
= (int *)malloc(sizeof(int));
1143 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1147 compressions
= (int *)realloc(job
->compressions
,
1148 (job
->num_files
+ 1) * sizeof(int));
1149 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1150 (job
->num_files
+ 1) *
1151 sizeof(mime_type_t
*));
1154 if (!compressions
|| !filetypes
)
1156 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
1158 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1159 _("Unable to allocate memory for file types!"));
1163 job
->compressions
= compressions
;
1164 job
->compressions
[job
->num_files
] = compression
;
1165 job
->filetypes
= filetypes
;
1166 job
->filetypes
[job
->num_files
] = filetype
;
1175 * 'add_job()' - Add a job to a print queue.
1178 static cupsd_job_t
* /* O - Job object */
1179 add_job(cupsd_client_t
*con
, /* I - Client connection */
1180 cupsd_printer_t
*printer
, /* I - Destination printer */
1181 mime_type_t
*filetype
) /* I - First print file type, if any */
1183 http_status_t status
; /* Policy status */
1184 ipp_attribute_t
*attr
, /* Current attribute */
1185 *auth_info
; /* auth-info attribute */
1186 const char *val
; /* Default option value */
1187 int priority
; /* Job priority */
1188 char *title
; /* Job name/title */
1189 cupsd_job_t
*job
; /* Current job */
1190 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1191 int kbytes
; /* Size of print file */
1192 int i
; /* Looping var */
1193 int lowerpagerange
; /* Page range bound */
1196 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1197 con
, con
->http
.fd
, printer
, printer
->name
,
1198 filetype
, filetype
->super
, filetype
->type
);
1201 * Check remote printing to non-shared printer...
1204 if (!printer
->shared
&&
1205 strcasecmp(con
->http
.hostname
, "localhost") &&
1206 strcasecmp(con
->http
.hostname
, ServerName
))
1208 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1209 _("The printer or class is not shared!"));
1217 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1219 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1221 send_http_error(con
, status
, printer
);
1224 else if ((printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
1225 !con
->username
[0] && !auth_info
)
1227 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1231 else if (auth_info
&& !con
->http
.tls
&&
1232 !httpAddrLocalhost(con
->http
.hostaddr
))
1235 * Require encryption of auth-info over non-local connections...
1238 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1241 #endif /* HAVE_SSL */
1244 * See if the printer is accepting jobs...
1247 if (!printer
->accepting
)
1249 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1250 _("Destination \"%s\" is not accepting jobs."),
1256 * Validate job template attributes; for now just document-format,
1257 * copies, number-up, and page-ranges...
1260 if (filetype
&& printer
->filetypes
&&
1261 !cupsArrayFind(printer
->filetypes
, filetype
))
1263 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1264 /* MIME media type string */
1267 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1270 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1271 _("Unsupported format \'%s\'!"), mimetype
);
1273 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1274 "document-format", NULL
, mimetype
);
1279 if ((attr
= ippFindAttribute(con
->request
, "copies",
1280 IPP_TAG_INTEGER
)) != NULL
)
1282 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1284 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1285 attr
->values
[0].integer
);
1286 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1287 "copies", attr
->values
[0].integer
);
1292 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1293 IPP_TAG_INTEGER
)) != NULL
)
1295 if (attr
->values
[0].integer
!= 1 &&
1296 attr
->values
[0].integer
!= 2 &&
1297 attr
->values
[0].integer
!= 4 &&
1298 attr
->values
[0].integer
!= 6 &&
1299 attr
->values
[0].integer
!= 9 &&
1300 attr
->values
[0].integer
!= 16)
1302 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1303 attr
->values
[0].integer
);
1304 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1305 "number-up", attr
->values
[0].integer
);
1310 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1311 IPP_TAG_RANGE
)) != NULL
)
1313 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1315 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1316 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1318 send_ipp_status(con
, IPP_BAD_REQUEST
,
1319 _("Bad page-ranges values %d-%d."),
1320 attr
->values
[i
].range
.lower
,
1321 attr
->values
[i
].range
.upper
);
1325 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1330 * Make sure we aren't over our limit...
1333 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1336 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1338 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1339 _("Too many active jobs."));
1343 if (!check_quotas(con
, printer
))
1345 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1350 * Create the job and set things up...
1353 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1354 IPP_TAG_INTEGER
)) != NULL
)
1355 priority
= attr
->values
[0].integer
;
1358 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1359 printer
->options
)) != NULL
)
1360 priority
= atoi(val
);
1364 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1368 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1369 IPP_TAG_NAME
)) != NULL
)
1370 title
= attr
->values
[0].string
.text
;
1372 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1373 title
= "Untitled");
1375 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1377 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1378 _("Unable to add job for destination \"%s\"!"),
1383 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
1384 CUPS_PRINTER_REMOTE
);
1385 job
->attrs
= con
->request
;
1386 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1388 add_job_uuid(con
, job
);
1389 apply_printer_defaults(printer
, job
);
1391 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1393 if (con
->username
[0])
1395 cupsdSetString(&job
->username
, con
->username
);
1398 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1402 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1403 "add_job: requesting-user-name=\"%s\"",
1404 attr
->values
[0].string
.text
);
1406 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1409 cupsdSetString(&job
->username
, "anonymous");
1412 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1413 "job-originating-user-name", NULL
, job
->username
);
1416 attr
->group_tag
= IPP_TAG_JOB
;
1417 _cupsStrFree(attr
->name
);
1418 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1421 if (con
->username
[0] || auth_info
)
1423 save_auth_info(con
, job
, auth_info
);
1426 * Remove the auth-info attribute from the attribute data...
1431 if (job
->attrs
->prev
)
1432 job
->attrs
->prev
->next
= auth_info
->next
;
1434 job
->attrs
->attrs
= auth_info
->next
;
1436 if (job
->attrs
->last
== auth_info
)
1437 job
->attrs
->last
= job
->attrs
->prev
;
1439 _ippFreeAttr(auth_info
);
1443 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1444 IPP_TAG_ZERO
)) != NULL
)
1447 * Request contains a job-originating-host-name attribute; validate it...
1450 if (attr
->value_tag
!= IPP_TAG_NAME
||
1451 attr
->num_values
!= 1 ||
1452 strcmp(con
->http
.hostname
, "localhost"))
1455 * Can't override the value if we aren't connected via localhost.
1456 * Also, we can only have 1 value and it must be a name value.
1459 switch (attr
->value_tag
)
1461 case IPP_TAG_STRING
:
1462 case IPP_TAG_TEXTLANG
:
1463 case IPP_TAG_NAMELANG
:
1466 case IPP_TAG_KEYWORD
:
1468 case IPP_TAG_URISCHEME
:
1469 case IPP_TAG_CHARSET
:
1470 case IPP_TAG_LANGUAGE
:
1471 case IPP_TAG_MIMETYPE
:
1473 * Free old strings...
1476 for (i
= 0; i
< attr
->num_values
; i
++)
1478 _cupsStrFree(attr
->values
[i
].string
.text
);
1479 attr
->values
[i
].string
.text
= NULL
;
1480 if (attr
->values
[i
].string
.charset
)
1482 _cupsStrFree(attr
->values
[i
].string
.charset
);
1483 attr
->values
[i
].string
.charset
= NULL
;
1492 * Use the default connection hostname instead...
1495 attr
->value_tag
= IPP_TAG_NAME
;
1496 attr
->num_values
= 1;
1497 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1500 attr
->group_tag
= IPP_TAG_JOB
;
1505 * No job-originating-host-name attribute, so use the hostname from
1509 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1510 "job-originating-host-name", NULL
, con
->http
.hostname
);
1513 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1515 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1516 "time-at-processing", 0);
1517 attr
->value_tag
= IPP_TAG_NOVALUE
;
1518 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1519 "time-at-completed", 0);
1520 attr
->value_tag
= IPP_TAG_NOVALUE
;
1523 * Add remaining job attributes...
1526 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1527 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1528 "job-state", IPP_JOB_STOPPED
);
1529 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1530 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1531 "job-media-sheets-completed", 0);
1532 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1534 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1537 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1538 IPP_TAG_INTEGER
)) != NULL
)
1539 attr
->values
[0].integer
= 0;
1541 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1544 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1545 IPP_TAG_KEYWORD
)) == NULL
)
1546 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1549 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1550 printer
->options
)) == NULL
)
1553 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1554 "job-hold-until", NULL
, val
);
1556 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1557 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1560 * Hold job until specified time...
1563 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1565 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1566 job
->state_value
= IPP_JOB_HELD
;
1568 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1570 job
->hold_until
= time(NULL
) + 60;
1571 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1572 job
->state_value
= IPP_JOB_HELD
;
1576 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1577 job
->state_value
= IPP_JOB_PENDING
;
1580 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1584 * Add job sheets options...
1587 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1588 IPP_TAG_ZERO
)) == NULL
)
1590 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1591 "Adding default job-sheets values \"%s,%s\"...",
1592 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1594 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1596 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1597 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1600 job
->job_sheets
= attr
;
1603 * Enforce classification level if set...
1608 cupsdLogMessage(CUPSD_LOG_INFO
,
1609 "Classification=\"%s\", ClassifyOverride=%d",
1610 Classification
? Classification
: "(null)",
1613 if (ClassifyOverride
)
1615 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1616 (attr
->num_values
== 1 ||
1617 !strcmp(attr
->values
[1].string
.text
, "none")))
1620 * Force the leading banner to have the classification on it...
1623 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1625 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1626 "job-sheets=\"%s,none\", "
1627 "job-originating-user-name=\"%s\"",
1628 job
->id
, Classification
, job
->username
);
1630 else if (attr
->num_values
== 2 &&
1631 strcmp(attr
->values
[0].string
.text
,
1632 attr
->values
[1].string
.text
) &&
1633 strcmp(attr
->values
[0].string
.text
, "none") &&
1634 strcmp(attr
->values
[1].string
.text
, "none"))
1637 * Can't put two different security markings on the same document!
1640 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1642 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1643 "job-sheets=\"%s,%s\", "
1644 "job-originating-user-name=\"%s\"",
1645 job
->id
, attr
->values
[0].string
.text
,
1646 attr
->values
[1].string
.text
, job
->username
);
1648 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1649 strcmp(attr
->values
[0].string
.text
, "none") &&
1650 (attr
->num_values
== 1 ||
1651 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1652 strcmp(attr
->values
[1].string
.text
, "none"))))
1654 if (attr
->num_values
== 1)
1655 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1656 "[Job %d] CLASSIFICATION OVERRIDDEN "
1657 "job-sheets=\"%s\", "
1658 "job-originating-user-name=\"%s\"",
1659 job
->id
, attr
->values
[0].string
.text
, job
->username
);
1661 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1662 "[Job %d] CLASSIFICATION OVERRIDDEN "
1663 "job-sheets=\"%s,%s\",fffff "
1664 "job-originating-user-name=\"%s\"",
1665 job
->id
, attr
->values
[0].string
.text
,
1666 attr
->values
[1].string
.text
, job
->username
);
1669 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1670 (attr
->num_values
== 1 ||
1671 strcmp(attr
->values
[1].string
.text
, Classification
)))
1674 * Force the banner to have the classification on it...
1677 if (attr
->num_values
> 1 &&
1678 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1680 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1681 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1685 if (attr
->num_values
== 1 ||
1686 strcmp(attr
->values
[0].string
.text
, "none"))
1687 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1689 if (attr
->num_values
> 1 &&
1690 strcmp(attr
->values
[1].string
.text
, "none"))
1691 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1694 if (attr
->num_values
> 1)
1695 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1696 "[Job %d] CLASSIFICATION FORCED "
1697 "job-sheets=\"%s,%s\", "
1698 "job-originating-user-name=\"%s\"",
1699 job
->id
, attr
->values
[0].string
.text
,
1700 attr
->values
[1].string
.text
, job
->username
);
1702 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1703 "[Job %d] CLASSIFICATION FORCED "
1704 "job-sheets=\"%s\", "
1705 "job-originating-user-name=\"%s\"",
1706 job
->id
, Classification
, job
->username
);
1711 * See if we need to add the starting sheet...
1714 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1716 cupsdLogMessage(CUPSD_LOG_INFO
,
1717 "Adding start banner page \"%s\" to job %d.",
1718 attr
->values
[0].string
.text
, job
->id
);
1720 kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
);
1722 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1725 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1726 IPP_TAG_ZERO
)) != NULL
)
1730 * Fill in the response info...
1733 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1734 LocalPort
, job
->id
);
1736 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1739 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1741 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1743 add_job_state_reasons(con
, job
);
1745 con
->response
->request
.status
.status_code
= IPP_OK
;
1748 * Add any job subscriptions...
1751 add_job_subscriptions(con
, job
);
1754 * Set all but the first two attributes to the job attributes group...
1757 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1758 attr
->group_tag
= IPP_TAG_JOB
;
1761 * Fire the "job created" event...
1764 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1767 * Return the new job...
1775 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1776 * upon the job and printer state...
1780 add_job_state_reasons(
1781 cupsd_client_t
*con
, /* I - Client connection */
1782 cupsd_job_t
*job
) /* I - Job info */
1784 cupsd_printer_t
*dest
; /* Destination printer */
1787 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1788 con
, con
->http
.fd
, job
? job
->id
: 0);
1790 switch (job
? job
->state_value
: IPP_JOB_CANCELED
)
1792 case IPP_JOB_PENDING
:
1793 dest
= cupsdFindDest(job
->dest
);
1795 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1796 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1797 "job-state-reasons", NULL
, "printer-stopped");
1799 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1800 "job-state-reasons", NULL
, "none");
1804 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1805 IPP_TAG_KEYWORD
) != NULL
||
1806 ippFindAttribute(job
->attrs
, "job-hold-until",
1807 IPP_TAG_NAME
) != NULL
)
1808 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1809 "job-state-reasons", NULL
, "job-hold-until-specified");
1811 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1812 "job-state-reasons", NULL
, "job-incoming");
1815 case IPP_JOB_PROCESSING
:
1816 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1817 "job-state-reasons", NULL
, "job-printing");
1820 case IPP_JOB_STOPPED
:
1821 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1822 "job-state-reasons", NULL
, "job-stopped");
1825 case IPP_JOB_CANCELED
:
1826 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1827 "job-state-reasons", NULL
, "job-canceled-by-user");
1830 case IPP_JOB_ABORTED
:
1831 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1832 "job-state-reasons", NULL
, "aborted-by-system");
1835 case IPP_JOB_COMPLETED
:
1836 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1837 "job-state-reasons", NULL
, "job-completed-successfully");
1844 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1848 add_job_subscriptions(
1849 cupsd_client_t
*con
, /* I - Client connection */
1850 cupsd_job_t
*job
) /* I - Newly created job */
1852 int i
; /* Looping var */
1853 ipp_attribute_t
*prev
, /* Previous attribute */
1854 *next
, /* Next attribute */
1855 *attr
; /* Current attribute */
1856 cupsd_subscription_t
*sub
; /* Subscription object */
1857 const char *recipient
, /* notify-recipient-uri */
1858 *pullmethod
; /* notify-pull-method */
1859 ipp_attribute_t
*user_data
; /* notify-user-data */
1860 int interval
; /* notify-time-interval */
1861 unsigned mask
; /* notify-events */
1865 * Find the first subscription group attribute; return if we have
1869 for (attr
= job
->attrs
->attrs
, prev
= NULL
;
1871 prev
= attr
, attr
= attr
->next
)
1872 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1879 * Process the subscription attributes in the request...
1888 mask
= CUPSD_EVENT_NONE
;
1890 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1892 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
1893 attr
->value_tag
== IPP_TAG_URI
)
1894 recipient
= attr
->values
[0].string
.text
;
1895 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1896 attr
->value_tag
== IPP_TAG_KEYWORD
)
1897 pullmethod
= attr
->values
[0].string
.text
;
1898 else if (!strcmp(attr
->name
, "notify-charset") &&
1899 attr
->value_tag
== IPP_TAG_CHARSET
&&
1900 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
1901 strcmp(attr
->values
[0].string
.text
, "utf-8"))
1903 send_ipp_status(con
, IPP_CHARSET
,
1904 _("Character set \"%s\" not supported!"),
1905 attr
->values
[0].string
.text
);
1908 else if (!strcmp(attr
->name
, "notify-natural-language") &&
1909 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
1910 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
1912 send_ipp_status(con
, IPP_CHARSET
,
1913 _("Language \"%s\" not supported!"),
1914 attr
->values
[0].string
.text
);
1917 else if (!strcmp(attr
->name
, "notify-user-data") &&
1918 attr
->value_tag
== IPP_TAG_STRING
)
1920 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
1922 send_ipp_status(con
, IPP_REQUEST_VALUE
,
1923 _("The notify-user-data value is too large "
1924 "(%d > 63 octets)!"),
1925 attr
->values
[0].unknown
.length
);
1931 else if (!strcmp(attr
->name
, "notify-events") &&
1932 attr
->value_tag
== IPP_TAG_KEYWORD
)
1934 for (i
= 0; i
< attr
->num_values
; i
++)
1935 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
1937 else if (!strcmp(attr
->name
, "notify-lease-duration"))
1939 send_ipp_status(con
, IPP_BAD_REQUEST
,
1940 _("The notify-lease-duration attribute cannot be "
1941 "used with job subscriptions."));
1944 else if (!strcmp(attr
->name
, "notify-time-interval") &&
1945 attr
->value_tag
== IPP_TAG_INTEGER
)
1946 interval
= attr
->values
[0].integer
;
1951 if (!recipient
&& !pullmethod
)
1954 if (mask
== CUPSD_EVENT_NONE
)
1955 mask
= CUPSD_EVENT_JOB_COMPLETED
;
1957 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
1960 sub
->interval
= interval
;
1962 cupsdSetString(&sub
->owner
, job
->username
);
1966 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
1967 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
1968 sub
->user_data_len
);
1971 ippAddSeparator(con
->response
);
1972 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
1973 "notify-subscription-id", sub
->id
);
1979 cupsdSaveAllSubscriptions();
1982 * Remove all of the subscription attributes from the job request...
1985 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
1989 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
1990 attr
->group_tag
== IPP_TAG_ZERO
)
1993 * Free and remove this attribute...
2001 job
->attrs
->attrs
= next
;
2007 job
->attrs
->last
= prev
;
2008 job
->attrs
->current
= prev
;
2013 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2015 * See RFC 4122 for the definition of UUIDs and the format.
2019 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
2020 cupsd_job_t
*job
) /* I - Job */
2022 char uuid
[1024]; /* job-uuid string */
2023 _cups_md5_state_t md5state
; /* MD5 state */
2024 unsigned char md5sum
[16]; /* MD5 digest/sum */
2028 * First see if the job already has a job-uuid attribute; if so, return...
2031 if (ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2035 * No job-uuid attribute, so build a version 3 UUID with the local job
2036 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
2037 * the ServerName, server name and port that the client connected to,
2038 * and local job ID...
2041 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
2042 con
->serverport
, job
->id
);
2044 _cupsMD5Init(&md5state
);
2045 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
2046 _cupsMD5Finish(&md5state
, md5sum
);
2049 * Format the UUID URI using the MD5 sum and job ID.
2052 snprintf(uuid
, sizeof(uuid
),
2053 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2054 "%02x%02x%02x%02x%02x%02x",
2055 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
2056 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
2057 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
2058 md5sum
[14], md5sum
[15]);
2060 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
2065 * 'add_printer()' - Add a printer to the system.
2069 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2070 ipp_attribute_t
*uri
) /* I - URI of printer */
2072 http_status_t status
; /* Policy status */
2073 int i
; /* Looping var */
2074 char method
[HTTP_MAX_URI
], /* Method portion of URI */
2075 username
[HTTP_MAX_URI
], /* Username portion of URI */
2076 host
[HTTP_MAX_URI
], /* Host portion of URI */
2077 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2078 int port
; /* Port portion of URI */
2079 cupsd_printer_t
*printer
; /* Printer/class */
2080 ipp_attribute_t
*attr
; /* Printer attribute */
2081 cups_file_t
*fp
; /* Script/PPD file */
2082 char line
[1024]; /* Line from file... */
2083 char srcfile
[1024], /* Source Script/PPD file */
2084 dstfile
[1024]; /* Destination Script/PPD file */
2085 int modify
; /* Non-zero if we are modifying */
2086 char newname
[IPP_MAX_NAME
]; /* New printer name */
2087 int need_restart_job
; /* Need to restart job? */
2090 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2091 con
->http
.fd
, uri
->values
[0].string
.text
);
2094 * Do we have a valid URI?
2097 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2098 sizeof(method
), username
, sizeof(username
), host
,
2099 sizeof(host
), &port
, resource
, sizeof(resource
));
2101 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2104 * No, return an error...
2107 send_ipp_status(con
, IPP_BAD_REQUEST
,
2108 _("The printer-uri must be of the form "
2109 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2114 * Do we have a valid printer name?
2117 if (!validate_name(resource
+ 10))
2120 * No, return an error...
2123 send_ipp_status(con
, IPP_BAD_REQUEST
,
2124 _("The printer-uri \"%s\" contains invalid characters."),
2125 uri
->values
[0].string
.text
);
2133 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2135 send_http_error(con
, status
, NULL
);
2140 * See if the printer already exists; if not, create a new printer...
2143 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2146 * Printer doesn't exist; see if we have a class of the same name...
2149 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2150 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2153 * Yes, return an error...
2156 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2157 _("A class named \"%s\" already exists!"),
2163 * No, add the printer...
2166 printer
= cupsdAddPrinter(resource
+ 10);
2169 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2172 * Rename the implicit printer to "AnyPrinter" or delete it...
2175 if (ImplicitAnyClasses
)
2177 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2178 cupsdRenamePrinter(printer
, newname
);
2181 cupsdDeletePrinter(printer
, 1);
2184 * Add the printer as a new local printer...
2187 printer
= cupsdAddPrinter(resource
+ 10);
2190 else if (printer
->type
& CUPS_PRINTER_REMOTE
)
2193 * Rename the remote printer to "Printer@server"...
2196 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2198 cupsdRenamePrinter(printer
, newname
);
2201 * Add the printer as a new local printer...
2204 printer
= cupsdAddPrinter(resource
+ 10);
2211 * Look for attributes and copy them over as needed...
2214 need_restart_job
= 0;
2216 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2217 IPP_TAG_TEXT
)) != NULL
)
2218 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2220 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2221 IPP_TAG_TEXT
)) != NULL
)
2222 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2224 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2225 IPP_TAG_URI
)) != NULL
)
2228 * Do we have a valid device URI?
2231 need_restart_job
= 1;
2233 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, method
,
2234 sizeof(method
), username
, sizeof(username
), host
,
2235 sizeof(host
), &port
, resource
, sizeof(resource
));
2237 if (!strcmp(method
, "file"))
2240 * See if the administrator has enabled file devices...
2243 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2246 * File devices are disabled and the URL is not file:/dev/null...
2249 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2250 _("File device URIs have been disabled! "
2251 "To enable, see the FileDevice directive in "
2252 "\"%s/cupsd.conf\"."),
2260 * See if the backend exists and is executable...
2263 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, method
);
2264 if (access(srcfile
, X_OK
))
2267 * Could not find device in list!
2270 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2271 attr
->values
[0].string
.text
);
2276 cupsdLogMessage(CUPSD_LOG_INFO
,
2277 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2279 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2281 cupsdSanitizeURI(printer
->device_uri
, resource
,
2284 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2287 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2288 IPP_TAG_KEYWORD
)) != NULL
)
2290 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2293 need_restart_job
= 1;
2295 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2297 for (i
= 0; i
< supported
->num_values
; i
++)
2298 if (!strcmp(supported
->values
[i
].string
.text
,
2299 attr
->values
[0].string
.text
))
2302 if (i
>= supported
->num_values
)
2304 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2305 attr
->values
[0].string
.text
);
2309 cupsdLogMessage(CUPSD_LOG_INFO
,
2310 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2311 printer
->name
, attr
->values
[0].string
.text
,
2312 printer
->port_monitor
);
2314 if (strcmp(attr
->values
[0].string
.text
, "none"))
2315 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2317 cupsdClearString(&printer
->port_monitor
);
2320 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2321 IPP_TAG_BOOLEAN
)) != NULL
)
2323 cupsdLogMessage(CUPSD_LOG_INFO
,
2324 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2325 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2327 printer
->accepting
= attr
->values
[0].boolean
;
2328 cupsdAddPrinterHistory(printer
);
2331 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2332 IPP_TAG_BOOLEAN
)) != NULL
)
2334 if (printer
->shared
&& !attr
->values
[0].boolean
)
2335 cupsdDeregisterPrinter(printer
, 1);
2337 cupsdLogMessage(CUPSD_LOG_INFO
,
2338 "Setting %s printer-is-shared to %d (was %d.)",
2339 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2341 printer
->shared
= attr
->values
[0].boolean
;
2344 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2345 IPP_TAG_ENUM
)) != NULL
)
2347 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2348 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2350 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2351 attr
->values
[0].integer
);
2355 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)", printer
->name
,
2356 attr
->values
[0].integer
, printer
->state
);
2358 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2359 cupsdStopPrinter(printer
, 0);
2362 need_restart_job
= 1;
2363 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2366 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2367 IPP_TAG_TEXT
)) != NULL
)
2369 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2370 sizeof(printer
->state_message
));
2371 cupsdAddPrinterHistory(printer
);
2374 set_printer_defaults(con
, printer
);
2377 * See if we have all required attributes...
2380 if (!printer
->device_uri
)
2381 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2384 * See if we have an interface script or PPD file attached to the request...
2389 need_restart_job
= 1;
2391 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2393 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2396 * Yes; get the first line from it...
2400 cupsFileGets(fp
, line
, sizeof(line
));
2404 * Then see what kind of file it is...
2407 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2410 if (!strncmp(line
, "*PPD-Adobe", 10))
2413 * The new file is a PPD file, so remove any old interface script
2414 * that might be lying around...
2422 * This must be an interface script, so move the file over to the
2423 * interfaces directory and make it executable...
2426 if (copy_file(srcfile
, dstfile
))
2428 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2429 _("Unable to copy interface script - %s!"),
2435 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2436 "Copied interface script successfully!");
2437 chmod(dstfile
, 0755);
2441 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2444 if (!strncmp(line
, "*PPD-Adobe", 10))
2447 * The new file is a PPD file, so move the file over to the
2448 * ppd directory and make it readable by all...
2451 if (copy_file(srcfile
, dstfile
))
2453 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2454 _("Unable to copy PPD file - %s!"),
2460 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2461 "Copied PPD file successfully!");
2462 chmod(dstfile
, 0644);
2468 * This must be an interface script, so remove any old PPD file that
2469 * may be lying around...
2476 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2477 IPP_TAG_NAME
)) != NULL
)
2479 need_restart_job
= 1;
2481 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2484 * Raw driver, remove any existing PPD or interface script files.
2487 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2491 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2501 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2505 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2508 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2510 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2515 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2516 "Copied PPD file successfully!");
2517 chmod(dstfile
, 0644);
2523 * Update the printer attributes and return...
2526 cupsdSetPrinterAttrs(printer
);
2527 cupsdSaveAllPrinters();
2529 if (need_restart_job
&& printer
->job
)
2534 * Stop the current job and then restart it below...
2537 job
= (cupsd_job_t
*)printer
->job
;
2539 cupsdStopJob(job
, 1);
2541 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2542 job
->state_value
= IPP_JOB_PENDING
;
2545 if (need_restart_job
)
2548 cupsdWritePrintcap();
2552 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2553 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2556 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2557 printer
->name
, get_username(con
));
2561 cupsdAddPrinterHistory(printer
);
2563 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2564 "New printer \"%s\" added by \"%s\".", printer
->name
,
2567 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2568 printer
->name
, get_username(con
));
2571 con
->response
->request
.status
.status_code
= IPP_OK
;
2576 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2577 * based upon the printer state...
2581 add_printer_state_reasons(
2582 cupsd_client_t
*con
, /* I - Client connection */
2583 cupsd_printer_t
*p
) /* I - Printer info */
2585 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2586 "add_printer_state_reasons(%p[%d], %p[%s])",
2587 con
, con
->http
.fd
, p
, p
->name
);
2589 if (p
->num_reasons
== 0)
2590 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2591 "printer-state-reasons", NULL
,
2592 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2594 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2595 "printer-state-reasons", p
->num_reasons
, NULL
,
2596 (const char * const *)p
->reasons
);
2601 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2602 * the specified printer or class.
2606 add_queued_job_count(
2607 cupsd_client_t
*con
, /* I - Client connection */
2608 cupsd_printer_t
*p
) /* I - Printer or class */
2610 int count
; /* Number of jobs on destination */
2613 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2614 con
, con
->http
.fd
, p
, p
->name
);
2616 count
= cupsdGetPrinterJobCount(p
->name
);
2618 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2619 "queued-job-count", count
);
2624 * 'apply_printer_defaults()' - Apply printer default options to a job.
2628 apply_printer_defaults(
2629 cupsd_printer_t
*printer
, /* I - Printer */
2630 cupsd_job_t
*job
) /* I - Job */
2632 int i
, /* Looping var */
2633 num_options
; /* Number of default options */
2634 cups_option_t
*options
, /* Default options */
2635 *option
; /* Current option */
2639 * Collect all of the default options and add the missing ones to the
2643 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
2646 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2648 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2653 * Encode these options as attributes in the job object...
2656 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2657 cupsFreeOptions(num_options
, options
);
2662 * 'authenticate_job()' - Set job authentication info.
2666 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2667 ipp_attribute_t
*uri
) /* I - Job URI */
2669 ipp_attribute_t
*attr
, /* job-id attribute */
2670 *auth_info
; /* auth-info attribute */
2671 int jobid
; /* Job ID */
2672 cupsd_job_t
*job
; /* Current job */
2673 char method
[HTTP_MAX_URI
],
2674 /* Method portion of URI */
2675 username
[HTTP_MAX_URI
],
2676 /* Username portion of URI */
2678 /* Host portion of URI */
2679 resource
[HTTP_MAX_URI
];
2680 /* Resource portion of URI */
2681 int port
; /* Port portion of URI */
2684 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2685 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
2688 * Start with "everything is OK" status...
2691 con
->response
->request
.status
.status_code
= IPP_OK
;
2694 * See if we have a job URI or a printer URI...
2697 if (!strcmp(uri
->name
, "printer-uri"))
2700 * Got a printer URI; see if we also have a job-id attribute...
2703 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2704 IPP_TAG_INTEGER
)) == NULL
)
2706 send_ipp_status(con
, IPP_BAD_REQUEST
,
2707 _("Got a printer-uri attribute but no job-id!"));
2711 jobid
= attr
->values
[0].integer
;
2716 * Got a job URI; parse it to get the job ID...
2719 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2720 sizeof(method
), username
, sizeof(username
), host
,
2721 sizeof(host
), &port
, resource
, sizeof(resource
));
2723 if (strncmp(resource
, "/jobs/", 6))
2729 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
2730 uri
->values
[0].string
.text
);
2734 jobid
= atoi(resource
+ 6);
2738 * See if the job exists...
2741 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2744 * Nope - return a "not found" error...
2747 send_ipp_status(con
, IPP_NOT_FOUND
,
2748 _("Job #%d does not exist!"), jobid
);
2753 * See if the job has been completed...
2756 if (job
->state_value
!= IPP_JOB_HELD
)
2759 * Return a "not-possible" error...
2762 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2763 _("Job #%d is not held for authentication!"),
2769 * See if we have already authenticated...
2772 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
2774 if (!con
->username
[0] && !auth_info
)
2776 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
2777 _("No authentication information provided!"));
2782 * See if the job is owned by the requesting user...
2785 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
2787 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
2792 * Save the authentication information for this job...
2795 save_auth_info(con
, job
, auth_info
);
2798 * Reset the job-hold-until value to "no-hold"...
2801 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2802 IPP_TAG_KEYWORD
)) == NULL
)
2803 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2807 attr
->value_tag
= IPP_TAG_KEYWORD
;
2808 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2812 * Release the job and return...
2815 cupsdReleaseJob(job
);
2817 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was authenticated by \"%s\".", jobid
,
2823 * 'cancel_all_jobs()' - Cancel all print jobs.
2827 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
2828 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2830 http_status_t status
; /* Policy status */
2831 cups_ptype_t dtype
; /* Destination type */
2832 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2833 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2834 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
2835 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2836 int port
; /* Port portion of URI */
2837 ipp_attribute_t
*attr
; /* Attribute in request */
2838 const char *username
; /* Username */
2839 int purge
; /* Purge? */
2840 cupsd_printer_t
*printer
; /* Printer */
2843 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
2844 con
->http
.fd
, uri
->values
[0].string
.text
);
2847 * See if we have a printer URI...
2850 if (strcmp(uri
->name
, "printer-uri"))
2852 send_ipp_status(con
, IPP_BAD_REQUEST
,
2853 _("The printer-uri attribute is required!"));
2858 * Get the username (if any) for the jobs we want to cancel (only if
2859 * "my-jobs" is specified...
2862 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
2863 IPP_TAG_BOOLEAN
)) != NULL
&&
2864 attr
->values
[0].boolean
)
2866 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
2867 IPP_TAG_NAME
)) != NULL
)
2868 username
= attr
->values
[0].string
.text
;
2871 send_ipp_status(con
, IPP_BAD_REQUEST
,
2872 _("Missing requesting-user-name attribute!"));
2880 * Look for the "purge-jobs" attribute...
2883 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
2884 IPP_TAG_BOOLEAN
)) != NULL
)
2885 purge
= attr
->values
[0].boolean
;
2890 * And if the destination is valid...
2893 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
2899 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
2900 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
2901 hostname
, sizeof(hostname
), &port
,
2902 resource
, sizeof(resource
));
2904 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
2905 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
2907 send_ipp_status(con
, IPP_NOT_FOUND
,
2908 _("The printer or class was not found."));
2916 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2918 send_http_error(con
, status
, NULL
);
2923 * Cancel all jobs on all printers...
2926 cupsdCancelJobs(NULL
, username
, purge
);
2928 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
2929 purge
? "purged" : "canceled", get_username(con
));
2937 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2940 send_http_error(con
, status
, printer
);
2945 * Cancel all of the jobs on the named printer...
2948 cupsdCancelJobs(printer
->name
, username
, purge
);
2950 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
2951 printer
->name
, purge
? "purged" : "canceled",
2955 con
->response
->request
.status
.status_code
= IPP_OK
;
2960 * 'cancel_job()' - Cancel a print job.
2964 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
2965 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2967 ipp_attribute_t
*attr
; /* Current attribute */
2968 int jobid
; /* Job ID */
2969 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2970 username
[HTTP_MAX_URI
], /* Username portion of URI */
2971 host
[HTTP_MAX_URI
], /* Host portion of URI */
2972 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2973 int port
; /* Port portion of URI */
2974 cupsd_job_t
*job
; /* Job information */
2975 cups_ptype_t dtype
; /* Destination type (printer or class) */
2976 cupsd_printer_t
*printer
; /* Printer data */
2979 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
2980 con
->http
.fd
, uri
->values
[0].string
.text
);
2983 * See if we have a job URI or a printer URI...
2986 if (!strcmp(uri
->name
, "printer-uri"))
2989 * Got a printer URI; see if we also have a job-id attribute...
2992 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2993 IPP_TAG_INTEGER
)) == NULL
)
2995 send_ipp_status(con
, IPP_BAD_REQUEST
,
2996 _("Got a printer-uri attribute but no job-id!"));
3000 if ((jobid
= attr
->values
[0].integer
) == 0)
3003 * Find the current job on the specified printer...
3006 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3012 send_ipp_status(con
, IPP_NOT_FOUND
,
3013 _("The printer or class was not found."));
3018 * See if the printer is currently printing a job...
3022 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3026 * No, see if there are any pending jobs...
3029 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3031 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3032 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3033 !strcasecmp(job
->dest
, printer
->name
))
3040 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3050 * Got a job URI; parse it to get the job ID...
3053 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3054 sizeof(scheme
), username
, sizeof(username
), host
,
3055 sizeof(host
), &port
, resource
, sizeof(resource
));
3057 if (strncmp(resource
, "/jobs/", 6))
3063 send_ipp_status(con
, IPP_BAD_REQUEST
,
3064 _("Bad job-uri attribute \"%s\"!"),
3065 uri
->values
[0].string
.text
);
3069 jobid
= atoi(resource
+ 6);
3073 * See if the job exists...
3076 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3079 * Nope - return a "not found" error...
3082 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3087 * See if the job is owned by the requesting user...
3090 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3092 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
3097 * See if the job is already completed, canceled, or aborted; if so,
3098 * we can't cancel...
3101 if (job
->state_value
>= IPP_JOB_CANCELED
)
3103 switch (job
->state_value
)
3105 case IPP_JOB_CANCELED
:
3106 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3107 _("Job #%d is already canceled - can\'t cancel."),
3111 case IPP_JOB_ABORTED
:
3112 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3113 _("Job #%d is already aborted - can\'t cancel."),
3118 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3119 _("Job #%d is already completed - can\'t cancel."),
3128 * Cancel the job and return...
3131 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
3134 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was canceled by \"%s\".", jobid
,
3137 con
->response
->request
.status
.status_code
= IPP_OK
;
3142 * 'cancel_subscription()' - Cancel a subscription.
3146 cancel_subscription(
3147 cupsd_client_t
*con
, /* I - Client connection */
3148 int sub_id
) /* I - Subscription ID */
3150 http_status_t status
; /* Policy status */
3151 cupsd_subscription_t
*sub
; /* Subscription */
3154 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3155 "cancel_subscription(con=%p[%d], sub_id=%d)",
3156 con
, con
->http
.fd
, sub_id
);
3159 * Is the subscription ID valid?
3162 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3165 * Bad subscription ID...
3168 send_ipp_status(con
, IPP_NOT_FOUND
,
3169 _("notify-subscription-id %d no good!"), sub_id
);
3177 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3179 con
, sub
->owner
)) != HTTP_OK
)
3181 send_http_error(con
, status
, sub
->dest
);
3186 * Cancel the subscription...
3189 cupsdDeleteSubscription(sub
, 1);
3191 con
->response
->request
.status
.status_code
= IPP_OK
;
3196 * 'check_quotas()' - Check quotas for a printer and user.
3199 static int /* O - 1 if OK, 0 if not */
3200 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3201 cupsd_printer_t
*p
) /* I - Printer or class */
3203 int i
; /* Looping var */
3204 char username
[33]; /* Username */
3205 cupsd_quota_t
*q
; /* Quota data */
3206 #ifdef HAVE_MBR_UID_TO_UUID
3208 * Use Apple membership APIs which require that all names represent
3209 * valid user account or group records accessible by the server.
3212 uuid_t usr_uuid
; /* UUID for job requesting user */
3213 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3214 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3215 int mbr_err
; /* Error from membership function */
3216 int is_member
; /* Is this user a member? */
3219 * Use standard POSIX APIs for checking users and groups...
3222 struct passwd
*pw
; /* User password data */
3223 #endif /* HAVE_MBR_UID_TO_UUID */
3226 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3227 con
, con
->http
.fd
, p
, p
->name
);
3237 * Figure out who is printing...
3240 strlcpy(username
, get_username(con
), sizeof(username
));
3243 * Check global active job limits for printers and users...
3246 if (MaxJobsPerPrinter
)
3249 * Check if there are too many pending jobs on this printer...
3252 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3254 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3263 * Check if there are too many pending jobs for this user...
3266 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3268 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3275 * Check against users...
3278 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3283 #ifdef HAVE_MBR_UID_TO_UUID
3285 * Get UUID for job requesting user...
3288 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3294 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3295 "check_quotas: UUID lookup failed for user \"%s\"",
3297 cupsdLogMessage(CUPSD_LOG_INFO
,
3298 "Denying user \"%s\" access to printer \"%s\" "
3299 "(unknown user)...",
3305 * Get UID and GID of requesting user...
3308 pw
= getpwnam(username
);
3310 #endif /* HAVE_MBR_UID_TO_UUID */
3312 for (i
= 0; i
< p
->num_users
; i
++)
3313 if (p
->users
[i
][0] == '@')
3316 * Check group membership...
3319 #ifdef HAVE_MBR_UID_TO_UUID
3320 if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
3324 * Invalid ACL entries are ignored for matching; just record a
3325 * warning in the log...
3328 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3329 "check_quotas: UUID lookup failed for ACL entry "
3330 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3331 cupsdLogMessage(CUPSD_LOG_WARN
,
3332 "Access control entry \"%s\" not a valid group name; "
3333 "entry ignored", p
->users
[i
]);
3337 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3341 * At this point, there should be no errors, but check anyways...
3344 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3345 "check_quotas: group \"%s\" membership check "
3346 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
3351 * Stop if we found a match...
3358 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3360 #endif /* HAVE_MBR_UID_TO_UUID */
3362 #ifdef HAVE_MBR_UID_TO_UUID
3365 if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
3369 * Invalid ACL entries are ignored for matching; just record a
3370 * warning in the log...
3373 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3374 "check_quotas: UUID lookup failed for ACL entry "
3375 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3376 cupsdLogMessage(CUPSD_LOG_WARN
,
3377 "Access control entry \"%s\" not a valid user name; "
3378 "entry ignored", p
->users
[i
]);
3382 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
3385 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3386 "check_quotas: User \"%s\" identity check failed "
3387 "(err=%d)", p
->users
[i
], mbr_err
);
3396 else if (!strcasecmp(username
, p
->users
[i
]))
3398 #endif /* HAVE_MBR_UID_TO_UUID */
3400 if ((i
< p
->num_users
) == p
->deny_users
)
3402 cupsdLogMessage(CUPSD_LOG_INFO
,
3403 "Denying user \"%s\" access to printer \"%s\"...",
3417 * TODO: Define these special page count values as constants!
3420 if (q
->page_count
== -4) /* special case: unlimited user */
3422 cupsdLogMessage(CUPSD_LOG_INFO
,
3423 "User \"%s\" request approved for printer %s (%s): "
3425 username
, p
->name
, p
->info
);
3426 q
->page_count
= 0; /* allow user to print */
3429 else if (q
->page_count
== -3) /* quota exceeded */
3431 cupsdLogMessage(CUPSD_LOG_INFO
,
3432 "User \"%s\" request denied for printer %s (%s): "
3433 "quota limit exceeded.",
3434 username
, p
->name
, p
->info
);
3435 q
->page_count
= 2; /* force quota exceeded failure */
3438 else if (q
->page_count
== -2) /* quota disabled for user */
3440 cupsdLogMessage(CUPSD_LOG_INFO
,
3441 "User \"%s\" request denied for printer %s (%s): "
3442 "printing disabled for user.",
3443 username
, p
->name
, p
->info
);
3444 q
->page_count
= 2; /* force quota exceeded failure */
3447 else if (q
->page_count
== -1) /* quota access error */
3449 cupsdLogMessage(CUPSD_LOG_INFO
,
3450 "User \"%s\" request denied for printer %s (%s): "
3451 "unable to determine quota limit.",
3452 username
, p
->name
, p
->info
);
3453 q
->page_count
= 2; /* force quota exceeded failure */
3456 else if (q
->page_count
< 0) /* user not found or other error */
3458 cupsdLogMessage(CUPSD_LOG_INFO
,
3459 "User \"%s\" request denied for printer %s (%s): "
3460 "user disabled / missing quota.",
3461 username
, p
->name
, p
->info
);
3462 q
->page_count
= 2; /* force quota exceeded failure */
3465 else /* page within user limits */
3467 q
->page_count
= 0; /* allow user to print */
3472 #endif /* __APPLE__ */
3473 if (p
->k_limit
|| p
->page_limit
)
3475 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3477 cupsdLogMessage(CUPSD_LOG_ERROR
,
3478 "Unable to allocate quota data for user \"%s\"!",
3483 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3484 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3486 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3493 * If we have gotten this far, we're done!
3501 * 'copy_attribute()' - Copy a single attribute.
3504 static ipp_attribute_t
* /* O - New attribute */
3506 ipp_t
*to
, /* O - Destination request/response */
3507 ipp_attribute_t
*attr
, /* I - Attribute to copy */
3508 int quickcopy
) /* I - Do a quick copy? */
3510 int i
; /* Looping var */
3511 ipp_attribute_t
*toattr
; /* Destination attribute */
3514 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3515 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
3516 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
3519 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
3522 toattr
= ippAddSeparator(to
);
3525 case IPP_TAG_INTEGER
:
3527 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3528 attr
->name
, attr
->num_values
, NULL
);
3530 for (i
= 0; i
< attr
->num_values
; i
++)
3531 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
3534 case IPP_TAG_BOOLEAN
:
3535 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
3536 attr
->num_values
, NULL
);
3538 for (i
= 0; i
< attr
->num_values
; i
++)
3539 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
3542 case IPP_TAG_STRING
:
3545 case IPP_TAG_KEYWORD
:
3547 case IPP_TAG_URISCHEME
:
3548 case IPP_TAG_CHARSET
:
3549 case IPP_TAG_LANGUAGE
:
3550 case IPP_TAG_MIMETYPE
:
3551 toattr
= ippAddStrings(to
, attr
->group_tag
,
3552 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3553 attr
->name
, attr
->num_values
, NULL
, NULL
);
3557 for (i
= 0; i
< attr
->num_values
; i
++)
3558 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3562 for (i
= 0; i
< attr
->num_values
; i
++)
3563 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3568 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
3569 attr
->values
[0].date
);
3572 case IPP_TAG_RESOLUTION
:
3573 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
3574 attr
->num_values
, IPP_RES_PER_INCH
,
3577 for (i
= 0; i
< attr
->num_values
; i
++)
3579 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
3580 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
3581 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
3585 case IPP_TAG_RANGE
:
3586 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
3587 attr
->num_values
, NULL
, NULL
);
3589 for (i
= 0; i
< attr
->num_values
; i
++)
3591 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
3592 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
3596 case IPP_TAG_TEXTLANG
:
3597 case IPP_TAG_NAMELANG
:
3598 toattr
= ippAddStrings(to
, attr
->group_tag
,
3599 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3600 attr
->name
, attr
->num_values
, NULL
, NULL
);
3604 for (i
= 0; i
< attr
->num_values
; i
++)
3606 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
3607 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3612 for (i
= 0; i
< attr
->num_values
; i
++)
3615 toattr
->values
[i
].string
.charset
=
3616 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
3618 toattr
->values
[i
].string
.charset
=
3619 toattr
->values
[0].string
.charset
;
3621 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3626 case IPP_TAG_BEGIN_COLLECTION
:
3627 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
3628 attr
->num_values
, NULL
);
3630 for (i
= 0; i
< attr
->num_values
; i
++)
3632 toattr
->values
[i
].collection
= ippNew();
3633 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
3634 NULL
, IPP_TAG_ZERO
, 0);
3639 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3640 attr
->name
, attr
->num_values
, NULL
);
3642 for (i
= 0; i
< attr
->num_values
; i
++)
3644 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
3646 if (toattr
->values
[i
].unknown
.length
> 0)
3648 if ((toattr
->values
[i
].unknown
.data
=
3649 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
3650 toattr
->values
[i
].unknown
.length
= 0;
3652 memcpy(toattr
->values
[i
].unknown
.data
,
3653 attr
->values
[i
].unknown
.data
,
3654 toattr
->values
[i
].unknown
.length
);
3657 break; /* anti-compiler-warning-code */
3665 * 'copy_attrs()' - Copy attributes from one request to another.
3669 copy_attrs(ipp_t
*to
, /* I - Destination request */
3670 ipp_t
*from
, /* I - Source request */
3671 cups_array_t
*ra
, /* I - Requested attributes */
3672 ipp_tag_t group
, /* I - Group to copy */
3673 int quickcopy
) /* I - Do a quick copy? */
3675 ipp_attribute_t
*fromattr
; /* Source attribute */
3678 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3679 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3680 to
, from
, ra
, group
, quickcopy
);
3685 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3688 * Filter attributes as needed...
3691 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3692 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
3695 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
3696 copy_attribute(to
, fromattr
, quickcopy
);
3702 * 'copy_banner()' - Copy a banner file to the requests directory for the
3706 static int /* O - Size of banner file in kbytes */
3707 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
3708 cupsd_job_t
*job
, /* I - Job information */
3709 const char *name
) /* I - Name of banner */
3711 int i
; /* Looping var */
3712 int kbytes
; /* Size of banner file in kbytes */
3713 char filename
[1024]; /* Job filename */
3714 cupsd_banner_t
*banner
; /* Pointer to banner */
3715 cups_file_t
*in
; /* Input file */
3716 cups_file_t
*out
; /* Output file */
3717 int ch
; /* Character from file */
3718 char attrname
[255], /* Name of attribute */
3719 *s
; /* Pointer into name */
3720 ipp_attribute_t
*attr
; /* Attribute */
3723 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
3724 con
, con
->http
.fd
, job
, job
->id
, name
? name
: "(null)");
3727 * Find the banner; return if not found or "none"...
3730 if (!name
|| !strcmp(name
, "none") ||
3731 (banner
= cupsdFindBanner(name
)) == NULL
)
3735 * Open the banner and job files...
3738 if (add_file(con
, job
, banner
->filetype
, 0))
3741 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
3743 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
3745 cupsdLogMessage(CUPSD_LOG_ERROR
,
3746 "copy_banner: Unable to create banner job file %s - %s",
3747 filename
, strerror(errno
));
3752 fchmod(cupsFileNumber(out
), 0640);
3753 fchown(cupsFileNumber(out
), RunUser
, Group
);
3756 * Try the localized banner file under the subdirectory...
3759 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
3761 if (strlen(attrname
) > 2 && attrname
[2] == '-')
3764 * Convert ll-cc to ll_CC...
3768 attrname
[3] = toupper(attrname
[3] & 255);
3769 attrname
[4] = toupper(attrname
[4] & 255);
3772 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3775 if (access(filename
, 0) && strlen(attrname
) > 2)
3778 * Wasn't able to find "ll_CC" locale file; try the non-national
3779 * localization banner directory.
3784 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3788 if (access(filename
, 0))
3791 * Use the non-localized banner file.
3794 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
3797 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
3801 cupsdLogMessage(CUPSD_LOG_ERROR
,
3802 "copy_banner: Unable to open banner template file %s - %s",
3803 filename
, strerror(errno
));
3809 * Parse the file to the end...
3812 while ((ch
= cupsFileGetChar(in
)) != EOF
)
3816 * Get an attribute name...
3819 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
3820 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
3822 else if (s
< (attrname
+ sizeof(attrname
) - 1))
3832 * Ignore { followed by stuff that is not an attribute name...
3835 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
3840 * See if it is defined...
3843 if (attrname
[0] == '?')
3848 if (!strcmp(s
, "printer-name"))
3850 cupsFilePuts(out
, job
->dest
);
3853 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
3856 * See if we have a leading question mark...
3859 if (attrname
[0] != '?')
3862 * Nope, write to file as-is; probably a PostScript procedure...
3865 cupsFilePrintf(out
, "{%s}", attrname
);
3872 * Output value(s)...
3875 for (i
= 0; i
< attr
->num_values
; i
++)
3878 cupsFilePutChar(out
, ',');
3880 switch (attr
->value_tag
)
3882 case IPP_TAG_INTEGER
:
3884 if (!strncmp(s
, "time-at-", 8))
3885 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
3887 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
3890 case IPP_TAG_BOOLEAN
:
3891 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
3894 case IPP_TAG_NOVALUE
:
3895 cupsFilePuts(out
, "novalue");
3898 case IPP_TAG_RANGE
:
3899 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
3900 attr
->values
[i
].range
.upper
);
3903 case IPP_TAG_RESOLUTION
:
3904 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3905 attr
->values
[i
].resolution
.yres
,
3906 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3911 case IPP_TAG_STRING
:
3914 case IPP_TAG_KEYWORD
:
3915 case IPP_TAG_CHARSET
:
3916 case IPP_TAG_LANGUAGE
:
3917 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
3920 * Need to quote strings for PS banners...
3925 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
3927 if (*p
== '(' || *p
== ')' || *p
== '\\')
3929 cupsFilePutChar(out
, '\\');
3930 cupsFilePutChar(out
, *p
);
3932 else if (*p
< 32 || *p
> 126)
3933 cupsFilePrintf(out
, "\\%03o", *p
& 255);
3935 cupsFilePutChar(out
, *p
);
3939 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
3943 break; /* anti-compiler-warning-code */
3947 else if (ch
== '\\') /* Quoted char */
3949 ch
= cupsFileGetChar(in
);
3951 if (ch
!= '{') /* Only do special handling for \{ */
3952 cupsFilePutChar(out
, '\\');
3954 cupsFilePutChar(out
, ch
);
3957 cupsFilePutChar(out
, ch
);
3961 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
3963 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
3964 IPP_TAG_INTEGER
)) != NULL
)
3965 attr
->values
[0].integer
+= kbytes
;
3974 * 'copy_file()' - Copy a PPD file or interface script...
3977 static int /* O - 0 = success, -1 = error */
3978 copy_file(const char *from
, /* I - Source file */
3979 const char *to
) /* I - Destination file */
3981 cups_file_t
*src
, /* Source file */
3982 *dst
; /* Destination file */
3983 int bytes
; /* Bytes to read/write */
3984 char buffer
[2048]; /* Copy buffer */
3987 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
3990 * Open the source and destination file for a copy...
3993 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
3996 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4003 * Copy the source file to the destination...
4006 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4007 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
4015 * Close both files and return...
4020 return (cupsFileClose(dst
));
4025 * 'copy_model()' - Copy a PPD model file, substituting default values
4029 static int /* O - 0 = success, -1 = error */
4030 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4031 const char *from
, /* I - Source file */
4032 const char *to
) /* I - Destination file */
4034 fd_set input
; /* select() input set */
4035 struct timeval timeout
; /* select() timeout */
4036 int maxfd
; /* Maximum file descriptor for select() */
4037 char tempfile
[1024]; /* Temporary PPD file */
4038 int tempfd
; /* Temporary PPD file descriptor */
4039 int temppid
; /* Process ID of cups-driverd */
4040 int temppipe
[2]; /* Temporary pipes */
4041 char *argv
[4], /* Command-line arguments */
4042 *envp
[MAX_ENV
]; /* Environment */
4043 cups_file_t
*src
, /* Source file */
4044 *dst
; /* Destination file */
4045 ppd_file_t
*ppd
; /* PPD file */
4046 int bytes
, /* Bytes from pipe */
4047 total
; /* Total bytes from pipe */
4048 char buffer
[2048]; /* Copy buffer */
4049 int i
; /* Looping var */
4050 char option
[PPD_MAX_NAME
], /* Option name */
4051 choice
[PPD_MAX_NAME
]; /* Choice name */
4052 int num_defaults
; /* Number of default options */
4053 cups_option_t
*defaults
; /* Default options */
4054 char cups_protocol
[PPD_MAX_LINE
];
4055 /* cupsProtocol attribute */
4056 int have_letter
, /* Have Letter size */
4057 have_a4
; /* Have A4 size */
4058 #ifdef HAVE_LIBPAPER
4059 char *paper_result
; /* Paper size name from libpaper */
4060 char system_paper
[64]; /* Paper size name buffer */
4061 #endif /* HAVE_LIBPAPER */
4064 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4065 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4069 * Run cups-driverd to get the PPD file...
4072 argv
[0] = "cups-driverd";
4074 argv
[2] = (char *)from
;
4077 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4079 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4080 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
4081 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4085 cupsdOpenPipe(temppipe
);
4087 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4088 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4090 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4091 -1, -1, 0, &temppid
))
4101 * Wait up to 30 seconds for the PPD file to be copied...
4106 if (temppipe
[0] > CGIPipes
[0])
4107 maxfd
= temppipe
[0] + 1;
4109 maxfd
= CGIPipes
[0] + 1;
4114 * See if we have data ready...
4120 FD_SET(temppipe
[0], &input
);
4121 FD_SET(CGIPipes
[0], &input
);
4123 timeout
.tv_sec
= 30;
4124 timeout
.tv_usec
= 0;
4126 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4136 * We have timed out...
4142 if (FD_ISSET(temppipe
[0], &input
))
4145 * Read the PPD file from the pipe, and write it to the PPD file.
4148 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4150 if (write(tempfd
, buffer
, bytes
) < bytes
)
4159 if (FD_ISSET(CGIPipes
[0], &input
))
4169 * No data from cups-deviced...
4172 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
4178 * Read the source file and see what page sizes are supported...
4181 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
4187 have_letter
= ppdPageSize(ppd
, "Letter") != NULL
;
4188 have_a4
= ppdPageSize(ppd
, "A4") != NULL
;
4191 * Open the destination (if possible) and set the default options...
4196 cups_protocol
[0] = '\0';
4198 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4201 * Read all of the default lines from the old PPD...
4204 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4205 if (!strncmp(buffer
, "*Default", 8))
4208 * Add the default option...
4211 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4212 choice
, sizeof(choice
)))
4214 ppd_option_t
*ppdo
; /* PPD option */
4218 * Only add the default if the default hasn't already been
4219 * set and the choice exists in the new PPD...
4222 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4223 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4224 ppdFindChoice(ppdo
, choice
))
4225 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4229 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4230 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4234 #ifdef HAVE_LIBPAPER
4235 else if ((paper_result
= systempapername()) != NULL
)
4238 * Set the default media sizes from the systemwide default...
4241 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
4242 system_paper
[0] = toupper(system_paper
[0] & 255);
4244 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
4245 (!strcmp(system_paper
, "A4") && have_a4
))
4247 num_defaults
= cupsAddOption("PageSize", system_paper
,
4248 num_defaults
, &defaults
);
4249 num_defaults
= cupsAddOption("PageRegion", system_paper
,
4250 num_defaults
, &defaults
);
4251 num_defaults
= cupsAddOption("PaperDimension", system_paper
,
4252 num_defaults
, &defaults
);
4253 num_defaults
= cupsAddOption("ImageableArea", system_paper
,
4254 num_defaults
, &defaults
);
4257 #endif /* HAVE_LIBPAPER */
4261 * Add the default media sizes...
4263 * Note: These values are generally not valid for large-format devices
4264 * like plotters, however it is probably safe to say that those
4265 * users will configure the media size after initially adding
4266 * the device anyways...
4269 if (!DefaultLanguage
||
4270 !strcasecmp(DefaultLanguage
, "C") ||
4271 !strcasecmp(DefaultLanguage
, "POSIX") ||
4272 !strcasecmp(DefaultLanguage
, "en") ||
4273 !strncasecmp(DefaultLanguage
, "en.", 3) ||
4274 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4275 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4276 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4279 * These are the only locales that will default to "letter" size...
4284 num_defaults
= cupsAddOption("PageSize", "Letter", num_defaults
,
4286 num_defaults
= cupsAddOption("PageRegion", "Letter", num_defaults
,
4288 num_defaults
= cupsAddOption("PaperDimension", "Letter", num_defaults
,
4290 num_defaults
= cupsAddOption("ImageableArea", "Letter", num_defaults
,
4297 * The rest default to "a4" size...
4300 num_defaults
= cupsAddOption("PageSize", "A4", num_defaults
,
4302 num_defaults
= cupsAddOption("PageRegion", "A4", num_defaults
,
4304 num_defaults
= cupsAddOption("PaperDimension", "A4", num_defaults
,
4306 num_defaults
= cupsAddOption("ImageableArea", "A4", num_defaults
,
4314 * Open the source file for a copy...
4317 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4319 cupsFreeOptions(num_defaults
, defaults
);
4325 * Open the destination file for a copy...
4328 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4330 cupsFreeOptions(num_defaults
, defaults
);
4337 * Copy the source file to the destination...
4340 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4342 if (!strncmp(buffer
, "*Default", 8))
4345 * Check for an previous default option choice...
4348 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4349 choice
, sizeof(choice
)))
4351 const char *val
; /* Default option value */
4354 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4357 * Substitute the previous choice...
4360 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4365 cupsFilePrintf(dst
, "%s\n", buffer
);
4368 if (cups_protocol
[0])
4369 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4371 cupsFreeOptions(num_defaults
, defaults
);
4374 * Close both files and return...
4381 return (cupsFileClose(dst
));
4386 * 'copy_job_attrs()' - Copy job attributes.
4390 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4391 cupsd_job_t
*job
, /* I - Job */
4392 cups_array_t
*ra
) /* I - Requested attributes array */
4394 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4398 * Send the requested attributes for each job...
4401 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4402 con
->servername
, con
->serverport
, "/jobs/%d",
4405 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
4406 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4407 "job-more-info", NULL
, job_uri
);
4409 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4410 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4411 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4412 job
->num_files
> 0);
4414 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
4415 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4416 "job-printer-up-time", time(NULL
));
4418 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4419 add_job_state_reasons(con
, job
);
4421 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4422 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4423 "job-uri", NULL
, job_uri
);
4425 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
4430 * 'copy_printer_attrs()' - Copy printer attributes.
4435 cupsd_client_t
*con
, /* I - Client connection */
4436 cupsd_printer_t
*printer
, /* I - Printer */
4437 cups_array_t
*ra
) /* I - Requested attributes array */
4439 char printer_uri
[HTTP_MAX_URI
];
4441 time_t curtime
; /* Current time */
4442 int i
; /* Looping var */
4443 ipp_attribute_t
*history
; /* History collection */
4447 * Copy the printer attributes to the response using requested-attributes
4448 * and document-format attributes that may be provided by the client.
4451 curtime
= time(NULL
);
4454 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
4455 printer
->recoverable
)
4456 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4457 "com.apple.print.recoverable-message", NULL
,
4458 printer
->recoverable
);
4459 #endif /* __APPLE__ */
4461 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4462 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
4463 "printer-alert", NULL
, printer
->alert
);
4465 if (printer
->alert_description
&&
4466 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4467 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4468 "printer-alert-description", NULL
,
4469 printer
->alert_description
);
4471 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4472 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4473 ippTimeToDate(curtime
));
4475 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4476 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4477 "printer-error-policy", NULL
, printer
->error_policy
);
4479 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4480 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
4481 printer
->accepting
);
4483 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4484 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
4487 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4488 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4489 "printer-op-policy", NULL
, printer
->op_policy
);
4491 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4492 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4495 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4496 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4497 "printer-state-change-time", printer
->state_time
);
4499 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
4500 cupsArrayFind(ra
, "printer-state-history"))
4503 * Printer history is only sent if specifically requested, so that
4504 * older CUPS/IPP clients won't barf on the collection attributes.
4507 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
4508 "printer-state-history",
4509 printer
->num_history
, NULL
);
4511 for (i
= 0; i
< printer
->num_history
; i
++)
4512 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
4513 NULL
, IPP_TAG_ZERO
, 0);
4516 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4517 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4518 "printer-state-message", NULL
, printer
->state_message
);
4520 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4521 add_printer_state_reasons(con
, printer
);
4523 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4525 int type
; /* printer-type value */
4529 * Add the CUPS-specific printer-type attribute...
4532 type
= printer
->type
;
4534 if (printer
== DefaultPrinter
)
4535 type
|= CUPS_PRINTER_DEFAULT
;
4537 if (!printer
->accepting
)
4538 type
|= CUPS_PRINTER_REJECTING
;
4540 if (!printer
->shared
)
4541 type
|= CUPS_PRINTER_NOT_SHARED
;
4543 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
4547 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4548 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4549 "printer-up-time", curtime
);
4551 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
4552 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
4555 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4556 "ipp", NULL
, con
->servername
, con
->serverport
,
4557 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4558 "/classes/%s" : "/printers/%s", printer
->name
);
4559 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4560 "printer-uri-supported", NULL
, printer_uri
);
4561 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4565 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4566 add_queued_job_count(con
, printer
);
4568 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
4569 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
4574 * 'copy_subscription_attrs()' - Copy subscription attributes.
4578 copy_subscription_attrs(
4579 cupsd_client_t
*con
, /* I - Client connection */
4580 cupsd_subscription_t
*sub
, /* I - Subscription */
4581 cups_array_t
*ra
) /* I - Requested attributes array */
4583 ipp_attribute_t
*attr
; /* Current attribute */
4584 char printer_uri
[HTTP_MAX_URI
];
4586 int count
; /* Number of events */
4587 unsigned mask
; /* Current event mask */
4588 const char *name
; /* Current event name */
4592 * Copy the subscription attributes to the response using the
4593 * requested-attributes attribute that may be provided by the client.
4596 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
4598 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
4601 * Simple event list...
4604 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
4605 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
4606 "notify-events", NULL
, name
);
4611 * Complex event list...
4614 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4615 if (sub
->mask
& mask
)
4618 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
4619 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
4620 "notify-events", count
, NULL
, NULL
);
4622 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4623 if (sub
->mask
& mask
)
4625 attr
->values
[count
].string
.text
=
4626 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
4633 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
4634 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4635 "notify-job-id", sub
->job
->id
);
4637 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
4638 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4639 "notify-lease-duration", sub
->lease
);
4641 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
4643 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4644 "ipp", NULL
, con
->servername
, con
->serverport
,
4645 "/printers/%s", sub
->dest
->name
);
4646 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4647 "notify-printer-uri", NULL
, printer_uri
);
4650 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
4651 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4652 "notify-recipient-uri", NULL
, sub
->recipient
);
4653 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
4654 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
4655 "notify-pull-method", NULL
, "ippget");
4657 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
4658 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
4659 "notify-subscriber-user-name", NULL
, sub
->owner
);
4661 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
4662 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4663 "notify-subscription-id", sub
->id
);
4665 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
4666 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4667 "notify-time-interval", sub
->interval
);
4669 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
4670 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
4671 sub
->user_data
, sub
->user_data_len
);
4676 * 'create_job()' - Print a file to a printer or class.
4680 create_job(cupsd_client_t
*con
, /* I - Client connection */
4681 ipp_attribute_t
*uri
) /* I - Printer URI */
4683 cupsd_printer_t
*printer
; /* Printer */
4684 cupsd_job_t
*job
; /* New job */
4687 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
4688 con
->http
.fd
, uri
->values
[0].string
.text
);
4691 * Is the destination valid?
4694 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
4700 send_ipp_status(con
, IPP_NOT_FOUND
,
4701 _("The printer or class was not found."));
4706 * Create the job object...
4709 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
4713 * Save and log the job...
4718 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d created on \"%s\" by \"%s\".",
4719 job
->id
, job
->dest
, job
->username
);
4724 * 'create_requested_array()' - Create an array for the requested-attributes.
4727 static cups_array_t
* /* O - Array of attributes or NULL */
4728 create_requested_array(ipp_t
*request
) /* I - IPP request */
4730 int i
; /* Looping var */
4731 ipp_attribute_t
*requested
; /* requested-attributes attribute */
4732 cups_array_t
*ra
; /* Requested attributes array */
4733 char *value
; /* Current value */
4737 * Get the requested-attributes attribute, and return NULL if we don't
4741 if ((requested
= ippFindAttribute(request
, "requested-attributes",
4742 IPP_TAG_KEYWORD
)) == NULL
)
4746 * If the attribute contains a single "all" keyword, return NULL...
4749 if (requested
->num_values
== 1 &&
4750 !strcmp(requested
->values
[0].string
.text
, "all"))
4754 * Create an array using "strcmp" as the comparison function...
4757 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
4759 for (i
= 0; i
< requested
->num_values
; i
++)
4761 value
= requested
->values
[i
].string
.text
;
4763 if (!strcmp(value
, "job-template"))
4765 cupsArrayAdd(ra
, "copies");
4766 cupsArrayAdd(ra
, "copies-default");
4767 cupsArrayAdd(ra
, "copies-supported");
4768 cupsArrayAdd(ra
, "finishings");
4769 cupsArrayAdd(ra
, "finishings-default");
4770 cupsArrayAdd(ra
, "finishings-supported");
4771 cupsArrayAdd(ra
, "job-hold-until");
4772 cupsArrayAdd(ra
, "job-hold-until-default");
4773 cupsArrayAdd(ra
, "job-hold-until-supported");
4774 cupsArrayAdd(ra
, "job-priority");
4775 cupsArrayAdd(ra
, "job-priority-default");
4776 cupsArrayAdd(ra
, "job-priority-supported");
4777 cupsArrayAdd(ra
, "job-sheets");
4778 cupsArrayAdd(ra
, "job-sheets-default");
4779 cupsArrayAdd(ra
, "job-sheets-supported");
4780 cupsArrayAdd(ra
, "media");
4781 cupsArrayAdd(ra
, "media-default");
4782 cupsArrayAdd(ra
, "media-supported");
4783 cupsArrayAdd(ra
, "multiple-document-handling");
4784 cupsArrayAdd(ra
, "multiple-document-handling-default");
4785 cupsArrayAdd(ra
, "multiple-document-handling-supported");
4786 cupsArrayAdd(ra
, "number-up");
4787 cupsArrayAdd(ra
, "number-up-default");
4788 cupsArrayAdd(ra
, "number-up-supported");
4789 cupsArrayAdd(ra
, "orientation-requested");
4790 cupsArrayAdd(ra
, "orientation-requested-default");
4791 cupsArrayAdd(ra
, "orientation-requested-supported");
4792 cupsArrayAdd(ra
, "page-ranges");
4793 cupsArrayAdd(ra
, "page-ranges-supported");
4794 cupsArrayAdd(ra
, "printer-resolution");
4795 cupsArrayAdd(ra
, "printer-resolution-default");
4796 cupsArrayAdd(ra
, "printer-resolution-supported");
4797 cupsArrayAdd(ra
, "print-quality");
4798 cupsArrayAdd(ra
, "print-quality-default");
4799 cupsArrayAdd(ra
, "print-quality-supported");
4800 cupsArrayAdd(ra
, "sides");
4801 cupsArrayAdd(ra
, "sides-default");
4802 cupsArrayAdd(ra
, "sides-supported");
4804 else if (!strcmp(value
, "job-description"))
4806 cupsArrayAdd(ra
, "date-time-at-completed");
4807 cupsArrayAdd(ra
, "date-time-at-creation");
4808 cupsArrayAdd(ra
, "date-time-at-processing");
4809 cupsArrayAdd(ra
, "job-detailed-status-message");
4810 cupsArrayAdd(ra
, "job-document-access-errors");
4811 cupsArrayAdd(ra
, "job-id");
4812 cupsArrayAdd(ra
, "job-impressions");
4813 cupsArrayAdd(ra
, "job-impressions-completed");
4814 cupsArrayAdd(ra
, "job-k-octets");
4815 cupsArrayAdd(ra
, "job-k-octets-processed");
4816 cupsArrayAdd(ra
, "job-media-sheets");
4817 cupsArrayAdd(ra
, "job-media-sheets-completed");
4818 cupsArrayAdd(ra
, "job-message-from-operator");
4819 cupsArrayAdd(ra
, "job-more-info");
4820 cupsArrayAdd(ra
, "job-name");
4821 cupsArrayAdd(ra
, "job-originating-user-name");
4822 cupsArrayAdd(ra
, "job-printer-up-time");
4823 cupsArrayAdd(ra
, "job-printer-uri");
4824 cupsArrayAdd(ra
, "job-state");
4825 cupsArrayAdd(ra
, "job-state-message");
4826 cupsArrayAdd(ra
, "job-state-reasons");
4827 cupsArrayAdd(ra
, "job-uri");
4828 cupsArrayAdd(ra
, "number-of-documents");
4829 cupsArrayAdd(ra
, "number-of-intervening-jobs");
4830 cupsArrayAdd(ra
, "output-device-assigned");
4831 cupsArrayAdd(ra
, "time-at-completed");
4832 cupsArrayAdd(ra
, "time-at-creation");
4833 cupsArrayAdd(ra
, "time-at-processing");
4835 else if (!strcmp(value
, "printer-description"))
4837 cupsArrayAdd(ra
, "charset-configured");
4838 cupsArrayAdd(ra
, "charset-supported");
4839 cupsArrayAdd(ra
, "color-supported");
4840 cupsArrayAdd(ra
, "compression-supported");
4841 cupsArrayAdd(ra
, "document-format-default");
4842 cupsArrayAdd(ra
, "document-format-supported");
4843 cupsArrayAdd(ra
, "generated-natural-language-supported");
4844 cupsArrayAdd(ra
, "ipp-versions-supported");
4845 cupsArrayAdd(ra
, "job-impressions-supported");
4846 cupsArrayAdd(ra
, "job-k-octets-supported");
4847 cupsArrayAdd(ra
, "job-media-sheets-supported");
4848 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
4849 cupsArrayAdd(ra
, "multiple-operation-time-out");
4850 cupsArrayAdd(ra
, "natural-language-configured");
4851 cupsArrayAdd(ra
, "notify-attributes-supported");
4852 cupsArrayAdd(ra
, "notify-lease-duration-default");
4853 cupsArrayAdd(ra
, "notify-lease-duration-supported");
4854 cupsArrayAdd(ra
, "notify-max-events-supported");
4855 cupsArrayAdd(ra
, "notify-events-default");
4856 cupsArrayAdd(ra
, "notify-events-supported");
4857 cupsArrayAdd(ra
, "notify-pull-method-supported");
4858 cupsArrayAdd(ra
, "notify-schemes-supported");
4859 cupsArrayAdd(ra
, "operations-supported");
4860 cupsArrayAdd(ra
, "pages-per-minute");
4861 cupsArrayAdd(ra
, "pages-per-minute-color");
4862 cupsArrayAdd(ra
, "pdl-override-supported");
4863 cupsArrayAdd(ra
, "printer-alert");
4864 cupsArrayAdd(ra
, "printer-alert-description");
4865 cupsArrayAdd(ra
, "printer-current-time");
4866 cupsArrayAdd(ra
, "printer-driver-installer");
4867 cupsArrayAdd(ra
, "printer-info");
4868 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
4869 cupsArrayAdd(ra
, "printer-location");
4870 cupsArrayAdd(ra
, "printer-make-and-model");
4871 cupsArrayAdd(ra
, "printer-message-from-operator");
4872 cupsArrayAdd(ra
, "printer-more-info");
4873 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
4874 cupsArrayAdd(ra
, "printer-name");
4875 cupsArrayAdd(ra
, "printer-state");
4876 cupsArrayAdd(ra
, "printer-state-message");
4877 cupsArrayAdd(ra
, "printer-state-reasons");
4878 cupsArrayAdd(ra
, "printer-up-time");
4879 cupsArrayAdd(ra
, "printer-uri-supported");
4880 cupsArrayAdd(ra
, "queued-job-count");
4881 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
4882 cupsArrayAdd(ra
, "uri-authentication-supported");
4883 cupsArrayAdd(ra
, "uri-security-supported");
4885 else if (!strcmp(value
, "printer-defaults"))
4887 char *name
; /* Option name */
4890 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
4892 name
= (char *)cupsArrayNext(CommonDefaults
))
4893 cupsArrayAdd(ra
, name
);
4895 else if (!strcmp(value
, "subscription-template"))
4897 cupsArrayAdd(ra
, "notify-attributes");
4898 cupsArrayAdd(ra
, "notify-charset");
4899 cupsArrayAdd(ra
, "notify-events");
4900 cupsArrayAdd(ra
, "notify-lease-duration");
4901 cupsArrayAdd(ra
, "notify-natural-language");
4902 cupsArrayAdd(ra
, "notify-pull-method");
4903 cupsArrayAdd(ra
, "notify-recipient-uri");
4904 cupsArrayAdd(ra
, "notify-time-interval");
4905 cupsArrayAdd(ra
, "notify-user-data");
4908 cupsArrayAdd(ra
, value
);
4916 * 'create_subscription()' - Create a notification subscription.
4920 create_subscription(
4921 cupsd_client_t
*con
, /* I - Client connection */
4922 ipp_attribute_t
*uri
) /* I - Printer URI */
4924 http_status_t status
; /* Policy status */
4925 int i
; /* Looping var */
4926 ipp_attribute_t
*attr
; /* Current attribute */
4927 cups_ptype_t dtype
; /* Destination type (printer or class) */
4928 char scheme
[HTTP_MAX_URI
],
4929 /* Scheme portion of URI */
4930 userpass
[HTTP_MAX_URI
],
4931 /* Username portion of URI */
4933 /* Host portion of URI */
4934 resource
[HTTP_MAX_URI
];
4935 /* Resource portion of URI */
4936 int port
; /* Port portion of URI */
4937 cupsd_printer_t
*printer
; /* Printer/class */
4938 cupsd_job_t
*job
; /* Job */
4939 int jobid
; /* Job ID */
4940 cupsd_subscription_t
*sub
; /* Subscription object */
4941 const char *username
, /* requesting-user-name or authenticated username */
4942 *recipient
, /* notify-recipient-uri */
4943 *pullmethod
; /* notify-pull-method */
4944 ipp_attribute_t
*user_data
; /* notify-user-data */
4945 int interval
, /* notify-time-interval */
4946 lease
; /* notify-lease-duration */
4947 unsigned mask
; /* notify-events */
4948 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
4949 *notify_lease
; /* notify-lease-duration(-default) */
4953 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4955 if (attr
->group_tag
!= IPP_TAG_ZERO
)
4956 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
4957 attr
->value_tag
, attr
->name
);
4959 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
4964 * Is the destination valid?
4967 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4968 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
4969 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
4971 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
4972 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
4973 sizeof(host
), &port
, resource
, sizeof(resource
));
4975 if (!strcmp(resource
, "/"))
4977 dtype
= (cups_ptype_t
)0;
4980 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
4982 dtype
= (cups_ptype_t
)0;
4985 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
4987 dtype
= CUPS_PRINTER_CLASS
;
4990 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
4996 send_ipp_status(con
, IPP_NOT_FOUND
,
4997 _("The printer or class was not found."));
5007 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5010 send_http_error(con
, status
, printer
);
5014 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5016 send_http_error(con
, status
, NULL
);
5021 * Get the user that is requesting the subscription...
5024 username
= get_username(con
);
5027 * Find the first subscription group attribute; return if we have
5031 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5032 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5037 send_ipp_status(con
, IPP_BAD_REQUEST
,
5038 _("No subscription attributes in request!"));
5043 * Process the subscription attributes in the request...
5046 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5054 lease
= DefaultLeaseDuration
;
5056 mask
= CUPSD_EVENT_NONE
;
5060 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5062 notify_lease
= ippFindAttribute(printer
->attrs
,
5063 "notify-lease-duration-default",
5067 lease
= notify_lease
->values
[0].integer
;
5071 notify_events
= NULL
;
5072 notify_lease
= NULL
;
5075 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5077 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5078 attr
->value_tag
== IPP_TAG_URI
)
5081 * Validate the recipient scheme against the ServerBin/notifier
5085 char notifier
[1024]; /* Notifier filename */
5088 recipient
= attr
->values
[0].string
.text
;
5090 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5091 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5092 host
, sizeof(host
), &port
,
5093 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5095 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5096 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
5097 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5098 "notify-status-code", IPP_URI_SCHEME
);
5102 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5104 if (access(notifier
, X_OK
))
5106 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5107 _("notify-recipient-uri URI \"%s\" uses unknown scheme!"),
5109 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5110 "notify-status-code", IPP_URI_SCHEME
);
5114 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5115 attr
->value_tag
== IPP_TAG_KEYWORD
)
5117 pullmethod
= attr
->values
[0].string
.text
;
5119 if (strcmp(pullmethod
, "ippget"))
5121 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5122 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
5123 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5124 "notify-status-code", IPP_ATTRIBUTES
);
5128 else if (!strcmp(attr
->name
, "notify-charset") &&
5129 attr
->value_tag
== IPP_TAG_CHARSET
&&
5130 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5131 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5133 send_ipp_status(con
, IPP_CHARSET
,
5134 _("Character set \"%s\" not supported!"),
5135 attr
->values
[0].string
.text
);
5138 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5139 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5140 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5142 send_ipp_status(con
, IPP_CHARSET
,
5143 _("Language \"%s\" not supported!"),
5144 attr
->values
[0].string
.text
);
5147 else if (!strcmp(attr
->name
, "notify-user-data") &&
5148 attr
->value_tag
== IPP_TAG_STRING
)
5150 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5152 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5153 _("The notify-user-data value is too large "
5154 "(%d > 63 octets)!"),
5155 attr
->values
[0].unknown
.length
);
5161 else if (!strcmp(attr
->name
, "notify-events") &&
5162 attr
->value_tag
== IPP_TAG_KEYWORD
)
5163 notify_events
= attr
;
5164 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5165 attr
->value_tag
== IPP_TAG_INTEGER
)
5166 lease
= attr
->values
[0].integer
;
5167 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5168 attr
->value_tag
== IPP_TAG_INTEGER
)
5169 interval
= attr
->values
[0].integer
;
5170 else if (!strcmp(attr
->name
, "notify-job-id") &&
5171 attr
->value_tag
== IPP_TAG_INTEGER
)
5172 jobid
= attr
->values
[0].integer
;
5179 for (i
= 0; i
< notify_events
->num_values
; i
++)
5180 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5184 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5186 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5187 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5188 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5190 if (!recipient
&& !pullmethod
)
5193 if (mask
== CUPSD_EVENT_NONE
)
5196 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5198 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5201 send_ipp_status(con
, IPP_BAD_REQUEST
,
5202 _("notify-events not specified!"));
5207 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5209 cupsdLogMessage(CUPSD_LOG_INFO
,
5210 "create_subscription: Limiting notify-lease-duration to "
5213 lease
= MaxLeaseDuration
;
5218 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5220 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
5227 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
5230 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
5233 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5234 "Added subscription %d for printer \"%s\"",
5235 sub
->id
, printer
->name
);
5237 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
5240 sub
->interval
= interval
;
5242 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5244 cupsdSetString(&sub
->owner
, username
);
5248 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5249 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5250 sub
->user_data_len
);
5253 ippAddSeparator(con
->response
);
5254 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5255 "notify-subscription-id", sub
->id
);
5257 con
->response
->request
.status
.status_code
= IPP_OK
;
5263 cupsdSaveAllSubscriptions();
5269 * 'delete_printer()' - Remove a printer or class from the system.
5273 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5274 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5276 http_status_t status
; /* Policy status */
5277 cups_ptype_t dtype
; /* Destination type (printer or class) */
5278 cupsd_printer_t
*printer
; /* Printer/class */
5279 char filename
[1024]; /* Script/PPD filename */
5282 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5283 con
->http
.fd
, uri
->values
[0].string
.text
);
5286 * Do we have a valid URI?
5289 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5295 send_ipp_status(con
, IPP_NOT_FOUND
,
5296 _("The printer or class was not found."));
5304 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5306 send_http_error(con
, status
, NULL
);
5311 * Remove old jobs...
5314 cupsdCancelJobs(printer
->name
, NULL
, 1);
5317 * Remove old subscriptions and send a "deleted printer" event...
5320 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5321 "%s \"%s\" deleted by \"%s\".",
5322 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5323 printer
->name
, get_username(con
));
5325 cupsdExpireSubscriptions(printer
, NULL
);
5328 * Remove any old PPD or script files...
5331 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5335 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5339 if (dtype
& CUPS_PRINTER_CLASS
)
5341 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5342 printer
->name
, get_username(con
));
5344 cupsdDeletePrinter(printer
, 0);
5345 cupsdSaveAllClasses();
5349 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5350 printer
->name
, get_username(con
));
5352 cupsdDeletePrinter(printer
, 0);
5353 cupsdSaveAllPrinters();
5356 cupsdWritePrintcap();
5359 * Return with no errors...
5362 con
->response
->request
.status
.status_code
= IPP_OK
;
5367 * 'get_default()' - Get the default destination.
5371 get_default(cupsd_client_t
*con
) /* I - Client connection */
5373 http_status_t status
; /* Policy status */
5374 cups_array_t
*ra
; /* Requested attributes array */
5377 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5383 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5385 send_http_error(con
, status
, NULL
);
5391 ra
= create_requested_array(con
->request
);
5393 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5395 cupsArrayDelete(ra
);
5397 con
->response
->request
.status
.status_code
= IPP_OK
;
5400 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5405 * 'get_devices()' - Get the list of available devices on the local system.
5409 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5411 http_status_t status
; /* Policy status */
5412 ipp_attribute_t
*limit
, /* Limit attribute */
5413 *requested
; /* requested-attributes attribute */
5414 char command
[1024], /* cups-deviced command */
5415 options
[1024], /* Options to pass to command */
5417 /* String for requested attributes */
5420 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5426 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5428 send_http_error(con
, status
, NULL
);
5433 * Run cups-deviced command with the given options...
5436 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5437 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5441 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5443 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5445 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5446 snprintf(options
, sizeof(options
),
5448 con
->request
->request
.op
.request_id
,
5449 limit
? limit
->values
[0].integer
: 0, (int)User
,
5452 if (cupsdSendCommand(con
, command
, options
, 1))
5455 * Command started successfully, don't send an IPP response here...
5458 ippDelete(con
->response
);
5459 con
->response
= NULL
;
5464 * Command failed, return "internal error" so the user knows something
5468 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5469 _("cups-deviced failed to execute."));
5475 * 'get_job_attrs()' - Get job attributes.
5479 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5480 ipp_attribute_t
*uri
) /* I - Job URI */
5482 http_status_t status
; /* Policy status */
5483 ipp_attribute_t
*attr
; /* Current attribute */
5484 int jobid
; /* Job ID */
5485 cupsd_job_t
*job
; /* Current job */
5486 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5487 username
[HTTP_MAX_URI
], /* Username portion of URI */
5488 host
[HTTP_MAX_URI
], /* Host portion of URI */
5489 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5490 int port
; /* Port portion of URI */
5491 cups_array_t
*ra
; /* Requested attributes array */
5494 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5495 con
->http
.fd
, uri
->values
[0].string
.text
);
5498 * See if we have a job URI or a printer URI...
5501 if (!strcmp(uri
->name
, "printer-uri"))
5504 * Got a printer URI; see if we also have a job-id attribute...
5507 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5508 IPP_TAG_INTEGER
)) == NULL
)
5510 send_ipp_status(con
, IPP_BAD_REQUEST
,
5511 _("Got a printer-uri attribute but no job-id!"));
5515 jobid
= attr
->values
[0].integer
;
5520 * Got a job URI; parse it to get the job ID...
5523 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5524 sizeof(method
), username
, sizeof(username
), host
,
5525 sizeof(host
), &port
, resource
, sizeof(resource
));
5527 if (strncmp(resource
, "/jobs/", 6))
5533 send_ipp_status(con
, IPP_BAD_REQUEST
,
5534 _("Bad job-uri attribute \"%s\"!"),
5535 uri
->values
[0].string
.text
);
5539 jobid
= atoi(resource
+ 6);
5543 * See if the job exists...
5546 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5549 * Nope - return a "not found" error...
5552 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5560 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5562 send_http_error(con
, status
, NULL
);
5567 * Copy attributes...
5572 ra
= create_requested_array(con
->request
);
5573 copy_job_attrs(con
, job
, ra
);
5574 cupsArrayDelete(ra
);
5576 con
->response
->request
.status
.status_code
= IPP_OK
;
5581 * 'get_jobs()' - Get a list of jobs for the specified printer.
5585 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
5586 ipp_attribute_t
*uri
) /* I - Printer URI */
5588 http_status_t status
; /* Policy status */
5589 ipp_attribute_t
*attr
; /* Current attribute */
5590 const char *dest
; /* Destination */
5591 cups_ptype_t dtype
; /* Destination type (printer or class) */
5592 cups_ptype_t dmask
; /* Destination type mask */
5593 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
5594 username
[HTTP_MAX_URI
], /* Username portion of URI */
5595 host
[HTTP_MAX_URI
], /* Host portion of URI */
5596 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5597 int port
; /* Port portion of URI */
5598 int completed
; /* Completed jobs? */
5599 int first_job_id
; /* First job ID */
5600 int limit
; /* Maximum number of jobs to return */
5601 int count
; /* Number of jobs that match */
5602 cupsd_job_t
*job
; /* Current job pointer */
5603 cupsd_printer_t
*printer
; /* Printer */
5604 cups_array_t
*list
; /* Which job list... */
5605 cups_array_t
*ra
; /* Requested attributes array */
5608 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
5609 uri
->values
[0].string
.text
);
5612 * Is the destination valid?
5615 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5616 sizeof(scheme
), username
, sizeof(username
), host
,
5617 sizeof(host
), &port
, resource
, sizeof(resource
));
5619 if (!strcmp(resource
, "/") ||
5620 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
5623 dtype
= (cups_ptype_t
)0;
5624 dmask
= (cups_ptype_t
)0;
5627 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5630 dtype
= (cups_ptype_t
)0;
5631 dmask
= CUPS_PRINTER_CLASS
;
5634 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5637 dtype
= CUPS_PRINTER_CLASS
;
5638 dmask
= CUPS_PRINTER_CLASS
;
5641 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
5648 send_ipp_status(con
, IPP_NOT_FOUND
,
5649 _("The printer or class was not found."));
5654 dtype
&= CUPS_PRINTER_CLASS
;
5655 dmask
= CUPS_PRINTER_CLASS
;
5664 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5667 send_http_error(con
, status
, printer
);
5671 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5673 send_http_error(con
, status
, NULL
);
5678 * See if the "which-jobs" attribute have been specified...
5681 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
5682 IPP_TAG_KEYWORD
)) != NULL
&&
5683 !strcmp(attr
->values
[0].string
.text
, "completed"))
5688 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
5700 * See if they want to limit the number of jobs reported...
5703 if ((attr
= ippFindAttribute(con
->request
, "limit",
5704 IPP_TAG_INTEGER
)) != NULL
)
5705 limit
= attr
->values
[0].integer
;
5709 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
5710 IPP_TAG_INTEGER
)) != NULL
)
5711 first_job_id
= attr
->values
[0].integer
;
5716 * See if we only want to see jobs for a specific user...
5719 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
5720 IPP_TAG_BOOLEAN
)) != NULL
&&
5721 attr
->values
[0].boolean
)
5722 strlcpy(username
, get_username(con
), sizeof(username
));
5726 ra
= create_requested_array(con
->request
);
5729 * OK, build a list of jobs for this printer...
5732 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
5733 count
< limit
&& job
;
5734 job
= (cupsd_job_t
*)cupsArrayNext(list
))
5737 * Filter out jobs that don't match...
5740 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
5742 if ((dest
&& strcmp(job
->dest
, dest
)) &&
5743 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
5745 if ((job
->dtype
& dmask
) != dtype
&&
5746 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
5748 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
5751 if (job
->id
< first_job_id
)
5759 if (username
[0] && strcasecmp(username
, job
->username
))
5763 ippAddSeparator(con
->response
);
5767 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
5769 copy_job_attrs(con
, job
, ra
);
5772 cupsArrayDelete(ra
);
5774 con
->response
->request
.status
.status_code
= IPP_OK
;
5779 * 'get_notifications()' - Get events for a subscription.
5783 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
5785 int i
, j
; /* Looping vars */
5786 http_status_t status
; /* Policy status */
5787 cupsd_subscription_t
*sub
; /* Subscription */
5788 ipp_attribute_t
*ids
, /* notify-subscription-ids */
5789 *sequences
; /* notify-sequence-numbers */
5790 int min_seq
; /* Minimum sequence number */
5791 int interval
; /* Poll interval */
5794 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
5798 * Get subscription attributes...
5801 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
5803 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
5808 send_ipp_status(con
, IPP_BAD_REQUEST
,
5809 _("Missing notify-subscription-ids attribute!"));
5814 * Are the subscription IDs valid?
5817 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
5819 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
5822 * Bad subscription ID...
5825 send_ipp_status(con
, IPP_NOT_FOUND
,
5826 _("notify-subscription-id %d no good!"),
5827 ids
->values
[i
].integer
);
5835 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5837 con
, sub
->owner
)) != HTTP_OK
)
5839 send_http_error(con
, status
, sub
->dest
);
5844 * Check the subscription type and update the interval accordingly.
5847 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
5850 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
5852 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
5858 * Tell the client to poll again in N seconds...
5862 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5863 "notify-get-interval", interval
);
5865 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5866 "printer-up-time", time(NULL
));
5869 * Copy the subscription event attributes to the response.
5872 con
->response
->request
.status
.status_code
=
5873 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
5875 for (i
= 0; i
< ids
->num_values
; i
++)
5878 * Get the subscription and sequence number...
5881 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
5883 if (sequences
&& i
< sequences
->num_values
)
5884 min_seq
= sequences
->values
[i
].integer
;
5889 * If we don't have any new events, nothing to do here...
5892 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
5896 * Otherwise copy all of the new events...
5899 if (sub
->first_event_id
> min_seq
)
5902 j
= min_seq
- sub
->first_event_id
;
5904 for (; j
< sub
->num_events
; j
++)
5906 ippAddSeparator(con
->response
);
5908 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
5909 IPP_TAG_EVENT_NOTIFICATION
, 0);
5916 * 'get_ppd()' - Get a named PPD from the local system.
5920 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
5921 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
5923 http_status_t status
; /* Policy status */
5924 cupsd_printer_t
*dest
; /* Destination */
5925 cups_ptype_t dtype
; /* Destination type */
5928 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
5929 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
5931 if (!strcmp(uri
->name
, "ppd-name"))
5934 * Return a PPD file from cups-driverd...
5937 char command
[1024], /* cups-driverd command */
5938 options
[1024], /* Options to pass to command */
5939 ppd_name
[1024]; /* ppd-name */
5946 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5948 send_http_error(con
, status
, NULL
);
5953 * Run cups-driverd command with the given options...
5956 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
5957 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
5958 snprintf(options
, sizeof(options
), "get+%d+%s",
5959 con
->request
->request
.op
.request_id
, ppd_name
);
5961 if (cupsdSendCommand(con
, command
, options
, 0))
5964 * Command started successfully, don't send an IPP response here...
5967 ippDelete(con
->response
);
5968 con
->response
= NULL
;
5973 * Command failed, return "internal error" so the user knows something
5977 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5978 _("cups-driverd failed to execute."));
5981 else if (!strcmp(uri
->name
, "printer-uri") &&
5982 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
5984 int i
; /* Looping var */
5985 char filename
[1024]; /* PPD filename */
5992 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
5994 send_http_error(con
, status
, NULL
);
5999 * See if we need the PPD for a class or remote printer...
6002 if (dtype
& CUPS_PRINTER_REMOTE
)
6004 send_ipp_status(con
, CUPS_SEE_OTHER
, NULL
);
6005 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6006 "printer-uri", NULL
, dest
->uri
);
6009 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
6011 for (i
= 0; i
< dest
->num_printers
; i
++)
6012 if (!(dest
->printers
[i
]->type
&
6013 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
6014 CUPS_PRINTER_REMOTE
)))
6017 if (i
< dest
->num_printers
)
6018 dest
= dest
->printers
[i
];
6021 send_ipp_status(con
, CUPS_SEE_OTHER
, NULL
);
6022 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6023 "printer-uri", NULL
, dest
->printers
[0]->uri
);
6029 * Found the printer with the PPD file, now see if there is one...
6032 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6035 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
6037 send_ipp_status(con
, IPP_NOT_FOUND
,
6038 _("The PPD file \"%s\" could not be opened: %s"),
6039 uri
->values
[i
].string
.text
, strerror(errno
));
6043 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6047 send_ipp_status(con
, IPP_OK
, NULL
);
6050 send_ipp_status(con
, IPP_NOT_FOUND
,
6051 _("The PPD file \"%s\" could not be found."),
6052 uri
->values
[0].string
.text
);
6057 * 'get_ppds()' - Get the list of PPD files on the local system.
6061 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
6063 http_status_t status
; /* Policy status */
6064 ipp_attribute_t
*limit
, /* Limit attribute */
6065 *device
, /* ppd-device-id attribute */
6066 *language
, /* ppd-natural-language attribute */
6067 *make
, /* ppd-make attribute */
6068 *model
, /* ppd-make-and-model attribute */
6069 *product
, /* ppd-product attribute */
6070 *psversion
, /* ppd-psverion attribute */
6071 *requested
; /* requested-attributes attribute */
6072 char command
[1024], /* cups-driverd command */
6073 options
[1024], /* Options to pass to command */
6074 device_str
[256],/* Escaped ppd-device-id string */
6076 /* Escaped ppd-natural-language string */
6077 make_str
[256], /* Escaped ppd-make string */
6078 model_str
[256], /* Escaped ppd-make-and-model string */
6080 /* Escaped ppd-product string */
6082 /* Escaped ppd-psversion string */
6084 /* String for requested attributes */
6087 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
6093 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6095 send_http_error(con
, status
, NULL
);
6100 * Run cups-driverd command with the given options...
6103 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6104 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
6105 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
6107 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
6108 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
6110 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
6111 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
6112 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6116 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6118 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6121 url_encode_attr(device
, device_str
, sizeof(device_str
));
6123 device_str
[0] = '\0';
6126 url_encode_attr(language
, language_str
, sizeof(language_str
));
6128 language_str
[0] = '\0';
6131 url_encode_attr(make
, make_str
, sizeof(make_str
));
6136 url_encode_attr(model
, model_str
, sizeof(model_str
));
6138 model_str
[0] = '\0';
6141 url_encode_attr(product
, product_str
, sizeof(product_str
));
6143 product_str
[0] = '\0';
6146 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
6148 psversion_str
[0] = '\0';
6150 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6151 snprintf(options
, sizeof(options
), "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s",
6152 con
->request
->request
.op
.request_id
,
6153 limit
? limit
->values
[0].integer
: 0,
6155 device
? "%20" : "", device_str
,
6156 language
? "%20" : "", language_str
,
6157 make
? "%20" : "", make_str
,
6158 model
? "%20" : "", model_str
,
6159 product
? "%20" : "", product_str
,
6160 psversion
? "%20" : "", psversion_str
);
6162 if (cupsdSendCommand(con
, command
, options
, 0))
6165 * Command started successfully, don't send an IPP response here...
6168 ippDelete(con
->response
);
6169 con
->response
= NULL
;
6174 * Command failed, return "internal error" so the user knows something
6178 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6179 _("cups-driverd failed to execute."));
6185 * 'get_printer_attrs()' - Get printer attributes.
6189 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
6190 ipp_attribute_t
*uri
) /* I - Printer URI */
6192 http_status_t status
; /* Policy status */
6193 cups_ptype_t dtype
; /* Destination type (printer or class) */
6194 cupsd_printer_t
*printer
; /* Printer/class */
6195 cups_array_t
*ra
; /* Requested attributes array */
6198 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
6199 con
->http
.fd
, uri
->values
[0].string
.text
);
6202 * Is the destination valid?
6205 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6211 send_ipp_status(con
, IPP_NOT_FOUND
,
6212 _("The printer or class was not found."));
6220 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6222 send_http_error(con
, status
, printer
);
6227 * Send the attributes...
6230 ra
= create_requested_array(con
->request
);
6232 copy_printer_attrs(con
, printer
, ra
);
6234 cupsArrayDelete(ra
);
6236 con
->response
->request
.status
.status_code
= IPP_OK
;
6241 * 'get_printers()' - Get a list of printers or classes.
6245 get_printers(cupsd_client_t
*con
, /* I - Client connection */
6246 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
6248 http_status_t status
; /* Policy status */
6249 ipp_attribute_t
*attr
; /* Current attribute */
6250 int limit
; /* Maximum number of printers to return */
6251 int count
; /* Number of printers that match */
6252 cupsd_printer_t
*printer
; /* Current printer pointer */
6253 int printer_type
, /* printer-type attribute */
6254 printer_mask
; /* printer-type-mask attribute */
6255 char *location
; /* Location string */
6256 const char *username
; /* Current user */
6257 char *first_printer_name
; /* first-printer-name attribute */
6258 cups_array_t
*ra
; /* Requested attributes array */
6261 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
6262 con
->http
.fd
, type
);
6268 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6270 send_http_error(con
, status
, NULL
);
6275 * Check for printers...
6278 if (!Printers
|| !cupsArrayCount(Printers
))
6280 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
6285 * See if they want to limit the number of printers reported...
6288 if ((attr
= ippFindAttribute(con
->request
, "limit",
6289 IPP_TAG_INTEGER
)) != NULL
)
6290 limit
= attr
->values
[0].integer
;
6294 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
6295 IPP_TAG_NAME
)) != NULL
)
6296 first_printer_name
= attr
->values
[0].string
.text
;
6298 first_printer_name
= NULL
;
6301 * Support filtering...
6304 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
6305 IPP_TAG_ENUM
)) != NULL
)
6306 printer_type
= attr
->values
[0].integer
;
6310 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
6311 IPP_TAG_ENUM
)) != NULL
)
6312 printer_mask
= attr
->values
[0].integer
;
6316 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
6317 IPP_TAG_TEXT
)) != NULL
)
6318 location
= attr
->values
[0].string
.text
;
6322 if (con
->username
[0])
6323 username
= con
->username
;
6324 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6325 IPP_TAG_NAME
)) != NULL
)
6326 username
= attr
->values
[0].string
.text
;
6330 ra
= create_requested_array(con
->request
);
6333 * OK, build a list of printers for this printer...
6336 if (first_printer_name
)
6338 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
6339 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
6342 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
6345 count
< limit
&& printer
;
6346 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
6348 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
6349 (printer
->type
& printer_mask
) == printer_type
&&
6350 (!location
|| !printer
->location
||
6351 !strcasecmp(printer
->location
, location
)))
6354 * If HideImplicitMembers is enabled, see if this printer or class
6355 * is a member of an implicit class...
6358 if (ImplicitClasses
&& HideImplicitMembers
&&
6359 printer
->in_implicit_class
)
6363 * If a username is specified, see if it is allowed or denied
6367 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
6371 * Add the group separator as needed...
6375 ippAddSeparator(con
->response
);
6380 * Send the attributes...
6383 copy_printer_attrs(con
, printer
, ra
);
6387 cupsArrayDelete(ra
);
6389 con
->response
->request
.status
.status_code
= IPP_OK
;
6394 * 'get_subscription_attrs()' - Get subscription attributes.
6398 get_subscription_attrs(
6399 cupsd_client_t
*con
, /* I - Client connection */
6400 int sub_id
) /* I - Subscription ID */
6402 http_status_t status
; /* Policy status */
6403 cupsd_subscription_t
*sub
; /* Subscription */
6404 cups_array_t
*ra
; /* Requested attributes array */
6407 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6408 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
6409 con
, con
->http
.fd
, sub_id
);
6412 * Is the subscription ID valid?
6415 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
6418 * Bad subscription ID...
6421 send_ipp_status(con
, IPP_NOT_FOUND
,
6422 _("notify-subscription-id %d no good!"), sub_id
);
6430 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6432 con
, sub
->owner
)) != HTTP_OK
)
6434 send_http_error(con
, status
, sub
->dest
);
6439 * Copy the subscription attributes to the response using the
6440 * requested-attributes attribute that may be provided by the client.
6443 ra
= create_requested_array(con
->request
);
6445 copy_subscription_attrs(con
, sub
, ra
);
6447 cupsArrayDelete(ra
);
6449 con
->response
->request
.status
.status_code
= IPP_OK
;
6454 * 'get_subscriptions()' - Get subscriptions.
6458 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
6459 ipp_attribute_t
*uri
) /* I - Printer/job URI */
6461 http_status_t status
; /* Policy status */
6462 int count
; /* Number of subscriptions */
6463 int limit
; /* Limit */
6464 cupsd_subscription_t
*sub
; /* Subscription */
6465 cups_array_t
*ra
; /* Requested attributes array */
6466 ipp_attribute_t
*attr
; /* Attribute */
6467 cups_ptype_t dtype
; /* Destination type (printer or class) */
6468 char scheme
[HTTP_MAX_URI
],
6469 /* Scheme portion of URI */
6470 username
[HTTP_MAX_URI
],
6471 /* Username portion of URI */
6473 /* Host portion of URI */
6474 resource
[HTTP_MAX_URI
];
6475 /* Resource portion of URI */
6476 int port
; /* Port portion of URI */
6477 cupsd_job_t
*job
; /* Job pointer */
6478 cupsd_printer_t
*printer
; /* Printer */
6481 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6482 "get_subscriptions(con=%p[%d], uri=%s)",
6483 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
6486 * Is the destination valid?
6489 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6490 sizeof(scheme
), username
, sizeof(username
), host
,
6491 sizeof(host
), &port
, resource
, sizeof(resource
));
6493 if (!strcmp(resource
, "/") ||
6494 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
6495 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
6496 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
6501 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
6504 job
= cupsdFindJob(atoi(resource
+ 6));
6508 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
6513 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6519 send_ipp_status(con
, IPP_NOT_FOUND
,
6520 _("The printer or class was not found."));
6523 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
6524 IPP_TAG_INTEGER
)) != NULL
)
6526 job
= cupsdFindJob(attr
->values
[0].integer
);
6530 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
6531 attr
->values
[0].integer
);
6542 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
6544 con
, NULL
)) != HTTP_OK
)
6546 send_http_error(con
, status
, printer
);
6551 * Copy the subscription attributes to the response using the
6552 * requested-attributes attribute that may be provided by the client.
6555 ra
= create_requested_array(con
->request
);
6557 if ((attr
= ippFindAttribute(con
->request
, "limit",
6558 IPP_TAG_INTEGER
)) != NULL
)
6559 limit
= attr
->values
[0].integer
;
6564 * See if we only want to see subscriptions for a specific user...
6567 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
6568 IPP_TAG_BOOLEAN
)) != NULL
&&
6569 attr
->values
[0].boolean
)
6570 strlcpy(username
, get_username(con
), sizeof(username
));
6574 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
6576 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
6577 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
6578 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
6580 ippAddSeparator(con
->response
);
6581 copy_subscription_attrs(con
, sub
, ra
);
6584 if (limit
&& count
>= limit
)
6588 cupsArrayDelete(ra
);
6591 con
->response
->request
.status
.status_code
= IPP_OK
;
6593 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
6598 * 'get_username()' - Get the username associated with a request.
6601 static const char * /* O - Username */
6602 get_username(cupsd_client_t
*con
) /* I - Connection */
6604 ipp_attribute_t
*attr
; /* Attribute */
6607 if (con
->username
[0])
6608 return (con
->username
);
6609 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6610 IPP_TAG_NAME
)) != NULL
)
6611 return (attr
->values
[0].string
.text
);
6613 return ("anonymous");
6618 * 'hold_job()' - Hold a print job.
6622 hold_job(cupsd_client_t
*con
, /* I - Client connection */
6623 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
6625 ipp_attribute_t
*attr
, /* Current job-hold-until */
6626 *newattr
; /* New job-hold-until */
6627 int jobid
; /* Job ID */
6628 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6629 username
[HTTP_MAX_URI
], /* Username portion of URI */
6630 host
[HTTP_MAX_URI
], /* Host portion of URI */
6631 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6632 int port
; /* Port portion of URI */
6633 cupsd_job_t
*job
; /* Job information */
6636 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
6637 uri
->values
[0].string
.text
);
6640 * See if we have a job URI or a printer URI...
6643 if (!strcmp(uri
->name
, "printer-uri"))
6646 * Got a printer URI; see if we also have a job-id attribute...
6649 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6650 IPP_TAG_INTEGER
)) == NULL
)
6652 send_ipp_status(con
, IPP_BAD_REQUEST
,
6653 _("Got a printer-uri attribute but no job-id!"));
6657 jobid
= attr
->values
[0].integer
;
6662 * Got a job URI; parse it to get the job ID...
6665 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6666 sizeof(method
), username
, sizeof(username
), host
,
6667 sizeof(host
), &port
, resource
, sizeof(resource
));
6669 if (strncmp(resource
, "/jobs/", 6))
6675 send_ipp_status(con
, IPP_BAD_REQUEST
,
6676 _("Bad job-uri attribute \"%s\"!"),
6677 uri
->values
[0].string
.text
);
6681 jobid
= atoi(resource
+ 6);
6685 * See if the job exists...
6688 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6691 * Nope - return a "not found" error...
6694 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6699 * See if the job is owned by the requesting user...
6702 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6704 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
6709 * Hold the job and return...
6714 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
6715 "Job held by user.");
6717 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
6718 IPP_TAG_KEYWORD
)) == NULL
)
6719 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
6721 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
6722 IPP_TAG_KEYWORD
)) == NULL
)
6723 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
6728 * Free the old hold value and copy the new one over...
6731 _cupsStrFree(attr
->values
[0].string
.text
);
6735 attr
->value_tag
= newattr
->value_tag
;
6736 attr
->values
[0].string
.text
=
6737 _cupsStrAlloc(newattr
->values
[0].string
.text
);
6741 attr
->value_tag
= IPP_TAG_KEYWORD
;
6742 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
6746 * Hold job until specified time...
6749 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
6751 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
6752 "Job job-hold-until value changed by user.");
6755 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was held by \"%s\".", jobid
,
6758 con
->response
->request
.status
.status_code
= IPP_OK
;
6763 * 'move_job()' - Move a job to a new destination.
6767 move_job(cupsd_client_t
*con
, /* I - Client connection */
6768 ipp_attribute_t
*uri
) /* I - Job URI */
6770 http_status_t status
; /* Policy status */
6771 ipp_attribute_t
*attr
; /* Current attribute */
6772 int jobid
; /* Job ID */
6773 cupsd_job_t
*job
; /* Current job */
6774 const char *src
; /* Source printer/class */
6775 cups_ptype_t stype
, /* Source type (printer or class) */
6776 dtype
; /* Destination type (printer or class) */
6777 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6778 username
[HTTP_MAX_URI
], /* Username portion of URI */
6779 host
[HTTP_MAX_URI
], /* Host portion of URI */
6780 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6781 int port
; /* Port portion of URI */
6782 cupsd_printer_t
*sprinter
, /* Source printer */
6783 *dprinter
; /* Destination printer */
6786 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
6787 uri
->values
[0].string
.text
);
6790 * Get the new printer or class...
6793 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
6794 IPP_TAG_URI
)) == NULL
)
6797 * Need job-printer-uri...
6800 send_ipp_status(con
, IPP_BAD_REQUEST
,
6801 _("job-printer-uri attribute missing!"));
6805 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
6811 send_ipp_status(con
, IPP_NOT_FOUND
,
6812 _("The printer or class was not found."));
6820 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
6823 send_http_error(con
, status
, dprinter
);
6828 * See if we have a job URI or a printer URI...
6831 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6832 sizeof(scheme
), username
, sizeof(username
), host
,
6833 sizeof(host
), &port
, resource
, sizeof(resource
));
6835 if (!strcmp(uri
->name
, "printer-uri"))
6838 * Got a printer URI; see if we also have a job-id attribute...
6841 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6842 IPP_TAG_INTEGER
)) == NULL
)
6848 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
6849 &sprinter
)) == NULL
)
6855 send_ipp_status(con
, IPP_NOT_FOUND
,
6856 _("The printer or class was not found."));
6865 * Otherwise, just move a single job...
6868 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
6871 * Nope - return a "not found" error...
6874 send_ipp_status(con
, IPP_NOT_FOUND
,
6875 _("Job #%d does not exist!"), attr
->values
[0].integer
);
6881 * Job found, initialize source pointers...
6892 * Got a job URI; parse it to get the job ID...
6895 if (strncmp(resource
, "/jobs/", 6))
6901 send_ipp_status(con
, IPP_BAD_REQUEST
,
6902 _("Bad job-uri attribute \"%s\"!"),
6903 uri
->values
[0].string
.text
);
6908 * See if the job exists...
6911 jobid
= atoi(resource
+ 6);
6913 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6916 * Nope - return a "not found" error...
6919 send_ipp_status(con
, IPP_NOT_FOUND
,
6920 _("Job #%d does not exist!"), jobid
);
6926 * Job found, initialize source pointers...
6935 * Now move the job or jobs...
6941 * See if the job has been completed...
6944 if (job
->state_value
> IPP_JOB_STOPPED
)
6947 * Return a "not-possible" error...
6950 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6951 _("Job #%d is finished and cannot be altered!"),
6957 * See if the job is owned by the requesting user...
6960 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6962 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
6967 * Move the job to a different printer or class...
6970 cupsdMoveJob(job
, dprinter
);
6975 * Got the source printer, now look through the jobs...
6978 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
6980 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
6983 * See if the job is pointing at the source printer or has not been
6987 if (strcasecmp(job
->dest
, src
) ||
6988 job
->state_value
> IPP_JOB_STOPPED
)
6992 * See if the job can be moved by the requesting user...
6995 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6999 * Move the job to a different printer or class...
7002 cupsdMoveJob(job
, dprinter
);
7007 * Start jobs if possible...
7013 * Return with "everything is OK" status...
7016 con
->response
->request
.status
.status_code
= IPP_OK
;
7021 * 'ppd_parse_line()' - Parse a PPD default line.
7024 static int /* O - 0 on success, -1 on failure */
7025 ppd_parse_line(const char *line
, /* I - Line */
7026 char *option
, /* O - Option name */
7027 int olen
, /* I - Size of option name */
7028 char *choice
, /* O - Choice name */
7029 int clen
) /* I - Size of choice name */
7032 * Verify this is a default option line...
7035 if (strncmp(line
, "*Default", 8))
7039 * Read the option name...
7042 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
7052 * Skip everything else up to the colon (:)...
7055 while (*line
&& *line
!= ':')
7064 * Now grab the option choice, skipping leading whitespace...
7067 while (isspace(*line
& 255))
7070 for (clen
--; isalnum(*line
& 255); line
++)
7080 * Return with no errors...
7088 * 'print_job()' - Print a file to a printer or class.
7092 print_job(cupsd_client_t
*con
, /* I - Client connection */
7093 ipp_attribute_t
*uri
) /* I - Printer URI */
7095 ipp_attribute_t
*attr
; /* Current attribute */
7096 ipp_attribute_t
*format
; /* Document-format attribute */
7097 const char *default_format
; /* document-format-default value */
7098 cupsd_job_t
*job
; /* New job */
7099 char filename
[1024]; /* Job filename */
7100 mime_type_t
*filetype
; /* Type of file */
7101 char super
[MIME_MAX_SUPER
], /* Supertype of file */
7102 type
[MIME_MAX_TYPE
], /* Subtype of file */
7103 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7104 /* Textual name of mime type */
7105 cupsd_printer_t
*printer
; /* Printer data */
7106 struct stat fileinfo
; /* File information */
7107 int kbytes
; /* Size of file */
7108 int compression
; /* Document compression */
7111 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
7112 uri
->values
[0].string
.text
);
7115 * Validate print file attributes, for now just document-format and
7116 * compression (CUPS only supports "none" and "gzip")...
7119 compression
= CUPS_FILE_NONE
;
7121 if ((attr
= ippFindAttribute(con
->request
, "compression",
7122 IPP_TAG_KEYWORD
)) != NULL
)
7124 if (strcmp(attr
->values
[0].string
.text
, "none")
7126 && strcmp(attr
->values
[0].string
.text
, "gzip")
7127 #endif /* HAVE_LIBZ */
7130 send_ipp_status(con
, IPP_ATTRIBUTES
,
7131 _("Unsupported compression \"%s\"!"),
7132 attr
->values
[0].string
.text
);
7133 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7134 "compression", NULL
, attr
->values
[0].string
.text
);
7139 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7140 compression
= CUPS_FILE_GZIP
;
7141 #endif /* HAVE_LIBZ */
7145 * Do we have a file to print?
7150 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7155 * Is the destination valid?
7158 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
7164 send_ipp_status(con
, IPP_NOT_FOUND
,
7165 _("The printer or class was not found."));
7170 * Is it a format we support?
7173 if ((format
= ippFindAttribute(con
->request
, "document-format",
7174 IPP_TAG_MIMETYPE
)) != NULL
)
7177 * Grab format from client...
7180 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
7183 send_ipp_status(con
, IPP_BAD_REQUEST
,
7184 _("Could not scan type \"%s\"!"),
7185 format
->values
[0].string
.text
);
7189 else if ((default_format
= cupsGetOption("document-format",
7190 printer
->num_options
,
7191 printer
->options
)) != NULL
)
7194 * Use default document format...
7197 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
7199 send_ipp_status(con
, IPP_BAD_REQUEST
,
7200 _("Could not scan type \"%s\"!"),
7211 strcpy(super
, "application");
7212 strcpy(type
, "octet-stream");
7215 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7218 * Auto-type the file...
7221 ipp_attribute_t
*doc_name
; /* document-name attribute */
7224 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
7226 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7227 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7228 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7232 filetype
= mimeType(MimeDatabase
, super
, type
);
7235 filetype
= mimeType(MimeDatabase
, super
, type
);
7239 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
7242 * Replace the document-format attribute value with the auto-typed or
7246 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7251 _cupsStrFree(format
->values
[0].string
.text
);
7253 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7256 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7257 "document-format", NULL
, mimetype
);
7261 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7262 _("Unsupported format \'%s/%s\'!"), super
, type
);
7263 cupsdLogMessage(CUPSD_LOG_INFO
,
7264 "Hint: Do you have the raw file printing rules enabled?");
7267 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7268 "document-format", NULL
, format
->values
[0].string
.text
);
7273 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: request file type is %s/%s.",
7274 filetype
->super
, filetype
->type
);
7277 * Read any embedded job ticket info from PS files...
7280 if (!strcasecmp(filetype
->super
, "application") &&
7281 !strcasecmp(filetype
->type
, "postscript"))
7282 read_ps_job_ticket(con
);
7285 * Create the job object...
7288 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
7292 * Update quota data...
7295 if (stat(con
->filename
, &fileinfo
))
7298 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7300 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7302 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7303 IPP_TAG_INTEGER
)) != NULL
)
7304 attr
->values
[0].integer
+= kbytes
;
7307 * Add the job file...
7310 if (add_file(con
, job
, filetype
, compression
))
7313 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
7315 rename(con
->filename
, filename
);
7316 cupsdClearString(&con
->filename
);
7319 * See if we need to add the ending sheet...
7322 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
7324 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
7325 attr
&& attr
->num_values
> 1)
7331 cupsdLogMessage(CUPSD_LOG_INFO
, "Adding end banner page \"%s\" to job %d.",
7332 attr
->values
[1].string
.text
, job
->id
);
7334 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
7336 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7340 * Log and save the job...
7343 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d queued on \"%s\" by \"%s\".", job
->id
,
7344 job
->dest
, job
->username
);
7345 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Job %d hold_until = %d", job
->id
,
7346 (int)job
->hold_until
);
7351 * Start the job if possible...
7359 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
7361 * This function only gets called when printing a single PostScript
7362 * file using the Print-Job operation. It doesn't work for Create-Job +
7363 * Send-File, since the job attributes need to be set at job creation
7364 * time for banners to work. The embedded PS job ticket stuff is here
7365 * only to allow the Windows printer driver for CUPS to pass in JCL
7366 * options and IPP attributes which otherwise would be lost.
7368 * The format of a PS job ticket is simple:
7370 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
7372 * %cupsJobTicket: attr1=value1
7373 * %cupsJobTicket: attr2=value2
7375 * %cupsJobTicket: attrN=valueN
7377 * Job ticket lines must appear immediately after the first line that
7378 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
7379 * looking for job ticket info when it finds a line that does not begin
7380 * with "%cupsJobTicket:".
7382 * The maximum length of a job ticket line, including the prefix, is
7383 * 255 characters to conform with the Adobe DSC.
7385 * Read-only attributes are rejected with a notice to the error log in
7386 * case a malicious user tries anything. Since the job ticket is read
7387 * prior to attribute validation in print_job(), job ticket attributes
7388 * will go through the same validation as IPP attributes...
7392 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
7394 cups_file_t
*fp
; /* File to read from */
7395 char line
[256]; /* Line data */
7396 int num_options
; /* Number of options */
7397 cups_option_t
*options
; /* Options */
7398 ipp_t
*ticket
; /* New attributes */
7399 ipp_attribute_t
*attr
, /* Current attribute */
7400 *attr2
, /* Job attribute */
7401 *prev2
; /* Previous job attribute */
7405 * First open the print file...
7408 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
7410 cupsdLogMessage(CUPSD_LOG_ERROR
,
7411 "read_ps_job_ticket: Unable to open PostScript print file "
7418 * Skip the first line...
7421 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
7423 cupsdLogMessage(CUPSD_LOG_ERROR
,
7424 "read_ps_job_ticket: Unable to read from PostScript print "
7431 if (strncmp(line
, "%!PS-Adobe-", 11))
7434 * Not a DSC-compliant file, so no job ticket info will be available...
7442 * Read job ticket info from the file...
7448 while (cupsFileGets(fp
, line
, sizeof(line
)))
7451 * Stop at the first non-ticket line...
7454 if (strncmp(line
, "%cupsJobTicket:", 15))
7458 * Add the options to the option array...
7461 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
7465 * Done with the file; see if we have any options...
7470 if (num_options
== 0)
7474 * OK, convert the options to an attribute list, and apply them to
7479 cupsEncodeOptions(ticket
, num_options
, options
);
7482 * See what the user wants to change.
7485 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
7487 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
7490 if (!strcmp(attr
->name
, "job-originating-host-name") ||
7491 !strcmp(attr
->name
, "job-originating-user-name") ||
7492 !strcmp(attr
->name
, "job-media-sheets-completed") ||
7493 !strcmp(attr
->name
, "job-k-octets") ||
7494 !strcmp(attr
->name
, "job-id") ||
7495 !strncmp(attr
->name
, "job-state", 9) ||
7496 !strncmp(attr
->name
, "time-at-", 8))
7497 continue; /* Read-only attrs */
7499 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
7500 IPP_TAG_ZERO
)) != NULL
)
7503 * Some other value; first free the old value...
7506 if (con
->request
->attrs
== attr2
)
7508 con
->request
->attrs
= attr2
->next
;
7513 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
7514 if (prev2
->next
== attr2
)
7516 prev2
->next
= attr2
->next
;
7521 if (con
->request
->last
== attr2
)
7522 con
->request
->last
= prev2
;
7524 _ippFreeAttr(attr2
);
7528 * Add new option by copying it...
7531 copy_attribute(con
->request
, attr
, 0);
7535 * Then free the attribute list and option array...
7539 cupsFreeOptions(num_options
, options
);
7544 * 'reject_jobs()' - Reject print jobs to a printer.
7548 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
7549 ipp_attribute_t
*uri
) /* I - Printer or class URI */
7551 http_status_t status
; /* Policy status */
7552 cups_ptype_t dtype
; /* Destination type (printer or class) */
7553 cupsd_printer_t
*printer
; /* Printer data */
7554 ipp_attribute_t
*attr
; /* printer-state-message text */
7557 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
7558 con
->http
.fd
, uri
->values
[0].string
.text
);
7561 * Is the destination valid?
7564 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7570 send_ipp_status(con
, IPP_NOT_FOUND
,
7571 _("The printer or class was not found."));
7579 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7581 send_http_error(con
, status
, printer
);
7586 * Reject jobs sent to the printer...
7589 printer
->accepting
= 0;
7591 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
7592 IPP_TAG_TEXT
)) == NULL
)
7593 strcpy(printer
->state_message
, "Rejecting Jobs");
7595 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
7596 sizeof(printer
->state_message
));
7598 cupsdAddPrinterHistory(printer
);
7600 if (dtype
& CUPS_PRINTER_CLASS
)
7602 cupsdSaveAllClasses();
7604 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
7605 printer
->name
, get_username(con
));
7609 cupsdSaveAllPrinters();
7611 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
7612 printer
->name
, get_username(con
));
7616 * Everything was ok, so return OK status...
7619 con
->response
->request
.status
.status_code
= IPP_OK
;
7624 * 'release_job()' - Release a held print job.
7628 release_job(cupsd_client_t
*con
, /* I - Client connection */
7629 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7631 ipp_attribute_t
*attr
; /* Current attribute */
7632 int jobid
; /* Job ID */
7633 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7634 username
[HTTP_MAX_URI
], /* Username portion of URI */
7635 host
[HTTP_MAX_URI
], /* Host portion of URI */
7636 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7637 int port
; /* Port portion of URI */
7638 cupsd_job_t
*job
; /* Job information */
7641 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
7642 con
->http
.fd
, uri
->values
[0].string
.text
);
7645 * See if we have a job URI or a printer URI...
7648 if (!strcmp(uri
->name
, "printer-uri"))
7651 * Got a printer URI; see if we also have a job-id attribute...
7654 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7655 IPP_TAG_INTEGER
)) == NULL
)
7657 send_ipp_status(con
, IPP_BAD_REQUEST
,
7658 _("Got a printer-uri attribute but no job-id!"));
7662 jobid
= attr
->values
[0].integer
;
7667 * Got a job URI; parse it to get the job ID...
7670 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7671 sizeof(method
), username
, sizeof(username
), host
,
7672 sizeof(host
), &port
, resource
, sizeof(resource
));
7674 if (strncmp(resource
, "/jobs/", 6))
7680 send_ipp_status(con
, IPP_BAD_REQUEST
,
7681 _("Bad job-uri attribute \"%s\"!"),
7682 uri
->values
[0].string
.text
);
7686 jobid
= atoi(resource
+ 6);
7690 * See if the job exists...
7693 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7696 * Nope - return a "not found" error...
7699 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7704 * See if job is "held"...
7707 if (job
->state_value
!= IPP_JOB_HELD
)
7710 * Nope - return a "not possible" error...
7713 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
7718 * See if the job is owned by the requesting user...
7721 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7723 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
7728 * Reset the job-hold-until value to "no-hold"...
7731 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7732 IPP_TAG_KEYWORD
)) == NULL
)
7733 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7737 _cupsStrFree(attr
->values
[0].string
.text
);
7739 attr
->value_tag
= IPP_TAG_KEYWORD
;
7740 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
7742 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
7743 "Job job-hold-until value changed by user.");
7747 * Release the job and return...
7750 cupsdReleaseJob(job
);
7752 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
7753 "Job released by user.");
7755 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was released by \"%s\".", jobid
,
7758 con
->response
->request
.status
.status_code
= IPP_OK
;
7763 * 'renew_subscription()' - Renew an existing subscription...
7768 cupsd_client_t
*con
, /* I - Client connection */
7769 int sub_id
) /* I - Subscription ID */
7771 http_status_t status
; /* Policy status */
7772 cupsd_subscription_t
*sub
; /* Subscription */
7773 ipp_attribute_t
*lease
; /* notify-lease-duration */
7776 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7777 "renew_subscription(con=%p[%d], sub_id=%d)",
7778 con
, con
->http
.fd
, sub_id
);
7781 * Is the subscription ID valid?
7784 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7787 * Bad subscription ID...
7790 send_ipp_status(con
, IPP_NOT_FOUND
,
7791 _("notify-subscription-id %d no good!"), sub_id
);
7798 * Job subscriptions cannot be renewed...
7801 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7802 _("Job subscriptions cannot be renewed!"));
7810 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7812 con
, sub
->owner
)) != HTTP_OK
)
7814 send_http_error(con
, status
, sub
->dest
);
7819 * Renew the subscription...
7822 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
7825 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
7827 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
7829 cupsdLogMessage(CUPSD_LOG_INFO
,
7830 "renew_subscription: Limiting notify-lease-duration to "
7833 sub
->lease
= MaxLeaseDuration
;
7836 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
7838 cupsdSaveAllSubscriptions();
7840 con
->response
->request
.status
.status_code
= IPP_OK
;
7842 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
7843 "notify-lease-duration", sub
->lease
);
7848 * 'restart_job()' - Restart an old print job.
7852 restart_job(cupsd_client_t
*con
, /* I - Client connection */
7853 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7855 ipp_attribute_t
*attr
; /* Current attribute */
7856 int jobid
; /* Job ID */
7857 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7858 username
[HTTP_MAX_URI
], /* Username portion of URI */
7859 host
[HTTP_MAX_URI
], /* Host portion of URI */
7860 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7861 int port
; /* Port portion of URI */
7862 cupsd_job_t
*job
; /* Job information */
7865 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
7866 con
->http
.fd
, uri
->values
[0].string
.text
);
7869 * See if we have a job URI or a printer URI...
7872 if (!strcmp(uri
->name
, "printer-uri"))
7875 * Got a printer URI; see if we also have a job-id attribute...
7878 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7879 IPP_TAG_INTEGER
)) == NULL
)
7881 send_ipp_status(con
, IPP_BAD_REQUEST
,
7882 _("Got a printer-uri attribute but no job-id!"));
7886 jobid
= attr
->values
[0].integer
;
7891 * Got a job URI; parse it to get the job ID...
7894 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7895 sizeof(method
), username
, sizeof(username
), host
,
7896 sizeof(host
), &port
, resource
, sizeof(resource
));
7898 if (strncmp(resource
, "/jobs/", 6))
7904 send_ipp_status(con
, IPP_BAD_REQUEST
,
7905 _("Bad job-uri attribute \"%s\"!"),
7906 uri
->values
[0].string
.text
);
7910 jobid
= atoi(resource
+ 6);
7914 * See if the job exists...
7917 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7920 * Nope - return a "not found" error...
7923 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7928 * See if job is in any of the "completed" states...
7931 if (job
->state_value
<= IPP_JOB_PROCESSING
)
7934 * Nope - return a "not possible" error...
7937 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
7943 * See if we have retained the job files...
7948 if (!job
->attrs
|| job
->num_files
== 0)
7951 * Nope - return a "not possible" error...
7954 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7955 _("Job #%d cannot be restarted - no files!"), jobid
);
7960 * See if the job is owned by the requesting user...
7963 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7965 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
7970 * Restart the job and return...
7973 cupsdRestartJob(job
);
7975 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was restarted by \"%s\".", jobid
,
7978 con
->response
->request
.status
.status_code
= IPP_OK
;
7983 * 'save_auth_info()' - Save authentication information for a job.
7988 cupsd_client_t
*con
, /* I - Client connection */
7989 cupsd_job_t
*job
, /* I - Job */
7990 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
7992 int i
; /* Looping var */
7993 char filename
[1024]; /* Job authentication filename */
7994 cups_file_t
*fp
; /* Job authentication file */
7995 char line
[2048]; /* Line for file */
7999 * This function saves the in-memory authentication information for
8000 * a job so that it can be used to authenticate with a remote host.
8001 * The information is stored in a file that is readable only by the
8002 * root user. The fields are Base-64 encoded, each on a separate line,
8003 * followed by random number (up to 1024) of newlines to limit the
8004 * amount of information that is exposed.
8006 * Because of the potential for exposing of authentication information,
8007 * this functionality is only enabled when running cupsd as root.
8009 * This caching only works for the Basic and BasicDigest authentication
8010 * types. Digest authentication cannot be cached this way, and in
8011 * the future Kerberos authentication may make all of this obsolete.
8013 * Authentication information is saved whenever an authenticated
8014 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
8017 * This information is deleted after a job is completed or canceled,
8018 * so reprints may require subsequent re-authentication.
8025 * Create the authentication file and change permissions...
8028 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
8029 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
8031 cupsdLogMessage(CUPSD_LOG_ERROR
,
8032 "Unable to save authentication info to \"%s\" - %s",
8033 filename
, strerror(errno
));
8037 fchown(cupsFileNumber(fp
), 0, 0);
8038 fchmod(cupsFileNumber(fp
), 0400);
8043 * Write 1 to 4 auth values...
8046 for (i
= 0; i
< auth_info
->num_values
; i
++)
8048 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
8049 strlen(auth_info
->values
[i
].string
.text
));
8050 cupsFilePrintf(fp
, "%s\n", line
);
8056 * Write the authenticated username...
8059 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
8060 cupsFilePrintf(fp
, "%s\n", line
);
8063 * Write the authenticated password...
8066 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
8067 cupsFilePrintf(fp
, "%s\n", line
);
8071 * Write a random number of newlines to the end of the file...
8074 for (i
= (rand() % 1024); i
>= 0; i
--)
8075 cupsFilePutChar(fp
, '\n');
8078 * Close the file and return...
8083 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8084 save_krb5_creds(con
, job
);
8085 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8089 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8091 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
8095 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
8096 cupsd_job_t
*job
) /* I - Job */
8098 krb5_context krb_context
; /* Kerberos context */
8099 krb5_ccache ccache
; /* Credentials cache */
8100 OM_uint32 major_status
, /* Major status code */
8101 minor_status
; /* Minor status code */
8106 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
8110 if (krb5_init_context
== NULL
)
8112 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8113 "save_krb5_creds: GSSAPI/Kerberos framework is not "
8117 # endif /* __APPLE__ */
8120 * Setup a cached context for the job filters to use...
8123 if (krb5_init_context(&krb_context
))
8125 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize Kerberos context");
8129 # ifdef HAVE_HEIMDAL
8130 if (krb5_cc_gen_new(krb_context
, &krb5_fcc_ops
, &ccache
))
8132 if (krb5_cc_gen_new(krb_context
, &ccache
))
8133 # endif /* HAVE_HEIMDAL */
8135 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create new credentials");
8139 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
8142 if (GSS_ERROR(major_status
))
8144 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
8145 "Unable to import client credentials cache");
8146 krb5_cc_destroy(krb_context
, ccache
);
8150 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
8151 krb5_cc_get_name(krb_context
, ccache
));
8152 krb5_cc_close(krb_context
, ccache
);
8154 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8158 * 'send_document()' - Send a file to a printer or class.
8162 send_document(cupsd_client_t
*con
, /* I - Client connection */
8163 ipp_attribute_t
*uri
) /* I - Printer URI */
8165 ipp_attribute_t
*attr
; /* Current attribute */
8166 ipp_attribute_t
*format
; /* Document-format attribute */
8167 const char *default_format
;/* document-format-default value */
8168 int jobid
; /* Job ID number */
8169 cupsd_job_t
*job
; /* Current job */
8170 char job_uri
[HTTP_MAX_URI
],
8172 method
[HTTP_MAX_URI
],
8173 /* Method portion of URI */
8174 username
[HTTP_MAX_URI
],
8175 /* Username portion of URI */
8177 /* Host portion of URI */
8178 resource
[HTTP_MAX_URI
];
8179 /* Resource portion of URI */
8180 int port
; /* Port portion of URI */
8181 mime_type_t
*filetype
; /* Type of file */
8182 char super
[MIME_MAX_SUPER
],
8183 /* Supertype of file */
8184 type
[MIME_MAX_TYPE
],
8185 /* Subtype of file */
8186 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8187 /* Textual name of mime type */
8188 char filename
[1024]; /* Job filename */
8189 cupsd_printer_t
*printer
; /* Current printer */
8190 struct stat fileinfo
; /* File information */
8191 int kbytes
; /* Size of file */
8192 int compression
; /* Type of compression */
8195 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
8196 con
->http
.fd
, uri
->values
[0].string
.text
);
8199 * See if we have a job URI or a printer URI...
8202 if (!strcmp(uri
->name
, "printer-uri"))
8205 * Got a printer URI; see if we also have a job-id attribute...
8208 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8209 IPP_TAG_INTEGER
)) == NULL
)
8211 send_ipp_status(con
, IPP_BAD_REQUEST
,
8212 _("Got a printer-uri attribute but no job-id!"));
8216 jobid
= attr
->values
[0].integer
;
8221 * Got a job URI; parse it to get the job ID...
8224 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8225 sizeof(method
), username
, sizeof(username
), host
,
8226 sizeof(host
), &port
, resource
, sizeof(resource
));
8228 if (strncmp(resource
, "/jobs/", 6))
8234 send_ipp_status(con
, IPP_BAD_REQUEST
,
8235 _("Bad job-uri attribute \"%s\"!"),
8236 uri
->values
[0].string
.text
);
8240 jobid
= atoi(resource
+ 6);
8244 * See if the job exists...
8247 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8250 * Nope - return a "not found" error...
8253 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8257 printer
= cupsdFindDest(job
->dest
);
8260 * See if the job is owned by the requesting user...
8263 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8265 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
8270 * OK, see if the client is sending the document compressed - CUPS
8271 * only supports "none" and "gzip".
8274 compression
= CUPS_FILE_NONE
;
8276 if ((attr
= ippFindAttribute(con
->request
, "compression",
8277 IPP_TAG_KEYWORD
)) != NULL
)
8279 if (strcmp(attr
->values
[0].string
.text
, "none")
8281 && strcmp(attr
->values
[0].string
.text
, "gzip")
8282 #endif /* HAVE_LIBZ */
8285 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
8286 attr
->values
[0].string
.text
);
8287 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8288 "compression", NULL
, attr
->values
[0].string
.text
);
8293 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8294 compression
= CUPS_FILE_GZIP
;
8295 #endif /* HAVE_LIBZ */
8299 * Do we have a file to print?
8304 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
8309 * Is it a format we support?
8312 if ((format
= ippFindAttribute(con
->request
, "document-format",
8313 IPP_TAG_MIMETYPE
)) != NULL
)
8316 * Grab format from client...
8319 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
8321 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
8322 format
->values
[0].string
.text
);
8326 else if ((default_format
= cupsGetOption("document-format",
8327 printer
->num_options
,
8328 printer
->options
)) != NULL
)
8331 * Use default document format...
8334 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
8336 send_ipp_status(con
, IPP_BAD_REQUEST
,
8337 _("Could not scan type \"%s\"!"),
8345 * No document format attribute? Auto-type it!
8348 strcpy(super
, "application");
8349 strcpy(type
, "octet-stream");
8352 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8355 * Auto-type the file...
8358 ipp_attribute_t
*doc_name
; /* document-name attribute */
8361 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
8363 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8364 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8365 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8369 filetype
= mimeType(MimeDatabase
, super
, type
);
8372 filetype
= mimeType(MimeDatabase
, super
, type
);
8376 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8379 * Replace the document-format attribute value with the auto-typed or
8383 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8388 _cupsStrFree(format
->values
[0].string
.text
);
8390 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8393 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8394 "document-format", NULL
, mimetype
);
8398 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8399 _("Unsupported format \'%s/%s\'!"), super
, type
);
8400 cupsdLogMessage(CUPSD_LOG_INFO
,
8401 "Hint: Do you have the raw file printing rules enabled?");
8404 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8405 "document-format", NULL
, format
->values
[0].string
.text
);
8410 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
8412 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8415 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8416 _("Unsupported format \'%s\'!"), mimetype
);
8418 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8419 "document-format", NULL
, mimetype
);
8424 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8425 "send_document: request file type is %s/%s.",
8426 filetype
->super
, filetype
->type
);
8429 * Add the file to the job...
8434 if (add_file(con
, job
, filetype
, compression
))
8437 if (stat(con
->filename
, &fileinfo
))
8440 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8442 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8444 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8445 IPP_TAG_INTEGER
)) != NULL
)
8446 attr
->values
[0].integer
+= kbytes
;
8448 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8450 rename(con
->filename
, filename
);
8452 cupsdClearString(&con
->filename
);
8454 cupsdLogMessage(CUPSD_LOG_INFO
,
8455 "File of type %s/%s queued in job #%d by \"%s\".",
8456 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
8459 * Start the job if this is the last document...
8462 if ((attr
= ippFindAttribute(con
->request
, "last-document",
8463 IPP_TAG_BOOLEAN
)) != NULL
&&
8464 attr
->values
[0].boolean
)
8467 * See if we need to add the ending sheet...
8471 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
8472 (attr
= ippFindAttribute(job
->attrs
, "job-sheets",
8473 IPP_TAG_ZERO
)) != NULL
&&
8474 attr
->num_values
> 1)
8480 cupsdLogMessage(CUPSD_LOG_INFO
,
8481 "Adding end banner page \"%s\" to job %d.",
8482 attr
->values
[1].string
.text
, job
->id
);
8484 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
8486 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8489 if (job
->state_value
== IPP_JOB_STOPPED
)
8491 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
8492 job
->state_value
= IPP_JOB_PENDING
;
8494 else if (job
->state_value
== IPP_JOB_HELD
)
8496 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8497 IPP_TAG_KEYWORD
)) == NULL
)
8498 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8500 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
8502 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
8503 job
->state_value
= IPP_JOB_PENDING
;
8510 * Start the job if possible... Since cupsdCheckJobs() can cancel a
8511 * job if it doesn't print, we need to re-find the job afterwards...
8518 job
= cupsdFindJob(jobid
);
8522 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8523 IPP_TAG_KEYWORD
)) == NULL
)
8524 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8526 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
8528 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
8529 job
->state_value
= IPP_JOB_HELD
;
8530 job
->hold_until
= time(NULL
) + 60;
8536 * Fill in the response info...
8539 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
8542 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
8545 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
8547 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
8548 job
? job
->state_value
: IPP_JOB_CANCELED
);
8549 add_job_state_reasons(con
, job
);
8551 con
->response
->request
.status
.status_code
= IPP_OK
;
8556 * 'send_http_error()' - Send a HTTP error back to the IPP client.
8561 cupsd_client_t
*con
, /* I - Client connection */
8562 http_status_t status
, /* I - HTTP status code */
8563 cupsd_printer_t
*printer
) /* I - Printer, if any */
8565 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
8566 ippOpString(con
->request
->request
.op
.operation_id
),
8567 httpStatus(status
));
8569 if (status
== HTTP_UNAUTHORIZED
&&
8570 printer
&& printer
->num_auth_info_required
> 0 &&
8571 !strcmp(printer
->auth_info_required
[0], "negotiate"))
8572 cupsdSendError(con
, status
, AUTH_NEGOTIATE
);
8574 cupsdSendError(con
, status
, AUTH_NONE
);
8576 ippDelete(con
->response
);
8577 con
->response
= NULL
;
8584 * 'send_ipp_status()' - Send a status back to the IPP client.
8588 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
8589 ipp_status_t status
, /* I - IPP status code */
8590 const char *message
, /* I - Status message */
8591 ...) /* I - Additional args as needed */
8593 va_list ap
; /* Pointer to additional args */
8594 char formatted
[1024]; /* Formatted errror message */
8599 va_start(ap
, message
);
8600 vsnprintf(formatted
, sizeof(formatted
),
8601 _cupsLangString(con
->language
, message
), ap
);
8604 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
8605 ippOpString(con
->request
->request
.op
.operation_id
),
8606 ippErrorString(status
), formatted
);
8609 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
8610 ippOpString(con
->request
->request
.op
.operation_id
),
8611 ippErrorString(status
));
8613 con
->response
->request
.status
.status_code
= status
;
8615 if (ippFindAttribute(con
->response
, "attributes-charset",
8616 IPP_TAG_ZERO
) == NULL
)
8617 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
8618 "attributes-charset", NULL
, DefaultCharset
);
8620 if (ippFindAttribute(con
->response
, "attributes-natural-language",
8621 IPP_TAG_ZERO
) == NULL
)
8622 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
8623 "attributes-natural-language", NULL
, DefaultLanguage
);
8626 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
8627 "status-message", NULL
, formatted
);
8632 * 'set_default()' - Set the default destination...
8636 set_default(cupsd_client_t
*con
, /* I - Client connection */
8637 ipp_attribute_t
*uri
) /* I - Printer URI */
8639 http_status_t status
; /* Policy status */
8640 cups_ptype_t dtype
; /* Destination type (printer or class) */
8641 cupsd_printer_t
*printer
; /* Printer */
8644 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
8645 con
->http
.fd
, uri
->values
[0].string
.text
);
8648 * Is the destination valid?
8651 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8657 send_ipp_status(con
, IPP_NOT_FOUND
,
8658 _("The printer or class was not found."));
8666 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
8668 send_http_error(con
, status
, NULL
);
8673 * Set it as the default...
8676 DefaultPrinter
= printer
;
8678 cupsdSaveAllPrinters();
8679 cupsdSaveAllClasses();
8681 cupsdWritePrintcap();
8683 cupsdLogMessage(CUPSD_LOG_INFO
,
8684 "Default destination set to \"%s\" by \"%s\".",
8685 printer
->name
, get_username(con
));
8688 * Everything was ok, so return OK status...
8691 con
->response
->request
.status
.status_code
= IPP_OK
;
8696 * 'set_job_attrs()' - Set job attributes.
8700 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
8701 ipp_attribute_t
*uri
) /* I - Job URI */
8703 ipp_attribute_t
*attr
, /* Current attribute */
8704 *attr2
; /* Job attribute */
8705 int jobid
; /* Job ID */
8706 cupsd_job_t
*job
; /* Current job */
8707 char method
[HTTP_MAX_URI
],
8708 /* Method portion of URI */
8709 username
[HTTP_MAX_URI
],
8710 /* Username portion of URI */
8712 /* Host portion of URI */
8713 resource
[HTTP_MAX_URI
];
8714 /* Resource portion of URI */
8715 int port
; /* Port portion of URI */
8716 int event
; /* Events? */
8719 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
8720 con
->http
.fd
, uri
->values
[0].string
.text
);
8723 * Start with "everything is OK" status...
8726 con
->response
->request
.status
.status_code
= IPP_OK
;
8729 * See if we have a job URI or a printer URI...
8732 if (!strcmp(uri
->name
, "printer-uri"))
8735 * Got a printer URI; see if we also have a job-id attribute...
8738 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8739 IPP_TAG_INTEGER
)) == NULL
)
8741 send_ipp_status(con
, IPP_BAD_REQUEST
,
8742 _("Got a printer-uri attribute but no job-id!"));
8746 jobid
= attr
->values
[0].integer
;
8751 * Got a job URI; parse it to get the job ID...
8754 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8755 sizeof(method
), username
, sizeof(username
), host
,
8756 sizeof(host
), &port
, resource
, sizeof(resource
));
8758 if (strncmp(resource
, "/jobs/", 6))
8764 send_ipp_status(con
, IPP_BAD_REQUEST
,
8765 _("Bad job-uri attribute \"%s\"!"),
8766 uri
->values
[0].string
.text
);
8770 jobid
= atoi(resource
+ 6);
8774 * See if the job exists...
8777 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8780 * Nope - return a "not found" error...
8783 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8788 * See if the job has been completed...
8791 if (job
->state_value
> IPP_JOB_STOPPED
)
8794 * Return a "not-possible" error...
8797 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8798 _("Job #%d is finished and cannot be altered!"), jobid
);
8803 * See if the job is owned by the requesting user...
8806 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8808 send_http_error(con
, HTTP_UNAUTHORIZED
, NULL
);
8813 * See what the user wants to change.
8820 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8822 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8825 if (!strcmp(attr
->name
, "attributes-charset") ||
8826 !strcmp(attr
->name
, "attributes-natural-language") ||
8827 !strcmp(attr
->name
, "document-compression") ||
8828 !strcmp(attr
->name
, "document-format") ||
8829 !strcmp(attr
->name
, "job-detailed-status-messages") ||
8830 !strcmp(attr
->name
, "job-document-access-errors") ||
8831 !strcmp(attr
->name
, "job-id") ||
8832 !strcmp(attr
->name
, "job-k-octets") ||
8833 !strcmp(attr
->name
, "job-originating-host-name") ||
8834 !strcmp(attr
->name
, "job-originating-user-name") ||
8835 !strcmp(attr
->name
, "job-printer-up-time") ||
8836 !strcmp(attr
->name
, "job-printer-uri") ||
8837 !strcmp(attr
->name
, "job-sheets") ||
8838 !strcmp(attr
->name
, "job-state-message") ||
8839 !strcmp(attr
->name
, "job-state-reasons") ||
8840 !strcmp(attr
->name
, "job-uri") ||
8841 !strcmp(attr
->name
, "number-of-documents") ||
8842 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
8843 !strcmp(attr
->name
, "output-device-assigned") ||
8844 !strncmp(attr
->name
, "date-time-at-", 13) ||
8845 !strncmp(attr
->name
, "job-impressions", 15) ||
8846 !strncmp(attr
->name
, "job-k-octets", 12) ||
8847 !strncmp(attr
->name
, "job-media-sheets", 16) ||
8848 !strncmp(attr
->name
, "time-at-", 8))
8854 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
8855 _("%s cannot be changed."), attr
->name
);
8857 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8858 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8863 if (!strcmp(attr
->name
, "job-priority"))
8866 * Change the job priority...
8869 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8871 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
8873 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8874 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8876 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
8878 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8879 _("Job is completed and cannot be changed."));
8882 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8884 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
8885 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
8888 else if (!strcmp(attr
->name
, "job-state"))
8891 * Change the job state...
8894 if (attr
->value_tag
!= IPP_TAG_ENUM
)
8896 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
8898 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8899 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8903 switch (attr
->values
[0].integer
)
8905 case IPP_JOB_PENDING
:
8907 if (job
->state_value
> IPP_JOB_HELD
)
8909 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8910 _("Job state cannot be changed."));
8913 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8915 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8916 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
8918 event
|= CUPSD_EVENT_JOB_STATE
;
8922 case IPP_JOB_PROCESSING
:
8923 case IPP_JOB_STOPPED
:
8924 if (job
->state_value
!= attr
->values
[0].integer
)
8926 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8927 _("Job state cannot be changed."));
8932 case IPP_JOB_CANCELED
:
8933 case IPP_JOB_ABORTED
:
8934 case IPP_JOB_COMPLETED
:
8935 if (job
->state_value
> IPP_JOB_PROCESSING
)
8937 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8938 _("Job state cannot be changed."));
8941 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8942 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
8947 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
8949 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8950 IPP_TAG_ZERO
)) != NULL
)
8953 * Some other value; first free the old value...
8956 if (job
->attrs
->prev
)
8957 job
->attrs
->prev
->next
= attr2
->next
;
8959 job
->attrs
->attrs
= attr2
->next
;
8961 if (job
->attrs
->last
== attr2
)
8962 job
->attrs
->last
= job
->attrs
->prev
;
8964 _ippFreeAttr(attr2
);
8967 * Then copy the attribute...
8970 copy_attribute(job
->attrs
, attr
, 0);
8973 * See if the job-name or job-hold-until is being changed.
8976 if (!strcmp(attr
->name
, "job-hold-until"))
8978 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
8980 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
8981 cupsdReleaseJob(job
);
8985 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
8988 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
8991 * Delete the attribute...
8994 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8995 IPP_TAG_ZERO
)) != NULL
)
8997 if (job
->attrs
->prev
)
8998 job
->attrs
->prev
->next
= attr2
->next
;
9000 job
->attrs
->attrs
= attr2
->next
;
9002 if (attr2
== job
->attrs
->last
)
9003 job
->attrs
->last
= job
->attrs
->prev
;
9005 _ippFreeAttr(attr2
);
9007 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9013 * Add new option by copying it...
9016 copy_attribute(job
->attrs
, attr
, 0);
9018 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9029 * Send events as needed...
9032 if (event
& CUPSD_EVENT_JOB_STATE
)
9033 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
9034 job
->state_value
== IPP_JOB_HELD
?
9035 "Job held by user." : "Job restarted by user.");
9037 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
9038 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
9039 "Job options changed by user.");
9042 * Start jobs if possible...
9050 * 'set_printer_defaults()' - Set printer default options from a request.
9054 set_printer_defaults(
9055 cupsd_client_t
*con
, /* I - Client connection */
9056 cupsd_printer_t
*printer
) /* I - Printer */
9058 int i
; /* Looping var */
9059 ipp_attribute_t
*attr
; /* Current attribute */
9060 int namelen
; /* Length of attribute name */
9061 char name
[256], /* New attribute name */
9062 value
[256]; /* String version of integer attrs */
9065 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9068 * Skip non-printer attributes...
9071 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
9074 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
9076 if (!strcmp(attr
->name
, "job-sheets-default"))
9079 * Only allow keywords and names...
9082 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9086 * Only allow job-sheets-default to be set when running without a
9087 * system high classification level...
9093 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
9095 if (attr
->num_values
> 1)
9096 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
9098 cupsdSetString(&printer
->job_sheets
[1], "none");
9100 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
9102 cupsdFreePrinterUsers(printer
);
9104 printer
->deny_users
= 0;
9106 if (attr
->value_tag
== IPP_TAG_NAME
&&
9107 (attr
->num_values
> 1 ||
9108 strcmp(attr
->values
[0].string
.text
, "all")))
9110 for (i
= 0; i
< attr
->num_values
; i
++)
9111 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9114 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
9116 cupsdFreePrinterUsers(printer
);
9118 printer
->deny_users
= 1;
9120 if (attr
->value_tag
== IPP_TAG_NAME
&&
9121 (attr
->num_values
> 1 ||
9122 strcmp(attr
->values
[0].string
.text
, "none")))
9124 for (i
= 0; i
< attr
->num_values
; i
++)
9125 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9128 else if (!strcmp(attr
->name
, "job-quota-period"))
9130 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9133 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
9134 attr
->values
[0].integer
);
9135 cupsdFreeQuotas(printer
);
9137 printer
->quota_period
= attr
->values
[0].integer
;
9139 else if (!strcmp(attr
->name
, "job-k-limit"))
9141 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9144 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
9145 attr
->values
[0].integer
);
9146 cupsdFreeQuotas(printer
);
9148 printer
->k_limit
= attr
->values
[0].integer
;
9150 else if (!strcmp(attr
->name
, "job-page-limit"))
9152 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9155 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
9156 attr
->values
[0].integer
);
9157 cupsdFreeQuotas(printer
);
9159 printer
->page_limit
= attr
->values
[0].integer
;
9161 else if (!strcmp(attr
->name
, "printer-op-policy"))
9163 cupsd_policy_t
*p
; /* Policy */
9166 if (attr
->value_tag
!= IPP_TAG_NAME
)
9169 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
9171 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9172 "Setting printer-op-policy to \"%s\"...",
9173 attr
->values
[0].string
.text
);
9174 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
9175 printer
->op_policy_ptr
= p
;
9179 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9180 _("Unknown printer-op-policy \"%s\"."),
9181 attr
->values
[0].string
.text
);
9185 else if (!strcmp(attr
->name
, "printer-error-policy"))
9187 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9190 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
9191 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
9192 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
9194 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9195 _("Unknown printer-error-policy \"%s\"."),
9196 attr
->values
[0].string
.text
);
9200 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9201 "Setting printer-error-policy to \"%s\"...",
9202 attr
->values
[0].string
.text
);
9203 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
9207 * Skip any other non-default attributes...
9210 namelen
= strlen(attr
->name
);
9211 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
9212 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
9216 * OK, anything else must be a user-defined default...
9219 strlcpy(name
, attr
->name
, sizeof(name
));
9220 name
[namelen
- 8] = '\0'; /* Strip "-default" */
9222 switch (attr
->value_tag
)
9224 case IPP_TAG_DELETEATTR
:
9225 printer
->num_options
= cupsRemoveOption(name
,
9226 printer
->num_options
,
9227 &(printer
->options
));
9228 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9229 "Deleting %s", attr
->name
);
9233 case IPP_TAG_KEYWORD
:
9235 printer
->num_options
= cupsAddOption(name
,
9236 attr
->values
[0].string
.text
,
9237 printer
->num_options
,
9238 &(printer
->options
));
9239 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9240 "Setting %s to \"%s\"...", attr
->name
,
9241 attr
->values
[0].string
.text
);
9244 case IPP_TAG_BOOLEAN
:
9245 printer
->num_options
= cupsAddOption(name
,
9246 attr
->values
[0].boolean
?
9248 printer
->num_options
,
9249 &(printer
->options
));
9250 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9251 "Setting %s to %s...", attr
->name
,
9252 attr
->values
[0].boolean
? "true" : "false");
9255 case IPP_TAG_INTEGER
:
9257 sprintf(value
, "%d", attr
->values
[0].integer
);
9258 printer
->num_options
= cupsAddOption(name
, value
,
9259 printer
->num_options
,
9260 &(printer
->options
));
9261 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9262 "Setting %s to %s...", attr
->name
, value
);
9265 case IPP_TAG_RANGE
:
9266 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
9267 attr
->values
[0].range
.upper
);
9268 printer
->num_options
= cupsAddOption(name
, value
,
9269 printer
->num_options
,
9270 &(printer
->options
));
9271 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9272 "Setting %s to %s...", attr
->name
, value
);
9275 case IPP_TAG_RESOLUTION
:
9276 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
9277 attr
->values
[0].resolution
.yres
,
9278 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
9280 printer
->num_options
= cupsAddOption(name
, value
,
9281 printer
->num_options
,
9282 &(printer
->options
));
9283 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9284 "Setting %s to %s...", attr
->name
, value
);
9288 /* Do nothing for other values */
9296 * 'start_printer()' - Start a printer.
9300 start_printer(cupsd_client_t
*con
, /* I - Client connection */
9301 ipp_attribute_t
*uri
) /* I - Printer URI */
9303 http_status_t status
; /* Policy status */
9304 cups_ptype_t dtype
; /* Destination type (printer or class) */
9305 cupsd_printer_t
*printer
; /* Printer data */
9308 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
9309 con
->http
.fd
, uri
->values
[0].string
.text
);
9312 * Is the destination valid?
9315 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9321 send_ipp_status(con
, IPP_NOT_FOUND
,
9322 _("The printer or class was not found."));
9330 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9332 send_http_error(con
, status
, printer
);
9337 * Start the printer...
9340 printer
->state_message
[0] = '\0';
9342 cupsdStartPrinter(printer
, 1);
9344 if (dtype
& CUPS_PRINTER_CLASS
)
9345 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
9346 printer
->name
, get_username(con
));
9348 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
9349 printer
->name
, get_username(con
));
9354 * Everything was ok, so return OK status...
9357 con
->response
->request
.status
.status_code
= IPP_OK
;
9362 * 'stop_printer()' - Stop a printer.
9366 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
9367 ipp_attribute_t
*uri
) /* I - Printer URI */
9369 http_status_t status
; /* Policy status */
9370 cups_ptype_t dtype
; /* Destination type (printer or class) */
9371 cupsd_printer_t
*printer
; /* Printer data */
9372 ipp_attribute_t
*attr
; /* printer-state-message attribute */
9375 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
9376 con
->http
.fd
, uri
->values
[0].string
.text
);
9379 * Is the destination valid?
9382 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9388 send_ipp_status(con
, IPP_NOT_FOUND
,
9389 _("The printer or class was not found."));
9397 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9399 send_http_error(con
, status
, printer
);
9404 * Stop the printer...
9407 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
9408 IPP_TAG_TEXT
)) == NULL
)
9409 strcpy(printer
->state_message
, "Paused");
9412 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
9413 sizeof(printer
->state_message
));
9416 cupsdStopPrinter(printer
, 1);
9418 if (dtype
& CUPS_PRINTER_CLASS
)
9419 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
9420 printer
->name
, get_username(con
));
9422 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
9423 printer
->name
, get_username(con
));
9426 * Everything was ok, so return OK status...
9429 con
->response
->request
.status
.status_code
= IPP_OK
;
9434 * 'url_encode_attr()' - URL-encode a string attribute.
9438 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
9439 char *buffer
,/* I - String buffer */
9440 int bufsize
)/* I - Size of buffer */
9442 int i
; /* Looping var */
9443 char *bufptr
, /* Pointer into buffer */
9444 *bufend
; /* End of buffer */
9447 strlcpy(buffer
, attr
->name
, bufsize
);
9448 bufptr
= buffer
+ strlen(buffer
);
9449 bufend
= buffer
+ bufsize
- 1;
9451 for (i
= 0; i
< attr
->num_values
; i
++)
9453 if (bufptr
>= bufend
)
9461 if (bufptr
>= bufend
)
9466 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
9467 bufptr
, bufend
- bufptr
+ 1);
9469 if (bufptr
>= bufend
)
9480 * 'url_encode_string()' - URL-encode a string.
9483 static char * /* O - End of string */
9484 url_encode_string(const char *s
, /* I - String */
9485 char *buffer
, /* I - String buffer */
9486 int bufsize
) /* I - Size of buffer */
9488 char *bufptr
, /* Pointer into buffer */
9489 *bufend
; /* End of buffer */
9490 static const char *hex
= "0123456789ABCDEF";
9495 bufend
= buffer
+ bufsize
- 1;
9497 while (*s
&& bufptr
< bufend
)
9499 if (*s
== ' ' || *s
== '%')
9501 if (bufptr
>= (bufend
- 2))
9505 *bufptr
++ = hex
[(*s
>> 4) & 15];
9506 *bufptr
++ = hex
[*s
& 15];
9510 else if (*s
== '\'' || *s
== '\\')
9512 if (bufptr
>= (bufend
- 1))
9529 * 'user_allowed()' - See if a user is allowed to print to a queue.
9532 static int /* O - 0 if not allowed, 1 if allowed */
9533 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
9534 const char *username
) /* I - Username */
9536 int i
; /* Looping var */
9537 struct passwd
*pw
; /* User password data */
9540 if (p
->num_users
== 0)
9543 if (!strcmp(username
, "root"))
9546 pw
= getpwnam(username
);
9549 for (i
= 0; i
< p
->num_users
; i
++)
9551 if (p
->users
[i
][0] == '@')
9554 * Check group membership...
9557 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
9560 else if (!strcasecmp(username
, p
->users
[i
]))
9564 return ((i
< p
->num_users
) != p
->deny_users
);
9569 * 'validate_job()' - Validate printer options and destination.
9573 validate_job(cupsd_client_t
*con
, /* I - Client connection */
9574 ipp_attribute_t
*uri
) /* I - Printer URI */
9576 http_status_t status
; /* Policy status */
9577 ipp_attribute_t
*attr
; /* Current attribute */
9578 ipp_attribute_t
*format
; /* Document-format attribute */
9579 cups_ptype_t dtype
; /* Destination type (printer or class) */
9580 char super
[MIME_MAX_SUPER
],
9581 /* Supertype of file */
9582 type
[MIME_MAX_TYPE
];
9583 /* Subtype of file */
9584 cupsd_printer_t
*printer
; /* Printer */
9587 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
9588 con
->http
.fd
, uri
->values
[0].string
.text
);
9591 * OK, see if the client is sending the document compressed - CUPS
9592 * doesn't support compression yet...
9595 if ((attr
= ippFindAttribute(con
->request
, "compression",
9596 IPP_TAG_KEYWORD
)) != NULL
&&
9597 !strcmp(attr
->values
[0].string
.text
, "none"))
9599 send_ipp_status(con
, IPP_ATTRIBUTES
,
9600 _("Unsupported compression attribute %s!"),
9601 attr
->values
[0].string
.text
);
9602 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9603 "compression", NULL
, attr
->values
[0].string
.text
);
9608 * Is it a format we support?
9611 if ((format
= ippFindAttribute(con
->request
, "document-format",
9612 IPP_TAG_MIMETYPE
)) != NULL
)
9614 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
9616 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9617 format
->values
[0].string
.text
);
9621 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
9622 !mimeType(MimeDatabase
, super
, type
))
9624 cupsdLogMessage(CUPSD_LOG_INFO
,
9625 "Hint: Do you have the raw file printing rules enabled?");
9626 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9627 _("Unsupported format \"%s\"!"),
9628 format
->values
[0].string
.text
);
9629 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9630 "document-format", NULL
, format
->values
[0].string
.text
);
9636 * Is the destination valid?
9639 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9645 send_ipp_status(con
, IPP_NOT_FOUND
,
9646 _("The printer or class was not found."));
9654 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9656 send_http_error(con
, status
, printer
);
9661 * Everything was ok, so return OK status...
9664 con
->response
->request
.status
.status_code
= IPP_OK
;
9669 * 'validate_name()' - Make sure the printer name only contains valid chars.
9672 static int /* O - 0 if name is no good, 1 if name is good */
9673 validate_name(const char *name
) /* I - Name to check */
9675 const char *ptr
; /* Pointer into name */
9679 * Scan the whole name...
9682 for (ptr
= name
; *ptr
; ptr
++)
9683 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
9687 * All the characters are good; validate the length, too...
9690 return ((ptr
- name
) < 128);
9695 * 'validate_user()' - Validate the user for the request.
9698 static int /* O - 1 if permitted, 0 otherwise */
9699 validate_user(cupsd_job_t
*job
, /* I - Job */
9700 cupsd_client_t
*con
, /* I - Client connection */
9701 const char *owner
, /* I - Owner of job/resource */
9702 char *username
, /* O - Authenticated username */
9703 int userlen
) /* I - Length of username */
9705 cupsd_printer_t
*printer
; /* Printer for job */
9708 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9709 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
9711 job
? job
->id
: 0, con
->http
.fd
, owner
? owner
: "(null)",
9718 if (!con
|| !owner
|| !username
|| userlen
<= 0)
9722 * Get the best authenticated username that is available.
9725 strlcpy(username
, get_username(con
), userlen
);
9728 * Check the username against the owner...
9731 printer
= cupsdFindDest(job
->dest
);
9733 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
9734 con
, owner
) == HTTP_OK
);
9739 * End of "$Id: ipp.c 6508 2007-05-03 20:07:14Z mike $".