2 * "$Id: ipp.c 181 2006-06-22 20:01:18Z jlovell $"
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 scheme
[HTTP_MAX_URI
],
4650 /* Scheme 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
, scheme
,
4691 sizeof(scheme
), 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
)
4787 * Validate the recipient scheme against the ServerBin/notifier
4791 char notifier
[1024]; /* Notifier filename */
4794 recipient
= attr
->values
[0].string
.text
;
4796 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
, scheme
,
4797 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
4798 sizeof(host
), &port
, resource
, sizeof(resource
)))
4800 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4801 _("Bad notify-recipient URI \"%s\"!"), recipient
);
4802 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
4803 "notify-status-code", IPP_URI_SCHEME
);
4807 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
4809 if (access(notifier
, X_OK
))
4811 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4812 _("notify-recipient URI \"%s\" uses unknown scheme!"),
4814 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
4815 "notify-status-code", IPP_URI_SCHEME
);
4819 else if (!strcmp(attr
->name
, "notify-pull-method") &&
4820 attr
->value_tag
== IPP_TAG_KEYWORD
)
4822 pullmethod
= attr
->values
[0].string
.text
;
4824 if (strcmp(pullmethod
, "ippget"))
4826 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4827 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
4828 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
4829 "notify-status-code", IPP_ATTRIBUTES
);
4833 else if (!strcmp(attr
->name
, "notify-charset") &&
4834 attr
->value_tag
== IPP_TAG_CHARSET
&&
4835 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
4836 strcmp(attr
->values
[0].string
.text
, "utf-8"))
4838 send_ipp_status(con
, IPP_CHARSET
,
4839 _("Character set \"%s\" not supported!"),
4840 attr
->values
[0].string
.text
);
4843 else if (!strcmp(attr
->name
, "notify-natural-language") &&
4844 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
4845 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
4847 send_ipp_status(con
, IPP_CHARSET
,
4848 _("Language \"%s\" not supported!"),
4849 attr
->values
[0].string
.text
);
4852 else if (!strcmp(attr
->name
, "notify-user-data") &&
4853 attr
->value_tag
== IPP_TAG_STRING
)
4855 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
4857 send_ipp_status(con
, IPP_REQUEST_VALUE
,
4858 _("The notify-user-data value is too large "
4859 "(%d > 63 octets)!"),
4860 attr
->values
[0].unknown
.length
);
4866 else if (!strcmp(attr
->name
, "notify-events") &&
4867 attr
->value_tag
== IPP_TAG_KEYWORD
)
4869 for (i
= 0; i
< attr
->num_values
; i
++)
4870 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
4872 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
4873 attr
->value_tag
== IPP_TAG_INTEGER
)
4874 lease
= attr
->values
[0].integer
;
4875 else if (!strcmp(attr
->name
, "notify-time-interval") &&
4876 attr
->value_tag
== IPP_TAG_INTEGER
)
4877 interval
= attr
->values
[0].integer
;
4878 else if (!strcmp(attr
->name
, "notify-job-id") &&
4879 attr
->value_tag
== IPP_TAG_INTEGER
)
4880 jobid
= attr
->values
[0].integer
;
4886 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
4888 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
4889 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
4890 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
4892 if (!recipient
&& !pullmethod
)
4895 if (mask
== CUPSD_EVENT_NONE
)
4898 mask
= CUPSD_EVENT_JOB_COMPLETED
;
4900 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4903 send_ipp_status(con
, IPP_BAD_REQUEST
,
4904 _("notify-events not specified!"));
4909 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
4911 cupsdLogMessage(CUPSD_LOG_INFO
,
4912 "create_subscription: Limiting notify-lease-duration to "
4915 lease
= MaxLeaseDuration
;
4920 if ((job
= cupsdFindJob(jobid
)) == NULL
)
4922 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
4929 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
4932 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
4935 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4936 "Added subscription %d for printer \"%s\"",
4937 sub
->id
, printer
->name
);
4939 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
4942 sub
->interval
= interval
;
4944 sub
->expire
= lease
? time(NULL
) + lease
: 0;
4946 cupsdSetString(&sub
->owner
, username
);
4950 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
4951 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
4952 sub
->user_data_len
);
4955 ippAddSeparator(con
->response
);
4956 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4957 "notify-subscription-id", sub
->id
);
4959 con
->response
->request
.status
.status_code
= IPP_OK
;
4965 cupsdSaveAllSubscriptions();
4971 * 'delete_printer()' - Remove a printer or class from the system.
4975 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
4976 ipp_attribute_t
*uri
) /* I - URI of printer or class */
4978 http_status_t status
; /* Policy status */
4979 cups_ptype_t dtype
; /* Destination type (printer or class) */
4980 cupsd_printer_t
*printer
; /* Printer/class */
4981 char filename
[1024]; /* Script/PPD filename */
4984 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
4985 con
->http
.fd
, uri
->values
[0].string
.text
);
4988 * Do we have a valid URI?
4991 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
4997 send_ipp_status(con
, IPP_NOT_FOUND
,
4998 _("The printer or class was not found."));
5006 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5008 send_http_error(con
, status
);
5013 * Remove old jobs...
5016 cupsdCancelJobs(printer
->name
, NULL
, 1);
5019 * Remove old subscriptions and send a "deleted printer" event...
5022 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5023 "%s \"%s\" deleted by \"%s\".",
5024 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5025 printer
->name
, get_username(con
));
5027 cupsdExpireSubscriptions(printer
, NULL
);
5030 * Remove any old PPD or script files...
5033 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5037 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5041 if (dtype
& CUPS_PRINTER_CLASS
)
5043 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5044 printer
->name
, get_username(con
));
5046 cupsdDeletePrinter(printer
, 0);
5047 cupsdSaveAllClasses();
5051 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5052 printer
->name
, get_username(con
));
5054 cupsdDeletePrinter(printer
, 0);
5055 cupsdSaveAllPrinters();
5058 cupsdWritePrintcap();
5061 * Return with no errors...
5064 con
->response
->request
.status
.status_code
= IPP_OK
;
5069 * 'get_default()' - Get the default destination.
5073 get_default(cupsd_client_t
*con
) /* I - Client connection */
5075 http_status_t status
; /* Policy status */
5076 cups_array_t
*ra
; /* Requested attributes array */
5079 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5085 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5087 send_http_error(con
, status
);
5093 ra
= create_requested_array(con
->request
);
5095 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5097 cupsArrayDelete(ra
);
5099 con
->response
->request
.status
.status_code
= IPP_OK
;
5102 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5107 * 'get_devices()' - Get the list of available devices on the local system.
5111 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5113 http_status_t status
; /* Policy status */
5114 ipp_attribute_t
*limit
, /* Limit attribute */
5115 *requested
; /* requested-attributes attribute */
5116 char command
[1024], /* cups-deviced command */
5117 options
[1024], /* Options to pass to command */
5119 /* String for requested attributes */
5122 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5128 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5130 send_http_error(con
, status
);
5135 * Run cups-deviced command with the given options...
5138 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5139 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5143 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5145 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5147 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5148 snprintf(options
, sizeof(options
),
5150 con
->request
->request
.op
.request_id
,
5151 limit
? limit
->values
[0].integer
: 0, (int)User
,
5154 if (cupsdSendCommand(con
, command
, options
, 1))
5157 * Command started successfully, don't send an IPP response here...
5160 ippDelete(con
->response
);
5161 con
->response
= NULL
;
5166 * Command failed, return "internal error" so the user knows something
5170 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5171 _("cups-deviced failed to execute."));
5177 * 'get_job_attrs()' - Get job attributes.
5181 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5182 ipp_attribute_t
*uri
) /* I - Job URI */
5184 http_status_t status
; /* Policy status */
5185 ipp_attribute_t
*attr
; /* Current attribute */
5186 int jobid
; /* Job ID */
5187 cupsd_job_t
*job
; /* Current job */
5188 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5189 username
[HTTP_MAX_URI
], /* Username portion of URI */
5190 host
[HTTP_MAX_URI
], /* Host portion of URI */
5191 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5192 int port
; /* Port portion of URI */
5193 cups_array_t
*ra
; /* Requested attributes array */
5196 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5197 con
->http
.fd
, uri
->values
[0].string
.text
);
5200 * See if we have a job URI or a printer URI...
5203 if (!strcmp(uri
->name
, "printer-uri"))
5206 * Got a printer URI; see if we also have a job-id attribute...
5209 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5210 IPP_TAG_INTEGER
)) == NULL
)
5212 send_ipp_status(con
, IPP_BAD_REQUEST
,
5213 _("Got a printer-uri attribute but no job-id!"));
5217 jobid
= attr
->values
[0].integer
;
5222 * Got a job URI; parse it to get the job ID...
5225 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5226 sizeof(method
), username
, sizeof(username
), host
,
5227 sizeof(host
), &port
, resource
, sizeof(resource
));
5229 if (strncmp(resource
, "/jobs/", 6))
5235 send_ipp_status(con
, IPP_BAD_REQUEST
,
5236 _("Bad job-uri attribute \"%s\"!"),
5237 uri
->values
[0].string
.text
);
5241 jobid
= atoi(resource
+ 6);
5245 * See if the job exists...
5248 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5251 * Nope - return a "not found" error...
5254 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5262 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5264 send_http_error(con
, status
);
5269 * Copy attributes...
5274 ra
= create_requested_array(con
->request
);
5275 copy_job_attrs(con
, job
, ra
);
5276 cupsArrayDelete(ra
);
5278 con
->response
->request
.status
.status_code
= IPP_OK
;
5283 * 'get_jobs()' - Get a list of jobs for the specified printer.
5287 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
5288 ipp_attribute_t
*uri
) /* I - Printer URI */
5290 http_status_t status
; /* Policy status */
5291 ipp_attribute_t
*attr
; /* Current attribute */
5292 const char *dest
; /* Destination */
5293 cups_ptype_t dtype
; /* Destination type (printer or class) */
5294 cups_ptype_t dmask
; /* Destination type mask */
5295 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
5296 username
[HTTP_MAX_URI
], /* Username portion of URI */
5297 host
[HTTP_MAX_URI
], /* Host portion of URI */
5298 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5299 int port
; /* Port portion of URI */
5300 int completed
; /* Completed jobs? */
5301 int first_job_id
; /* First job ID */
5302 int limit
; /* Maximum number of jobs to return */
5303 int count
; /* Number of jobs that match */
5304 cupsd_job_t
*job
; /* Current job pointer */
5305 cupsd_printer_t
*printer
; /* Printer */
5306 cups_array_t
*list
; /* Which job list... */
5307 cups_array_t
*ra
; /* Requested attributes array */
5310 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
5311 uri
->values
[0].string
.text
);
5314 * Is the destination valid?
5317 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5318 sizeof(scheme
), username
, sizeof(username
), host
,
5319 sizeof(host
), &port
, resource
, sizeof(resource
));
5321 if (!strcmp(resource
, "/") ||
5322 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
5325 dtype
= (cups_ptype_t
)0;
5326 dmask
= (cups_ptype_t
)0;
5329 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5332 dtype
= (cups_ptype_t
)0;
5333 dmask
= CUPS_PRINTER_CLASS
;
5336 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5339 dtype
= CUPS_PRINTER_CLASS
;
5340 dmask
= CUPS_PRINTER_CLASS
;
5343 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
5350 send_ipp_status(con
, IPP_NOT_FOUND
,
5351 _("The printer or class was not found."));
5355 dmask
= CUPS_PRINTER_CLASS
;
5363 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5366 send_http_error(con
, status
);
5370 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5372 send_http_error(con
, status
);
5377 * See if the "which-jobs" attribute have been specified...
5380 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
5381 IPP_TAG_KEYWORD
)) != NULL
&&
5382 !strcmp(attr
->values
[0].string
.text
, "completed"))
5387 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
5399 * See if they want to limit the number of jobs reported...
5402 if ((attr
= ippFindAttribute(con
->request
, "limit",
5403 IPP_TAG_INTEGER
)) != NULL
)
5404 limit
= attr
->values
[0].integer
;
5408 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
5409 IPP_TAG_INTEGER
)) != NULL
)
5410 first_job_id
= attr
->values
[0].integer
;
5415 * See if we only want to see jobs for a specific user...
5418 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
5419 IPP_TAG_BOOLEAN
)) != NULL
&&
5420 attr
->values
[0].boolean
)
5421 strlcpy(username
, get_username(con
), sizeof(username
));
5425 ra
= create_requested_array(con
->request
);
5428 * OK, build a list of jobs for this printer...
5431 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
5432 count
< limit
&& job
;
5433 job
= (cupsd_job_t
*)cupsArrayNext(list
))
5436 * Filter out jobs that don't match...
5439 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
5441 if ((dest
&& strcmp(job
->dest
, dest
)) &&
5442 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
5444 if ((job
->dtype
& dmask
) != dtype
&&
5445 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
5447 if (username
[0] && strcasecmp(username
, job
->username
))
5450 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
5453 if (job
->id
< first_job_id
)
5462 ippAddSeparator(con
->response
);
5466 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
5468 copy_job_attrs(con
, job
, ra
);
5471 cupsArrayDelete(ra
);
5473 con
->response
->request
.status
.status_code
= IPP_OK
;
5478 * 'get_notifications()' - Get events for a subscription.
5482 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
5484 int i
, j
; /* Looping vars */
5485 http_status_t status
; /* Policy status */
5486 cupsd_subscription_t
*sub
; /* Subscription */
5487 ipp_attribute_t
*ids
, /* notify-subscription-ids */
5488 *sequences
; /* notify-sequence-numbers */
5489 int min_seq
; /* Minimum sequence number */
5490 int interval
; /* Poll interval */
5493 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_subscription_attrs(con=%p[%d])",
5497 * Get subscription attributes...
5500 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
5502 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
5507 send_ipp_status(con
, IPP_BAD_REQUEST
,
5508 _("Missing notify-subscription-ids attribute!"));
5513 * Are the subscription IDs valid?
5516 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
5518 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
5521 * Bad subscription ID...
5524 send_ipp_status(con
, IPP_NOT_FOUND
,
5525 _("notify-subscription-id %d no good!"),
5526 ids
->values
[i
].integer
);
5534 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5536 con
, sub
->owner
)) != HTTP_OK
)
5538 send_http_error(con
, status
);
5543 * Check the subscription type and update the interval accordingly.
5546 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
5549 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
5551 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
5557 * Tell the client to poll again in N seconds...
5561 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5562 "notify-get-interval", interval
);
5564 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5565 "printer-up-time", time(NULL
));
5568 * Copy the subscription event attributes to the response.
5571 con
->response
->request
.status
.status_code
=
5572 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
5574 for (i
= 0; i
< ids
->num_values
; i
++)
5577 * Get the subscription and sequence number...
5580 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
5582 if (sequences
&& i
< sequences
->num_values
)
5583 min_seq
= sequences
->values
[i
].integer
;
5588 * If we don't have any new events, nothing to do here...
5591 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
5595 * Otherwise copy all of the new events...
5598 if (sub
->first_event_id
> min_seq
)
5601 j
= min_seq
- sub
->first_event_id
;
5603 for (; j
< sub
->num_events
; j
++)
5605 ippAddSeparator(con
->response
);
5607 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
5608 IPP_TAG_EVENT_NOTIFICATION
, 0);
5615 * 'get_ppds()' - Get the list of PPD files on the local system.
5619 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
5621 http_status_t status
; /* Policy status */
5622 ipp_attribute_t
*limit
, /* Limit attribute */
5623 *make
, /* ppd-make attribute */
5624 *requested
; /* requested-attributes attribute */
5625 char command
[1024], /* cups-deviced command */
5626 options
[1024], /* Options to pass to command */
5628 /* String for requested attributes */
5629 make_str
[256]; /* Escaped ppd-make string */
5632 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
5638 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5640 send_http_error(con
, status
);
5645 * Run cups-driverd command with the given options...
5648 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5649 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
5650 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5654 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5656 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5659 url_encode_attr(make
, make_str
, sizeof(make_str
));
5663 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
5664 snprintf(options
, sizeof(options
), "list+%d+%d+%s%s%s",
5665 con
->request
->request
.op
.request_id
,
5666 limit
? limit
->values
[0].integer
: 0,
5667 requested_str
, make
? "%20" : "", make_str
);
5669 if (cupsdSendCommand(con
, command
, options
, 0))
5672 * Command started successfully, don't send an IPP response here...
5675 ippDelete(con
->response
);
5676 con
->response
= NULL
;
5681 * Command failed, return "internal error" so the user knows something
5685 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5686 _("cups-driverd failed to execute."));
5692 * 'get_printer_attrs()' - Get printer attributes.
5696 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
5697 ipp_attribute_t
*uri
) /* I - Printer URI */
5699 http_status_t status
; /* Policy status */
5700 cups_ptype_t dtype
; /* Destination type (printer or class) */
5701 cupsd_printer_t
*printer
; /* Printer/class */
5702 cups_array_t
*ra
; /* Requested attributes array */
5705 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
5706 con
->http
.fd
, uri
->values
[0].string
.text
);
5709 * Is the destination valid?
5712 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5718 send_ipp_status(con
, IPP_NOT_FOUND
,
5719 _("The printer or class was not found."));
5727 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
5729 send_http_error(con
, status
);
5734 * Send the attributes...
5737 ra
= create_requested_array(con
->request
);
5739 copy_printer_attrs(con
, printer
, ra
);
5741 cupsArrayDelete(ra
);
5743 con
->response
->request
.status
.status_code
= IPP_OK
;
5748 * 'get_printers()' - Get a list of printers or classes.
5752 get_printers(cupsd_client_t
*con
, /* I - Client connection */
5753 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
5755 http_status_t status
; /* Policy status */
5756 ipp_attribute_t
*attr
; /* Current attribute */
5757 int limit
; /* Maximum number of printers to return */
5758 int count
; /* Number of printers that match */
5759 cupsd_printer_t
*printer
; /* Current printer pointer */
5760 int printer_type
, /* printer-type attribute */
5761 printer_mask
; /* printer-type-mask attribute */
5762 char *location
; /* Location string */
5763 const char *username
; /* Current user */
5764 char *first_printer_name
; /* first-printer-name attribute */
5765 cups_array_t
*ra
; /* Requested attributes array */
5768 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
5769 con
->http
.fd
, type
);
5775 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5777 send_http_error(con
, status
);
5782 * Check for printers...
5785 if (!Printers
|| !cupsArrayCount(Printers
))
5787 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
5792 * See if they want to limit the number of printers reported...
5795 if ((attr
= ippFindAttribute(con
->request
, "limit",
5796 IPP_TAG_INTEGER
)) != NULL
)
5797 limit
= attr
->values
[0].integer
;
5801 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
5802 IPP_TAG_NAME
)) != NULL
)
5803 first_printer_name
= attr
->values
[0].string
.text
;
5805 first_printer_name
= NULL
;
5808 * Support filtering...
5811 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
5812 IPP_TAG_ENUM
)) != NULL
)
5813 printer_type
= attr
->values
[0].integer
;
5817 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
5818 IPP_TAG_ENUM
)) != NULL
)
5819 printer_mask
= attr
->values
[0].integer
;
5823 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
5824 IPP_TAG_TEXT
)) != NULL
)
5825 location
= attr
->values
[0].string
.text
;
5829 if (con
->username
[0])
5830 username
= con
->username
;
5831 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
5832 IPP_TAG_NAME
)) != NULL
)
5833 username
= attr
->values
[0].string
.text
;
5837 ra
= create_requested_array(con
->request
);
5840 * OK, build a list of printers for this printer...
5843 if (first_printer_name
)
5845 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
5846 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5849 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5852 count
< limit
&& printer
;
5853 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
5855 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
5856 (printer
->type
& printer_mask
) == printer_type
&&
5857 (!location
|| !printer
->location
||
5858 !strcasecmp(printer
->location
, location
)))
5861 * If HideImplicitMembers is enabled, see if this printer or class
5862 * is a member of an implicit class...
5865 if (ImplicitClasses
&& HideImplicitMembers
&&
5866 printer
->in_implicit_class
)
5870 * If a username is specified, see if it is allowed or denied
5874 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
5878 * Add the group separator as needed...
5882 ippAddSeparator(con
->response
);
5887 * Send the attributes...
5890 copy_printer_attrs(con
, printer
, ra
);
5894 cupsArrayDelete(ra
);
5896 con
->response
->request
.status
.status_code
= IPP_OK
;
5901 * 'get_subscription_attrs()' - Get subscription attributes.
5905 get_subscription_attrs(
5906 cupsd_client_t
*con
, /* I - Client connection */
5907 int sub_id
) /* I - Subscription ID */
5909 http_status_t status
; /* Policy status */
5910 cupsd_subscription_t
*sub
; /* Subscription */
5911 cups_array_t
*ra
; /* Requested attributes array */
5914 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5915 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
5916 con
, con
->http
.fd
, sub_id
);
5919 * Is the subscription ID valid?
5922 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
5925 * Bad subscription ID...
5928 send_ipp_status(con
, IPP_NOT_FOUND
,
5929 _("notify-subscription-id %d no good!"), sub_id
);
5937 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5939 con
, sub
->owner
)) != HTTP_OK
)
5941 send_http_error(con
, status
);
5946 * Copy the subscription attributes to the response using the
5947 * requested-attributes attribute that may be provided by the client.
5950 ra
= create_requested_array(con
->request
);
5952 copy_subscription_attrs(con
, sub
, ra
);
5954 cupsArrayDelete(ra
);
5956 con
->response
->request
.status
.status_code
= IPP_OK
;
5961 * 'get_subscriptions()' - Get subscriptions.
5965 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
5966 ipp_attribute_t
*uri
) /* I - Printer/job URI */
5968 http_status_t status
; /* Policy status */
5969 int count
; /* Number of subscriptions */
5970 int limit
; /* Limit */
5971 cupsd_subscription_t
*sub
; /* Subscription */
5972 cups_array_t
*ra
; /* Requested attributes array */
5973 ipp_attribute_t
*attr
; /* Attribute */
5974 cups_ptype_t dtype
; /* Destination type (printer or class) */
5975 char scheme
[HTTP_MAX_URI
],
5976 /* Scheme portion of URI */
5977 username
[HTTP_MAX_URI
],
5978 /* Username portion of URI */
5980 /* Host portion of URI */
5981 resource
[HTTP_MAX_URI
];
5982 /* Resource portion of URI */
5983 int port
; /* Port portion of URI */
5984 cupsd_job_t
*job
; /* Job pointer */
5985 cupsd_printer_t
*printer
; /* Printer */
5988 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5989 "get_subscriptions(con=%p[%d], uri=%s)",
5990 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5993 * Is the destination valid?
5996 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5997 sizeof(scheme
), username
, sizeof(username
), host
,
5998 sizeof(host
), &port
, resource
, sizeof(resource
));
6000 if (!strcmp(resource
, "/") ||
6001 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
6002 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
6003 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
6008 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
6011 job
= cupsdFindJob(atoi(resource
+ 6));
6015 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
6020 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6026 send_ipp_status(con
, IPP_NOT_FOUND
,
6027 _("The printer or class was not found."));
6030 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
6031 IPP_TAG_INTEGER
)) != NULL
)
6033 job
= cupsdFindJob(attr
->values
[0].integer
);
6037 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
6038 attr
->values
[0].integer
);
6049 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
6051 con
, NULL
)) != HTTP_OK
)
6053 send_http_error(con
, status
);
6058 * Copy the subscription attributes to the response using the
6059 * requested-attributes attribute that may be provided by the client.
6062 ra
= create_requested_array(con
->request
);
6064 if ((attr
= ippFindAttribute(con
->request
, "limit",
6065 IPP_TAG_INTEGER
)) != NULL
)
6066 limit
= attr
->values
[0].integer
;
6071 * See if we only want to see subscriptions for a specific user...
6074 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
6075 IPP_TAG_BOOLEAN
)) != NULL
&&
6076 attr
->values
[0].boolean
)
6077 strlcpy(username
, get_username(con
), sizeof(username
));
6081 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
6083 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
6084 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
6085 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
6087 ippAddSeparator(con
->response
);
6088 copy_subscription_attrs(con
, sub
, ra
);
6091 if (limit
&& count
>= limit
)
6095 cupsArrayDelete(ra
);
6098 con
->response
->request
.status
.status_code
= IPP_OK
;
6100 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
6105 * 'get_username()' - Get the username associated with a request.
6108 static const char * /* O - Username */
6109 get_username(cupsd_client_t
*con
) /* I - Connection */
6111 ipp_attribute_t
*attr
; /* Attribute */
6114 if (con
->username
[0])
6115 return (con
->username
);
6116 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6117 IPP_TAG_NAME
)) != NULL
)
6118 return (attr
->values
[0].string
.text
);
6120 return ("anonymous");
6125 * 'hold_job()' - Hold a print job.
6129 hold_job(cupsd_client_t
*con
, /* I - Client connection */
6130 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
6132 ipp_attribute_t
*attr
, /* Current job-hold-until */
6133 *newattr
; /* New job-hold-until */
6134 int jobid
; /* Job ID */
6135 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6136 username
[HTTP_MAX_URI
], /* Username portion of URI */
6137 host
[HTTP_MAX_URI
], /* Host portion of URI */
6138 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6139 int port
; /* Port portion of URI */
6140 cupsd_job_t
*job
; /* Job information */
6143 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
6144 uri
->values
[0].string
.text
);
6147 * See if we have a job URI or a printer URI...
6150 if (!strcmp(uri
->name
, "printer-uri"))
6153 * Got a printer URI; see if we also have a job-id attribute...
6156 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6157 IPP_TAG_INTEGER
)) == NULL
)
6159 send_ipp_status(con
, IPP_BAD_REQUEST
,
6160 _("Got a printer-uri attribute but no job-id!"));
6164 jobid
= attr
->values
[0].integer
;
6169 * Got a job URI; parse it to get the job ID...
6172 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6173 sizeof(method
), username
, sizeof(username
), host
,
6174 sizeof(host
), &port
, resource
, sizeof(resource
));
6176 if (strncmp(resource
, "/jobs/", 6))
6182 send_ipp_status(con
, IPP_BAD_REQUEST
,
6183 _("Bad job-uri attribute \"%s\"!"),
6184 uri
->values
[0].string
.text
);
6188 jobid
= atoi(resource
+ 6);
6192 * See if the job exists...
6195 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6198 * Nope - return a "not found" error...
6201 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6206 * See if the job is owned by the requesting user...
6209 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6211 send_http_error(con
, HTTP_UNAUTHORIZED
);
6216 * Hold the job and return...
6221 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
6222 IPP_TAG_KEYWORD
)) == NULL
)
6223 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
6225 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
6226 IPP_TAG_KEYWORD
)) == NULL
)
6227 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
6232 * Free the old hold value and copy the new one over...
6235 _cupsStrFree(attr
->values
[0].string
.text
);
6239 attr
->value_tag
= newattr
->value_tag
;
6240 attr
->values
[0].string
.text
=
6241 _cupsStrAlloc(newattr
->values
[0].string
.text
);
6245 attr
->value_tag
= IPP_TAG_KEYWORD
;
6246 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
6250 * Hold job until specified time...
6253 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
6256 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was held by \"%s\".", jobid
,
6259 con
->response
->request
.status
.status_code
= IPP_OK
;
6264 * 'move_job()' - Move a job to a new destination.
6268 move_job(cupsd_client_t
*con
, /* I - Client connection */
6269 ipp_attribute_t
*uri
) /* I - Job URI */
6271 http_status_t status
; /* Policy status */
6272 ipp_attribute_t
*attr
; /* Current attribute */
6273 int jobid
; /* Job ID */
6274 cupsd_job_t
*job
; /* Current job */
6275 const char *src
, /* Source printer/class */
6276 *dest
; /* Destination */
6277 cups_ptype_t stype
, /* Source type (printer or class) */
6278 dtype
; /* Destination type (printer or class) */
6279 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6280 username
[HTTP_MAX_URI
], /* Username portion of URI */
6281 host
[HTTP_MAX_URI
], /* Host portion of URI */
6282 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6283 int port
; /* Port portion of URI */
6284 cupsd_printer_t
*sprinter
, /* Source printer */
6285 *dprinter
; /* Destination printer */
6288 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
6289 uri
->values
[0].string
.text
);
6292 * Get the new printer or class...
6295 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
6296 IPP_TAG_URI
)) == NULL
)
6299 * Need job-printer-uri...
6302 send_ipp_status(con
, IPP_BAD_REQUEST
,
6303 _("job-printer-uri attribute missing!"));
6307 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
,
6308 &dprinter
)) == NULL
)
6314 send_ipp_status(con
, IPP_NOT_FOUND
,
6315 _("The printer or class was not found."));
6323 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
6326 send_http_error(con
, status
);
6331 * See if we have a job URI or a printer URI...
6334 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6335 sizeof(scheme
), username
, sizeof(username
), host
,
6336 sizeof(host
), &port
, resource
, sizeof(resource
));
6338 if (!strcmp(uri
->name
, "printer-uri"))
6341 * Got a printer URI; see if we also have a job-id attribute...
6344 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6345 IPP_TAG_INTEGER
)) == NULL
)
6351 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
6352 &sprinter
)) == NULL
)
6358 send_ipp_status(con
, IPP_NOT_FOUND
,
6359 _("The printer or class was not found."));
6368 * Otherwise, just move a single job...
6371 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
6374 * Nope - return a "not found" error...
6377 send_ipp_status(con
, IPP_NOT_FOUND
,
6378 _("Job #%d does not exist!"), attr
->values
[0].integer
);
6384 * Job found, initialize source pointers...
6395 * Got a job URI; parse it to get the job ID...
6398 if (strncmp(resource
, "/jobs/", 6))
6404 send_ipp_status(con
, IPP_BAD_REQUEST
,
6405 _("Bad job-uri attribute \"%s\"!"),
6406 uri
->values
[0].string
.text
);
6411 * See if the job exists...
6414 jobid
= atoi(resource
+ 6);
6416 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6419 * Nope - return a "not found" error...
6422 send_ipp_status(con
, IPP_NOT_FOUND
,
6423 _("Job #%d does not exist!"), jobid
);
6429 * Job found, initialize source pointers...
6438 * Now move the job or jobs...
6444 * See if the job has been completed...
6447 if (job
->state_value
> IPP_JOB_STOPPED
)
6450 * Return a "not-possible" error...
6453 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6454 _("Job #%d is finished and cannot be altered!"),
6460 * See if the job is owned by the requesting user...
6463 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6465 send_http_error(con
, HTTP_UNAUTHORIZED
);
6470 * Move the job to a different printer or class...
6473 cupsdMoveJob(job
, dprinter
);
6478 * Got the source printer, now look through the jobs...
6481 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
6483 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
6486 * See if the job is pointing at the source printer or has not been
6490 if (strcasecmp(job
->dest
, src
) ||
6491 job
->state_value
> IPP_JOB_STOPPED
)
6495 * See if the job can be moved by the requesting user...
6498 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6502 * Move the job to a different printer or class...
6505 cupsdMoveJob(job
, dprinter
);
6510 * Start jobs if possible...
6516 * Return with "everything is OK" status...
6519 con
->response
->request
.status
.status_code
= IPP_OK
;
6524 * 'ppd_add_default()' - Add a PPD default choice.
6527 static int /* O - Number of defaults */
6529 const char *option
, /* I - Option name */
6530 const char *choice
, /* I - Choice name */
6531 int num_defaults
, /* I - Number of defaults */
6532 ppd_default_t
**defaults
) /* IO - Defaults */
6534 int i
; /* Looping var */
6535 ppd_default_t
*temp
; /* Temporary defaults array */
6539 * First check if the option already has a default value; the PPD spec
6540 * says that the first one is used...
6543 for (i
= 0, temp
= *defaults
; i
< num_defaults
; i
++)
6544 if (!strcmp(option
, temp
[i
].option
))
6545 return (num_defaults
);
6548 * Now add the option...
6551 if (num_defaults
== 0)
6552 temp
= malloc(sizeof(ppd_default_t
));
6554 temp
= realloc(*defaults
, (num_defaults
+ 1) * sizeof(ppd_default_t
));
6558 cupsdLogMessage(CUPSD_LOG_ERROR
, "ppd_add_default: Unable to add default value for \"%s\" - %s",
6559 option
, strerror(errno
));
6560 return (num_defaults
);
6564 temp
+= num_defaults
;
6566 strlcpy(temp
->option
, option
, sizeof(temp
->option
));
6567 strlcpy(temp
->choice
, choice
, sizeof(temp
->choice
));
6569 return (num_defaults
+ 1);
6574 * 'ppd_parse_line()' - Parse a PPD default line.
6577 static int /* O - 0 on success, -1 on failure */
6578 ppd_parse_line(const char *line
, /* I - Line */
6579 char *option
, /* O - Option name */
6580 int olen
, /* I - Size of option name */
6581 char *choice
, /* O - Choice name */
6582 int clen
) /* I - Size of choice name */
6585 * Verify this is a default option line...
6588 if (strncmp(line
, "*Default", 8))
6592 * Read the option name...
6595 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
6605 * Skip everything else up to the colon (:)...
6608 while (*line
&& *line
!= ':')
6617 * Now grab the option choice, skipping leading whitespace...
6620 while (isspace(*line
& 255))
6623 for (clen
--; isalnum(*line
& 255); line
++)
6633 * Return with no errors...
6641 * 'print_job()' - Print a file to a printer or class.
6645 print_job(cupsd_client_t
*con
, /* I - Client connection */
6646 ipp_attribute_t
*uri
) /* I - Printer URI */
6648 ipp_attribute_t
*attr
; /* Current attribute */
6649 ipp_attribute_t
*format
; /* Document-format attribute */
6650 const char *default_format
; /* document-format-default value */
6651 cupsd_job_t
*job
; /* New job */
6652 char filename
[1024]; /* Job filename */
6653 mime_type_t
*filetype
; /* Type of file */
6654 char super
[MIME_MAX_SUPER
], /* Supertype of file */
6655 type
[MIME_MAX_TYPE
], /* Subtype of file */
6656 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
6657 /* Textual name of mime type */
6658 cupsd_printer_t
*printer
; /* Printer data */
6659 struct stat fileinfo
; /* File information */
6660 int kbytes
; /* Size of file */
6661 int compression
; /* Document compression */
6664 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
6665 uri
->values
[0].string
.text
);
6668 * Validate print file attributes, for now just document-format and
6669 * compression (CUPS only supports "none" and "gzip")...
6672 compression
= CUPS_FILE_NONE
;
6674 if ((attr
= ippFindAttribute(con
->request
, "compression",
6675 IPP_TAG_KEYWORD
)) != NULL
)
6677 if (strcmp(attr
->values
[0].string
.text
, "none")
6679 && strcmp(attr
->values
[0].string
.text
, "gzip")
6680 #endif /* HAVE_LIBZ */
6683 send_ipp_status(con
, IPP_ATTRIBUTES
,
6684 _("Unsupported compression \"%s\"!"),
6685 attr
->values
[0].string
.text
);
6686 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6687 "compression", NULL
, attr
->values
[0].string
.text
);
6692 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
6693 compression
= CUPS_FILE_GZIP
;
6694 #endif /* HAVE_LIBZ */
6698 * Do we have a file to print?
6703 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
6708 * Is the destination valid?
6711 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
6717 send_ipp_status(con
, IPP_NOT_FOUND
,
6718 _("The printer or class was not found."));
6723 * Is it a format we support?
6726 if ((format
= ippFindAttribute(con
->request
, "document-format",
6727 IPP_TAG_MIMETYPE
)) != NULL
)
6730 * Grab format from client...
6733 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
6736 send_ipp_status(con
, IPP_BAD_REQUEST
,
6737 _("Could not scan type \"%s\"!"),
6738 format
->values
[0].string
.text
);
6742 else if ((default_format
= cupsGetOption("document-format",
6743 printer
->num_options
,
6744 printer
->options
)) != NULL
)
6747 * Use default document format...
6750 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
6752 send_ipp_status(con
, IPP_BAD_REQUEST
,
6753 _("Could not scan type \"%s\"!"),
6764 strcpy(super
, "application");
6765 strcpy(type
, "octet-stream");
6768 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
6771 * Auto-type the file...
6774 ipp_attribute_t
*doc_name
; /* document-name attribute */
6777 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
6779 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
6780 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
6781 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
6785 filetype
= mimeType(MimeDatabase
, super
, type
);
6788 filetype
= mimeType(MimeDatabase
, super
, type
);
6792 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
6795 * Replace the document-format attribute value with the auto-typed or
6799 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
6804 _cupsStrFree(format
->values
[0].string
.text
);
6806 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
6809 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
6810 "document-format", NULL
, mimetype
);
6814 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
6815 _("Unsupported format \'%s/%s\'!"), super
, type
);
6816 cupsdLogMessage(CUPSD_LOG_INFO
,
6817 "Hint: Do you have the raw file printing rules enabled?");
6820 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
6821 "document-format", NULL
, format
->values
[0].string
.text
);
6826 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: request file type is %s/%s.",
6827 filetype
->super
, filetype
->type
);
6830 * Read any embedded job ticket info from PS files...
6833 if (!strcasecmp(filetype
->super
, "application") &&
6834 !strcasecmp(filetype
->type
, "postscript"))
6835 read_ps_job_ticket(con
);
6838 * Create the job object...
6841 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
6845 * Update quota data...
6848 if (stat(con
->filename
, &fileinfo
))
6851 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
6853 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6855 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
6856 IPP_TAG_INTEGER
)) != NULL
)
6857 attr
->values
[0].integer
+= kbytes
;
6860 * Add the job file...
6863 if (add_file(con
, job
, filetype
, compression
))
6866 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
6868 rename(con
->filename
, filename
);
6869 cupsdClearString(&con
->filename
);
6872 * See if we need to add the ending sheet...
6875 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
6877 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
6878 attr
&& attr
->num_values
> 1)
6884 cupsdLogMessage(CUPSD_LOG_INFO
, "Adding end banner page \"%s\" to job %d.",
6885 attr
->values
[1].string
.text
, job
->id
);
6887 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
6889 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6893 * Log and save the job...
6896 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d queued on \"%s\" by \"%s\".", job
->id
,
6897 job
->dest
, job
->username
);
6898 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Job %d hold_until = %d", job
->id
,
6899 (int)job
->hold_until
);
6904 * Start the job if possible...
6912 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
6914 * This function only gets called when printing a single PostScript
6915 * file using the Print-Job operation. It doesn't work for Create-Job +
6916 * Send-File, since the job attributes need to be set at job creation
6917 * time for banners to work. The embedded PS job ticket stuff is here
6918 * only to allow the Windows printer driver for CUPS to pass in JCL
6919 * options and IPP attributes which otherwise would be lost.
6921 * The format of a PS job ticket is simple:
6923 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
6925 * %cupsJobTicket: attr1=value1
6926 * %cupsJobTicket: attr2=value2
6928 * %cupsJobTicket: attrN=valueN
6930 * Job ticket lines must appear immediately after the first line that
6931 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
6932 * looking for job ticket info when it finds a line that does not begin
6933 * with "%cupsJobTicket:".
6935 * The maximum length of a job ticket line, including the prefix, is
6936 * 255 characters to conform with the Adobe DSC.
6938 * Read-only attributes are rejected with a notice to the error log in
6939 * case a malicious user tries anything. Since the job ticket is read
6940 * prior to attribute validation in print_job(), job ticket attributes
6941 * will go through the same validation as IPP attributes...
6945 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
6947 cups_file_t
*fp
; /* File to read from */
6948 char line
[256]; /* Line data */
6949 int num_options
; /* Number of options */
6950 cups_option_t
*options
; /* Options */
6951 ipp_t
*ticket
; /* New attributes */
6952 ipp_attribute_t
*attr
, /* Current attribute */
6953 *attr2
, /* Job attribute */
6954 *prev2
; /* Previous job attribute */
6958 * First open the print file...
6961 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
6963 cupsdLogMessage(CUPSD_LOG_ERROR
,
6964 "read_ps_job_ticket: Unable to open PostScript print file "
6971 * Skip the first line...
6974 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
6976 cupsdLogMessage(CUPSD_LOG_ERROR
,
6977 "read_ps_job_ticket: Unable to read from PostScript print "
6984 if (strncmp(line
, "%!PS-Adobe-", 11))
6987 * Not a DSC-compliant file, so no job ticket info will be available...
6995 * Read job ticket info from the file...
7001 while (cupsFileGets(fp
, line
, sizeof(line
)))
7004 * Stop at the first non-ticket line...
7007 if (strncmp(line
, "%cupsJobTicket:", 15))
7011 * Add the options to the option array...
7014 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
7018 * Done with the file; see if we have any options...
7023 if (num_options
== 0)
7027 * OK, convert the options to an attribute list, and apply them to
7032 cupsEncodeOptions(ticket
, num_options
, options
);
7035 * See what the user wants to change.
7038 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
7040 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
7043 if (!strcmp(attr
->name
, "job-originating-host-name") ||
7044 !strcmp(attr
->name
, "job-originating-user-name") ||
7045 !strcmp(attr
->name
, "job-media-sheets-completed") ||
7046 !strcmp(attr
->name
, "job-k-octets") ||
7047 !strcmp(attr
->name
, "job-id") ||
7048 !strncmp(attr
->name
, "job-state", 9) ||
7049 !strncmp(attr
->name
, "time-at-", 8))
7050 continue; /* Read-only attrs */
7052 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
7053 IPP_TAG_ZERO
)) != NULL
)
7056 * Some other value; first free the old value...
7059 if (con
->request
->attrs
== attr2
)
7061 con
->request
->attrs
= attr2
->next
;
7066 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
7067 if (prev2
->next
== attr2
)
7069 prev2
->next
= attr2
->next
;
7074 if (con
->request
->last
== attr2
)
7075 con
->request
->last
= prev2
;
7077 _ippFreeAttr(attr2
);
7081 * Add new option by copying it...
7084 copy_attribute(con
->request
, attr
, 0);
7088 * Then free the attribute list and option array...
7092 cupsFreeOptions(num_options
, options
);
7097 * 'reject_jobs()' - Reject print jobs to a printer.
7101 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
7102 ipp_attribute_t
*uri
) /* I - Printer or class URI */
7104 http_status_t status
; /* Policy status */
7105 cups_ptype_t dtype
; /* Destination type (printer or class) */
7106 cupsd_printer_t
*printer
; /* Printer data */
7107 ipp_attribute_t
*attr
; /* printer-state-message text */
7110 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
7111 con
->http
.fd
, uri
->values
[0].string
.text
);
7114 * Is the destination valid?
7117 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7123 send_ipp_status(con
, IPP_NOT_FOUND
,
7124 _("The printer or class was not found."));
7132 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7134 send_http_error(con
, status
);
7139 * Reject jobs sent to the printer...
7142 printer
->accepting
= 0;
7144 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
7145 IPP_TAG_TEXT
)) == NULL
)
7146 strcpy(printer
->state_message
, "Rejecting Jobs");
7148 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
7149 sizeof(printer
->state_message
));
7151 cupsdAddPrinterHistory(printer
);
7153 if (dtype
& CUPS_PRINTER_CLASS
)
7155 cupsdSaveAllClasses();
7157 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
7158 printer
->name
, get_username(con
));
7162 cupsdSaveAllPrinters();
7164 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
7165 printer
->name
, get_username(con
));
7169 * Everything was ok, so return OK status...
7172 con
->response
->request
.status
.status_code
= IPP_OK
;
7177 * 'release_job()' - Release a held print job.
7181 release_job(cupsd_client_t
*con
, /* I - Client connection */
7182 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7184 ipp_attribute_t
*attr
; /* Current attribute */
7185 int jobid
; /* Job ID */
7186 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7187 username
[HTTP_MAX_URI
], /* Username portion of URI */
7188 host
[HTTP_MAX_URI
], /* Host portion of URI */
7189 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7190 int port
; /* Port portion of URI */
7191 cupsd_job_t
*job
; /* Job information */
7194 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
7195 con
->http
.fd
, uri
->values
[0].string
.text
);
7198 * See if we have a job URI or a printer URI...
7201 if (!strcmp(uri
->name
, "printer-uri"))
7204 * Got a printer URI; see if we also have a job-id attribute...
7207 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7208 IPP_TAG_INTEGER
)) == NULL
)
7210 send_ipp_status(con
, IPP_BAD_REQUEST
,
7211 _("Got a printer-uri attribute but no job-id!"));
7215 jobid
= attr
->values
[0].integer
;
7220 * Got a job URI; parse it to get the job ID...
7223 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7224 sizeof(method
), username
, sizeof(username
), host
,
7225 sizeof(host
), &port
, resource
, sizeof(resource
));
7227 if (strncmp(resource
, "/jobs/", 6))
7233 send_ipp_status(con
, IPP_BAD_REQUEST
,
7234 _("Bad job-uri attribute \"%s\"!"),
7235 uri
->values
[0].string
.text
);
7239 jobid
= atoi(resource
+ 6);
7243 * See if the job exists...
7246 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7249 * Nope - return a "not found" error...
7252 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7257 * See if job is "held"...
7260 if (job
->state_value
!= IPP_JOB_HELD
)
7263 * Nope - return a "not possible" error...
7266 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
7271 * See if the job is owned by the requesting user...
7274 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7276 send_http_error(con
, HTTP_UNAUTHORIZED
);
7281 * Reset the job-hold-until value to "no-hold"...
7284 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7285 IPP_TAG_KEYWORD
)) == NULL
)
7286 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7290 _cupsStrFree(attr
->values
[0].string
.text
);
7292 attr
->value_tag
= IPP_TAG_KEYWORD
;
7293 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
7297 * Release the job and return...
7300 cupsdReleaseJob(job
);
7302 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was released by \"%s\".", jobid
,
7305 con
->response
->request
.status
.status_code
= IPP_OK
;
7310 * 'renew_subscription()' - Renew an existing subscription...
7315 cupsd_client_t
*con
, /* I - Client connection */
7316 int sub_id
) /* I - Subscription ID */
7318 http_status_t status
; /* Policy status */
7319 cupsd_subscription_t
*sub
; /* Subscription */
7320 ipp_attribute_t
*lease
; /* notify-lease-duration */
7323 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7324 "renew_subscription(con=%p[%d], sub_id=%d)",
7325 con
, con
->http
.fd
, sub_id
);
7328 * Is the subscription ID valid?
7331 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7334 * Bad subscription ID...
7337 send_ipp_status(con
, IPP_NOT_FOUND
,
7338 _("notify-subscription-id %d no good!"), sub_id
);
7345 * Job subscriptions cannot be renewed...
7348 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7349 _("Job subscriptions cannot be renewed!"));
7357 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7359 con
, sub
->owner
)) != HTTP_OK
)
7361 send_http_error(con
, status
);
7366 * Renew the subscription...
7369 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
7372 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
7374 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
7376 cupsdLogMessage(CUPSD_LOG_INFO
,
7377 "renew_subscription: Limiting notify-lease-duration to "
7380 sub
->lease
= MaxLeaseDuration
;
7383 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
7385 cupsdSaveAllSubscriptions();
7387 con
->response
->request
.status
.status_code
= IPP_OK
;
7392 * 'restart_job()' - Restart an old print job.
7396 restart_job(cupsd_client_t
*con
, /* I - Client connection */
7397 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7399 ipp_attribute_t
*attr
; /* Current attribute */
7400 int jobid
; /* Job ID */
7401 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7402 username
[HTTP_MAX_URI
], /* Username portion of URI */
7403 host
[HTTP_MAX_URI
], /* Host portion of URI */
7404 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7405 int port
; /* Port portion of URI */
7406 cupsd_job_t
*job
; /* Job information */
7409 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
7410 con
->http
.fd
, uri
->values
[0].string
.text
);
7413 * See if we have a job URI or a printer URI...
7416 if (!strcmp(uri
->name
, "printer-uri"))
7419 * Got a printer URI; see if we also have a job-id attribute...
7422 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7423 IPP_TAG_INTEGER
)) == NULL
)
7425 send_ipp_status(con
, IPP_BAD_REQUEST
,
7426 _("Got a printer-uri attribute but no job-id!"));
7430 jobid
= attr
->values
[0].integer
;
7435 * Got a job URI; parse it to get the job ID...
7438 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7439 sizeof(method
), username
, sizeof(username
), host
,
7440 sizeof(host
), &port
, resource
, sizeof(resource
));
7442 if (strncmp(resource
, "/jobs/", 6))
7448 send_ipp_status(con
, IPP_BAD_REQUEST
,
7449 _("Bad job-uri attribute \"%s\"!"),
7450 uri
->values
[0].string
.text
);
7454 jobid
= atoi(resource
+ 6);
7458 * See if the job exists...
7461 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7464 * Nope - return a "not found" error...
7467 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7472 * See if job is in any of the "completed" states...
7475 if (job
->state_value
<= IPP_JOB_PROCESSING
)
7478 * Nope - return a "not possible" error...
7481 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
7487 * See if we have retained the job files...
7492 if (!job
->attrs
||job
->num_files
== 0)
7495 * Nope - return a "not possible" error...
7498 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7499 _("Job #%d cannot be restarted - no files!"), jobid
);
7504 * See if the job is owned by the requesting user...
7507 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7509 send_http_error(con
, HTTP_UNAUTHORIZED
);
7514 * Restart the job and return...
7517 cupsdRestartJob(job
);
7519 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was restarted by \"%s\".", jobid
,
7522 con
->response
->request
.status
.status_code
= IPP_OK
;
7527 * 'save_auth_info()' - Save authentication information for a job.
7531 save_auth_info(cupsd_client_t
*con
, /* I - Client connection */
7532 cupsd_job_t
*job
) /* I - Job */
7534 int i
; /* Looping var */
7535 char filename
[1024]; /* Job authentication filename */
7536 cups_file_t
*fp
; /* Job authentication file */
7537 char line
[1024]; /* Line for file */
7541 * This function saves the in-memory authentication information for
7542 * a job so that it can be used to authenticate with a remote host.
7543 * The information is stored in a file that is readable only by the
7544 * root user. The username and password are Base-64 encoded, each
7545 * on a separate line, followed by random number (up to 1024) of
7546 * newlines to limit the amount of information that is exposed.
7548 * Because of the potential for exposing of authentication information,
7549 * this functionality is only enabled when running cupsd as root.
7551 * This caching only works for the Basic and BasicDigest authentication
7552 * types. Digest authentication cannot be cached this way, and in
7553 * the future Kerberos authentication may make all of this obsolete.
7555 * Authentication information is saved whenever an authenticated
7556 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
7559 * This information is deleted after a job is completed or canceled,
7560 * so reprints may require subsequent re-authentication.
7567 * Create the authentication file and change permissions...
7570 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
7571 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
7573 cupsdLogMessage(CUPSD_LOG_ERROR
,
7574 "Unable to save authentication info to \"%s\" - %s",
7575 filename
, strerror(errno
));
7579 fchown(cupsFileNumber(fp
), 0, 0);
7580 fchmod(cupsFileNumber(fp
), 0400);
7583 * Write the authenticated username...
7586 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
7587 cupsFilePrintf(fp
, "%s\n", line
);
7590 * Write the authenticated password...
7593 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
7594 cupsFilePrintf(fp
, "%s\n", line
);
7597 * Write a random number of newlines to the end of the file...
7600 for (i
= (rand() % 1024); i
>= 0; i
--)
7601 cupsFilePutChar(fp
, '\n');
7604 * Close the file and return...
7612 * 'send_document()' - Send a file to a printer or class.
7616 send_document(cupsd_client_t
*con
, /* I - Client connection */
7617 ipp_attribute_t
*uri
) /* I - Printer URI */
7619 ipp_attribute_t
*attr
; /* Current attribute */
7620 ipp_attribute_t
*format
; /* Document-format attribute */
7621 const char *default_format
;/* document-format-default value */
7622 int jobid
; /* Job ID number */
7623 cupsd_job_t
*job
; /* Current job */
7624 char job_uri
[HTTP_MAX_URI
],
7626 method
[HTTP_MAX_URI
],
7627 /* Method portion of URI */
7628 username
[HTTP_MAX_URI
],
7629 /* Username portion of URI */
7631 /* Host portion of URI */
7632 resource
[HTTP_MAX_URI
];
7633 /* Resource portion of URI */
7634 int port
; /* Port portion of URI */
7635 mime_type_t
*filetype
; /* Type of file */
7636 char super
[MIME_MAX_SUPER
],
7637 /* Supertype of file */
7638 type
[MIME_MAX_TYPE
],
7639 /* Subtype of file */
7640 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7641 /* Textual name of mime type */
7642 char filename
[1024]; /* Job filename */
7643 cupsd_printer_t
*printer
; /* Current printer */
7644 struct stat fileinfo
; /* File information */
7645 int kbytes
; /* Size of file */
7646 int compression
; /* Type of compression */
7649 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
7650 con
->http
.fd
, uri
->values
[0].string
.text
);
7653 * See if we have a job URI or a printer URI...
7656 if (!strcmp(uri
->name
, "printer-uri"))
7659 * Got a printer URI; see if we also have a job-id attribute...
7662 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7663 IPP_TAG_INTEGER
)) == NULL
)
7665 send_ipp_status(con
, IPP_BAD_REQUEST
,
7666 _("Got a printer-uri attribute but no job-id!"));
7670 jobid
= attr
->values
[0].integer
;
7675 * Got a job URI; parse it to get the job ID...
7678 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7679 sizeof(method
), username
, sizeof(username
), host
,
7680 sizeof(host
), &port
, resource
, sizeof(resource
));
7682 if (strncmp(resource
, "/jobs/", 6))
7688 send_ipp_status(con
, IPP_BAD_REQUEST
,
7689 _("Bad job-uri attribute \"%s\"!"),
7690 uri
->values
[0].string
.text
);
7694 jobid
= atoi(resource
+ 6);
7698 * See if the job exists...
7701 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7704 * Nope - return a "not found" error...
7707 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7711 printer
= cupsdFindDest(job
->dest
);
7714 * See if the job is owned by the requesting user...
7717 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7719 send_http_error(con
, HTTP_UNAUTHORIZED
);
7724 * OK, see if the client is sending the document compressed - CUPS
7725 * only supports "none" and "gzip".
7728 compression
= CUPS_FILE_NONE
;
7730 if ((attr
= ippFindAttribute(con
->request
, "compression",
7731 IPP_TAG_KEYWORD
)) != NULL
)
7733 if (strcmp(attr
->values
[0].string
.text
, "none")
7735 && strcmp(attr
->values
[0].string
.text
, "gzip")
7736 #endif /* HAVE_LIBZ */
7739 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
7740 attr
->values
[0].string
.text
);
7741 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7742 "compression", NULL
, attr
->values
[0].string
.text
);
7747 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7748 compression
= CUPS_FILE_GZIP
;
7749 #endif /* HAVE_LIBZ */
7753 * Do we have a file to print?
7758 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7763 * Is it a format we support?
7766 if ((format
= ippFindAttribute(con
->request
, "document-format",
7767 IPP_TAG_MIMETYPE
)) != NULL
)
7770 * Grab format from client...
7773 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
7775 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
7776 format
->values
[0].string
.text
);
7780 else if ((default_format
= cupsGetOption("document-format",
7781 printer
->num_options
,
7782 printer
->options
)) != NULL
)
7785 * Use default document format...
7788 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
7790 send_ipp_status(con
, IPP_BAD_REQUEST
,
7791 _("Could not scan type \"%s\"!"),
7799 * No document format attribute? Auto-type it!
7802 strcpy(super
, "application");
7803 strcpy(type
, "octet-stream");
7806 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7809 * Auto-type the file...
7812 ipp_attribute_t
*doc_name
; /* document-name attribute */
7815 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
7817 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7818 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7819 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7823 filetype
= mimeType(MimeDatabase
, super
, type
);
7826 filetype
= mimeType(MimeDatabase
, super
, type
);
7830 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
7833 * Replace the document-format attribute value with the auto-typed or
7837 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7842 _cupsStrFree(format
->values
[0].string
.text
);
7844 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7847 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7848 "document-format", NULL
, mimetype
);
7852 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7853 _("Unsupported format \'%s/%s\'!"), super
, type
);
7854 cupsdLogMessage(CUPSD_LOG_INFO
,
7855 "Hint: Do you have the raw file printing rules enabled?");
7858 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7859 "document-format", NULL
, format
->values
[0].string
.text
);
7864 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
7866 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7869 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7870 _("Unsupported format \'%s\'!"), mimetype
);
7872 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7873 "document-format", NULL
, mimetype
);
7878 cupsdLogMessage(CUPSD_LOG_DEBUG
,
7879 "send_document: request file type is %s/%s.",
7880 filetype
->super
, filetype
->type
);
7883 * Add the file to the job...
7888 if (add_file(con
, job
, filetype
, compression
))
7891 if (stat(con
->filename
, &fileinfo
))
7894 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7896 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7898 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7899 IPP_TAG_INTEGER
)) != NULL
)
7900 attr
->values
[0].integer
+= kbytes
;
7902 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
7904 rename(con
->filename
, filename
);
7906 cupsdClearString(&con
->filename
);
7908 cupsdLogMessage(CUPSD_LOG_INFO
,
7909 "File of type %s/%s queued in job #%d by \"%s\".",
7910 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
7913 * Start the job if this is the last document...
7916 if ((attr
= ippFindAttribute(con
->request
, "last-document",
7917 IPP_TAG_BOOLEAN
)) != NULL
&&
7918 attr
->values
[0].boolean
)
7921 * See if we need to add the ending sheet...
7925 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
7926 (attr
= ippFindAttribute(job
->attrs
, "job-sheets",
7927 IPP_TAG_ZERO
)) != NULL
&&
7928 attr
->num_values
> 1)
7934 cupsdLogMessage(CUPSD_LOG_INFO
,
7935 "Adding end banner page \"%s\" to job %d.",
7936 attr
->values
[1].string
.text
, job
->id
);
7938 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
7940 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7943 if (job
->state_value
== IPP_JOB_STOPPED
)
7945 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7946 job
->state_value
= IPP_JOB_PENDING
;
7948 else if (job
->state_value
== IPP_JOB_HELD
)
7950 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7951 IPP_TAG_KEYWORD
)) == NULL
)
7952 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7954 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
7956 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7957 job
->state_value
= IPP_JOB_PENDING
;
7964 * Start the job if possible... Since cupsdCheckJobs() can cancel a
7965 * job if it doesn't print, we need to re-find the job afterwards...
7972 job
= cupsdFindJob(jobid
);
7976 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7977 IPP_TAG_KEYWORD
)) == NULL
)
7978 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7980 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
7982 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
7983 job
->state_value
= IPP_JOB_HELD
;
7984 job
->hold_until
= time(NULL
) + 60;
7990 * Fill in the response info...
7993 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
7996 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
7999 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
8001 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
8002 job
? job
->state_value
: IPP_JOB_CANCELLED
);
8003 add_job_state_reasons(con
, job
);
8005 con
->response
->request
.status
.status_code
= IPP_OK
;
8010 * 'send_http_error()' - Send a HTTP error back to the IPP client.
8014 send_http_error(cupsd_client_t
*con
, /* I - Client connection */
8015 http_status_t status
) /* I - HTTP status code */
8017 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
8018 ippOpString(con
->request
->request
.op
.operation_id
),
8019 httpStatus(status
));
8021 cupsdSendError(con
, status
);
8023 ippDelete(con
->response
);
8024 con
->response
= NULL
;
8031 * 'send_ipp_status()' - Send a status back to the IPP client.
8035 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
8036 ipp_status_t status
, /* I - IPP status code */
8037 const char *message
, /* I - Status message */
8038 ...) /* I - Additional args as needed */
8040 va_list ap
; /* Pointer to additional args */
8041 char formatted
[1024]; /* Formatted errror message */
8046 va_start(ap
, message
);
8047 vsnprintf(formatted
, sizeof(formatted
),
8048 _cupsLangString(con
->language
, message
), ap
);
8051 cupsdLogMessage(status
>= IPP_BAD_REQUEST
? CUPSD_LOG_ERROR
: CUPSD_LOG_INFO
,
8053 ippOpString(con
->request
->request
.op
.operation_id
),
8054 ippErrorString(status
), formatted
);
8057 cupsdLogMessage(status
>= IPP_BAD_REQUEST
? CUPSD_LOG_ERROR
: CUPSD_LOG_INFO
,
8059 ippOpString(con
->request
->request
.op
.operation_id
),
8060 ippErrorString(status
));
8062 con
->response
->request
.status
.status_code
= status
;
8064 if (ippFindAttribute(con
->response
, "attributes-charset",
8065 IPP_TAG_ZERO
) == NULL
)
8066 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
8067 "attributes-charset", NULL
, DefaultCharset
);
8069 if (ippFindAttribute(con
->response
, "attributes-natural-language",
8070 IPP_TAG_ZERO
) == NULL
)
8071 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
8072 "attributes-natural-language", NULL
, DefaultLanguage
);
8075 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
8076 "status-message", NULL
, formatted
);
8081 * 'set_default()' - Set the default destination...
8085 set_default(cupsd_client_t
*con
, /* I - Client connection */
8086 ipp_attribute_t
*uri
) /* I - Printer URI */
8088 http_status_t status
; /* Policy status */
8089 cups_ptype_t dtype
; /* Destination type (printer or class) */
8090 cupsd_printer_t
*printer
; /* Printer */
8093 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
8094 con
->http
.fd
, uri
->values
[0].string
.text
);
8097 * Is the destination valid?
8100 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8106 send_ipp_status(con
, IPP_NOT_FOUND
,
8107 _("The printer or class was not found."));
8115 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
8117 send_http_error(con
, status
);
8122 * Set it as the default...
8125 DefaultPrinter
= printer
;
8127 cupsdSaveAllPrinters();
8128 cupsdSaveAllClasses();
8130 cupsdWritePrintcap();
8132 cupsdLogMessage(CUPSD_LOG_INFO
,
8133 "Default destination set to \"%s\" by \"%s\".",
8134 printer
->name
, get_username(con
));
8137 * Everything was ok, so return OK status...
8140 con
->response
->request
.status
.status_code
= IPP_OK
;
8145 * 'set_job_attrs()' - Set job attributes.
8149 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
8150 ipp_attribute_t
*uri
) /* I - Job URI */
8152 ipp_attribute_t
*attr
, /* Current attribute */
8153 *attr2
; /* Job attribute */
8154 int jobid
; /* Job ID */
8155 cupsd_job_t
*job
; /* Current job */
8156 char method
[HTTP_MAX_URI
],
8157 /* Method portion of URI */
8158 username
[HTTP_MAX_URI
],
8159 /* Username portion of URI */
8161 /* Host portion of URI */
8162 resource
[HTTP_MAX_URI
];
8163 /* Resource portion of URI */
8164 int port
; /* Port portion of URI */
8167 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
8168 con
->http
.fd
, uri
->values
[0].string
.text
);
8171 * Start with "everything is OK" status...
8174 con
->response
->request
.status
.status_code
= IPP_OK
;
8177 * See if we have a job URI or a printer URI...
8180 if (!strcmp(uri
->name
, "printer-uri"))
8183 * Got a printer URI; see if we also have a job-id attribute...
8186 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8187 IPP_TAG_INTEGER
)) == NULL
)
8189 send_ipp_status(con
, IPP_BAD_REQUEST
,
8190 _("Got a printer-uri attribute but no job-id!"));
8194 jobid
= attr
->values
[0].integer
;
8199 * Got a job URI; parse it to get the job ID...
8202 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8203 sizeof(method
), username
, sizeof(username
), host
,
8204 sizeof(host
), &port
, resource
, sizeof(resource
));
8206 if (strncmp(resource
, "/jobs/", 6))
8212 send_ipp_status(con
, IPP_BAD_REQUEST
,
8213 _("Bad job-uri attribute \"%s\"!"),
8214 uri
->values
[0].string
.text
);
8218 jobid
= atoi(resource
+ 6);
8222 * See if the job exists...
8225 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8228 * Nope - return a "not found" error...
8231 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8236 * See if the job has been completed...
8239 if (job
->state_value
> IPP_JOB_STOPPED
)
8242 * Return a "not-possible" error...
8245 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8246 _("Job #%d is finished and cannot be altered!"), jobid
);
8251 * See if the job is owned by the requesting user...
8254 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8256 send_http_error(con
, HTTP_UNAUTHORIZED
);
8261 * See what the user wants to change.
8266 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8268 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8271 if (!strcmp(attr
->name
, "attributes-charset") ||
8272 !strcmp(attr
->name
, "attributes-natural-language") ||
8273 !strcmp(attr
->name
, "document-compression") ||
8274 !strcmp(attr
->name
, "document-format") ||
8275 !strcmp(attr
->name
, "job-detailed-status-messages") ||
8276 !strcmp(attr
->name
, "job-document-access-errors") ||
8277 !strcmp(attr
->name
, "job-id") ||
8278 !strcmp(attr
->name
, "job-k-octets") ||
8279 !strcmp(attr
->name
, "job-originating-host-name") ||
8280 !strcmp(attr
->name
, "job-originating-user-name") ||
8281 !strcmp(attr
->name
, "job-printer-up-time") ||
8282 !strcmp(attr
->name
, "job-printer-uri") ||
8283 !strcmp(attr
->name
, "job-sheets") ||
8284 !strcmp(attr
->name
, "job-state-message") ||
8285 !strcmp(attr
->name
, "job-state-reasons") ||
8286 !strcmp(attr
->name
, "job-uri") ||
8287 !strcmp(attr
->name
, "number-of-documents") ||
8288 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
8289 !strcmp(attr
->name
, "output-device-assigned") ||
8290 !strncmp(attr
->name
, "date-time-at-", 13) ||
8291 !strncmp(attr
->name
, "job-impressions", 15) ||
8292 !strncmp(attr
->name
, "job-k-octets", 12) ||
8293 !strncmp(attr
->name
, "job-media-sheets", 16) ||
8294 !strncmp(attr
->name
, "time-at-", 8))
8300 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
8301 _("%s cannot be changed."), attr
->name
);
8303 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8304 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8309 if (!strcmp(attr
->name
, "job-priority"))
8312 * Change the job priority...
8315 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8317 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
8319 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8320 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8322 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
8324 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8325 _("Job is completed and cannot be changed."));
8328 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8329 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
8331 else if (!strcmp(attr
->name
, "job-state"))
8334 * Change the job state...
8337 if (attr
->value_tag
!= IPP_TAG_ENUM
)
8339 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
8341 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8342 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8346 switch (attr
->values
[0].integer
)
8348 case IPP_JOB_PENDING
:
8350 if (job
->state_value
> IPP_JOB_HELD
)
8352 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8353 _("Job state cannot be changed."));
8356 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8358 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8359 job
->state_value
= attr
->values
[0].integer
;
8363 case IPP_JOB_PROCESSING
:
8364 case IPP_JOB_STOPPED
:
8365 if (job
->state_value
!= attr
->values
[0].integer
)
8367 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8368 _("Job state cannot be changed."));
8373 case IPP_JOB_CANCELLED
:
8374 case IPP_JOB_ABORTED
:
8375 case IPP_JOB_COMPLETED
:
8376 if (job
->state_value
> IPP_JOB_PROCESSING
)
8378 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8379 _("Job state cannot be changed."));
8382 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8384 cupsdCancelJob(job
, 0);
8388 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8389 job
->state_value
= attr
->values
[0].integer
;
8397 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
8399 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8400 IPP_TAG_ZERO
)) != NULL
)
8403 * Some other value; first free the old value...
8406 if (job
->attrs
->prev
)
8407 job
->attrs
->prev
->next
= attr2
->next
;
8409 job
->attrs
->attrs
= attr2
->next
;
8411 if (job
->attrs
->last
== attr2
)
8412 job
->attrs
->last
= job
->attrs
->prev
;
8414 _ippFreeAttr(attr2
);
8417 * Then copy the attribute...
8420 copy_attribute(job
->attrs
, attr
, 0);
8423 * See if the job-name or job-hold-until is being changed.
8426 if (!strcmp(attr
->name
, "job-hold-until"))
8428 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
8430 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
8431 cupsdReleaseJob(job
);
8436 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
8439 * Delete the attribute...
8442 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8443 IPP_TAG_ZERO
)) != NULL
)
8445 if (job
->attrs
->prev
)
8446 job
->attrs
->prev
->next
= attr2
->next
;
8448 job
->attrs
->attrs
= attr2
->next
;
8450 if (attr2
== job
->attrs
->last
)
8451 job
->attrs
->last
= job
->attrs
->prev
;
8453 _ippFreeAttr(attr2
);
8459 * Add new option by copying it...
8462 copy_attribute(job
->attrs
, attr
, 0);
8473 * Start jobs if possible...
8481 * 'set_printer_defaults()' - Set printer default options from a request.
8485 set_printer_defaults(
8486 cupsd_client_t
*con
, /* I - Client connection */
8487 cupsd_printer_t
*printer
) /* I - Printer */
8489 int i
; /* Looping var */
8490 ipp_attribute_t
*attr
; /* Current attribute */
8491 int namelen
; /* Length of attribute name */
8492 char name
[256], /* New attribute name */
8493 value
[256]; /* String version of integer attrs */
8496 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8499 * Skip non-printer attributes...
8502 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
8505 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
8507 if (!strcmp(attr
->name
, "job-sheets-default"))
8510 * Only allow keywords and names...
8513 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8517 * Only allow job-sheets-default to be set when running without a
8518 * system high classification level...
8524 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
8526 if (attr
->num_values
> 1)
8527 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
8529 cupsdSetString(&printer
->job_sheets
[1], "none");
8531 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
8533 cupsdFreePrinterUsers(printer
);
8535 printer
->deny_users
= 0;
8537 if (attr
->value_tag
== IPP_TAG_NAME
&&
8538 (attr
->num_values
> 1 ||
8539 strcmp(attr
->values
[0].string
.text
, "all")))
8541 for (i
= 0; i
< attr
->num_values
; i
++)
8542 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8545 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
8547 cupsdFreePrinterUsers(printer
);
8549 printer
->deny_users
= 1;
8551 if (attr
->value_tag
== IPP_TAG_NAME
&&
8552 (attr
->num_values
> 1 ||
8553 strcmp(attr
->values
[0].string
.text
, "none")))
8555 for (i
= 0; i
< attr
->num_values
; i
++)
8556 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8559 else if (!strcmp(attr
->name
, "job-quota-period"))
8561 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8564 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
8565 attr
->values
[0].integer
);
8566 cupsdFreeQuotas(printer
);
8568 printer
->quota_period
= attr
->values
[0].integer
;
8570 else if (!strcmp(attr
->name
, "job-k-limit"))
8572 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8575 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
8576 attr
->values
[0].integer
);
8577 cupsdFreeQuotas(printer
);
8579 printer
->k_limit
= attr
->values
[0].integer
;
8581 else if (!strcmp(attr
->name
, "job-page-limit"))
8583 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8586 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
8587 attr
->values
[0].integer
);
8588 cupsdFreeQuotas(printer
);
8590 printer
->page_limit
= attr
->values
[0].integer
;
8592 else if (!strcmp(attr
->name
, "printer-op-policy"))
8594 cupsd_policy_t
*p
; /* Policy */
8597 if (attr
->value_tag
!= IPP_TAG_NAME
)
8600 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
8602 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8603 "Setting printer-op-policy to \"%s\"...",
8604 attr
->values
[0].string
.text
);
8605 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
8606 printer
->op_policy_ptr
= p
;
8610 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8611 _("Unknown printer-op-policy \"%s\"."),
8612 attr
->values
[0].string
.text
);
8616 else if (!strcmp(attr
->name
, "printer-error-policy"))
8618 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8621 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
8622 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
8623 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
8625 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8626 _("Unknown printer-error-policy \"%s\"."),
8627 attr
->values
[0].string
.text
);
8631 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8632 "Setting printer-error-policy to \"%s\"...",
8633 attr
->values
[0].string
.text
);
8634 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
8636 else if (!strcmp(attr
->name
, "notify-lease-duration-default") ||
8637 !strcmp(attr
->name
, "notify-events-default"))
8641 * Skip any other non-default attributes...
8644 namelen
= strlen(attr
->name
);
8645 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
8646 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
8650 * OK, anything else must be a user-defined default...
8653 strlcpy(name
, attr
->name
, sizeof(name
));
8654 name
[namelen
- 8] = '\0'; /* Strip "-default" */
8656 switch (attr
->value_tag
)
8658 case IPP_TAG_DELETEATTR
:
8659 printer
->num_options
= cupsRemoveOption(name
,
8660 printer
->num_options
,
8661 &(printer
->options
));
8662 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8663 "Deleting %s", attr
->name
);
8667 case IPP_TAG_KEYWORD
:
8669 printer
->num_options
= cupsAddOption(name
,
8670 attr
->values
[0].string
.text
,
8671 printer
->num_options
,
8672 &(printer
->options
));
8673 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8674 "Setting %s to \"%s\"...", attr
->name
,
8675 attr
->values
[0].string
.text
);
8678 case IPP_TAG_BOOLEAN
:
8679 printer
->num_options
= cupsAddOption(name
,
8680 attr
->values
[0].boolean
?
8682 printer
->num_options
,
8683 &(printer
->options
));
8684 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8685 "Setting %s to %s...", attr
->name
,
8686 attr
->values
[0].boolean
? "true" : "false");
8689 case IPP_TAG_INTEGER
:
8691 sprintf(value
, "%d", attr
->values
[0].integer
);
8692 printer
->num_options
= cupsAddOption(name
, value
,
8693 printer
->num_options
,
8694 &(printer
->options
));
8695 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8696 "Setting %s to %s...", attr
->name
, value
);
8699 case IPP_TAG_RANGE
:
8700 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
8701 attr
->values
[0].range
.upper
);
8702 printer
->num_options
= cupsAddOption(name
, value
,
8703 printer
->num_options
,
8704 &(printer
->options
));
8705 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8706 "Setting %s to %s...", attr
->name
, value
);
8709 case IPP_TAG_RESOLUTION
:
8710 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
8711 attr
->values
[0].resolution
.yres
,
8712 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
8714 printer
->num_options
= cupsAddOption(name
, value
,
8715 printer
->num_options
,
8716 &(printer
->options
));
8717 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8718 "Setting %s to %s...", attr
->name
, value
);
8722 /* Do nothing for other values */
8730 * 'start_printer()' - Start a printer.
8734 start_printer(cupsd_client_t
*con
, /* I - Client connection */
8735 ipp_attribute_t
*uri
) /* I - Printer URI */
8737 http_status_t status
; /* Policy status */
8738 cups_ptype_t dtype
; /* Destination type (printer or class) */
8739 cupsd_printer_t
*printer
; /* Printer data */
8742 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
8743 con
->http
.fd
, uri
->values
[0].string
.text
);
8746 * Is the destination valid?
8749 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8755 send_ipp_status(con
, IPP_NOT_FOUND
,
8756 _("The printer or class was not found."));
8764 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8766 send_http_error(con
, status
);
8771 * Start the printer...
8774 printer
->state_message
[0] = '\0';
8776 cupsdStartPrinter(printer
, 1);
8778 if (dtype
& CUPS_PRINTER_CLASS
)
8780 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
8781 printer
->name
, get_username(con
));
8782 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
8783 "Class \"%s\" started by \"%s\".", printer
->name
,
8788 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
8789 printer
->name
, get_username(con
));
8790 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
8791 "Printer \"%s\" started by \"%s\".", printer
->name
,
8798 * Everything was ok, so return OK status...
8801 con
->response
->request
.status
.status_code
= IPP_OK
;
8806 * 'stop_printer()' - Stop a printer.
8810 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
8811 ipp_attribute_t
*uri
) /* I - Printer URI */
8813 http_status_t status
; /* Policy status */
8814 cups_ptype_t dtype
; /* Destination type (printer or class) */
8815 cupsd_printer_t
*printer
; /* Printer data */
8816 ipp_attribute_t
*attr
; /* printer-state-message attribute */
8819 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
8820 con
->http
.fd
, uri
->values
[0].string
.text
);
8823 * Is the destination valid?
8826 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8832 send_ipp_status(con
, IPP_NOT_FOUND
,
8833 _("The printer or class was not found."));
8841 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8843 send_http_error(con
, status
);
8848 * Stop the printer...
8851 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8852 IPP_TAG_TEXT
)) == NULL
)
8853 strcpy(printer
->state_message
, "Paused");
8856 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8857 sizeof(printer
->state_message
));
8860 cupsdStopPrinter(printer
, 1);
8862 if (dtype
& CUPS_PRINTER_CLASS
)
8864 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
8865 printer
->name
, get_username(con
));
8866 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
8867 "Class \"%s\" stopped by \"%s\".", printer
->name
,
8872 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
8873 printer
->name
, get_username(con
));
8874 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
8875 "Printer \"%s\" stopped by \"%s\".", printer
->name
,
8880 * Everything was ok, so return OK status...
8883 con
->response
->request
.status
.status_code
= IPP_OK
;
8888 * 'url_encode_attr()' - URL-encode a string attribute.
8892 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
8893 char *buffer
,/* I - String buffer */
8894 int bufsize
)/* I - Size of buffer */
8896 int i
; /* Looping var */
8897 char *bufptr
, /* Pointer into buffer */
8898 *bufend
, /* End of buffer */
8899 *valptr
; /* Pointer into value */
8902 strlcpy(buffer
, attr
->name
, bufsize
);
8903 bufptr
= buffer
+ strlen(buffer
);
8904 bufend
= buffer
+ bufsize
- 1;
8906 for (i
= 0; i
< attr
->num_values
; i
++)
8908 if (bufptr
>= bufend
)
8916 if (bufptr
>= bufend
)
8921 for (valptr
= attr
->values
[i
].string
.text
;
8922 *valptr
&& bufptr
< bufend
;
8926 if (bufptr
>= (bufend
- 2))
8933 else if (*valptr
== '\'' || *valptr
== '\\')
8936 *bufptr
++ = *valptr
;
8939 *bufptr
++ = *valptr
;
8941 if (bufptr
>= bufend
)
8952 * 'user_allowed()' - See if a user is allowed to print to a queue.
8955 static int /* O - 0 if not allowed, 1 if allowed */
8956 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
8957 const char *username
) /* I - Username */
8959 int i
; /* Looping var */
8960 struct passwd
*pw
; /* User password data */
8963 if (p
->num_users
== 0)
8966 if (!strcmp(username
, "root"))
8969 pw
= getpwnam(username
);
8972 for (i
= 0; i
< p
->num_users
; i
++)
8974 if (p
->users
[i
][0] == '@')
8977 * Check group membership...
8980 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
8983 else if (!strcasecmp(username
, p
->users
[i
]))
8987 return ((i
< p
->num_users
) != p
->deny_users
);
8992 * 'validate_job()' - Validate printer options and destination.
8996 validate_job(cupsd_client_t
*con
, /* I - Client connection */
8997 ipp_attribute_t
*uri
) /* I - Printer URI */
8999 http_status_t status
; /* Policy status */
9000 ipp_attribute_t
*attr
; /* Current attribute */
9001 ipp_attribute_t
*format
; /* Document-format attribute */
9002 cups_ptype_t dtype
; /* Destination type (printer or class) */
9003 char super
[MIME_MAX_SUPER
],
9004 /* Supertype of file */
9005 type
[MIME_MAX_TYPE
];
9006 /* Subtype of file */
9007 cupsd_printer_t
*printer
; /* Printer */
9010 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
9011 con
->http
.fd
, uri
->values
[0].string
.text
);
9014 * OK, see if the client is sending the document compressed - CUPS
9015 * doesn't support compression yet...
9018 if ((attr
= ippFindAttribute(con
->request
, "compression",
9019 IPP_TAG_KEYWORD
)) != NULL
&&
9020 !strcmp(attr
->values
[0].string
.text
, "none"))
9022 send_ipp_status(con
, IPP_ATTRIBUTES
,
9023 _("Unsupported compression attribute %s!"),
9024 attr
->values
[0].string
.text
);
9025 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9026 "compression", NULL
, attr
->values
[0].string
.text
);
9031 * Is it a format we support?
9034 if ((format
= ippFindAttribute(con
->request
, "document-format",
9035 IPP_TAG_MIMETYPE
)) != NULL
)
9037 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
9039 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9040 format
->values
[0].string
.text
);
9044 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
9045 !mimeType(MimeDatabase
, super
, type
))
9047 cupsdLogMessage(CUPSD_LOG_INFO
,
9048 "Hint: Do you have the raw file printing rules enabled?");
9049 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9050 _("Unsupported format \"%s\"!"),
9051 format
->values
[0].string
.text
);
9052 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9053 "document-format", NULL
, format
->values
[0].string
.text
);
9059 * Is the destination valid?
9062 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9068 send_ipp_status(con
, IPP_NOT_FOUND
,
9069 _("The printer or class was not found."));
9077 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9079 send_http_error(con
, status
);
9084 * Everything was ok, so return OK status...
9087 con
->response
->request
.status
.status_code
= IPP_OK
;
9092 * 'validate_name()' - Make sure the printer name only contains valid chars.
9095 static int /* O - 0 if name is no good, 1 if name is good */
9096 validate_name(const char *name
) /* I - Name to check */
9098 const char *ptr
; /* Pointer into name */
9102 * Scan the whole name...
9105 for (ptr
= name
; *ptr
; ptr
++)
9106 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
9110 * All the characters are good; validate the length, too...
9113 return ((ptr
- name
) < 128);
9118 * 'validate_user()' - Validate the user for the request.
9121 static int /* O - 1 if permitted, 0 otherwise */
9122 validate_user(cupsd_job_t
*job
, /* I - Job */
9123 cupsd_client_t
*con
, /* I - Client connection */
9124 const char *owner
, /* I - Owner of job/resource */
9125 char *username
, /* O - Authenticated username */
9126 int userlen
) /* I - Length of username */
9128 cupsd_printer_t
*printer
; /* Printer for job */
9131 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9132 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
9134 job
? job
->id
: 0, con
->http
.fd
, owner
? owner
: "(null)",
9141 if (!con
|| !owner
|| !username
|| userlen
<= 0)
9145 * Get the best authenticated username that is available.
9148 strlcpy(username
, get_username(con
), userlen
);
9151 * Check the username against the owner...
9154 printer
= cupsdFindDest(job
->dest
);
9156 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
9157 con
, owner
) == HTTP_OK
);
9162 * End of "$Id: ipp.c 181 2006-06-22 20:01:18Z jlovell $".