2 * "$Id: ipp.c 5284 2006-03-13 13:34:24Z 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 switch (job
->state_value
)
3019 case IPP_JOB_CANCELLED
:
3020 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3021 _("Job #%d is already cancelled - can\'t cancel."),
3025 case IPP_JOB_ABORTED
:
3026 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3027 _("Job #%d is already aborted - can\'t cancel."),
3032 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3033 _("Job #%d is already completed - can\'t cancel."),
3042 * Cancel the job and return...
3045 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3046 "Job cancelled by \"%s\".", username
);
3048 cupsdCancelJob(job
, 0);
3051 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was cancelled by \"%s\".", jobid
,
3054 con
->response
->request
.status
.status_code
= IPP_OK
;
3059 * 'cancel_subscription()' - Cancel a subscription.
3063 cancel_subscription(
3064 cupsd_client_t
*con
, /* I - Client connection */
3065 int sub_id
) /* I - Subscription ID */
3067 http_status_t status
; /* Policy status */
3068 cupsd_subscription_t
*sub
; /* Subscription */
3071 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3072 "cancel_subscription(con=%p[%d], sub_id=%d)",
3073 con
, con
->http
.fd
, sub_id
);
3076 * Is the subscription ID valid?
3079 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3082 * Bad subscription ID...
3085 send_ipp_status(con
, IPP_NOT_FOUND
,
3086 _("notify-subscription-id %d no good!"), sub_id
);
3094 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3096 con
, sub
->owner
)) != HTTP_OK
)
3098 send_http_error(con
, status
);
3103 * Cancel the subscription...
3106 cupsdDeleteSubscription(sub
, 1);
3108 con
->response
->request
.status
.status_code
= IPP_OK
;
3113 * 'check_quotas()' - Check quotas for a printer and user.
3116 static int /* O - 1 if OK, 0 if not */
3117 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3118 cupsd_printer_t
*p
) /* I - Printer or class */
3120 int i
; /* Looping var */
3121 char username
[33]; /* Username */
3122 cupsd_quota_t
*q
; /* Quota data */
3123 struct passwd
*pw
; /* User password data */
3126 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3127 con
, con
->http
.fd
, p
, p
->name
);
3137 * Figure out who is printing...
3140 strlcpy(username
, get_username(con
), sizeof(username
));
3143 * Check global active job limits for printers and users...
3146 if (MaxJobsPerPrinter
)
3149 * Check if there are too many pending jobs on this printer...
3152 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3154 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3163 * Check if there are too many pending jobs for this user...
3166 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3168 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3175 * Check against users...
3178 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3183 pw
= getpwnam(username
);
3186 for (i
= 0; i
< p
->num_users
; i
++)
3187 if (p
->users
[i
][0] == '@')
3190 * Check group membership...
3193 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3196 else if (!strcasecmp(username
, p
->users
[i
]))
3199 if ((i
< p
->num_users
) == p
->deny_users
)
3201 cupsdLogMessage(CUPSD_LOG_INFO
,
3202 "Denying user \"%s\" access to printer \"%s\"...",
3212 if (p
->k_limit
|| p
->page_limit
)
3214 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3216 cupsdLogMessage(CUPSD_LOG_ERROR
,
3217 "Unable to allocate quota data for user \"%s\"!",
3222 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3223 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3225 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3232 * If we have gotten this far, we're done!
3240 * 'copy_attribute()' - Copy a single attribute.
3243 static ipp_attribute_t
* /* O - New attribute */
3245 ipp_t
*to
, /* O - Destination request/response */
3246 ipp_attribute_t
*attr
, /* I - Attribute to copy */
3247 int quickcopy
) /* I - Do a quick copy? */
3249 int i
; /* Looping var */
3250 ipp_attribute_t
*toattr
; /* Destination attribute */
3253 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3254 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
3255 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
3258 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
3261 toattr
= ippAddSeparator(to
);
3264 case IPP_TAG_INTEGER
:
3266 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3267 attr
->name
, attr
->num_values
, NULL
);
3269 for (i
= 0; i
< attr
->num_values
; i
++)
3270 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
3273 case IPP_TAG_BOOLEAN
:
3274 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
3275 attr
->num_values
, NULL
);
3277 for (i
= 0; i
< attr
->num_values
; i
++)
3278 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
3281 case IPP_TAG_STRING
:
3284 case IPP_TAG_KEYWORD
:
3286 case IPP_TAG_URISCHEME
:
3287 case IPP_TAG_CHARSET
:
3288 case IPP_TAG_LANGUAGE
:
3289 case IPP_TAG_MIMETYPE
:
3290 toattr
= ippAddStrings(to
, attr
->group_tag
,
3291 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3292 attr
->name
, attr
->num_values
, NULL
, NULL
);
3296 for (i
= 0; i
< attr
->num_values
; i
++)
3297 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3301 for (i
= 0; i
< attr
->num_values
; i
++)
3302 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3307 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
3308 attr
->values
[0].date
);
3311 case IPP_TAG_RESOLUTION
:
3312 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
3313 attr
->num_values
, IPP_RES_PER_INCH
,
3316 for (i
= 0; i
< attr
->num_values
; i
++)
3318 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
3319 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
3320 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
3324 case IPP_TAG_RANGE
:
3325 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
3326 attr
->num_values
, NULL
, NULL
);
3328 for (i
= 0; i
< attr
->num_values
; i
++)
3330 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
3331 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
3335 case IPP_TAG_TEXTLANG
:
3336 case IPP_TAG_NAMELANG
:
3337 toattr
= ippAddStrings(to
, attr
->group_tag
,
3338 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3339 attr
->name
, attr
->num_values
, NULL
, NULL
);
3343 for (i
= 0; i
< attr
->num_values
; i
++)
3345 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
3346 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3351 for (i
= 0; i
< attr
->num_values
; i
++)
3354 toattr
->values
[i
].string
.charset
=
3355 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
3357 toattr
->values
[i
].string
.charset
=
3358 toattr
->values
[0].string
.charset
;
3360 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3365 case IPP_TAG_BEGIN_COLLECTION
:
3366 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
3367 attr
->num_values
, NULL
);
3369 for (i
= 0; i
< attr
->num_values
; i
++)
3371 toattr
->values
[i
].collection
= ippNew();
3372 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
3373 NULL
, IPP_TAG_ZERO
, 0);
3378 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3379 attr
->name
, attr
->num_values
, NULL
);
3381 for (i
= 0; i
< attr
->num_values
; i
++)
3383 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
3385 if (toattr
->values
[i
].unknown
.length
> 0)
3387 if ((toattr
->values
[i
].unknown
.data
=
3388 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
3389 toattr
->values
[i
].unknown
.length
= 0;
3391 memcpy(toattr
->values
[i
].unknown
.data
,
3392 attr
->values
[i
].unknown
.data
,
3393 toattr
->values
[i
].unknown
.length
);
3396 break; /* anti-compiler-warning-code */
3404 * 'copy_attrs()' - Copy attributes from one request to another.
3408 copy_attrs(ipp_t
*to
, /* I - Destination request */
3409 ipp_t
*from
, /* I - Source request */
3410 cups_array_t
*ra
, /* I - Requested attributes */
3411 ipp_tag_t group
, /* I - Group to copy */
3412 int quickcopy
) /* I - Do a quick copy? */
3414 ipp_attribute_t
*fromattr
; /* Source attribute */
3417 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3418 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3419 to
, from
, ra
, group
, quickcopy
);
3424 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3427 * Filter attributes as needed...
3430 if (group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3431 fromattr
->group_tag
!= IPP_TAG_ZERO
&& !fromattr
->name
)
3434 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
3435 copy_attribute(to
, fromattr
, quickcopy
);
3441 * 'copy_banner()' - Copy a banner file to the requests directory for the
3445 static int /* O - Size of banner file in kbytes */
3446 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
3447 cupsd_job_t
*job
, /* I - Job information */
3448 const char *name
) /* I - Name of banner */
3450 int i
; /* Looping var */
3451 int kbytes
; /* Size of banner file in kbytes */
3452 char filename
[1024]; /* Job filename */
3453 cupsd_banner_t
*banner
; /* Pointer to banner */
3454 cups_file_t
*in
; /* Input file */
3455 cups_file_t
*out
; /* Output file */
3456 int ch
; /* Character from file */
3457 char attrname
[255], /* Name of attribute */
3458 *s
; /* Pointer into name */
3459 ipp_attribute_t
*attr
; /* Attribute */
3462 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
3463 con
, con
->http
.fd
, job
, job
->id
, name
? name
: "(null)");
3466 * Find the banner; return if not found or "none"...
3469 if (!name
|| !strcmp(name
, "none") ||
3470 (banner
= cupsdFindBanner(name
)) == NULL
)
3474 * Open the banner and job files...
3477 if (add_file(con
, job
, banner
->filetype
, 0))
3480 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
3482 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
3484 cupsdLogMessage(CUPSD_LOG_ERROR
,
3485 "copy_banner: Unable to create banner job file %s - %s",
3486 filename
, strerror(errno
));
3491 fchmod(cupsFileNumber(out
), 0640);
3492 fchown(cupsFileNumber(out
), RunUser
, Group
);
3495 * Try the localized banner file under the subdirectory...
3498 strlcpy(attrname
, con
->request
->attrs
->next
->values
[0].string
.text
,
3500 if (strlen(attrname
) > 2 && attrname
[2] == '-')
3503 * Convert ll-cc to ll_CC...
3507 attrname
[3] = toupper(attrname
[3] & 255);
3508 attrname
[4] = toupper(attrname
[4] & 255);
3511 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3514 if (access(filename
, 0) && strlen(attrname
) > 2)
3517 * Wasn't able to find "ll_CC" locale file; try the non-national
3518 * localization banner directory.
3523 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3527 if (access(filename
, 0))
3530 * Use the non-localized banner file.
3533 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
3536 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
3540 cupsdLogMessage(CUPSD_LOG_ERROR
,
3541 "copy_banner: Unable to open banner template file %s - %s",
3542 filename
, strerror(errno
));
3548 * Parse the file to the end...
3551 while ((ch
= cupsFileGetChar(in
)) != EOF
)
3555 * Get an attribute name...
3558 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
3559 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
3561 else if (s
< (attrname
+ sizeof(attrname
) - 1))
3571 * Ignore { followed by stuff that is not an attribute name...
3574 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
3579 * See if it is defined...
3582 if (attrname
[0] == '?')
3587 if (!strcmp(s
, "printer-name"))
3589 cupsFilePuts(out
, job
->dest
);
3592 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
3595 * See if we have a leading question mark...
3598 if (attrname
[0] != '?')
3601 * Nope, write to file as-is; probably a PostScript procedure...
3604 cupsFilePrintf(out
, "{%s}", attrname
);
3611 * Output value(s)...
3614 for (i
= 0; i
< attr
->num_values
; i
++)
3617 cupsFilePutChar(out
, ',');
3619 switch (attr
->value_tag
)
3621 case IPP_TAG_INTEGER
:
3623 if (!strncmp(s
, "time-at-", 8))
3624 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
3626 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
3629 case IPP_TAG_BOOLEAN
:
3630 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
3633 case IPP_TAG_NOVALUE
:
3634 cupsFilePuts(out
, "novalue");
3637 case IPP_TAG_RANGE
:
3638 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
3639 attr
->values
[i
].range
.upper
);
3642 case IPP_TAG_RESOLUTION
:
3643 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3644 attr
->values
[i
].resolution
.yres
,
3645 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3650 case IPP_TAG_STRING
:
3653 case IPP_TAG_KEYWORD
:
3654 case IPP_TAG_CHARSET
:
3655 case IPP_TAG_LANGUAGE
:
3656 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
3659 * Need to quote strings for PS banners...
3664 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
3666 if (*p
== '(' || *p
== ')' || *p
== '\\')
3668 cupsFilePutChar(out
, '\\');
3669 cupsFilePutChar(out
, *p
);
3671 else if (*p
< 32 || *p
> 126)
3672 cupsFilePrintf(out
, "\\%03o", *p
& 255);
3674 cupsFilePutChar(out
, *p
);
3678 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
3682 break; /* anti-compiler-warning-code */
3686 else if (ch
== '\\') /* Quoted char */
3688 ch
= cupsFileGetChar(in
);
3690 if (ch
!= '{') /* Only do special handling for \{ */
3691 cupsFilePutChar(out
, '\\');
3693 cupsFilePutChar(out
, ch
);
3696 cupsFilePutChar(out
, ch
);
3700 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
3702 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
3703 IPP_TAG_INTEGER
)) != NULL
)
3704 attr
->values
[0].integer
+= kbytes
;
3713 * 'copy_file()' - Copy a PPD file or interface script...
3716 static int /* O - 0 = success, -1 = error */
3717 copy_file(const char *from
, /* I - Source file */
3718 const char *to
) /* I - Destination file */
3720 cups_file_t
*src
, /* Source file */
3721 *dst
; /* Destination file */
3722 int bytes
; /* Bytes to read/write */
3723 char buffer
[2048]; /* Copy buffer */
3726 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
3729 * Open the source and destination file for a copy...
3732 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
3735 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
3742 * Copy the source file to the destination...
3745 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
3746 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
3754 * Close both files and return...
3759 return (cupsFileClose(dst
));
3764 * 'copy_model()' - Copy a PPD model file, substituting default values
3768 static int /* O - 0 = success, -1 = error */
3769 copy_model(cupsd_client_t
*con
, /* I - Client connection */
3770 const char *from
, /* I - Source file */
3771 const char *to
) /* I - Destination file */
3773 fd_set
*input
; /* select() input set */
3774 struct timeval timeout
; /* select() timeout */
3775 int maxfd
; /* Maximum file descriptor for select() */
3776 char tempfile
[1024]; /* Temporary PPD file */
3777 int tempfd
; /* Temporary PPD file descriptor */
3778 int temppid
; /* Process ID of cups-driverd */
3779 int temppipe
[2]; /* Temporary pipes */
3780 char *argv
[4], /* Command-line arguments */
3781 *envp
[MAX_ENV
]; /* Environment */
3782 cups_file_t
*src
, /* Source file */
3783 *dst
; /* Destination file */
3784 int bytes
, /* Bytes from pipe */
3785 total
; /* Total bytes from pipe */
3786 char buffer
[2048], /* Copy buffer */
3787 *ptr
; /* Pointer into buffer */
3788 int i
; /* Looping var */
3789 char option
[PPD_MAX_NAME
], /* Option name */
3790 choice
[PPD_MAX_NAME
]; /* Choice name */
3791 int num_defaults
; /* Number of default options */
3792 ppd_default_t
*defaults
; /* Default options */
3793 char cups_protocol
[PPD_MAX_LINE
];
3794 /* cupsProtocol attribute */
3795 int have_letter
, /* Have Letter size */
3796 have_a4
; /* Have A4 size */
3797 #ifdef HAVE_LIBPAPER
3798 char *paper_result
; /* Paper size name from libpaper */
3799 char system_paper
[64]; /* Paper size name buffer */
3800 #endif /* HAVE_LIBPAPER */
3803 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3804 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
3808 * Run cups-driverd to get the PPD file...
3811 argv
[0] = "cups-driverd";
3813 argv
[2] = (char *)from
;
3816 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3818 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
3819 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
3820 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3824 cupsdOpenPipe(temppipe
);
3826 if ((input
= calloc(1, SetSize
)) == NULL
)
3831 cupsdLogMessage(CUPSD_LOG_ERROR
,
3832 "copy_model: Unable to allocate %d bytes for select()...",
3837 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3838 "copy_model: Running \"cups-driverd cat %s\"...", from
);
3840 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
3852 * Wait up to 30 seconds for the PPD file to be copied...
3857 if (temppipe
[0] > CGIPipes
[0])
3858 maxfd
= temppipe
[0] + 1;
3860 maxfd
= CGIPipes
[0] + 1;
3865 * See if we have data ready...
3870 FD_SET(temppipe
[0], input
);
3871 FD_SET(CGIPipes
[0], input
);
3873 timeout
.tv_sec
= 30;
3874 timeout
.tv_usec
= 0;
3876 if ((i
= select(maxfd
, input
, NULL
, NULL
, &timeout
)) < 0)
3886 * We have timed out...
3892 if (FD_ISSET(temppipe
[0], input
))
3895 * Read the PPD file from the pipe, and write it to the PPD file.
3898 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
3900 if (write(tempfd
, buffer
, bytes
) < bytes
)
3909 if (FD_ISSET(CGIPipes
[0], input
))
3921 * No data from cups-deviced...
3924 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
3930 * Read the source file and see what page sizes are supported...
3933 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
3942 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
3943 if (!strncmp(buffer
, "*PageSize ", 10))
3946 * Strip UI text and command data from the end of the line...
3949 if ((ptr
= strchr(buffer
+ 10, '/')) != NULL
)
3951 if ((ptr
= strchr(buffer
+ 10, ':')) != NULL
)
3954 for (ptr
= buffer
+ 10; isspace(*ptr
); ptr
++);
3957 * Look for Letter and A4 page sizes...
3960 if (!strcmp(ptr
, "Letter"))
3963 if (!strcmp(ptr
, "A4"))
3967 cupsFileRewind(src
);
3970 * Open the destination (if possible) and set the default options...
3975 cups_protocol
[0] = '\0';
3977 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
3980 * Read all of the default lines from the old PPD...
3983 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
3984 if (!strncmp(buffer
, "*Default", 8))
3987 * Add the default option...
3990 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
3991 choice
, sizeof(choice
)))
3992 num_defaults
= ppd_add_default(option
, choice
, num_defaults
,
3995 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
3996 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4000 #ifdef HAVE_LIBPAPER
4001 else if ((paper_result
= systempapername()) != NULL
)
4004 * Set the default media sizes from the systemwide default...
4007 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
4008 system_paper
[0] = toupper(system_paper
[0] & 255);
4010 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
4011 (!strcmp(system_paper
, "A4") && have_a4
))
4013 num_defaults
= ppd_add_default("PageSize", system_paper
,
4014 num_defaults
, &defaults
);
4015 num_defaults
= ppd_add_default("PageRegion", system_paper
,
4016 num_defaults
, &defaults
);
4017 num_defaults
= ppd_add_default("PaperDimension", system_paper
,
4018 num_defaults
, &defaults
);
4019 num_defaults
= ppd_add_default("ImageableArea", system_paper
,
4020 num_defaults
, &defaults
);
4023 #endif /* HAVE_LIBPAPER */
4027 * Add the default media sizes...
4029 * Note: These values are generally not valid for large-format devices
4030 * like plotters, however it is probably safe to say that those
4031 * users will configure the media size after initially adding
4032 * the device anyways...
4035 if (!DefaultLanguage
||
4036 !strcasecmp(DefaultLanguage
, "C") ||
4037 !strcasecmp(DefaultLanguage
, "POSIX") ||
4038 !strcasecmp(DefaultLanguage
, "en") ||
4039 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4040 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4041 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4044 * These are the only locales that will default to "letter" size...
4049 num_defaults
= ppd_add_default("PageSize", "Letter", num_defaults
,
4051 num_defaults
= ppd_add_default("PageRegion", "Letter", num_defaults
,
4053 num_defaults
= ppd_add_default("PaperDimension", "Letter", num_defaults
,
4055 num_defaults
= ppd_add_default("ImageableArea", "Letter", num_defaults
,
4062 * The rest default to "a4" size...
4065 num_defaults
= ppd_add_default("PageSize", "A4", num_defaults
,
4067 num_defaults
= ppd_add_default("PageRegion", "A4", num_defaults
,
4069 num_defaults
= ppd_add_default("PaperDimension", "A4", num_defaults
,
4071 num_defaults
= ppd_add_default("ImageableArea", "A4", num_defaults
,
4077 * Open the destination file for a copy...
4080 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4082 if (num_defaults
> 0)
4091 * Copy the source file to the destination...
4094 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4096 if (!strncmp(buffer
, "*Default", 8))
4099 * Check for an previous default option choice...
4102 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4103 choice
, sizeof(choice
)))
4105 for (i
= 0; i
< num_defaults
; i
++)
4106 if (!strcmp(option
, defaults
[i
].option
))
4109 * Substitute the previous choice...
4112 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
,
4113 defaults
[i
].choice
);
4119 cupsFilePrintf(dst
, "%s\n", buffer
);
4122 if (cups_protocol
[0])
4123 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4125 if (num_defaults
> 0)
4129 * Close both files and return...
4136 return (cupsFileClose(dst
));
4141 * 'copy_job_attrs()' - Copy job attributes.
4145 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4146 cupsd_job_t
*job
, /* I - Job */
4147 cups_array_t
*ra
) /* I - Requested attributes array */
4149 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4153 * Send the requested attributes for each job...
4156 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4157 con
->servername
, con
->serverport
, "/jobs/%d",
4160 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
4161 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4162 "job-more-info", NULL
, job_uri
);
4164 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4165 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4166 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4167 job
->num_files
> 0);
4169 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
4170 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4171 "job-printer-up-time", time(NULL
));
4173 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4174 add_job_state_reasons(con
, job
);
4176 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4177 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4178 "job-uri", NULL
, job_uri
);
4180 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
4185 * 'copy_printer_attrs()' - Copy printer attributes.
4190 cupsd_client_t
*con
, /* I - Client connection */
4191 cupsd_printer_t
*printer
, /* I - Printer */
4192 cups_array_t
*ra
) /* I - Requested attributes array */
4194 char printer_uri
[HTTP_MAX_URI
];
4196 time_t curtime
; /* Current time */
4197 int i
; /* Looping var */
4198 ipp_attribute_t
*history
; /* History collection */
4202 * Copy the printer attributes to the response using requested-attributes
4203 * and document-format attributes that may be provided by the client.
4206 curtime
= time(NULL
);
4209 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
4210 printer
->recoverable
)
4211 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4212 "com.apple.print.recoverable-message", NULL
,
4213 printer
->recoverable
);
4214 #endif /* __APPLE__ */
4216 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4217 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4218 ippTimeToDate(curtime
));
4220 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4221 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4222 "printer-error-policy", NULL
, printer
->error_policy
);
4224 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4225 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
4226 printer
->accepting
);
4228 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4229 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
4232 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4233 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4234 "printer-op-policy", NULL
, printer
->op_policy
);
4236 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4237 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4240 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4241 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4242 "printer-state-change-time", printer
->state_time
);
4244 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
4245 cupsArrayFind(ra
, "printer-state-history"))
4248 * Printer history is only sent if specifically requested, so that
4249 * older CUPS/IPP clients won't barf on the collection attributes.
4252 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
4253 "printer-state-history",
4254 printer
->num_history
, NULL
);
4256 for (i
= 0; i
< printer
->num_history
; i
++)
4257 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
4258 NULL
, IPP_TAG_ZERO
, 0);
4261 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4262 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4263 "printer-state-message", NULL
, printer
->state_message
);
4265 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4266 add_printer_state_reasons(con
, printer
);
4268 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4270 int type
; /* printer-type value */
4274 * Add the CUPS-specific printer-type attribute...
4277 type
= printer
->type
;
4279 if (printer
== DefaultPrinter
)
4280 type
|= CUPS_PRINTER_DEFAULT
;
4282 if (!printer
->accepting
)
4283 type
|= CUPS_PRINTER_REJECTING
;
4285 if (!printer
->shared
)
4286 type
|= CUPS_PRINTER_NOT_SHARED
;
4288 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
4292 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4293 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4294 "printer-up-time", curtime
);
4296 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
4297 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
4300 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4301 "ipp", NULL
, con
->servername
, con
->serverport
,
4302 "/printers/%s", printer
->name
);
4303 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4304 "printer-uri-supported", NULL
, printer_uri
);
4305 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4309 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4310 add_queued_job_count(con
, printer
);
4312 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
4313 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
4318 * 'copy_subscription_attrs()' - Copy subscription attributes.
4322 copy_subscription_attrs(
4323 cupsd_client_t
*con
, /* I - Client connection */
4324 cupsd_subscription_t
*sub
, /* I - Subscription */
4325 cups_array_t
*ra
) /* I - Requested attributes array */
4327 ipp_attribute_t
*attr
; /* Current attribute */
4328 char printer_uri
[HTTP_MAX_URI
];
4330 int count
; /* Number of events */
4331 unsigned mask
; /* Current event mask */
4332 const char *name
; /* Current event name */
4336 * Copy the subscription attributes to the response using the
4337 * requested-attributes attribute that may be provided by the client.
4340 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
4342 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
4345 * Simple event list...
4348 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
4349 IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
4350 "notify-events", NULL
, name
);
4355 * Complex event list...
4358 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4359 if (sub
->mask
& mask
)
4362 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
4363 IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
4364 "notify-events", count
, NULL
, NULL
);
4366 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4367 if (sub
->mask
& mask
)
4369 attr
->values
[count
].string
.text
=
4370 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
4377 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
4378 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4379 "notify-job-id", sub
->job
->id
);
4381 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
4382 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4383 "notify-lease-duration", sub
->lease
);
4385 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
4387 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4388 "ipp", NULL
, con
->servername
, con
->serverport
,
4389 "/printers/%s", sub
->dest
->name
);
4390 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4391 "notify-printer-uri", NULL
, printer_uri
);
4394 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
4395 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4396 "notify-recipient-uri", NULL
, sub
->recipient
);
4397 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
4398 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
4399 "notify-pull-method", NULL
, "ippget");
4401 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
4402 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
4403 "notify-subscriber-user-name", NULL
, sub
->owner
);
4405 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
4406 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4407 "notify-subscription-id", sub
->id
);
4409 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
4410 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4411 "notify-time-interval", sub
->interval
);
4413 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
4414 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
4415 sub
->user_data
, sub
->user_data_len
);
4420 * 'create_job()' - Print a file to a printer or class.
4424 create_job(cupsd_client_t
*con
, /* I - Client connection */
4425 ipp_attribute_t
*uri
) /* I - Printer URI */
4427 cupsd_job_t
*job
; /* New job */
4430 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
4431 con
->http
.fd
, uri
->values
[0].string
.text
);
4434 * Create the job object...
4437 if ((job
= add_job(con
, uri
, NULL
)) == NULL
)
4441 * Save and log the job...
4446 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d created on \"%s\" by \"%s\".",
4447 job
->id
, job
->dest
, job
->username
);
4452 * 'create_requested_array()' - Create an array for the requested-attributes.
4455 static cups_array_t
* /* O - Array of attributes or NULL */
4456 create_requested_array(ipp_t
*request
) /* I - IPP request */
4458 int i
; /* Looping var */
4459 ipp_attribute_t
*requested
; /* requested-attributes attribute */
4460 cups_array_t
*ra
; /* Requested attributes array */
4461 char *value
; /* Current value */
4465 * Get the requested-attributes attribute, and return NULL if we don't
4469 if ((requested
= ippFindAttribute(request
, "requested-attributes",
4470 IPP_TAG_KEYWORD
)) == NULL
)
4474 * If the attribute contains a single "all" keyword, return NULL...
4477 if (requested
->num_values
== 1 &&
4478 !strcmp(requested
->values
[0].string
.text
, "all"))
4482 * Create an array using "strcmp" as the comparison function...
4485 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
4487 for (i
= 0; i
< requested
->num_values
; i
++)
4489 value
= requested
->values
[i
].string
.text
;
4491 if (!strcmp(value
, "job-template"))
4493 cupsArrayAdd(ra
, "copies");
4494 cupsArrayAdd(ra
, "copies-default");
4495 cupsArrayAdd(ra
, "copies-supported");
4496 cupsArrayAdd(ra
, "finishings");
4497 cupsArrayAdd(ra
, "finishings-default");
4498 cupsArrayAdd(ra
, "finishings-supported");
4499 cupsArrayAdd(ra
, "job-hold-until");
4500 cupsArrayAdd(ra
, "job-hold-until-default");
4501 cupsArrayAdd(ra
, "job-hold-until-supported");
4502 cupsArrayAdd(ra
, "job-priority");
4503 cupsArrayAdd(ra
, "job-priority-default");
4504 cupsArrayAdd(ra
, "job-priority-supported");
4505 cupsArrayAdd(ra
, "job-sheets");
4506 cupsArrayAdd(ra
, "job-sheets-default");
4507 cupsArrayAdd(ra
, "job-sheets-supported");
4508 cupsArrayAdd(ra
, "media");
4509 cupsArrayAdd(ra
, "media-default");
4510 cupsArrayAdd(ra
, "media-supported");
4511 cupsArrayAdd(ra
, "multiple-document-handling");
4512 cupsArrayAdd(ra
, "multiple-document-handling-default");
4513 cupsArrayAdd(ra
, "multiple-document-handling-supported");
4514 cupsArrayAdd(ra
, "number-up");
4515 cupsArrayAdd(ra
, "number-up-default");
4516 cupsArrayAdd(ra
, "number-up-supported");
4517 cupsArrayAdd(ra
, "orientation-requested");
4518 cupsArrayAdd(ra
, "orientation-requested-default");
4519 cupsArrayAdd(ra
, "orientation-requested-supported");
4520 cupsArrayAdd(ra
, "page-ranges");
4521 cupsArrayAdd(ra
, "page-ranges-supported");
4522 cupsArrayAdd(ra
, "printer-resolution");
4523 cupsArrayAdd(ra
, "printer-resolution-default");
4524 cupsArrayAdd(ra
, "printer-resolution-supported");
4525 cupsArrayAdd(ra
, "print-quality");
4526 cupsArrayAdd(ra
, "print-quality-default");
4527 cupsArrayAdd(ra
, "print-quality-supported");
4528 cupsArrayAdd(ra
, "sides");
4529 cupsArrayAdd(ra
, "sides-default");
4530 cupsArrayAdd(ra
, "sides-supported");
4532 else if (!strcmp(value
, "job-description"))
4534 cupsArrayAdd(ra
, "date-time-at-completed");
4535 cupsArrayAdd(ra
, "date-time-at-creation");
4536 cupsArrayAdd(ra
, "date-time-at-processing");
4537 cupsArrayAdd(ra
, "job-detailed-status-message");
4538 cupsArrayAdd(ra
, "job-document-access-errors");
4539 cupsArrayAdd(ra
, "job-id");
4540 cupsArrayAdd(ra
, "job-impressions");
4541 cupsArrayAdd(ra
, "job-impressions-completed");
4542 cupsArrayAdd(ra
, "job-k-octets");
4543 cupsArrayAdd(ra
, "job-k-octets-processed");
4544 cupsArrayAdd(ra
, "job-media-sheets");
4545 cupsArrayAdd(ra
, "job-media-sheets-completed");
4546 cupsArrayAdd(ra
, "job-message-from-operator");
4547 cupsArrayAdd(ra
, "job-more-info");
4548 cupsArrayAdd(ra
, "job-name");
4549 cupsArrayAdd(ra
, "job-originating-user-name");
4550 cupsArrayAdd(ra
, "job-printer-up-time");
4551 cupsArrayAdd(ra
, "job-printer-uri");
4552 cupsArrayAdd(ra
, "job-state");
4553 cupsArrayAdd(ra
, "job-state-message");
4554 cupsArrayAdd(ra
, "job-state-reasons");
4555 cupsArrayAdd(ra
, "job-uri");
4556 cupsArrayAdd(ra
, "number-of-documents");
4557 cupsArrayAdd(ra
, "number-of-intervening-jobs");
4558 cupsArrayAdd(ra
, "output-device-assigned");
4559 cupsArrayAdd(ra
, "time-at-completed");
4560 cupsArrayAdd(ra
, "time-at-creation");
4561 cupsArrayAdd(ra
, "time-at-processing");
4563 else if (!strcmp(value
, "printer-description"))
4565 cupsArrayAdd(ra
, "charset-configured");
4566 cupsArrayAdd(ra
, "charset-supported");
4567 cupsArrayAdd(ra
, "color-supported");
4568 cupsArrayAdd(ra
, "compression-supported");
4569 cupsArrayAdd(ra
, "document-format-default");
4570 cupsArrayAdd(ra
, "document-format-supported");
4571 cupsArrayAdd(ra
, "generated-natural-language-supported");
4572 cupsArrayAdd(ra
, "ipp-versions-supported");
4573 cupsArrayAdd(ra
, "job-impressions-supported");
4574 cupsArrayAdd(ra
, "job-k-octets-supported");
4575 cupsArrayAdd(ra
, "job-media-sheets-supported");
4576 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
4577 cupsArrayAdd(ra
, "multiple-operation-time-out");
4578 cupsArrayAdd(ra
, "natural-language-configured");
4579 cupsArrayAdd(ra
, "notify-attributes-supported");
4580 cupsArrayAdd(ra
, "notify-lease-duration-default");
4581 cupsArrayAdd(ra
, "notify-lease-duration-supported");
4582 cupsArrayAdd(ra
, "notify-max-events-supported");
4583 cupsArrayAdd(ra
, "notify-notify-events-default");
4584 cupsArrayAdd(ra
, "notify-notify-events-supported");
4585 cupsArrayAdd(ra
, "notify-pull-method-supported");
4586 cupsArrayAdd(ra
, "notify-schemes-supported");
4587 cupsArrayAdd(ra
, "operations-supported");
4588 cupsArrayAdd(ra
, "pages-per-minute");
4589 cupsArrayAdd(ra
, "pages-per-minute-color");
4590 cupsArrayAdd(ra
, "pdl-override-supported");
4591 cupsArrayAdd(ra
, "printer-current-time");
4592 cupsArrayAdd(ra
, "printer-driver-installer");
4593 cupsArrayAdd(ra
, "printer-info");
4594 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
4595 cupsArrayAdd(ra
, "printer-location");
4596 cupsArrayAdd(ra
, "printer-make-and-model");
4597 cupsArrayAdd(ra
, "printer-message-from-operator");
4598 cupsArrayAdd(ra
, "printer-more-info");
4599 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
4600 cupsArrayAdd(ra
, "printer-name");
4601 cupsArrayAdd(ra
, "printer-state");
4602 cupsArrayAdd(ra
, "printer-state-message");
4603 cupsArrayAdd(ra
, "printer-state-reasons");
4604 cupsArrayAdd(ra
, "printer-up-time");
4605 cupsArrayAdd(ra
, "printer-uri-supported");
4606 cupsArrayAdd(ra
, "queued-job-count");
4607 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
4608 cupsArrayAdd(ra
, "uri-authentication-supported");
4609 cupsArrayAdd(ra
, "uri-security-supported");
4611 else if (!strcmp(value
, "subscription-template"))
4613 cupsArrayAdd(ra
, "notify-attributes");
4614 cupsArrayAdd(ra
, "notify-charset");
4615 cupsArrayAdd(ra
, "notify-events");
4616 cupsArrayAdd(ra
, "notify-lease-duration");
4617 cupsArrayAdd(ra
, "notify-natural-language");
4618 cupsArrayAdd(ra
, "notify-pull-method");
4619 cupsArrayAdd(ra
, "notify-recipient-uri");
4620 cupsArrayAdd(ra
, "notify-time-interval");
4621 cupsArrayAdd(ra
, "notify-user-data");
4624 cupsArrayAdd(ra
, value
);
4632 * 'create_subscription()' - Create a notification subscription.
4636 create_subscription(
4637 cupsd_client_t
*con
, /* I - Client connection */
4638 ipp_attribute_t
*uri
) /* I - Printer URI */
4640 http_status_t status
; /* Policy status */
4641 int i
; /* Looping var */
4642 ipp_attribute_t
*attr
; /* Current attribute */
4643 const char *dest
; /* Destination */
4644 cups_ptype_t dtype
; /* Destination type (printer or class) */
4645 char method
[HTTP_MAX_URI
],
4646 /* Method portion of URI */
4647 userpass
[HTTP_MAX_URI
],
4648 /* Username portion of URI */
4650 /* Host portion of URI */
4651 resource
[HTTP_MAX_URI
];
4652 /* Resource portion of URI */
4653 int port
; /* Port portion of URI */
4654 cupsd_printer_t
*printer
; /* Printer/class */
4655 cupsd_job_t
*job
; /* Job */
4656 int jobid
; /* Job ID */
4657 cupsd_subscription_t
*sub
; /* Subscription object */
4658 const char *username
, /* requesting-user-name or authenticated username */
4659 *recipient
, /* notify-recipient-uri */
4660 *pullmethod
; /* notify-pull-method */
4661 ipp_attribute_t
*user_data
; /* notify-user-data */
4662 int interval
, /* notify-time-interval */
4663 lease
; /* notify-lease-duration */
4664 unsigned mask
; /* notify-events */
4668 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4670 if (attr
->group_tag
!= IPP_TAG_ZERO
)
4671 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
4672 attr
->value_tag
, attr
->name
);
4674 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
4679 * Is the destination valid?
4682 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4683 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
4684 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
4686 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
4687 sizeof(method
), userpass
, sizeof(userpass
), host
,
4688 sizeof(host
), &port
, resource
, sizeof(resource
));
4690 if (!strcmp(resource
, "/"))
4693 dtype
= (cups_ptype_t
)0;
4696 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
4699 dtype
= (cups_ptype_t
)0;
4702 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
4705 dtype
= CUPS_PRINTER_CLASS
;
4708 else if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
4714 send_ipp_status(con
, IPP_NOT_FOUND
,
4715 _("The printer or class was not found."));
4725 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
4727 send_http_error(con
, status
);
4731 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
4733 send_http_error(con
, status
);
4738 * Get the user that is requesting the subscription...
4741 username
= get_username(con
);
4744 * Find the first subscription group attribute; return if we have
4748 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4749 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
4754 send_ipp_status(con
, IPP_BAD_REQUEST
,
4755 _("No subscription attributes in request!"));
4760 * Process the subscription attributes in the request...
4763 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
4771 lease
= DefaultLeaseDuration
;
4773 mask
= CUPSD_EVENT_NONE
;
4775 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
4777 if (!strcmp(attr
->name
, "notify-recipient") &&
4778 attr
->value_tag
== IPP_TAG_URI
)
4779 recipient
= attr
->values
[0].string
.text
;
4780 else if (!strcmp(attr
->name
, "notify-pull-method") &&
4781 attr
->value_tag
== IPP_TAG_KEYWORD
)
4782 pullmethod
= attr
->values
[0].string
.text
;
4783 else if (!strcmp(attr
->name
, "notify-charset") &&
4784 attr
->value_tag
== IPP_TAG_CHARSET
&&
4785 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
4786 strcmp(attr
->values
[0].string
.text
, "utf-8"))
4788 send_ipp_status(con
, IPP_CHARSET
,
4789 _("Character set \"%s\" not supported!"),
4790 attr
->values
[0].string
.text
);
4793 else if (!strcmp(attr
->name
, "notify-natural-language") &&
4794 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
4795 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
4797 send_ipp_status(con
, IPP_CHARSET
,
4798 _("Language \"%s\" not supported!"),
4799 attr
->values
[0].string
.text
);
4802 else if (!strcmp(attr
->name
, "notify-user-data") &&
4803 attr
->value_tag
== IPP_TAG_STRING
)
4805 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
4807 send_ipp_status(con
, IPP_REQUEST_VALUE
,
4808 _("The notify-user-data value is too large "
4809 "(%d > 63 octets)!"),
4810 attr
->values
[0].unknown
.length
);
4816 else if (!strcmp(attr
->name
, "notify-events") &&
4817 attr
->value_tag
== IPP_TAG_KEYWORD
)
4819 for (i
= 0; i
< attr
->num_values
; i
++)
4820 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
4822 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
4823 attr
->value_tag
== IPP_TAG_INTEGER
)
4824 lease
= attr
->values
[0].integer
;
4825 else if (!strcmp(attr
->name
, "notify-time-interval") &&
4826 attr
->value_tag
== IPP_TAG_INTEGER
)
4827 interval
= attr
->values
[0].integer
;
4828 else if (!strcmp(attr
->name
, "notify-job-id") &&
4829 attr
->value_tag
== IPP_TAG_INTEGER
)
4830 jobid
= attr
->values
[0].integer
;
4836 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
4838 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
4839 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
4840 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
4842 if (!recipient
&& !pullmethod
)
4845 if (mask
== CUPSD_EVENT_NONE
)
4848 mask
= CUPSD_EVENT_JOB_COMPLETED
;
4850 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4853 send_ipp_status(con
, IPP_BAD_REQUEST
,
4854 _("notify-events not specified!"));
4859 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
4861 cupsdLogMessage(CUPSD_LOG_INFO
,
4862 "create_subscription: Limiting notify-lease-duration to "
4865 lease
= MaxLeaseDuration
;
4870 if ((job
= cupsdFindJob(jobid
)) == NULL
)
4872 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
4879 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
4882 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
4885 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4886 "Added subscription %d for printer \"%s\"",
4887 sub
->id
, printer
->name
);
4889 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
4892 sub
->interval
= interval
;
4894 sub
->expire
= lease
? time(NULL
) + lease
: 0;
4896 cupsdSetString(&sub
->owner
, username
);
4900 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
4901 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
4902 sub
->user_data_len
);
4905 ippAddSeparator(con
->response
);
4906 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4907 "notify-subscription-id", sub
->id
);
4909 con
->response
->request
.status
.status_code
= IPP_OK
;
4915 cupsdSaveAllSubscriptions();
4921 * 'delete_printer()' - Remove a printer or class from the system.
4925 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
4926 ipp_attribute_t
*uri
) /* I - URI of printer or class */
4928 http_status_t status
; /* Policy status */
4929 const char *dest
; /* Destination */
4930 cups_ptype_t dtype
; /* Destination type (printer or class) */
4931 char method
[HTTP_MAX_URI
], /* Method portion of URI */
4932 username
[HTTP_MAX_URI
], /* Username portion of URI */
4933 host
[HTTP_MAX_URI
], /* Host portion of URI */
4934 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
4935 int port
; /* Port portion of URI */
4936 cupsd_printer_t
*printer
; /* Printer/class */
4937 char filename
[1024]; /* Script/PPD filename */
4940 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
4941 con
->http
.fd
, uri
->values
[0].string
.text
);
4944 * Do we have a valid URI?
4947 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
4948 sizeof(method
), username
, sizeof(username
), host
,
4949 sizeof(host
), &port
, resource
, sizeof(resource
));
4951 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
4957 send_ipp_status(con
, IPP_NOT_FOUND
,
4958 _("The printer or class was not found."));
4966 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
4968 send_http_error(con
, status
);
4973 * Remove old jobs...
4976 cupsdCancelJobs(dest
, NULL
, 1);
4979 * Remove old subscriptions and send a "deleted printer" event...
4982 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
4983 "%s \"%s\" deleted by \"%s\".",
4984 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
4985 dest
, get_username(con
));
4987 cupsdExpireSubscriptions(printer
, NULL
);
4990 * Remove any old PPD or script files...
4993 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
, dest
);
4996 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
, dest
);
4999 if (dtype
& CUPS_PRINTER_CLASS
)
5001 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".", dest
,
5004 cupsdDeletePrinter(printer
, 0);
5005 cupsdSaveAllClasses();
5009 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".", dest
,
5012 cupsdDeletePrinter(printer
, 0);
5013 cupsdSaveAllPrinters();
5016 cupsdWritePrintcap();
5019 * Return with no errors...
5022 con
->response
->request
.status
.status_code
= IPP_OK
;
5027 * 'get_default()' - Get the default destination.
5031 get_default(cupsd_client_t
*con
) /* I - Client connection */
5033 http_status_t status
; /* Policy status */
5034 cups_array_t
*ra
; /* Requested attributes array */
5037 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5043 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5045 send_http_error(con
, status
);
5051 ra
= create_requested_array(con
->request
);
5053 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5055 cupsArrayDelete(ra
);
5057 con
->response
->request
.status
.status_code
= IPP_OK
;
5060 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5065 * 'get_devices()' - Get the list of available devices on the local system.
5069 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5071 http_status_t status
; /* Policy status */
5072 int i
; /* Looping var */
5073 ipp_attribute_t
*limit
, /* Limit attribute */
5074 *requested
; /* requested-attributes attribute */
5075 char command
[1024], /* cups-deviced command */
5076 options
[1024], /* Options to pass to command */
5077 attrs
[1024], /* String for requested attributes */
5078 *aptr
; /* Pointer into string */
5079 int alen
; /* Length of attribute value */
5082 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5088 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5090 send_http_error(con
, status
);
5095 * Run cups-deviced command with the given options...
5098 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5099 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5104 for (i
= 0, aptr
= attrs
; i
< requested
->num_values
; i
++)
5107 * Check that we have enough room...
5110 alen
= strlen(requested
->values
[i
].string
.text
);
5111 if (alen
> (sizeof(attrs
) - (aptr
- attrs
) - 2))
5115 * Put commas between values...
5122 * Add the value to the end of the string...
5125 strcpy(aptr
, requested
->values
[i
].string
.text
);
5130 * If we have more attribute names than will fit, default to "all"...
5133 if (i
< requested
->num_values
)
5134 strcpy(attrs
, "all");
5137 strcpy(attrs
, "all");
5139 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5140 snprintf(options
, sizeof(options
),
5141 "%d+%d+%d+requested-attributes=%s",
5142 con
->request
->request
.op
.request_id
,
5143 limit
? limit
->values
[0].integer
: 0, (int)User
,
5146 if (cupsdSendCommand(con
, command
, options
, 1))
5149 * Command started successfully, don't send an IPP response here...
5152 ippDelete(con
->response
);
5153 con
->response
= NULL
;
5158 * Command failed, return "internal error" so the user knows something
5162 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5163 _("cups-deviced failed to execute."));
5169 * 'get_job_attrs()' - Get job attributes.
5173 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5174 ipp_attribute_t
*uri
) /* I - Job URI */
5176 http_status_t status
; /* Policy status */
5177 ipp_attribute_t
*attr
; /* Current attribute */
5178 int jobid
; /* Job ID */
5179 cupsd_job_t
*job
; /* Current job */
5180 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5181 username
[HTTP_MAX_URI
], /* Username portion of URI */
5182 host
[HTTP_MAX_URI
], /* Host portion of URI */
5183 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5184 int port
; /* Port portion of URI */
5185 cups_array_t
*ra
; /* Requested attributes array */
5188 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5189 con
->http
.fd
, uri
->values
[0].string
.text
);
5192 * See if we have a job URI or a printer URI...
5195 if (!strcmp(uri
->name
, "printer-uri"))
5198 * Got a printer URI; see if we also have a job-id attribute...
5201 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5202 IPP_TAG_INTEGER
)) == NULL
)
5204 send_ipp_status(con
, IPP_BAD_REQUEST
,
5205 _("Got a printer-uri attribute but no job-id!"));
5209 jobid
= attr
->values
[0].integer
;
5214 * Got a job URI; parse it to get the job ID...
5217 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5218 sizeof(method
), username
, sizeof(username
), host
,
5219 sizeof(host
), &port
, resource
, sizeof(resource
));
5221 if (strncmp(resource
, "/jobs/", 6))
5227 send_ipp_status(con
, IPP_BAD_REQUEST
,
5228 _("Bad job-uri attribute \"%s\"!"),
5229 uri
->values
[0].string
.text
);
5233 jobid
= atoi(resource
+ 6);
5237 * See if the job exists...
5240 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5243 * Nope - return a "not found" error...
5246 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5254 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5256 send_http_error(con
, status
);
5261 * Copy attributes...
5266 ra
= create_requested_array(con
->request
);
5267 copy_job_attrs(con
, job
, ra
);
5268 cupsArrayDelete(ra
);
5270 con
->response
->request
.status
.status_code
= IPP_OK
;
5275 * 'get_jobs()' - Get a list of jobs for the specified printer.
5279 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
5280 ipp_attribute_t
*uri
) /* I - Printer URI */
5282 http_status_t status
; /* Policy status */
5283 ipp_attribute_t
*attr
; /* Current attribute */
5284 const char *dest
; /* Destination */
5285 cups_ptype_t dtype
; /* Destination type (printer or class) */
5286 cups_ptype_t dmask
; /* Destination type mask */
5287 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5288 username
[HTTP_MAX_URI
], /* Username portion of URI */
5289 host
[HTTP_MAX_URI
], /* Host portion of URI */
5290 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5291 int port
; /* Port portion of URI */
5292 int completed
; /* Completed jobs? */
5293 int first_job_id
; /* First job ID */
5294 int limit
; /* Maximum number of jobs to return */
5295 int count
; /* Number of jobs that match */
5296 cupsd_job_t
*job
; /* Current job pointer */
5297 cupsd_printer_t
*printer
; /* Printer */
5298 cups_array_t
*list
; /* Which job list... */
5299 cups_array_t
*ra
; /* Requested attributes array */
5302 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
5303 uri
->values
[0].string
.text
);
5306 * Is the destination valid?
5309 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5310 sizeof(method
), username
, sizeof(username
), host
,
5311 sizeof(host
), &port
, resource
, sizeof(resource
));
5313 if (!strcmp(resource
, "/") ||
5314 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
5317 dtype
= (cups_ptype_t
)0;
5318 dmask
= (cups_ptype_t
)0;
5321 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5324 dtype
= (cups_ptype_t
)0;
5325 dmask
= CUPS_PRINTER_CLASS
;
5328 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5331 dtype
= CUPS_PRINTER_CLASS
;
5332 dmask
= CUPS_PRINTER_CLASS
;
5335 else if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
5341 send_ipp_status(con
, IPP_NOT_FOUND
,
5342 _("The printer or class was not found."));
5346 dmask
= CUPS_PRINTER_CLASS
;
5354 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
5356 send_http_error(con
, status
);
5360 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5362 send_http_error(con
, status
);
5367 * See if the "which-jobs" attribute have been specified...
5370 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
5371 IPP_TAG_KEYWORD
)) != NULL
&&
5372 !strcmp(attr
->values
[0].string
.text
, "completed"))
5377 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
5389 * See if they want to limit the number of jobs reported...
5392 if ((attr
= ippFindAttribute(con
->request
, "limit",
5393 IPP_TAG_INTEGER
)) != NULL
)
5394 limit
= attr
->values
[0].integer
;
5398 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
5399 IPP_TAG_INTEGER
)) != NULL
)
5400 first_job_id
= attr
->values
[0].integer
;
5405 * See if we only want to see jobs for a specific user...
5408 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
5409 IPP_TAG_BOOLEAN
)) != NULL
&&
5410 attr
->values
[0].boolean
)
5411 strlcpy(username
, get_username(con
), sizeof(username
));
5415 ra
= create_requested_array(con
->request
);
5418 * OK, build a list of jobs for this printer...
5421 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
5422 count
< limit
&& job
;
5423 job
= (cupsd_job_t
*)cupsArrayNext(list
))
5426 * Filter out jobs that don't match...
5429 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
5431 if ((dest
&& strcmp(job
->dest
, dest
)) &&
5432 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
5434 if ((job
->dtype
& dmask
) != dtype
&&
5435 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
5437 if (username
[0] && strcasecmp(username
, job
->username
))
5440 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
5443 if (job
->id
< first_job_id
)
5452 ippAddSeparator(con
->response
);
5456 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
5458 copy_job_attrs(con
, job
, ra
);
5461 cupsArrayDelete(ra
);
5463 con
->response
->request
.status
.status_code
= IPP_OK
;
5468 * 'get_notifications()' - Get events for a subscription.
5472 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
5474 int i
, j
; /* Looping vars */
5475 http_status_t status
; /* Policy status */
5476 cupsd_subscription_t
*sub
; /* Subscription */
5477 ipp_attribute_t
*ids
, /* notify-subscription-ids */
5478 *sequences
; /* notify-sequence-numbers */
5479 int min_seq
; /* Minimum sequence number */
5480 int interval
; /* Poll interval */
5483 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_subscription_attrs(con=%p[%d])",
5487 * Get subscription attributes...
5490 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
5492 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
5497 send_ipp_status(con
, IPP_BAD_REQUEST
,
5498 _("Missing notify-subscription-ids attribute!"));
5503 * Are the subscription IDs valid?
5506 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
5508 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
5511 * Bad subscription ID...
5514 send_ipp_status(con
, IPP_NOT_FOUND
,
5515 _("notify-subscription-id %d no good!"),
5516 ids
->values
[i
].integer
);
5524 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5526 con
, sub
->owner
)) != HTTP_OK
)
5528 send_http_error(con
, status
);
5533 * Check the subscription type and update the interval accordingly.
5536 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
5539 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
5541 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
5547 * Tell the client to poll again in N seconds...
5551 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5552 "notify-get-interval", interval
);
5554 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5555 "printer-up-time", time(NULL
));
5558 * Copy the subscription event attributes to the response.
5561 con
->response
->request
.status
.status_code
=
5562 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
5564 for (i
= 0; i
< ids
->num_values
; i
++)
5567 * Get the subscription and sequence number...
5570 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
5572 if (sequences
&& i
< sequences
->num_values
)
5573 min_seq
= sequences
->values
[i
].integer
;
5578 * If we don't have any new events, nothing to do here...
5581 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
5585 * Otherwise copy all of the new events...
5588 if (sub
->first_event_id
> min_seq
)
5591 j
= min_seq
- sub
->first_event_id
;
5593 for (; j
< sub
->num_events
; j
++)
5595 ippAddSeparator(con
->response
);
5597 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
5598 IPP_TAG_EVENT_NOTIFICATION
, 0);
5605 * 'get_ppds()' - Get the list of PPD files on the local system.
5609 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
5611 http_status_t status
; /* Policy status */
5612 int i
; /* Looping var */
5613 ipp_attribute_t
*limit
, /* Limit attribute */
5614 *make
, /* ppd-make attribute */
5615 *requested
; /* requested-attributes attribute */
5616 char command
[1024], /* cups-deviced command */
5617 options
[1024], /* Options to pass to command */
5618 attrs
[1024], /* String for requested attributes */
5619 *aptr
; /* Pointer into string */
5620 int alen
; /* Length of attribute value */
5623 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
5629 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5631 send_http_error(con
, status
);
5636 * Run cups-driverd command with the given options...
5639 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5640 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
5641 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5646 for (i
= 0, aptr
= attrs
; i
< requested
->num_values
; i
++)
5649 * Check that we have enough room...
5652 alen
= strlen(requested
->values
[i
].string
.text
);
5653 if (alen
> (sizeof(attrs
) - (aptr
- attrs
) - 2))
5657 * Put commas between values...
5664 * Add the value to the end of the string...
5667 strcpy(aptr
, requested
->values
[i
].string
.text
);
5672 * If we have more attribute names than will fit, default to "all"...
5675 if (i
< requested
->num_values
)
5676 strcpy(attrs
, "all");
5679 strcpy(attrs
, "all");
5681 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
5682 snprintf(options
, sizeof(options
),
5683 "list+%d+%d+requested-attributes=%s%s%s",
5684 con
->request
->request
.op
.request_id
,
5685 limit
? limit
->values
[0].integer
: 0,
5687 make
? "%20ppd-make=" : "",
5688 make
? make
->values
[0].string
.text
: "");
5690 if (cupsdSendCommand(con
, command
, options
, 0))
5693 * Command started successfully, don't send an IPP response here...
5696 ippDelete(con
->response
);
5697 con
->response
= NULL
;
5702 * Command failed, return "internal error" so the user knows something
5706 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5707 _("cups-driverd failed to execute."));
5713 * 'get_printer_attrs()' - Get printer attributes.
5717 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
5718 ipp_attribute_t
*uri
) /* I - Printer URI */
5720 http_status_t status
; /* Policy status */
5721 const char *dest
; /* Destination */
5722 cups_ptype_t dtype
; /* Destination type (printer or class) */
5723 char method
[HTTP_MAX_URI
],
5724 /* Method portion of URI */
5725 username
[HTTP_MAX_URI
],
5726 /* Username portion of URI */
5728 /* Host portion of URI */
5729 resource
[HTTP_MAX_URI
];
5730 /* Resource portion of URI */
5731 int port
; /* Port portion of URI */
5732 cupsd_printer_t
*printer
; /* Printer/class */
5733 cups_array_t
*ra
; /* Requested attributes array */
5736 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
5737 con
->http
.fd
, uri
->values
[0].string
.text
);
5740 * Is the destination valid?
5743 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5744 sizeof(method
), username
, sizeof(username
), host
,
5745 sizeof(host
), &port
, resource
, sizeof(resource
));
5747 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
5753 send_ipp_status(con
, IPP_NOT_FOUND
,
5754 _("The printer or class was not found."));
5762 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
5764 send_http_error(con
, status
);
5769 * Send the attributes...
5772 ra
= create_requested_array(con
->request
);
5774 copy_printer_attrs(con
, printer
, ra
);
5776 cupsArrayDelete(ra
);
5778 con
->response
->request
.status
.status_code
= IPP_OK
;
5783 * 'get_printers()' - Get a list of printers or classes.
5787 get_printers(cupsd_client_t
*con
, /* I - Client connection */
5788 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
5790 http_status_t status
; /* Policy status */
5791 ipp_attribute_t
*attr
; /* Current attribute */
5792 int limit
; /* Maximum number of printers to return */
5793 int count
; /* Number of printers that match */
5794 cupsd_printer_t
*printer
; /* Current printer pointer */
5795 int printer_type
, /* printer-type attribute */
5796 printer_mask
; /* printer-type-mask attribute */
5797 char *location
; /* Location string */
5798 const char *username
; /* Current user */
5799 char *first_printer_name
; /* first-printer-name attribute */
5800 cups_array_t
*ra
; /* Requested attributes array */
5803 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
5804 con
->http
.fd
, type
);
5810 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5812 send_http_error(con
, status
);
5817 * Check for printers...
5820 if (!Printers
|| !cupsArrayCount(Printers
))
5822 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
5827 * See if they want to limit the number of printers reported...
5830 if ((attr
= ippFindAttribute(con
->request
, "limit",
5831 IPP_TAG_INTEGER
)) != NULL
)
5832 limit
= attr
->values
[0].integer
;
5836 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
5837 IPP_TAG_NAME
)) != NULL
)
5838 first_printer_name
= attr
->values
[0].string
.text
;
5840 first_printer_name
= NULL
;
5843 * Support filtering...
5846 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
5847 IPP_TAG_ENUM
)) != NULL
)
5848 printer_type
= attr
->values
[0].integer
;
5852 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
5853 IPP_TAG_ENUM
)) != NULL
)
5854 printer_mask
= attr
->values
[0].integer
;
5858 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
5859 IPP_TAG_TEXT
)) != NULL
)
5860 location
= attr
->values
[0].string
.text
;
5864 if (con
->username
[0])
5865 username
= con
->username
;
5866 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
5867 IPP_TAG_NAME
)) != NULL
)
5868 username
= attr
->values
[0].string
.text
;
5872 ra
= create_requested_array(con
->request
);
5875 * OK, build a list of printers for this printer...
5878 if (first_printer_name
)
5880 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
5881 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5884 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5887 count
< limit
&& printer
;
5888 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
5890 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
5891 (printer
->type
& printer_mask
) == printer_type
&&
5892 (!location
|| !printer
->location
||
5893 !strcasecmp(printer
->location
, location
)))
5896 * If HideImplicitMembers is enabled, see if this printer or class
5897 * is a member of an implicit class...
5900 if (ImplicitClasses
&& HideImplicitMembers
&&
5901 printer
->in_implicit_class
)
5905 * If a username is specified, see if it is allowed or denied
5909 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
5913 * Add the group separator as needed...
5917 ippAddSeparator(con
->response
);
5922 * Send the attributes...
5925 copy_printer_attrs(con
, printer
, ra
);
5929 cupsArrayDelete(ra
);
5931 con
->response
->request
.status
.status_code
= IPP_OK
;
5936 * 'get_subscription_attrs()' - Get subscription attributes.
5940 get_subscription_attrs(
5941 cupsd_client_t
*con
, /* I - Client connection */
5942 int sub_id
) /* I - Subscription ID */
5944 http_status_t status
; /* Policy status */
5945 cupsd_subscription_t
*sub
; /* Subscription */
5946 cups_array_t
*ra
; /* Requested attributes array */
5949 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5950 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
5951 con
, con
->http
.fd
, sub_id
);
5954 * Is the subscription ID valid?
5957 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
5960 * Bad subscription ID...
5963 send_ipp_status(con
, IPP_NOT_FOUND
,
5964 _("notify-subscription-id %d no good!"), sub_id
);
5972 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5974 con
, sub
->owner
)) != HTTP_OK
)
5976 send_http_error(con
, status
);
5981 * Copy the subscription attributes to the response using the
5982 * requested-attributes attribute that may be provided by the client.
5985 ra
= create_requested_array(con
->request
);
5987 copy_subscription_attrs(con
, sub
, ra
);
5989 cupsArrayDelete(ra
);
5991 con
->response
->request
.status
.status_code
= IPP_OK
;
5996 * 'get_subscriptions()' - Get subscriptions.
6000 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
6001 ipp_attribute_t
*uri
) /* I - Printer/job URI */
6003 http_status_t status
; /* Policy status */
6004 int count
; /* Number of subscriptions */
6005 int limit
; /* Limit */
6006 cupsd_subscription_t
*sub
; /* Subscription */
6007 cups_array_t
*ra
; /* Requested attributes array */
6008 ipp_attribute_t
*attr
; /* Attribute */
6009 cups_ptype_t dtype
; /* Destination type (printer or class) */
6010 char method
[HTTP_MAX_URI
],
6011 /* Method portion of URI */
6012 username
[HTTP_MAX_URI
],
6013 /* Username portion of URI */
6015 /* Host portion of URI */
6016 resource
[HTTP_MAX_URI
];
6017 /* Resource portion of URI */
6018 int port
; /* Port portion of URI */
6019 cupsd_job_t
*job
; /* Job pointer */
6020 cupsd_printer_t
*printer
; /* Printer */
6023 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6024 "get_subscriptions(con=%p[%d], uri=%s)",
6025 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
6028 * Is the destination valid?
6031 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6032 sizeof(method
), username
, sizeof(username
), host
,
6033 sizeof(host
), &port
, resource
, sizeof(resource
));
6035 if (!strcmp(resource
, "/") ||
6036 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
6037 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
6038 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
6043 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
6046 job
= cupsdFindJob(atoi(resource
+ 6));
6050 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
6055 else if (!cupsdValidateDest(host
, resource
, &dtype
, &printer
))
6061 send_ipp_status(con
, IPP_NOT_FOUND
,
6062 _("The printer or class was not found."));
6065 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
6066 IPP_TAG_INTEGER
)) != NULL
)
6068 job
= cupsdFindJob(attr
->values
[0].integer
);
6072 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
6073 attr
->values
[0].integer
);
6084 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
6086 con
, NULL
)) != HTTP_OK
)
6088 send_http_error(con
, status
);
6093 * Copy the subscription attributes to the response using the
6094 * requested-attributes attribute that may be provided by the client.
6097 ra
= create_requested_array(con
->request
);
6099 if ((attr
= ippFindAttribute(con
->request
, "limit",
6100 IPP_TAG_INTEGER
)) != NULL
)
6101 limit
= attr
->values
[0].integer
;
6106 * See if we only want to see subscriptions for a specific user...
6109 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
6110 IPP_TAG_BOOLEAN
)) != NULL
&&
6111 attr
->values
[0].boolean
)
6112 strlcpy(username
, get_username(con
), sizeof(username
));
6116 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
6118 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
6119 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
6120 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
6122 ippAddSeparator(con
->response
);
6123 copy_subscription_attrs(con
, sub
, ra
);
6126 if (limit
&& count
>= limit
)
6130 cupsArrayDelete(ra
);
6133 con
->response
->request
.status
.status_code
= IPP_OK
;
6135 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
6140 * 'get_username()' - Get the username associated with a request.
6143 static const char * /* O - Username */
6144 get_username(cupsd_client_t
*con
) /* I - Connection */
6146 ipp_attribute_t
*attr
; /* Attribute */
6149 if (con
->username
[0])
6150 return (con
->username
);
6151 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6152 IPP_TAG_NAME
)) != NULL
)
6153 return (attr
->values
[0].string
.text
);
6155 return ("anonymous");
6160 * 'hold_job()' - Hold a print job.
6164 hold_job(cupsd_client_t
*con
, /* I - Client connection */
6165 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
6167 ipp_attribute_t
*attr
, /* Current job-hold-until */
6168 *newattr
; /* New job-hold-until */
6169 int jobid
; /* Job ID */
6170 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6171 username
[HTTP_MAX_URI
], /* Username portion of URI */
6172 host
[HTTP_MAX_URI
], /* Host portion of URI */
6173 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6174 int port
; /* Port portion of URI */
6175 cupsd_job_t
*job
; /* Job information */
6178 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
6179 uri
->values
[0].string
.text
);
6182 * See if we have a job URI or a printer URI...
6185 if (!strcmp(uri
->name
, "printer-uri"))
6188 * Got a printer URI; see if we also have a job-id attribute...
6191 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6192 IPP_TAG_INTEGER
)) == NULL
)
6194 send_ipp_status(con
, IPP_BAD_REQUEST
,
6195 _("Got a printer-uri attribute but no job-id!"));
6199 jobid
= attr
->values
[0].integer
;
6204 * Got a job URI; parse it to get the job ID...
6207 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6208 sizeof(method
), username
, sizeof(username
), host
,
6209 sizeof(host
), &port
, resource
, sizeof(resource
));
6211 if (strncmp(resource
, "/jobs/", 6))
6217 send_ipp_status(con
, IPP_BAD_REQUEST
,
6218 _("Bad job-uri attribute \"%s\"!"),
6219 uri
->values
[0].string
.text
);
6223 jobid
= atoi(resource
+ 6);
6227 * See if the job exists...
6230 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6233 * Nope - return a "not found" error...
6236 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6241 * See if the job is owned by the requesting user...
6244 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6246 send_http_error(con
, HTTP_UNAUTHORIZED
);
6251 * Hold the job and return...
6256 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
6257 IPP_TAG_KEYWORD
)) == NULL
)
6258 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
6260 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
6261 IPP_TAG_KEYWORD
)) == NULL
)
6262 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
6267 * Free the old hold value and copy the new one over...
6270 _cupsStrFree(attr
->values
[0].string
.text
);
6274 attr
->value_tag
= newattr
->value_tag
;
6275 attr
->values
[0].string
.text
=
6276 _cupsStrAlloc(newattr
->values
[0].string
.text
);
6280 attr
->value_tag
= IPP_TAG_KEYWORD
;
6281 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
6285 * Hold job until specified time...
6288 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
6291 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was held by \"%s\".", jobid
,
6294 con
->response
->request
.status
.status_code
= IPP_OK
;
6299 * 'move_job()' - Move a job to a new destination.
6303 move_job(cupsd_client_t
*con
, /* I - Client connection */
6304 ipp_attribute_t
*uri
) /* I - Job URI */
6306 http_status_t status
; /* Policy status */
6307 ipp_attribute_t
*attr
; /* Current attribute */
6308 int jobid
; /* Job ID */
6309 cupsd_job_t
*job
; /* Current job */
6310 const char *src
, /* Source printer/class */
6311 *dest
; /* Destination */
6312 cups_ptype_t stype
, /* Source type (printer or class) */
6313 dtype
; /* Destination type (printer or class) */
6314 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6315 username
[HTTP_MAX_URI
], /* Username portion of URI */
6316 host
[HTTP_MAX_URI
], /* Host portion of URI */
6317 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6318 int port
; /* Port portion of URI */
6319 cupsd_printer_t
*sprinter
, /* Source printer */
6320 *dprinter
; /* Destination printer */
6323 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
6324 uri
->values
[0].string
.text
);
6327 * Get the new printer or class...
6330 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
6331 IPP_TAG_URI
)) == NULL
)
6334 * Need job-printer-uri...
6337 send_ipp_status(con
, IPP_BAD_REQUEST
,
6338 _("job-printer-uri attribute missing!"));
6342 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, method
,
6343 sizeof(method
), username
, sizeof(username
), host
,
6344 sizeof(host
), &port
, resource
, sizeof(resource
));
6346 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &dprinter
)) == NULL
)
6352 send_ipp_status(con
, IPP_NOT_FOUND
,
6353 _("The printer or class was not found."));
6361 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6363 send_http_error(con
, status
);
6368 * See if we have a job URI or a printer URI...
6371 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6372 sizeof(method
), username
, sizeof(username
), host
,
6373 sizeof(host
), &port
, resource
, sizeof(resource
));
6375 if (!strcmp(uri
->name
, "printer-uri"))
6378 * Got a printer URI; see if we also have a job-id attribute...
6381 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6382 IPP_TAG_INTEGER
)) == NULL
)
6388 if ((src
= cupsdValidateDest(host
, resource
, &stype
, &sprinter
)) == NULL
)
6394 send_ipp_status(con
, IPP_NOT_FOUND
,
6395 _("The printer or class was not found."));
6404 * Otherwise, just move a single job...
6407 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
6410 * Nope - return a "not found" error...
6413 send_ipp_status(con
, IPP_NOT_FOUND
,
6414 _("Job #%d does not exist!"), attr
->values
[0].integer
);
6420 * Job found, initialize source pointers...
6431 * Got a job URI; parse it to get the job ID...
6434 if (strncmp(resource
, "/jobs/", 6))
6440 send_ipp_status(con
, IPP_BAD_REQUEST
,
6441 _("Bad job-uri attribute \"%s\"!"),
6442 uri
->values
[0].string
.text
);
6447 * See if the job exists...
6450 jobid
= atoi(resource
+ 6);
6452 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6455 * Nope - return a "not found" error...
6458 send_ipp_status(con
, IPP_NOT_FOUND
,
6459 _("Job #%d does not exist!"), jobid
);
6465 * Job found, initialize source pointers...
6474 * Now move the job or jobs...
6480 * See if the job has been completed...
6483 if (job
->state_value
> IPP_JOB_STOPPED
)
6486 * Return a "not-possible" error...
6489 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6490 _("Job #%d is finished and cannot be altered!"),
6496 * See if the job is owned by the requesting user...
6499 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6501 send_http_error(con
, HTTP_UNAUTHORIZED
);
6506 * Move the job to a different printer or class...
6509 cupsdMoveJob(job
, dest
);
6514 * Got the source printer, now look through the jobs...
6517 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
6519 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
6522 * See if the job is pointing at the source printer or has not been
6526 if (strcasecmp(job
->dest
, src
) ||
6527 job
->state_value
> IPP_JOB_STOPPED
)
6531 * See if the job can be moved by the requesting user...
6534 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6538 * Move the job to a different printer or class...
6541 cupsdMoveJob(job
, dest
);
6546 * Start jobs if possible...
6552 * Return with "everything is OK" status...
6555 con
->response
->request
.status
.status_code
= IPP_OK
;
6560 * 'ppd_add_default()' - Add a PPD default choice.
6563 static int /* O - Number of defaults */
6565 const char *option
, /* I - Option name */
6566 const char *choice
, /* I - Choice name */
6567 int num_defaults
, /* I - Number of defaults */
6568 ppd_default_t
**defaults
) /* IO - Defaults */
6570 int i
; /* Looping var */
6571 ppd_default_t
*temp
; /* Temporary defaults array */
6575 * First check if the option already has a default value; the PPD spec
6576 * says that the first one is used...
6579 for (i
= 0, temp
= *defaults
; i
< num_defaults
; i
++)
6580 if (!strcmp(option
, temp
[i
].option
))
6581 return (num_defaults
);
6584 * Now add the option...
6587 if (num_defaults
== 0)
6588 temp
= malloc(sizeof(ppd_default_t
));
6590 temp
= realloc(*defaults
, (num_defaults
+ 1) * sizeof(ppd_default_t
));
6594 cupsdLogMessage(CUPSD_LOG_ERROR
, "ppd_add_default: Unable to add default value for \"%s\" - %s",
6595 option
, strerror(errno
));
6596 return (num_defaults
);
6600 temp
+= num_defaults
;
6602 strlcpy(temp
->option
, option
, sizeof(temp
->option
));
6603 strlcpy(temp
->choice
, choice
, sizeof(temp
->choice
));
6605 return (num_defaults
+ 1);
6610 * 'ppd_parse_line()' - Parse a PPD default line.
6613 static int /* O - 0 on success, -1 on failure */
6614 ppd_parse_line(const char *line
, /* I - Line */
6615 char *option
, /* O - Option name */
6616 int olen
, /* I - Size of option name */
6617 char *choice
, /* O - Choice name */
6618 int clen
) /* I - Size of choice name */
6621 * Verify this is a default option line...
6624 if (strncmp(line
, "*Default", 8))
6628 * Read the option name...
6631 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
6641 * Skip everything else up to the colon (:)...
6644 while (*line
&& *line
!= ':')
6653 * Now grab the option choice, skipping leading whitespace...
6656 while (isspace(*line
& 255))
6659 for (clen
--; isalnum(*line
& 255); line
++)
6669 * Return with no errors...
6677 * 'print_job()' - Print a file to a printer or class.
6681 print_job(cupsd_client_t
*con
, /* I - Client connection */
6682 ipp_attribute_t
*uri
) /* I - Printer URI */
6684 ipp_attribute_t
*attr
; /* Current attribute */
6685 ipp_attribute_t
*format
; /* Document-format attribute */
6686 cupsd_job_t
*job
; /* New job */
6687 char filename
[1024]; /* Job filename */
6688 mime_type_t
*filetype
; /* Type of file */
6689 char super
[MIME_MAX_SUPER
], /* Supertype of file */
6690 type
[MIME_MAX_TYPE
], /* Subtype of file */
6691 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
6692 /* Textual name of mime type */
6693 cupsd_printer_t
*printer
; /* Printer data */
6694 struct stat fileinfo
; /* File information */
6695 int kbytes
; /* Size of file */
6696 int compression
; /* Document compression */
6699 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
6700 uri
->values
[0].string
.text
);
6703 * Validate print file attributes, for now just document-format and
6704 * compression (CUPS only supports "none" and "gzip")...
6707 compression
= CUPS_FILE_NONE
;
6709 if ((attr
= ippFindAttribute(con
->request
, "compression",
6710 IPP_TAG_KEYWORD
)) != NULL
)
6712 if (strcmp(attr
->values
[0].string
.text
, "none")
6714 && strcmp(attr
->values
[0].string
.text
, "gzip")
6715 #endif /* HAVE_LIBZ */
6718 send_ipp_status(con
, IPP_ATTRIBUTES
,
6719 _("Unsupported compression \"%s\"!"),
6720 attr
->values
[0].string
.text
);
6721 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6722 "compression", NULL
, attr
->values
[0].string
.text
);
6727 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
6728 compression
= CUPS_FILE_GZIP
;
6729 #endif /* HAVE_LIBZ */
6733 * Do we have a file to print?
6738 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
6743 * Is it a format we support?
6746 if ((format
= ippFindAttribute(con
->request
, "document-format",
6747 IPP_TAG_MIMETYPE
)) != NULL
)
6750 * Grab format from client...
6753 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
6755 send_ipp_status(con
, IPP_BAD_REQUEST
,
6756 _("Could not scan type \"%s\"!"),
6757 format
->values
[0].string
.text
);
6764 * No document format attribute? Auto-type it!
6767 strcpy(super
, "application");
6768 strcpy(type
, "octet-stream");
6771 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
6774 * Auto-type the file...
6777 ipp_attribute_t
*doc_name
; /* document-name attribute */
6780 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
6782 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
6783 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
6784 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
6790 * Replace the document-format attribute value with the auto-typed one.
6793 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
6798 _cupsStrFree(format
->values
[0].string
.text
);
6800 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
6803 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
6804 "document-format", NULL
, mimetype
);
6807 filetype
= mimeType(MimeDatabase
, super
, type
);
6810 filetype
= mimeType(MimeDatabase
, super
, type
);
6814 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
6815 _("Unsupported format \'%s/%s\'!"), super
, type
);
6816 cupsdLogMessage(CUPSD_LOG_INFO
,
6817 "Hint: Do you have the raw file printing rules enabled?");
6820 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
6821 "document-format", NULL
, format
->values
[0].string
.text
);
6826 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: request file type is %s/%s.",
6827 filetype
->super
, filetype
->type
);
6830 * Read any embedded job ticket info from PS files...
6833 if (!strcasecmp(filetype
->super
, "application") &&
6834 !strcasecmp(filetype
->type
, "postscript"))
6835 read_ps_job_ticket(con
);
6838 * Create the job object...
6841 if ((job
= add_job(con
, uri
, &printer
)) == NULL
)
6845 * Update quota data...
6848 if (stat(con
->filename
, &fileinfo
))
6851 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
6853 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6855 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
6856 IPP_TAG_INTEGER
)) != NULL
)
6857 attr
->values
[0].integer
+= kbytes
;
6860 * Add the job file...
6863 if (add_file(con
, job
, filetype
, compression
))
6866 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
6868 rename(con
->filename
, filename
);
6869 cupsdClearString(&con
->filename
);
6872 * See if we need to add the ending sheet...
6875 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
6876 attr
->num_values
> 1)
6882 cupsdLogMessage(CUPSD_LOG_INFO
, "Adding end banner page \"%s\" to job %d.",
6883 attr
->values
[1].string
.text
, job
->id
);
6885 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
6887 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6891 * Log and save the job...
6894 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d queued on \"%s\" by \"%s\".", job
->id
,
6895 job
->dest
, job
->username
);
6896 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Job %d hold_until = %d", job
->id
,
6897 (int)job
->hold_until
);
6902 * Start the job if possible...
6910 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
6912 * This function only gets called when printing a single PostScript
6913 * file using the Print-Job operation. It doesn't work for Create-Job +
6914 * Send-File, since the job attributes need to be set at job creation
6915 * time for banners to work. The embedded PS job ticket stuff is here
6916 * only to allow the Windows printer driver for CUPS to pass in JCL
6917 * options and IPP attributes which otherwise would be lost.
6919 * The format of a PS job ticket is simple:
6921 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
6923 * %cupsJobTicket: attr1=value1
6924 * %cupsJobTicket: attr2=value2
6926 * %cupsJobTicket: attrN=valueN
6928 * Job ticket lines must appear immediately after the first line that
6929 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
6930 * looking for job ticket info when it finds a line that does not begin
6931 * with "%cupsJobTicket:".
6933 * The maximum length of a job ticket line, including the prefix, is
6934 * 255 characters to conform with the Adobe DSC.
6936 * Read-only attributes are rejected with a notice to the error log in
6937 * case a malicious user tries anything. Since the job ticket is read
6938 * prior to attribute validation in print_job(), job ticket attributes
6939 * will go through the same validation as IPP attributes...
6943 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
6945 cups_file_t
*fp
; /* File to read from */
6946 char line
[256]; /* Line data */
6947 int num_options
; /* Number of options */
6948 cups_option_t
*options
; /* Options */
6949 ipp_t
*ticket
; /* New attributes */
6950 ipp_attribute_t
*attr
, /* Current attribute */
6951 *attr2
, /* Job attribute */
6952 *prev2
; /* Previous job attribute */
6956 * First open the print file...
6959 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
6961 cupsdLogMessage(CUPSD_LOG_ERROR
,
6962 "read_ps_job_ticket: Unable to open PostScript print file "
6969 * Skip the first line...
6972 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
6974 cupsdLogMessage(CUPSD_LOG_ERROR
,
6975 "read_ps_job_ticket: Unable to read from PostScript print "
6982 if (strncmp(line
, "%!PS-Adobe-", 11))
6985 * Not a DSC-compliant file, so no job ticket info will be available...
6993 * Read job ticket info from the file...
6999 while (cupsFileGets(fp
, line
, sizeof(line
)))
7002 * Stop at the first non-ticket line...
7005 if (strncmp(line
, "%cupsJobTicket:", 15))
7009 * Add the options to the option array...
7012 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
7016 * Done with the file; see if we have any options...
7021 if (num_options
== 0)
7025 * OK, convert the options to an attribute list, and apply them to
7030 cupsEncodeOptions(ticket
, num_options
, options
);
7033 * See what the user wants to change.
7036 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
7038 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
7041 if (!strcmp(attr
->name
, "job-originating-host-name") ||
7042 !strcmp(attr
->name
, "job-originating-user-name") ||
7043 !strcmp(attr
->name
, "job-media-sheets-completed") ||
7044 !strcmp(attr
->name
, "job-k-octets") ||
7045 !strcmp(attr
->name
, "job-id") ||
7046 !strncmp(attr
->name
, "job-state", 9) ||
7047 !strncmp(attr
->name
, "time-at-", 8))
7048 continue; /* Read-only attrs */
7050 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
7051 IPP_TAG_ZERO
)) != NULL
)
7054 * Some other value; first free the old value...
7057 if (con
->request
->attrs
== attr2
)
7059 con
->request
->attrs
= attr2
->next
;
7064 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
7065 if (prev2
->next
== attr2
)
7067 prev2
->next
= attr2
->next
;
7072 if (con
->request
->last
== attr2
)
7073 con
->request
->last
= prev2
;
7075 _ippFreeAttr(attr2
);
7079 * Add new option by copying it...
7082 copy_attribute(con
->request
, attr
, 0);
7086 * Then free the attribute list and option array...
7090 cupsFreeOptions(num_options
, options
);
7095 * 'reject_jobs()' - Reject print jobs to a printer.
7099 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
7100 ipp_attribute_t
*uri
) /* I - Printer or class URI */
7102 http_status_t status
; /* Policy status */
7103 cups_ptype_t dtype
; /* Destination type (printer or class) */
7104 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7105 username
[HTTP_MAX_URI
], /* Username portion of URI */
7106 host
[HTTP_MAX_URI
], /* Host portion of URI */
7107 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7108 int port
; /* Port portion of URI */
7109 const char *name
; /* Printer name */
7110 cupsd_printer_t
*printer
; /* Printer data */
7111 ipp_attribute_t
*attr
; /* printer-state-message text */
7114 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
7115 con
->http
.fd
, uri
->values
[0].string
.text
);
7118 * Is the destination valid?
7121 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7122 sizeof(method
), username
, sizeof(username
), host
,
7123 sizeof(host
), &port
, resource
, sizeof(resource
));
7125 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
7131 send_ipp_status(con
, IPP_NOT_FOUND
,
7132 _("The printer or class was not found."));
7140 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7142 send_http_error(con
, status
);
7147 * Reject jobs sent to the printer...
7150 printer
->accepting
= 0;
7152 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
7153 IPP_TAG_TEXT
)) == NULL
)
7154 strcpy(printer
->state_message
, "Rejecting Jobs");
7156 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
7157 sizeof(printer
->state_message
));
7159 cupsdAddPrinterHistory(printer
);
7161 if (dtype
& CUPS_PRINTER_CLASS
)
7163 cupsdSaveAllClasses();
7165 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
7166 name
, get_username(con
));
7170 cupsdSaveAllPrinters();
7172 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
7173 name
, get_username(con
));
7177 * Everything was ok, so return OK status...
7180 con
->response
->request
.status
.status_code
= IPP_OK
;
7185 * 'release_job()' - Release a held print job.
7189 release_job(cupsd_client_t
*con
, /* I - Client connection */
7190 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7192 ipp_attribute_t
*attr
; /* Current attribute */
7193 int jobid
; /* Job ID */
7194 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7195 username
[HTTP_MAX_URI
], /* Username portion of URI */
7196 host
[HTTP_MAX_URI
], /* Host portion of URI */
7197 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7198 int port
; /* Port portion of URI */
7199 cupsd_job_t
*job
; /* Job information */
7202 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
7203 con
->http
.fd
, uri
->values
[0].string
.text
);
7206 * See if we have a job URI or a printer URI...
7209 if (!strcmp(uri
->name
, "printer-uri"))
7212 * Got a printer URI; see if we also have a job-id attribute...
7215 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7216 IPP_TAG_INTEGER
)) == NULL
)
7218 send_ipp_status(con
, IPP_BAD_REQUEST
,
7219 _("Got a printer-uri attribute but no job-id!"));
7223 jobid
= attr
->values
[0].integer
;
7228 * Got a job URI; parse it to get the job ID...
7231 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7232 sizeof(method
), username
, sizeof(username
), host
,
7233 sizeof(host
), &port
, resource
, sizeof(resource
));
7235 if (strncmp(resource
, "/jobs/", 6))
7241 send_ipp_status(con
, IPP_BAD_REQUEST
,
7242 _("Bad job-uri attribute \"%s\"!"),
7243 uri
->values
[0].string
.text
);
7247 jobid
= atoi(resource
+ 6);
7251 * See if the job exists...
7254 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7257 * Nope - return a "not found" error...
7260 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7265 * See if job is "held"...
7268 if (job
->state_value
!= IPP_JOB_HELD
)
7271 * Nope - return a "not possible" error...
7274 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
7279 * See if the job is owned by the requesting user...
7282 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7284 send_http_error(con
, HTTP_UNAUTHORIZED
);
7289 * Reset the job-hold-until value to "no-hold"...
7292 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7293 IPP_TAG_KEYWORD
)) == NULL
)
7294 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7298 _cupsStrFree(attr
->values
[0].string
.text
);
7300 attr
->value_tag
= IPP_TAG_KEYWORD
;
7301 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
7305 * Release the job and return...
7308 cupsdReleaseJob(job
);
7310 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was released by \"%s\".", jobid
,
7313 con
->response
->request
.status
.status_code
= IPP_OK
;
7318 * 'renew_subscription()' - Renew an existing subscription...
7323 cupsd_client_t
*con
, /* I - Client connection */
7324 int sub_id
) /* I - Subscription ID */
7326 http_status_t status
; /* Policy status */
7327 cupsd_subscription_t
*sub
; /* Subscription */
7328 ipp_attribute_t
*lease
; /* notify-lease-duration */
7331 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7332 "renew_subscription(con=%p[%d], sub_id=%d)",
7333 con
, con
->http
.fd
, sub_id
);
7336 * Is the subscription ID valid?
7339 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7342 * Bad subscription ID...
7345 send_ipp_status(con
, IPP_NOT_FOUND
,
7346 _("notify-subscription-id %d no good!"), sub_id
);
7353 * Job subscriptions cannot be renewed...
7356 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7357 _("Job subscriptions cannot be renewed!"));
7365 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7367 con
, sub
->owner
)) != HTTP_OK
)
7369 send_http_error(con
, status
);
7374 * Renew the subscription...
7377 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
7380 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
7382 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
7384 cupsdLogMessage(CUPSD_LOG_INFO
,
7385 "renew_subscription: Limiting notify-lease-duration to "
7388 sub
->lease
= MaxLeaseDuration
;
7391 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
7393 cupsdSaveAllSubscriptions();
7395 con
->response
->request
.status
.status_code
= IPP_OK
;
7400 * 'restart_job()' - Restart an old print job.
7404 restart_job(cupsd_client_t
*con
, /* I - Client connection */
7405 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7407 ipp_attribute_t
*attr
; /* Current attribute */
7408 int jobid
; /* Job ID */
7409 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7410 username
[HTTP_MAX_URI
], /* Username portion of URI */
7411 host
[HTTP_MAX_URI
], /* Host portion of URI */
7412 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7413 int port
; /* Port portion of URI */
7414 cupsd_job_t
*job
; /* Job information */
7417 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
7418 con
->http
.fd
, uri
->values
[0].string
.text
);
7421 * See if we have a job URI or a printer URI...
7424 if (!strcmp(uri
->name
, "printer-uri"))
7427 * Got a printer URI; see if we also have a job-id attribute...
7430 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7431 IPP_TAG_INTEGER
)) == NULL
)
7433 send_ipp_status(con
, IPP_BAD_REQUEST
,
7434 _("Got a printer-uri attribute but no job-id!"));
7438 jobid
= attr
->values
[0].integer
;
7443 * Got a job URI; parse it to get the job ID...
7446 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7447 sizeof(method
), username
, sizeof(username
), host
,
7448 sizeof(host
), &port
, resource
, sizeof(resource
));
7450 if (strncmp(resource
, "/jobs/", 6))
7456 send_ipp_status(con
, IPP_BAD_REQUEST
,
7457 _("Bad job-uri attribute \"%s\"!"),
7458 uri
->values
[0].string
.text
);
7462 jobid
= atoi(resource
+ 6);
7466 * See if the job exists...
7469 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7472 * Nope - return a "not found" error...
7475 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7480 * See if job is in any of the "completed" states...
7483 if (job
->state_value
<= IPP_JOB_PROCESSING
)
7486 * Nope - return a "not possible" error...
7489 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
7495 * See if we have retained the job files...
7500 if (!job
->attrs
||job
->num_files
== 0)
7503 * Nope - return a "not possible" error...
7506 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7507 _("Job #%d cannot be restarted - no files!"), jobid
);
7512 * See if the job is owned by the requesting user...
7515 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7517 send_http_error(con
, HTTP_UNAUTHORIZED
);
7522 * Restart the job and return...
7525 cupsdRestartJob(job
);
7527 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was restarted by \"%s\".", jobid
,
7530 con
->response
->request
.status
.status_code
= IPP_OK
;
7535 * 'save_auth_info()' - Save authentication information for a job.
7539 save_auth_info(cupsd_client_t
*con
, /* I - Client connection */
7540 cupsd_job_t
*job
) /* I - Job */
7542 int i
; /* Looping var */
7543 char filename
[1024]; /* Job authentication filename */
7544 cups_file_t
*fp
; /* Job authentication file */
7545 char line
[1024]; /* Line for file */
7549 * This function saves the in-memory authentication information for
7550 * a job so that it can be used to authenticate with a remote host.
7551 * The information is stored in a file that is readable only by the
7552 * root user. The username and password are Base-64 encoded, each
7553 * on a separate line, followed by random number (up to 1024) of
7554 * newlines to limit the amount of information that is exposed.
7556 * Because of the potential for exposing of authentication information,
7557 * this functionality is only enabled when running cupsd as root.
7559 * This caching only works for the Basic and BasicDigest authentication
7560 * types. Digest authentication cannot be cached this way, and in
7561 * the future Kerberos authentication may make all of this obsolete.
7563 * Authentication information is saved whenever an authenticated
7564 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
7567 * This information is deleted after a job is completed or canceled,
7568 * so reprints may require subsequent re-authentication.
7575 * Create the authentication file and change permissions...
7578 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
7579 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
7581 cupsdLogMessage(CUPSD_LOG_ERROR
,
7582 "Unable to save authentication info to \"%s\" - %s",
7583 filename
, strerror(errno
));
7587 fchown(cupsFileNumber(fp
), 0, 0);
7588 fchmod(cupsFileNumber(fp
), 0400);
7591 * Write the authenticated username...
7594 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
7595 cupsFilePrintf(fp
, "%s\n", line
);
7598 * Write the authenticated password...
7601 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
7602 cupsFilePrintf(fp
, "%s\n", line
);
7605 * Write a random number of newlines to the end of the file...
7608 for (i
= (rand() % 1024); i
>= 0; i
--)
7609 cupsFilePutChar(fp
, '\n');
7612 * Close the file and return...
7620 * 'send_document()' - Send a file to a printer or class.
7624 send_document(cupsd_client_t
*con
, /* I - Client connection */
7625 ipp_attribute_t
*uri
) /* I - Printer URI */
7627 ipp_attribute_t
*attr
; /* Current attribute */
7628 ipp_attribute_t
*format
; /* Document-format attribute */
7629 int jobid
; /* Job ID number */
7630 cupsd_job_t
*job
; /* Current job */
7631 char job_uri
[HTTP_MAX_URI
],
7633 method
[HTTP_MAX_URI
],
7634 /* Method portion of URI */
7635 username
[HTTP_MAX_URI
],
7636 /* Username portion of URI */
7638 /* Host portion of URI */
7639 resource
[HTTP_MAX_URI
];
7640 /* Resource portion of URI */
7641 int port
; /* Port portion of URI */
7642 mime_type_t
*filetype
; /* Type of file */
7643 char super
[MIME_MAX_SUPER
],
7644 /* Supertype of file */
7645 type
[MIME_MAX_TYPE
],
7646 /* Subtype of file */
7647 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7648 /* Textual name of mime type */
7649 char filename
[1024]; /* Job filename */
7650 cupsd_printer_t
*printer
; /* Current printer */
7651 struct stat fileinfo
; /* File information */
7652 int kbytes
; /* Size of file */
7653 int compression
; /* Type of compression */
7656 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
7657 con
->http
.fd
, uri
->values
[0].string
.text
);
7660 * See if we have a job URI or a printer URI...
7663 if (!strcmp(uri
->name
, "printer-uri"))
7666 * Got a printer URI; see if we also have a job-id attribute...
7669 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7670 IPP_TAG_INTEGER
)) == NULL
)
7672 send_ipp_status(con
, IPP_BAD_REQUEST
,
7673 _("Got a printer-uri attribute but no job-id!"));
7677 jobid
= attr
->values
[0].integer
;
7682 * Got a job URI; parse it to get the job ID...
7685 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7686 sizeof(method
), username
, sizeof(username
), host
,
7687 sizeof(host
), &port
, resource
, sizeof(resource
));
7689 if (strncmp(resource
, "/jobs/", 6))
7695 send_ipp_status(con
, IPP_BAD_REQUEST
,
7696 _("Bad job-uri attribute \"%s\"!"),
7697 uri
->values
[0].string
.text
);
7701 jobid
= atoi(resource
+ 6);
7705 * See if the job exists...
7708 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7711 * Nope - return a "not found" error...
7714 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7719 * See if the job is owned by the requesting user...
7722 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7724 send_http_error(con
, HTTP_UNAUTHORIZED
);
7729 * OK, see if the client is sending the document compressed - CUPS
7730 * only supports "none" and "gzip".
7733 compression
= CUPS_FILE_NONE
;
7735 if ((attr
= ippFindAttribute(con
->request
, "compression",
7736 IPP_TAG_KEYWORD
)) != NULL
)
7738 if (strcmp(attr
->values
[0].string
.text
, "none")
7740 && strcmp(attr
->values
[0].string
.text
, "gzip")
7741 #endif /* HAVE_LIBZ */
7744 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
7745 attr
->values
[0].string
.text
);
7746 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7747 "compression", NULL
, attr
->values
[0].string
.text
);
7752 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7753 compression
= CUPS_FILE_GZIP
;
7754 #endif /* HAVE_LIBZ */
7758 * Do we have a file to print?
7763 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7768 * Is it a format we support?
7771 if ((format
= ippFindAttribute(con
->request
, "document-format",
7772 IPP_TAG_MIMETYPE
)) != NULL
)
7775 * Grab format from client...
7778 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
7780 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
7781 format
->values
[0].string
.text
);
7788 * No document format attribute? Auto-type it!
7791 strcpy(super
, "application");
7792 strcpy(type
, "octet-stream");
7795 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7798 * Auto-type the file...
7801 ipp_attribute_t
*doc_name
; /* document-name attribute */
7804 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
7806 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7807 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7808 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7814 * Replace the document-format attribute value with the auto-typed one.
7817 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7822 _cupsStrFree(format
->values
[0].string
.text
);
7823 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7826 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7827 "document-format", NULL
, mimetype
);
7830 filetype
= mimeType(MimeDatabase
, super
, type
);
7833 filetype
= mimeType(MimeDatabase
, super
, type
);
7837 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7838 _("Unsupported format \'%s/%s\'!"), super
, type
);
7839 cupsdLogMessage(CUPSD_LOG_INFO
,
7840 "Hint: Do you have the raw file printing rules enabled?");
7843 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7844 "document-format", NULL
, format
->values
[0].string
.text
);
7849 cupsdLogMessage(CUPSD_LOG_DEBUG
,
7850 "send_document: request file type is %s/%s.",
7851 filetype
->super
, filetype
->type
);
7854 * Add the file to the job...
7859 if (add_file(con
, job
, filetype
, compression
))
7862 if (job
->dtype
& CUPS_PRINTER_CLASS
)
7863 printer
= cupsdFindClass(job
->dest
);
7865 printer
= cupsdFindPrinter(job
->dest
);
7867 if (stat(con
->filename
, &fileinfo
))
7870 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7872 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7874 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7875 IPP_TAG_INTEGER
)) != NULL
)
7876 attr
->values
[0].integer
+= kbytes
;
7878 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
7880 rename(con
->filename
, filename
);
7882 cupsdClearString(&con
->filename
);
7884 cupsdLogMessage(CUPSD_LOG_INFO
,
7885 "File of type %s/%s queued in job #%d by \"%s\".",
7886 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
7889 * Start the job if this is the last document...
7892 if ((attr
= ippFindAttribute(con
->request
, "last-document",
7893 IPP_TAG_BOOLEAN
)) != NULL
&&
7894 attr
->values
[0].boolean
)
7897 * See if we need to add the ending sheet...
7901 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
7902 (attr
= ippFindAttribute(job
->attrs
, "job-sheets",
7903 IPP_TAG_ZERO
)) != NULL
&&
7904 attr
->num_values
> 1)
7910 cupsdLogMessage(CUPSD_LOG_INFO
,
7911 "Adding end banner page \"%s\" to job %d.",
7912 attr
->values
[1].string
.text
, job
->id
);
7914 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
7916 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7919 if (job
->state_value
== IPP_JOB_STOPPED
)
7921 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7922 job
->state_value
= IPP_JOB_PENDING
;
7924 else if (job
->state_value
== IPP_JOB_HELD
)
7926 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7927 IPP_TAG_KEYWORD
)) == NULL
)
7928 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7930 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
7932 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7933 job
->state_value
= IPP_JOB_PENDING
;
7940 * Start the job if possible... Since cupsdCheckJobs() can cancel a
7941 * job if it doesn't print, we need to re-find the job afterwards...
7948 job
= cupsdFindJob(jobid
);
7952 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7953 IPP_TAG_KEYWORD
)) == NULL
)
7954 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7956 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
7958 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
7959 job
->state_value
= IPP_JOB_HELD
;
7960 job
->hold_until
= time(NULL
) + 60;
7966 * Fill in the response info...
7969 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
7972 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
7975 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
7977 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
7978 job
? job
->state_value
: IPP_JOB_CANCELLED
);
7979 add_job_state_reasons(con
, job
);
7981 con
->response
->request
.status
.status_code
= IPP_OK
;
7986 * 'send_http_error()' - Send a HTTP error back to the IPP client.
7990 send_http_error(cupsd_client_t
*con
, /* I - Client connection */
7991 http_status_t status
) /* I - HTTP status code */
7993 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
7994 ippOpString(con
->request
->request
.op
.operation_id
),
7995 httpStatus(status
));
7997 cupsdSendError(con
, status
);
7999 ippDelete(con
->response
);
8000 con
->response
= NULL
;
8007 * 'send_ipp_status()' - Send a status back to the IPP client.
8011 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
8012 ipp_status_t status
, /* I - IPP status code */
8013 const char *message
, /* I - Status message */
8014 ...) /* I - Additional args as needed */
8016 va_list ap
; /* Pointer to additional args */
8017 char formatted
[1024]; /* Formatted errror message */
8022 va_start(ap
, message
);
8023 vsnprintf(formatted
, sizeof(formatted
),
8024 _cupsLangString(con
->language
, message
), ap
);
8027 cupsdLogMessage(status
>= IPP_BAD_REQUEST
? CUPSD_LOG_ERROR
: CUPSD_LOG_INFO
,
8029 ippOpString(con
->request
->request
.op
.operation_id
),
8030 ippErrorString(status
), formatted
);
8033 cupsdLogMessage(status
>= IPP_BAD_REQUEST
? CUPSD_LOG_ERROR
: CUPSD_LOG_INFO
,
8035 ippOpString(con
->request
->request
.op
.operation_id
),
8036 ippErrorString(status
));
8038 con
->response
->request
.status
.status_code
= status
;
8040 if (ippFindAttribute(con
->response
, "attributes-charset",
8041 IPP_TAG_ZERO
) == NULL
)
8042 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
8043 "attributes-charset", NULL
, DefaultCharset
);
8045 if (ippFindAttribute(con
->response
, "attributes-natural-language",
8046 IPP_TAG_ZERO
) == NULL
)
8047 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
8048 "attributes-natural-language", NULL
, DefaultLanguage
);
8051 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
8052 "status-message", NULL
, formatted
);
8057 * 'set_default()' - Set the default destination...
8061 set_default(cupsd_client_t
*con
, /* I - Client connection */
8062 ipp_attribute_t
*uri
) /* I - Printer URI */
8064 http_status_t status
; /* Policy status */
8065 cups_ptype_t dtype
; /* Destination type (printer or class) */
8066 char method
[HTTP_MAX_URI
],
8067 /* Method portion of URI */
8068 username
[HTTP_MAX_URI
],
8069 /* Username portion of URI */
8071 /* Host portion of URI */
8072 resource
[HTTP_MAX_URI
];
8073 /* Resource portion of URI */
8074 int port
; /* Port portion of URI */
8075 const char *name
; /* Printer name */
8076 cupsd_printer_t
*printer
; /* Printer */
8079 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
8080 con
->http
.fd
, uri
->values
[0].string
.text
);
8083 * Is the destination valid?
8086 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8087 sizeof(method
), username
, sizeof(username
), host
,
8088 sizeof(host
), &port
, resource
, sizeof(resource
));
8090 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
8096 send_ipp_status(con
, IPP_NOT_FOUND
,
8097 _("The printer or class was not found."));
8105 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
8107 send_http_error(con
, status
);
8112 * Set it as the default...
8115 DefaultPrinter
= printer
;
8117 cupsdSaveAllPrinters();
8118 cupsdSaveAllClasses();
8120 cupsdWritePrintcap();
8122 cupsdLogMessage(CUPSD_LOG_INFO
,
8123 "Default destination set to \"%s\" by \"%s\".", name
,
8127 * Everything was ok, so return OK status...
8130 con
->response
->request
.status
.status_code
= IPP_OK
;
8135 * 'set_job_attrs()' - Set job attributes.
8139 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
8140 ipp_attribute_t
*uri
) /* I - Job URI */
8142 ipp_attribute_t
*attr
, /* Current attribute */
8143 *attr2
; /* Job attribute */
8144 int jobid
; /* Job ID */
8145 cupsd_job_t
*job
; /* Current job */
8146 char method
[HTTP_MAX_URI
],
8147 /* Method portion of URI */
8148 username
[HTTP_MAX_URI
],
8149 /* Username portion of URI */
8151 /* Host portion of URI */
8152 resource
[HTTP_MAX_URI
];
8153 /* Resource portion of URI */
8154 int port
; /* Port portion of URI */
8157 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
8158 con
->http
.fd
, uri
->values
[0].string
.text
);
8161 * Start with "everything is OK" status...
8164 con
->response
->request
.status
.status_code
= IPP_OK
;
8167 * See if we have a job URI or a printer URI...
8170 if (!strcmp(uri
->name
, "printer-uri"))
8173 * Got a printer URI; see if we also have a job-id attribute...
8176 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8177 IPP_TAG_INTEGER
)) == NULL
)
8179 send_ipp_status(con
, IPP_BAD_REQUEST
,
8180 _("Got a printer-uri attribute but no job-id!"));
8184 jobid
= attr
->values
[0].integer
;
8189 * Got a job URI; parse it to get the job ID...
8192 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8193 sizeof(method
), username
, sizeof(username
), host
,
8194 sizeof(host
), &port
, resource
, sizeof(resource
));
8196 if (strncmp(resource
, "/jobs/", 6))
8202 send_ipp_status(con
, IPP_BAD_REQUEST
,
8203 _("Bad job-uri attribute \"%s\"!"),
8204 uri
->values
[0].string
.text
);
8208 jobid
= atoi(resource
+ 6);
8212 * See if the job exists...
8215 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8218 * Nope - return a "not found" error...
8221 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8226 * See if the job has been completed...
8229 if (job
->state_value
> IPP_JOB_STOPPED
)
8232 * Return a "not-possible" error...
8235 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8236 _("Job #%d is finished and cannot be altered!"), jobid
);
8241 * See if the job is owned by the requesting user...
8244 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8246 send_http_error(con
, HTTP_UNAUTHORIZED
);
8251 * See what the user wants to change.
8256 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8258 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8261 if (!strcmp(attr
->name
, "attributes-charset") ||
8262 !strcmp(attr
->name
, "attributes-natural-language") ||
8263 !strcmp(attr
->name
, "document-compression") ||
8264 !strcmp(attr
->name
, "document-format") ||
8265 !strcmp(attr
->name
, "job-detailed-status-messages") ||
8266 !strcmp(attr
->name
, "job-document-access-errors") ||
8267 !strcmp(attr
->name
, "job-id") ||
8268 !strcmp(attr
->name
, "job-k-octets") ||
8269 !strcmp(attr
->name
, "job-originating-host-name") ||
8270 !strcmp(attr
->name
, "job-originating-user-name") ||
8271 !strcmp(attr
->name
, "job-printer-up-time") ||
8272 !strcmp(attr
->name
, "job-printer-uri") ||
8273 !strcmp(attr
->name
, "job-sheets") ||
8274 !strcmp(attr
->name
, "job-state-message") ||
8275 !strcmp(attr
->name
, "job-state-reasons") ||
8276 !strcmp(attr
->name
, "job-uri") ||
8277 !strcmp(attr
->name
, "number-of-documents") ||
8278 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
8279 !strcmp(attr
->name
, "output-device-assigned") ||
8280 !strncmp(attr
->name
, "date-time-at-", 13) ||
8281 !strncmp(attr
->name
, "job-impressions", 15) ||
8282 !strncmp(attr
->name
, "job-k-octets", 12) ||
8283 !strncmp(attr
->name
, "job-media-sheets", 16) ||
8284 !strncmp(attr
->name
, "time-at-", 8))
8290 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
8291 _("%s cannot be changed."), attr
->name
);
8293 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8294 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8299 if (!strcmp(attr
->name
, "job-priority"))
8302 * Change the job priority...
8305 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8307 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
8309 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8310 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8312 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
8314 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8315 _("Job is completed and cannot be changed."));
8318 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8319 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
8321 else if (!strcmp(attr
->name
, "job-state"))
8324 * Change the job state...
8327 if (attr
->value_tag
!= IPP_TAG_ENUM
)
8329 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
8331 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8332 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8336 switch (attr
->values
[0].integer
)
8338 case IPP_JOB_PENDING
:
8340 if (job
->state_value
> IPP_JOB_HELD
)
8342 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8343 _("Job state cannot be changed."));
8346 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8348 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8349 job
->state_value
= attr
->values
[0].integer
;
8353 case IPP_JOB_PROCESSING
:
8354 case IPP_JOB_STOPPED
:
8355 if (job
->state_value
!= attr
->values
[0].integer
)
8357 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8358 _("Job state cannot be changed."));
8363 case IPP_JOB_CANCELLED
:
8364 case IPP_JOB_ABORTED
:
8365 case IPP_JOB_COMPLETED
:
8366 if (job
->state_value
> IPP_JOB_PROCESSING
)
8368 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8369 _("Job state cannot be changed."));
8372 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8374 cupsdCancelJob(job
, 0);
8378 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8379 job
->state_value
= attr
->values
[0].integer
;
8387 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
8389 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8390 IPP_TAG_ZERO
)) != NULL
)
8393 * Some other value; first free the old value...
8396 if (job
->attrs
->prev
)
8397 job
->attrs
->prev
->next
= attr2
->next
;
8399 job
->attrs
->attrs
= attr2
->next
;
8401 if (job
->attrs
->last
== attr2
)
8402 job
->attrs
->last
= job
->attrs
->prev
;
8404 _ippFreeAttr(attr2
);
8407 * Then copy the attribute...
8410 copy_attribute(job
->attrs
, attr
, 0);
8413 * See if the job-name or job-hold-until is being changed.
8416 if (!strcmp(attr
->name
, "job-hold-until"))
8418 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
8420 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
8421 cupsdReleaseJob(job
);
8426 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
8429 * Delete the attribute...
8432 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8433 IPP_TAG_ZERO
)) != NULL
)
8435 if (job
->attrs
->prev
)
8436 job
->attrs
->prev
->next
= attr2
->next
;
8438 job
->attrs
->attrs
= attr2
->next
;
8440 if (attr2
== job
->attrs
->last
)
8441 job
->attrs
->last
= job
->attrs
->prev
;
8443 _ippFreeAttr(attr2
);
8449 * Add new option by copying it...
8452 copy_attribute(job
->attrs
, attr
, 0);
8463 * Start jobs if possible...
8471 * 'set_printer_defaults()' - Set printer default options from a request.
8475 set_printer_defaults(
8476 cupsd_client_t
*con
, /* I - Client connection */
8477 cupsd_printer_t
*printer
) /* I - Printer */
8479 int i
; /* Looping var */
8480 ipp_attribute_t
*attr
; /* Current attribute */
8481 int namelen
; /* Length of attribute name */
8482 char name
[256], /* New attribute name */
8483 value
[256]; /* String version of integer attrs */
8486 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8489 * Skip non-printer attributes...
8492 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
8495 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
8497 if (!strcmp(attr
->name
, "job-sheets-default"))
8500 * Only allow keywords and names...
8503 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8507 * Only allow job-sheets-default to be set when running without a
8508 * system high classification level...
8514 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
8516 if (attr
->num_values
> 1)
8517 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
8519 cupsdSetString(&printer
->job_sheets
[1], "none");
8521 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
8523 cupsdFreePrinterUsers(printer
);
8525 printer
->deny_users
= 0;
8527 if (attr
->value_tag
== IPP_TAG_NAME
&&
8528 (attr
->num_values
> 1 ||
8529 strcmp(attr
->values
[0].string
.text
, "all")))
8531 for (i
= 0; i
< attr
->num_values
; i
++)
8532 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8535 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
8537 cupsdFreePrinterUsers(printer
);
8539 printer
->deny_users
= 1;
8541 if (attr
->value_tag
== IPP_TAG_NAME
&&
8542 (attr
->num_values
> 1 ||
8543 strcmp(attr
->values
[0].string
.text
, "none")))
8545 for (i
= 0; i
< attr
->num_values
; i
++)
8546 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8549 else if (!strcmp(attr
->name
, "job-quota-period"))
8551 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8554 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
8555 attr
->values
[0].integer
);
8556 cupsdFreeQuotas(printer
);
8558 printer
->quota_period
= attr
->values
[0].integer
;
8560 else if (!strcmp(attr
->name
, "job-k-limit"))
8562 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8565 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
8566 attr
->values
[0].integer
);
8567 cupsdFreeQuotas(printer
);
8569 printer
->k_limit
= attr
->values
[0].integer
;
8571 else if (!strcmp(attr
->name
, "job-page-limit"))
8573 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8576 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
8577 attr
->values
[0].integer
);
8578 cupsdFreeQuotas(printer
);
8580 printer
->page_limit
= attr
->values
[0].integer
;
8582 else if (!strcmp(attr
->name
, "printer-op-policy"))
8584 cupsd_policy_t
*p
; /* Policy */
8587 if (attr
->value_tag
!= IPP_TAG_NAME
)
8590 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
8592 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8593 "Setting printer-op-policy to \"%s\"...",
8594 attr
->values
[0].string
.text
);
8595 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
8596 printer
->op_policy_ptr
= p
;
8600 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8601 _("Unknown printer-op-policy \"%s\"."),
8602 attr
->values
[0].string
.text
);
8606 else if (!strcmp(attr
->name
, "printer-error-policy"))
8608 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8611 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
8612 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
8613 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
8615 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8616 _("Unknown printer-error-policy \"%s\"."),
8617 attr
->values
[0].string
.text
);
8621 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8622 "Setting printer-error-policy to \"%s\"...",
8623 attr
->values
[0].string
.text
);
8624 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
8626 else if (!strcmp(attr
->name
, "document-format-default") ||
8627 !strcmp(attr
->name
, "notify-lease-duration-default") ||
8628 !strcmp(attr
->name
, "notify-notify-events-default"))
8632 * Skip any other non-default attributes...
8635 namelen
= strlen(attr
->name
);
8636 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
8637 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
8641 * OK, anything else must be a user-defined default...
8644 strlcpy(name
, attr
->name
, sizeof(name
));
8645 name
[namelen
- 8] = '\0'; /* Strip "-default" */
8647 switch (attr
->value_tag
)
8649 case IPP_TAG_DELETEATTR
:
8650 printer
->num_options
= cupsRemoveOption(name
,
8651 printer
->num_options
,
8652 &(printer
->options
));
8653 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8654 "Deleting %s", attr
->name
);
8658 case IPP_TAG_KEYWORD
:
8660 printer
->num_options
= cupsAddOption(name
,
8661 attr
->values
[0].string
.text
,
8662 printer
->num_options
,
8663 &(printer
->options
));
8664 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8665 "Setting %s to \"%s\"...", attr
->name
,
8666 attr
->values
[0].string
.text
);
8669 case IPP_TAG_BOOLEAN
:
8670 printer
->num_options
= cupsAddOption(name
,
8671 attr
->values
[0].boolean
?
8673 printer
->num_options
,
8674 &(printer
->options
));
8675 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8676 "Setting %s to %s...", attr
->name
,
8677 attr
->values
[0].boolean
? "true" : "false");
8680 case IPP_TAG_INTEGER
:
8682 sprintf(value
, "%d", attr
->values
[0].integer
);
8683 printer
->num_options
= cupsAddOption(name
, value
,
8684 printer
->num_options
,
8685 &(printer
->options
));
8686 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8687 "Setting %s to %s...", attr
->name
, value
);
8690 case IPP_TAG_RANGE
:
8691 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
8692 attr
->values
[0].range
.upper
);
8693 printer
->num_options
= cupsAddOption(name
, value
,
8694 printer
->num_options
,
8695 &(printer
->options
));
8696 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8697 "Setting %s to %s...", attr
->name
, value
);
8700 case IPP_TAG_RESOLUTION
:
8701 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
8702 attr
->values
[0].resolution
.yres
,
8703 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
8705 printer
->num_options
= cupsAddOption(name
, value
,
8706 printer
->num_options
,
8707 &(printer
->options
));
8708 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8709 "Setting %s to %s...", attr
->name
, value
);
8713 /* Do nothing for other values */
8721 * 'start_printer()' - Start a printer.
8725 start_printer(cupsd_client_t
*con
, /* I - Client connection */
8726 ipp_attribute_t
*uri
) /* I - Printer URI */
8728 http_status_t status
; /* Policy status */
8729 cups_ptype_t dtype
; /* Destination type (printer or class) */
8730 char method
[HTTP_MAX_URI
],
8731 /* Method portion of URI */
8732 username
[HTTP_MAX_URI
],
8733 /* Username portion of URI */
8735 /* Host portion of URI */
8736 resource
[HTTP_MAX_URI
];
8737 /* Resource portion of URI */
8738 int port
; /* Port portion of URI */
8739 const char *name
; /* Printer name */
8740 cupsd_printer_t
*printer
; /* Printer data */
8743 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
8744 con
->http
.fd
, uri
->values
[0].string
.text
);
8747 * Is the destination valid?
8750 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8751 sizeof(method
), username
, sizeof(username
), host
,
8752 sizeof(host
), &port
, resource
, sizeof(resource
));
8754 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
8760 send_ipp_status(con
, IPP_NOT_FOUND
,
8761 _("The printer or class was not found."));
8769 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8771 send_http_error(con
, status
);
8776 * Start the printer...
8779 printer
->state_message
[0] = '\0';
8781 cupsdStartPrinter(printer
, 1);
8783 if (dtype
& CUPS_PRINTER_CLASS
)
8784 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".", name
,
8787 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".", name
,
8793 * Everything was ok, so return OK status...
8796 con
->response
->request
.status
.status_code
= IPP_OK
;
8801 * 'stop_printer()' - Stop a printer.
8805 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
8806 ipp_attribute_t
*uri
) /* I - Printer URI */
8808 http_status_t status
; /* Policy status */
8809 cups_ptype_t dtype
; /* Destination type (printer or class) */
8810 char method
[HTTP_MAX_URI
],
8811 /* Method portion of URI */
8812 username
[HTTP_MAX_URI
],
8813 /* Username portion of URI */
8815 /* Host portion of URI */
8816 resource
[HTTP_MAX_URI
];
8817 /* Resource portion of URI */
8818 int port
; /* Port portion of URI */
8819 const char *name
; /* Printer name */
8820 cupsd_printer_t
*printer
; /* Printer data */
8821 ipp_attribute_t
*attr
; /* printer-state-message attribute */
8824 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
8825 con
->http
.fd
, uri
->values
[0].string
.text
);
8828 * Is the destination valid?
8831 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8832 sizeof(method
), username
, sizeof(username
), host
,
8833 sizeof(host
), &port
, resource
, sizeof(resource
));
8835 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
8841 send_ipp_status(con
, IPP_NOT_FOUND
,
8842 _("The printer or class was not found."));
8850 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8852 send_http_error(con
, status
);
8857 * Stop the printer...
8860 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8861 IPP_TAG_TEXT
)) == NULL
)
8862 strcpy(printer
->state_message
, "Paused");
8865 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8866 sizeof(printer
->state_message
));
8869 cupsdStopPrinter(printer
, 1);
8871 if (dtype
& CUPS_PRINTER_CLASS
)
8872 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".", name
,
8875 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".", name
,
8879 * Everything was ok, so return OK status...
8882 con
->response
->request
.status
.status_code
= IPP_OK
;
8887 * 'user_allowed()' - See if a user is allowed to print to a queue.
8890 static int /* O - 0 if not allowed, 1 if allowed */
8891 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
8892 const char *username
) /* I - Username */
8894 int i
; /* Looping var */
8895 struct passwd
*pw
; /* User password data */
8898 if (p
->num_users
== 0)
8901 if (!strcmp(username
, "root"))
8904 pw
= getpwnam(username
);
8907 for (i
= 0; i
< p
->num_users
; i
++)
8909 if (p
->users
[i
][0] == '@')
8912 * Check group membership...
8915 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
8918 else if (!strcasecmp(username
, p
->users
[i
]))
8922 return ((i
< p
->num_users
) != p
->deny_users
);
8927 * 'validate_job()' - Validate printer options and destination.
8931 validate_job(cupsd_client_t
*con
, /* I - Client connection */
8932 ipp_attribute_t
*uri
) /* I - Printer URI */
8934 http_status_t status
; /* Policy status */
8935 ipp_attribute_t
*attr
; /* Current attribute */
8936 ipp_attribute_t
*format
; /* Document-format attribute */
8937 cups_ptype_t dtype
; /* Destination type (printer or class) */
8938 char method
[HTTP_MAX_URI
],
8939 /* Method portion of URI */
8940 username
[HTTP_MAX_URI
],
8941 /* Username portion of URI */
8943 /* Host portion of URI */
8944 resource
[HTTP_MAX_URI
];
8945 /* Resource portion of URI */
8946 int port
; /* Port portion of URI */
8947 char super
[MIME_MAX_SUPER
],
8948 /* Supertype of file */
8949 type
[MIME_MAX_TYPE
];
8950 /* Subtype of file */
8951 cupsd_printer_t
*printer
; /* Printer */
8954 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
8955 con
->http
.fd
, uri
->values
[0].string
.text
);
8958 * OK, see if the client is sending the document compressed - CUPS
8959 * doesn't support compression yet...
8962 if ((attr
= ippFindAttribute(con
->request
, "compression",
8963 IPP_TAG_KEYWORD
)) != NULL
&&
8964 !strcmp(attr
->values
[0].string
.text
, "none"))
8966 send_ipp_status(con
, IPP_ATTRIBUTES
,
8967 _("Unsupported compression attribute %s!"),
8968 attr
->values
[0].string
.text
);
8969 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8970 "compression", NULL
, attr
->values
[0].string
.text
);
8975 * Is it a format we support?
8978 if ((format
= ippFindAttribute(con
->request
, "document-format",
8979 IPP_TAG_MIMETYPE
)) != NULL
)
8981 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
8983 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
8984 format
->values
[0].string
.text
);
8988 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
8989 !mimeType(MimeDatabase
, super
, type
))
8991 cupsdLogMessage(CUPSD_LOG_INFO
,
8992 "Hint: Do you have the raw file printing rules enabled?");
8993 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8994 _("Unsupported format \"%s\"!"),
8995 format
->values
[0].string
.text
);
8996 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8997 "document-format", NULL
, format
->values
[0].string
.text
);
9003 * Is the destination valid?
9006 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9007 sizeof(method
), username
, sizeof(username
), host
,
9008 sizeof(host
), &port
, resource
, sizeof(resource
));
9010 if (cupsdValidateDest(host
, resource
, &dtype
, &printer
) == NULL
)
9016 send_ipp_status(con
, IPP_NOT_FOUND
,
9017 _("The printer or class was not found."));
9025 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9027 send_http_error(con
, status
);
9032 * Everything was ok, so return OK status...
9035 con
->response
->request
.status
.status_code
= IPP_OK
;
9040 * 'validate_name()' - Make sure the printer name only contains valid chars.
9043 static int /* O - 0 if name is no good, 1 if name is good */
9044 validate_name(const char *name
) /* I - Name to check */
9046 const char *ptr
; /* Pointer into name */
9050 * Scan the whole name...
9053 for (ptr
= name
; *ptr
; ptr
++)
9054 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
9058 * All the characters are good; validate the length, too...
9061 return ((ptr
- name
) < 128);
9066 * 'validate_user()' - Validate the user for the request.
9069 static int /* O - 1 if permitted, 0 otherwise */
9070 validate_user(cupsd_job_t
*job
, /* I - Job */
9071 cupsd_client_t
*con
, /* I - Client connection */
9072 const char *owner
, /* I - Owner of job/resource */
9073 char *username
, /* O - Authenticated username */
9074 int userlen
) /* I - Length of username */
9076 cupsd_printer_t
*printer
; /* Printer for job */
9079 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9080 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
9082 job
? job
->id
: 0, con
->http
.fd
, owner
? owner
: "(null)",
9089 if (!con
|| !owner
|| !username
|| userlen
<= 0)
9093 * Get the best authenticated username that is available.
9096 strlcpy(username
, get_username(con
), userlen
);
9099 * Check the username against the owner...
9102 if (job
->dtype
& CUPS_PRINTER_CLASS
)
9103 printer
= cupsdFindClass(job
->dest
);
9105 printer
= cupsdFindPrinter(job
->dest
);
9107 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
9108 con
, owner
) == HTTP_OK
);
9113 * End of "$Id: ipp.c 5284 2006-03-13 13:34:24Z mike $".