4 * IPP routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 * This file contains Kerberos support code, copyright 2006 by
11 * These coded instructions, statements, and computer programs are the
12 * property of Easy Software Products and are protected by Federal
13 * copyright law. Distribution and use rights are outlined in the file
14 * "LICENSE.txt" which should have been included with this file. If this
15 * file is missing or damaged please contact Easy Software Products
18 * Attn: CUPS Licensing Information
19 * Easy Software Products
20 * 44141 Airport View Drive, Suite 204
21 * Hollywood, Maryland 20636 USA
23 * Voice: (301) 373-9600
24 * EMail: cups-info@cups.org
25 * WWW: http://www.cups.org
29 * cupsdProcessIPPRequest() - Process an incoming IPP request...
30 * accept_jobs() - Accept print jobs to a printer.
31 * add_class() - Add a class to the system.
32 * add_file() - Add a file to a job.
33 * add_job() - Add a job to a print queue.
34 * add_job_state_reasons() - Add the "job-state-reasons" attribute based
35 * upon the job and printer state...
36 * add_job_subscriptions() - Add any subcriptions for a job.
37 * add_job_uuid() - Add job-uuid attribute to a job.
38 * add_printer() - Add a printer to the system.
39 * add_printer_state_reasons() - Add the "printer-state-reasons" attribute
40 * based upon the printer state...
41 * add_queued_job_count() - Add the "queued-job-count" attribute for
42 * apply_printer_defaults() - Apply printer default options to a job.
43 * authenticate_job() - Set job authentication info.
44 * cancel_all_jobs() - Cancel all print jobs.
45 * cancel_job() - Cancel a print job.
46 * cancel_subscription() - Cancel a subscription.
47 * check_quotas() - Check quotas for a printer and user.
48 * copy_attribute() - Copy a single attribute.
49 * copy_attrs() - Copy attributes from one request to another.
50 * copy_banner() - Copy a banner file to the requests directory
51 * for the specified job.
52 * copy_file() - Copy a PPD file or interface script...
53 * copy_model() - Copy a PPD model file, substituting default
55 * copy_job_attrs() - Copy job attributes.
56 * copy_printer_attrs() - Copy printer attributes.
57 * copy_subscription_attrs() - Copy subscription attributes.
58 * create_job() - Print a file to a printer or class.
59 * create_requested_array() - Create an array for the requested-attributes.
60 * create_subscription() - Create a notification subscription.
61 * delete_printer() - Remove a printer or class from the system.
62 * get_default() - Get the default destination.
63 * get_devices() - Get the list of available devices on the
65 * get_job_attrs() - Get job attributes.
66 * get_jobs() - Get a list of jobs for the specified printer.
67 * get_notifications() - Get events for a subscription.
68 * get_ppds() - Get the list of PPD files on the local
70 * get_printer_attrs() - Get printer attributes.
71 * get_printers() - Get a list of printers.
72 * get_subscription_attrs() - Get subscription attributes.
73 * get_subscriptions() - Get subscriptions.
74 * get_username() - Get the username associated with a request.
75 * hold_job() - Hold a print job.
76 * move_job() - Move a job to a new destination.
77 * ppd_parse_line() - Parse a PPD default line.
78 * print_job() - Print a file to a printer or class.
79 * read_ps_line() - Read a line from a PS file...
80 * read_ps_job_ticket() - Reads a job ticket embedded in a PS file.
81 * reject_jobs() - Reject print jobs to a printer.
82 * release_job() - Release a held print job.
83 * restart_job() - Restart an old print job.
84 * save_auth_info() - Save authentication information for a job.
85 * save_krb5_creds() - Save Kerberos credentials for a job.
86 * send_document() - Send a file to a printer or class.
87 * send_http_error() - Send a HTTP error back to the IPP client.
88 * send_ipp_status() - Send a status back to the IPP client.
89 * set_default() - Set the default destination...
90 * set_job_attrs() - Set job attributes.
91 * set_printer_defaults() - Set printer default options from a request.
92 * start_printer() - Start a printer.
93 * stop_printer() - Stop a printer.
94 * url_encode_attr() - URL-encode a string attribute.
95 * user_allowed() - See if a user is allowed to print to a queue.
96 * validate_job() - Validate printer options and destination.
97 * validate_name() - Make sure the printer name only contains
99 * validate_user() - Validate the user for the request.
103 * Include necessary headers...
110 #endif /* HAVE_KRB5_H */
114 #endif /* HAVE_LIBPAPER */
121 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
122 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
123 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
124 mime_type_t
*filetype
, int compression
);
125 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
126 mime_type_t
*filetype
);
127 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
128 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
129 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
130 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
131 static void add_printer_state_reasons(cupsd_client_t
*con
,
133 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
134 static void apply_printer_defaults(cupsd_printer_t
*printer
,
136 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
137 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
138 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
139 static void cancel_subscription(cupsd_client_t
*con
, int id
);
140 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
141 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
143 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
144 ipp_tag_t group
, int quickcopy
);
145 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
147 static int copy_file(const char *from
, const char *to
);
148 static int copy_model(cupsd_client_t
*con
, const char *from
,
150 static void copy_job_attrs(cupsd_client_t
*con
,
153 static void copy_printer_attrs(cupsd_client_t
*con
,
154 cupsd_printer_t
*printer
,
156 static void copy_subscription_attrs(cupsd_client_t
*con
,
157 cupsd_subscription_t
*sub
,
159 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
160 static cups_array_t
*create_requested_array(ipp_t
*request
);
161 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
162 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
163 static void get_default(cupsd_client_t
*con
);
164 static void get_devices(cupsd_client_t
*con
);
165 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
166 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
167 static void get_notifications(cupsd_client_t
*con
);
168 static void get_ppds(cupsd_client_t
*con
);
169 static void get_printers(cupsd_client_t
*con
, int type
);
170 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
171 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
172 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
173 static const char *get_username(cupsd_client_t
*con
);
174 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
175 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
176 static int ppd_parse_line(const char *line
, char *option
, int olen
,
177 char *choice
, int clen
);
178 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
179 static void read_ps_job_ticket(cupsd_client_t
*con
);
180 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
181 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
182 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
183 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
184 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
);
185 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
186 static void save_krb5_creds(cupsd_client_t
*con
, cupsd_job_t
*job
);
187 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
188 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
189 static void send_http_error(cupsd_client_t
*con
, http_status_t status
);
190 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
191 const char *message
, ...)
193 __attribute__ ((__format__ (__printf__
, 3, 4)))
194 # endif /* __GNUC__ */
196 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
197 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
198 static void set_printer_defaults(cupsd_client_t
*con
,
199 cupsd_printer_t
*printer
);
200 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
201 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
202 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
,
204 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
205 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
206 static int validate_name(const char *name
);
207 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
,
208 const char *owner
, char *username
,
213 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request...
216 int /* O - 1 on success, 0 on failure */
217 cupsdProcessIPPRequest(
218 cupsd_client_t
*con
) /* I - Client connection */
220 ipp_tag_t group
; /* Current group tag */
221 ipp_attribute_t
*attr
; /* Current attribute */
222 ipp_attribute_t
*charset
; /* Character set attribute */
223 ipp_attribute_t
*language
; /* Language attribute */
224 ipp_attribute_t
*uri
; /* Printer URI attribute */
225 ipp_attribute_t
*username
; /* requesting-user-name attr */
226 int sub_id
; /* Subscription ID */
229 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
230 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
231 con
, con
->http
.fd
, con
->request
->request
.op
.operation_id
);
234 * First build an empty response message for this request...
237 con
->response
= ippNew();
239 con
->response
->request
.status
.version
[0] = con
->request
->request
.op
.version
[0];
240 con
->response
->request
.status
.version
[1] = con
->request
->request
.op
.version
[1];
241 con
->response
->request
.status
.request_id
= con
->request
->request
.op
.request_id
;
244 * Then validate the request header and required attributes...
247 if (con
->request
->request
.any
.version
[0] != 1)
250 * Return an error, since we only support IPP 1.x.
253 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
254 "%04X %s Bad request version number %d.%d",
255 IPP_VERSION_NOT_SUPPORTED
, con
->http
.hostname
,
256 con
->request
->request
.any
.version
[0],
257 con
->request
->request
.any
.version
[1]);
259 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
260 _("Bad request version number %d.%d!"),
261 con
->request
->request
.any
.version
[0],
262 con
->request
->request
.any
.version
[1]);
264 else if (!con
->request
->attrs
)
266 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
267 "%04X %s No attributes in request",
268 IPP_BAD_REQUEST
, con
->http
.hostname
);
270 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request!"));
275 * Make sure that the attributes are provided in the correct order and
276 * don't repeat groups...
279 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
282 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
285 * Out of order; return an error...
288 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
289 "%04X %s Attribute groups are out of order",
290 IPP_BAD_REQUEST
, con
->http
.hostname
);
292 send_ipp_status(con
, IPP_BAD_REQUEST
,
293 _("Attribute groups are out of order (%x < %x)!"),
294 attr
->group_tag
, group
);
298 group
= attr
->group_tag
;
303 * Then make sure that the first three attributes are:
306 * attributes-natural-language
307 * printer-uri/job-uri
310 attr
= con
->request
->attrs
;
311 if (attr
&& !strcmp(attr
->name
, "attributes-charset") &&
312 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
320 if (attr
&& !strcmp(attr
->name
, "attributes-natural-language") &&
321 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
326 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
327 IPP_TAG_URI
)) != NULL
)
329 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
330 IPP_TAG_URI
)) != NULL
)
336 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
337 "attributes-charset", NULL
, charset
->values
[0].string
.text
);
339 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
340 "attributes-charset", NULL
, DefaultCharset
);
343 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
344 "attributes-natural-language", NULL
,
345 language
->values
[0].string
.text
);
347 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
348 "attributes-natural-language", NULL
, DefaultLanguage
);
350 if (!charset
|| !language
||
352 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
353 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
354 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
355 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
356 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
359 * Return an error, since attributes-charset,
360 * attributes-natural-language, and printer-uri/job-uri are required
361 * for all operations.
366 cupsdLogMessage(CUPSD_LOG_ERROR
,
367 "Missing attributes-charset attribute!");
369 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
370 "%04X %s Missing attributes-charset attribute",
371 IPP_BAD_REQUEST
, con
->http
.hostname
);
376 cupsdLogMessage(CUPSD_LOG_ERROR
,
377 "Missing attributes-natural-language attribute!");
379 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
380 "%04X %s Missing attributes-natural-language attribute",
381 IPP_BAD_REQUEST
, con
->http
.hostname
);
386 cupsdLogMessage(CUPSD_LOG_ERROR
,
387 "Missing printer-uri or job-uri attribute!");
389 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
390 "%04X %s Missing printer-uri or job-uri attribute",
391 IPP_BAD_REQUEST
, con
->http
.hostname
);
394 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
396 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
397 cupsdLogMessage(CUPSD_LOG_DEBUG
,
398 "attr \"%s\": group_tag = %x, value_tag = %x",
399 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
402 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
404 send_ipp_status(con
, IPP_BAD_REQUEST
,
405 _("Missing required attributes!"));
410 * OK, all the checks pass so far; make sure requesting-user-name is
411 * not "root" from a remote host...
414 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
415 IPP_TAG_NAME
)) != NULL
)
418 * Check for root user...
421 if (!strcmp(username
->values
[0].string
.text
, "root") &&
422 strcasecmp(con
->http
.hostname
, "localhost") &&
423 strcmp(con
->username
, "root"))
426 * Remote unauthenticated user masquerading as local root...
429 _cupsStrFree(username
->values
[0].string
.text
);
430 username
->values
[0].string
.text
= _cupsStrAlloc(RemoteRoot
);
434 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
435 IPP_TAG_INTEGER
)) != NULL
)
436 sub_id
= attr
->values
[0].integer
;
441 * Then try processing the operation...
445 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
446 ippOpString(con
->request
->request
.op
.operation_id
),
447 uri
->values
[0].string
.text
);
449 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
450 ippOpString(con
->request
->request
.op
.operation_id
));
452 switch (con
->request
->request
.op
.operation_id
)
458 case IPP_VALIDATE_JOB
:
459 validate_job(con
, uri
);
462 case IPP_CREATE_JOB
:
463 create_job(con
, uri
);
466 case IPP_SEND_DOCUMENT
:
467 send_document(con
, uri
);
470 case IPP_CANCEL_JOB
:
471 cancel_job(con
, uri
);
474 case IPP_GET_JOB_ATTRIBUTES
:
475 get_job_attrs(con
, uri
);
482 case IPP_GET_PRINTER_ATTRIBUTES
:
483 get_printer_attrs(con
, uri
);
490 case IPP_RELEASE_JOB
:
491 release_job(con
, uri
);
494 case IPP_RESTART_JOB
:
495 restart_job(con
, uri
);
498 case IPP_PAUSE_PRINTER
:
499 stop_printer(con
, uri
);
502 case IPP_RESUME_PRINTER
:
503 start_printer(con
, uri
);
506 case IPP_PURGE_JOBS
:
507 cancel_all_jobs(con
, uri
);
510 case IPP_SET_JOB_ATTRIBUTES
:
511 set_job_attrs(con
, uri
);
514 case CUPS_GET_DEFAULT
:
518 case CUPS_GET_PRINTERS
:
519 get_printers(con
, 0);
522 case CUPS_GET_CLASSES
:
523 get_printers(con
, CUPS_PRINTER_CLASS
);
526 case CUPS_ADD_PRINTER
:
527 add_printer(con
, uri
);
530 case CUPS_DELETE_PRINTER
:
531 delete_printer(con
, uri
);
534 case CUPS_ADD_CLASS
:
538 case CUPS_DELETE_CLASS
:
539 delete_printer(con
, uri
);
542 case CUPS_ACCEPT_JOBS
:
543 case IPP_ENABLE_PRINTER
:
544 accept_jobs(con
, uri
);
547 case CUPS_REJECT_JOBS
:
548 case IPP_DISABLE_PRINTER
:
549 reject_jobs(con
, uri
);
552 case CUPS_SET_DEFAULT
:
553 set_default(con
, uri
);
556 case CUPS_GET_DEVICES
:
568 case CUPS_AUTHENTICATE_JOB
:
569 authenticate_job(con
, uri
);
572 case IPP_CREATE_PRINTER_SUBSCRIPTION
:
573 case IPP_CREATE_JOB_SUBSCRIPTION
:
574 create_subscription(con
, uri
);
577 case IPP_GET_SUBSCRIPTION_ATTRIBUTES
:
578 get_subscription_attrs(con
, sub_id
);
581 case IPP_GET_SUBSCRIPTIONS
:
582 get_subscriptions(con
, uri
);
585 case IPP_RENEW_SUBSCRIPTION
:
586 renew_subscription(con
, sub_id
);
589 case IPP_CANCEL_SUBSCRIPTION
:
590 cancel_subscription(con
, sub_id
);
593 case IPP_GET_NOTIFICATIONS
:
594 get_notifications(con
);
598 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
599 "%04X %s Operation %04X (%s) not supported",
600 IPP_OPERATION_NOT_SUPPORTED
, con
->http
.hostname
,
601 con
->request
->request
.op
.operation_id
,
602 ippOpString(con
->request
->request
.op
.operation_id
));
604 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
605 _("%s not supported!"),
606 ippOpString(con
->request
->request
.op
.operation_id
));
616 * Sending data from the scheduler...
619 cupsdLogMessage(CUPSD_LOG_DEBUG
,
620 "cupsdProcessIPPRequest: %d status_code=%x (%s)",
621 con
->http
.fd
, con
->response
->request
.status
.status_code
,
622 ippErrorString(con
->response
->request
.status
.status_code
));
624 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp"))
626 #ifdef CUPSD_USE_CHUNKING
628 * Because older versions of CUPS (1.1.17 and older) and some IPP
629 * clients do not implement chunking properly, we cannot use
630 * chunking by default. This may become the default in future
631 * CUPS releases, or we might add a configuration directive for
635 if (con
->http
.version
== HTTP_1_1
)
637 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n") < 0)
640 if (cupsdFlushHeader(con
) < 0)
643 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
646 #endif /* CUPSD_USE_CHUNKING */
648 size_t length
; /* Length of response */
651 length
= ippLength(con
->response
);
653 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
654 CUPS_LLCAST length
) < 0)
657 if (cupsdFlushHeader(con
) < 0)
660 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
661 con
->http
.data_remaining
= length
;
664 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
665 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
668 * Tell the caller the response header was sent successfully...
676 * Tell the caller the response header could not be sent...
685 * Sending data from a subprocess like cups-deviced; tell the caller
686 * everything is A-OK so far...
695 * 'accept_jobs()' - Accept print jobs to a printer.
699 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
700 ipp_attribute_t
*uri
) /* I - Printer or class URI */
702 http_status_t status
; /* Policy status */
703 cups_ptype_t dtype
; /* Destination type (printer or class) */
704 cupsd_printer_t
*printer
; /* Printer data */
707 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
708 con
->http
.fd
, uri
->values
[0].string
.text
);
711 * Is the destination valid?
714 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
720 send_ipp_status(con
, IPP_NOT_FOUND
,
721 _("The printer or class was not found."));
729 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
731 send_http_error(con
, status
);
736 * Accept jobs sent to the printer...
739 printer
->accepting
= 1;
740 printer
->state_message
[0] = '\0';
742 cupsdAddPrinterHistory(printer
);
744 if (dtype
& CUPS_PRINTER_CLASS
)
746 cupsdSaveAllClasses();
748 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
749 printer
->name
, get_username(con
));
753 cupsdSaveAllPrinters();
755 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" now accepting jobs (\"%s\").",
756 printer
->name
, get_username(con
));
760 * Everything was ok, so return OK status...
763 con
->response
->request
.status
.status_code
= IPP_OK
;
768 * 'add_class()' - Add a class to the system.
772 add_class(cupsd_client_t
*con
, /* I - Client connection */
773 ipp_attribute_t
*uri
) /* I - URI of class */
775 http_status_t status
; /* Policy status */
776 int i
; /* Looping var */
777 char method
[HTTP_MAX_URI
], /* Method portion of URI */
778 username
[HTTP_MAX_URI
], /* Username portion of URI */
779 host
[HTTP_MAX_URI
], /* Host portion of URI */
780 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
781 int port
; /* Port portion of URI */
782 cupsd_printer_t
*pclass
, /* Class */
783 *member
; /* Member printer/class */
784 cups_ptype_t dtype
; /* Destination type */
785 ipp_attribute_t
*attr
; /* Printer attribute */
786 int modify
; /* Non-zero if we just modified */
787 char newname
[IPP_MAX_NAME
]; /* New class name */
788 int need_restart_job
; /* Need to restart job? */
791 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
792 con
->http
.fd
, uri
->values
[0].string
.text
);
795 * Do we have a valid URI?
798 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
799 sizeof(method
), username
, sizeof(username
), host
,
800 sizeof(host
), &port
, resource
, sizeof(resource
));
803 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
806 * No, return an error...
809 send_ipp_status(con
, IPP_BAD_REQUEST
,
810 _("The printer-uri must be of the form "
811 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
816 * Do we have a valid printer name?
819 if (!validate_name(resource
+ 9))
822 * No, return an error...
825 send_ipp_status(con
, IPP_BAD_REQUEST
,
826 _("The printer-uri \"%s\" contains invalid characters."),
827 uri
->values
[0].string
.text
);
835 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
837 send_http_error(con
, status
);
842 * See if the class already exists; if not, create a new class...
845 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
848 * Class doesn't exist; see if we have a printer of the same name...
851 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
852 !(pclass
->type
& CUPS_PRINTER_REMOTE
))
855 * Yes, return an error...
858 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
859 _("A printer named \"%s\" already exists!"),
865 * No, add the pclass...
868 pclass
= cupsdAddClass(resource
+ 9);
871 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
874 * Rename the implicit class to "AnyClass" or remove it...
877 if (ImplicitAnyClasses
)
879 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
880 cupsdRenamePrinter(pclass
, newname
);
883 cupsdDeletePrinter(pclass
, 1);
886 * Add the class as a new local class...
889 pclass
= cupsdAddClass(resource
+ 9);
892 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
895 * Rename the remote class to "Class"...
898 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
899 cupsdRenamePrinter(pclass
, newname
);
902 * Add the class as a new local class...
905 pclass
= cupsdAddClass(resource
+ 9);
912 * Look for attributes and copy them over as needed...
915 need_restart_job
= 0;
917 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
918 IPP_TAG_TEXT
)) != NULL
)
919 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
921 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
922 IPP_TAG_TEXT
)) != NULL
)
923 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
925 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
926 IPP_TAG_BOOLEAN
)) != NULL
)
928 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-is-accepting-jobs to %d (was %d.)",
929 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
931 pclass
->accepting
= attr
->values
[0].boolean
;
932 cupsdAddPrinterHistory(pclass
);
935 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
936 IPP_TAG_BOOLEAN
)) != NULL
)
938 if (pclass
->shared
&& !attr
->values
[0].boolean
)
939 cupsdDeregisterPrinter(pclass
, 1);
941 cupsdLogMessage(CUPSD_LOG_INFO
,
942 "Setting %s printer-is-shared to %d (was %d.)",
943 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
945 pclass
->shared
= attr
->values
[0].boolean
;
948 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
949 IPP_TAG_ENUM
)) != NULL
)
951 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
952 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
954 send_ipp_status(con
, IPP_BAD_REQUEST
,
955 _("Attempt to set %s printer-state to bad value %d!"),
956 pclass
->name
, attr
->values
[0].integer
);
960 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)", pclass
->name
,
961 attr
->values
[0].integer
, pclass
->state
);
963 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
964 cupsdStopPrinter(pclass
, 0);
967 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
968 need_restart_job
= 1;
971 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
972 IPP_TAG_TEXT
)) != NULL
)
974 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
975 sizeof(pclass
->state_message
));
976 cupsdAddPrinterHistory(pclass
);
978 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
979 IPP_TAG_URI
)) != NULL
)
982 * Clear the printer array as needed...
985 need_restart_job
= 1;
987 if (pclass
->num_printers
> 0)
989 free(pclass
->printers
);
990 pclass
->num_printers
= 0;
994 * Add each printer or class that is listed...
997 for (i
= 0; i
< attr
->num_values
; i
++)
1000 * Search for the printer or class URI...
1003 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1009 send_ipp_status(con
, IPP_NOT_FOUND
,
1010 _("The printer or class was not found."));
1015 * Add it to the class...
1018 cupsdAddPrinterToClass(pclass
, member
);
1022 set_printer_defaults(con
, pclass
);
1025 * Update the printer class attributes and return...
1028 cupsdSetPrinterAttrs(pclass
);
1029 cupsdSaveAllClasses();
1031 if (need_restart_job
&& pclass
->job
)
1036 * Stop the current job and then restart it below...
1039 job
= (cupsd_job_t
*)pclass
->job
;
1041 cupsdStopJob(job
, 1);
1043 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1044 job
->state_value
= IPP_JOB_PENDING
;
1047 if (need_restart_job
)
1050 cupsdWritePrintcap();
1054 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1055 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1058 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1059 pclass
->name
, get_username(con
));
1063 cupsdAddPrinterHistory(pclass
);
1065 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1066 "New class \"%s\" added by \"%s\".", pclass
->name
,
1069 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1070 pclass
->name
, get_username(con
));
1073 con
->response
->request
.status
.status_code
= IPP_OK
;
1078 * 'add_file()' - Add a file to a job.
1081 static int /* O - 0 on success, -1 on error */
1082 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1083 cupsd_job_t
*job
, /* I - Job to add to */
1084 mime_type_t
*filetype
, /* I - Type of file */
1085 int compression
) /* I - Compression */
1087 mime_type_t
**filetypes
; /* New filetypes array... */
1088 int *compressions
; /* New compressions array... */
1091 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1092 "add_file(con=%p[%d], job=%d, filetype=%s/%s, compression=%d)",
1093 con
, con
->http
.fd
, job
->id
, filetype
->super
, filetype
->type
,
1097 * Add the file to the job...
1100 if (job
->num_files
== 0)
1102 compressions
= (int *)malloc(sizeof(int));
1103 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1107 compressions
= (int *)realloc(job
->compressions
,
1108 (job
->num_files
+ 1) * sizeof(int));
1109 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1110 (job
->num_files
+ 1) *
1111 sizeof(mime_type_t
*));
1114 if (!compressions
|| !filetypes
)
1116 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
1118 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1119 _("Unable to allocate memory for file types!"));
1123 job
->compressions
= compressions
;
1124 job
->compressions
[job
->num_files
] = compression
;
1125 job
->filetypes
= filetypes
;
1126 job
->filetypes
[job
->num_files
] = filetype
;
1135 * 'add_job()' - Add a job to a print queue.
1138 static cupsd_job_t
* /* O - Job object */
1139 add_job(cupsd_client_t
*con
, /* I - Client connection */
1140 cupsd_printer_t
*printer
, /* I - Destination printer */
1141 mime_type_t
*filetype
) /* I - First print file type, if any */
1143 http_status_t status
; /* Policy status */
1144 ipp_attribute_t
*attr
; /* Current attribute */
1145 const char *val
; /* Default option value */
1146 int priority
; /* Job priority */
1147 char *title
; /* Job name/title */
1148 cupsd_job_t
*job
; /* Current job */
1149 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1150 int kbytes
; /* Size of print file */
1151 int i
; /* Looping var */
1152 int lowerpagerange
; /* Page range bound */
1155 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1156 con
, con
->http
.fd
, printer
, printer
->name
,
1157 filetype
, filetype
->super
, filetype
->type
);
1160 * Check remote printing to non-shared printer...
1163 if (!printer
->shared
&&
1164 strcasecmp(con
->http
.hostname
, "localhost") &&
1165 strcasecmp(con
->http
.hostname
, ServerName
))
1167 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1168 _("The printer or class is not shared!"));
1176 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1178 send_http_error(con
, status
);
1181 else if ((printer
->type
& CUPS_PRINTER_AUTHENTICATED
) && !con
->username
[0])
1183 send_http_error(con
, HTTP_UNAUTHORIZED
);
1188 * See if the printer is accepting jobs...
1191 if (!printer
->accepting
)
1193 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1194 _("Destination \"%s\" is not accepting jobs."),
1200 * Validate job template attributes; for now just document-format,
1201 * copies, number-up, and page-ranges...
1204 if (filetype
&& printer
->filetypes
&&
1205 !cupsArrayFind(printer
->filetypes
, filetype
))
1207 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1208 /* MIME media type string */
1211 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1214 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1215 _("Unsupported format \'%s\'!"), mimetype
);
1217 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1218 "document-format", NULL
, mimetype
);
1223 if ((attr
= ippFindAttribute(con
->request
, "copies",
1224 IPP_TAG_INTEGER
)) != NULL
)
1226 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1228 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1229 attr
->values
[0].integer
);
1230 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1231 "copies", attr
->values
[0].integer
);
1236 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1237 IPP_TAG_INTEGER
)) != NULL
)
1239 if (attr
->values
[0].integer
!= 1 &&
1240 attr
->values
[0].integer
!= 2 &&
1241 attr
->values
[0].integer
!= 4 &&
1242 attr
->values
[0].integer
!= 6 &&
1243 attr
->values
[0].integer
!= 9 &&
1244 attr
->values
[0].integer
!= 16)
1246 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1247 attr
->values
[0].integer
);
1248 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1249 "number-up", attr
->values
[0].integer
);
1254 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1255 IPP_TAG_RANGE
)) != NULL
)
1257 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1259 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1260 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1262 send_ipp_status(con
, IPP_BAD_REQUEST
,
1263 _("Bad page-ranges values %d-%d."),
1264 attr
->values
[i
].range
.lower
,
1265 attr
->values
[i
].range
.upper
);
1269 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1274 * Make sure we aren't over our limit...
1277 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1280 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1282 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1283 _("Too many active jobs."));
1287 if (!check_quotas(con
, printer
))
1289 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1294 * Create the job and set things up...
1297 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1298 IPP_TAG_INTEGER
)) != NULL
)
1299 priority
= attr
->values
[0].integer
;
1302 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1303 printer
->options
)) != NULL
)
1304 priority
= atoi(val
);
1308 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1312 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1313 IPP_TAG_NAME
)) != NULL
)
1314 title
= attr
->values
[0].string
.text
;
1316 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1317 title
= "Untitled");
1319 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1321 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1322 _("Unable to add job for destination \"%s\"!"),
1327 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
1328 CUPS_PRINTER_REMOTE
);
1329 job
->attrs
= con
->request
;
1330 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1332 add_job_uuid(con
, job
);
1333 apply_printer_defaults(printer
, job
);
1335 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1337 if (con
->username
[0])
1339 cupsdSetString(&job
->username
, con
->username
);
1342 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1344 save_auth_info(con
, job
);
1348 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1349 "add_job: requesting-user-name=\"%s\"",
1350 attr
->values
[0].string
.text
);
1352 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1355 cupsdSetString(&job
->username
, "anonymous");
1358 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1359 "job-originating-user-name", NULL
, job
->username
);
1362 attr
->group_tag
= IPP_TAG_JOB
;
1363 _cupsStrFree(attr
->name
);
1364 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1367 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1368 IPP_TAG_ZERO
)) != NULL
)
1371 * Request contains a job-originating-host-name attribute; validate it...
1374 if (attr
->value_tag
!= IPP_TAG_NAME
||
1375 attr
->num_values
!= 1 ||
1376 strcmp(con
->http
.hostname
, "localhost"))
1379 * Can't override the value if we aren't connected via localhost.
1380 * Also, we can only have 1 value and it must be a name value.
1383 switch (attr
->value_tag
)
1385 case IPP_TAG_STRING
:
1386 case IPP_TAG_TEXTLANG
:
1387 case IPP_TAG_NAMELANG
:
1390 case IPP_TAG_KEYWORD
:
1392 case IPP_TAG_URISCHEME
:
1393 case IPP_TAG_CHARSET
:
1394 case IPP_TAG_LANGUAGE
:
1395 case IPP_TAG_MIMETYPE
:
1397 * Free old strings...
1400 for (i
= 0; i
< attr
->num_values
; i
++)
1402 _cupsStrFree(attr
->values
[i
].string
.text
);
1403 attr
->values
[i
].string
.text
= NULL
;
1404 if (attr
->values
[i
].string
.charset
)
1406 _cupsStrFree(attr
->values
[i
].string
.charset
);
1407 attr
->values
[i
].string
.charset
= NULL
;
1416 * Use the default connection hostname instead...
1419 attr
->value_tag
= IPP_TAG_NAME
;
1420 attr
->num_values
= 1;
1421 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1424 attr
->group_tag
= IPP_TAG_JOB
;
1429 * No job-originating-host-name attribute, so use the hostname from
1433 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1434 "job-originating-host-name", NULL
, con
->http
.hostname
);
1437 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1439 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1440 "time-at-processing", 0);
1441 attr
->value_tag
= IPP_TAG_NOVALUE
;
1442 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1443 "time-at-completed", 0);
1444 attr
->value_tag
= IPP_TAG_NOVALUE
;
1447 * Add remaining job attributes...
1450 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1451 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1452 "job-state", IPP_JOB_STOPPED
);
1453 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1454 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1455 "job-media-sheets-completed", 0);
1456 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1458 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1461 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1462 IPP_TAG_INTEGER
)) != NULL
)
1463 attr
->values
[0].integer
= 0;
1465 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1468 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1469 IPP_TAG_KEYWORD
)) == NULL
)
1470 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1473 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1474 printer
->options
)) == NULL
)
1477 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1478 "job-hold-until", NULL
, val
);
1480 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1481 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1484 * Hold job until specified time...
1487 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1489 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1490 job
->state_value
= IPP_JOB_HELD
;
1492 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1494 job
->hold_until
= time(NULL
) + 60;
1495 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1496 job
->state_value
= IPP_JOB_HELD
;
1500 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1501 job
->state_value
= IPP_JOB_PENDING
;
1504 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1508 * Add job sheets options...
1511 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1512 IPP_TAG_ZERO
)) == NULL
)
1514 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1515 "Adding default job-sheets values \"%s,%s\"...",
1516 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1518 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1520 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1521 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1524 job
->job_sheets
= attr
;
1527 * Enforce classification level if set...
1532 cupsdLogMessage(CUPSD_LOG_INFO
,
1533 "Classification=\"%s\", ClassifyOverride=%d",
1534 Classification
? Classification
: "(null)",
1537 if (ClassifyOverride
)
1539 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1540 (attr
->num_values
== 1 ||
1541 !strcmp(attr
->values
[1].string
.text
, "none")))
1544 * Force the leading banner to have the classification on it...
1547 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1549 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1550 "job-sheets=\"%s,none\", "
1551 "job-originating-user-name=\"%s\"",
1552 job
->id
, Classification
, job
->username
);
1554 else if (attr
->num_values
== 2 &&
1555 strcmp(attr
->values
[0].string
.text
,
1556 attr
->values
[1].string
.text
) &&
1557 strcmp(attr
->values
[0].string
.text
, "none") &&
1558 strcmp(attr
->values
[1].string
.text
, "none"))
1561 * Can't put two different security markings on the same document!
1564 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1566 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1567 "job-sheets=\"%s,%s\", "
1568 "job-originating-user-name=\"%s\"",
1569 job
->id
, attr
->values
[0].string
.text
,
1570 attr
->values
[1].string
.text
, job
->username
);
1572 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1573 strcmp(attr
->values
[0].string
.text
, "none") &&
1574 (attr
->num_values
== 1 ||
1575 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1576 strcmp(attr
->values
[1].string
.text
, "none"))))
1578 if (attr
->num_values
== 1)
1579 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1580 "[Job %d] CLASSIFICATION OVERRIDDEN "
1581 "job-sheets=\"%s\", "
1582 "job-originating-user-name=\"%s\"",
1583 job
->id
, attr
->values
[0].string
.text
, job
->username
);
1585 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1586 "[Job %d] CLASSIFICATION OVERRIDDEN "
1587 "job-sheets=\"%s,%s\",fffff "
1588 "job-originating-user-name=\"%s\"",
1589 job
->id
, attr
->values
[0].string
.text
,
1590 attr
->values
[1].string
.text
, job
->username
);
1593 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1594 (attr
->num_values
== 1 ||
1595 strcmp(attr
->values
[1].string
.text
, Classification
)))
1598 * Force the banner to have the classification on it...
1601 if (attr
->num_values
> 1 &&
1602 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1604 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1605 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1609 if (attr
->num_values
== 1 ||
1610 strcmp(attr
->values
[0].string
.text
, "none"))
1611 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1613 if (attr
->num_values
> 1 &&
1614 strcmp(attr
->values
[1].string
.text
, "none"))
1615 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1618 if (attr
->num_values
> 1)
1619 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1620 "[Job %d] CLASSIFICATION FORCED "
1621 "job-sheets=\"%s,%s\", "
1622 "job-originating-user-name=\"%s\"",
1623 job
->id
, attr
->values
[0].string
.text
,
1624 attr
->values
[1].string
.text
, job
->username
);
1626 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1627 "[Job %d] CLASSIFICATION FORCED "
1628 "job-sheets=\"%s\", "
1629 "job-originating-user-name=\"%s\"",
1630 job
->id
, Classification
, job
->username
);
1635 * See if we need to add the starting sheet...
1638 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1640 cupsdLogMessage(CUPSD_LOG_INFO
,
1641 "Adding start banner page \"%s\" to job %d.",
1642 attr
->values
[0].string
.text
, job
->id
);
1644 kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
);
1646 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1649 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1650 IPP_TAG_ZERO
)) != NULL
)
1654 * Fill in the response info...
1657 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1658 LocalPort
, job
->id
);
1660 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1663 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1665 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1667 add_job_state_reasons(con
, job
);
1669 con
->response
->request
.status
.status_code
= IPP_OK
;
1672 * Add any job subscriptions...
1675 add_job_subscriptions(con
, job
);
1678 * Set all but the first two attributes to the job attributes group...
1681 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1682 attr
->group_tag
= IPP_TAG_JOB
;
1685 * Fire the "job created" event...
1688 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1691 * Return the new job...
1699 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1700 * upon the job and printer state...
1704 add_job_state_reasons(
1705 cupsd_client_t
*con
, /* I - Client connection */
1706 cupsd_job_t
*job
) /* I - Job info */
1708 cupsd_printer_t
*dest
; /* Destination printer */
1711 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1712 con
, con
->http
.fd
, job
? job
->id
: 0);
1714 switch (job
? job
->state_value
: IPP_JOB_CANCELED
)
1716 case IPP_JOB_PENDING
:
1717 dest
= cupsdFindDest(job
->dest
);
1719 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1720 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1721 "job-state-reasons", NULL
, "printer-stopped");
1723 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1724 "job-state-reasons", NULL
, "none");
1728 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1729 IPP_TAG_KEYWORD
) != NULL
||
1730 ippFindAttribute(job
->attrs
, "job-hold-until",
1731 IPP_TAG_NAME
) != NULL
)
1732 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1733 "job-state-reasons", NULL
, "job-hold-until-specified");
1735 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1736 "job-state-reasons", NULL
, "job-incoming");
1739 case IPP_JOB_PROCESSING
:
1740 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1741 "job-state-reasons", NULL
, "job-printing");
1744 case IPP_JOB_STOPPED
:
1745 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1746 "job-state-reasons", NULL
, "job-stopped");
1749 case IPP_JOB_CANCELED
:
1750 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1751 "job-state-reasons", NULL
, "job-canceled-by-user");
1754 case IPP_JOB_ABORTED
:
1755 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1756 "job-state-reasons", NULL
, "aborted-by-system");
1759 case IPP_JOB_COMPLETED
:
1760 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1761 "job-state-reasons", NULL
, "job-completed-successfully");
1768 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1772 add_job_subscriptions(
1773 cupsd_client_t
*con
, /* I - Client connection */
1774 cupsd_job_t
*job
) /* I - Newly created job */
1776 int i
; /* Looping var */
1777 ipp_attribute_t
*prev
, /* Previous attribute */
1778 *next
, /* Next attribute */
1779 *attr
; /* Current attribute */
1780 cupsd_subscription_t
*sub
; /* Subscription object */
1781 const char *recipient
, /* notify-recipient-uri */
1782 *pullmethod
; /* notify-pull-method */
1783 ipp_attribute_t
*user_data
; /* notify-user-data */
1784 int interval
; /* notify-time-interval */
1785 unsigned mask
; /* notify-events */
1789 * Find the first subscription group attribute; return if we have
1793 for (attr
= job
->attrs
->attrs
, prev
= NULL
;
1795 prev
= attr
, attr
= attr
->next
)
1796 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1803 * Process the subscription attributes in the request...
1812 mask
= CUPSD_EVENT_NONE
;
1814 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1816 if (!strcmp(attr
->name
, "notify-recipient") &&
1817 attr
->value_tag
== IPP_TAG_URI
)
1818 recipient
= attr
->values
[0].string
.text
;
1819 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1820 attr
->value_tag
== IPP_TAG_KEYWORD
)
1821 pullmethod
= attr
->values
[0].string
.text
;
1822 else if (!strcmp(attr
->name
, "notify-charset") &&
1823 attr
->value_tag
== IPP_TAG_CHARSET
&&
1824 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
1825 strcmp(attr
->values
[0].string
.text
, "utf-8"))
1827 send_ipp_status(con
, IPP_CHARSET
,
1828 _("Character set \"%s\" not supported!"),
1829 attr
->values
[0].string
.text
);
1832 else if (!strcmp(attr
->name
, "notify-natural-language") &&
1833 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
1834 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
1836 send_ipp_status(con
, IPP_CHARSET
,
1837 _("Language \"%s\" not supported!"),
1838 attr
->values
[0].string
.text
);
1841 else if (!strcmp(attr
->name
, "notify-user-data") &&
1842 attr
->value_tag
== IPP_TAG_STRING
)
1844 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
1846 send_ipp_status(con
, IPP_REQUEST_VALUE
,
1847 _("The notify-user-data value is too large "
1848 "(%d > 63 octets)!"),
1849 attr
->values
[0].unknown
.length
);
1855 else if (!strcmp(attr
->name
, "notify-events") &&
1856 attr
->value_tag
== IPP_TAG_KEYWORD
)
1858 for (i
= 0; i
< attr
->num_values
; i
++)
1859 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
1861 else if (!strcmp(attr
->name
, "notify-lease-duration"))
1863 send_ipp_status(con
, IPP_BAD_REQUEST
,
1864 _("The notify-lease-duration attribute cannot be "
1865 "used with job subscriptions."));
1868 else if (!strcmp(attr
->name
, "notify-time-interval") &&
1869 attr
->value_tag
== IPP_TAG_INTEGER
)
1870 interval
= attr
->values
[0].integer
;
1875 if (!recipient
&& !pullmethod
)
1878 if (mask
== CUPSD_EVENT_NONE
)
1879 mask
= CUPSD_EVENT_JOB_COMPLETED
;
1881 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
1884 sub
->interval
= interval
;
1886 cupsdSetString(&sub
->owner
, job
->username
);
1890 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
1891 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
1892 sub
->user_data_len
);
1895 ippAddSeparator(con
->response
);
1896 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
1897 "notify-subscription-id", sub
->id
);
1903 cupsdSaveAllSubscriptions();
1906 * Remove all of the subscription attributes from the job request...
1909 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
1913 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
1914 attr
->group_tag
== IPP_TAG_ZERO
)
1917 * Free and remove this attribute...
1925 job
->attrs
->attrs
= next
;
1931 job
->attrs
->last
= prev
;
1932 job
->attrs
->current
= prev
;
1937 * 'add_job_uuid()' - Add job-uuid attribute to a job.
1939 * See RFC 4122 for the definition of UUIDs and the format.
1943 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
1944 cupsd_job_t
*job
) /* I - Job */
1946 char uuid
[1024]; /* job-uuid string */
1947 _cups_md5_state_t md5state
; /* MD5 state */
1948 unsigned char md5sum
[16]; /* MD5 digest/sum */
1952 * First see if the job already has a job-uuid attribute; if so, return...
1955 if (ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
1959 * No job-uuid attribute, so build a version 3 UUID with the local job
1960 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
1961 * the ServerName, server name and port that the client connected to,
1962 * and local job ID...
1965 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
1966 con
->serverport
, job
->id
);
1968 _cupsMD5Init(&md5state
);
1969 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
1970 _cupsMD5Finish(&md5state
, md5sum
);
1973 * Format the UUID URI using the MD5 sum and job ID.
1976 snprintf(uuid
, sizeof(uuid
),
1977 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
1978 "%02x%02x%02x%02x%02x%02x",
1979 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
1980 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
1981 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
1982 md5sum
[14], md5sum
[15]);
1984 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
1989 * 'add_printer()' - Add a printer to the system.
1993 add_printer(cupsd_client_t
*con
, /* I - Client connection */
1994 ipp_attribute_t
*uri
) /* I - URI of printer */
1996 http_status_t status
; /* Policy status */
1997 int i
; /* Looping var */
1998 char method
[HTTP_MAX_URI
], /* Method portion of URI */
1999 username
[HTTP_MAX_URI
], /* Username portion of URI */
2000 host
[HTTP_MAX_URI
], /* Host portion of URI */
2001 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2002 int port
; /* Port portion of URI */
2003 cupsd_printer_t
*printer
; /* Printer/class */
2004 ipp_attribute_t
*attr
; /* Printer attribute */
2005 cups_file_t
*fp
; /* Script/PPD file */
2006 char line
[1024]; /* Line from file... */
2007 char srcfile
[1024], /* Source Script/PPD file */
2008 dstfile
[1024]; /* Destination Script/PPD file */
2009 int modify
; /* Non-zero if we are modifying */
2010 char newname
[IPP_MAX_NAME
]; /* New printer name */
2011 int need_restart_job
; /* Need to restart job? */
2014 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2015 con
->http
.fd
, uri
->values
[0].string
.text
);
2018 * Do we have a valid URI?
2021 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2022 sizeof(method
), username
, sizeof(username
), host
,
2023 sizeof(host
), &port
, resource
, sizeof(resource
));
2025 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2028 * No, return an error...
2031 send_ipp_status(con
, IPP_BAD_REQUEST
,
2032 _("The printer-uri must be of the form "
2033 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2038 * Do we have a valid printer name?
2041 if (!validate_name(resource
+ 10))
2044 * No, return an error...
2047 send_ipp_status(con
, IPP_BAD_REQUEST
,
2048 _("The printer-uri \"%s\" contains invalid characters."),
2049 uri
->values
[0].string
.text
);
2057 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2059 send_http_error(con
, status
);
2064 * See if the printer already exists; if not, create a new printer...
2067 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2070 * Printer doesn't exist; see if we have a class of the same name...
2073 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2074 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2077 * Yes, return an error...
2080 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2081 _("A class named \"%s\" already exists!"),
2087 * No, add the printer...
2090 printer
= cupsdAddPrinter(resource
+ 10);
2093 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2096 * Rename the implicit printer to "AnyPrinter" or delete it...
2099 if (ImplicitAnyClasses
)
2101 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2102 cupsdRenamePrinter(printer
, newname
);
2105 cupsdDeletePrinter(printer
, 1);
2108 * Add the printer as a new local printer...
2111 printer
= cupsdAddPrinter(resource
+ 10);
2114 else if (printer
->type
& CUPS_PRINTER_REMOTE
)
2117 * Rename the remote printer to "Printer@server"...
2120 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2122 cupsdRenamePrinter(printer
, newname
);
2125 * Add the printer as a new local printer...
2128 printer
= cupsdAddPrinter(resource
+ 10);
2135 * Look for attributes and copy them over as needed...
2138 need_restart_job
= 0;
2140 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2141 IPP_TAG_TEXT
)) != NULL
)
2142 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2144 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2145 IPP_TAG_TEXT
)) != NULL
)
2146 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2148 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2149 IPP_TAG_URI
)) != NULL
)
2152 * Do we have a valid device URI?
2155 need_restart_job
= 1;
2157 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, method
,
2158 sizeof(method
), username
, sizeof(username
), host
,
2159 sizeof(host
), &port
, resource
, sizeof(resource
));
2161 if (!strcmp(method
, "file"))
2164 * See if the administrator has enabled file devices...
2167 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2170 * File devices are disabled and the URL is not file:/dev/null...
2173 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2174 _("File device URIs have been disabled! "
2175 "To enable, see the FileDevice directive in "
2176 "\"%s/cupsd.conf\"."),
2184 * See if the backend exists and is executable...
2187 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, method
);
2188 if (access(srcfile
, X_OK
))
2191 * Could not find device in list!
2194 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2195 attr
->values
[0].string
.text
);
2200 cupsdLogMessage(CUPSD_LOG_INFO
,
2201 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2203 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2205 cupsdSanitizeURI(printer
->device_uri
, resource
,
2208 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2211 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2212 IPP_TAG_KEYWORD
)) != NULL
)
2214 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2217 need_restart_job
= 1;
2219 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2221 for (i
= 0; i
< supported
->num_values
; i
++)
2222 if (!strcmp(supported
->values
[i
].string
.text
,
2223 attr
->values
[0].string
.text
))
2226 if (i
>= supported
->num_values
)
2228 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2229 attr
->values
[0].string
.text
);
2233 cupsdLogMessage(CUPSD_LOG_INFO
,
2234 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2235 printer
->name
, attr
->values
[0].string
.text
,
2236 printer
->port_monitor
);
2238 if (strcmp(attr
->values
[0].string
.text
, "none"))
2239 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2241 cupsdClearString(&printer
->port_monitor
);
2244 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2245 IPP_TAG_BOOLEAN
)) != NULL
)
2247 cupsdLogMessage(CUPSD_LOG_INFO
,
2248 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2249 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2251 printer
->accepting
= attr
->values
[0].boolean
;
2252 cupsdAddPrinterHistory(printer
);
2255 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2256 IPP_TAG_BOOLEAN
)) != NULL
)
2258 if (printer
->shared
&& !attr
->values
[0].boolean
)
2259 cupsdDeregisterPrinter(printer
, 1);
2261 cupsdLogMessage(CUPSD_LOG_INFO
,
2262 "Setting %s printer-is-shared to %d (was %d.)",
2263 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2265 printer
->shared
= attr
->values
[0].boolean
;
2268 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2269 IPP_TAG_ENUM
)) != NULL
)
2271 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2272 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2274 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2275 attr
->values
[0].integer
);
2279 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)", printer
->name
,
2280 attr
->values
[0].integer
, printer
->state
);
2282 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2283 cupsdStopPrinter(printer
, 0);
2286 need_restart_job
= 1;
2287 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2290 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2291 IPP_TAG_TEXT
)) != NULL
)
2293 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2294 sizeof(printer
->state_message
));
2295 cupsdAddPrinterHistory(printer
);
2298 set_printer_defaults(con
, printer
);
2301 * See if we have all required attributes...
2304 if (!printer
->device_uri
)
2305 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2308 * See if we have an interface script or PPD file attached to the request...
2313 need_restart_job
= 1;
2315 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2317 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2320 * Yes; get the first line from it...
2324 cupsFileGets(fp
, line
, sizeof(line
));
2328 * Then see what kind of file it is...
2331 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2334 if (!strncmp(line
, "*PPD-Adobe", 10))
2337 * The new file is a PPD file, so remove any old interface script
2338 * that might be lying around...
2346 * This must be an interface script, so move the file over to the
2347 * interfaces directory and make it executable...
2350 if (copy_file(srcfile
, dstfile
))
2352 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2353 _("Unable to copy interface script - %s!"),
2359 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2360 "Copied interface script successfully!");
2361 chmod(dstfile
, 0755);
2365 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2368 if (!strncmp(line
, "*PPD-Adobe", 10))
2371 * The new file is a PPD file, so move the file over to the
2372 * ppd directory and make it readable by all...
2375 if (copy_file(srcfile
, dstfile
))
2377 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2378 _("Unable to copy PPD file - %s!"),
2384 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2385 "Copied PPD file successfully!");
2386 chmod(dstfile
, 0644);
2392 * This must be an interface script, so remove any old PPD file that
2393 * may be lying around...
2400 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2401 IPP_TAG_NAME
)) != NULL
)
2403 need_restart_job
= 1;
2405 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2408 * Raw driver, remove any existing PPD or interface script files.
2411 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2415 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2425 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2429 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2432 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2434 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2439 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2440 "Copied PPD file successfully!");
2441 chmod(dstfile
, 0644);
2447 * Update the printer attributes and return...
2450 cupsdSetPrinterAttrs(printer
);
2451 cupsdSaveAllPrinters();
2453 if (need_restart_job
&& printer
->job
)
2458 * Stop the current job and then restart it below...
2461 job
= (cupsd_job_t
*)printer
->job
;
2463 cupsdStopJob(job
, 1);
2465 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2466 job
->state_value
= IPP_JOB_PENDING
;
2469 if (need_restart_job
)
2472 cupsdWritePrintcap();
2476 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2477 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2480 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2481 printer
->name
, get_username(con
));
2485 cupsdAddPrinterHistory(printer
);
2487 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2488 "New printer \"%s\" added by \"%s\".", printer
->name
,
2491 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2492 printer
->name
, get_username(con
));
2495 con
->response
->request
.status
.status_code
= IPP_OK
;
2500 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2501 * based upon the printer state...
2505 add_printer_state_reasons(
2506 cupsd_client_t
*con
, /* I - Client connection */
2507 cupsd_printer_t
*p
) /* I - Printer info */
2509 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2510 "add_printer_state_reasons(%p[%d], %p[%s])",
2511 con
, con
->http
.fd
, p
, p
->name
);
2513 if (p
->num_reasons
== 0)
2514 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2515 "printer-state-reasons", NULL
,
2516 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2518 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2519 "printer-state-reasons", p
->num_reasons
, NULL
,
2520 (const char * const *)p
->reasons
);
2525 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2526 * the specified printer or class.
2530 add_queued_job_count(
2531 cupsd_client_t
*con
, /* I - Client connection */
2532 cupsd_printer_t
*p
) /* I - Printer or class */
2534 int count
; /* Number of jobs on destination */
2537 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2538 con
, con
->http
.fd
, p
, p
->name
);
2540 count
= cupsdGetPrinterJobCount(p
->name
);
2542 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2543 "queued-job-count", count
);
2548 * 'apply_printer_defaults()' - Apply printer default options to a job.
2552 apply_printer_defaults(
2553 cupsd_printer_t
*printer
, /* I - Printer */
2554 cupsd_job_t
*job
) /* I - Job */
2556 int i
, /* Looping var */
2557 num_options
; /* Number of default options */
2558 cups_option_t
*options
, /* Default options */
2559 *option
; /* Current option */
2563 * Collect all of the default options and add the missing ones to the
2567 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
2570 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2572 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2577 * Encode these options as attributes in the job object...
2580 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2581 cupsFreeOptions(num_options
, options
);
2586 * 'authenticate_job()' - Set job authentication info.
2590 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2591 ipp_attribute_t
*uri
) /* I - Job URI */
2593 ipp_attribute_t
*attr
; /* Job-id attribute */
2594 int jobid
; /* Job ID */
2595 cupsd_job_t
*job
; /* Current job */
2596 char method
[HTTP_MAX_URI
],
2597 /* Method portion of URI */
2598 username
[HTTP_MAX_URI
],
2599 /* Username portion of URI */
2601 /* Host portion of URI */
2602 resource
[HTTP_MAX_URI
];
2603 /* Resource portion of URI */
2604 int port
; /* Port portion of URI */
2607 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2608 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
2611 * Start with "everything is OK" status...
2614 con
->response
->request
.status
.status_code
= IPP_OK
;
2617 * See if we have a job URI or a printer URI...
2620 if (!strcmp(uri
->name
, "printer-uri"))
2623 * Got a printer URI; see if we also have a job-id attribute...
2626 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2627 IPP_TAG_INTEGER
)) == NULL
)
2629 send_ipp_status(con
, IPP_BAD_REQUEST
,
2630 _("Got a printer-uri attribute but no job-id!"));
2634 jobid
= attr
->values
[0].integer
;
2639 * Got a job URI; parse it to get the job ID...
2642 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2643 sizeof(method
), username
, sizeof(username
), host
,
2644 sizeof(host
), &port
, resource
, sizeof(resource
));
2646 if (strncmp(resource
, "/jobs/", 6))
2652 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
2653 uri
->values
[0].string
.text
);
2657 jobid
= atoi(resource
+ 6);
2661 * See if the job exists...
2664 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2667 * Nope - return a "not found" error...
2670 send_ipp_status(con
, IPP_NOT_FOUND
,
2671 _("Job #%d does not exist!"), jobid
);
2676 * See if the job has been completed...
2679 if (job
->state_value
!= IPP_JOB_HELD
)
2682 * Return a "not-possible" error...
2685 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2686 _("Job #%d is not held for authentication!"),
2692 * See if we have already authenticated...
2695 if (!con
->username
[0])
2697 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
2698 _("No authentication information provided!"));
2703 * See if the job is owned by the requesting user...
2706 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
2708 send_http_error(con
, HTTP_UNAUTHORIZED
);
2713 * Save the authentication information for this job...
2716 save_auth_info(con
, job
);
2719 * Reset the job-hold-until value to "no-hold"...
2722 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2723 IPP_TAG_KEYWORD
)) == NULL
)
2724 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2728 attr
->value_tag
= IPP_TAG_KEYWORD
;
2729 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2733 * Release the job and return...
2736 cupsdReleaseJob(job
);
2738 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was authenticated by \"%s\".", jobid
,
2744 * 'cancel_all_jobs()' - Cancel all print jobs.
2748 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
2749 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2751 http_status_t status
; /* Policy status */
2752 cups_ptype_t dtype
; /* Destination type */
2753 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2754 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2755 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
2756 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2757 int port
; /* Port portion of URI */
2758 ipp_attribute_t
*attr
; /* Attribute in request */
2759 const char *username
; /* Username */
2760 int purge
; /* Purge? */
2761 cupsd_printer_t
*printer
; /* Printer */
2764 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
2765 con
->http
.fd
, uri
->values
[0].string
.text
);
2768 * See if we have a printer URI...
2771 if (strcmp(uri
->name
, "printer-uri"))
2773 send_ipp_status(con
, IPP_BAD_REQUEST
,
2774 _("The printer-uri attribute is required!"));
2779 * Get the username (if any) for the jobs we want to cancel (only if
2780 * "my-jobs" is specified...
2783 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
2784 IPP_TAG_BOOLEAN
)) != NULL
&&
2785 attr
->values
[0].boolean
)
2787 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
2788 IPP_TAG_NAME
)) != NULL
)
2789 username
= attr
->values
[0].string
.text
;
2792 send_ipp_status(con
, IPP_BAD_REQUEST
,
2793 _("Missing requesting-user-name attribute!"));
2801 * Look for the "purge-jobs" attribute...
2804 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
2805 IPP_TAG_BOOLEAN
)) != NULL
)
2806 purge
= attr
->values
[0].boolean
;
2811 * And if the destination is valid...
2814 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
2820 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
2821 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
2822 hostname
, sizeof(hostname
), &port
,
2823 resource
, sizeof(resource
));
2825 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
2826 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
2828 send_ipp_status(con
, IPP_NOT_FOUND
,
2829 _("The printer or class was not found."));
2837 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2839 send_http_error(con
, status
);
2844 * Cancel all jobs on all printers...
2847 cupsdCancelJobs(NULL
, username
, purge
);
2849 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
2850 purge
? "purged" : "canceled", get_username(con
));
2858 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2861 send_http_error(con
, status
);
2866 * Cancel all of the jobs on the named printer...
2869 cupsdCancelJobs(printer
->name
, username
, purge
);
2871 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
2872 printer
->name
, purge
? "purged" : "canceled",
2876 con
->response
->request
.status
.status_code
= IPP_OK
;
2881 * 'cancel_job()' - Cancel a print job.
2885 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
2886 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2888 ipp_attribute_t
*attr
; /* Current attribute */
2889 int jobid
; /* Job ID */
2890 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2891 username
[HTTP_MAX_URI
], /* Username portion of URI */
2892 host
[HTTP_MAX_URI
], /* Host portion of URI */
2893 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2894 int port
; /* Port portion of URI */
2895 cupsd_job_t
*job
; /* Job information */
2896 cups_ptype_t dtype
; /* Destination type (printer or class) */
2897 cupsd_printer_t
*printer
; /* Printer data */
2900 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
2901 con
->http
.fd
, uri
->values
[0].string
.text
);
2904 * See if we have a job URI or a printer URI...
2907 if (!strcmp(uri
->name
, "printer-uri"))
2910 * Got a printer URI; see if we also have a job-id attribute...
2913 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2914 IPP_TAG_INTEGER
)) == NULL
)
2916 send_ipp_status(con
, IPP_BAD_REQUEST
,
2917 _("Got a printer-uri attribute but no job-id!"));
2921 if ((jobid
= attr
->values
[0].integer
) == 0)
2924 * Find the current job on the specified printer...
2927 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
2933 send_ipp_status(con
, IPP_NOT_FOUND
,
2934 _("The printer or class was not found."));
2939 * See if the printer is currently printing a job...
2943 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
2947 * No, see if there are any pending jobs...
2950 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
2952 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
2953 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
2954 !strcasecmp(job
->dest
, printer
->name
))
2961 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
2971 * Got a job URI; parse it to get the job ID...
2974 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2975 sizeof(scheme
), username
, sizeof(username
), host
,
2976 sizeof(host
), &port
, resource
, sizeof(resource
));
2978 if (strncmp(resource
, "/jobs/", 6))
2984 send_ipp_status(con
, IPP_BAD_REQUEST
,
2985 _("Bad job-uri attribute \"%s\"!"),
2986 uri
->values
[0].string
.text
);
2990 jobid
= atoi(resource
+ 6);
2994 * See if the job exists...
2997 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3000 * Nope - return a "not found" error...
3003 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3008 * See if the job is owned by the requesting user...
3011 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3013 send_http_error(con
, HTTP_UNAUTHORIZED
);
3018 * See if the job is already completed, canceled, or aborted; if so,
3019 * we can't cancel...
3022 if (job
->state_value
>= IPP_JOB_CANCELED
)
3024 switch (job
->state_value
)
3026 case IPP_JOB_CANCELED
:
3027 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3028 _("Job #%d is already canceled - can\'t cancel."),
3032 case IPP_JOB_ABORTED
:
3033 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3034 _("Job #%d is already aborted - can\'t cancel."),
3039 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3040 _("Job #%d is already completed - can\'t cancel."),
3049 * Cancel the job and return...
3052 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
3055 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was canceled by \"%s\".", jobid
,
3058 con
->response
->request
.status
.status_code
= IPP_OK
;
3063 * 'cancel_subscription()' - Cancel a subscription.
3067 cancel_subscription(
3068 cupsd_client_t
*con
, /* I - Client connection */
3069 int sub_id
) /* I - Subscription ID */
3071 http_status_t status
; /* Policy status */
3072 cupsd_subscription_t
*sub
; /* Subscription */
3075 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3076 "cancel_subscription(con=%p[%d], sub_id=%d)",
3077 con
, con
->http
.fd
, sub_id
);
3080 * Is the subscription ID valid?
3083 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3086 * Bad subscription ID...
3089 send_ipp_status(con
, IPP_NOT_FOUND
,
3090 _("notify-subscription-id %d no good!"), sub_id
);
3098 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3100 con
, sub
->owner
)) != HTTP_OK
)
3102 send_http_error(con
, status
);
3107 * Cancel the subscription...
3110 cupsdDeleteSubscription(sub
, 1);
3112 con
->response
->request
.status
.status_code
= IPP_OK
;
3117 * 'check_quotas()' - Check quotas for a printer and user.
3120 static int /* O - 1 if OK, 0 if not */
3121 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3122 cupsd_printer_t
*p
) /* I - Printer or class */
3124 int i
; /* Looping var */
3125 char username
[33]; /* Username */
3126 cupsd_quota_t
*q
; /* Quota data */
3127 struct passwd
*pw
; /* User password data */
3130 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3131 con
, con
->http
.fd
, p
, p
->name
);
3141 * Figure out who is printing...
3144 strlcpy(username
, get_username(con
), sizeof(username
));
3147 * Check global active job limits for printers and users...
3150 if (MaxJobsPerPrinter
)
3153 * Check if there are too many pending jobs on this printer...
3156 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3158 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3167 * Check if there are too many pending jobs for this user...
3170 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3172 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3179 * Check against users...
3182 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3187 pw
= getpwnam(username
);
3190 for (i
= 0; i
< p
->num_users
; i
++)
3191 if (p
->users
[i
][0] == '@')
3194 * Check group membership...
3197 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3200 else if (!strcasecmp(username
, p
->users
[i
]))
3203 if ((i
< p
->num_users
) == p
->deny_users
)
3205 cupsdLogMessage(CUPSD_LOG_INFO
,
3206 "Denying user \"%s\" access to printer \"%s\"...",
3216 if (p
->k_limit
|| p
->page_limit
)
3218 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3220 cupsdLogMessage(CUPSD_LOG_ERROR
,
3221 "Unable to allocate quota data for user \"%s\"!",
3226 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3227 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3229 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3236 * If we have gotten this far, we're done!
3244 * 'copy_attribute()' - Copy a single attribute.
3247 static ipp_attribute_t
* /* O - New attribute */
3249 ipp_t
*to
, /* O - Destination request/response */
3250 ipp_attribute_t
*attr
, /* I - Attribute to copy */
3251 int quickcopy
) /* I - Do a quick copy? */
3253 int i
; /* Looping var */
3254 ipp_attribute_t
*toattr
; /* Destination attribute */
3257 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3258 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
3259 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
3262 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
3265 toattr
= ippAddSeparator(to
);
3268 case IPP_TAG_INTEGER
:
3270 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3271 attr
->name
, attr
->num_values
, NULL
);
3273 for (i
= 0; i
< attr
->num_values
; i
++)
3274 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
3277 case IPP_TAG_BOOLEAN
:
3278 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
3279 attr
->num_values
, NULL
);
3281 for (i
= 0; i
< attr
->num_values
; i
++)
3282 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
3285 case IPP_TAG_STRING
:
3288 case IPP_TAG_KEYWORD
:
3290 case IPP_TAG_URISCHEME
:
3291 case IPP_TAG_CHARSET
:
3292 case IPP_TAG_LANGUAGE
:
3293 case IPP_TAG_MIMETYPE
:
3294 toattr
= ippAddStrings(to
, attr
->group_tag
,
3295 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3296 attr
->name
, attr
->num_values
, NULL
, NULL
);
3300 for (i
= 0; i
< attr
->num_values
; i
++)
3301 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3305 for (i
= 0; i
< attr
->num_values
; i
++)
3306 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3311 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
3312 attr
->values
[0].date
);
3315 case IPP_TAG_RESOLUTION
:
3316 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
3317 attr
->num_values
, IPP_RES_PER_INCH
,
3320 for (i
= 0; i
< attr
->num_values
; i
++)
3322 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
3323 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
3324 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
3328 case IPP_TAG_RANGE
:
3329 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
3330 attr
->num_values
, NULL
, NULL
);
3332 for (i
= 0; i
< attr
->num_values
; i
++)
3334 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
3335 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
3339 case IPP_TAG_TEXTLANG
:
3340 case IPP_TAG_NAMELANG
:
3341 toattr
= ippAddStrings(to
, attr
->group_tag
,
3342 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3343 attr
->name
, attr
->num_values
, NULL
, NULL
);
3347 for (i
= 0; i
< attr
->num_values
; i
++)
3349 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
3350 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3355 for (i
= 0; i
< attr
->num_values
; i
++)
3358 toattr
->values
[i
].string
.charset
=
3359 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
3361 toattr
->values
[i
].string
.charset
=
3362 toattr
->values
[0].string
.charset
;
3364 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3369 case IPP_TAG_BEGIN_COLLECTION
:
3370 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
3371 attr
->num_values
, NULL
);
3373 for (i
= 0; i
< attr
->num_values
; i
++)
3375 toattr
->values
[i
].collection
= ippNew();
3376 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
3377 NULL
, IPP_TAG_ZERO
, 0);
3382 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3383 attr
->name
, attr
->num_values
, NULL
);
3385 for (i
= 0; i
< attr
->num_values
; i
++)
3387 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
3389 if (toattr
->values
[i
].unknown
.length
> 0)
3391 if ((toattr
->values
[i
].unknown
.data
=
3392 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
3393 toattr
->values
[i
].unknown
.length
= 0;
3395 memcpy(toattr
->values
[i
].unknown
.data
,
3396 attr
->values
[i
].unknown
.data
,
3397 toattr
->values
[i
].unknown
.length
);
3400 break; /* anti-compiler-warning-code */
3408 * 'copy_attrs()' - Copy attributes from one request to another.
3412 copy_attrs(ipp_t
*to
, /* I - Destination request */
3413 ipp_t
*from
, /* I - Source request */
3414 cups_array_t
*ra
, /* I - Requested attributes */
3415 ipp_tag_t group
, /* I - Group to copy */
3416 int quickcopy
) /* I - Do a quick copy? */
3418 ipp_attribute_t
*fromattr
; /* Source attribute */
3421 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3422 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3423 to
, from
, ra
, group
, quickcopy
);
3428 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3431 * Filter attributes as needed...
3434 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3435 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
3438 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
3439 copy_attribute(to
, fromattr
, quickcopy
);
3445 * 'copy_banner()' - Copy a banner file to the requests directory for the
3449 static int /* O - Size of banner file in kbytes */
3450 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
3451 cupsd_job_t
*job
, /* I - Job information */
3452 const char *name
) /* I - Name of banner */
3454 int i
; /* Looping var */
3455 int kbytes
; /* Size of banner file in kbytes */
3456 char filename
[1024]; /* Job filename */
3457 cupsd_banner_t
*banner
; /* Pointer to banner */
3458 cups_file_t
*in
; /* Input file */
3459 cups_file_t
*out
; /* Output file */
3460 int ch
; /* Character from file */
3461 char attrname
[255], /* Name of attribute */
3462 *s
; /* Pointer into name */
3463 ipp_attribute_t
*attr
; /* Attribute */
3466 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
3467 con
, con
->http
.fd
, job
, job
->id
, name
? name
: "(null)");
3470 * Find the banner; return if not found or "none"...
3473 if (!name
|| !strcmp(name
, "none") ||
3474 (banner
= cupsdFindBanner(name
)) == NULL
)
3478 * Open the banner and job files...
3481 if (add_file(con
, job
, banner
->filetype
, 0))
3484 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
3486 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
3488 cupsdLogMessage(CUPSD_LOG_ERROR
,
3489 "copy_banner: Unable to create banner job file %s - %s",
3490 filename
, strerror(errno
));
3495 fchmod(cupsFileNumber(out
), 0640);
3496 fchown(cupsFileNumber(out
), RunUser
, Group
);
3499 * Try the localized banner file under the subdirectory...
3502 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
3504 if (strlen(attrname
) > 2 && attrname
[2] == '-')
3507 * Convert ll-cc to ll_CC...
3511 attrname
[3] = toupper(attrname
[3] & 255);
3512 attrname
[4] = toupper(attrname
[4] & 255);
3515 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3518 if (access(filename
, 0) && strlen(attrname
) > 2)
3521 * Wasn't able to find "ll_CC" locale file; try the non-national
3522 * localization banner directory.
3527 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3531 if (access(filename
, 0))
3534 * Use the non-localized banner file.
3537 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
3540 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
3544 cupsdLogMessage(CUPSD_LOG_ERROR
,
3545 "copy_banner: Unable to open banner template file %s - %s",
3546 filename
, strerror(errno
));
3552 * Parse the file to the end...
3555 while ((ch
= cupsFileGetChar(in
)) != EOF
)
3559 * Get an attribute name...
3562 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
3563 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
3565 else if (s
< (attrname
+ sizeof(attrname
) - 1))
3575 * Ignore { followed by stuff that is not an attribute name...
3578 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
3583 * See if it is defined...
3586 if (attrname
[0] == '?')
3591 if (!strcmp(s
, "printer-name"))
3593 cupsFilePuts(out
, job
->dest
);
3596 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
3599 * See if we have a leading question mark...
3602 if (attrname
[0] != '?')
3605 * Nope, write to file as-is; probably a PostScript procedure...
3608 cupsFilePrintf(out
, "{%s}", attrname
);
3615 * Output value(s)...
3618 for (i
= 0; i
< attr
->num_values
; i
++)
3621 cupsFilePutChar(out
, ',');
3623 switch (attr
->value_tag
)
3625 case IPP_TAG_INTEGER
:
3627 if (!strncmp(s
, "time-at-", 8))
3628 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
3630 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
3633 case IPP_TAG_BOOLEAN
:
3634 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
3637 case IPP_TAG_NOVALUE
:
3638 cupsFilePuts(out
, "novalue");
3641 case IPP_TAG_RANGE
:
3642 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
3643 attr
->values
[i
].range
.upper
);
3646 case IPP_TAG_RESOLUTION
:
3647 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3648 attr
->values
[i
].resolution
.yres
,
3649 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3654 case IPP_TAG_STRING
:
3657 case IPP_TAG_KEYWORD
:
3658 case IPP_TAG_CHARSET
:
3659 case IPP_TAG_LANGUAGE
:
3660 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
3663 * Need to quote strings for PS banners...
3668 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
3670 if (*p
== '(' || *p
== ')' || *p
== '\\')
3672 cupsFilePutChar(out
, '\\');
3673 cupsFilePutChar(out
, *p
);
3675 else if (*p
< 32 || *p
> 126)
3676 cupsFilePrintf(out
, "\\%03o", *p
& 255);
3678 cupsFilePutChar(out
, *p
);
3682 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
3686 break; /* anti-compiler-warning-code */
3690 else if (ch
== '\\') /* Quoted char */
3692 ch
= cupsFileGetChar(in
);
3694 if (ch
!= '{') /* Only do special handling for \{ */
3695 cupsFilePutChar(out
, '\\');
3697 cupsFilePutChar(out
, ch
);
3700 cupsFilePutChar(out
, ch
);
3704 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
3706 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
3707 IPP_TAG_INTEGER
)) != NULL
)
3708 attr
->values
[0].integer
+= kbytes
;
3717 * 'copy_file()' - Copy a PPD file or interface script...
3720 static int /* O - 0 = success, -1 = error */
3721 copy_file(const char *from
, /* I - Source file */
3722 const char *to
) /* I - Destination file */
3724 cups_file_t
*src
, /* Source file */
3725 *dst
; /* Destination file */
3726 int bytes
; /* Bytes to read/write */
3727 char buffer
[2048]; /* Copy buffer */
3730 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
3733 * Open the source and destination file for a copy...
3736 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
3739 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
3746 * Copy the source file to the destination...
3749 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
3750 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
3758 * Close both files and return...
3763 return (cupsFileClose(dst
));
3768 * 'copy_model()' - Copy a PPD model file, substituting default values
3772 static int /* O - 0 = success, -1 = error */
3773 copy_model(cupsd_client_t
*con
, /* I - Client connection */
3774 const char *from
, /* I - Source file */
3775 const char *to
) /* I - Destination file */
3777 fd_set input
; /* select() input set */
3778 struct timeval timeout
; /* select() timeout */
3779 int maxfd
; /* Maximum file descriptor for select() */
3780 char tempfile
[1024]; /* Temporary PPD file */
3781 int tempfd
; /* Temporary PPD file descriptor */
3782 int temppid
; /* Process ID of cups-driverd */
3783 int temppipe
[2]; /* Temporary pipes */
3784 char *argv
[4], /* Command-line arguments */
3785 *envp
[MAX_ENV
]; /* Environment */
3786 cups_file_t
*src
, /* Source file */
3787 *dst
; /* Destination file */
3788 ppd_file_t
*ppd
; /* PPD file */
3789 int bytes
, /* Bytes from pipe */
3790 total
; /* Total bytes from pipe */
3791 char buffer
[2048]; /* Copy buffer */
3792 int i
; /* Looping var */
3793 char option
[PPD_MAX_NAME
], /* Option name */
3794 choice
[PPD_MAX_NAME
]; /* Choice name */
3795 int num_defaults
; /* Number of default options */
3796 cups_option_t
*defaults
; /* Default options */
3797 char cups_protocol
[PPD_MAX_LINE
];
3798 /* cupsProtocol attribute */
3799 int have_letter
, /* Have Letter size */
3800 have_a4
; /* Have A4 size */
3801 #ifdef HAVE_LIBPAPER
3802 char *paper_result
; /* Paper size name from libpaper */
3803 char system_paper
[64]; /* Paper size name buffer */
3804 #endif /* HAVE_LIBPAPER */
3807 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3808 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
3812 * Run cups-driverd to get the PPD file...
3815 argv
[0] = "cups-driverd";
3817 argv
[2] = (char *)from
;
3820 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3822 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
3823 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
3824 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3828 cupsdOpenPipe(temppipe
);
3830 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3831 "copy_model: Running \"cups-driverd cat %s\"...", from
);
3833 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
3834 -1, -1, 0, &temppid
))
3844 * Wait up to 30 seconds for the PPD file to be copied...
3849 if (temppipe
[0] > CGIPipes
[0])
3850 maxfd
= temppipe
[0] + 1;
3852 maxfd
= CGIPipes
[0] + 1;
3857 * See if we have data ready...
3863 FD_SET(temppipe
[0], &input
);
3864 FD_SET(CGIPipes
[0], &input
);
3866 timeout
.tv_sec
= 30;
3867 timeout
.tv_usec
= 0;
3869 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
3879 * We have timed out...
3885 if (FD_ISSET(temppipe
[0], &input
))
3888 * Read the PPD file from the pipe, and write it to the PPD file.
3891 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
3893 if (write(tempfd
, buffer
, bytes
) < bytes
)
3902 if (FD_ISSET(CGIPipes
[0], &input
))
3912 * No data from cups-deviced...
3915 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
3921 * Read the source file and see what page sizes are supported...
3924 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
3930 have_letter
= ppdPageSize(ppd
, "Letter") != NULL
;
3931 have_a4
= ppdPageSize(ppd
, "A4") != NULL
;
3934 * Open the destination (if possible) and set the default options...
3939 cups_protocol
[0] = '\0';
3941 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
3944 * Read all of the default lines from the old PPD...
3947 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
3948 if (!strncmp(buffer
, "*Default", 8))
3951 * Add the default option...
3954 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
3955 choice
, sizeof(choice
)))
3957 ppd_option_t
*ppdo
; /* PPD option */
3961 * Only add the default if the default hasn't already been
3962 * set and the choice exists in the new PPD...
3965 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
3966 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
3967 ppdFindChoice(ppdo
, choice
))
3968 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
3972 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
3973 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
3977 #ifdef HAVE_LIBPAPER
3978 else if ((paper_result
= systempapername()) != NULL
)
3981 * Set the default media sizes from the systemwide default...
3984 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
3985 system_paper
[0] = toupper(system_paper
[0] & 255);
3987 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
3988 (!strcmp(system_paper
, "A4") && have_a4
))
3990 num_defaults
= cupsAddOption("PageSize", system_paper
,
3991 num_defaults
, &defaults
);
3992 num_defaults
= cupsAddOption("PageRegion", system_paper
,
3993 num_defaults
, &defaults
);
3994 num_defaults
= cupsAddOption("PaperDimension", system_paper
,
3995 num_defaults
, &defaults
);
3996 num_defaults
= cupsAddOption("ImageableArea", system_paper
,
3997 num_defaults
, &defaults
);
4000 #endif /* HAVE_LIBPAPER */
4004 * Add the default media sizes...
4006 * Note: These values are generally not valid for large-format devices
4007 * like plotters, however it is probably safe to say that those
4008 * users will configure the media size after initially adding
4009 * the device anyways...
4012 if (!DefaultLanguage
||
4013 !strcasecmp(DefaultLanguage
, "C") ||
4014 !strcasecmp(DefaultLanguage
, "POSIX") ||
4015 !strcasecmp(DefaultLanguage
, "en") ||
4016 !strncasecmp(DefaultLanguage
, "en.", 3) ||
4017 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4018 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4019 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4022 * These are the only locales that will default to "letter" size...
4027 num_defaults
= cupsAddOption("PageSize", "Letter", num_defaults
,
4029 num_defaults
= cupsAddOption("PageRegion", "Letter", num_defaults
,
4031 num_defaults
= cupsAddOption("PaperDimension", "Letter", num_defaults
,
4033 num_defaults
= cupsAddOption("ImageableArea", "Letter", num_defaults
,
4040 * The rest default to "a4" size...
4043 num_defaults
= cupsAddOption("PageSize", "A4", num_defaults
,
4045 num_defaults
= cupsAddOption("PageRegion", "A4", num_defaults
,
4047 num_defaults
= cupsAddOption("PaperDimension", "A4", num_defaults
,
4049 num_defaults
= cupsAddOption("ImageableArea", "A4", num_defaults
,
4057 * Open the source file for a copy...
4060 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4062 cupsFreeOptions(num_defaults
, defaults
);
4068 * Open the destination file for a copy...
4071 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4073 cupsFreeOptions(num_defaults
, defaults
);
4080 * Copy the source file to the destination...
4083 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4085 if (!strncmp(buffer
, "*Default", 8))
4088 * Check for an previous default option choice...
4091 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4092 choice
, sizeof(choice
)))
4094 const char *val
; /* Default option value */
4097 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4100 * Substitute the previous choice...
4103 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4108 cupsFilePrintf(dst
, "%s\n", buffer
);
4111 if (cups_protocol
[0])
4112 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4114 cupsFreeOptions(num_defaults
, defaults
);
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 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4291 "/classes/%s" : "/printers/%s", printer
->name
);
4292 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4293 "printer-uri-supported", NULL
, printer_uri
);
4294 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4298 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4299 add_queued_job_count(con
, printer
);
4301 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
4302 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
4307 * 'copy_subscription_attrs()' - Copy subscription attributes.
4311 copy_subscription_attrs(
4312 cupsd_client_t
*con
, /* I - Client connection */
4313 cupsd_subscription_t
*sub
, /* I - Subscription */
4314 cups_array_t
*ra
) /* I - Requested attributes array */
4316 ipp_attribute_t
*attr
; /* Current attribute */
4317 char printer_uri
[HTTP_MAX_URI
];
4319 int count
; /* Number of events */
4320 unsigned mask
; /* Current event mask */
4321 const char *name
; /* Current event name */
4325 * Copy the subscription attributes to the response using the
4326 * requested-attributes attribute that may be provided by the client.
4329 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
4331 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
4334 * Simple event list...
4337 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
4338 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
4339 "notify-events", NULL
, name
);
4344 * Complex event list...
4347 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4348 if (sub
->mask
& mask
)
4351 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
4352 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
4353 "notify-events", count
, NULL
, NULL
);
4355 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4356 if (sub
->mask
& mask
)
4358 attr
->values
[count
].string
.text
=
4359 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
4366 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
4367 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4368 "notify-job-id", sub
->job
->id
);
4370 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
4371 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4372 "notify-lease-duration", sub
->lease
);
4374 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
4376 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4377 "ipp", NULL
, con
->servername
, con
->serverport
,
4378 "/printers/%s", sub
->dest
->name
);
4379 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4380 "notify-printer-uri", NULL
, printer_uri
);
4383 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
4384 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4385 "notify-recipient-uri", NULL
, sub
->recipient
);
4386 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
4387 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
4388 "notify-pull-method", NULL
, "ippget");
4390 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
4391 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
4392 "notify-subscriber-user-name", NULL
, sub
->owner
);
4394 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
4395 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4396 "notify-subscription-id", sub
->id
);
4398 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
4399 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4400 "notify-time-interval", sub
->interval
);
4402 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
4403 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
4404 sub
->user_data
, sub
->user_data_len
);
4409 * 'create_job()' - Print a file to a printer or class.
4413 create_job(cupsd_client_t
*con
, /* I - Client connection */
4414 ipp_attribute_t
*uri
) /* I - Printer URI */
4416 cupsd_printer_t
*printer
; /* Printer */
4417 cupsd_job_t
*job
; /* New job */
4420 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
4421 con
->http
.fd
, uri
->values
[0].string
.text
);
4424 * Is the destination valid?
4427 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
4433 send_ipp_status(con
, IPP_NOT_FOUND
,
4434 _("The printer or class was not found."));
4439 * Create the job object...
4442 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
4446 * Save and log the job...
4451 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d created on \"%s\" by \"%s\".",
4452 job
->id
, job
->dest
, job
->username
);
4457 * 'create_requested_array()' - Create an array for the requested-attributes.
4460 static cups_array_t
* /* O - Array of attributes or NULL */
4461 create_requested_array(ipp_t
*request
) /* I - IPP request */
4463 int i
; /* Looping var */
4464 ipp_attribute_t
*requested
; /* requested-attributes attribute */
4465 cups_array_t
*ra
; /* Requested attributes array */
4466 char *value
; /* Current value */
4470 * Get the requested-attributes attribute, and return NULL if we don't
4474 if ((requested
= ippFindAttribute(request
, "requested-attributes",
4475 IPP_TAG_KEYWORD
)) == NULL
)
4479 * If the attribute contains a single "all" keyword, return NULL...
4482 if (requested
->num_values
== 1 &&
4483 !strcmp(requested
->values
[0].string
.text
, "all"))
4487 * Create an array using "strcmp" as the comparison function...
4490 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
4492 for (i
= 0; i
< requested
->num_values
; i
++)
4494 value
= requested
->values
[i
].string
.text
;
4496 if (!strcmp(value
, "job-template"))
4498 cupsArrayAdd(ra
, "copies");
4499 cupsArrayAdd(ra
, "copies-default");
4500 cupsArrayAdd(ra
, "copies-supported");
4501 cupsArrayAdd(ra
, "finishings");
4502 cupsArrayAdd(ra
, "finishings-default");
4503 cupsArrayAdd(ra
, "finishings-supported");
4504 cupsArrayAdd(ra
, "job-hold-until");
4505 cupsArrayAdd(ra
, "job-hold-until-default");
4506 cupsArrayAdd(ra
, "job-hold-until-supported");
4507 cupsArrayAdd(ra
, "job-priority");
4508 cupsArrayAdd(ra
, "job-priority-default");
4509 cupsArrayAdd(ra
, "job-priority-supported");
4510 cupsArrayAdd(ra
, "job-sheets");
4511 cupsArrayAdd(ra
, "job-sheets-default");
4512 cupsArrayAdd(ra
, "job-sheets-supported");
4513 cupsArrayAdd(ra
, "media");
4514 cupsArrayAdd(ra
, "media-default");
4515 cupsArrayAdd(ra
, "media-supported");
4516 cupsArrayAdd(ra
, "multiple-document-handling");
4517 cupsArrayAdd(ra
, "multiple-document-handling-default");
4518 cupsArrayAdd(ra
, "multiple-document-handling-supported");
4519 cupsArrayAdd(ra
, "number-up");
4520 cupsArrayAdd(ra
, "number-up-default");
4521 cupsArrayAdd(ra
, "number-up-supported");
4522 cupsArrayAdd(ra
, "orientation-requested");
4523 cupsArrayAdd(ra
, "orientation-requested-default");
4524 cupsArrayAdd(ra
, "orientation-requested-supported");
4525 cupsArrayAdd(ra
, "page-ranges");
4526 cupsArrayAdd(ra
, "page-ranges-supported");
4527 cupsArrayAdd(ra
, "printer-resolution");
4528 cupsArrayAdd(ra
, "printer-resolution-default");
4529 cupsArrayAdd(ra
, "printer-resolution-supported");
4530 cupsArrayAdd(ra
, "print-quality");
4531 cupsArrayAdd(ra
, "print-quality-default");
4532 cupsArrayAdd(ra
, "print-quality-supported");
4533 cupsArrayAdd(ra
, "sides");
4534 cupsArrayAdd(ra
, "sides-default");
4535 cupsArrayAdd(ra
, "sides-supported");
4537 else if (!strcmp(value
, "job-description"))
4539 cupsArrayAdd(ra
, "date-time-at-completed");
4540 cupsArrayAdd(ra
, "date-time-at-creation");
4541 cupsArrayAdd(ra
, "date-time-at-processing");
4542 cupsArrayAdd(ra
, "job-detailed-status-message");
4543 cupsArrayAdd(ra
, "job-document-access-errors");
4544 cupsArrayAdd(ra
, "job-id");
4545 cupsArrayAdd(ra
, "job-impressions");
4546 cupsArrayAdd(ra
, "job-impressions-completed");
4547 cupsArrayAdd(ra
, "job-k-octets");
4548 cupsArrayAdd(ra
, "job-k-octets-processed");
4549 cupsArrayAdd(ra
, "job-media-sheets");
4550 cupsArrayAdd(ra
, "job-media-sheets-completed");
4551 cupsArrayAdd(ra
, "job-message-from-operator");
4552 cupsArrayAdd(ra
, "job-more-info");
4553 cupsArrayAdd(ra
, "job-name");
4554 cupsArrayAdd(ra
, "job-originating-user-name");
4555 cupsArrayAdd(ra
, "job-printer-up-time");
4556 cupsArrayAdd(ra
, "job-printer-uri");
4557 cupsArrayAdd(ra
, "job-state");
4558 cupsArrayAdd(ra
, "job-state-message");
4559 cupsArrayAdd(ra
, "job-state-reasons");
4560 cupsArrayAdd(ra
, "job-uri");
4561 cupsArrayAdd(ra
, "number-of-documents");
4562 cupsArrayAdd(ra
, "number-of-intervening-jobs");
4563 cupsArrayAdd(ra
, "output-device-assigned");
4564 cupsArrayAdd(ra
, "time-at-completed");
4565 cupsArrayAdd(ra
, "time-at-creation");
4566 cupsArrayAdd(ra
, "time-at-processing");
4568 else if (!strcmp(value
, "printer-description"))
4570 cupsArrayAdd(ra
, "charset-configured");
4571 cupsArrayAdd(ra
, "charset-supported");
4572 cupsArrayAdd(ra
, "color-supported");
4573 cupsArrayAdd(ra
, "compression-supported");
4574 cupsArrayAdd(ra
, "document-format-default");
4575 cupsArrayAdd(ra
, "document-format-supported");
4576 cupsArrayAdd(ra
, "generated-natural-language-supported");
4577 cupsArrayAdd(ra
, "ipp-versions-supported");
4578 cupsArrayAdd(ra
, "job-impressions-supported");
4579 cupsArrayAdd(ra
, "job-k-octets-supported");
4580 cupsArrayAdd(ra
, "job-media-sheets-supported");
4581 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
4582 cupsArrayAdd(ra
, "multiple-operation-time-out");
4583 cupsArrayAdd(ra
, "natural-language-configured");
4584 cupsArrayAdd(ra
, "notify-attributes-supported");
4585 cupsArrayAdd(ra
, "notify-lease-duration-default");
4586 cupsArrayAdd(ra
, "notify-lease-duration-supported");
4587 cupsArrayAdd(ra
, "notify-max-events-supported");
4588 cupsArrayAdd(ra
, "notify-events-default");
4589 cupsArrayAdd(ra
, "notify-events-supported");
4590 cupsArrayAdd(ra
, "notify-pull-method-supported");
4591 cupsArrayAdd(ra
, "notify-schemes-supported");
4592 cupsArrayAdd(ra
, "operations-supported");
4593 cupsArrayAdd(ra
, "pages-per-minute");
4594 cupsArrayAdd(ra
, "pages-per-minute-color");
4595 cupsArrayAdd(ra
, "pdl-override-supported");
4596 cupsArrayAdd(ra
, "printer-current-time");
4597 cupsArrayAdd(ra
, "printer-driver-installer");
4598 cupsArrayAdd(ra
, "printer-info");
4599 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
4600 cupsArrayAdd(ra
, "printer-location");
4601 cupsArrayAdd(ra
, "printer-make-and-model");
4602 cupsArrayAdd(ra
, "printer-message-from-operator");
4603 cupsArrayAdd(ra
, "printer-more-info");
4604 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
4605 cupsArrayAdd(ra
, "printer-name");
4606 cupsArrayAdd(ra
, "printer-state");
4607 cupsArrayAdd(ra
, "printer-state-message");
4608 cupsArrayAdd(ra
, "printer-state-reasons");
4609 cupsArrayAdd(ra
, "printer-up-time");
4610 cupsArrayAdd(ra
, "printer-uri-supported");
4611 cupsArrayAdd(ra
, "queued-job-count");
4612 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
4613 cupsArrayAdd(ra
, "uri-authentication-supported");
4614 cupsArrayAdd(ra
, "uri-security-supported");
4616 else if (!strcmp(value
, "printer-defaults"))
4618 char *name
; /* Option name */
4621 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
4623 name
= (char *)cupsArrayNext(CommonDefaults
))
4624 cupsArrayAdd(ra
, name
);
4626 else if (!strcmp(value
, "subscription-template"))
4628 cupsArrayAdd(ra
, "notify-attributes");
4629 cupsArrayAdd(ra
, "notify-charset");
4630 cupsArrayAdd(ra
, "notify-events");
4631 cupsArrayAdd(ra
, "notify-lease-duration");
4632 cupsArrayAdd(ra
, "notify-natural-language");
4633 cupsArrayAdd(ra
, "notify-pull-method");
4634 cupsArrayAdd(ra
, "notify-recipient-uri");
4635 cupsArrayAdd(ra
, "notify-time-interval");
4636 cupsArrayAdd(ra
, "notify-user-data");
4639 cupsArrayAdd(ra
, value
);
4647 * 'create_subscription()' - Create a notification subscription.
4651 create_subscription(
4652 cupsd_client_t
*con
, /* I - Client connection */
4653 ipp_attribute_t
*uri
) /* I - Printer URI */
4655 http_status_t status
; /* Policy status */
4656 int i
; /* Looping var */
4657 ipp_attribute_t
*attr
; /* Current attribute */
4658 cups_ptype_t dtype
; /* Destination type (printer or class) */
4659 char scheme
[HTTP_MAX_URI
],
4660 /* Scheme portion of URI */
4661 userpass
[HTTP_MAX_URI
],
4662 /* Username portion of URI */
4664 /* Host portion of URI */
4665 resource
[HTTP_MAX_URI
];
4666 /* Resource portion of URI */
4667 int port
; /* Port portion of URI */
4668 cupsd_printer_t
*printer
; /* Printer/class */
4669 cupsd_job_t
*job
; /* Job */
4670 int jobid
; /* Job ID */
4671 cupsd_subscription_t
*sub
; /* Subscription object */
4672 const char *username
, /* requesting-user-name or authenticated username */
4673 *recipient
, /* notify-recipient-uri */
4674 *pullmethod
; /* notify-pull-method */
4675 ipp_attribute_t
*user_data
; /* notify-user-data */
4676 int interval
, /* notify-time-interval */
4677 lease
; /* notify-lease-duration */
4678 unsigned mask
; /* notify-events */
4679 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
4680 *notify_lease
; /* notify-lease-duration(-default) */
4684 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4686 if (attr
->group_tag
!= IPP_TAG_ZERO
)
4687 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
4688 attr
->value_tag
, attr
->name
);
4690 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
4695 * Is the destination valid?
4698 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4699 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
4700 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
4702 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
4703 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
4704 sizeof(host
), &port
, resource
, sizeof(resource
));
4706 if (!strcmp(resource
, "/"))
4708 dtype
= (cups_ptype_t
)0;
4711 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
4713 dtype
= (cups_ptype_t
)0;
4716 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
4718 dtype
= CUPS_PRINTER_CLASS
;
4721 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
4727 send_ipp_status(con
, IPP_NOT_FOUND
,
4728 _("The printer or class was not found."));
4738 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
4741 send_http_error(con
, status
);
4745 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
4747 send_http_error(con
, status
);
4752 * Get the user that is requesting the subscription...
4755 username
= get_username(con
);
4758 * Find the first subscription group attribute; return if we have
4762 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4763 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
4768 send_ipp_status(con
, IPP_BAD_REQUEST
,
4769 _("No subscription attributes in request!"));
4774 * Process the subscription attributes in the request...
4777 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
4785 lease
= DefaultLeaseDuration
;
4787 mask
= CUPSD_EVENT_NONE
;
4791 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
4793 notify_lease
= ippFindAttribute(printer
->attrs
,
4794 "notify-lease-duration-default",
4798 lease
= notify_lease
->values
[0].integer
;
4802 notify_events
= NULL
;
4803 notify_lease
= NULL
;
4806 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
4808 if (!strcmp(attr
->name
, "notify-recipient") &&
4809 attr
->value_tag
== IPP_TAG_URI
)
4812 * Validate the recipient scheme against the ServerBin/notifier
4816 char notifier
[1024]; /* Notifier filename */
4819 recipient
= attr
->values
[0].string
.text
;
4821 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
4822 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
4823 host
, sizeof(host
), &port
,
4824 resource
, sizeof(resource
)) < HTTP_URI_OK
)
4826 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4827 _("Bad notify-recipient URI \"%s\"!"), recipient
);
4828 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
4829 "notify-status-code", IPP_URI_SCHEME
);
4833 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
4835 if (access(notifier
, X_OK
))
4837 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4838 _("notify-recipient URI \"%s\" uses unknown scheme!"),
4840 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
4841 "notify-status-code", IPP_URI_SCHEME
);
4845 else if (!strcmp(attr
->name
, "notify-pull-method") &&
4846 attr
->value_tag
== IPP_TAG_KEYWORD
)
4848 pullmethod
= attr
->values
[0].string
.text
;
4850 if (strcmp(pullmethod
, "ippget"))
4852 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4853 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
4854 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
4855 "notify-status-code", IPP_ATTRIBUTES
);
4859 else if (!strcmp(attr
->name
, "notify-charset") &&
4860 attr
->value_tag
== IPP_TAG_CHARSET
&&
4861 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
4862 strcmp(attr
->values
[0].string
.text
, "utf-8"))
4864 send_ipp_status(con
, IPP_CHARSET
,
4865 _("Character set \"%s\" not supported!"),
4866 attr
->values
[0].string
.text
);
4869 else if (!strcmp(attr
->name
, "notify-natural-language") &&
4870 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
4871 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
4873 send_ipp_status(con
, IPP_CHARSET
,
4874 _("Language \"%s\" not supported!"),
4875 attr
->values
[0].string
.text
);
4878 else if (!strcmp(attr
->name
, "notify-user-data") &&
4879 attr
->value_tag
== IPP_TAG_STRING
)
4881 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
4883 send_ipp_status(con
, IPP_REQUEST_VALUE
,
4884 _("The notify-user-data value is too large "
4885 "(%d > 63 octets)!"),
4886 attr
->values
[0].unknown
.length
);
4892 else if (!strcmp(attr
->name
, "notify-events") &&
4893 attr
->value_tag
== IPP_TAG_KEYWORD
)
4894 notify_events
= attr
;
4895 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
4896 attr
->value_tag
== IPP_TAG_INTEGER
)
4897 lease
= attr
->values
[0].integer
;
4898 else if (!strcmp(attr
->name
, "notify-time-interval") &&
4899 attr
->value_tag
== IPP_TAG_INTEGER
)
4900 interval
= attr
->values
[0].integer
;
4901 else if (!strcmp(attr
->name
, "notify-job-id") &&
4902 attr
->value_tag
== IPP_TAG_INTEGER
)
4903 jobid
= attr
->values
[0].integer
;
4910 for (i
= 0; i
< notify_events
->num_values
; i
++)
4911 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
4915 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
4917 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
4918 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
4919 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
4921 if (!recipient
&& !pullmethod
)
4924 if (mask
== CUPSD_EVENT_NONE
)
4927 mask
= CUPSD_EVENT_JOB_COMPLETED
;
4929 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4932 send_ipp_status(con
, IPP_BAD_REQUEST
,
4933 _("notify-events not specified!"));
4938 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
4940 cupsdLogMessage(CUPSD_LOG_INFO
,
4941 "create_subscription: Limiting notify-lease-duration to "
4944 lease
= MaxLeaseDuration
;
4949 if ((job
= cupsdFindJob(jobid
)) == NULL
)
4951 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
4958 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
4961 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
4964 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4965 "Added subscription %d for printer \"%s\"",
4966 sub
->id
, printer
->name
);
4968 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
4971 sub
->interval
= interval
;
4973 sub
->expire
= lease
? time(NULL
) + lease
: 0;
4975 cupsdSetString(&sub
->owner
, username
);
4979 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
4980 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
4981 sub
->user_data_len
);
4984 ippAddSeparator(con
->response
);
4985 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4986 "notify-subscription-id", sub
->id
);
4988 con
->response
->request
.status
.status_code
= IPP_OK
;
4994 cupsdSaveAllSubscriptions();
5000 * 'delete_printer()' - Remove a printer or class from the system.
5004 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5005 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5007 http_status_t status
; /* Policy status */
5008 cups_ptype_t dtype
; /* Destination type (printer or class) */
5009 cupsd_printer_t
*printer
; /* Printer/class */
5010 char filename
[1024]; /* Script/PPD filename */
5013 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5014 con
->http
.fd
, uri
->values
[0].string
.text
);
5017 * Do we have a valid URI?
5020 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5026 send_ipp_status(con
, IPP_NOT_FOUND
,
5027 _("The printer or class was not found."));
5035 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5037 send_http_error(con
, status
);
5042 * Remove old jobs...
5045 cupsdCancelJobs(printer
->name
, NULL
, 1);
5048 * Remove old subscriptions and send a "deleted printer" event...
5051 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5052 "%s \"%s\" deleted by \"%s\".",
5053 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5054 printer
->name
, get_username(con
));
5056 cupsdExpireSubscriptions(printer
, NULL
);
5059 * Remove any old PPD or script files...
5062 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5066 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5070 if (dtype
& CUPS_PRINTER_CLASS
)
5072 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5073 printer
->name
, get_username(con
));
5075 cupsdDeletePrinter(printer
, 0);
5076 cupsdSaveAllClasses();
5080 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5081 printer
->name
, get_username(con
));
5083 cupsdDeletePrinter(printer
, 0);
5084 cupsdSaveAllPrinters();
5087 cupsdWritePrintcap();
5090 * Return with no errors...
5093 con
->response
->request
.status
.status_code
= IPP_OK
;
5098 * 'get_default()' - Get the default destination.
5102 get_default(cupsd_client_t
*con
) /* I - Client connection */
5104 http_status_t status
; /* Policy status */
5105 cups_array_t
*ra
; /* Requested attributes array */
5108 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5114 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5116 send_http_error(con
, status
);
5122 ra
= create_requested_array(con
->request
);
5124 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5126 cupsArrayDelete(ra
);
5128 con
->response
->request
.status
.status_code
= IPP_OK
;
5131 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5136 * 'get_devices()' - Get the list of available devices on the local system.
5140 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5142 http_status_t status
; /* Policy status */
5143 ipp_attribute_t
*limit
, /* Limit attribute */
5144 *requested
; /* requested-attributes attribute */
5145 char command
[1024], /* cups-deviced command */
5146 options
[1024], /* Options to pass to command */
5148 /* String for requested attributes */
5151 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5157 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5159 send_http_error(con
, status
);
5164 * Run cups-deviced command with the given options...
5167 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5168 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5172 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5174 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5176 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5177 snprintf(options
, sizeof(options
),
5179 con
->request
->request
.op
.request_id
,
5180 limit
? limit
->values
[0].integer
: 0, (int)User
,
5183 if (cupsdSendCommand(con
, command
, options
, 1))
5186 * Command started successfully, don't send an IPP response here...
5189 ippDelete(con
->response
);
5190 con
->response
= NULL
;
5195 * Command failed, return "internal error" so the user knows something
5199 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5200 _("cups-deviced failed to execute."));
5206 * 'get_job_attrs()' - Get job attributes.
5210 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5211 ipp_attribute_t
*uri
) /* I - Job URI */
5213 http_status_t status
; /* Policy status */
5214 ipp_attribute_t
*attr
; /* Current attribute */
5215 int jobid
; /* Job ID */
5216 cupsd_job_t
*job
; /* Current job */
5217 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5218 username
[HTTP_MAX_URI
], /* Username portion of URI */
5219 host
[HTTP_MAX_URI
], /* Host portion of URI */
5220 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5221 int port
; /* Port portion of URI */
5222 cups_array_t
*ra
; /* Requested attributes array */
5225 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5226 con
->http
.fd
, uri
->values
[0].string
.text
);
5229 * See if we have a job URI or a printer URI...
5232 if (!strcmp(uri
->name
, "printer-uri"))
5235 * Got a printer URI; see if we also have a job-id attribute...
5238 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5239 IPP_TAG_INTEGER
)) == NULL
)
5241 send_ipp_status(con
, IPP_BAD_REQUEST
,
5242 _("Got a printer-uri attribute but no job-id!"));
5246 jobid
= attr
->values
[0].integer
;
5251 * Got a job URI; parse it to get the job ID...
5254 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5255 sizeof(method
), username
, sizeof(username
), host
,
5256 sizeof(host
), &port
, resource
, sizeof(resource
));
5258 if (strncmp(resource
, "/jobs/", 6))
5264 send_ipp_status(con
, IPP_BAD_REQUEST
,
5265 _("Bad job-uri attribute \"%s\"!"),
5266 uri
->values
[0].string
.text
);
5270 jobid
= atoi(resource
+ 6);
5274 * See if the job exists...
5277 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5280 * Nope - return a "not found" error...
5283 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5291 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5293 send_http_error(con
, status
);
5298 * Copy attributes...
5303 ra
= create_requested_array(con
->request
);
5304 copy_job_attrs(con
, job
, ra
);
5305 cupsArrayDelete(ra
);
5307 con
->response
->request
.status
.status_code
= IPP_OK
;
5312 * 'get_jobs()' - Get a list of jobs for the specified printer.
5316 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
5317 ipp_attribute_t
*uri
) /* I - Printer URI */
5319 http_status_t status
; /* Policy status */
5320 ipp_attribute_t
*attr
; /* Current attribute */
5321 const char *dest
; /* Destination */
5322 cups_ptype_t dtype
; /* Destination type (printer or class) */
5323 cups_ptype_t dmask
; /* Destination type mask */
5324 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
5325 username
[HTTP_MAX_URI
], /* Username portion of URI */
5326 host
[HTTP_MAX_URI
], /* Host portion of URI */
5327 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5328 int port
; /* Port portion of URI */
5329 int completed
; /* Completed jobs? */
5330 int first_job_id
; /* First job ID */
5331 int limit
; /* Maximum number of jobs to return */
5332 int count
; /* Number of jobs that match */
5333 cupsd_job_t
*job
; /* Current job pointer */
5334 cupsd_printer_t
*printer
; /* Printer */
5335 cups_array_t
*list
; /* Which job list... */
5336 cups_array_t
*ra
; /* Requested attributes array */
5339 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
5340 uri
->values
[0].string
.text
);
5343 * Is the destination valid?
5346 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5347 sizeof(scheme
), username
, sizeof(username
), host
,
5348 sizeof(host
), &port
, resource
, sizeof(resource
));
5350 if (!strcmp(resource
, "/") ||
5351 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
5354 dtype
= (cups_ptype_t
)0;
5355 dmask
= (cups_ptype_t
)0;
5358 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5361 dtype
= (cups_ptype_t
)0;
5362 dmask
= CUPS_PRINTER_CLASS
;
5365 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5368 dtype
= CUPS_PRINTER_CLASS
;
5369 dmask
= CUPS_PRINTER_CLASS
;
5372 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
5379 send_ipp_status(con
, IPP_NOT_FOUND
,
5380 _("The printer or class was not found."));
5385 dtype
&= CUPS_PRINTER_CLASS
;
5386 dmask
= CUPS_PRINTER_CLASS
;
5395 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5398 send_http_error(con
, status
);
5402 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5404 send_http_error(con
, status
);
5409 * See if the "which-jobs" attribute have been specified...
5412 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
5413 IPP_TAG_KEYWORD
)) != NULL
&&
5414 !strcmp(attr
->values
[0].string
.text
, "completed"))
5419 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
5431 * See if they want to limit the number of jobs reported...
5434 if ((attr
= ippFindAttribute(con
->request
, "limit",
5435 IPP_TAG_INTEGER
)) != NULL
)
5436 limit
= attr
->values
[0].integer
;
5440 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
5441 IPP_TAG_INTEGER
)) != NULL
)
5442 first_job_id
= attr
->values
[0].integer
;
5447 * See if we only want to see jobs for a specific user...
5450 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
5451 IPP_TAG_BOOLEAN
)) != NULL
&&
5452 attr
->values
[0].boolean
)
5453 strlcpy(username
, get_username(con
), sizeof(username
));
5457 ra
= create_requested_array(con
->request
);
5460 * OK, build a list of jobs for this printer...
5463 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
5464 count
< limit
&& job
;
5465 job
= (cupsd_job_t
*)cupsArrayNext(list
))
5468 * Filter out jobs that don't match...
5471 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
5473 if ((dest
&& strcmp(job
->dest
, dest
)) &&
5474 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
5476 if ((job
->dtype
& dmask
) != dtype
&&
5477 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
5479 if (username
[0] && strcasecmp(username
, job
->username
))
5482 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
5485 if (job
->id
< first_job_id
)
5494 ippAddSeparator(con
->response
);
5498 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
5500 copy_job_attrs(con
, job
, ra
);
5503 cupsArrayDelete(ra
);
5505 con
->response
->request
.status
.status_code
= IPP_OK
;
5510 * 'get_notifications()' - Get events for a subscription.
5514 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
5516 int i
, j
; /* Looping vars */
5517 http_status_t status
; /* Policy status */
5518 cupsd_subscription_t
*sub
; /* Subscription */
5519 ipp_attribute_t
*ids
, /* notify-subscription-ids */
5520 *sequences
; /* notify-sequence-numbers */
5521 int min_seq
; /* Minimum sequence number */
5522 int interval
; /* Poll interval */
5525 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_subscription_attrs(con=%p[%d])",
5529 * Get subscription attributes...
5532 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
5534 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
5539 send_ipp_status(con
, IPP_BAD_REQUEST
,
5540 _("Missing notify-subscription-ids attribute!"));
5545 * Are the subscription IDs valid?
5548 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
5550 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
5553 * Bad subscription ID...
5556 send_ipp_status(con
, IPP_NOT_FOUND
,
5557 _("notify-subscription-id %d no good!"),
5558 ids
->values
[i
].integer
);
5566 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5568 con
, sub
->owner
)) != HTTP_OK
)
5570 send_http_error(con
, status
);
5575 * Check the subscription type and update the interval accordingly.
5578 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
5581 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
5583 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
5589 * Tell the client to poll again in N seconds...
5593 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5594 "notify-get-interval", interval
);
5596 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5597 "printer-up-time", time(NULL
));
5600 * Copy the subscription event attributes to the response.
5603 con
->response
->request
.status
.status_code
=
5604 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
5606 for (i
= 0; i
< ids
->num_values
; i
++)
5609 * Get the subscription and sequence number...
5612 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
5614 if (sequences
&& i
< sequences
->num_values
)
5615 min_seq
= sequences
->values
[i
].integer
;
5620 * If we don't have any new events, nothing to do here...
5623 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
5627 * Otherwise copy all of the new events...
5630 if (sub
->first_event_id
> min_seq
)
5633 j
= min_seq
- sub
->first_event_id
;
5635 for (; j
< sub
->num_events
; j
++)
5637 ippAddSeparator(con
->response
);
5639 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
5640 IPP_TAG_EVENT_NOTIFICATION
, 0);
5647 * 'get_ppds()' - Get the list of PPD files on the local system.
5651 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
5653 http_status_t status
; /* Policy status */
5654 ipp_attribute_t
*limit
, /* Limit attribute */
5655 *make
, /* ppd-make attribute */
5656 *requested
; /* requested-attributes attribute */
5657 char command
[1024], /* cups-deviced command */
5658 options
[1024], /* Options to pass to command */
5660 /* String for requested attributes */
5661 make_str
[256]; /* Escaped ppd-make string */
5664 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
5670 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5672 send_http_error(con
, status
);
5677 * Run cups-driverd command with the given options...
5680 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5681 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
5682 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5686 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5688 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5691 url_encode_attr(make
, make_str
, sizeof(make_str
));
5695 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
5696 snprintf(options
, sizeof(options
), "list+%d+%d+%s%s%s",
5697 con
->request
->request
.op
.request_id
,
5698 limit
? limit
->values
[0].integer
: 0,
5699 requested_str
, make
? "%20" : "", make_str
);
5701 if (cupsdSendCommand(con
, command
, options
, 0))
5704 * Command started successfully, don't send an IPP response here...
5707 ippDelete(con
->response
);
5708 con
->response
= NULL
;
5713 * Command failed, return "internal error" so the user knows something
5717 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5718 _("cups-driverd failed to execute."));
5724 * 'get_printer_attrs()' - Get printer attributes.
5728 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
5729 ipp_attribute_t
*uri
) /* I - Printer URI */
5731 http_status_t status
; /* Policy status */
5732 cups_ptype_t dtype
; /* Destination type (printer or class) */
5733 cupsd_printer_t
*printer
; /* Printer/class */
5734 cups_array_t
*ra
; /* Requested attributes array */
5737 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
5738 con
->http
.fd
, uri
->values
[0].string
.text
);
5741 * Is the destination valid?
5744 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5750 send_ipp_status(con
, IPP_NOT_FOUND
,
5751 _("The printer or class was not found."));
5759 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
5761 send_http_error(con
, status
);
5766 * Send the attributes...
5769 ra
= create_requested_array(con
->request
);
5771 copy_printer_attrs(con
, printer
, ra
);
5773 cupsArrayDelete(ra
);
5775 con
->response
->request
.status
.status_code
= IPP_OK
;
5780 * 'get_printers()' - Get a list of printers or classes.
5784 get_printers(cupsd_client_t
*con
, /* I - Client connection */
5785 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
5787 http_status_t status
; /* Policy status */
5788 ipp_attribute_t
*attr
; /* Current attribute */
5789 int limit
; /* Maximum number of printers to return */
5790 int count
; /* Number of printers that match */
5791 cupsd_printer_t
*printer
; /* Current printer pointer */
5792 int printer_type
, /* printer-type attribute */
5793 printer_mask
; /* printer-type-mask attribute */
5794 char *location
; /* Location string */
5795 const char *username
; /* Current user */
5796 char *first_printer_name
; /* first-printer-name attribute */
5797 cups_array_t
*ra
; /* Requested attributes array */
5800 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
5801 con
->http
.fd
, type
);
5807 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5809 send_http_error(con
, status
);
5814 * Check for printers...
5817 if (!Printers
|| !cupsArrayCount(Printers
))
5819 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
5824 * See if they want to limit the number of printers reported...
5827 if ((attr
= ippFindAttribute(con
->request
, "limit",
5828 IPP_TAG_INTEGER
)) != NULL
)
5829 limit
= attr
->values
[0].integer
;
5833 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
5834 IPP_TAG_NAME
)) != NULL
)
5835 first_printer_name
= attr
->values
[0].string
.text
;
5837 first_printer_name
= NULL
;
5840 * Support filtering...
5843 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
5844 IPP_TAG_ENUM
)) != NULL
)
5845 printer_type
= attr
->values
[0].integer
;
5849 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
5850 IPP_TAG_ENUM
)) != NULL
)
5851 printer_mask
= attr
->values
[0].integer
;
5855 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
5856 IPP_TAG_TEXT
)) != NULL
)
5857 location
= attr
->values
[0].string
.text
;
5861 if (con
->username
[0])
5862 username
= con
->username
;
5863 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
5864 IPP_TAG_NAME
)) != NULL
)
5865 username
= attr
->values
[0].string
.text
;
5869 ra
= create_requested_array(con
->request
);
5872 * OK, build a list of printers for this printer...
5875 if (first_printer_name
)
5877 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
5878 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5881 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5884 count
< limit
&& printer
;
5885 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
5887 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
5888 (printer
->type
& printer_mask
) == printer_type
&&
5889 (!location
|| !printer
->location
||
5890 !strcasecmp(printer
->location
, location
)))
5893 * If HideImplicitMembers is enabled, see if this printer or class
5894 * is a member of an implicit class...
5897 if (ImplicitClasses
&& HideImplicitMembers
&&
5898 printer
->in_implicit_class
)
5902 * If a username is specified, see if it is allowed or denied
5906 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
5910 * Add the group separator as needed...
5914 ippAddSeparator(con
->response
);
5919 * Send the attributes...
5922 copy_printer_attrs(con
, printer
, ra
);
5926 cupsArrayDelete(ra
);
5928 con
->response
->request
.status
.status_code
= IPP_OK
;
5933 * 'get_subscription_attrs()' - Get subscription attributes.
5937 get_subscription_attrs(
5938 cupsd_client_t
*con
, /* I - Client connection */
5939 int sub_id
) /* I - Subscription ID */
5941 http_status_t status
; /* Policy status */
5942 cupsd_subscription_t
*sub
; /* Subscription */
5943 cups_array_t
*ra
; /* Requested attributes array */
5946 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5947 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
5948 con
, con
->http
.fd
, sub_id
);
5951 * Is the subscription ID valid?
5954 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
5957 * Bad subscription ID...
5960 send_ipp_status(con
, IPP_NOT_FOUND
,
5961 _("notify-subscription-id %d no good!"), sub_id
);
5969 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5971 con
, sub
->owner
)) != HTTP_OK
)
5973 send_http_error(con
, status
);
5978 * Copy the subscription attributes to the response using the
5979 * requested-attributes attribute that may be provided by the client.
5982 ra
= create_requested_array(con
->request
);
5984 copy_subscription_attrs(con
, sub
, ra
);
5986 cupsArrayDelete(ra
);
5988 con
->response
->request
.status
.status_code
= IPP_OK
;
5993 * 'get_subscriptions()' - Get subscriptions.
5997 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
5998 ipp_attribute_t
*uri
) /* I - Printer/job URI */
6000 http_status_t status
; /* Policy status */
6001 int count
; /* Number of subscriptions */
6002 int limit
; /* Limit */
6003 cupsd_subscription_t
*sub
; /* Subscription */
6004 cups_array_t
*ra
; /* Requested attributes array */
6005 ipp_attribute_t
*attr
; /* Attribute */
6006 cups_ptype_t dtype
; /* Destination type (printer or class) */
6007 char scheme
[HTTP_MAX_URI
],
6008 /* Scheme portion of URI */
6009 username
[HTTP_MAX_URI
],
6010 /* Username portion of URI */
6012 /* Host portion of URI */
6013 resource
[HTTP_MAX_URI
];
6014 /* Resource portion of URI */
6015 int port
; /* Port portion of URI */
6016 cupsd_job_t
*job
; /* Job pointer */
6017 cupsd_printer_t
*printer
; /* Printer */
6020 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6021 "get_subscriptions(con=%p[%d], uri=%s)",
6022 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
6025 * Is the destination valid?
6028 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6029 sizeof(scheme
), username
, sizeof(username
), host
,
6030 sizeof(host
), &port
, resource
, sizeof(resource
));
6032 if (!strcmp(resource
, "/") ||
6033 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
6034 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
6035 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
6040 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
6043 job
= cupsdFindJob(atoi(resource
+ 6));
6047 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
6052 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6058 send_ipp_status(con
, IPP_NOT_FOUND
,
6059 _("The printer or class was not found."));
6062 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
6063 IPP_TAG_INTEGER
)) != NULL
)
6065 job
= cupsdFindJob(attr
->values
[0].integer
);
6069 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
6070 attr
->values
[0].integer
);
6081 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
6083 con
, NULL
)) != HTTP_OK
)
6085 send_http_error(con
, status
);
6090 * Copy the subscription attributes to the response using the
6091 * requested-attributes attribute that may be provided by the client.
6094 ra
= create_requested_array(con
->request
);
6096 if ((attr
= ippFindAttribute(con
->request
, "limit",
6097 IPP_TAG_INTEGER
)) != NULL
)
6098 limit
= attr
->values
[0].integer
;
6103 * See if we only want to see subscriptions for a specific user...
6106 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
6107 IPP_TAG_BOOLEAN
)) != NULL
&&
6108 attr
->values
[0].boolean
)
6109 strlcpy(username
, get_username(con
), sizeof(username
));
6113 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
6115 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
6116 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
6117 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
6119 ippAddSeparator(con
->response
);
6120 copy_subscription_attrs(con
, sub
, ra
);
6123 if (limit
&& count
>= limit
)
6127 cupsArrayDelete(ra
);
6130 con
->response
->request
.status
.status_code
= IPP_OK
;
6132 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
6137 * 'get_username()' - Get the username associated with a request.
6140 static const char * /* O - Username */
6141 get_username(cupsd_client_t
*con
) /* I - Connection */
6143 ipp_attribute_t
*attr
; /* Attribute */
6146 if (con
->username
[0])
6147 return (con
->username
);
6148 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6149 IPP_TAG_NAME
)) != NULL
)
6150 return (attr
->values
[0].string
.text
);
6152 return ("anonymous");
6157 * 'hold_job()' - Hold a print job.
6161 hold_job(cupsd_client_t
*con
, /* I - Client connection */
6162 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
6164 ipp_attribute_t
*attr
, /* Current job-hold-until */
6165 *newattr
; /* New job-hold-until */
6166 int jobid
; /* Job ID */
6167 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6168 username
[HTTP_MAX_URI
], /* Username portion of URI */
6169 host
[HTTP_MAX_URI
], /* Host portion of URI */
6170 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6171 int port
; /* Port portion of URI */
6172 cupsd_job_t
*job
; /* Job information */
6175 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
6176 uri
->values
[0].string
.text
);
6179 * See if we have a job URI or a printer URI...
6182 if (!strcmp(uri
->name
, "printer-uri"))
6185 * Got a printer URI; see if we also have a job-id attribute...
6188 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6189 IPP_TAG_INTEGER
)) == NULL
)
6191 send_ipp_status(con
, IPP_BAD_REQUEST
,
6192 _("Got a printer-uri attribute but no job-id!"));
6196 jobid
= attr
->values
[0].integer
;
6201 * Got a job URI; parse it to get the job ID...
6204 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6205 sizeof(method
), username
, sizeof(username
), host
,
6206 sizeof(host
), &port
, resource
, sizeof(resource
));
6208 if (strncmp(resource
, "/jobs/", 6))
6214 send_ipp_status(con
, IPP_BAD_REQUEST
,
6215 _("Bad job-uri attribute \"%s\"!"),
6216 uri
->values
[0].string
.text
);
6220 jobid
= atoi(resource
+ 6);
6224 * See if the job exists...
6227 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6230 * Nope - return a "not found" error...
6233 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6238 * See if the job is owned by the requesting user...
6241 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6243 send_http_error(con
, HTTP_UNAUTHORIZED
);
6248 * Hold the job and return...
6253 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
6254 "Job held by user.");
6256 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
6257 IPP_TAG_KEYWORD
)) == NULL
)
6258 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
6260 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
6261 IPP_TAG_KEYWORD
)) == NULL
)
6262 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
6267 * Free the old hold value and copy the new one over...
6270 _cupsStrFree(attr
->values
[0].string
.text
);
6274 attr
->value_tag
= newattr
->value_tag
;
6275 attr
->values
[0].string
.text
=
6276 _cupsStrAlloc(newattr
->values
[0].string
.text
);
6280 attr
->value_tag
= IPP_TAG_KEYWORD
;
6281 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
6285 * Hold job until specified time...
6288 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
6290 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
6291 "Job job-hold-until value changed by user.");
6294 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was held by \"%s\".", jobid
,
6297 con
->response
->request
.status
.status_code
= IPP_OK
;
6302 * 'move_job()' - Move a job to a new destination.
6306 move_job(cupsd_client_t
*con
, /* I - Client connection */
6307 ipp_attribute_t
*uri
) /* I - Job URI */
6309 http_status_t status
; /* Policy status */
6310 ipp_attribute_t
*attr
; /* Current attribute */
6311 int jobid
; /* Job ID */
6312 cupsd_job_t
*job
; /* Current job */
6313 const char *src
; /* Source printer/class */
6314 cups_ptype_t stype
, /* Source type (printer or class) */
6315 dtype
; /* Destination type (printer or class) */
6316 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6317 username
[HTTP_MAX_URI
], /* Username portion of URI */
6318 host
[HTTP_MAX_URI
], /* Host portion of URI */
6319 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6320 int port
; /* Port portion of URI */
6321 cupsd_printer_t
*sprinter
, /* Source printer */
6322 *dprinter
; /* Destination printer */
6325 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
6326 uri
->values
[0].string
.text
);
6329 * Get the new printer or class...
6332 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
6333 IPP_TAG_URI
)) == NULL
)
6336 * Need job-printer-uri...
6339 send_ipp_status(con
, IPP_BAD_REQUEST
,
6340 _("job-printer-uri attribute missing!"));
6344 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
6350 send_ipp_status(con
, IPP_NOT_FOUND
,
6351 _("The printer or class was not found."));
6359 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
6362 send_http_error(con
, status
);
6367 * See if we have a job URI or a printer URI...
6370 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6371 sizeof(scheme
), username
, sizeof(username
), host
,
6372 sizeof(host
), &port
, resource
, sizeof(resource
));
6374 if (!strcmp(uri
->name
, "printer-uri"))
6377 * Got a printer URI; see if we also have a job-id attribute...
6380 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6381 IPP_TAG_INTEGER
)) == NULL
)
6387 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
6388 &sprinter
)) == NULL
)
6394 send_ipp_status(con
, IPP_NOT_FOUND
,
6395 _("The printer or class was not found."));
6404 * Otherwise, just move a single job...
6407 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
6410 * Nope - return a "not found" error...
6413 send_ipp_status(con
, IPP_NOT_FOUND
,
6414 _("Job #%d does not exist!"), attr
->values
[0].integer
);
6420 * Job found, initialize source pointers...
6431 * Got a job URI; parse it to get the job ID...
6434 if (strncmp(resource
, "/jobs/", 6))
6440 send_ipp_status(con
, IPP_BAD_REQUEST
,
6441 _("Bad job-uri attribute \"%s\"!"),
6442 uri
->values
[0].string
.text
);
6447 * See if the job exists...
6450 jobid
= atoi(resource
+ 6);
6452 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6455 * Nope - return a "not found" error...
6458 send_ipp_status(con
, IPP_NOT_FOUND
,
6459 _("Job #%d does not exist!"), jobid
);
6465 * Job found, initialize source pointers...
6474 * Now move the job or jobs...
6480 * See if the job has been completed...
6483 if (job
->state_value
> IPP_JOB_STOPPED
)
6486 * Return a "not-possible" error...
6489 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6490 _("Job #%d is finished and cannot be altered!"),
6496 * See if the job is owned by the requesting user...
6499 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6501 send_http_error(con
, HTTP_UNAUTHORIZED
);
6506 * Move the job to a different printer or class...
6509 cupsdMoveJob(job
, dprinter
);
6514 * Got the source printer, now look through the jobs...
6517 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
6519 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
6522 * See if the job is pointing at the source printer or has not been
6526 if (strcasecmp(job
->dest
, src
) ||
6527 job
->state_value
> IPP_JOB_STOPPED
)
6531 * See if the job can be moved by the requesting user...
6534 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6538 * Move the job to a different printer or class...
6541 cupsdMoveJob(job
, dprinter
);
6546 * Start jobs if possible...
6552 * Return with "everything is OK" status...
6555 con
->response
->request
.status
.status_code
= IPP_OK
;
6560 * 'ppd_parse_line()' - Parse a PPD default line.
6563 static int /* O - 0 on success, -1 on failure */
6564 ppd_parse_line(const char *line
, /* I - Line */
6565 char *option
, /* O - Option name */
6566 int olen
, /* I - Size of option name */
6567 char *choice
, /* O - Choice name */
6568 int clen
) /* I - Size of choice name */
6571 * Verify this is a default option line...
6574 if (strncmp(line
, "*Default", 8))
6578 * Read the option name...
6581 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
6591 * Skip everything else up to the colon (:)...
6594 while (*line
&& *line
!= ':')
6603 * Now grab the option choice, skipping leading whitespace...
6606 while (isspace(*line
& 255))
6609 for (clen
--; isalnum(*line
& 255); line
++)
6619 * Return with no errors...
6627 * 'print_job()' - Print a file to a printer or class.
6631 print_job(cupsd_client_t
*con
, /* I - Client connection */
6632 ipp_attribute_t
*uri
) /* I - Printer URI */
6634 ipp_attribute_t
*attr
; /* Current attribute */
6635 ipp_attribute_t
*format
; /* Document-format attribute */
6636 const char *default_format
; /* document-format-default value */
6637 cupsd_job_t
*job
; /* New job */
6638 char filename
[1024]; /* Job filename */
6639 mime_type_t
*filetype
; /* Type of file */
6640 char super
[MIME_MAX_SUPER
], /* Supertype of file */
6641 type
[MIME_MAX_TYPE
], /* Subtype of file */
6642 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
6643 /* Textual name of mime type */
6644 cupsd_printer_t
*printer
; /* Printer data */
6645 struct stat fileinfo
; /* File information */
6646 int kbytes
; /* Size of file */
6647 int compression
; /* Document compression */
6650 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
6651 uri
->values
[0].string
.text
);
6654 * Validate print file attributes, for now just document-format and
6655 * compression (CUPS only supports "none" and "gzip")...
6658 compression
= CUPS_FILE_NONE
;
6660 if ((attr
= ippFindAttribute(con
->request
, "compression",
6661 IPP_TAG_KEYWORD
)) != NULL
)
6663 if (strcmp(attr
->values
[0].string
.text
, "none")
6665 && strcmp(attr
->values
[0].string
.text
, "gzip")
6666 #endif /* HAVE_LIBZ */
6669 send_ipp_status(con
, IPP_ATTRIBUTES
,
6670 _("Unsupported compression \"%s\"!"),
6671 attr
->values
[0].string
.text
);
6672 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6673 "compression", NULL
, attr
->values
[0].string
.text
);
6678 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
6679 compression
= CUPS_FILE_GZIP
;
6680 #endif /* HAVE_LIBZ */
6684 * Do we have a file to print?
6689 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
6694 * Is the destination valid?
6697 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
6703 send_ipp_status(con
, IPP_NOT_FOUND
,
6704 _("The printer or class was not found."));
6709 * Is it a format we support?
6712 if ((format
= ippFindAttribute(con
->request
, "document-format",
6713 IPP_TAG_MIMETYPE
)) != NULL
)
6716 * Grab format from client...
6719 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
6722 send_ipp_status(con
, IPP_BAD_REQUEST
,
6723 _("Could not scan type \"%s\"!"),
6724 format
->values
[0].string
.text
);
6728 else if ((default_format
= cupsGetOption("document-format",
6729 printer
->num_options
,
6730 printer
->options
)) != NULL
)
6733 * Use default document format...
6736 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
6738 send_ipp_status(con
, IPP_BAD_REQUEST
,
6739 _("Could not scan type \"%s\"!"),
6750 strcpy(super
, "application");
6751 strcpy(type
, "octet-stream");
6754 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
6757 * Auto-type the file...
6760 ipp_attribute_t
*doc_name
; /* document-name attribute */
6763 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
6765 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
6766 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
6767 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
6771 filetype
= mimeType(MimeDatabase
, super
, type
);
6774 filetype
= mimeType(MimeDatabase
, super
, type
);
6778 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
6781 * Replace the document-format attribute value with the auto-typed or
6785 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
6790 _cupsStrFree(format
->values
[0].string
.text
);
6792 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
6795 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
6796 "document-format", NULL
, mimetype
);
6800 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
6801 _("Unsupported format \'%s/%s\'!"), super
, type
);
6802 cupsdLogMessage(CUPSD_LOG_INFO
,
6803 "Hint: Do you have the raw file printing rules enabled?");
6806 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
6807 "document-format", NULL
, format
->values
[0].string
.text
);
6812 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: request file type is %s/%s.",
6813 filetype
->super
, filetype
->type
);
6816 * Read any embedded job ticket info from PS files...
6819 if (!strcasecmp(filetype
->super
, "application") &&
6820 !strcasecmp(filetype
->type
, "postscript"))
6821 read_ps_job_ticket(con
);
6824 * Create the job object...
6827 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
6831 * Update quota data...
6834 if (stat(con
->filename
, &fileinfo
))
6837 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
6839 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6841 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
6842 IPP_TAG_INTEGER
)) != NULL
)
6843 attr
->values
[0].integer
+= kbytes
;
6846 * Add the job file...
6849 if (add_file(con
, job
, filetype
, compression
))
6852 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
6854 rename(con
->filename
, filename
);
6855 cupsdClearString(&con
->filename
);
6858 * See if we need to add the ending sheet...
6861 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
6863 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
6864 attr
&& attr
->num_values
> 1)
6870 cupsdLogMessage(CUPSD_LOG_INFO
, "Adding end banner page \"%s\" to job %d.",
6871 attr
->values
[1].string
.text
, job
->id
);
6873 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
6875 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6879 * Log and save the job...
6882 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d queued on \"%s\" by \"%s\".", job
->id
,
6883 job
->dest
, job
->username
);
6884 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Job %d hold_until = %d", job
->id
,
6885 (int)job
->hold_until
);
6890 * Start the job if possible...
6898 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
6900 * This function only gets called when printing a single PostScript
6901 * file using the Print-Job operation. It doesn't work for Create-Job +
6902 * Send-File, since the job attributes need to be set at job creation
6903 * time for banners to work. The embedded PS job ticket stuff is here
6904 * only to allow the Windows printer driver for CUPS to pass in JCL
6905 * options and IPP attributes which otherwise would be lost.
6907 * The format of a PS job ticket is simple:
6909 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
6911 * %cupsJobTicket: attr1=value1
6912 * %cupsJobTicket: attr2=value2
6914 * %cupsJobTicket: attrN=valueN
6916 * Job ticket lines must appear immediately after the first line that
6917 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
6918 * looking for job ticket info when it finds a line that does not begin
6919 * with "%cupsJobTicket:".
6921 * The maximum length of a job ticket line, including the prefix, is
6922 * 255 characters to conform with the Adobe DSC.
6924 * Read-only attributes are rejected with a notice to the error log in
6925 * case a malicious user tries anything. Since the job ticket is read
6926 * prior to attribute validation in print_job(), job ticket attributes
6927 * will go through the same validation as IPP attributes...
6931 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
6933 cups_file_t
*fp
; /* File to read from */
6934 char line
[256]; /* Line data */
6935 int num_options
; /* Number of options */
6936 cups_option_t
*options
; /* Options */
6937 ipp_t
*ticket
; /* New attributes */
6938 ipp_attribute_t
*attr
, /* Current attribute */
6939 *attr2
, /* Job attribute */
6940 *prev2
; /* Previous job attribute */
6944 * First open the print file...
6947 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
6949 cupsdLogMessage(CUPSD_LOG_ERROR
,
6950 "read_ps_job_ticket: Unable to open PostScript print file "
6957 * Skip the first line...
6960 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
6962 cupsdLogMessage(CUPSD_LOG_ERROR
,
6963 "read_ps_job_ticket: Unable to read from PostScript print "
6970 if (strncmp(line
, "%!PS-Adobe-", 11))
6973 * Not a DSC-compliant file, so no job ticket info will be available...
6981 * Read job ticket info from the file...
6987 while (cupsFileGets(fp
, line
, sizeof(line
)))
6990 * Stop at the first non-ticket line...
6993 if (strncmp(line
, "%cupsJobTicket:", 15))
6997 * Add the options to the option array...
7000 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
7004 * Done with the file; see if we have any options...
7009 if (num_options
== 0)
7013 * OK, convert the options to an attribute list, and apply them to
7018 cupsEncodeOptions(ticket
, num_options
, options
);
7021 * See what the user wants to change.
7024 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
7026 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
7029 if (!strcmp(attr
->name
, "job-originating-host-name") ||
7030 !strcmp(attr
->name
, "job-originating-user-name") ||
7031 !strcmp(attr
->name
, "job-media-sheets-completed") ||
7032 !strcmp(attr
->name
, "job-k-octets") ||
7033 !strcmp(attr
->name
, "job-id") ||
7034 !strncmp(attr
->name
, "job-state", 9) ||
7035 !strncmp(attr
->name
, "time-at-", 8))
7036 continue; /* Read-only attrs */
7038 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
7039 IPP_TAG_ZERO
)) != NULL
)
7042 * Some other value; first free the old value...
7045 if (con
->request
->attrs
== attr2
)
7047 con
->request
->attrs
= attr2
->next
;
7052 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
7053 if (prev2
->next
== attr2
)
7055 prev2
->next
= attr2
->next
;
7060 if (con
->request
->last
== attr2
)
7061 con
->request
->last
= prev2
;
7063 _ippFreeAttr(attr2
);
7067 * Add new option by copying it...
7070 copy_attribute(con
->request
, attr
, 0);
7074 * Then free the attribute list and option array...
7078 cupsFreeOptions(num_options
, options
);
7083 * 'reject_jobs()' - Reject print jobs to a printer.
7087 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
7088 ipp_attribute_t
*uri
) /* I - Printer or class URI */
7090 http_status_t status
; /* Policy status */
7091 cups_ptype_t dtype
; /* Destination type (printer or class) */
7092 cupsd_printer_t
*printer
; /* Printer data */
7093 ipp_attribute_t
*attr
; /* printer-state-message text */
7096 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
7097 con
->http
.fd
, uri
->values
[0].string
.text
);
7100 * Is the destination valid?
7103 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7109 send_ipp_status(con
, IPP_NOT_FOUND
,
7110 _("The printer or class was not found."));
7118 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7120 send_http_error(con
, status
);
7125 * Reject jobs sent to the printer...
7128 printer
->accepting
= 0;
7130 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
7131 IPP_TAG_TEXT
)) == NULL
)
7132 strcpy(printer
->state_message
, "Rejecting Jobs");
7134 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
7135 sizeof(printer
->state_message
));
7137 cupsdAddPrinterHistory(printer
);
7139 if (dtype
& CUPS_PRINTER_CLASS
)
7141 cupsdSaveAllClasses();
7143 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
7144 printer
->name
, get_username(con
));
7148 cupsdSaveAllPrinters();
7150 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
7151 printer
->name
, get_username(con
));
7155 * Everything was ok, so return OK status...
7158 con
->response
->request
.status
.status_code
= IPP_OK
;
7163 * 'release_job()' - Release a held print job.
7167 release_job(cupsd_client_t
*con
, /* I - Client connection */
7168 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7170 ipp_attribute_t
*attr
; /* Current attribute */
7171 int jobid
; /* Job ID */
7172 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7173 username
[HTTP_MAX_URI
], /* Username portion of URI */
7174 host
[HTTP_MAX_URI
], /* Host portion of URI */
7175 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7176 int port
; /* Port portion of URI */
7177 cupsd_job_t
*job
; /* Job information */
7180 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
7181 con
->http
.fd
, uri
->values
[0].string
.text
);
7184 * See if we have a job URI or a printer URI...
7187 if (!strcmp(uri
->name
, "printer-uri"))
7190 * Got a printer URI; see if we also have a job-id attribute...
7193 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7194 IPP_TAG_INTEGER
)) == NULL
)
7196 send_ipp_status(con
, IPP_BAD_REQUEST
,
7197 _("Got a printer-uri attribute but no job-id!"));
7201 jobid
= attr
->values
[0].integer
;
7206 * Got a job URI; parse it to get the job ID...
7209 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7210 sizeof(method
), username
, sizeof(username
), host
,
7211 sizeof(host
), &port
, resource
, sizeof(resource
));
7213 if (strncmp(resource
, "/jobs/", 6))
7219 send_ipp_status(con
, IPP_BAD_REQUEST
,
7220 _("Bad job-uri attribute \"%s\"!"),
7221 uri
->values
[0].string
.text
);
7225 jobid
= atoi(resource
+ 6);
7229 * See if the job exists...
7232 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7235 * Nope - return a "not found" error...
7238 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7243 * See if job is "held"...
7246 if (job
->state_value
!= IPP_JOB_HELD
)
7249 * Nope - return a "not possible" error...
7252 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
7257 * See if the job is owned by the requesting user...
7260 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7262 send_http_error(con
, HTTP_UNAUTHORIZED
);
7267 * Reset the job-hold-until value to "no-hold"...
7270 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7271 IPP_TAG_KEYWORD
)) == NULL
)
7272 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7276 _cupsStrFree(attr
->values
[0].string
.text
);
7278 attr
->value_tag
= IPP_TAG_KEYWORD
;
7279 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
7281 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
7282 "Job job-hold-until value changed by user.");
7286 * Release the job and return...
7289 cupsdReleaseJob(job
);
7291 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
7292 "Job released by user.");
7294 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was released by \"%s\".", jobid
,
7297 con
->response
->request
.status
.status_code
= IPP_OK
;
7302 * 'renew_subscription()' - Renew an existing subscription...
7307 cupsd_client_t
*con
, /* I - Client connection */
7308 int sub_id
) /* I - Subscription ID */
7310 http_status_t status
; /* Policy status */
7311 cupsd_subscription_t
*sub
; /* Subscription */
7312 ipp_attribute_t
*lease
; /* notify-lease-duration */
7315 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7316 "renew_subscription(con=%p[%d], sub_id=%d)",
7317 con
, con
->http
.fd
, sub_id
);
7320 * Is the subscription ID valid?
7323 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7326 * Bad subscription ID...
7329 send_ipp_status(con
, IPP_NOT_FOUND
,
7330 _("notify-subscription-id %d no good!"), sub_id
);
7337 * Job subscriptions cannot be renewed...
7340 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7341 _("Job subscriptions cannot be renewed!"));
7349 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7351 con
, sub
->owner
)) != HTTP_OK
)
7353 send_http_error(con
, status
);
7358 * Renew the subscription...
7361 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
7364 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
7366 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
7368 cupsdLogMessage(CUPSD_LOG_INFO
,
7369 "renew_subscription: Limiting notify-lease-duration to "
7372 sub
->lease
= MaxLeaseDuration
;
7375 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
7377 cupsdSaveAllSubscriptions();
7379 con
->response
->request
.status
.status_code
= IPP_OK
;
7384 * 'restart_job()' - Restart an old print job.
7388 restart_job(cupsd_client_t
*con
, /* I - Client connection */
7389 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7391 ipp_attribute_t
*attr
; /* Current attribute */
7392 int jobid
; /* Job ID */
7393 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7394 username
[HTTP_MAX_URI
], /* Username portion of URI */
7395 host
[HTTP_MAX_URI
], /* Host portion of URI */
7396 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7397 int port
; /* Port portion of URI */
7398 cupsd_job_t
*job
; /* Job information */
7401 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
7402 con
->http
.fd
, uri
->values
[0].string
.text
);
7405 * See if we have a job URI or a printer URI...
7408 if (!strcmp(uri
->name
, "printer-uri"))
7411 * Got a printer URI; see if we also have a job-id attribute...
7414 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7415 IPP_TAG_INTEGER
)) == NULL
)
7417 send_ipp_status(con
, IPP_BAD_REQUEST
,
7418 _("Got a printer-uri attribute but no job-id!"));
7422 jobid
= attr
->values
[0].integer
;
7427 * Got a job URI; parse it to get the job ID...
7430 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7431 sizeof(method
), username
, sizeof(username
), host
,
7432 sizeof(host
), &port
, resource
, sizeof(resource
));
7434 if (strncmp(resource
, "/jobs/", 6))
7440 send_ipp_status(con
, IPP_BAD_REQUEST
,
7441 _("Bad job-uri attribute \"%s\"!"),
7442 uri
->values
[0].string
.text
);
7446 jobid
= atoi(resource
+ 6);
7450 * See if the job exists...
7453 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7456 * Nope - return a "not found" error...
7459 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7464 * See if job is in any of the "completed" states...
7467 if (job
->state_value
<= IPP_JOB_PROCESSING
)
7470 * Nope - return a "not possible" error...
7473 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
7479 * See if we have retained the job files...
7484 if (!job
->attrs
|| job
->num_files
== 0)
7487 * Nope - return a "not possible" error...
7490 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7491 _("Job #%d cannot be restarted - no files!"), jobid
);
7496 * See if the job is owned by the requesting user...
7499 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7501 send_http_error(con
, HTTP_UNAUTHORIZED
);
7506 * Restart the job and return...
7509 cupsdRestartJob(job
);
7511 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was restarted by \"%s\".", jobid
,
7514 con
->response
->request
.status
.status_code
= IPP_OK
;
7519 * 'save_auth_info()' - Save authentication information for a job.
7523 save_auth_info(cupsd_client_t
*con
, /* I - Client connection */
7524 cupsd_job_t
*job
) /* I - Job */
7526 int i
; /* Looping var */
7527 char filename
[1024]; /* Job authentication filename */
7528 cups_file_t
*fp
; /* Job authentication file */
7529 char line
[2048]; /* Line for file */
7533 * This function saves the in-memory authentication information for
7534 * a job so that it can be used to authenticate with a remote host.
7535 * The information is stored in a file that is readable only by the
7536 * root user. The username and password are Base-64 encoded, each
7537 * on a separate line, followed by random number (up to 1024) of
7538 * newlines to limit the amount of information that is exposed.
7540 * Because of the potential for exposing of authentication information,
7541 * this functionality is only enabled when running cupsd as root.
7543 * This caching only works for the Basic and BasicDigest authentication
7544 * types. Digest authentication cannot be cached this way, and in
7545 * the future Kerberos authentication may make all of this obsolete.
7547 * Authentication information is saved whenever an authenticated
7548 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
7551 * This information is deleted after a job is completed or canceled,
7552 * so reprints may require subsequent re-authentication.
7559 * Create the authentication file and change permissions...
7562 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
7563 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
7565 cupsdLogMessage(CUPSD_LOG_ERROR
,
7566 "Unable to save authentication info to \"%s\" - %s",
7567 filename
, strerror(errno
));
7571 fchown(cupsFileNumber(fp
), 0, 0);
7572 fchmod(cupsFileNumber(fp
), 0400);
7575 * Write the authenticated username...
7578 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
7579 cupsFilePrintf(fp
, "%s\n", line
);
7582 * Write the authenticated password...
7585 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
7586 cupsFilePrintf(fp
, "%s\n", line
);
7589 * Write a random number of newlines to the end of the file...
7592 for (i
= (rand() % 1024); i
>= 0; i
--)
7593 cupsFilePutChar(fp
, '\n');
7596 * Close the file and return...
7601 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
7602 save_krb5_creds(con
, job
);
7603 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
7607 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
7609 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
7613 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
7614 cupsd_job_t
*job
) /* I - Job */
7617 krb5_context krb_context
; /* Kerberos context */
7618 krb5_ccache ccache
; /* Credentials cache */
7619 OM_uint32 major_status
, /* Major status code */
7620 minor_status
; /* Minor status code */
7624 * Setup a cached context for the job filters to use...
7627 if (krb5_init_context(&krb_context
))
7629 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize Kerberos context");
7633 # ifdef HAVE_HEIMDAL
7634 if (krb5_cc_gen_new(krb_context
, &krb5_fcc_ops
, &ccache
))
7636 if (krb5_cc_gen_new(krb_context
, &ccache
))
7637 # endif /* HAVE_HEIMDAL */
7639 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create new credentials");
7643 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
7646 if (GSS_ERROR(major_status
))
7648 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
7649 "Unable to import client credentials cache");
7650 krb5_cc_destroy(krb_context
, ccache
);
7654 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
7655 krb5_cc_get_name(krb_context
, ccache
));
7656 krb5_cc_close(krb_context
, ccache
);
7657 # endif /* !__APPLE__ */
7659 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
7663 * 'send_document()' - Send a file to a printer or class.
7667 send_document(cupsd_client_t
*con
, /* I - Client connection */
7668 ipp_attribute_t
*uri
) /* I - Printer URI */
7670 ipp_attribute_t
*attr
; /* Current attribute */
7671 ipp_attribute_t
*format
; /* Document-format attribute */
7672 const char *default_format
;/* document-format-default value */
7673 int jobid
; /* Job ID number */
7674 cupsd_job_t
*job
; /* Current job */
7675 char job_uri
[HTTP_MAX_URI
],
7677 method
[HTTP_MAX_URI
],
7678 /* Method portion of URI */
7679 username
[HTTP_MAX_URI
],
7680 /* Username portion of URI */
7682 /* Host portion of URI */
7683 resource
[HTTP_MAX_URI
];
7684 /* Resource portion of URI */
7685 int port
; /* Port portion of URI */
7686 mime_type_t
*filetype
; /* Type of file */
7687 char super
[MIME_MAX_SUPER
],
7688 /* Supertype of file */
7689 type
[MIME_MAX_TYPE
],
7690 /* Subtype of file */
7691 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7692 /* Textual name of mime type */
7693 char filename
[1024]; /* Job filename */
7694 cupsd_printer_t
*printer
; /* Current printer */
7695 struct stat fileinfo
; /* File information */
7696 int kbytes
; /* Size of file */
7697 int compression
; /* Type of compression */
7700 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
7701 con
->http
.fd
, uri
->values
[0].string
.text
);
7704 * See if we have a job URI or a printer URI...
7707 if (!strcmp(uri
->name
, "printer-uri"))
7710 * Got a printer URI; see if we also have a job-id attribute...
7713 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7714 IPP_TAG_INTEGER
)) == NULL
)
7716 send_ipp_status(con
, IPP_BAD_REQUEST
,
7717 _("Got a printer-uri attribute but no job-id!"));
7721 jobid
= attr
->values
[0].integer
;
7726 * Got a job URI; parse it to get the job ID...
7729 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7730 sizeof(method
), username
, sizeof(username
), host
,
7731 sizeof(host
), &port
, resource
, sizeof(resource
));
7733 if (strncmp(resource
, "/jobs/", 6))
7739 send_ipp_status(con
, IPP_BAD_REQUEST
,
7740 _("Bad job-uri attribute \"%s\"!"),
7741 uri
->values
[0].string
.text
);
7745 jobid
= atoi(resource
+ 6);
7749 * See if the job exists...
7752 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7755 * Nope - return a "not found" error...
7758 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7762 printer
= cupsdFindDest(job
->dest
);
7765 * See if the job is owned by the requesting user...
7768 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7770 send_http_error(con
, HTTP_UNAUTHORIZED
);
7775 * OK, see if the client is sending the document compressed - CUPS
7776 * only supports "none" and "gzip".
7779 compression
= CUPS_FILE_NONE
;
7781 if ((attr
= ippFindAttribute(con
->request
, "compression",
7782 IPP_TAG_KEYWORD
)) != NULL
)
7784 if (strcmp(attr
->values
[0].string
.text
, "none")
7786 && strcmp(attr
->values
[0].string
.text
, "gzip")
7787 #endif /* HAVE_LIBZ */
7790 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
7791 attr
->values
[0].string
.text
);
7792 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7793 "compression", NULL
, attr
->values
[0].string
.text
);
7798 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7799 compression
= CUPS_FILE_GZIP
;
7800 #endif /* HAVE_LIBZ */
7804 * Do we have a file to print?
7809 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7814 * Is it a format we support?
7817 if ((format
= ippFindAttribute(con
->request
, "document-format",
7818 IPP_TAG_MIMETYPE
)) != NULL
)
7821 * Grab format from client...
7824 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
7826 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
7827 format
->values
[0].string
.text
);
7831 else if ((default_format
= cupsGetOption("document-format",
7832 printer
->num_options
,
7833 printer
->options
)) != NULL
)
7836 * Use default document format...
7839 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
7841 send_ipp_status(con
, IPP_BAD_REQUEST
,
7842 _("Could not scan type \"%s\"!"),
7850 * No document format attribute? Auto-type it!
7853 strcpy(super
, "application");
7854 strcpy(type
, "octet-stream");
7857 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7860 * Auto-type the file...
7863 ipp_attribute_t
*doc_name
; /* document-name attribute */
7866 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
7868 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7869 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7870 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7874 filetype
= mimeType(MimeDatabase
, super
, type
);
7877 filetype
= mimeType(MimeDatabase
, super
, type
);
7881 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
7884 * Replace the document-format attribute value with the auto-typed or
7888 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7893 _cupsStrFree(format
->values
[0].string
.text
);
7895 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7898 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7899 "document-format", NULL
, mimetype
);
7903 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7904 _("Unsupported format \'%s/%s\'!"), super
, type
);
7905 cupsdLogMessage(CUPSD_LOG_INFO
,
7906 "Hint: Do you have the raw file printing rules enabled?");
7909 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7910 "document-format", NULL
, format
->values
[0].string
.text
);
7915 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
7917 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7920 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7921 _("Unsupported format \'%s\'!"), mimetype
);
7923 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7924 "document-format", NULL
, mimetype
);
7929 cupsdLogMessage(CUPSD_LOG_DEBUG
,
7930 "send_document: request file type is %s/%s.",
7931 filetype
->super
, filetype
->type
);
7934 * Add the file to the job...
7939 if (add_file(con
, job
, filetype
, compression
))
7942 if (stat(con
->filename
, &fileinfo
))
7945 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7947 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7949 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7950 IPP_TAG_INTEGER
)) != NULL
)
7951 attr
->values
[0].integer
+= kbytes
;
7953 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
7955 rename(con
->filename
, filename
);
7957 cupsdClearString(&con
->filename
);
7959 cupsdLogMessage(CUPSD_LOG_INFO
,
7960 "File of type %s/%s queued in job #%d by \"%s\".",
7961 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
7964 * Start the job if this is the last document...
7967 if ((attr
= ippFindAttribute(con
->request
, "last-document",
7968 IPP_TAG_BOOLEAN
)) != NULL
&&
7969 attr
->values
[0].boolean
)
7972 * See if we need to add the ending sheet...
7976 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
7977 (attr
= ippFindAttribute(job
->attrs
, "job-sheets",
7978 IPP_TAG_ZERO
)) != NULL
&&
7979 attr
->num_values
> 1)
7985 cupsdLogMessage(CUPSD_LOG_INFO
,
7986 "Adding end banner page \"%s\" to job %d.",
7987 attr
->values
[1].string
.text
, job
->id
);
7989 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
7991 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7994 if (job
->state_value
== IPP_JOB_STOPPED
)
7996 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7997 job
->state_value
= IPP_JOB_PENDING
;
7999 else if (job
->state_value
== IPP_JOB_HELD
)
8001 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8002 IPP_TAG_KEYWORD
)) == NULL
)
8003 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8005 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
8007 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
8008 job
->state_value
= IPP_JOB_PENDING
;
8015 * Start the job if possible... Since cupsdCheckJobs() can cancel a
8016 * job if it doesn't print, we need to re-find the job afterwards...
8023 job
= cupsdFindJob(jobid
);
8027 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8028 IPP_TAG_KEYWORD
)) == NULL
)
8029 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8031 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
8033 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
8034 job
->state_value
= IPP_JOB_HELD
;
8035 job
->hold_until
= time(NULL
) + 60;
8041 * Fill in the response info...
8044 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
8047 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
8050 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
8052 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
8053 job
? job
->state_value
: IPP_JOB_CANCELED
);
8054 add_job_state_reasons(con
, job
);
8056 con
->response
->request
.status
.status_code
= IPP_OK
;
8061 * 'send_http_error()' - Send a HTTP error back to the IPP client.
8065 send_http_error(cupsd_client_t
*con
, /* I - Client connection */
8066 http_status_t status
) /* I - HTTP status code */
8068 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
8069 ippOpString(con
->request
->request
.op
.operation_id
),
8070 httpStatus(status
));
8072 cupsdSendError(con
, status
);
8074 ippDelete(con
->response
);
8075 con
->response
= NULL
;
8082 * 'send_ipp_status()' - Send a status back to the IPP client.
8086 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
8087 ipp_status_t status
, /* I - IPP status code */
8088 const char *message
, /* I - Status message */
8089 ...) /* I - Additional args as needed */
8091 va_list ap
; /* Pointer to additional args */
8092 char formatted
[1024]; /* Formatted errror message */
8097 va_start(ap
, message
);
8098 vsnprintf(formatted
, sizeof(formatted
),
8099 _cupsLangString(con
->language
, message
), ap
);
8102 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
8103 ippOpString(con
->request
->request
.op
.operation_id
),
8104 ippErrorString(status
), formatted
);
8107 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
8108 ippOpString(con
->request
->request
.op
.operation_id
),
8109 ippErrorString(status
));
8111 con
->response
->request
.status
.status_code
= status
;
8113 if (ippFindAttribute(con
->response
, "attributes-charset",
8114 IPP_TAG_ZERO
) == NULL
)
8115 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
8116 "attributes-charset", NULL
, DefaultCharset
);
8118 if (ippFindAttribute(con
->response
, "attributes-natural-language",
8119 IPP_TAG_ZERO
) == NULL
)
8120 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
8121 "attributes-natural-language", NULL
, DefaultLanguage
);
8124 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
8125 "status-message", NULL
, formatted
);
8130 * 'set_default()' - Set the default destination...
8134 set_default(cupsd_client_t
*con
, /* I - Client connection */
8135 ipp_attribute_t
*uri
) /* I - Printer URI */
8137 http_status_t status
; /* Policy status */
8138 cups_ptype_t dtype
; /* Destination type (printer or class) */
8139 cupsd_printer_t
*printer
; /* Printer */
8142 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
8143 con
->http
.fd
, uri
->values
[0].string
.text
);
8146 * Is the destination valid?
8149 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8155 send_ipp_status(con
, IPP_NOT_FOUND
,
8156 _("The printer or class was not found."));
8164 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
8166 send_http_error(con
, status
);
8171 * Set it as the default...
8174 DefaultPrinter
= printer
;
8176 cupsdSaveAllPrinters();
8177 cupsdSaveAllClasses();
8179 cupsdWritePrintcap();
8181 cupsdLogMessage(CUPSD_LOG_INFO
,
8182 "Default destination set to \"%s\" by \"%s\".",
8183 printer
->name
, get_username(con
));
8186 * Everything was ok, so return OK status...
8189 con
->response
->request
.status
.status_code
= IPP_OK
;
8194 * 'set_job_attrs()' - Set job attributes.
8198 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
8199 ipp_attribute_t
*uri
) /* I - Job URI */
8201 ipp_attribute_t
*attr
, /* Current attribute */
8202 *attr2
; /* Job attribute */
8203 int jobid
; /* Job ID */
8204 cupsd_job_t
*job
; /* Current job */
8205 char method
[HTTP_MAX_URI
],
8206 /* Method portion of URI */
8207 username
[HTTP_MAX_URI
],
8208 /* Username portion of URI */
8210 /* Host portion of URI */
8211 resource
[HTTP_MAX_URI
];
8212 /* Resource portion of URI */
8213 int port
; /* Port portion of URI */
8214 int event
; /* Events? */
8217 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
8218 con
->http
.fd
, uri
->values
[0].string
.text
);
8221 * Start with "everything is OK" status...
8224 con
->response
->request
.status
.status_code
= IPP_OK
;
8227 * See if we have a job URI or a printer URI...
8230 if (!strcmp(uri
->name
, "printer-uri"))
8233 * Got a printer URI; see if we also have a job-id attribute...
8236 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8237 IPP_TAG_INTEGER
)) == NULL
)
8239 send_ipp_status(con
, IPP_BAD_REQUEST
,
8240 _("Got a printer-uri attribute but no job-id!"));
8244 jobid
= attr
->values
[0].integer
;
8249 * Got a job URI; parse it to get the job ID...
8252 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8253 sizeof(method
), username
, sizeof(username
), host
,
8254 sizeof(host
), &port
, resource
, sizeof(resource
));
8256 if (strncmp(resource
, "/jobs/", 6))
8262 send_ipp_status(con
, IPP_BAD_REQUEST
,
8263 _("Bad job-uri attribute \"%s\"!"),
8264 uri
->values
[0].string
.text
);
8268 jobid
= atoi(resource
+ 6);
8272 * See if the job exists...
8275 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8278 * Nope - return a "not found" error...
8281 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8286 * See if the job has been completed...
8289 if (job
->state_value
> IPP_JOB_STOPPED
)
8292 * Return a "not-possible" error...
8295 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8296 _("Job #%d is finished and cannot be altered!"), jobid
);
8301 * See if the job is owned by the requesting user...
8304 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8306 send_http_error(con
, HTTP_UNAUTHORIZED
);
8311 * See what the user wants to change.
8318 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8320 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8323 if (!strcmp(attr
->name
, "attributes-charset") ||
8324 !strcmp(attr
->name
, "attributes-natural-language") ||
8325 !strcmp(attr
->name
, "document-compression") ||
8326 !strcmp(attr
->name
, "document-format") ||
8327 !strcmp(attr
->name
, "job-detailed-status-messages") ||
8328 !strcmp(attr
->name
, "job-document-access-errors") ||
8329 !strcmp(attr
->name
, "job-id") ||
8330 !strcmp(attr
->name
, "job-k-octets") ||
8331 !strcmp(attr
->name
, "job-originating-host-name") ||
8332 !strcmp(attr
->name
, "job-originating-user-name") ||
8333 !strcmp(attr
->name
, "job-printer-up-time") ||
8334 !strcmp(attr
->name
, "job-printer-uri") ||
8335 !strcmp(attr
->name
, "job-sheets") ||
8336 !strcmp(attr
->name
, "job-state-message") ||
8337 !strcmp(attr
->name
, "job-state-reasons") ||
8338 !strcmp(attr
->name
, "job-uri") ||
8339 !strcmp(attr
->name
, "number-of-documents") ||
8340 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
8341 !strcmp(attr
->name
, "output-device-assigned") ||
8342 !strncmp(attr
->name
, "date-time-at-", 13) ||
8343 !strncmp(attr
->name
, "job-impressions", 15) ||
8344 !strncmp(attr
->name
, "job-k-octets", 12) ||
8345 !strncmp(attr
->name
, "job-media-sheets", 16) ||
8346 !strncmp(attr
->name
, "time-at-", 8))
8352 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
8353 _("%s cannot be changed."), attr
->name
);
8355 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8356 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8361 if (!strcmp(attr
->name
, "job-priority"))
8364 * Change the job priority...
8367 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8369 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
8371 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8372 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8374 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
8376 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8377 _("Job is completed and cannot be changed."));
8380 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8382 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
8383 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
8386 else if (!strcmp(attr
->name
, "job-state"))
8389 * Change the job state...
8392 if (attr
->value_tag
!= IPP_TAG_ENUM
)
8394 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
8396 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8397 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8401 switch (attr
->values
[0].integer
)
8403 case IPP_JOB_PENDING
:
8405 if (job
->state_value
> IPP_JOB_HELD
)
8407 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8408 _("Job state cannot be changed."));
8411 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8413 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8414 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
8416 event
|= CUPSD_EVENT_JOB_STATE
;
8420 case IPP_JOB_PROCESSING
:
8421 case IPP_JOB_STOPPED
:
8422 if (job
->state_value
!= attr
->values
[0].integer
)
8424 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8425 _("Job state cannot be changed."));
8430 case IPP_JOB_CANCELED
:
8431 case IPP_JOB_ABORTED
:
8432 case IPP_JOB_COMPLETED
:
8433 if (job
->state_value
> IPP_JOB_PROCESSING
)
8435 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8436 _("Job state cannot be changed."));
8439 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8440 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
8445 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
8447 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8448 IPP_TAG_ZERO
)) != NULL
)
8451 * Some other value; first free the old value...
8454 if (job
->attrs
->prev
)
8455 job
->attrs
->prev
->next
= attr2
->next
;
8457 job
->attrs
->attrs
= attr2
->next
;
8459 if (job
->attrs
->last
== attr2
)
8460 job
->attrs
->last
= job
->attrs
->prev
;
8462 _ippFreeAttr(attr2
);
8465 * Then copy the attribute...
8468 copy_attribute(job
->attrs
, attr
, 0);
8471 * See if the job-name or job-hold-until is being changed.
8474 if (!strcmp(attr
->name
, "job-hold-until"))
8476 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
8478 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
8479 cupsdReleaseJob(job
);
8483 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
8486 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
8489 * Delete the attribute...
8492 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8493 IPP_TAG_ZERO
)) != NULL
)
8495 if (job
->attrs
->prev
)
8496 job
->attrs
->prev
->next
= attr2
->next
;
8498 job
->attrs
->attrs
= attr2
->next
;
8500 if (attr2
== job
->attrs
->last
)
8501 job
->attrs
->last
= job
->attrs
->prev
;
8503 _ippFreeAttr(attr2
);
8505 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
8511 * Add new option by copying it...
8514 copy_attribute(job
->attrs
, attr
, 0);
8516 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
8527 * Send events as needed...
8530 if (event
& CUPSD_EVENT_JOB_STATE
)
8531 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
8532 job
->state_value
== IPP_JOB_HELD
?
8533 "Job held by user." : "Job restarted by user.");
8535 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
8536 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
8537 "Job options changed by user.");
8540 * Start jobs if possible...
8548 * 'set_printer_defaults()' - Set printer default options from a request.
8552 set_printer_defaults(
8553 cupsd_client_t
*con
, /* I - Client connection */
8554 cupsd_printer_t
*printer
) /* I - Printer */
8556 int i
; /* Looping var */
8557 ipp_attribute_t
*attr
; /* Current attribute */
8558 int namelen
; /* Length of attribute name */
8559 char name
[256], /* New attribute name */
8560 value
[256]; /* String version of integer attrs */
8563 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8566 * Skip non-printer attributes...
8569 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
8572 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
8574 if (!strcmp(attr
->name
, "job-sheets-default"))
8577 * Only allow keywords and names...
8580 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8584 * Only allow job-sheets-default to be set when running without a
8585 * system high classification level...
8591 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
8593 if (attr
->num_values
> 1)
8594 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
8596 cupsdSetString(&printer
->job_sheets
[1], "none");
8598 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
8600 cupsdFreePrinterUsers(printer
);
8602 printer
->deny_users
= 0;
8604 if (attr
->value_tag
== IPP_TAG_NAME
&&
8605 (attr
->num_values
> 1 ||
8606 strcmp(attr
->values
[0].string
.text
, "all")))
8608 for (i
= 0; i
< attr
->num_values
; i
++)
8609 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8612 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
8614 cupsdFreePrinterUsers(printer
);
8616 printer
->deny_users
= 1;
8618 if (attr
->value_tag
== IPP_TAG_NAME
&&
8619 (attr
->num_values
> 1 ||
8620 strcmp(attr
->values
[0].string
.text
, "none")))
8622 for (i
= 0; i
< attr
->num_values
; i
++)
8623 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8626 else if (!strcmp(attr
->name
, "job-quota-period"))
8628 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8631 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
8632 attr
->values
[0].integer
);
8633 cupsdFreeQuotas(printer
);
8635 printer
->quota_period
= attr
->values
[0].integer
;
8637 else if (!strcmp(attr
->name
, "job-k-limit"))
8639 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8642 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
8643 attr
->values
[0].integer
);
8644 cupsdFreeQuotas(printer
);
8646 printer
->k_limit
= attr
->values
[0].integer
;
8648 else if (!strcmp(attr
->name
, "job-page-limit"))
8650 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8653 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
8654 attr
->values
[0].integer
);
8655 cupsdFreeQuotas(printer
);
8657 printer
->page_limit
= attr
->values
[0].integer
;
8659 else if (!strcmp(attr
->name
, "printer-op-policy"))
8661 cupsd_policy_t
*p
; /* Policy */
8664 if (attr
->value_tag
!= IPP_TAG_NAME
)
8667 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
8669 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8670 "Setting printer-op-policy to \"%s\"...",
8671 attr
->values
[0].string
.text
);
8672 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
8673 printer
->op_policy_ptr
= p
;
8677 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8678 _("Unknown printer-op-policy \"%s\"."),
8679 attr
->values
[0].string
.text
);
8683 else if (!strcmp(attr
->name
, "printer-error-policy"))
8685 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8688 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
8689 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
8690 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
8692 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8693 _("Unknown printer-error-policy \"%s\"."),
8694 attr
->values
[0].string
.text
);
8698 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8699 "Setting printer-error-policy to \"%s\"...",
8700 attr
->values
[0].string
.text
);
8701 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
8705 * Skip any other non-default attributes...
8708 namelen
= strlen(attr
->name
);
8709 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
8710 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
8714 * OK, anything else must be a user-defined default...
8717 strlcpy(name
, attr
->name
, sizeof(name
));
8718 name
[namelen
- 8] = '\0'; /* Strip "-default" */
8720 switch (attr
->value_tag
)
8722 case IPP_TAG_DELETEATTR
:
8723 printer
->num_options
= cupsRemoveOption(name
,
8724 printer
->num_options
,
8725 &(printer
->options
));
8726 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8727 "Deleting %s", attr
->name
);
8731 case IPP_TAG_KEYWORD
:
8733 printer
->num_options
= cupsAddOption(name
,
8734 attr
->values
[0].string
.text
,
8735 printer
->num_options
,
8736 &(printer
->options
));
8737 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8738 "Setting %s to \"%s\"...", attr
->name
,
8739 attr
->values
[0].string
.text
);
8742 case IPP_TAG_BOOLEAN
:
8743 printer
->num_options
= cupsAddOption(name
,
8744 attr
->values
[0].boolean
?
8746 printer
->num_options
,
8747 &(printer
->options
));
8748 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8749 "Setting %s to %s...", attr
->name
,
8750 attr
->values
[0].boolean
? "true" : "false");
8753 case IPP_TAG_INTEGER
:
8755 sprintf(value
, "%d", attr
->values
[0].integer
);
8756 printer
->num_options
= cupsAddOption(name
, value
,
8757 printer
->num_options
,
8758 &(printer
->options
));
8759 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8760 "Setting %s to %s...", attr
->name
, value
);
8763 case IPP_TAG_RANGE
:
8764 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
8765 attr
->values
[0].range
.upper
);
8766 printer
->num_options
= cupsAddOption(name
, value
,
8767 printer
->num_options
,
8768 &(printer
->options
));
8769 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8770 "Setting %s to %s...", attr
->name
, value
);
8773 case IPP_TAG_RESOLUTION
:
8774 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
8775 attr
->values
[0].resolution
.yres
,
8776 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
8778 printer
->num_options
= cupsAddOption(name
, value
,
8779 printer
->num_options
,
8780 &(printer
->options
));
8781 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8782 "Setting %s to %s...", attr
->name
, value
);
8786 /* Do nothing for other values */
8794 * 'start_printer()' - Start a printer.
8798 start_printer(cupsd_client_t
*con
, /* I - Client connection */
8799 ipp_attribute_t
*uri
) /* I - Printer URI */
8801 http_status_t status
; /* Policy status */
8802 cups_ptype_t dtype
; /* Destination type (printer or class) */
8803 cupsd_printer_t
*printer
; /* Printer data */
8806 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
8807 con
->http
.fd
, uri
->values
[0].string
.text
);
8810 * Is the destination valid?
8813 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8819 send_ipp_status(con
, IPP_NOT_FOUND
,
8820 _("The printer or class was not found."));
8828 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8830 send_http_error(con
, status
);
8835 * Start the printer...
8838 printer
->state_message
[0] = '\0';
8840 cupsdStartPrinter(printer
, 1);
8842 if (dtype
& CUPS_PRINTER_CLASS
)
8843 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
8844 printer
->name
, get_username(con
));
8846 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
8847 printer
->name
, get_username(con
));
8852 * Everything was ok, so return OK status...
8855 con
->response
->request
.status
.status_code
= IPP_OK
;
8860 * 'stop_printer()' - Stop a printer.
8864 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
8865 ipp_attribute_t
*uri
) /* I - Printer URI */
8867 http_status_t status
; /* Policy status */
8868 cups_ptype_t dtype
; /* Destination type (printer or class) */
8869 cupsd_printer_t
*printer
; /* Printer data */
8870 ipp_attribute_t
*attr
; /* printer-state-message attribute */
8873 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
8874 con
->http
.fd
, uri
->values
[0].string
.text
);
8877 * Is the destination valid?
8880 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8886 send_ipp_status(con
, IPP_NOT_FOUND
,
8887 _("The printer or class was not found."));
8895 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8897 send_http_error(con
, status
);
8902 * Stop the printer...
8905 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8906 IPP_TAG_TEXT
)) == NULL
)
8907 strcpy(printer
->state_message
, "Paused");
8910 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8911 sizeof(printer
->state_message
));
8914 cupsdStopPrinter(printer
, 1);
8916 if (dtype
& CUPS_PRINTER_CLASS
)
8917 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
8918 printer
->name
, get_username(con
));
8920 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
8921 printer
->name
, get_username(con
));
8924 * Everything was ok, so return OK status...
8927 con
->response
->request
.status
.status_code
= IPP_OK
;
8932 * 'url_encode_attr()' - URL-encode a string attribute.
8936 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
8937 char *buffer
,/* I - String buffer */
8938 int bufsize
)/* I - Size of buffer */
8940 int i
; /* Looping var */
8941 char *bufptr
, /* Pointer into buffer */
8942 *bufend
, /* End of buffer */
8943 *valptr
; /* Pointer into value */
8946 strlcpy(buffer
, attr
->name
, bufsize
);
8947 bufptr
= buffer
+ strlen(buffer
);
8948 bufend
= buffer
+ bufsize
- 1;
8950 for (i
= 0; i
< attr
->num_values
; i
++)
8952 if (bufptr
>= bufend
)
8960 if (bufptr
>= bufend
)
8965 for (valptr
= attr
->values
[i
].string
.text
;
8966 *valptr
&& bufptr
< bufend
;
8970 if (bufptr
>= (bufend
- 2))
8977 else if (*valptr
== '\'' || *valptr
== '\\')
8980 *bufptr
++ = *valptr
;
8983 *bufptr
++ = *valptr
;
8985 if (bufptr
>= bufend
)
8996 * 'user_allowed()' - See if a user is allowed to print to a queue.
8999 static int /* O - 0 if not allowed, 1 if allowed */
9000 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
9001 const char *username
) /* I - Username */
9003 int i
; /* Looping var */
9004 struct passwd
*pw
; /* User password data */
9007 if (p
->num_users
== 0)
9010 if (!strcmp(username
, "root"))
9013 pw
= getpwnam(username
);
9016 for (i
= 0; i
< p
->num_users
; i
++)
9018 if (p
->users
[i
][0] == '@')
9021 * Check group membership...
9024 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
9027 else if (!strcasecmp(username
, p
->users
[i
]))
9031 return ((i
< p
->num_users
) != p
->deny_users
);
9036 * 'validate_job()' - Validate printer options and destination.
9040 validate_job(cupsd_client_t
*con
, /* I - Client connection */
9041 ipp_attribute_t
*uri
) /* I - Printer URI */
9043 http_status_t status
; /* Policy status */
9044 ipp_attribute_t
*attr
; /* Current attribute */
9045 ipp_attribute_t
*format
; /* Document-format attribute */
9046 cups_ptype_t dtype
; /* Destination type (printer or class) */
9047 char super
[MIME_MAX_SUPER
],
9048 /* Supertype of file */
9049 type
[MIME_MAX_TYPE
];
9050 /* Subtype of file */
9051 cupsd_printer_t
*printer
; /* Printer */
9054 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
9055 con
->http
.fd
, uri
->values
[0].string
.text
);
9058 * OK, see if the client is sending the document compressed - CUPS
9059 * doesn't support compression yet...
9062 if ((attr
= ippFindAttribute(con
->request
, "compression",
9063 IPP_TAG_KEYWORD
)) != NULL
&&
9064 !strcmp(attr
->values
[0].string
.text
, "none"))
9066 send_ipp_status(con
, IPP_ATTRIBUTES
,
9067 _("Unsupported compression attribute %s!"),
9068 attr
->values
[0].string
.text
);
9069 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9070 "compression", NULL
, attr
->values
[0].string
.text
);
9075 * Is it a format we support?
9078 if ((format
= ippFindAttribute(con
->request
, "document-format",
9079 IPP_TAG_MIMETYPE
)) != NULL
)
9081 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
9083 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9084 format
->values
[0].string
.text
);
9088 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
9089 !mimeType(MimeDatabase
, super
, type
))
9091 cupsdLogMessage(CUPSD_LOG_INFO
,
9092 "Hint: Do you have the raw file printing rules enabled?");
9093 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9094 _("Unsupported format \"%s\"!"),
9095 format
->values
[0].string
.text
);
9096 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9097 "document-format", NULL
, format
->values
[0].string
.text
);
9103 * Is the destination valid?
9106 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9112 send_ipp_status(con
, IPP_NOT_FOUND
,
9113 _("The printer or class was not found."));
9121 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9123 send_http_error(con
, status
);
9128 * Everything was ok, so return OK status...
9131 con
->response
->request
.status
.status_code
= IPP_OK
;
9136 * 'validate_name()' - Make sure the printer name only contains valid chars.
9139 static int /* O - 0 if name is no good, 1 if name is good */
9140 validate_name(const char *name
) /* I - Name to check */
9142 const char *ptr
; /* Pointer into name */
9146 * Scan the whole name...
9149 for (ptr
= name
; *ptr
; ptr
++)
9150 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
9154 * All the characters are good; validate the length, too...
9157 return ((ptr
- name
) < 128);
9162 * 'validate_user()' - Validate the user for the request.
9165 static int /* O - 1 if permitted, 0 otherwise */
9166 validate_user(cupsd_job_t
*job
, /* I - Job */
9167 cupsd_client_t
*con
, /* I - Client connection */
9168 const char *owner
, /* I - Owner of job/resource */
9169 char *username
, /* O - Authenticated username */
9170 int userlen
) /* I - Length of username */
9172 cupsd_printer_t
*printer
; /* Printer for job */
9175 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9176 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
9178 job
? job
->id
: 0, con
->http
.fd
, owner
? owner
: "(null)",
9185 if (!con
|| !owner
|| !username
|| userlen
<= 0)
9189 * Get the best authenticated username that is available.
9192 strlcpy(username
, get_username(con
), userlen
);
9195 * Check the username against the owner...
9198 printer
= cupsdFindDest(job
->dest
);
9200 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
9201 con
, owner
) == HTTP_OK
);