2 * "$Id: ipp.c 5383 2006-04-07 15:36:10Z 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 mime_type_t
*filetype
);
131 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
132 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
133 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
134 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
135 static void add_printer_state_reasons(cupsd_client_t
*con
,
137 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
138 static void apply_printer_defaults(cupsd_printer_t
*printer
,
140 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
141 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
142 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
143 static void cancel_subscription(cupsd_client_t
*con
, int id
);
144 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
145 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
147 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
148 ipp_tag_t group
, int quickcopy
);
149 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
151 static int copy_file(const char *from
, const char *to
);
152 static int copy_model(cupsd_client_t
*con
, const char *from
,
154 static void copy_job_attrs(cupsd_client_t
*con
,
157 static void copy_printer_attrs(cupsd_client_t
*con
,
158 cupsd_printer_t
*printer
,
160 static void copy_subscription_attrs(cupsd_client_t
*con
,
161 cupsd_subscription_t
*sub
,
163 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
164 static cups_array_t
*create_requested_array(ipp_t
*request
);
165 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
166 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
167 static void get_default(cupsd_client_t
*con
);
168 static void get_devices(cupsd_client_t
*con
);
169 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
170 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
171 static void get_notifications(cupsd_client_t
*con
);
172 static void get_ppds(cupsd_client_t
*con
);
173 static void get_printers(cupsd_client_t
*con
, int type
);
174 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
175 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
176 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
177 static const char *get_username(cupsd_client_t
*con
);
178 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
179 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
180 static int ppd_add_default(const char *option
, const char *choice
,
181 int num_defaults
, ppd_default_t
**defaults
);
182 static int ppd_parse_line(const char *line
, char *option
, int olen
,
183 char *choice
, int clen
);
184 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
185 static void read_ps_job_ticket(cupsd_client_t
*con
);
186 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
187 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
188 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
189 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
190 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
);
191 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
192 static void send_http_error(cupsd_client_t
*con
, http_status_t status
);
193 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
194 const char *message
, ...)
196 __attribute__ ((__format__ (__printf__
, 3, 4)))
197 # endif /* __GNUC__ */
199 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
200 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
201 static void set_printer_defaults(cupsd_client_t
*con
,
202 cupsd_printer_t
*printer
);
203 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
204 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
205 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
206 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
207 static int validate_name(const char *name
);
208 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
,
209 const char *owner
, char *username
,
214 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request...
217 int /* O - 1 on success, 0 on failure */
218 cupsdProcessIPPRequest(
219 cupsd_client_t
*con
) /* I - Client connection */
221 ipp_tag_t group
; /* Current group tag */
222 ipp_attribute_t
*attr
; /* Current attribute */
223 ipp_attribute_t
*charset
; /* Character set attribute */
224 ipp_attribute_t
*language
; /* Language attribute */
225 ipp_attribute_t
*uri
; /* Printer URI attribute */
226 ipp_attribute_t
*username
; /* requesting-user-name attr */
227 int sub_id
; /* Subscription ID */
230 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
231 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
232 con
, con
->http
.fd
, con
->request
->request
.op
.operation_id
);
235 * First build an empty response message for this request...
238 con
->response
= ippNew();
240 con
->response
->request
.status
.version
[0] = con
->request
->request
.op
.version
[0];
241 con
->response
->request
.status
.version
[1] = con
->request
->request
.op
.version
[1];
242 con
->response
->request
.status
.request_id
= con
->request
->request
.op
.request_id
;
245 * Then validate the request header and required attributes...
248 if (con
->request
->request
.any
.version
[0] != 1)
251 * Return an error, since we only support IPP 1.x.
254 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
255 "%04X %s Bad request version number %d.%d",
256 IPP_VERSION_NOT_SUPPORTED
, con
->http
.hostname
,
257 con
->request
->request
.any
.version
[0],
258 con
->request
->request
.any
.version
[1]);
260 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
261 _("Bad request version number %d.%d!"),
262 con
->request
->request
.any
.version
[0],
263 con
->request
->request
.any
.version
[1]);
265 else if (!con
->request
->attrs
)
267 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
268 "%04X %s No attributes in request",
269 IPP_BAD_REQUEST
, con
->http
.hostname
);
271 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request!"));
276 * Make sure that the attributes are provided in the correct order and
277 * don't repeat groups...
280 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
283 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
286 * Out of order; return an error...
289 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
290 "%04X %s Attribute groups are out of order",
291 IPP_BAD_REQUEST
, con
->http
.hostname
);
293 send_ipp_status(con
, IPP_BAD_REQUEST
,
294 _("Attribute groups are out of order (%x < %x)!"),
295 attr
->group_tag
, group
);
299 group
= attr
->group_tag
;
304 * Then make sure that the first three attributes are:
307 * attributes-natural-language
308 * printer-uri/job-uri
311 attr
= con
->request
->attrs
;
312 if (attr
&& !strcmp(attr
->name
, "attributes-charset") &&
313 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
321 if (attr
&& !strcmp(attr
->name
, "attributes-natural-language") &&
322 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
327 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
328 IPP_TAG_URI
)) != NULL
)
330 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
331 IPP_TAG_URI
)) != NULL
)
337 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
338 "attributes-charset", NULL
, charset
->values
[0].string
.text
);
340 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
341 "attributes-charset", NULL
, DefaultCharset
);
344 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
345 "attributes-natural-language", NULL
,
346 language
->values
[0].string
.text
);
348 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
349 "attributes-natural-language", NULL
, DefaultLanguage
);
351 if (!charset
|| !language
||
353 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
354 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
355 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
356 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
357 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
360 * Return an error, since attributes-charset,
361 * attributes-natural-language, and printer-uri/job-uri are required
362 * for all operations.
367 cupsdLogMessage(CUPSD_LOG_ERROR
,
368 "Missing attributes-charset attribute!");
370 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
371 "%04X %s Missing attributes-charset attribute",
372 IPP_BAD_REQUEST
, con
->http
.hostname
);
377 cupsdLogMessage(CUPSD_LOG_ERROR
,
378 "Missing attributes-natural-language attribute!");
380 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
381 "%04X %s Missing attributes-natural-language attribute",
382 IPP_BAD_REQUEST
, con
->http
.hostname
);
387 cupsdLogMessage(CUPSD_LOG_ERROR
,
388 "Missing printer-uri or job-uri attribute!");
390 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
391 "%04X %s Missing printer-uri or job-uri attribute",
392 IPP_BAD_REQUEST
, con
->http
.hostname
);
395 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
397 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
398 cupsdLogMessage(CUPSD_LOG_DEBUG
,
399 "attr \"%s\": group_tag = %x, value_tag = %x",
400 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
403 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
405 send_ipp_status(con
, IPP_BAD_REQUEST
,
406 _("Missing required attributes!"));
411 * OK, all the checks pass so far; make sure requesting-user-name is
412 * not "root" from a remote host...
415 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
416 IPP_TAG_NAME
)) != NULL
)
419 * Check for root user...
422 if (!strcmp(username
->values
[0].string
.text
, "root") &&
423 strcasecmp(con
->http
.hostname
, "localhost") &&
424 strcmp(con
->username
, "root"))
427 * Remote unauthenticated user masquerading as local root...
430 _cupsStrFree(username
->values
[0].string
.text
);
431 username
->values
[0].string
.text
= _cupsStrAlloc(RemoteRoot
);
435 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
436 IPP_TAG_INTEGER
)) != NULL
)
437 sub_id
= attr
->values
[0].integer
;
442 * Then try processing the operation...
446 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
447 ippOpString(con
->request
->request
.op
.operation_id
),
448 uri
->values
[0].string
.text
);
450 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
451 ippOpString(con
->request
->request
.op
.operation_id
));
453 switch (con
->request
->request
.op
.operation_id
)
459 case IPP_VALIDATE_JOB
:
460 validate_job(con
, uri
);
463 case IPP_CREATE_JOB
:
464 create_job(con
, uri
);
467 case IPP_SEND_DOCUMENT
:
468 send_document(con
, uri
);
471 case IPP_CANCEL_JOB
:
472 cancel_job(con
, uri
);
475 case IPP_GET_JOB_ATTRIBUTES
:
476 get_job_attrs(con
, uri
);
483 case IPP_GET_PRINTER_ATTRIBUTES
:
484 get_printer_attrs(con
, uri
);
491 case IPP_RELEASE_JOB
:
492 release_job(con
, uri
);
495 case IPP_RESTART_JOB
:
496 restart_job(con
, uri
);
499 case IPP_PAUSE_PRINTER
:
500 stop_printer(con
, uri
);
503 case IPP_RESUME_PRINTER
:
504 start_printer(con
, uri
);
507 case IPP_PURGE_JOBS
:
508 cancel_all_jobs(con
, uri
);
511 case IPP_SET_JOB_ATTRIBUTES
:
512 set_job_attrs(con
, uri
);
515 case CUPS_GET_DEFAULT
:
519 case CUPS_GET_PRINTERS
:
520 get_printers(con
, 0);
523 case CUPS_GET_CLASSES
:
524 get_printers(con
, CUPS_PRINTER_CLASS
);
527 case CUPS_ADD_PRINTER
:
528 add_printer(con
, uri
);
531 case CUPS_DELETE_PRINTER
:
532 delete_printer(con
, uri
);
535 case CUPS_ADD_CLASS
:
539 case CUPS_DELETE_CLASS
:
540 delete_printer(con
, uri
);
543 case CUPS_ACCEPT_JOBS
:
544 case IPP_ENABLE_PRINTER
:
545 accept_jobs(con
, uri
);
548 case CUPS_REJECT_JOBS
:
549 case IPP_DISABLE_PRINTER
:
550 reject_jobs(con
, uri
);
553 case CUPS_SET_DEFAULT
:
554 set_default(con
, uri
);
557 case CUPS_GET_DEVICES
:
569 case CUPS_AUTHENTICATE_JOB
:
570 authenticate_job(con
, uri
);
573 case IPP_CREATE_PRINTER_SUBSCRIPTION
:
574 case IPP_CREATE_JOB_SUBSCRIPTION
:
575 create_subscription(con
, uri
);
578 case IPP_GET_SUBSCRIPTION_ATTRIBUTES
:
579 get_subscription_attrs(con
, sub_id
);
582 case IPP_GET_SUBSCRIPTIONS
:
583 get_subscriptions(con
, uri
);
586 case IPP_RENEW_SUBSCRIPTION
:
587 renew_subscription(con
, sub_id
);
590 case IPP_CANCEL_SUBSCRIPTION
:
591 cancel_subscription(con
, sub_id
);
594 case IPP_GET_NOTIFICATIONS
:
595 get_notifications(con
);
599 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
600 "%04X %s Operation %04X (%s) not supported",
601 IPP_OPERATION_NOT_SUPPORTED
, con
->http
.hostname
,
602 con
->request
->request
.op
.operation_id
,
603 ippOpString(con
->request
->request
.op
.operation_id
));
605 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
606 _("%s not supported!"),
607 ippOpString(con
->request
->request
.op
.operation_id
));
617 * Sending data from the scheduler...
620 cupsdLogMessage(CUPSD_LOG_DEBUG
,
621 "cupsdProcessIPPRequest: %d status_code=%x (%s)",
622 con
->http
.fd
, con
->response
->request
.status
.status_code
,
623 ippErrorString(con
->response
->request
.status
.status_code
));
625 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp"))
627 if (con
->http
.version
== HTTP_1_1
)
629 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
631 httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n");
635 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
636 con
->http
.data_remaining
= ippLength(con
->response
);
638 if (con
->http
.data_remaining
< INT_MAX
)
639 con
->http
._data_remaining
= con
->http
.data_remaining
;
641 con
->http
._data_remaining
= INT_MAX
;
643 httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
644 CUPS_LLCAST con
->http
.data_remaining
);
647 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
648 "cupsdProcessIPPRequest: Adding fd %d to OutputSet...",
651 FD_SET(con
->http
.fd
, OutputSet
);
654 * Tell the caller the response header was sent successfully...
662 * Tell the caller the response header could not be sent...
671 * Sending data from a subprocess like cups-deviced; tell the caller
672 * everything is A-OK so far...
681 * 'accept_jobs()' - Accept print jobs to a printer.
685 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
686 ipp_attribute_t
*uri
) /* I - Printer or class URI */
688 http_status_t status
; /* Policy status */
689 cups_ptype_t dtype
; /* Destination type (printer or class) */
690 char method
[HTTP_MAX_URI
], /* Method portion of URI */
691 username
[HTTP_MAX_URI
], /* Username portion of URI */
692 host
[HTTP_MAX_URI
], /* Host portion of URI */
693 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
694 int port
; /* Port portion of URI */
695 const char *name
; /* Printer name */
696 cupsd_printer_t
*printer
; /* Printer data */
699 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
700 con
->http
.fd
, uri
->values
[0].string
.text
);
703 * Is the destination valid?
706 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
707 sizeof(method
), username
, sizeof(username
), host
,
708 sizeof(host
), &port
, resource
, sizeof(resource
));
710 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
716 send_ipp_status(con
, IPP_NOT_FOUND
,
717 _("The printer or class was not found."));
725 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
727 send_http_error(con
, status
);
732 * Accept jobs sent to the printer...
735 printer
->accepting
= 1;
736 printer
->state_message
[0] = '\0';
738 cupsdAddPrinterHistory(printer
);
740 if (dtype
& CUPS_PRINTER_CLASS
)
741 cupsdSaveAllClasses();
743 cupsdSaveAllPrinters();
745 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" now accepting jobs (\"%s\").", name
,
749 * Everything was ok, so return OK status...
752 con
->response
->request
.status
.status_code
= IPP_OK
;
757 * 'add_class()' - Add a class to the system.
761 add_class(cupsd_client_t
*con
, /* I - Client connection */
762 ipp_attribute_t
*uri
) /* I - URI of class */
764 http_status_t status
; /* Policy status */
765 int i
; /* Looping var */
766 char method
[HTTP_MAX_URI
], /* Method portion of URI */
767 username
[HTTP_MAX_URI
], /* Username portion of URI */
768 host
[HTTP_MAX_URI
], /* Host portion of URI */
769 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
770 int port
; /* Port portion of URI */
771 cupsd_printer_t
*pclass
, /* Class */
772 *member
; /* Member printer/class */
773 cups_ptype_t dtype
; /* Destination type */
774 const char *dest
; /* Printer or class name */
775 ipp_attribute_t
*attr
; /* Printer attribute */
776 int modify
; /* Non-zero if we just modified */
777 char newname
[IPP_MAX_NAME
]; /* New class name */
778 int need_restart_job
; /* Need to restart job? */
781 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
782 con
->http
.fd
, uri
->values
[0].string
.text
);
785 * Do we have a valid URI?
788 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
789 sizeof(method
), username
, sizeof(username
), host
,
790 sizeof(host
), &port
, resource
, sizeof(resource
));
793 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
796 * No, return an error...
799 send_ipp_status(con
, IPP_BAD_REQUEST
,
800 _("The printer-uri must be of the form "
801 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
806 * Do we have a valid printer name?
809 if (!validate_name(resource
+ 9))
812 * No, return an error...
815 send_ipp_status(con
, IPP_BAD_REQUEST
,
816 _("The printer-uri \"%s\" contains invalid characters."),
817 uri
->values
[0].string
.text
);
825 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
827 send_http_error(con
, status
);
832 * See if the class already exists; if not, create a new class...
835 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
838 * Class doesn't exist; see if we have a printer of the same name...
841 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
842 !(pclass
->type
& CUPS_PRINTER_REMOTE
))
845 * Yes, return an error...
848 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
849 _("A printer named \"%s\" already exists!"),
855 * No, add the pclass...
858 pclass
= cupsdAddClass(resource
+ 9);
861 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
864 * Rename the implicit class to "AnyClass" or remove it...
867 if (ImplicitAnyClasses
)
869 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
870 cupsdRenamePrinter(pclass
, newname
);
873 cupsdDeletePrinter(pclass
, 1);
876 * Add the class as a new local class...
879 pclass
= cupsdAddClass(resource
+ 9);
882 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
885 * Rename the remote class to "Class"...
888 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
889 cupsdRenamePrinter(pclass
, newname
);
892 * Add the class as a new local class...
895 pclass
= cupsdAddClass(resource
+ 9);
902 * Look for attributes and copy them over as needed...
905 need_restart_job
= 0;
907 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
908 IPP_TAG_TEXT
)) != NULL
)
909 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
911 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
912 IPP_TAG_TEXT
)) != NULL
)
913 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
915 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
916 IPP_TAG_BOOLEAN
)) != NULL
)
918 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-is-accepting-jobs to %d (was %d.)",
919 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
921 pclass
->accepting
= attr
->values
[0].boolean
;
922 cupsdAddPrinterHistory(pclass
);
925 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
926 IPP_TAG_BOOLEAN
)) != NULL
)
928 if (pclass
->shared
&& !attr
->values
[0].boolean
)
929 cupsdSendBrowseDelete(pclass
);
931 cupsdLogMessage(CUPSD_LOG_INFO
,
932 "Setting %s printer-is-shared to %d (was %d.)",
933 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
935 pclass
->shared
= attr
->values
[0].boolean
;
938 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
939 IPP_TAG_ENUM
)) != NULL
)
941 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
942 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
944 send_ipp_status(con
, IPP_BAD_REQUEST
,
945 _("Attempt to set %s printer-state to bad value %d!"),
946 pclass
->name
, attr
->values
[0].integer
);
950 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)", pclass
->name
,
951 attr
->values
[0].integer
, pclass
->state
);
953 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
954 cupsdStopPrinter(pclass
, 0);
957 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
958 need_restart_job
= 1;
961 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
962 IPP_TAG_TEXT
)) != NULL
)
964 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
965 sizeof(pclass
->state_message
));
966 cupsdAddPrinterHistory(pclass
);
968 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
969 IPP_TAG_URI
)) != NULL
)
972 * Clear the printer array as needed...
975 need_restart_job
= 1;
977 if (pclass
->num_printers
> 0)
979 free(pclass
->printers
);
980 pclass
->num_printers
= 0;
984 * Add each printer or class that is listed...
987 for (i
= 0; i
< attr
->num_values
; i
++)
990 * Search for the printer or class URI...
993 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
, method
,
994 sizeof(method
), username
, sizeof(username
), host
,
995 sizeof(host
), &port
, resource
, sizeof(resource
));
997 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &member
)) == NULL
)
1003 send_ipp_status(con
, IPP_NOT_FOUND
,
1004 _("The printer or class was not found."));
1009 * Add it to the class...
1012 cupsdAddPrinterToClass(pclass
, member
);
1016 set_printer_defaults(con
, pclass
);
1019 * Update the printer class attributes and return...
1022 cupsdSetPrinterAttrs(pclass
);
1023 cupsdSaveAllClasses();
1025 if (need_restart_job
&& pclass
->job
)
1030 * Stop the current job and then restart it below...
1033 job
= (cupsd_job_t
*)pclass
->job
;
1035 cupsdStopJob(job
, 1);
1037 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1038 job
->state_value
= IPP_JOB_PENDING
;
1041 if (need_restart_job
)
1044 cupsdWritePrintcap();
1048 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1049 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1052 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1053 pclass
->name
, get_username(con
));
1057 cupsdAddPrinterHistory(pclass
);
1059 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1060 "New class \"%s\" added by \"%s\".", pclass
->name
,
1063 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1064 pclass
->name
, get_username(con
));
1067 con
->response
->request
.status
.status_code
= IPP_OK
;
1072 * 'add_file()' - Add a file to a job.
1075 static int /* O - 0 on success, -1 on error */
1076 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1077 cupsd_job_t
*job
, /* I - Job to add to */
1078 mime_type_t
*filetype
, /* I - Type of file */
1079 int compression
) /* I - Compression */
1081 mime_type_t
**filetypes
; /* New filetypes array... */
1082 int *compressions
; /* New compressions array... */
1085 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1086 "add_file(con=%p[%d], job=%d, filetype=%s/%s, compression=%d)",
1087 con
, con
->http
.fd
, job
->id
, filetype
->super
, filetype
->type
,
1091 * Add the file to the job...
1094 if (job
->num_files
== 0)
1096 compressions
= (int *)malloc(sizeof(int));
1097 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1101 compressions
= (int *)realloc(job
->compressions
,
1102 (job
->num_files
+ 1) * sizeof(int));
1103 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1104 (job
->num_files
+ 1) *
1105 sizeof(mime_type_t
*));
1108 if (!compressions
|| !filetypes
)
1110 cupsdCancelJob(job
, 1);
1112 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1113 _("Unable to allocate memory for file types!"));
1117 job
->compressions
= compressions
;
1118 job
->compressions
[job
->num_files
] = compression
;
1119 job
->filetypes
= filetypes
;
1120 job
->filetypes
[job
->num_files
] = filetype
;
1129 * 'add_job()' - Add a job to a print queue.
1132 static cupsd_job_t
* /* O - Job object */
1133 add_job(cupsd_client_t
*con
, /* I - Client connection */
1134 ipp_attribute_t
*uri
, /* I - printer-uri */
1135 cupsd_printer_t
**dprinter
, /* I - Destination printer */
1136 mime_type_t
*filetype
) /* I - First print file type, if any */
1138 http_status_t status
; /* Policy status */
1139 ipp_attribute_t
*attr
; /* Current attribute */
1140 const char *dest
; /* Destination */
1141 cups_ptype_t dtype
; /* Destination type (printer or class) */
1142 const char *val
; /* Default option value */
1143 int priority
; /* Job priority */
1144 char *title
; /* Job name/title */
1145 cupsd_job_t
*job
; /* Current job */
1146 char job_uri
[HTTP_MAX_URI
], /* Job URI */
1147 method
[HTTP_MAX_URI
], /* Method portion of URI */
1148 username
[HTTP_MAX_URI
], /* Username portion of URI */
1149 host
[HTTP_MAX_URI
], /* Host portion of URI */
1150 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1151 int port
; /* Port portion of URI */
1152 cupsd_printer_t
*printer
; /* Printer data */
1153 int kbytes
; /* Size of print file */
1154 int i
; /* Looping var */
1155 int lowerpagerange
; /* Page range bound */
1158 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %s)", con
,
1159 con
->http
.fd
, uri
->values
[0].string
.text
);
1162 * Is the destination valid?
1165 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
1166 sizeof(method
), username
, sizeof(username
), host
,
1167 sizeof(host
), &port
, resource
, sizeof(resource
));
1169 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
1175 send_ipp_status(con
, IPP_NOT_FOUND
,
1176 _("The printer or class was not found."));
1181 *dprinter
= printer
;
1184 * Check remote printing to non-shared printer...
1187 if (!printer
->shared
&&
1188 strcasecmp(con
->http
.hostname
, "localhost") &&
1189 strcasecmp(con
->http
.hostname
, ServerName
))
1191 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1192 _("The printer or class is not shared!"));
1200 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1202 send_http_error(con
, status
);
1205 else if ((printer
->type
& CUPS_PRINTER_AUTHENTICATED
) && !con
->username
[0])
1207 send_http_error(con
, HTTP_UNAUTHORIZED
);
1212 * See if the printer is accepting jobs...
1215 if (!printer
->accepting
)
1217 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1218 _("Destination \"%s\" is not accepting jobs."),
1224 * Validate job template attributes; for now just document-format,
1225 * copies, and page-ranges...
1228 if (filetype
&& printer
->filetypes
&&
1229 !cupsArrayFind(printer
->filetypes
, filetype
))
1231 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1232 /* MIME media type string */
1235 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1238 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1239 _("Unsupported format \'%s\'!"), mimetype
);
1241 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1242 "document-format", NULL
, mimetype
);
1247 if ((attr
= ippFindAttribute(con
->request
, "copies",
1248 IPP_TAG_INTEGER
)) != NULL
)
1250 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1252 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1253 attr
->values
[0].integer
);
1254 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1255 "copies", attr
->values
[0].integer
);
1260 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1261 IPP_TAG_RANGE
)) != NULL
)
1263 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1265 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1266 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1268 send_ipp_status(con
, IPP_BAD_REQUEST
,
1269 _("Bad page-ranges values %d-%d."),
1270 attr
->values
[i
].range
.lower
,
1271 attr
->values
[i
].range
.upper
);
1275 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1280 * Make sure we aren't over our limit...
1283 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1286 if (cupsArrayCount(Jobs
) >= MaxJobs
&& MaxJobs
)
1288 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1289 _("Too many active jobs."));
1293 if (!check_quotas(con
, printer
))
1295 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1300 * Create the job and set things up...
1303 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1304 IPP_TAG_INTEGER
)) != NULL
)
1305 priority
= attr
->values
[0].integer
;
1308 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1309 printer
->options
)) != NULL
)
1310 priority
= atoi(val
);
1314 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1318 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1319 IPP_TAG_NAME
)) != NULL
)
1320 title
= attr
->values
[0].string
.text
;
1322 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1323 title
= "Untitled");
1325 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1327 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1328 _("Unable to add job for destination \"%s\"!"), dest
);
1333 job
->attrs
= con
->request
;
1334 con
->request
= NULL
;
1336 add_job_uuid(con
, job
);
1337 apply_printer_defaults(printer
, job
);
1339 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1341 if (con
->username
[0])
1343 cupsdSetString(&job
->username
, con
->username
);
1346 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1348 save_auth_info(con
, job
);
1352 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1353 "add_job: requesting-user-name=\"%s\"",
1354 attr
->values
[0].string
.text
);
1356 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1359 cupsdSetString(&job
->username
, "anonymous");
1362 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1363 "job-originating-user-name", NULL
, job
->username
);
1366 attr
->group_tag
= IPP_TAG_JOB
;
1367 _cupsStrFree(attr
->name
);
1368 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1371 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1372 IPP_TAG_ZERO
)) != NULL
)
1375 * Request contains a job-originating-host-name attribute; validate it...
1378 if (attr
->value_tag
!= IPP_TAG_NAME
||
1379 attr
->num_values
!= 1 ||
1380 strcmp(con
->http
.hostname
, "localhost"))
1383 * Can't override the value if we aren't connected via localhost.
1384 * Also, we can only have 1 value and it must be a name value.
1387 switch (attr
->value_tag
)
1389 case IPP_TAG_STRING
:
1390 case IPP_TAG_TEXTLANG
:
1391 case IPP_TAG_NAMELANG
:
1394 case IPP_TAG_KEYWORD
:
1396 case IPP_TAG_URISCHEME
:
1397 case IPP_TAG_CHARSET
:
1398 case IPP_TAG_LANGUAGE
:
1399 case IPP_TAG_MIMETYPE
:
1401 * Free old strings...
1404 for (i
= 0; i
< attr
->num_values
; i
++)
1406 _cupsStrFree(attr
->values
[i
].string
.text
);
1407 attr
->values
[i
].string
.text
= NULL
;
1408 if (attr
->values
[i
].string
.charset
)
1410 _cupsStrFree(attr
->values
[i
].string
.charset
);
1411 attr
->values
[i
].string
.charset
= NULL
;
1420 * Use the default connection hostname instead...
1423 attr
->value_tag
= IPP_TAG_NAME
;
1424 attr
->num_values
= 1;
1425 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1428 attr
->group_tag
= IPP_TAG_JOB
;
1433 * No job-originating-host-name attribute, so use the hostname from
1437 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1438 "job-originating-host-name", NULL
, con
->http
.hostname
);
1441 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1443 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1444 "time-at-processing", 0);
1445 attr
->value_tag
= IPP_TAG_NOVALUE
;
1446 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1447 "time-at-completed", 0);
1448 attr
->value_tag
= IPP_TAG_NOVALUE
;
1451 * Add remaining job attributes...
1454 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1455 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1456 "job-state", IPP_JOB_STOPPED
);
1457 job
->state_value
= job
->state
->values
[0].integer
;
1458 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1459 "job-media-sheets-completed", 0);
1460 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1462 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1465 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1466 IPP_TAG_INTEGER
)) != NULL
)
1467 attr
->values
[0].integer
= 0;
1469 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1472 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1473 IPP_TAG_KEYWORD
)) == NULL
)
1474 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1477 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1478 printer
->options
)) == NULL
)
1481 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1482 "job-hold-until", NULL
, val
);
1484 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1485 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1488 * Hold job until specified time...
1491 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1493 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1494 job
->state_value
= IPP_JOB_HELD
;
1496 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1498 job
->hold_until
= time(NULL
) + 60;
1499 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1500 job
->state_value
= IPP_JOB_HELD
;
1504 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1505 job
->state_value
= IPP_JOB_PENDING
;
1508 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1512 * Add job sheets options...
1515 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1516 IPP_TAG_ZERO
)) == NULL
)
1518 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1519 "Adding default job-sheets values \"%s,%s\"...",
1520 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1522 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1524 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1525 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1528 job
->job_sheets
= attr
;
1531 * Enforce classification level if set...
1536 cupsdLogMessage(CUPSD_LOG_INFO
,
1537 "Classification=\"%s\", ClassifyOverride=%d",
1538 Classification
? Classification
: "(null)",
1541 if (ClassifyOverride
)
1543 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1544 (attr
->num_values
== 1 ||
1545 !strcmp(attr
->values
[1].string
.text
, "none")))
1548 * Force the leading banner to have the classification on it...
1551 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1553 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1554 "job-sheets=\"%s,none\", "
1555 "job-originating-user-name=\"%s\"",
1556 job
->id
, Classification
, job
->username
);
1558 else if (attr
->num_values
== 2 &&
1559 strcmp(attr
->values
[0].string
.text
,
1560 attr
->values
[1].string
.text
) &&
1561 strcmp(attr
->values
[0].string
.text
, "none") &&
1562 strcmp(attr
->values
[1].string
.text
, "none"))
1565 * Can't put two different security markings on the same document!
1568 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1570 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1571 "job-sheets=\"%s,%s\", "
1572 "job-originating-user-name=\"%s\"",
1573 job
->id
, attr
->values
[0].string
.text
,
1574 attr
->values
[1].string
.text
, job
->username
);
1576 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1577 strcmp(attr
->values
[0].string
.text
, "none") &&
1578 (attr
->num_values
== 1 ||
1579 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1580 strcmp(attr
->values
[1].string
.text
, "none"))))
1582 if (attr
->num_values
== 1)
1583 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1584 "[Job %d] CLASSIFICATION OVERRIDDEN "
1585 "job-sheets=\"%s\", "
1586 "job-originating-user-name=\"%s\"",
1587 job
->id
, attr
->values
[0].string
.text
, job
->username
);
1589 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1590 "[Job %d] CLASSIFICATION OVERRIDDEN "
1591 "job-sheets=\"%s,%s\",fffff "
1592 "job-originating-user-name=\"%s\"",
1593 job
->id
, attr
->values
[0].string
.text
,
1594 attr
->values
[1].string
.text
, job
->username
);
1597 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1598 (attr
->num_values
== 1 ||
1599 strcmp(attr
->values
[1].string
.text
, Classification
)))
1602 * Force the banner to have the classification on it...
1605 if (attr
->num_values
> 1 &&
1606 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1608 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1609 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1613 if (attr
->num_values
== 1 ||
1614 strcmp(attr
->values
[0].string
.text
, "none"))
1615 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1617 if (attr
->num_values
> 1 &&
1618 strcmp(attr
->values
[1].string
.text
, "none"))
1619 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1622 if (attr
->num_values
> 1)
1623 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1624 "[Job %d] CLASSIFICATION FORCED "
1625 "job-sheets=\"%s,%s\", "
1626 "job-originating-user-name=\"%s\"",
1627 job
->id
, attr
->values
[0].string
.text
,
1628 attr
->values
[1].string
.text
, job
->username
);
1630 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1631 "[Job %d] CLASSIFICATION FORCED "
1632 "job-sheets=\"%s\", "
1633 "job-originating-user-name=\"%s\"",
1634 job
->id
, Classification
, job
->username
);
1639 * See if we need to add the starting sheet...
1642 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1644 cupsdLogMessage(CUPSD_LOG_INFO
,
1645 "Adding start banner page \"%s\" to job %d.",
1646 attr
->values
[0].string
.text
, job
->id
);
1648 kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
);
1650 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1653 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1654 IPP_TAG_ZERO
)) != NULL
)
1658 * Fill in the response info...
1661 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1662 LocalPort
, job
->id
);
1664 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1667 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1669 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1671 add_job_state_reasons(con
, job
);
1673 con
->response
->request
.status
.status_code
= IPP_OK
;
1676 * Add any job subscriptions...
1679 add_job_subscriptions(con
, job
);
1682 * Set all but the first two attributes to the job attributes group...
1685 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1686 attr
->group_tag
= IPP_TAG_JOB
;
1689 * Fire the "job created" event...
1692 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1695 * Return the new job...
1703 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1704 * upon the job and printer state...
1708 add_job_state_reasons(
1709 cupsd_client_t
*con
, /* I - Client connection */
1710 cupsd_job_t
*job
) /* I - Job info */
1712 cupsd_printer_t
*dest
; /* Destination printer */
1715 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1716 con
, con
->http
.fd
, job
? job
->id
: 0);
1718 switch (job
? job
->state_value
: IPP_JOB_CANCELLED
)
1720 case IPP_JOB_PENDING
:
1721 dest
= cupsdFindDest(job
->dest
);
1723 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1724 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1725 "job-state-reasons", NULL
, "printer-stopped");
1727 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1728 "job-state-reasons", NULL
, "none");
1732 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1733 IPP_TAG_KEYWORD
) != NULL
||
1734 ippFindAttribute(job
->attrs
, "job-hold-until",
1735 IPP_TAG_NAME
) != NULL
)
1736 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1737 "job-state-reasons", NULL
, "job-hold-until-specified");
1739 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1740 "job-state-reasons", NULL
, "job-incoming");
1743 case IPP_JOB_PROCESSING
:
1744 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1745 "job-state-reasons", NULL
, "job-printing");
1748 case IPP_JOB_STOPPED
:
1749 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1750 "job-state-reasons", NULL
, "job-stopped");
1753 case IPP_JOB_CANCELLED
:
1754 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1755 "job-state-reasons", NULL
, "job-canceled-by-user");
1758 case IPP_JOB_ABORTED
:
1759 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1760 "job-state-reasons", NULL
, "aborted-by-system");
1763 case IPP_JOB_COMPLETED
:
1764 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1765 "job-state-reasons", NULL
, "job-completed-successfully");
1772 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1776 add_job_subscriptions(
1777 cupsd_client_t
*con
, /* I - Client connection */
1778 cupsd_job_t
*job
) /* I - Newly created job */
1780 int i
; /* Looping var */
1781 ipp_attribute_t
*prev
, /* Previous attribute */
1782 *next
, /* Next attribute */
1783 *attr
; /* Current attribute */
1784 cupsd_subscription_t
*sub
; /* Subscription object */
1785 const char *recipient
, /* notify-recipient-uri */
1786 *pullmethod
; /* notify-pull-method */
1787 ipp_attribute_t
*user_data
; /* notify-user-data */
1788 int interval
; /* notify-time-interval */
1789 unsigned mask
; /* notify-events */
1793 * Find the first subscription group attribute; return if we have
1797 for (attr
= job
->attrs
->attrs
, prev
= NULL
;
1799 prev
= attr
, attr
= attr
->next
)
1800 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1807 * Process the subscription attributes in the request...
1816 mask
= CUPSD_EVENT_NONE
;
1818 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1820 if (!strcmp(attr
->name
, "notify-recipient") &&
1821 attr
->value_tag
== IPP_TAG_URI
)
1822 recipient
= attr
->values
[0].string
.text
;
1823 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1824 attr
->value_tag
== IPP_TAG_KEYWORD
)
1825 pullmethod
= attr
->values
[0].string
.text
;
1826 else if (!strcmp(attr
->name
, "notify-charset") &&
1827 attr
->value_tag
== IPP_TAG_CHARSET
&&
1828 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
1829 strcmp(attr
->values
[0].string
.text
, "utf-8"))
1831 send_ipp_status(con
, IPP_CHARSET
,
1832 _("Character set \"%s\" not supported!"),
1833 attr
->values
[0].string
.text
);
1836 else if (!strcmp(attr
->name
, "notify-natural-language") &&
1837 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
1838 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
1840 send_ipp_status(con
, IPP_CHARSET
,
1841 _("Language \"%s\" not supported!"),
1842 attr
->values
[0].string
.text
);
1845 else if (!strcmp(attr
->name
, "notify-user-data") &&
1846 attr
->value_tag
== IPP_TAG_STRING
)
1848 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
1850 send_ipp_status(con
, IPP_REQUEST_VALUE
,
1851 _("The notify-user-data value is too large "
1852 "(%d > 63 octets)!"),
1853 attr
->values
[0].unknown
.length
);
1859 else if (!strcmp(attr
->name
, "notify-events") &&
1860 attr
->value_tag
== IPP_TAG_KEYWORD
)
1862 for (i
= 0; i
< attr
->num_values
; i
++)
1863 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
1865 else if (!strcmp(attr
->name
, "notify-lease-duration"))
1867 send_ipp_status(con
, IPP_BAD_REQUEST
,
1868 _("The notify-lease-duration attribute cannot be "
1869 "used with job subscriptions."));
1872 else if (!strcmp(attr
->name
, "notify-time-interval") &&
1873 attr
->value_tag
== IPP_TAG_INTEGER
)
1874 interval
= attr
->values
[0].integer
;
1879 if (!recipient
&& !pullmethod
)
1882 if (mask
== CUPSD_EVENT_NONE
)
1883 mask
= CUPSD_EVENT_JOB_COMPLETED
;
1885 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
1888 sub
->interval
= interval
;
1890 cupsdSetString(&sub
->owner
, job
->username
);
1894 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
1895 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
1896 sub
->user_data_len
);
1899 ippAddSeparator(con
->response
);
1900 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
1901 "notify-subscription-id", sub
->id
);
1907 cupsdSaveAllSubscriptions();
1910 * Remove all of the subscription attributes from the job request...
1913 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
1917 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
1918 attr
->group_tag
== IPP_TAG_ZERO
)
1921 * Free and remove this attribute...
1929 job
->attrs
->attrs
= next
;
1935 job
->attrs
->last
= prev
;
1936 job
->attrs
->current
= prev
;
1941 * 'add_job_uuid()' - Add job-uuid attribute to a job.
1943 * See RFC 4122 for the definition of UUIDs and the format.
1947 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
1948 cupsd_job_t
*job
) /* I - Job */
1950 char uuid
[1024]; /* job-uuid string */
1951 ipp_attribute_t
*attr
; /* job-uuid attribute */
1952 _cups_md5_state_t md5state
; /* MD5 state */
1953 unsigned char md5sum
[16]; /* MD5 digest/sum */
1957 * First see if the job already has a job-uuid attribute; if so, return...
1960 if ((attr
= ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
)) != NULL
)
1964 * No job-uuid attribute, so build a version 3 UUID with the local job
1965 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
1966 * the ServerName, server name and port that the client connected to,
1967 * and local job ID...
1970 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
1971 con
->serverport
, job
->id
);
1973 _cupsMD5Init(&md5state
);
1974 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
1975 _cupsMD5Finish(&md5state
, md5sum
);
1978 * Format the UUID URI using the MD5 sum and job ID.
1981 snprintf(uuid
, sizeof(uuid
),
1982 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
1983 "%02x%02x%02x%02x%02x%02x",
1984 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
1985 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
1986 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
1987 md5sum
[14], md5sum
[15]);
1989 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
1994 * 'add_printer()' - Add a printer to the system.
1998 add_printer(cupsd_client_t
*con
, /* I - Client connection */
1999 ipp_attribute_t
*uri
) /* I - URI of printer */
2001 http_status_t status
; /* Policy status */
2002 int i
; /* Looping var */
2003 char method
[HTTP_MAX_URI
], /* Method portion of URI */
2004 username
[HTTP_MAX_URI
], /* Username portion of URI */
2005 host
[HTTP_MAX_URI
], /* Host portion of URI */
2006 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2007 int port
; /* Port portion of URI */
2008 cupsd_printer_t
*printer
; /* Printer/class */
2009 ipp_attribute_t
*attr
; /* Printer attribute */
2010 cups_file_t
*fp
; /* Script/PPD file */
2011 char line
[1024]; /* Line from file... */
2012 char srcfile
[1024], /* Source Script/PPD file */
2013 dstfile
[1024]; /* Destination Script/PPD file */
2014 int modify
; /* Non-zero if we are modifying */
2015 char newname
[IPP_MAX_NAME
]; /* New printer name */
2016 int need_restart_job
; /* Need to restart job? */
2019 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2020 con
->http
.fd
, uri
->values
[0].string
.text
);
2023 * Do we have a valid URI?
2026 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2027 sizeof(method
), username
, sizeof(username
), host
,
2028 sizeof(host
), &port
, resource
, sizeof(resource
));
2030 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2033 * No, return an error...
2036 send_ipp_status(con
, IPP_BAD_REQUEST
,
2037 _("The printer-uri must be of the form "
2038 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2043 * Do we have a valid printer name?
2046 if (!validate_name(resource
+ 10))
2049 * No, return an error...
2052 send_ipp_status(con
, IPP_BAD_REQUEST
,
2053 _("The printer-uri \"%s\" contains invalid characters."),
2054 uri
->values
[0].string
.text
);
2062 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2064 send_http_error(con
, status
);
2069 * See if the printer already exists; if not, create a new printer...
2072 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2075 * Printer doesn't exist; see if we have a class of the same name...
2078 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2079 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2082 * Yes, return an error...
2085 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2086 _("A class named \"%s\" already exists!"),
2092 * No, add the printer...
2095 printer
= cupsdAddPrinter(resource
+ 10);
2098 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2101 * Rename the implicit printer to "AnyPrinter" or delete it...
2104 if (ImplicitAnyClasses
)
2106 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2107 cupsdRenamePrinter(printer
, newname
);
2110 cupsdDeletePrinter(printer
, 1);
2113 * Add the printer as a new local printer...
2116 printer
= cupsdAddPrinter(resource
+ 10);
2119 else if (printer
->type
& CUPS_PRINTER_REMOTE
)
2122 * Rename the remote printer to "Printer@server"...
2125 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2127 cupsdRenamePrinter(printer
, newname
);
2130 * Add the printer as a new local printer...
2133 printer
= cupsdAddPrinter(resource
+ 10);
2140 * Look for attributes and copy them over as needed...
2143 need_restart_job
= 0;
2145 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2146 IPP_TAG_TEXT
)) != NULL
)
2147 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2149 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2150 IPP_TAG_TEXT
)) != NULL
)
2151 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2153 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2154 IPP_TAG_URI
)) != NULL
)
2157 * Do we have a valid device URI?
2160 need_restart_job
= 1;
2162 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, method
,
2163 sizeof(method
), username
, sizeof(username
), host
,
2164 sizeof(host
), &port
, resource
, sizeof(resource
));
2166 if (!strcmp(method
, "file"))
2169 * See if the administrator has enabled file devices...
2172 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2175 * File devices are disabled and the URL is not file:/dev/null...
2178 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2179 _("File device URIs have been disabled! "
2180 "To enable, see the FileDevice directive in "
2181 "\"%s/cupsd.conf\"."),
2189 * See if the backend exists and is executable...
2192 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, method
);
2193 if (access(srcfile
, X_OK
))
2196 * Could not find device in list!
2199 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2200 attr
->values
[0].string
.text
);
2205 cupsdLogMessage(CUPSD_LOG_INFO
,
2206 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2208 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2210 cupsdSanitizeURI(printer
->device_uri
, resource
,
2213 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2216 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2217 IPP_TAG_KEYWORD
)) != NULL
)
2219 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2222 need_restart_job
= 1;
2224 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2226 for (i
= 0; i
< supported
->num_values
; i
++)
2227 if (!strcmp(supported
->values
[i
].string
.text
,
2228 attr
->values
[0].string
.text
))
2231 if (i
>= supported
->num_values
)
2233 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2234 attr
->values
[0].string
.text
);
2238 cupsdLogMessage(CUPSD_LOG_INFO
,
2239 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2240 printer
->name
, attr
->values
[0].string
.text
,
2241 printer
->port_monitor
);
2243 if (strcmp(attr
->values
[0].string
.text
, "none"))
2244 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2246 cupsdClearString(&printer
->port_monitor
);
2249 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2250 IPP_TAG_BOOLEAN
)) != NULL
)
2252 cupsdLogMessage(CUPSD_LOG_INFO
,
2253 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2254 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2256 printer
->accepting
= attr
->values
[0].boolean
;
2257 cupsdAddPrinterHistory(printer
);
2260 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2261 IPP_TAG_BOOLEAN
)) != NULL
)
2263 if (printer
->shared
&& !attr
->values
[0].boolean
)
2264 cupsdSendBrowseDelete(printer
);
2266 cupsdLogMessage(CUPSD_LOG_INFO
,
2267 "Setting %s printer-is-shared to %d (was %d.)",
2268 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2270 printer
->shared
= attr
->values
[0].boolean
;
2273 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2274 IPP_TAG_ENUM
)) != NULL
)
2276 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2277 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2279 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2280 attr
->values
[0].integer
);
2284 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)", printer
->name
,
2285 attr
->values
[0].integer
, printer
->state
);
2287 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2288 cupsdStopPrinter(printer
, 0);
2291 need_restart_job
= 1;
2292 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2295 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2296 IPP_TAG_TEXT
)) != NULL
)
2298 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2299 sizeof(printer
->state_message
));
2300 cupsdAddPrinterHistory(printer
);
2303 set_printer_defaults(con
, printer
);
2306 * See if we have all required attributes...
2309 if (!printer
->device_uri
)
2310 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2313 * See if we have an interface script or PPD file attached to the request...
2318 need_restart_job
= 1;
2320 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2322 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2325 * Yes; get the first line from it...
2329 cupsFileGets(fp
, line
, sizeof(line
));
2333 * Then see what kind of file it is...
2336 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2339 if (!strncmp(line
, "*PPD-Adobe", 10))
2342 * The new file is a PPD file, so remove any old interface script
2343 * that might be lying around...
2351 * This must be an interface script, so move the file over to the
2352 * interfaces directory and make it executable...
2355 if (copy_file(srcfile
, dstfile
))
2357 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2358 _("Unable to copy interface script - %s!"),
2364 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2365 "Copied interface script successfully!");
2366 chmod(dstfile
, 0755);
2370 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2373 if (!strncmp(line
, "*PPD-Adobe", 10))
2376 * The new file is a PPD file, so move the file over to the
2377 * ppd directory and make it readable by all...
2380 if (copy_file(srcfile
, dstfile
))
2382 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2383 _("Unable to copy PPD file - %s!"),
2389 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2390 "Copied PPD file successfully!");
2391 chmod(dstfile
, 0644);
2397 * This must be an interface script, so remove any old PPD file that
2398 * may be lying around...
2405 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2406 IPP_TAG_NAME
)) != NULL
)
2408 need_restart_job
= 1;
2410 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2413 * Raw driver, remove any existing PPD or interface script files.
2416 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2420 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2430 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2434 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2437 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2439 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2444 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2445 "Copied PPD file successfully!");
2446 chmod(dstfile
, 0644);
2452 * Update the printer attributes and return...
2455 cupsdSetPrinterAttrs(printer
);
2456 cupsdSaveAllPrinters();
2458 if (need_restart_job
&& printer
->job
)
2463 * Stop the current job and then restart it below...
2466 job
= (cupsd_job_t
*)printer
->job
;
2468 cupsdStopJob(job
, 1);
2470 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2471 job
->state_value
= IPP_JOB_PENDING
;
2474 if (need_restart_job
)
2477 cupsdWritePrintcap();
2481 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2482 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2485 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2486 printer
->name
, get_username(con
));
2490 cupsdAddPrinterHistory(printer
);
2492 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2493 "New printer \"%s\" added by \"%s\".", printer
->name
,
2496 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2497 printer
->name
, get_username(con
));
2500 con
->response
->request
.status
.status_code
= IPP_OK
;
2505 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2506 * based upon the printer state...
2510 add_printer_state_reasons(
2511 cupsd_client_t
*con
, /* I - Client connection */
2512 cupsd_printer_t
*p
) /* I - Printer info */
2514 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2515 "add_printer_state_reasons(%p[%d], %p[%s])",
2516 con
, con
->http
.fd
, p
, p
->name
);
2518 if (p
->num_reasons
== 0)
2519 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2520 "printer-state-reasons", NULL
,
2521 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2523 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2524 "printer-state-reasons", p
->num_reasons
, NULL
,
2525 (const char * const *)p
->reasons
);
2530 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2531 * the specified printer or class.
2535 add_queued_job_count(
2536 cupsd_client_t
*con
, /* I - Client connection */
2537 cupsd_printer_t
*p
) /* I - Printer or class */
2539 int count
; /* Number of jobs on destination */
2542 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2543 con
, con
->http
.fd
, p
, p
->name
);
2545 count
= cupsdGetPrinterJobCount(p
->name
);
2547 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2548 "queued-job-count", count
);
2553 * 'apply_printer_defaults()' - Apply printer default options to a job.
2557 apply_printer_defaults(
2558 cupsd_printer_t
*printer
, /* I - Printer */
2559 cupsd_job_t
*job
) /* I - Job */
2561 int i
, /* Looping var */
2562 num_options
; /* Number of default options */
2563 cups_option_t
*options
, /* Default options */
2564 *option
; /* Current option */
2568 * Collect all of the default options and add the missing ones to the
2572 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
2575 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2577 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2582 * Encode these options as attributes in the job object...
2585 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2586 cupsFreeOptions(num_options
, options
);
2591 * 'authenticate_job()' - Set job authentication info.
2595 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2596 ipp_attribute_t
*uri
) /* I - Job URI */
2598 ipp_attribute_t
*attr
; /* Job-id attribute */
2599 int jobid
; /* Job ID */
2600 cupsd_job_t
*job
; /* Current job */
2601 char method
[HTTP_MAX_URI
],
2602 /* Method portion of URI */
2603 username
[HTTP_MAX_URI
],
2604 /* Username portion of URI */
2606 /* Host portion of URI */
2607 resource
[HTTP_MAX_URI
];
2608 /* Resource portion of URI */
2609 int port
; /* Port portion of URI */
2612 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2613 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
2616 * Start with "everything is OK" status...
2619 con
->response
->request
.status
.status_code
= IPP_OK
;
2622 * See if we have a job URI or a printer URI...
2625 if (!strcmp(uri
->name
, "printer-uri"))
2628 * Got a printer URI; see if we also have a job-id attribute...
2631 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2632 IPP_TAG_INTEGER
)) == NULL
)
2634 send_ipp_status(con
, IPP_BAD_REQUEST
,
2635 _("Got a printer-uri attribute but no job-id!"));
2639 jobid
= attr
->values
[0].integer
;
2644 * Got a job URI; parse it to get the job ID...
2647 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2648 sizeof(method
), username
, sizeof(username
), host
,
2649 sizeof(host
), &port
, resource
, sizeof(resource
));
2651 if (strncmp(resource
, "/jobs/", 6))
2657 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
2658 uri
->values
[0].string
.text
);
2662 jobid
= atoi(resource
+ 6);
2666 * See if the job exists...
2669 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2672 * Nope - return a "not found" error...
2675 send_ipp_status(con
, IPP_NOT_FOUND
,
2676 _("Job #%d does not exist!"), jobid
);
2681 * See if the job has been completed...
2684 if (job
->state_value
!= IPP_JOB_HELD
)
2687 * Return a "not-possible" error...
2690 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2691 _("Job #%d is not held for authentication!"),
2697 * See if we have already authenticated...
2700 if (!con
->username
[0])
2702 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
2703 _("No authentication information provided!"));
2708 * See if the job is owned by the requesting user...
2711 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
2713 send_http_error(con
, HTTP_UNAUTHORIZED
);
2718 * Save the authentication information for this job...
2721 save_auth_info(con
, job
);
2724 * Reset the job-hold-until value to "no-hold"...
2727 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2728 IPP_TAG_KEYWORD
)) == NULL
)
2729 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2733 attr
->value_tag
= IPP_TAG_KEYWORD
;
2734 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2738 * Release the job and return...
2741 cupsdReleaseJob(job
);
2743 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was authenticated by \"%s\".", jobid
,
2749 * 'cancel_all_jobs()' - Cancel all print jobs.
2753 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
2754 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2756 http_status_t status
; /* Policy status */
2757 const char *dest
; /* Destination */
2758 cups_ptype_t dtype
; /* Destination type */
2759 char method
[HTTP_MAX_URI
], /* Method portion of URI */
2760 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2761 host
[HTTP_MAX_URI
], /* Host portion of URI */
2762 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2763 int port
; /* Port portion of URI */
2764 ipp_attribute_t
*attr
; /* Attribute in request */
2765 const char *username
; /* Username */
2766 int purge
; /* Purge? */
2767 cupsd_printer_t
*printer
; /* Printer */
2770 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
2771 con
->http
.fd
, uri
->values
[0].string
.text
);
2774 * See if we have a printer URI...
2777 if (strcmp(uri
->name
, "printer-uri"))
2779 send_ipp_status(con
, IPP_BAD_REQUEST
,
2780 _("The printer-uri attribute is required!"));
2785 * Get the username (if any) for the jobs we want to cancel (only if
2786 * "my-jobs" is specified...
2789 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
2790 IPP_TAG_BOOLEAN
)) != NULL
&&
2791 attr
->values
[0].boolean
)
2793 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
2794 IPP_TAG_NAME
)) != NULL
)
2795 username
= attr
->values
[0].string
.text
;
2798 send_ipp_status(con
, IPP_BAD_REQUEST
,
2799 _("Missing requesting-user-name attribute!"));
2807 * Look for the "purge-jobs" attribute...
2810 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
2811 IPP_TAG_BOOLEAN
)) != NULL
)
2812 purge
= attr
->values
[0].boolean
;
2817 * And if the destination is valid...
2820 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2821 sizeof(method
), userpass
, sizeof(userpass
), host
,
2822 sizeof(host
), &port
, resource
, sizeof(resource
));
2824 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
2830 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
2831 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
2833 send_ipp_status(con
, IPP_NOT_FOUND
,
2834 _("The printer or class was not found."));
2837 else if (strcmp(resource
, "/printers/"))
2839 send_ipp_status(con
, IPP_NOT_FOUND
,
2840 _("The printer-uri \"%s\" is not valid."),
2841 uri
->values
[0].string
.text
);
2849 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2851 send_http_error(con
, status
);
2856 * Cancel all jobs on all printers...
2859 cupsdCancelJobs(NULL
, username
, purge
);
2861 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
2862 purge
? "purged" : "cancelled", get_username(con
));
2870 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
2872 send_http_error(con
, status
);
2877 * Cancel all of the jobs on the named printer...
2880 cupsdCancelJobs(dest
, username
, purge
);
2882 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
2883 dest
, purge
? "purged" : "cancelled", get_username(con
));
2886 con
->response
->request
.status
.status_code
= IPP_OK
;
2891 * 'cancel_job()' - Cancel a print job.
2895 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
2896 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2898 ipp_attribute_t
*attr
; /* Current attribute */
2899 int jobid
; /* Job ID */
2900 char method
[HTTP_MAX_URI
], /* Method portion of URI */
2901 username
[HTTP_MAX_URI
], /* Username portion of URI */
2902 host
[HTTP_MAX_URI
], /* Host portion of URI */
2903 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2904 int port
; /* Port portion of URI */
2905 cupsd_job_t
*job
; /* Job information */
2906 const char *dest
; /* Destination */
2907 cups_ptype_t dtype
; /* Destination type (printer or class) */
2908 cupsd_printer_t
*printer
; /* Printer data */
2911 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
2912 con
->http
.fd
, uri
->values
[0].string
.text
);
2915 * See if we have a job URI or a printer URI...
2918 if (!strcmp(uri
->name
, "printer-uri"))
2921 * Got a printer URI; see if we also have a job-id attribute...
2924 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2925 IPP_TAG_INTEGER
)) == NULL
)
2927 send_ipp_status(con
, IPP_BAD_REQUEST
,
2928 _("Got a printer-uri attribute but no job-id!"));
2932 if ((jobid
= attr
->values
[0].integer
) == 0)
2935 * Find the current job on the specified printer...
2938 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2939 sizeof(method
), username
, sizeof(username
), host
,
2940 sizeof(host
), &port
, resource
, sizeof(resource
));
2942 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
2948 send_ipp_status(con
, IPP_NOT_FOUND
,
2949 _("The printer or class was not found."));
2954 * See if the printer is currently printing a job...
2958 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
2962 * No, see if there are any pending jobs...
2965 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
2967 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
2968 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
2969 !strcasecmp(job
->dest
, dest
))
2976 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
2986 * Got a job URI; parse it to get the job ID...
2989 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2990 sizeof(method
), username
, sizeof(username
), host
,
2991 sizeof(host
), &port
, resource
, sizeof(resource
));
2993 if (strncmp(resource
, "/jobs/", 6))
2999 send_ipp_status(con
, IPP_BAD_REQUEST
,
3000 _("Bad job-uri attribute \"%s\"!"),
3001 uri
->values
[0].string
.text
);
3005 jobid
= atoi(resource
+ 6);
3009 * See if the job exists...
3012 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3015 * Nope - return a "not found" error...
3018 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3023 * See if the job is owned by the requesting user...
3026 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3028 send_http_error(con
, HTTP_UNAUTHORIZED
);
3033 * See if the job is already completed, cancelled, or aborted; if so,
3034 * we can't cancel...
3037 if (job
->state_value
>= IPP_JOB_CANCELLED
)
3039 switch (job
->state_value
)
3041 case IPP_JOB_CANCELLED
:
3042 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3043 _("Job #%d is already cancelled - can\'t cancel."),
3047 case IPP_JOB_ABORTED
:
3048 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3049 _("Job #%d is already aborted - can\'t cancel."),
3054 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3055 _("Job #%d is already completed - can\'t cancel."),
3064 * Cancel the job and return...
3067 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3068 "Job cancelled by \"%s\".", username
);
3070 cupsdCancelJob(job
, 0);
3073 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was cancelled by \"%s\".", jobid
,
3076 con
->response
->request
.status
.status_code
= IPP_OK
;
3081 * 'cancel_subscription()' - Cancel a subscription.
3085 cancel_subscription(
3086 cupsd_client_t
*con
, /* I - Client connection */
3087 int sub_id
) /* I - Subscription ID */
3089 http_status_t status
; /* Policy status */
3090 cupsd_subscription_t
*sub
; /* Subscription */
3093 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3094 "cancel_subscription(con=%p[%d], sub_id=%d)",
3095 con
, con
->http
.fd
, sub_id
);
3098 * Is the subscription ID valid?
3101 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3104 * Bad subscription ID...
3107 send_ipp_status(con
, IPP_NOT_FOUND
,
3108 _("notify-subscription-id %d no good!"), sub_id
);
3116 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3118 con
, sub
->owner
)) != HTTP_OK
)
3120 send_http_error(con
, status
);
3125 * Cancel the subscription...
3128 cupsdDeleteSubscription(sub
, 1);
3130 con
->response
->request
.status
.status_code
= IPP_OK
;
3135 * 'check_quotas()' - Check quotas for a printer and user.
3138 static int /* O - 1 if OK, 0 if not */
3139 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3140 cupsd_printer_t
*p
) /* I - Printer or class */
3142 int i
; /* Looping var */
3143 char username
[33]; /* Username */
3144 cupsd_quota_t
*q
; /* Quota data */
3145 struct passwd
*pw
; /* User password data */
3148 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3149 con
, con
->http
.fd
, p
, p
->name
);
3159 * Figure out who is printing...
3162 strlcpy(username
, get_username(con
), sizeof(username
));
3165 * Check global active job limits for printers and users...
3168 if (MaxJobsPerPrinter
)
3171 * Check if there are too many pending jobs on this printer...
3174 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3176 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3185 * Check if there are too many pending jobs for this user...
3188 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3190 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3197 * Check against users...
3200 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3205 pw
= getpwnam(username
);
3208 for (i
= 0; i
< p
->num_users
; i
++)
3209 if (p
->users
[i
][0] == '@')
3212 * Check group membership...
3215 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3218 else if (!strcasecmp(username
, p
->users
[i
]))
3221 if ((i
< p
->num_users
) == p
->deny_users
)
3223 cupsdLogMessage(CUPSD_LOG_INFO
,
3224 "Denying user \"%s\" access to printer \"%s\"...",
3234 if (p
->k_limit
|| p
->page_limit
)
3236 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3238 cupsdLogMessage(CUPSD_LOG_ERROR
,
3239 "Unable to allocate quota data for user \"%s\"!",
3244 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3245 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3247 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3254 * If we have gotten this far, we're done!
3262 * 'copy_attribute()' - Copy a single attribute.
3265 static ipp_attribute_t
* /* O - New attribute */
3267 ipp_t
*to
, /* O - Destination request/response */
3268 ipp_attribute_t
*attr
, /* I - Attribute to copy */
3269 int quickcopy
) /* I - Do a quick copy? */
3271 int i
; /* Looping var */
3272 ipp_attribute_t
*toattr
; /* Destination attribute */
3275 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3276 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
3277 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
3280 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
3283 toattr
= ippAddSeparator(to
);
3286 case IPP_TAG_INTEGER
:
3288 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3289 attr
->name
, attr
->num_values
, NULL
);
3291 for (i
= 0; i
< attr
->num_values
; i
++)
3292 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
3295 case IPP_TAG_BOOLEAN
:
3296 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
3297 attr
->num_values
, NULL
);
3299 for (i
= 0; i
< attr
->num_values
; i
++)
3300 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
3303 case IPP_TAG_STRING
:
3306 case IPP_TAG_KEYWORD
:
3308 case IPP_TAG_URISCHEME
:
3309 case IPP_TAG_CHARSET
:
3310 case IPP_TAG_LANGUAGE
:
3311 case IPP_TAG_MIMETYPE
:
3312 toattr
= ippAddStrings(to
, attr
->group_tag
,
3313 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3314 attr
->name
, attr
->num_values
, NULL
, NULL
);
3318 for (i
= 0; i
< attr
->num_values
; i
++)
3319 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3323 for (i
= 0; i
< attr
->num_values
; i
++)
3324 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3329 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
3330 attr
->values
[0].date
);
3333 case IPP_TAG_RESOLUTION
:
3334 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
3335 attr
->num_values
, IPP_RES_PER_INCH
,
3338 for (i
= 0; i
< attr
->num_values
; i
++)
3340 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
3341 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
3342 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
3346 case IPP_TAG_RANGE
:
3347 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
3348 attr
->num_values
, NULL
, NULL
);
3350 for (i
= 0; i
< attr
->num_values
; i
++)
3352 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
3353 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
3357 case IPP_TAG_TEXTLANG
:
3358 case IPP_TAG_NAMELANG
:
3359 toattr
= ippAddStrings(to
, attr
->group_tag
,
3360 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3361 attr
->name
, attr
->num_values
, NULL
, NULL
);
3365 for (i
= 0; i
< attr
->num_values
; i
++)
3367 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
3368 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3373 for (i
= 0; i
< attr
->num_values
; i
++)
3376 toattr
->values
[i
].string
.charset
=
3377 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
3379 toattr
->values
[i
].string
.charset
=
3380 toattr
->values
[0].string
.charset
;
3382 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3387 case IPP_TAG_BEGIN_COLLECTION
:
3388 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
3389 attr
->num_values
, NULL
);
3391 for (i
= 0; i
< attr
->num_values
; i
++)
3393 toattr
->values
[i
].collection
= ippNew();
3394 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
3395 NULL
, IPP_TAG_ZERO
, 0);
3400 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3401 attr
->name
, attr
->num_values
, NULL
);
3403 for (i
= 0; i
< attr
->num_values
; i
++)
3405 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
3407 if (toattr
->values
[i
].unknown
.length
> 0)
3409 if ((toattr
->values
[i
].unknown
.data
=
3410 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
3411 toattr
->values
[i
].unknown
.length
= 0;
3413 memcpy(toattr
->values
[i
].unknown
.data
,
3414 attr
->values
[i
].unknown
.data
,
3415 toattr
->values
[i
].unknown
.length
);
3418 break; /* anti-compiler-warning-code */
3426 * 'copy_attrs()' - Copy attributes from one request to another.
3430 copy_attrs(ipp_t
*to
, /* I - Destination request */
3431 ipp_t
*from
, /* I - Source request */
3432 cups_array_t
*ra
, /* I - Requested attributes */
3433 ipp_tag_t group
, /* I - Group to copy */
3434 int quickcopy
) /* I - Do a quick copy? */
3436 ipp_attribute_t
*fromattr
; /* Source attribute */
3439 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3440 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3441 to
, from
, ra
, group
, quickcopy
);
3446 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3449 * Filter attributes as needed...
3452 if (group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3453 fromattr
->group_tag
!= IPP_TAG_ZERO
&& !fromattr
->name
)
3456 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
3457 copy_attribute(to
, fromattr
, quickcopy
);
3463 * 'copy_banner()' - Copy a banner file to the requests directory for the
3467 static int /* O - Size of banner file in kbytes */
3468 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
3469 cupsd_job_t
*job
, /* I - Job information */
3470 const char *name
) /* I - Name of banner */
3472 int i
; /* Looping var */
3473 int kbytes
; /* Size of banner file in kbytes */
3474 char filename
[1024]; /* Job filename */
3475 cupsd_banner_t
*banner
; /* Pointer to banner */
3476 cups_file_t
*in
; /* Input file */
3477 cups_file_t
*out
; /* Output file */
3478 int ch
; /* Character from file */
3479 char attrname
[255], /* Name of attribute */
3480 *s
; /* Pointer into name */
3481 ipp_attribute_t
*attr
; /* Attribute */
3484 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
3485 con
, con
->http
.fd
, job
, job
->id
, name
? name
: "(null)");
3488 * Find the banner; return if not found or "none"...
3491 if (!name
|| !strcmp(name
, "none") ||
3492 (banner
= cupsdFindBanner(name
)) == NULL
)
3496 * Open the banner and job files...
3499 if (add_file(con
, job
, banner
->filetype
, 0))
3502 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
3504 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
3506 cupsdLogMessage(CUPSD_LOG_ERROR
,
3507 "copy_banner: Unable to create banner job file %s - %s",
3508 filename
, strerror(errno
));
3513 fchmod(cupsFileNumber(out
), 0640);
3514 fchown(cupsFileNumber(out
), RunUser
, Group
);
3517 * Try the localized banner file under the subdirectory...
3520 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
3522 if (strlen(attrname
) > 2 && attrname
[2] == '-')
3525 * Convert ll-cc to ll_CC...
3529 attrname
[3] = toupper(attrname
[3] & 255);
3530 attrname
[4] = toupper(attrname
[4] & 255);
3533 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3536 if (access(filename
, 0) && strlen(attrname
) > 2)
3539 * Wasn't able to find "ll_CC" locale file; try the non-national
3540 * localization banner directory.
3545 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3549 if (access(filename
, 0))
3552 * Use the non-localized banner file.
3555 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
3558 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
3562 cupsdLogMessage(CUPSD_LOG_ERROR
,
3563 "copy_banner: Unable to open banner template file %s - %s",
3564 filename
, strerror(errno
));
3570 * Parse the file to the end...
3573 while ((ch
= cupsFileGetChar(in
)) != EOF
)
3577 * Get an attribute name...
3580 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
3581 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
3583 else if (s
< (attrname
+ sizeof(attrname
) - 1))
3593 * Ignore { followed by stuff that is not an attribute name...
3596 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
3601 * See if it is defined...
3604 if (attrname
[0] == '?')
3609 if (!strcmp(s
, "printer-name"))
3611 cupsFilePuts(out
, job
->dest
);
3614 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
3617 * See if we have a leading question mark...
3620 if (attrname
[0] != '?')
3623 * Nope, write to file as-is; probably a PostScript procedure...
3626 cupsFilePrintf(out
, "{%s}", attrname
);
3633 * Output value(s)...
3636 for (i
= 0; i
< attr
->num_values
; i
++)
3639 cupsFilePutChar(out
, ',');
3641 switch (attr
->value_tag
)
3643 case IPP_TAG_INTEGER
:
3645 if (!strncmp(s
, "time-at-", 8))
3646 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
3648 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
3651 case IPP_TAG_BOOLEAN
:
3652 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
3655 case IPP_TAG_NOVALUE
:
3656 cupsFilePuts(out
, "novalue");
3659 case IPP_TAG_RANGE
:
3660 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
3661 attr
->values
[i
].range
.upper
);
3664 case IPP_TAG_RESOLUTION
:
3665 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3666 attr
->values
[i
].resolution
.yres
,
3667 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3672 case IPP_TAG_STRING
:
3675 case IPP_TAG_KEYWORD
:
3676 case IPP_TAG_CHARSET
:
3677 case IPP_TAG_LANGUAGE
:
3678 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
3681 * Need to quote strings for PS banners...
3686 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
3688 if (*p
== '(' || *p
== ')' || *p
== '\\')
3690 cupsFilePutChar(out
, '\\');
3691 cupsFilePutChar(out
, *p
);
3693 else if (*p
< 32 || *p
> 126)
3694 cupsFilePrintf(out
, "\\%03o", *p
& 255);
3696 cupsFilePutChar(out
, *p
);
3700 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
3704 break; /* anti-compiler-warning-code */
3708 else if (ch
== '\\') /* Quoted char */
3710 ch
= cupsFileGetChar(in
);
3712 if (ch
!= '{') /* Only do special handling for \{ */
3713 cupsFilePutChar(out
, '\\');
3715 cupsFilePutChar(out
, ch
);
3718 cupsFilePutChar(out
, ch
);
3722 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
3724 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
3725 IPP_TAG_INTEGER
)) != NULL
)
3726 attr
->values
[0].integer
+= kbytes
;
3735 * 'copy_file()' - Copy a PPD file or interface script...
3738 static int /* O - 0 = success, -1 = error */
3739 copy_file(const char *from
, /* I - Source file */
3740 const char *to
) /* I - Destination file */
3742 cups_file_t
*src
, /* Source file */
3743 *dst
; /* Destination file */
3744 int bytes
; /* Bytes to read/write */
3745 char buffer
[2048]; /* Copy buffer */
3748 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
3751 * Open the source and destination file for a copy...
3754 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
3757 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
3764 * Copy the source file to the destination...
3767 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
3768 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
3776 * Close both files and return...
3781 return (cupsFileClose(dst
));
3786 * 'copy_model()' - Copy a PPD model file, substituting default values
3790 static int /* O - 0 = success, -1 = error */
3791 copy_model(cupsd_client_t
*con
, /* I - Client connection */
3792 const char *from
, /* I - Source file */
3793 const char *to
) /* I - Destination file */
3795 fd_set
*input
; /* select() input set */
3796 struct timeval timeout
; /* select() timeout */
3797 int maxfd
; /* Maximum file descriptor for select() */
3798 char tempfile
[1024]; /* Temporary PPD file */
3799 int tempfd
; /* Temporary PPD file descriptor */
3800 int temppid
; /* Process ID of cups-driverd */
3801 int temppipe
[2]; /* Temporary pipes */
3802 char *argv
[4], /* Command-line arguments */
3803 *envp
[MAX_ENV
]; /* Environment */
3804 cups_file_t
*src
, /* Source file */
3805 *dst
; /* Destination file */
3806 int bytes
, /* Bytes from pipe */
3807 total
; /* Total bytes from pipe */
3808 char buffer
[2048], /* Copy buffer */
3809 *ptr
; /* Pointer into buffer */
3810 int i
; /* Looping var */
3811 char option
[PPD_MAX_NAME
], /* Option name */
3812 choice
[PPD_MAX_NAME
]; /* Choice name */
3813 int num_defaults
; /* Number of default options */
3814 ppd_default_t
*defaults
; /* Default options */
3815 char cups_protocol
[PPD_MAX_LINE
];
3816 /* cupsProtocol attribute */
3817 int have_letter
, /* Have Letter size */
3818 have_a4
; /* Have A4 size */
3819 #ifdef HAVE_LIBPAPER
3820 char *paper_result
; /* Paper size name from libpaper */
3821 char system_paper
[64]; /* Paper size name buffer */
3822 #endif /* HAVE_LIBPAPER */
3825 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3826 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
3830 * Run cups-driverd to get the PPD file...
3833 argv
[0] = "cups-driverd";
3835 argv
[2] = (char *)from
;
3838 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3840 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
3841 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
3842 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3846 cupsdOpenPipe(temppipe
);
3848 if ((input
= calloc(1, SetSize
)) == NULL
)
3853 cupsdLogMessage(CUPSD_LOG_ERROR
,
3854 "copy_model: Unable to allocate %d bytes for select()...",
3859 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3860 "copy_model: Running \"cups-driverd cat %s\"...", from
);
3862 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
3874 * Wait up to 30 seconds for the PPD file to be copied...
3879 if (temppipe
[0] > CGIPipes
[0])
3880 maxfd
= temppipe
[0] + 1;
3882 maxfd
= CGIPipes
[0] + 1;
3887 * See if we have data ready...
3892 FD_SET(temppipe
[0], input
);
3893 FD_SET(CGIPipes
[0], input
);
3895 timeout
.tv_sec
= 30;
3896 timeout
.tv_usec
= 0;
3898 if ((i
= select(maxfd
, input
, NULL
, NULL
, &timeout
)) < 0)
3908 * We have timed out...
3914 if (FD_ISSET(temppipe
[0], input
))
3917 * Read the PPD file from the pipe, and write it to the PPD file.
3920 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
3922 if (write(tempfd
, buffer
, bytes
) < bytes
)
3931 if (FD_ISSET(CGIPipes
[0], input
))
3943 * No data from cups-deviced...
3946 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
3952 * Read the source file and see what page sizes are supported...
3955 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
3964 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
3965 if (!strncmp(buffer
, "*PageSize ", 10))
3968 * Strip UI text and command data from the end of the line...
3971 if ((ptr
= strchr(buffer
+ 10, '/')) != NULL
)
3973 if ((ptr
= strchr(buffer
+ 10, ':')) != NULL
)
3976 for (ptr
= buffer
+ 10; isspace(*ptr
); ptr
++);
3979 * Look for Letter and A4 page sizes...
3982 if (!strcmp(ptr
, "Letter"))
3985 if (!strcmp(ptr
, "A4"))
3989 cupsFileRewind(src
);
3992 * Open the destination (if possible) and set the default options...
3997 cups_protocol
[0] = '\0';
3999 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4002 * Read all of the default lines from the old PPD...
4005 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4006 if (!strncmp(buffer
, "*Default", 8))
4009 * Add the default option...
4012 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4013 choice
, sizeof(choice
)))
4014 num_defaults
= ppd_add_default(option
, choice
, num_defaults
,
4017 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4018 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4022 #ifdef HAVE_LIBPAPER
4023 else if ((paper_result
= systempapername()) != NULL
)
4026 * Set the default media sizes from the systemwide default...
4029 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
4030 system_paper
[0] = toupper(system_paper
[0] & 255);
4032 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
4033 (!strcmp(system_paper
, "A4") && have_a4
))
4035 num_defaults
= ppd_add_default("PageSize", system_paper
,
4036 num_defaults
, &defaults
);
4037 num_defaults
= ppd_add_default("PageRegion", system_paper
,
4038 num_defaults
, &defaults
);
4039 num_defaults
= ppd_add_default("PaperDimension", system_paper
,
4040 num_defaults
, &defaults
);
4041 num_defaults
= ppd_add_default("ImageableArea", system_paper
,
4042 num_defaults
, &defaults
);
4045 #endif /* HAVE_LIBPAPER */
4049 * Add the default media sizes...
4051 * Note: These values are generally not valid for large-format devices
4052 * like plotters, however it is probably safe to say that those
4053 * users will configure the media size after initially adding
4054 * the device anyways...
4057 if (!DefaultLanguage
||
4058 !strcasecmp(DefaultLanguage
, "C") ||
4059 !strcasecmp(DefaultLanguage
, "POSIX") ||
4060 !strcasecmp(DefaultLanguage
, "en") ||
4061 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4062 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4063 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4066 * These are the only locales that will default to "letter" size...
4071 num_defaults
= ppd_add_default("PageSize", "Letter", num_defaults
,
4073 num_defaults
= ppd_add_default("PageRegion", "Letter", num_defaults
,
4075 num_defaults
= ppd_add_default("PaperDimension", "Letter", num_defaults
,
4077 num_defaults
= ppd_add_default("ImageableArea", "Letter", num_defaults
,
4084 * The rest default to "a4" size...
4087 num_defaults
= ppd_add_default("PageSize", "A4", num_defaults
,
4089 num_defaults
= ppd_add_default("PageRegion", "A4", num_defaults
,
4091 num_defaults
= ppd_add_default("PaperDimension", "A4", num_defaults
,
4093 num_defaults
= ppd_add_default("ImageableArea", "A4", num_defaults
,
4099 * Open the destination file for a copy...
4102 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4104 if (num_defaults
> 0)
4113 * Copy the source file to the destination...
4116 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4118 if (!strncmp(buffer
, "*Default", 8))
4121 * Check for an previous default option choice...
4124 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4125 choice
, sizeof(choice
)))
4127 for (i
= 0; i
< num_defaults
; i
++)
4128 if (!strcmp(option
, defaults
[i
].option
))
4131 * Substitute the previous choice...
4134 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
,
4135 defaults
[i
].choice
);
4141 cupsFilePrintf(dst
, "%s\n", buffer
);
4144 if (cups_protocol
[0])
4145 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4147 if (num_defaults
> 0)
4151 * Close both files and return...
4158 return (cupsFileClose(dst
));
4163 * 'copy_job_attrs()' - Copy job attributes.
4167 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4168 cupsd_job_t
*job
, /* I - Job */
4169 cups_array_t
*ra
) /* I - Requested attributes array */
4171 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4175 * Send the requested attributes for each job...
4178 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4179 con
->servername
, con
->serverport
, "/jobs/%d",
4182 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
4183 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4184 "job-more-info", NULL
, job_uri
);
4186 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4187 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4188 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4189 job
->num_files
> 0);
4191 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
4192 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4193 "job-printer-up-time", time(NULL
));
4195 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4196 add_job_state_reasons(con
, job
);
4198 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4199 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4200 "job-uri", NULL
, job_uri
);
4202 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
4207 * 'copy_printer_attrs()' - Copy printer attributes.
4212 cupsd_client_t
*con
, /* I - Client connection */
4213 cupsd_printer_t
*printer
, /* I - Printer */
4214 cups_array_t
*ra
) /* I - Requested attributes array */
4216 char printer_uri
[HTTP_MAX_URI
];
4218 time_t curtime
; /* Current time */
4219 int i
; /* Looping var */
4220 ipp_attribute_t
*history
; /* History collection */
4224 * Copy the printer attributes to the response using requested-attributes
4225 * and document-format attributes that may be provided by the client.
4228 curtime
= time(NULL
);
4231 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
4232 printer
->recoverable
)
4233 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4234 "com.apple.print.recoverable-message", NULL
,
4235 printer
->recoverable
);
4236 #endif /* __APPLE__ */
4238 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4239 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4240 ippTimeToDate(curtime
));
4242 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4243 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4244 "printer-error-policy", NULL
, printer
->error_policy
);
4246 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4247 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
4248 printer
->accepting
);
4250 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4251 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
4254 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4255 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4256 "printer-op-policy", NULL
, printer
->op_policy
);
4258 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4259 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4262 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4263 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4264 "printer-state-change-time", printer
->state_time
);
4266 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
4267 cupsArrayFind(ra
, "printer-state-history"))
4270 * Printer history is only sent if specifically requested, so that
4271 * older CUPS/IPP clients won't barf on the collection attributes.
4274 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
4275 "printer-state-history",
4276 printer
->num_history
, NULL
);
4278 for (i
= 0; i
< printer
->num_history
; i
++)
4279 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
4280 NULL
, IPP_TAG_ZERO
, 0);
4283 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4284 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4285 "printer-state-message", NULL
, printer
->state_message
);
4287 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4288 add_printer_state_reasons(con
, printer
);
4290 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4292 int type
; /* printer-type value */
4296 * Add the CUPS-specific printer-type attribute...
4299 type
= printer
->type
;
4301 if (printer
== DefaultPrinter
)
4302 type
|= CUPS_PRINTER_DEFAULT
;
4304 if (!printer
->accepting
)
4305 type
|= CUPS_PRINTER_REJECTING
;
4307 if (!printer
->shared
)
4308 type
|= CUPS_PRINTER_NOT_SHARED
;
4310 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
4314 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4315 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4316 "printer-up-time", curtime
);
4318 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
4319 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
4322 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4323 "ipp", NULL
, con
->servername
, con
->serverport
,
4324 "/printers/%s", printer
->name
);
4325 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4326 "printer-uri-supported", NULL
, printer_uri
);
4327 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4331 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4332 add_queued_job_count(con
, printer
);
4334 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
4335 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
4340 * 'copy_subscription_attrs()' - Copy subscription attributes.
4344 copy_subscription_attrs(
4345 cupsd_client_t
*con
, /* I - Client connection */
4346 cupsd_subscription_t
*sub
, /* I - Subscription */
4347 cups_array_t
*ra
) /* I - Requested attributes array */
4349 ipp_attribute_t
*attr
; /* Current attribute */
4350 char printer_uri
[HTTP_MAX_URI
];
4352 int count
; /* Number of events */
4353 unsigned mask
; /* Current event mask */
4354 const char *name
; /* Current event name */
4358 * Copy the subscription attributes to the response using the
4359 * requested-attributes attribute that may be provided by the client.
4362 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
4364 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
4367 * Simple event list...
4370 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
4371 IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
4372 "notify-events", NULL
, name
);
4377 * Complex event list...
4380 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4381 if (sub
->mask
& mask
)
4384 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
4385 IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
4386 "notify-events", count
, NULL
, NULL
);
4388 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4389 if (sub
->mask
& mask
)
4391 attr
->values
[count
].string
.text
=
4392 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
4399 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
4400 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4401 "notify-job-id", sub
->job
->id
);
4403 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
4404 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4405 "notify-lease-duration", sub
->lease
);
4407 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
4409 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4410 "ipp", NULL
, con
->servername
, con
->serverport
,
4411 "/printers/%s", sub
->dest
->name
);
4412 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4413 "notify-printer-uri", NULL
, printer_uri
);
4416 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
4417 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4418 "notify-recipient-uri", NULL
, sub
->recipient
);
4419 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
4420 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
4421 "notify-pull-method", NULL
, "ippget");
4423 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
4424 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
4425 "notify-subscriber-user-name", NULL
, sub
->owner
);
4427 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
4428 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4429 "notify-subscription-id", sub
->id
);
4431 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
4432 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4433 "notify-time-interval", sub
->interval
);
4435 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
4436 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
4437 sub
->user_data
, sub
->user_data_len
);
4442 * 'create_job()' - Print a file to a printer or class.
4446 create_job(cupsd_client_t
*con
, /* I - Client connection */
4447 ipp_attribute_t
*uri
) /* I - Printer URI */
4449 cupsd_job_t
*job
; /* New job */
4452 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
4453 con
->http
.fd
, uri
->values
[0].string
.text
);
4456 * Create the job object...
4459 if ((job
= add_job(con
, uri
, NULL
, NULL
)) == NULL
)
4463 * Save and log the job...
4468 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d created on \"%s\" by \"%s\".",
4469 job
->id
, job
->dest
, job
->username
);
4474 * 'create_requested_array()' - Create an array for the requested-attributes.
4477 static cups_array_t
* /* O - Array of attributes or NULL */
4478 create_requested_array(ipp_t
*request
) /* I - IPP request */
4480 int i
; /* Looping var */
4481 ipp_attribute_t
*requested
; /* requested-attributes attribute */
4482 cups_array_t
*ra
; /* Requested attributes array */
4483 char *value
; /* Current value */
4487 * Get the requested-attributes attribute, and return NULL if we don't
4491 if ((requested
= ippFindAttribute(request
, "requested-attributes",
4492 IPP_TAG_KEYWORD
)) == NULL
)
4496 * If the attribute contains a single "all" keyword, return NULL...
4499 if (requested
->num_values
== 1 &&
4500 !strcmp(requested
->values
[0].string
.text
, "all"))
4504 * Create an array using "strcmp" as the comparison function...
4507 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
4509 for (i
= 0; i
< requested
->num_values
; i
++)
4511 value
= requested
->values
[i
].string
.text
;
4513 if (!strcmp(value
, "job-template"))
4515 cupsArrayAdd(ra
, "copies");
4516 cupsArrayAdd(ra
, "copies-default");
4517 cupsArrayAdd(ra
, "copies-supported");
4518 cupsArrayAdd(ra
, "finishings");
4519 cupsArrayAdd(ra
, "finishings-default");
4520 cupsArrayAdd(ra
, "finishings-supported");
4521 cupsArrayAdd(ra
, "job-hold-until");
4522 cupsArrayAdd(ra
, "job-hold-until-default");
4523 cupsArrayAdd(ra
, "job-hold-until-supported");
4524 cupsArrayAdd(ra
, "job-priority");
4525 cupsArrayAdd(ra
, "job-priority-default");
4526 cupsArrayAdd(ra
, "job-priority-supported");
4527 cupsArrayAdd(ra
, "job-sheets");
4528 cupsArrayAdd(ra
, "job-sheets-default");
4529 cupsArrayAdd(ra
, "job-sheets-supported");
4530 cupsArrayAdd(ra
, "media");
4531 cupsArrayAdd(ra
, "media-default");
4532 cupsArrayAdd(ra
, "media-supported");
4533 cupsArrayAdd(ra
, "multiple-document-handling");
4534 cupsArrayAdd(ra
, "multiple-document-handling-default");
4535 cupsArrayAdd(ra
, "multiple-document-handling-supported");
4536 cupsArrayAdd(ra
, "number-up");
4537 cupsArrayAdd(ra
, "number-up-default");
4538 cupsArrayAdd(ra
, "number-up-supported");
4539 cupsArrayAdd(ra
, "orientation-requested");
4540 cupsArrayAdd(ra
, "orientation-requested-default");
4541 cupsArrayAdd(ra
, "orientation-requested-supported");
4542 cupsArrayAdd(ra
, "page-ranges");
4543 cupsArrayAdd(ra
, "page-ranges-supported");
4544 cupsArrayAdd(ra
, "printer-resolution");
4545 cupsArrayAdd(ra
, "printer-resolution-default");
4546 cupsArrayAdd(ra
, "printer-resolution-supported");
4547 cupsArrayAdd(ra
, "print-quality");
4548 cupsArrayAdd(ra
, "print-quality-default");
4549 cupsArrayAdd(ra
, "print-quality-supported");
4550 cupsArrayAdd(ra
, "sides");
4551 cupsArrayAdd(ra
, "sides-default");
4552 cupsArrayAdd(ra
, "sides-supported");
4554 else if (!strcmp(value
, "job-description"))
4556 cupsArrayAdd(ra
, "date-time-at-completed");
4557 cupsArrayAdd(ra
, "date-time-at-creation");
4558 cupsArrayAdd(ra
, "date-time-at-processing");
4559 cupsArrayAdd(ra
, "job-detailed-status-message");
4560 cupsArrayAdd(ra
, "job-document-access-errors");
4561 cupsArrayAdd(ra
, "job-id");
4562 cupsArrayAdd(ra
, "job-impressions");
4563 cupsArrayAdd(ra
, "job-impressions-completed");
4564 cupsArrayAdd(ra
, "job-k-octets");
4565 cupsArrayAdd(ra
, "job-k-octets-processed");
4566 cupsArrayAdd(ra
, "job-media-sheets");
4567 cupsArrayAdd(ra
, "job-media-sheets-completed");
4568 cupsArrayAdd(ra
, "job-message-from-operator");
4569 cupsArrayAdd(ra
, "job-more-info");
4570 cupsArrayAdd(ra
, "job-name");
4571 cupsArrayAdd(ra
, "job-originating-user-name");
4572 cupsArrayAdd(ra
, "job-printer-up-time");
4573 cupsArrayAdd(ra
, "job-printer-uri");
4574 cupsArrayAdd(ra
, "job-state");
4575 cupsArrayAdd(ra
, "job-state-message");
4576 cupsArrayAdd(ra
, "job-state-reasons");
4577 cupsArrayAdd(ra
, "job-uri");
4578 cupsArrayAdd(ra
, "number-of-documents");
4579 cupsArrayAdd(ra
, "number-of-intervening-jobs");
4580 cupsArrayAdd(ra
, "output-device-assigned");
4581 cupsArrayAdd(ra
, "time-at-completed");
4582 cupsArrayAdd(ra
, "time-at-creation");
4583 cupsArrayAdd(ra
, "time-at-processing");
4585 else if (!strcmp(value
, "printer-description"))
4587 cupsArrayAdd(ra
, "charset-configured");
4588 cupsArrayAdd(ra
, "charset-supported");
4589 cupsArrayAdd(ra
, "color-supported");
4590 cupsArrayAdd(ra
, "compression-supported");
4591 cupsArrayAdd(ra
, "document-format-default");
4592 cupsArrayAdd(ra
, "document-format-supported");
4593 cupsArrayAdd(ra
, "generated-natural-language-supported");
4594 cupsArrayAdd(ra
, "ipp-versions-supported");
4595 cupsArrayAdd(ra
, "job-impressions-supported");
4596 cupsArrayAdd(ra
, "job-k-octets-supported");
4597 cupsArrayAdd(ra
, "job-media-sheets-supported");
4598 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
4599 cupsArrayAdd(ra
, "multiple-operation-time-out");
4600 cupsArrayAdd(ra
, "natural-language-configured");
4601 cupsArrayAdd(ra
, "notify-attributes-supported");
4602 cupsArrayAdd(ra
, "notify-lease-duration-default");
4603 cupsArrayAdd(ra
, "notify-lease-duration-supported");
4604 cupsArrayAdd(ra
, "notify-max-events-supported");
4605 cupsArrayAdd(ra
, "notify-notify-events-default");
4606 cupsArrayAdd(ra
, "notify-notify-events-supported");
4607 cupsArrayAdd(ra
, "notify-pull-method-supported");
4608 cupsArrayAdd(ra
, "notify-schemes-supported");
4609 cupsArrayAdd(ra
, "operations-supported");
4610 cupsArrayAdd(ra
, "pages-per-minute");
4611 cupsArrayAdd(ra
, "pages-per-minute-color");
4612 cupsArrayAdd(ra
, "pdl-override-supported");
4613 cupsArrayAdd(ra
, "printer-current-time");
4614 cupsArrayAdd(ra
, "printer-driver-installer");
4615 cupsArrayAdd(ra
, "printer-info");
4616 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
4617 cupsArrayAdd(ra
, "printer-location");
4618 cupsArrayAdd(ra
, "printer-make-and-model");
4619 cupsArrayAdd(ra
, "printer-message-from-operator");
4620 cupsArrayAdd(ra
, "printer-more-info");
4621 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
4622 cupsArrayAdd(ra
, "printer-name");
4623 cupsArrayAdd(ra
, "printer-state");
4624 cupsArrayAdd(ra
, "printer-state-message");
4625 cupsArrayAdd(ra
, "printer-state-reasons");
4626 cupsArrayAdd(ra
, "printer-up-time");
4627 cupsArrayAdd(ra
, "printer-uri-supported");
4628 cupsArrayAdd(ra
, "queued-job-count");
4629 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
4630 cupsArrayAdd(ra
, "uri-authentication-supported");
4631 cupsArrayAdd(ra
, "uri-security-supported");
4633 else if (!strcmp(value
, "subscription-template"))
4635 cupsArrayAdd(ra
, "notify-attributes");
4636 cupsArrayAdd(ra
, "notify-charset");
4637 cupsArrayAdd(ra
, "notify-events");
4638 cupsArrayAdd(ra
, "notify-lease-duration");
4639 cupsArrayAdd(ra
, "notify-natural-language");
4640 cupsArrayAdd(ra
, "notify-pull-method");
4641 cupsArrayAdd(ra
, "notify-recipient-uri");
4642 cupsArrayAdd(ra
, "notify-time-interval");
4643 cupsArrayAdd(ra
, "notify-user-data");
4646 cupsArrayAdd(ra
, value
);
4654 * 'create_subscription()' - Create a notification subscription.
4658 create_subscription(
4659 cupsd_client_t
*con
, /* I - Client connection */
4660 ipp_attribute_t
*uri
) /* I - Printer URI */
4662 http_status_t status
; /* Policy status */
4663 int i
; /* Looping var */
4664 ipp_attribute_t
*attr
; /* Current attribute */
4665 const char *dest
; /* Destination */
4666 cups_ptype_t dtype
; /* Destination type (printer or class) */
4667 char method
[HTTP_MAX_URI
],
4668 /* Method portion of URI */
4669 userpass
[HTTP_MAX_URI
],
4670 /* Username portion of URI */
4672 /* Host portion of URI */
4673 resource
[HTTP_MAX_URI
];
4674 /* Resource portion of URI */
4675 int port
; /* Port portion of URI */
4676 cupsd_printer_t
*printer
; /* Printer/class */
4677 cupsd_job_t
*job
; /* Job */
4678 int jobid
; /* Job ID */
4679 cupsd_subscription_t
*sub
; /* Subscription object */
4680 const char *username
, /* requesting-user-name or authenticated username */
4681 *recipient
, /* notify-recipient-uri */
4682 *pullmethod
; /* notify-pull-method */
4683 ipp_attribute_t
*user_data
; /* notify-user-data */
4684 int interval
, /* notify-time-interval */
4685 lease
; /* notify-lease-duration */
4686 unsigned mask
; /* notify-events */
4690 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4692 if (attr
->group_tag
!= IPP_TAG_ZERO
)
4693 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
4694 attr
->value_tag
, attr
->name
);
4696 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
4701 * Is the destination valid?
4704 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4705 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
4706 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
4708 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
4709 sizeof(method
), userpass
, sizeof(userpass
), host
,
4710 sizeof(host
), &port
, resource
, sizeof(resource
));
4712 if (!strcmp(resource
, "/"))
4715 dtype
= (cups_ptype_t
)0;
4718 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
4721 dtype
= (cups_ptype_t
)0;
4724 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
4727 dtype
= CUPS_PRINTER_CLASS
;
4730 else if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
4736 send_ipp_status(con
, IPP_NOT_FOUND
,
4737 _("The printer or class was not found."));
4747 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
4749 send_http_error(con
, status
);
4753 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
4755 send_http_error(con
, status
);
4760 * Get the user that is requesting the subscription...
4763 username
= get_username(con
);
4766 * Find the first subscription group attribute; return if we have
4770 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4771 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
4776 send_ipp_status(con
, IPP_BAD_REQUEST
,
4777 _("No subscription attributes in request!"));
4782 * Process the subscription attributes in the request...
4785 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
4793 lease
= DefaultLeaseDuration
;
4795 mask
= CUPSD_EVENT_NONE
;
4797 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
4799 if (!strcmp(attr
->name
, "notify-recipient") &&
4800 attr
->value_tag
== IPP_TAG_URI
)
4801 recipient
= attr
->values
[0].string
.text
;
4802 else if (!strcmp(attr
->name
, "notify-pull-method") &&
4803 attr
->value_tag
== IPP_TAG_KEYWORD
)
4804 pullmethod
= attr
->values
[0].string
.text
;
4805 else if (!strcmp(attr
->name
, "notify-charset") &&
4806 attr
->value_tag
== IPP_TAG_CHARSET
&&
4807 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
4808 strcmp(attr
->values
[0].string
.text
, "utf-8"))
4810 send_ipp_status(con
, IPP_CHARSET
,
4811 _("Character set \"%s\" not supported!"),
4812 attr
->values
[0].string
.text
);
4815 else if (!strcmp(attr
->name
, "notify-natural-language") &&
4816 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
4817 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
4819 send_ipp_status(con
, IPP_CHARSET
,
4820 _("Language \"%s\" not supported!"),
4821 attr
->values
[0].string
.text
);
4824 else if (!strcmp(attr
->name
, "notify-user-data") &&
4825 attr
->value_tag
== IPP_TAG_STRING
)
4827 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
4829 send_ipp_status(con
, IPP_REQUEST_VALUE
,
4830 _("The notify-user-data value is too large "
4831 "(%d > 63 octets)!"),
4832 attr
->values
[0].unknown
.length
);
4838 else if (!strcmp(attr
->name
, "notify-events") &&
4839 attr
->value_tag
== IPP_TAG_KEYWORD
)
4841 for (i
= 0; i
< attr
->num_values
; i
++)
4842 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
4844 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
4845 attr
->value_tag
== IPP_TAG_INTEGER
)
4846 lease
= attr
->values
[0].integer
;
4847 else if (!strcmp(attr
->name
, "notify-time-interval") &&
4848 attr
->value_tag
== IPP_TAG_INTEGER
)
4849 interval
= attr
->values
[0].integer
;
4850 else if (!strcmp(attr
->name
, "notify-job-id") &&
4851 attr
->value_tag
== IPP_TAG_INTEGER
)
4852 jobid
= attr
->values
[0].integer
;
4858 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
4860 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
4861 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
4862 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
4864 if (!recipient
&& !pullmethod
)
4867 if (mask
== CUPSD_EVENT_NONE
)
4870 mask
= CUPSD_EVENT_JOB_COMPLETED
;
4872 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4875 send_ipp_status(con
, IPP_BAD_REQUEST
,
4876 _("notify-events not specified!"));
4881 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
4883 cupsdLogMessage(CUPSD_LOG_INFO
,
4884 "create_subscription: Limiting notify-lease-duration to "
4887 lease
= MaxLeaseDuration
;
4892 if ((job
= cupsdFindJob(jobid
)) == NULL
)
4894 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
4901 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
4904 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
4907 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4908 "Added subscription %d for printer \"%s\"",
4909 sub
->id
, printer
->name
);
4911 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
4914 sub
->interval
= interval
;
4916 sub
->expire
= lease
? time(NULL
) + lease
: 0;
4918 cupsdSetString(&sub
->owner
, username
);
4922 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
4923 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
4924 sub
->user_data_len
);
4927 ippAddSeparator(con
->response
);
4928 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4929 "notify-subscription-id", sub
->id
);
4931 con
->response
->request
.status
.status_code
= IPP_OK
;
4937 cupsdSaveAllSubscriptions();
4943 * 'delete_printer()' - Remove a printer or class from the system.
4947 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
4948 ipp_attribute_t
*uri
) /* I - URI of printer or class */
4950 http_status_t status
; /* Policy status */
4951 const char *dest
; /* Destination */
4952 cups_ptype_t dtype
; /* Destination type (printer or class) */
4953 char method
[HTTP_MAX_URI
], /* Method portion of URI */
4954 username
[HTTP_MAX_URI
], /* Username portion of URI */
4955 host
[HTTP_MAX_URI
], /* Host portion of URI */
4956 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
4957 int port
; /* Port portion of URI */
4958 cupsd_printer_t
*printer
; /* Printer/class */
4959 char filename
[1024]; /* Script/PPD filename */
4962 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
4963 con
->http
.fd
, uri
->values
[0].string
.text
);
4966 * Do we have a valid URI?
4969 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
4970 sizeof(method
), username
, sizeof(username
), host
,
4971 sizeof(host
), &port
, resource
, sizeof(resource
));
4973 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
4979 send_ipp_status(con
, IPP_NOT_FOUND
,
4980 _("The printer or class was not found."));
4988 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
4990 send_http_error(con
, status
);
4995 * Remove old jobs...
4998 cupsdCancelJobs(dest
, NULL
, 1);
5001 * Remove old subscriptions and send a "deleted printer" event...
5004 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5005 "%s \"%s\" deleted by \"%s\".",
5006 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5007 dest
, get_username(con
));
5009 cupsdExpireSubscriptions(printer
, NULL
);
5012 * Remove any old PPD or script files...
5015 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
, dest
);
5018 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
, dest
);
5021 if (dtype
& CUPS_PRINTER_CLASS
)
5023 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".", dest
,
5026 cupsdDeletePrinter(printer
, 0);
5027 cupsdSaveAllClasses();
5031 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".", dest
,
5034 cupsdDeletePrinter(printer
, 0);
5035 cupsdSaveAllPrinters();
5038 cupsdWritePrintcap();
5041 * Return with no errors...
5044 con
->response
->request
.status
.status_code
= IPP_OK
;
5049 * 'get_default()' - Get the default destination.
5053 get_default(cupsd_client_t
*con
) /* I - Client connection */
5055 http_status_t status
; /* Policy status */
5056 cups_array_t
*ra
; /* Requested attributes array */
5059 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5065 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5067 send_http_error(con
, status
);
5073 ra
= create_requested_array(con
->request
);
5075 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5077 cupsArrayDelete(ra
);
5079 con
->response
->request
.status
.status_code
= IPP_OK
;
5082 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5087 * 'get_devices()' - Get the list of available devices on the local system.
5091 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5093 http_status_t status
; /* Policy status */
5094 int i
; /* Looping var */
5095 ipp_attribute_t
*limit
, /* Limit attribute */
5096 *requested
; /* requested-attributes attribute */
5097 char command
[1024], /* cups-deviced command */
5098 options
[1024], /* Options to pass to command */
5099 attrs
[1024], /* String for requested attributes */
5100 *aptr
; /* Pointer into string */
5101 int alen
; /* Length of attribute value */
5104 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5110 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5112 send_http_error(con
, status
);
5117 * Run cups-deviced command with the given options...
5120 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5121 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5126 for (i
= 0, aptr
= attrs
; i
< requested
->num_values
; i
++)
5129 * Check that we have enough room...
5132 alen
= strlen(requested
->values
[i
].string
.text
);
5133 if (alen
> (sizeof(attrs
) - (aptr
- attrs
) - 2))
5137 * Put commas between values...
5144 * Add the value to the end of the string...
5147 strcpy(aptr
, requested
->values
[i
].string
.text
);
5152 * If we have more attribute names than will fit, default to "all"...
5155 if (i
< requested
->num_values
)
5156 strcpy(attrs
, "all");
5159 strcpy(attrs
, "all");
5161 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5162 snprintf(options
, sizeof(options
),
5163 "%d+%d+%d+requested-attributes=%s",
5164 con
->request
->request
.op
.request_id
,
5165 limit
? limit
->values
[0].integer
: 0, (int)User
,
5168 if (cupsdSendCommand(con
, command
, options
, 1))
5171 * Command started successfully, don't send an IPP response here...
5174 ippDelete(con
->response
);
5175 con
->response
= NULL
;
5180 * Command failed, return "internal error" so the user knows something
5184 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5185 _("cups-deviced failed to execute."));
5191 * 'get_job_attrs()' - Get job attributes.
5195 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5196 ipp_attribute_t
*uri
) /* I - Job URI */
5198 http_status_t status
; /* Policy status */
5199 ipp_attribute_t
*attr
; /* Current attribute */
5200 int jobid
; /* Job ID */
5201 cupsd_job_t
*job
; /* Current job */
5202 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5203 username
[HTTP_MAX_URI
], /* Username portion of URI */
5204 host
[HTTP_MAX_URI
], /* Host portion of URI */
5205 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5206 int port
; /* Port portion of URI */
5207 cups_array_t
*ra
; /* Requested attributes array */
5210 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5211 con
->http
.fd
, uri
->values
[0].string
.text
);
5214 * See if we have a job URI or a printer URI...
5217 if (!strcmp(uri
->name
, "printer-uri"))
5220 * Got a printer URI; see if we also have a job-id attribute...
5223 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5224 IPP_TAG_INTEGER
)) == NULL
)
5226 send_ipp_status(con
, IPP_BAD_REQUEST
,
5227 _("Got a printer-uri attribute but no job-id!"));
5231 jobid
= attr
->values
[0].integer
;
5236 * Got a job URI; parse it to get the job ID...
5239 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5240 sizeof(method
), username
, sizeof(username
), host
,
5241 sizeof(host
), &port
, resource
, sizeof(resource
));
5243 if (strncmp(resource
, "/jobs/", 6))
5249 send_ipp_status(con
, IPP_BAD_REQUEST
,
5250 _("Bad job-uri attribute \"%s\"!"),
5251 uri
->values
[0].string
.text
);
5255 jobid
= atoi(resource
+ 6);
5259 * See if the job exists...
5262 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5265 * Nope - return a "not found" error...
5268 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5276 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5278 send_http_error(con
, status
);
5283 * Copy attributes...
5288 ra
= create_requested_array(con
->request
);
5289 copy_job_attrs(con
, job
, ra
);
5290 cupsArrayDelete(ra
);
5292 con
->response
->request
.status
.status_code
= IPP_OK
;
5297 * 'get_jobs()' - Get a list of jobs for the specified printer.
5301 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
5302 ipp_attribute_t
*uri
) /* I - Printer URI */
5304 http_status_t status
; /* Policy status */
5305 ipp_attribute_t
*attr
; /* Current attribute */
5306 const char *dest
; /* Destination */
5307 cups_ptype_t dtype
; /* Destination type (printer or class) */
5308 cups_ptype_t dmask
; /* Destination type mask */
5309 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5310 username
[HTTP_MAX_URI
], /* Username portion of URI */
5311 host
[HTTP_MAX_URI
], /* Host portion of URI */
5312 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5313 int port
; /* Port portion of URI */
5314 int completed
; /* Completed jobs? */
5315 int first_job_id
; /* First job ID */
5316 int limit
; /* Maximum number of jobs to return */
5317 int count
; /* Number of jobs that match */
5318 cupsd_job_t
*job
; /* Current job pointer */
5319 cupsd_printer_t
*printer
; /* Printer */
5320 cups_array_t
*list
; /* Which job list... */
5321 cups_array_t
*ra
; /* Requested attributes array */
5324 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
5325 uri
->values
[0].string
.text
);
5328 * Is the destination valid?
5331 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5332 sizeof(method
), username
, sizeof(username
), host
,
5333 sizeof(host
), &port
, resource
, sizeof(resource
));
5335 if (!strcmp(resource
, "/") ||
5336 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
5339 dtype
= (cups_ptype_t
)0;
5340 dmask
= (cups_ptype_t
)0;
5343 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5346 dtype
= (cups_ptype_t
)0;
5347 dmask
= CUPS_PRINTER_CLASS
;
5350 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5353 dtype
= CUPS_PRINTER_CLASS
;
5354 dmask
= CUPS_PRINTER_CLASS
;
5357 else if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
5363 send_ipp_status(con
, IPP_NOT_FOUND
,
5364 _("The printer or class was not found."));
5368 dmask
= CUPS_PRINTER_CLASS
;
5376 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
5378 send_http_error(con
, status
);
5382 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5384 send_http_error(con
, status
);
5389 * See if the "which-jobs" attribute have been specified...
5392 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
5393 IPP_TAG_KEYWORD
)) != NULL
&&
5394 !strcmp(attr
->values
[0].string
.text
, "completed"))
5399 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
5411 * See if they want to limit the number of jobs reported...
5414 if ((attr
= ippFindAttribute(con
->request
, "limit",
5415 IPP_TAG_INTEGER
)) != NULL
)
5416 limit
= attr
->values
[0].integer
;
5420 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
5421 IPP_TAG_INTEGER
)) != NULL
)
5422 first_job_id
= attr
->values
[0].integer
;
5427 * See if we only want to see jobs for a specific user...
5430 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
5431 IPP_TAG_BOOLEAN
)) != NULL
&&
5432 attr
->values
[0].boolean
)
5433 strlcpy(username
, get_username(con
), sizeof(username
));
5437 ra
= create_requested_array(con
->request
);
5440 * OK, build a list of jobs for this printer...
5443 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
5444 count
< limit
&& job
;
5445 job
= (cupsd_job_t
*)cupsArrayNext(list
))
5448 * Filter out jobs that don't match...
5451 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
5453 if ((dest
&& strcmp(job
->dest
, dest
)) &&
5454 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
5456 if ((job
->dtype
& dmask
) != dtype
&&
5457 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
5459 if (username
[0] && strcasecmp(username
, job
->username
))
5462 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
5465 if (job
->id
< first_job_id
)
5474 ippAddSeparator(con
->response
);
5478 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
5480 copy_job_attrs(con
, job
, ra
);
5483 cupsArrayDelete(ra
);
5485 con
->response
->request
.status
.status_code
= IPP_OK
;
5490 * 'get_notifications()' - Get events for a subscription.
5494 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
5496 int i
, j
; /* Looping vars */
5497 http_status_t status
; /* Policy status */
5498 cupsd_subscription_t
*sub
; /* Subscription */
5499 ipp_attribute_t
*ids
, /* notify-subscription-ids */
5500 *sequences
; /* notify-sequence-numbers */
5501 int min_seq
; /* Minimum sequence number */
5502 int interval
; /* Poll interval */
5505 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_subscription_attrs(con=%p[%d])",
5509 * Get subscription attributes...
5512 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
5514 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
5519 send_ipp_status(con
, IPP_BAD_REQUEST
,
5520 _("Missing notify-subscription-ids attribute!"));
5525 * Are the subscription IDs valid?
5528 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
5530 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
5533 * Bad subscription ID...
5536 send_ipp_status(con
, IPP_NOT_FOUND
,
5537 _("notify-subscription-id %d no good!"),
5538 ids
->values
[i
].integer
);
5546 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5548 con
, sub
->owner
)) != HTTP_OK
)
5550 send_http_error(con
, status
);
5555 * Check the subscription type and update the interval accordingly.
5558 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
5561 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
5563 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
5569 * Tell the client to poll again in N seconds...
5573 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5574 "notify-get-interval", interval
);
5576 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5577 "printer-up-time", time(NULL
));
5580 * Copy the subscription event attributes to the response.
5583 con
->response
->request
.status
.status_code
=
5584 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
5586 for (i
= 0; i
< ids
->num_values
; i
++)
5589 * Get the subscription and sequence number...
5592 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
5594 if (sequences
&& i
< sequences
->num_values
)
5595 min_seq
= sequences
->values
[i
].integer
;
5600 * If we don't have any new events, nothing to do here...
5603 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
5607 * Otherwise copy all of the new events...
5610 if (sub
->first_event_id
> min_seq
)
5613 j
= min_seq
- sub
->first_event_id
;
5615 for (; j
< sub
->num_events
; j
++)
5617 ippAddSeparator(con
->response
);
5619 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
5620 IPP_TAG_EVENT_NOTIFICATION
, 0);
5627 * 'get_ppds()' - Get the list of PPD files on the local system.
5631 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
5633 http_status_t status
; /* Policy status */
5634 int i
; /* Looping var */
5635 ipp_attribute_t
*limit
, /* Limit attribute */
5636 *make
, /* ppd-make attribute */
5637 *requested
; /* requested-attributes attribute */
5638 char command
[1024], /* cups-deviced command */
5639 options
[1024], /* Options to pass to command */
5640 attrs
[1024], /* String for requested attributes */
5641 *aptr
; /* Pointer into string */
5642 int alen
; /* Length of attribute value */
5645 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
5651 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5653 send_http_error(con
, status
);
5658 * Run cups-driverd command with the given options...
5661 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5662 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
5663 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5668 for (i
= 0, aptr
= attrs
; i
< requested
->num_values
; i
++)
5671 * Check that we have enough room...
5674 alen
= strlen(requested
->values
[i
].string
.text
);
5675 if (alen
> (sizeof(attrs
) - (aptr
- attrs
) - 2))
5679 * Put commas between values...
5686 * Add the value to the end of the string...
5689 strcpy(aptr
, requested
->values
[i
].string
.text
);
5694 * If we have more attribute names than will fit, default to "all"...
5697 if (i
< requested
->num_values
)
5698 strcpy(attrs
, "all");
5701 strcpy(attrs
, "all");
5703 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
5704 snprintf(options
, sizeof(options
),
5705 "list+%d+%d+requested-attributes=%s%s%s",
5706 con
->request
->request
.op
.request_id
,
5707 limit
? limit
->values
[0].integer
: 0,
5709 make
? "%20ppd-make=" : "",
5710 make
? make
->values
[0].string
.text
: "");
5712 if (cupsdSendCommand(con
, command
, options
, 0))
5715 * Command started successfully, don't send an IPP response here...
5718 ippDelete(con
->response
);
5719 con
->response
= NULL
;
5724 * Command failed, return "internal error" so the user knows something
5728 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5729 _("cups-driverd failed to execute."));
5735 * 'get_printer_attrs()' - Get printer attributes.
5739 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
5740 ipp_attribute_t
*uri
) /* I - Printer URI */
5742 http_status_t status
; /* Policy status */
5743 const char *dest
; /* Destination */
5744 cups_ptype_t dtype
; /* Destination type (printer or class) */
5745 char method
[HTTP_MAX_URI
],
5746 /* Method portion of URI */
5747 username
[HTTP_MAX_URI
],
5748 /* Username portion of URI */
5750 /* Host portion of URI */
5751 resource
[HTTP_MAX_URI
];
5752 /* Resource portion of URI */
5753 int port
; /* Port portion of URI */
5754 cupsd_printer_t
*printer
; /* Printer/class */
5755 cups_array_t
*ra
; /* Requested attributes array */
5758 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
5759 con
->http
.fd
, uri
->values
[0].string
.text
);
5762 * Is the destination valid?
5765 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5766 sizeof(method
), username
, sizeof(username
), host
,
5767 sizeof(host
), &port
, resource
, sizeof(resource
));
5769 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
5775 send_ipp_status(con
, IPP_NOT_FOUND
,
5776 _("The printer or class was not found."));
5784 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
5786 send_http_error(con
, status
);
5791 * Send the attributes...
5794 ra
= create_requested_array(con
->request
);
5796 copy_printer_attrs(con
, printer
, ra
);
5798 cupsArrayDelete(ra
);
5800 con
->response
->request
.status
.status_code
= IPP_OK
;
5805 * 'get_printers()' - Get a list of printers or classes.
5809 get_printers(cupsd_client_t
*con
, /* I - Client connection */
5810 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
5812 http_status_t status
; /* Policy status */
5813 ipp_attribute_t
*attr
; /* Current attribute */
5814 int limit
; /* Maximum number of printers to return */
5815 int count
; /* Number of printers that match */
5816 cupsd_printer_t
*printer
; /* Current printer pointer */
5817 int printer_type
, /* printer-type attribute */
5818 printer_mask
; /* printer-type-mask attribute */
5819 char *location
; /* Location string */
5820 const char *username
; /* Current user */
5821 char *first_printer_name
; /* first-printer-name attribute */
5822 cups_array_t
*ra
; /* Requested attributes array */
5825 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
5826 con
->http
.fd
, type
);
5832 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5834 send_http_error(con
, status
);
5839 * Check for printers...
5842 if (!Printers
|| !cupsArrayCount(Printers
))
5844 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
5849 * See if they want to limit the number of printers reported...
5852 if ((attr
= ippFindAttribute(con
->request
, "limit",
5853 IPP_TAG_INTEGER
)) != NULL
)
5854 limit
= attr
->values
[0].integer
;
5858 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
5859 IPP_TAG_NAME
)) != NULL
)
5860 first_printer_name
= attr
->values
[0].string
.text
;
5862 first_printer_name
= NULL
;
5865 * Support filtering...
5868 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
5869 IPP_TAG_ENUM
)) != NULL
)
5870 printer_type
= attr
->values
[0].integer
;
5874 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
5875 IPP_TAG_ENUM
)) != NULL
)
5876 printer_mask
= attr
->values
[0].integer
;
5880 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
5881 IPP_TAG_TEXT
)) != NULL
)
5882 location
= attr
->values
[0].string
.text
;
5886 if (con
->username
[0])
5887 username
= con
->username
;
5888 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
5889 IPP_TAG_NAME
)) != NULL
)
5890 username
= attr
->values
[0].string
.text
;
5894 ra
= create_requested_array(con
->request
);
5897 * OK, build a list of printers for this printer...
5900 if (first_printer_name
)
5902 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
5903 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5906 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5909 count
< limit
&& printer
;
5910 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
5912 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
5913 (printer
->type
& printer_mask
) == printer_type
&&
5914 (!location
|| !printer
->location
||
5915 !strcasecmp(printer
->location
, location
)))
5918 * If HideImplicitMembers is enabled, see if this printer or class
5919 * is a member of an implicit class...
5922 if (ImplicitClasses
&& HideImplicitMembers
&&
5923 printer
->in_implicit_class
)
5927 * If a username is specified, see if it is allowed or denied
5931 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
5935 * Add the group separator as needed...
5939 ippAddSeparator(con
->response
);
5944 * Send the attributes...
5947 copy_printer_attrs(con
, printer
, ra
);
5951 cupsArrayDelete(ra
);
5953 con
->response
->request
.status
.status_code
= IPP_OK
;
5958 * 'get_subscription_attrs()' - Get subscription attributes.
5962 get_subscription_attrs(
5963 cupsd_client_t
*con
, /* I - Client connection */
5964 int sub_id
) /* I - Subscription ID */
5966 http_status_t status
; /* Policy status */
5967 cupsd_subscription_t
*sub
; /* Subscription */
5968 cups_array_t
*ra
; /* Requested attributes array */
5971 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5972 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
5973 con
, con
->http
.fd
, sub_id
);
5976 * Is the subscription ID valid?
5979 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
5982 * Bad subscription ID...
5985 send_ipp_status(con
, IPP_NOT_FOUND
,
5986 _("notify-subscription-id %d no good!"), sub_id
);
5994 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5996 con
, sub
->owner
)) != HTTP_OK
)
5998 send_http_error(con
, status
);
6003 * Copy the subscription attributes to the response using the
6004 * requested-attributes attribute that may be provided by the client.
6007 ra
= create_requested_array(con
->request
);
6009 copy_subscription_attrs(con
, sub
, ra
);
6011 cupsArrayDelete(ra
);
6013 con
->response
->request
.status
.status_code
= IPP_OK
;
6018 * 'get_subscriptions()' - Get subscriptions.
6022 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
6023 ipp_attribute_t
*uri
) /* I - Printer/job URI */
6025 http_status_t status
; /* Policy status */
6026 int count
; /* Number of subscriptions */
6027 int limit
; /* Limit */
6028 cupsd_subscription_t
*sub
; /* Subscription */
6029 cups_array_t
*ra
; /* Requested attributes array */
6030 ipp_attribute_t
*attr
; /* Attribute */
6031 cups_ptype_t dtype
; /* Destination type (printer or class) */
6032 char method
[HTTP_MAX_URI
],
6033 /* Method portion of URI */
6034 username
[HTTP_MAX_URI
],
6035 /* Username portion of URI */
6037 /* Host portion of URI */
6038 resource
[HTTP_MAX_URI
];
6039 /* Resource portion of URI */
6040 int port
; /* Port portion of URI */
6041 cupsd_job_t
*job
; /* Job pointer */
6042 cupsd_printer_t
*printer
; /* Printer */
6045 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6046 "get_subscriptions(con=%p[%d], uri=%s)",
6047 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
6050 * Is the destination valid?
6053 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6054 sizeof(method
), username
, sizeof(username
), host
,
6055 sizeof(host
), &port
, resource
, sizeof(resource
));
6057 if (!strcmp(resource
, "/") ||
6058 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
6059 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
6060 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
6065 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
6068 job
= cupsdFindJob(atoi(resource
+ 6));
6072 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
6077 else if (!cupsdValidateDest(host
, resource
, &dtype
, &printer
))
6083 send_ipp_status(con
, IPP_NOT_FOUND
,
6084 _("The printer or class was not found."));
6087 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
6088 IPP_TAG_INTEGER
)) != NULL
)
6090 job
= cupsdFindJob(attr
->values
[0].integer
);
6094 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
6095 attr
->values
[0].integer
);
6106 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
6108 con
, NULL
)) != HTTP_OK
)
6110 send_http_error(con
, status
);
6115 * Copy the subscription attributes to the response using the
6116 * requested-attributes attribute that may be provided by the client.
6119 ra
= create_requested_array(con
->request
);
6121 if ((attr
= ippFindAttribute(con
->request
, "limit",
6122 IPP_TAG_INTEGER
)) != NULL
)
6123 limit
= attr
->values
[0].integer
;
6128 * See if we only want to see subscriptions for a specific user...
6131 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
6132 IPP_TAG_BOOLEAN
)) != NULL
&&
6133 attr
->values
[0].boolean
)
6134 strlcpy(username
, get_username(con
), sizeof(username
));
6138 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
6140 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
6141 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
6142 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
6144 ippAddSeparator(con
->response
);
6145 copy_subscription_attrs(con
, sub
, ra
);
6148 if (limit
&& count
>= limit
)
6152 cupsArrayDelete(ra
);
6155 con
->response
->request
.status
.status_code
= IPP_OK
;
6157 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
6162 * 'get_username()' - Get the username associated with a request.
6165 static const char * /* O - Username */
6166 get_username(cupsd_client_t
*con
) /* I - Connection */
6168 ipp_attribute_t
*attr
; /* Attribute */
6171 if (con
->username
[0])
6172 return (con
->username
);
6173 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6174 IPP_TAG_NAME
)) != NULL
)
6175 return (attr
->values
[0].string
.text
);
6177 return ("anonymous");
6182 * 'hold_job()' - Hold a print job.
6186 hold_job(cupsd_client_t
*con
, /* I - Client connection */
6187 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
6189 ipp_attribute_t
*attr
, /* Current job-hold-until */
6190 *newattr
; /* New job-hold-until */
6191 int jobid
; /* Job ID */
6192 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6193 username
[HTTP_MAX_URI
], /* Username portion of URI */
6194 host
[HTTP_MAX_URI
], /* Host portion of URI */
6195 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6196 int port
; /* Port portion of URI */
6197 cupsd_job_t
*job
; /* Job information */
6200 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
6201 uri
->values
[0].string
.text
);
6204 * See if we have a job URI or a printer URI...
6207 if (!strcmp(uri
->name
, "printer-uri"))
6210 * Got a printer URI; see if we also have a job-id attribute...
6213 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6214 IPP_TAG_INTEGER
)) == NULL
)
6216 send_ipp_status(con
, IPP_BAD_REQUEST
,
6217 _("Got a printer-uri attribute but no job-id!"));
6221 jobid
= attr
->values
[0].integer
;
6226 * Got a job URI; parse it to get the job ID...
6229 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6230 sizeof(method
), username
, sizeof(username
), host
,
6231 sizeof(host
), &port
, resource
, sizeof(resource
));
6233 if (strncmp(resource
, "/jobs/", 6))
6239 send_ipp_status(con
, IPP_BAD_REQUEST
,
6240 _("Bad job-uri attribute \"%s\"!"),
6241 uri
->values
[0].string
.text
);
6245 jobid
= atoi(resource
+ 6);
6249 * See if the job exists...
6252 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6255 * Nope - return a "not found" error...
6258 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6263 * See if the job is owned by the requesting user...
6266 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6268 send_http_error(con
, HTTP_UNAUTHORIZED
);
6273 * Hold the job and return...
6278 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
6279 IPP_TAG_KEYWORD
)) == NULL
)
6280 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
6282 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
6283 IPP_TAG_KEYWORD
)) == NULL
)
6284 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
6289 * Free the old hold value and copy the new one over...
6292 _cupsStrFree(attr
->values
[0].string
.text
);
6296 attr
->value_tag
= newattr
->value_tag
;
6297 attr
->values
[0].string
.text
=
6298 _cupsStrAlloc(newattr
->values
[0].string
.text
);
6302 attr
->value_tag
= IPP_TAG_KEYWORD
;
6303 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
6307 * Hold job until specified time...
6310 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
6313 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was held by \"%s\".", jobid
,
6316 con
->response
->request
.status
.status_code
= IPP_OK
;
6321 * 'move_job()' - Move a job to a new destination.
6325 move_job(cupsd_client_t
*con
, /* I - Client connection */
6326 ipp_attribute_t
*uri
) /* I - Job URI */
6328 http_status_t status
; /* Policy status */
6329 ipp_attribute_t
*attr
; /* Current attribute */
6330 int jobid
; /* Job ID */
6331 cupsd_job_t
*job
; /* Current job */
6332 const char *src
, /* Source printer/class */
6333 *dest
; /* Destination */
6334 cups_ptype_t stype
, /* Source type (printer or class) */
6335 dtype
; /* Destination type (printer or class) */
6336 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6337 username
[HTTP_MAX_URI
], /* Username portion of URI */
6338 host
[HTTP_MAX_URI
], /* Host portion of URI */
6339 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6340 int port
; /* Port portion of URI */
6341 cupsd_printer_t
*sprinter
, /* Source printer */
6342 *dprinter
; /* Destination printer */
6345 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
6346 uri
->values
[0].string
.text
);
6349 * Get the new printer or class...
6352 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
6353 IPP_TAG_URI
)) == NULL
)
6356 * Need job-printer-uri...
6359 send_ipp_status(con
, IPP_BAD_REQUEST
,
6360 _("job-printer-uri attribute missing!"));
6364 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, method
,
6365 sizeof(method
), username
, sizeof(username
), host
,
6366 sizeof(host
), &port
, resource
, sizeof(resource
));
6368 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &dprinter
)) == NULL
)
6374 send_ipp_status(con
, IPP_NOT_FOUND
,
6375 _("The printer or class was not found."));
6383 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6385 send_http_error(con
, status
);
6390 * See if we have a job URI or a printer URI...
6393 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6394 sizeof(method
), username
, sizeof(username
), host
,
6395 sizeof(host
), &port
, resource
, sizeof(resource
));
6397 if (!strcmp(uri
->name
, "printer-uri"))
6400 * Got a printer URI; see if we also have a job-id attribute...
6403 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6404 IPP_TAG_INTEGER
)) == NULL
)
6410 if ((src
= cupsdValidateDest(host
, resource
, &stype
, &sprinter
)) == NULL
)
6416 send_ipp_status(con
, IPP_NOT_FOUND
,
6417 _("The printer or class was not found."));
6426 * Otherwise, just move a single job...
6429 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
6432 * Nope - return a "not found" error...
6435 send_ipp_status(con
, IPP_NOT_FOUND
,
6436 _("Job #%d does not exist!"), attr
->values
[0].integer
);
6442 * Job found, initialize source pointers...
6453 * Got a job URI; parse it to get the job ID...
6456 if (strncmp(resource
, "/jobs/", 6))
6462 send_ipp_status(con
, IPP_BAD_REQUEST
,
6463 _("Bad job-uri attribute \"%s\"!"),
6464 uri
->values
[0].string
.text
);
6469 * See if the job exists...
6472 jobid
= atoi(resource
+ 6);
6474 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6477 * Nope - return a "not found" error...
6480 send_ipp_status(con
, IPP_NOT_FOUND
,
6481 _("Job #%d does not exist!"), jobid
);
6487 * Job found, initialize source pointers...
6496 * Now move the job or jobs...
6502 * See if the job has been completed...
6505 if (job
->state_value
> IPP_JOB_STOPPED
)
6508 * Return a "not-possible" error...
6511 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6512 _("Job #%d is finished and cannot be altered!"),
6518 * See if the job is owned by the requesting user...
6521 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6523 send_http_error(con
, HTTP_UNAUTHORIZED
);
6528 * Move the job to a different printer or class...
6531 cupsdMoveJob(job
, dprinter
);
6536 * Got the source printer, now look through the jobs...
6539 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
6541 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
6544 * See if the job is pointing at the source printer or has not been
6548 if (strcasecmp(job
->dest
, src
) ||
6549 job
->state_value
> IPP_JOB_STOPPED
)
6553 * See if the job can be moved by the requesting user...
6556 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6560 * Move the job to a different printer or class...
6563 cupsdMoveJob(job
, dprinter
);
6568 * Start jobs if possible...
6574 * Return with "everything is OK" status...
6577 con
->response
->request
.status
.status_code
= IPP_OK
;
6582 * 'ppd_add_default()' - Add a PPD default choice.
6585 static int /* O - Number of defaults */
6587 const char *option
, /* I - Option name */
6588 const char *choice
, /* I - Choice name */
6589 int num_defaults
, /* I - Number of defaults */
6590 ppd_default_t
**defaults
) /* IO - Defaults */
6592 int i
; /* Looping var */
6593 ppd_default_t
*temp
; /* Temporary defaults array */
6597 * First check if the option already has a default value; the PPD spec
6598 * says that the first one is used...
6601 for (i
= 0, temp
= *defaults
; i
< num_defaults
; i
++)
6602 if (!strcmp(option
, temp
[i
].option
))
6603 return (num_defaults
);
6606 * Now add the option...
6609 if (num_defaults
== 0)
6610 temp
= malloc(sizeof(ppd_default_t
));
6612 temp
= realloc(*defaults
, (num_defaults
+ 1) * sizeof(ppd_default_t
));
6616 cupsdLogMessage(CUPSD_LOG_ERROR
, "ppd_add_default: Unable to add default value for \"%s\" - %s",
6617 option
, strerror(errno
));
6618 return (num_defaults
);
6622 temp
+= num_defaults
;
6624 strlcpy(temp
->option
, option
, sizeof(temp
->option
));
6625 strlcpy(temp
->choice
, choice
, sizeof(temp
->choice
));
6627 return (num_defaults
+ 1);
6632 * 'ppd_parse_line()' - Parse a PPD default line.
6635 static int /* O - 0 on success, -1 on failure */
6636 ppd_parse_line(const char *line
, /* I - Line */
6637 char *option
, /* O - Option name */
6638 int olen
, /* I - Size of option name */
6639 char *choice
, /* O - Choice name */
6640 int clen
) /* I - Size of choice name */
6643 * Verify this is a default option line...
6646 if (strncmp(line
, "*Default", 8))
6650 * Read the option name...
6653 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
6663 * Skip everything else up to the colon (:)...
6666 while (*line
&& *line
!= ':')
6675 * Now grab the option choice, skipping leading whitespace...
6678 while (isspace(*line
& 255))
6681 for (clen
--; isalnum(*line
& 255); line
++)
6691 * Return with no errors...
6699 * 'print_job()' - Print a file to a printer or class.
6703 print_job(cupsd_client_t
*con
, /* I - Client connection */
6704 ipp_attribute_t
*uri
) /* I - Printer URI */
6706 ipp_attribute_t
*attr
; /* Current attribute */
6707 ipp_attribute_t
*format
; /* Document-format attribute */
6708 cupsd_job_t
*job
; /* New job */
6709 char filename
[1024]; /* Job filename */
6710 mime_type_t
*filetype
; /* Type of file */
6711 char super
[MIME_MAX_SUPER
], /* Supertype of file */
6712 type
[MIME_MAX_TYPE
], /* Subtype of file */
6713 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
6714 /* Textual name of mime type */
6715 cupsd_printer_t
*printer
; /* Printer data */
6716 struct stat fileinfo
; /* File information */
6717 int kbytes
; /* Size of file */
6718 int compression
; /* Document compression */
6721 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
6722 uri
->values
[0].string
.text
);
6725 * Validate print file attributes, for now just document-format and
6726 * compression (CUPS only supports "none" and "gzip")...
6729 compression
= CUPS_FILE_NONE
;
6731 if ((attr
= ippFindAttribute(con
->request
, "compression",
6732 IPP_TAG_KEYWORD
)) != NULL
)
6734 if (strcmp(attr
->values
[0].string
.text
, "none")
6736 && strcmp(attr
->values
[0].string
.text
, "gzip")
6737 #endif /* HAVE_LIBZ */
6740 send_ipp_status(con
, IPP_ATTRIBUTES
,
6741 _("Unsupported compression \"%s\"!"),
6742 attr
->values
[0].string
.text
);
6743 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6744 "compression", NULL
, attr
->values
[0].string
.text
);
6749 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
6750 compression
= CUPS_FILE_GZIP
;
6751 #endif /* HAVE_LIBZ */
6755 * Do we have a file to print?
6760 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
6765 * Is it a format we support?
6768 if ((format
= ippFindAttribute(con
->request
, "document-format",
6769 IPP_TAG_MIMETYPE
)) != NULL
)
6772 * Grab format from client...
6775 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
6777 send_ipp_status(con
, IPP_BAD_REQUEST
,
6778 _("Could not scan type \"%s\"!"),
6779 format
->values
[0].string
.text
);
6786 * No document format attribute? Auto-type it!
6789 strcpy(super
, "application");
6790 strcpy(type
, "octet-stream");
6793 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
6796 * Auto-type the file...
6799 ipp_attribute_t
*doc_name
; /* document-name attribute */
6802 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
6804 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
6805 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
6806 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
6812 * Replace the document-format attribute value with the auto-typed one.
6815 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
6820 _cupsStrFree(format
->values
[0].string
.text
);
6822 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
6825 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
6826 "document-format", NULL
, mimetype
);
6829 filetype
= mimeType(MimeDatabase
, super
, type
);
6832 filetype
= mimeType(MimeDatabase
, super
, type
);
6836 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
6837 _("Unsupported format \'%s/%s\'!"), super
, type
);
6838 cupsdLogMessage(CUPSD_LOG_INFO
,
6839 "Hint: Do you have the raw file printing rules enabled?");
6842 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
6843 "document-format", NULL
, format
->values
[0].string
.text
);
6848 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: request file type is %s/%s.",
6849 filetype
->super
, filetype
->type
);
6852 * Read any embedded job ticket info from PS files...
6855 if (!strcasecmp(filetype
->super
, "application") &&
6856 !strcasecmp(filetype
->type
, "postscript"))
6857 read_ps_job_ticket(con
);
6860 * Create the job object...
6863 if ((job
= add_job(con
, uri
, &printer
, filetype
)) == NULL
)
6867 * Update quota data...
6870 if (stat(con
->filename
, &fileinfo
))
6873 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
6875 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6877 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
6878 IPP_TAG_INTEGER
)) != NULL
)
6879 attr
->values
[0].integer
+= kbytes
;
6882 * Add the job file...
6885 if (add_file(con
, job
, filetype
, compression
))
6888 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
6890 rename(con
->filename
, filename
);
6891 cupsdClearString(&con
->filename
);
6894 * See if we need to add the ending sheet...
6897 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
6898 attr
->num_values
> 1)
6904 cupsdLogMessage(CUPSD_LOG_INFO
, "Adding end banner page \"%s\" to job %d.",
6905 attr
->values
[1].string
.text
, job
->id
);
6907 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
6909 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6913 * Log and save the job...
6916 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d queued on \"%s\" by \"%s\".", job
->id
,
6917 job
->dest
, job
->username
);
6918 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Job %d hold_until = %d", job
->id
,
6919 (int)job
->hold_until
);
6924 * Start the job if possible...
6932 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
6934 * This function only gets called when printing a single PostScript
6935 * file using the Print-Job operation. It doesn't work for Create-Job +
6936 * Send-File, since the job attributes need to be set at job creation
6937 * time for banners to work. The embedded PS job ticket stuff is here
6938 * only to allow the Windows printer driver for CUPS to pass in JCL
6939 * options and IPP attributes which otherwise would be lost.
6941 * The format of a PS job ticket is simple:
6943 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
6945 * %cupsJobTicket: attr1=value1
6946 * %cupsJobTicket: attr2=value2
6948 * %cupsJobTicket: attrN=valueN
6950 * Job ticket lines must appear immediately after the first line that
6951 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
6952 * looking for job ticket info when it finds a line that does not begin
6953 * with "%cupsJobTicket:".
6955 * The maximum length of a job ticket line, including the prefix, is
6956 * 255 characters to conform with the Adobe DSC.
6958 * Read-only attributes are rejected with a notice to the error log in
6959 * case a malicious user tries anything. Since the job ticket is read
6960 * prior to attribute validation in print_job(), job ticket attributes
6961 * will go through the same validation as IPP attributes...
6965 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
6967 cups_file_t
*fp
; /* File to read from */
6968 char line
[256]; /* Line data */
6969 int num_options
; /* Number of options */
6970 cups_option_t
*options
; /* Options */
6971 ipp_t
*ticket
; /* New attributes */
6972 ipp_attribute_t
*attr
, /* Current attribute */
6973 *attr2
, /* Job attribute */
6974 *prev2
; /* Previous job attribute */
6978 * First open the print file...
6981 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
6983 cupsdLogMessage(CUPSD_LOG_ERROR
,
6984 "read_ps_job_ticket: Unable to open PostScript print file "
6991 * Skip the first line...
6994 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
6996 cupsdLogMessage(CUPSD_LOG_ERROR
,
6997 "read_ps_job_ticket: Unable to read from PostScript print "
7004 if (strncmp(line
, "%!PS-Adobe-", 11))
7007 * Not a DSC-compliant file, so no job ticket info will be available...
7015 * Read job ticket info from the file...
7021 while (cupsFileGets(fp
, line
, sizeof(line
)))
7024 * Stop at the first non-ticket line...
7027 if (strncmp(line
, "%cupsJobTicket:", 15))
7031 * Add the options to the option array...
7034 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
7038 * Done with the file; see if we have any options...
7043 if (num_options
== 0)
7047 * OK, convert the options to an attribute list, and apply them to
7052 cupsEncodeOptions(ticket
, num_options
, options
);
7055 * See what the user wants to change.
7058 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
7060 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
7063 if (!strcmp(attr
->name
, "job-originating-host-name") ||
7064 !strcmp(attr
->name
, "job-originating-user-name") ||
7065 !strcmp(attr
->name
, "job-media-sheets-completed") ||
7066 !strcmp(attr
->name
, "job-k-octets") ||
7067 !strcmp(attr
->name
, "job-id") ||
7068 !strncmp(attr
->name
, "job-state", 9) ||
7069 !strncmp(attr
->name
, "time-at-", 8))
7070 continue; /* Read-only attrs */
7072 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
7073 IPP_TAG_ZERO
)) != NULL
)
7076 * Some other value; first free the old value...
7079 if (con
->request
->attrs
== attr2
)
7081 con
->request
->attrs
= attr2
->next
;
7086 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
7087 if (prev2
->next
== attr2
)
7089 prev2
->next
= attr2
->next
;
7094 if (con
->request
->last
== attr2
)
7095 con
->request
->last
= prev2
;
7097 _ippFreeAttr(attr2
);
7101 * Add new option by copying it...
7104 copy_attribute(con
->request
, attr
, 0);
7108 * Then free the attribute list and option array...
7112 cupsFreeOptions(num_options
, options
);
7117 * 'reject_jobs()' - Reject print jobs to a printer.
7121 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
7122 ipp_attribute_t
*uri
) /* I - Printer or class URI */
7124 http_status_t status
; /* Policy status */
7125 cups_ptype_t dtype
; /* Destination type (printer or class) */
7126 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7127 username
[HTTP_MAX_URI
], /* Username portion of URI */
7128 host
[HTTP_MAX_URI
], /* Host portion of URI */
7129 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7130 int port
; /* Port portion of URI */
7131 const char *name
; /* Printer name */
7132 cupsd_printer_t
*printer
; /* Printer data */
7133 ipp_attribute_t
*attr
; /* printer-state-message text */
7136 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
7137 con
->http
.fd
, uri
->values
[0].string
.text
);
7140 * Is the destination valid?
7143 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7144 sizeof(method
), username
, sizeof(username
), host
,
7145 sizeof(host
), &port
, resource
, sizeof(resource
));
7147 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
7153 send_ipp_status(con
, IPP_NOT_FOUND
,
7154 _("The printer or class was not found."));
7162 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7164 send_http_error(con
, status
);
7169 * Reject jobs sent to the printer...
7172 printer
->accepting
= 0;
7174 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
7175 IPP_TAG_TEXT
)) == NULL
)
7176 strcpy(printer
->state_message
, "Rejecting Jobs");
7178 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
7179 sizeof(printer
->state_message
));
7181 cupsdAddPrinterHistory(printer
);
7183 if (dtype
& CUPS_PRINTER_CLASS
)
7185 cupsdSaveAllClasses();
7187 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
7188 name
, get_username(con
));
7192 cupsdSaveAllPrinters();
7194 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
7195 name
, get_username(con
));
7199 * Everything was ok, so return OK status...
7202 con
->response
->request
.status
.status_code
= IPP_OK
;
7207 * 'release_job()' - Release a held print job.
7211 release_job(cupsd_client_t
*con
, /* I - Client connection */
7212 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7214 ipp_attribute_t
*attr
; /* Current attribute */
7215 int jobid
; /* Job ID */
7216 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7217 username
[HTTP_MAX_URI
], /* Username portion of URI */
7218 host
[HTTP_MAX_URI
], /* Host portion of URI */
7219 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7220 int port
; /* Port portion of URI */
7221 cupsd_job_t
*job
; /* Job information */
7224 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
7225 con
->http
.fd
, uri
->values
[0].string
.text
);
7228 * See if we have a job URI or a printer URI...
7231 if (!strcmp(uri
->name
, "printer-uri"))
7234 * Got a printer URI; see if we also have a job-id attribute...
7237 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7238 IPP_TAG_INTEGER
)) == NULL
)
7240 send_ipp_status(con
, IPP_BAD_REQUEST
,
7241 _("Got a printer-uri attribute but no job-id!"));
7245 jobid
= attr
->values
[0].integer
;
7250 * Got a job URI; parse it to get the job ID...
7253 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7254 sizeof(method
), username
, sizeof(username
), host
,
7255 sizeof(host
), &port
, resource
, sizeof(resource
));
7257 if (strncmp(resource
, "/jobs/", 6))
7263 send_ipp_status(con
, IPP_BAD_REQUEST
,
7264 _("Bad job-uri attribute \"%s\"!"),
7265 uri
->values
[0].string
.text
);
7269 jobid
= atoi(resource
+ 6);
7273 * See if the job exists...
7276 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7279 * Nope - return a "not found" error...
7282 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7287 * See if job is "held"...
7290 if (job
->state_value
!= IPP_JOB_HELD
)
7293 * Nope - return a "not possible" error...
7296 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
7301 * See if the job is owned by the requesting user...
7304 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7306 send_http_error(con
, HTTP_UNAUTHORIZED
);
7311 * Reset the job-hold-until value to "no-hold"...
7314 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7315 IPP_TAG_KEYWORD
)) == NULL
)
7316 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7320 _cupsStrFree(attr
->values
[0].string
.text
);
7322 attr
->value_tag
= IPP_TAG_KEYWORD
;
7323 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
7327 * Release the job and return...
7330 cupsdReleaseJob(job
);
7332 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was released by \"%s\".", jobid
,
7335 con
->response
->request
.status
.status_code
= IPP_OK
;
7340 * 'renew_subscription()' - Renew an existing subscription...
7345 cupsd_client_t
*con
, /* I - Client connection */
7346 int sub_id
) /* I - Subscription ID */
7348 http_status_t status
; /* Policy status */
7349 cupsd_subscription_t
*sub
; /* Subscription */
7350 ipp_attribute_t
*lease
; /* notify-lease-duration */
7353 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7354 "renew_subscription(con=%p[%d], sub_id=%d)",
7355 con
, con
->http
.fd
, sub_id
);
7358 * Is the subscription ID valid?
7361 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7364 * Bad subscription ID...
7367 send_ipp_status(con
, IPP_NOT_FOUND
,
7368 _("notify-subscription-id %d no good!"), sub_id
);
7375 * Job subscriptions cannot be renewed...
7378 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7379 _("Job subscriptions cannot be renewed!"));
7387 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7389 con
, sub
->owner
)) != HTTP_OK
)
7391 send_http_error(con
, status
);
7396 * Renew the subscription...
7399 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
7402 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
7404 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
7406 cupsdLogMessage(CUPSD_LOG_INFO
,
7407 "renew_subscription: Limiting notify-lease-duration to "
7410 sub
->lease
= MaxLeaseDuration
;
7413 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
7415 cupsdSaveAllSubscriptions();
7417 con
->response
->request
.status
.status_code
= IPP_OK
;
7422 * 'restart_job()' - Restart an old print job.
7426 restart_job(cupsd_client_t
*con
, /* I - Client connection */
7427 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7429 ipp_attribute_t
*attr
; /* Current attribute */
7430 int jobid
; /* Job ID */
7431 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7432 username
[HTTP_MAX_URI
], /* Username portion of URI */
7433 host
[HTTP_MAX_URI
], /* Host portion of URI */
7434 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7435 int port
; /* Port portion of URI */
7436 cupsd_job_t
*job
; /* Job information */
7439 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
7440 con
->http
.fd
, uri
->values
[0].string
.text
);
7443 * See if we have a job URI or a printer URI...
7446 if (!strcmp(uri
->name
, "printer-uri"))
7449 * Got a printer URI; see if we also have a job-id attribute...
7452 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7453 IPP_TAG_INTEGER
)) == NULL
)
7455 send_ipp_status(con
, IPP_BAD_REQUEST
,
7456 _("Got a printer-uri attribute but no job-id!"));
7460 jobid
= attr
->values
[0].integer
;
7465 * Got a job URI; parse it to get the job ID...
7468 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7469 sizeof(method
), username
, sizeof(username
), host
,
7470 sizeof(host
), &port
, resource
, sizeof(resource
));
7472 if (strncmp(resource
, "/jobs/", 6))
7478 send_ipp_status(con
, IPP_BAD_REQUEST
,
7479 _("Bad job-uri attribute \"%s\"!"),
7480 uri
->values
[0].string
.text
);
7484 jobid
= atoi(resource
+ 6);
7488 * See if the job exists...
7491 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7494 * Nope - return a "not found" error...
7497 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7502 * See if job is in any of the "completed" states...
7505 if (job
->state_value
<= IPP_JOB_PROCESSING
)
7508 * Nope - return a "not possible" error...
7511 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
7517 * See if we have retained the job files...
7522 if (!job
->attrs
||job
->num_files
== 0)
7525 * Nope - return a "not possible" error...
7528 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7529 _("Job #%d cannot be restarted - no files!"), jobid
);
7534 * See if the job is owned by the requesting user...
7537 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7539 send_http_error(con
, HTTP_UNAUTHORIZED
);
7544 * Restart the job and return...
7547 cupsdRestartJob(job
);
7549 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was restarted by \"%s\".", jobid
,
7552 con
->response
->request
.status
.status_code
= IPP_OK
;
7557 * 'save_auth_info()' - Save authentication information for a job.
7561 save_auth_info(cupsd_client_t
*con
, /* I - Client connection */
7562 cupsd_job_t
*job
) /* I - Job */
7564 int i
; /* Looping var */
7565 char filename
[1024]; /* Job authentication filename */
7566 cups_file_t
*fp
; /* Job authentication file */
7567 char line
[1024]; /* Line for file */
7571 * This function saves the in-memory authentication information for
7572 * a job so that it can be used to authenticate with a remote host.
7573 * The information is stored in a file that is readable only by the
7574 * root user. The username and password are Base-64 encoded, each
7575 * on a separate line, followed by random number (up to 1024) of
7576 * newlines to limit the amount of information that is exposed.
7578 * Because of the potential for exposing of authentication information,
7579 * this functionality is only enabled when running cupsd as root.
7581 * This caching only works for the Basic and BasicDigest authentication
7582 * types. Digest authentication cannot be cached this way, and in
7583 * the future Kerberos authentication may make all of this obsolete.
7585 * Authentication information is saved whenever an authenticated
7586 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
7589 * This information is deleted after a job is completed or canceled,
7590 * so reprints may require subsequent re-authentication.
7597 * Create the authentication file and change permissions...
7600 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
7601 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
7603 cupsdLogMessage(CUPSD_LOG_ERROR
,
7604 "Unable to save authentication info to \"%s\" - %s",
7605 filename
, strerror(errno
));
7609 fchown(cupsFileNumber(fp
), 0, 0);
7610 fchmod(cupsFileNumber(fp
), 0400);
7613 * Write the authenticated username...
7616 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
7617 cupsFilePrintf(fp
, "%s\n", line
);
7620 * Write the authenticated password...
7623 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
7624 cupsFilePrintf(fp
, "%s\n", line
);
7627 * Write a random number of newlines to the end of the file...
7630 for (i
= (rand() % 1024); i
>= 0; i
--)
7631 cupsFilePutChar(fp
, '\n');
7634 * Close the file and return...
7642 * 'send_document()' - Send a file to a printer or class.
7646 send_document(cupsd_client_t
*con
, /* I - Client connection */
7647 ipp_attribute_t
*uri
) /* I - Printer URI */
7649 ipp_attribute_t
*attr
; /* Current attribute */
7650 ipp_attribute_t
*format
; /* Document-format attribute */
7651 int jobid
; /* Job ID number */
7652 cupsd_job_t
*job
; /* Current job */
7653 char job_uri
[HTTP_MAX_URI
],
7655 method
[HTTP_MAX_URI
],
7656 /* Method portion of URI */
7657 username
[HTTP_MAX_URI
],
7658 /* Username portion of URI */
7660 /* Host portion of URI */
7661 resource
[HTTP_MAX_URI
];
7662 /* Resource portion of URI */
7663 int port
; /* Port portion of URI */
7664 mime_type_t
*filetype
; /* Type of file */
7665 char super
[MIME_MAX_SUPER
],
7666 /* Supertype of file */
7667 type
[MIME_MAX_TYPE
],
7668 /* Subtype of file */
7669 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7670 /* Textual name of mime type */
7671 char filename
[1024]; /* Job filename */
7672 cupsd_printer_t
*printer
; /* Current printer */
7673 struct stat fileinfo
; /* File information */
7674 int kbytes
; /* Size of file */
7675 int compression
; /* Type of compression */
7678 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
7679 con
->http
.fd
, uri
->values
[0].string
.text
);
7682 * See if we have a job URI or a printer URI...
7685 if (!strcmp(uri
->name
, "printer-uri"))
7688 * Got a printer URI; see if we also have a job-id attribute...
7691 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7692 IPP_TAG_INTEGER
)) == NULL
)
7694 send_ipp_status(con
, IPP_BAD_REQUEST
,
7695 _("Got a printer-uri attribute but no job-id!"));
7699 jobid
= attr
->values
[0].integer
;
7704 * Got a job URI; parse it to get the job ID...
7707 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7708 sizeof(method
), username
, sizeof(username
), host
,
7709 sizeof(host
), &port
, resource
, sizeof(resource
));
7711 if (strncmp(resource
, "/jobs/", 6))
7717 send_ipp_status(con
, IPP_BAD_REQUEST
,
7718 _("Bad job-uri attribute \"%s\"!"),
7719 uri
->values
[0].string
.text
);
7723 jobid
= atoi(resource
+ 6);
7727 * See if the job exists...
7730 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7733 * Nope - return a "not found" error...
7736 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7741 * See if the job is owned by the requesting user...
7744 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7746 send_http_error(con
, HTTP_UNAUTHORIZED
);
7751 * OK, see if the client is sending the document compressed - CUPS
7752 * only supports "none" and "gzip".
7755 compression
= CUPS_FILE_NONE
;
7757 if ((attr
= ippFindAttribute(con
->request
, "compression",
7758 IPP_TAG_KEYWORD
)) != NULL
)
7760 if (strcmp(attr
->values
[0].string
.text
, "none")
7762 && strcmp(attr
->values
[0].string
.text
, "gzip")
7763 #endif /* HAVE_LIBZ */
7766 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
7767 attr
->values
[0].string
.text
);
7768 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7769 "compression", NULL
, attr
->values
[0].string
.text
);
7774 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7775 compression
= CUPS_FILE_GZIP
;
7776 #endif /* HAVE_LIBZ */
7780 * Do we have a file to print?
7785 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7790 * Is it a format we support?
7793 if ((format
= ippFindAttribute(con
->request
, "document-format",
7794 IPP_TAG_MIMETYPE
)) != NULL
)
7797 * Grab format from client...
7800 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
7802 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
7803 format
->values
[0].string
.text
);
7810 * No document format attribute? Auto-type it!
7813 strcpy(super
, "application");
7814 strcpy(type
, "octet-stream");
7817 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7820 * Auto-type the file...
7823 ipp_attribute_t
*doc_name
; /* document-name attribute */
7826 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
7828 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7829 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7830 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7836 * Replace the document-format attribute value with the auto-typed one.
7839 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7844 _cupsStrFree(format
->values
[0].string
.text
);
7845 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7848 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7849 "document-format", NULL
, mimetype
);
7852 filetype
= mimeType(MimeDatabase
, super
, type
);
7855 filetype
= mimeType(MimeDatabase
, super
, type
);
7859 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7860 _("Unsupported format \'%s/%s\'!"), super
, type
);
7861 cupsdLogMessage(CUPSD_LOG_INFO
,
7862 "Hint: Do you have the raw file printing rules enabled?");
7865 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7866 "document-format", NULL
, format
->values
[0].string
.text
);
7871 printer
= cupsdFindDest(job
->dest
);
7873 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
7875 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7878 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7879 _("Unsupported format \'%s\'!"), mimetype
);
7881 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7882 "document-format", NULL
, mimetype
);
7887 cupsdLogMessage(CUPSD_LOG_DEBUG
,
7888 "send_document: request file type is %s/%s.",
7889 filetype
->super
, filetype
->type
);
7892 * Add the file to the job...
7897 if (add_file(con
, job
, filetype
, compression
))
7900 if (stat(con
->filename
, &fileinfo
))
7903 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7905 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7907 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7908 IPP_TAG_INTEGER
)) != NULL
)
7909 attr
->values
[0].integer
+= kbytes
;
7911 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
7913 rename(con
->filename
, filename
);
7915 cupsdClearString(&con
->filename
);
7917 cupsdLogMessage(CUPSD_LOG_INFO
,
7918 "File of type %s/%s queued in job #%d by \"%s\".",
7919 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
7922 * Start the job if this is the last document...
7925 if ((attr
= ippFindAttribute(con
->request
, "last-document",
7926 IPP_TAG_BOOLEAN
)) != NULL
&&
7927 attr
->values
[0].boolean
)
7930 * See if we need to add the ending sheet...
7934 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
7935 (attr
= ippFindAttribute(job
->attrs
, "job-sheets",
7936 IPP_TAG_ZERO
)) != NULL
&&
7937 attr
->num_values
> 1)
7943 cupsdLogMessage(CUPSD_LOG_INFO
,
7944 "Adding end banner page \"%s\" to job %d.",
7945 attr
->values
[1].string
.text
, job
->id
);
7947 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
7949 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7952 if (job
->state_value
== IPP_JOB_STOPPED
)
7954 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7955 job
->state_value
= IPP_JOB_PENDING
;
7957 else if (job
->state_value
== IPP_JOB_HELD
)
7959 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7960 IPP_TAG_KEYWORD
)) == NULL
)
7961 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7963 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
7965 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7966 job
->state_value
= IPP_JOB_PENDING
;
7973 * Start the job if possible... Since cupsdCheckJobs() can cancel a
7974 * job if it doesn't print, we need to re-find the job afterwards...
7981 job
= cupsdFindJob(jobid
);
7985 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7986 IPP_TAG_KEYWORD
)) == NULL
)
7987 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7989 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
7991 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
7992 job
->state_value
= IPP_JOB_HELD
;
7993 job
->hold_until
= time(NULL
) + 60;
7999 * Fill in the response info...
8002 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
8005 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
8008 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
8010 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
8011 job
? job
->state_value
: IPP_JOB_CANCELLED
);
8012 add_job_state_reasons(con
, job
);
8014 con
->response
->request
.status
.status_code
= IPP_OK
;
8019 * 'send_http_error()' - Send a HTTP error back to the IPP client.
8023 send_http_error(cupsd_client_t
*con
, /* I - Client connection */
8024 http_status_t status
) /* I - HTTP status code */
8026 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
8027 ippOpString(con
->request
->request
.op
.operation_id
),
8028 httpStatus(status
));
8030 cupsdSendError(con
, status
);
8032 ippDelete(con
->response
);
8033 con
->response
= NULL
;
8040 * 'send_ipp_status()' - Send a status back to the IPP client.
8044 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
8045 ipp_status_t status
, /* I - IPP status code */
8046 const char *message
, /* I - Status message */
8047 ...) /* I - Additional args as needed */
8049 va_list ap
; /* Pointer to additional args */
8050 char formatted
[1024]; /* Formatted errror message */
8055 va_start(ap
, message
);
8056 vsnprintf(formatted
, sizeof(formatted
),
8057 _cupsLangString(con
->language
, message
), ap
);
8060 cupsdLogMessage(status
>= IPP_BAD_REQUEST
? CUPSD_LOG_ERROR
: CUPSD_LOG_INFO
,
8062 ippOpString(con
->request
->request
.op
.operation_id
),
8063 ippErrorString(status
), formatted
);
8066 cupsdLogMessage(status
>= IPP_BAD_REQUEST
? CUPSD_LOG_ERROR
: CUPSD_LOG_INFO
,
8068 ippOpString(con
->request
->request
.op
.operation_id
),
8069 ippErrorString(status
));
8071 con
->response
->request
.status
.status_code
= status
;
8073 if (ippFindAttribute(con
->response
, "attributes-charset",
8074 IPP_TAG_ZERO
) == NULL
)
8075 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
8076 "attributes-charset", NULL
, DefaultCharset
);
8078 if (ippFindAttribute(con
->response
, "attributes-natural-language",
8079 IPP_TAG_ZERO
) == NULL
)
8080 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
8081 "attributes-natural-language", NULL
, DefaultLanguage
);
8084 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
8085 "status-message", NULL
, formatted
);
8090 * 'set_default()' - Set the default destination...
8094 set_default(cupsd_client_t
*con
, /* I - Client connection */
8095 ipp_attribute_t
*uri
) /* I - Printer URI */
8097 http_status_t status
; /* Policy status */
8098 cups_ptype_t dtype
; /* Destination type (printer or class) */
8099 char method
[HTTP_MAX_URI
],
8100 /* Method portion of URI */
8101 username
[HTTP_MAX_URI
],
8102 /* Username portion of URI */
8104 /* Host portion of URI */
8105 resource
[HTTP_MAX_URI
];
8106 /* Resource portion of URI */
8107 int port
; /* Port portion of URI */
8108 const char *name
; /* Printer name */
8109 cupsd_printer_t
*printer
; /* Printer */
8112 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
8113 con
->http
.fd
, uri
->values
[0].string
.text
);
8116 * Is the destination valid?
8119 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8120 sizeof(method
), username
, sizeof(username
), host
,
8121 sizeof(host
), &port
, resource
, sizeof(resource
));
8123 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
8129 send_ipp_status(con
, IPP_NOT_FOUND
,
8130 _("The printer or class was not found."));
8138 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
8140 send_http_error(con
, status
);
8145 * Set it as the default...
8148 DefaultPrinter
= printer
;
8150 cupsdSaveAllPrinters();
8151 cupsdSaveAllClasses();
8153 cupsdWritePrintcap();
8155 cupsdLogMessage(CUPSD_LOG_INFO
,
8156 "Default destination set to \"%s\" by \"%s\".", name
,
8160 * Everything was ok, so return OK status...
8163 con
->response
->request
.status
.status_code
= IPP_OK
;
8168 * 'set_job_attrs()' - Set job attributes.
8172 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
8173 ipp_attribute_t
*uri
) /* I - Job URI */
8175 ipp_attribute_t
*attr
, /* Current attribute */
8176 *attr2
; /* Job attribute */
8177 int jobid
; /* Job ID */
8178 cupsd_job_t
*job
; /* Current job */
8179 char method
[HTTP_MAX_URI
],
8180 /* Method portion of URI */
8181 username
[HTTP_MAX_URI
],
8182 /* Username portion of URI */
8184 /* Host portion of URI */
8185 resource
[HTTP_MAX_URI
];
8186 /* Resource portion of URI */
8187 int port
; /* Port portion of URI */
8190 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
8191 con
->http
.fd
, uri
->values
[0].string
.text
);
8194 * Start with "everything is OK" status...
8197 con
->response
->request
.status
.status_code
= IPP_OK
;
8200 * See if we have a job URI or a printer URI...
8203 if (!strcmp(uri
->name
, "printer-uri"))
8206 * Got a printer URI; see if we also have a job-id attribute...
8209 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8210 IPP_TAG_INTEGER
)) == NULL
)
8212 send_ipp_status(con
, IPP_BAD_REQUEST
,
8213 _("Got a printer-uri attribute but no job-id!"));
8217 jobid
= attr
->values
[0].integer
;
8222 * Got a job URI; parse it to get the job ID...
8225 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8226 sizeof(method
), username
, sizeof(username
), host
,
8227 sizeof(host
), &port
, resource
, sizeof(resource
));
8229 if (strncmp(resource
, "/jobs/", 6))
8235 send_ipp_status(con
, IPP_BAD_REQUEST
,
8236 _("Bad job-uri attribute \"%s\"!"),
8237 uri
->values
[0].string
.text
);
8241 jobid
= atoi(resource
+ 6);
8245 * See if the job exists...
8248 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8251 * Nope - return a "not found" error...
8254 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8259 * See if the job has been completed...
8262 if (job
->state_value
> IPP_JOB_STOPPED
)
8265 * Return a "not-possible" error...
8268 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8269 _("Job #%d is finished and cannot be altered!"), jobid
);
8274 * See if the job is owned by the requesting user...
8277 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8279 send_http_error(con
, HTTP_UNAUTHORIZED
);
8284 * See what the user wants to change.
8289 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8291 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8294 if (!strcmp(attr
->name
, "attributes-charset") ||
8295 !strcmp(attr
->name
, "attributes-natural-language") ||
8296 !strcmp(attr
->name
, "document-compression") ||
8297 !strcmp(attr
->name
, "document-format") ||
8298 !strcmp(attr
->name
, "job-detailed-status-messages") ||
8299 !strcmp(attr
->name
, "job-document-access-errors") ||
8300 !strcmp(attr
->name
, "job-id") ||
8301 !strcmp(attr
->name
, "job-k-octets") ||
8302 !strcmp(attr
->name
, "job-originating-host-name") ||
8303 !strcmp(attr
->name
, "job-originating-user-name") ||
8304 !strcmp(attr
->name
, "job-printer-up-time") ||
8305 !strcmp(attr
->name
, "job-printer-uri") ||
8306 !strcmp(attr
->name
, "job-sheets") ||
8307 !strcmp(attr
->name
, "job-state-message") ||
8308 !strcmp(attr
->name
, "job-state-reasons") ||
8309 !strcmp(attr
->name
, "job-uri") ||
8310 !strcmp(attr
->name
, "number-of-documents") ||
8311 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
8312 !strcmp(attr
->name
, "output-device-assigned") ||
8313 !strncmp(attr
->name
, "date-time-at-", 13) ||
8314 !strncmp(attr
->name
, "job-impressions", 15) ||
8315 !strncmp(attr
->name
, "job-k-octets", 12) ||
8316 !strncmp(attr
->name
, "job-media-sheets", 16) ||
8317 !strncmp(attr
->name
, "time-at-", 8))
8323 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
8324 _("%s cannot be changed."), attr
->name
);
8326 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8327 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8332 if (!strcmp(attr
->name
, "job-priority"))
8335 * Change the job priority...
8338 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8340 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
8342 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8343 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8345 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
8347 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8348 _("Job is completed and cannot be changed."));
8351 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8352 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
8354 else if (!strcmp(attr
->name
, "job-state"))
8357 * Change the job state...
8360 if (attr
->value_tag
!= IPP_TAG_ENUM
)
8362 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
8364 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8365 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8369 switch (attr
->values
[0].integer
)
8371 case IPP_JOB_PENDING
:
8373 if (job
->state_value
> IPP_JOB_HELD
)
8375 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8376 _("Job state cannot be changed."));
8379 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8381 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8382 job
->state_value
= attr
->values
[0].integer
;
8386 case IPP_JOB_PROCESSING
:
8387 case IPP_JOB_STOPPED
:
8388 if (job
->state_value
!= attr
->values
[0].integer
)
8390 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8391 _("Job state cannot be changed."));
8396 case IPP_JOB_CANCELLED
:
8397 case IPP_JOB_ABORTED
:
8398 case IPP_JOB_COMPLETED
:
8399 if (job
->state_value
> IPP_JOB_PROCESSING
)
8401 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8402 _("Job state cannot be changed."));
8405 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8407 cupsdCancelJob(job
, 0);
8411 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8412 job
->state_value
= attr
->values
[0].integer
;
8420 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
8422 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8423 IPP_TAG_ZERO
)) != NULL
)
8426 * Some other value; first free the old value...
8429 if (job
->attrs
->prev
)
8430 job
->attrs
->prev
->next
= attr2
->next
;
8432 job
->attrs
->attrs
= attr2
->next
;
8434 if (job
->attrs
->last
== attr2
)
8435 job
->attrs
->last
= job
->attrs
->prev
;
8437 _ippFreeAttr(attr2
);
8440 * Then copy the attribute...
8443 copy_attribute(job
->attrs
, attr
, 0);
8446 * See if the job-name or job-hold-until is being changed.
8449 if (!strcmp(attr
->name
, "job-hold-until"))
8451 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
8453 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
8454 cupsdReleaseJob(job
);
8459 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
8462 * Delete the attribute...
8465 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8466 IPP_TAG_ZERO
)) != NULL
)
8468 if (job
->attrs
->prev
)
8469 job
->attrs
->prev
->next
= attr2
->next
;
8471 job
->attrs
->attrs
= attr2
->next
;
8473 if (attr2
== job
->attrs
->last
)
8474 job
->attrs
->last
= job
->attrs
->prev
;
8476 _ippFreeAttr(attr2
);
8482 * Add new option by copying it...
8485 copy_attribute(job
->attrs
, attr
, 0);
8496 * Start jobs if possible...
8504 * 'set_printer_defaults()' - Set printer default options from a request.
8508 set_printer_defaults(
8509 cupsd_client_t
*con
, /* I - Client connection */
8510 cupsd_printer_t
*printer
) /* I - Printer */
8512 int i
; /* Looping var */
8513 ipp_attribute_t
*attr
; /* Current attribute */
8514 int namelen
; /* Length of attribute name */
8515 char name
[256], /* New attribute name */
8516 value
[256]; /* String version of integer attrs */
8519 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8522 * Skip non-printer attributes...
8525 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
8528 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
8530 if (!strcmp(attr
->name
, "job-sheets-default"))
8533 * Only allow keywords and names...
8536 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8540 * Only allow job-sheets-default to be set when running without a
8541 * system high classification level...
8547 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
8549 if (attr
->num_values
> 1)
8550 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
8552 cupsdSetString(&printer
->job_sheets
[1], "none");
8554 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
8556 cupsdFreePrinterUsers(printer
);
8558 printer
->deny_users
= 0;
8560 if (attr
->value_tag
== IPP_TAG_NAME
&&
8561 (attr
->num_values
> 1 ||
8562 strcmp(attr
->values
[0].string
.text
, "all")))
8564 for (i
= 0; i
< attr
->num_values
; i
++)
8565 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8568 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
8570 cupsdFreePrinterUsers(printer
);
8572 printer
->deny_users
= 1;
8574 if (attr
->value_tag
== IPP_TAG_NAME
&&
8575 (attr
->num_values
> 1 ||
8576 strcmp(attr
->values
[0].string
.text
, "none")))
8578 for (i
= 0; i
< attr
->num_values
; i
++)
8579 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8582 else if (!strcmp(attr
->name
, "job-quota-period"))
8584 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8587 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
8588 attr
->values
[0].integer
);
8589 cupsdFreeQuotas(printer
);
8591 printer
->quota_period
= attr
->values
[0].integer
;
8593 else if (!strcmp(attr
->name
, "job-k-limit"))
8595 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8598 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
8599 attr
->values
[0].integer
);
8600 cupsdFreeQuotas(printer
);
8602 printer
->k_limit
= attr
->values
[0].integer
;
8604 else if (!strcmp(attr
->name
, "job-page-limit"))
8606 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8609 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
8610 attr
->values
[0].integer
);
8611 cupsdFreeQuotas(printer
);
8613 printer
->page_limit
= attr
->values
[0].integer
;
8615 else if (!strcmp(attr
->name
, "printer-op-policy"))
8617 cupsd_policy_t
*p
; /* Policy */
8620 if (attr
->value_tag
!= IPP_TAG_NAME
)
8623 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
8625 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8626 "Setting printer-op-policy to \"%s\"...",
8627 attr
->values
[0].string
.text
);
8628 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
8629 printer
->op_policy_ptr
= p
;
8633 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8634 _("Unknown printer-op-policy \"%s\"."),
8635 attr
->values
[0].string
.text
);
8639 else if (!strcmp(attr
->name
, "printer-error-policy"))
8641 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8644 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
8645 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
8646 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
8648 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8649 _("Unknown printer-error-policy \"%s\"."),
8650 attr
->values
[0].string
.text
);
8654 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8655 "Setting printer-error-policy to \"%s\"...",
8656 attr
->values
[0].string
.text
);
8657 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
8659 else if (!strcmp(attr
->name
, "document-format-default") ||
8660 !strcmp(attr
->name
, "notify-lease-duration-default") ||
8661 !strcmp(attr
->name
, "notify-notify-events-default"))
8665 * Skip any other non-default attributes...
8668 namelen
= strlen(attr
->name
);
8669 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
8670 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
8674 * OK, anything else must be a user-defined default...
8677 strlcpy(name
, attr
->name
, sizeof(name
));
8678 name
[namelen
- 8] = '\0'; /* Strip "-default" */
8680 switch (attr
->value_tag
)
8682 case IPP_TAG_DELETEATTR
:
8683 printer
->num_options
= cupsRemoveOption(name
,
8684 printer
->num_options
,
8685 &(printer
->options
));
8686 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8687 "Deleting %s", attr
->name
);
8691 case IPP_TAG_KEYWORD
:
8693 printer
->num_options
= cupsAddOption(name
,
8694 attr
->values
[0].string
.text
,
8695 printer
->num_options
,
8696 &(printer
->options
));
8697 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8698 "Setting %s to \"%s\"...", attr
->name
,
8699 attr
->values
[0].string
.text
);
8702 case IPP_TAG_BOOLEAN
:
8703 printer
->num_options
= cupsAddOption(name
,
8704 attr
->values
[0].boolean
?
8706 printer
->num_options
,
8707 &(printer
->options
));
8708 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8709 "Setting %s to %s...", attr
->name
,
8710 attr
->values
[0].boolean
? "true" : "false");
8713 case IPP_TAG_INTEGER
:
8715 sprintf(value
, "%d", attr
->values
[0].integer
);
8716 printer
->num_options
= cupsAddOption(name
, value
,
8717 printer
->num_options
,
8718 &(printer
->options
));
8719 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8720 "Setting %s to %s...", attr
->name
, value
);
8723 case IPP_TAG_RANGE
:
8724 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
8725 attr
->values
[0].range
.upper
);
8726 printer
->num_options
= cupsAddOption(name
, value
,
8727 printer
->num_options
,
8728 &(printer
->options
));
8729 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8730 "Setting %s to %s...", attr
->name
, value
);
8733 case IPP_TAG_RESOLUTION
:
8734 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
8735 attr
->values
[0].resolution
.yres
,
8736 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
8738 printer
->num_options
= cupsAddOption(name
, value
,
8739 printer
->num_options
,
8740 &(printer
->options
));
8741 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8742 "Setting %s to %s...", attr
->name
, value
);
8746 /* Do nothing for other values */
8754 * 'start_printer()' - Start a printer.
8758 start_printer(cupsd_client_t
*con
, /* I - Client connection */
8759 ipp_attribute_t
*uri
) /* I - Printer URI */
8761 http_status_t status
; /* Policy status */
8762 cups_ptype_t dtype
; /* Destination type (printer or class) */
8763 char method
[HTTP_MAX_URI
],
8764 /* Method portion of URI */
8765 username
[HTTP_MAX_URI
],
8766 /* Username portion of URI */
8768 /* Host portion of URI */
8769 resource
[HTTP_MAX_URI
];
8770 /* Resource portion of URI */
8771 int port
; /* Port portion of URI */
8772 const char *name
; /* Printer name */
8773 cupsd_printer_t
*printer
; /* Printer data */
8776 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
8777 con
->http
.fd
, uri
->values
[0].string
.text
);
8780 * Is the destination valid?
8783 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8784 sizeof(method
), username
, sizeof(username
), host
,
8785 sizeof(host
), &port
, resource
, sizeof(resource
));
8787 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
8793 send_ipp_status(con
, IPP_NOT_FOUND
,
8794 _("The printer or class was not found."));
8802 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8804 send_http_error(con
, status
);
8809 * Start the printer...
8812 printer
->state_message
[0] = '\0';
8814 cupsdStartPrinter(printer
, 1);
8816 if (dtype
& CUPS_PRINTER_CLASS
)
8818 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".", name
,
8820 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
8821 "Class \"%s\" started by \"%s\".", name
, get_username(con
));
8825 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".", name
,
8827 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
8828 "Printer \"%s\" started by \"%s\".", name
, get_username(con
));
8834 * Everything was ok, so return OK status...
8837 con
->response
->request
.status
.status_code
= IPP_OK
;
8842 * 'stop_printer()' - Stop a printer.
8846 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
8847 ipp_attribute_t
*uri
) /* I - Printer URI */
8849 http_status_t status
; /* Policy status */
8850 cups_ptype_t dtype
; /* Destination type (printer or class) */
8851 char method
[HTTP_MAX_URI
],
8852 /* Method portion of URI */
8853 username
[HTTP_MAX_URI
],
8854 /* Username portion of URI */
8856 /* Host portion of URI */
8857 resource
[HTTP_MAX_URI
];
8858 /* Resource portion of URI */
8859 int port
; /* Port portion of URI */
8860 const char *name
; /* Printer name */
8861 cupsd_printer_t
*printer
; /* Printer data */
8862 ipp_attribute_t
*attr
; /* printer-state-message attribute */
8865 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
8866 con
->http
.fd
, uri
->values
[0].string
.text
);
8869 * Is the destination valid?
8872 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8873 sizeof(method
), username
, sizeof(username
), host
,
8874 sizeof(host
), &port
, resource
, sizeof(resource
));
8876 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
8882 send_ipp_status(con
, IPP_NOT_FOUND
,
8883 _("The printer or class was not found."));
8891 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8893 send_http_error(con
, status
);
8898 * Stop the printer...
8901 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8902 IPP_TAG_TEXT
)) == NULL
)
8903 strcpy(printer
->state_message
, "Paused");
8906 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8907 sizeof(printer
->state_message
));
8910 cupsdStopPrinter(printer
, 1);
8912 if (dtype
& CUPS_PRINTER_CLASS
)
8914 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".", name
,
8916 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
8917 "Class \"%s\" stopped by \"%s\".", name
, get_username(con
));
8921 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".", name
,
8923 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
8924 "Printer \"%s\" stopped by \"%s\".", name
, get_username(con
));
8928 * Everything was ok, so return OK status...
8931 con
->response
->request
.status
.status_code
= IPP_OK
;
8936 * 'user_allowed()' - See if a user is allowed to print to a queue.
8939 static int /* O - 0 if not allowed, 1 if allowed */
8940 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
8941 const char *username
) /* I - Username */
8943 int i
; /* Looping var */
8944 struct passwd
*pw
; /* User password data */
8947 if (p
->num_users
== 0)
8950 if (!strcmp(username
, "root"))
8953 pw
= getpwnam(username
);
8956 for (i
= 0; i
< p
->num_users
; i
++)
8958 if (p
->users
[i
][0] == '@')
8961 * Check group membership...
8964 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
8967 else if (!strcasecmp(username
, p
->users
[i
]))
8971 return ((i
< p
->num_users
) != p
->deny_users
);
8976 * 'validate_job()' - Validate printer options and destination.
8980 validate_job(cupsd_client_t
*con
, /* I - Client connection */
8981 ipp_attribute_t
*uri
) /* I - Printer URI */
8983 http_status_t status
; /* Policy status */
8984 ipp_attribute_t
*attr
; /* Current attribute */
8985 ipp_attribute_t
*format
; /* Document-format attribute */
8986 cups_ptype_t dtype
; /* Destination type (printer or class) */
8987 char method
[HTTP_MAX_URI
],
8988 /* Method portion of URI */
8989 username
[HTTP_MAX_URI
],
8990 /* Username portion of URI */
8992 /* Host portion of URI */
8993 resource
[HTTP_MAX_URI
];
8994 /* Resource portion of URI */
8995 int port
; /* Port portion of URI */
8996 char super
[MIME_MAX_SUPER
],
8997 /* Supertype of file */
8998 type
[MIME_MAX_TYPE
];
8999 /* Subtype of file */
9000 cupsd_printer_t
*printer
; /* Printer */
9003 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
9004 con
->http
.fd
, uri
->values
[0].string
.text
);
9007 * OK, see if the client is sending the document compressed - CUPS
9008 * doesn't support compression yet...
9011 if ((attr
= ippFindAttribute(con
->request
, "compression",
9012 IPP_TAG_KEYWORD
)) != NULL
&&
9013 !strcmp(attr
->values
[0].string
.text
, "none"))
9015 send_ipp_status(con
, IPP_ATTRIBUTES
,
9016 _("Unsupported compression attribute %s!"),
9017 attr
->values
[0].string
.text
);
9018 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9019 "compression", NULL
, attr
->values
[0].string
.text
);
9024 * Is it a format we support?
9027 if ((format
= ippFindAttribute(con
->request
, "document-format",
9028 IPP_TAG_MIMETYPE
)) != NULL
)
9030 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
9032 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9033 format
->values
[0].string
.text
);
9037 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
9038 !mimeType(MimeDatabase
, super
, type
))
9040 cupsdLogMessage(CUPSD_LOG_INFO
,
9041 "Hint: Do you have the raw file printing rules enabled?");
9042 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9043 _("Unsupported format \"%s\"!"),
9044 format
->values
[0].string
.text
);
9045 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9046 "document-format", NULL
, format
->values
[0].string
.text
);
9052 * Is the destination valid?
9055 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9056 sizeof(method
), username
, sizeof(username
), host
,
9057 sizeof(host
), &port
, resource
, sizeof(resource
));
9059 if (cupsdValidateDest(host
, resource
, &dtype
, &printer
) == NULL
)
9065 send_ipp_status(con
, IPP_NOT_FOUND
,
9066 _("The printer or class was not found."));
9074 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9076 send_http_error(con
, status
);
9081 * Everything was ok, so return OK status...
9084 con
->response
->request
.status
.status_code
= IPP_OK
;
9089 * 'validate_name()' - Make sure the printer name only contains valid chars.
9092 static int /* O - 0 if name is no good, 1 if name is good */
9093 validate_name(const char *name
) /* I - Name to check */
9095 const char *ptr
; /* Pointer into name */
9099 * Scan the whole name...
9102 for (ptr
= name
; *ptr
; ptr
++)
9103 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
9107 * All the characters are good; validate the length, too...
9110 return ((ptr
- name
) < 128);
9115 * 'validate_user()' - Validate the user for the request.
9118 static int /* O - 1 if permitted, 0 otherwise */
9119 validate_user(cupsd_job_t
*job
, /* I - Job */
9120 cupsd_client_t
*con
, /* I - Client connection */
9121 const char *owner
, /* I - Owner of job/resource */
9122 char *username
, /* O - Authenticated username */
9123 int userlen
) /* I - Length of username */
9125 cupsd_printer_t
*printer
; /* Printer for job */
9128 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9129 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
9131 job
? job
->id
: 0, con
->http
.fd
, owner
? owner
: "(null)",
9138 if (!con
|| !owner
|| !username
|| userlen
<= 0)
9142 * Get the best authenticated username that is available.
9145 strlcpy(username
, get_username(con
), userlen
);
9148 * Check the username against the owner...
9151 printer
= cupsdFindDest(job
->dest
);
9153 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
9154 con
, owner
) == HTTP_OK
);
9159 * End of "$Id: ipp.c 5383 2006-04-07 15:36:10Z mike $".