2 * "$Id: ipp.c 5232 2006-03-05 17:59:19Z mike $"
4 * IPP routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdProcessIPPRequest() - Process an incoming IPP request...
27 * accept_jobs() - Accept print jobs to a printer.
28 * add_class() - Add a class to the system.
29 * add_file() - Add a file to a job.
30 * add_job() - Add a job to a print queue.
31 * add_job_state_reasons() - Add the "job-state-reasons" attribute based
32 * upon the job and printer state...
33 * add_job_subscriptions() - Add any subcriptions for a job.
34 * add_job_uuid() - Add job-uuid attribute to a job.
35 * add_printer() - Add a printer to the system.
36 * add_printer_state_reasons() - Add the "printer-state-reasons" attribute
37 * based upon the printer state...
38 * add_queued_job_count() - Add the "queued-job-count" attribute for
39 * apply_printer_defaults() - Apply printer default options to a job.
40 * authenticate_job() - Set job authentication info.
41 * cancel_all_jobs() - Cancel all print jobs.
42 * cancel_job() - Cancel a print job.
43 * cancel_subscription() - Cancel a subscription.
44 * check_quotas() - Check quotas for a printer and user.
45 * copy_attribute() - Copy a single attribute.
46 * copy_attrs() - Copy attributes from one request to another.
47 * copy_banner() - Copy a banner file to the requests directory
48 * for the specified job.
49 * copy_file() - Copy a PPD file or interface script...
50 * copy_model() - Copy a PPD model file, substituting default
52 * copy_job_attrs() - Copy job attributes.
53 * copy_printer_attrs() - Copy printer attributes.
54 * copy_subscription_attrs() - Copy subscription attributes.
55 * create_job() - Print a file to a printer or class.
56 * create_requested_array() - Create an array for the requested-attributes.
57 * create_subscription() - Create a notification subscription.
58 * delete_printer() - Remove a printer or class from the system.
59 * get_default() - Get the default destination.
60 * get_devices() - Get the list of available devices on the
62 * get_job_attrs() - Get job attributes.
63 * get_jobs() - Get a list of jobs for the specified printer.
64 * get_notifications() - Get events for a subscription.
65 * get_ppds() - Get the list of PPD files on the local
67 * get_printer_attrs() - Get printer attributes.
68 * get_printers() - Get a list of printers.
69 * get_subscription_attrs() - Get subscription attributes.
70 * get_subscriptions() - Get subscriptions.
71 * get_username() - Get the username associated with a request.
72 * hold_job() - Hold a print job.
73 * move_job() - Move a job to a new destination.
74 * ppd_add_default() - Add a PPD default choice.
75 * ppd_parse_line() - Parse a PPD default line.
76 * print_job() - Print a file to a printer or class.
77 * read_ps_line() - Read a line from a PS file...
78 * read_ps_job_ticket() - Reads a job ticket embedded in a PS file.
79 * reject_jobs() - Reject print jobs to a printer.
80 * release_job() - Release a held print job.
81 * restart_job() - Restart an old print job.
82 * save_auth_info() - Save authentication information for a job.
83 * send_document() - Send a file to a printer or class.
84 * send_http_error() - Send a HTTP error back to the IPP client.
85 * send_ipp_status() - Send a status back to the IPP client.
86 * set_default() - Set the default destination...
87 * set_job_attrs() - Set job attributes.
88 * set_printer_defaults() - Set printer default options from a request.
89 * start_printer() - Start a printer.
90 * stop_printer() - Stop a printer.
91 * user_allowed() - See if a user is allowed to print to a queue.
92 * validate_job() - Validate printer options and destination.
93 * validate_name() - Make sure the printer name only contains
95 * validate_user() - Validate the user for the request.
99 * Include necessary headers...
106 #endif /* HAVE_LIBPAPER */
110 * PPD default choice structure...
115 char option
[PPD_MAX_NAME
]; /* Main keyword (option name) */
116 char choice
[PPD_MAX_NAME
]; /* Option keyword (choice name) */
124 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
125 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
126 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
127 mime_type_t
*filetype
, int compression
);
128 static cupsd_job_t
*add_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
,
129 cupsd_printer_t
**dprinter
);
130 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
131 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
132 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
133 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
134 static void add_printer_state_reasons(cupsd_client_t
*con
,
136 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
137 static void apply_printer_defaults(cupsd_printer_t
*printer
,
139 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
140 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
141 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
142 static void cancel_subscription(cupsd_client_t
*con
, int id
);
143 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
144 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
146 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
147 ipp_tag_t group
, int quickcopy
);
148 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
150 static int copy_file(const char *from
, const char *to
);
151 static int copy_model(cupsd_client_t
*con
, const char *from
,
153 static void copy_job_attrs(cupsd_client_t
*con
,
156 static void copy_printer_attrs(cupsd_client_t
*con
,
157 cupsd_printer_t
*printer
,
159 static void copy_subscription_attrs(cupsd_client_t
*con
,
160 cupsd_subscription_t
*sub
,
162 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
163 static cups_array_t
*create_requested_array(ipp_t
*request
);
164 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
165 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
166 static void get_default(cupsd_client_t
*con
);
167 static void get_devices(cupsd_client_t
*con
);
168 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
169 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
170 static void get_notifications(cupsd_client_t
*con
);
171 static void get_ppds(cupsd_client_t
*con
);
172 static void get_printers(cupsd_client_t
*con
, int type
);
173 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
174 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
175 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
176 static const char *get_username(cupsd_client_t
*con
);
177 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
178 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
179 static int ppd_add_default(const char *option
, const char *choice
,
180 int num_defaults
, ppd_default_t
**defaults
);
181 static int ppd_parse_line(const char *line
, char *option
, int olen
,
182 char *choice
, int clen
);
183 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
184 static void read_ps_job_ticket(cupsd_client_t
*con
);
185 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
186 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
187 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
188 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
189 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
);
190 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
191 static void send_http_error(cupsd_client_t
*con
, http_status_t status
);
192 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
193 const char *message
, ...)
195 __attribute__ ((__format__ (__printf__
, 3, 4)))
196 # endif /* __GNUC__ */
198 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
199 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
200 static void set_printer_defaults(cupsd_client_t
*con
,
201 cupsd_printer_t
*printer
);
202 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
203 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
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 if (con
->http
.version
== HTTP_1_1
)
628 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
630 httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n");
634 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
635 con
->http
.data_remaining
= ippLength(con
->response
);
637 if (con
->http
.data_remaining
< INT_MAX
)
638 con
->http
._data_remaining
= con
->http
.data_remaining
;
640 con
->http
._data_remaining
= INT_MAX
;
642 httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
643 CUPS_LLCAST con
->http
.data_remaining
);
646 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
647 "cupsdProcessIPPRequest: Adding fd %d to OutputSet...",
650 FD_SET(con
->http
.fd
, OutputSet
);
653 * Tell the caller the response header was sent successfully...
661 * Tell the caller the response header could not be sent...
670 * Sending data from a subprocess like cups-deviced; tell the caller
671 * everything is A-OK so far...
680 * 'accept_jobs()' - Accept print jobs to a printer.
684 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
685 ipp_attribute_t
*uri
) /* I - Printer or class URI */
687 http_status_t status
; /* Policy status */
688 cups_ptype_t dtype
; /* Destination type (printer or class) */
689 char method
[HTTP_MAX_URI
], /* Method portion of URI */
690 username
[HTTP_MAX_URI
], /* Username portion of URI */
691 host
[HTTP_MAX_URI
], /* Host portion of URI */
692 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
693 int port
; /* Port portion of URI */
694 const char *name
; /* Printer name */
695 cupsd_printer_t
*printer
; /* Printer data */
698 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
699 con
->http
.fd
, uri
->values
[0].string
.text
);
702 * Is the destination valid?
705 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
706 sizeof(method
), username
, sizeof(username
), host
,
707 sizeof(host
), &port
, resource
, sizeof(resource
));
709 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
715 send_ipp_status(con
, IPP_NOT_FOUND
,
716 _("The printer or class was not found."));
724 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
726 send_http_error(con
, status
);
731 * Accept jobs sent to the printer...
734 printer
->accepting
= 1;
735 printer
->state_message
[0] = '\0';
737 cupsdAddPrinterHistory(printer
);
739 if (dtype
& CUPS_PRINTER_CLASS
)
740 cupsdSaveAllClasses();
742 cupsdSaveAllPrinters();
744 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" now accepting jobs (\"%s\").", name
,
748 * Everything was ok, so return OK status...
751 con
->response
->request
.status
.status_code
= IPP_OK
;
756 * 'add_class()' - Add a class to the system.
760 add_class(cupsd_client_t
*con
, /* I - Client connection */
761 ipp_attribute_t
*uri
) /* I - URI of class */
763 http_status_t status
; /* Policy status */
764 int i
; /* Looping var */
765 char method
[HTTP_MAX_URI
], /* Method portion of URI */
766 username
[HTTP_MAX_URI
], /* Username portion of URI */
767 host
[HTTP_MAX_URI
], /* Host portion of URI */
768 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
769 int port
; /* Port portion of URI */
770 cupsd_printer_t
*pclass
, /* Class */
771 *member
; /* Member printer/class */
772 cups_ptype_t dtype
; /* Destination type */
773 const char *dest
; /* Printer or class name */
774 ipp_attribute_t
*attr
; /* Printer attribute */
775 int modify
; /* Non-zero if we just modified */
776 char newname
[IPP_MAX_NAME
]; /* New class name */
777 int need_restart_job
; /* Need to restart job? */
780 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
781 con
->http
.fd
, uri
->values
[0].string
.text
);
784 * Do we have a valid URI?
787 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
788 sizeof(method
), username
, sizeof(username
), host
,
789 sizeof(host
), &port
, resource
, sizeof(resource
));
792 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
795 * No, return an error...
798 send_ipp_status(con
, IPP_BAD_REQUEST
,
799 _("The printer-uri must be of the form "
800 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
805 * Do we have a valid printer name?
808 if (!validate_name(resource
+ 9))
811 * No, return an error...
814 send_ipp_status(con
, IPP_BAD_REQUEST
,
815 _("The printer-uri \"%s\" contains invalid characters."),
816 uri
->values
[0].string
.text
);
824 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
826 send_http_error(con
, status
);
831 * See if the class already exists; if not, create a new class...
834 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
837 * Class doesn't exist; see if we have a printer of the same name...
840 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
841 !(pclass
->type
& CUPS_PRINTER_REMOTE
))
844 * Yes, return an error...
847 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
848 _("A printer named \"%s\" already exists!"),
854 * No, add the pclass...
857 pclass
= cupsdAddClass(resource
+ 9);
860 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
863 * Rename the implicit class to "AnyClass" or remove it...
866 if (ImplicitAnyClasses
)
868 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
869 cupsdRenamePrinter(pclass
, newname
);
872 cupsdDeletePrinter(pclass
, 1);
875 * Add the class as a new local class...
878 pclass
= cupsdAddClass(resource
+ 9);
881 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
884 * Rename the remote class to "Class"...
887 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
888 cupsdRenamePrinter(pclass
, newname
);
891 * Add the class as a new local class...
894 pclass
= cupsdAddClass(resource
+ 9);
901 * Look for attributes and copy them over as needed...
904 need_restart_job
= 0;
906 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
907 IPP_TAG_TEXT
)) != NULL
)
908 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
910 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
911 IPP_TAG_TEXT
)) != NULL
)
912 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
914 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
915 IPP_TAG_BOOLEAN
)) != NULL
)
917 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-is-accepting-jobs to %d (was %d.)",
918 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
920 pclass
->accepting
= attr
->values
[0].boolean
;
921 cupsdAddPrinterHistory(pclass
);
924 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
925 IPP_TAG_BOOLEAN
)) != NULL
)
927 if (pclass
->shared
&& !attr
->values
[0].boolean
)
928 cupsdSendBrowseDelete(pclass
);
930 cupsdLogMessage(CUPSD_LOG_INFO
,
931 "Setting %s printer-is-shared to %d (was %d.)",
932 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
934 pclass
->shared
= attr
->values
[0].boolean
;
937 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
938 IPP_TAG_ENUM
)) != NULL
)
940 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
941 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
943 send_ipp_status(con
, IPP_BAD_REQUEST
,
944 _("Attempt to set %s printer-state to bad value %d!"),
945 pclass
->name
, attr
->values
[0].integer
);
949 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)", pclass
->name
,
950 attr
->values
[0].integer
, pclass
->state
);
952 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
953 cupsdStopPrinter(pclass
, 0);
956 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
957 need_restart_job
= 1;
960 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
961 IPP_TAG_TEXT
)) != NULL
)
963 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
964 sizeof(pclass
->state_message
));
965 cupsdAddPrinterHistory(pclass
);
967 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
968 IPP_TAG_URI
)) != NULL
)
971 * Clear the printer array as needed...
974 need_restart_job
= 1;
976 if (pclass
->num_printers
> 0)
978 free(pclass
->printers
);
979 pclass
->num_printers
= 0;
983 * Add each printer or class that is listed...
986 for (i
= 0; i
< attr
->num_values
; i
++)
989 * Search for the printer or class URI...
992 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
, method
,
993 sizeof(method
), username
, sizeof(username
), host
,
994 sizeof(host
), &port
, resource
, sizeof(resource
));
996 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &member
)) == NULL
)
1002 send_ipp_status(con
, IPP_NOT_FOUND
,
1003 _("The printer or class was not found."));
1008 * Add it to the class...
1011 cupsdAddPrinterToClass(pclass
, member
);
1015 set_printer_defaults(con
, pclass
);
1018 * Update the printer class attributes and return...
1021 cupsdSetPrinterAttrs(pclass
);
1022 cupsdSaveAllClasses();
1024 if (need_restart_job
&& pclass
->job
)
1029 * Stop the current job and then restart it below...
1032 job
= (cupsd_job_t
*)pclass
->job
;
1034 cupsdStopJob(job
, 1);
1036 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1037 job
->state_value
= IPP_JOB_PENDING
;
1040 if (need_restart_job
)
1043 cupsdWritePrintcap();
1047 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1048 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1051 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1052 pclass
->name
, get_username(con
));
1056 cupsdAddPrinterHistory(pclass
);
1058 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1059 "New class \"%s\" added by \"%s\".", pclass
->name
,
1062 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1063 pclass
->name
, get_username(con
));
1066 con
->response
->request
.status
.status_code
= IPP_OK
;
1071 * 'add_file()' - Add a file to a job.
1074 static int /* O - 0 on success, -1 on error */
1075 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1076 cupsd_job_t
*job
, /* I - Job to add to */
1077 mime_type_t
*filetype
, /* I - Type of file */
1078 int compression
) /* I - Compression */
1080 mime_type_t
**filetypes
; /* New filetypes array... */
1081 int *compressions
; /* New compressions array... */
1084 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1085 "add_file(con=%p[%d], job=%d, filetype=%s/%s, compression=%d)",
1086 con
, con
->http
.fd
, job
->id
, filetype
->super
, filetype
->type
,
1090 * Add the file to the job...
1093 if (job
->num_files
== 0)
1095 compressions
= (int *)malloc(sizeof(int));
1096 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1100 compressions
= (int *)realloc(job
->compressions
,
1101 (job
->num_files
+ 1) * sizeof(int));
1102 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1103 (job
->num_files
+ 1) *
1104 sizeof(mime_type_t
*));
1107 if (!compressions
|| !filetypes
)
1109 cupsdCancelJob(job
, 1);
1111 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1112 _("Unable to allocate memory for file types!"));
1116 job
->compressions
= compressions
;
1117 job
->compressions
[job
->num_files
] = compression
;
1118 job
->filetypes
= filetypes
;
1119 job
->filetypes
[job
->num_files
] = filetype
;
1128 * 'add_job()' - Add a job to a print queue.
1131 static cupsd_job_t
* /* O - Job object */
1132 add_job(cupsd_client_t
*con
, /* I - Client connection */
1133 ipp_attribute_t
*uri
, /* I - printer-uri */
1134 cupsd_printer_t
**dprinter
) /* I - Destination printer */
1136 http_status_t status
; /* Policy status */
1137 ipp_attribute_t
*attr
; /* Current attribute */
1138 const char *dest
; /* Destination */
1139 cups_ptype_t dtype
; /* Destination type (printer or class) */
1140 const char *val
; /* Default option value */
1141 int priority
; /* Job priority */
1142 char *title
; /* Job name/title */
1143 cupsd_job_t
*job
; /* Current job */
1144 char job_uri
[HTTP_MAX_URI
], /* Job URI */
1145 method
[HTTP_MAX_URI
], /* Method portion of URI */
1146 username
[HTTP_MAX_URI
], /* Username portion of URI */
1147 host
[HTTP_MAX_URI
], /* Host portion of URI */
1148 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1149 int port
; /* Port portion of URI */
1150 cupsd_printer_t
*printer
; /* Printer data */
1151 int kbytes
; /* Size of print file */
1152 int i
; /* Looping var */
1153 int lowerpagerange
; /* Page range bound */
1156 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %s)", con
,
1157 con
->http
.fd
, uri
->values
[0].string
.text
);
1160 * Is the destination valid?
1163 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
1164 sizeof(method
), username
, sizeof(username
), host
,
1165 sizeof(host
), &port
, resource
, sizeof(resource
));
1167 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
1173 send_ipp_status(con
, IPP_NOT_FOUND
,
1174 _("The printer or class was not found."));
1179 *dprinter
= printer
;
1182 * Check remote printing to non-shared printer...
1185 if (!printer
->shared
&&
1186 strcasecmp(con
->http
.hostname
, "localhost") &&
1187 strcasecmp(con
->http
.hostname
, ServerName
))
1189 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1190 _("The printer or class is not shared!"));
1198 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1200 send_http_error(con
, status
);
1203 else if ((printer
->type
& CUPS_PRINTER_AUTHENTICATED
) && !con
->username
[0])
1205 send_http_error(con
, HTTP_UNAUTHORIZED
);
1210 * See if the printer is accepting jobs...
1213 if (!printer
->accepting
)
1215 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1216 _("Destination \"%s\" is not accepting jobs."),
1222 * Validate job template attributes; for now just copies and page-ranges...
1225 if ((attr
= ippFindAttribute(con
->request
, "copies",
1226 IPP_TAG_INTEGER
)) != NULL
)
1228 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1230 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1231 attr
->values
[0].integer
);
1232 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1233 "copies", attr
->values
[0].integer
);
1238 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1239 IPP_TAG_RANGE
)) != NULL
)
1241 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1243 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1244 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1246 send_ipp_status(con
, IPP_BAD_REQUEST
,
1247 _("Bad page-ranges values %d-%d."),
1248 attr
->values
[i
].range
.lower
,
1249 attr
->values
[i
].range
.upper
);
1253 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1258 * Make sure we aren't over our limit...
1261 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1264 if (cupsArrayCount(Jobs
) >= MaxJobs
&& MaxJobs
)
1266 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1267 _("Too many active jobs."));
1271 if (!check_quotas(con
, printer
))
1273 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1278 * Create the job and set things up...
1281 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1282 IPP_TAG_INTEGER
)) != NULL
)
1283 priority
= attr
->values
[0].integer
;
1286 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1287 printer
->options
)) != NULL
)
1288 priority
= atoi(val
);
1292 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1296 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1297 IPP_TAG_NAME
)) != NULL
)
1298 title
= attr
->values
[0].string
.text
;
1300 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1301 title
= "Untitled");
1303 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1305 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1306 _("Unable to add job for destination \"%s\"!"), dest
);
1311 job
->attrs
= con
->request
;
1312 con
->request
= NULL
;
1314 add_job_uuid(con
, job
);
1315 apply_printer_defaults(printer
, job
);
1317 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1319 if (con
->username
[0])
1321 cupsdSetString(&job
->username
, con
->username
);
1324 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1326 save_auth_info(con
, job
);
1330 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1331 "add_job: requesting-user-name=\"%s\"",
1332 attr
->values
[0].string
.text
);
1334 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1337 cupsdSetString(&job
->username
, "anonymous");
1340 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1341 "job-originating-user-name", NULL
, job
->username
);
1344 attr
->group_tag
= IPP_TAG_JOB
;
1345 _cupsStrFree(attr
->name
);
1346 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1349 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1350 IPP_TAG_ZERO
)) != NULL
)
1353 * Request contains a job-originating-host-name attribute; validate it...
1356 if (attr
->value_tag
!= IPP_TAG_NAME
||
1357 attr
->num_values
!= 1 ||
1358 strcmp(con
->http
.hostname
, "localhost"))
1361 * Can't override the value if we aren't connected via localhost.
1362 * Also, we can only have 1 value and it must be a name value.
1365 switch (attr
->value_tag
)
1367 case IPP_TAG_STRING
:
1368 case IPP_TAG_TEXTLANG
:
1369 case IPP_TAG_NAMELANG
:
1372 case IPP_TAG_KEYWORD
:
1374 case IPP_TAG_URISCHEME
:
1375 case IPP_TAG_CHARSET
:
1376 case IPP_TAG_LANGUAGE
:
1377 case IPP_TAG_MIMETYPE
:
1379 * Free old strings...
1382 for (i
= 0; i
< attr
->num_values
; i
++)
1384 _cupsStrFree(attr
->values
[i
].string
.text
);
1385 attr
->values
[i
].string
.text
= NULL
;
1386 if (attr
->values
[i
].string
.charset
)
1388 _cupsStrFree(attr
->values
[i
].string
.charset
);
1389 attr
->values
[i
].string
.charset
= NULL
;
1398 * Use the default connection hostname instead...
1401 attr
->value_tag
= IPP_TAG_NAME
;
1402 attr
->num_values
= 1;
1403 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1406 attr
->group_tag
= IPP_TAG_JOB
;
1411 * No job-originating-host-name attribute, so use the hostname from
1415 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1416 "job-originating-host-name", NULL
, con
->http
.hostname
);
1419 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1421 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1422 "time-at-processing", 0);
1423 attr
->value_tag
= IPP_TAG_NOVALUE
;
1424 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1425 "time-at-completed", 0);
1426 attr
->value_tag
= IPP_TAG_NOVALUE
;
1429 * Add remaining job attributes...
1432 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1433 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1434 "job-state", IPP_JOB_STOPPED
);
1435 job
->state_value
= job
->state
->values
[0].integer
;
1436 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1437 "job-media-sheets-completed", 0);
1438 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1440 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1443 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1444 IPP_TAG_INTEGER
)) != NULL
)
1445 attr
->values
[0].integer
= 0;
1447 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1450 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1451 IPP_TAG_KEYWORD
)) == NULL
)
1452 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1455 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1456 printer
->options
)) == NULL
)
1459 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1460 "job-hold-until", NULL
, val
);
1462 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1463 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1466 * Hold job until specified time...
1469 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1471 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1473 job
->hold_until
= time(NULL
) + 60;
1474 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1475 job
->state_value
= IPP_JOB_HELD
;
1479 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1480 job
->state_value
= IPP_JOB_PENDING
;
1483 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1487 * Add job sheets options...
1490 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1491 IPP_TAG_ZERO
)) == NULL
)
1493 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1494 "Adding default job-sheets values \"%s,%s\"...",
1495 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1497 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1499 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1500 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1503 job
->job_sheets
= attr
;
1506 * Enforce classification level if set...
1511 cupsdLogMessage(CUPSD_LOG_INFO
,
1512 "Classification=\"%s\", ClassifyOverride=%d",
1513 Classification
? Classification
: "(null)",
1516 if (ClassifyOverride
)
1518 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1519 (attr
->num_values
== 1 ||
1520 !strcmp(attr
->values
[1].string
.text
, "none")))
1523 * Force the leading banner to have the classification on it...
1526 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1528 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1529 "job-sheets=\"%s,none\", "
1530 "job-originating-user-name=\"%s\"",
1531 job
->id
, Classification
, job
->username
);
1533 else if (attr
->num_values
== 2 &&
1534 strcmp(attr
->values
[0].string
.text
,
1535 attr
->values
[1].string
.text
) &&
1536 strcmp(attr
->values
[0].string
.text
, "none") &&
1537 strcmp(attr
->values
[1].string
.text
, "none"))
1540 * Can't put two different security markings on the same document!
1543 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1545 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1546 "job-sheets=\"%s,%s\", "
1547 "job-originating-user-name=\"%s\"",
1548 job
->id
, attr
->values
[0].string
.text
,
1549 attr
->values
[1].string
.text
, job
->username
);
1551 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1552 strcmp(attr
->values
[0].string
.text
, "none") &&
1553 (attr
->num_values
== 1 ||
1554 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1555 strcmp(attr
->values
[1].string
.text
, "none"))))
1557 if (attr
->num_values
== 1)
1558 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1559 "[Job %d] CLASSIFICATION OVERRIDDEN "
1560 "job-sheets=\"%s\", "
1561 "job-originating-user-name=\"%s\"",
1562 job
->id
, attr
->values
[0].string
.text
, job
->username
);
1564 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1565 "[Job %d] CLASSIFICATION OVERRIDDEN "
1566 "job-sheets=\"%s,%s\",fffff "
1567 "job-originating-user-name=\"%s\"",
1568 job
->id
, attr
->values
[0].string
.text
,
1569 attr
->values
[1].string
.text
, job
->username
);
1572 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1573 (attr
->num_values
== 1 ||
1574 strcmp(attr
->values
[1].string
.text
, Classification
)))
1577 * Force the banner to have the classification on it...
1580 if (attr
->num_values
> 1 &&
1581 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1583 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1584 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1588 if (attr
->num_values
== 1 ||
1589 strcmp(attr
->values
[0].string
.text
, "none"))
1590 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1592 if (attr
->num_values
> 1 &&
1593 strcmp(attr
->values
[1].string
.text
, "none"))
1594 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1597 if (attr
->num_values
> 1)
1598 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1599 "[Job %d] CLASSIFICATION FORCED "
1600 "job-sheets=\"%s,%s\", "
1601 "job-originating-user-name=\"%s\"",
1602 job
->id
, attr
->values
[0].string
.text
,
1603 attr
->values
[1].string
.text
, job
->username
);
1605 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1606 "[Job %d] CLASSIFICATION FORCED "
1607 "job-sheets=\"%s\", "
1608 "job-originating-user-name=\"%s\"",
1609 job
->id
, Classification
, job
->username
);
1614 * See if we need to add the starting sheet...
1617 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1619 cupsdLogMessage(CUPSD_LOG_INFO
,
1620 "Adding start banner page \"%s\" to job %d.",
1621 attr
->values
[0].string
.text
, job
->id
);
1623 kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
);
1625 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1628 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1629 IPP_TAG_ZERO
)) != NULL
)
1633 * Fill in the response info...
1636 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1637 LocalPort
, job
->id
);
1639 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1642 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1644 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1646 add_job_state_reasons(con
, job
);
1648 con
->response
->request
.status
.status_code
= IPP_OK
;
1651 * Add any job subscriptions...
1654 add_job_subscriptions(con
, job
);
1657 * Set all but the first two attributes to the job attributes group...
1660 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1661 attr
->group_tag
= IPP_TAG_JOB
;
1664 * Fire the "job created" event...
1667 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1670 * Return the new job...
1678 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1679 * upon the job and printer state...
1683 add_job_state_reasons(
1684 cupsd_client_t
*con
, /* I - Client connection */
1685 cupsd_job_t
*job
) /* I - Job info */
1687 cupsd_printer_t
*dest
; /* Destination printer */
1690 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1691 con
, con
->http
.fd
, job
? job
->id
: 0);
1693 switch (job
? job
->state_value
: IPP_JOB_CANCELLED
)
1695 case IPP_JOB_PENDING
:
1696 if (job
->dtype
& CUPS_PRINTER_CLASS
)
1697 dest
= cupsdFindClass(job
->dest
);
1699 dest
= cupsdFindPrinter(job
->dest
);
1701 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1702 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1703 "job-state-reasons", NULL
, "printer-stopped");
1705 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1706 "job-state-reasons", NULL
, "none");
1710 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1711 IPP_TAG_KEYWORD
) != NULL
||
1712 ippFindAttribute(job
->attrs
, "job-hold-until",
1713 IPP_TAG_NAME
) != NULL
)
1714 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1715 "job-state-reasons", NULL
, "job-hold-until-specified");
1717 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1718 "job-state-reasons", NULL
, "job-incoming");
1721 case IPP_JOB_PROCESSING
:
1722 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1723 "job-state-reasons", NULL
, "job-printing");
1726 case IPP_JOB_STOPPED
:
1727 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1728 "job-state-reasons", NULL
, "job-stopped");
1731 case IPP_JOB_CANCELLED
:
1732 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1733 "job-state-reasons", NULL
, "job-canceled-by-user");
1736 case IPP_JOB_ABORTED
:
1737 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1738 "job-state-reasons", NULL
, "aborted-by-system");
1741 case IPP_JOB_COMPLETED
:
1742 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1743 "job-state-reasons", NULL
, "job-completed-successfully");
1750 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1754 add_job_subscriptions(
1755 cupsd_client_t
*con
, /* I - Client connection */
1756 cupsd_job_t
*job
) /* I - Newly created job */
1758 int i
; /* Looping var */
1759 ipp_attribute_t
*prev
, /* Previous attribute */
1760 *next
, /* Next attribute */
1761 *attr
; /* Current attribute */
1762 cupsd_subscription_t
*sub
; /* Subscription object */
1763 const char *recipient
, /* notify-recipient-uri */
1764 *pullmethod
; /* notify-pull-method */
1765 ipp_attribute_t
*user_data
; /* notify-user-data */
1766 int interval
; /* notify-time-interval */
1767 unsigned mask
; /* notify-events */
1771 * Find the first subscription group attribute; return if we have
1775 for (attr
= job
->attrs
->attrs
, prev
= NULL
;
1777 prev
= attr
, attr
= attr
->next
)
1778 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1785 * Process the subscription attributes in the request...
1794 mask
= CUPSD_EVENT_NONE
;
1796 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1798 if (!strcmp(attr
->name
, "notify-recipient") &&
1799 attr
->value_tag
== IPP_TAG_URI
)
1800 recipient
= attr
->values
[0].string
.text
;
1801 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1802 attr
->value_tag
== IPP_TAG_KEYWORD
)
1803 pullmethod
= attr
->values
[0].string
.text
;
1804 else if (!strcmp(attr
->name
, "notify-charset") &&
1805 attr
->value_tag
== IPP_TAG_CHARSET
&&
1806 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
1807 strcmp(attr
->values
[0].string
.text
, "utf-8"))
1809 send_ipp_status(con
, IPP_CHARSET
,
1810 _("Character set \"%s\" not supported!"),
1811 attr
->values
[0].string
.text
);
1814 else if (!strcmp(attr
->name
, "notify-natural-language") &&
1815 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
1816 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
1818 send_ipp_status(con
, IPP_CHARSET
,
1819 _("Language \"%s\" not supported!"),
1820 attr
->values
[0].string
.text
);
1823 else if (!strcmp(attr
->name
, "notify-user-data") &&
1824 attr
->value_tag
== IPP_TAG_STRING
)
1826 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
1828 send_ipp_status(con
, IPP_REQUEST_VALUE
,
1829 _("The notify-user-data value is too large "
1830 "(%d > 63 octets)!"),
1831 attr
->values
[0].unknown
.length
);
1837 else if (!strcmp(attr
->name
, "notify-events") &&
1838 attr
->value_tag
== IPP_TAG_KEYWORD
)
1840 for (i
= 0; i
< attr
->num_values
; i
++)
1841 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
1843 else if (!strcmp(attr
->name
, "notify-lease-duration"))
1845 send_ipp_status(con
, IPP_BAD_REQUEST
,
1846 _("The notify-lease-duration attribute cannot be "
1847 "used with job subscriptions."));
1850 else if (!strcmp(attr
->name
, "notify-time-interval") &&
1851 attr
->value_tag
== IPP_TAG_INTEGER
)
1852 interval
= attr
->values
[0].integer
;
1857 if (!recipient
&& !pullmethod
)
1860 if (mask
== CUPSD_EVENT_NONE
)
1861 mask
= CUPSD_EVENT_JOB_COMPLETED
;
1863 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
1866 sub
->interval
= interval
;
1868 cupsdSetString(&sub
->owner
, job
->username
);
1872 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
1873 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
1874 sub
->user_data_len
);
1877 ippAddSeparator(con
->response
);
1878 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
1879 "notify-subscription-id", sub
->id
);
1885 cupsdSaveAllSubscriptions();
1888 * Remove all of the subscription attributes from the job request...
1891 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
1895 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
1896 attr
->group_tag
== IPP_TAG_ZERO
)
1899 * Free and remove this attribute...
1907 job
->attrs
->attrs
= next
;
1913 job
->attrs
->last
= prev
;
1914 job
->attrs
->current
= prev
;
1919 * 'add_job_uuid()' - Add job-uuid attribute to a job.
1921 * See RFC 4122 for the definition of UUIDs and the format.
1925 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
1926 cupsd_job_t
*job
) /* I - Job */
1928 char uuid
[1024]; /* job-uuid string */
1929 ipp_attribute_t
*attr
; /* job-uuid attribute */
1930 _cups_md5_state_t md5state
; /* MD5 state */
1931 unsigned char md5sum
[16]; /* MD5 digest/sum */
1935 * First see if the job already has a job-uuid attribute; if so, return...
1938 if ((attr
= ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
)) != NULL
)
1942 * No job-uuid attribute, so build a version 3 UUID with the local job
1943 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
1944 * the ServerName, server name and port that the client connected to,
1945 * and local job ID...
1948 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
1949 con
->serverport
, job
->id
);
1951 _cupsMD5Init(&md5state
);
1952 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
1953 _cupsMD5Finish(&md5state
, md5sum
);
1956 * Format the UUID URI using the MD5 sum and job ID.
1959 snprintf(uuid
, sizeof(uuid
),
1960 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
1961 "%02x%02x%02x%02x%02x%02x",
1962 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
1963 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
1964 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
1965 md5sum
[14], md5sum
[15]);
1967 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
1972 * 'add_printer()' - Add a printer to the system.
1976 add_printer(cupsd_client_t
*con
, /* I - Client connection */
1977 ipp_attribute_t
*uri
) /* I - URI of printer */
1979 http_status_t status
; /* Policy status */
1980 int i
; /* Looping var */
1981 char method
[HTTP_MAX_URI
], /* Method portion of URI */
1982 username
[HTTP_MAX_URI
], /* Username portion of URI */
1983 host
[HTTP_MAX_URI
], /* Host portion of URI */
1984 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1985 int port
; /* Port portion of URI */
1986 cupsd_printer_t
*printer
; /* Printer/class */
1987 ipp_attribute_t
*attr
; /* Printer attribute */
1988 cups_file_t
*fp
; /* Script/PPD file */
1989 char line
[1024]; /* Line from file... */
1990 char srcfile
[1024], /* Source Script/PPD file */
1991 dstfile
[1024]; /* Destination Script/PPD file */
1992 int modify
; /* Non-zero if we are modifying */
1993 char newname
[IPP_MAX_NAME
]; /* New printer name */
1994 int need_restart_job
; /* Need to restart job? */
1997 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
1998 con
->http
.fd
, uri
->values
[0].string
.text
);
2001 * Do we have a valid URI?
2004 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2005 sizeof(method
), username
, sizeof(username
), host
,
2006 sizeof(host
), &port
, resource
, sizeof(resource
));
2008 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2011 * No, return an error...
2014 send_ipp_status(con
, IPP_BAD_REQUEST
,
2015 _("The printer-uri must be of the form "
2016 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2021 * Do we have a valid printer name?
2024 if (!validate_name(resource
+ 10))
2027 * No, return an error...
2030 send_ipp_status(con
, IPP_BAD_REQUEST
,
2031 _("The printer-uri \"%s\" contains invalid characters."),
2032 uri
->values
[0].string
.text
);
2040 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2042 send_http_error(con
, status
);
2047 * See if the printer already exists; if not, create a new printer...
2050 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2053 * Printer doesn't exist; see if we have a class of the same name...
2056 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2057 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2060 * Yes, return an error...
2063 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2064 _("A class named \"%s\" already exists!"),
2070 * No, add the printer...
2073 printer
= cupsdAddPrinter(resource
+ 10);
2076 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2079 * Rename the implicit printer to "AnyPrinter" or delete it...
2082 if (ImplicitAnyClasses
)
2084 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2085 cupsdRenamePrinter(printer
, newname
);
2088 cupsdDeletePrinter(printer
, 1);
2091 * Add the printer as a new local printer...
2094 printer
= cupsdAddPrinter(resource
+ 10);
2097 else if (printer
->type
& CUPS_PRINTER_REMOTE
)
2100 * Rename the remote printer to "Printer@server"...
2103 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2105 cupsdRenamePrinter(printer
, newname
);
2108 * Add the printer as a new local printer...
2111 printer
= cupsdAddPrinter(resource
+ 10);
2118 * Look for attributes and copy them over as needed...
2121 need_restart_job
= 0;
2123 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2124 IPP_TAG_TEXT
)) != NULL
)
2125 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2127 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2128 IPP_TAG_TEXT
)) != NULL
)
2129 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2131 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2132 IPP_TAG_URI
)) != NULL
)
2135 * Do we have a valid device URI?
2138 need_restart_job
= 1;
2140 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, method
,
2141 sizeof(method
), username
, sizeof(username
), host
,
2142 sizeof(host
), &port
, resource
, sizeof(resource
));
2144 if (!strcmp(method
, "file"))
2147 * See if the administrator has enabled file devices...
2150 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2153 * File devices are disabled and the URL is not file:/dev/null...
2156 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2157 _("File device URIs have been disabled! "
2158 "To enable, see the FileDevice directive in "
2159 "\"%s/cupsd.conf\"."),
2167 * See if the backend exists and is executable...
2170 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, method
);
2171 if (access(srcfile
, X_OK
))
2174 * Could not find device in list!
2177 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2178 attr
->values
[0].string
.text
);
2183 cupsdLogMessage(CUPSD_LOG_INFO
,
2184 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2186 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2188 cupsdSanitizeURI(printer
->device_uri
, resource
,
2191 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2194 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2195 IPP_TAG_KEYWORD
)) != NULL
)
2197 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2200 need_restart_job
= 1;
2202 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2204 for (i
= 0; i
< supported
->num_values
; i
++)
2205 if (!strcmp(supported
->values
[i
].string
.text
,
2206 attr
->values
[0].string
.text
))
2209 if (i
>= supported
->num_values
)
2211 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2212 attr
->values
[0].string
.text
);
2216 cupsdLogMessage(CUPSD_LOG_INFO
,
2217 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2218 printer
->name
, attr
->values
[0].string
.text
,
2219 printer
->port_monitor
);
2221 if (strcmp(attr
->values
[0].string
.text
, "none"))
2222 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2224 cupsdClearString(&printer
->port_monitor
);
2227 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2228 IPP_TAG_BOOLEAN
)) != NULL
)
2230 cupsdLogMessage(CUPSD_LOG_INFO
,
2231 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2232 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2234 printer
->accepting
= attr
->values
[0].boolean
;
2235 cupsdAddPrinterHistory(printer
);
2238 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2239 IPP_TAG_BOOLEAN
)) != NULL
)
2241 if (printer
->shared
&& !attr
->values
[0].boolean
)
2242 cupsdSendBrowseDelete(printer
);
2244 cupsdLogMessage(CUPSD_LOG_INFO
,
2245 "Setting %s printer-is-shared to %d (was %d.)",
2246 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2248 printer
->shared
= attr
->values
[0].boolean
;
2251 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2252 IPP_TAG_ENUM
)) != NULL
)
2254 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2255 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2257 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2258 attr
->values
[0].integer
);
2262 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)", printer
->name
,
2263 attr
->values
[0].integer
, printer
->state
);
2265 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2266 cupsdStopPrinter(printer
, 0);
2269 need_restart_job
= 1;
2270 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2273 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2274 IPP_TAG_TEXT
)) != NULL
)
2276 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2277 sizeof(printer
->state_message
));
2278 cupsdAddPrinterHistory(printer
);
2281 set_printer_defaults(con
, printer
);
2284 * See if we have all required attributes...
2287 if (!printer
->device_uri
)
2288 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2291 * See if we have an interface script or PPD file attached to the request...
2296 need_restart_job
= 1;
2298 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2300 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2303 * Yes; get the first line from it...
2307 cupsFileGets(fp
, line
, sizeof(line
));
2311 * Then see what kind of file it is...
2314 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2317 if (!strncmp(line
, "*PPD-Adobe", 10))
2320 * The new file is a PPD file, so remove any old interface script
2321 * that might be lying around...
2329 * This must be an interface script, so move the file over to the
2330 * interfaces directory and make it executable...
2333 if (copy_file(srcfile
, dstfile
))
2335 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2336 _("Unable to copy interface script - %s!"),
2342 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2343 "Copied interface script successfully!");
2344 chmod(dstfile
, 0755);
2348 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2351 if (!strncmp(line
, "*PPD-Adobe", 10))
2354 * The new file is a PPD file, so move the file over to the
2355 * ppd directory and make it readable by all...
2358 if (copy_file(srcfile
, dstfile
))
2360 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2361 _("Unable to copy PPD file - %s!"),
2367 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2368 "Copied PPD file successfully!");
2369 chmod(dstfile
, 0644);
2375 * This must be an interface script, so remove any old PPD file that
2376 * may be lying around...
2383 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2384 IPP_TAG_NAME
)) != NULL
)
2386 need_restart_job
= 1;
2388 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2391 * Raw driver, remove any existing PPD or interface script files.
2394 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2398 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2408 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2412 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2415 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2417 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2422 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2423 "Copied PPD file successfully!");
2424 chmod(dstfile
, 0644);
2430 * Update the printer attributes and return...
2433 cupsdSetPrinterAttrs(printer
);
2434 cupsdSaveAllPrinters();
2436 if (need_restart_job
&& printer
->job
)
2441 * Stop the current job and then restart it below...
2444 job
= (cupsd_job_t
*)printer
->job
;
2446 cupsdStopJob(job
, 1);
2448 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2449 job
->state_value
= IPP_JOB_PENDING
;
2452 if (need_restart_job
)
2455 cupsdWritePrintcap();
2459 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2460 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2463 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2464 printer
->name
, get_username(con
));
2468 cupsdAddPrinterHistory(printer
);
2470 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2471 "New printer \"%s\" added by \"%s\".", printer
->name
,
2474 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2475 printer
->name
, get_username(con
));
2478 con
->response
->request
.status
.status_code
= IPP_OK
;
2483 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2484 * based upon the printer state...
2488 add_printer_state_reasons(
2489 cupsd_client_t
*con
, /* I - Client connection */
2490 cupsd_printer_t
*p
) /* I - Printer info */
2492 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2493 "add_printer_state_reasons(%p[%d], %p[%s])",
2494 con
, con
->http
.fd
, p
, p
->name
);
2496 if (p
->num_reasons
== 0)
2497 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2498 "printer-state-reasons", NULL
,
2499 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2501 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2502 "printer-state-reasons", p
->num_reasons
, NULL
,
2503 (const char * const *)p
->reasons
);
2508 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2509 * the specified printer or class.
2513 add_queued_job_count(
2514 cupsd_client_t
*con
, /* I - Client connection */
2515 cupsd_printer_t
*p
) /* I - Printer or class */
2517 int count
; /* Number of jobs on destination */
2520 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2521 con
, con
->http
.fd
, p
, p
->name
);
2523 count
= cupsdGetPrinterJobCount(p
->name
);
2525 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2526 "queued-job-count", count
);
2531 * 'apply_printer_defaults()' - Apply printer default options to a job.
2535 apply_printer_defaults(
2536 cupsd_printer_t
*printer
, /* I - Printer */
2537 cupsd_job_t
*job
) /* I - Job */
2539 int i
, /* Looping var */
2540 num_options
; /* Number of default options */
2541 cups_option_t
*options
, /* Default options */
2542 *option
; /* Current option */
2546 * Collect all of the default options and add the missing ones to the
2550 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
2553 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2555 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2560 * Encode these options as attributes in the job object...
2563 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2564 cupsFreeOptions(num_options
, options
);
2569 * 'authenticate_job()' - Set job authentication info.
2573 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2574 ipp_attribute_t
*uri
) /* I - Job URI */
2576 ipp_attribute_t
*attr
; /* Job-id attribute */
2577 int jobid
; /* Job ID */
2578 cupsd_job_t
*job
; /* Current job */
2579 char method
[HTTP_MAX_URI
],
2580 /* Method portion of URI */
2581 username
[HTTP_MAX_URI
],
2582 /* Username portion of URI */
2584 /* Host portion of URI */
2585 resource
[HTTP_MAX_URI
];
2586 /* Resource portion of URI */
2587 int port
; /* Port portion of URI */
2590 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2591 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
2594 * Start with "everything is OK" status...
2597 con
->response
->request
.status
.status_code
= IPP_OK
;
2600 * See if we have a job URI or a printer URI...
2603 if (!strcmp(uri
->name
, "printer-uri"))
2606 * Got a printer URI; see if we also have a job-id attribute...
2609 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2610 IPP_TAG_INTEGER
)) == NULL
)
2612 send_ipp_status(con
, IPP_BAD_REQUEST
,
2613 _("Got a printer-uri attribute but no job-id!"));
2617 jobid
= attr
->values
[0].integer
;
2622 * Got a job URI; parse it to get the job ID...
2625 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2626 sizeof(method
), username
, sizeof(username
), host
,
2627 sizeof(host
), &port
, resource
, sizeof(resource
));
2629 if (strncmp(resource
, "/jobs/", 6))
2635 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
2636 uri
->values
[0].string
.text
);
2640 jobid
= atoi(resource
+ 6);
2644 * See if the job exists...
2647 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2650 * Nope - return a "not found" error...
2653 send_ipp_status(con
, IPP_NOT_FOUND
,
2654 _("Job #%d does not exist!"), jobid
);
2659 * See if the job has been completed...
2662 if (job
->state_value
!= IPP_JOB_HELD
)
2665 * Return a "not-possible" error...
2668 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2669 _("Job #%d is not held for authentication!"),
2675 * See if we have already authenticated...
2678 if (!con
->username
[0])
2680 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
2681 _("No authentication information provided!"));
2686 * See if the job is owned by the requesting user...
2689 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
2691 send_http_error(con
, HTTP_UNAUTHORIZED
);
2696 * Save the authentication information for this job...
2699 save_auth_info(con
, job
);
2702 * Reset the job-hold-until value to "no-hold"...
2705 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2706 IPP_TAG_KEYWORD
)) == NULL
)
2707 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2711 attr
->value_tag
= IPP_TAG_KEYWORD
;
2712 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2716 * Release the job and return...
2719 cupsdReleaseJob(job
);
2721 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was authenticated by \"%s\".", jobid
,
2727 * 'cancel_all_jobs()' - Cancel all print jobs.
2731 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
2732 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2734 http_status_t status
; /* Policy status */
2735 const char *dest
; /* Destination */
2736 cups_ptype_t dtype
; /* Destination type */
2737 char method
[HTTP_MAX_URI
], /* Method portion of URI */
2738 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2739 host
[HTTP_MAX_URI
], /* Host portion of URI */
2740 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2741 int port
; /* Port portion of URI */
2742 ipp_attribute_t
*attr
; /* Attribute in request */
2743 const char *username
; /* Username */
2744 int purge
; /* Purge? */
2745 cupsd_printer_t
*printer
; /* Printer */
2748 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
2749 con
->http
.fd
, uri
->values
[0].string
.text
);
2752 * See if we have a printer URI...
2755 if (strcmp(uri
->name
, "printer-uri"))
2757 send_ipp_status(con
, IPP_BAD_REQUEST
,
2758 _("The printer-uri attribute is required!"));
2763 * Get the username (if any) for the jobs we want to cancel (only if
2764 * "my-jobs" is specified...
2767 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
2768 IPP_TAG_BOOLEAN
)) != NULL
&&
2769 attr
->values
[0].boolean
)
2771 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
2772 IPP_TAG_NAME
)) != NULL
)
2773 username
= attr
->values
[0].string
.text
;
2776 send_ipp_status(con
, IPP_BAD_REQUEST
,
2777 _("Missing requesting-user-name attribute!"));
2785 * Look for the "purge-jobs" attribute...
2788 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
2789 IPP_TAG_BOOLEAN
)) != NULL
)
2790 purge
= attr
->values
[0].boolean
;
2795 * And if the destination is valid...
2798 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2799 sizeof(method
), userpass
, sizeof(userpass
), host
,
2800 sizeof(host
), &port
, resource
, sizeof(resource
));
2802 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
2808 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
2809 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
2811 send_ipp_status(con
, IPP_NOT_FOUND
,
2812 _("The printer or class was not found."));
2815 else if (strcmp(resource
, "/printers/"))
2817 send_ipp_status(con
, IPP_NOT_FOUND
,
2818 _("The printer-uri \"%s\" is not valid."),
2819 uri
->values
[0].string
.text
);
2827 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2829 send_http_error(con
, status
);
2834 * Cancel all jobs on all printers...
2837 cupsdCancelJobs(NULL
, username
, purge
);
2839 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
2840 purge
? "purged" : "cancelled", get_username(con
));
2848 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
2850 send_http_error(con
, status
);
2855 * Cancel all of the jobs on the named printer...
2858 cupsdCancelJobs(dest
, username
, purge
);
2860 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
2861 dest
, purge
? "purged" : "cancelled", get_username(con
));
2864 con
->response
->request
.status
.status_code
= IPP_OK
;
2869 * 'cancel_job()' - Cancel a print job.
2873 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
2874 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2876 ipp_attribute_t
*attr
; /* Current attribute */
2877 int jobid
; /* Job ID */
2878 char method
[HTTP_MAX_URI
], /* Method portion of URI */
2879 username
[HTTP_MAX_URI
], /* Username portion of URI */
2880 host
[HTTP_MAX_URI
], /* Host portion of URI */
2881 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2882 int port
; /* Port portion of URI */
2883 cupsd_job_t
*job
; /* Job information */
2884 const char *dest
; /* Destination */
2885 cups_ptype_t dtype
; /* Destination type (printer or class) */
2886 cupsd_printer_t
*printer
; /* Printer data */
2889 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
2890 con
->http
.fd
, uri
->values
[0].string
.text
);
2893 * See if we have a job URI or a printer URI...
2896 if (!strcmp(uri
->name
, "printer-uri"))
2899 * Got a printer URI; see if we also have a job-id attribute...
2902 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2903 IPP_TAG_INTEGER
)) == NULL
)
2905 send_ipp_status(con
, IPP_BAD_REQUEST
,
2906 _("Got a printer-uri attribute but no job-id!"));
2910 if ((jobid
= attr
->values
[0].integer
) == 0)
2913 * Find the current job on the specified printer...
2916 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2917 sizeof(method
), username
, sizeof(username
), host
,
2918 sizeof(host
), &port
, resource
, sizeof(resource
));
2920 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
2926 send_ipp_status(con
, IPP_NOT_FOUND
,
2927 _("The printer or class was not found."));
2932 * See if the printer is currently printing a job...
2936 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
2940 * No, see if there are any pending jobs...
2943 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
2945 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
2946 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
2947 !strcasecmp(job
->dest
, dest
))
2954 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
2964 * Got a job URI; parse it to get the job ID...
2967 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2968 sizeof(method
), username
, sizeof(username
), host
,
2969 sizeof(host
), &port
, resource
, sizeof(resource
));
2971 if (strncmp(resource
, "/jobs/", 6))
2977 send_ipp_status(con
, IPP_BAD_REQUEST
,
2978 _("Bad job-uri attribute \"%s\"!"),
2979 uri
->values
[0].string
.text
);
2983 jobid
= atoi(resource
+ 6);
2987 * See if the job exists...
2990 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2993 * Nope - return a "not found" error...
2996 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3001 * See if the job is owned by the requesting user...
3004 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3006 send_http_error(con
, HTTP_UNAUTHORIZED
);
3011 * See if the job is already completed, cancelled, or aborted; if so,
3012 * we can't cancel...
3015 if (job
->state_value
>= IPP_JOB_CANCELLED
)
3017 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3018 _("Job #%d is already %s - can\'t cancel."), jobid
,
3019 job
->state_value
== IPP_JOB_CANCELLED
? "cancelled" :
3020 job
->state_value
== IPP_JOB_ABORTED
? "aborted" :
3026 * Cancel the job and return...
3029 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3030 "Job cancelled by \"%s\".", username
);
3032 cupsdCancelJob(job
, 0);
3035 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was cancelled by \"%s\".", jobid
,
3038 con
->response
->request
.status
.status_code
= IPP_OK
;
3043 * 'cancel_subscription()' - Cancel a subscription.
3047 cancel_subscription(
3048 cupsd_client_t
*con
, /* I - Client connection */
3049 int sub_id
) /* I - Subscription ID */
3051 http_status_t status
; /* Policy status */
3052 cupsd_subscription_t
*sub
; /* Subscription */
3055 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3056 "cancel_subscription(con=%p[%d], sub_id=%d)",
3057 con
, con
->http
.fd
, sub_id
);
3060 * Is the subscription ID valid?
3063 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3066 * Bad subscription ID...
3069 send_ipp_status(con
, IPP_NOT_FOUND
,
3070 _("notify-subscription-id %d no good!"), sub_id
);
3078 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3080 con
, sub
->owner
)) != HTTP_OK
)
3082 send_http_error(con
, status
);
3087 * Cancel the subscription...
3090 cupsdDeleteSubscription(sub
, 1);
3092 con
->response
->request
.status
.status_code
= IPP_OK
;
3097 * 'check_quotas()' - Check quotas for a printer and user.
3100 static int /* O - 1 if OK, 0 if not */
3101 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3102 cupsd_printer_t
*p
) /* I - Printer or class */
3104 int i
; /* Looping var */
3105 char username
[33]; /* Username */
3106 cupsd_quota_t
*q
; /* Quota data */
3107 struct passwd
*pw
; /* User password data */
3110 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3111 con
, con
->http
.fd
, p
, p
->name
);
3121 * Figure out who is printing...
3124 strlcpy(username
, get_username(con
), sizeof(username
));
3127 * Check global active job limits for printers and users...
3130 if (MaxJobsPerPrinter
)
3133 * Check if there are too many pending jobs on this printer...
3136 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3138 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3147 * Check if there are too many pending jobs for this user...
3150 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3152 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3159 * Check against users...
3162 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3167 pw
= getpwnam(username
);
3170 for (i
= 0; i
< p
->num_users
; i
++)
3171 if (p
->users
[i
][0] == '@')
3174 * Check group membership...
3177 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3180 else if (!strcasecmp(username
, p
->users
[i
]))
3183 if ((i
< p
->num_users
) == p
->deny_users
)
3185 cupsdLogMessage(CUPSD_LOG_INFO
,
3186 "Denying user \"%s\" access to printer \"%s\"...",
3196 if (p
->k_limit
|| p
->page_limit
)
3198 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3200 cupsdLogMessage(CUPSD_LOG_ERROR
,
3201 "Unable to allocate quota data for user \"%s\"!",
3206 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3207 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3209 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3216 * If we have gotten this far, we're done!
3224 * 'copy_attribute()' - Copy a single attribute.
3227 static ipp_attribute_t
* /* O - New attribute */
3229 ipp_t
*to
, /* O - Destination request/response */
3230 ipp_attribute_t
*attr
, /* I - Attribute to copy */
3231 int quickcopy
) /* I - Do a quick copy? */
3233 int i
; /* Looping var */
3234 ipp_attribute_t
*toattr
; /* Destination attribute */
3237 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3238 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
3239 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
3242 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
3245 toattr
= ippAddSeparator(to
);
3248 case IPP_TAG_INTEGER
:
3250 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3251 attr
->name
, attr
->num_values
, NULL
);
3253 for (i
= 0; i
< attr
->num_values
; i
++)
3254 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
3257 case IPP_TAG_BOOLEAN
:
3258 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
3259 attr
->num_values
, NULL
);
3261 for (i
= 0; i
< attr
->num_values
; i
++)
3262 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
3265 case IPP_TAG_STRING
:
3268 case IPP_TAG_KEYWORD
:
3270 case IPP_TAG_URISCHEME
:
3271 case IPP_TAG_CHARSET
:
3272 case IPP_TAG_LANGUAGE
:
3273 case IPP_TAG_MIMETYPE
:
3274 toattr
= ippAddStrings(to
, attr
->group_tag
,
3275 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3276 attr
->name
, attr
->num_values
, NULL
, NULL
);
3280 for (i
= 0; i
< attr
->num_values
; i
++)
3281 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3285 for (i
= 0; i
< attr
->num_values
; i
++)
3286 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3291 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
3292 attr
->values
[0].date
);
3295 case IPP_TAG_RESOLUTION
:
3296 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
3297 attr
->num_values
, IPP_RES_PER_INCH
,
3300 for (i
= 0; i
< attr
->num_values
; i
++)
3302 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
3303 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
3304 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
3308 case IPP_TAG_RANGE
:
3309 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
3310 attr
->num_values
, NULL
, NULL
);
3312 for (i
= 0; i
< attr
->num_values
; i
++)
3314 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
3315 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
3319 case IPP_TAG_TEXTLANG
:
3320 case IPP_TAG_NAMELANG
:
3321 toattr
= ippAddStrings(to
, attr
->group_tag
,
3322 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3323 attr
->name
, attr
->num_values
, NULL
, NULL
);
3327 for (i
= 0; i
< attr
->num_values
; i
++)
3329 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
3330 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3335 for (i
= 0; i
< attr
->num_values
; i
++)
3338 toattr
->values
[i
].string
.charset
=
3339 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
3341 toattr
->values
[i
].string
.charset
=
3342 toattr
->values
[0].string
.charset
;
3344 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3349 case IPP_TAG_BEGIN_COLLECTION
:
3350 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
3351 attr
->num_values
, NULL
);
3353 for (i
= 0; i
< attr
->num_values
; i
++)
3355 toattr
->values
[i
].collection
= ippNew();
3356 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
3357 NULL
, IPP_TAG_ZERO
, 0);
3362 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3363 attr
->name
, attr
->num_values
, NULL
);
3365 for (i
= 0; i
< attr
->num_values
; i
++)
3367 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
3369 if (toattr
->values
[i
].unknown
.length
> 0)
3371 if ((toattr
->values
[i
].unknown
.data
=
3372 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
3373 toattr
->values
[i
].unknown
.length
= 0;
3375 memcpy(toattr
->values
[i
].unknown
.data
,
3376 attr
->values
[i
].unknown
.data
,
3377 toattr
->values
[i
].unknown
.length
);
3380 break; /* anti-compiler-warning-code */
3388 * 'copy_attrs()' - Copy attributes from one request to another.
3392 copy_attrs(ipp_t
*to
, /* I - Destination request */
3393 ipp_t
*from
, /* I - Source request */
3394 cups_array_t
*ra
, /* I - Requested attributes */
3395 ipp_tag_t group
, /* I - Group to copy */
3396 int quickcopy
) /* I - Do a quick copy? */
3398 ipp_attribute_t
*fromattr
; /* Source attribute */
3401 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3402 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3403 to
, from
, ra
, group
, quickcopy
);
3408 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3411 * Filter attributes as needed...
3414 if (group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3415 fromattr
->group_tag
!= IPP_TAG_ZERO
&& !fromattr
->name
)
3418 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
3419 copy_attribute(to
, fromattr
, quickcopy
);
3425 * 'copy_banner()' - Copy a banner file to the requests directory for the
3429 static int /* O - Size of banner file in kbytes */
3430 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
3431 cupsd_job_t
*job
, /* I - Job information */
3432 const char *name
) /* I - Name of banner */
3434 int i
; /* Looping var */
3435 int kbytes
; /* Size of banner file in kbytes */
3436 char filename
[1024]; /* Job filename */
3437 cupsd_banner_t
*banner
; /* Pointer to banner */
3438 cups_file_t
*in
; /* Input file */
3439 cups_file_t
*out
; /* Output file */
3440 int ch
; /* Character from file */
3441 char attrname
[255], /* Name of attribute */
3442 *s
; /* Pointer into name */
3443 ipp_attribute_t
*attr
; /* Attribute */
3446 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
3447 con
, con
->http
.fd
, job
, job
->id
, name
? name
: "(null)");
3450 * Find the banner; return if not found or "none"...
3453 if (!name
|| !strcmp(name
, "none") ||
3454 (banner
= cupsdFindBanner(name
)) == NULL
)
3458 * Open the banner and job files...
3461 if (add_file(con
, job
, banner
->filetype
, 0))
3464 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
3466 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
3468 cupsdLogMessage(CUPSD_LOG_ERROR
,
3469 "copy_banner: Unable to create banner job file %s - %s",
3470 filename
, strerror(errno
));
3475 fchmod(cupsFileNumber(out
), 0640);
3476 fchown(cupsFileNumber(out
), RunUser
, Group
);
3479 * Try the localized banner file under the subdirectory...
3482 strlcpy(attrname
, con
->request
->attrs
->next
->values
[0].string
.text
,
3484 if (strlen(attrname
) > 2 && attrname
[2] == '-')
3487 * Convert ll-cc to ll_CC...
3491 attrname
[3] = toupper(attrname
[3] & 255);
3492 attrname
[4] = toupper(attrname
[4] & 255);
3495 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3498 if (access(filename
, 0) && strlen(attrname
) > 2)
3501 * Wasn't able to find "ll_CC" locale file; try the non-national
3502 * localization banner directory.
3507 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3511 if (access(filename
, 0))
3514 * Use the non-localized banner file.
3517 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
3520 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
3524 cupsdLogMessage(CUPSD_LOG_ERROR
,
3525 "copy_banner: Unable to open banner template file %s - %s",
3526 filename
, strerror(errno
));
3532 * Parse the file to the end...
3535 while ((ch
= cupsFileGetChar(in
)) != EOF
)
3539 * Get an attribute name...
3542 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
3543 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
3545 else if (s
< (attrname
+ sizeof(attrname
) - 1))
3555 * Ignore { followed by stuff that is not an attribute name...
3558 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
3563 * See if it is defined...
3566 if (attrname
[0] == '?')
3571 if (!strcmp(s
, "printer-name"))
3573 cupsFilePuts(out
, job
->dest
);
3576 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
3579 * See if we have a leading question mark...
3582 if (attrname
[0] != '?')
3585 * Nope, write to file as-is; probably a PostScript procedure...
3588 cupsFilePrintf(out
, "{%s}", attrname
);
3595 * Output value(s)...
3598 for (i
= 0; i
< attr
->num_values
; i
++)
3601 cupsFilePutChar(out
, ',');
3603 switch (attr
->value_tag
)
3605 case IPP_TAG_INTEGER
:
3607 if (!strncmp(s
, "time-at-", 8))
3608 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
3610 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
3613 case IPP_TAG_BOOLEAN
:
3614 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
3617 case IPP_TAG_NOVALUE
:
3618 cupsFilePuts(out
, "novalue");
3621 case IPP_TAG_RANGE
:
3622 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
3623 attr
->values
[i
].range
.upper
);
3626 case IPP_TAG_RESOLUTION
:
3627 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3628 attr
->values
[i
].resolution
.yres
,
3629 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3634 case IPP_TAG_STRING
:
3637 case IPP_TAG_KEYWORD
:
3638 case IPP_TAG_CHARSET
:
3639 case IPP_TAG_LANGUAGE
:
3640 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
3643 * Need to quote strings for PS banners...
3648 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
3650 if (*p
== '(' || *p
== ')' || *p
== '\\')
3652 cupsFilePutChar(out
, '\\');
3653 cupsFilePutChar(out
, *p
);
3655 else if (*p
< 32 || *p
> 126)
3656 cupsFilePrintf(out
, "\\%03o", *p
& 255);
3658 cupsFilePutChar(out
, *p
);
3662 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
3666 break; /* anti-compiler-warning-code */
3670 else if (ch
== '\\') /* Quoted char */
3672 ch
= cupsFileGetChar(in
);
3674 if (ch
!= '{') /* Only do special handling for \{ */
3675 cupsFilePutChar(out
, '\\');
3677 cupsFilePutChar(out
, ch
);
3680 cupsFilePutChar(out
, ch
);
3684 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
3686 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
3687 IPP_TAG_INTEGER
)) != NULL
)
3688 attr
->values
[0].integer
+= kbytes
;
3697 * 'copy_file()' - Copy a PPD file or interface script...
3700 static int /* O - 0 = success, -1 = error */
3701 copy_file(const char *from
, /* I - Source file */
3702 const char *to
) /* I - Destination file */
3704 cups_file_t
*src
, /* Source file */
3705 *dst
; /* Destination file */
3706 int bytes
; /* Bytes to read/write */
3707 char buffer
[2048]; /* Copy buffer */
3710 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
3713 * Open the source and destination file for a copy...
3716 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
3719 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
3726 * Copy the source file to the destination...
3729 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
3730 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
3738 * Close both files and return...
3743 return (cupsFileClose(dst
));
3748 * 'copy_model()' - Copy a PPD model file, substituting default values
3752 static int /* O - 0 = success, -1 = error */
3753 copy_model(cupsd_client_t
*con
, /* I - Client connection */
3754 const char *from
, /* I - Source file */
3755 const char *to
) /* I - Destination file */
3757 fd_set
*input
; /* select() input set */
3758 struct timeval timeout
; /* select() timeout */
3759 int maxfd
; /* Maximum file descriptor for select() */
3760 char tempfile
[1024]; /* Temporary PPD file */
3761 int tempfd
; /* Temporary PPD file descriptor */
3762 int temppid
; /* Process ID of cups-driverd */
3763 int temppipe
[2]; /* Temporary pipes */
3764 char *argv
[4], /* Command-line arguments */
3765 *envp
[MAX_ENV
]; /* Environment */
3766 cups_file_t
*src
, /* Source file */
3767 *dst
; /* Destination file */
3768 int bytes
, /* Bytes from pipe */
3769 total
; /* Total bytes from pipe */
3770 char buffer
[2048], /* Copy buffer */
3771 *ptr
; /* Pointer into buffer */
3772 int i
; /* Looping var */
3773 char option
[PPD_MAX_NAME
], /* Option name */
3774 choice
[PPD_MAX_NAME
]; /* Choice name */
3775 int num_defaults
; /* Number of default options */
3776 ppd_default_t
*defaults
; /* Default options */
3777 char cups_protocol
[PPD_MAX_LINE
];
3778 /* cupsProtocol attribute */
3779 int have_letter
, /* Have Letter size */
3780 have_a4
; /* Have A4 size */
3781 #ifdef HAVE_LIBPAPER
3782 char *paper_result
; /* Paper size name from libpaper */
3783 char system_paper
[64]; /* Paper size name buffer */
3784 #endif /* HAVE_LIBPAPER */
3787 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3788 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
3792 * Run cups-driverd to get the PPD file...
3795 argv
[0] = "cups-driverd";
3797 argv
[2] = (char *)from
;
3800 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3802 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
3803 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
3804 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3808 cupsdOpenPipe(temppipe
);
3810 if ((input
= calloc(1, SetSize
)) == NULL
)
3815 cupsdLogMessage(CUPSD_LOG_ERROR
,
3816 "copy_model: Unable to allocate %d bytes for select()...",
3821 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3822 "copy_model: Running \"cups-driverd cat %s\"...", from
);
3824 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
3836 * Wait up to 30 seconds for the PPD file to be copied...
3841 if (temppipe
[0] > CGIPipes
[0])
3842 maxfd
= temppipe
[0] + 1;
3844 maxfd
= CGIPipes
[0] + 1;
3849 * See if we have data ready...
3854 FD_SET(temppipe
[0], input
);
3855 FD_SET(CGIPipes
[0], input
);
3857 timeout
.tv_sec
= 30;
3858 timeout
.tv_usec
= 0;
3860 if ((i
= select(maxfd
, input
, NULL
, NULL
, &timeout
)) < 0)
3870 * We have timed out...
3876 if (FD_ISSET(temppipe
[0], input
))
3879 * Read the PPD file from the pipe, and write it to the PPD file.
3882 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
3884 if (write(tempfd
, buffer
, bytes
) < bytes
)
3893 if (FD_ISSET(CGIPipes
[0], input
))
3905 * No data from cups-deviced...
3908 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
3914 * Read the source file and see what page sizes are supported...
3917 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
3926 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
3927 if (!strncmp(buffer
, "*PageSize ", 10))
3930 * Strip UI text and command data from the end of the line...
3933 if ((ptr
= strchr(buffer
+ 10, '/')) != NULL
)
3935 if ((ptr
= strchr(buffer
+ 10, ':')) != NULL
)
3938 for (ptr
= buffer
+ 10; isspace(*ptr
); ptr
++);
3941 * Look for Letter and A4 page sizes...
3944 if (!strcmp(ptr
, "Letter"))
3947 if (!strcmp(ptr
, "A4"))
3951 cupsFileRewind(src
);
3954 * Open the destination (if possible) and set the default options...
3959 cups_protocol
[0] = '\0';
3961 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
3964 * Read all of the default lines from the old PPD...
3967 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
3968 if (!strncmp(buffer
, "*Default", 8))
3971 * Add the default option...
3974 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
3975 choice
, sizeof(choice
)))
3976 num_defaults
= ppd_add_default(option
, choice
, num_defaults
,
3979 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
3980 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
3984 #ifdef HAVE_LIBPAPER
3985 else if ((paper_result
= systempapername()) != NULL
)
3988 * Set the default media sizes from the systemwide default...
3991 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
3992 system_paper
[0] = toupper(system_paper
[0] & 255);
3994 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
3995 (!strcmp(system_paper
, "A4") && have_a4
))
3997 num_defaults
= ppd_add_default("PageSize", system_paper
,
3998 num_defaults
, &defaults
);
3999 num_defaults
= ppd_add_default("PageRegion", system_paper
,
4000 num_defaults
, &defaults
);
4001 num_defaults
= ppd_add_default("PaperDimension", system_paper
,
4002 num_defaults
, &defaults
);
4003 num_defaults
= ppd_add_default("ImageableArea", system_paper
,
4004 num_defaults
, &defaults
);
4007 #endif /* HAVE_LIBPAPER */
4011 * Add the default media sizes...
4013 * Note: These values are generally not valid for large-format devices
4014 * like plotters, however it is probably safe to say that those
4015 * users will configure the media size after initially adding
4016 * the device anyways...
4019 if (!DefaultLanguage
||
4020 !strcasecmp(DefaultLanguage
, "C") ||
4021 !strcasecmp(DefaultLanguage
, "POSIX") ||
4022 !strcasecmp(DefaultLanguage
, "en") ||
4023 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4024 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4025 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4028 * These are the only locales that will default to "letter" size...
4033 num_defaults
= ppd_add_default("PageSize", "Letter", num_defaults
,
4035 num_defaults
= ppd_add_default("PageRegion", "Letter", num_defaults
,
4037 num_defaults
= ppd_add_default("PaperDimension", "Letter", num_defaults
,
4039 num_defaults
= ppd_add_default("ImageableArea", "Letter", num_defaults
,
4046 * The rest default to "a4" size...
4049 num_defaults
= ppd_add_default("PageSize", "A4", num_defaults
,
4051 num_defaults
= ppd_add_default("PageRegion", "A4", num_defaults
,
4053 num_defaults
= ppd_add_default("PaperDimension", "A4", num_defaults
,
4055 num_defaults
= ppd_add_default("ImageableArea", "A4", num_defaults
,
4061 * Open the destination file for a copy...
4064 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4066 if (num_defaults
> 0)
4075 * Copy the source file to the destination...
4078 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4080 if (!strncmp(buffer
, "*Default", 8))
4083 * Check for an previous default option choice...
4086 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4087 choice
, sizeof(choice
)))
4089 for (i
= 0; i
< num_defaults
; i
++)
4090 if (!strcmp(option
, defaults
[i
].option
))
4093 * Substitute the previous choice...
4096 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
,
4097 defaults
[i
].choice
);
4103 cupsFilePrintf(dst
, "%s\n", buffer
);
4106 if (cups_protocol
[0])
4107 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4109 if (num_defaults
> 0)
4113 * Close both files and return...
4120 return (cupsFileClose(dst
));
4125 * 'copy_job_attrs()' - Copy job attributes.
4129 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4130 cupsd_job_t
*job
, /* I - Job */
4131 cups_array_t
*ra
) /* I - Requested attributes array */
4133 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4137 * Send the requested attributes for each job...
4140 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4141 con
->servername
, con
->serverport
, "/jobs/%d",
4144 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
4145 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4146 "job-more-info", NULL
, job_uri
);
4148 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4149 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4150 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4151 job
->num_files
> 0);
4153 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
4154 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4155 "job-printer-up-time", time(NULL
));
4157 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4158 add_job_state_reasons(con
, job
);
4160 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4161 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4162 "job-uri", NULL
, job_uri
);
4164 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
4169 * 'copy_printer_attrs()' - Copy printer attributes.
4174 cupsd_client_t
*con
, /* I - Client connection */
4175 cupsd_printer_t
*printer
, /* I - Printer */
4176 cups_array_t
*ra
) /* I - Requested attributes array */
4178 char printer_uri
[HTTP_MAX_URI
];
4180 time_t curtime
; /* Current time */
4181 int i
; /* Looping var */
4182 ipp_attribute_t
*history
; /* History collection */
4186 * Copy the printer attributes to the response using requested-attributes
4187 * and document-format attributes that may be provided by the client.
4190 curtime
= time(NULL
);
4193 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
4194 printer
->recoverable
)
4195 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4196 "com.apple.print.recoverable-message", NULL
,
4197 printer
->recoverable
);
4198 #endif /* __APPLE__ */
4200 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4201 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4202 ippTimeToDate(curtime
));
4204 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4205 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4206 "printer-error-policy", NULL
, printer
->error_policy
);
4208 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4209 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
4210 printer
->accepting
);
4212 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4213 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
4216 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4217 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4218 "printer-op-policy", NULL
, printer
->op_policy
);
4220 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4221 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4224 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4225 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4226 "printer-state-change-time", printer
->state_time
);
4228 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
4229 cupsArrayFind(ra
, "printer-state-history"))
4232 * Printer history is only sent if specifically requested, so that
4233 * older CUPS/IPP clients won't barf on the collection attributes.
4236 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
4237 "printer-state-history",
4238 printer
->num_history
, NULL
);
4240 for (i
= 0; i
< printer
->num_history
; i
++)
4241 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
4242 NULL
, IPP_TAG_ZERO
, 0);
4245 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4246 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4247 "printer-state-message", NULL
, printer
->state_message
);
4249 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4250 add_printer_state_reasons(con
, printer
);
4252 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4254 int type
; /* printer-type value */
4258 * Add the CUPS-specific printer-type attribute...
4261 type
= printer
->type
;
4263 if (printer
== DefaultPrinter
)
4264 type
|= CUPS_PRINTER_DEFAULT
;
4266 if (!printer
->accepting
)
4267 type
|= CUPS_PRINTER_REJECTING
;
4269 if (!printer
->shared
)
4270 type
|= CUPS_PRINTER_NOT_SHARED
;
4272 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
4276 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4277 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4278 "printer-up-time", curtime
);
4280 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
4281 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
4284 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4285 "ipp", NULL
, con
->servername
, con
->serverport
,
4286 "/printers/%s", printer
->name
);
4287 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4288 "printer-uri-supported", NULL
, printer_uri
);
4289 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4293 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4294 add_queued_job_count(con
, printer
);
4296 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
4297 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
4302 * 'copy_subscription_attrs()' - Copy subscription attributes.
4306 copy_subscription_attrs(
4307 cupsd_client_t
*con
, /* I - Client connection */
4308 cupsd_subscription_t
*sub
, /* I - Subscription */
4309 cups_array_t
*ra
) /* I - Requested attributes array */
4311 ipp_attribute_t
*attr
; /* Current attribute */
4312 char printer_uri
[HTTP_MAX_URI
];
4314 int count
; /* Number of events */
4315 unsigned mask
; /* Current event mask */
4316 const char *name
; /* Current event name */
4320 * Copy the subscription attributes to the response using the
4321 * requested-attributes attribute that may be provided by the client.
4324 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
4326 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
4329 * Simple event list...
4332 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
4333 IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
4334 "notify-events", NULL
, name
);
4339 * Complex event list...
4342 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4343 if (sub
->mask
& mask
)
4346 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
4347 IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
4348 "notify-events", count
, NULL
, NULL
);
4350 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4351 if (sub
->mask
& mask
)
4353 attr
->values
[count
].string
.text
=
4354 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
4361 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
4362 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4363 "notify-job-id", sub
->job
->id
);
4365 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
4366 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4367 "notify-lease-duration", sub
->lease
);
4369 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
4371 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4372 "ipp", NULL
, con
->servername
, con
->serverport
,
4373 "/printers/%s", sub
->dest
->name
);
4374 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4375 "notify-printer-uri", NULL
, printer_uri
);
4378 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
4379 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4380 "notify-recipient-uri", NULL
, sub
->recipient
);
4381 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
4382 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
4383 "notify-pull-method", NULL
, "ippget");
4385 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
4386 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
4387 "notify-subscriber-user-name", NULL
, sub
->owner
);
4389 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
4390 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4391 "notify-subscription-id", sub
->id
);
4393 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
4394 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4395 "notify-time-interval", sub
->interval
);
4397 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
4398 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
4399 sub
->user_data
, sub
->user_data_len
);
4404 * 'create_job()' - Print a file to a printer or class.
4408 create_job(cupsd_client_t
*con
, /* I - Client connection */
4409 ipp_attribute_t
*uri
) /* I - Printer URI */
4411 cupsd_job_t
*job
; /* New job */
4414 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
4415 con
->http
.fd
, uri
->values
[0].string
.text
);
4418 * Create the job object...
4421 if ((job
= add_job(con
, uri
, NULL
)) == NULL
)
4425 * Save and log the job...
4430 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d created on \"%s\" by \"%s\".",
4431 job
->id
, job
->dest
, job
->username
);
4436 * 'create_requested_array()' - Create an array for the requested-attributes.
4439 static cups_array_t
* /* O - Array of attributes or NULL */
4440 create_requested_array(ipp_t
*request
) /* I - IPP request */
4442 int i
; /* Looping var */
4443 ipp_attribute_t
*requested
; /* requested-attributes attribute */
4444 cups_array_t
*ra
; /* Requested attributes array */
4445 char *value
; /* Current value */
4449 * Get the requested-attributes attribute, and return NULL if we don't
4453 if ((requested
= ippFindAttribute(request
, "requested-attributes",
4454 IPP_TAG_KEYWORD
)) == NULL
)
4458 * If the attribute contains a single "all" keyword, return NULL...
4461 if (requested
->num_values
== 1 &&
4462 !strcmp(requested
->values
[0].string
.text
, "all"))
4466 * Create an array using "strcmp" as the comparison function...
4469 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
4471 for (i
= 0; i
< requested
->num_values
; i
++)
4473 value
= requested
->values
[i
].string
.text
;
4475 if (!strcmp(value
, "job-template"))
4477 cupsArrayAdd(ra
, "copies");
4478 cupsArrayAdd(ra
, "copies-default");
4479 cupsArrayAdd(ra
, "copies-supported");
4480 cupsArrayAdd(ra
, "finishings");
4481 cupsArrayAdd(ra
, "finishings-default");
4482 cupsArrayAdd(ra
, "finishings-supported");
4483 cupsArrayAdd(ra
, "job-hold-until");
4484 cupsArrayAdd(ra
, "job-hold-until-default");
4485 cupsArrayAdd(ra
, "job-hold-until-supported");
4486 cupsArrayAdd(ra
, "job-priority");
4487 cupsArrayAdd(ra
, "job-priority-default");
4488 cupsArrayAdd(ra
, "job-priority-supported");
4489 cupsArrayAdd(ra
, "job-sheets");
4490 cupsArrayAdd(ra
, "job-sheets-default");
4491 cupsArrayAdd(ra
, "job-sheets-supported");
4492 cupsArrayAdd(ra
, "media");
4493 cupsArrayAdd(ra
, "media-default");
4494 cupsArrayAdd(ra
, "media-supported");
4495 cupsArrayAdd(ra
, "multiple-document-handling");
4496 cupsArrayAdd(ra
, "multiple-document-handling-default");
4497 cupsArrayAdd(ra
, "multiple-document-handling-supported");
4498 cupsArrayAdd(ra
, "number-up");
4499 cupsArrayAdd(ra
, "number-up-default");
4500 cupsArrayAdd(ra
, "number-up-supported");
4501 cupsArrayAdd(ra
, "orientation-requested");
4502 cupsArrayAdd(ra
, "orientation-requested-default");
4503 cupsArrayAdd(ra
, "orientation-requested-supported");
4504 cupsArrayAdd(ra
, "page-ranges");
4505 cupsArrayAdd(ra
, "page-ranges-supported");
4506 cupsArrayAdd(ra
, "printer-resolution");
4507 cupsArrayAdd(ra
, "printer-resolution-default");
4508 cupsArrayAdd(ra
, "printer-resolution-supported");
4509 cupsArrayAdd(ra
, "print-quality");
4510 cupsArrayAdd(ra
, "print-quality-default");
4511 cupsArrayAdd(ra
, "print-quality-supported");
4512 cupsArrayAdd(ra
, "sides");
4513 cupsArrayAdd(ra
, "sides-default");
4514 cupsArrayAdd(ra
, "sides-supported");
4516 else if (!strcmp(value
, "job-description"))
4518 cupsArrayAdd(ra
, "date-time-at-completed");
4519 cupsArrayAdd(ra
, "date-time-at-creation");
4520 cupsArrayAdd(ra
, "date-time-at-processing");
4521 cupsArrayAdd(ra
, "job-detailed-status-message");
4522 cupsArrayAdd(ra
, "job-document-access-errors");
4523 cupsArrayAdd(ra
, "job-id");
4524 cupsArrayAdd(ra
, "job-impressions");
4525 cupsArrayAdd(ra
, "job-impressions-completed");
4526 cupsArrayAdd(ra
, "job-k-octets");
4527 cupsArrayAdd(ra
, "job-k-octets-processed");
4528 cupsArrayAdd(ra
, "job-media-sheets");
4529 cupsArrayAdd(ra
, "job-media-sheets-completed");
4530 cupsArrayAdd(ra
, "job-message-from-operator");
4531 cupsArrayAdd(ra
, "job-more-info");
4532 cupsArrayAdd(ra
, "job-name");
4533 cupsArrayAdd(ra
, "job-originating-user-name");
4534 cupsArrayAdd(ra
, "job-printer-up-time");
4535 cupsArrayAdd(ra
, "job-printer-uri");
4536 cupsArrayAdd(ra
, "job-state");
4537 cupsArrayAdd(ra
, "job-state-message");
4538 cupsArrayAdd(ra
, "job-state-reasons");
4539 cupsArrayAdd(ra
, "job-uri");
4540 cupsArrayAdd(ra
, "number-of-documents");
4541 cupsArrayAdd(ra
, "number-of-intervening-jobs");
4542 cupsArrayAdd(ra
, "output-device-assigned");
4543 cupsArrayAdd(ra
, "time-at-completed");
4544 cupsArrayAdd(ra
, "time-at-creation");
4545 cupsArrayAdd(ra
, "time-at-processing");
4547 else if (!strcmp(value
, "printer-description"))
4549 cupsArrayAdd(ra
, "charset-configured");
4550 cupsArrayAdd(ra
, "charset-supported");
4551 cupsArrayAdd(ra
, "color-supported");
4552 cupsArrayAdd(ra
, "compression-supported");
4553 cupsArrayAdd(ra
, "document-format-default");
4554 cupsArrayAdd(ra
, "document-format-supported");
4555 cupsArrayAdd(ra
, "generated-natural-language-supported");
4556 cupsArrayAdd(ra
, "ipp-versions-supported");
4557 cupsArrayAdd(ra
, "job-impressions-supported");
4558 cupsArrayAdd(ra
, "job-k-octets-supported");
4559 cupsArrayAdd(ra
, "job-media-sheets-supported");
4560 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
4561 cupsArrayAdd(ra
, "multiple-operation-time-out");
4562 cupsArrayAdd(ra
, "natural-language-configured");
4563 cupsArrayAdd(ra
, "notify-attributes-supported");
4564 cupsArrayAdd(ra
, "notify-lease-duration-default");
4565 cupsArrayAdd(ra
, "notify-lease-duration-supported");
4566 cupsArrayAdd(ra
, "notify-max-events-supported");
4567 cupsArrayAdd(ra
, "notify-notify-events-default");
4568 cupsArrayAdd(ra
, "notify-notify-events-supported");
4569 cupsArrayAdd(ra
, "notify-pull-method-supported");
4570 cupsArrayAdd(ra
, "notify-schemes-supported");
4571 cupsArrayAdd(ra
, "operations-supported");
4572 cupsArrayAdd(ra
, "pages-per-minute");
4573 cupsArrayAdd(ra
, "pages-per-minute-color");
4574 cupsArrayAdd(ra
, "pdl-override-supported");
4575 cupsArrayAdd(ra
, "printer-current-time");
4576 cupsArrayAdd(ra
, "printer-driver-installer");
4577 cupsArrayAdd(ra
, "printer-info");
4578 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
4579 cupsArrayAdd(ra
, "printer-location");
4580 cupsArrayAdd(ra
, "printer-make-and-model");
4581 cupsArrayAdd(ra
, "printer-message-from-operator");
4582 cupsArrayAdd(ra
, "printer-more-info");
4583 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
4584 cupsArrayAdd(ra
, "printer-name");
4585 cupsArrayAdd(ra
, "printer-state");
4586 cupsArrayAdd(ra
, "printer-state-message");
4587 cupsArrayAdd(ra
, "printer-state-reasons");
4588 cupsArrayAdd(ra
, "printer-up-time");
4589 cupsArrayAdd(ra
, "printer-uri-supported");
4590 cupsArrayAdd(ra
, "queued-job-count");
4591 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
4592 cupsArrayAdd(ra
, "uri-authentication-supported");
4593 cupsArrayAdd(ra
, "uri-security-supported");
4595 else if (!strcmp(value
, "subscription-template"))
4597 cupsArrayAdd(ra
, "notify-attributes");
4598 cupsArrayAdd(ra
, "notify-charset");
4599 cupsArrayAdd(ra
, "notify-events");
4600 cupsArrayAdd(ra
, "notify-lease-duration");
4601 cupsArrayAdd(ra
, "notify-natural-language");
4602 cupsArrayAdd(ra
, "notify-pull-method");
4603 cupsArrayAdd(ra
, "notify-recipient-uri");
4604 cupsArrayAdd(ra
, "notify-time-interval");
4605 cupsArrayAdd(ra
, "notify-user-data");
4608 cupsArrayAdd(ra
, value
);
4616 * 'create_subscription()' - Create a notification subscription.
4620 create_subscription(
4621 cupsd_client_t
*con
, /* I - Client connection */
4622 ipp_attribute_t
*uri
) /* I - Printer URI */
4624 http_status_t status
; /* Policy status */
4625 int i
; /* Looping var */
4626 ipp_attribute_t
*attr
; /* Current attribute */
4627 const char *dest
; /* Destination */
4628 cups_ptype_t dtype
; /* Destination type (printer or class) */
4629 char method
[HTTP_MAX_URI
],
4630 /* Method portion of URI */
4631 userpass
[HTTP_MAX_URI
],
4632 /* Username portion of URI */
4634 /* Host portion of URI */
4635 resource
[HTTP_MAX_URI
];
4636 /* Resource portion of URI */
4637 int port
; /* Port portion of URI */
4638 cupsd_printer_t
*printer
; /* Printer/class */
4639 cupsd_job_t
*job
; /* Job */
4640 int jobid
; /* Job ID */
4641 cupsd_subscription_t
*sub
; /* Subscription object */
4642 const char *username
, /* requesting-user-name or authenticated username */
4643 *recipient
, /* notify-recipient-uri */
4644 *pullmethod
; /* notify-pull-method */
4645 ipp_attribute_t
*user_data
; /* notify-user-data */
4646 int interval
, /* notify-time-interval */
4647 lease
; /* notify-lease-duration */
4648 unsigned mask
; /* notify-events */
4652 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4654 if (attr
->group_tag
!= IPP_TAG_ZERO
)
4655 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
4656 attr
->value_tag
, attr
->name
);
4658 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
4663 * Is the destination valid?
4666 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4667 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
4668 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
4670 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
4671 sizeof(method
), userpass
, sizeof(userpass
), host
,
4672 sizeof(host
), &port
, resource
, sizeof(resource
));
4674 if (!strcmp(resource
, "/"))
4677 dtype
= (cups_ptype_t
)0;
4680 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
4683 dtype
= (cups_ptype_t
)0;
4686 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
4689 dtype
= CUPS_PRINTER_CLASS
;
4692 else if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
4698 send_ipp_status(con
, IPP_NOT_FOUND
,
4699 _("The printer or class was not found."));
4709 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
4711 send_http_error(con
, status
);
4715 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
4717 send_http_error(con
, status
);
4722 * Get the user that is requesting the subscription...
4725 username
= get_username(con
);
4728 * Find the first subscription group attribute; return if we have
4732 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4733 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
4738 send_ipp_status(con
, IPP_BAD_REQUEST
,
4739 _("No subscription attributes in request!"));
4744 * Process the subscription attributes in the request...
4747 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
4755 lease
= DefaultLeaseDuration
;
4757 mask
= CUPSD_EVENT_NONE
;
4759 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
4761 if (!strcmp(attr
->name
, "notify-recipient") &&
4762 attr
->value_tag
== IPP_TAG_URI
)
4763 recipient
= attr
->values
[0].string
.text
;
4764 else if (!strcmp(attr
->name
, "notify-pull-method") &&
4765 attr
->value_tag
== IPP_TAG_KEYWORD
)
4766 pullmethod
= attr
->values
[0].string
.text
;
4767 else if (!strcmp(attr
->name
, "notify-charset") &&
4768 attr
->value_tag
== IPP_TAG_CHARSET
&&
4769 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
4770 strcmp(attr
->values
[0].string
.text
, "utf-8"))
4772 send_ipp_status(con
, IPP_CHARSET
,
4773 _("Character set \"%s\" not supported!"),
4774 attr
->values
[0].string
.text
);
4777 else if (!strcmp(attr
->name
, "notify-natural-language") &&
4778 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
4779 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
4781 send_ipp_status(con
, IPP_CHARSET
,
4782 _("Language \"%s\" not supported!"),
4783 attr
->values
[0].string
.text
);
4786 else if (!strcmp(attr
->name
, "notify-user-data") &&
4787 attr
->value_tag
== IPP_TAG_STRING
)
4789 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
4791 send_ipp_status(con
, IPP_REQUEST_VALUE
,
4792 _("The notify-user-data value is too large "
4793 "(%d > 63 octets)!"),
4794 attr
->values
[0].unknown
.length
);
4800 else if (!strcmp(attr
->name
, "notify-events") &&
4801 attr
->value_tag
== IPP_TAG_KEYWORD
)
4803 for (i
= 0; i
< attr
->num_values
; i
++)
4804 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
4806 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
4807 attr
->value_tag
== IPP_TAG_INTEGER
)
4808 lease
= attr
->values
[0].integer
;
4809 else if (!strcmp(attr
->name
, "notify-time-interval") &&
4810 attr
->value_tag
== IPP_TAG_INTEGER
)
4811 interval
= attr
->values
[0].integer
;
4812 else if (!strcmp(attr
->name
, "notify-job-id") &&
4813 attr
->value_tag
== IPP_TAG_INTEGER
)
4814 jobid
= attr
->values
[0].integer
;
4820 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
4822 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
4823 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
4824 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
4826 if (!recipient
&& !pullmethod
)
4829 if (mask
== CUPSD_EVENT_NONE
)
4832 mask
= CUPSD_EVENT_JOB_COMPLETED
;
4834 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4837 send_ipp_status(con
, IPP_BAD_REQUEST
,
4838 _("notify-events not specified!"));
4843 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
4845 cupsdLogMessage(CUPSD_LOG_INFO
,
4846 "create_subscription: Limiting notify-lease-duration to "
4849 lease
= MaxLeaseDuration
;
4854 if ((job
= cupsdFindJob(jobid
)) == NULL
)
4856 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
4863 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
4866 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
4869 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4870 "Added subscription %d for printer \"%s\"",
4871 sub
->id
, printer
->name
);
4873 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
4876 sub
->interval
= interval
;
4878 sub
->expire
= lease
? time(NULL
) + lease
: 0;
4880 cupsdSetString(&sub
->owner
, username
);
4884 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
4885 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
4886 sub
->user_data_len
);
4889 ippAddSeparator(con
->response
);
4890 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4891 "notify-subscription-id", sub
->id
);
4893 con
->response
->request
.status
.status_code
= IPP_OK
;
4899 cupsdSaveAllSubscriptions();
4905 * 'delete_printer()' - Remove a printer or class from the system.
4909 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
4910 ipp_attribute_t
*uri
) /* I - URI of printer or class */
4912 http_status_t status
; /* Policy status */
4913 const char *dest
; /* Destination */
4914 cups_ptype_t dtype
; /* Destination type (printer or class) */
4915 char method
[HTTP_MAX_URI
], /* Method portion of URI */
4916 username
[HTTP_MAX_URI
], /* Username portion of URI */
4917 host
[HTTP_MAX_URI
], /* Host portion of URI */
4918 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
4919 int port
; /* Port portion of URI */
4920 cupsd_printer_t
*printer
; /* Printer/class */
4921 char filename
[1024]; /* Script/PPD filename */
4924 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
4925 con
->http
.fd
, uri
->values
[0].string
.text
);
4928 * Do we have a valid URI?
4931 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
4932 sizeof(method
), username
, sizeof(username
), host
,
4933 sizeof(host
), &port
, resource
, sizeof(resource
));
4935 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
4941 send_ipp_status(con
, IPP_NOT_FOUND
,
4942 _("The printer or class was not found."));
4950 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
4952 send_http_error(con
, status
);
4957 * Remove old jobs...
4960 cupsdCancelJobs(dest
, NULL
, 1);
4963 * Remove old subscriptions and send a "deleted printer" event...
4966 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
4967 "%s \"%s\" deleted by \"%s\".",
4968 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
4969 dest
, get_username(con
));
4971 cupsdExpireSubscriptions(printer
, NULL
);
4974 * Remove any old PPD or script files...
4977 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
, dest
);
4980 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
, dest
);
4983 if (dtype
& CUPS_PRINTER_CLASS
)
4985 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".", dest
,
4988 cupsdDeletePrinter(printer
, 0);
4989 cupsdSaveAllClasses();
4993 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".", dest
,
4996 cupsdDeletePrinter(printer
, 0);
4997 cupsdSaveAllPrinters();
5000 cupsdWritePrintcap();
5003 * Return with no errors...
5006 con
->response
->request
.status
.status_code
= IPP_OK
;
5011 * 'get_default()' - Get the default destination.
5015 get_default(cupsd_client_t
*con
) /* I - Client connection */
5017 http_status_t status
; /* Policy status */
5018 cups_array_t
*ra
; /* Requested attributes array */
5021 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5027 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5029 send_http_error(con
, status
);
5035 ra
= create_requested_array(con
->request
);
5037 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5039 cupsArrayDelete(ra
);
5041 con
->response
->request
.status
.status_code
= IPP_OK
;
5044 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5049 * 'get_devices()' - Get the list of available devices on the local system.
5053 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5055 http_status_t status
; /* Policy status */
5056 int i
; /* Looping var */
5057 ipp_attribute_t
*limit
, /* Limit attribute */
5058 *requested
; /* requested-attributes attribute */
5059 char command
[1024], /* cups-deviced command */
5060 options
[1024], /* Options to pass to command */
5061 attrs
[1024], /* String for requested attributes */
5062 *aptr
; /* Pointer into string */
5063 int alen
; /* Length of attribute value */
5066 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5072 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5074 send_http_error(con
, status
);
5079 * Run cups-deviced command with the given options...
5082 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5083 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5088 for (i
= 0, aptr
= attrs
; i
< requested
->num_values
; i
++)
5091 * Check that we have enough room...
5094 alen
= strlen(requested
->values
[i
].string
.text
);
5095 if (alen
> (sizeof(attrs
) - (aptr
- attrs
) - 2))
5099 * Put commas between values...
5106 * Add the value to the end of the string...
5109 strcpy(aptr
, requested
->values
[i
].string
.text
);
5114 * If we have more attribute names than will fit, default to "all"...
5117 if (i
< requested
->num_values
)
5118 strcpy(attrs
, "all");
5121 strcpy(attrs
, "all");
5123 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5124 snprintf(options
, sizeof(options
),
5125 "%d+%d+%d+requested-attributes=%s",
5126 con
->request
->request
.op
.request_id
,
5127 limit
? limit
->values
[0].integer
: 0, User
,
5130 if (cupsdSendCommand(con
, command
, options
, 1))
5133 * Command started successfully, don't send an IPP response here...
5136 ippDelete(con
->response
);
5137 con
->response
= NULL
;
5142 * Command failed, return "internal error" so the user knows something
5146 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5147 _("cups-deviced failed to execute."));
5153 * 'get_job_attrs()' - Get job attributes.
5157 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5158 ipp_attribute_t
*uri
) /* I - Job URI */
5160 http_status_t status
; /* Policy status */
5161 ipp_attribute_t
*attr
; /* Current attribute */
5162 int jobid
; /* Job ID */
5163 cupsd_job_t
*job
; /* Current job */
5164 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5165 username
[HTTP_MAX_URI
], /* Username portion of URI */
5166 host
[HTTP_MAX_URI
], /* Host portion of URI */
5167 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5168 int port
; /* Port portion of URI */
5169 cups_array_t
*ra
; /* Requested attributes array */
5172 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5173 con
->http
.fd
, uri
->values
[0].string
.text
);
5176 * See if we have a job URI or a printer URI...
5179 if (!strcmp(uri
->name
, "printer-uri"))
5182 * Got a printer URI; see if we also have a job-id attribute...
5185 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5186 IPP_TAG_INTEGER
)) == NULL
)
5188 send_ipp_status(con
, IPP_BAD_REQUEST
,
5189 _("Got a printer-uri attribute but no job-id!"));
5193 jobid
= attr
->values
[0].integer
;
5198 * Got a job URI; parse it to get the job ID...
5201 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5202 sizeof(method
), username
, sizeof(username
), host
,
5203 sizeof(host
), &port
, resource
, sizeof(resource
));
5205 if (strncmp(resource
, "/jobs/", 6))
5211 send_ipp_status(con
, IPP_BAD_REQUEST
,
5212 _("Bad job-uri attribute \"%s\"!"),
5213 uri
->values
[0].string
.text
);
5217 jobid
= atoi(resource
+ 6);
5221 * See if the job exists...
5224 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5227 * Nope - return a "not found" error...
5230 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5238 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5240 send_http_error(con
, status
);
5245 * Copy attributes...
5250 ra
= create_requested_array(con
->request
);
5251 copy_job_attrs(con
, job
, ra
);
5252 cupsArrayDelete(ra
);
5254 con
->response
->request
.status
.status_code
= IPP_OK
;
5259 * 'get_jobs()' - Get a list of jobs for the specified printer.
5263 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
5264 ipp_attribute_t
*uri
) /* I - Printer URI */
5266 http_status_t status
; /* Policy status */
5267 ipp_attribute_t
*attr
; /* Current attribute */
5268 const char *dest
; /* Destination */
5269 cups_ptype_t dtype
; /* Destination type (printer or class) */
5270 cups_ptype_t dmask
; /* Destination type mask */
5271 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5272 username
[HTTP_MAX_URI
], /* Username portion of URI */
5273 host
[HTTP_MAX_URI
], /* Host portion of URI */
5274 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5275 int port
; /* Port portion of URI */
5276 int completed
; /* Completed jobs? */
5277 int first_job_id
; /* First job ID */
5278 int limit
; /* Maximum number of jobs to return */
5279 int count
; /* Number of jobs that match */
5280 cupsd_job_t
*job
; /* Current job pointer */
5281 cupsd_printer_t
*printer
; /* Printer */
5282 cups_array_t
*list
; /* Which job list... */
5283 cups_array_t
*ra
; /* Requested attributes array */
5286 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
5287 uri
->values
[0].string
.text
);
5290 * Is the destination valid?
5293 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5294 sizeof(method
), username
, sizeof(username
), host
,
5295 sizeof(host
), &port
, resource
, sizeof(resource
));
5297 if (!strcmp(resource
, "/") ||
5298 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
5301 dtype
= (cups_ptype_t
)0;
5302 dmask
= (cups_ptype_t
)0;
5305 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5308 dtype
= (cups_ptype_t
)0;
5309 dmask
= CUPS_PRINTER_CLASS
;
5312 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5315 dtype
= CUPS_PRINTER_CLASS
;
5316 dmask
= CUPS_PRINTER_CLASS
;
5319 else if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
5325 send_ipp_status(con
, IPP_NOT_FOUND
,
5326 _("The printer or class was not found."));
5330 dmask
= CUPS_PRINTER_CLASS
;
5338 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
5340 send_http_error(con
, status
);
5344 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5346 send_http_error(con
, status
);
5351 * See if the "which-jobs" attribute have been specified...
5354 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
5355 IPP_TAG_KEYWORD
)) != NULL
&&
5356 !strcmp(attr
->values
[0].string
.text
, "completed"))
5361 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
5373 * See if they want to limit the number of jobs reported...
5376 if ((attr
= ippFindAttribute(con
->request
, "limit",
5377 IPP_TAG_INTEGER
)) != NULL
)
5378 limit
= attr
->values
[0].integer
;
5382 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
5383 IPP_TAG_INTEGER
)) != NULL
)
5384 first_job_id
= attr
->values
[0].integer
;
5389 * See if we only want to see jobs for a specific user...
5392 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
5393 IPP_TAG_BOOLEAN
)) != NULL
&&
5394 attr
->values
[0].boolean
)
5395 strlcpy(username
, get_username(con
), sizeof(username
));
5399 ra
= create_requested_array(con
->request
);
5402 * OK, build a list of jobs for this printer...
5405 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
5406 count
< limit
&& job
;
5407 job
= (cupsd_job_t
*)cupsArrayNext(list
))
5410 * Filter out jobs that don't match...
5413 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
5415 if ((dest
&& strcmp(job
->dest
, dest
)) &&
5416 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
5418 if ((job
->dtype
& dmask
) != dtype
&&
5419 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
5421 if (username
[0] && strcasecmp(username
, job
->username
))
5424 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
5427 if (job
->id
< first_job_id
)
5436 ippAddSeparator(con
->response
);
5440 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
5442 copy_job_attrs(con
, job
, ra
);
5445 cupsArrayDelete(ra
);
5447 con
->response
->request
.status
.status_code
= IPP_OK
;
5452 * 'get_notifications()' - Get events for a subscription.
5456 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
5458 int i
, j
; /* Looping vars */
5459 http_status_t status
; /* Policy status */
5460 cupsd_subscription_t
*sub
; /* Subscription */
5461 ipp_attribute_t
*ids
, /* notify-subscription-ids */
5462 *sequences
; /* notify-sequence-numbers */
5463 int min_seq
; /* Minimum sequence number */
5464 int interval
; /* Poll interval */
5467 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_subscription_attrs(con=%p[%d])",
5471 * Get subscription attributes...
5474 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
5476 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
5481 send_ipp_status(con
, IPP_BAD_REQUEST
,
5482 _("Missing notify-subscription-ids attribute!"));
5487 * Are the subscription IDs valid?
5490 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
5492 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
5495 * Bad subscription ID...
5498 send_ipp_status(con
, IPP_NOT_FOUND
,
5499 _("notify-subscription-id %d no good!"),
5500 ids
->values
[i
].integer
);
5508 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5510 con
, sub
->owner
)) != HTTP_OK
)
5512 send_http_error(con
, status
);
5517 * Check the subscription type and update the interval accordingly.
5520 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
5523 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
5525 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
5531 * Tell the client to poll again in N seconds...
5535 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5536 "notify-get-interval", interval
);
5538 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5539 "printer-up-time", time(NULL
));
5542 * Copy the subscription event attributes to the response.
5545 con
->response
->request
.status
.status_code
=
5546 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
5548 for (i
= 0; i
< ids
->num_values
; i
++)
5551 * Get the subscription and sequence number...
5554 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
5556 if (sequences
&& i
< sequences
->num_values
)
5557 min_seq
= sequences
->values
[i
].integer
;
5562 * If we don't have any new events, nothing to do here...
5565 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
5569 * Otherwise copy all of the new events...
5572 if (sub
->first_event_id
> min_seq
)
5575 j
= min_seq
- sub
->first_event_id
;
5577 for (; j
< sub
->num_events
; j
++)
5579 ippAddSeparator(con
->response
);
5581 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
5582 IPP_TAG_EVENT_NOTIFICATION
, 0);
5589 * 'get_ppds()' - Get the list of PPD files on the local system.
5593 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
5595 http_status_t status
; /* Policy status */
5596 int i
; /* Looping var */
5597 ipp_attribute_t
*limit
, /* Limit attribute */
5598 *make
, /* ppd-make attribute */
5599 *requested
; /* requested-attributes attribute */
5600 char command
[1024], /* cups-deviced command */
5601 options
[1024], /* Options to pass to command */
5602 attrs
[1024], /* String for requested attributes */
5603 *aptr
; /* Pointer into string */
5604 int alen
; /* Length of attribute value */
5607 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
5613 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5615 send_http_error(con
, status
);
5620 * Run cups-driverd command with the given options...
5623 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5624 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
5625 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5630 for (i
= 0, aptr
= attrs
; i
< requested
->num_values
; i
++)
5633 * Check that we have enough room...
5636 alen
= strlen(requested
->values
[i
].string
.text
);
5637 if (alen
> (sizeof(attrs
) - (aptr
- attrs
) - 2))
5641 * Put commas between values...
5648 * Add the value to the end of the string...
5651 strcpy(aptr
, requested
->values
[i
].string
.text
);
5656 * If we have more attribute names than will fit, default to "all"...
5659 if (i
< requested
->num_values
)
5660 strcpy(attrs
, "all");
5663 strcpy(attrs
, "all");
5665 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
5666 snprintf(options
, sizeof(options
),
5667 "list+%d+%d+requested-attributes=%s%s%s",
5668 con
->request
->request
.op
.request_id
,
5669 limit
? limit
->values
[0].integer
: 0,
5671 make
? "%20ppd-make=" : "",
5672 make
? make
->values
[0].string
.text
: "");
5674 if (cupsdSendCommand(con
, command
, options
, 0))
5677 * Command started successfully, don't send an IPP response here...
5680 ippDelete(con
->response
);
5681 con
->response
= NULL
;
5686 * Command failed, return "internal error" so the user knows something
5690 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5691 _("cups-driverd failed to execute."));
5697 * 'get_printer_attrs()' - Get printer attributes.
5701 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
5702 ipp_attribute_t
*uri
) /* I - Printer URI */
5704 http_status_t status
; /* Policy status */
5705 const char *dest
; /* Destination */
5706 cups_ptype_t dtype
; /* Destination type (printer or class) */
5707 char method
[HTTP_MAX_URI
],
5708 /* Method portion of URI */
5709 username
[HTTP_MAX_URI
],
5710 /* Username portion of URI */
5712 /* Host portion of URI */
5713 resource
[HTTP_MAX_URI
];
5714 /* Resource portion of URI */
5715 int port
; /* Port portion of URI */
5716 cupsd_printer_t
*printer
; /* Printer/class */
5717 cups_array_t
*ra
; /* Requested attributes array */
5720 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
5721 con
->http
.fd
, uri
->values
[0].string
.text
);
5724 * Is the destination valid?
5727 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5728 sizeof(method
), username
, sizeof(username
), host
,
5729 sizeof(host
), &port
, resource
, sizeof(resource
));
5731 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
5737 send_ipp_status(con
, IPP_NOT_FOUND
,
5738 _("The printer or class was not found."));
5746 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
5748 send_http_error(con
, status
);
5753 * Send the attributes...
5756 ra
= create_requested_array(con
->request
);
5758 copy_printer_attrs(con
, printer
, ra
);
5760 cupsArrayDelete(ra
);
5762 con
->response
->request
.status
.status_code
= IPP_OK
;
5767 * 'get_printers()' - Get a list of printers or classes.
5771 get_printers(cupsd_client_t
*con
, /* I - Client connection */
5772 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
5774 http_status_t status
; /* Policy status */
5775 ipp_attribute_t
*attr
; /* Current attribute */
5776 int limit
; /* Maximum number of printers to return */
5777 int count
; /* Number of printers that match */
5778 cupsd_printer_t
*printer
; /* Current printer pointer */
5779 int printer_type
, /* printer-type attribute */
5780 printer_mask
; /* printer-type-mask attribute */
5781 char *location
; /* Location string */
5782 const char *username
; /* Current user */
5783 char *first_printer_name
; /* first-printer-name attribute */
5784 cups_array_t
*ra
; /* Requested attributes array */
5787 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
5788 con
->http
.fd
, type
);
5794 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5796 send_http_error(con
, status
);
5801 * Check for printers...
5804 if (!Printers
|| !cupsArrayCount(Printers
))
5806 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
5811 * See if they want to limit the number of printers reported...
5814 if ((attr
= ippFindAttribute(con
->request
, "limit",
5815 IPP_TAG_INTEGER
)) != NULL
)
5816 limit
= attr
->values
[0].integer
;
5820 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
5821 IPP_TAG_NAME
)) != NULL
)
5822 first_printer_name
= attr
->values
[0].string
.text
;
5824 first_printer_name
= NULL
;
5827 * Support filtering...
5830 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
5831 IPP_TAG_ENUM
)) != NULL
)
5832 printer_type
= attr
->values
[0].integer
;
5836 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
5837 IPP_TAG_ENUM
)) != NULL
)
5838 printer_mask
= attr
->values
[0].integer
;
5842 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
5843 IPP_TAG_TEXT
)) != NULL
)
5844 location
= attr
->values
[0].string
.text
;
5848 if (con
->username
[0])
5849 username
= con
->username
;
5850 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
5851 IPP_TAG_NAME
)) != NULL
)
5852 username
= attr
->values
[0].string
.text
;
5856 ra
= create_requested_array(con
->request
);
5859 * OK, build a list of printers for this printer...
5862 if (first_printer_name
)
5864 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
5865 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5868 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5871 count
< limit
&& printer
;
5872 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
5874 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
5875 (printer
->type
& printer_mask
) == printer_type
&&
5876 (!location
|| !printer
->location
||
5877 !strcasecmp(printer
->location
, location
)))
5880 * If HideImplicitMembers is enabled, see if this printer or class
5881 * is a member of an implicit class...
5884 if (ImplicitClasses
&& HideImplicitMembers
&&
5885 printer
->in_implicit_class
)
5889 * If a username is specified, see if it is allowed or denied
5893 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
5897 * Add the group separator as needed...
5901 ippAddSeparator(con
->response
);
5906 * Send the attributes...
5909 copy_printer_attrs(con
, printer
, ra
);
5913 cupsArrayDelete(ra
);
5915 con
->response
->request
.status
.status_code
= IPP_OK
;
5920 * 'get_subscription_attrs()' - Get subscription attributes.
5924 get_subscription_attrs(
5925 cupsd_client_t
*con
, /* I - Client connection */
5926 int sub_id
) /* I - Subscription ID */
5928 http_status_t status
; /* Policy status */
5929 cupsd_subscription_t
*sub
; /* Subscription */
5930 cups_array_t
*ra
; /* Requested attributes array */
5933 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5934 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
5935 con
, con
->http
.fd
, sub_id
);
5938 * Is the subscription ID valid?
5941 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
5944 * Bad subscription ID...
5947 send_ipp_status(con
, IPP_NOT_FOUND
,
5948 _("notify-subscription-id %d no good!"), sub_id
);
5956 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5958 con
, sub
->owner
)) != HTTP_OK
)
5960 send_http_error(con
, status
);
5965 * Copy the subscription attributes to the response using the
5966 * requested-attributes attribute that may be provided by the client.
5969 ra
= create_requested_array(con
->request
);
5971 copy_subscription_attrs(con
, sub
, ra
);
5973 cupsArrayDelete(ra
);
5975 con
->response
->request
.status
.status_code
= IPP_OK
;
5980 * 'get_subscriptions()' - Get subscriptions.
5984 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
5985 ipp_attribute_t
*uri
) /* I - Printer/job URI */
5987 http_status_t status
; /* Policy status */
5988 int count
; /* Number of subscriptions */
5989 int limit
; /* Limit */
5990 cupsd_subscription_t
*sub
; /* Subscription */
5991 cups_array_t
*ra
; /* Requested attributes array */
5992 ipp_attribute_t
*attr
; /* Attribute */
5993 cups_ptype_t dtype
; /* Destination type (printer or class) */
5994 char method
[HTTP_MAX_URI
],
5995 /* Method portion of URI */
5996 username
[HTTP_MAX_URI
],
5997 /* Username portion of URI */
5999 /* Host portion of URI */
6000 resource
[HTTP_MAX_URI
];
6001 /* Resource portion of URI */
6002 int port
; /* Port portion of URI */
6003 cupsd_job_t
*job
; /* Job pointer */
6004 cupsd_printer_t
*printer
; /* Printer */
6007 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6008 "get_subscriptions(con=%p[%d], uri=%s)",
6009 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
6012 * Is the destination valid?
6015 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6016 sizeof(method
), username
, sizeof(username
), host
,
6017 sizeof(host
), &port
, resource
, sizeof(resource
));
6019 if (!strcmp(resource
, "/") ||
6020 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
6021 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
6022 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
6027 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
6030 job
= cupsdFindJob(atoi(resource
+ 6));
6034 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
6039 else if (!cupsdValidateDest(host
, resource
, &dtype
, &printer
))
6045 send_ipp_status(con
, IPP_NOT_FOUND
,
6046 _("The printer or class was not found."));
6049 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
6050 IPP_TAG_INTEGER
)) != NULL
)
6052 job
= cupsdFindJob(attr
->values
[0].integer
);
6056 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
6057 attr
->values
[0].integer
);
6068 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
6070 con
, NULL
)) != HTTP_OK
)
6072 send_http_error(con
, status
);
6077 * Copy the subscription attributes to the response using the
6078 * requested-attributes attribute that may be provided by the client.
6081 ra
= create_requested_array(con
->request
);
6083 if ((attr
= ippFindAttribute(con
->request
, "limit",
6084 IPP_TAG_INTEGER
)) != NULL
)
6085 limit
= attr
->values
[0].integer
;
6090 * See if we only want to see subscriptions for a specific user...
6093 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
6094 IPP_TAG_BOOLEAN
)) != NULL
&&
6095 attr
->values
[0].boolean
)
6096 strlcpy(username
, get_username(con
), sizeof(username
));
6100 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
6102 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
6103 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
6104 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
6106 ippAddSeparator(con
->response
);
6107 copy_subscription_attrs(con
, sub
, ra
);
6110 if (limit
&& count
>= limit
)
6114 cupsArrayDelete(ra
);
6117 con
->response
->request
.status
.status_code
= IPP_OK
;
6119 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
6124 * 'get_username()' - Get the username associated with a request.
6127 static const char * /* O - Username */
6128 get_username(cupsd_client_t
*con
) /* I - Connection */
6130 ipp_attribute_t
*attr
; /* Attribute */
6133 if (con
->username
[0])
6134 return (con
->username
);
6135 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6136 IPP_TAG_NAME
)) != NULL
)
6137 return (attr
->values
[0].string
.text
);
6139 return ("anonymous");
6144 * 'hold_job()' - Hold a print job.
6148 hold_job(cupsd_client_t
*con
, /* I - Client connection */
6149 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
6151 ipp_attribute_t
*attr
, /* Current job-hold-until */
6152 *newattr
; /* New job-hold-until */
6153 int jobid
; /* Job ID */
6154 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6155 username
[HTTP_MAX_URI
], /* Username portion of URI */
6156 host
[HTTP_MAX_URI
], /* Host portion of URI */
6157 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6158 int port
; /* Port portion of URI */
6159 cupsd_job_t
*job
; /* Job information */
6162 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
6163 uri
->values
[0].string
.text
);
6166 * See if we have a job URI or a printer URI...
6169 if (!strcmp(uri
->name
, "printer-uri"))
6172 * Got a printer URI; see if we also have a job-id attribute...
6175 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6176 IPP_TAG_INTEGER
)) == NULL
)
6178 send_ipp_status(con
, IPP_BAD_REQUEST
,
6179 _("Got a printer-uri attribute but no job-id!"));
6183 jobid
= attr
->values
[0].integer
;
6188 * Got a job URI; parse it to get the job ID...
6191 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6192 sizeof(method
), username
, sizeof(username
), host
,
6193 sizeof(host
), &port
, resource
, sizeof(resource
));
6195 if (strncmp(resource
, "/jobs/", 6))
6201 send_ipp_status(con
, IPP_BAD_REQUEST
,
6202 _("Bad job-uri attribute \"%s\"!"),
6203 uri
->values
[0].string
.text
);
6207 jobid
= atoi(resource
+ 6);
6211 * See if the job exists...
6214 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6217 * Nope - return a "not found" error...
6220 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6225 * See if the job is owned by the requesting user...
6228 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6230 send_http_error(con
, HTTP_UNAUTHORIZED
);
6235 * Hold the job and return...
6240 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
6241 IPP_TAG_KEYWORD
)) == NULL
)
6242 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
6244 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
6245 IPP_TAG_KEYWORD
)) == NULL
)
6246 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
6251 * Free the old hold value and copy the new one over...
6254 _cupsStrFree(attr
->values
[0].string
.text
);
6258 attr
->value_tag
= newattr
->value_tag
;
6259 attr
->values
[0].string
.text
=
6260 _cupsStrAlloc(newattr
->values
[0].string
.text
);
6264 attr
->value_tag
= IPP_TAG_KEYWORD
;
6265 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
6269 * Hold job until specified time...
6272 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
6275 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was held by \"%s\".", jobid
,
6278 con
->response
->request
.status
.status_code
= IPP_OK
;
6283 * 'move_job()' - Move a job to a new destination.
6287 move_job(cupsd_client_t
*con
, /* I - Client connection */
6288 ipp_attribute_t
*uri
) /* I - Job URI */
6290 http_status_t status
; /* Policy status */
6291 ipp_attribute_t
*attr
; /* Current attribute */
6292 int jobid
; /* Job ID */
6293 cupsd_job_t
*job
; /* Current job */
6294 const char *src
, /* Source printer/class */
6295 *dest
; /* Destination */
6296 cups_ptype_t stype
, /* Source type (printer or class) */
6297 dtype
; /* Destination type (printer or class) */
6298 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6299 username
[HTTP_MAX_URI
], /* Username portion of URI */
6300 host
[HTTP_MAX_URI
], /* Host portion of URI */
6301 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6302 int port
; /* Port portion of URI */
6303 cupsd_printer_t
*sprinter
, /* Source printer */
6304 *dprinter
; /* Destination printer */
6307 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
6308 uri
->values
[0].string
.text
);
6311 * Get the new printer or class...
6314 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
6315 IPP_TAG_URI
)) == NULL
)
6318 * Need job-printer-uri...
6321 send_ipp_status(con
, IPP_BAD_REQUEST
,
6322 _("job-printer-uri attribute missing!"));
6326 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, method
,
6327 sizeof(method
), username
, sizeof(username
), host
,
6328 sizeof(host
), &port
, resource
, sizeof(resource
));
6330 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &dprinter
)) == NULL
)
6336 send_ipp_status(con
, IPP_NOT_FOUND
,
6337 _("The printer or class was not found."));
6345 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6347 send_http_error(con
, status
);
6352 * See if we have a job URI or a printer URI...
6355 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6356 sizeof(method
), username
, sizeof(username
), host
,
6357 sizeof(host
), &port
, resource
, sizeof(resource
));
6359 if (!strcmp(uri
->name
, "printer-uri"))
6362 * Got a printer URI; see if we also have a job-id attribute...
6365 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6366 IPP_TAG_INTEGER
)) == NULL
)
6372 if ((src
= cupsdValidateDest(host
, resource
, &stype
, &sprinter
)) == NULL
)
6378 send_ipp_status(con
, IPP_NOT_FOUND
,
6379 _("The printer or class was not found."));
6388 * Otherwise, just move a single job...
6391 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
6394 * Nope - return a "not found" error...
6397 send_ipp_status(con
, IPP_NOT_FOUND
,
6398 _("Job #%d does not exist!"), attr
->values
[0].integer
);
6404 * Job found, initialize source pointers...
6415 * Got a job URI; parse it to get the job ID...
6418 if (strncmp(resource
, "/jobs/", 6))
6424 send_ipp_status(con
, IPP_BAD_REQUEST
,
6425 _("Bad job-uri attribute \"%s\"!"),
6426 uri
->values
[0].string
.text
);
6431 * See if the job exists...
6434 jobid
= atoi(resource
+ 6);
6436 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6439 * Nope - return a "not found" error...
6442 send_ipp_status(con
, IPP_NOT_FOUND
,
6443 _("Job #%d does not exist!"), jobid
);
6449 * Job found, initialize source pointers...
6458 * Now move the job or jobs...
6464 * See if the job has been completed...
6467 if (job
->state_value
> IPP_JOB_STOPPED
)
6470 * Return a "not-possible" error...
6473 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6474 _("Job #%d is finished and cannot be altered!"),
6480 * See if the job is owned by the requesting user...
6483 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6485 send_http_error(con
, HTTP_UNAUTHORIZED
);
6490 * Move the job to a different printer or class...
6493 cupsdMoveJob(job
, dest
);
6498 * Got the source printer, now look through the jobs...
6501 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
6503 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
6506 * See if the job is pointing at the source printer or has not been
6510 if (strcasecmp(job
->dest
, src
) ||
6511 job
->state_value
> IPP_JOB_STOPPED
)
6515 * See if the job can be moved by the requesting user...
6518 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6522 * Move the job to a different printer or class...
6525 cupsdMoveJob(job
, dest
);
6530 * Start jobs if possible...
6536 * Return with "everything is OK" status...
6539 con
->response
->request
.status
.status_code
= IPP_OK
;
6544 * 'ppd_add_default()' - Add a PPD default choice.
6547 static int /* O - Number of defaults */
6549 const char *option
, /* I - Option name */
6550 const char *choice
, /* I - Choice name */
6551 int num_defaults
, /* I - Number of defaults */
6552 ppd_default_t
**defaults
) /* IO - Defaults */
6554 int i
; /* Looping var */
6555 ppd_default_t
*temp
; /* Temporary defaults array */
6559 * First check if the option already has a default value; the PPD spec
6560 * says that the first one is used...
6563 for (i
= 0, temp
= *defaults
; i
< num_defaults
; i
++)
6564 if (!strcmp(option
, temp
[i
].option
))
6565 return (num_defaults
);
6568 * Now add the option...
6571 if (num_defaults
== 0)
6572 temp
= malloc(sizeof(ppd_default_t
));
6574 temp
= realloc(*defaults
, (num_defaults
+ 1) * sizeof(ppd_default_t
));
6578 cupsdLogMessage(CUPSD_LOG_ERROR
, "ppd_add_default: Unable to add default value for \"%s\" - %s",
6579 option
, strerror(errno
));
6580 return (num_defaults
);
6584 temp
+= num_defaults
;
6586 strlcpy(temp
->option
, option
, sizeof(temp
->option
));
6587 strlcpy(temp
->choice
, choice
, sizeof(temp
->choice
));
6589 return (num_defaults
+ 1);
6594 * 'ppd_parse_line()' - Parse a PPD default line.
6597 static int /* O - 0 on success, -1 on failure */
6598 ppd_parse_line(const char *line
, /* I - Line */
6599 char *option
, /* O - Option name */
6600 int olen
, /* I - Size of option name */
6601 char *choice
, /* O - Choice name */
6602 int clen
) /* I - Size of choice name */
6605 * Verify this is a default option line...
6608 if (strncmp(line
, "*Default", 8))
6612 * Read the option name...
6615 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
6625 * Skip everything else up to the colon (:)...
6628 while (*line
&& *line
!= ':')
6637 * Now grab the option choice, skipping leading whitespace...
6640 while (isspace(*line
& 255))
6643 for (clen
--; isalnum(*line
& 255); line
++)
6653 * Return with no errors...
6661 * 'print_job()' - Print a file to a printer or class.
6665 print_job(cupsd_client_t
*con
, /* I - Client connection */
6666 ipp_attribute_t
*uri
) /* I - Printer URI */
6668 ipp_attribute_t
*attr
; /* Current attribute */
6669 ipp_attribute_t
*format
; /* Document-format attribute */
6670 cupsd_job_t
*job
; /* New job */
6671 char filename
[1024]; /* Job filename */
6672 mime_type_t
*filetype
; /* Type of file */
6673 char super
[MIME_MAX_SUPER
], /* Supertype of file */
6674 type
[MIME_MAX_TYPE
], /* Subtype of file */
6675 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
6676 /* Textual name of mime type */
6677 cupsd_printer_t
*printer
; /* Printer data */
6678 struct stat fileinfo
; /* File information */
6679 int kbytes
; /* Size of file */
6680 int compression
; /* Document compression */
6683 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
6684 uri
->values
[0].string
.text
);
6687 * Validate print file attributes, for now just document-format and
6688 * compression (CUPS only supports "none" and "gzip")...
6691 compression
= CUPS_FILE_NONE
;
6693 if ((attr
= ippFindAttribute(con
->request
, "compression",
6694 IPP_TAG_KEYWORD
)) != NULL
)
6696 if (strcmp(attr
->values
[0].string
.text
, "none")
6698 && strcmp(attr
->values
[0].string
.text
, "gzip")
6699 #endif /* HAVE_LIBZ */
6702 send_ipp_status(con
, IPP_ATTRIBUTES
,
6703 _("Unsupported compression \"%s\"!"),
6704 attr
->values
[0].string
.text
);
6705 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6706 "compression", NULL
, attr
->values
[0].string
.text
);
6711 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
6712 compression
= CUPS_FILE_GZIP
;
6713 #endif /* HAVE_LIBZ */
6717 * Do we have a file to print?
6722 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
6727 * Is it a format we support?
6730 if ((format
= ippFindAttribute(con
->request
, "document-format",
6731 IPP_TAG_MIMETYPE
)) != NULL
)
6734 * Grab format from client...
6737 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
6739 send_ipp_status(con
, IPP_BAD_REQUEST
,
6740 _("Could not scan type \"%s\"!"),
6741 format
->values
[0].string
.text
);
6748 * No document format attribute? Auto-type it!
6751 strcpy(super
, "application");
6752 strcpy(type
, "octet-stream");
6755 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
6758 * Auto-type the file...
6761 ipp_attribute_t
*doc_name
; /* document-name attribute */
6764 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
6766 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
6767 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
6768 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
6774 * Replace the document-format attribute value with the auto-typed one.
6777 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
6782 _cupsStrFree(format
->values
[0].string
.text
);
6784 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
6787 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
6788 "document-format", NULL
, mimetype
);
6791 filetype
= mimeType(MimeDatabase
, super
, type
);
6794 filetype
= mimeType(MimeDatabase
, super
, type
);
6798 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
6799 _("Unsupported format \'%s/%s\'!"), super
, type
);
6800 cupsdLogMessage(CUPSD_LOG_INFO
,
6801 "Hint: Do you have the raw file printing rules enabled?");
6804 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
6805 "document-format", NULL
, format
->values
[0].string
.text
);
6810 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: request file type is %s/%s.",
6811 filetype
->super
, filetype
->type
);
6814 * Read any embedded job ticket info from PS files...
6817 if (!strcasecmp(filetype
->super
, "application") &&
6818 !strcasecmp(filetype
->type
, "postscript"))
6819 read_ps_job_ticket(con
);
6822 * Create the job object...
6825 if ((job
= add_job(con
, uri
, &printer
)) == NULL
)
6829 * Update quota data...
6832 if (stat(con
->filename
, &fileinfo
))
6835 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
6837 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6839 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
6840 IPP_TAG_INTEGER
)) != NULL
)
6841 attr
->values
[0].integer
+= kbytes
;
6844 * Add the job file...
6847 if (add_file(con
, job
, filetype
, compression
))
6850 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
6852 rename(con
->filename
, filename
);
6853 cupsdClearString(&con
->filename
);
6856 * See if we need to add the ending sheet...
6859 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
6860 attr
->num_values
> 1)
6866 cupsdLogMessage(CUPSD_LOG_INFO
, "Adding end banner page \"%s\" to job %d.",
6867 attr
->values
[1].string
.text
, job
->id
);
6869 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
6871 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6875 * Log and save the job...
6878 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d queued on \"%s\" by \"%s\".", job
->id
,
6879 job
->dest
, job
->username
);
6880 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Job %d hold_until = %d", job
->id
,
6881 (int)job
->hold_until
);
6886 * Start the job if possible...
6894 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
6896 * This function only gets called when printing a single PostScript
6897 * file using the Print-Job operation. It doesn't work for Create-Job +
6898 * Send-File, since the job attributes need to be set at job creation
6899 * time for banners to work. The embedded PS job ticket stuff is here
6900 * only to allow the Windows printer driver for CUPS to pass in JCL
6901 * options and IPP attributes which otherwise would be lost.
6903 * The format of a PS job ticket is simple:
6905 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
6907 * %cupsJobTicket: attr1=value1
6908 * %cupsJobTicket: attr2=value2
6910 * %cupsJobTicket: attrN=valueN
6912 * Job ticket lines must appear immediately after the first line that
6913 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
6914 * looking for job ticket info when it finds a line that does not begin
6915 * with "%cupsJobTicket:".
6917 * The maximum length of a job ticket line, including the prefix, is
6918 * 255 characters to conform with the Adobe DSC.
6920 * Read-only attributes are rejected with a notice to the error log in
6921 * case a malicious user tries anything. Since the job ticket is read
6922 * prior to attribute validation in print_job(), job ticket attributes
6923 * will go through the same validation as IPP attributes...
6927 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
6929 cups_file_t
*fp
; /* File to read from */
6930 char line
[256]; /* Line data */
6931 int num_options
; /* Number of options */
6932 cups_option_t
*options
; /* Options */
6933 ipp_t
*ticket
; /* New attributes */
6934 ipp_attribute_t
*attr
, /* Current attribute */
6935 *attr2
, /* Job attribute */
6936 *prev2
; /* Previous job attribute */
6940 * First open the print file...
6943 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
6945 cupsdLogMessage(CUPSD_LOG_ERROR
,
6946 "read_ps_job_ticket: Unable to open PostScript print file "
6953 * Skip the first line...
6956 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
6958 cupsdLogMessage(CUPSD_LOG_ERROR
,
6959 "read_ps_job_ticket: Unable to read from PostScript print "
6966 if (strncmp(line
, "%!PS-Adobe-", 11))
6969 * Not a DSC-compliant file, so no job ticket info will be available...
6977 * Read job ticket info from the file...
6983 while (cupsFileGets(fp
, line
, sizeof(line
)))
6986 * Stop at the first non-ticket line...
6989 if (strncmp(line
, "%cupsJobTicket:", 15))
6993 * Add the options to the option array...
6996 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
7000 * Done with the file; see if we have any options...
7005 if (num_options
== 0)
7009 * OK, convert the options to an attribute list, and apply them to
7014 cupsEncodeOptions(ticket
, num_options
, options
);
7017 * See what the user wants to change.
7020 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
7022 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
7025 if (!strcmp(attr
->name
, "job-originating-host-name") ||
7026 !strcmp(attr
->name
, "job-originating-user-name") ||
7027 !strcmp(attr
->name
, "job-media-sheets-completed") ||
7028 !strcmp(attr
->name
, "job-k-octets") ||
7029 !strcmp(attr
->name
, "job-id") ||
7030 !strncmp(attr
->name
, "job-state", 9) ||
7031 !strncmp(attr
->name
, "time-at-", 8))
7032 continue; /* Read-only attrs */
7034 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
7035 IPP_TAG_ZERO
)) != NULL
)
7038 * Some other value; first free the old value...
7041 if (con
->request
->attrs
== attr2
)
7043 con
->request
->attrs
= attr2
->next
;
7048 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
7049 if (prev2
->next
== attr2
)
7051 prev2
->next
= attr2
->next
;
7056 if (con
->request
->last
== attr2
)
7057 con
->request
->last
= prev2
;
7059 _ippFreeAttr(attr2
);
7063 * Add new option by copying it...
7066 copy_attribute(con
->request
, attr
, 0);
7070 * Then free the attribute list and option array...
7074 cupsFreeOptions(num_options
, options
);
7079 * 'reject_jobs()' - Reject print jobs to a printer.
7083 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
7084 ipp_attribute_t
*uri
) /* I - Printer or class URI */
7086 http_status_t status
; /* Policy status */
7087 cups_ptype_t dtype
; /* Destination type (printer or class) */
7088 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7089 username
[HTTP_MAX_URI
], /* Username portion of URI */
7090 host
[HTTP_MAX_URI
], /* Host portion of URI */
7091 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7092 int port
; /* Port portion of URI */
7093 const char *name
; /* Printer name */
7094 cupsd_printer_t
*printer
; /* Printer data */
7095 ipp_attribute_t
*attr
; /* printer-state-message text */
7098 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
7099 con
->http
.fd
, uri
->values
[0].string
.text
);
7102 * Is the destination valid?
7105 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7106 sizeof(method
), username
, sizeof(username
), host
,
7107 sizeof(host
), &port
, resource
, sizeof(resource
));
7109 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
7115 send_ipp_status(con
, IPP_NOT_FOUND
,
7116 _("The printer or class was not found."));
7124 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7126 send_http_error(con
, status
);
7131 * Reject jobs sent to the printer...
7134 printer
->accepting
= 0;
7136 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
7137 IPP_TAG_TEXT
)) == NULL
)
7138 strcpy(printer
->state_message
, "Rejecting Jobs");
7140 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
7141 sizeof(printer
->state_message
));
7143 cupsdAddPrinterHistory(printer
);
7145 if (dtype
& CUPS_PRINTER_CLASS
)
7147 cupsdSaveAllClasses();
7149 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
7150 name
, get_username(con
));
7154 cupsdSaveAllPrinters();
7156 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
7157 name
, get_username(con
));
7161 * Everything was ok, so return OK status...
7164 con
->response
->request
.status
.status_code
= IPP_OK
;
7169 * 'release_job()' - Release a held print job.
7173 release_job(cupsd_client_t
*con
, /* I - Client connection */
7174 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7176 ipp_attribute_t
*attr
; /* Current attribute */
7177 int jobid
; /* Job ID */
7178 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7179 username
[HTTP_MAX_URI
], /* Username portion of URI */
7180 host
[HTTP_MAX_URI
], /* Host portion of URI */
7181 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7182 int port
; /* Port portion of URI */
7183 cupsd_job_t
*job
; /* Job information */
7186 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
7187 con
->http
.fd
, uri
->values
[0].string
.text
);
7190 * See if we have a job URI or a printer URI...
7193 if (!strcmp(uri
->name
, "printer-uri"))
7196 * Got a printer URI; see if we also have a job-id attribute...
7199 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7200 IPP_TAG_INTEGER
)) == NULL
)
7202 send_ipp_status(con
, IPP_BAD_REQUEST
,
7203 _("Got a printer-uri attribute but no job-id!"));
7207 jobid
= attr
->values
[0].integer
;
7212 * Got a job URI; parse it to get the job ID...
7215 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7216 sizeof(method
), username
, sizeof(username
), host
,
7217 sizeof(host
), &port
, resource
, sizeof(resource
));
7219 if (strncmp(resource
, "/jobs/", 6))
7225 send_ipp_status(con
, IPP_BAD_REQUEST
,
7226 _("Bad job-uri attribute \"%s\"!"),
7227 uri
->values
[0].string
.text
);
7231 jobid
= atoi(resource
+ 6);
7235 * See if the job exists...
7238 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7241 * Nope - return a "not found" error...
7244 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7249 * See if job is "held"...
7252 if (job
->state_value
!= IPP_JOB_HELD
)
7255 * Nope - return a "not possible" error...
7258 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
7263 * See if the job is owned by the requesting user...
7266 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7268 send_http_error(con
, HTTP_UNAUTHORIZED
);
7273 * Reset the job-hold-until value to "no-hold"...
7276 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7277 IPP_TAG_KEYWORD
)) == NULL
)
7278 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7282 _cupsStrFree(attr
->values
[0].string
.text
);
7284 attr
->value_tag
= IPP_TAG_KEYWORD
;
7285 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
7289 * Release the job and return...
7292 cupsdReleaseJob(job
);
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
[1024]; /* 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...
7604 * 'send_document()' - Send a file to a printer or class.
7608 send_document(cupsd_client_t
*con
, /* I - Client connection */
7609 ipp_attribute_t
*uri
) /* I - Printer URI */
7611 ipp_attribute_t
*attr
; /* Current attribute */
7612 ipp_attribute_t
*format
; /* Document-format attribute */
7613 int jobid
; /* Job ID number */
7614 cupsd_job_t
*job
; /* Current job */
7615 char job_uri
[HTTP_MAX_URI
],
7617 method
[HTTP_MAX_URI
],
7618 /* Method portion of URI */
7619 username
[HTTP_MAX_URI
],
7620 /* Username portion of URI */
7622 /* Host portion of URI */
7623 resource
[HTTP_MAX_URI
];
7624 /* Resource portion of URI */
7625 int port
; /* Port portion of URI */
7626 mime_type_t
*filetype
; /* Type of file */
7627 char super
[MIME_MAX_SUPER
],
7628 /* Supertype of file */
7629 type
[MIME_MAX_TYPE
],
7630 /* Subtype of file */
7631 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7632 /* Textual name of mime type */
7633 char filename
[1024]; /* Job filename */
7634 cupsd_printer_t
*printer
; /* Current printer */
7635 struct stat fileinfo
; /* File information */
7636 int kbytes
; /* Size of file */
7637 int compression
; /* Type of compression */
7640 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
7641 con
->http
.fd
, uri
->values
[0].string
.text
);
7644 * See if we have a job URI or a printer URI...
7647 if (!strcmp(uri
->name
, "printer-uri"))
7650 * Got a printer URI; see if we also have a job-id attribute...
7653 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7654 IPP_TAG_INTEGER
)) == NULL
)
7656 send_ipp_status(con
, IPP_BAD_REQUEST
,
7657 _("Got a printer-uri attribute but no job-id!"));
7661 jobid
= attr
->values
[0].integer
;
7666 * Got a job URI; parse it to get the job ID...
7669 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7670 sizeof(method
), username
, sizeof(username
), host
,
7671 sizeof(host
), &port
, resource
, sizeof(resource
));
7673 if (strncmp(resource
, "/jobs/", 6))
7679 send_ipp_status(con
, IPP_BAD_REQUEST
,
7680 _("Bad job-uri attribute \"%s\"!"),
7681 uri
->values
[0].string
.text
);
7685 jobid
= atoi(resource
+ 6);
7689 * See if the job exists...
7692 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7695 * Nope - return a "not found" error...
7698 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7703 * See if the job is owned by the requesting user...
7706 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7708 send_http_error(con
, HTTP_UNAUTHORIZED
);
7713 * OK, see if the client is sending the document compressed - CUPS
7714 * only supports "none" and "gzip".
7717 compression
= CUPS_FILE_NONE
;
7719 if ((attr
= ippFindAttribute(con
->request
, "compression",
7720 IPP_TAG_KEYWORD
)) != NULL
)
7722 if (strcmp(attr
->values
[0].string
.text
, "none")
7724 && strcmp(attr
->values
[0].string
.text
, "gzip")
7725 #endif /* HAVE_LIBZ */
7728 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
7729 attr
->values
[0].string
.text
);
7730 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7731 "compression", NULL
, attr
->values
[0].string
.text
);
7736 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7737 compression
= CUPS_FILE_GZIP
;
7738 #endif /* HAVE_LIBZ */
7742 * Do we have a file to print?
7747 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7752 * Is it a format we support?
7755 if ((format
= ippFindAttribute(con
->request
, "document-format",
7756 IPP_TAG_MIMETYPE
)) != NULL
)
7759 * Grab format from client...
7762 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
7764 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
7765 format
->values
[0].string
.text
);
7772 * No document format attribute? Auto-type it!
7775 strcpy(super
, "application");
7776 strcpy(type
, "octet-stream");
7779 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7782 * Auto-type the file...
7785 ipp_attribute_t
*doc_name
; /* document-name attribute */
7788 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
7790 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7791 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7792 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7798 * Replace the document-format attribute value with the auto-typed one.
7801 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7806 _cupsStrFree(format
->values
[0].string
.text
);
7807 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7810 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7811 "document-format", NULL
, mimetype
);
7814 filetype
= mimeType(MimeDatabase
, super
, type
);
7817 filetype
= mimeType(MimeDatabase
, super
, type
);
7821 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7822 _("Unsupported format \'%s/%s\'!"), super
, type
);
7823 cupsdLogMessage(CUPSD_LOG_INFO
,
7824 "Hint: Do you have the raw file printing rules enabled?");
7827 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7828 "document-format", NULL
, format
->values
[0].string
.text
);
7833 cupsdLogMessage(CUPSD_LOG_DEBUG
,
7834 "send_document: request file type is %s/%s.",
7835 filetype
->super
, filetype
->type
);
7838 * Add the file to the job...
7843 if (add_file(con
, job
, filetype
, compression
))
7846 if (job
->dtype
& CUPS_PRINTER_CLASS
)
7847 printer
= cupsdFindClass(job
->dest
);
7849 printer
= cupsdFindPrinter(job
->dest
);
7851 if (stat(con
->filename
, &fileinfo
))
7854 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7856 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7858 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7859 IPP_TAG_INTEGER
)) != NULL
)
7860 attr
->values
[0].integer
+= kbytes
;
7862 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
7864 rename(con
->filename
, filename
);
7866 cupsdClearString(&con
->filename
);
7868 cupsdLogMessage(CUPSD_LOG_INFO
,
7869 "File of type %s/%s queued in job #%d by \"%s\".",
7870 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
7873 * Start the job if this is the last document...
7876 if ((attr
= ippFindAttribute(con
->request
, "last-document",
7877 IPP_TAG_BOOLEAN
)) != NULL
&&
7878 attr
->values
[0].boolean
)
7881 * See if we need to add the ending sheet...
7885 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
7886 (attr
= ippFindAttribute(job
->attrs
, "job-sheets",
7887 IPP_TAG_ZERO
)) != NULL
&&
7888 attr
->num_values
> 1)
7894 cupsdLogMessage(CUPSD_LOG_INFO
,
7895 "Adding end banner page \"%s\" to job %d.",
7896 attr
->values
[1].string
.text
, job
->id
);
7898 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
7900 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7903 if (job
->state_value
== IPP_JOB_STOPPED
)
7905 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7906 job
->state_value
= IPP_JOB_PENDING
;
7908 else if (job
->state_value
== IPP_JOB_HELD
)
7910 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7911 IPP_TAG_KEYWORD
)) == NULL
)
7912 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7914 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
7916 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7917 job
->state_value
= IPP_JOB_PENDING
;
7924 * Start the job if possible... Since cupsdCheckJobs() can cancel a
7925 * job if it doesn't print, we need to re-find the job afterwards...
7932 job
= cupsdFindJob(jobid
);
7936 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7937 IPP_TAG_KEYWORD
)) == NULL
)
7938 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7940 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
7942 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
7943 job
->state_value
= IPP_JOB_HELD
;
7944 job
->hold_until
= time(NULL
) + 60;
7950 * Fill in the response info...
7953 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
7956 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
7959 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
7961 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
7962 job
? job
->state_value
: IPP_JOB_CANCELLED
);
7963 add_job_state_reasons(con
, job
);
7965 con
->response
->request
.status
.status_code
= IPP_OK
;
7970 * 'send_http_error()' - Send a HTTP error back to the IPP client.
7974 send_http_error(cupsd_client_t
*con
, /* I - Client connection */
7975 http_status_t status
) /* I - HTTP status code */
7977 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
7978 ippOpString(con
->request
->request
.op
.operation_id
),
7979 httpStatus(status
));
7981 cupsdSendError(con
, status
);
7983 ippDelete(con
->response
);
7984 con
->response
= NULL
;
7991 * 'send_ipp_status()' - Send a status back to the IPP client.
7995 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
7996 ipp_status_t status
, /* I - IPP status code */
7997 const char *message
, /* I - Status message */
7998 ...) /* I - Additional args as needed */
8000 va_list ap
; /* Pointer to additional args */
8001 char formatted
[1024]; /* Formatted errror message */
8006 va_start(ap
, message
);
8007 vsnprintf(formatted
, sizeof(formatted
),
8008 _cupsLangString(con
->language
, message
), ap
);
8011 cupsdLogMessage(status
>= IPP_BAD_REQUEST
? CUPSD_LOG_ERROR
: CUPSD_LOG_INFO
,
8013 ippOpString(con
->request
->request
.op
.operation_id
),
8014 ippErrorString(status
), formatted
);
8017 cupsdLogMessage(status
>= IPP_BAD_REQUEST
? CUPSD_LOG_ERROR
: CUPSD_LOG_INFO
,
8019 ippOpString(con
->request
->request
.op
.operation_id
),
8020 ippErrorString(status
));
8022 con
->response
->request
.status
.status_code
= status
;
8024 if (ippFindAttribute(con
->response
, "attributes-charset",
8025 IPP_TAG_ZERO
) == NULL
)
8026 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
8027 "attributes-charset", NULL
, DefaultCharset
);
8029 if (ippFindAttribute(con
->response
, "attributes-natural-language",
8030 IPP_TAG_ZERO
) == NULL
)
8031 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
8032 "attributes-natural-language", NULL
, DefaultLanguage
);
8035 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
8036 "status-message", NULL
, formatted
);
8041 * 'set_default()' - Set the default destination...
8045 set_default(cupsd_client_t
*con
, /* I - Client connection */
8046 ipp_attribute_t
*uri
) /* I - Printer URI */
8048 http_status_t status
; /* Policy status */
8049 cups_ptype_t dtype
; /* Destination type (printer or class) */
8050 char method
[HTTP_MAX_URI
],
8051 /* Method portion of URI */
8052 username
[HTTP_MAX_URI
],
8053 /* Username portion of URI */
8055 /* Host portion of URI */
8056 resource
[HTTP_MAX_URI
];
8057 /* Resource portion of URI */
8058 int port
; /* Port portion of URI */
8059 const char *name
; /* Printer name */
8060 cupsd_printer_t
*printer
; /* Printer */
8063 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
8064 con
->http
.fd
, uri
->values
[0].string
.text
);
8067 * Is the destination valid?
8070 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8071 sizeof(method
), username
, sizeof(username
), host
,
8072 sizeof(host
), &port
, resource
, sizeof(resource
));
8074 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
8080 send_ipp_status(con
, IPP_NOT_FOUND
,
8081 _("The printer or class was not found."));
8089 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
8091 send_http_error(con
, status
);
8096 * Set it as the default...
8099 DefaultPrinter
= printer
;
8101 cupsdSaveAllPrinters();
8102 cupsdSaveAllClasses();
8104 cupsdWritePrintcap();
8106 cupsdLogMessage(CUPSD_LOG_INFO
,
8107 "Default destination set to \"%s\" by \"%s\".", name
,
8111 * Everything was ok, so return OK status...
8114 con
->response
->request
.status
.status_code
= IPP_OK
;
8119 * 'set_job_attrs()' - Set job attributes.
8123 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
8124 ipp_attribute_t
*uri
) /* I - Job URI */
8126 ipp_attribute_t
*attr
, /* Current attribute */
8127 *attr2
; /* Job attribute */
8128 int jobid
; /* Job ID */
8129 cupsd_job_t
*job
; /* Current job */
8130 char method
[HTTP_MAX_URI
],
8131 /* Method portion of URI */
8132 username
[HTTP_MAX_URI
],
8133 /* Username portion of URI */
8135 /* Host portion of URI */
8136 resource
[HTTP_MAX_URI
];
8137 /* Resource portion of URI */
8138 int port
; /* Port portion of URI */
8141 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
8142 con
->http
.fd
, uri
->values
[0].string
.text
);
8145 * Start with "everything is OK" status...
8148 con
->response
->request
.status
.status_code
= IPP_OK
;
8151 * See if we have a job URI or a printer URI...
8154 if (!strcmp(uri
->name
, "printer-uri"))
8157 * Got a printer URI; see if we also have a job-id attribute...
8160 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8161 IPP_TAG_INTEGER
)) == NULL
)
8163 send_ipp_status(con
, IPP_BAD_REQUEST
,
8164 _("Got a printer-uri attribute but no job-id!"));
8168 jobid
= attr
->values
[0].integer
;
8173 * Got a job URI; parse it to get the job ID...
8176 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8177 sizeof(method
), username
, sizeof(username
), host
,
8178 sizeof(host
), &port
, resource
, sizeof(resource
));
8180 if (strncmp(resource
, "/jobs/", 6))
8186 send_ipp_status(con
, IPP_BAD_REQUEST
,
8187 _("Bad job-uri attribute \"%s\"!"),
8188 uri
->values
[0].string
.text
);
8192 jobid
= atoi(resource
+ 6);
8196 * See if the job exists...
8199 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8202 * Nope - return a "not found" error...
8205 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8210 * See if the job has been completed...
8213 if (job
->state_value
> IPP_JOB_STOPPED
)
8216 * Return a "not-possible" error...
8219 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8220 _("Job #%d is finished and cannot be altered!"), jobid
);
8225 * See if the job is owned by the requesting user...
8228 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8230 send_http_error(con
, HTTP_UNAUTHORIZED
);
8235 * See what the user wants to change.
8240 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8242 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8245 if (!strcmp(attr
->name
, "attributes-charset") ||
8246 !strcmp(attr
->name
, "attributes-natural-language") ||
8247 !strcmp(attr
->name
, "document-compression") ||
8248 !strcmp(attr
->name
, "document-format") ||
8249 !strcmp(attr
->name
, "job-detailed-status-messages") ||
8250 !strcmp(attr
->name
, "job-document-access-errors") ||
8251 !strcmp(attr
->name
, "job-id") ||
8252 !strcmp(attr
->name
, "job-k-octets") ||
8253 !strcmp(attr
->name
, "job-originating-host-name") ||
8254 !strcmp(attr
->name
, "job-originating-user-name") ||
8255 !strcmp(attr
->name
, "job-printer-up-time") ||
8256 !strcmp(attr
->name
, "job-printer-uri") ||
8257 !strcmp(attr
->name
, "job-sheets") ||
8258 !strcmp(attr
->name
, "job-state-message") ||
8259 !strcmp(attr
->name
, "job-state-reasons") ||
8260 !strcmp(attr
->name
, "job-uri") ||
8261 !strcmp(attr
->name
, "number-of-documents") ||
8262 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
8263 !strcmp(attr
->name
, "output-device-assigned") ||
8264 !strncmp(attr
->name
, "date-time-at-", 13) ||
8265 !strncmp(attr
->name
, "job-impressions", 15) ||
8266 !strncmp(attr
->name
, "job-k-octets", 12) ||
8267 !strncmp(attr
->name
, "job-media-sheets", 16) ||
8268 !strncmp(attr
->name
, "time-at-", 8))
8274 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
8275 _("%s cannot be changed."), attr
->name
);
8277 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8278 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8283 if (!strcmp(attr
->name
, "job-priority"))
8286 * Change the job priority...
8289 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8291 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
8293 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8294 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8296 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
8298 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8299 _("Job is completed and cannot be changed."));
8302 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8303 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
8305 else if (!strcmp(attr
->name
, "job-state"))
8308 * Change the job state...
8311 if (attr
->value_tag
!= IPP_TAG_ENUM
)
8313 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
8315 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8316 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8320 switch (attr
->values
[0].integer
)
8322 case IPP_JOB_PENDING
:
8324 if (job
->state_value
> IPP_JOB_HELD
)
8326 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8327 _("Job state cannot be changed."));
8330 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8332 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8333 job
->state_value
= attr
->values
[0].integer
;
8337 case IPP_JOB_PROCESSING
:
8338 case IPP_JOB_STOPPED
:
8339 if (job
->state_value
!= attr
->values
[0].integer
)
8341 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8342 _("Job state cannot be changed."));
8347 case IPP_JOB_CANCELLED
:
8348 case IPP_JOB_ABORTED
:
8349 case IPP_JOB_COMPLETED
:
8350 if (job
->state_value
> IPP_JOB_PROCESSING
)
8352 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8353 _("Job state cannot be changed."));
8356 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8358 cupsdCancelJob(job
, 0);
8362 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8363 job
->state_value
= attr
->values
[0].integer
;
8371 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
8373 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8374 IPP_TAG_ZERO
)) != NULL
)
8377 * Some other value; first free the old value...
8380 if (job
->attrs
->prev
)
8381 job
->attrs
->prev
->next
= attr2
->next
;
8383 job
->attrs
->attrs
= attr2
->next
;
8385 if (job
->attrs
->last
== attr2
)
8386 job
->attrs
->last
= job
->attrs
->prev
;
8388 _ippFreeAttr(attr2
);
8391 * Then copy the attribute...
8394 copy_attribute(job
->attrs
, attr
, 0);
8397 * See if the job-name or job-hold-until is being changed.
8400 if (!strcmp(attr
->name
, "job-hold-until"))
8402 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
8404 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
8405 cupsdReleaseJob(job
);
8410 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
8413 * Delete the attribute...
8416 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8417 IPP_TAG_ZERO
)) != NULL
)
8419 if (job
->attrs
->prev
)
8420 job
->attrs
->prev
->next
= attr2
->next
;
8422 job
->attrs
->attrs
= attr2
->next
;
8424 if (attr2
== job
->attrs
->last
)
8425 job
->attrs
->last
= job
->attrs
->prev
;
8427 _ippFreeAttr(attr2
);
8433 * Add new option by copying it...
8436 copy_attribute(job
->attrs
, attr
, 0);
8447 * Start jobs if possible...
8455 * 'set_printer_defaults()' - Set printer default options from a request.
8459 set_printer_defaults(
8460 cupsd_client_t
*con
, /* I - Client connection */
8461 cupsd_printer_t
*printer
) /* I - Printer */
8463 int i
; /* Looping var */
8464 ipp_attribute_t
*attr
; /* Current attribute */
8465 int namelen
; /* Length of attribute name */
8466 char name
[256], /* New attribute name */
8467 value
[256]; /* String version of integer attrs */
8470 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8473 * Skip non-printer attributes...
8476 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
8479 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
8481 if (!strcmp(attr
->name
, "job-sheets-default"))
8484 * Only allow keywords and names...
8487 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8491 * Only allow job-sheets-default to be set when running without a
8492 * system high classification level...
8498 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
8500 if (attr
->num_values
> 1)
8501 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
8503 cupsdSetString(&printer
->job_sheets
[1], "none");
8505 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
8507 cupsdFreePrinterUsers(printer
);
8509 printer
->deny_users
= 0;
8511 if (attr
->value_tag
== IPP_TAG_NAME
&&
8512 (attr
->num_values
> 1 ||
8513 strcmp(attr
->values
[0].string
.text
, "all")))
8515 for (i
= 0; i
< attr
->num_values
; i
++)
8516 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8519 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
8521 cupsdFreePrinterUsers(printer
);
8523 printer
->deny_users
= 1;
8525 if (attr
->value_tag
== IPP_TAG_NAME
&&
8526 (attr
->num_values
> 1 ||
8527 strcmp(attr
->values
[0].string
.text
, "none")))
8529 for (i
= 0; i
< attr
->num_values
; i
++)
8530 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8533 else if (!strcmp(attr
->name
, "job-quota-period"))
8535 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8538 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
8539 attr
->values
[0].integer
);
8540 cupsdFreeQuotas(printer
);
8542 printer
->quota_period
= attr
->values
[0].integer
;
8544 else if (!strcmp(attr
->name
, "job-k-limit"))
8546 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8549 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
8550 attr
->values
[0].integer
);
8551 cupsdFreeQuotas(printer
);
8553 printer
->k_limit
= attr
->values
[0].integer
;
8555 else if (!strcmp(attr
->name
, "job-page-limit"))
8557 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8560 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
8561 attr
->values
[0].integer
);
8562 cupsdFreeQuotas(printer
);
8564 printer
->page_limit
= attr
->values
[0].integer
;
8566 else if (!strcmp(attr
->name
, "printer-op-policy"))
8568 cupsd_policy_t
*p
; /* Policy */
8571 if (attr
->value_tag
!= IPP_TAG_NAME
)
8574 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
8576 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8577 "Setting printer-op-policy to \"%s\"...",
8578 attr
->values
[0].string
.text
);
8579 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
8580 printer
->op_policy_ptr
= p
;
8584 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8585 _("Unknown printer-op-policy \"%s\"."),
8586 attr
->values
[0].string
.text
);
8590 else if (!strcmp(attr
->name
, "printer-error-policy"))
8592 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8595 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
8596 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
8597 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
8599 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8600 _("Unknown printer-error-policy \"%s\"."),
8601 attr
->values
[0].string
.text
);
8605 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8606 "Setting printer-error-policy to \"%s\"...",
8607 attr
->values
[0].string
.text
);
8608 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
8610 else if (!strcmp(attr
->name
, "document-format-default") ||
8611 !strcmp(attr
->name
, "notify-lease-duration-default") ||
8612 !strcmp(attr
->name
, "notify-notify-events-default"))
8616 * Skip any other non-default attributes...
8619 namelen
= strlen(attr
->name
);
8620 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
8621 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
8625 * OK, anything else must be a user-defined default...
8628 strlcpy(name
, attr
->name
, sizeof(name
));
8629 name
[namelen
- 8] = '\0'; /* Strip "-default" */
8631 switch (attr
->value_tag
)
8633 case IPP_TAG_DELETEATTR
:
8634 printer
->num_options
= cupsRemoveOption(name
,
8635 printer
->num_options
,
8636 &(printer
->options
));
8637 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8638 "Deleting %s", attr
->name
);
8642 case IPP_TAG_KEYWORD
:
8644 printer
->num_options
= cupsAddOption(name
,
8645 attr
->values
[0].string
.text
,
8646 printer
->num_options
,
8647 &(printer
->options
));
8648 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8649 "Setting %s to \"%s\"...", attr
->name
,
8650 attr
->values
[0].string
.text
);
8653 case IPP_TAG_BOOLEAN
:
8654 printer
->num_options
= cupsAddOption(name
,
8655 attr
->values
[0].boolean
?
8657 printer
->num_options
,
8658 &(printer
->options
));
8659 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8660 "Setting %s to %s...", attr
->name
,
8661 attr
->values
[0].boolean
? "true" : "false");
8664 case IPP_TAG_INTEGER
:
8666 sprintf(value
, "%d", attr
->values
[0].integer
);
8667 printer
->num_options
= cupsAddOption(name
, value
,
8668 printer
->num_options
,
8669 &(printer
->options
));
8670 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8671 "Setting %s to %s...", attr
->name
, value
);
8674 case IPP_TAG_RANGE
:
8675 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
8676 attr
->values
[0].range
.upper
);
8677 printer
->num_options
= cupsAddOption(name
, value
,
8678 printer
->num_options
,
8679 &(printer
->options
));
8680 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8681 "Setting %s to %s...", attr
->name
, value
);
8684 case IPP_TAG_RESOLUTION
:
8685 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
8686 attr
->values
[0].resolution
.yres
,
8687 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
8689 printer
->num_options
= cupsAddOption(name
, value
,
8690 printer
->num_options
,
8691 &(printer
->options
));
8692 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8693 "Setting %s to %s...", attr
->name
, value
);
8697 /* Do nothing for other values */
8705 * 'start_printer()' - Start a printer.
8709 start_printer(cupsd_client_t
*con
, /* I - Client connection */
8710 ipp_attribute_t
*uri
) /* I - Printer URI */
8712 http_status_t status
; /* Policy status */
8713 cups_ptype_t dtype
; /* Destination type (printer or class) */
8714 char method
[HTTP_MAX_URI
],
8715 /* Method portion of URI */
8716 username
[HTTP_MAX_URI
],
8717 /* Username portion of URI */
8719 /* Host portion of URI */
8720 resource
[HTTP_MAX_URI
];
8721 /* Resource portion of URI */
8722 int port
; /* Port portion of URI */
8723 const char *name
; /* Printer name */
8724 cupsd_printer_t
*printer
; /* Printer data */
8727 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
8728 con
->http
.fd
, uri
->values
[0].string
.text
);
8731 * Is the destination valid?
8734 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8735 sizeof(method
), username
, sizeof(username
), host
,
8736 sizeof(host
), &port
, resource
, sizeof(resource
));
8738 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
8744 send_ipp_status(con
, IPP_NOT_FOUND
,
8745 _("The printer or class was not found."));
8753 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8755 send_http_error(con
, status
);
8760 * Start the printer...
8763 printer
->state_message
[0] = '\0';
8765 cupsdStartPrinter(printer
, 1);
8767 if (dtype
& CUPS_PRINTER_CLASS
)
8768 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".", name
,
8771 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".", name
,
8777 * Everything was ok, so return OK status...
8780 con
->response
->request
.status
.status_code
= IPP_OK
;
8785 * 'stop_printer()' - Stop a printer.
8789 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
8790 ipp_attribute_t
*uri
) /* I - Printer URI */
8792 http_status_t status
; /* Policy status */
8793 cups_ptype_t dtype
; /* Destination type (printer or class) */
8794 char method
[HTTP_MAX_URI
],
8795 /* Method portion of URI */
8796 username
[HTTP_MAX_URI
],
8797 /* Username portion of URI */
8799 /* Host portion of URI */
8800 resource
[HTTP_MAX_URI
];
8801 /* Resource portion of URI */
8802 int port
; /* Port portion of URI */
8803 const char *name
; /* Printer name */
8804 cupsd_printer_t
*printer
; /* Printer data */
8805 ipp_attribute_t
*attr
; /* printer-state-message attribute */
8808 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
8809 con
->http
.fd
, uri
->values
[0].string
.text
);
8812 * Is the destination valid?
8815 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8816 sizeof(method
), username
, sizeof(username
), host
,
8817 sizeof(host
), &port
, resource
, sizeof(resource
));
8819 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
8825 send_ipp_status(con
, IPP_NOT_FOUND
,
8826 _("The printer or class was not found."));
8834 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8836 send_http_error(con
, status
);
8841 * Stop the printer...
8844 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8845 IPP_TAG_TEXT
)) == NULL
)
8846 strcpy(printer
->state_message
, "Paused");
8849 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8850 sizeof(printer
->state_message
));
8853 cupsdStopPrinter(printer
, 1);
8855 if (dtype
& CUPS_PRINTER_CLASS
)
8856 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".", name
,
8859 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".", name
,
8863 * Everything was ok, so return OK status...
8866 con
->response
->request
.status
.status_code
= IPP_OK
;
8871 * 'user_allowed()' - See if a user is allowed to print to a queue.
8874 static int /* O - 0 if not allowed, 1 if allowed */
8875 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
8876 const char *username
) /* I - Username */
8878 int i
; /* Looping var */
8879 struct passwd
*pw
; /* User password data */
8882 if (p
->num_users
== 0)
8885 if (!strcmp(username
, "root"))
8888 pw
= getpwnam(username
);
8891 for (i
= 0; i
< p
->num_users
; i
++)
8893 if (p
->users
[i
][0] == '@')
8896 * Check group membership...
8899 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
8902 else if (!strcasecmp(username
, p
->users
[i
]))
8906 return ((i
< p
->num_users
) != p
->deny_users
);
8911 * 'validate_job()' - Validate printer options and destination.
8915 validate_job(cupsd_client_t
*con
, /* I - Client connection */
8916 ipp_attribute_t
*uri
) /* I - Printer URI */
8918 http_status_t status
; /* Policy status */
8919 ipp_attribute_t
*attr
; /* Current attribute */
8920 ipp_attribute_t
*format
; /* Document-format attribute */
8921 cups_ptype_t dtype
; /* Destination type (printer or class) */
8922 char method
[HTTP_MAX_URI
],
8923 /* Method portion of URI */
8924 username
[HTTP_MAX_URI
],
8925 /* Username portion of URI */
8927 /* Host portion of URI */
8928 resource
[HTTP_MAX_URI
];
8929 /* Resource portion of URI */
8930 int port
; /* Port portion of URI */
8931 char super
[MIME_MAX_SUPER
],
8932 /* Supertype of file */
8933 type
[MIME_MAX_TYPE
];
8934 /* Subtype of file */
8935 cupsd_printer_t
*printer
; /* Printer */
8938 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
8939 con
->http
.fd
, uri
->values
[0].string
.text
);
8942 * OK, see if the client is sending the document compressed - CUPS
8943 * doesn't support compression yet...
8946 if ((attr
= ippFindAttribute(con
->request
, "compression",
8947 IPP_TAG_KEYWORD
)) != NULL
&&
8948 !strcmp(attr
->values
[0].string
.text
, "none"))
8950 send_ipp_status(con
, IPP_ATTRIBUTES
,
8951 _("Unsupported compression attribute %s!"),
8952 attr
->values
[0].string
.text
);
8953 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8954 "compression", NULL
, attr
->values
[0].string
.text
);
8959 * Is it a format we support?
8962 if ((format
= ippFindAttribute(con
->request
, "document-format",
8963 IPP_TAG_MIMETYPE
)) != NULL
)
8965 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
8967 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
8968 format
->values
[0].string
.text
);
8972 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
8973 !mimeType(MimeDatabase
, super
, type
))
8975 cupsdLogMessage(CUPSD_LOG_INFO
,
8976 "Hint: Do you have the raw file printing rules enabled?");
8977 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8978 _("Unsupported format \"%s\"!"),
8979 format
->values
[0].string
.text
);
8980 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8981 "document-format", NULL
, format
->values
[0].string
.text
);
8987 * Is the destination valid?
8990 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8991 sizeof(method
), username
, sizeof(username
), host
,
8992 sizeof(host
), &port
, resource
, sizeof(resource
));
8994 if (cupsdValidateDest(host
, resource
, &dtype
, &printer
) == NULL
)
9000 send_ipp_status(con
, IPP_NOT_FOUND
,
9001 _("The printer or class was not found."));
9009 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9011 send_http_error(con
, status
);
9016 * Everything was ok, so return OK status...
9019 con
->response
->request
.status
.status_code
= IPP_OK
;
9024 * 'validate_name()' - Make sure the printer name only contains valid chars.
9027 static int /* O - 0 if name is no good, 1 if name is good */
9028 validate_name(const char *name
) /* I - Name to check */
9030 const char *ptr
; /* Pointer into name */
9034 * Scan the whole name...
9037 for (ptr
= name
; *ptr
; ptr
++)
9038 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
9042 * All the characters are good; validate the length, too...
9045 return ((ptr
- name
) < 128);
9050 * 'validate_user()' - Validate the user for the request.
9053 static int /* O - 1 if permitted, 0 otherwise */
9054 validate_user(cupsd_job_t
*job
, /* I - Job */
9055 cupsd_client_t
*con
, /* I - Client connection */
9056 const char *owner
, /* I - Owner of job/resource */
9057 char *username
, /* O - Authenticated username */
9058 int userlen
) /* I - Length of username */
9060 cupsd_printer_t
*printer
; /* Printer for job */
9063 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9064 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
9066 job
? job
->id
: 0, con
->http
.fd
, owner
? owner
: "(null)",
9073 if (!con
|| !owner
|| !username
|| userlen
<= 0)
9077 * Get the best authenticated username that is available.
9080 strlcpy(username
, get_username(con
), userlen
);
9083 * Check the username against the owner...
9086 if (job
->dtype
& CUPS_PRINTER_CLASS
)
9087 printer
= cupsdFindClass(job
->dest
);
9089 printer
= cupsdFindPrinter(job
->dest
);
9091 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
9092 con
, owner
) == HTTP_OK
);
9097 * End of "$Id: ipp.c 5232 2006-03-05 17:59:19Z mike $".