2 * "$Id: ipp.c 5334 2006-03-24 01:20:03Z 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 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1495 job
->hold_until
= time(NULL
) + 60;
1496 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1497 job
->state_value
= IPP_JOB_HELD
;
1501 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1502 job
->state_value
= IPP_JOB_PENDING
;
1505 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1509 * Add job sheets options...
1512 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1513 IPP_TAG_ZERO
)) == NULL
)
1515 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1516 "Adding default job-sheets values \"%s,%s\"...",
1517 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1519 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1521 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1522 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1525 job
->job_sheets
= attr
;
1528 * Enforce classification level if set...
1533 cupsdLogMessage(CUPSD_LOG_INFO
,
1534 "Classification=\"%s\", ClassifyOverride=%d",
1535 Classification
? Classification
: "(null)",
1538 if (ClassifyOverride
)
1540 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1541 (attr
->num_values
== 1 ||
1542 !strcmp(attr
->values
[1].string
.text
, "none")))
1545 * Force the leading banner to have the classification on it...
1548 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1550 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1551 "job-sheets=\"%s,none\", "
1552 "job-originating-user-name=\"%s\"",
1553 job
->id
, Classification
, job
->username
);
1555 else if (attr
->num_values
== 2 &&
1556 strcmp(attr
->values
[0].string
.text
,
1557 attr
->values
[1].string
.text
) &&
1558 strcmp(attr
->values
[0].string
.text
, "none") &&
1559 strcmp(attr
->values
[1].string
.text
, "none"))
1562 * Can't put two different security markings on the same document!
1565 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1567 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1568 "job-sheets=\"%s,%s\", "
1569 "job-originating-user-name=\"%s\"",
1570 job
->id
, attr
->values
[0].string
.text
,
1571 attr
->values
[1].string
.text
, job
->username
);
1573 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1574 strcmp(attr
->values
[0].string
.text
, "none") &&
1575 (attr
->num_values
== 1 ||
1576 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1577 strcmp(attr
->values
[1].string
.text
, "none"))))
1579 if (attr
->num_values
== 1)
1580 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1581 "[Job %d] CLASSIFICATION OVERRIDDEN "
1582 "job-sheets=\"%s\", "
1583 "job-originating-user-name=\"%s\"",
1584 job
->id
, attr
->values
[0].string
.text
, job
->username
);
1586 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1587 "[Job %d] CLASSIFICATION OVERRIDDEN "
1588 "job-sheets=\"%s,%s\",fffff "
1589 "job-originating-user-name=\"%s\"",
1590 job
->id
, attr
->values
[0].string
.text
,
1591 attr
->values
[1].string
.text
, job
->username
);
1594 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1595 (attr
->num_values
== 1 ||
1596 strcmp(attr
->values
[1].string
.text
, Classification
)))
1599 * Force the banner to have the classification on it...
1602 if (attr
->num_values
> 1 &&
1603 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1605 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1606 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1610 if (attr
->num_values
== 1 ||
1611 strcmp(attr
->values
[0].string
.text
, "none"))
1612 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1614 if (attr
->num_values
> 1 &&
1615 strcmp(attr
->values
[1].string
.text
, "none"))
1616 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1619 if (attr
->num_values
> 1)
1620 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1621 "[Job %d] CLASSIFICATION FORCED "
1622 "job-sheets=\"%s,%s\", "
1623 "job-originating-user-name=\"%s\"",
1624 job
->id
, attr
->values
[0].string
.text
,
1625 attr
->values
[1].string
.text
, job
->username
);
1627 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1628 "[Job %d] CLASSIFICATION FORCED "
1629 "job-sheets=\"%s\", "
1630 "job-originating-user-name=\"%s\"",
1631 job
->id
, Classification
, job
->username
);
1636 * See if we need to add the starting sheet...
1639 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1641 cupsdLogMessage(CUPSD_LOG_INFO
,
1642 "Adding start banner page \"%s\" to job %d.",
1643 attr
->values
[0].string
.text
, job
->id
);
1645 kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
);
1647 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1650 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1651 IPP_TAG_ZERO
)) != NULL
)
1655 * Fill in the response info...
1658 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1659 LocalPort
, job
->id
);
1661 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1664 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1666 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1668 add_job_state_reasons(con
, job
);
1670 con
->response
->request
.status
.status_code
= IPP_OK
;
1673 * Add any job subscriptions...
1676 add_job_subscriptions(con
, job
);
1679 * Set all but the first two attributes to the job attributes group...
1682 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1683 attr
->group_tag
= IPP_TAG_JOB
;
1686 * Fire the "job created" event...
1689 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1692 * Return the new job...
1700 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1701 * upon the job and printer state...
1705 add_job_state_reasons(
1706 cupsd_client_t
*con
, /* I - Client connection */
1707 cupsd_job_t
*job
) /* I - Job info */
1709 cupsd_printer_t
*dest
; /* Destination printer */
1712 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1713 con
, con
->http
.fd
, job
? job
->id
: 0);
1715 switch (job
? job
->state_value
: IPP_JOB_CANCELLED
)
1717 case IPP_JOB_PENDING
:
1718 dest
= cupsdFindDest(job
->dest
);
1720 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1721 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1722 "job-state-reasons", NULL
, "printer-stopped");
1724 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1725 "job-state-reasons", NULL
, "none");
1729 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1730 IPP_TAG_KEYWORD
) != NULL
||
1731 ippFindAttribute(job
->attrs
, "job-hold-until",
1732 IPP_TAG_NAME
) != NULL
)
1733 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1734 "job-state-reasons", NULL
, "job-hold-until-specified");
1736 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1737 "job-state-reasons", NULL
, "job-incoming");
1740 case IPP_JOB_PROCESSING
:
1741 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1742 "job-state-reasons", NULL
, "job-printing");
1745 case IPP_JOB_STOPPED
:
1746 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1747 "job-state-reasons", NULL
, "job-stopped");
1750 case IPP_JOB_CANCELLED
:
1751 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1752 "job-state-reasons", NULL
, "job-canceled-by-user");
1755 case IPP_JOB_ABORTED
:
1756 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1757 "job-state-reasons", NULL
, "aborted-by-system");
1760 case IPP_JOB_COMPLETED
:
1761 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1762 "job-state-reasons", NULL
, "job-completed-successfully");
1769 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1773 add_job_subscriptions(
1774 cupsd_client_t
*con
, /* I - Client connection */
1775 cupsd_job_t
*job
) /* I - Newly created job */
1777 int i
; /* Looping var */
1778 ipp_attribute_t
*prev
, /* Previous attribute */
1779 *next
, /* Next attribute */
1780 *attr
; /* Current attribute */
1781 cupsd_subscription_t
*sub
; /* Subscription object */
1782 const char *recipient
, /* notify-recipient-uri */
1783 *pullmethod
; /* notify-pull-method */
1784 ipp_attribute_t
*user_data
; /* notify-user-data */
1785 int interval
; /* notify-time-interval */
1786 unsigned mask
; /* notify-events */
1790 * Find the first subscription group attribute; return if we have
1794 for (attr
= job
->attrs
->attrs
, prev
= NULL
;
1796 prev
= attr
, attr
= attr
->next
)
1797 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1804 * Process the subscription attributes in the request...
1813 mask
= CUPSD_EVENT_NONE
;
1815 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1817 if (!strcmp(attr
->name
, "notify-recipient") &&
1818 attr
->value_tag
== IPP_TAG_URI
)
1819 recipient
= attr
->values
[0].string
.text
;
1820 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1821 attr
->value_tag
== IPP_TAG_KEYWORD
)
1822 pullmethod
= attr
->values
[0].string
.text
;
1823 else if (!strcmp(attr
->name
, "notify-charset") &&
1824 attr
->value_tag
== IPP_TAG_CHARSET
&&
1825 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
1826 strcmp(attr
->values
[0].string
.text
, "utf-8"))
1828 send_ipp_status(con
, IPP_CHARSET
,
1829 _("Character set \"%s\" not supported!"),
1830 attr
->values
[0].string
.text
);
1833 else if (!strcmp(attr
->name
, "notify-natural-language") &&
1834 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
1835 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
1837 send_ipp_status(con
, IPP_CHARSET
,
1838 _("Language \"%s\" not supported!"),
1839 attr
->values
[0].string
.text
);
1842 else if (!strcmp(attr
->name
, "notify-user-data") &&
1843 attr
->value_tag
== IPP_TAG_STRING
)
1845 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
1847 send_ipp_status(con
, IPP_REQUEST_VALUE
,
1848 _("The notify-user-data value is too large "
1849 "(%d > 63 octets)!"),
1850 attr
->values
[0].unknown
.length
);
1856 else if (!strcmp(attr
->name
, "notify-events") &&
1857 attr
->value_tag
== IPP_TAG_KEYWORD
)
1859 for (i
= 0; i
< attr
->num_values
; i
++)
1860 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
1862 else if (!strcmp(attr
->name
, "notify-lease-duration"))
1864 send_ipp_status(con
, IPP_BAD_REQUEST
,
1865 _("The notify-lease-duration attribute cannot be "
1866 "used with job subscriptions."));
1869 else if (!strcmp(attr
->name
, "notify-time-interval") &&
1870 attr
->value_tag
== IPP_TAG_INTEGER
)
1871 interval
= attr
->values
[0].integer
;
1876 if (!recipient
&& !pullmethod
)
1879 if (mask
== CUPSD_EVENT_NONE
)
1880 mask
= CUPSD_EVENT_JOB_COMPLETED
;
1882 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
1885 sub
->interval
= interval
;
1887 cupsdSetString(&sub
->owner
, job
->username
);
1891 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
1892 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
1893 sub
->user_data_len
);
1896 ippAddSeparator(con
->response
);
1897 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
1898 "notify-subscription-id", sub
->id
);
1904 cupsdSaveAllSubscriptions();
1907 * Remove all of the subscription attributes from the job request...
1910 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
1914 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
1915 attr
->group_tag
== IPP_TAG_ZERO
)
1918 * Free and remove this attribute...
1926 job
->attrs
->attrs
= next
;
1932 job
->attrs
->last
= prev
;
1933 job
->attrs
->current
= prev
;
1938 * 'add_job_uuid()' - Add job-uuid attribute to a job.
1940 * See RFC 4122 for the definition of UUIDs and the format.
1944 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
1945 cupsd_job_t
*job
) /* I - Job */
1947 char uuid
[1024]; /* job-uuid string */
1948 ipp_attribute_t
*attr
; /* job-uuid attribute */
1949 _cups_md5_state_t md5state
; /* MD5 state */
1950 unsigned char md5sum
[16]; /* MD5 digest/sum */
1954 * First see if the job already has a job-uuid attribute; if so, return...
1957 if ((attr
= ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
)) != NULL
)
1961 * No job-uuid attribute, so build a version 3 UUID with the local job
1962 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
1963 * the ServerName, server name and port that the client connected to,
1964 * and local job ID...
1967 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
1968 con
->serverport
, job
->id
);
1970 _cupsMD5Init(&md5state
);
1971 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
1972 _cupsMD5Finish(&md5state
, md5sum
);
1975 * Format the UUID URI using the MD5 sum and job ID.
1978 snprintf(uuid
, sizeof(uuid
),
1979 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
1980 "%02x%02x%02x%02x%02x%02x",
1981 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
1982 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
1983 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
1984 md5sum
[14], md5sum
[15]);
1986 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
1991 * 'add_printer()' - Add a printer to the system.
1995 add_printer(cupsd_client_t
*con
, /* I - Client connection */
1996 ipp_attribute_t
*uri
) /* I - URI of printer */
1998 http_status_t status
; /* Policy status */
1999 int i
; /* Looping var */
2000 char method
[HTTP_MAX_URI
], /* Method portion of URI */
2001 username
[HTTP_MAX_URI
], /* Username portion of URI */
2002 host
[HTTP_MAX_URI
], /* Host portion of URI */
2003 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2004 int port
; /* Port portion of URI */
2005 cupsd_printer_t
*printer
; /* Printer/class */
2006 ipp_attribute_t
*attr
; /* Printer attribute */
2007 cups_file_t
*fp
; /* Script/PPD file */
2008 char line
[1024]; /* Line from file... */
2009 char srcfile
[1024], /* Source Script/PPD file */
2010 dstfile
[1024]; /* Destination Script/PPD file */
2011 int modify
; /* Non-zero if we are modifying */
2012 char newname
[IPP_MAX_NAME
]; /* New printer name */
2013 int need_restart_job
; /* Need to restart job? */
2016 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2017 con
->http
.fd
, uri
->values
[0].string
.text
);
2020 * Do we have a valid URI?
2023 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2024 sizeof(method
), username
, sizeof(username
), host
,
2025 sizeof(host
), &port
, resource
, sizeof(resource
));
2027 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2030 * No, return an error...
2033 send_ipp_status(con
, IPP_BAD_REQUEST
,
2034 _("The printer-uri must be of the form "
2035 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2040 * Do we have a valid printer name?
2043 if (!validate_name(resource
+ 10))
2046 * No, return an error...
2049 send_ipp_status(con
, IPP_BAD_REQUEST
,
2050 _("The printer-uri \"%s\" contains invalid characters."),
2051 uri
->values
[0].string
.text
);
2059 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2061 send_http_error(con
, status
);
2066 * See if the printer already exists; if not, create a new printer...
2069 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2072 * Printer doesn't exist; see if we have a class of the same name...
2075 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2076 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2079 * Yes, return an error...
2082 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2083 _("A class named \"%s\" already exists!"),
2089 * No, add the printer...
2092 printer
= cupsdAddPrinter(resource
+ 10);
2095 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2098 * Rename the implicit printer to "AnyPrinter" or delete it...
2101 if (ImplicitAnyClasses
)
2103 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2104 cupsdRenamePrinter(printer
, newname
);
2107 cupsdDeletePrinter(printer
, 1);
2110 * Add the printer as a new local printer...
2113 printer
= cupsdAddPrinter(resource
+ 10);
2116 else if (printer
->type
& CUPS_PRINTER_REMOTE
)
2119 * Rename the remote printer to "Printer@server"...
2122 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2124 cupsdRenamePrinter(printer
, newname
);
2127 * Add the printer as a new local printer...
2130 printer
= cupsdAddPrinter(resource
+ 10);
2137 * Look for attributes and copy them over as needed...
2140 need_restart_job
= 0;
2142 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2143 IPP_TAG_TEXT
)) != NULL
)
2144 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2146 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2147 IPP_TAG_TEXT
)) != NULL
)
2148 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2150 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2151 IPP_TAG_URI
)) != NULL
)
2154 * Do we have a valid device URI?
2157 need_restart_job
= 1;
2159 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, method
,
2160 sizeof(method
), username
, sizeof(username
), host
,
2161 sizeof(host
), &port
, resource
, sizeof(resource
));
2163 if (!strcmp(method
, "file"))
2166 * See if the administrator has enabled file devices...
2169 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2172 * File devices are disabled and the URL is not file:/dev/null...
2175 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2176 _("File device URIs have been disabled! "
2177 "To enable, see the FileDevice directive in "
2178 "\"%s/cupsd.conf\"."),
2186 * See if the backend exists and is executable...
2189 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, method
);
2190 if (access(srcfile
, X_OK
))
2193 * Could not find device in list!
2196 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2197 attr
->values
[0].string
.text
);
2202 cupsdLogMessage(CUPSD_LOG_INFO
,
2203 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2205 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2207 cupsdSanitizeURI(printer
->device_uri
, resource
,
2210 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2213 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2214 IPP_TAG_KEYWORD
)) != NULL
)
2216 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2219 need_restart_job
= 1;
2221 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2223 for (i
= 0; i
< supported
->num_values
; i
++)
2224 if (!strcmp(supported
->values
[i
].string
.text
,
2225 attr
->values
[0].string
.text
))
2228 if (i
>= supported
->num_values
)
2230 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2231 attr
->values
[0].string
.text
);
2235 cupsdLogMessage(CUPSD_LOG_INFO
,
2236 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2237 printer
->name
, attr
->values
[0].string
.text
,
2238 printer
->port_monitor
);
2240 if (strcmp(attr
->values
[0].string
.text
, "none"))
2241 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2243 cupsdClearString(&printer
->port_monitor
);
2246 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2247 IPP_TAG_BOOLEAN
)) != NULL
)
2249 cupsdLogMessage(CUPSD_LOG_INFO
,
2250 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2251 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2253 printer
->accepting
= attr
->values
[0].boolean
;
2254 cupsdAddPrinterHistory(printer
);
2257 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2258 IPP_TAG_BOOLEAN
)) != NULL
)
2260 if (printer
->shared
&& !attr
->values
[0].boolean
)
2261 cupsdSendBrowseDelete(printer
);
2263 cupsdLogMessage(CUPSD_LOG_INFO
,
2264 "Setting %s printer-is-shared to %d (was %d.)",
2265 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2267 printer
->shared
= attr
->values
[0].boolean
;
2270 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2271 IPP_TAG_ENUM
)) != NULL
)
2273 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2274 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2276 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2277 attr
->values
[0].integer
);
2281 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)", printer
->name
,
2282 attr
->values
[0].integer
, printer
->state
);
2284 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2285 cupsdStopPrinter(printer
, 0);
2288 need_restart_job
= 1;
2289 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2292 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2293 IPP_TAG_TEXT
)) != NULL
)
2295 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2296 sizeof(printer
->state_message
));
2297 cupsdAddPrinterHistory(printer
);
2300 set_printer_defaults(con
, printer
);
2303 * See if we have all required attributes...
2306 if (!printer
->device_uri
)
2307 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2310 * See if we have an interface script or PPD file attached to the request...
2315 need_restart_job
= 1;
2317 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2319 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2322 * Yes; get the first line from it...
2326 cupsFileGets(fp
, line
, sizeof(line
));
2330 * Then see what kind of file it is...
2333 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2336 if (!strncmp(line
, "*PPD-Adobe", 10))
2339 * The new file is a PPD file, so remove any old interface script
2340 * that might be lying around...
2348 * This must be an interface script, so move the file over to the
2349 * interfaces directory and make it executable...
2352 if (copy_file(srcfile
, dstfile
))
2354 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2355 _("Unable to copy interface script - %s!"),
2361 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2362 "Copied interface script successfully!");
2363 chmod(dstfile
, 0755);
2367 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2370 if (!strncmp(line
, "*PPD-Adobe", 10))
2373 * The new file is a PPD file, so move the file over to the
2374 * ppd directory and make it readable by all...
2377 if (copy_file(srcfile
, dstfile
))
2379 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2380 _("Unable to copy PPD file - %s!"),
2386 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2387 "Copied PPD file successfully!");
2388 chmod(dstfile
, 0644);
2394 * This must be an interface script, so remove any old PPD file that
2395 * may be lying around...
2402 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2403 IPP_TAG_NAME
)) != NULL
)
2405 need_restart_job
= 1;
2407 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2410 * Raw driver, remove any existing PPD or interface script files.
2413 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2417 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2427 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2431 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2434 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2436 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2441 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2442 "Copied PPD file successfully!");
2443 chmod(dstfile
, 0644);
2449 * Update the printer attributes and return...
2452 cupsdSetPrinterAttrs(printer
);
2453 cupsdSaveAllPrinters();
2455 if (need_restart_job
&& printer
->job
)
2460 * Stop the current job and then restart it below...
2463 job
= (cupsd_job_t
*)printer
->job
;
2465 cupsdStopJob(job
, 1);
2467 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2468 job
->state_value
= IPP_JOB_PENDING
;
2471 if (need_restart_job
)
2474 cupsdWritePrintcap();
2478 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2479 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2482 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2483 printer
->name
, get_username(con
));
2487 cupsdAddPrinterHistory(printer
);
2489 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2490 "New printer \"%s\" added by \"%s\".", printer
->name
,
2493 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2494 printer
->name
, get_username(con
));
2497 con
->response
->request
.status
.status_code
= IPP_OK
;
2502 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2503 * based upon the printer state...
2507 add_printer_state_reasons(
2508 cupsd_client_t
*con
, /* I - Client connection */
2509 cupsd_printer_t
*p
) /* I - Printer info */
2511 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2512 "add_printer_state_reasons(%p[%d], %p[%s])",
2513 con
, con
->http
.fd
, p
, p
->name
);
2515 if (p
->num_reasons
== 0)
2516 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2517 "printer-state-reasons", NULL
,
2518 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2520 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2521 "printer-state-reasons", p
->num_reasons
, NULL
,
2522 (const char * const *)p
->reasons
);
2527 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2528 * the specified printer or class.
2532 add_queued_job_count(
2533 cupsd_client_t
*con
, /* I - Client connection */
2534 cupsd_printer_t
*p
) /* I - Printer or class */
2536 int count
; /* Number of jobs on destination */
2539 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2540 con
, con
->http
.fd
, p
, p
->name
);
2542 count
= cupsdGetPrinterJobCount(p
->name
);
2544 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2545 "queued-job-count", count
);
2550 * 'apply_printer_defaults()' - Apply printer default options to a job.
2554 apply_printer_defaults(
2555 cupsd_printer_t
*printer
, /* I - Printer */
2556 cupsd_job_t
*job
) /* I - Job */
2558 int i
, /* Looping var */
2559 num_options
; /* Number of default options */
2560 cups_option_t
*options
, /* Default options */
2561 *option
; /* Current option */
2565 * Collect all of the default options and add the missing ones to the
2569 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
2572 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2574 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2579 * Encode these options as attributes in the job object...
2582 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2583 cupsFreeOptions(num_options
, options
);
2588 * 'authenticate_job()' - Set job authentication info.
2592 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2593 ipp_attribute_t
*uri
) /* I - Job URI */
2595 ipp_attribute_t
*attr
; /* Job-id attribute */
2596 int jobid
; /* Job ID */
2597 cupsd_job_t
*job
; /* Current job */
2598 char method
[HTTP_MAX_URI
],
2599 /* Method portion of URI */
2600 username
[HTTP_MAX_URI
],
2601 /* Username portion of URI */
2603 /* Host portion of URI */
2604 resource
[HTTP_MAX_URI
];
2605 /* Resource portion of URI */
2606 int port
; /* Port portion of URI */
2609 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2610 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
2613 * Start with "everything is OK" status...
2616 con
->response
->request
.status
.status_code
= IPP_OK
;
2619 * See if we have a job URI or a printer URI...
2622 if (!strcmp(uri
->name
, "printer-uri"))
2625 * Got a printer URI; see if we also have a job-id attribute...
2628 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2629 IPP_TAG_INTEGER
)) == NULL
)
2631 send_ipp_status(con
, IPP_BAD_REQUEST
,
2632 _("Got a printer-uri attribute but no job-id!"));
2636 jobid
= attr
->values
[0].integer
;
2641 * Got a job URI; parse it to get the job ID...
2644 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2645 sizeof(method
), username
, sizeof(username
), host
,
2646 sizeof(host
), &port
, resource
, sizeof(resource
));
2648 if (strncmp(resource
, "/jobs/", 6))
2654 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
2655 uri
->values
[0].string
.text
);
2659 jobid
= atoi(resource
+ 6);
2663 * See if the job exists...
2666 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2669 * Nope - return a "not found" error...
2672 send_ipp_status(con
, IPP_NOT_FOUND
,
2673 _("Job #%d does not exist!"), jobid
);
2678 * See if the job has been completed...
2681 if (job
->state_value
!= IPP_JOB_HELD
)
2684 * Return a "not-possible" error...
2687 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2688 _("Job #%d is not held for authentication!"),
2694 * See if we have already authenticated...
2697 if (!con
->username
[0])
2699 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
2700 _("No authentication information provided!"));
2705 * See if the job is owned by the requesting user...
2708 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
2710 send_http_error(con
, HTTP_UNAUTHORIZED
);
2715 * Save the authentication information for this job...
2718 save_auth_info(con
, job
);
2721 * Reset the job-hold-until value to "no-hold"...
2724 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2725 IPP_TAG_KEYWORD
)) == NULL
)
2726 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2730 attr
->value_tag
= IPP_TAG_KEYWORD
;
2731 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2735 * Release the job and return...
2738 cupsdReleaseJob(job
);
2740 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was authenticated by \"%s\".", jobid
,
2746 * 'cancel_all_jobs()' - Cancel all print jobs.
2750 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
2751 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2753 http_status_t status
; /* Policy status */
2754 const char *dest
; /* Destination */
2755 cups_ptype_t dtype
; /* Destination type */
2756 char method
[HTTP_MAX_URI
], /* Method portion of URI */
2757 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2758 host
[HTTP_MAX_URI
], /* Host portion of URI */
2759 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2760 int port
; /* Port portion of URI */
2761 ipp_attribute_t
*attr
; /* Attribute in request */
2762 const char *username
; /* Username */
2763 int purge
; /* Purge? */
2764 cupsd_printer_t
*printer
; /* Printer */
2767 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
2768 con
->http
.fd
, uri
->values
[0].string
.text
);
2771 * See if we have a printer URI...
2774 if (strcmp(uri
->name
, "printer-uri"))
2776 send_ipp_status(con
, IPP_BAD_REQUEST
,
2777 _("The printer-uri attribute is required!"));
2782 * Get the username (if any) for the jobs we want to cancel (only if
2783 * "my-jobs" is specified...
2786 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
2787 IPP_TAG_BOOLEAN
)) != NULL
&&
2788 attr
->values
[0].boolean
)
2790 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
2791 IPP_TAG_NAME
)) != NULL
)
2792 username
= attr
->values
[0].string
.text
;
2795 send_ipp_status(con
, IPP_BAD_REQUEST
,
2796 _("Missing requesting-user-name attribute!"));
2804 * Look for the "purge-jobs" attribute...
2807 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
2808 IPP_TAG_BOOLEAN
)) != NULL
)
2809 purge
= attr
->values
[0].boolean
;
2814 * And if the destination is valid...
2817 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2818 sizeof(method
), userpass
, sizeof(userpass
), host
,
2819 sizeof(host
), &port
, resource
, sizeof(resource
));
2821 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
2827 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
2828 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
2830 send_ipp_status(con
, IPP_NOT_FOUND
,
2831 _("The printer or class was not found."));
2834 else if (strcmp(resource
, "/printers/"))
2836 send_ipp_status(con
, IPP_NOT_FOUND
,
2837 _("The printer-uri \"%s\" is not valid."),
2838 uri
->values
[0].string
.text
);
2846 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2848 send_http_error(con
, status
);
2853 * Cancel all jobs on all printers...
2856 cupsdCancelJobs(NULL
, username
, purge
);
2858 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
2859 purge
? "purged" : "cancelled", get_username(con
));
2867 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
2869 send_http_error(con
, status
);
2874 * Cancel all of the jobs on the named printer...
2877 cupsdCancelJobs(dest
, username
, purge
);
2879 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
2880 dest
, purge
? "purged" : "cancelled", get_username(con
));
2883 con
->response
->request
.status
.status_code
= IPP_OK
;
2888 * 'cancel_job()' - Cancel a print job.
2892 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
2893 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
2895 ipp_attribute_t
*attr
; /* Current attribute */
2896 int jobid
; /* Job ID */
2897 char method
[HTTP_MAX_URI
], /* Method portion of URI */
2898 username
[HTTP_MAX_URI
], /* Username portion of URI */
2899 host
[HTTP_MAX_URI
], /* Host portion of URI */
2900 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2901 int port
; /* Port portion of URI */
2902 cupsd_job_t
*job
; /* Job information */
2903 const char *dest
; /* Destination */
2904 cups_ptype_t dtype
; /* Destination type (printer or class) */
2905 cupsd_printer_t
*printer
; /* Printer data */
2908 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
2909 con
->http
.fd
, uri
->values
[0].string
.text
);
2912 * See if we have a job URI or a printer URI...
2915 if (!strcmp(uri
->name
, "printer-uri"))
2918 * Got a printer URI; see if we also have a job-id attribute...
2921 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2922 IPP_TAG_INTEGER
)) == NULL
)
2924 send_ipp_status(con
, IPP_BAD_REQUEST
,
2925 _("Got a printer-uri attribute but no job-id!"));
2929 if ((jobid
= attr
->values
[0].integer
) == 0)
2932 * Find the current job on the specified printer...
2935 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2936 sizeof(method
), username
, sizeof(username
), host
,
2937 sizeof(host
), &port
, resource
, sizeof(resource
));
2939 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
2945 send_ipp_status(con
, IPP_NOT_FOUND
,
2946 _("The printer or class was not found."));
2951 * See if the printer is currently printing a job...
2955 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
2959 * No, see if there are any pending jobs...
2962 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
2964 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
2965 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
2966 !strcasecmp(job
->dest
, dest
))
2973 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
2983 * Got a job URI; parse it to get the job ID...
2986 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
2987 sizeof(method
), username
, sizeof(username
), host
,
2988 sizeof(host
), &port
, resource
, sizeof(resource
));
2990 if (strncmp(resource
, "/jobs/", 6))
2996 send_ipp_status(con
, IPP_BAD_REQUEST
,
2997 _("Bad job-uri attribute \"%s\"!"),
2998 uri
->values
[0].string
.text
);
3002 jobid
= atoi(resource
+ 6);
3006 * See if the job exists...
3009 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3012 * Nope - return a "not found" error...
3015 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3020 * See if the job is owned by the requesting user...
3023 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3025 send_http_error(con
, HTTP_UNAUTHORIZED
);
3030 * See if the job is already completed, cancelled, or aborted; if so,
3031 * we can't cancel...
3034 if (job
->state_value
>= IPP_JOB_CANCELLED
)
3036 switch (job
->state_value
)
3038 case IPP_JOB_CANCELLED
:
3039 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3040 _("Job #%d is already cancelled - can\'t cancel."),
3044 case IPP_JOB_ABORTED
:
3045 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3046 _("Job #%d is already aborted - can\'t cancel."),
3051 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3052 _("Job #%d is already completed - can\'t cancel."),
3061 * Cancel the job and return...
3064 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3065 "Job cancelled by \"%s\".", username
);
3067 cupsdCancelJob(job
, 0);
3070 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was cancelled by \"%s\".", jobid
,
3073 con
->response
->request
.status
.status_code
= IPP_OK
;
3078 * 'cancel_subscription()' - Cancel a subscription.
3082 cancel_subscription(
3083 cupsd_client_t
*con
, /* I - Client connection */
3084 int sub_id
) /* I - Subscription ID */
3086 http_status_t status
; /* Policy status */
3087 cupsd_subscription_t
*sub
; /* Subscription */
3090 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3091 "cancel_subscription(con=%p[%d], sub_id=%d)",
3092 con
, con
->http
.fd
, sub_id
);
3095 * Is the subscription ID valid?
3098 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3101 * Bad subscription ID...
3104 send_ipp_status(con
, IPP_NOT_FOUND
,
3105 _("notify-subscription-id %d no good!"), sub_id
);
3113 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3115 con
, sub
->owner
)) != HTTP_OK
)
3117 send_http_error(con
, status
);
3122 * Cancel the subscription...
3125 cupsdDeleteSubscription(sub
, 1);
3127 con
->response
->request
.status
.status_code
= IPP_OK
;
3132 * 'check_quotas()' - Check quotas for a printer and user.
3135 static int /* O - 1 if OK, 0 if not */
3136 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3137 cupsd_printer_t
*p
) /* I - Printer or class */
3139 int i
; /* Looping var */
3140 char username
[33]; /* Username */
3141 cupsd_quota_t
*q
; /* Quota data */
3142 struct passwd
*pw
; /* User password data */
3145 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3146 con
, con
->http
.fd
, p
, p
->name
);
3156 * Figure out who is printing...
3159 strlcpy(username
, get_username(con
), sizeof(username
));
3162 * Check global active job limits for printers and users...
3165 if (MaxJobsPerPrinter
)
3168 * Check if there are too many pending jobs on this printer...
3171 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3173 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3182 * Check if there are too many pending jobs for this user...
3185 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3187 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3194 * Check against users...
3197 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3202 pw
= getpwnam(username
);
3205 for (i
= 0; i
< p
->num_users
; i
++)
3206 if (p
->users
[i
][0] == '@')
3209 * Check group membership...
3212 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3215 else if (!strcasecmp(username
, p
->users
[i
]))
3218 if ((i
< p
->num_users
) == p
->deny_users
)
3220 cupsdLogMessage(CUPSD_LOG_INFO
,
3221 "Denying user \"%s\" access to printer \"%s\"...",
3231 if (p
->k_limit
|| p
->page_limit
)
3233 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3235 cupsdLogMessage(CUPSD_LOG_ERROR
,
3236 "Unable to allocate quota data for user \"%s\"!",
3241 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3242 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3244 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3251 * If we have gotten this far, we're done!
3259 * 'copy_attribute()' - Copy a single attribute.
3262 static ipp_attribute_t
* /* O - New attribute */
3264 ipp_t
*to
, /* O - Destination request/response */
3265 ipp_attribute_t
*attr
, /* I - Attribute to copy */
3266 int quickcopy
) /* I - Do a quick copy? */
3268 int i
; /* Looping var */
3269 ipp_attribute_t
*toattr
; /* Destination attribute */
3272 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3273 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
3274 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
3277 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
3280 toattr
= ippAddSeparator(to
);
3283 case IPP_TAG_INTEGER
:
3285 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3286 attr
->name
, attr
->num_values
, NULL
);
3288 for (i
= 0; i
< attr
->num_values
; i
++)
3289 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
3292 case IPP_TAG_BOOLEAN
:
3293 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
3294 attr
->num_values
, NULL
);
3296 for (i
= 0; i
< attr
->num_values
; i
++)
3297 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
3300 case IPP_TAG_STRING
:
3303 case IPP_TAG_KEYWORD
:
3305 case IPP_TAG_URISCHEME
:
3306 case IPP_TAG_CHARSET
:
3307 case IPP_TAG_LANGUAGE
:
3308 case IPP_TAG_MIMETYPE
:
3309 toattr
= ippAddStrings(to
, attr
->group_tag
,
3310 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3311 attr
->name
, attr
->num_values
, NULL
, NULL
);
3315 for (i
= 0; i
< attr
->num_values
; i
++)
3316 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3320 for (i
= 0; i
< attr
->num_values
; i
++)
3321 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3326 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
3327 attr
->values
[0].date
);
3330 case IPP_TAG_RESOLUTION
:
3331 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
3332 attr
->num_values
, IPP_RES_PER_INCH
,
3335 for (i
= 0; i
< attr
->num_values
; i
++)
3337 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
3338 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
3339 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
3343 case IPP_TAG_RANGE
:
3344 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
3345 attr
->num_values
, NULL
, NULL
);
3347 for (i
= 0; i
< attr
->num_values
; i
++)
3349 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
3350 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
3354 case IPP_TAG_TEXTLANG
:
3355 case IPP_TAG_NAMELANG
:
3356 toattr
= ippAddStrings(to
, attr
->group_tag
,
3357 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
3358 attr
->name
, attr
->num_values
, NULL
, NULL
);
3362 for (i
= 0; i
< attr
->num_values
; i
++)
3364 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
3365 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
3370 for (i
= 0; i
< attr
->num_values
; i
++)
3373 toattr
->values
[i
].string
.charset
=
3374 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
3376 toattr
->values
[i
].string
.charset
=
3377 toattr
->values
[0].string
.charset
;
3379 toattr
->values
[i
].string
.text
= _cupsStrAlloc(attr
->values
[i
].string
.text
);
3384 case IPP_TAG_BEGIN_COLLECTION
:
3385 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
3386 attr
->num_values
, NULL
);
3388 for (i
= 0; i
< attr
->num_values
; i
++)
3390 toattr
->values
[i
].collection
= ippNew();
3391 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
3392 NULL
, IPP_TAG_ZERO
, 0);
3397 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
3398 attr
->name
, attr
->num_values
, NULL
);
3400 for (i
= 0; i
< attr
->num_values
; i
++)
3402 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
3404 if (toattr
->values
[i
].unknown
.length
> 0)
3406 if ((toattr
->values
[i
].unknown
.data
=
3407 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
3408 toattr
->values
[i
].unknown
.length
= 0;
3410 memcpy(toattr
->values
[i
].unknown
.data
,
3411 attr
->values
[i
].unknown
.data
,
3412 toattr
->values
[i
].unknown
.length
);
3415 break; /* anti-compiler-warning-code */
3423 * 'copy_attrs()' - Copy attributes from one request to another.
3427 copy_attrs(ipp_t
*to
, /* I - Destination request */
3428 ipp_t
*from
, /* I - Source request */
3429 cups_array_t
*ra
, /* I - Requested attributes */
3430 ipp_tag_t group
, /* I - Group to copy */
3431 int quickcopy
) /* I - Do a quick copy? */
3433 ipp_attribute_t
*fromattr
; /* Source attribute */
3436 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3437 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3438 to
, from
, ra
, group
, quickcopy
);
3443 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3446 * Filter attributes as needed...
3449 if (group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3450 fromattr
->group_tag
!= IPP_TAG_ZERO
&& !fromattr
->name
)
3453 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
3454 copy_attribute(to
, fromattr
, quickcopy
);
3460 * 'copy_banner()' - Copy a banner file to the requests directory for the
3464 static int /* O - Size of banner file in kbytes */
3465 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
3466 cupsd_job_t
*job
, /* I - Job information */
3467 const char *name
) /* I - Name of banner */
3469 int i
; /* Looping var */
3470 int kbytes
; /* Size of banner file in kbytes */
3471 char filename
[1024]; /* Job filename */
3472 cupsd_banner_t
*banner
; /* Pointer to banner */
3473 cups_file_t
*in
; /* Input file */
3474 cups_file_t
*out
; /* Output file */
3475 int ch
; /* Character from file */
3476 char attrname
[255], /* Name of attribute */
3477 *s
; /* Pointer into name */
3478 ipp_attribute_t
*attr
; /* Attribute */
3481 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
3482 con
, con
->http
.fd
, job
, job
->id
, name
? name
: "(null)");
3485 * Find the banner; return if not found or "none"...
3488 if (!name
|| !strcmp(name
, "none") ||
3489 (banner
= cupsdFindBanner(name
)) == NULL
)
3493 * Open the banner and job files...
3496 if (add_file(con
, job
, banner
->filetype
, 0))
3499 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
3501 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
3503 cupsdLogMessage(CUPSD_LOG_ERROR
,
3504 "copy_banner: Unable to create banner job file %s - %s",
3505 filename
, strerror(errno
));
3510 fchmod(cupsFileNumber(out
), 0640);
3511 fchown(cupsFileNumber(out
), RunUser
, Group
);
3514 * Try the localized banner file under the subdirectory...
3517 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
3519 if (strlen(attrname
) > 2 && attrname
[2] == '-')
3522 * Convert ll-cc to ll_CC...
3526 attrname
[3] = toupper(attrname
[3] & 255);
3527 attrname
[4] = toupper(attrname
[4] & 255);
3530 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3533 if (access(filename
, 0) && strlen(attrname
) > 2)
3536 * Wasn't able to find "ll_CC" locale file; try the non-national
3537 * localization banner directory.
3542 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
3546 if (access(filename
, 0))
3549 * Use the non-localized banner file.
3552 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
3555 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
3559 cupsdLogMessage(CUPSD_LOG_ERROR
,
3560 "copy_banner: Unable to open banner template file %s - %s",
3561 filename
, strerror(errno
));
3567 * Parse the file to the end...
3570 while ((ch
= cupsFileGetChar(in
)) != EOF
)
3574 * Get an attribute name...
3577 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
3578 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
3580 else if (s
< (attrname
+ sizeof(attrname
) - 1))
3590 * Ignore { followed by stuff that is not an attribute name...
3593 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
3598 * See if it is defined...
3601 if (attrname
[0] == '?')
3606 if (!strcmp(s
, "printer-name"))
3608 cupsFilePuts(out
, job
->dest
);
3611 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
3614 * See if we have a leading question mark...
3617 if (attrname
[0] != '?')
3620 * Nope, write to file as-is; probably a PostScript procedure...
3623 cupsFilePrintf(out
, "{%s}", attrname
);
3630 * Output value(s)...
3633 for (i
= 0; i
< attr
->num_values
; i
++)
3636 cupsFilePutChar(out
, ',');
3638 switch (attr
->value_tag
)
3640 case IPP_TAG_INTEGER
:
3642 if (!strncmp(s
, "time-at-", 8))
3643 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
3645 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
3648 case IPP_TAG_BOOLEAN
:
3649 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
3652 case IPP_TAG_NOVALUE
:
3653 cupsFilePuts(out
, "novalue");
3656 case IPP_TAG_RANGE
:
3657 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
3658 attr
->values
[i
].range
.upper
);
3661 case IPP_TAG_RESOLUTION
:
3662 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3663 attr
->values
[i
].resolution
.yres
,
3664 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3669 case IPP_TAG_STRING
:
3672 case IPP_TAG_KEYWORD
:
3673 case IPP_TAG_CHARSET
:
3674 case IPP_TAG_LANGUAGE
:
3675 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
3678 * Need to quote strings for PS banners...
3683 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
3685 if (*p
== '(' || *p
== ')' || *p
== '\\')
3687 cupsFilePutChar(out
, '\\');
3688 cupsFilePutChar(out
, *p
);
3690 else if (*p
< 32 || *p
> 126)
3691 cupsFilePrintf(out
, "\\%03o", *p
& 255);
3693 cupsFilePutChar(out
, *p
);
3697 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
3701 break; /* anti-compiler-warning-code */
3705 else if (ch
== '\\') /* Quoted char */
3707 ch
= cupsFileGetChar(in
);
3709 if (ch
!= '{') /* Only do special handling for \{ */
3710 cupsFilePutChar(out
, '\\');
3712 cupsFilePutChar(out
, ch
);
3715 cupsFilePutChar(out
, ch
);
3719 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
3721 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
3722 IPP_TAG_INTEGER
)) != NULL
)
3723 attr
->values
[0].integer
+= kbytes
;
3732 * 'copy_file()' - Copy a PPD file or interface script...
3735 static int /* O - 0 = success, -1 = error */
3736 copy_file(const char *from
, /* I - Source file */
3737 const char *to
) /* I - Destination file */
3739 cups_file_t
*src
, /* Source file */
3740 *dst
; /* Destination file */
3741 int bytes
; /* Bytes to read/write */
3742 char buffer
[2048]; /* Copy buffer */
3745 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
3748 * Open the source and destination file for a copy...
3751 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
3754 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
3761 * Copy the source file to the destination...
3764 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
3765 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
3773 * Close both files and return...
3778 return (cupsFileClose(dst
));
3783 * 'copy_model()' - Copy a PPD model file, substituting default values
3787 static int /* O - 0 = success, -1 = error */
3788 copy_model(cupsd_client_t
*con
, /* I - Client connection */
3789 const char *from
, /* I - Source file */
3790 const char *to
) /* I - Destination file */
3792 fd_set
*input
; /* select() input set */
3793 struct timeval timeout
; /* select() timeout */
3794 int maxfd
; /* Maximum file descriptor for select() */
3795 char tempfile
[1024]; /* Temporary PPD file */
3796 int tempfd
; /* Temporary PPD file descriptor */
3797 int temppid
; /* Process ID of cups-driverd */
3798 int temppipe
[2]; /* Temporary pipes */
3799 char *argv
[4], /* Command-line arguments */
3800 *envp
[MAX_ENV
]; /* Environment */
3801 cups_file_t
*src
, /* Source file */
3802 *dst
; /* Destination file */
3803 int bytes
, /* Bytes from pipe */
3804 total
; /* Total bytes from pipe */
3805 char buffer
[2048], /* Copy buffer */
3806 *ptr
; /* Pointer into buffer */
3807 int i
; /* Looping var */
3808 char option
[PPD_MAX_NAME
], /* Option name */
3809 choice
[PPD_MAX_NAME
]; /* Choice name */
3810 int num_defaults
; /* Number of default options */
3811 ppd_default_t
*defaults
; /* Default options */
3812 char cups_protocol
[PPD_MAX_LINE
];
3813 /* cupsProtocol attribute */
3814 int have_letter
, /* Have Letter size */
3815 have_a4
; /* Have A4 size */
3816 #ifdef HAVE_LIBPAPER
3817 char *paper_result
; /* Paper size name from libpaper */
3818 char system_paper
[64]; /* Paper size name buffer */
3819 #endif /* HAVE_LIBPAPER */
3822 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3823 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
3827 * Run cups-driverd to get the PPD file...
3830 argv
[0] = "cups-driverd";
3832 argv
[2] = (char *)from
;
3835 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3837 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
3838 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
3839 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3843 cupsdOpenPipe(temppipe
);
3845 if ((input
= calloc(1, SetSize
)) == NULL
)
3850 cupsdLogMessage(CUPSD_LOG_ERROR
,
3851 "copy_model: Unable to allocate %d bytes for select()...",
3856 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3857 "copy_model: Running \"cups-driverd cat %s\"...", from
);
3859 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
3871 * Wait up to 30 seconds for the PPD file to be copied...
3876 if (temppipe
[0] > CGIPipes
[0])
3877 maxfd
= temppipe
[0] + 1;
3879 maxfd
= CGIPipes
[0] + 1;
3884 * See if we have data ready...
3889 FD_SET(temppipe
[0], input
);
3890 FD_SET(CGIPipes
[0], input
);
3892 timeout
.tv_sec
= 30;
3893 timeout
.tv_usec
= 0;
3895 if ((i
= select(maxfd
, input
, NULL
, NULL
, &timeout
)) < 0)
3905 * We have timed out...
3911 if (FD_ISSET(temppipe
[0], input
))
3914 * Read the PPD file from the pipe, and write it to the PPD file.
3917 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
3919 if (write(tempfd
, buffer
, bytes
) < bytes
)
3928 if (FD_ISSET(CGIPipes
[0], input
))
3940 * No data from cups-deviced...
3943 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
3949 * Read the source file and see what page sizes are supported...
3952 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
3961 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
3962 if (!strncmp(buffer
, "*PageSize ", 10))
3965 * Strip UI text and command data from the end of the line...
3968 if ((ptr
= strchr(buffer
+ 10, '/')) != NULL
)
3970 if ((ptr
= strchr(buffer
+ 10, ':')) != NULL
)
3973 for (ptr
= buffer
+ 10; isspace(*ptr
); ptr
++);
3976 * Look for Letter and A4 page sizes...
3979 if (!strcmp(ptr
, "Letter"))
3982 if (!strcmp(ptr
, "A4"))
3986 cupsFileRewind(src
);
3989 * Open the destination (if possible) and set the default options...
3994 cups_protocol
[0] = '\0';
3996 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
3999 * Read all of the default lines from the old PPD...
4002 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4003 if (!strncmp(buffer
, "*Default", 8))
4006 * Add the default option...
4009 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4010 choice
, sizeof(choice
)))
4011 num_defaults
= ppd_add_default(option
, choice
, num_defaults
,
4014 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4015 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4019 #ifdef HAVE_LIBPAPER
4020 else if ((paper_result
= systempapername()) != NULL
)
4023 * Set the default media sizes from the systemwide default...
4026 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
4027 system_paper
[0] = toupper(system_paper
[0] & 255);
4029 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
4030 (!strcmp(system_paper
, "A4") && have_a4
))
4032 num_defaults
= ppd_add_default("PageSize", system_paper
,
4033 num_defaults
, &defaults
);
4034 num_defaults
= ppd_add_default("PageRegion", system_paper
,
4035 num_defaults
, &defaults
);
4036 num_defaults
= ppd_add_default("PaperDimension", system_paper
,
4037 num_defaults
, &defaults
);
4038 num_defaults
= ppd_add_default("ImageableArea", system_paper
,
4039 num_defaults
, &defaults
);
4042 #endif /* HAVE_LIBPAPER */
4046 * Add the default media sizes...
4048 * Note: These values are generally not valid for large-format devices
4049 * like plotters, however it is probably safe to say that those
4050 * users will configure the media size after initially adding
4051 * the device anyways...
4054 if (!DefaultLanguage
||
4055 !strcasecmp(DefaultLanguage
, "C") ||
4056 !strcasecmp(DefaultLanguage
, "POSIX") ||
4057 !strcasecmp(DefaultLanguage
, "en") ||
4058 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4059 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4060 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4063 * These are the only locales that will default to "letter" size...
4068 num_defaults
= ppd_add_default("PageSize", "Letter", num_defaults
,
4070 num_defaults
= ppd_add_default("PageRegion", "Letter", num_defaults
,
4072 num_defaults
= ppd_add_default("PaperDimension", "Letter", num_defaults
,
4074 num_defaults
= ppd_add_default("ImageableArea", "Letter", num_defaults
,
4081 * The rest default to "a4" size...
4084 num_defaults
= ppd_add_default("PageSize", "A4", num_defaults
,
4086 num_defaults
= ppd_add_default("PageRegion", "A4", num_defaults
,
4088 num_defaults
= ppd_add_default("PaperDimension", "A4", num_defaults
,
4090 num_defaults
= ppd_add_default("ImageableArea", "A4", num_defaults
,
4096 * Open the destination file for a copy...
4099 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4101 if (num_defaults
> 0)
4110 * Copy the source file to the destination...
4113 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4115 if (!strncmp(buffer
, "*Default", 8))
4118 * Check for an previous default option choice...
4121 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4122 choice
, sizeof(choice
)))
4124 for (i
= 0; i
< num_defaults
; i
++)
4125 if (!strcmp(option
, defaults
[i
].option
))
4128 * Substitute the previous choice...
4131 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
,
4132 defaults
[i
].choice
);
4138 cupsFilePrintf(dst
, "%s\n", buffer
);
4141 if (cups_protocol
[0])
4142 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4144 if (num_defaults
> 0)
4148 * Close both files and return...
4155 return (cupsFileClose(dst
));
4160 * 'copy_job_attrs()' - Copy job attributes.
4164 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4165 cupsd_job_t
*job
, /* I - Job */
4166 cups_array_t
*ra
) /* I - Requested attributes array */
4168 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4172 * Send the requested attributes for each job...
4175 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4176 con
->servername
, con
->serverport
, "/jobs/%d",
4179 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
4180 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4181 "job-more-info", NULL
, job_uri
);
4183 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4184 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4185 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4186 job
->num_files
> 0);
4188 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
4189 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4190 "job-printer-up-time", time(NULL
));
4192 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4193 add_job_state_reasons(con
, job
);
4195 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4196 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4197 "job-uri", NULL
, job_uri
);
4199 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
4204 * 'copy_printer_attrs()' - Copy printer attributes.
4209 cupsd_client_t
*con
, /* I - Client connection */
4210 cupsd_printer_t
*printer
, /* I - Printer */
4211 cups_array_t
*ra
) /* I - Requested attributes array */
4213 char printer_uri
[HTTP_MAX_URI
];
4215 time_t curtime
; /* Current time */
4216 int i
; /* Looping var */
4217 ipp_attribute_t
*history
; /* History collection */
4221 * Copy the printer attributes to the response using requested-attributes
4222 * and document-format attributes that may be provided by the client.
4225 curtime
= time(NULL
);
4228 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
4229 printer
->recoverable
)
4230 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4231 "com.apple.print.recoverable-message", NULL
,
4232 printer
->recoverable
);
4233 #endif /* __APPLE__ */
4235 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4236 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4237 ippTimeToDate(curtime
));
4239 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4240 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4241 "printer-error-policy", NULL
, printer
->error_policy
);
4243 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4244 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
4245 printer
->accepting
);
4247 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4248 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
4251 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4252 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4253 "printer-op-policy", NULL
, printer
->op_policy
);
4255 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4256 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4259 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4260 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4261 "printer-state-change-time", printer
->state_time
);
4263 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
4264 cupsArrayFind(ra
, "printer-state-history"))
4267 * Printer history is only sent if specifically requested, so that
4268 * older CUPS/IPP clients won't barf on the collection attributes.
4271 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
4272 "printer-state-history",
4273 printer
->num_history
, NULL
);
4275 for (i
= 0; i
< printer
->num_history
; i
++)
4276 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
4277 NULL
, IPP_TAG_ZERO
, 0);
4280 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4281 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4282 "printer-state-message", NULL
, printer
->state_message
);
4284 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4285 add_printer_state_reasons(con
, printer
);
4287 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4289 int type
; /* printer-type value */
4293 * Add the CUPS-specific printer-type attribute...
4296 type
= printer
->type
;
4298 if (printer
== DefaultPrinter
)
4299 type
|= CUPS_PRINTER_DEFAULT
;
4301 if (!printer
->accepting
)
4302 type
|= CUPS_PRINTER_REJECTING
;
4304 if (!printer
->shared
)
4305 type
|= CUPS_PRINTER_NOT_SHARED
;
4307 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
4311 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4312 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4313 "printer-up-time", curtime
);
4315 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
4316 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
4319 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4320 "ipp", NULL
, con
->servername
, con
->serverport
,
4321 "/printers/%s", printer
->name
);
4322 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4323 "printer-uri-supported", NULL
, printer_uri
);
4324 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4328 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4329 add_queued_job_count(con
, printer
);
4331 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
4332 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
4337 * 'copy_subscription_attrs()' - Copy subscription attributes.
4341 copy_subscription_attrs(
4342 cupsd_client_t
*con
, /* I - Client connection */
4343 cupsd_subscription_t
*sub
, /* I - Subscription */
4344 cups_array_t
*ra
) /* I - Requested attributes array */
4346 ipp_attribute_t
*attr
; /* Current attribute */
4347 char printer_uri
[HTTP_MAX_URI
];
4349 int count
; /* Number of events */
4350 unsigned mask
; /* Current event mask */
4351 const char *name
; /* Current event name */
4355 * Copy the subscription attributes to the response using the
4356 * requested-attributes attribute that may be provided by the client.
4359 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
4361 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
4364 * Simple event list...
4367 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
4368 IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
4369 "notify-events", NULL
, name
);
4374 * Complex event list...
4377 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4378 if (sub
->mask
& mask
)
4381 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
4382 IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
4383 "notify-events", count
, NULL
, NULL
);
4385 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
4386 if (sub
->mask
& mask
)
4388 attr
->values
[count
].string
.text
=
4389 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
4396 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
4397 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4398 "notify-job-id", sub
->job
->id
);
4400 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
4401 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4402 "notify-lease-duration", sub
->lease
);
4404 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
4406 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4407 "ipp", NULL
, con
->servername
, con
->serverport
,
4408 "/printers/%s", sub
->dest
->name
);
4409 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4410 "notify-printer-uri", NULL
, printer_uri
);
4413 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
4414 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
4415 "notify-recipient-uri", NULL
, sub
->recipient
);
4416 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
4417 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
4418 "notify-pull-method", NULL
, "ippget");
4420 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
4421 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
4422 "notify-subscriber-user-name", NULL
, sub
->owner
);
4424 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
4425 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4426 "notify-subscription-id", sub
->id
);
4428 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
4429 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4430 "notify-time-interval", sub
->interval
);
4432 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
4433 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
4434 sub
->user_data
, sub
->user_data_len
);
4439 * 'create_job()' - Print a file to a printer or class.
4443 create_job(cupsd_client_t
*con
, /* I - Client connection */
4444 ipp_attribute_t
*uri
) /* I - Printer URI */
4446 cupsd_job_t
*job
; /* New job */
4449 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
4450 con
->http
.fd
, uri
->values
[0].string
.text
);
4453 * Create the job object...
4456 if ((job
= add_job(con
, uri
, NULL
, NULL
)) == NULL
)
4460 * Save and log the job...
4465 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d created on \"%s\" by \"%s\".",
4466 job
->id
, job
->dest
, job
->username
);
4471 * 'create_requested_array()' - Create an array for the requested-attributes.
4474 static cups_array_t
* /* O - Array of attributes or NULL */
4475 create_requested_array(ipp_t
*request
) /* I - IPP request */
4477 int i
; /* Looping var */
4478 ipp_attribute_t
*requested
; /* requested-attributes attribute */
4479 cups_array_t
*ra
; /* Requested attributes array */
4480 char *value
; /* Current value */
4484 * Get the requested-attributes attribute, and return NULL if we don't
4488 if ((requested
= ippFindAttribute(request
, "requested-attributes",
4489 IPP_TAG_KEYWORD
)) == NULL
)
4493 * If the attribute contains a single "all" keyword, return NULL...
4496 if (requested
->num_values
== 1 &&
4497 !strcmp(requested
->values
[0].string
.text
, "all"))
4501 * Create an array using "strcmp" as the comparison function...
4504 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
4506 for (i
= 0; i
< requested
->num_values
; i
++)
4508 value
= requested
->values
[i
].string
.text
;
4510 if (!strcmp(value
, "job-template"))
4512 cupsArrayAdd(ra
, "copies");
4513 cupsArrayAdd(ra
, "copies-default");
4514 cupsArrayAdd(ra
, "copies-supported");
4515 cupsArrayAdd(ra
, "finishings");
4516 cupsArrayAdd(ra
, "finishings-default");
4517 cupsArrayAdd(ra
, "finishings-supported");
4518 cupsArrayAdd(ra
, "job-hold-until");
4519 cupsArrayAdd(ra
, "job-hold-until-default");
4520 cupsArrayAdd(ra
, "job-hold-until-supported");
4521 cupsArrayAdd(ra
, "job-priority");
4522 cupsArrayAdd(ra
, "job-priority-default");
4523 cupsArrayAdd(ra
, "job-priority-supported");
4524 cupsArrayAdd(ra
, "job-sheets");
4525 cupsArrayAdd(ra
, "job-sheets-default");
4526 cupsArrayAdd(ra
, "job-sheets-supported");
4527 cupsArrayAdd(ra
, "media");
4528 cupsArrayAdd(ra
, "media-default");
4529 cupsArrayAdd(ra
, "media-supported");
4530 cupsArrayAdd(ra
, "multiple-document-handling");
4531 cupsArrayAdd(ra
, "multiple-document-handling-default");
4532 cupsArrayAdd(ra
, "multiple-document-handling-supported");
4533 cupsArrayAdd(ra
, "number-up");
4534 cupsArrayAdd(ra
, "number-up-default");
4535 cupsArrayAdd(ra
, "number-up-supported");
4536 cupsArrayAdd(ra
, "orientation-requested");
4537 cupsArrayAdd(ra
, "orientation-requested-default");
4538 cupsArrayAdd(ra
, "orientation-requested-supported");
4539 cupsArrayAdd(ra
, "page-ranges");
4540 cupsArrayAdd(ra
, "page-ranges-supported");
4541 cupsArrayAdd(ra
, "printer-resolution");
4542 cupsArrayAdd(ra
, "printer-resolution-default");
4543 cupsArrayAdd(ra
, "printer-resolution-supported");
4544 cupsArrayAdd(ra
, "print-quality");
4545 cupsArrayAdd(ra
, "print-quality-default");
4546 cupsArrayAdd(ra
, "print-quality-supported");
4547 cupsArrayAdd(ra
, "sides");
4548 cupsArrayAdd(ra
, "sides-default");
4549 cupsArrayAdd(ra
, "sides-supported");
4551 else if (!strcmp(value
, "job-description"))
4553 cupsArrayAdd(ra
, "date-time-at-completed");
4554 cupsArrayAdd(ra
, "date-time-at-creation");
4555 cupsArrayAdd(ra
, "date-time-at-processing");
4556 cupsArrayAdd(ra
, "job-detailed-status-message");
4557 cupsArrayAdd(ra
, "job-document-access-errors");
4558 cupsArrayAdd(ra
, "job-id");
4559 cupsArrayAdd(ra
, "job-impressions");
4560 cupsArrayAdd(ra
, "job-impressions-completed");
4561 cupsArrayAdd(ra
, "job-k-octets");
4562 cupsArrayAdd(ra
, "job-k-octets-processed");
4563 cupsArrayAdd(ra
, "job-media-sheets");
4564 cupsArrayAdd(ra
, "job-media-sheets-completed");
4565 cupsArrayAdd(ra
, "job-message-from-operator");
4566 cupsArrayAdd(ra
, "job-more-info");
4567 cupsArrayAdd(ra
, "job-name");
4568 cupsArrayAdd(ra
, "job-originating-user-name");
4569 cupsArrayAdd(ra
, "job-printer-up-time");
4570 cupsArrayAdd(ra
, "job-printer-uri");
4571 cupsArrayAdd(ra
, "job-state");
4572 cupsArrayAdd(ra
, "job-state-message");
4573 cupsArrayAdd(ra
, "job-state-reasons");
4574 cupsArrayAdd(ra
, "job-uri");
4575 cupsArrayAdd(ra
, "number-of-documents");
4576 cupsArrayAdd(ra
, "number-of-intervening-jobs");
4577 cupsArrayAdd(ra
, "output-device-assigned");
4578 cupsArrayAdd(ra
, "time-at-completed");
4579 cupsArrayAdd(ra
, "time-at-creation");
4580 cupsArrayAdd(ra
, "time-at-processing");
4582 else if (!strcmp(value
, "printer-description"))
4584 cupsArrayAdd(ra
, "charset-configured");
4585 cupsArrayAdd(ra
, "charset-supported");
4586 cupsArrayAdd(ra
, "color-supported");
4587 cupsArrayAdd(ra
, "compression-supported");
4588 cupsArrayAdd(ra
, "document-format-default");
4589 cupsArrayAdd(ra
, "document-format-supported");
4590 cupsArrayAdd(ra
, "generated-natural-language-supported");
4591 cupsArrayAdd(ra
, "ipp-versions-supported");
4592 cupsArrayAdd(ra
, "job-impressions-supported");
4593 cupsArrayAdd(ra
, "job-k-octets-supported");
4594 cupsArrayAdd(ra
, "job-media-sheets-supported");
4595 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
4596 cupsArrayAdd(ra
, "multiple-operation-time-out");
4597 cupsArrayAdd(ra
, "natural-language-configured");
4598 cupsArrayAdd(ra
, "notify-attributes-supported");
4599 cupsArrayAdd(ra
, "notify-lease-duration-default");
4600 cupsArrayAdd(ra
, "notify-lease-duration-supported");
4601 cupsArrayAdd(ra
, "notify-max-events-supported");
4602 cupsArrayAdd(ra
, "notify-notify-events-default");
4603 cupsArrayAdd(ra
, "notify-notify-events-supported");
4604 cupsArrayAdd(ra
, "notify-pull-method-supported");
4605 cupsArrayAdd(ra
, "notify-schemes-supported");
4606 cupsArrayAdd(ra
, "operations-supported");
4607 cupsArrayAdd(ra
, "pages-per-minute");
4608 cupsArrayAdd(ra
, "pages-per-minute-color");
4609 cupsArrayAdd(ra
, "pdl-override-supported");
4610 cupsArrayAdd(ra
, "printer-current-time");
4611 cupsArrayAdd(ra
, "printer-driver-installer");
4612 cupsArrayAdd(ra
, "printer-info");
4613 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
4614 cupsArrayAdd(ra
, "printer-location");
4615 cupsArrayAdd(ra
, "printer-make-and-model");
4616 cupsArrayAdd(ra
, "printer-message-from-operator");
4617 cupsArrayAdd(ra
, "printer-more-info");
4618 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
4619 cupsArrayAdd(ra
, "printer-name");
4620 cupsArrayAdd(ra
, "printer-state");
4621 cupsArrayAdd(ra
, "printer-state-message");
4622 cupsArrayAdd(ra
, "printer-state-reasons");
4623 cupsArrayAdd(ra
, "printer-up-time");
4624 cupsArrayAdd(ra
, "printer-uri-supported");
4625 cupsArrayAdd(ra
, "queued-job-count");
4626 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
4627 cupsArrayAdd(ra
, "uri-authentication-supported");
4628 cupsArrayAdd(ra
, "uri-security-supported");
4630 else if (!strcmp(value
, "subscription-template"))
4632 cupsArrayAdd(ra
, "notify-attributes");
4633 cupsArrayAdd(ra
, "notify-charset");
4634 cupsArrayAdd(ra
, "notify-events");
4635 cupsArrayAdd(ra
, "notify-lease-duration");
4636 cupsArrayAdd(ra
, "notify-natural-language");
4637 cupsArrayAdd(ra
, "notify-pull-method");
4638 cupsArrayAdd(ra
, "notify-recipient-uri");
4639 cupsArrayAdd(ra
, "notify-time-interval");
4640 cupsArrayAdd(ra
, "notify-user-data");
4643 cupsArrayAdd(ra
, value
);
4651 * 'create_subscription()' - Create a notification subscription.
4655 create_subscription(
4656 cupsd_client_t
*con
, /* I - Client connection */
4657 ipp_attribute_t
*uri
) /* I - Printer URI */
4659 http_status_t status
; /* Policy status */
4660 int i
; /* Looping var */
4661 ipp_attribute_t
*attr
; /* Current attribute */
4662 const char *dest
; /* Destination */
4663 cups_ptype_t dtype
; /* Destination type (printer or class) */
4664 char method
[HTTP_MAX_URI
],
4665 /* Method portion of URI */
4666 userpass
[HTTP_MAX_URI
],
4667 /* Username portion of URI */
4669 /* Host portion of URI */
4670 resource
[HTTP_MAX_URI
];
4671 /* Resource portion of URI */
4672 int port
; /* Port portion of URI */
4673 cupsd_printer_t
*printer
; /* Printer/class */
4674 cupsd_job_t
*job
; /* Job */
4675 int jobid
; /* Job ID */
4676 cupsd_subscription_t
*sub
; /* Subscription object */
4677 const char *username
, /* requesting-user-name or authenticated username */
4678 *recipient
, /* notify-recipient-uri */
4679 *pullmethod
; /* notify-pull-method */
4680 ipp_attribute_t
*user_data
; /* notify-user-data */
4681 int interval
, /* notify-time-interval */
4682 lease
; /* notify-lease-duration */
4683 unsigned mask
; /* notify-events */
4687 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4689 if (attr
->group_tag
!= IPP_TAG_ZERO
)
4690 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
4691 attr
->value_tag
, attr
->name
);
4693 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
4698 * Is the destination valid?
4701 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4702 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
4703 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
4705 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
4706 sizeof(method
), userpass
, sizeof(userpass
), host
,
4707 sizeof(host
), &port
, resource
, sizeof(resource
));
4709 if (!strcmp(resource
, "/"))
4712 dtype
= (cups_ptype_t
)0;
4715 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
4718 dtype
= (cups_ptype_t
)0;
4721 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
4724 dtype
= CUPS_PRINTER_CLASS
;
4727 else if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
4733 send_ipp_status(con
, IPP_NOT_FOUND
,
4734 _("The printer or class was not found."));
4744 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
4746 send_http_error(con
, status
);
4750 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
4752 send_http_error(con
, status
);
4757 * Get the user that is requesting the subscription...
4760 username
= get_username(con
);
4763 * Find the first subscription group attribute; return if we have
4767 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
4768 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
4773 send_ipp_status(con
, IPP_BAD_REQUEST
,
4774 _("No subscription attributes in request!"));
4779 * Process the subscription attributes in the request...
4782 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
4790 lease
= DefaultLeaseDuration
;
4792 mask
= CUPSD_EVENT_NONE
;
4794 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
4796 if (!strcmp(attr
->name
, "notify-recipient") &&
4797 attr
->value_tag
== IPP_TAG_URI
)
4798 recipient
= attr
->values
[0].string
.text
;
4799 else if (!strcmp(attr
->name
, "notify-pull-method") &&
4800 attr
->value_tag
== IPP_TAG_KEYWORD
)
4801 pullmethod
= attr
->values
[0].string
.text
;
4802 else if (!strcmp(attr
->name
, "notify-charset") &&
4803 attr
->value_tag
== IPP_TAG_CHARSET
&&
4804 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
4805 strcmp(attr
->values
[0].string
.text
, "utf-8"))
4807 send_ipp_status(con
, IPP_CHARSET
,
4808 _("Character set \"%s\" not supported!"),
4809 attr
->values
[0].string
.text
);
4812 else if (!strcmp(attr
->name
, "notify-natural-language") &&
4813 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
4814 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
4816 send_ipp_status(con
, IPP_CHARSET
,
4817 _("Language \"%s\" not supported!"),
4818 attr
->values
[0].string
.text
);
4821 else if (!strcmp(attr
->name
, "notify-user-data") &&
4822 attr
->value_tag
== IPP_TAG_STRING
)
4824 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
4826 send_ipp_status(con
, IPP_REQUEST_VALUE
,
4827 _("The notify-user-data value is too large "
4828 "(%d > 63 octets)!"),
4829 attr
->values
[0].unknown
.length
);
4835 else if (!strcmp(attr
->name
, "notify-events") &&
4836 attr
->value_tag
== IPP_TAG_KEYWORD
)
4838 for (i
= 0; i
< attr
->num_values
; i
++)
4839 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
4841 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
4842 attr
->value_tag
== IPP_TAG_INTEGER
)
4843 lease
= attr
->values
[0].integer
;
4844 else if (!strcmp(attr
->name
, "notify-time-interval") &&
4845 attr
->value_tag
== IPP_TAG_INTEGER
)
4846 interval
= attr
->values
[0].integer
;
4847 else if (!strcmp(attr
->name
, "notify-job-id") &&
4848 attr
->value_tag
== IPP_TAG_INTEGER
)
4849 jobid
= attr
->values
[0].integer
;
4855 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
4857 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
4858 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
4859 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
4861 if (!recipient
&& !pullmethod
)
4864 if (mask
== CUPSD_EVENT_NONE
)
4867 mask
= CUPSD_EVENT_JOB_COMPLETED
;
4869 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4872 send_ipp_status(con
, IPP_BAD_REQUEST
,
4873 _("notify-events not specified!"));
4878 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
4880 cupsdLogMessage(CUPSD_LOG_INFO
,
4881 "create_subscription: Limiting notify-lease-duration to "
4884 lease
= MaxLeaseDuration
;
4889 if ((job
= cupsdFindJob(jobid
)) == NULL
)
4891 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
4898 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
4901 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
4904 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4905 "Added subscription %d for printer \"%s\"",
4906 sub
->id
, printer
->name
);
4908 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
4911 sub
->interval
= interval
;
4913 sub
->expire
= lease
? time(NULL
) + lease
: 0;
4915 cupsdSetString(&sub
->owner
, username
);
4919 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
4920 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
4921 sub
->user_data_len
);
4924 ippAddSeparator(con
->response
);
4925 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
4926 "notify-subscription-id", sub
->id
);
4928 con
->response
->request
.status
.status_code
= IPP_OK
;
4934 cupsdSaveAllSubscriptions();
4940 * 'delete_printer()' - Remove a printer or class from the system.
4944 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
4945 ipp_attribute_t
*uri
) /* I - URI of printer or class */
4947 http_status_t status
; /* Policy status */
4948 const char *dest
; /* Destination */
4949 cups_ptype_t dtype
; /* Destination type (printer or class) */
4950 char method
[HTTP_MAX_URI
], /* Method portion of URI */
4951 username
[HTTP_MAX_URI
], /* Username portion of URI */
4952 host
[HTTP_MAX_URI
], /* Host portion of URI */
4953 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
4954 int port
; /* Port portion of URI */
4955 cupsd_printer_t
*printer
; /* Printer/class */
4956 char filename
[1024]; /* Script/PPD filename */
4959 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
4960 con
->http
.fd
, uri
->values
[0].string
.text
);
4963 * Do we have a valid URI?
4966 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
4967 sizeof(method
), username
, sizeof(username
), host
,
4968 sizeof(host
), &port
, resource
, sizeof(resource
));
4970 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
4976 send_ipp_status(con
, IPP_NOT_FOUND
,
4977 _("The printer or class was not found."));
4985 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
4987 send_http_error(con
, status
);
4992 * Remove old jobs...
4995 cupsdCancelJobs(dest
, NULL
, 1);
4998 * Remove old subscriptions and send a "deleted printer" event...
5001 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5002 "%s \"%s\" deleted by \"%s\".",
5003 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5004 dest
, get_username(con
));
5006 cupsdExpireSubscriptions(printer
, NULL
);
5009 * Remove any old PPD or script files...
5012 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
, dest
);
5015 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
, dest
);
5018 if (dtype
& CUPS_PRINTER_CLASS
)
5020 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".", dest
,
5023 cupsdDeletePrinter(printer
, 0);
5024 cupsdSaveAllClasses();
5028 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".", dest
,
5031 cupsdDeletePrinter(printer
, 0);
5032 cupsdSaveAllPrinters();
5035 cupsdWritePrintcap();
5038 * Return with no errors...
5041 con
->response
->request
.status
.status_code
= IPP_OK
;
5046 * 'get_default()' - Get the default destination.
5050 get_default(cupsd_client_t
*con
) /* I - Client connection */
5052 http_status_t status
; /* Policy status */
5053 cups_array_t
*ra
; /* Requested attributes array */
5056 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5062 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5064 send_http_error(con
, status
);
5070 ra
= create_requested_array(con
->request
);
5072 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5074 cupsArrayDelete(ra
);
5076 con
->response
->request
.status
.status_code
= IPP_OK
;
5079 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5084 * 'get_devices()' - Get the list of available devices on the local system.
5088 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5090 http_status_t status
; /* Policy status */
5091 int i
; /* Looping var */
5092 ipp_attribute_t
*limit
, /* Limit attribute */
5093 *requested
; /* requested-attributes attribute */
5094 char command
[1024], /* cups-deviced command */
5095 options
[1024], /* Options to pass to command */
5096 attrs
[1024], /* String for requested attributes */
5097 *aptr
; /* Pointer into string */
5098 int alen
; /* Length of attribute value */
5101 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5107 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5109 send_http_error(con
, status
);
5114 * Run cups-deviced command with the given options...
5117 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5118 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5123 for (i
= 0, aptr
= attrs
; i
< requested
->num_values
; i
++)
5126 * Check that we have enough room...
5129 alen
= strlen(requested
->values
[i
].string
.text
);
5130 if (alen
> (sizeof(attrs
) - (aptr
- attrs
) - 2))
5134 * Put commas between values...
5141 * Add the value to the end of the string...
5144 strcpy(aptr
, requested
->values
[i
].string
.text
);
5149 * If we have more attribute names than will fit, default to "all"...
5152 if (i
< requested
->num_values
)
5153 strcpy(attrs
, "all");
5156 strcpy(attrs
, "all");
5158 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5159 snprintf(options
, sizeof(options
),
5160 "%d+%d+%d+requested-attributes=%s",
5161 con
->request
->request
.op
.request_id
,
5162 limit
? limit
->values
[0].integer
: 0, (int)User
,
5165 if (cupsdSendCommand(con
, command
, options
, 1))
5168 * Command started successfully, don't send an IPP response here...
5171 ippDelete(con
->response
);
5172 con
->response
= NULL
;
5177 * Command failed, return "internal error" so the user knows something
5181 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5182 _("cups-deviced failed to execute."));
5188 * 'get_job_attrs()' - Get job attributes.
5192 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5193 ipp_attribute_t
*uri
) /* I - Job URI */
5195 http_status_t status
; /* Policy status */
5196 ipp_attribute_t
*attr
; /* Current attribute */
5197 int jobid
; /* Job ID */
5198 cupsd_job_t
*job
; /* Current job */
5199 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5200 username
[HTTP_MAX_URI
], /* Username portion of URI */
5201 host
[HTTP_MAX_URI
], /* Host portion of URI */
5202 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5203 int port
; /* Port portion of URI */
5204 cups_array_t
*ra
; /* Requested attributes array */
5207 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5208 con
->http
.fd
, uri
->values
[0].string
.text
);
5211 * See if we have a job URI or a printer URI...
5214 if (!strcmp(uri
->name
, "printer-uri"))
5217 * Got a printer URI; see if we also have a job-id attribute...
5220 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5221 IPP_TAG_INTEGER
)) == NULL
)
5223 send_ipp_status(con
, IPP_BAD_REQUEST
,
5224 _("Got a printer-uri attribute but no job-id!"));
5228 jobid
= attr
->values
[0].integer
;
5233 * Got a job URI; parse it to get the job ID...
5236 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5237 sizeof(method
), username
, sizeof(username
), host
,
5238 sizeof(host
), &port
, resource
, sizeof(resource
));
5240 if (strncmp(resource
, "/jobs/", 6))
5246 send_ipp_status(con
, IPP_BAD_REQUEST
,
5247 _("Bad job-uri attribute \"%s\"!"),
5248 uri
->values
[0].string
.text
);
5252 jobid
= atoi(resource
+ 6);
5256 * See if the job exists...
5259 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5262 * Nope - return a "not found" error...
5265 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
5273 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5275 send_http_error(con
, status
);
5280 * Copy attributes...
5285 ra
= create_requested_array(con
->request
);
5286 copy_job_attrs(con
, job
, ra
);
5287 cupsArrayDelete(ra
);
5289 con
->response
->request
.status
.status_code
= IPP_OK
;
5294 * 'get_jobs()' - Get a list of jobs for the specified printer.
5298 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
5299 ipp_attribute_t
*uri
) /* I - Printer URI */
5301 http_status_t status
; /* Policy status */
5302 ipp_attribute_t
*attr
; /* Current attribute */
5303 const char *dest
; /* Destination */
5304 cups_ptype_t dtype
; /* Destination type (printer or class) */
5305 cups_ptype_t dmask
; /* Destination type mask */
5306 char method
[HTTP_MAX_URI
], /* Method portion of URI */
5307 username
[HTTP_MAX_URI
], /* Username portion of URI */
5308 host
[HTTP_MAX_URI
], /* Host portion of URI */
5309 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5310 int port
; /* Port portion of URI */
5311 int completed
; /* Completed jobs? */
5312 int first_job_id
; /* First job ID */
5313 int limit
; /* Maximum number of jobs to return */
5314 int count
; /* Number of jobs that match */
5315 cupsd_job_t
*job
; /* Current job pointer */
5316 cupsd_printer_t
*printer
; /* Printer */
5317 cups_array_t
*list
; /* Which job list... */
5318 cups_array_t
*ra
; /* Requested attributes array */
5321 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
5322 uri
->values
[0].string
.text
);
5325 * Is the destination valid?
5328 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5329 sizeof(method
), username
, sizeof(username
), host
,
5330 sizeof(host
), &port
, resource
, sizeof(resource
));
5332 if (!strcmp(resource
, "/") ||
5333 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
5336 dtype
= (cups_ptype_t
)0;
5337 dmask
= (cups_ptype_t
)0;
5340 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5343 dtype
= (cups_ptype_t
)0;
5344 dmask
= CUPS_PRINTER_CLASS
;
5347 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5350 dtype
= CUPS_PRINTER_CLASS
;
5351 dmask
= CUPS_PRINTER_CLASS
;
5354 else if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
5360 send_ipp_status(con
, IPP_NOT_FOUND
,
5361 _("The printer or class was not found."));
5365 dmask
= CUPS_PRINTER_CLASS
;
5373 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
5375 send_http_error(con
, status
);
5379 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5381 send_http_error(con
, status
);
5386 * See if the "which-jobs" attribute have been specified...
5389 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
5390 IPP_TAG_KEYWORD
)) != NULL
&&
5391 !strcmp(attr
->values
[0].string
.text
, "completed"))
5396 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
5408 * See if they want to limit the number of jobs reported...
5411 if ((attr
= ippFindAttribute(con
->request
, "limit",
5412 IPP_TAG_INTEGER
)) != NULL
)
5413 limit
= attr
->values
[0].integer
;
5417 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
5418 IPP_TAG_INTEGER
)) != NULL
)
5419 first_job_id
= attr
->values
[0].integer
;
5424 * See if we only want to see jobs for a specific user...
5427 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
5428 IPP_TAG_BOOLEAN
)) != NULL
&&
5429 attr
->values
[0].boolean
)
5430 strlcpy(username
, get_username(con
), sizeof(username
));
5434 ra
= create_requested_array(con
->request
);
5437 * OK, build a list of jobs for this printer...
5440 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
5441 count
< limit
&& job
;
5442 job
= (cupsd_job_t
*)cupsArrayNext(list
))
5445 * Filter out jobs that don't match...
5448 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
5450 if ((dest
&& strcmp(job
->dest
, dest
)) &&
5451 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
5453 if ((job
->dtype
& dmask
) != dtype
&&
5454 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
5456 if (username
[0] && strcasecmp(username
, job
->username
))
5459 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
5462 if (job
->id
< first_job_id
)
5471 ippAddSeparator(con
->response
);
5475 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
5477 copy_job_attrs(con
, job
, ra
);
5480 cupsArrayDelete(ra
);
5482 con
->response
->request
.status
.status_code
= IPP_OK
;
5487 * 'get_notifications()' - Get events for a subscription.
5491 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
5493 int i
, j
; /* Looping vars */
5494 http_status_t status
; /* Policy status */
5495 cupsd_subscription_t
*sub
; /* Subscription */
5496 ipp_attribute_t
*ids
, /* notify-subscription-ids */
5497 *sequences
; /* notify-sequence-numbers */
5498 int min_seq
; /* Minimum sequence number */
5499 int interval
; /* Poll interval */
5502 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_subscription_attrs(con=%p[%d])",
5506 * Get subscription attributes...
5509 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
5511 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
5516 send_ipp_status(con
, IPP_BAD_REQUEST
,
5517 _("Missing notify-subscription-ids attribute!"));
5522 * Are the subscription IDs valid?
5525 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
5527 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
5530 * Bad subscription ID...
5533 send_ipp_status(con
, IPP_NOT_FOUND
,
5534 _("notify-subscription-id %d no good!"),
5535 ids
->values
[i
].integer
);
5543 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5545 con
, sub
->owner
)) != HTTP_OK
)
5547 send_http_error(con
, status
);
5552 * Check the subscription type and update the interval accordingly.
5555 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
5558 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
5560 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
5566 * Tell the client to poll again in N seconds...
5570 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5571 "notify-get-interval", interval
);
5573 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
5574 "printer-up-time", time(NULL
));
5577 * Copy the subscription event attributes to the response.
5580 con
->response
->request
.status
.status_code
=
5581 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
5583 for (i
= 0; i
< ids
->num_values
; i
++)
5586 * Get the subscription and sequence number...
5589 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
5591 if (sequences
&& i
< sequences
->num_values
)
5592 min_seq
= sequences
->values
[i
].integer
;
5597 * If we don't have any new events, nothing to do here...
5600 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
5604 * Otherwise copy all of the new events...
5607 if (sub
->first_event_id
> min_seq
)
5610 j
= min_seq
- sub
->first_event_id
;
5612 for (; j
< sub
->num_events
; j
++)
5614 ippAddSeparator(con
->response
);
5616 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
5617 IPP_TAG_EVENT_NOTIFICATION
, 0);
5624 * 'get_ppds()' - Get the list of PPD files on the local system.
5628 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
5630 http_status_t status
; /* Policy status */
5631 int i
; /* Looping var */
5632 ipp_attribute_t
*limit
, /* Limit attribute */
5633 *make
, /* ppd-make attribute */
5634 *requested
; /* requested-attributes attribute */
5635 char command
[1024], /* cups-deviced command */
5636 options
[1024], /* Options to pass to command */
5637 attrs
[1024], /* String for requested attributes */
5638 *aptr
; /* Pointer into string */
5639 int alen
; /* Length of attribute value */
5642 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
5648 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5650 send_http_error(con
, status
);
5655 * Run cups-driverd command with the given options...
5658 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5659 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
5660 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5665 for (i
= 0, aptr
= attrs
; i
< requested
->num_values
; i
++)
5668 * Check that we have enough room...
5671 alen
= strlen(requested
->values
[i
].string
.text
);
5672 if (alen
> (sizeof(attrs
) - (aptr
- attrs
) - 2))
5676 * Put commas between values...
5683 * Add the value to the end of the string...
5686 strcpy(aptr
, requested
->values
[i
].string
.text
);
5691 * If we have more attribute names than will fit, default to "all"...
5694 if (i
< requested
->num_values
)
5695 strcpy(attrs
, "all");
5698 strcpy(attrs
, "all");
5700 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
5701 snprintf(options
, sizeof(options
),
5702 "list+%d+%d+requested-attributes=%s%s%s",
5703 con
->request
->request
.op
.request_id
,
5704 limit
? limit
->values
[0].integer
: 0,
5706 make
? "%20ppd-make=" : "",
5707 make
? make
->values
[0].string
.text
: "");
5709 if (cupsdSendCommand(con
, command
, options
, 0))
5712 * Command started successfully, don't send an IPP response here...
5715 ippDelete(con
->response
);
5716 con
->response
= NULL
;
5721 * Command failed, return "internal error" so the user knows something
5725 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5726 _("cups-driverd failed to execute."));
5732 * 'get_printer_attrs()' - Get printer attributes.
5736 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
5737 ipp_attribute_t
*uri
) /* I - Printer URI */
5739 http_status_t status
; /* Policy status */
5740 const char *dest
; /* Destination */
5741 cups_ptype_t dtype
; /* Destination type (printer or class) */
5742 char method
[HTTP_MAX_URI
],
5743 /* Method portion of URI */
5744 username
[HTTP_MAX_URI
],
5745 /* Username portion of URI */
5747 /* Host portion of URI */
5748 resource
[HTTP_MAX_URI
];
5749 /* Resource portion of URI */
5750 int port
; /* Port portion of URI */
5751 cupsd_printer_t
*printer
; /* Printer/class */
5752 cups_array_t
*ra
; /* Requested attributes array */
5755 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
5756 con
->http
.fd
, uri
->values
[0].string
.text
);
5759 * Is the destination valid?
5762 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
5763 sizeof(method
), username
, sizeof(username
), host
,
5764 sizeof(host
), &port
, resource
, sizeof(resource
));
5766 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
5772 send_ipp_status(con
, IPP_NOT_FOUND
,
5773 _("The printer or class was not found."));
5781 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
5783 send_http_error(con
, status
);
5788 * Send the attributes...
5791 ra
= create_requested_array(con
->request
);
5793 copy_printer_attrs(con
, printer
, ra
);
5795 cupsArrayDelete(ra
);
5797 con
->response
->request
.status
.status_code
= IPP_OK
;
5802 * 'get_printers()' - Get a list of printers or classes.
5806 get_printers(cupsd_client_t
*con
, /* I - Client connection */
5807 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
5809 http_status_t status
; /* Policy status */
5810 ipp_attribute_t
*attr
; /* Current attribute */
5811 int limit
; /* Maximum number of printers to return */
5812 int count
; /* Number of printers that match */
5813 cupsd_printer_t
*printer
; /* Current printer pointer */
5814 int printer_type
, /* printer-type attribute */
5815 printer_mask
; /* printer-type-mask attribute */
5816 char *location
; /* Location string */
5817 const char *username
; /* Current user */
5818 char *first_printer_name
; /* first-printer-name attribute */
5819 cups_array_t
*ra
; /* Requested attributes array */
5822 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
5823 con
->http
.fd
, type
);
5829 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5831 send_http_error(con
, status
);
5836 * Check for printers...
5839 if (!Printers
|| !cupsArrayCount(Printers
))
5841 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
5846 * See if they want to limit the number of printers reported...
5849 if ((attr
= ippFindAttribute(con
->request
, "limit",
5850 IPP_TAG_INTEGER
)) != NULL
)
5851 limit
= attr
->values
[0].integer
;
5855 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
5856 IPP_TAG_NAME
)) != NULL
)
5857 first_printer_name
= attr
->values
[0].string
.text
;
5859 first_printer_name
= NULL
;
5862 * Support filtering...
5865 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
5866 IPP_TAG_ENUM
)) != NULL
)
5867 printer_type
= attr
->values
[0].integer
;
5871 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
5872 IPP_TAG_ENUM
)) != NULL
)
5873 printer_mask
= attr
->values
[0].integer
;
5877 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
5878 IPP_TAG_TEXT
)) != NULL
)
5879 location
= attr
->values
[0].string
.text
;
5883 if (con
->username
[0])
5884 username
= con
->username
;
5885 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
5886 IPP_TAG_NAME
)) != NULL
)
5887 username
= attr
->values
[0].string
.text
;
5891 ra
= create_requested_array(con
->request
);
5894 * OK, build a list of printers for this printer...
5897 if (first_printer_name
)
5899 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
5900 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5903 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
5906 count
< limit
&& printer
;
5907 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
5909 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
5910 (printer
->type
& printer_mask
) == printer_type
&&
5911 (!location
|| !printer
->location
||
5912 !strcasecmp(printer
->location
, location
)))
5915 * If HideImplicitMembers is enabled, see if this printer or class
5916 * is a member of an implicit class...
5919 if (ImplicitClasses
&& HideImplicitMembers
&&
5920 printer
->in_implicit_class
)
5924 * If a username is specified, see if it is allowed or denied
5928 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
5932 * Add the group separator as needed...
5936 ippAddSeparator(con
->response
);
5941 * Send the attributes...
5944 copy_printer_attrs(con
, printer
, ra
);
5948 cupsArrayDelete(ra
);
5950 con
->response
->request
.status
.status_code
= IPP_OK
;
5955 * 'get_subscription_attrs()' - Get subscription attributes.
5959 get_subscription_attrs(
5960 cupsd_client_t
*con
, /* I - Client connection */
5961 int sub_id
) /* I - Subscription ID */
5963 http_status_t status
; /* Policy status */
5964 cupsd_subscription_t
*sub
; /* Subscription */
5965 cups_array_t
*ra
; /* Requested attributes array */
5968 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5969 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
5970 con
, con
->http
.fd
, sub_id
);
5973 * Is the subscription ID valid?
5976 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
5979 * Bad subscription ID...
5982 send_ipp_status(con
, IPP_NOT_FOUND
,
5983 _("notify-subscription-id %d no good!"), sub_id
);
5991 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
5993 con
, sub
->owner
)) != HTTP_OK
)
5995 send_http_error(con
, status
);
6000 * Copy the subscription attributes to the response using the
6001 * requested-attributes attribute that may be provided by the client.
6004 ra
= create_requested_array(con
->request
);
6006 copy_subscription_attrs(con
, sub
, ra
);
6008 cupsArrayDelete(ra
);
6010 con
->response
->request
.status
.status_code
= IPP_OK
;
6015 * 'get_subscriptions()' - Get subscriptions.
6019 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
6020 ipp_attribute_t
*uri
) /* I - Printer/job URI */
6022 http_status_t status
; /* Policy status */
6023 int count
; /* Number of subscriptions */
6024 int limit
; /* Limit */
6025 cupsd_subscription_t
*sub
; /* Subscription */
6026 cups_array_t
*ra
; /* Requested attributes array */
6027 ipp_attribute_t
*attr
; /* Attribute */
6028 cups_ptype_t dtype
; /* Destination type (printer or class) */
6029 char method
[HTTP_MAX_URI
],
6030 /* Method portion of URI */
6031 username
[HTTP_MAX_URI
],
6032 /* Username portion of URI */
6034 /* Host portion of URI */
6035 resource
[HTTP_MAX_URI
];
6036 /* Resource portion of URI */
6037 int port
; /* Port portion of URI */
6038 cupsd_job_t
*job
; /* Job pointer */
6039 cupsd_printer_t
*printer
; /* Printer */
6042 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6043 "get_subscriptions(con=%p[%d], uri=%s)",
6044 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
6047 * Is the destination valid?
6050 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6051 sizeof(method
), username
, sizeof(username
), host
,
6052 sizeof(host
), &port
, resource
, sizeof(resource
));
6054 if (!strcmp(resource
, "/") ||
6055 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
6056 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
6057 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
6062 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
6065 job
= cupsdFindJob(atoi(resource
+ 6));
6069 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
6074 else if (!cupsdValidateDest(host
, resource
, &dtype
, &printer
))
6080 send_ipp_status(con
, IPP_NOT_FOUND
,
6081 _("The printer or class was not found."));
6084 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
6085 IPP_TAG_INTEGER
)) != NULL
)
6087 job
= cupsdFindJob(attr
->values
[0].integer
);
6091 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
6092 attr
->values
[0].integer
);
6103 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
6105 con
, NULL
)) != HTTP_OK
)
6107 send_http_error(con
, status
);
6112 * Copy the subscription attributes to the response using the
6113 * requested-attributes attribute that may be provided by the client.
6116 ra
= create_requested_array(con
->request
);
6118 if ((attr
= ippFindAttribute(con
->request
, "limit",
6119 IPP_TAG_INTEGER
)) != NULL
)
6120 limit
= attr
->values
[0].integer
;
6125 * See if we only want to see subscriptions for a specific user...
6128 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
6129 IPP_TAG_BOOLEAN
)) != NULL
&&
6130 attr
->values
[0].boolean
)
6131 strlcpy(username
, get_username(con
), sizeof(username
));
6135 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
6137 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
6138 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
6139 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
6141 ippAddSeparator(con
->response
);
6142 copy_subscription_attrs(con
, sub
, ra
);
6145 if (limit
&& count
>= limit
)
6149 cupsArrayDelete(ra
);
6152 con
->response
->request
.status
.status_code
= IPP_OK
;
6154 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
6159 * 'get_username()' - Get the username associated with a request.
6162 static const char * /* O - Username */
6163 get_username(cupsd_client_t
*con
) /* I - Connection */
6165 ipp_attribute_t
*attr
; /* Attribute */
6168 if (con
->username
[0])
6169 return (con
->username
);
6170 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
6171 IPP_TAG_NAME
)) != NULL
)
6172 return (attr
->values
[0].string
.text
);
6174 return ("anonymous");
6179 * 'hold_job()' - Hold a print job.
6183 hold_job(cupsd_client_t
*con
, /* I - Client connection */
6184 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
6186 ipp_attribute_t
*attr
, /* Current job-hold-until */
6187 *newattr
; /* New job-hold-until */
6188 int jobid
; /* Job ID */
6189 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6190 username
[HTTP_MAX_URI
], /* Username portion of URI */
6191 host
[HTTP_MAX_URI
], /* Host portion of URI */
6192 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6193 int port
; /* Port portion of URI */
6194 cupsd_job_t
*job
; /* Job information */
6197 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
6198 uri
->values
[0].string
.text
);
6201 * See if we have a job URI or a printer URI...
6204 if (!strcmp(uri
->name
, "printer-uri"))
6207 * Got a printer URI; see if we also have a job-id attribute...
6210 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6211 IPP_TAG_INTEGER
)) == NULL
)
6213 send_ipp_status(con
, IPP_BAD_REQUEST
,
6214 _("Got a printer-uri attribute but no job-id!"));
6218 jobid
= attr
->values
[0].integer
;
6223 * Got a job URI; parse it to get the job ID...
6226 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6227 sizeof(method
), username
, sizeof(username
), host
,
6228 sizeof(host
), &port
, resource
, sizeof(resource
));
6230 if (strncmp(resource
, "/jobs/", 6))
6236 send_ipp_status(con
, IPP_BAD_REQUEST
,
6237 _("Bad job-uri attribute \"%s\"!"),
6238 uri
->values
[0].string
.text
);
6242 jobid
= atoi(resource
+ 6);
6246 * See if the job exists...
6249 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6252 * Nope - return a "not found" error...
6255 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6260 * See if the job is owned by the requesting user...
6263 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6265 send_http_error(con
, HTTP_UNAUTHORIZED
);
6270 * Hold the job and return...
6275 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
6276 IPP_TAG_KEYWORD
)) == NULL
)
6277 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
6279 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
6280 IPP_TAG_KEYWORD
)) == NULL
)
6281 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
6286 * Free the old hold value and copy the new one over...
6289 _cupsStrFree(attr
->values
[0].string
.text
);
6293 attr
->value_tag
= newattr
->value_tag
;
6294 attr
->values
[0].string
.text
=
6295 _cupsStrAlloc(newattr
->values
[0].string
.text
);
6299 attr
->value_tag
= IPP_TAG_KEYWORD
;
6300 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
6304 * Hold job until specified time...
6307 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
6310 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was held by \"%s\".", jobid
,
6313 con
->response
->request
.status
.status_code
= IPP_OK
;
6318 * 'move_job()' - Move a job to a new destination.
6322 move_job(cupsd_client_t
*con
, /* I - Client connection */
6323 ipp_attribute_t
*uri
) /* I - Job URI */
6325 http_status_t status
; /* Policy status */
6326 ipp_attribute_t
*attr
; /* Current attribute */
6327 int jobid
; /* Job ID */
6328 cupsd_job_t
*job
; /* Current job */
6329 const char *src
, /* Source printer/class */
6330 *dest
; /* Destination */
6331 cups_ptype_t stype
, /* Source type (printer or class) */
6332 dtype
; /* Destination type (printer or class) */
6333 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6334 username
[HTTP_MAX_URI
], /* Username portion of URI */
6335 host
[HTTP_MAX_URI
], /* Host portion of URI */
6336 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6337 int port
; /* Port portion of URI */
6338 cupsd_printer_t
*sprinter
, /* Source printer */
6339 *dprinter
; /* Destination printer */
6342 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
6343 uri
->values
[0].string
.text
);
6346 * Get the new printer or class...
6349 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
6350 IPP_TAG_URI
)) == NULL
)
6353 * Need job-printer-uri...
6356 send_ipp_status(con
, IPP_BAD_REQUEST
,
6357 _("job-printer-uri attribute missing!"));
6361 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, method
,
6362 sizeof(method
), username
, sizeof(username
), host
,
6363 sizeof(host
), &port
, resource
, sizeof(resource
));
6365 if ((dest
= cupsdValidateDest(host
, resource
, &dtype
, &dprinter
)) == NULL
)
6371 send_ipp_status(con
, IPP_NOT_FOUND
,
6372 _("The printer or class was not found."));
6380 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6382 send_http_error(con
, status
);
6387 * See if we have a job URI or a printer URI...
6390 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6391 sizeof(method
), username
, sizeof(username
), host
,
6392 sizeof(host
), &port
, resource
, sizeof(resource
));
6394 if (!strcmp(uri
->name
, "printer-uri"))
6397 * Got a printer URI; see if we also have a job-id attribute...
6400 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6401 IPP_TAG_INTEGER
)) == NULL
)
6407 if ((src
= cupsdValidateDest(host
, resource
, &stype
, &sprinter
)) == NULL
)
6413 send_ipp_status(con
, IPP_NOT_FOUND
,
6414 _("The printer or class was not found."));
6423 * Otherwise, just move a single job...
6426 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
6429 * Nope - return a "not found" error...
6432 send_ipp_status(con
, IPP_NOT_FOUND
,
6433 _("Job #%d does not exist!"), attr
->values
[0].integer
);
6439 * Job found, initialize source pointers...
6450 * Got a job URI; parse it to get the job ID...
6453 if (strncmp(resource
, "/jobs/", 6))
6459 send_ipp_status(con
, IPP_BAD_REQUEST
,
6460 _("Bad job-uri attribute \"%s\"!"),
6461 uri
->values
[0].string
.text
);
6466 * See if the job exists...
6469 jobid
= atoi(resource
+ 6);
6471 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6474 * Nope - return a "not found" error...
6477 send_ipp_status(con
, IPP_NOT_FOUND
,
6478 _("Job #%d does not exist!"), jobid
);
6484 * Job found, initialize source pointers...
6493 * Now move the job or jobs...
6499 * See if the job has been completed...
6502 if (job
->state_value
> IPP_JOB_STOPPED
)
6505 * Return a "not-possible" error...
6508 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6509 _("Job #%d is finished and cannot be altered!"),
6515 * See if the job is owned by the requesting user...
6518 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6520 send_http_error(con
, HTTP_UNAUTHORIZED
);
6525 * Move the job to a different printer or class...
6528 cupsdMoveJob(job
, dest
);
6533 * Got the source printer, now look through the jobs...
6536 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
6538 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
6541 * See if the job is pointing at the source printer or has not been
6545 if (strcasecmp(job
->dest
, src
) ||
6546 job
->state_value
> IPP_JOB_STOPPED
)
6550 * See if the job can be moved by the requesting user...
6553 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
6557 * Move the job to a different printer or class...
6560 cupsdMoveJob(job
, dest
);
6565 * Start jobs if possible...
6571 * Return with "everything is OK" status...
6574 con
->response
->request
.status
.status_code
= IPP_OK
;
6579 * 'ppd_add_default()' - Add a PPD default choice.
6582 static int /* O - Number of defaults */
6584 const char *option
, /* I - Option name */
6585 const char *choice
, /* I - Choice name */
6586 int num_defaults
, /* I - Number of defaults */
6587 ppd_default_t
**defaults
) /* IO - Defaults */
6589 int i
; /* Looping var */
6590 ppd_default_t
*temp
; /* Temporary defaults array */
6594 * First check if the option already has a default value; the PPD spec
6595 * says that the first one is used...
6598 for (i
= 0, temp
= *defaults
; i
< num_defaults
; i
++)
6599 if (!strcmp(option
, temp
[i
].option
))
6600 return (num_defaults
);
6603 * Now add the option...
6606 if (num_defaults
== 0)
6607 temp
= malloc(sizeof(ppd_default_t
));
6609 temp
= realloc(*defaults
, (num_defaults
+ 1) * sizeof(ppd_default_t
));
6613 cupsdLogMessage(CUPSD_LOG_ERROR
, "ppd_add_default: Unable to add default value for \"%s\" - %s",
6614 option
, strerror(errno
));
6615 return (num_defaults
);
6619 temp
+= num_defaults
;
6621 strlcpy(temp
->option
, option
, sizeof(temp
->option
));
6622 strlcpy(temp
->choice
, choice
, sizeof(temp
->choice
));
6624 return (num_defaults
+ 1);
6629 * 'ppd_parse_line()' - Parse a PPD default line.
6632 static int /* O - 0 on success, -1 on failure */
6633 ppd_parse_line(const char *line
, /* I - Line */
6634 char *option
, /* O - Option name */
6635 int olen
, /* I - Size of option name */
6636 char *choice
, /* O - Choice name */
6637 int clen
) /* I - Size of choice name */
6640 * Verify this is a default option line...
6643 if (strncmp(line
, "*Default", 8))
6647 * Read the option name...
6650 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
6660 * Skip everything else up to the colon (:)...
6663 while (*line
&& *line
!= ':')
6672 * Now grab the option choice, skipping leading whitespace...
6675 while (isspace(*line
& 255))
6678 for (clen
--; isalnum(*line
& 255); line
++)
6688 * Return with no errors...
6696 * 'print_job()' - Print a file to a printer or class.
6700 print_job(cupsd_client_t
*con
, /* I - Client connection */
6701 ipp_attribute_t
*uri
) /* I - Printer URI */
6703 ipp_attribute_t
*attr
; /* Current attribute */
6704 ipp_attribute_t
*format
; /* Document-format attribute */
6705 cupsd_job_t
*job
; /* New job */
6706 char filename
[1024]; /* Job filename */
6707 mime_type_t
*filetype
; /* Type of file */
6708 char super
[MIME_MAX_SUPER
], /* Supertype of file */
6709 type
[MIME_MAX_TYPE
], /* Subtype of file */
6710 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
6711 /* Textual name of mime type */
6712 cupsd_printer_t
*printer
; /* Printer data */
6713 struct stat fileinfo
; /* File information */
6714 int kbytes
; /* Size of file */
6715 int compression
; /* Document compression */
6718 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
6719 uri
->values
[0].string
.text
);
6722 * Validate print file attributes, for now just document-format and
6723 * compression (CUPS only supports "none" and "gzip")...
6726 compression
= CUPS_FILE_NONE
;
6728 if ((attr
= ippFindAttribute(con
->request
, "compression",
6729 IPP_TAG_KEYWORD
)) != NULL
)
6731 if (strcmp(attr
->values
[0].string
.text
, "none")
6733 && strcmp(attr
->values
[0].string
.text
, "gzip")
6734 #endif /* HAVE_LIBZ */
6737 send_ipp_status(con
, IPP_ATTRIBUTES
,
6738 _("Unsupported compression \"%s\"!"),
6739 attr
->values
[0].string
.text
);
6740 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6741 "compression", NULL
, attr
->values
[0].string
.text
);
6746 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
6747 compression
= CUPS_FILE_GZIP
;
6748 #endif /* HAVE_LIBZ */
6752 * Do we have a file to print?
6757 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
6762 * Is it a format we support?
6765 if ((format
= ippFindAttribute(con
->request
, "document-format",
6766 IPP_TAG_MIMETYPE
)) != NULL
)
6769 * Grab format from client...
6772 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
6774 send_ipp_status(con
, IPP_BAD_REQUEST
,
6775 _("Could not scan type \"%s\"!"),
6776 format
->values
[0].string
.text
);
6783 * No document format attribute? Auto-type it!
6786 strcpy(super
, "application");
6787 strcpy(type
, "octet-stream");
6790 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
6793 * Auto-type the file...
6796 ipp_attribute_t
*doc_name
; /* document-name attribute */
6799 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
6801 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
6802 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
6803 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
6809 * Replace the document-format attribute value with the auto-typed one.
6812 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
6817 _cupsStrFree(format
->values
[0].string
.text
);
6819 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
6822 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
6823 "document-format", NULL
, mimetype
);
6826 filetype
= mimeType(MimeDatabase
, super
, type
);
6829 filetype
= mimeType(MimeDatabase
, super
, type
);
6833 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
6834 _("Unsupported format \'%s/%s\'!"), super
, type
);
6835 cupsdLogMessage(CUPSD_LOG_INFO
,
6836 "Hint: Do you have the raw file printing rules enabled?");
6839 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
6840 "document-format", NULL
, format
->values
[0].string
.text
);
6845 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: request file type is %s/%s.",
6846 filetype
->super
, filetype
->type
);
6849 * Read any embedded job ticket info from PS files...
6852 if (!strcasecmp(filetype
->super
, "application") &&
6853 !strcasecmp(filetype
->type
, "postscript"))
6854 read_ps_job_ticket(con
);
6857 * Create the job object...
6860 if ((job
= add_job(con
, uri
, &printer
, filetype
)) == NULL
)
6864 * Update quota data...
6867 if (stat(con
->filename
, &fileinfo
))
6870 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
6872 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6874 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
6875 IPP_TAG_INTEGER
)) != NULL
)
6876 attr
->values
[0].integer
+= kbytes
;
6879 * Add the job file...
6882 if (add_file(con
, job
, filetype
, compression
))
6885 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
6887 rename(con
->filename
, filename
);
6888 cupsdClearString(&con
->filename
);
6891 * See if we need to add the ending sheet...
6894 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
6895 attr
->num_values
> 1)
6901 cupsdLogMessage(CUPSD_LOG_INFO
, "Adding end banner page \"%s\" to job %d.",
6902 attr
->values
[1].string
.text
, job
->id
);
6904 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
6906 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
6910 * Log and save the job...
6913 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d queued on \"%s\" by \"%s\".", job
->id
,
6914 job
->dest
, job
->username
);
6915 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Job %d hold_until = %d", job
->id
,
6916 (int)job
->hold_until
);
6921 * Start the job if possible...
6929 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
6931 * This function only gets called when printing a single PostScript
6932 * file using the Print-Job operation. It doesn't work for Create-Job +
6933 * Send-File, since the job attributes need to be set at job creation
6934 * time for banners to work. The embedded PS job ticket stuff is here
6935 * only to allow the Windows printer driver for CUPS to pass in JCL
6936 * options and IPP attributes which otherwise would be lost.
6938 * The format of a PS job ticket is simple:
6940 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
6942 * %cupsJobTicket: attr1=value1
6943 * %cupsJobTicket: attr2=value2
6945 * %cupsJobTicket: attrN=valueN
6947 * Job ticket lines must appear immediately after the first line that
6948 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
6949 * looking for job ticket info when it finds a line that does not begin
6950 * with "%cupsJobTicket:".
6952 * The maximum length of a job ticket line, including the prefix, is
6953 * 255 characters to conform with the Adobe DSC.
6955 * Read-only attributes are rejected with a notice to the error log in
6956 * case a malicious user tries anything. Since the job ticket is read
6957 * prior to attribute validation in print_job(), job ticket attributes
6958 * will go through the same validation as IPP attributes...
6962 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
6964 cups_file_t
*fp
; /* File to read from */
6965 char line
[256]; /* Line data */
6966 int num_options
; /* Number of options */
6967 cups_option_t
*options
; /* Options */
6968 ipp_t
*ticket
; /* New attributes */
6969 ipp_attribute_t
*attr
, /* Current attribute */
6970 *attr2
, /* Job attribute */
6971 *prev2
; /* Previous job attribute */
6975 * First open the print file...
6978 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
6980 cupsdLogMessage(CUPSD_LOG_ERROR
,
6981 "read_ps_job_ticket: Unable to open PostScript print file "
6988 * Skip the first line...
6991 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
6993 cupsdLogMessage(CUPSD_LOG_ERROR
,
6994 "read_ps_job_ticket: Unable to read from PostScript print "
7001 if (strncmp(line
, "%!PS-Adobe-", 11))
7004 * Not a DSC-compliant file, so no job ticket info will be available...
7012 * Read job ticket info from the file...
7018 while (cupsFileGets(fp
, line
, sizeof(line
)))
7021 * Stop at the first non-ticket line...
7024 if (strncmp(line
, "%cupsJobTicket:", 15))
7028 * Add the options to the option array...
7031 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
7035 * Done with the file; see if we have any options...
7040 if (num_options
== 0)
7044 * OK, convert the options to an attribute list, and apply them to
7049 cupsEncodeOptions(ticket
, num_options
, options
);
7052 * See what the user wants to change.
7055 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
7057 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
7060 if (!strcmp(attr
->name
, "job-originating-host-name") ||
7061 !strcmp(attr
->name
, "job-originating-user-name") ||
7062 !strcmp(attr
->name
, "job-media-sheets-completed") ||
7063 !strcmp(attr
->name
, "job-k-octets") ||
7064 !strcmp(attr
->name
, "job-id") ||
7065 !strncmp(attr
->name
, "job-state", 9) ||
7066 !strncmp(attr
->name
, "time-at-", 8))
7067 continue; /* Read-only attrs */
7069 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
7070 IPP_TAG_ZERO
)) != NULL
)
7073 * Some other value; first free the old value...
7076 if (con
->request
->attrs
== attr2
)
7078 con
->request
->attrs
= attr2
->next
;
7083 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
7084 if (prev2
->next
== attr2
)
7086 prev2
->next
= attr2
->next
;
7091 if (con
->request
->last
== attr2
)
7092 con
->request
->last
= prev2
;
7094 _ippFreeAttr(attr2
);
7098 * Add new option by copying it...
7101 copy_attribute(con
->request
, attr
, 0);
7105 * Then free the attribute list and option array...
7109 cupsFreeOptions(num_options
, options
);
7114 * 'reject_jobs()' - Reject print jobs to a printer.
7118 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
7119 ipp_attribute_t
*uri
) /* I - Printer or class URI */
7121 http_status_t status
; /* Policy status */
7122 cups_ptype_t dtype
; /* Destination type (printer or class) */
7123 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7124 username
[HTTP_MAX_URI
], /* Username portion of URI */
7125 host
[HTTP_MAX_URI
], /* Host portion of URI */
7126 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7127 int port
; /* Port portion of URI */
7128 const char *name
; /* Printer name */
7129 cupsd_printer_t
*printer
; /* Printer data */
7130 ipp_attribute_t
*attr
; /* printer-state-message text */
7133 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
7134 con
->http
.fd
, uri
->values
[0].string
.text
);
7137 * Is the destination valid?
7140 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7141 sizeof(method
), username
, sizeof(username
), host
,
7142 sizeof(host
), &port
, resource
, sizeof(resource
));
7144 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
7150 send_ipp_status(con
, IPP_NOT_FOUND
,
7151 _("The printer or class was not found."));
7159 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7161 send_http_error(con
, status
);
7166 * Reject jobs sent to the printer...
7169 printer
->accepting
= 0;
7171 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
7172 IPP_TAG_TEXT
)) == NULL
)
7173 strcpy(printer
->state_message
, "Rejecting Jobs");
7175 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
7176 sizeof(printer
->state_message
));
7178 cupsdAddPrinterHistory(printer
);
7180 if (dtype
& CUPS_PRINTER_CLASS
)
7182 cupsdSaveAllClasses();
7184 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
7185 name
, get_username(con
));
7189 cupsdSaveAllPrinters();
7191 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
7192 name
, get_username(con
));
7196 * Everything was ok, so return OK status...
7199 con
->response
->request
.status
.status_code
= IPP_OK
;
7204 * 'release_job()' - Release a held print job.
7208 release_job(cupsd_client_t
*con
, /* I - Client connection */
7209 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7211 ipp_attribute_t
*attr
; /* Current attribute */
7212 int jobid
; /* Job ID */
7213 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7214 username
[HTTP_MAX_URI
], /* Username portion of URI */
7215 host
[HTTP_MAX_URI
], /* Host portion of URI */
7216 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7217 int port
; /* Port portion of URI */
7218 cupsd_job_t
*job
; /* Job information */
7221 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
7222 con
->http
.fd
, uri
->values
[0].string
.text
);
7225 * See if we have a job URI or a printer URI...
7228 if (!strcmp(uri
->name
, "printer-uri"))
7231 * Got a printer URI; see if we also have a job-id attribute...
7234 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7235 IPP_TAG_INTEGER
)) == NULL
)
7237 send_ipp_status(con
, IPP_BAD_REQUEST
,
7238 _("Got a printer-uri attribute but no job-id!"));
7242 jobid
= attr
->values
[0].integer
;
7247 * Got a job URI; parse it to get the job ID...
7250 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7251 sizeof(method
), username
, sizeof(username
), host
,
7252 sizeof(host
), &port
, resource
, sizeof(resource
));
7254 if (strncmp(resource
, "/jobs/", 6))
7260 send_ipp_status(con
, IPP_BAD_REQUEST
,
7261 _("Bad job-uri attribute \"%s\"!"),
7262 uri
->values
[0].string
.text
);
7266 jobid
= atoi(resource
+ 6);
7270 * See if the job exists...
7273 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7276 * Nope - return a "not found" error...
7279 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7284 * See if job is "held"...
7287 if (job
->state_value
!= IPP_JOB_HELD
)
7290 * Nope - return a "not possible" error...
7293 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
7298 * See if the job is owned by the requesting user...
7301 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7303 send_http_error(con
, HTTP_UNAUTHORIZED
);
7308 * Reset the job-hold-until value to "no-hold"...
7311 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7312 IPP_TAG_KEYWORD
)) == NULL
)
7313 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7317 _cupsStrFree(attr
->values
[0].string
.text
);
7319 attr
->value_tag
= IPP_TAG_KEYWORD
;
7320 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
7324 * Release the job and return...
7327 cupsdReleaseJob(job
);
7329 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was released by \"%s\".", jobid
,
7332 con
->response
->request
.status
.status_code
= IPP_OK
;
7337 * 'renew_subscription()' - Renew an existing subscription...
7342 cupsd_client_t
*con
, /* I - Client connection */
7343 int sub_id
) /* I - Subscription ID */
7345 http_status_t status
; /* Policy status */
7346 cupsd_subscription_t
*sub
; /* Subscription */
7347 ipp_attribute_t
*lease
; /* notify-lease-duration */
7350 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7351 "renew_subscription(con=%p[%d], sub_id=%d)",
7352 con
, con
->http
.fd
, sub_id
);
7355 * Is the subscription ID valid?
7358 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7361 * Bad subscription ID...
7364 send_ipp_status(con
, IPP_NOT_FOUND
,
7365 _("notify-subscription-id %d no good!"), sub_id
);
7372 * Job subscriptions cannot be renewed...
7375 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7376 _("Job subscriptions cannot be renewed!"));
7384 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7386 con
, sub
->owner
)) != HTTP_OK
)
7388 send_http_error(con
, status
);
7393 * Renew the subscription...
7396 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
7399 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
7401 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
7403 cupsdLogMessage(CUPSD_LOG_INFO
,
7404 "renew_subscription: Limiting notify-lease-duration to "
7407 sub
->lease
= MaxLeaseDuration
;
7410 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
7412 cupsdSaveAllSubscriptions();
7414 con
->response
->request
.status
.status_code
= IPP_OK
;
7419 * 'restart_job()' - Restart an old print job.
7423 restart_job(cupsd_client_t
*con
, /* I - Client connection */
7424 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7426 ipp_attribute_t
*attr
; /* Current attribute */
7427 int jobid
; /* Job ID */
7428 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7429 username
[HTTP_MAX_URI
], /* Username portion of URI */
7430 host
[HTTP_MAX_URI
], /* Host portion of URI */
7431 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7432 int port
; /* Port portion of URI */
7433 cupsd_job_t
*job
; /* Job information */
7436 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
7437 con
->http
.fd
, uri
->values
[0].string
.text
);
7440 * See if we have a job URI or a printer URI...
7443 if (!strcmp(uri
->name
, "printer-uri"))
7446 * Got a printer URI; see if we also have a job-id attribute...
7449 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7450 IPP_TAG_INTEGER
)) == NULL
)
7452 send_ipp_status(con
, IPP_BAD_REQUEST
,
7453 _("Got a printer-uri attribute but no job-id!"));
7457 jobid
= attr
->values
[0].integer
;
7462 * Got a job URI; parse it to get the job ID...
7465 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7466 sizeof(method
), username
, sizeof(username
), host
,
7467 sizeof(host
), &port
, resource
, sizeof(resource
));
7469 if (strncmp(resource
, "/jobs/", 6))
7475 send_ipp_status(con
, IPP_BAD_REQUEST
,
7476 _("Bad job-uri attribute \"%s\"!"),
7477 uri
->values
[0].string
.text
);
7481 jobid
= atoi(resource
+ 6);
7485 * See if the job exists...
7488 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7491 * Nope - return a "not found" error...
7494 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7499 * See if job is in any of the "completed" states...
7502 if (job
->state_value
<= IPP_JOB_PROCESSING
)
7505 * Nope - return a "not possible" error...
7508 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
7514 * See if we have retained the job files...
7519 if (!job
->attrs
||job
->num_files
== 0)
7522 * Nope - return a "not possible" error...
7525 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7526 _("Job #%d cannot be restarted - no files!"), jobid
);
7531 * See if the job is owned by the requesting user...
7534 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7536 send_http_error(con
, HTTP_UNAUTHORIZED
);
7541 * Restart the job and return...
7544 cupsdRestartJob(job
);
7546 cupsdLogMessage(CUPSD_LOG_INFO
, "Job %d was restarted by \"%s\".", jobid
,
7549 con
->response
->request
.status
.status_code
= IPP_OK
;
7554 * 'save_auth_info()' - Save authentication information for a job.
7558 save_auth_info(cupsd_client_t
*con
, /* I - Client connection */
7559 cupsd_job_t
*job
) /* I - Job */
7561 int i
; /* Looping var */
7562 char filename
[1024]; /* Job authentication filename */
7563 cups_file_t
*fp
; /* Job authentication file */
7564 char line
[1024]; /* Line for file */
7568 * This function saves the in-memory authentication information for
7569 * a job so that it can be used to authenticate with a remote host.
7570 * The information is stored in a file that is readable only by the
7571 * root user. The username and password are Base-64 encoded, each
7572 * on a separate line, followed by random number (up to 1024) of
7573 * newlines to limit the amount of information that is exposed.
7575 * Because of the potential for exposing of authentication information,
7576 * this functionality is only enabled when running cupsd as root.
7578 * This caching only works for the Basic and BasicDigest authentication
7579 * types. Digest authentication cannot be cached this way, and in
7580 * the future Kerberos authentication may make all of this obsolete.
7582 * Authentication information is saved whenever an authenticated
7583 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
7586 * This information is deleted after a job is completed or canceled,
7587 * so reprints may require subsequent re-authentication.
7594 * Create the authentication file and change permissions...
7597 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
7598 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
7600 cupsdLogMessage(CUPSD_LOG_ERROR
,
7601 "Unable to save authentication info to \"%s\" - %s",
7602 filename
, strerror(errno
));
7606 fchown(cupsFileNumber(fp
), 0, 0);
7607 fchmod(cupsFileNumber(fp
), 0400);
7610 * Write the authenticated username...
7613 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
7614 cupsFilePrintf(fp
, "%s\n", line
);
7617 * Write the authenticated password...
7620 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
7621 cupsFilePrintf(fp
, "%s\n", line
);
7624 * Write a random number of newlines to the end of the file...
7627 for (i
= (rand() % 1024); i
>= 0; i
--)
7628 cupsFilePutChar(fp
, '\n');
7631 * Close the file and return...
7639 * 'send_document()' - Send a file to a printer or class.
7643 send_document(cupsd_client_t
*con
, /* I - Client connection */
7644 ipp_attribute_t
*uri
) /* I - Printer URI */
7646 ipp_attribute_t
*attr
; /* Current attribute */
7647 ipp_attribute_t
*format
; /* Document-format attribute */
7648 int jobid
; /* Job ID number */
7649 cupsd_job_t
*job
; /* Current job */
7650 char job_uri
[HTTP_MAX_URI
],
7652 method
[HTTP_MAX_URI
],
7653 /* Method portion of URI */
7654 username
[HTTP_MAX_URI
],
7655 /* Username portion of URI */
7657 /* Host portion of URI */
7658 resource
[HTTP_MAX_URI
];
7659 /* Resource portion of URI */
7660 int port
; /* Port portion of URI */
7661 mime_type_t
*filetype
; /* Type of file */
7662 char super
[MIME_MAX_SUPER
],
7663 /* Supertype of file */
7664 type
[MIME_MAX_TYPE
],
7665 /* Subtype of file */
7666 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7667 /* Textual name of mime type */
7668 char filename
[1024]; /* Job filename */
7669 cupsd_printer_t
*printer
; /* Current printer */
7670 struct stat fileinfo
; /* File information */
7671 int kbytes
; /* Size of file */
7672 int compression
; /* Type of compression */
7675 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
7676 con
->http
.fd
, uri
->values
[0].string
.text
);
7679 * See if we have a job URI or a printer URI...
7682 if (!strcmp(uri
->name
, "printer-uri"))
7685 * Got a printer URI; see if we also have a job-id attribute...
7688 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7689 IPP_TAG_INTEGER
)) == NULL
)
7691 send_ipp_status(con
, IPP_BAD_REQUEST
,
7692 _("Got a printer-uri attribute but no job-id!"));
7696 jobid
= attr
->values
[0].integer
;
7701 * Got a job URI; parse it to get the job ID...
7704 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7705 sizeof(method
), username
, sizeof(username
), host
,
7706 sizeof(host
), &port
, resource
, sizeof(resource
));
7708 if (strncmp(resource
, "/jobs/", 6))
7714 send_ipp_status(con
, IPP_BAD_REQUEST
,
7715 _("Bad job-uri attribute \"%s\"!"),
7716 uri
->values
[0].string
.text
);
7720 jobid
= atoi(resource
+ 6);
7724 * See if the job exists...
7727 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7730 * Nope - return a "not found" error...
7733 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7738 * See if the job is owned by the requesting user...
7741 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7743 send_http_error(con
, HTTP_UNAUTHORIZED
);
7748 * OK, see if the client is sending the document compressed - CUPS
7749 * only supports "none" and "gzip".
7752 compression
= CUPS_FILE_NONE
;
7754 if ((attr
= ippFindAttribute(con
->request
, "compression",
7755 IPP_TAG_KEYWORD
)) != NULL
)
7757 if (strcmp(attr
->values
[0].string
.text
, "none")
7759 && strcmp(attr
->values
[0].string
.text
, "gzip")
7760 #endif /* HAVE_LIBZ */
7763 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
7764 attr
->values
[0].string
.text
);
7765 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7766 "compression", NULL
, attr
->values
[0].string
.text
);
7771 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7772 compression
= CUPS_FILE_GZIP
;
7773 #endif /* HAVE_LIBZ */
7777 * Do we have a file to print?
7782 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7787 * Is it a format we support?
7790 if ((format
= ippFindAttribute(con
->request
, "document-format",
7791 IPP_TAG_MIMETYPE
)) != NULL
)
7794 * Grab format from client...
7797 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
7799 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
7800 format
->values
[0].string
.text
);
7807 * No document format attribute? Auto-type it!
7810 strcpy(super
, "application");
7811 strcpy(type
, "octet-stream");
7814 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7817 * Auto-type the file...
7820 ipp_attribute_t
*doc_name
; /* document-name attribute */
7823 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
7825 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7826 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7827 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7833 * Replace the document-format attribute value with the auto-typed one.
7836 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7841 _cupsStrFree(format
->values
[0].string
.text
);
7842 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7845 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7846 "document-format", NULL
, mimetype
);
7849 filetype
= mimeType(MimeDatabase
, super
, type
);
7852 filetype
= mimeType(MimeDatabase
, super
, type
);
7856 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7857 _("Unsupported format \'%s/%s\'!"), super
, type
);
7858 cupsdLogMessage(CUPSD_LOG_INFO
,
7859 "Hint: Do you have the raw file printing rules enabled?");
7862 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7863 "document-format", NULL
, format
->values
[0].string
.text
);
7868 printer
= cupsdFindDest(job
->dest
);
7870 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
7872 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7875 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7876 _("Unsupported format \'%s\'!"), mimetype
);
7878 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7879 "document-format", NULL
, mimetype
);
7884 cupsdLogMessage(CUPSD_LOG_DEBUG
,
7885 "send_document: request file type is %s/%s.",
7886 filetype
->super
, filetype
->type
);
7889 * Add the file to the job...
7894 if (add_file(con
, job
, filetype
, compression
))
7897 if (stat(con
->filename
, &fileinfo
))
7900 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7902 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7904 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7905 IPP_TAG_INTEGER
)) != NULL
)
7906 attr
->values
[0].integer
+= kbytes
;
7908 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
7910 rename(con
->filename
, filename
);
7912 cupsdClearString(&con
->filename
);
7914 cupsdLogMessage(CUPSD_LOG_INFO
,
7915 "File of type %s/%s queued in job #%d by \"%s\".",
7916 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
7919 * Start the job if this is the last document...
7922 if ((attr
= ippFindAttribute(con
->request
, "last-document",
7923 IPP_TAG_BOOLEAN
)) != NULL
&&
7924 attr
->values
[0].boolean
)
7927 * See if we need to add the ending sheet...
7931 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
7932 (attr
= ippFindAttribute(job
->attrs
, "job-sheets",
7933 IPP_TAG_ZERO
)) != NULL
&&
7934 attr
->num_values
> 1)
7940 cupsdLogMessage(CUPSD_LOG_INFO
,
7941 "Adding end banner page \"%s\" to job %d.",
7942 attr
->values
[1].string
.text
, job
->id
);
7944 kbytes
= copy_banner(con
, job
, attr
->values
[1].string
.text
);
7946 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7949 if (job
->state_value
== IPP_JOB_STOPPED
)
7951 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7952 job
->state_value
= IPP_JOB_PENDING
;
7954 else if (job
->state_value
== IPP_JOB_HELD
)
7956 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7957 IPP_TAG_KEYWORD
)) == NULL
)
7958 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7960 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
7962 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
7963 job
->state_value
= IPP_JOB_PENDING
;
7970 * Start the job if possible... Since cupsdCheckJobs() can cancel a
7971 * job if it doesn't print, we need to re-find the job afterwards...
7978 job
= cupsdFindJob(jobid
);
7982 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7983 IPP_TAG_KEYWORD
)) == NULL
)
7984 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7986 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
7988 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
7989 job
->state_value
= IPP_JOB_HELD
;
7990 job
->hold_until
= time(NULL
) + 60;
7996 * Fill in the response info...
7999 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
8002 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
8005 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
8007 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
8008 job
? job
->state_value
: IPP_JOB_CANCELLED
);
8009 add_job_state_reasons(con
, job
);
8011 con
->response
->request
.status
.status_code
= IPP_OK
;
8016 * 'send_http_error()' - Send a HTTP error back to the IPP client.
8020 send_http_error(cupsd_client_t
*con
, /* I - Client connection */
8021 http_status_t status
) /* I - HTTP status code */
8023 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
8024 ippOpString(con
->request
->request
.op
.operation_id
),
8025 httpStatus(status
));
8027 cupsdSendError(con
, status
);
8029 ippDelete(con
->response
);
8030 con
->response
= NULL
;
8037 * 'send_ipp_status()' - Send a status back to the IPP client.
8041 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
8042 ipp_status_t status
, /* I - IPP status code */
8043 const char *message
, /* I - Status message */
8044 ...) /* I - Additional args as needed */
8046 va_list ap
; /* Pointer to additional args */
8047 char formatted
[1024]; /* Formatted errror message */
8052 va_start(ap
, message
);
8053 vsnprintf(formatted
, sizeof(formatted
),
8054 _cupsLangString(con
->language
, message
), ap
);
8057 cupsdLogMessage(status
>= IPP_BAD_REQUEST
? CUPSD_LOG_ERROR
: CUPSD_LOG_INFO
,
8059 ippOpString(con
->request
->request
.op
.operation_id
),
8060 ippErrorString(status
), formatted
);
8063 cupsdLogMessage(status
>= IPP_BAD_REQUEST
? CUPSD_LOG_ERROR
: CUPSD_LOG_INFO
,
8065 ippOpString(con
->request
->request
.op
.operation_id
),
8066 ippErrorString(status
));
8068 con
->response
->request
.status
.status_code
= status
;
8070 if (ippFindAttribute(con
->response
, "attributes-charset",
8071 IPP_TAG_ZERO
) == NULL
)
8072 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
8073 "attributes-charset", NULL
, DefaultCharset
);
8075 if (ippFindAttribute(con
->response
, "attributes-natural-language",
8076 IPP_TAG_ZERO
) == NULL
)
8077 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
8078 "attributes-natural-language", NULL
, DefaultLanguage
);
8081 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
8082 "status-message", NULL
, formatted
);
8087 * 'set_default()' - Set the default destination...
8091 set_default(cupsd_client_t
*con
, /* I - Client connection */
8092 ipp_attribute_t
*uri
) /* I - Printer URI */
8094 http_status_t status
; /* Policy status */
8095 cups_ptype_t dtype
; /* Destination type (printer or class) */
8096 char method
[HTTP_MAX_URI
],
8097 /* Method portion of URI */
8098 username
[HTTP_MAX_URI
],
8099 /* Username portion of URI */
8101 /* Host portion of URI */
8102 resource
[HTTP_MAX_URI
];
8103 /* Resource portion of URI */
8104 int port
; /* Port portion of URI */
8105 const char *name
; /* Printer name */
8106 cupsd_printer_t
*printer
; /* Printer */
8109 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
8110 con
->http
.fd
, uri
->values
[0].string
.text
);
8113 * Is the destination valid?
8116 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8117 sizeof(method
), username
, sizeof(username
), host
,
8118 sizeof(host
), &port
, resource
, sizeof(resource
));
8120 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
8126 send_ipp_status(con
, IPP_NOT_FOUND
,
8127 _("The printer or class was not found."));
8135 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
8137 send_http_error(con
, status
);
8142 * Set it as the default...
8145 DefaultPrinter
= printer
;
8147 cupsdSaveAllPrinters();
8148 cupsdSaveAllClasses();
8150 cupsdWritePrintcap();
8152 cupsdLogMessage(CUPSD_LOG_INFO
,
8153 "Default destination set to \"%s\" by \"%s\".", name
,
8157 * Everything was ok, so return OK status...
8160 con
->response
->request
.status
.status_code
= IPP_OK
;
8165 * 'set_job_attrs()' - Set job attributes.
8169 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
8170 ipp_attribute_t
*uri
) /* I - Job URI */
8172 ipp_attribute_t
*attr
, /* Current attribute */
8173 *attr2
; /* Job attribute */
8174 int jobid
; /* Job ID */
8175 cupsd_job_t
*job
; /* Current job */
8176 char method
[HTTP_MAX_URI
],
8177 /* Method portion of URI */
8178 username
[HTTP_MAX_URI
],
8179 /* Username portion of URI */
8181 /* Host portion of URI */
8182 resource
[HTTP_MAX_URI
];
8183 /* Resource portion of URI */
8184 int port
; /* Port portion of URI */
8187 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
8188 con
->http
.fd
, uri
->values
[0].string
.text
);
8191 * Start with "everything is OK" status...
8194 con
->response
->request
.status
.status_code
= IPP_OK
;
8197 * See if we have a job URI or a printer URI...
8200 if (!strcmp(uri
->name
, "printer-uri"))
8203 * Got a printer URI; see if we also have a job-id attribute...
8206 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8207 IPP_TAG_INTEGER
)) == NULL
)
8209 send_ipp_status(con
, IPP_BAD_REQUEST
,
8210 _("Got a printer-uri attribute but no job-id!"));
8214 jobid
= attr
->values
[0].integer
;
8219 * Got a job URI; parse it to get the job ID...
8222 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8223 sizeof(method
), username
, sizeof(username
), host
,
8224 sizeof(host
), &port
, resource
, sizeof(resource
));
8226 if (strncmp(resource
, "/jobs/", 6))
8232 send_ipp_status(con
, IPP_BAD_REQUEST
,
8233 _("Bad job-uri attribute \"%s\"!"),
8234 uri
->values
[0].string
.text
);
8238 jobid
= atoi(resource
+ 6);
8242 * See if the job exists...
8245 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8248 * Nope - return a "not found" error...
8251 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8256 * See if the job has been completed...
8259 if (job
->state_value
> IPP_JOB_STOPPED
)
8262 * Return a "not-possible" error...
8265 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8266 _("Job #%d is finished and cannot be altered!"), jobid
);
8271 * See if the job is owned by the requesting user...
8274 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8276 send_http_error(con
, HTTP_UNAUTHORIZED
);
8281 * See what the user wants to change.
8286 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8288 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8291 if (!strcmp(attr
->name
, "attributes-charset") ||
8292 !strcmp(attr
->name
, "attributes-natural-language") ||
8293 !strcmp(attr
->name
, "document-compression") ||
8294 !strcmp(attr
->name
, "document-format") ||
8295 !strcmp(attr
->name
, "job-detailed-status-messages") ||
8296 !strcmp(attr
->name
, "job-document-access-errors") ||
8297 !strcmp(attr
->name
, "job-id") ||
8298 !strcmp(attr
->name
, "job-k-octets") ||
8299 !strcmp(attr
->name
, "job-originating-host-name") ||
8300 !strcmp(attr
->name
, "job-originating-user-name") ||
8301 !strcmp(attr
->name
, "job-printer-up-time") ||
8302 !strcmp(attr
->name
, "job-printer-uri") ||
8303 !strcmp(attr
->name
, "job-sheets") ||
8304 !strcmp(attr
->name
, "job-state-message") ||
8305 !strcmp(attr
->name
, "job-state-reasons") ||
8306 !strcmp(attr
->name
, "job-uri") ||
8307 !strcmp(attr
->name
, "number-of-documents") ||
8308 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
8309 !strcmp(attr
->name
, "output-device-assigned") ||
8310 !strncmp(attr
->name
, "date-time-at-", 13) ||
8311 !strncmp(attr
->name
, "job-impressions", 15) ||
8312 !strncmp(attr
->name
, "job-k-octets", 12) ||
8313 !strncmp(attr
->name
, "job-media-sheets", 16) ||
8314 !strncmp(attr
->name
, "time-at-", 8))
8320 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
8321 _("%s cannot be changed."), attr
->name
);
8323 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8324 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8329 if (!strcmp(attr
->name
, "job-priority"))
8332 * Change the job priority...
8335 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8337 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
8339 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8340 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8342 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
8344 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8345 _("Job is completed and cannot be changed."));
8348 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8349 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
8351 else if (!strcmp(attr
->name
, "job-state"))
8354 * Change the job state...
8357 if (attr
->value_tag
!= IPP_TAG_ENUM
)
8359 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
8361 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
8362 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
8366 switch (attr
->values
[0].integer
)
8368 case IPP_JOB_PENDING
:
8370 if (job
->state_value
> IPP_JOB_HELD
)
8372 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8373 _("Job state cannot be changed."));
8376 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8378 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8379 job
->state_value
= attr
->values
[0].integer
;
8383 case IPP_JOB_PROCESSING
:
8384 case IPP_JOB_STOPPED
:
8385 if (job
->state_value
!= attr
->values
[0].integer
)
8387 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8388 _("Job state cannot be changed."));
8393 case IPP_JOB_CANCELLED
:
8394 case IPP_JOB_ABORTED
:
8395 case IPP_JOB_COMPLETED
:
8396 if (job
->state_value
> IPP_JOB_PROCESSING
)
8398 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8399 _("Job state cannot be changed."));
8402 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
8404 cupsdCancelJob(job
, 0);
8408 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
8409 job
->state_value
= attr
->values
[0].integer
;
8417 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
8419 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8420 IPP_TAG_ZERO
)) != NULL
)
8423 * Some other value; first free the old value...
8426 if (job
->attrs
->prev
)
8427 job
->attrs
->prev
->next
= attr2
->next
;
8429 job
->attrs
->attrs
= attr2
->next
;
8431 if (job
->attrs
->last
== attr2
)
8432 job
->attrs
->last
= job
->attrs
->prev
;
8434 _ippFreeAttr(attr2
);
8437 * Then copy the attribute...
8440 copy_attribute(job
->attrs
, attr
, 0);
8443 * See if the job-name or job-hold-until is being changed.
8446 if (!strcmp(attr
->name
, "job-hold-until"))
8448 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
8450 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
8451 cupsdReleaseJob(job
);
8456 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
8459 * Delete the attribute...
8462 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
8463 IPP_TAG_ZERO
)) != NULL
)
8465 if (job
->attrs
->prev
)
8466 job
->attrs
->prev
->next
= attr2
->next
;
8468 job
->attrs
->attrs
= attr2
->next
;
8470 if (attr2
== job
->attrs
->last
)
8471 job
->attrs
->last
= job
->attrs
->prev
;
8473 _ippFreeAttr(attr2
);
8479 * Add new option by copying it...
8482 copy_attribute(job
->attrs
, attr
, 0);
8493 * Start jobs if possible...
8501 * 'set_printer_defaults()' - Set printer default options from a request.
8505 set_printer_defaults(
8506 cupsd_client_t
*con
, /* I - Client connection */
8507 cupsd_printer_t
*printer
) /* I - Printer */
8509 int i
; /* Looping var */
8510 ipp_attribute_t
*attr
; /* Current attribute */
8511 int namelen
; /* Length of attribute name */
8512 char name
[256], /* New attribute name */
8513 value
[256]; /* String version of integer attrs */
8516 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
8519 * Skip non-printer attributes...
8522 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
8525 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
8527 if (!strcmp(attr
->name
, "job-sheets-default"))
8530 * Only allow keywords and names...
8533 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8537 * Only allow job-sheets-default to be set when running without a
8538 * system high classification level...
8544 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
8546 if (attr
->num_values
> 1)
8547 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
8549 cupsdSetString(&printer
->job_sheets
[1], "none");
8551 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
8553 cupsdFreePrinterUsers(printer
);
8555 printer
->deny_users
= 0;
8557 if (attr
->value_tag
== IPP_TAG_NAME
&&
8558 (attr
->num_values
> 1 ||
8559 strcmp(attr
->values
[0].string
.text
, "all")))
8561 for (i
= 0; i
< attr
->num_values
; i
++)
8562 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8565 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
8567 cupsdFreePrinterUsers(printer
);
8569 printer
->deny_users
= 1;
8571 if (attr
->value_tag
== IPP_TAG_NAME
&&
8572 (attr
->num_values
> 1 ||
8573 strcmp(attr
->values
[0].string
.text
, "none")))
8575 for (i
= 0; i
< attr
->num_values
; i
++)
8576 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
8579 else if (!strcmp(attr
->name
, "job-quota-period"))
8581 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8584 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
8585 attr
->values
[0].integer
);
8586 cupsdFreeQuotas(printer
);
8588 printer
->quota_period
= attr
->values
[0].integer
;
8590 else if (!strcmp(attr
->name
, "job-k-limit"))
8592 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8595 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
8596 attr
->values
[0].integer
);
8597 cupsdFreeQuotas(printer
);
8599 printer
->k_limit
= attr
->values
[0].integer
;
8601 else if (!strcmp(attr
->name
, "job-page-limit"))
8603 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
8606 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
8607 attr
->values
[0].integer
);
8608 cupsdFreeQuotas(printer
);
8610 printer
->page_limit
= attr
->values
[0].integer
;
8612 else if (!strcmp(attr
->name
, "printer-op-policy"))
8614 cupsd_policy_t
*p
; /* Policy */
8617 if (attr
->value_tag
!= IPP_TAG_NAME
)
8620 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
8622 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8623 "Setting printer-op-policy to \"%s\"...",
8624 attr
->values
[0].string
.text
);
8625 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
8626 printer
->op_policy_ptr
= p
;
8630 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8631 _("Unknown printer-op-policy \"%s\"."),
8632 attr
->values
[0].string
.text
);
8636 else if (!strcmp(attr
->name
, "printer-error-policy"))
8638 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
8641 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
8642 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
8643 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
8645 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8646 _("Unknown printer-error-policy \"%s\"."),
8647 attr
->values
[0].string
.text
);
8651 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8652 "Setting printer-error-policy to \"%s\"...",
8653 attr
->values
[0].string
.text
);
8654 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
8656 else if (!strcmp(attr
->name
, "document-format-default") ||
8657 !strcmp(attr
->name
, "notify-lease-duration-default") ||
8658 !strcmp(attr
->name
, "notify-notify-events-default"))
8662 * Skip any other non-default attributes...
8665 namelen
= strlen(attr
->name
);
8666 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
8667 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
8671 * OK, anything else must be a user-defined default...
8674 strlcpy(name
, attr
->name
, sizeof(name
));
8675 name
[namelen
- 8] = '\0'; /* Strip "-default" */
8677 switch (attr
->value_tag
)
8679 case IPP_TAG_DELETEATTR
:
8680 printer
->num_options
= cupsRemoveOption(name
,
8681 printer
->num_options
,
8682 &(printer
->options
));
8683 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8684 "Deleting %s", attr
->name
);
8688 case IPP_TAG_KEYWORD
:
8690 printer
->num_options
= cupsAddOption(name
,
8691 attr
->values
[0].string
.text
,
8692 printer
->num_options
,
8693 &(printer
->options
));
8694 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8695 "Setting %s to \"%s\"...", attr
->name
,
8696 attr
->values
[0].string
.text
);
8699 case IPP_TAG_BOOLEAN
:
8700 printer
->num_options
= cupsAddOption(name
,
8701 attr
->values
[0].boolean
?
8703 printer
->num_options
,
8704 &(printer
->options
));
8705 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8706 "Setting %s to %s...", attr
->name
,
8707 attr
->values
[0].boolean
? "true" : "false");
8710 case IPP_TAG_INTEGER
:
8712 sprintf(value
, "%d", attr
->values
[0].integer
);
8713 printer
->num_options
= cupsAddOption(name
, value
,
8714 printer
->num_options
,
8715 &(printer
->options
));
8716 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8717 "Setting %s to %s...", attr
->name
, value
);
8720 case IPP_TAG_RANGE
:
8721 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
8722 attr
->values
[0].range
.upper
);
8723 printer
->num_options
= cupsAddOption(name
, value
,
8724 printer
->num_options
,
8725 &(printer
->options
));
8726 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8727 "Setting %s to %s...", attr
->name
, value
);
8730 case IPP_TAG_RESOLUTION
:
8731 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
8732 attr
->values
[0].resolution
.yres
,
8733 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
8735 printer
->num_options
= cupsAddOption(name
, value
,
8736 printer
->num_options
,
8737 &(printer
->options
));
8738 cupsdLogMessage(CUPSD_LOG_DEBUG
,
8739 "Setting %s to %s...", attr
->name
, value
);
8743 /* Do nothing for other values */
8751 * 'start_printer()' - Start a printer.
8755 start_printer(cupsd_client_t
*con
, /* I - Client connection */
8756 ipp_attribute_t
*uri
) /* I - Printer URI */
8758 http_status_t status
; /* Policy status */
8759 cups_ptype_t dtype
; /* Destination type (printer or class) */
8760 char method
[HTTP_MAX_URI
],
8761 /* Method portion of URI */
8762 username
[HTTP_MAX_URI
],
8763 /* Username portion of URI */
8765 /* Host portion of URI */
8766 resource
[HTTP_MAX_URI
];
8767 /* Resource portion of URI */
8768 int port
; /* Port portion of URI */
8769 const char *name
; /* Printer name */
8770 cupsd_printer_t
*printer
; /* Printer data */
8773 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
8774 con
->http
.fd
, uri
->values
[0].string
.text
);
8777 * Is the destination valid?
8780 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8781 sizeof(method
), username
, sizeof(username
), host
,
8782 sizeof(host
), &port
, resource
, sizeof(resource
));
8784 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
8790 send_ipp_status(con
, IPP_NOT_FOUND
,
8791 _("The printer or class was not found."));
8799 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8801 send_http_error(con
, status
);
8806 * Start the printer...
8809 printer
->state_message
[0] = '\0';
8811 cupsdStartPrinter(printer
, 1);
8813 if (dtype
& CUPS_PRINTER_CLASS
)
8814 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".", name
,
8817 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".", name
,
8823 * Everything was ok, so return OK status...
8826 con
->response
->request
.status
.status_code
= IPP_OK
;
8831 * 'stop_printer()' - Stop a printer.
8835 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
8836 ipp_attribute_t
*uri
) /* I - Printer URI */
8838 http_status_t status
; /* Policy status */
8839 cups_ptype_t dtype
; /* Destination type (printer or class) */
8840 char method
[HTTP_MAX_URI
],
8841 /* Method portion of URI */
8842 username
[HTTP_MAX_URI
],
8843 /* Username portion of URI */
8845 /* Host portion of URI */
8846 resource
[HTTP_MAX_URI
];
8847 /* Resource portion of URI */
8848 int port
; /* Port portion of URI */
8849 const char *name
; /* Printer name */
8850 cupsd_printer_t
*printer
; /* Printer data */
8851 ipp_attribute_t
*attr
; /* printer-state-message attribute */
8854 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
8855 con
->http
.fd
, uri
->values
[0].string
.text
);
8858 * Is the destination valid?
8861 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8862 sizeof(method
), username
, sizeof(username
), host
,
8863 sizeof(host
), &port
, resource
, sizeof(resource
));
8865 if ((name
= cupsdValidateDest(host
, resource
, &dtype
, &printer
)) == NULL
)
8871 send_ipp_status(con
, IPP_NOT_FOUND
,
8872 _("The printer or class was not found."));
8880 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8882 send_http_error(con
, status
);
8887 * Stop the printer...
8890 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8891 IPP_TAG_TEXT
)) == NULL
)
8892 strcpy(printer
->state_message
, "Paused");
8895 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8896 sizeof(printer
->state_message
));
8899 cupsdStopPrinter(printer
, 1);
8901 if (dtype
& CUPS_PRINTER_CLASS
)
8902 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".", name
,
8905 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".", name
,
8909 * Everything was ok, so return OK status...
8912 con
->response
->request
.status
.status_code
= IPP_OK
;
8917 * 'user_allowed()' - See if a user is allowed to print to a queue.
8920 static int /* O - 0 if not allowed, 1 if allowed */
8921 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
8922 const char *username
) /* I - Username */
8924 int i
; /* Looping var */
8925 struct passwd
*pw
; /* User password data */
8928 if (p
->num_users
== 0)
8931 if (!strcmp(username
, "root"))
8934 pw
= getpwnam(username
);
8937 for (i
= 0; i
< p
->num_users
; i
++)
8939 if (p
->users
[i
][0] == '@')
8942 * Check group membership...
8945 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
8948 else if (!strcasecmp(username
, p
->users
[i
]))
8952 return ((i
< p
->num_users
) != p
->deny_users
);
8957 * 'validate_job()' - Validate printer options and destination.
8961 validate_job(cupsd_client_t
*con
, /* I - Client connection */
8962 ipp_attribute_t
*uri
) /* I - Printer URI */
8964 http_status_t status
; /* Policy status */
8965 ipp_attribute_t
*attr
; /* Current attribute */
8966 ipp_attribute_t
*format
; /* Document-format attribute */
8967 cups_ptype_t dtype
; /* Destination type (printer or class) */
8968 char method
[HTTP_MAX_URI
],
8969 /* Method portion of URI */
8970 username
[HTTP_MAX_URI
],
8971 /* Username portion of URI */
8973 /* Host portion of URI */
8974 resource
[HTTP_MAX_URI
];
8975 /* Resource portion of URI */
8976 int port
; /* Port portion of URI */
8977 char super
[MIME_MAX_SUPER
],
8978 /* Supertype of file */
8979 type
[MIME_MAX_TYPE
];
8980 /* Subtype of file */
8981 cupsd_printer_t
*printer
; /* Printer */
8984 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
8985 con
->http
.fd
, uri
->values
[0].string
.text
);
8988 * OK, see if the client is sending the document compressed - CUPS
8989 * doesn't support compression yet...
8992 if ((attr
= ippFindAttribute(con
->request
, "compression",
8993 IPP_TAG_KEYWORD
)) != NULL
&&
8994 !strcmp(attr
->values
[0].string
.text
, "none"))
8996 send_ipp_status(con
, IPP_ATTRIBUTES
,
8997 _("Unsupported compression attribute %s!"),
8998 attr
->values
[0].string
.text
);
8999 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9000 "compression", NULL
, attr
->values
[0].string
.text
);
9005 * Is it a format we support?
9008 if ((format
= ippFindAttribute(con
->request
, "document-format",
9009 IPP_TAG_MIMETYPE
)) != NULL
)
9011 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
, type
) != 2)
9013 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9014 format
->values
[0].string
.text
);
9018 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
9019 !mimeType(MimeDatabase
, super
, type
))
9021 cupsdLogMessage(CUPSD_LOG_INFO
,
9022 "Hint: Do you have the raw file printing rules enabled?");
9023 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9024 _("Unsupported format \"%s\"!"),
9025 format
->values
[0].string
.text
);
9026 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9027 "document-format", NULL
, format
->values
[0].string
.text
);
9033 * Is the destination valid?
9036 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9037 sizeof(method
), username
, sizeof(username
), host
,
9038 sizeof(host
), &port
, resource
, sizeof(resource
));
9040 if (cupsdValidateDest(host
, resource
, &dtype
, &printer
) == NULL
)
9046 send_ipp_status(con
, IPP_NOT_FOUND
,
9047 _("The printer or class was not found."));
9055 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9057 send_http_error(con
, status
);
9062 * Everything was ok, so return OK status...
9065 con
->response
->request
.status
.status_code
= IPP_OK
;
9070 * 'validate_name()' - Make sure the printer name only contains valid chars.
9073 static int /* O - 0 if name is no good, 1 if name is good */
9074 validate_name(const char *name
) /* I - Name to check */
9076 const char *ptr
; /* Pointer into name */
9080 * Scan the whole name...
9083 for (ptr
= name
; *ptr
; ptr
++)
9084 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
9088 * All the characters are good; validate the length, too...
9091 return ((ptr
- name
) < 128);
9096 * 'validate_user()' - Validate the user for the request.
9099 static int /* O - 1 if permitted, 0 otherwise */
9100 validate_user(cupsd_job_t
*job
, /* I - Job */
9101 cupsd_client_t
*con
, /* I - Client connection */
9102 const char *owner
, /* I - Owner of job/resource */
9103 char *username
, /* O - Authenticated username */
9104 int userlen
) /* I - Length of username */
9106 cupsd_printer_t
*printer
; /* Printer for job */
9109 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9110 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
9112 job
? job
->id
: 0, con
->http
.fd
, owner
? owner
: "(null)",
9119 if (!con
|| !owner
|| !username
|| userlen
<= 0)
9123 * Get the best authenticated username that is available.
9126 strlcpy(username
, get_username(con
), userlen
);
9129 * Check the username against the owner...
9132 printer
= cupsdFindDest(job
->dest
);
9134 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
9135 con
, owner
) == HTTP_OK
);
9140 * End of "$Id: ipp.c 5334 2006-03-24 01:20:03Z mike $".