2 * "$Id: ipp.c 5552 2006-05-20 12:00:37Z mike $"
4 * IPP routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdProcessIPPRequest() - Process an incoming IPP request...
27 * accept_jobs() - Accept print jobs to a printer.
28 * add_class() - Add a class to the system.
29 * add_file() - Add a file to a job.
30 * add_job() - Add a job to a print queue.
31 * add_job_state_reasons() - Add the "job-state-reasons" attribute based
32 * upon the job and printer state...
33 * add_job_subscriptions() - Add any subcriptions for a job.
34 * add_job_uuid() - Add job-uuid attribute to a job.
35 * add_printer() - Add a printer to the system.
36 * add_printer_state_reasons() - Add the "printer-state-reasons" attribute
37 * based upon the printer state...
38 * add_queued_job_count() - Add the "queued-job-count" attribute for
39 * apply_printer_defaults() - Apply printer default options to a job.
40 * authenticate_job() - Set job authentication info.
41 * cancel_all_jobs() - Cancel all print jobs.
42 * cancel_job() - Cancel a print job.
43 * cancel_subscription() - Cancel a subscription.
44 * check_quotas() - Check quotas for a printer and user.
45 * copy_attribute() - Copy a single attribute.
46 * copy_attrs() - Copy attributes from one request to another.
47 * copy_banner() - Copy a banner file to the requests directory
48 * for the specified job.
49 * copy_file() - Copy a PPD file or interface script...
50 * copy_model() - Copy a PPD model file, substituting default
52 * copy_job_attrs() - Copy job attributes.
53 * copy_printer_attrs() - Copy printer attributes.
54 * copy_subscription_attrs() - Copy subscription attributes.
55 * create_job() - Print a file to a printer or class.
56 * create_requested_array() - Create an array for the requested-attributes.
57 * create_subscription() - Create a notification subscription.
58 * delete_printer() - Remove a printer or class from the system.
59 * get_default() - Get the default destination.
60 * get_devices() - Get the list of available devices on the
62 * get_job_attrs() - Get job attributes.
63 * get_jobs() - Get a list of jobs for the specified printer.
64 * get_notifications() - Get events for a subscription.
65 * get_ppds() - Get the list of PPD files on the local
67 * get_printer_attrs() - Get printer attributes.
68 * get_printers() - Get a list of printers.
69 * get_subscription_attrs() - Get subscription attributes.
70 * get_subscriptions() - Get subscriptions.
71 * get_username() - Get the username associated with a request.
72 * hold_job() - Hold a print job.
73 * move_job() - Move a job to a new destination.
74 * ppd_add_default() - Add a PPD default choice.
75 * ppd_parse_line() - Parse a PPD default line.
76 * print_job() - Print a file to a printer or class.
77 * read_ps_line() - Read a line from a PS file...
78 * read_ps_job_ticket() - Reads a job ticket embedded in a PS file.
79 * reject_jobs() - Reject print jobs to a printer.
80 * release_job() - Release a held print job.
81 * restart_job() - Restart an old print job.
82 * save_auth_info() - Save authentication information for a job.
83 * send_document() - Send a file to a printer or class.
84 * send_http_error() - Send a HTTP error back to the IPP client.
85 * send_ipp_status() - Send a status back to the IPP client.
86 * set_default() - Set the default destination...
87 * set_job_attrs() - Set job attributes.
88 * set_printer_defaults() - Set printer default options from a request.
89 * start_printer() - Start a printer.
90 * stop_printer() - Stop a printer.
91 * url_encode_attr() - URL-encode a string attribute.
92 * user_allowed() - See if a user is allowed to print to a queue.
93 * validate_job() - Validate printer options and destination.
94 * validate_name() - Make sure the printer name only contains
96 * validate_user() - Validate the user for the request.
100 * Include necessary headers...
107 #endif /* HAVE_LIBPAPER */
111 * PPD default choice structure...
116 char option
[PPD_MAX_NAME
]; /* Main keyword (option name) */
117 char choice
[PPD_MAX_NAME
]; /* Option keyword (choice name) */
125 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
126 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
127 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
128 mime_type_t
*filetype
, int compression
);
129 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
130 mime_type_t
*filetype
);
131 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
132 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
133 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
134 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
135 static void add_printer_state_reasons(cupsd_client_t
*con
,
137 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
138 static void apply_printer_defaults(cupsd_printer_t
*printer
,
140 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
141 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
142 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
143 static void cancel_subscription(cupsd_client_t
*con
, int id
);
144 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
145 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
147 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
148 ipp_tag_t group
, int quickcopy
);
149 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
151 static int copy_file(const char *from
, const char *to
);
152 static int copy_model(cupsd_client_t
*con
, const char *from
,
154 static void copy_job_attrs(cupsd_client_t
*con
,
157 static void copy_printer_attrs(cupsd_client_t
*con
,
158 cupsd_printer_t
*printer
,
160 static void copy_subscription_attrs(cupsd_client_t
*con
,
161 cupsd_subscription_t
*sub
,
163 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
164 static cups_array_t
*create_requested_array(ipp_t
*request
);
165 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
166 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
167 static void get_default(cupsd_client_t
*con
);
168 static void get_devices(cupsd_client_t
*con
);
169 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
170 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
171 static void get_notifications(cupsd_client_t
*con
);
172 static void get_ppds(cupsd_client_t
*con
);
173 static void get_printers(cupsd_client_t
*con
, int type
);
174 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
175 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
176 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
177 static const char *get_username(cupsd_client_t
*con
);
178 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
179 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
180 static int ppd_add_default(const char *option
, const char *choice
,
181 int num_defaults
, ppd_default_t
**defaults
);
182 static int ppd_parse_line(const char *line
, char *option
, int olen
,
183 char *choice
, int clen
);
184 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
185 static void read_ps_job_ticket(cupsd_client_t
*con
);
186 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
187 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
188 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
189 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
190 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
);
191 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
192 static void send_http_error(cupsd_client_t
*con
, http_status_t status
);
193 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
194 const char *message
, ...)
196 __attribute__ ((__format__ (__printf__
, 3, 4)))
197 # endif /* __GNUC__ */
199 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
200 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
201 static void set_printer_defaults(cupsd_client_t
*con
,
202 cupsd_printer_t
*printer
);
203 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
204 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
205 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
,
207 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
208 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
209 static int validate_name(const char *name
);
210 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
,
211 const char *owner
, char *username
,
216 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request...
219 int /* O - 1 on success, 0 on failure */
220 cupsdProcessIPPRequest(
221 cupsd_client_t
*con
) /* I - Client connection */
223 ipp_tag_t group
; /* Current group tag */
224 ipp_attribute_t
*attr
; /* Current attribute */
225 ipp_attribute_t
*charset
; /* Character set attribute */
226 ipp_attribute_t
*language
; /* Language attribute */
227 ipp_attribute_t
*uri
; /* Printer URI attribute */
228 ipp_attribute_t
*username
; /* requesting-user-name attr */
229 int sub_id
; /* Subscription ID */
232 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
233 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
234 con
, con
->http
.fd
, con
->request
->request
.op
.operation_id
);
237 * First build an empty response message for this request...
240 con
->response
= ippNew();
242 con
->response
->request
.status
.version
[0] = con
->request
->request
.op
.version
[0];
243 con
->response
->request
.status
.version
[1] = con
->request
->request
.op
.version
[1];
244 con
->response
->request
.status
.request_id
= con
->request
->request
.op
.request_id
;
247 * Then validate the request header and required attributes...
250 if (con
->request
->request
.any
.version
[0] != 1)
253 * Return an error, since we only support IPP 1.x.
256 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
257 "%04X %s Bad request version number %d.%d",
258 IPP_VERSION_NOT_SUPPORTED
, con
->http
.hostname
,
259 con
->request
->request
.any
.version
[0],
260 con
->request
->request
.any
.version
[1]);
262 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
263 _("Bad request version number %d.%d!"),
264 con
->request
->request
.any
.version
[0],
265 con
->request
->request
.any
.version
[1]);
267 else if (!con
->request
->attrs
)
269 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
270 "%04X %s No attributes in request",
271 IPP_BAD_REQUEST
, con
->http
.hostname
);
273 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request!"));
278 * Make sure that the attributes are provided in the correct order and
279 * don't repeat groups...
282 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
285 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
288 * Out of order; return an error...
291 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
292 "%04X %s Attribute groups are out of order",
293 IPP_BAD_REQUEST
, con
->http
.hostname
);
295 send_ipp_status(con
, IPP_BAD_REQUEST
,
296 _("Attribute groups are out of order (%x < %x)!"),
297 attr
->group_tag
, group
);
301 group
= attr
->group_tag
;
306 * Then make sure that the first three attributes are:
309 * attributes-natural-language
310 * printer-uri/job-uri
313 attr
= con
->request
->attrs
;
314 if (attr
&& !strcmp(attr
->name
, "attributes-charset") &&
315 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
323 if (attr
&& !strcmp(attr
->name
, "attributes-natural-language") &&
324 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
329 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
330 IPP_TAG_URI
)) != NULL
)
332 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
333 IPP_TAG_URI
)) != NULL
)
339 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
340 "attributes-charset", NULL
, charset
->values
[0].string
.text
);
342 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
343 "attributes-charset", NULL
, DefaultCharset
);
346 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
347 "attributes-natural-language", NULL
,
348 language
->values
[0].string
.text
);
350 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
351 "attributes-natural-language", NULL
, DefaultLanguage
);
353 if (!charset
|| !language
||
355 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
356 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
357 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
358 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
359 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
362 * Return an error, since attributes-charset,
363 * attributes-natural-language, and printer-uri/job-uri are required
364 * for all operations.
369 cupsdLogMessage(CUPSD_LOG_ERROR
,
370 "Missing attributes-charset attribute!");
372 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
373 "%04X %s Missing attributes-charset attribute",
374 IPP_BAD_REQUEST
, con
->http
.hostname
);
379 cupsdLogMessage(CUPSD_LOG_ERROR
,
380 "Missing attributes-natural-language attribute!");
382 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
383 "%04X %s Missing attributes-natural-language attribute",
384 IPP_BAD_REQUEST
, con
->http
.hostname
);
389 cupsdLogMessage(CUPSD_LOG_ERROR
,
390 "Missing printer-uri or job-uri attribute!");
392 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
393 "%04X %s Missing printer-uri or job-uri attribute",
394 IPP_BAD_REQUEST
, con
->http
.hostname
);
397 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
399 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
400 cupsdLogMessage(CUPSD_LOG_DEBUG
,
401 "attr \"%s\": group_tag = %x, value_tag = %x",
402 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
405 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
407 send_ipp_status(con
, IPP_BAD_REQUEST
,
408 _("Missing required attributes!"));
413 * OK, all the checks pass so far; make sure requesting-user-name is
414 * not "root" from a remote host...
417 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
418 IPP_TAG_NAME
)) != NULL
)
421 * Check for root user...
424 if (!strcmp(username
->values
[0].string
.text
, "root") &&
425 strcasecmp(con
->http
.hostname
, "localhost") &&
426 strcmp(con
->username
, "root"))
429 * Remote unauthenticated user masquerading as local root...
432 _cupsStrFree(username
->values
[0].string
.text
);
433 username
->values
[0].string
.text
= _cupsStrAlloc(RemoteRoot
);
437 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
438 IPP_TAG_INTEGER
)) != NULL
)
439 sub_id
= attr
->values
[0].integer
;
444 * Then try processing the operation...
448 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
449 ippOpString(con
->request
->request
.op
.operation_id
),
450 uri
->values
[0].string
.text
);
452 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
453 ippOpString(con
->request
->request
.op
.operation_id
));
455 switch (con
->request
->request
.op
.operation_id
)
461 case IPP_VALIDATE_JOB
:
462 validate_job(con
, uri
);
465 case IPP_CREATE_JOB
:
466 create_job(con
, uri
);
469 case IPP_SEND_DOCUMENT
:
470 send_document(con
, uri
);
473 case IPP_CANCEL_JOB
:
474 cancel_job(con
, uri
);
477 case IPP_GET_JOB_ATTRIBUTES
:
478 get_job_attrs(con
, uri
);
485 case IPP_GET_PRINTER_ATTRIBUTES
:
486 get_printer_attrs(con
, uri
);
493 case IPP_RELEASE_JOB
:
494 release_job(con
, uri
);
497 case IPP_RESTART_JOB
:
498 restart_job(con
, uri
);
501 case IPP_PAUSE_PRINTER
:
502 stop_printer(con
, uri
);
505 case IPP_RESUME_PRINTER
:
506 start_printer(con
, uri
);
509 case IPP_PURGE_JOBS
:
510 cancel_all_jobs(con
, uri
);
513 case IPP_SET_JOB_ATTRIBUTES
:
514 set_job_attrs(con
, uri
);
517 case CUPS_GET_DEFAULT
:
521 case CUPS_GET_PRINTERS
:
522 get_printers(con
, 0);
525 case CUPS_GET_CLASSES
:
526 get_printers(con
, CUPS_PRINTER_CLASS
);
529 case CUPS_ADD_PRINTER
:
530 add_printer(con
, uri
);
533 case CUPS_DELETE_PRINTER
:
534 delete_printer(con
, uri
);
537 case CUPS_ADD_CLASS
:
541 case CUPS_DELETE_CLASS
:
542 delete_printer(con
, uri
);
545 case CUPS_ACCEPT_JOBS
:
546 case IPP_ENABLE_PRINTER
:
547 accept_jobs(con
, uri
);
550 case CUPS_REJECT_JOBS
:
551 case IPP_DISABLE_PRINTER
:
552 reject_jobs(con
, uri
);
555 case CUPS_SET_DEFAULT
:
556 set_default(con
, uri
);
559 case CUPS_GET_DEVICES
:
571 case CUPS_AUTHENTICATE_JOB
:
572 authenticate_job(con
, uri
);
575 case IPP_CREATE_PRINTER_SUBSCRIPTION
:
576 case IPP_CREATE_JOB_SUBSCRIPTION
:
577 create_subscription(con
, uri
);
580 case IPP_GET_SUBSCRIPTION_ATTRIBUTES
:
581 get_subscription_attrs(con
, sub_id
);
584 case IPP_GET_SUBSCRIPTIONS
:
585 get_subscriptions(con
, uri
);
588 case IPP_RENEW_SUBSCRIPTION
:
589 renew_subscription(con
, sub_id
);
592 case IPP_CANCEL_SUBSCRIPTION
:
593 cancel_subscription(con
, sub_id
);
596 case IPP_GET_NOTIFICATIONS
:
597 get_notifications(con
);
601 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
602 "%04X %s Operation %04X (%s) not supported",
603 IPP_OPERATION_NOT_SUPPORTED
, con
->http
.hostname
,
604 con
->request
->request
.op
.operation_id
,
605 ippOpString(con
->request
->request
.op
.operation_id
));
607 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
608 _("%s not supported!"),
609 ippOpString(con
->request
->request
.op
.operation_id
));
619 * Sending data from the scheduler...
622 cupsdLogMessage(CUPSD_LOG_DEBUG
,
623 "cupsdProcessIPPRequest: %d status_code=%x (%s)",
624 con
->http
.fd
, con
->response
->request
.status
.status_code
,
625 ippErrorString(con
->response
->request
.status
.status_code
));
627 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp"))
629 #ifdef CUPSD_USE_CHUNKING
631 * Because older versions of CUPS (1.1.17 and older) and some IPP
632 * clients do not implement chunking properly, we should not use
633 * chunking by default. This may become the default in future
634 * CUPS releases, or we might add a configuration directive for
638 if (con
->http
.version
== HTTP_1_1
)
640 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
642 httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n");
645 #endif /* CUPSD_USE_CHUNKING */
647 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
648 con
->http
.data_remaining
= ippLength(con
->response
);
650 if (con
->http
.data_remaining
< INT_MAX
)
651 con
->http
._data_remaining
= con
->http
.data_remaining
;
653 con
->http
._data_remaining
= INT_MAX
;
655 httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
656 CUPS_LLCAST con
->http
.data_remaining
);
659 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
660 "cupsdProcessIPPRequest: Adding fd %d to OutputSet...",
663 FD_SET(con
->http
.fd
, OutputSet
);
666 * Tell the caller the response header was sent successfully...
674 * Tell the caller the response header could not be sent...
683 * Sending data from a subprocess like cups-deviced; tell the caller
684 * everything is A-OK so far...
693 * 'accept_jobs()' - Accept print jobs to a printer.
697 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
698 ipp_attribute_t
*uri
) /* I - Printer or class URI */
700 http_status_t status
; /* Policy status */
701 cups_ptype_t dtype
; /* Destination type (printer or class) */
702 cupsd_printer_t
*printer
; /* Printer data */
705 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
706 con
->http
.fd
, uri
->values
[0].string
.text
);
709 * Is the destination valid?
712 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
718 send_ipp_status(con
, IPP_NOT_FOUND
,
719 _("The printer or class was not found."));
727 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
729 send_http_error(con
, status
);
734 * Accept jobs sent to the printer...
737 printer
->accepting
= 1;
738 printer
->state_message
[0] = '\0';
740 cupsdAddPrinterHistory(printer
);
742 if (dtype
& CUPS_PRINTER_CLASS
)
744 cupsdSaveAllClasses();
746 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
747 printer
->name
, get_username(con
));
751 cupsdSaveAllPrinters();
753 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" now accepting jobs (\"%s\").",
754 printer
->name
, get_username(con
));
758 * Everything was ok, so return OK status...
761 con
->response
->request
.status
.status_code
= IPP_OK
;
766 * 'add_class()' - Add a class to the system.
770 add_class(cupsd_client_t
*con
, /* I - Client connection */
771 ipp_attribute_t
*uri
) /* I - URI of class */
773 http_status_t status
; /* Policy status */
774 int i
; /* Looping var */
775 char method
[HTTP_MAX_URI
], /* Method portion of URI */
776 username
[HTTP_MAX_URI
], /* Username portion of URI */
777 host
[HTTP_MAX_URI
], /* Host portion of URI */
778 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
779 int port
; /* Port portion of URI */
780 cupsd_printer_t
*pclass
, /* Class */
781 *member
; /* Member printer/class */
782 cups_ptype_t dtype
; /* Destination type */
783 ipp_attribute_t
*attr
; /* Printer attribute */
784 int modify
; /* Non-zero if we just modified */
785 char newname
[IPP_MAX_NAME
]; /* New class name */
786 int need_restart_job
; /* Need to restart job? */
789 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
790 con
->http
.fd
, uri
->values
[0].string
.text
);
793 * Do we have a valid URI?
796 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
797 sizeof(method
), username
, sizeof(username
), host
,
798 sizeof(host
), &port
, resource
, sizeof(resource
));
801 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
804 * No, return an error...
807 send_ipp_status(con
, IPP_BAD_REQUEST
,
808 _("The printer-uri must be of the form "
809 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
814 * Do we have a valid printer name?
817 if (!validate_name(resource
+ 9))
820 * No, return an error...
823 send_ipp_status(con
, IPP_BAD_REQUEST
,
824 _("The printer-uri \"%s\" contains invalid characters."),
825 uri
->values
[0].string
.text
);
833 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
835 send_http_error(con
, status
);
840 * See if the class already exists; if not, create a new class...
843 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
846 * Class doesn't exist; see if we have a printer of the same name...
849 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
850 !(pclass
->type
& CUPS_PRINTER_REMOTE
))
853 * Yes, return an error...
856 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
857 _("A printer named \"%s\" already exists!"),
863 * No, add the pclass...
866 pclass
= cupsdAddClass(resource
+ 9);
869 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
872 * Rename the implicit class to "AnyClass" or remove it...
875 if (ImplicitAnyClasses
)
877 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
878 cupsdRenamePrinter(pclass
, newname
);
881 cupsdDeletePrinter(pclass
, 1);
884 * Add the class as a new local class...
887 pclass
= cupsdAddClass(resource
+ 9);
890 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
893 * Rename the remote class to "Class"...
896 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
897 cupsdRenamePrinter(pclass
, newname
);
900 * Add the class as a new local class...
903 pclass
= cupsdAddClass(resource
+ 9);
910 * Look for attributes and copy them over as needed...
913 need_restart_job
= 0;
915 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
916 IPP_TAG_TEXT
)) != NULL
)
917 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
919 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
920 IPP_TAG_TEXT
)) != NULL
)
921 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
923 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
924 IPP_TAG_BOOLEAN
)) != NULL
)
926 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-is-accepting-jobs to %d (was %d.)",
927 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
929 pclass
->accepting
= attr
->values
[0].boolean
;
930 cupsdAddPrinterHistory(pclass
);
933 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
934 IPP_TAG_BOOLEAN
)) != NULL
)
936 if (pclass
->shared
&& !attr
->values
[0].boolean
)
937 cupsdSendBrowseDelete(pclass
);
939 cupsdLogMessage(CUPSD_LOG_INFO
,
940 "Setting %s printer-is-shared to %d (was %d.)",
941 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
943 pclass
->shared
= attr
->values
[0].boolean
;
946 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
947 IPP_TAG_ENUM
)) != NULL
)
949 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
950 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
952 send_ipp_status(con
, IPP_BAD_REQUEST
,
953 _("Attempt to set %s printer-state to bad value %d!"),
954 pclass
->name
, attr
->values
[0].integer
);
958 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)", pclass
->name
,
959 attr
->values
[0].integer
, pclass
->state
);
961 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
962 cupsdStopPrinter(pclass
, 0);
965 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
966 need_restart_job
= 1;
969 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
970 IPP_TAG_TEXT
)) != NULL
)
972 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
973 sizeof(pclass
->state_message
));
974 cupsdAddPrinterHistory(pclass
);
976 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
977 IPP_TAG_URI
)) != NULL
)
980 * Clear the printer array as needed...
983 need_restart_job
= 1;
985 if (pclass
->num_printers
> 0)
987 free(pclass
->printers
);
988 pclass
->num_printers
= 0;
992 * Add each printer or class that is listed...
995 for (i
= 0; i
< attr
->num_values
; i
++)
998 * Search for the printer or class URI...
1001 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1007 send_ipp_status(con
, IPP_NOT_FOUND
,
1008 _("The printer or class was not found."));
1013 * Add it to the class...
1016 cupsdAddPrinterToClass(pclass
, member
);
1020 set_printer_defaults(con
, pclass
);
1023 * Update the printer class attributes and return...
1026 cupsdSetPrinterAttrs(pclass
);
1027 cupsdSaveAllClasses();
1029 if (need_restart_job
&& pclass
->job
)
1034 * Stop the current job and then restart it below...
1037 job
= (cupsd_job_t
*)pclass
->job
;
1039 cupsdStopJob(job
, 1);
1041 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1042 job
->state_value
= IPP_JOB_PENDING
;
1045 if (need_restart_job
)
1048 cupsdWritePrintcap();
1052 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1053 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1056 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1057 pclass
->name
, get_username(con
));
1061 cupsdAddPrinterHistory(pclass
);
1063 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1064 "New class \"%s\" added by \"%s\".", pclass
->name
,
1067 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1068 pclass
->name
, get_username(con
));
1071 con
->response
->request
.status
.status_code
= IPP_OK
;
1076 * 'add_file()' - Add a file to a job.
1079 static int /* O - 0 on success, -1 on error */
1080 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1081 cupsd_job_t
*job
, /* I - Job to add to */
1082 mime_type_t
*filetype
, /* I - Type of file */
1083 int compression
) /* I - Compression */
1085 mime_type_t
**filetypes
; /* New filetypes array... */
1086 int *compressions
; /* New compressions array... */
1089 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1090 "add_file(con=%p[%d], job=%d, filetype=%s/%s, compression=%d)",
1091 con
, con
->http
.fd
, job
->id
, filetype
->super
, filetype
->type
,
1095 * Add the file to the job...
1098 if (job
->num_files
== 0)
1100 compressions
= (int *)malloc(sizeof(int));
1101 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1105 compressions
= (int *)realloc(job
->compressions
,
1106 (job
->num_files
+ 1) * sizeof(int));
1107 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1108 (job
->num_files
+ 1) *
1109 sizeof(mime_type_t
*));
1112 if (!compressions
|| !filetypes
)
1114 cupsdCancelJob(job
, 1);
1116 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1117 _("Unable to allocate memory for file types!"));
1121 job
->compressions
= compressions
;
1122 job
->compressions
[job
->num_files
] = compression
;
1123 job
->filetypes
= filetypes
;
1124 job
->filetypes
[job
->num_files
] = filetype
;
1133 * 'add_job()' - Add a job to a print queue.
1136 static cupsd_job_t
* /* O - Job object */
1137 add_job(cupsd_client_t
*con
, /* I - Client connection */
1138 cupsd_printer_t
*printer
, /* I - Destination printer */
1139 mime_type_t
*filetype
) /* I - First print file type, if any */
1141 http_status_t status
; /* Policy status */
1142 ipp_attribute_t
*attr
; /* Current attribute */
1143 const char *val
; /* Default option value */
1144 int priority
; /* Job priority */
1145 char *title
; /* Job name/title */
1146 cupsd_job_t
*job
; /* Current job */
1147 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1148 int kbytes
; /* Size of print file */
1149 int i
; /* Looping var */
1150 int lowerpagerange
; /* Page range bound */
1153 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1154 con
, con
->http
.fd
, printer
, printer
->name
,
1155 filetype
, filetype
->super
, filetype
->type
);
1158 * Check remote printing to non-shared printer...
1161 if (!printer
->shared
&&
1162 strcasecmp(con
->http
.hostname
, "localhost") &&
1163 strcasecmp(con
->http
.hostname
, ServerName
))
1165 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1166 _("The printer or class is not shared!"));
1174 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1176 send_http_error(con
, status
);
1179 else if ((printer
->type
& CUPS_PRINTER_AUTHENTICATED
) && !con
->username
[0])
1181 send_http_error(con
, HTTP_UNAUTHORIZED
);
1186 * See if the printer is accepting jobs...
1189 if (!printer
->accepting
)
1191 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1192 _("Destination \"%s\" is not accepting jobs."),
1198 * Validate job template attributes; for now just document-format,
1199 * copies, and page-ranges...
1202 if (filetype
&& printer
->filetypes
&&
1203 !cupsArrayFind(printer
->filetypes
, filetype
))
1205 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1206 /* MIME media type string */
1209 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1212 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1213 _("Unsupported format \'%s\'!"), mimetype
);
1215 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1216 "document-format", NULL
, mimetype
);
1221 if ((attr
= ippFindAttribute(con
->request
, "copies",
1222 IPP_TAG_INTEGER
)) != NULL
)
1224 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1226 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1227 attr
->values
[0].integer
);
1228 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1229 "copies", attr
->values
[0].integer
);
1234 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1235 IPP_TAG_RANGE
)) != NULL
)
1237 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1239 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1240 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1242 send_ipp_status(con
, IPP_BAD_REQUEST
,
1243 _("Bad page-ranges values %d-%d."),
1244 attr
->values
[i
].range
.lower
,
1245 attr
->values
[i
].range
.upper
);
1249 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1254 * Make sure we aren't over our limit...
1257 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1260 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1262 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1263 _("Too many active jobs."));
1267 if (!check_quotas(con
, printer
))
1269 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1274 * Create the job and set things up...
1277 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1278 IPP_TAG_INTEGER
)) != NULL
)
1279 priority
= attr
->values
[0].integer
;
1282 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1283 printer
->options
)) != NULL
)
1284 priority
= atoi(val
);
1288 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1292 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1293 IPP_TAG_NAME
)) != NULL
)
1294 title
= attr
->values
[0].string
.text
;
1296 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1297 title
= "Untitled");
1299 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1301 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1302 _("Unable to add job for destination \"%s\"!"),
1307 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
1308 CUPS_PRINTER_REMOTE
);
1309 job
->attrs
= con
->request
;
1310 con
->request
= NULL
;
1312 add_job_uuid(con
, job
);
1313 apply_printer_defaults(printer
, job
);
1315 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1317 if (con
->username
[0])
1319 cupsdSetString(&job
->username
, con
->username
);
1322 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1324 save_auth_info(con
, job
);
1328 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1329 "add_job: requesting-user-name=\"%s\"",
1330 attr
->values
[0].string
.text
);
1332 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1335 cupsdSetString(&job
->username
, "anonymous");
1338 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1339 "job-originating-user-name", NULL
, job
->username
);
1342 attr
->group_tag
= IPP_TAG_JOB
;
1343 _cupsStrFree(attr
->name
);
1344 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1347 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1348 IPP_TAG_ZERO
)) != NULL
)
1351 * Request contains a job-originating-host-name attribute; validate it...
1354 if (attr
->value_tag
!= IPP_TAG_NAME
||
1355 attr
->num_values
!= 1 ||
1356 strcmp(con
->http
.hostname
, "localhost"))
1359 * Can't override the value if we aren't connected via localhost.
1360 * Also, we can only have 1 value and it must be a name value.
1363 switch (attr
->value_tag
)
1365 case IPP_TAG_STRING
:
1366 case IPP_TAG_TEXTLANG
:
1367 case IPP_TAG_NAMELANG
:
1370 case IPP_TAG_KEYWORD
:
1372 case IPP_TAG_URISCHEME
:
1373 case IPP_TAG_CHARSET
:
1374 case IPP_TAG_LANGUAGE
:
1375 case IPP_TAG_MIMETYPE
:
1377 * Free old strings...
1380 for (i
= 0; i
< attr
->num_values
; i
++)
1382 _cupsStrFree(attr
->values
[i
].string
.text
);
1383 attr
->values
[i
].string
.text
= NULL
;
1384 if (attr
->values
[i
].string
.charset
)
1386 _cupsStrFree(attr
->values
[i
].string
.charset
);
1387 attr
->values
[i
].string
.charset
= NULL
;
1396 * Use the default connection hostname instead...
1399 attr
->value_tag
= IPP_TAG_NAME
;
1400 attr
->num_values
= 1;
1401 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1404 attr
->group_tag
= IPP_TAG_JOB
;
1409 * No job-originating-host-name attribute, so use the hostname from
1413 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1414 "job-originating-host-name", NULL
, con
->http
.hostname
);
1417 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1419 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1420 "time-at-processing", 0);
1421 attr
->value_tag
= IPP_TAG_NOVALUE
;
1422 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1423 "time-at-completed", 0);
1424 attr
->value_tag
= IPP_TAG_NOVALUE
;
1427 * Add remaining job attributes...
1430 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1431 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1432 "job-state", IPP_JOB_STOPPED
);
1433 job
->state_value
= job
->state
->values
[0].integer
;
1434 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1435 "job-media-sheets-completed", 0);
1436 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1438 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1441 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1442 IPP_TAG_INTEGER
)) != NULL
)
1443 attr
->values
[0].integer
= 0;
1445 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1448 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1449 IPP_TAG_KEYWORD
)) == NULL
)
1450 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1453 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1454 printer
->options
)) == NULL
)
1457 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1458 "job-hold-until", NULL
, val
);
1460 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1461 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1464 * Hold job until specified time...
1467 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1469 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1470 job
->state_value
= IPP_JOB_HELD
;
1472 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1474 job
->hold_until
= time(NULL
) + 60;
1475 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1476 job
->state_value
= IPP_JOB_HELD
;
1480 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1481 job
->state_value
= IPP_JOB_PENDING
;
1484 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1488 * Add job sheets options...
1491 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1492 IPP_TAG_ZERO
)) == NULL
)
1494 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1495 "Adding default job-sheets values \"%s,%s\"...",
1496 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1498 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1500 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1501 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1504 job
->job_sheets
= attr
;
1507 * Enforce classification level if set...
1512 cupsdLogMessage(CUPSD_LOG_INFO
,
1513 "Classification=\"%s\", ClassifyOverride=%d",
1514 Classification
? Classification
: "(null)",
1517 if (ClassifyOverride
)
1519 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1520 (attr
->num_values
== 1 ||
1521 !strcmp(attr
->values
[1].string
.text
, "none")))
1524 * Force the leading banner to have the classification on it...
1527 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1529 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1530 "job-sheets=\"%s,none\", "
1531 "job-originating-user-name=\"%s\"",
1532 job
->id
, Classification
, job
->username
);
1534 else if (attr
->num_values
== 2 &&
1535 strcmp(attr
->values
[0].string
.text
,
1536 attr
->values
[1].string
.text
) &&
1537 strcmp(attr
->values
[0].string
.text
, "none") &&
1538 strcmp(attr
->values
[1].string
.text
, "none"))
1541 * Can't put two different security markings on the same document!
1544 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1546 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1547 "job-sheets=\"%s,%s\", "
1548 "job-originating-user-name=\"%s\"",
1549 job
->id
, attr
->values
[0].string
.text
,
1550 attr
->values
[1].string
.text
, job
->username
);
1552 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1553 strcmp(attr
->values
[0].string
.text
, "none") &&
1554 (attr
->num_values
== 1 ||
1555 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1556 strcmp(attr
->values
[1].string
.text
, "none"))))
1558 if (attr
->num_values
== 1)
1559 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1560 "[Job %d] CLASSIFICATION OVERRIDDEN "
1561 "job-sheets=\"%s\", "
1562 "job-originating-user-name=\"%s\"",
1563 job
->id
, attr
->values
[0].string
.text
, job
->username
);
1565 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1566 "[Job %d] CLASSIFICATION OVERRIDDEN "
1567 "job-sheets=\"%s,%s\",fffff "
1568 "job-originating-user-name=\"%s\"",
1569 job
->id
, attr
->values
[0].string
.text
,
1570 attr
->values
[1].string
.text
, job
->username
);
1573 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1574 (attr
->num_values
== 1 ||
1575 strcmp(attr
->values
[1].string
.text
, Classification
)))
1578 * Force the banner to have the classification on it...
1581 if (attr
->num_values
> 1 &&
1582 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1584 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1585 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1589 if (attr
->num_values
== 1 ||
1590 strcmp(attr
->values
[0].string
.text
, "none"))
1591 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1593 if (attr
->num_values
> 1 &&
1594 strcmp(attr
->values
[1].string
.text
, "none"))
1595 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1598 if (attr
->num_values
> 1)
1599 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1600 "[Job %d] CLASSIFICATION FORCED "
1601 "job-sheets=\"%s,%s\", "
1602 "job-originating-user-name=\"%s\"",
1603 job
->id
, attr
->values
[0].string
.text
,
1604 attr
->values
[1].string
.text
, job
->username
);
1606 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1607 "[Job %d] CLASSIFICATION FORCED "
1608 "job-sheets=\"%s\", "
1609 "job-originating-user-name=\"%s\"",
1610 job
->id
, Classification
, job
->username
);
1615 * See if we need to add the starting sheet...
1618 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1620 cupsdLogMessage(CUPSD_LOG_INFO
,
1621 "Adding start banner page \"%s\" to job %d.",
1622 attr
->values
[0].string
.text
, job
->id
);
1624 kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
);
1626 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1629 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1630 IPP_TAG_ZERO
)) != NULL
)
1634 * Fill in the response info...
1637 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1638 LocalPort
, job
->id
);
1640 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1643 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1645 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1647 add_job_state_reasons(con
, job
);
1649 con
->response
->request
.status
.status_code
= IPP_OK
;
1652 * Add any job subscriptions...
1655 add_job_subscriptions(con
, job
);
1658 * Set all but the first two attributes to the job attributes group...
1661 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1662 attr
->group_tag
= IPP_TAG_JOB
;
1665 * Fire the "job created" event...
1668 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1671 * Return the new job...
1679 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1680 * upon the job and printer state...
1684 add_job_state_reasons(
1685 cupsd_client_t
*con
, /* I - Client connection */
1686 cupsd_job_t
*job
) /* I - Job info */
1688 cupsd_printer_t
*dest
; /* Destination printer */
1691 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1692 con
, con
->http
.fd
, job
? job
->id
: 0);
1694 switch (job
? job
->state_value
: IPP_JOB_CANCELLED
)
1696 case IPP_JOB_PENDING
:
1697 dest
= cupsdFindDest(job
->dest
);
1699 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1700 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1701 "job-state-reasons", NULL
, "printer-stopped");
1703 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1704 "job-state-reasons", NULL
, "none");
1708 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1709 IPP_TAG_KEYWORD
) != NULL
||
1710 ippFindAttribute(job
->attrs
, "job-hold-until",
1711 IPP_TAG_NAME
) != NULL
)
1712 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1713 "job-state-reasons", NULL
, "job-hold-until-specified");
1715 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1716 "job-state-reasons", NULL
, "job-incoming");
1719 case IPP_JOB_PROCESSING
:
1720 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1721 "job-state-reasons", NULL
, "job-printing");
1724 case IPP_JOB_STOPPED
:
1725 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1726 "job-state-reasons", NULL
, "job-stopped");
1729 case IPP_JOB_CANCELLED
:
1730 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1731 "job-state-reasons", NULL
, "job-canceled-by-user");
1734 case IPP_JOB_ABORTED
:
1735 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1736 "job-state-reasons", NULL
, "aborted-by-system");
1739 case IPP_JOB_COMPLETED
:
1740 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1741 "job-state-reasons", NULL
, "job-completed-successfully");
1748 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1752 add_job_subscriptions(
1753 cupsd_client_t
*con
, /* I - Client connection */
1754 cupsd_job_t
*job
) /* I - Newly created job */
1756 int i
; /* Looping var */
1757 ipp_attribute_t
*prev
, /* Previous attribute */
1758 *next
, /* Next attribute */
1759 *attr
; /* Current attribute */
1760 cupsd_subscription_t
*sub
; /* Subscription object */
1761 const char *recipient
, /* notify-recipient-uri */
1762 *pullmethod
; /* notify-pull-method */
1763 ipp_attribute_t
*user_data
; /* notify-user-data */
1764 int interval
; /* notify-time-interval */
1765 unsigned mask
; /* notify-events */
1769 * Find the first subscription group attribute; return if we have
1773 for (attr
= job
->attrs
->attrs
, prev
= NULL
;
1775 prev
= attr
, attr
= attr
->next
)
1776 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1783 * Process the subscription attributes in the request...
1792 mask
= CUPSD_EVENT_NONE
;
1794 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1796 if (!strcmp(attr
->name
, "notify-recipient") &&
1797 attr
->value_tag
== IPP_TAG_URI
)
1798 recipient
= attr
->values
[0].string
.text
;
1799 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1800 attr
->value_tag
== IPP_TAG_KEYWORD
)
1801 pullmethod
= attr
->values
[0].string
.text
;
1802 else if (!strcmp(attr
->name
, "notify-charset") &&
1803 attr
->value_tag
== IPP_TAG_CHARSET
&&
1804 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
1805 strcmp(attr
->values
[0].string
.text
, "utf-8"))
1807 send_ipp_status(con
, IPP_CHARSET
,
1808 _("Character set \"%s\" not supported!"),
1809 attr
->values
[0].string
.text
);
1812 else if (!strcmp(attr
->name
, "notify-natural-language") &&
1813 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
1814 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
1816 send_ipp_status(con
, IPP_CHARSET
,
1817 _("Language \"%s\" not supported!"),
1818 attr
->values
[0].string
.text
);
1821 else if (!strcmp(attr
->name
, "notify-user-data") &&
1822 attr
->value_tag
== IPP_TAG_STRING
)
1824 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
1826 send_ipp_status(con
, IPP_REQUEST_VALUE
,
1827 _("The notify-user-data value is too large "
1828 "(%d > 63 octets)!"),
1829 attr
->values
[0].unknown
.length
);
1835 else if (!strcmp(attr
->name
, "notify-events") &&
1836 attr
->value_tag
== IPP_TAG_KEYWORD
)
1838 for (i
= 0; i
< attr
->num_values
; i
++)
1839 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
1841 else if (!strcmp(attr
->name
, "notify-lease-duration"))
1843 send_ipp_status(con
, IPP_BAD_REQUEST
,
1844 _("The notify-lease-duration attribute cannot be "
1845 "used with job subscriptions."));
1848 else if (!strcmp(attr
->name
, "notify-time-interval") &&
1849 attr
->value_tag
== IPP_TAG_INTEGER
)
1850 interval
= attr
->values
[0].integer
;
1855 if (!recipient
&& !pullmethod
)
1858 if (mask
== CUPSD_EVENT_NONE
)
1859 mask
= CUPSD_EVENT_JOB_COMPLETED
;
1861 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
1864 sub
->interval
= interval
;
1866 cupsdSetString(&sub
->owner
, job
->username
);
1870 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
1871 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
1872 sub
->user_data_len
);
1875 ippAddSeparator(con
->response
);
1876 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
1877 "notify-subscription-id", sub
->id
);
1883 cupsdSaveAllSubscriptions();
1886 * Remove all of the subscription attributes from the job request...
1889 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
1893 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
1894 attr
->group_tag
== IPP_TAG_ZERO
)
1897 * Free and remove this attribute...
1905 job
->attrs
->attrs
= next
;
1911 job
->attrs
->last
= prev
;
1912 job
->attrs
->current
= prev
;
1917 * 'add_job_uuid()' - Add job-uuid attribute to a job.
1919 * See RFC 4122 for the definition of UUIDs and the format.
1923 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
1924 cupsd_job_t
*job
) /* I - Job */
1926 char uuid
[1024]; /* job-uuid string */
1927 ipp_attribute_t
*attr
; /* job-uuid attribute */
1928 _cups_md5_state_t md5state
; /* MD5 state */
1929 unsigned char md5sum
[16]; /* MD5 digest/sum */
1933 * First see if the job already has a job-uuid attribute; if so, return...
1936 if ((attr
= ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
)) != NULL
)
1940 * No job-uuid attribute, so build a version 3 UUID with the local job
1941 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
1942 * the ServerName, server name and port that the client connected to,
1943 * and local job ID...
1946 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
1947 con
->serverport
, job
->id
);
1949 _cupsMD5Init(&md5state
);
1950 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
1951 _cupsMD5Finish(&md5state
, md5sum
);
1954 * Format the UUID URI using the MD5 sum and job ID.
1957 snprintf(uuid
, sizeof(uuid
),
1958 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
1959 "%02x%02x%02x%02x%02x%02x",
1960 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
1961 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
1962 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
1963 md5sum
[14], md5sum
[15]);
1965 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
1970 * 'add_printer()' - Add a printer to the system.
1974 add_printer(cupsd_client_t
*con
, /* I - Client connection */
1975 ipp_attribute_t
*uri
) /* I - URI of printer */
1977 http_status_t status
; /* Policy status */
1978 int i
; /* Looping var */
1979 char method
[HTTP_MAX_URI
], /* Method portion of URI */
1980 username
[HTTP_MAX_URI
], /* Username portion of URI */
1981 host
[HTTP_MAX_URI
], /* Host portion of URI */
1982 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1983 int port
; /* Port portion of URI */
1984 cupsd_printer_t
*printer
; /* Printer/class */
1985 ipp_attribute_t
*attr
; /* Printer attribute */
1986 cups_file_t
*fp
; /* Script/PPD file */
1987 char line
[1024]; /* Line from file... */
1988 char srcfile
[1024], /* Source Script/PPD file */
1989 dstfile
[1024]; /* Destination Script/PPD file */
1990 int modify
; /* Non-zero if we are modifying */
1991 char newname
[IPP_MAX_NAME
]; /* New printer name */
1992 int need_restart_job
; /* Need to restart job? */
1995 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
1996 con
->http
.fd
, uri
->values
[0].string
.text
);
1999 * Do we have a valid URI?
2002 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2003 sizeof(method
), username
, sizeof(username
), host
,
2004 sizeof(host
), &port
, resource
, sizeof(resource
));
2006 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2009 * No, return an error...
2012 send_ipp_status(con
, IPP_BAD_REQUEST
,
2013 _("The printer-uri must be of the form "
2014 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2019 * Do we have a valid printer name?
2022 if (!validate_name(resource
+ 10))
2025 * No, return an error...
2028 send_ipp_status(con
, IPP_BAD_REQUEST
,
2029 _("The printer-uri \"%s\" contains invalid characters."),
2030 uri
->values
[0].string
.text
);
2038 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2040 send_http_error(con
, status
);
2045 * See if the printer already exists; if not, create a new printer...
2048 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2051 * Printer doesn't exist; see if we have a class of the same name...
2054 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2055 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2058 * Yes, return an error...
2061 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2062 _("A class named \"%s\" already exists!"),
2068 * No, add the printer...
2071 printer
= cupsdAddPrinter(resource
+ 10);
2074 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2077 * Rename the implicit printer to "AnyPrinter" or delete it...
2080 if (ImplicitAnyClasses
)
2082 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2083 cupsdRenamePrinter(printer
, newname
);
2086 cupsdDeletePrinter(printer
, 1);
2089 * Add the printer as a new local printer...
2092 printer
= cupsdAddPrinter(resource
+ 10);
2095 else if (printer
->type
& CUPS_PRINTER_REMOTE
)
2098 * Rename the remote printer to "Printer@server"...
2101 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2103 cupsdRenamePrinter(printer
, newname
);
2106 * Add the printer as a new local printer...
2109 printer
= cupsdAddPrinter(resource
+ 10);
2116 * Look for attributes and copy them over as needed...
2119 need_restart_job
= 0;
2121 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2122 IPP_TAG_TEXT
)) != NULL
)
2123 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2125 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2126 IPP_TAG_TEXT
)) != NULL
)
2127 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2129 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2130 IPP_TAG_URI
)) != NULL
)
2133 * Do we have a valid device URI?
2136 need_restart_job
= 1;
2138 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, method
,
2139 sizeof(method
), username
, sizeof(username
), host
,
2140 sizeof(host
), &port
, resource
, sizeof(resource
));
2142 if (!strcmp(method
, "file"))
2145 * See if the administrator has enabled file devices...
2148 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2151 * File devices are disabled and the URL is not file:/dev/null...
2154 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2155 _("File device URIs have been disabled! "
2156 "To enable, see the FileDevice directive in "
2157 "\"%s/cupsd.conf\"."),
2165 * See if the backend exists and is executable...
2168 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, method
);
2169 if (access(srcfile
, X_OK
))
2172 * Could not find device in list!
2175 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2176 attr
->values
[0].string
.text
);
2181 cupsdLogMessage(CUPSD_LOG_INFO
,
2182 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2184 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2186 cupsdSanitizeURI(printer
->device_uri
, resource
,
2189 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2192 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2193 IPP_TAG_KEYWORD
)) != NULL
)
2195 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2198 need_restart_job
= 1;
2200 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2202 for (i
= 0; i
< supported
->num_values
; i
++)
2203 if (!strcmp(supported
->values
[i
].string
.text
,
2204 attr
->values
[0].string
.text
))
2207 if (i
>= supported
->num_values
)
2209 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2210 attr
->values
[0].string
.text
);
2214 cupsdLogMessage(CUPSD_LOG_INFO
,
2215 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2216 printer
->name
, attr
->values
[0].string
.text
,
2217 printer
->port_monitor
);
2219 if (strcmp(attr
->values
[0].string
.text
, "none"))
2220 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2222 cupsdClearString(&printer
->port_monitor
);
2225 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2226 IPP_TAG_BOOLEAN
)) != NULL
)
2228 cupsdLogMessage(CUPSD_LOG_INFO
,
2229 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2230 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2232 printer
->accepting
= attr
->values
[0].boolean
;
2233 cupsdAddPrinterHistory(printer
);
2236 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2237 IPP_TAG_BOOLEAN
)) != NULL
)
2239 if (printer
->shared
&& !attr
->values
[0].boolean
)
2240 cupsdSendBrowseDelete(printer
);
2242 cupsdLogMessage(CUPSD_LOG_INFO
,
2243 "Setting %s printer-is-shared to %d (was %d.)",
2244 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2246 printer
->shared
= attr
->values
[0].boolean
;
2249 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2250 IPP_TAG_ENUM
)) != NULL
)
2252 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2253 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2255 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2256 attr
->values
[0].integer
);
2260 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)", printer
->name
,
2261 attr
->values
[0].integer
, printer
->state
);
2263 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2264 cupsdStopPrinter(printer
, 0);
2267 need_restart_job
= 1;
2268 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2271 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2272 IPP_TAG_TEXT
)) != NULL
)
2274 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2275 sizeof(printer
->state_message
));
2276 cupsdAddPrinterHistory(printer
);
2279 set_printer_defaults(con
, printer
);
2282 * See if we have all required attributes...
2285 if (!printer
->device_uri
)
2286 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2289 * See if we have an interface script or PPD file attached to the request...
2294 need_restart_job
= 1;
2296 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2298 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2301 * Yes; get the first line from it...
2305 cupsFileGets(fp
, line
, sizeof(line
));
2309 * Then see what kind of file it is...
2312 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2315 if (!strncmp(line
, "*PPD-Adobe", 10))
2318 * The new file is a PPD file, so remove any old interface script
2319 * that might be lying around...
2327 * This must be an interface script, so move the file over to the
2328 * interfaces directory and make it executable...
2331 if (copy_file(srcfile
, dstfile
))
2333 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2334 _("Unable to copy interface script - %s!"),
2340 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2341 "Copied interface script successfully!");
2342 chmod(dstfile
, 0755);
2346 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2349 if (!strncmp(line
, "*PPD-Adobe", 10))
2352 * The new file is a PPD file, so move the file over to the
2353 * ppd directory and make it readable by all...
2356 if (copy_file(srcfile
, dstfile
))
2358 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2359 _("Unable to copy PPD file - %s!"),
2365 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2366 "Copied PPD file successfully!");
2367 chmod(dstfile
, 0644);
2373 * This must be an interface script, so remove any old PPD file that
2374 * may be lying around...
2381 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2382 IPP_TAG_NAME
)) != NULL
)
2384 need_restart_job
= 1;
2386 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2389 * Raw driver, remove any existing PPD or interface script files.
2392 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2396 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2406 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2410 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2413 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2415 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2420 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2421 "Copied PPD file successfully!");
2422 chmod(dstfile
, 0644);
2428 * Update the printer attributes and return...
2431 cupsdSetPrinterAttrs(printer
);
2432 cupsdSaveAllPrinters();
2434 if (need_restart_job
&& printer
->job
)
2439 * Stop the current job and then restart it below...
2442 job
= (cupsd_job_t
*)printer
->job
;
2444 cupsdStopJob(job
, 1);
2446 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2447 job
->state_value
= IPP_JOB_PENDING
;
2450 if (need_restart_job
)
2453 cupsdWritePrintcap();
2457 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2458 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2461 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2462 printer
->name
, get_username(con
));
2466 cupsdAddPrinterHistory(printer
);
2468 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2469 "New printer \"%s\" added by \"%s\".", printer
->name
,
2472 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2473 printer
->name
, get_username(con
));
2476 con
->response
->request
.status
.status_code
= IPP_OK
;
2481 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2482 * based upon the printer state...
2486 add_printer_state_reasons(
2487 cupsd_client_t
*con
, /* I - Client connection */
2488 cupsd_printer_t
*p
) /* I - Printer info */
2490 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2491 "add_printer_state_reasons(%p[%d], %p[%s])",
2492 con
, con
->http
.fd
, p
, p
->name
);
2494 if (p
->num_reasons
== 0)
2495 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2496 "printer-state-reasons", NULL
,
2497 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2499 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2500 "printer-state-reasons", p
->num_reasons
, NULL
,
2501 (const char * const *)p
->reasons
);
2506 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2507 * the specified printer or class.
2511 add_queued_job_count(
2512 cupsd_client_t
*con
, /* I - Client connection */
2513 cupsd_printer_t
*p
) /* I - Printer or class */
2515 int count
; /* Number of jobs on destination */
2518 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2519 con
, con
->http
.fd
, p
, p
->name
);
2521 count
= cupsdGetPrinterJobCount(p
->name
);
2523 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2524 "queued-job-count", count
);
2529 * 'apply_printer_defaults()' - Apply printer default options to a job.
2533 apply_printer_defaults(
2534 cupsd_printer_t
*printer
, /* I - Printer */
2535 cupsd_job_t
*job
) /* I - Job */
2537 int i
, /* Looping var */
2538 num_options
; /* Number of default options */
2539 cups_option_t
*options
, /* Default options */
2540 *option
; /* Current option */
2544 * Collect all of the default options and add the missing ones to the
2548 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
2551 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2553 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2558 * Encode these options as attributes in the job object...
2561 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2562 cupsFreeOptions(num_options
, options
);
2567 * 'authenticate_job()' - Set job authentication info.
2571 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2572 ipp_attribute_t
*uri
) /* I - Job URI */
2574 ipp_attribute_t
*attr
; /* Job-id attribute */
2575 int jobid
; /* Job ID */
2576 cupsd_job_t
*job
; /* Current job */
2577 char method
[HTTP_MAX_URI
],
2578 /* Method portion of URI */
2579 username
[HTTP_MAX_URI
],
2580 /* Username portion of URI */
2582 /* Host portion of URI */
2583 resource
[HTTP_MAX_URI
];
2584 /* Resource portion of URI */
2585 int port
; /* Port portion of URI */
2588 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2589 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
2592 * Start with "everything is OK" status...
2595 con
->response
->request
.status
.status_code
= IPP_OK
;
2598 * See if we have a job URI or a printer URI...
2601 if (!strcmp(uri
->name
, "printer-uri"))
2604 * Got a printer URI; see if we also have a job-id attribute...
2607 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2608 IPP_TAG_INTEGER
)) == NULL
)
2610 send_ipp_status(con
, IPP_BAD_REQUEST
,
2611 _("Got a printer-uri attribute but no job-id!"));
2615 jobid
= attr
->values
[0].integer
;
2620 * Got a job URI; parse it to get the job ID...
2623 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2624 sizeof(method
), username
, sizeof(username
), host
,
2625 sizeof(host
), &port
, resource
, sizeof(resource
));
2627 if (strncmp(resource
, "/jobs/", 6))
2633 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
2634 uri
->values
[0].string
.text
);
2638 jobid
= atoi(resource
+ 6);
2642 * See if the job exists...
2645 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2648 * Nope - return a "not found" error...
2651 send_ipp_status(con
, IPP_NOT_FOUND
,
2652 _("Job #%d does not exist!"), jobid
);
2657 * See if the job has been completed...
2660 if (job
->state_value
!= IPP_JOB_HELD
)
2663 * Return a "not-possible" error...
2666 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2667 _("Job #%d is not held for authentication!"),
2673 * See if we have already authenticated...
2676 if (!con
->username
[0])
2678 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
2679 _("No authentication information provided!"));
2684 * See if the job is owned by the requesting user...
2687 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
2689 send_http_error(con
, HTTP_UNAUTHORIZED
);
2694 * Save the authentication information for this job...
2697 save_auth_info(con
, job
);
2700 * Reset the job-hold-until value to "no-hold"...
2703 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2704 IPP_TAG_KEYWORD
)) == NULL
)
2705 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2709 attr
->value_tag
= IPP_TAG_KEYWORD
;
2710 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2714 * Release the job and return...
2717 cupsdReleaseJob(job
);
2719 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was authenticated by \"%s\".", jobid
,
2725 * 'cancel_all_jobs()' - Cancel all print jobs.
2729 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
2730 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2732 http_status_t status
; /* Policy status */
2733 cups_ptype_t dtype
; /* Destination type */
2734 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2735 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2736 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
2737 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2738 int port
; /* Port portion of URI */
2739 ipp_attribute_t
*attr
; /* Attribute in request */
2740 const char *username
; /* Username */
2741 int purge
; /* Purge? */
2742 cupsd_printer_t
*printer
; /* Printer */
2745 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
2746 con
->http
.fd
, uri
->values
[0].string
.text
);
2749 * See if we have a printer URI...
2752 if (strcmp(uri
->name
, "printer-uri"))
2754 send_ipp_status(con
, IPP_BAD_REQUEST
,
2755 _("The printer-uri attribute is required!"));
2760 * Get the username (if any) for the jobs we want to cancel (only if
2761 * "my-jobs" is specified...
2764 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
2765 IPP_TAG_BOOLEAN
)) != NULL
&&
2766 attr
->values
[0].boolean
)
2768 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
2769 IPP_TAG_NAME
)) != NULL
)
2770 username
= attr
->values
[0].string
.text
;
2773 send_ipp_status(con
, IPP_BAD_REQUEST
,
2774 _("Missing requesting-user-name attribute!"));
2782 * Look for the "purge-jobs" attribute...
2785 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
2786 IPP_TAG_BOOLEAN
)) != NULL
)
2787 purge
= attr
->values
[0].boolean
;
2792 * And if the destination is valid...
2795 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
2801 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
2802 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
2803 hostname
, sizeof(hostname
), &port
,
2804 resource
, sizeof(resource
));
2806 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
2807 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
2809 send_ipp_status(con
, IPP_NOT_FOUND
,
2810 _("The printer or class was not found."));
2818 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2820 send_http_error(con
, status
);
2825 * Cancel all jobs on all printers...
2828 cupsdCancelJobs(NULL
, username
, purge
);
2830 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
2831 purge
? "purged" : "cancelled", get_username(con
));
2839 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2842 send_http_error(con
, status
);
2847 * Cancel all of the jobs on the named printer...
2850 cupsdCancelJobs(printer
->name
, username
, purge
);
2852 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
2853 printer
->name
, purge
? "purged" : "cancelled",
2857 con
->response
->request
.status
.status_code
= IPP_OK
;
2862 * 'cancel_job()' - Cancel a print job.
2866 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
2867 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2869 ipp_attribute_t
*attr
; /* Current attribute */
2870 int jobid
; /* Job ID */
2871 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2872 username
[HTTP_MAX_URI
], /* Username portion of URI */
2873 host
[HTTP_MAX_URI
], /* Host portion of URI */
2874 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2875 int port
; /* Port portion of URI */
2876 cupsd_job_t
*job
; /* Job information */
2877 cups_ptype_t dtype
; /* Destination type (printer or class) */
2878 cupsd_printer_t
*printer
; /* Printer data */
2881 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
2882 con
->http
.fd
, uri
->values
[0].string
.text
);
2885 * See if we have a job URI or a printer URI...
2888 if (!strcmp(uri
->name
, "printer-uri"))
2891 * Got a printer URI; see if we also have a job-id attribute...
2894 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2895 IPP_TAG_INTEGER
)) == NULL
)
2897 send_ipp_status(con
, IPP_BAD_REQUEST
,
2898 _("Got a printer-uri attribute but no job-id!"));
2902 if ((jobid
= attr
->values
[0].integer
) == 0)
2905 * Find the current job on the specified printer...
2908 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
2914 send_ipp_status(con
, IPP_NOT_FOUND
,
2915 _("The printer or class was not found."));
2920 * See if the printer is currently printing a job...
2924 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
2928 * No, see if there are any pending jobs...
2931 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
2933 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
2934 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
2935 !strcasecmp(job
->dest
, printer
->name
))
2942 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
2952 * Got a job URI; parse it to get the job ID...
2955 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2956 sizeof(scheme
), username
, sizeof(username
), host
,
2957 sizeof(host
), &port
, resource
, sizeof(resource
));
2959 if (strncmp(resource
, "/jobs/", 6))
2965 send_ipp_status(con
, IPP_BAD_REQUEST
,
2966 _("Bad job-uri attribute \"%s\"!"),
2967 uri
->values
[0].string
.text
);
2971 jobid
= atoi(resource
+ 6);
2975 * See if the job exists...
2978 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2981 * Nope - return a "not found" error...
2984 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
2989 * See if the job is owned by the requesting user...
2992 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
2994 send_http_error(con
, HTTP_UNAUTHORIZED
);
2999 * See if the job is already completed, cancelled, or aborted; if so,
3000 * we can't cancel...
3003 if (job
->state_value
>= IPP_JOB_CANCELLED
)
3005 switch (job
->state_value
)
3007 case IPP_JOB_CANCELLED
:
3008 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3009 _("Job #%d is already cancelled - can\'t cancel."),
3013 case IPP_JOB_ABORTED
:
3014 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3015 _("Job #%d is already aborted - can\'t cancel."),
3020 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3021 _("Job #%d is already completed - can\'t cancel."),
3030 * Cancel the job and return...
3033 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3034 "Job cancelled by \"%s\".", username
);
3036 cupsdCancelJob(job
, 0);
3039 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was cancelled by \"%s\".", jobid
,
3042 con
->response
->request
.status
.status_code
= IPP_OK
;
3047 * 'cancel_subscription()' - Cancel a subscription.
3051 cancel_subscription(
3052 cupsd_client_t
*con
, /* I - Client connection */
3053 int sub_id
) /* I - Subscription ID */
3055 http_status_t status
; /* Policy status */
3056 cupsd_subscription_t
*sub
; /* Subscription */
3059 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3060 "cancel_subscription(con=%p[%d], sub_id=%d)",
3061 con
, con
->http
.fd
, sub_id
);
3064 * Is the subscription ID valid?
3067 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3070 * Bad subscription ID...
3073 send_ipp_status(con
, IPP_NOT_FOUND
,
3074 _("notify-subscription-id %d no good!"), sub_id
);
3082 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3084 con
, sub
->owner
)) != HTTP_OK
)
3086 send_http_error(con
, status
);
3091 * Cancel the subscription...
3094 cupsdDeleteSubscription(sub
, 1);
3096 con
->response
->request
.status
.status_code
= IPP_OK
;
3101 * 'check_quotas()' - Check quotas for a printer and user.
3104 static int /* O - 1 if OK, 0 if not */
3105 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3106 cupsd_printer_t
*p
) /* I - Printer or class */
3108 int i
; /* Looping var */
3109 char username
[33]; /* Username */
3110 cupsd_quota_t
*q
; /* Quota data */
3111 struct passwd
*pw
; /* User password data */
3114 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3115 con
, con
->http
.fd
, p
, p
->name
);
3125 * Figure out who is printing...
3128 strlcpy(username
, get_username(con
), sizeof(username
));
3131 * Check global active job limits for printers and users...
3134 if (MaxJobsPerPrinter
)
3137 * Check if there are too many pending jobs on this printer...
3140 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3142 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3151 * Check if there are too many pending jobs for this user...
3154 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3156 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3163 * Check against users...
3166 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3171 pw
= getpwnam(username
);
3174 for (i
= 0; i
< p
->num_users
; i
++)
3175 if (p
->users
[i
][0] == '@')
3178 * Check group membership...
3181 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3184 else if (!strcasecmp(username
, p
->users
[i
]))
3187 if ((i
< p
->num_users
) == p
->deny_users
)
3189 cupsdLogMessage(CUPSD_LOG_INFO
,
3190 "Denying user \"%s\" access to printer \"%s\"...",
3200 if (p
->k_limit
|| p
->page_limit
)
3202 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3204 cupsdLogMessage(CUPSD_LOG_ERROR
,
3205 "Unable to allocate quota data for user \"%s\"!",
3210 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3211 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3213 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3220 * If we have gotten this far, we're done!
3228 * 'copy_attribute()' - Copy a single attribute.
3231 static ipp_attribute_t
* /* O - New attribute */
3233 ipp_t
*to
, /* O - Destination request/response */
3234 ipp_attribute_t
*attr
, /* I - Attribute to copy */
3235 int quickcopy
) /* I - Do a quick copy? */
3237 int i
; /* Looping var */
3238 ipp_attribute_t
*toattr
; /* Destination attribute */
3241 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3242 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
3243 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
3246 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
3249 toattr
= ippAddSeparator(to
);
3252 case IPP_TAG_INTEGER
:
3254 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3255 attr
->name
, attr
->num_values
, NULL
);
3257 for (i
= 0; i
< attr
->num_values
; i
++)
3258 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
3261 case IPP_TAG_BOOLEAN
:
3262 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
3263 attr
->num_values
, NULL
);
3265 for (i
= 0; i
< attr
->num_values
; i
++)
3266 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
3269 case IPP_TAG_STRING
:
3272 case IPP_TAG_KEYWORD
:
3274 case IPP_TAG_URISCHEME
:
3275 case IPP_TAG_CHARSET
:
3276 case IPP_TAG_LANGUAGE
:
3277 case IPP_TAG_MIMETYPE
:
3278 toattr
= ippAddStrings(to
, attr
->group_tag
,
3279 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3280 attr
->name
, attr
->num_values
, NULL
, NULL
);
3284 for (i
= 0; i
< attr
->num_values
; i
++)
3285 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3289 for (i
= 0; i
< attr
->num_values
; i
++)
3290 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3295 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
3296 attr
->values
[0].date
);
3299 case IPP_TAG_RESOLUTION
:
3300 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
3301 attr
->num_values
, IPP_RES_PER_INCH
,
3304 for (i
= 0; i
< attr
->num_values
; i
++)
3306 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
3307 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
3308 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
3312 case IPP_TAG_RANGE
:
3313 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
3314 attr
->num_values
, NULL
, NULL
);
3316 for (i
= 0; i
< attr
->num_values
; i
++)
3318 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
3319 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
3323 case IPP_TAG_TEXTLANG
:
3324 case IPP_TAG_NAMELANG
:
3325 toattr
= ippAddStrings(to
, attr
->group_tag
,
3326 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3327 attr
->name
, attr
->num_values
, NULL
, NULL
);
3331 for (i
= 0; i
< attr
->num_values
; i
++)
3333 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
3334 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3339 for (i
= 0; i
< attr
->num_values
; i
++)
3342 toattr
->values
[i
].string
.charset
=
3343 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
3345 toattr
->values
[i
].string
.charset
=
3346 toattr
->values
[0].string
.charset
;
3348 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3353 case IPP_TAG_BEGIN_COLLECTION
:
3354 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
3355 attr
->num_values
, NULL
);
3357 for (i
= 0; i
< attr
->num_values
; i
++)
3359 toattr
->values
[i
].collection
= ippNew();
3360 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
3361 NULL
, IPP_TAG_ZERO
, 0);
3366 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3367 attr
->name
, attr
->num_values
, NULL
);
3369 for (i
= 0; i
< attr
->num_values
; i
++)
3371 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
3373 if (toattr
->values
[i
].unknown
.length
> 0)
3375 if ((toattr
->values
[i
].unknown
.data
=
3376 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
3377 toattr
->values
[i
].unknown
.length
= 0;
3379 memcpy(toattr
->values
[i
].unknown
.data
,
3380 attr
->values
[i
].unknown
.data
,
3381 toattr
->values
[i
].unknown
.length
);
3384 break; /* anti-compiler-warning-code */
3392 * 'copy_attrs()' - Copy attributes from one request to another.
3396 copy_attrs(ipp_t
*to
, /* I - Destination request */
3397 ipp_t
*from
, /* I - Source request */
3398 cups_array_t
*ra
, /* I - Requested attributes */
3399 ipp_tag_t group
, /* I - Group to copy */
3400 int quickcopy
) /* I - Do a quick copy? */
3402 ipp_attribute_t
*fromattr
; /* Source attribute */
3405 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3406 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3407 to
, from
, ra
, group
, quickcopy
);
3412 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3415 * Filter attributes as needed...
3418 if (group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3419 fromattr
->group_tag
!= IPP_TAG_ZERO
&& !fromattr
->name
)
3422 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
3423 copy_attribute(to
, fromattr
, quickcopy
);
3429 * 'copy_banner()' - Copy a banner file to the requests directory for the
3433 static int /* O - Size of banner file in kbytes */
3434 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
3435 cupsd_job_t
*job
, /* I - Job information */
3436 const char *name
) /* I - Name of banner */
3438 int i
; /* Looping var */
3439 int kbytes
; /* Size of banner file in kbytes */
3440 char filename
[1024]; /* Job filename */
3441 cupsd_banner_t
*banner
; /* Pointer to banner */
3442 cups_file_t
*in
; /* Input file */
3443 cups_file_t
*out
; /* Output file */
3444 int ch
; /* Character from file */
3445 char attrname
[255], /* Name of attribute */
3446 *s
; /* Pointer into name */
3447 ipp_attribute_t
*attr
; /* Attribute */
3450 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
3451 con
, con
->http
.fd
, job
, job
->id
, name
? name
: "(null)");
3454 * Find the banner; return if not found or "none"...
3457 if (!name
|| !strcmp(name
, "none") ||
3458 (banner
= cupsdFindBanner(name
)) == NULL
)
3462 * Open the banner and job files...
3465 if (add_file(con
, job
, banner
->filetype
, 0))
3468 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
3470 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
3472 cupsdLogMessage(CUPSD_LOG_ERROR
,
3473 "copy_banner: Unable to create banner job file %s - %s",
3474 filename
, strerror(errno
));
3479 fchmod(cupsFileNumber(out
), 0640);
3480 fchown(cupsFileNumber(out
), RunUser
, Group
);
3483 * Try the localized banner file under the subdirectory...
3486 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
3488 if (strlen(attrname
) > 2 && attrname
[2] == '-')
3491 * Convert ll-cc to ll_CC...
3495 attrname
[3] = toupper(attrname
[3] & 255);
3496 attrname
[4] = toupper(attrname
[4] & 255);
3499 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3502 if (access(filename
, 0) && strlen(attrname
) > 2)
3505 * Wasn't able to find "ll_CC" locale file; try the non-national
3506 * localization banner directory.
3511 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3515 if (access(filename
, 0))
3518 * Use the non-localized banner file.
3521 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
3524 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
3528 cupsdLogMessage(CUPSD_LOG_ERROR
,
3529 "copy_banner: Unable to open banner template file %s - %s",
3530 filename
, strerror(errno
));
3536 * Parse the file to the end...
3539 while ((ch
= cupsFileGetChar(in
)) != EOF
)
3543 * Get an attribute name...
3546 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
3547 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
3549 else if (s
< (attrname
+ sizeof(attrname
) - 1))
3559 * Ignore { followed by stuff that is not an attribute name...
3562 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
3567 * See if it is defined...
3570 if (attrname
[0] == '?')
3575 if (!strcmp(s
, "printer-name"))
3577 cupsFilePuts(out
, job
->dest
);
3580 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
3583 * See if we have a leading question mark...
3586 if (attrname
[0] != '?')
3589 * Nope, write to file as-is; probably a PostScript procedure...
3592 cupsFilePrintf(out
, "{%s}", attrname
);
3599 * Output value(s)...
3602 for (i
= 0; i
< attr
->num_values
; i
++)
3605 cupsFilePutChar(out
, ',');
3607 switch (attr
->value_tag
)
3609 case IPP_TAG_INTEGER
:
3611 if (!strncmp(s
, "time-at-", 8))
3612 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
3614 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
3617 case IPP_TAG_BOOLEAN
:
3618 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
3621 case IPP_TAG_NOVALUE
:
3622 cupsFilePuts(out
, "novalue");
3625 case IPP_TAG_RANGE
:
3626 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
3627 attr
->values
[i
].range
.upper
);
3630 case IPP_TAG_RESOLUTION
:
3631 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3632 attr
->values
[i
].resolution
.yres
,
3633 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3638 case IPP_TAG_STRING
:
3641 case IPP_TAG_KEYWORD
:
3642 case IPP_TAG_CHARSET
:
3643 case IPP_TAG_LANGUAGE
:
3644 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
3647 * Need to quote strings for PS banners...
3652 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
3654 if (*p
== '(' || *p
== ')' || *p
== '\\')
3656 cupsFilePutChar(out
, '\\');
3657 cupsFilePutChar(out
, *p
);
3659 else if (*p
< 32 || *p
> 126)
3660 cupsFilePrintf(out
, "\\%03o", *p
& 255);
3662 cupsFilePutChar(out
, *p
);
3666 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
3670 break; /* anti-compiler-warning-code */
3674 else if (ch
== '\\') /* Quoted char */
3676 ch
= cupsFileGetChar(in
);
3678 if (ch
!= '{') /* Only do special handling for \{ */
3679 cupsFilePutChar(out
, '\\');
3681 cupsFilePutChar(out
, ch
);
3684 cupsFilePutChar(out
, ch
);
3688 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
3690 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
3691 IPP_TAG_INTEGER
)) != NULL
)
3692 attr
->values
[0].integer
+= kbytes
;
3701 * 'copy_file()' - Copy a PPD file or interface script...
3704 static int /* O - 0 = success, -1 = error */
3705 copy_file(const char *from
, /* I - Source file */
3706 const char *to
) /* I - Destination file */
3708 cups_file_t
*src
, /* Source file */
3709 *dst
; /* Destination file */
3710 int bytes
; /* Bytes to read/write */
3711 char buffer
[2048]; /* Copy buffer */
3714 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
3717 * Open the source and destination file for a copy...
3720 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
3723 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
3730 * Copy the source file to the destination...
3733 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
3734 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
3742 * Close both files and return...
3747 return (cupsFileClose(dst
));
3752 * 'copy_model()' - Copy a PPD model file, substituting default values
3756 static int /* O - 0 = success, -1 = error */
3757 copy_model(cupsd_client_t
*con
, /* I - Client connection */
3758 const char *from
, /* I - Source file */
3759 const char *to
) /* I - Destination file */
3761 fd_set
*input
; /* select() input set */
3762 struct timeval timeout
; /* select() timeout */
3763 int maxfd
; /* Maximum file descriptor for select() */
3764 char tempfile
[1024]; /* Temporary PPD file */
3765 int tempfd
; /* Temporary PPD file descriptor */
3766 int temppid
; /* Process ID of cups-driverd */
3767 int temppipe
[2]; /* Temporary pipes */
3768 char *argv
[4], /* Command-line arguments */
3769 *envp
[MAX_ENV
]; /* Environment */
3770 cups_file_t
*src
, /* Source file */
3771 *dst
; /* Destination file */
3772 int bytes
, /* Bytes from pipe */
3773 total
; /* Total bytes from pipe */
3774 char buffer
[2048], /* Copy buffer */
3775 *ptr
; /* Pointer into buffer */
3776 int i
; /* Looping var */
3777 char option
[PPD_MAX_NAME
], /* Option name */
3778 choice
[PPD_MAX_NAME
]; /* Choice name */
3779 int num_defaults
; /* Number of default options */
3780 ppd_default_t
*defaults
; /* Default options */
3781 char cups_protocol
[PPD_MAX_LINE
];
3782 /* cupsProtocol attribute */
3783 int have_letter
, /* Have Letter size */
3784 have_a4
; /* Have A4 size */
3785 #ifdef HAVE_LIBPAPER
3786 char *paper_result
; /* Paper size name from libpaper */
3787 char system_paper
[64]; /* Paper size name buffer */
3788 #endif /* HAVE_LIBPAPER */
3791 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3792 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
3796 * Run cups-driverd to get the PPD file...
3799 argv
[0] = "cups-driverd";
3801 argv
[2] = (char *)from
;
3804 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3806 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
3807 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
3808 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3812 cupsdOpenPipe(temppipe
);
3814 if ((input
= calloc(1, SetSize
)) == NULL
)
3819 cupsdLogMessage(CUPSD_LOG_ERROR
,
3820 "copy_model: Unable to allocate %d bytes for select()...",
3825 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3826 "copy_model: Running \"cups-driverd cat %s\"...", from
);
3828 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
3840 * Wait up to 30 seconds for the PPD file to be copied...
3845 if (temppipe
[0] > CGIPipes
[0])
3846 maxfd
= temppipe
[0] + 1;
3848 maxfd
= CGIPipes
[0] + 1;
3853 * See if we have data ready...
3858 FD_SET(temppipe
[0], input
);
3859 FD_SET(CGIPipes
[0], input
);
3861 timeout
.tv_sec
= 30;
3862 timeout
.tv_usec
= 0;
3864 if ((i
= select(maxfd
, input
, NULL
, NULL
, &timeout
)) < 0)
3874 * We have timed out...
3880 if (FD_ISSET(temppipe
[0], input
))
3883 * Read the PPD file from the pipe, and write it to the PPD file.
3886 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
3888 if (write(tempfd
, buffer
, bytes
) < bytes
)
3897 if (FD_ISSET(CGIPipes
[0], input
))
3909 * No data from cups-deviced...
3912 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
3918 * Read the source file and see what page sizes are supported...
3921 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
3930 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
3931 if (!strncmp(buffer
, "*PageSize ", 10))
3934 * Strip UI text and command data from the end of the line...
3937 if ((ptr
= strchr(buffer
+ 10, '/')) != NULL
)
3939 if ((ptr
= strchr(buffer
+ 10, ':')) != NULL
)
3942 for (ptr
= buffer
+ 10; isspace(*ptr
); ptr
++);
3945 * Look for Letter and A4 page sizes...
3948 if (!strcmp(ptr
, "Letter"))
3951 if (!strcmp(ptr
, "A4"))
3955 cupsFileRewind(src
);
3958 * Open the destination (if possible) and set the default options...
3963 cups_protocol
[0] = '\0';
3965 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
3968 * Read all of the default lines from the old PPD...
3971 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
3972 if (!strncmp(buffer
, "*Default", 8))
3975 * Add the default option...
3978 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
3979 choice
, sizeof(choice
)))
3980 num_defaults
= ppd_add_default(option
, choice
, num_defaults
,
3983 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
3984 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
3988 #ifdef HAVE_LIBPAPER
3989 else if ((paper_result
= systempapername()) != NULL
)
3992 * Set the default media sizes from the systemwide default...
3995 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
3996 system_paper
[0] = toupper(system_paper
[0] & 255);
3998 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
3999 (!strcmp(system_paper
, "A4") && have_a4
))
4001 num_defaults
= ppd_add_default("PageSize", system_paper
,
4002 num_defaults
, &defaults
);
4003 num_defaults
= ppd_add_default("PageRegion", system_paper
,
4004 num_defaults
, &defaults
);
4005 num_defaults
= ppd_add_default("PaperDimension", system_paper
,
4006 num_defaults
, &defaults
);
4007 num_defaults
= ppd_add_default("ImageableArea", system_paper
,
4008 num_defaults
, &defaults
);
4011 #endif /* HAVE_LIBPAPER */
4015 * Add the default media sizes...
4017 * Note: These values are generally not valid for large-format devices
4018 * like plotters, however it is probably safe to say that those
4019 * users will configure the media size after initially adding
4020 * the device anyways...
4023 if (!DefaultLanguage
||
4024 !strcasecmp(DefaultLanguage
, "C") ||
4025 !strcasecmp(DefaultLanguage
, "POSIX") ||
4026 !strcasecmp(DefaultLanguage
, "en") ||
4027 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4028 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4029 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4032 * These are the only locales that will default to "letter" size...
4037 num_defaults
= ppd_add_default("PageSize", "Letter", num_defaults
,
4039 num_defaults
= ppd_add_default("PageRegion", "Letter", num_defaults
,
4041 num_defaults
= ppd_add_default("PaperDimension", "Letter", num_defaults
,
4043 num_defaults
= ppd_add_default("ImageableArea", "Letter", num_defaults
,
4050 * The rest default to "a4" size...
4053 num_defaults
= ppd_add_default("PageSize", "A4", num_defaults
,
4055 num_defaults
= ppd_add_default("PageRegion", "A4", num_defaults
,
4057 num_defaults
= ppd_add_default("PaperDimension", "A4", num_defaults
,
4059 num_defaults
= ppd_add_default("ImageableArea", "A4", num_defaults
,
4065 * Open the destination file for a copy...
4068 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4070 if (num_defaults
> 0)
4079 * Copy the source file to the destination...
4082 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4084 if (!strncmp(buffer
, "*Default", 8))
4087 * Check for an previous default option choice...
4090 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4091 choice
, sizeof(choice
)))
4093 for (i
= 0; i
< num_defaults
; i
++)
4094 if (!strcmp(option
, defaults
[i
].option
))
4097 * Substitute the previous choice...
4100 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
,
4101 defaults
[i
].choice
);
4107 cupsFilePrintf(dst
, "%s\n", buffer
);
4110 if (cups_protocol
[0])
4111 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4113 if (num_defaults
> 0)
4117 * Close both files and return...
4124 return (cupsFileClose(dst
));
4129 * 'copy_job_attrs()' - Copy job attributes.
4133 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4134 cupsd_job_t
*job
, /* I - Job */
4135 cups_array_t
*ra
) /* I - Requested attributes array */
4137 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4141 * Send the requested attributes for each job...
4144 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4145 con
->servername
, con
->serverport
, "/jobs/%d",
4148 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
4149 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4150 "job-more-info", NULL
, job_uri
);
4152 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4153 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4154 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4155 job
->num_files
> 0);
4157 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
4158 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4159 "job-printer-up-time", time(NULL
));
4161 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4162 add_job_state_reasons(con
, job
);
4164 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4165 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4166 "job-uri", NULL
, job_uri
);
4168 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
4173 * 'copy_printer_attrs()' - Copy printer attributes.
4178 cupsd_client_t
*con
, /* I - Client connection */
4179 cupsd_printer_t
*printer
, /* I - Printer */
4180 cups_array_t
*ra
) /* I - Requested attributes array */
4182 char printer_uri
[HTTP_MAX_URI
];
4184 time_t curtime
; /* Current time */
4185 int i
; /* Looping var */
4186 ipp_attribute_t
*history
; /* History collection */
4190 * Copy the printer attributes to the response using requested-attributes
4191 * and document-format attributes that may be provided by the client.
4194 curtime
= time(NULL
);
4197 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
4198 printer
->recoverable
)
4199 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4200 "com.apple.print.recoverable-message", NULL
,
4201 printer
->recoverable
);
4202 #endif /* __APPLE__ */
4204 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4205 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4206 ippTimeToDate(curtime
));
4208 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4209 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4210 "printer-error-policy", NULL
, printer
->error_policy
);
4212 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4213 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
4214 printer
->accepting
);
4216 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4217 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
4220 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4221 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4222 "printer-op-policy", NULL
, printer
->op_policy
);
4224 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4225 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4228 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4229 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4230 "printer-state-change-time", printer
->state_time
);
4232 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
4233 cupsArrayFind(ra
, "printer-state-history"))
4236 * Printer history is only sent if specifically requested, so that
4237 * older CUPS/IPP clients won't barf on the collection attributes.
4240 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
4241 "printer-state-history",
4242 printer
->num_history
, NULL
);
4244 for (i
= 0; i
< printer
->num_history
; i
++)
4245 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
4246 NULL
, IPP_TAG_ZERO
, 0);
4249 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4250 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4251 "printer-state-message", NULL
, printer
->state_message
);
4253 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4254 add_printer_state_reasons(con
, printer
);
4256 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4258 int type
; /* printer-type value */
4262 * Add the CUPS-specific printer-type attribute...
4265 type
= printer
->type
;
4267 if (printer
== DefaultPrinter
)
4268 type
|= CUPS_PRINTER_DEFAULT
;
4270 if (!printer
->accepting
)
4271 type
|= CUPS_PRINTER_REJECTING
;
4273 if (!printer
->shared
)
4274 type
|= CUPS_PRINTER_NOT_SHARED
;
4276 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
4280 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4281 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4282 "printer-up-time", curtime
);
4284 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
4285 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
4288 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4289 "ipp", NULL
, con
->servername
, con
->serverport
,
4290 "/printers/%s", printer
->name
);
4291 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4292 "printer-uri-supported", NULL
, printer_uri
);
4293 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4297 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4298 add_queued_job_count(con
, printer
);
4300 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
4301 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
4306 * 'copy_subscription_attrs()' - Copy subscription attributes.
4310 copy_subscription_attrs(
4311 cupsd_client_t
*con
, /* I - Client connection */
4312 cupsd_subscription_t
*sub
, /* I - Subscription */
4313 cups_array_t
*ra
) /* I - Requested attributes array */
4315 ipp_attribute_t
*attr
; /* Current attribute */
4316 char printer_uri
[HTTP_MAX_URI
];
4318 int count
; /* Number of events */
4319 unsigned mask
; /* Current event mask */
4320 const char *name
; /* Current event name */
4324 * Copy the subscription attributes to the response using the
4325 * requested-attributes attribute that may be provided by the client.
4328 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
4330 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
4333 * Simple event list...
4336 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
4337 IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
4338 "notify-events", NULL
, name
);
4343 * Complex event list...
4346 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4347 if (sub
->mask
& mask
)
4350 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
4351 IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
4352 "notify-events", count
, NULL
, NULL
);
4354 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4355 if (sub
->mask
& mask
)
4357 attr
->values
[count
].string
.text
=
4358 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
4365 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
4366 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4367 "notify-job-id", sub
->job
->id
);
4369 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
4370 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4371 "notify-lease-duration", sub
->lease
);
4373 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
4375 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4376 "ipp", NULL
, con
->servername
, con
->serverport
,
4377 "/printers/%s", sub
->dest
->name
);
4378 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4379 "notify-printer-uri", NULL
, printer_uri
);
4382 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
4383 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4384 "notify-recipient-uri", NULL
, sub
->recipient
);
4385 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
4386 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
4387 "notify-pull-method", NULL
, "ippget");
4389 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
4390 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
4391 "notify-subscriber-user-name", NULL
, sub
->owner
);
4393 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
4394 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4395 "notify-subscription-id", sub
->id
);
4397 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
4398 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4399 "notify-time-interval", sub
->interval
);
4401 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
4402 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
4403 sub
->user_data
, sub
->user_data_len
);
4408 * 'create_job()' - Print a file to a printer or class.
4412 create_job(cupsd_client_t
*con
, /* I - Client connection */
4413 ipp_attribute_t
*uri
) /* I - Printer URI */
4415 cupsd_printer_t
*printer
; /* Printer */
4416 cupsd_job_t
*job
; /* New job */
4419 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
4420 con
->http
.fd
, uri
->values
[0].string
.text
);
4423 * Is the destination valid?
4426 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
4432 send_ipp_status(con
, IPP_NOT_FOUND
,
4433 _("The printer or class was not found."));
4438 * Create the job object...
4441 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
4445 * Save and log the job...
4450 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d created on \"%s\" by \"%s\".",
4451 job
->id
, job
->dest
, job
->username
);
4456 * 'create_requested_array()' - Create an array for the requested-attributes.
4459 static cups_array_t
* /* O - Array of attributes or NULL */
4460 create_requested_array(ipp_t
*request
) /* I - IPP request */
4462 int i
; /* Looping var */
4463 ipp_attribute_t
*requested
; /* requested-attributes attribute */
4464 cups_array_t
*ra
; /* Requested attributes array */
4465 char *value
; /* Current value */
4469 * Get the requested-attributes attribute, and return NULL if we don't
4473 if ((requested
= ippFindAttribute(request
, "requested-attributes",
4474 IPP_TAG_KEYWORD
)) == NULL
)
4478 * If the attribute contains a single "all" keyword, return NULL...
4481 if (requested
->num_values
== 1 &&
4482 !strcmp(requested
->values
[0].string
.text
, "all"))
4486 * Create an array using "strcmp" as the comparison function...
4489 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
4491 for (i
= 0; i
< requested
->num_values
; i
++)
4493 value
= requested
->values
[i
].string
.text
;
4495 if (!strcmp(value
, "job-template"))
4497 cupsArrayAdd(ra
, "copies");
4498 cupsArrayAdd(ra
, "copies-default");
4499 cupsArrayAdd(ra
, "copies-supported");
4500 cupsArrayAdd(ra
, "finishings");
4501 cupsArrayAdd(ra
, "finishings-default");
4502 cupsArrayAdd(ra
, "finishings-supported");
4503 cupsArrayAdd(ra
, "job-hold-until");
4504 cupsArrayAdd(ra
, "job-hold-until-default");
4505 cupsArrayAdd(ra
, "job-hold-until-supported");
4506 cupsArrayAdd(ra
, "job-priority");
4507 cupsArrayAdd(ra
, "job-priority-default");
4508 cupsArrayAdd(ra
, "job-priority-supported");
4509 cupsArrayAdd(ra
, "job-sheets");
4510 cupsArrayAdd(ra
, "job-sheets-default");
4511 cupsArrayAdd(ra
, "job-sheets-supported");
4512 cupsArrayAdd(ra
, "media");
4513 cupsArrayAdd(ra
, "media-default");
4514 cupsArrayAdd(ra
, "media-supported");
4515 cupsArrayAdd(ra
, "multiple-document-handling");
4516 cupsArrayAdd(ra
, "multiple-document-handling-default");
4517 cupsArrayAdd(ra
, "multiple-document-handling-supported");
4518 cupsArrayAdd(ra
, "number-up");
4519 cupsArrayAdd(ra
, "number-up-default");
4520 cupsArrayAdd(ra
, "number-up-supported");
4521 cupsArrayAdd(ra
, "orientation-requested");
4522 cupsArrayAdd(ra
, "orientation-requested-default");
4523 cupsArrayAdd(ra
, "orientation-requested-supported");
4524 cupsArrayAdd(ra
, "page-ranges");
4525 cupsArrayAdd(ra
, "page-ranges-supported");
4526 cupsArrayAdd(ra
, "printer-resolution");
4527 cupsArrayAdd(ra
, "printer-resolution-default");
4528 cupsArrayAdd(ra
, "printer-resolution-supported");
4529 cupsArrayAdd(ra
, "print-quality");
4530 cupsArrayAdd(ra
, "print-quality-default");
4531 cupsArrayAdd(ra
, "print-quality-supported");
4532 cupsArrayAdd(ra
, "sides");
4533 cupsArrayAdd(ra
, "sides-default");
4534 cupsArrayAdd(ra
, "sides-supported");
4536 else if (!strcmp(value
, "job-description"))
4538 cupsArrayAdd(ra
, "date-time-at-completed");
4539 cupsArrayAdd(ra
, "date-time-at-creation");
4540 cupsArrayAdd(ra
, "date-time-at-processing");
4541 cupsArrayAdd(ra
, "job-detailed-status-message");
4542 cupsArrayAdd(ra
, "job-document-access-errors");
4543 cupsArrayAdd(ra
, "job-id");
4544 cupsArrayAdd(ra
, "job-impressions");
4545 cupsArrayAdd(ra
, "job-impressions-completed");
4546 cupsArrayAdd(ra
, "job-k-octets");
4547 cupsArrayAdd(ra
, "job-k-octets-processed");
4548 cupsArrayAdd(ra
, "job-media-sheets");
4549 cupsArrayAdd(ra
, "job-media-sheets-completed");
4550 cupsArrayAdd(ra
, "job-message-from-operator");
4551 cupsArrayAdd(ra
, "job-more-info");
4552 cupsArrayAdd(ra
, "job-name");
4553 cupsArrayAdd(ra
, "job-originating-user-name");
4554 cupsArrayAdd(ra
, "job-printer-up-time");
4555 cupsArrayAdd(ra
, "job-printer-uri");
4556 cupsArrayAdd(ra
, "job-state");
4557 cupsArrayAdd(ra
, "job-state-message");
4558 cupsArrayAdd(ra
, "job-state-reasons");
4559 cupsArrayAdd(ra
, "job-uri");
4560 cupsArrayAdd(ra
, "number-of-documents");
4561 cupsArrayAdd(ra
, "number-of-intervening-jobs");
4562 cupsArrayAdd(ra
, "output-device-assigned");
4563 cupsArrayAdd(ra
, "time-at-completed");
4564 cupsArrayAdd(ra
, "time-at-creation");
4565 cupsArrayAdd(ra
, "time-at-processing");
4567 else if (!strcmp(value
, "printer-description"))
4569 cupsArrayAdd(ra
, "charset-configured");
4570 cupsArrayAdd(ra
, "charset-supported");
4571 cupsArrayAdd(ra
, "color-supported");
4572 cupsArrayAdd(ra
, "compression-supported");
4573 cupsArrayAdd(ra
, "document-format-default");
4574 cupsArrayAdd(ra
, "document-format-supported");
4575 cupsArrayAdd(ra
, "generated-natural-language-supported");
4576 cupsArrayAdd(ra
, "ipp-versions-supported");
4577 cupsArrayAdd(ra
, "job-impressions-supported");
4578 cupsArrayAdd(ra
, "job-k-octets-supported");
4579 cupsArrayAdd(ra
, "job-media-sheets-supported");
4580 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
4581 cupsArrayAdd(ra
, "multiple-operation-time-out");
4582 cupsArrayAdd(ra
, "natural-language-configured");
4583 cupsArrayAdd(ra
, "notify-attributes-supported");
4584 cupsArrayAdd(ra
, "notify-lease-duration-default");
4585 cupsArrayAdd(ra
, "notify-lease-duration-supported");
4586 cupsArrayAdd(ra
, "notify-max-events-supported");
4587 cupsArrayAdd(ra
, "notify-events-default");
4588 cupsArrayAdd(ra
, "notify-events-supported");
4589 cupsArrayAdd(ra
, "notify-pull-method-supported");
4590 cupsArrayAdd(ra
, "notify-schemes-supported");
4591 cupsArrayAdd(ra
, "operations-supported");
4592 cupsArrayAdd(ra
, "pages-per-minute");
4593 cupsArrayAdd(ra
, "pages-per-minute-color");
4594 cupsArrayAdd(ra
, "pdl-override-supported");
4595 cupsArrayAdd(ra
, "printer-current-time");
4596 cupsArrayAdd(ra
, "printer-driver-installer");
4597 cupsArrayAdd(ra
, "printer-info");
4598 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
4599 cupsArrayAdd(ra
, "printer-location");
4600 cupsArrayAdd(ra
, "printer-make-and-model");
4601 cupsArrayAdd(ra
, "printer-message-from-operator");
4602 cupsArrayAdd(ra
, "printer-more-info");
4603 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
4604 cupsArrayAdd(ra
, "printer-name");
4605 cupsArrayAdd(ra
, "printer-state");
4606 cupsArrayAdd(ra
, "printer-state-message");
4607 cupsArrayAdd(ra
, "printer-state-reasons");
4608 cupsArrayAdd(ra
, "printer-up-time");
4609 cupsArrayAdd(ra
, "printer-uri-supported");
4610 cupsArrayAdd(ra
, "queued-job-count");
4611 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
4612 cupsArrayAdd(ra
, "uri-authentication-supported");
4613 cupsArrayAdd(ra
, "uri-security-supported");
4615 else if (!strcmp(value
, "subscription-template"))
4617 cupsArrayAdd(ra
, "notify-attributes");
4618 cupsArrayAdd(ra
, "notify-charset");
4619 cupsArrayAdd(ra
, "notify-events");
4620 cupsArrayAdd(ra
, "notify-lease-duration");
4621 cupsArrayAdd(ra
, "notify-natural-language");
4622 cupsArrayAdd(ra
, "notify-pull-method");
4623 cupsArrayAdd(ra
, "notify-recipient-uri");
4624 cupsArrayAdd(ra
, "notify-time-interval");
4625 cupsArrayAdd(ra
, "notify-user-data");
4628 cupsArrayAdd(ra
, value
);
4636 * 'create_subscription()' - Create a notification subscription.
4640 create_subscription(
4641 cupsd_client_t
*con
, /* I - Client connection */
4642 ipp_attribute_t
*uri
) /* I - Printer URI */
4644 http_status_t status
; /* Policy status */
4645 int i
; /* Looping var */
4646 ipp_attribute_t
*attr
; /* Current attribute */
4647 const char *dest
; /* Destination */
4648 cups_ptype_t dtype
; /* Destination type (printer or class) */
4649 char method
[HTTP_MAX_URI
],
4650 /* Method portion of URI */
4651 userpass
[HTTP_MAX_URI
],
4652 /* Username portion of URI */
4654 /* Host portion of URI */
4655 resource
[HTTP_MAX_URI
];
4656 /* Resource portion of URI */
4657 int port
; /* Port portion of URI */
4658 cupsd_printer_t
*printer
; /* Printer/class */
4659 cupsd_job_t
*job
; /* Job */
4660 int jobid
; /* Job ID */
4661 cupsd_subscription_t
*sub
; /* Subscription object */
4662 const char *username
, /* requesting-user-name or authenticated username */
4663 *recipient
, /* notify-recipient-uri */
4664 *pullmethod
; /* notify-pull-method */
4665 ipp_attribute_t
*user_data
; /* notify-user-data */
4666 int interval
, /* notify-time-interval */
4667 lease
; /* notify-lease-duration */
4668 unsigned mask
; /* notify-events */
4672 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4674 if (attr
->group_tag
!= IPP_TAG_ZERO
)
4675 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
4676 attr
->value_tag
, attr
->name
);
4678 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
4683 * Is the destination valid?
4686 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4687 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
4688 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
4690 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
4691 sizeof(method
), userpass
, sizeof(userpass
), host
,
4692 sizeof(host
), &port
, resource
, sizeof(resource
));
4694 if (!strcmp(resource
, "/"))
4697 dtype
= (cups_ptype_t
)0;
4700 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
4703 dtype
= (cups_ptype_t
)0;
4706 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
4709 dtype
= CUPS_PRINTER_CLASS
;
4712 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
4719 send_ipp_status(con
, IPP_NOT_FOUND
,
4720 _("The printer or class was not found."));
4730 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
4733 send_http_error(con
, status
);
4737 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
4739 send_http_error(con
, status
);
4744 * Get the user that is requesting the subscription...
4747 username
= get_username(con
);
4750 * Find the first subscription group attribute; return if we have
4754 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4755 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
4760 send_ipp_status(con
, IPP_BAD_REQUEST
,
4761 _("No subscription attributes in request!"));
4766 * Process the subscription attributes in the request...
4769 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
4777 lease
= DefaultLeaseDuration
;
4779 mask
= CUPSD_EVENT_NONE
;
4781 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
4783 if (!strcmp(attr
->name
, "notify-recipient") &&
4784 attr
->value_tag
== IPP_TAG_URI
)
4785 recipient
= attr
->values
[0].string
.text
;
4786 else if (!strcmp(attr
->name
, "notify-pull-method") &&
4787 attr
->value_tag
== IPP_TAG_KEYWORD
)
4788 pullmethod
= attr
->values
[0].string
.text
;
4789 else if (!strcmp(attr
->name
, "notify-charset") &&
4790 attr
->value_tag
== IPP_TAG_CHARSET
&&
4791 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
4792 strcmp(attr
->values
[0].string
.text
, "utf-8"))
4794 send_ipp_status(con
, IPP_CHARSET
,
4795 _("Character set \"%s\" not supported!"),
4796 attr
->values
[0].string
.text
);
4799 else if (!strcmp(attr
->name
, "notify-natural-language") &&
4800 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
4801 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
4803 send_ipp_status(con
, IPP_CHARSET
,
4804 _("Language \"%s\" not supported!"),
4805 attr
->values
[0].string
.text
);
4808 else if (!strcmp(attr
->name
, "notify-user-data") &&
4809 attr
->value_tag
== IPP_TAG_STRING
)
4811 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
4813 send_ipp_status(con
, IPP_REQUEST_VALUE
,
4814 _("The notify-user-data value is too large "
4815 "(%d > 63 octets)!"),
4816 attr
->values
[0].unknown
.length
);
4822 else if (!strcmp(attr
->name
, "notify-events") &&
4823 attr
->value_tag
== IPP_TAG_KEYWORD
)
4825 for (i
= 0; i
< attr
->num_values
; i
++)
4826 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
4828 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
4829 attr
->value_tag
== IPP_TAG_INTEGER
)
4830 lease
= attr
->values
[0].integer
;
4831 else if (!strcmp(attr
->name
, "notify-time-interval") &&
4832 attr
->value_tag
== IPP_TAG_INTEGER
)
4833 interval
= attr
->values
[0].integer
;
4834 else if (!strcmp(attr
->name
, "notify-job-id") &&
4835 attr
->value_tag
== IPP_TAG_INTEGER
)
4836 jobid
= attr
->values
[0].integer
;
4842 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
4844 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
4845 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
4846 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
4848 if (!recipient
&& !pullmethod
)
4851 if (mask
== CUPSD_EVENT_NONE
)
4854 mask
= CUPSD_EVENT_JOB_COMPLETED
;
4856 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4859 send_ipp_status(con
, IPP_BAD_REQUEST
,
4860 _("notify-events not specified!"));
4865 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
4867 cupsdLogMessage(CUPSD_LOG_INFO
,
4868 "create_subscription: Limiting notify-lease-duration to "
4871 lease
= MaxLeaseDuration
;
4876 if ((job
= cupsdFindJob(jobid
)) == NULL
)
4878 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
4885 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
4888 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
4891 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4892 "Added subscription %d for printer \"%s\"",
4893 sub
->id
, printer
->name
);
4895 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
4898 sub
->interval
= interval
;
4900 sub
->expire
= lease
? time(NULL
) + lease
: 0;
4902 cupsdSetString(&sub
->owner
, username
);
4906 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
4907 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
4908 sub
->user_data_len
);
4911 ippAddSeparator(con
->response
);
4912 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4913 "notify-subscription-id", sub
->id
);
4915 con
->response
->request
.status
.status_code
= IPP_OK
;
4921 cupsdSaveAllSubscriptions();
4927 * 'delete_printer()' - Remove a printer or class from the system.
4931 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
4932 ipp_attribute_t
*uri
) /* I - URI of printer or class */
4934 http_status_t status
; /* Policy status */
4935 cups_ptype_t dtype
; /* Destination type (printer or class) */
4936 cupsd_printer_t
*printer
; /* Printer/class */
4937 char filename
[1024]; /* Script/PPD filename */
4940 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
4941 con
->http
.fd
, uri
->values
[0].string
.text
);
4944 * Do we have a valid URI?
4947 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
4953 send_ipp_status(con
, IPP_NOT_FOUND
,
4954 _("The printer or class was not found."));
4962 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
4964 send_http_error(con
, status
);
4969 * Remove old jobs...
4972 cupsdCancelJobs(printer
->name
, NULL
, 1);
4975 * Remove old subscriptions and send a "deleted printer" event...
4978 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
4979 "%s \"%s\" deleted by \"%s\".",
4980 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
4981 printer
->name
, get_username(con
));
4983 cupsdExpireSubscriptions(printer
, NULL
);
4986 * Remove any old PPD or script files...
4989 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
4993 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
4997 if (dtype
& CUPS_PRINTER_CLASS
)
4999 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5000 printer
->name
, get_username(con
));
5002 cupsdDeletePrinter(printer
, 0);
5003 cupsdSaveAllClasses();
5007 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5008 printer
->name
, get_username(con
));
5010 cupsdDeletePrinter(printer
, 0);
5011 cupsdSaveAllPrinters();
5014 cupsdWritePrintcap();
5017 * Return with no errors...
5020 con
->response
->request
.status
.status_code
= IPP_OK
;
5025 * 'get_default()' - Get the default destination.
5029 get_default(cupsd_client_t
*con
) /* I - Client connection */
5031 http_status_t status
; /* Policy status */
5032 cups_array_t
*ra
; /* Requested attributes array */
5035 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5041 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5043 send_http_error(con
, status
);
5049 ra
= create_requested_array(con
->request
);
5051 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5053 cupsArrayDelete(ra
);
5055 con
->response
->request
.status
.status_code
= IPP_OK
;
5058 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5063 * 'get_devices()' - Get the list of available devices on the local system.
5067 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5069 http_status_t status
; /* Policy status */
5070 ipp_attribute_t
*limit
, /* Limit attribute */
5071 *requested
; /* requested-attributes attribute */
5072 char command
[1024], /* cups-deviced command */
5073 options
[1024], /* Options to pass to command */
5075 /* String for requested attributes */
5078 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5084 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5086 send_http_error(con
, status
);
5091 * Run cups-deviced command with the given options...
5094 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5095 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5099 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5101 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5103 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5104 snprintf(options
, sizeof(options
),
5106 con
->request
->request
.op
.request_id
,
5107 limit
? limit
->values
[0].integer
: 0, (int)User
,
5110 if (cupsdSendCommand(con
, command
, options
, 1))
5113 * Command started successfully, don't send an IPP response here...
5116 ippDelete(con
->response
);
5117 con
->response
= NULL
;
5122 * Command failed, return "internal error" so the user knows something
5126 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5127 _("cups-deviced failed to execute."));
5133 * 'get_job_attrs()' - Get job attributes.
5137 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5138 ipp_attribute_t
*uri
) /* I - Job URI */
5140 http_status_t status
; /* Policy status */
5141 ipp_attribute_t
*attr
; /* Current attribute */
5142 int jobid
; /* Job ID */
5143 cupsd_job_t
*job
; /* Current job */
5144 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5145 username
[HTTP_MAX_URI
], /* Username portion of URI */
5146 host
[HTTP_MAX_URI
], /* Host portion of URI */
5147 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5148 int port
; /* Port portion of URI */
5149 cups_array_t
*ra
; /* Requested attributes array */
5152 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5153 con
->http
.fd
, uri
->values
[0].string
.text
);
5156 * See if we have a job URI or a printer URI...
5159 if (!strcmp(uri
->name
, "printer-uri"))
5162 * Got a printer URI; see if we also have a job-id attribute...
5165 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5166 IPP_TAG_INTEGER
)) == NULL
)
5168 send_ipp_status(con
, IPP_BAD_REQUEST
,
5169 _("Got a printer-uri attribute but no job-id!"));
5173 jobid
= attr
->values
[0].integer
;
5178 * Got a job URI; parse it to get the job ID...
5181 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5182 sizeof(method
), username
, sizeof(username
), host
,
5183 sizeof(host
), &port
, resource
, sizeof(resource
));
5185 if (strncmp(resource
, "/jobs/", 6))
5191 send_ipp_status(con
, IPP_BAD_REQUEST
,
5192 _("Bad job-uri attribute \"%s\"!"),
5193 uri
->values
[0].string
.text
);
5197 jobid
= atoi(resource
+ 6);
5201 * See if the job exists...
5204 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5207 * Nope - return a "not found" error...
5210 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5218 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5220 send_http_error(con
, status
);
5225 * Copy attributes...
5230 ra
= create_requested_array(con
->request
);
5231 copy_job_attrs(con
, job
, ra
);
5232 cupsArrayDelete(ra
);
5234 con
->response
->request
.status
.status_code
= IPP_OK
;
5239 * 'get_jobs()' - Get a list of jobs for the specified printer.
5243 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
5244 ipp_attribute_t
*uri
) /* I - Printer URI */
5246 http_status_t status
; /* Policy status */
5247 ipp_attribute_t
*attr
; /* Current attribute */
5248 const char *dest
; /* Destination */
5249 cups_ptype_t dtype
; /* Destination type (printer or class) */
5250 cups_ptype_t dmask
; /* Destination type mask */
5251 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
5252 username
[HTTP_MAX_URI
], /* Username portion of URI */
5253 host
[HTTP_MAX_URI
], /* Host portion of URI */
5254 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5255 int port
; /* Port portion of URI */
5256 int completed
; /* Completed jobs? */
5257 int first_job_id
; /* First job ID */
5258 int limit
; /* Maximum number of jobs to return */
5259 int count
; /* Number of jobs that match */
5260 cupsd_job_t
*job
; /* Current job pointer */
5261 cupsd_printer_t
*printer
; /* Printer */
5262 cups_array_t
*list
; /* Which job list... */
5263 cups_array_t
*ra
; /* Requested attributes array */
5266 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
5267 uri
->values
[0].string
.text
);
5270 * Is the destination valid?
5273 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5274 sizeof(scheme
), username
, sizeof(username
), host
,
5275 sizeof(host
), &port
, resource
, sizeof(resource
));
5277 if (!strcmp(resource
, "/") ||
5278 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
5281 dtype
= (cups_ptype_t
)0;
5282 dmask
= (cups_ptype_t
)0;
5285 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5288 dtype
= (cups_ptype_t
)0;
5289 dmask
= CUPS_PRINTER_CLASS
;
5292 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5295 dtype
= CUPS_PRINTER_CLASS
;
5296 dmask
= CUPS_PRINTER_CLASS
;
5299 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
5306 send_ipp_status(con
, IPP_NOT_FOUND
,
5307 _("The printer or class was not found."));
5311 dmask
= CUPS_PRINTER_CLASS
;
5319 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5322 send_http_error(con
, status
);
5326 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5328 send_http_error(con
, status
);
5333 * See if the "which-jobs" attribute have been specified...
5336 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
5337 IPP_TAG_KEYWORD
)) != NULL
&&
5338 !strcmp(attr
->values
[0].string
.text
, "completed"))
5343 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
5355 * See if they want to limit the number of jobs reported...
5358 if ((attr
= ippFindAttribute(con
->request
, "limit",
5359 IPP_TAG_INTEGER
)) != NULL
)
5360 limit
= attr
->values
[0].integer
;
5364 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
5365 IPP_TAG_INTEGER
)) != NULL
)
5366 first_job_id
= attr
->values
[0].integer
;
5371 * See if we only want to see jobs for a specific user...
5374 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
5375 IPP_TAG_BOOLEAN
)) != NULL
&&
5376 attr
->values
[0].boolean
)
5377 strlcpy(username
, get_username(con
), sizeof(username
));
5381 ra
= create_requested_array(con
->request
);
5384 * OK, build a list of jobs for this printer...
5387 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
5388 count
< limit
&& job
;
5389 job
= (cupsd_job_t
*)cupsArrayNext(list
))
5392 * Filter out jobs that don't match...
5395 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
5397 if ((dest
&& strcmp(job
->dest
, dest
)) &&
5398 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
5400 if ((job
->dtype
& dmask
) != dtype
&&
5401 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
5403 if (username
[0] && strcasecmp(username
, job
->username
))
5406 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
5409 if (job
->id
< first_job_id
)
5418 ippAddSeparator(con
->response
);
5422 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
5424 copy_job_attrs(con
, job
, ra
);
5427 cupsArrayDelete(ra
);
5429 con
->response
->request
.status
.status_code
= IPP_OK
;
5434 * 'get_notifications()' - Get events for a subscription.
5438 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
5440 int i
, j
; /* Looping vars */
5441 http_status_t status
; /* Policy status */
5442 cupsd_subscription_t
*sub
; /* Subscription */
5443 ipp_attribute_t
*ids
, /* notify-subscription-ids */
5444 *sequences
; /* notify-sequence-numbers */
5445 int min_seq
; /* Minimum sequence number */
5446 int interval
; /* Poll interval */
5449 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_subscription_attrs(con=%p[%d])",
5453 * Get subscription attributes...
5456 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
5458 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
5463 send_ipp_status(con
, IPP_BAD_REQUEST
,
5464 _("Missing notify-subscription-ids attribute!"));
5469 * Are the subscription IDs valid?
5472 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
5474 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
5477 * Bad subscription ID...
5480 send_ipp_status(con
, IPP_NOT_FOUND
,
5481 _("notify-subscription-id %d no good!"),
5482 ids
->values
[i
].integer
);
5490 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5492 con
, sub
->owner
)) != HTTP_OK
)
5494 send_http_error(con
, status
);
5499 * Check the subscription type and update the interval accordingly.
5502 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
5505 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
5507 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
5513 * Tell the client to poll again in N seconds...
5517 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5518 "notify-get-interval", interval
);
5520 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5521 "printer-up-time", time(NULL
));
5524 * Copy the subscription event attributes to the response.
5527 con
->response
->request
.status
.status_code
=
5528 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
5530 for (i
= 0; i
< ids
->num_values
; i
++)
5533 * Get the subscription and sequence number...
5536 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
5538 if (sequences
&& i
< sequences
->num_values
)
5539 min_seq
= sequences
->values
[i
].integer
;
5544 * If we don't have any new events, nothing to do here...
5547 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
5551 * Otherwise copy all of the new events...
5554 if (sub
->first_event_id
> min_seq
)
5557 j
= min_seq
- sub
->first_event_id
;
5559 for (; j
< sub
->num_events
; j
++)
5561 ippAddSeparator(con
->response
);
5563 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
5564 IPP_TAG_EVENT_NOTIFICATION
, 0);
5571 * 'get_ppds()' - Get the list of PPD files on the local system.
5575 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
5577 http_status_t status
; /* Policy status */
5578 ipp_attribute_t
*limit
, /* Limit attribute */
5579 *make
, /* ppd-make attribute */
5580 *requested
; /* requested-attributes attribute */
5581 char command
[1024], /* cups-deviced command */
5582 options
[1024], /* Options to pass to command */
5584 /* String for requested attributes */
5585 make_str
[256]; /* Escaped ppd-make string */
5588 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
5594 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5596 send_http_error(con
, status
);
5601 * Run cups-driverd command with the given options...
5604 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5605 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
5606 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5610 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5612 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5615 url_encode_attr(make
, make_str
, sizeof(make_str
));
5619 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
5620 snprintf(options
, sizeof(options
), "list+%d+%d+%s%s%s",
5621 con
->request
->request
.op
.request_id
,
5622 limit
? limit
->values
[0].integer
: 0,
5623 requested_str
, make
? "%20" : "", make_str
);
5625 if (cupsdSendCommand(con
, command
, options
, 0))
5628 * Command started successfully, don't send an IPP response here...
5631 ippDelete(con
->response
);
5632 con
->response
= NULL
;
5637 * Command failed, return "internal error" so the user knows something
5641 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5642 _("cups-driverd failed to execute."));
5648 * 'get_printer_attrs()' - Get printer attributes.
5652 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
5653 ipp_attribute_t
*uri
) /* I - Printer URI */
5655 http_status_t status
; /* Policy status */
5656 cups_ptype_t dtype
; /* Destination type (printer or class) */
5657 cupsd_printer_t
*printer
; /* Printer/class */
5658 cups_array_t
*ra
; /* Requested attributes array */
5661 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
5662 con
->http
.fd
, uri
->values
[0].string
.text
);
5665 * Is the destination valid?
5668 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5674 send_ipp_status(con
, IPP_NOT_FOUND
,
5675 _("The printer or class was not found."));
5683 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
5685 send_http_error(con
, status
);
5690 * Send the attributes...
5693 ra
= create_requested_array(con
->request
);
5695 copy_printer_attrs(con
, printer
, ra
);
5697 cupsArrayDelete(ra
);
5699 con
->response
->request
.status
.status_code
= IPP_OK
;
5704 * 'get_printers()' - Get a list of printers or classes.
5708 get_printers(cupsd_client_t
*con
, /* I - Client connection */
5709 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
5711 http_status_t status
; /* Policy status */
5712 ipp_attribute_t
*attr
; /* Current attribute */
5713 int limit
; /* Maximum number of printers to return */
5714 int count
; /* Number of printers that match */
5715 cupsd_printer_t
*printer
; /* Current printer pointer */
5716 int printer_type
, /* printer-type attribute */
5717 printer_mask
; /* printer-type-mask attribute */
5718 char *location
; /* Location string */
5719 const char *username
; /* Current user */
5720 char *first_printer_name
; /* first-printer-name attribute */
5721 cups_array_t
*ra
; /* Requested attributes array */
5724 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
5725 con
->http
.fd
, type
);
5731 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5733 send_http_error(con
, status
);
5738 * Check for printers...
5741 if (!Printers
|| !cupsArrayCount(Printers
))
5743 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
5748 * See if they want to limit the number of printers reported...
5751 if ((attr
= ippFindAttribute(con
->request
, "limit",
5752 IPP_TAG_INTEGER
)) != NULL
)
5753 limit
= attr
->values
[0].integer
;
5757 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
5758 IPP_TAG_NAME
)) != NULL
)
5759 first_printer_name
= attr
->values
[0].string
.text
;
5761 first_printer_name
= NULL
;
5764 * Support filtering...
5767 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
5768 IPP_TAG_ENUM
)) != NULL
)
5769 printer_type
= attr
->values
[0].integer
;
5773 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
5774 IPP_TAG_ENUM
)) != NULL
)
5775 printer_mask
= attr
->values
[0].integer
;
5779 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
5780 IPP_TAG_TEXT
)) != NULL
)
5781 location
= attr
->values
[0].string
.text
;
5785 if (con
->username
[0])
5786 username
= con
->username
;
5787 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
5788 IPP_TAG_NAME
)) != NULL
)
5789 username
= attr
->values
[0].string
.text
;
5793 ra
= create_requested_array(con
->request
);
5796 * OK, build a list of printers for this printer...
5799 if (first_printer_name
)
5801 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
5802 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5805 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5808 count
< limit
&& printer
;
5809 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
5811 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
5812 (printer
->type
& printer_mask
) == printer_type
&&
5813 (!location
|| !printer
->location
||
5814 !strcasecmp(printer
->location
, location
)))
5817 * If HideImplicitMembers is enabled, see if this printer or class
5818 * is a member of an implicit class...
5821 if (ImplicitClasses
&& HideImplicitMembers
&&
5822 printer
->in_implicit_class
)
5826 * If a username is specified, see if it is allowed or denied
5830 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
5834 * Add the group separator as needed...
5838 ippAddSeparator(con
->response
);
5843 * Send the attributes...
5846 copy_printer_attrs(con
, printer
, ra
);
5850 cupsArrayDelete(ra
);
5852 con
->response
->request
.status
.status_code
= IPP_OK
;
5857 * 'get_subscription_attrs()' - Get subscription attributes.
5861 get_subscription_attrs(
5862 cupsd_client_t
*con
, /* I - Client connection */
5863 int sub_id
) /* I - Subscription ID */
5865 http_status_t status
; /* Policy status */
5866 cupsd_subscription_t
*sub
; /* Subscription */
5867 cups_array_t
*ra
; /* Requested attributes array */
5870 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5871 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
5872 con
, con
->http
.fd
, sub_id
);
5875 * Is the subscription ID valid?
5878 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
5881 * Bad subscription ID...
5884 send_ipp_status(con
, IPP_NOT_FOUND
,
5885 _("notify-subscription-id %d no good!"), sub_id
);
5893 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5895 con
, sub
->owner
)) != HTTP_OK
)
5897 send_http_error(con
, status
);
5902 * Copy the subscription attributes to the response using the
5903 * requested-attributes attribute that may be provided by the client.
5906 ra
= create_requested_array(con
->request
);
5908 copy_subscription_attrs(con
, sub
, ra
);
5910 cupsArrayDelete(ra
);
5912 con
->response
->request
.status
.status_code
= IPP_OK
;
5917 * 'get_subscriptions()' - Get subscriptions.
5921 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
5922 ipp_attribute_t
*uri
) /* I - Printer/job URI */
5924 http_status_t status
; /* Policy status */
5925 int count
; /* Number of subscriptions */
5926 int limit
; /* Limit */
5927 cupsd_subscription_t
*sub
; /* Subscription */
5928 cups_array_t
*ra
; /* Requested attributes array */
5929 ipp_attribute_t
*attr
; /* Attribute */
5930 cups_ptype_t dtype
; /* Destination type (printer or class) */
5931 char scheme
[HTTP_MAX_URI
],
5932 /* Scheme portion of URI */
5933 username
[HTTP_MAX_URI
],
5934 /* Username portion of URI */
5936 /* Host portion of URI */
5937 resource
[HTTP_MAX_URI
];
5938 /* Resource portion of URI */
5939 int port
; /* Port portion of URI */
5940 cupsd_job_t
*job
; /* Job pointer */
5941 cupsd_printer_t
*printer
; /* Printer */
5944 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5945 "get_subscriptions(con=%p[%d], uri=%s)",
5946 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5949 * Is the destination valid?
5952 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5953 sizeof(scheme
), username
, sizeof(username
), host
,
5954 sizeof(host
), &port
, resource
, sizeof(resource
));
5956 if (!strcmp(resource
, "/") ||
5957 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
5958 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
5959 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
5964 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
5967 job
= cupsdFindJob(atoi(resource
+ 6));
5971 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
5976 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5982 send_ipp_status(con
, IPP_NOT_FOUND
,
5983 _("The printer or class was not found."));
5986 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
5987 IPP_TAG_INTEGER
)) != NULL
)
5989 job
= cupsdFindJob(attr
->values
[0].integer
);
5993 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
5994 attr
->values
[0].integer
);
6005 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
6007 con
, NULL
)) != HTTP_OK
)
6009 send_http_error(con
, status
);
6014 * Copy the subscription attributes to the response using the
6015 * requested-attributes attribute that may be provided by the client.
6018 ra
= create_requested_array(con
->request
);
6020 if ((attr
= ippFindAttribute(con
->request
, "limit",
6021 IPP_TAG_INTEGER
)) != NULL
)
6022 limit
= attr
->values
[0].integer
;
6027 * See if we only want to see subscriptions for a specific user...
6030 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
6031 IPP_TAG_BOOLEAN
)) != NULL
&&
6032 attr
->values
[0].boolean
)
6033 strlcpy(username
, get_username(con
), sizeof(username
));
6037 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
6039 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
6040 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
6041 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
6043 ippAddSeparator(con
->response
);
6044 copy_subscription_attrs(con
, sub
, ra
);
6047 if (limit
&& count
>= limit
)
6051 cupsArrayDelete(ra
);
6054 con
->response
->request
.status
.status_code
= IPP_OK
;
6056 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
6061 * 'get_username()' - Get the username associated with a request.
6064 static const char * /* O - Username */
6065 get_username(cupsd_client_t
*con
) /* I - Connection */
6067 ipp_attribute_t
*attr
; /* Attribute */
6070 if (con
->username
[0])
6071 return (con
->username
);
6072 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6073 IPP_TAG_NAME
)) != NULL
)
6074 return (attr
->values
[0].string
.text
);
6076 return ("anonymous");
6081 * 'hold_job()' - Hold a print job.
6085 hold_job(cupsd_client_t
*con
, /* I - Client connection */
6086 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
6088 ipp_attribute_t
*attr
, /* Current job-hold-until */
6089 *newattr
; /* New job-hold-until */
6090 int jobid
; /* Job ID */
6091 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6092 username
[HTTP_MAX_URI
], /* Username portion of URI */
6093 host
[HTTP_MAX_URI
], /* Host portion of URI */
6094 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6095 int port
; /* Port portion of URI */
6096 cupsd_job_t
*job
; /* Job information */
6099 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
6100 uri
->values
[0].string
.text
);
6103 * See if we have a job URI or a printer URI...
6106 if (!strcmp(uri
->name
, "printer-uri"))
6109 * Got a printer URI; see if we also have a job-id attribute...
6112 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6113 IPP_TAG_INTEGER
)) == NULL
)
6115 send_ipp_status(con
, IPP_BAD_REQUEST
,
6116 _("Got a printer-uri attribute but no job-id!"));
6120 jobid
= attr
->values
[0].integer
;
6125 * Got a job URI; parse it to get the job ID...
6128 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6129 sizeof(method
), username
, sizeof(username
), host
,
6130 sizeof(host
), &port
, resource
, sizeof(resource
));
6132 if (strncmp(resource
, "/jobs/", 6))
6138 send_ipp_status(con
, IPP_BAD_REQUEST
,
6139 _("Bad job-uri attribute \"%s\"!"),
6140 uri
->values
[0].string
.text
);
6144 jobid
= atoi(resource
+ 6);
6148 * See if the job exists...
6151 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6154 * Nope - return a "not found" error...
6157 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6162 * See if the job is owned by the requesting user...
6165 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6167 send_http_error(con
, HTTP_UNAUTHORIZED
);
6172 * Hold the job and return...
6177 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
6178 IPP_TAG_KEYWORD
)) == NULL
)
6179 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
6181 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
6182 IPP_TAG_KEYWORD
)) == NULL
)
6183 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
6188 * Free the old hold value and copy the new one over...
6191 _cupsStrFree(attr
->values
[0].string
.text
);
6195 attr
->value_tag
= newattr
->value_tag
;
6196 attr
->values
[0].string
.text
=
6197 _cupsStrAlloc(newattr
->values
[0].string
.text
);
6201 attr
->value_tag
= IPP_TAG_KEYWORD
;
6202 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
6206 * Hold job until specified time...
6209 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
6212 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was held by \"%s\".", jobid
,
6215 con
->response
->request
.status
.status_code
= IPP_OK
;
6220 * 'move_job()' - Move a job to a new destination.
6224 move_job(cupsd_client_t
*con
, /* I - Client connection */
6225 ipp_attribute_t
*uri
) /* I - Job URI */
6227 http_status_t status
; /* Policy status */
6228 ipp_attribute_t
*attr
; /* Current attribute */
6229 int jobid
; /* Job ID */
6230 cupsd_job_t
*job
; /* Current job */
6231 const char *src
, /* Source printer/class */
6232 *dest
; /* Destination */
6233 cups_ptype_t stype
, /* Source type (printer or class) */
6234 dtype
; /* Destination type (printer or class) */
6235 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6236 username
[HTTP_MAX_URI
], /* Username portion of URI */
6237 host
[HTTP_MAX_URI
], /* Host portion of URI */
6238 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6239 int port
; /* Port portion of URI */
6240 cupsd_printer_t
*sprinter
, /* Source printer */
6241 *dprinter
; /* Destination printer */
6244 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
6245 uri
->values
[0].string
.text
);
6248 * Get the new printer or class...
6251 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
6252 IPP_TAG_URI
)) == NULL
)
6255 * Need job-printer-uri...
6258 send_ipp_status(con
, IPP_BAD_REQUEST
,
6259 _("job-printer-uri attribute missing!"));
6263 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
,
6264 &dprinter
)) == NULL
)
6270 send_ipp_status(con
, IPP_NOT_FOUND
,
6271 _("The printer or class was not found."));
6279 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
6282 send_http_error(con
, status
);
6287 * See if we have a job URI or a printer URI...
6290 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6291 sizeof(scheme
), username
, sizeof(username
), host
,
6292 sizeof(host
), &port
, resource
, sizeof(resource
));
6294 if (!strcmp(uri
->name
, "printer-uri"))
6297 * Got a printer URI; see if we also have a job-id attribute...
6300 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6301 IPP_TAG_INTEGER
)) == NULL
)
6307 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
6308 &sprinter
)) == NULL
)
6314 send_ipp_status(con
, IPP_NOT_FOUND
,
6315 _("The printer or class was not found."));
6324 * Otherwise, just move a single job...
6327 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
6330 * Nope - return a "not found" error...
6333 send_ipp_status(con
, IPP_NOT_FOUND
,
6334 _("Job #%d does not exist!"), attr
->values
[0].integer
);
6340 * Job found, initialize source pointers...
6351 * Got a job URI; parse it to get the job ID...
6354 if (strncmp(resource
, "/jobs/", 6))
6360 send_ipp_status(con
, IPP_BAD_REQUEST
,
6361 _("Bad job-uri attribute \"%s\"!"),
6362 uri
->values
[0].string
.text
);
6367 * See if the job exists...
6370 jobid
= atoi(resource
+ 6);
6372 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6375 * Nope - return a "not found" error...
6378 send_ipp_status(con
, IPP_NOT_FOUND
,
6379 _("Job #%d does not exist!"), jobid
);
6385 * Job found, initialize source pointers...
6394 * Now move the job or jobs...
6400 * See if the job has been completed...
6403 if (job
->state_value
> IPP_JOB_STOPPED
)
6406 * Return a "not-possible" error...
6409 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6410 _("Job #%d is finished and cannot be altered!"),
6416 * See if the job is owned by the requesting user...
6419 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6421 send_http_error(con
, HTTP_UNAUTHORIZED
);
6426 * Move the job to a different printer or class...
6429 cupsdMoveJob(job
, dprinter
);
6434 * Got the source printer, now look through the jobs...
6437 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
6439 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
6442 * See if the job is pointing at the source printer or has not been
6446 if (strcasecmp(job
->dest
, src
) ||
6447 job
->state_value
> IPP_JOB_STOPPED
)
6451 * See if the job can be moved by the requesting user...
6454 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6458 * Move the job to a different printer or class...
6461 cupsdMoveJob(job
, dprinter
);
6466 * Start jobs if possible...
6472 * Return with "everything is OK" status...
6475 con
->response
->request
.status
.status_code
= IPP_OK
;
6480 * 'ppd_add_default()' - Add a PPD default choice.
6483 static int /* O - Number of defaults */
6485 const char *option
, /* I - Option name */
6486 const char *choice
, /* I - Choice name */
6487 int num_defaults
, /* I - Number of defaults */
6488 ppd_default_t
**defaults
) /* IO - Defaults */
6490 int i
; /* Looping var */
6491 ppd_default_t
*temp
; /* Temporary defaults array */
6495 * First check if the option already has a default value; the PPD spec
6496 * says that the first one is used...
6499 for (i
= 0, temp
= *defaults
; i
< num_defaults
; i
++)
6500 if (!strcmp(option
, temp
[i
].option
))
6501 return (num_defaults
);
6504 * Now add the option...
6507 if (num_defaults
== 0)
6508 temp
= malloc(sizeof(ppd_default_t
));
6510 temp
= realloc(*defaults
, (num_defaults
+ 1) * sizeof(ppd_default_t
));
6514 cupsdLogMessage(CUPSD_LOG_ERROR
, "ppd_add_default: Unable to add default value for \"%s\" - %s",
6515 option
, strerror(errno
));
6516 return (num_defaults
);
6520 temp
+= num_defaults
;
6522 strlcpy(temp
->option
, option
, sizeof(temp
->option
));
6523 strlcpy(temp
->choice
, choice
, sizeof(temp
->choice
));
6525 return (num_defaults
+ 1);
6530 * 'ppd_parse_line()' - Parse a PPD default line.
6533 static int /* O - 0 on success, -1 on failure */
6534 ppd_parse_line(const char *line
, /* I - Line */
6535 char *option
, /* O - Option name */
6536 int olen
, /* I - Size of option name */
6537 char *choice
, /* O - Choice name */
6538 int clen
) /* I - Size of choice name */
6541 * Verify this is a default option line...
6544 if (strncmp(line
, "*Default", 8))
6548 * Read the option name...
6551 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
6561 * Skip everything else up to the colon (:)...
6564 while (*line
&& *line
!= ':')
6573 * Now grab the option choice, skipping leading whitespace...
6576 while (isspace(*line
& 255))
6579 for (clen
--; isalnum(*line
& 255); line
++)
6589 * Return with no errors...
6597 * 'print_job()' - Print a file to a printer or class.
6601 print_job(cupsd_client_t
*con
, /* I - Client connection */
6602 ipp_attribute_t
*uri
) /* I - Printer URI */
6604 ipp_attribute_t
*attr
; /* Current attribute */
6605 ipp_attribute_t
*format
; /* Document-format attribute */
6606 const char *default_format
; /* document-format-default value */
6607 cupsd_job_t
*job
; /* New job */
6608 char filename
[1024]; /* Job filename */
6609 mime_type_t
*filetype
; /* Type of file */
6610 char super
[MIME_MAX_SUPER
], /* Supertype of file */
6611 type
[MIME_MAX_TYPE
], /* Subtype of file */
6612 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
6613 /* Textual name of mime type */
6614 cupsd_printer_t
*printer
; /* Printer data */
6615 struct stat fileinfo
; /* File information */
6616 int kbytes
; /* Size of file */
6617 int compression
; /* Document compression */
6620 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
6621 uri
->values
[0].string
.text
);
6624 * Validate print file attributes, for now just document-format and
6625 * compression (CUPS only supports "none" and "gzip")...
6628 compression
= CUPS_FILE_NONE
;
6630 if ((attr
= ippFindAttribute(con
->request
, "compression",
6631 IPP_TAG_KEYWORD
)) != NULL
)
6633 if (strcmp(attr
->values
[0].string
.text
, "none")
6635 && strcmp(attr
->values
[0].string
.text
, "gzip")
6636 #endif /* HAVE_LIBZ */
6639 send_ipp_status(con
, IPP_ATTRIBUTES
,
6640 _("Unsupported compression \"%s\"!"),
6641 attr
->values
[0].string
.text
);
6642 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6643 "compression", NULL
, attr
->values
[0].string
.text
);
6648 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
6649 compression
= CUPS_FILE_GZIP
;
6650 #endif /* HAVE_LIBZ */
6654 * Do we have a file to print?
6659 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
6664 * Is the destination valid?
6667 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
6673 send_ipp_status(con
, IPP_NOT_FOUND
,
6674 _("The printer or class was not found."));
6679 * Is it a format we support?
6682 if ((format
= ippFindAttribute(con
->request
, "document-format",
6683 IPP_TAG_MIMETYPE
)) != NULL
)
6686 * Grab format from client...
6689 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
6692 send_ipp_status(con
, IPP_BAD_REQUEST
,
6693 _("Could not scan type \"%s\"!"),
6694 format
->values
[0].string
.text
);
6698 else if ((default_format
= cupsGetOption("document-format",
6699 printer
->num_options
,
6700 printer
->options
)) != NULL
)
6703 * Use default document format...
6706 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
6708 send_ipp_status(con
, IPP_BAD_REQUEST
,
6709 _("Could not scan type \"%s\"!"),
6720 strcpy(super
, "application");
6721 strcpy(type
, "octet-stream");
6724 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
6727 * Auto-type the file...
6730 ipp_attribute_t
*doc_name
; /* document-name attribute */
6733 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
6735 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
6736 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
6737 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
6741 filetype
= mimeType(MimeDatabase
, super
, type
);
6744 filetype
= mimeType(MimeDatabase
, super
, type
);
6748 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
6751 * Replace the document-format attribute value with the auto-typed or
6755 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
6760 _cupsStrFree(format
->values
[0].string
.text
);
6762 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
6765 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
6766 "document-format", NULL
, mimetype
);
6770 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
6771 _("Unsupported format \'%s/%s\'!"), super
, type
);
6772 cupsdLogMessage(CUPSD_LOG_INFO
,
6773 "Hint: Do you have the raw file printing rules enabled?");
6776 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
6777 "document-format", NULL
, format
->values
[0].string
.text
);
6782 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: request file type is %s/%s.",
6783 filetype
->super
, filetype
->type
);
6786 * Read any embedded job ticket info from PS files...
6789 if (!strcasecmp(filetype
->super
, "application") &&
6790 !strcasecmp(filetype
->type
, "postscript"))
6791 read_ps_job_ticket(con
);
6794 * Create the job object...
6797 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
6801 * Update quota data...
6804 if (stat(con
->filename
, &fileinfo
))
6807 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
6809 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6811 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
6812 IPP_TAG_INTEGER
)) != NULL
)
6813 attr
->values
[0].integer
+= kbytes
;
6816 * Add the job file...
6819 if (add_file(con
, job
, filetype
, compression
))
6822 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
6824 rename(con
->filename
, filename
);
6825 cupsdClearString(&con
->filename
);
6828 * See if we need to add the ending sheet...
6831 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
6833 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
6834 attr
&& attr
->num_values
> 1)
6840 cupsdLogMessage(CUPSD_LOG_INFO
, "Adding end banner page \"%s\" to job %d.",
6841 attr
->values
[1].string
.text
, job
->id
);
6843 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
6845 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6849 * Log and save the job...
6852 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d queued on \"%s\" by \"%s\".", job
->id
,
6853 job
->dest
, job
->username
);
6854 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Job %d hold_until = %d", job
->id
,
6855 (int)job
->hold_until
);
6860 * Start the job if possible...
6868 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
6870 * This function only gets called when printing a single PostScript
6871 * file using the Print-Job operation. It doesn't work for Create-Job +
6872 * Send-File, since the job attributes need to be set at job creation
6873 * time for banners to work. The embedded PS job ticket stuff is here
6874 * only to allow the Windows printer driver for CUPS to pass in JCL
6875 * options and IPP attributes which otherwise would be lost.
6877 * The format of a PS job ticket is simple:
6879 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
6881 * %cupsJobTicket: attr1=value1
6882 * %cupsJobTicket: attr2=value2
6884 * %cupsJobTicket: attrN=valueN
6886 * Job ticket lines must appear immediately after the first line that
6887 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
6888 * looking for job ticket info when it finds a line that does not begin
6889 * with "%cupsJobTicket:".
6891 * The maximum length of a job ticket line, including the prefix, is
6892 * 255 characters to conform with the Adobe DSC.
6894 * Read-only attributes are rejected with a notice to the error log in
6895 * case a malicious user tries anything. Since the job ticket is read
6896 * prior to attribute validation in print_job(), job ticket attributes
6897 * will go through the same validation as IPP attributes...
6901 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
6903 cups_file_t
*fp
; /* File to read from */
6904 char line
[256]; /* Line data */
6905 int num_options
; /* Number of options */
6906 cups_option_t
*options
; /* Options */
6907 ipp_t
*ticket
; /* New attributes */
6908 ipp_attribute_t
*attr
, /* Current attribute */
6909 *attr2
, /* Job attribute */
6910 *prev2
; /* Previous job attribute */
6914 * First open the print file...
6917 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
6919 cupsdLogMessage(CUPSD_LOG_ERROR
,
6920 "read_ps_job_ticket: Unable to open PostScript print file "
6927 * Skip the first line...
6930 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
6932 cupsdLogMessage(CUPSD_LOG_ERROR
,
6933 "read_ps_job_ticket: Unable to read from PostScript print "
6940 if (strncmp(line
, "%!PS-Adobe-", 11))
6943 * Not a DSC-compliant file, so no job ticket info will be available...
6951 * Read job ticket info from the file...
6957 while (cupsFileGets(fp
, line
, sizeof(line
)))
6960 * Stop at the first non-ticket line...
6963 if (strncmp(line
, "%cupsJobTicket:", 15))
6967 * Add the options to the option array...
6970 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
6974 * Done with the file; see if we have any options...
6979 if (num_options
== 0)
6983 * OK, convert the options to an attribute list, and apply them to
6988 cupsEncodeOptions(ticket
, num_options
, options
);
6991 * See what the user wants to change.
6994 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
6996 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
6999 if (!strcmp(attr
->name
, "job-originating-host-name") ||
7000 !strcmp(attr
->name
, "job-originating-user-name") ||
7001 !strcmp(attr
->name
, "job-media-sheets-completed") ||
7002 !strcmp(attr
->name
, "job-k-octets") ||
7003 !strcmp(attr
->name
, "job-id") ||
7004 !strncmp(attr
->name
, "job-state", 9) ||
7005 !strncmp(attr
->name
, "time-at-", 8))
7006 continue; /* Read-only attrs */
7008 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
7009 IPP_TAG_ZERO
)) != NULL
)
7012 * Some other value; first free the old value...
7015 if (con
->request
->attrs
== attr2
)
7017 con
->request
->attrs
= attr2
->next
;
7022 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
7023 if (prev2
->next
== attr2
)
7025 prev2
->next
= attr2
->next
;
7030 if (con
->request
->last
== attr2
)
7031 con
->request
->last
= prev2
;
7033 _ippFreeAttr(attr2
);
7037 * Add new option by copying it...
7040 copy_attribute(con
->request
, attr
, 0);
7044 * Then free the attribute list and option array...
7048 cupsFreeOptions(num_options
, options
);
7053 * 'reject_jobs()' - Reject print jobs to a printer.
7057 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
7058 ipp_attribute_t
*uri
) /* I - Printer or class URI */
7060 http_status_t status
; /* Policy status */
7061 cups_ptype_t dtype
; /* Destination type (printer or class) */
7062 cupsd_printer_t
*printer
; /* Printer data */
7063 ipp_attribute_t
*attr
; /* printer-state-message text */
7066 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
7067 con
->http
.fd
, uri
->values
[0].string
.text
);
7070 * Is the destination valid?
7073 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7079 send_ipp_status(con
, IPP_NOT_FOUND
,
7080 _("The printer or class was not found."));
7088 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7090 send_http_error(con
, status
);
7095 * Reject jobs sent to the printer...
7098 printer
->accepting
= 0;
7100 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
7101 IPP_TAG_TEXT
)) == NULL
)
7102 strcpy(printer
->state_message
, "Rejecting Jobs");
7104 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
7105 sizeof(printer
->state_message
));
7107 cupsdAddPrinterHistory(printer
);
7109 if (dtype
& CUPS_PRINTER_CLASS
)
7111 cupsdSaveAllClasses();
7113 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
7114 printer
->name
, get_username(con
));
7118 cupsdSaveAllPrinters();
7120 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
7121 printer
->name
, get_username(con
));
7125 * Everything was ok, so return OK status...
7128 con
->response
->request
.status
.status_code
= IPP_OK
;
7133 * 'release_job()' - Release a held print job.
7137 release_job(cupsd_client_t
*con
, /* I - Client connection */
7138 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7140 ipp_attribute_t
*attr
; /* Current attribute */
7141 int jobid
; /* Job ID */
7142 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7143 username
[HTTP_MAX_URI
], /* Username portion of URI */
7144 host
[HTTP_MAX_URI
], /* Host portion of URI */
7145 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7146 int port
; /* Port portion of URI */
7147 cupsd_job_t
*job
; /* Job information */
7150 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
7151 con
->http
.fd
, uri
->values
[0].string
.text
);
7154 * See if we have a job URI or a printer URI...
7157 if (!strcmp(uri
->name
, "printer-uri"))
7160 * Got a printer URI; see if we also have a job-id attribute...
7163 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7164 IPP_TAG_INTEGER
)) == NULL
)
7166 send_ipp_status(con
, IPP_BAD_REQUEST
,
7167 _("Got a printer-uri attribute but no job-id!"));
7171 jobid
= attr
->values
[0].integer
;
7176 * Got a job URI; parse it to get the job ID...
7179 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7180 sizeof(method
), username
, sizeof(username
), host
,
7181 sizeof(host
), &port
, resource
, sizeof(resource
));
7183 if (strncmp(resource
, "/jobs/", 6))
7189 send_ipp_status(con
, IPP_BAD_REQUEST
,
7190 _("Bad job-uri attribute \"%s\"!"),
7191 uri
->values
[0].string
.text
);
7195 jobid
= atoi(resource
+ 6);
7199 * See if the job exists...
7202 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7205 * Nope - return a "not found" error...
7208 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7213 * See if job is "held"...
7216 if (job
->state_value
!= IPP_JOB_HELD
)
7219 * Nope - return a "not possible" error...
7222 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
7227 * See if the job is owned by the requesting user...
7230 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7232 send_http_error(con
, HTTP_UNAUTHORIZED
);
7237 * Reset the job-hold-until value to "no-hold"...
7240 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7241 IPP_TAG_KEYWORD
)) == NULL
)
7242 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7246 _cupsStrFree(attr
->values
[0].string
.text
);
7248 attr
->value_tag
= IPP_TAG_KEYWORD
;
7249 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
7253 * Release the job and return...
7256 cupsdReleaseJob(job
);
7258 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was released by \"%s\".", jobid
,
7261 con
->response
->request
.status
.status_code
= IPP_OK
;
7266 * 'renew_subscription()' - Renew an existing subscription...
7271 cupsd_client_t
*con
, /* I - Client connection */
7272 int sub_id
) /* I - Subscription ID */
7274 http_status_t status
; /* Policy status */
7275 cupsd_subscription_t
*sub
; /* Subscription */
7276 ipp_attribute_t
*lease
; /* notify-lease-duration */
7279 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7280 "renew_subscription(con=%p[%d], sub_id=%d)",
7281 con
, con
->http
.fd
, sub_id
);
7284 * Is the subscription ID valid?
7287 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7290 * Bad subscription ID...
7293 send_ipp_status(con
, IPP_NOT_FOUND
,
7294 _("notify-subscription-id %d no good!"), sub_id
);
7301 * Job subscriptions cannot be renewed...
7304 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7305 _("Job subscriptions cannot be renewed!"));
7313 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7315 con
, sub
->owner
)) != HTTP_OK
)
7317 send_http_error(con
, status
);
7322 * Renew the subscription...
7325 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
7328 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
7330 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
7332 cupsdLogMessage(CUPSD_LOG_INFO
,
7333 "renew_subscription: Limiting notify-lease-duration to "
7336 sub
->lease
= MaxLeaseDuration
;
7339 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
7341 cupsdSaveAllSubscriptions();
7343 con
->response
->request
.status
.status_code
= IPP_OK
;
7348 * 'restart_job()' - Restart an old print job.
7352 restart_job(cupsd_client_t
*con
, /* I - Client connection */
7353 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7355 ipp_attribute_t
*attr
; /* Current attribute */
7356 int jobid
; /* Job ID */
7357 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7358 username
[HTTP_MAX_URI
], /* Username portion of URI */
7359 host
[HTTP_MAX_URI
], /* Host portion of URI */
7360 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7361 int port
; /* Port portion of URI */
7362 cupsd_job_t
*job
; /* Job information */
7365 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
7366 con
->http
.fd
, uri
->values
[0].string
.text
);
7369 * See if we have a job URI or a printer URI...
7372 if (!strcmp(uri
->name
, "printer-uri"))
7375 * Got a printer URI; see if we also have a job-id attribute...
7378 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7379 IPP_TAG_INTEGER
)) == NULL
)
7381 send_ipp_status(con
, IPP_BAD_REQUEST
,
7382 _("Got a printer-uri attribute but no job-id!"));
7386 jobid
= attr
->values
[0].integer
;
7391 * Got a job URI; parse it to get the job ID...
7394 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7395 sizeof(method
), username
, sizeof(username
), host
,
7396 sizeof(host
), &port
, resource
, sizeof(resource
));
7398 if (strncmp(resource
, "/jobs/", 6))
7404 send_ipp_status(con
, IPP_BAD_REQUEST
,
7405 _("Bad job-uri attribute \"%s\"!"),
7406 uri
->values
[0].string
.text
);
7410 jobid
= atoi(resource
+ 6);
7414 * See if the job exists...
7417 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7420 * Nope - return a "not found" error...
7423 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7428 * See if job is in any of the "completed" states...
7431 if (job
->state_value
<= IPP_JOB_PROCESSING
)
7434 * Nope - return a "not possible" error...
7437 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
7443 * See if we have retained the job files...
7448 if (!job
->attrs
||job
->num_files
== 0)
7451 * Nope - return a "not possible" error...
7454 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7455 _("Job #%d cannot be restarted - no files!"), jobid
);
7460 * See if the job is owned by the requesting user...
7463 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7465 send_http_error(con
, HTTP_UNAUTHORIZED
);
7470 * Restart the job and return...
7473 cupsdRestartJob(job
);
7475 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was restarted by \"%s\".", jobid
,
7478 con
->response
->request
.status
.status_code
= IPP_OK
;
7483 * 'save_auth_info()' - Save authentication information for a job.
7487 save_auth_info(cupsd_client_t
*con
, /* I - Client connection */
7488 cupsd_job_t
*job
) /* I - Job */
7490 int i
; /* Looping var */
7491 char filename
[1024]; /* Job authentication filename */
7492 cups_file_t
*fp
; /* Job authentication file */
7493 char line
[1024]; /* Line for file */
7497 * This function saves the in-memory authentication information for
7498 * a job so that it can be used to authenticate with a remote host.
7499 * The information is stored in a file that is readable only by the
7500 * root user. The username and password are Base-64 encoded, each
7501 * on a separate line, followed by random number (up to 1024) of
7502 * newlines to limit the amount of information that is exposed.
7504 * Because of the potential for exposing of authentication information,
7505 * this functionality is only enabled when running cupsd as root.
7507 * This caching only works for the Basic and BasicDigest authentication
7508 * types. Digest authentication cannot be cached this way, and in
7509 * the future Kerberos authentication may make all of this obsolete.
7511 * Authentication information is saved whenever an authenticated
7512 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
7515 * This information is deleted after a job is completed or canceled,
7516 * so reprints may require subsequent re-authentication.
7523 * Create the authentication file and change permissions...
7526 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
7527 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
7529 cupsdLogMessage(CUPSD_LOG_ERROR
,
7530 "Unable to save authentication info to \"%s\" - %s",
7531 filename
, strerror(errno
));
7535 fchown(cupsFileNumber(fp
), 0, 0);
7536 fchmod(cupsFileNumber(fp
), 0400);
7539 * Write the authenticated username...
7542 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
7543 cupsFilePrintf(fp
, "%s\n", line
);
7546 * Write the authenticated password...
7549 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
7550 cupsFilePrintf(fp
, "%s\n", line
);
7553 * Write a random number of newlines to the end of the file...
7556 for (i
= (rand() % 1024); i
>= 0; i
--)
7557 cupsFilePutChar(fp
, '\n');
7560 * Close the file and return...
7568 * 'send_document()' - Send a file to a printer or class.
7572 send_document(cupsd_client_t
*con
, /* I - Client connection */
7573 ipp_attribute_t
*uri
) /* I - Printer URI */
7575 ipp_attribute_t
*attr
; /* Current attribute */
7576 ipp_attribute_t
*format
; /* Document-format attribute */
7577 const char *default_format
;/* document-format-default value */
7578 int jobid
; /* Job ID number */
7579 cupsd_job_t
*job
; /* Current job */
7580 char job_uri
[HTTP_MAX_URI
],
7582 method
[HTTP_MAX_URI
],
7583 /* Method portion of URI */
7584 username
[HTTP_MAX_URI
],
7585 /* Username portion of URI */
7587 /* Host portion of URI */
7588 resource
[HTTP_MAX_URI
];
7589 /* Resource portion of URI */
7590 int port
; /* Port portion of URI */
7591 mime_type_t
*filetype
; /* Type of file */
7592 char super
[MIME_MAX_SUPER
],
7593 /* Supertype of file */
7594 type
[MIME_MAX_TYPE
],
7595 /* Subtype of file */
7596 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7597 /* Textual name of mime type */
7598 char filename
[1024]; /* Job filename */
7599 cupsd_printer_t
*printer
; /* Current printer */
7600 struct stat fileinfo
; /* File information */
7601 int kbytes
; /* Size of file */
7602 int compression
; /* Type of compression */
7605 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
7606 con
->http
.fd
, uri
->values
[0].string
.text
);
7609 * See if we have a job URI or a printer URI...
7612 if (!strcmp(uri
->name
, "printer-uri"))
7615 * Got a printer URI; see if we also have a job-id attribute...
7618 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7619 IPP_TAG_INTEGER
)) == NULL
)
7621 send_ipp_status(con
, IPP_BAD_REQUEST
,
7622 _("Got a printer-uri attribute but no job-id!"));
7626 jobid
= attr
->values
[0].integer
;
7631 * Got a job URI; parse it to get the job ID...
7634 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7635 sizeof(method
), username
, sizeof(username
), host
,
7636 sizeof(host
), &port
, resource
, sizeof(resource
));
7638 if (strncmp(resource
, "/jobs/", 6))
7644 send_ipp_status(con
, IPP_BAD_REQUEST
,
7645 _("Bad job-uri attribute \"%s\"!"),
7646 uri
->values
[0].string
.text
);
7650 jobid
= atoi(resource
+ 6);
7654 * See if the job exists...
7657 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7660 * Nope - return a "not found" error...
7663 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7667 printer
= cupsdFindDest(job
->dest
);
7670 * See if the job is owned by the requesting user...
7673 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7675 send_http_error(con
, HTTP_UNAUTHORIZED
);
7680 * OK, see if the client is sending the document compressed - CUPS
7681 * only supports "none" and "gzip".
7684 compression
= CUPS_FILE_NONE
;
7686 if ((attr
= ippFindAttribute(con
->request
, "compression",
7687 IPP_TAG_KEYWORD
)) != NULL
)
7689 if (strcmp(attr
->values
[0].string
.text
, "none")
7691 && strcmp(attr
->values
[0].string
.text
, "gzip")
7692 #endif /* HAVE_LIBZ */
7695 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
7696 attr
->values
[0].string
.text
);
7697 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7698 "compression", NULL
, attr
->values
[0].string
.text
);
7703 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7704 compression
= CUPS_FILE_GZIP
;
7705 #endif /* HAVE_LIBZ */
7709 * Do we have a file to print?
7714 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7719 * Is it a format we support?
7722 if ((format
= ippFindAttribute(con
->request
, "document-format",
7723 IPP_TAG_MIMETYPE
)) != NULL
)
7726 * Grab format from client...
7729 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
7731 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
7732 format
->values
[0].string
.text
);
7736 else if ((default_format
= cupsGetOption("document-format",
7737 printer
->num_options
,
7738 printer
->options
)) != NULL
)
7741 * Use default document format...
7744 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
7746 send_ipp_status(con
, IPP_BAD_REQUEST
,
7747 _("Could not scan type \"%s\"!"),
7755 * No document format attribute? Auto-type it!
7758 strcpy(super
, "application");
7759 strcpy(type
, "octet-stream");
7762 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7765 * Auto-type the file...
7768 ipp_attribute_t
*doc_name
; /* document-name attribute */
7771 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
7773 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7774 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7775 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7779 filetype
= mimeType(MimeDatabase
, super
, type
);
7782 filetype
= mimeType(MimeDatabase
, super
, type
);
7786 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
7789 * Replace the document-format attribute value with the auto-typed or
7793 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7798 _cupsStrFree(format
->values
[0].string
.text
);
7800 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7803 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7804 "document-format", NULL
, mimetype
);
7808 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7809 _("Unsupported format \'%s/%s\'!"), super
, type
);
7810 cupsdLogMessage(CUPSD_LOG_INFO
,
7811 "Hint: Do you have the raw file printing rules enabled?");
7814 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7815 "document-format", NULL
, format
->values
[0].string
.text
);
7820 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
7822 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7825 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7826 _("Unsupported format \'%s\'!"), mimetype
);
7828 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7829 "document-format", NULL
, mimetype
);
7834 cupsdLogMessage(CUPSD_LOG_DEBUG
,
7835 "send_document: request file type is %s/%s.",
7836 filetype
->super
, filetype
->type
);
7839 * Add the file to the job...
7844 if (add_file(con
, job
, filetype
, compression
))
7847 if (stat(con
->filename
, &fileinfo
))
7850 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7852 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7854 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7855 IPP_TAG_INTEGER
)) != NULL
)
7856 attr
->values
[0].integer
+= kbytes
;
7858 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
7860 rename(con
->filename
, filename
);
7862 cupsdClearString(&con
->filename
);
7864 cupsdLogMessage(CUPSD_LOG_INFO
,
7865 "File of type %s/%s queued in job #%d by \"%s\".",
7866 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
7869 * Start the job if this is the last document...
7872 if ((attr
= ippFindAttribute(con
->request
, "last-document",
7873 IPP_TAG_BOOLEAN
)) != NULL
&&
7874 attr
->values
[0].boolean
)
7877 * See if we need to add the ending sheet...
7881 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
7882 (attr
= ippFindAttribute(job
->attrs
, "job-sheets",
7883 IPP_TAG_ZERO
)) != NULL
&&
7884 attr
->num_values
> 1)
7890 cupsdLogMessage(CUPSD_LOG_INFO
,
7891 "Adding end banner page \"%s\" to job %d.",
7892 attr
->values
[1].string
.text
, job
->id
);
7894 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
7896 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7899 if (job
->state_value
== IPP_JOB_STOPPED
)
7901 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7902 job
->state_value
= IPP_JOB_PENDING
;
7904 else if (job
->state_value
== IPP_JOB_HELD
)
7906 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7907 IPP_TAG_KEYWORD
)) == NULL
)
7908 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7910 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
7912 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7913 job
->state_value
= IPP_JOB_PENDING
;
7920 * Start the job if possible... Since cupsdCheckJobs() can cancel a
7921 * job if it doesn't print, we need to re-find the job afterwards...
7928 job
= cupsdFindJob(jobid
);
7932 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7933 IPP_TAG_KEYWORD
)) == NULL
)
7934 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7936 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
7938 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
7939 job
->state_value
= IPP_JOB_HELD
;
7940 job
->hold_until
= time(NULL
) + 60;
7946 * Fill in the response info...
7949 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
7952 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
7955 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
7957 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
7958 job
? job
->state_value
: IPP_JOB_CANCELLED
);
7959 add_job_state_reasons(con
, job
);
7961 con
->response
->request
.status
.status_code
= IPP_OK
;
7966 * 'send_http_error()' - Send a HTTP error back to the IPP client.
7970 send_http_error(cupsd_client_t
*con
, /* I - Client connection */
7971 http_status_t status
) /* I - HTTP status code */
7973 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
7974 ippOpString(con
->request
->request
.op
.operation_id
),
7975 httpStatus(status
));
7977 cupsdSendError(con
, status
);
7979 ippDelete(con
->response
);
7980 con
->response
= NULL
;
7987 * 'send_ipp_status()' - Send a status back to the IPP client.
7991 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
7992 ipp_status_t status
, /* I - IPP status code */
7993 const char *message
, /* I - Status message */
7994 ...) /* I - Additional args as needed */
7996 va_list ap
; /* Pointer to additional args */
7997 char formatted
[1024]; /* Formatted errror message */
8002 va_start(ap
, message
);
8003 vsnprintf(formatted
, sizeof(formatted
),
8004 _cupsLangString(con
->language
, message
), ap
);
8007 cupsdLogMessage(status
>= IPP_BAD_REQUEST
? CUPSD_LOG_ERROR
: CUPSD_LOG_INFO
,
8009 ippOpString(con
->request
->request
.op
.operation_id
),
8010 ippErrorString(status
), formatted
);
8013 cupsdLogMessage(status
>= IPP_BAD_REQUEST
? CUPSD_LOG_ERROR
: CUPSD_LOG_INFO
,
8015 ippOpString(con
->request
->request
.op
.operation_id
),
8016 ippErrorString(status
));
8018 con
->response
->request
.status
.status_code
= status
;
8020 if (ippFindAttribute(con
->response
, "attributes-charset",
8021 IPP_TAG_ZERO
) == NULL
)
8022 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
8023 "attributes-charset", NULL
, DefaultCharset
);
8025 if (ippFindAttribute(con
->response
, "attributes-natural-language",
8026 IPP_TAG_ZERO
) == NULL
)
8027 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
8028 "attributes-natural-language", NULL
, DefaultLanguage
);
8031 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
8032 "status-message", NULL
, formatted
);
8037 * 'set_default()' - Set the default destination...
8041 set_default(cupsd_client_t
*con
, /* I - Client connection */
8042 ipp_attribute_t
*uri
) /* I - Printer URI */
8044 http_status_t status
; /* Policy status */
8045 cups_ptype_t dtype
; /* Destination type (printer or class) */
8046 cupsd_printer_t
*printer
; /* Printer */
8049 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
8050 con
->http
.fd
, uri
->values
[0].string
.text
);
8053 * Is the destination valid?
8056 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8062 send_ipp_status(con
, IPP_NOT_FOUND
,
8063 _("The printer or class was not found."));
8071 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
8073 send_http_error(con
, status
);
8078 * Set it as the default...
8081 DefaultPrinter
= printer
;
8083 cupsdSaveAllPrinters();
8084 cupsdSaveAllClasses();
8086 cupsdWritePrintcap();
8088 cupsdLogMessage(CUPSD_LOG_INFO
,
8089 "Default destination set to \"%s\" by \"%s\".",
8090 printer
->name
, get_username(con
));
8093 * Everything was ok, so return OK status...
8096 con
->response
->request
.status
.status_code
= IPP_OK
;
8101 * 'set_job_attrs()' - Set job attributes.
8105 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
8106 ipp_attribute_t
*uri
) /* I - Job URI */
8108 ipp_attribute_t
*attr
, /* Current attribute */
8109 *attr2
; /* Job attribute */
8110 int jobid
; /* Job ID */
8111 cupsd_job_t
*job
; /* Current job */
8112 char method
[HTTP_MAX_URI
],
8113 /* Method portion of URI */
8114 username
[HTTP_MAX_URI
],
8115 /* Username portion of URI */
8117 /* Host portion of URI */
8118 resource
[HTTP_MAX_URI
];
8119 /* Resource portion of URI */
8120 int port
; /* Port portion of URI */
8123 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
8124 con
->http
.fd
, uri
->values
[0].string
.text
);
8127 * Start with "everything is OK" status...
8130 con
->response
->request
.status
.status_code
= IPP_OK
;
8133 * See if we have a job URI or a printer URI...
8136 if (!strcmp(uri
->name
, "printer-uri"))
8139 * Got a printer URI; see if we also have a job-id attribute...
8142 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8143 IPP_TAG_INTEGER
)) == NULL
)
8145 send_ipp_status(con
, IPP_BAD_REQUEST
,
8146 _("Got a printer-uri attribute but no job-id!"));
8150 jobid
= attr
->values
[0].integer
;
8155 * Got a job URI; parse it to get the job ID...
8158 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8159 sizeof(method
), username
, sizeof(username
), host
,
8160 sizeof(host
), &port
, resource
, sizeof(resource
));
8162 if (strncmp(resource
, "/jobs/", 6))
8168 send_ipp_status(con
, IPP_BAD_REQUEST
,
8169 _("Bad job-uri attribute \"%s\"!"),
8170 uri
->values
[0].string
.text
);
8174 jobid
= atoi(resource
+ 6);
8178 * See if the job exists...
8181 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8184 * Nope - return a "not found" error...
8187 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8192 * See if the job has been completed...
8195 if (job
->state_value
> IPP_JOB_STOPPED
)
8198 * Return a "not-possible" error...
8201 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8202 _("Job #%d is finished and cannot be altered!"), jobid
);
8207 * See if the job is owned by the requesting user...
8210 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8212 send_http_error(con
, HTTP_UNAUTHORIZED
);
8217 * See what the user wants to change.
8222 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8224 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8227 if (!strcmp(attr
->name
, "attributes-charset") ||
8228 !strcmp(attr
->name
, "attributes-natural-language") ||
8229 !strcmp(attr
->name
, "document-compression") ||
8230 !strcmp(attr
->name
, "document-format") ||
8231 !strcmp(attr
->name
, "job-detailed-status-messages") ||
8232 !strcmp(attr
->name
, "job-document-access-errors") ||
8233 !strcmp(attr
->name
, "job-id") ||
8234 !strcmp(attr
->name
, "job-k-octets") ||
8235 !strcmp(attr
->name
, "job-originating-host-name") ||
8236 !strcmp(attr
->name
, "job-originating-user-name") ||
8237 !strcmp(attr
->name
, "job-printer-up-time") ||
8238 !strcmp(attr
->name
, "job-printer-uri") ||
8239 !strcmp(attr
->name
, "job-sheets") ||
8240 !strcmp(attr
->name
, "job-state-message") ||
8241 !strcmp(attr
->name
, "job-state-reasons") ||
8242 !strcmp(attr
->name
, "job-uri") ||
8243 !strcmp(attr
->name
, "number-of-documents") ||
8244 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
8245 !strcmp(attr
->name
, "output-device-assigned") ||
8246 !strncmp(attr
->name
, "date-time-at-", 13) ||
8247 !strncmp(attr
->name
, "job-impressions", 15) ||
8248 !strncmp(attr
->name
, "job-k-octets", 12) ||
8249 !strncmp(attr
->name
, "job-media-sheets", 16) ||
8250 !strncmp(attr
->name
, "time-at-", 8))
8256 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
8257 _("%s cannot be changed."), attr
->name
);
8259 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8260 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8265 if (!strcmp(attr
->name
, "job-priority"))
8268 * Change the job priority...
8271 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8273 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
8275 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8276 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8278 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
8280 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8281 _("Job is completed and cannot be changed."));
8284 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8285 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
8287 else if (!strcmp(attr
->name
, "job-state"))
8290 * Change the job state...
8293 if (attr
->value_tag
!= IPP_TAG_ENUM
)
8295 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
8297 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8298 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8302 switch (attr
->values
[0].integer
)
8304 case IPP_JOB_PENDING
:
8306 if (job
->state_value
> IPP_JOB_HELD
)
8308 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8309 _("Job state cannot be changed."));
8312 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8314 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8315 job
->state_value
= attr
->values
[0].integer
;
8319 case IPP_JOB_PROCESSING
:
8320 case IPP_JOB_STOPPED
:
8321 if (job
->state_value
!= attr
->values
[0].integer
)
8323 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8324 _("Job state cannot be changed."));
8329 case IPP_JOB_CANCELLED
:
8330 case IPP_JOB_ABORTED
:
8331 case IPP_JOB_COMPLETED
:
8332 if (job
->state_value
> IPP_JOB_PROCESSING
)
8334 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8335 _("Job state cannot be changed."));
8338 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8340 cupsdCancelJob(job
, 0);
8344 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8345 job
->state_value
= attr
->values
[0].integer
;
8353 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
8355 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8356 IPP_TAG_ZERO
)) != NULL
)
8359 * Some other value; first free the old value...
8362 if (job
->attrs
->prev
)
8363 job
->attrs
->prev
->next
= attr2
->next
;
8365 job
->attrs
->attrs
= attr2
->next
;
8367 if (job
->attrs
->last
== attr2
)
8368 job
->attrs
->last
= job
->attrs
->prev
;
8370 _ippFreeAttr(attr2
);
8373 * Then copy the attribute...
8376 copy_attribute(job
->attrs
, attr
, 0);
8379 * See if the job-name or job-hold-until is being changed.
8382 if (!strcmp(attr
->name
, "job-hold-until"))
8384 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
8386 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
8387 cupsdReleaseJob(job
);
8392 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
8395 * Delete the attribute...
8398 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8399 IPP_TAG_ZERO
)) != NULL
)
8401 if (job
->attrs
->prev
)
8402 job
->attrs
->prev
->next
= attr2
->next
;
8404 job
->attrs
->attrs
= attr2
->next
;
8406 if (attr2
== job
->attrs
->last
)
8407 job
->attrs
->last
= job
->attrs
->prev
;
8409 _ippFreeAttr(attr2
);
8415 * Add new option by copying it...
8418 copy_attribute(job
->attrs
, attr
, 0);
8429 * Start jobs if possible...
8437 * 'set_printer_defaults()' - Set printer default options from a request.
8441 set_printer_defaults(
8442 cupsd_client_t
*con
, /* I - Client connection */
8443 cupsd_printer_t
*printer
) /* I - Printer */
8445 int i
; /* Looping var */
8446 ipp_attribute_t
*attr
; /* Current attribute */
8447 int namelen
; /* Length of attribute name */
8448 char name
[256], /* New attribute name */
8449 value
[256]; /* String version of integer attrs */
8452 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8455 * Skip non-printer attributes...
8458 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
8461 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
8463 if (!strcmp(attr
->name
, "job-sheets-default"))
8466 * Only allow keywords and names...
8469 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8473 * Only allow job-sheets-default to be set when running without a
8474 * system high classification level...
8480 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
8482 if (attr
->num_values
> 1)
8483 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
8485 cupsdSetString(&printer
->job_sheets
[1], "none");
8487 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
8489 cupsdFreePrinterUsers(printer
);
8491 printer
->deny_users
= 0;
8493 if (attr
->value_tag
== IPP_TAG_NAME
&&
8494 (attr
->num_values
> 1 ||
8495 strcmp(attr
->values
[0].string
.text
, "all")))
8497 for (i
= 0; i
< attr
->num_values
; i
++)
8498 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8501 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
8503 cupsdFreePrinterUsers(printer
);
8505 printer
->deny_users
= 1;
8507 if (attr
->value_tag
== IPP_TAG_NAME
&&
8508 (attr
->num_values
> 1 ||
8509 strcmp(attr
->values
[0].string
.text
, "none")))
8511 for (i
= 0; i
< attr
->num_values
; i
++)
8512 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8515 else if (!strcmp(attr
->name
, "job-quota-period"))
8517 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8520 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
8521 attr
->values
[0].integer
);
8522 cupsdFreeQuotas(printer
);
8524 printer
->quota_period
= attr
->values
[0].integer
;
8526 else if (!strcmp(attr
->name
, "job-k-limit"))
8528 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8531 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
8532 attr
->values
[0].integer
);
8533 cupsdFreeQuotas(printer
);
8535 printer
->k_limit
= attr
->values
[0].integer
;
8537 else if (!strcmp(attr
->name
, "job-page-limit"))
8539 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8542 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
8543 attr
->values
[0].integer
);
8544 cupsdFreeQuotas(printer
);
8546 printer
->page_limit
= attr
->values
[0].integer
;
8548 else if (!strcmp(attr
->name
, "printer-op-policy"))
8550 cupsd_policy_t
*p
; /* Policy */
8553 if (attr
->value_tag
!= IPP_TAG_NAME
)
8556 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
8558 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8559 "Setting printer-op-policy to \"%s\"...",
8560 attr
->values
[0].string
.text
);
8561 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
8562 printer
->op_policy_ptr
= p
;
8566 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8567 _("Unknown printer-op-policy \"%s\"."),
8568 attr
->values
[0].string
.text
);
8572 else if (!strcmp(attr
->name
, "printer-error-policy"))
8574 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8577 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
8578 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
8579 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
8581 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8582 _("Unknown printer-error-policy \"%s\"."),
8583 attr
->values
[0].string
.text
);
8587 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8588 "Setting printer-error-policy to \"%s\"...",
8589 attr
->values
[0].string
.text
);
8590 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
8592 else if (!strcmp(attr
->name
, "notify-lease-duration-default") ||
8593 !strcmp(attr
->name
, "notify-events-default"))
8597 * Skip any other non-default attributes...
8600 namelen
= strlen(attr
->name
);
8601 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
8602 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
8606 * OK, anything else must be a user-defined default...
8609 strlcpy(name
, attr
->name
, sizeof(name
));
8610 name
[namelen
- 8] = '\0'; /* Strip "-default" */
8612 switch (attr
->value_tag
)
8614 case IPP_TAG_DELETEATTR
:
8615 printer
->num_options
= cupsRemoveOption(name
,
8616 printer
->num_options
,
8617 &(printer
->options
));
8618 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8619 "Deleting %s", attr
->name
);
8623 case IPP_TAG_KEYWORD
:
8625 printer
->num_options
= cupsAddOption(name
,
8626 attr
->values
[0].string
.text
,
8627 printer
->num_options
,
8628 &(printer
->options
));
8629 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8630 "Setting %s to \"%s\"...", attr
->name
,
8631 attr
->values
[0].string
.text
);
8634 case IPP_TAG_BOOLEAN
:
8635 printer
->num_options
= cupsAddOption(name
,
8636 attr
->values
[0].boolean
?
8638 printer
->num_options
,
8639 &(printer
->options
));
8640 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8641 "Setting %s to %s...", attr
->name
,
8642 attr
->values
[0].boolean
? "true" : "false");
8645 case IPP_TAG_INTEGER
:
8647 sprintf(value
, "%d", attr
->values
[0].integer
);
8648 printer
->num_options
= cupsAddOption(name
, value
,
8649 printer
->num_options
,
8650 &(printer
->options
));
8651 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8652 "Setting %s to %s...", attr
->name
, value
);
8655 case IPP_TAG_RANGE
:
8656 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
8657 attr
->values
[0].range
.upper
);
8658 printer
->num_options
= cupsAddOption(name
, value
,
8659 printer
->num_options
,
8660 &(printer
->options
));
8661 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8662 "Setting %s to %s...", attr
->name
, value
);
8665 case IPP_TAG_RESOLUTION
:
8666 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
8667 attr
->values
[0].resolution
.yres
,
8668 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
8670 printer
->num_options
= cupsAddOption(name
, value
,
8671 printer
->num_options
,
8672 &(printer
->options
));
8673 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8674 "Setting %s to %s...", attr
->name
, value
);
8678 /* Do nothing for other values */
8686 * 'start_printer()' - Start a printer.
8690 start_printer(cupsd_client_t
*con
, /* I - Client connection */
8691 ipp_attribute_t
*uri
) /* I - Printer URI */
8693 http_status_t status
; /* Policy status */
8694 cups_ptype_t dtype
; /* Destination type (printer or class) */
8695 cupsd_printer_t
*printer
; /* Printer data */
8698 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
8699 con
->http
.fd
, uri
->values
[0].string
.text
);
8702 * Is the destination valid?
8705 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8711 send_ipp_status(con
, IPP_NOT_FOUND
,
8712 _("The printer or class was not found."));
8720 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8722 send_http_error(con
, status
);
8727 * Start the printer...
8730 printer
->state_message
[0] = '\0';
8732 cupsdStartPrinter(printer
, 1);
8734 if (dtype
& CUPS_PRINTER_CLASS
)
8736 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
8737 printer
->name
, get_username(con
));
8738 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
8739 "Class \"%s\" started by \"%s\".", printer
->name
,
8744 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
8745 printer
->name
, get_username(con
));
8746 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
8747 "Printer \"%s\" started by \"%s\".", printer
->name
,
8754 * Everything was ok, so return OK status...
8757 con
->response
->request
.status
.status_code
= IPP_OK
;
8762 * 'stop_printer()' - Stop a printer.
8766 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
8767 ipp_attribute_t
*uri
) /* I - Printer URI */
8769 http_status_t status
; /* Policy status */
8770 cups_ptype_t dtype
; /* Destination type (printer or class) */
8771 cupsd_printer_t
*printer
; /* Printer data */
8772 ipp_attribute_t
*attr
; /* printer-state-message attribute */
8775 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
8776 con
->http
.fd
, uri
->values
[0].string
.text
);
8779 * Is the destination valid?
8782 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8788 send_ipp_status(con
, IPP_NOT_FOUND
,
8789 _("The printer or class was not found."));
8797 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8799 send_http_error(con
, status
);
8804 * Stop the printer...
8807 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8808 IPP_TAG_TEXT
)) == NULL
)
8809 strcpy(printer
->state_message
, "Paused");
8812 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8813 sizeof(printer
->state_message
));
8816 cupsdStopPrinter(printer
, 1);
8818 if (dtype
& CUPS_PRINTER_CLASS
)
8820 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
8821 printer
->name
, get_username(con
));
8822 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
8823 "Class \"%s\" stopped by \"%s\".", printer
->name
,
8828 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
8829 printer
->name
, get_username(con
));
8830 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
8831 "Printer \"%s\" stopped by \"%s\".", printer
->name
,
8836 * Everything was ok, so return OK status...
8839 con
->response
->request
.status
.status_code
= IPP_OK
;
8844 * 'url_encode_attr()' - URL-encode a string attribute.
8848 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
8849 char *buffer
,/* I - String buffer */
8850 int bufsize
)/* I - Size of buffer */
8852 int i
; /* Looping var */
8853 char *bufptr
, /* Pointer into buffer */
8854 *bufend
, /* End of buffer */
8855 *valptr
; /* Pointer into value */
8858 strlcpy(buffer
, attr
->name
, bufsize
);
8859 bufptr
= buffer
+ strlen(buffer
);
8860 bufend
= buffer
+ bufsize
- 1;
8862 for (i
= 0; i
< attr
->num_values
; i
++)
8864 if (bufptr
>= bufend
)
8872 if (bufptr
>= bufend
)
8877 for (valptr
= attr
->values
[i
].string
.text
;
8878 *valptr
&& bufptr
< bufend
;
8882 if (bufptr
>= (bufend
- 2))
8889 else if (*valptr
== '\'' || *valptr
== '\\')
8892 *bufptr
++ = *valptr
;
8895 *bufptr
++ = *valptr
;
8897 if (bufptr
>= bufend
)
8908 * 'user_allowed()' - See if a user is allowed to print to a queue.
8911 static int /* O - 0 if not allowed, 1 if allowed */
8912 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
8913 const char *username
) /* I - Username */
8915 int i
; /* Looping var */
8916 struct passwd
*pw
; /* User password data */
8919 if (p
->num_users
== 0)
8922 if (!strcmp(username
, "root"))
8925 pw
= getpwnam(username
);
8928 for (i
= 0; i
< p
->num_users
; i
++)
8930 if (p
->users
[i
][0] == '@')
8933 * Check group membership...
8936 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
8939 else if (!strcasecmp(username
, p
->users
[i
]))
8943 return ((i
< p
->num_users
) != p
->deny_users
);
8948 * 'validate_job()' - Validate printer options and destination.
8952 validate_job(cupsd_client_t
*con
, /* I - Client connection */
8953 ipp_attribute_t
*uri
) /* I - Printer URI */
8955 http_status_t status
; /* Policy status */
8956 ipp_attribute_t
*attr
; /* Current attribute */
8957 ipp_attribute_t
*format
; /* Document-format attribute */
8958 cups_ptype_t dtype
; /* Destination type (printer or class) */
8959 char super
[MIME_MAX_SUPER
],
8960 /* Supertype of file */
8961 type
[MIME_MAX_TYPE
];
8962 /* Subtype of file */
8963 cupsd_printer_t
*printer
; /* Printer */
8966 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
8967 con
->http
.fd
, uri
->values
[0].string
.text
);
8970 * OK, see if the client is sending the document compressed - CUPS
8971 * doesn't support compression yet...
8974 if ((attr
= ippFindAttribute(con
->request
, "compression",
8975 IPP_TAG_KEYWORD
)) != NULL
&&
8976 !strcmp(attr
->values
[0].string
.text
, "none"))
8978 send_ipp_status(con
, IPP_ATTRIBUTES
,
8979 _("Unsupported compression attribute %s!"),
8980 attr
->values
[0].string
.text
);
8981 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8982 "compression", NULL
, attr
->values
[0].string
.text
);
8987 * Is it a format we support?
8990 if ((format
= ippFindAttribute(con
->request
, "document-format",
8991 IPP_TAG_MIMETYPE
)) != NULL
)
8993 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
8995 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
8996 format
->values
[0].string
.text
);
9000 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
9001 !mimeType(MimeDatabase
, super
, type
))
9003 cupsdLogMessage(CUPSD_LOG_INFO
,
9004 "Hint: Do you have the raw file printing rules enabled?");
9005 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9006 _("Unsupported format \"%s\"!"),
9007 format
->values
[0].string
.text
);
9008 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9009 "document-format", NULL
, format
->values
[0].string
.text
);
9015 * Is the destination valid?
9018 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9024 send_ipp_status(con
, IPP_NOT_FOUND
,
9025 _("The printer or class was not found."));
9033 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9035 send_http_error(con
, status
);
9040 * Everything was ok, so return OK status...
9043 con
->response
->request
.status
.status_code
= IPP_OK
;
9048 * 'validate_name()' - Make sure the printer name only contains valid chars.
9051 static int /* O - 0 if name is no good, 1 if name is good */
9052 validate_name(const char *name
) /* I - Name to check */
9054 const char *ptr
; /* Pointer into name */
9058 * Scan the whole name...
9061 for (ptr
= name
; *ptr
; ptr
++)
9062 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
9066 * All the characters are good; validate the length, too...
9069 return ((ptr
- name
) < 128);
9074 * 'validate_user()' - Validate the user for the request.
9077 static int /* O - 1 if permitted, 0 otherwise */
9078 validate_user(cupsd_job_t
*job
, /* I - Job */
9079 cupsd_client_t
*con
, /* I - Client connection */
9080 const char *owner
, /* I - Owner of job/resource */
9081 char *username
, /* O - Authenticated username */
9082 int userlen
) /* I - Length of username */
9084 cupsd_printer_t
*printer
; /* Printer for job */
9087 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9088 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
9090 job
? job
->id
: 0, con
->http
.fd
, owner
? owner
: "(null)",
9097 if (!con
|| !owner
|| !username
|| userlen
<= 0)
9101 * Get the best authenticated username that is available.
9104 strlcpy(username
, get_username(con
), userlen
);
9107 * Check the username against the owner...
9110 printer
= cupsdFindDest(job
->dest
);
9112 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
9113 con
, owner
) == HTTP_OK
);
9118 * End of "$Id: ipp.c 5552 2006-05-20 12:00:37Z mike $".