2 * "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $"
4 * IPP routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
23 * Include necessary headers...
30 #endif /* HAVE_LIBPAPER */
33 # include <ApplicationServices/ApplicationServices.h>
34 # include <CoreFoundation/CoreFoundation.h>
35 # ifdef HAVE_MEMBERSHIP_H
36 # include <membership.h>
37 # endif /* HAVE_MEMBERSHIP_H */
38 # ifdef HAVE_MEMBERSHIPPRIV_H
39 # include <membershipPriv.h>
41 extern int mbr_user_name_to_uuid(const char* name
, uuid_t uu
);
42 extern int mbr_group_name_to_uuid(const char* name
, uuid_t uu
);
43 extern int mbr_check_membership_by_id(uuid_t user
, gid_t group
, int* ismember
);
44 # endif /* HAVE_MEMBERSHIPPRIV_H */
45 #endif /* __APPLE__ */
52 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
53 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
54 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
55 mime_type_t
*filetype
, int compression
);
56 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
57 mime_type_t
*filetype
);
58 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
59 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
60 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
61 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
62 static void add_printer_state_reasons(cupsd_client_t
*con
,
64 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
66 static unsigned apple_hash_name(const char *name
);
67 static void apple_init_profile(CMDeviceProfileInfo
*profile
, unsigned id
,
68 const char *name
, const char *iccfile
);
69 static void apple_register_profiles(cupsd_printer_t
*p
);
70 static void apple_unregister_profiles(cupsd_printer_t
*p
);
71 #endif /* __APPLE__ */
72 static void apply_printer_defaults(cupsd_printer_t
*printer
,
74 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
75 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
76 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
77 static void cancel_subscription(cupsd_client_t
*con
, int id
);
78 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
79 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
81 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
82 ipp_tag_t group
, int quickcopy
);
83 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
85 static int copy_file(const char *from
, const char *to
);
86 static int copy_model(cupsd_client_t
*con
, const char *from
,
88 static void copy_job_attrs(cupsd_client_t
*con
,
91 static void copy_printer_attrs(cupsd_client_t
*con
,
92 cupsd_printer_t
*printer
,
94 static void copy_subscription_attrs(cupsd_client_t
*con
,
95 cupsd_subscription_t
*sub
,
97 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
98 static cups_array_t
*create_requested_array(ipp_t
*request
);
99 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
100 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
101 static void get_default(cupsd_client_t
*con
);
102 static void get_devices(cupsd_client_t
*con
);
103 static void get_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
104 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
105 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
106 static void get_notifications(cupsd_client_t
*con
);
107 static void get_ppd(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
108 static void get_ppds(cupsd_client_t
*con
);
109 static void get_printers(cupsd_client_t
*con
, int type
);
110 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
111 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
112 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
113 static const char *get_username(cupsd_client_t
*con
);
114 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
115 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
116 static int ppd_parse_line(const char *line
, char *option
, int olen
,
117 char *choice
, int clen
);
118 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
119 static void read_ps_job_ticket(cupsd_client_t
*con
);
120 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
121 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
122 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
123 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
124 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
,
125 ipp_attribute_t
*auth_info
);
126 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
127 static void save_krb5_creds(cupsd_client_t
*con
, cupsd_job_t
*job
);
128 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
129 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
130 static void send_http_error(cupsd_client_t
*con
, http_status_t status
,
131 cupsd_printer_t
*printer
);
132 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
133 const char *message
, ...)
135 __attribute__ ((__format__ (__printf__
, 3, 4)))
136 # endif /* __GNUC__ */
138 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
139 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
140 static void set_printer_defaults(cupsd_client_t
*con
,
141 cupsd_printer_t
*printer
);
142 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
143 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
144 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
,
146 static char *url_encode_string(const char *s
, char *buffer
, int bufsize
);
147 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
148 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
149 static int validate_name(const char *name
);
150 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
,
151 const char *owner
, char *username
,
156 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
159 int /* O - 1 on success, 0 on failure */
160 cupsdProcessIPPRequest(
161 cupsd_client_t
*con
) /* I - Client connection */
163 ipp_tag_t group
; /* Current group tag */
164 ipp_attribute_t
*attr
; /* Current attribute */
165 ipp_attribute_t
*charset
; /* Character set attribute */
166 ipp_attribute_t
*language
; /* Language attribute */
167 ipp_attribute_t
*uri
; /* Printer URI attribute */
168 ipp_attribute_t
*username
; /* requesting-user-name attr */
169 int sub_id
; /* Subscription ID */
172 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
173 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
174 con
, con
->http
.fd
, con
->request
->request
.op
.operation_id
);
177 * First build an empty response message for this request...
180 con
->response
= ippNew();
182 con
->response
->request
.status
.version
[0] =
183 con
->request
->request
.op
.version
[0];
184 con
->response
->request
.status
.version
[1] =
185 con
->request
->request
.op
.version
[1];
186 con
->response
->request
.status
.request_id
=
187 con
->request
->request
.op
.request_id
;
190 * Then validate the request header and required attributes...
193 if (con
->request
->request
.any
.version
[0] != 1)
196 * Return an error, since we only support IPP 1.x.
199 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
200 "%04X %s Bad request version number %d.%d",
201 IPP_VERSION_NOT_SUPPORTED
, con
->http
.hostname
,
202 con
->request
->request
.any
.version
[0],
203 con
->request
->request
.any
.version
[1]);
205 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
206 _("Bad request version number %d.%d!"),
207 con
->request
->request
.any
.version
[0],
208 con
->request
->request
.any
.version
[1]);
210 else if (!con
->request
->attrs
)
212 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
213 "%04X %s No attributes in request",
214 IPP_BAD_REQUEST
, con
->http
.hostname
);
216 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request!"));
221 * Make sure that the attributes are provided in the correct order and
222 * don't repeat groups...
225 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
228 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
231 * Out of order; return an error...
234 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
235 "%04X %s Attribute groups are out of order",
236 IPP_BAD_REQUEST
, con
->http
.hostname
);
238 send_ipp_status(con
, IPP_BAD_REQUEST
,
239 _("Attribute groups are out of order (%x < %x)!"),
240 attr
->group_tag
, group
);
244 group
= attr
->group_tag
;
249 * Then make sure that the first three attributes are:
252 * attributes-natural-language
253 * printer-uri/job-uri
256 attr
= con
->request
->attrs
;
257 if (attr
&& !strcmp(attr
->name
, "attributes-charset") &&
258 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
266 if (attr
&& !strcmp(attr
->name
, "attributes-natural-language") &&
267 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
272 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
273 IPP_TAG_URI
)) != NULL
)
275 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
276 IPP_TAG_URI
)) != NULL
)
278 else if (con
->request
->request
.op
.operation_id
== CUPS_GET_PPD
)
279 uri
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
);
284 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
285 "attributes-charset", NULL
,
286 charset
->values
[0].string
.text
);
288 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
289 "attributes-charset", NULL
, DefaultCharset
);
292 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
293 "attributes-natural-language", NULL
,
294 language
->values
[0].string
.text
);
296 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
297 "attributes-natural-language", NULL
, DefaultLanguage
);
300 strcasecmp(charset
->values
[0].string
.text
, "us-ascii") &&
301 strcasecmp(charset
->values
[0].string
.text
, "utf-8"))
304 * Bad character set...
307 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unsupported character set \"%s\"!",
308 charset
->values
[0].string
.text
);
309 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
310 "%04X %s Unsupported attributes-charset value \"%s\"",
311 IPP_CHARSET
, con
->http
.hostname
,
312 charset
->values
[0].string
.text
);
313 send_ipp_status(con
, IPP_BAD_REQUEST
,
314 _("Unsupported character set \"%s\"!"),
315 charset
->values
[0].string
.text
);
317 else if (!charset
|| !language
||
319 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
320 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
321 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
322 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
323 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
326 * Return an error, since attributes-charset,
327 * attributes-natural-language, and printer-uri/job-uri are required
328 * for all operations.
333 cupsdLogMessage(CUPSD_LOG_ERROR
,
334 "Missing attributes-charset attribute!");
336 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
337 "%04X %s Missing attributes-charset attribute",
338 IPP_BAD_REQUEST
, con
->http
.hostname
);
343 cupsdLogMessage(CUPSD_LOG_ERROR
,
344 "Missing attributes-natural-language attribute!");
346 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
347 "%04X %s Missing attributes-natural-language attribute",
348 IPP_BAD_REQUEST
, con
->http
.hostname
);
353 cupsdLogMessage(CUPSD_LOG_ERROR
,
354 "Missing printer-uri, job-uri, or ppd-name "
357 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
358 "%04X %s Missing printer-uri, job-uri, or ppd-name "
359 "attribute", IPP_BAD_REQUEST
, con
->http
.hostname
);
362 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
364 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
365 cupsdLogMessage(CUPSD_LOG_DEBUG
,
366 "attr \"%s\": group_tag = %x, value_tag = %x",
367 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
370 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
372 send_ipp_status(con
, IPP_BAD_REQUEST
,
373 _("Missing required attributes!"));
378 * OK, all the checks pass so far; make sure requesting-user-name is
379 * not "root" from a remote host...
382 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
383 IPP_TAG_NAME
)) != NULL
)
386 * Check for root user...
389 if (!strcmp(username
->values
[0].string
.text
, "root") &&
390 strcasecmp(con
->http
.hostname
, "localhost") &&
391 strcmp(con
->username
, "root"))
394 * Remote unauthenticated user masquerading as local root...
397 _cupsStrFree(username
->values
[0].string
.text
);
398 username
->values
[0].string
.text
= _cupsStrAlloc(RemoteRoot
);
402 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
403 IPP_TAG_INTEGER
)) != NULL
)
404 sub_id
= attr
->values
[0].integer
;
409 * Then try processing the operation...
413 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
414 ippOpString(con
->request
->request
.op
.operation_id
),
415 uri
->values
[0].string
.text
);
417 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
418 ippOpString(con
->request
->request
.op
.operation_id
));
420 switch (con
->request
->request
.op
.operation_id
)
426 case IPP_VALIDATE_JOB
:
427 validate_job(con
, uri
);
430 case IPP_CREATE_JOB
:
431 create_job(con
, uri
);
434 case IPP_SEND_DOCUMENT
:
435 send_document(con
, uri
);
438 case IPP_CANCEL_JOB
:
439 cancel_job(con
, uri
);
442 case IPP_GET_JOB_ATTRIBUTES
:
443 get_job_attrs(con
, uri
);
450 case IPP_GET_PRINTER_ATTRIBUTES
:
451 get_printer_attrs(con
, uri
);
458 case IPP_RELEASE_JOB
:
459 release_job(con
, uri
);
462 case IPP_RESTART_JOB
:
463 restart_job(con
, uri
);
466 case IPP_PAUSE_PRINTER
:
467 stop_printer(con
, uri
);
470 case IPP_RESUME_PRINTER
:
471 start_printer(con
, uri
);
474 case IPP_PURGE_JOBS
:
475 cancel_all_jobs(con
, uri
);
478 case IPP_SET_JOB_ATTRIBUTES
:
479 set_job_attrs(con
, uri
);
482 case CUPS_GET_DEFAULT
:
486 case CUPS_GET_PRINTERS
:
487 get_printers(con
, 0);
490 case CUPS_GET_CLASSES
:
491 get_printers(con
, CUPS_PRINTER_CLASS
);
494 case CUPS_ADD_PRINTER
:
495 add_printer(con
, uri
);
498 case CUPS_DELETE_PRINTER
:
499 delete_printer(con
, uri
);
502 case CUPS_ADD_CLASS
:
506 case CUPS_DELETE_CLASS
:
507 delete_printer(con
, uri
);
510 case CUPS_ACCEPT_JOBS
:
511 case IPP_ENABLE_PRINTER
:
512 accept_jobs(con
, uri
);
515 case CUPS_REJECT_JOBS
:
516 case IPP_DISABLE_PRINTER
:
517 reject_jobs(con
, uri
);
520 case CUPS_SET_DEFAULT
:
521 set_default(con
, uri
);
524 case CUPS_GET_DEVICES
:
528 case CUPS_GET_DOCUMENT
:
529 get_document(con
, uri
);
544 case CUPS_AUTHENTICATE_JOB
:
545 authenticate_job(con
, uri
);
548 case IPP_CREATE_PRINTER_SUBSCRIPTION
:
549 case IPP_CREATE_JOB_SUBSCRIPTION
:
550 create_subscription(con
, uri
);
553 case IPP_GET_SUBSCRIPTION_ATTRIBUTES
:
554 get_subscription_attrs(con
, sub_id
);
557 case IPP_GET_SUBSCRIPTIONS
:
558 get_subscriptions(con
, uri
);
561 case IPP_RENEW_SUBSCRIPTION
:
562 renew_subscription(con
, sub_id
);
565 case IPP_CANCEL_SUBSCRIPTION
:
566 cancel_subscription(con
, sub_id
);
569 case IPP_GET_NOTIFICATIONS
:
570 get_notifications(con
);
574 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
575 "%04X %s Operation %04X (%s) not supported",
576 IPP_OPERATION_NOT_SUPPORTED
, con
->http
.hostname
,
577 con
->request
->request
.op
.operation_id
,
578 ippOpString(con
->request
->request
.op
.operation_id
));
580 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
581 _("%s not supported!"),
583 con
->request
->request
.op
.operation_id
));
593 * Sending data from the scheduler...
596 cupsdLogMessage(CUPSD_LOG_DEBUG
,
597 "cupsdProcessIPPRequest: %d status_code=%x (%s)",
598 con
->http
.fd
, con
->response
->request
.status
.status_code
,
599 ippErrorString(con
->response
->request
.status
.status_code
));
601 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp", CUPSD_AUTH_NONE
))
603 #ifdef CUPSD_USE_CHUNKING
605 * Because older versions of CUPS (1.1.17 and older) and some IPP
606 * clients do not implement chunking properly, we cannot use
607 * chunking by default. This may become the default in future
608 * CUPS releases, or we might add a configuration directive for
612 if (con
->http
.version
== HTTP_1_1
)
614 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n") < 0)
617 if (cupsdFlushHeader(con
) < 0)
620 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
623 #endif /* CUPSD_USE_CHUNKING */
625 size_t length
; /* Length of response */
628 length
= ippLength(con
->response
);
630 if (con
->file
>= 0 && !con
->pipe_pid
)
632 struct stat fileinfo
; /* File information */
635 if (!fstat(con
->file
, &fileinfo
))
636 length
+= fileinfo
.st_size
;
639 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
640 CUPS_LLCAST length
) < 0)
643 if (cupsdFlushHeader(con
) < 0)
646 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
647 con
->http
.data_remaining
= length
;
649 if (con
->http
.data_remaining
<= INT_MAX
)
650 con
->http
._data_remaining
= con
->http
.data_remaining
;
652 con
->http
._data_remaining
= INT_MAX
;
655 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
656 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
659 * Tell the caller the response header was sent successfully...
667 * Tell the caller the response header could not be sent...
676 * Sending data from a subprocess like cups-deviced; tell the caller
677 * everything is A-OK so far...
686 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
689 int /* O - 0 on success, -1 on error */
690 cupsdTimeoutJob(cupsd_job_t
*job
) /* I - Job to timeout */
692 cupsd_printer_t
*printer
; /* Destination printer or class */
693 ipp_attribute_t
*attr
; /* job-sheets attribute */
694 int kbytes
; /* Kilobytes in banner */
697 job
->pending_timeout
= 0;
700 * See if we need to add the ending sheet...
703 printer
= cupsdFindDest(job
->dest
);
704 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
707 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
708 attr
&& attr
->num_values
> 1)
714 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Adding end banner page \"%s\".",
715 job
->id
, attr
->values
[1].string
.text
);
717 if ((kbytes
= copy_banner(NULL
, job
, attr
->values
[1].string
.text
)) < 0)
720 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
728 * 'accept_jobs()' - Accept print jobs to a printer.
732 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
733 ipp_attribute_t
*uri
) /* I - Printer or class URI */
735 http_status_t status
; /* Policy status */
736 cups_ptype_t dtype
; /* Destination type (printer/class) */
737 cupsd_printer_t
*printer
; /* Printer data */
740 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
741 con
->http
.fd
, uri
->values
[0].string
.text
);
744 * Is the destination valid?
747 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
753 send_ipp_status(con
, IPP_NOT_FOUND
,
754 _("The printer or class was not found."));
762 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
764 send_http_error(con
, status
, printer
);
769 * Accept jobs sent to the printer...
772 printer
->accepting
= 1;
773 printer
->state_message
[0] = '\0';
775 cupsdAddPrinterHistory(printer
);
777 if (dtype
& CUPS_PRINTER_CLASS
)
779 cupsdSaveAllClasses();
781 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
782 printer
->name
, get_username(con
));
786 cupsdSaveAllPrinters();
788 cupsdLogMessage(CUPSD_LOG_INFO
,
789 "Printer \"%s\" now accepting jobs (\"%s\").",
790 printer
->name
, get_username(con
));
794 * Everything was ok, so return OK status...
797 con
->response
->request
.status
.status_code
= IPP_OK
;
802 * 'add_class()' - Add a class to the system.
806 add_class(cupsd_client_t
*con
, /* I - Client connection */
807 ipp_attribute_t
*uri
) /* I - URI of class */
809 http_status_t status
; /* Policy status */
810 int i
; /* Looping var */
811 char method
[HTTP_MAX_URI
], /* Method portion of URI */
812 username
[HTTP_MAX_URI
], /* Username portion of URI */
813 host
[HTTP_MAX_URI
], /* Host portion of URI */
814 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
815 int port
; /* Port portion of URI */
816 cupsd_printer_t
*pclass
, /* Class */
817 *member
; /* Member printer/class */
818 cups_ptype_t dtype
; /* Destination type */
819 ipp_attribute_t
*attr
; /* Printer attribute */
820 int modify
; /* Non-zero if we just modified */
821 char newname
[IPP_MAX_NAME
]; /* New class name */
822 int need_restart_job
; /* Need to restart job? */
825 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
826 con
->http
.fd
, uri
->values
[0].string
.text
);
829 * Do we have a valid URI?
832 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
833 sizeof(method
), username
, sizeof(username
), host
,
834 sizeof(host
), &port
, resource
, sizeof(resource
));
837 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
840 * No, return an error...
843 send_ipp_status(con
, IPP_BAD_REQUEST
,
844 _("The printer-uri must be of the form "
845 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
850 * Do we have a valid printer name?
853 if (!validate_name(resource
+ 9))
856 * No, return an error...
859 send_ipp_status(con
, IPP_BAD_REQUEST
,
860 _("The printer-uri \"%s\" contains invalid characters."),
861 uri
->values
[0].string
.text
);
866 * See if the class already exists; if not, create a new class...
869 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
872 * Class doesn't exist; see if we have a printer of the same name...
875 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
876 !(pclass
->type
& CUPS_PRINTER_DISCOVERED
))
879 * Yes, return an error...
882 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
883 _("A printer named \"%s\" already exists!"),
889 * No, check the default policy and then add the class...
892 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
894 send_http_error(con
, status
, NULL
);
898 pclass
= cupsdAddClass(resource
+ 9);
901 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
904 * Check the default policy, then rename the implicit class to "AnyClass"
908 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
910 send_http_error(con
, status
, NULL
);
914 if (ImplicitAnyClasses
)
916 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
917 cupsdRenamePrinter(pclass
, newname
);
920 cupsdDeletePrinter(pclass
, 1);
923 * Add the class as a new local class...
926 pclass
= cupsdAddClass(resource
+ 9);
929 else if (pclass
->type
& CUPS_PRINTER_DISCOVERED
)
932 * Check the default policy, then rename the remote class to "Class"...
935 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
937 send_http_error(con
, status
, NULL
);
941 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
942 cupsdRenamePrinter(pclass
, newname
);
945 * Add the class as a new local class...
948 pclass
= cupsdAddClass(resource
+ 9);
951 else if ((status
= cupsdCheckPolicy(pclass
->op_policy_ptr
, con
,
954 send_http_error(con
, status
, pclass
);
961 * Look for attributes and copy them over as needed...
964 need_restart_job
= 0;
966 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
967 IPP_TAG_TEXT
)) != NULL
)
968 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
970 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
971 IPP_TAG_TEXT
)) != NULL
)
972 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
974 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
975 IPP_TAG_BOOLEAN
)) != NULL
)
977 cupsdLogMessage(CUPSD_LOG_INFO
,
978 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
979 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
981 pclass
->accepting
= attr
->values
[0].boolean
;
982 cupsdAddPrinterHistory(pclass
);
985 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
986 IPP_TAG_BOOLEAN
)) != NULL
)
988 if (pclass
->shared
&& !attr
->values
[0].boolean
)
989 cupsdDeregisterPrinter(pclass
, 1);
991 cupsdLogMessage(CUPSD_LOG_INFO
,
992 "Setting %s printer-is-shared to %d (was %d.)",
993 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
995 pclass
->shared
= attr
->values
[0].boolean
;
998 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
999 IPP_TAG_ENUM
)) != NULL
)
1001 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
1002 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
1004 send_ipp_status(con
, IPP_BAD_REQUEST
,
1005 _("Attempt to set %s printer-state to bad value %d!"),
1006 pclass
->name
, attr
->values
[0].integer
);
1010 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
1011 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
1013 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1014 cupsdStopPrinter(pclass
, 0);
1017 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1018 need_restart_job
= 1;
1021 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1022 IPP_TAG_TEXT
)) != NULL
)
1024 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1025 sizeof(pclass
->state_message
));
1026 cupsdAddPrinterHistory(pclass
);
1028 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1029 IPP_TAG_URI
)) != NULL
)
1032 * Clear the printer array as needed...
1035 need_restart_job
= 1;
1037 if (pclass
->num_printers
> 0)
1039 free(pclass
->printers
);
1040 pclass
->num_printers
= 0;
1044 * Add each printer or class that is listed...
1047 for (i
= 0; i
< attr
->num_values
; i
++)
1050 * Search for the printer or class URI...
1053 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1059 send_ipp_status(con
, IPP_NOT_FOUND
,
1060 _("The printer or class was not found."));
1065 * Add it to the class...
1068 cupsdAddPrinterToClass(pclass
, member
);
1072 set_printer_defaults(con
, pclass
);
1074 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1075 IPP_TAG_KEYWORD
)) != NULL
)
1076 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1079 * Update the printer class attributes and return...
1082 cupsdSetPrinterAttrs(pclass
);
1083 cupsdSaveAllClasses();
1085 if (need_restart_job
&& pclass
->job
)
1090 * Stop the current job and then restart it below...
1093 job
= (cupsd_job_t
*)pclass
->job
;
1095 cupsdStopJob(job
, 1);
1097 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1098 job
->state_value
= IPP_JOB_PENDING
;
1101 if (need_restart_job
)
1104 cupsdWritePrintcap();
1108 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1109 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1112 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1113 pclass
->name
, get_username(con
));
1117 cupsdAddPrinterHistory(pclass
);
1119 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1120 "New class \"%s\" added by \"%s\".", pclass
->name
,
1123 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1124 pclass
->name
, get_username(con
));
1127 con
->response
->request
.status
.status_code
= IPP_OK
;
1132 * 'add_file()' - Add a file to a job.
1135 static int /* O - 0 on success, -1 on error */
1136 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1137 cupsd_job_t
*job
, /* I - Job to add to */
1138 mime_type_t
*filetype
, /* I - Type of file */
1139 int compression
) /* I - Compression */
1141 mime_type_t
**filetypes
; /* New filetypes array... */
1142 int *compressions
; /* New compressions array... */
1145 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1146 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1147 "compression=%d)", con
, con
? con
->http
.fd
: -1, job
->id
,
1148 filetype
->super
, filetype
->type
, compression
);
1151 * Add the file to the job...
1154 if (job
->num_files
== 0)
1156 compressions
= (int *)malloc(sizeof(int));
1157 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1161 compressions
= (int *)realloc(job
->compressions
,
1162 (job
->num_files
+ 1) * sizeof(int));
1163 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1164 (job
->num_files
+ 1) *
1165 sizeof(mime_type_t
*));
1168 if (!compressions
|| !filetypes
)
1170 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
1173 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1174 _("Unable to allocate memory for file types!"));
1179 job
->compressions
= compressions
;
1180 job
->compressions
[job
->num_files
] = compression
;
1181 job
->filetypes
= filetypes
;
1182 job
->filetypes
[job
->num_files
] = filetype
;
1191 * 'add_job()' - Add a job to a print queue.
1194 static cupsd_job_t
* /* O - Job object */
1195 add_job(cupsd_client_t
*con
, /* I - Client connection */
1196 cupsd_printer_t
*printer
, /* I - Destination printer */
1197 mime_type_t
*filetype
) /* I - First print file type, if any */
1199 http_status_t status
; /* Policy status */
1200 ipp_attribute_t
*attr
, /* Current attribute */
1201 *auth_info
; /* auth-info attribute */
1202 const char *val
; /* Default option value */
1203 int priority
; /* Job priority */
1204 char *title
; /* Job name/title */
1205 cupsd_job_t
*job
; /* Current job */
1206 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1207 int kbytes
; /* Size of print file */
1208 int i
; /* Looping var */
1209 int lowerpagerange
; /* Page range bound */
1212 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1213 con
, con
->http
.fd
, printer
, printer
->name
,
1214 filetype
, filetype
? filetype
->super
: "none",
1215 filetype
? filetype
->type
: "none");
1218 * Check remote printing to non-shared printer...
1221 if (!printer
->shared
&&
1222 strcasecmp(con
->http
.hostname
, "localhost") &&
1223 strcasecmp(con
->http
.hostname
, ServerName
))
1225 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1226 _("The printer or class is not shared!"));
1234 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1236 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1238 send_http_error(con
, status
, printer
);
1241 else if ((printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
1242 !con
->username
[0] && !auth_info
)
1244 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1248 else if (auth_info
&& !con
->http
.tls
&&
1249 !httpAddrLocalhost(con
->http
.hostaddr
))
1252 * Require encryption of auth-info over non-local connections...
1255 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1258 #endif /* HAVE_SSL */
1261 * See if the printer is accepting jobs...
1264 if (!printer
->accepting
)
1266 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1267 _("Destination \"%s\" is not accepting jobs."),
1273 * Validate job template attributes; for now just document-format,
1274 * copies, number-up, and page-ranges...
1277 if (filetype
&& printer
->filetypes
&&
1278 !cupsArrayFind(printer
->filetypes
, filetype
))
1280 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1281 /* MIME media type string */
1284 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1287 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1288 _("Unsupported format \'%s\'!"), mimetype
);
1290 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1291 "document-format", NULL
, mimetype
);
1296 if ((attr
= ippFindAttribute(con
->request
, "copies",
1297 IPP_TAG_INTEGER
)) != NULL
)
1299 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1301 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1302 attr
->values
[0].integer
);
1303 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1304 "copies", attr
->values
[0].integer
);
1309 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1310 IPP_TAG_INTEGER
)) != NULL
)
1312 if (attr
->values
[0].integer
!= 1 &&
1313 attr
->values
[0].integer
!= 2 &&
1314 attr
->values
[0].integer
!= 4 &&
1315 attr
->values
[0].integer
!= 6 &&
1316 attr
->values
[0].integer
!= 9 &&
1317 attr
->values
[0].integer
!= 16)
1319 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1320 attr
->values
[0].integer
);
1321 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1322 "number-up", attr
->values
[0].integer
);
1327 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1328 IPP_TAG_RANGE
)) != NULL
)
1330 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1332 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1333 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1335 send_ipp_status(con
, IPP_BAD_REQUEST
,
1336 _("Bad page-ranges values %d-%d."),
1337 attr
->values
[i
].range
.lower
,
1338 attr
->values
[i
].range
.upper
);
1342 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1347 * Make sure we aren't over our limit...
1350 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1353 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1355 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1356 _("Too many active jobs."));
1360 if ((i
= check_quotas(con
, printer
)) < 0)
1362 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1367 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1372 * Create the job and set things up...
1375 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1376 IPP_TAG_INTEGER
)) != NULL
)
1377 priority
= attr
->values
[0].integer
;
1380 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1381 printer
->options
)) != NULL
)
1382 priority
= atoi(val
);
1386 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1390 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1391 IPP_TAG_NAME
)) != NULL
)
1392 title
= attr
->values
[0].string
.text
;
1394 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1395 title
= "Untitled");
1397 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1399 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1400 _("Unable to add job for destination \"%s\"!"),
1405 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
1406 CUPS_PRINTER_REMOTE
);
1407 job
->attrs
= con
->request
;
1408 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1410 add_job_uuid(con
, job
);
1411 apply_printer_defaults(printer
, job
);
1413 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1415 if (con
->username
[0])
1417 cupsdSetString(&job
->username
, con
->username
);
1420 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1424 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1425 "add_job: requesting-user-name=\"%s\"",
1426 attr
->values
[0].string
.text
);
1428 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1431 cupsdSetString(&job
->username
, "anonymous");
1434 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1435 "job-originating-user-name", NULL
, job
->username
);
1438 attr
->group_tag
= IPP_TAG_JOB
;
1439 _cupsStrFree(attr
->name
);
1440 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1443 if (con
->username
[0] || auth_info
)
1445 save_auth_info(con
, job
, auth_info
);
1448 * Remove the auth-info attribute from the attribute data...
1452 ippDeleteAttribute(job
->attrs
, auth_info
);
1455 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1456 IPP_TAG_ZERO
)) != NULL
)
1459 * Request contains a job-originating-host-name attribute; validate it...
1462 if (attr
->value_tag
!= IPP_TAG_NAME
||
1463 attr
->num_values
!= 1 ||
1464 strcmp(con
->http
.hostname
, "localhost"))
1467 * Can't override the value if we aren't connected via localhost.
1468 * Also, we can only have 1 value and it must be a name value.
1471 switch (attr
->value_tag
)
1473 case IPP_TAG_STRING
:
1474 case IPP_TAG_TEXTLANG
:
1475 case IPP_TAG_NAMELANG
:
1478 case IPP_TAG_KEYWORD
:
1480 case IPP_TAG_URISCHEME
:
1481 case IPP_TAG_CHARSET
:
1482 case IPP_TAG_LANGUAGE
:
1483 case IPP_TAG_MIMETYPE
:
1485 * Free old strings...
1488 for (i
= 0; i
< attr
->num_values
; i
++)
1490 _cupsStrFree(attr
->values
[i
].string
.text
);
1491 attr
->values
[i
].string
.text
= NULL
;
1492 if (attr
->values
[i
].string
.charset
)
1494 _cupsStrFree(attr
->values
[i
].string
.charset
);
1495 attr
->values
[i
].string
.charset
= NULL
;
1504 * Use the default connection hostname instead...
1507 attr
->value_tag
= IPP_TAG_NAME
;
1508 attr
->num_values
= 1;
1509 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1512 attr
->group_tag
= IPP_TAG_JOB
;
1517 * No job-originating-host-name attribute, so use the hostname from
1521 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1522 "job-originating-host-name", NULL
, con
->http
.hostname
);
1525 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1527 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1528 "time-at-processing", 0);
1529 attr
->value_tag
= IPP_TAG_NOVALUE
;
1530 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1531 "time-at-completed", 0);
1532 attr
->value_tag
= IPP_TAG_NOVALUE
;
1535 * Add remaining job attributes...
1538 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1539 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1540 "job-state", IPP_JOB_STOPPED
);
1541 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1542 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1543 "job-media-sheets-completed", 0);
1544 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1546 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1549 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1550 IPP_TAG_INTEGER
)) != NULL
)
1551 attr
->values
[0].integer
= 0;
1553 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1556 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1557 IPP_TAG_KEYWORD
)) == NULL
)
1558 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1561 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1562 printer
->options
)) == NULL
)
1565 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1566 "job-hold-until", NULL
, val
);
1568 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1569 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1572 * Hold job until specified time...
1575 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1577 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1578 job
->state_value
= IPP_JOB_HELD
;
1580 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1582 job
->hold_until
= time(NULL
) + 60;
1583 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1584 job
->state_value
= IPP_JOB_HELD
;
1588 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1589 job
->state_value
= IPP_JOB_PENDING
;
1592 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1596 * Add job sheets options...
1599 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1600 IPP_TAG_ZERO
)) == NULL
)
1602 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1603 "Adding default job-sheets values \"%s,%s\"...",
1604 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1606 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1608 attr
->values
[0].string
.text
= _cupsStrAlloc(printer
->job_sheets
[0]);
1609 attr
->values
[1].string
.text
= _cupsStrAlloc(printer
->job_sheets
[1]);
1612 job
->job_sheets
= attr
;
1615 * Enforce classification level if set...
1620 cupsdLogMessage(CUPSD_LOG_INFO
,
1621 "Classification=\"%s\", ClassifyOverride=%d",
1622 Classification
? Classification
: "(null)",
1625 if (ClassifyOverride
)
1627 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1628 (attr
->num_values
== 1 ||
1629 !strcmp(attr
->values
[1].string
.text
, "none")))
1632 * Force the leading banner to have the classification on it...
1635 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1637 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1638 "job-sheets=\"%s,none\", "
1639 "job-originating-user-name=\"%s\"",
1640 job
->id
, Classification
, job
->username
);
1642 else if (attr
->num_values
== 2 &&
1643 strcmp(attr
->values
[0].string
.text
,
1644 attr
->values
[1].string
.text
) &&
1645 strcmp(attr
->values
[0].string
.text
, "none") &&
1646 strcmp(attr
->values
[1].string
.text
, "none"))
1649 * Can't put two different security markings on the same document!
1652 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1654 cupsdLogMessage(CUPSD_LOG_NOTICE
, "[Job %d] CLASSIFICATION FORCED "
1655 "job-sheets=\"%s,%s\", "
1656 "job-originating-user-name=\"%s\"",
1657 job
->id
, attr
->values
[0].string
.text
,
1658 attr
->values
[1].string
.text
, job
->username
);
1660 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1661 strcmp(attr
->values
[0].string
.text
, "none") &&
1662 (attr
->num_values
== 1 ||
1663 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1664 strcmp(attr
->values
[1].string
.text
, "none"))))
1666 if (attr
->num_values
== 1)
1667 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1668 "[Job %d] CLASSIFICATION OVERRIDDEN "
1669 "job-sheets=\"%s\", "
1670 "job-originating-user-name=\"%s\"",
1671 job
->id
, attr
->values
[0].string
.text
, job
->username
);
1673 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1674 "[Job %d] CLASSIFICATION OVERRIDDEN "
1675 "job-sheets=\"%s,%s\",fffff "
1676 "job-originating-user-name=\"%s\"",
1677 job
->id
, attr
->values
[0].string
.text
,
1678 attr
->values
[1].string
.text
, job
->username
);
1681 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1682 (attr
->num_values
== 1 ||
1683 strcmp(attr
->values
[1].string
.text
, Classification
)))
1686 * Force the banner to have the classification on it...
1689 if (attr
->num_values
> 1 &&
1690 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1692 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1693 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1697 if (attr
->num_values
== 1 ||
1698 strcmp(attr
->values
[0].string
.text
, "none"))
1699 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1701 if (attr
->num_values
> 1 &&
1702 strcmp(attr
->values
[1].string
.text
, "none"))
1703 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1706 if (attr
->num_values
> 1)
1707 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1708 "[Job %d] CLASSIFICATION FORCED "
1709 "job-sheets=\"%s,%s\", "
1710 "job-originating-user-name=\"%s\"",
1711 job
->id
, attr
->values
[0].string
.text
,
1712 attr
->values
[1].string
.text
, job
->username
);
1714 cupsdLogMessage(CUPSD_LOG_NOTICE
,
1715 "[Job %d] CLASSIFICATION FORCED "
1716 "job-sheets=\"%s\", "
1717 "job-originating-user-name=\"%s\"",
1718 job
->id
, Classification
, job
->username
);
1723 * See if we need to add the starting sheet...
1726 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1728 cupsdLogMessage(CUPSD_LOG_INFO
,
1729 "[Job %d] Adding start banner page \"%s\".",
1730 job
->id
, attr
->values
[0].string
.text
);
1732 if ((kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
)) < 0)
1735 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1738 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1739 IPP_TAG_ZERO
)) != NULL
)
1743 * Fill in the response info...
1746 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1747 LocalPort
, job
->id
);
1749 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1752 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1754 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1756 add_job_state_reasons(con
, job
);
1758 con
->response
->request
.status
.status_code
= IPP_OK
;
1761 * Add any job subscriptions...
1764 add_job_subscriptions(con
, job
);
1767 * Set all but the first two attributes to the job attributes group...
1770 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1771 attr
->group_tag
= IPP_TAG_JOB
;
1774 * Fire the "job created" event...
1777 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1780 * Return the new job...
1788 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1789 * upon the job and printer state...
1793 add_job_state_reasons(
1794 cupsd_client_t
*con
, /* I - Client connection */
1795 cupsd_job_t
*job
) /* I - Job info */
1797 cupsd_printer_t
*dest
; /* Destination printer */
1800 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1801 con
, con
->http
.fd
, job
? job
->id
: 0);
1803 switch (job
? job
->state_value
: IPP_JOB_CANCELED
)
1805 case IPP_JOB_PENDING
:
1806 dest
= cupsdFindDest(job
->dest
);
1808 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1809 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1810 "job-state-reasons", NULL
, "printer-stopped");
1812 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1813 "job-state-reasons", NULL
, "none");
1817 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1818 IPP_TAG_KEYWORD
) != NULL
||
1819 ippFindAttribute(job
->attrs
, "job-hold-until",
1820 IPP_TAG_NAME
) != NULL
)
1821 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1822 "job-state-reasons", NULL
, "job-hold-until-specified");
1824 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1825 "job-state-reasons", NULL
, "job-incoming");
1828 case IPP_JOB_PROCESSING
:
1829 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1830 "job-state-reasons", NULL
, "job-printing");
1833 case IPP_JOB_STOPPED
:
1834 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1835 "job-state-reasons", NULL
, "job-stopped");
1838 case IPP_JOB_CANCELED
:
1839 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1840 "job-state-reasons", NULL
, "job-canceled-by-user");
1843 case IPP_JOB_ABORTED
:
1844 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1845 "job-state-reasons", NULL
, "aborted-by-system");
1848 case IPP_JOB_COMPLETED
:
1849 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1850 "job-state-reasons", NULL
, "job-completed-successfully");
1857 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1861 add_job_subscriptions(
1862 cupsd_client_t
*con
, /* I - Client connection */
1863 cupsd_job_t
*job
) /* I - Newly created job */
1865 int i
; /* Looping var */
1866 ipp_attribute_t
*prev
, /* Previous attribute */
1867 *next
, /* Next attribute */
1868 *attr
; /* Current attribute */
1869 cupsd_subscription_t
*sub
; /* Subscription object */
1870 const char *recipient
, /* notify-recipient-uri */
1871 *pullmethod
; /* notify-pull-method */
1872 ipp_attribute_t
*user_data
; /* notify-user-data */
1873 int interval
; /* notify-time-interval */
1874 unsigned mask
; /* notify-events */
1878 * Find the first subscription group attribute; return if we have
1882 for (attr
= job
->attrs
->attrs
, prev
= NULL
;
1884 prev
= attr
, attr
= attr
->next
)
1885 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1892 * Process the subscription attributes in the request...
1901 mask
= CUPSD_EVENT_NONE
;
1903 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1905 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
1906 attr
->value_tag
== IPP_TAG_URI
)
1907 recipient
= attr
->values
[0].string
.text
;
1908 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1909 attr
->value_tag
== IPP_TAG_KEYWORD
)
1910 pullmethod
= attr
->values
[0].string
.text
;
1911 else if (!strcmp(attr
->name
, "notify-charset") &&
1912 attr
->value_tag
== IPP_TAG_CHARSET
&&
1913 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
1914 strcmp(attr
->values
[0].string
.text
, "utf-8"))
1916 send_ipp_status(con
, IPP_CHARSET
,
1917 _("Character set \"%s\" not supported!"),
1918 attr
->values
[0].string
.text
);
1921 else if (!strcmp(attr
->name
, "notify-natural-language") &&
1922 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
1923 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
1925 send_ipp_status(con
, IPP_CHARSET
,
1926 _("Language \"%s\" not supported!"),
1927 attr
->values
[0].string
.text
);
1930 else if (!strcmp(attr
->name
, "notify-user-data") &&
1931 attr
->value_tag
== IPP_TAG_STRING
)
1933 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
1935 send_ipp_status(con
, IPP_REQUEST_VALUE
,
1936 _("The notify-user-data value is too large "
1937 "(%d > 63 octets)!"),
1938 attr
->values
[0].unknown
.length
);
1944 else if (!strcmp(attr
->name
, "notify-events") &&
1945 attr
->value_tag
== IPP_TAG_KEYWORD
)
1947 for (i
= 0; i
< attr
->num_values
; i
++)
1948 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
1950 else if (!strcmp(attr
->name
, "notify-lease-duration"))
1952 send_ipp_status(con
, IPP_BAD_REQUEST
,
1953 _("The notify-lease-duration attribute cannot be "
1954 "used with job subscriptions."));
1957 else if (!strcmp(attr
->name
, "notify-time-interval") &&
1958 attr
->value_tag
== IPP_TAG_INTEGER
)
1959 interval
= attr
->values
[0].integer
;
1964 if (!recipient
&& !pullmethod
)
1967 if (mask
== CUPSD_EVENT_NONE
)
1968 mask
= CUPSD_EVENT_JOB_COMPLETED
;
1970 sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
, recipient
,
1973 sub
->interval
= interval
;
1975 cupsdSetString(&sub
->owner
, job
->username
);
1979 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
1980 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
1981 sub
->user_data_len
);
1984 ippAddSeparator(con
->response
);
1985 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
1986 "notify-subscription-id", sub
->id
);
1992 cupsdSaveAllSubscriptions();
1995 * Remove all of the subscription attributes from the job request...
1998 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2002 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2003 attr
->group_tag
== IPP_TAG_ZERO
)
2006 * Free and remove this attribute...
2014 job
->attrs
->attrs
= next
;
2020 job
->attrs
->last
= prev
;
2021 job
->attrs
->current
= prev
;
2026 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2028 * See RFC 4122 for the definition of UUIDs and the format.
2032 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
2033 cupsd_job_t
*job
) /* I - Job */
2035 char uuid
[1024]; /* job-uuid string */
2036 _cups_md5_state_t md5state
; /* MD5 state */
2037 unsigned char md5sum
[16]; /* MD5 digest/sum */
2041 * First see if the job already has a job-uuid attribute; if so, return...
2044 if (ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2048 * No job-uuid attribute, so build a version 3 UUID with the local job
2049 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
2050 * the ServerName, server name and port that the client connected to,
2051 * and local job ID...
2054 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
2055 con
->serverport
, job
->id
);
2057 _cupsMD5Init(&md5state
);
2058 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
2059 _cupsMD5Finish(&md5state
, md5sum
);
2062 * Format the UUID URI using the MD5 sum and job ID.
2065 snprintf(uuid
, sizeof(uuid
),
2066 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2067 "%02x%02x%02x%02x%02x%02x",
2068 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
2069 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
2070 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
2071 md5sum
[14], md5sum
[15]);
2073 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
2078 * 'add_printer()' - Add a printer to the system.
2082 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2083 ipp_attribute_t
*uri
) /* I - URI of printer */
2085 http_status_t status
; /* Policy status */
2086 int i
; /* Looping var */
2087 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2088 username
[HTTP_MAX_URI
], /* Username portion of URI */
2089 host
[HTTP_MAX_URI
], /* Host portion of URI */
2090 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2091 int port
; /* Port portion of URI */
2092 cupsd_printer_t
*printer
; /* Printer/class */
2093 ipp_attribute_t
*attr
; /* Printer attribute */
2094 cups_file_t
*fp
; /* Script/PPD file */
2095 char line
[1024]; /* Line from file... */
2096 char srcfile
[1024], /* Source Script/PPD file */
2097 dstfile
[1024]; /* Destination Script/PPD file */
2098 int modify
; /* Non-zero if we are modifying */
2099 char newname
[IPP_MAX_NAME
]; /* New printer name */
2100 int need_restart_job
; /* Need to restart job? */
2101 int set_device_uri
, /* Did we set the device URI? */
2102 set_port_monitor
; /* Did we set the port monitor? */
2105 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2106 con
->http
.fd
, uri
->values
[0].string
.text
);
2109 * Do we have a valid URI?
2112 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2113 sizeof(scheme
), username
, sizeof(username
), host
,
2114 sizeof(host
), &port
, resource
, sizeof(resource
));
2116 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2119 * No, return an error...
2122 send_ipp_status(con
, IPP_BAD_REQUEST
,
2123 _("The printer-uri must be of the form "
2124 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2129 * Do we have a valid printer name?
2132 if (!validate_name(resource
+ 10))
2135 * No, return an error...
2138 send_ipp_status(con
, IPP_BAD_REQUEST
,
2139 _("The printer-uri \"%s\" contains invalid characters."),
2140 uri
->values
[0].string
.text
);
2145 * See if the printer already exists; if not, create a new printer...
2148 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2151 * Printer doesn't exist; see if we have a class of the same name...
2154 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2155 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
2158 * Yes, return an error...
2161 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2162 _("A class named \"%s\" already exists!"),
2168 * No, check the default policy then add the printer...
2171 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2173 send_http_error(con
, status
, NULL
);
2177 printer
= cupsdAddPrinter(resource
+ 10);
2180 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2183 * Check the default policy, then rename the implicit printer to
2184 * "AnyPrinter" or delete it...
2187 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2189 send_http_error(con
, status
, NULL
);
2193 if (ImplicitAnyClasses
)
2195 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2196 cupsdRenamePrinter(printer
, newname
);
2199 cupsdDeletePrinter(printer
, 1);
2202 * Add the printer as a new local printer...
2205 printer
= cupsdAddPrinter(resource
+ 10);
2208 else if (printer
->type
& CUPS_PRINTER_DISCOVERED
)
2211 * Check the default policy, then rename the remote printer to
2212 * "Printer@server"...
2215 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2217 send_http_error(con
, status
, NULL
);
2221 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2223 cupsdRenamePrinter(printer
, newname
);
2226 * Add the printer as a new local printer...
2229 printer
= cupsdAddPrinter(resource
+ 10);
2232 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2235 send_http_error(con
, status
, printer
);
2242 * Look for attributes and copy them over as needed...
2245 need_restart_job
= 0;
2247 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2248 IPP_TAG_TEXT
)) != NULL
)
2249 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2251 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2252 IPP_TAG_TEXT
)) != NULL
)
2253 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2257 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2258 IPP_TAG_URI
)) != NULL
)
2261 * Do we have a valid device URI?
2264 http_uri_status_t uri_status
; /* URI separation status */
2267 need_restart_job
= 1;
2269 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2270 attr
->values
[0].string
.text
,
2271 scheme
, sizeof(scheme
),
2272 username
, sizeof(username
),
2273 host
, sizeof(host
), &port
,
2274 resource
, sizeof(resource
));
2276 if (uri_status
< HTTP_URI_OK
)
2278 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2279 attr
->values
[0].string
.text
);
2280 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2281 "add_printer: httpSeparateURI returned %d", uri_status
);
2285 if (!strcmp(scheme
, "file"))
2288 * See if the administrator has enabled file devices...
2291 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2294 * File devices are disabled and the URL is not file:/dev/null...
2297 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2298 _("File device URIs have been disabled! "
2299 "To enable, see the FileDevice directive in "
2300 "\"%s/cupsd.conf\"."),
2308 * See if the backend exists and is executable...
2311 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2312 if (access(srcfile
, X_OK
))
2315 * Could not find device in list!
2318 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2319 attr
->values
[0].string
.text
);
2324 cupsdLogMessage(CUPSD_LOG_INFO
,
2325 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2327 cupsdSanitizeURI(attr
->values
[0].string
.text
, line
,
2329 cupsdSanitizeURI(printer
->device_uri
, resource
,
2332 cupsdSetString(&printer
->device_uri
, attr
->values
[0].string
.text
);
2336 set_port_monitor
= 0;
2338 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2339 IPP_TAG_NAME
)) != NULL
)
2341 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2344 need_restart_job
= 1;
2346 supported
= ippFindAttribute(printer
->attrs
, "port-monitor-supported",
2350 for (i
= 0; i
< supported
->num_values
; i
++)
2351 if (!strcmp(supported
->values
[i
].string
.text
,
2352 attr
->values
[0].string
.text
))
2356 if (!supported
|| i
>= supported
->num_values
)
2358 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2359 attr
->values
[0].string
.text
);
2363 cupsdLogMessage(CUPSD_LOG_INFO
,
2364 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2365 printer
->name
, attr
->values
[0].string
.text
,
2366 printer
->port_monitor
? printer
->port_monitor
: "none");
2368 if (strcmp(attr
->values
[0].string
.text
, "none"))
2369 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2371 cupsdClearString(&printer
->port_monitor
);
2373 set_port_monitor
= 1;
2376 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2377 IPP_TAG_BOOLEAN
)) != NULL
)
2379 cupsdLogMessage(CUPSD_LOG_INFO
,
2380 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2381 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2383 printer
->accepting
= attr
->values
[0].boolean
;
2384 cupsdAddPrinterHistory(printer
);
2387 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2388 IPP_TAG_BOOLEAN
)) != NULL
)
2390 if (printer
->shared
&& !attr
->values
[0].boolean
)
2391 cupsdDeregisterPrinter(printer
, 1);
2393 cupsdLogMessage(CUPSD_LOG_INFO
,
2394 "Setting %s printer-is-shared to %d (was %d.)",
2395 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2397 printer
->shared
= attr
->values
[0].boolean
;
2400 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2401 IPP_TAG_ENUM
)) != NULL
)
2403 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2404 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2406 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2407 attr
->values
[0].integer
);
2411 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2412 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2414 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2415 cupsdStopPrinter(printer
, 0);
2418 need_restart_job
= 1;
2419 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2422 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2423 IPP_TAG_TEXT
)) != NULL
)
2425 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2426 sizeof(printer
->state_message
));
2427 cupsdAddPrinterHistory(printer
);
2430 set_printer_defaults(con
, printer
);
2432 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2433 IPP_TAG_KEYWORD
)) != NULL
)
2434 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2437 * See if we have all required attributes...
2440 if (!printer
->device_uri
)
2441 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2444 * See if we have an interface script or PPD file attached to the request...
2449 need_restart_job
= 1;
2451 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2453 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2456 * Yes; get the first line from it...
2460 cupsFileGets(fp
, line
, sizeof(line
));
2464 * Then see what kind of file it is...
2467 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2470 if (!strncmp(line
, "*PPD-Adobe", 10))
2473 * The new file is a PPD file, so remove any old interface script
2474 * that might be lying around...
2482 * This must be an interface script, so move the file over to the
2483 * interfaces directory and make it executable...
2486 if (copy_file(srcfile
, dstfile
))
2488 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2489 _("Unable to copy interface script - %s!"),
2494 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2495 "Copied interface script successfully!");
2496 chmod(dstfile
, 0755);
2499 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2502 if (!strncmp(line
, "*PPD-Adobe", 10))
2505 * The new file is a PPD file, so move the file over to the
2506 * ppd directory and make it readable by all...
2509 if (copy_file(srcfile
, dstfile
))
2511 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2512 _("Unable to copy PPD file - %s!"),
2517 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2518 "Copied PPD file successfully!");
2519 chmod(dstfile
, 0644);
2523 * (Re)register color profiles...
2526 apple_unregister_profiles(printer
);
2527 apple_register_profiles(printer
);
2528 #endif /* __APPLE__ */
2533 * This must be an interface script, so remove any old PPD file that
2534 * may be lying around...
2541 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2542 IPP_TAG_NAME
)) != NULL
)
2544 need_restart_job
= 1;
2546 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2549 * Raw driver, remove any existing PPD or interface script files.
2552 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2556 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2566 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2570 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2573 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2575 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2579 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2580 "Copied PPD file successfully!");
2581 chmod(dstfile
, 0644);
2585 * (Re)register color profiles...
2588 apple_unregister_profiles(printer
);
2589 apple_register_profiles(printer
);
2590 #endif /* __APPLE__ */
2595 * If we set the device URI but not the port monitor, check which port
2596 * monitor to use by default...
2599 if (set_device_uri
&& !set_port_monitor
)
2601 ppd_file_t
*ppd
; /* PPD file */
2602 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2605 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2606 sizeof(scheme
), username
, sizeof(username
), host
,
2607 sizeof(host
), &port
, resource
, sizeof(resource
));
2609 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2611 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2613 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2615 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2616 if (!strcmp(scheme
, ppdattr
->spec
))
2618 cupsdLogMessage(CUPSD_LOG_INFO
,
2619 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2620 printer
->name
, ppdattr
->value
,
2621 printer
->port_monitor
? printer
->port_monitor
2624 if (strcmp(ppdattr
->value
, "none"))
2625 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2627 cupsdClearString(&printer
->port_monitor
);
2637 * Update the printer attributes and return...
2640 cupsdSetPrinterAttrs(printer
);
2641 cupsdSaveAllPrinters();
2643 if (need_restart_job
&& printer
->job
)
2648 * Stop the current job and then restart it below...
2651 job
= (cupsd_job_t
*)printer
->job
;
2653 cupsdStopJob(job
, 1);
2655 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2656 job
->state_value
= IPP_JOB_PENDING
;
2659 if (need_restart_job
)
2662 cupsdWritePrintcap();
2666 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2667 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2670 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2671 printer
->name
, get_username(con
));
2675 cupsdAddPrinterHistory(printer
);
2677 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2678 "New printer \"%s\" added by \"%s\".", printer
->name
,
2681 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2682 printer
->name
, get_username(con
));
2685 con
->response
->request
.status
.status_code
= IPP_OK
;
2690 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2691 * based upon the printer state...
2695 add_printer_state_reasons(
2696 cupsd_client_t
*con
, /* I - Client connection */
2697 cupsd_printer_t
*p
) /* I - Printer info */
2699 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2700 "add_printer_state_reasons(%p[%d], %p[%s])",
2701 con
, con
->http
.fd
, p
, p
->name
);
2703 if (p
->num_reasons
== 0)
2704 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2705 "printer-state-reasons", NULL
,
2706 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2708 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2709 "printer-state-reasons", p
->num_reasons
, NULL
,
2710 (const char * const *)p
->reasons
);
2715 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2716 * the specified printer or class.
2720 add_queued_job_count(
2721 cupsd_client_t
*con
, /* I - Client connection */
2722 cupsd_printer_t
*p
) /* I - Printer or class */
2724 int count
; /* Number of jobs on destination */
2727 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2728 con
, con
->http
.fd
, p
, p
->name
);
2730 count
= cupsdGetPrinterJobCount(p
->name
);
2732 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2733 "queued-job-count", count
);
2739 * 'apple_hash_name()' - Calculate a 32-bit hash of a printer/profile name.
2742 static unsigned /* O - Hash value */
2743 apple_hash_name(const char *name
) /* I - Printer/profile name */
2745 int mult
; /* Multiplier */
2746 unsigned hash
= 0; /* Hash value */
2749 for (mult
= 1; *name
&& mult
<= 128; mult
++, name
++)
2750 hash
+= (*name
& 255) & mult
;
2757 * 'apple_init_profile()' - Initialize a color profile.
2762 CMDeviceProfileInfo
*profile
, /* I - Profile record */
2763 unsigned id
, /* I - Profile ID */
2764 const char *name
, /* I - Profile name */
2765 const char *iccfile
) /* I - ICC filename */
2767 char url
[1024]; /* URL for profile filename */
2768 CFMutableDictionaryRef dict
; /* Dictionary for name */
2772 httpAssembleURI(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "file", NULL
, "", 0,
2775 dict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
2776 &kCFTypeDictionaryKeyCallBacks
,
2777 &kCFTypeDictionaryValueCallBacks
);
2779 profile
->dataVersion
= cmDeviceProfileInfoVersion1
;
2780 profile
->profileID
= id
;
2781 profile
->profileLoc
.locType
= iccfile
? cmPathBasedProfile
: cmNoProfileBase
;
2782 profile
->profileName
= dict
;
2785 strlcpy(profile
->profileLoc
.u
.pathLoc
.path
, iccfile
,
2786 sizeof(profile
->profileLoc
.u
.pathLoc
.path
));
2788 CFDictionarySetValue(dict
, CFSTR("PMColorDeviceProfileID"),
2789 CFNumberCreate(kCFAllocatorDefault
,
2790 kCFNumberSInt32Type
,
2791 &profile
->profileID
));
2792 CFDictionarySetValue(dict
, CFSTR("PMColorDeviceProfileName"),
2793 CFStringCreateWithCString(kCFAllocatorDefault
,
2795 kCFStringEncodingUTF8
));
2797 CFDictionarySetValue(dict
, CFSTR("PMColorDeviceProfileURL"),
2798 CFStringCreateWithCString(kCFAllocatorDefault
,
2800 kCFStringEncodingUTF8
));
2805 * 'apple_register_profiles()' - Register color profiles for a printer.
2809 apple_register_profiles(
2810 cupsd_printer_t
*p
) /* I - Printer */
2812 int i
; /* Looping var */
2813 char ppdfile
[1024], /* PPD filename */
2814 iccfile
[1024]; /* ICC filename */
2815 ppd_file_t
*ppd
; /* PPD file */
2816 ppd_attr_t
*attr
; /* cupsICCProfile attributes */
2817 ppd_option_t
*cm_option
; /* Color model option */
2818 ppd_choice_t
*cm_choice
; /* Color model choice */
2819 int num_profiles
; /* Number of profiles */
2820 CMError error
; /* Last error */
2821 unsigned id
; /* Printer device ID */
2822 CFMutableDictionaryRef name
; /* Printer device name dictionary */
2823 CMDeviceScope scope
= /* Scope of the registration */
2825 kCFPreferencesAnyUser
,
2826 kCFPreferencesCurrentHost
2828 CMDeviceProfileArrayPtr profiles
; /* Profiles */
2829 CMDeviceProfileInfo
*profile
; /* Current profile */
2833 * Try opening the PPD file for this printer...
2836 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
2837 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
2841 * See if we have any profiles...
2844 for (num_profiles
= 0, attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
2846 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
2847 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
2849 if (attr
->value
[0] != '/')
2850 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
2853 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
2855 if (access(iccfile
, 0))
2862 * If we have profiles, add them...
2865 if (num_profiles
> 0)
2868 * Build the array of profiles...
2870 * Note: This calloc actually requests slightly more memory than needed.
2873 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
2875 cupsdLogMessage(CUPSD_LOG_ERROR
,
2876 "Unable to allocate memory for %d profiles!",
2882 profiles
->profileCount
= num_profiles
;
2884 for (profile
= profiles
->profiles
,
2885 attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
2887 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
2888 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
2891 * Add this profile...
2894 if (attr
->value
[0] != '/')
2895 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
2898 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
2900 if (access(iccfile
, 0))
2903 apple_init_profile(profile
, apple_hash_name(attr
->spec
), attr
->spec
,
2909 else if ((cm_option
= ppdFindOption(ppd
, "ColorModel")) != NULL
)
2912 * Extract standard grayscale, RGB, or CMYK profiles...
2915 for (num_profiles
= 0, i
= cm_option
->num_choices
,
2916 cm_choice
= cm_option
->choices
;
2919 if (!strcmp(cm_choice
->choice
, "Gray") ||
2920 !strcmp(cm_choice
->choice
, "RGB") ||
2921 !strcmp(cm_choice
->choice
, "CMYK"))
2925 * Build the array of profiles...
2927 * Note: This calloc actually requests slightly more memory than needed.
2930 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
2932 cupsdLogMessage(CUPSD_LOG_ERROR
,
2933 "Unable to allocate memory for %d profiles!",
2939 profiles
->profileCount
= num_profiles
;
2941 for (profile
= profiles
->profiles
, i
= cm_option
->num_choices
,
2942 cm_choice
= cm_option
->choices
;
2945 if (!strcmp(cm_choice
->choice
, "Gray"))
2947 apple_init_profile(profile
, apple_hash_name("Gray.."), "Gray", NULL
);
2950 else if (!strcmp(cm_choice
->choice
, "RGB"))
2952 apple_init_profile(profile
, apple_hash_name("RGB.."), "RGB", NULL
);
2955 else if (!strcmp(cm_choice
->choice
, "CMYK"))
2957 apple_init_profile(profile
, apple_hash_name("CMYK.."), "CMYK", NULL
);
2964 * Use the default colorspace...
2969 if ((profiles
= calloc(1, sizeof(CMDeviceProfileArray
))) == NULL
)
2971 cupsdLogMessage(CUPSD_LOG_ERROR
,
2972 "Unable to allocate memory for %d profiles!",
2978 profiles
->profileCount
= 1;
2980 switch (ppd
->colorspace
)
2983 apple_init_profile(profiles
->profiles
, apple_hash_name("Gray.."),
2987 apple_init_profile(profiles
->profiles
, apple_hash_name("RGB.."),
2991 apple_init_profile(profiles
->profiles
, apple_hash_name("CMYK.."),
3002 if (num_profiles
> 0)
3005 * Get the device ID hash and pathelogical name dictionary.
3008 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\"",
3011 id
= apple_hash_name(p
->name
);
3012 name
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3013 &kCFTypeDictionaryKeyCallBacks
,
3014 &kCFTypeDictionaryValueCallBacks
);
3016 CFDictionarySetValue(name
, CFSTR("en"),
3017 CFStringCreateWithCString(kCFAllocatorDefault
, p
->name
,
3018 kCFStringEncodingUTF8
));
3021 * Register the device with ColorSync...
3024 error
= CMRegisterColorDevice(cmPrinterDeviceClass
, id
, name
, &scope
);
3027 * Register the profiles...
3031 error
= CMSetDeviceFactoryProfiles(cmPrinterDeviceClass
, id
,
3032 profiles
->profiles
[0].profileID
,
3040 cupsdLogMessage(CUPSD_LOG_ERROR
,
3041 "Unable to register ICC color profiles for \"%s\" - %d",
3042 p
->name
, (int)error
);
3044 for (profile
= profiles
->profiles
;
3046 profile
++, num_profiles
--)
3047 CFRelease(profile
->profileName
);
3058 * 'apple_unregister_profiles()' - Remove color profiles for the specified
3063 apple_unregister_profiles(
3064 cupsd_printer_t
*p
) /* I - Printer */
3066 CMUnregisterColorDevice(cmPrinterDeviceClass
, apple_hash_name(p
->name
));
3068 #endif /* __APPLE__ */
3071 * 'apply_printer_defaults()' - Apply printer default options to a job.
3075 apply_printer_defaults(
3076 cupsd_printer_t
*printer
, /* I - Printer */
3077 cupsd_job_t
*job
) /* I - Job */
3079 int i
, /* Looping var */
3080 num_options
; /* Number of default options */
3081 cups_option_t
*options
, /* Default options */
3082 *option
; /* Current option */
3086 * Collect all of the default options and add the missing ones to the
3090 for (i
= printer
->num_options
, num_options
= 0, option
= printer
->options
;
3093 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
3095 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
3100 * Encode these options as attributes in the job object...
3103 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
3104 cupsFreeOptions(num_options
, options
);
3109 * 'authenticate_job()' - Set job authentication info.
3113 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
3114 ipp_attribute_t
*uri
) /* I - Job URI */
3116 ipp_attribute_t
*attr
, /* job-id attribute */
3117 *auth_info
; /* auth-info attribute */
3118 int jobid
; /* Job ID */
3119 cupsd_job_t
*job
; /* Current job */
3120 char method
[HTTP_MAX_URI
],
3121 /* Method portion of URI */
3122 username
[HTTP_MAX_URI
],
3123 /* Username portion of URI */
3125 /* Host portion of URI */
3126 resource
[HTTP_MAX_URI
];
3127 /* Resource portion of URI */
3128 int port
; /* Port portion of URI */
3131 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
3132 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
3135 * Start with "everything is OK" status...
3138 con
->response
->request
.status
.status_code
= IPP_OK
;
3141 * See if we have a job URI or a printer URI...
3144 if (!strcmp(uri
->name
, "printer-uri"))
3147 * Got a printer URI; see if we also have a job-id attribute...
3150 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3151 IPP_TAG_INTEGER
)) == NULL
)
3153 send_ipp_status(con
, IPP_BAD_REQUEST
,
3154 _("Got a printer-uri attribute but no job-id!"));
3158 jobid
= attr
->values
[0].integer
;
3163 * Got a job URI; parse it to get the job ID...
3166 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
3167 sizeof(method
), username
, sizeof(username
), host
,
3168 sizeof(host
), &port
, resource
, sizeof(resource
));
3170 if (strncmp(resource
, "/jobs/", 6))
3176 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
3177 uri
->values
[0].string
.text
);
3181 jobid
= atoi(resource
+ 6);
3185 * See if the job exists...
3188 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3191 * Nope - return a "not found" error...
3194 send_ipp_status(con
, IPP_NOT_FOUND
,
3195 _("Job #%d does not exist!"), jobid
);
3200 * See if the job has been completed...
3203 if (job
->state_value
!= IPP_JOB_HELD
)
3206 * Return a "not-possible" error...
3209 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3210 _("Job #%d is not held for authentication!"),
3216 * See if we have already authenticated...
3219 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3221 if (!con
->username
[0] && !auth_info
)
3223 cupsd_printer_t
*printer
; /* Job destination */
3227 * No auth data. If we need to authenticate via Kerberos, send a
3228 * HTTP auth challenge, otherwise just return an IPP error...
3231 printer
= cupsdFindDest(job
->dest
);
3233 if (printer
&& printer
->num_auth_info_required
> 0 &&
3234 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3235 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3237 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3238 _("No authentication information provided!"));
3243 * See if the job is owned by the requesting user...
3246 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3248 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3253 * Save the authentication information for this job...
3256 save_auth_info(con
, job
, auth_info
);
3259 * Reset the job-hold-until value to "no-hold"...
3262 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3263 IPP_TAG_KEYWORD
)) == NULL
)
3264 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3268 attr
->value_tag
= IPP_TAG_KEYWORD
;
3269 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
3273 * Release the job and return...
3276 cupsdReleaseJob(job
);
3278 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Authenticated by \"%s\".", jobid
,
3284 * 'cancel_all_jobs()' - Cancel all print jobs.
3288 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3289 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3291 http_status_t status
; /* Policy status */
3292 cups_ptype_t dtype
; /* Destination type */
3293 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3294 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3295 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3296 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3297 int port
; /* Port portion of URI */
3298 ipp_attribute_t
*attr
; /* Attribute in request */
3299 const char *username
; /* Username */
3300 int purge
; /* Purge? */
3301 cupsd_printer_t
*printer
; /* Printer */
3304 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3305 con
->http
.fd
, uri
->values
[0].string
.text
);
3308 * See if we have a printer URI...
3311 if (strcmp(uri
->name
, "printer-uri"))
3313 send_ipp_status(con
, IPP_BAD_REQUEST
,
3314 _("The printer-uri attribute is required!"));
3319 * Get the username (if any) for the jobs we want to cancel (only if
3320 * "my-jobs" is specified...
3323 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3324 IPP_TAG_BOOLEAN
)) != NULL
&&
3325 attr
->values
[0].boolean
)
3327 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3328 IPP_TAG_NAME
)) != NULL
)
3329 username
= attr
->values
[0].string
.text
;
3332 send_ipp_status(con
, IPP_BAD_REQUEST
,
3333 _("Missing requesting-user-name attribute!"));
3341 * Look for the "purge-jobs" attribute...
3344 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3345 IPP_TAG_BOOLEAN
)) != NULL
)
3346 purge
= attr
->values
[0].boolean
;
3351 * And if the destination is valid...
3354 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3360 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3361 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3362 hostname
, sizeof(hostname
), &port
,
3363 resource
, sizeof(resource
));
3365 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3366 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3368 send_ipp_status(con
, IPP_NOT_FOUND
,
3369 _("The printer or class was not found."));
3377 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3379 send_http_error(con
, status
, NULL
);
3384 * Cancel all jobs on all printers...
3387 cupsdCancelJobs(NULL
, username
, purge
);
3389 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3390 purge
? "purged" : "canceled", get_username(con
));
3398 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3401 send_http_error(con
, status
, printer
);
3406 * Cancel all of the jobs on the named printer...
3409 cupsdCancelJobs(printer
->name
, username
, purge
);
3411 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3412 printer
->name
, purge
? "purged" : "canceled",
3416 con
->response
->request
.status
.status_code
= IPP_OK
;
3421 * 'cancel_job()' - Cancel a print job.
3425 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3426 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3428 ipp_attribute_t
*attr
; /* Current attribute */
3429 int jobid
; /* Job ID */
3430 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3431 username
[HTTP_MAX_URI
], /* Username portion of URI */
3432 host
[HTTP_MAX_URI
], /* Host portion of URI */
3433 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3434 int port
; /* Port portion of URI */
3435 cupsd_job_t
*job
; /* Job information */
3436 cups_ptype_t dtype
; /* Destination type (printer/class) */
3437 cupsd_printer_t
*printer
; /* Printer data */
3438 int purge
; /* Purge the job? */
3441 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3442 con
->http
.fd
, uri
->values
[0].string
.text
);
3445 * See if we have a job URI or a printer URI...
3448 if (!strcmp(uri
->name
, "printer-uri"))
3451 * Got a printer URI; see if we also have a job-id attribute...
3454 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3455 IPP_TAG_INTEGER
)) == NULL
)
3457 send_ipp_status(con
, IPP_BAD_REQUEST
,
3458 _("Got a printer-uri attribute but no job-id!"));
3462 if ((jobid
= attr
->values
[0].integer
) == 0)
3465 * Find the current job on the specified printer...
3468 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3474 send_ipp_status(con
, IPP_NOT_FOUND
,
3475 _("The printer or class was not found."));
3480 * See if the printer is currently printing a job...
3484 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3488 * No, see if there are any pending jobs...
3491 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3493 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3494 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3495 !strcasecmp(job
->dest
, printer
->name
))
3502 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3504 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3505 if (job
->state_value
== IPP_JOB_STOPPED
&&
3506 !strcasecmp(job
->dest
, printer
->name
))
3513 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3524 * Got a job URI; parse it to get the job ID...
3527 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3528 sizeof(scheme
), username
, sizeof(username
), host
,
3529 sizeof(host
), &port
, resource
, sizeof(resource
));
3531 if (strncmp(resource
, "/jobs/", 6))
3537 send_ipp_status(con
, IPP_BAD_REQUEST
,
3538 _("Bad job-uri attribute \"%s\"!"),
3539 uri
->values
[0].string
.text
);
3543 jobid
= atoi(resource
+ 6);
3547 * Look for the "purge-job" attribute...
3550 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3551 IPP_TAG_BOOLEAN
)) != NULL
)
3552 purge
= attr
->values
[0].boolean
;
3557 * See if the job exists...
3560 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3563 * Nope - return a "not found" error...
3566 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
3571 * See if the job is owned by the requesting user...
3574 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3576 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3581 * See if the job is already completed, canceled, or aborted; if so,
3582 * we can't cancel...
3585 if (job
->state_value
>= IPP_JOB_CANCELED
&& !purge
)
3587 switch (job
->state_value
)
3589 case IPP_JOB_CANCELED
:
3590 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3591 _("Job #%d is already canceled - can\'t cancel."),
3595 case IPP_JOB_ABORTED
:
3596 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3597 _("Job #%d is already aborted - can\'t cancel."),
3602 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3603 _("Job #%d is already completed - can\'t cancel."),
3612 * Cancel the job and return...
3615 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
3619 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
3622 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3625 con
->response
->request
.status
.status_code
= IPP_OK
;
3630 * 'cancel_subscription()' - Cancel a subscription.
3634 cancel_subscription(
3635 cupsd_client_t
*con
, /* I - Client connection */
3636 int sub_id
) /* I - Subscription ID */
3638 http_status_t status
; /* Policy status */
3639 cupsd_subscription_t
*sub
; /* Subscription */
3642 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3643 "cancel_subscription(con=%p[%d], sub_id=%d)",
3644 con
, con
->http
.fd
, sub_id
);
3647 * Is the subscription ID valid?
3650 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3653 * Bad subscription ID...
3656 send_ipp_status(con
, IPP_NOT_FOUND
,
3657 _("notify-subscription-id %d no good!"), sub_id
);
3665 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3667 con
, sub
->owner
)) != HTTP_OK
)
3669 send_http_error(con
, status
, sub
->dest
);
3674 * Cancel the subscription...
3677 cupsdDeleteSubscription(sub
, 1);
3679 con
->response
->request
.status
.status_code
= IPP_OK
;
3684 * 'check_quotas()' - Check quotas for a printer and user.
3687 static int /* O - 1 if OK, 0 if not */
3688 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3689 cupsd_printer_t
*p
) /* I - Printer or class */
3691 int i
; /* Looping var */
3692 char username
[33]; /* Username */
3693 cupsd_quota_t
*q
; /* Quota data */
3694 #ifdef HAVE_MBR_UID_TO_UUID
3696 * Use Apple membership APIs which require that all names represent
3697 * valid user account or group records accessible by the server.
3700 uuid_t usr_uuid
; /* UUID for job requesting user */
3701 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3702 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3703 int mbr_err
; /* Error from membership function */
3704 int is_member
; /* Is this user a member? */
3707 * Use standard POSIX APIs for checking users and groups...
3710 struct passwd
*pw
; /* User password data */
3711 #endif /* HAVE_MBR_UID_TO_UUID */
3714 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3715 con
, con
->http
.fd
, p
, p
->name
);
3718 * Figure out who is printing...
3721 strlcpy(username
, get_username(con
), sizeof(username
));
3724 * Check global active job limits for printers and users...
3727 if (MaxJobsPerPrinter
)
3730 * Check if there are too many pending jobs on this printer...
3733 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3735 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3744 * Check if there are too many pending jobs for this user...
3747 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3749 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3756 * Check against users...
3759 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3764 #ifdef HAVE_MBR_UID_TO_UUID
3766 * Get UUID for job requesting user...
3769 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3775 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3776 "check_quotas: UUID lookup failed for user \"%s\"",
3778 cupsdLogMessage(CUPSD_LOG_INFO
,
3779 "Denying user \"%s\" access to printer \"%s\" "
3780 "(unknown user)...",
3786 * Get UID and GID of requesting user...
3789 pw
= getpwnam(username
);
3791 #endif /* HAVE_MBR_UID_TO_UUID */
3793 for (i
= 0; i
< p
->num_users
; i
++)
3794 if (p
->users
[i
][0] == '@')
3797 * Check group membership...
3800 #ifdef HAVE_MBR_UID_TO_UUID
3801 if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
3805 * Invalid ACL entries are ignored for matching; just record a
3806 * warning in the log...
3809 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3810 "check_quotas: UUID lookup failed for ACL entry "
3811 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3812 cupsdLogMessage(CUPSD_LOG_WARN
,
3813 "Access control entry \"%s\" not a valid group name; "
3814 "entry ignored", p
->users
[i
]);
3818 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3822 * At this point, there should be no errors, but check anyways...
3825 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3826 "check_quotas: group \"%s\" membership check "
3827 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
3832 * Stop if we found a match...
3839 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
3841 #endif /* HAVE_MBR_UID_TO_UUID */
3843 #ifdef HAVE_MBR_UID_TO_UUID
3846 if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
3850 * Invalid ACL entries are ignored for matching; just record a
3851 * warning in the log...
3854 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3855 "check_quotas: UUID lookup failed for ACL entry "
3856 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
3857 cupsdLogMessage(CUPSD_LOG_WARN
,
3858 "Access control entry \"%s\" not a valid user name; "
3859 "entry ignored", p
->users
[i
]);
3863 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
3866 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3867 "check_quotas: User \"%s\" identity check failed "
3868 "(err=%d)", p
->users
[i
], mbr_err
);
3877 else if (!strcasecmp(username
, p
->users
[i
]))
3879 #endif /* HAVE_MBR_UID_TO_UUID */
3881 if ((i
< p
->num_users
) == p
->deny_users
)
3883 cupsdLogMessage(CUPSD_LOG_INFO
,
3884 "Denying user \"%s\" access to printer \"%s\"...",
3895 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
3898 * TODO: Define these special page count values as constants!
3901 if (q
->page_count
== -4) /* special case: unlimited user */
3903 cupsdLogMessage(CUPSD_LOG_INFO
,
3904 "User \"%s\" request approved for printer %s (%s): "
3906 username
, p
->name
, p
->info
);
3907 q
->page_count
= 0; /* allow user to print */
3910 else if (q
->page_count
== -3) /* quota exceeded */
3912 cupsdLogMessage(CUPSD_LOG_INFO
,
3913 "User \"%s\" request denied for printer %s (%s): "
3914 "quota limit exceeded.",
3915 username
, p
->name
, p
->info
);
3916 q
->page_count
= 2; /* force quota exceeded failure */
3919 else if (q
->page_count
== -2) /* quota disabled for user */
3921 cupsdLogMessage(CUPSD_LOG_INFO
,
3922 "User \"%s\" request denied for printer %s (%s): "
3923 "printing disabled for user.",
3924 username
, p
->name
, p
->info
);
3925 q
->page_count
= 2; /* force quota exceeded failure */
3928 else if (q
->page_count
== -1) /* quota access error */
3930 cupsdLogMessage(CUPSD_LOG_INFO
,
3931 "User \"%s\" request denied for printer %s (%s): "
3932 "unable to determine quota limit.",
3933 username
, p
->name
, p
->info
);
3934 q
->page_count
= 2; /* force quota exceeded failure */
3937 else if (q
->page_count
< 0) /* user not found or other error */
3939 cupsdLogMessage(CUPSD_LOG_INFO
,
3940 "User \"%s\" request denied for printer %s (%s): "
3941 "user disabled / missing quota.",
3942 username
, p
->name
, p
->info
);
3943 q
->page_count
= 2; /* force quota exceeded failure */
3946 else /* page within user limits */
3948 q
->page_count
= 0; /* allow user to print */
3953 #endif /* __APPLE__ */
3954 if (p
->k_limit
|| p
->page_limit
)
3956 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3958 cupsdLogMessage(CUPSD_LOG_ERROR
,
3959 "Unable to allocate quota data for user \"%s\"!",
3964 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3965 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3967 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3974 * If we have gotten this far, we're done!
3982 * 'copy_attribute()' - Copy a single attribute.
3985 static ipp_attribute_t
* /* O - New attribute */
3987 ipp_t
*to
, /* O - Destination request/response */
3988 ipp_attribute_t
*attr
, /* I - Attribute to copy */
3989 int quickcopy
) /* I - Do a quick copy? */
3991 int i
; /* Looping var */
3992 ipp_attribute_t
*toattr
; /* Destination attribute */
3995 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3996 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
3997 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
4000 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
4003 toattr
= ippAddSeparator(to
);
4006 case IPP_TAG_INTEGER
:
4008 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4009 attr
->name
, attr
->num_values
, NULL
);
4011 for (i
= 0; i
< attr
->num_values
; i
++)
4012 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
4015 case IPP_TAG_BOOLEAN
:
4016 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
4017 attr
->num_values
, NULL
);
4019 for (i
= 0; i
< attr
->num_values
; i
++)
4020 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
4023 case IPP_TAG_STRING
:
4026 case IPP_TAG_KEYWORD
:
4028 case IPP_TAG_URISCHEME
:
4029 case IPP_TAG_CHARSET
:
4030 case IPP_TAG_LANGUAGE
:
4031 case IPP_TAG_MIMETYPE
:
4032 toattr
= ippAddStrings(to
, attr
->group_tag
,
4033 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4034 attr
->name
, attr
->num_values
, NULL
, NULL
);
4038 for (i
= 0; i
< attr
->num_values
; i
++)
4039 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4043 for (i
= 0; i
< attr
->num_values
; i
++)
4044 toattr
->values
[i
].string
.text
=
4045 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4050 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
4051 attr
->values
[0].date
);
4054 case IPP_TAG_RESOLUTION
:
4055 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
4056 attr
->num_values
, IPP_RES_PER_INCH
,
4059 for (i
= 0; i
< attr
->num_values
; i
++)
4061 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
4062 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
4063 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
4067 case IPP_TAG_RANGE
:
4068 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
4069 attr
->num_values
, NULL
, NULL
);
4071 for (i
= 0; i
< attr
->num_values
; i
++)
4073 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
4074 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
4078 case IPP_TAG_TEXTLANG
:
4079 case IPP_TAG_NAMELANG
:
4080 toattr
= ippAddStrings(to
, attr
->group_tag
,
4081 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4082 attr
->name
, attr
->num_values
, NULL
, NULL
);
4086 for (i
= 0; i
< attr
->num_values
; i
++)
4088 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
4089 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4094 for (i
= 0; i
< attr
->num_values
; i
++)
4097 toattr
->values
[i
].string
.charset
=
4098 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
4100 toattr
->values
[i
].string
.charset
=
4101 toattr
->values
[0].string
.charset
;
4103 toattr
->values
[i
].string
.text
=
4104 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4109 case IPP_TAG_BEGIN_COLLECTION
:
4110 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
4111 attr
->num_values
, NULL
);
4113 for (i
= 0; i
< attr
->num_values
; i
++)
4115 toattr
->values
[i
].collection
= ippNew();
4116 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
4117 NULL
, IPP_TAG_ZERO
, 0);
4122 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4123 attr
->name
, attr
->num_values
, NULL
);
4125 for (i
= 0; i
< attr
->num_values
; i
++)
4127 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
4129 if (toattr
->values
[i
].unknown
.length
> 0)
4131 if ((toattr
->values
[i
].unknown
.data
=
4132 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
4133 toattr
->values
[i
].unknown
.length
= 0;
4135 memcpy(toattr
->values
[i
].unknown
.data
,
4136 attr
->values
[i
].unknown
.data
,
4137 toattr
->values
[i
].unknown
.length
);
4140 break; /* anti-compiler-warning-code */
4148 * 'copy_attrs()' - Copy attributes from one request to another.
4152 copy_attrs(ipp_t
*to
, /* I - Destination request */
4153 ipp_t
*from
, /* I - Source request */
4154 cups_array_t
*ra
, /* I - Requested attributes */
4155 ipp_tag_t group
, /* I - Group to copy */
4156 int quickcopy
) /* I - Do a quick copy? */
4158 ipp_attribute_t
*fromattr
; /* Source attribute */
4161 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4162 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4163 to
, from
, ra
, group
, quickcopy
);
4168 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4171 * Filter attributes as needed...
4174 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4175 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4178 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4179 copy_attribute(to
, fromattr
, quickcopy
);
4185 * 'copy_banner()' - Copy a banner file to the requests directory for the
4189 static int /* O - Size of banner file in kbytes */
4190 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4191 cupsd_job_t
*job
, /* I - Job information */
4192 const char *name
) /* I - Name of banner */
4194 int i
; /* Looping var */
4195 int kbytes
; /* Size of banner file in kbytes */
4196 char filename
[1024]; /* Job filename */
4197 cupsd_banner_t
*banner
; /* Pointer to banner */
4198 cups_file_t
*in
; /* Input file */
4199 cups_file_t
*out
; /* Output file */
4200 int ch
; /* Character from file */
4201 char attrname
[255], /* Name of attribute */
4202 *s
; /* Pointer into name */
4203 ipp_attribute_t
*attr
; /* Attribute */
4206 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_banner(%p[%d], %p[%d], %s)",
4207 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
4208 name
? name
: "(null)");
4211 * Find the banner; return if not found or "none"...
4214 if (!name
|| !strcmp(name
, "none") ||
4215 (banner
= cupsdFindBanner(name
)) == NULL
)
4219 * Open the banner and job files...
4222 if (add_file(con
, job
, banner
->filetype
, 0))
4225 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4227 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4229 cupsdLogMessage(CUPSD_LOG_ERROR
,
4230 "copy_banner: Unable to create banner job file %s - %s",
4231 filename
, strerror(errno
));
4236 fchmod(cupsFileNumber(out
), 0640);
4237 fchown(cupsFileNumber(out
), RunUser
, Group
);
4240 * Try the localized banner file under the subdirectory...
4243 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4245 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4248 * Convert ll-cc to ll_CC...
4252 attrname
[3] = toupper(attrname
[3] & 255);
4253 attrname
[4] = toupper(attrname
[4] & 255);
4256 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4259 if (access(filename
, 0) && strlen(attrname
) > 2)
4262 * Wasn't able to find "ll_CC" locale file; try the non-national
4263 * localization banner directory.
4268 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4272 if (access(filename
, 0))
4275 * Use the non-localized banner file.
4278 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4281 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4285 cupsdLogMessage(CUPSD_LOG_ERROR
,
4286 "copy_banner: Unable to open banner template file %s - %s",
4287 filename
, strerror(errno
));
4293 * Parse the file to the end...
4296 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4300 * Get an attribute name...
4303 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4304 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4306 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4316 * Ignore { followed by stuff that is not an attribute name...
4319 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4324 * See if it is defined...
4327 if (attrname
[0] == '?')
4332 if (!strcmp(s
, "printer-name"))
4334 cupsFilePuts(out
, job
->dest
);
4337 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4340 * See if we have a leading question mark...
4343 if (attrname
[0] != '?')
4346 * Nope, write to file as-is; probably a PostScript procedure...
4349 cupsFilePrintf(out
, "{%s}", attrname
);
4356 * Output value(s)...
4359 for (i
= 0; i
< attr
->num_values
; i
++)
4362 cupsFilePutChar(out
, ',');
4364 switch (attr
->value_tag
)
4366 case IPP_TAG_INTEGER
:
4368 if (!strncmp(s
, "time-at-", 8))
4369 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
4371 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4374 case IPP_TAG_BOOLEAN
:
4375 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4378 case IPP_TAG_NOVALUE
:
4379 cupsFilePuts(out
, "novalue");
4382 case IPP_TAG_RANGE
:
4383 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4384 attr
->values
[i
].range
.upper
);
4387 case IPP_TAG_RESOLUTION
:
4388 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4389 attr
->values
[i
].resolution
.yres
,
4390 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4395 case IPP_TAG_STRING
:
4398 case IPP_TAG_KEYWORD
:
4399 case IPP_TAG_CHARSET
:
4400 case IPP_TAG_LANGUAGE
:
4401 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4404 * Need to quote strings for PS banners...
4409 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4411 if (*p
== '(' || *p
== ')' || *p
== '\\')
4413 cupsFilePutChar(out
, '\\');
4414 cupsFilePutChar(out
, *p
);
4416 else if (*p
< 32 || *p
> 126)
4417 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4419 cupsFilePutChar(out
, *p
);
4423 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4427 break; /* anti-compiler-warning-code */
4431 else if (ch
== '\\') /* Quoted char */
4433 ch
= cupsFileGetChar(in
);
4435 if (ch
!= '{') /* Only do special handling for \{ */
4436 cupsFilePutChar(out
, '\\');
4438 cupsFilePutChar(out
, ch
);
4441 cupsFilePutChar(out
, ch
);
4445 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4447 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4448 IPP_TAG_INTEGER
)) != NULL
)
4449 attr
->values
[0].integer
+= kbytes
;
4458 * 'copy_file()' - Copy a PPD file or interface script...
4461 static int /* O - 0 = success, -1 = error */
4462 copy_file(const char *from
, /* I - Source file */
4463 const char *to
) /* I - Destination file */
4465 cups_file_t
*src
, /* Source file */
4466 *dst
; /* Destination file */
4467 int bytes
; /* Bytes to read/write */
4468 char buffer
[2048]; /* Copy buffer */
4471 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4474 * Open the source and destination file for a copy...
4477 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4480 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4487 * Copy the source file to the destination...
4490 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4491 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
4499 * Close both files and return...
4504 return (cupsFileClose(dst
));
4509 * 'copy_model()' - Copy a PPD model file, substituting default values
4513 static int /* O - 0 = success, -1 = error */
4514 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4515 const char *from
, /* I - Source file */
4516 const char *to
) /* I - Destination file */
4518 fd_set input
; /* select() input set */
4519 struct timeval timeout
; /* select() timeout */
4520 int maxfd
; /* Max file descriptor for select() */
4521 char tempfile
[1024]; /* Temporary PPD file */
4522 int tempfd
; /* Temporary PPD file descriptor */
4523 int temppid
; /* Process ID of cups-driverd */
4524 int temppipe
[2]; /* Temporary pipes */
4525 char *argv
[4], /* Command-line arguments */
4526 *envp
[MAX_ENV
]; /* Environment */
4527 cups_file_t
*src
, /* Source file */
4528 *dst
; /* Destination file */
4529 ppd_file_t
*ppd
; /* PPD file */
4530 int bytes
, /* Bytes from pipe */
4531 total
; /* Total bytes from pipe */
4532 char buffer
[2048]; /* Copy buffer */
4533 int i
; /* Looping var */
4534 char option
[PPD_MAX_NAME
], /* Option name */
4535 choice
[PPD_MAX_NAME
]; /* Choice name */
4536 int num_defaults
; /* Number of default options */
4537 cups_option_t
*defaults
; /* Default options */
4538 char cups_protocol
[PPD_MAX_LINE
];
4539 /* cupsProtocol attribute */
4540 int have_letter
, /* Have Letter size */
4541 have_a4
; /* Have A4 size */
4542 #ifdef HAVE_LIBPAPER
4543 char *paper_result
; /* Paper size name from libpaper */
4544 char system_paper
[64]; /* Paper size name buffer */
4545 #endif /* HAVE_LIBPAPER */
4548 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4549 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4553 * Run cups-driverd to get the PPD file...
4556 argv
[0] = "cups-driverd";
4558 argv
[2] = (char *)from
;
4561 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4563 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4564 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
4565 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4569 cupsdOpenPipe(temppipe
);
4571 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4572 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4574 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4575 -1, -1, 0, DefaultProfile
, &temppid
))
4586 * Wait up to 30 seconds for the PPD file to be copied...
4591 if (temppipe
[0] > CGIPipes
[0])
4592 maxfd
= temppipe
[0] + 1;
4594 maxfd
= CGIPipes
[0] + 1;
4599 * See if we have data ready...
4605 FD_SET(temppipe
[0], &input
);
4606 FD_SET(CGIPipes
[0], &input
);
4608 timeout
.tv_sec
= 30;
4609 timeout
.tv_usec
= 0;
4611 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4621 * We have timed out...
4627 if (FD_ISSET(temppipe
[0], &input
))
4630 * Read the PPD file from the pipe, and write it to the PPD file.
4633 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4635 if (write(tempfd
, buffer
, bytes
) < bytes
)
4644 if (FD_ISSET(CGIPipes
[0], &input
))
4654 * No data from cups-deviced...
4657 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
4663 * Read the source file and see what page sizes are supported...
4666 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
4672 have_letter
= ppdPageSize(ppd
, "Letter") != NULL
;
4673 have_a4
= ppdPageSize(ppd
, "A4") != NULL
;
4676 * Open the destination (if possible) and set the default options...
4681 cups_protocol
[0] = '\0';
4683 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4686 * Read all of the default lines from the old PPD...
4689 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4690 if (!strncmp(buffer
, "*Default", 8))
4693 * Add the default option...
4696 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4697 choice
, sizeof(choice
)))
4699 ppd_option_t
*ppdo
; /* PPD option */
4703 * Only add the default if the default hasn't already been
4704 * set and the choice exists in the new PPD...
4707 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4708 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4709 ppdFindChoice(ppdo
, choice
))
4710 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4714 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4715 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4719 #ifdef HAVE_LIBPAPER
4720 else if ((paper_result
= systempapername()) != NULL
)
4723 * Set the default media sizes from the systemwide default...
4726 strlcpy(system_paper
, paper_result
, sizeof(system_paper
));
4727 system_paper
[0] = toupper(system_paper
[0] & 255);
4729 if ((!strcmp(system_paper
, "Letter") && have_letter
) ||
4730 (!strcmp(system_paper
, "A4") && have_a4
))
4732 num_defaults
= cupsAddOption("PageSize", system_paper
,
4733 num_defaults
, &defaults
);
4734 num_defaults
= cupsAddOption("PageRegion", system_paper
,
4735 num_defaults
, &defaults
);
4736 num_defaults
= cupsAddOption("PaperDimension", system_paper
,
4737 num_defaults
, &defaults
);
4738 num_defaults
= cupsAddOption("ImageableArea", system_paper
,
4739 num_defaults
, &defaults
);
4742 #endif /* HAVE_LIBPAPER */
4746 * Add the default media sizes...
4748 * Note: These values are generally not valid for large-format devices
4749 * like plotters, however it is probably safe to say that those
4750 * users will configure the media size after initially adding
4751 * the device anyways...
4754 if (!DefaultLanguage
||
4755 !strcasecmp(DefaultLanguage
, "C") ||
4756 !strcasecmp(DefaultLanguage
, "POSIX") ||
4757 !strcasecmp(DefaultLanguage
, "en") ||
4758 !strncasecmp(DefaultLanguage
, "en.", 3) ||
4759 !strncasecmp(DefaultLanguage
, "en_US", 5) ||
4760 !strncasecmp(DefaultLanguage
, "en_CA", 5) ||
4761 !strncasecmp(DefaultLanguage
, "fr_CA", 5))
4764 * These are the only locales that will default to "letter" size...
4769 num_defaults
= cupsAddOption("PageSize", "Letter", num_defaults
,
4771 num_defaults
= cupsAddOption("PageRegion", "Letter", num_defaults
,
4773 num_defaults
= cupsAddOption("PaperDimension", "Letter", num_defaults
,
4775 num_defaults
= cupsAddOption("ImageableArea", "Letter", num_defaults
,
4782 * The rest default to "a4" size...
4785 num_defaults
= cupsAddOption("PageSize", "A4", num_defaults
,
4787 num_defaults
= cupsAddOption("PageRegion", "A4", num_defaults
,
4789 num_defaults
= cupsAddOption("PaperDimension", "A4", num_defaults
,
4791 num_defaults
= cupsAddOption("ImageableArea", "A4", num_defaults
,
4799 * Open the source file for a copy...
4802 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4804 cupsFreeOptions(num_defaults
, defaults
);
4810 * Open the destination file for a copy...
4813 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4815 cupsFreeOptions(num_defaults
, defaults
);
4822 * Copy the source file to the destination...
4825 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4827 if (!strncmp(buffer
, "*Default", 8))
4830 * Check for an previous default option choice...
4833 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4834 choice
, sizeof(choice
)))
4836 const char *val
; /* Default option value */
4839 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4842 * Substitute the previous choice...
4845 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4850 cupsFilePrintf(dst
, "%s\n", buffer
);
4853 if (cups_protocol
[0])
4854 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4856 cupsFreeOptions(num_defaults
, defaults
);
4859 * Close both files and return...
4866 return (cupsFileClose(dst
));
4871 * 'copy_job_attrs()' - Copy job attributes.
4875 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4876 cupsd_job_t
*job
, /* I - Job */
4877 cups_array_t
*ra
) /* I - Requested attributes array */
4879 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4883 * Send the requested attributes for each job...
4886 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4887 con
->servername
, con
->serverport
, "/jobs/%d",
4890 if (!ra
|| cupsArrayFind(ra
, "document-count"))
4891 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4892 "document-count", job
->num_files
);
4894 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
4895 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4896 "job-more-info", NULL
, job_uri
);
4898 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4899 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4900 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4901 job
->num_files
> 0);
4903 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
4904 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4905 "job-printer-up-time", time(NULL
));
4907 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4908 add_job_state_reasons(con
, job
);
4910 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4911 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4912 "job-uri", NULL
, job_uri
);
4914 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
4919 * 'copy_printer_attrs()' - Copy printer attributes.
4924 cupsd_client_t
*con
, /* I - Client connection */
4925 cupsd_printer_t
*printer
, /* I - Printer */
4926 cups_array_t
*ra
) /* I - Requested attributes array */
4928 char printer_uri
[HTTP_MAX_URI
];
4930 time_t curtime
; /* Current time */
4931 int i
; /* Looping var */
4932 ipp_attribute_t
*history
; /* History collection */
4936 * Copy the printer attributes to the response using requested-attributes
4937 * and document-format attributes that may be provided by the client.
4940 curtime
= time(NULL
);
4943 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
4944 printer
->recoverable
)
4945 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4946 "com.apple.print.recoverable-message", NULL
,
4947 printer
->recoverable
);
4948 #endif /* __APPLE__ */
4950 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
4951 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4952 "marker-change-time", printer
->marker_time
);
4954 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4955 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
4956 "printer-alert", NULL
, printer
->alert
);
4958 if (printer
->alert_description
&&
4959 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4960 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4961 "printer-alert-description", NULL
,
4962 printer
->alert_description
);
4964 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4965 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4966 ippTimeToDate(curtime
));
4969 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
4971 if (printer
->reg_name
)
4972 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4973 "printer-dns-sd-name", NULL
, printer
->reg_name
);
4975 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
4976 "printer-dns-sd-name", 0);
4978 #endif /* HAVE_DNSSD */
4980 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4981 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4982 "printer-error-policy", NULL
, printer
->error_policy
);
4984 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4985 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
4986 printer
->accepting
);
4988 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4989 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
4992 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4993 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4994 "printer-op-policy", NULL
, printer
->op_policy
);
4996 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4997 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
5000 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
5001 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5002 "printer-state-change-time", printer
->state_time
);
5004 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
5005 cupsArrayFind(ra
, "printer-state-history"))
5008 * Printer history is only sent if specifically requested, so that
5009 * older CUPS/IPP clients won't barf on the collection attributes.
5012 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
5013 "printer-state-history",
5014 printer
->num_history
, NULL
);
5016 for (i
= 0; i
< printer
->num_history
; i
++)
5017 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
5018 NULL
, IPP_TAG_ZERO
, 0);
5021 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
5022 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5023 "printer-state-message", NULL
, printer
->state_message
);
5025 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
5026 add_printer_state_reasons(con
, printer
);
5028 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5030 int type
; /* printer-type value */
5034 * Add the CUPS-specific printer-type attribute...
5037 type
= printer
->type
;
5039 if (printer
== DefaultPrinter
)
5040 type
|= CUPS_PRINTER_DEFAULT
;
5042 if (!printer
->accepting
)
5043 type
|= CUPS_PRINTER_REJECTING
;
5045 if (!printer
->shared
)
5046 type
|= CUPS_PRINTER_NOT_SHARED
;
5048 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
5052 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5053 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5054 "printer-up-time", curtime
);
5056 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
5057 !ippFindAttribute(printer
->attrs
, "printer-uri-supported",
5060 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5061 "ipp", NULL
, con
->servername
, con
->serverport
,
5062 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5063 "/classes/%s" : "/printers/%s", printer
->name
);
5064 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5065 "printer-uri-supported", NULL
, printer_uri
);
5066 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5070 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5071 add_queued_job_count(con
, printer
);
5073 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
5074 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
5079 * 'copy_subscription_attrs()' - Copy subscription attributes.
5083 copy_subscription_attrs(
5084 cupsd_client_t
*con
, /* I - Client connection */
5085 cupsd_subscription_t
*sub
, /* I - Subscription */
5086 cups_array_t
*ra
) /* I - Requested attributes array */
5088 ipp_attribute_t
*attr
; /* Current attribute */
5089 char printer_uri
[HTTP_MAX_URI
];
5091 int count
; /* Number of events */
5092 unsigned mask
; /* Current event mask */
5093 const char *name
; /* Current event name */
5097 * Copy the subscription attributes to the response using the
5098 * requested-attributes attribute that may be provided by the client.
5101 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
5103 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5106 * Simple event list...
5109 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5110 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5111 "notify-events", NULL
, name
);
5116 * Complex event list...
5119 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5120 if (sub
->mask
& mask
)
5123 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5124 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5125 "notify-events", count
, NULL
, NULL
);
5127 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5128 if (sub
->mask
& mask
)
5130 attr
->values
[count
].string
.text
=
5131 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5138 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5139 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5140 "notify-job-id", sub
->job
->id
);
5142 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
5143 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5144 "notify-lease-duration", sub
->lease
);
5146 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5148 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5149 "ipp", NULL
, con
->servername
, con
->serverport
,
5150 "/printers/%s", sub
->dest
->name
);
5151 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5152 "notify-printer-uri", NULL
, printer_uri
);
5155 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
5156 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5157 "notify-recipient-uri", NULL
, sub
->recipient
);
5158 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
5159 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5160 "notify-pull-method", NULL
, "ippget");
5162 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
5163 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5164 "notify-subscriber-user-name", NULL
, sub
->owner
);
5166 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5167 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5168 "notify-subscription-id", sub
->id
);
5170 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
5171 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5172 "notify-time-interval", sub
->interval
);
5174 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5175 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5176 sub
->user_data
, sub
->user_data_len
);
5181 * 'create_job()' - Print a file to a printer or class.
5185 create_job(cupsd_client_t
*con
, /* I - Client connection */
5186 ipp_attribute_t
*uri
) /* I - Printer URI */
5188 cupsd_printer_t
*printer
; /* Printer */
5189 cupsd_job_t
*job
; /* New job */
5192 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5193 con
->http
.fd
, uri
->values
[0].string
.text
);
5196 * Is the destination valid?
5199 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5205 send_ipp_status(con
, IPP_NOT_FOUND
,
5206 _("The printer or class was not found."));
5211 * Create the job object...
5214 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5217 job
->pending_timeout
= 1;
5220 * Save and log the job...
5225 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
5226 job
->id
, job
->dest
, job
->username
);
5231 * 'create_requested_array()' - Create an array for the requested-attributes.
5234 static cups_array_t
* /* O - Array of attributes or NULL */
5235 create_requested_array(ipp_t
*request
) /* I - IPP request */
5237 int i
; /* Looping var */
5238 ipp_attribute_t
*requested
; /* requested-attributes attribute */
5239 cups_array_t
*ra
; /* Requested attributes array */
5240 char *value
; /* Current value */
5244 * Get the requested-attributes attribute, and return NULL if we don't
5248 if ((requested
= ippFindAttribute(request
, "requested-attributes",
5249 IPP_TAG_KEYWORD
)) == NULL
)
5253 * If the attribute contains a single "all" keyword, return NULL...
5256 if (requested
->num_values
== 1 &&
5257 !strcmp(requested
->values
[0].string
.text
, "all"))
5261 * Create an array using "strcmp" as the comparison function...
5264 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
5266 for (i
= 0; i
< requested
->num_values
; i
++)
5268 value
= requested
->values
[i
].string
.text
;
5270 if (!strcmp(value
, "job-template"))
5272 cupsArrayAdd(ra
, "copies");
5273 cupsArrayAdd(ra
, "copies-default");
5274 cupsArrayAdd(ra
, "copies-supported");
5275 cupsArrayAdd(ra
, "finishings");
5276 cupsArrayAdd(ra
, "finishings-default");
5277 cupsArrayAdd(ra
, "finishings-supported");
5278 cupsArrayAdd(ra
, "job-hold-until");
5279 cupsArrayAdd(ra
, "job-hold-until-default");
5280 cupsArrayAdd(ra
, "job-hold-until-supported");
5281 cupsArrayAdd(ra
, "job-priority");
5282 cupsArrayAdd(ra
, "job-priority-default");
5283 cupsArrayAdd(ra
, "job-priority-supported");
5284 cupsArrayAdd(ra
, "job-sheets");
5285 cupsArrayAdd(ra
, "job-sheets-default");
5286 cupsArrayAdd(ra
, "job-sheets-supported");
5287 cupsArrayAdd(ra
, "media");
5288 cupsArrayAdd(ra
, "media-default");
5289 cupsArrayAdd(ra
, "media-supported");
5290 cupsArrayAdd(ra
, "multiple-document-handling");
5291 cupsArrayAdd(ra
, "multiple-document-handling-default");
5292 cupsArrayAdd(ra
, "multiple-document-handling-supported");
5293 cupsArrayAdd(ra
, "number-up");
5294 cupsArrayAdd(ra
, "number-up-default");
5295 cupsArrayAdd(ra
, "number-up-supported");
5296 cupsArrayAdd(ra
, "orientation-requested");
5297 cupsArrayAdd(ra
, "orientation-requested-default");
5298 cupsArrayAdd(ra
, "orientation-requested-supported");
5299 cupsArrayAdd(ra
, "page-ranges");
5300 cupsArrayAdd(ra
, "page-ranges-supported");
5301 cupsArrayAdd(ra
, "printer-resolution");
5302 cupsArrayAdd(ra
, "printer-resolution-default");
5303 cupsArrayAdd(ra
, "printer-resolution-supported");
5304 cupsArrayAdd(ra
, "print-quality");
5305 cupsArrayAdd(ra
, "print-quality-default");
5306 cupsArrayAdd(ra
, "print-quality-supported");
5307 cupsArrayAdd(ra
, "sides");
5308 cupsArrayAdd(ra
, "sides-default");
5309 cupsArrayAdd(ra
, "sides-supported");
5311 else if (!strcmp(value
, "job-description"))
5313 cupsArrayAdd(ra
, "date-time-at-completed");
5314 cupsArrayAdd(ra
, "date-time-at-creation");
5315 cupsArrayAdd(ra
, "date-time-at-processing");
5316 cupsArrayAdd(ra
, "job-detailed-status-message");
5317 cupsArrayAdd(ra
, "job-document-access-errors");
5318 cupsArrayAdd(ra
, "job-id");
5319 cupsArrayAdd(ra
, "job-impressions");
5320 cupsArrayAdd(ra
, "job-impressions-completed");
5321 cupsArrayAdd(ra
, "job-k-octets");
5322 cupsArrayAdd(ra
, "job-k-octets-processed");
5323 cupsArrayAdd(ra
, "job-media-sheets");
5324 cupsArrayAdd(ra
, "job-media-sheets-completed");
5325 cupsArrayAdd(ra
, "job-message-from-operator");
5326 cupsArrayAdd(ra
, "job-more-info");
5327 cupsArrayAdd(ra
, "job-name");
5328 cupsArrayAdd(ra
, "job-originating-user-name");
5329 cupsArrayAdd(ra
, "job-printer-up-time");
5330 cupsArrayAdd(ra
, "job-printer-uri");
5331 cupsArrayAdd(ra
, "job-state");
5332 cupsArrayAdd(ra
, "job-state-message");
5333 cupsArrayAdd(ra
, "job-state-reasons");
5334 cupsArrayAdd(ra
, "job-uri");
5335 cupsArrayAdd(ra
, "number-of-documents");
5336 cupsArrayAdd(ra
, "number-of-intervening-jobs");
5337 cupsArrayAdd(ra
, "output-device-assigned");
5338 cupsArrayAdd(ra
, "time-at-completed");
5339 cupsArrayAdd(ra
, "time-at-creation");
5340 cupsArrayAdd(ra
, "time-at-processing");
5342 else if (!strcmp(value
, "printer-description"))
5344 cupsArrayAdd(ra
, "charset-configured");
5345 cupsArrayAdd(ra
, "charset-supported");
5346 cupsArrayAdd(ra
, "color-supported");
5347 cupsArrayAdd(ra
, "compression-supported");
5348 cupsArrayAdd(ra
, "document-format-default");
5349 cupsArrayAdd(ra
, "document-format-supported");
5350 cupsArrayAdd(ra
, "generated-natural-language-supported");
5351 cupsArrayAdd(ra
, "ipp-versions-supported");
5352 cupsArrayAdd(ra
, "job-impressions-supported");
5353 cupsArrayAdd(ra
, "job-k-octets-supported");
5354 cupsArrayAdd(ra
, "job-media-sheets-supported");
5355 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
5356 cupsArrayAdd(ra
, "multiple-operation-time-out");
5357 cupsArrayAdd(ra
, "natural-language-configured");
5358 cupsArrayAdd(ra
, "notify-attributes-supported");
5359 cupsArrayAdd(ra
, "notify-lease-duration-default");
5360 cupsArrayAdd(ra
, "notify-lease-duration-supported");
5361 cupsArrayAdd(ra
, "notify-max-events-supported");
5362 cupsArrayAdd(ra
, "notify-events-default");
5363 cupsArrayAdd(ra
, "notify-events-supported");
5364 cupsArrayAdd(ra
, "notify-pull-method-supported");
5365 cupsArrayAdd(ra
, "notify-schemes-supported");
5366 cupsArrayAdd(ra
, "operations-supported");
5367 cupsArrayAdd(ra
, "pages-per-minute");
5368 cupsArrayAdd(ra
, "pages-per-minute-color");
5369 cupsArrayAdd(ra
, "pdl-override-supported");
5370 cupsArrayAdd(ra
, "printer-alert");
5371 cupsArrayAdd(ra
, "printer-alert-description");
5372 cupsArrayAdd(ra
, "printer-current-time");
5373 cupsArrayAdd(ra
, "printer-driver-installer");
5374 cupsArrayAdd(ra
, "printer-info");
5375 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
5376 cupsArrayAdd(ra
, "printer-location");
5377 cupsArrayAdd(ra
, "printer-make-and-model");
5378 cupsArrayAdd(ra
, "printer-message-from-operator");
5379 cupsArrayAdd(ra
, "printer-more-info");
5380 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
5381 cupsArrayAdd(ra
, "printer-name");
5382 cupsArrayAdd(ra
, "printer-state");
5383 cupsArrayAdd(ra
, "printer-state-message");
5384 cupsArrayAdd(ra
, "printer-state-reasons");
5385 cupsArrayAdd(ra
, "printer-up-time");
5386 cupsArrayAdd(ra
, "printer-uri-supported");
5387 cupsArrayAdd(ra
, "queued-job-count");
5388 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
5389 cupsArrayAdd(ra
, "uri-authentication-supported");
5390 cupsArrayAdd(ra
, "uri-security-supported");
5392 else if (!strcmp(value
, "printer-defaults"))
5394 char *name
; /* Option name */
5397 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5399 name
= (char *)cupsArrayNext(CommonDefaults
))
5400 cupsArrayAdd(ra
, name
);
5402 else if (!strcmp(value
, "subscription-template"))
5404 cupsArrayAdd(ra
, "notify-attributes");
5405 cupsArrayAdd(ra
, "notify-charset");
5406 cupsArrayAdd(ra
, "notify-events");
5407 cupsArrayAdd(ra
, "notify-lease-duration");
5408 cupsArrayAdd(ra
, "notify-natural-language");
5409 cupsArrayAdd(ra
, "notify-pull-method");
5410 cupsArrayAdd(ra
, "notify-recipient-uri");
5411 cupsArrayAdd(ra
, "notify-time-interval");
5412 cupsArrayAdd(ra
, "notify-user-data");
5415 cupsArrayAdd(ra
, value
);
5423 * 'create_subscription()' - Create a notification subscription.
5427 create_subscription(
5428 cupsd_client_t
*con
, /* I - Client connection */
5429 ipp_attribute_t
*uri
) /* I - Printer URI */
5431 http_status_t status
; /* Policy status */
5432 int i
; /* Looping var */
5433 ipp_attribute_t
*attr
; /* Current attribute */
5434 cups_ptype_t dtype
; /* Destination type (printer/class) */
5435 char scheme
[HTTP_MAX_URI
],
5436 /* Scheme portion of URI */
5437 userpass
[HTTP_MAX_URI
],
5438 /* Username portion of URI */
5440 /* Host portion of URI */
5441 resource
[HTTP_MAX_URI
];
5442 /* Resource portion of URI */
5443 int port
; /* Port portion of URI */
5444 cupsd_printer_t
*printer
; /* Printer/class */
5445 cupsd_job_t
*job
; /* Job */
5446 int jobid
; /* Job ID */
5447 cupsd_subscription_t
*sub
; /* Subscription object */
5448 const char *username
, /* requesting-user-name or
5449 authenticated username */
5450 *recipient
, /* notify-recipient-uri */
5451 *pullmethod
; /* notify-pull-method */
5452 ipp_attribute_t
*user_data
; /* notify-user-data */
5453 int interval
, /* notify-time-interval */
5454 lease
; /* notify-lease-duration */
5455 unsigned mask
; /* notify-events */
5456 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5457 *notify_lease
; /* notify-lease-duration(-default) */
5461 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5463 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5464 cupsdLogMessage(CUPSD_LOG_DEBUG
, "g%04x v%04x %s", attr
->group_tag
,
5465 attr
->value_tag
, attr
->name
);
5467 cupsdLogMessage(CUPSD_LOG_DEBUG
, "----SEP----");
5472 * Is the destination valid?
5475 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5476 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5477 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5479 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5480 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5481 sizeof(host
), &port
, resource
, sizeof(resource
));
5483 if (!strcmp(resource
, "/"))
5485 dtype
= (cups_ptype_t
)0;
5488 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5490 dtype
= (cups_ptype_t
)0;
5493 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5495 dtype
= CUPS_PRINTER_CLASS
;
5498 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5504 send_ipp_status(con
, IPP_NOT_FOUND
,
5505 _("The printer or class was not found."));
5515 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5518 send_http_error(con
, status
, printer
);
5522 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5524 send_http_error(con
, status
, NULL
);
5529 * Get the user that is requesting the subscription...
5532 username
= get_username(con
);
5535 * Find the first subscription group attribute; return if we have
5539 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5540 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5545 send_ipp_status(con
, IPP_BAD_REQUEST
,
5546 _("No subscription attributes in request!"));
5551 * Process the subscription attributes in the request...
5554 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5562 lease
= DefaultLeaseDuration
;
5564 mask
= CUPSD_EVENT_NONE
;
5568 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5570 notify_lease
= ippFindAttribute(printer
->attrs
,
5571 "notify-lease-duration-default",
5575 lease
= notify_lease
->values
[0].integer
;
5579 notify_events
= NULL
;
5580 notify_lease
= NULL
;
5583 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5585 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5586 attr
->value_tag
== IPP_TAG_URI
)
5589 * Validate the recipient scheme against the ServerBin/notifier
5593 char notifier
[1024]; /* Notifier filename */
5596 recipient
= attr
->values
[0].string
.text
;
5598 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5599 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5600 host
, sizeof(host
), &port
,
5601 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5603 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5604 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
5605 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5606 "notify-status-code", IPP_URI_SCHEME
);
5610 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5612 if (access(notifier
, X_OK
))
5614 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5615 _("notify-recipient-uri URI \"%s\" uses unknown "
5616 "scheme!"), recipient
);
5617 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5618 "notify-status-code", IPP_URI_SCHEME
);
5622 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5623 attr
->value_tag
== IPP_TAG_KEYWORD
)
5625 pullmethod
= attr
->values
[0].string
.text
;
5627 if (strcmp(pullmethod
, "ippget"))
5629 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5630 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
5631 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5632 "notify-status-code", IPP_ATTRIBUTES
);
5636 else if (!strcmp(attr
->name
, "notify-charset") &&
5637 attr
->value_tag
== IPP_TAG_CHARSET
&&
5638 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5639 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5641 send_ipp_status(con
, IPP_CHARSET
,
5642 _("Character set \"%s\" not supported!"),
5643 attr
->values
[0].string
.text
);
5646 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5647 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5648 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5650 send_ipp_status(con
, IPP_CHARSET
,
5651 _("Language \"%s\" not supported!"),
5652 attr
->values
[0].string
.text
);
5655 else if (!strcmp(attr
->name
, "notify-user-data") &&
5656 attr
->value_tag
== IPP_TAG_STRING
)
5658 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5660 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5661 _("The notify-user-data value is too large "
5662 "(%d > 63 octets)!"),
5663 attr
->values
[0].unknown
.length
);
5669 else if (!strcmp(attr
->name
, "notify-events") &&
5670 attr
->value_tag
== IPP_TAG_KEYWORD
)
5671 notify_events
= attr
;
5672 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5673 attr
->value_tag
== IPP_TAG_INTEGER
)
5674 lease
= attr
->values
[0].integer
;
5675 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5676 attr
->value_tag
== IPP_TAG_INTEGER
)
5677 interval
= attr
->values
[0].integer
;
5678 else if (!strcmp(attr
->name
, "notify-job-id") &&
5679 attr
->value_tag
== IPP_TAG_INTEGER
)
5680 jobid
= attr
->values
[0].integer
;
5687 for (i
= 0; i
< notify_events
->num_values
; i
++)
5688 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5692 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5694 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5695 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5696 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5698 if (!recipient
&& !pullmethod
)
5701 if (mask
== CUPSD_EVENT_NONE
)
5704 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5706 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5709 send_ipp_status(con
, IPP_BAD_REQUEST
,
5710 _("notify-events not specified!"));
5715 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5717 cupsdLogMessage(CUPSD_LOG_INFO
,
5718 "create_subscription: Limiting notify-lease-duration to "
5721 lease
= MaxLeaseDuration
;
5726 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5728 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
5735 sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0);
5738 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
5741 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5742 "Added subscription %d for printer \"%s\"",
5743 sub
->id
, printer
->name
);
5745 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
5748 sub
->interval
= interval
;
5750 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5752 cupsdSetString(&sub
->owner
, username
);
5756 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5757 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5758 sub
->user_data_len
);
5761 ippAddSeparator(con
->response
);
5762 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5763 "notify-subscription-id", sub
->id
);
5765 con
->response
->request
.status
.status_code
= IPP_OK
;
5771 cupsdSaveAllSubscriptions();
5777 * 'delete_printer()' - Remove a printer or class from the system.
5781 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5782 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5784 http_status_t status
; /* Policy status */
5785 cups_ptype_t dtype
; /* Destination type (printer/class) */
5786 cupsd_printer_t
*printer
; /* Printer/class */
5787 char filename
[1024]; /* Script/PPD filename */
5790 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5791 con
->http
.fd
, uri
->values
[0].string
.text
);
5794 * Do we have a valid URI?
5797 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5803 send_ipp_status(con
, IPP_NOT_FOUND
,
5804 _("The printer or class was not found."));
5812 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5814 send_http_error(con
, status
, NULL
);
5819 * Remove old jobs...
5822 cupsdCancelJobs(printer
->name
, NULL
, 1);
5825 * Remove old subscriptions and send a "deleted printer" event...
5828 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5829 "%s \"%s\" deleted by \"%s\".",
5830 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5831 printer
->name
, get_username(con
));
5833 cupsdExpireSubscriptions(printer
, NULL
);
5836 * Remove any old PPD or script files...
5839 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5843 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5849 * Unregister color profiles...
5852 apple_unregister_profiles(printer
);
5853 #endif /* __APPLE__ */
5855 if (dtype
& CUPS_PRINTER_CLASS
)
5857 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5858 printer
->name
, get_username(con
));
5860 cupsdDeletePrinter(printer
, 0);
5861 cupsdSaveAllClasses();
5865 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5866 printer
->name
, get_username(con
));
5868 cupsdDeletePrinter(printer
, 0);
5869 cupsdSaveAllPrinters();
5872 cupsdWritePrintcap();
5875 * Return with no errors...
5878 con
->response
->request
.status
.status_code
= IPP_OK
;
5883 * 'get_default()' - Get the default destination.
5887 get_default(cupsd_client_t
*con
) /* I - Client connection */
5889 http_status_t status
; /* Policy status */
5890 cups_array_t
*ra
; /* Requested attributes array */
5893 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
5899 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5901 send_http_error(con
, status
, NULL
);
5907 ra
= create_requested_array(con
->request
);
5909 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5911 cupsArrayDelete(ra
);
5913 con
->response
->request
.status
.status_code
= IPP_OK
;
5916 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
5921 * 'get_devices()' - Get the list of available devices on the local system.
5925 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5927 http_status_t status
; /* Policy status */
5928 ipp_attribute_t
*limit
, /* Limit attribute */
5929 *requested
; /* requested-attributes attribute */
5930 char command
[1024], /* cups-deviced command */
5931 options
[1024], /* Options to pass to command */
5933 /* String for requested attributes */
5936 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
5942 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5944 send_http_error(con
, status
, NULL
);
5949 * Run cups-deviced command with the given options...
5952 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5953 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5957 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5959 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5961 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5962 snprintf(options
, sizeof(options
),
5964 con
->request
->request
.op
.request_id
,
5965 limit
? limit
->values
[0].integer
: 0, (int)User
,
5968 if (cupsdSendCommand(con
, command
, options
, 1))
5971 * Command started successfully, don't send an IPP response here...
5974 ippDelete(con
->response
);
5975 con
->response
= NULL
;
5980 * Command failed, return "internal error" so the user knows something
5984 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5985 _("cups-deviced failed to execute."));
5991 * 'get_document()' - Get a copy of a job file.
5995 get_document(cupsd_client_t
*con
, /* I - Client connection */
5996 ipp_attribute_t
*uri
) /* I - Job URI */
5998 http_status_t status
; /* Policy status */
5999 ipp_attribute_t
*attr
; /* Current attribute */
6000 int jobid
; /* Job ID */
6001 int docnum
; /* Document number */
6002 cupsd_job_t
*job
; /* Current job */
6003 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6004 username
[HTTP_MAX_URI
], /* Username portion of URI */
6005 host
[HTTP_MAX_URI
], /* Host portion of URI */
6006 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6007 int port
; /* Port portion of URI */
6008 char filename
[1024], /* Filename for document */
6009 format
[1024]; /* Format for document */
6012 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6013 con
->http
.fd
, uri
->values
[0].string
.text
);
6016 * See if we have a job URI or a printer URI...
6019 if (!strcmp(uri
->name
, "printer-uri"))
6022 * Got a printer URI; see if we also have a job-id attribute...
6025 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6026 IPP_TAG_INTEGER
)) == NULL
)
6028 send_ipp_status(con
, IPP_BAD_REQUEST
,
6029 _("Got a printer-uri attribute but no job-id!"));
6033 jobid
= attr
->values
[0].integer
;
6038 * Got a job URI; parse it to get the job ID...
6041 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6042 sizeof(method
), username
, sizeof(username
), host
,
6043 sizeof(host
), &port
, resource
, sizeof(resource
));
6045 if (strncmp(resource
, "/jobs/", 6))
6051 send_ipp_status(con
, IPP_BAD_REQUEST
,
6052 _("Bad job-uri attribute \"%s\"!"),
6053 uri
->values
[0].string
.text
);
6057 jobid
= atoi(resource
+ 6);
6061 * See if the job exists...
6064 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6067 * Nope - return a "not found" error...
6070 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6078 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6080 send_http_error(con
, status
, NULL
);
6085 * Get the document number...
6088 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6089 IPP_TAG_INTEGER
)) == NULL
)
6091 send_ipp_status(con
, IPP_BAD_REQUEST
,
6092 _("Missing document-number attribute!"));
6096 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6097 attr
->num_values
> 1)
6099 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
6104 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6106 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6108 cupsdLogMessage(CUPSD_LOG_ERROR
,
6109 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6111 send_ipp_status(con
, IPP_NOT_FOUND
,
6112 _("Unable to open document %d in job %d!"), docnum
, jobid
);
6116 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6120 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6121 job
->filetypes
[docnum
- 1]->type
);
6123 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6125 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6127 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6128 IPP_TAG_NAME
)) != NULL
)
6129 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6130 NULL
, attr
->values
[0].string
.text
);
6135 * 'get_job_attrs()' - Get job attributes.
6139 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6140 ipp_attribute_t
*uri
) /* I - Job URI */
6142 http_status_t status
; /* Policy status */
6143 ipp_attribute_t
*attr
; /* Current attribute */
6144 int jobid
; /* Job ID */
6145 cupsd_job_t
*job
; /* Current job */
6146 char method
[HTTP_MAX_URI
], /* Method portion of URI */
6147 username
[HTTP_MAX_URI
], /* Username portion of URI */
6148 host
[HTTP_MAX_URI
], /* Host portion of URI */
6149 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6150 int port
; /* Port portion of URI */
6151 cups_array_t
*ra
; /* Requested attributes array */
6154 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6155 con
->http
.fd
, uri
->values
[0].string
.text
);
6158 * See if we have a job URI or a printer URI...
6161 if (!strcmp(uri
->name
, "printer-uri"))
6164 * Got a printer URI; see if we also have a job-id attribute...
6167 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6168 IPP_TAG_INTEGER
)) == NULL
)
6170 send_ipp_status(con
, IPP_BAD_REQUEST
,
6171 _("Got a printer-uri attribute but no job-id!"));
6175 jobid
= attr
->values
[0].integer
;
6180 * Got a job URI; parse it to get the job ID...
6183 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
6184 sizeof(method
), username
, sizeof(username
), host
,
6185 sizeof(host
), &port
, resource
, sizeof(resource
));
6187 if (strncmp(resource
, "/jobs/", 6))
6193 send_ipp_status(con
, IPP_BAD_REQUEST
,
6194 _("Bad job-uri attribute \"%s\"!"),
6195 uri
->values
[0].string
.text
);
6199 jobid
= atoi(resource
+ 6);
6203 * See if the job exists...
6206 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6209 * Nope - return a "not found" error...
6212 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6220 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6222 send_http_error(con
, status
, NULL
);
6227 * Copy attributes...
6232 ra
= create_requested_array(con
->request
);
6233 copy_job_attrs(con
, job
, ra
);
6234 cupsArrayDelete(ra
);
6236 con
->response
->request
.status
.status_code
= IPP_OK
;
6241 * 'get_jobs()' - Get a list of jobs for the specified printer.
6245 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6246 ipp_attribute_t
*uri
) /* I - Printer URI */
6248 http_status_t status
; /* Policy status */
6249 ipp_attribute_t
*attr
; /* Current attribute */
6250 const char *dest
; /* Destination */
6251 cups_ptype_t dtype
; /* Destination type (printer/class) */
6252 cups_ptype_t dmask
; /* Destination type mask */
6253 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6254 username
[HTTP_MAX_URI
], /* Username portion of URI */
6255 host
[HTTP_MAX_URI
], /* Host portion of URI */
6256 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6257 int port
; /* Port portion of URI */
6258 int completed
; /* Completed jobs? */
6259 int first_job_id
; /* First job ID */
6260 int limit
; /* Maximum number of jobs to return */
6261 int count
; /* Number of jobs that match */
6262 cupsd_job_t
*job
; /* Current job pointer */
6263 cupsd_printer_t
*printer
; /* Printer */
6264 cups_array_t
*list
; /* Which job list... */
6265 cups_array_t
*ra
; /* Requested attributes array */
6268 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
6269 uri
->values
[0].string
.text
);
6272 * Is the destination valid?
6275 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6276 sizeof(scheme
), username
, sizeof(username
), host
,
6277 sizeof(host
), &port
, resource
, sizeof(resource
));
6279 if (!strcmp(resource
, "/") ||
6280 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6))
6283 dtype
= (cups_ptype_t
)0;
6284 dmask
= (cups_ptype_t
)0;
6287 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6290 dtype
= (cups_ptype_t
)0;
6291 dmask
= CUPS_PRINTER_CLASS
;
6294 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6297 dtype
= CUPS_PRINTER_CLASS
;
6298 dmask
= CUPS_PRINTER_CLASS
;
6301 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6308 send_ipp_status(con
, IPP_NOT_FOUND
,
6309 _("The printer or class was not found."));
6314 dtype
&= CUPS_PRINTER_CLASS
;
6315 dmask
= CUPS_PRINTER_CLASS
;
6324 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6327 send_http_error(con
, status
, printer
);
6331 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6333 send_http_error(con
, status
, NULL
);
6338 * See if the "which-jobs" attribute have been specified...
6341 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6342 IPP_TAG_KEYWORD
)) != NULL
&&
6343 !strcmp(attr
->values
[0].string
.text
, "completed"))
6348 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
6360 * See if they want to limit the number of jobs reported...
6363 if ((attr
= ippFindAttribute(con
->request
, "limit",
6364 IPP_TAG_INTEGER
)) != NULL
)
6365 limit
= attr
->values
[0].integer
;
6369 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
6370 IPP_TAG_INTEGER
)) != NULL
)
6371 first_job_id
= attr
->values
[0].integer
;
6376 * See if we only want to see jobs for a specific user...
6379 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
6380 IPP_TAG_BOOLEAN
)) != NULL
&&
6381 attr
->values
[0].boolean
)
6382 strlcpy(username
, get_username(con
), sizeof(username
));
6386 ra
= create_requested_array(con
->request
);
6389 * OK, build a list of jobs for this printer...
6392 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6393 count
< limit
&& job
;
6394 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6397 * Filter out jobs that don't match...
6400 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
6402 if (!job
->dest
|| !job
->username
)
6405 if (!job
->dest
|| !job
->username
)
6408 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6409 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6411 if ((job
->dtype
& dmask
) != dtype
&&
6412 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6414 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
6417 if (job
->id
< first_job_id
)
6425 if (username
[0] && strcasecmp(username
, job
->username
))
6429 ippAddSeparator(con
->response
);
6433 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
6435 copy_job_attrs(con
, job
, ra
);
6438 cupsArrayDelete(ra
);
6440 con
->response
->request
.status
.status_code
= IPP_OK
;
6445 * 'get_notifications()' - Get events for a subscription.
6449 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6451 int i
, j
; /* Looping vars */
6452 http_status_t status
; /* Policy status */
6453 cupsd_subscription_t
*sub
; /* Subscription */
6454 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6455 *sequences
; /* notify-sequence-numbers */
6456 int min_seq
; /* Minimum sequence number */
6457 int interval
; /* Poll interval */
6460 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6464 * Get subscription attributes...
6467 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6469 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6474 send_ipp_status(con
, IPP_BAD_REQUEST
,
6475 _("Missing notify-subscription-ids attribute!"));
6480 * Are the subscription IDs valid?
6483 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6485 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6488 * Bad subscription ID...
6491 send_ipp_status(con
, IPP_NOT_FOUND
,
6492 _("notify-subscription-id %d no good!"),
6493 ids
->values
[i
].integer
);
6501 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6503 con
, sub
->owner
)) != HTTP_OK
)
6505 send_http_error(con
, status
, sub
->dest
);
6510 * Check the subscription type and update the interval accordingly.
6513 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6516 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6518 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6524 * Tell the client to poll again in N seconds...
6528 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6529 "notify-get-interval", interval
);
6531 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6532 "printer-up-time", time(NULL
));
6535 * Copy the subscription event attributes to the response.
6538 con
->response
->request
.status
.status_code
=
6539 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6541 for (i
= 0; i
< ids
->num_values
; i
++)
6544 * Get the subscription and sequence number...
6547 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6549 if (sequences
&& i
< sequences
->num_values
)
6550 min_seq
= sequences
->values
[i
].integer
;
6555 * If we don't have any new events, nothing to do here...
6558 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
6562 * Otherwise copy all of the new events...
6565 if (sub
->first_event_id
> min_seq
)
6568 j
= min_seq
- sub
->first_event_id
;
6570 for (; j
< sub
->num_events
; j
++)
6572 ippAddSeparator(con
->response
);
6574 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
6575 IPP_TAG_EVENT_NOTIFICATION
, 0);
6582 * 'get_ppd()' - Get a named PPD from the local system.
6586 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
6587 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
6589 http_status_t status
; /* Policy status */
6590 cupsd_printer_t
*dest
; /* Destination */
6591 cups_ptype_t dtype
; /* Destination type */
6594 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
6595 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
6597 if (!strcmp(uri
->name
, "ppd-name"))
6600 * Return a PPD file from cups-driverd...
6603 char command
[1024], /* cups-driverd command */
6604 options
[1024], /* Options to pass to command */
6605 ppd_name
[1024]; /* ppd-name */
6612 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6614 send_http_error(con
, status
, NULL
);
6619 * Run cups-driverd command with the given options...
6622 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6623 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
6624 snprintf(options
, sizeof(options
), "get+%d+%s",
6625 con
->request
->request
.op
.request_id
, ppd_name
);
6627 if (cupsdSendCommand(con
, command
, options
, 0))
6630 * Command started successfully, don't send an IPP response here...
6633 ippDelete(con
->response
);
6634 con
->response
= NULL
;
6639 * Command failed, return "internal error" so the user knows something
6643 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6644 _("cups-driverd failed to execute."));
6647 else if (!strcmp(uri
->name
, "printer-uri") &&
6648 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
6650 int i
; /* Looping var */
6651 char filename
[1024]; /* PPD filename */
6658 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6660 send_http_error(con
, status
, dest
);
6665 * See if we need the PPD for a class or remote printer...
6668 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6671 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
6673 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6674 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6675 "printer-uri", NULL
, dest
->uri
);
6678 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
6680 for (i
= 0; i
< dest
->num_printers
; i
++)
6681 if (!(dest
->printers
[i
]->type
&
6682 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
6684 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6685 dest
->printers
[i
]->name
);
6687 if (!access(filename
, 0))
6691 if (i
< dest
->num_printers
)
6692 dest
= dest
->printers
[i
];
6695 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6696 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6697 "printer-uri", NULL
, dest
->printers
[0]->uri
);
6703 * Found the printer with the PPD file, now see if there is one...
6706 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
6708 send_ipp_status(con
, IPP_NOT_FOUND
,
6709 _("The PPD file \"%s\" could not be opened: %s"),
6710 uri
->values
[0].string
.text
, strerror(errno
));
6714 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6718 con
->response
->request
.status
.status_code
= IPP_OK
;
6721 send_ipp_status(con
, IPP_NOT_FOUND
,
6722 _("The PPD file \"%s\" could not be found."),
6723 uri
->values
[0].string
.text
);
6728 * 'get_ppds()' - Get the list of PPD files on the local system.
6732 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
6734 http_status_t status
; /* Policy status */
6735 ipp_attribute_t
*limit
, /* Limit attribute */
6736 *device
, /* ppd-device-id attribute */
6737 *language
, /* ppd-natural-language attribute */
6738 *make
, /* ppd-make attribute */
6739 *model
, /* ppd-make-and-model attribute */
6740 *model_number
, /* ppd-model-number attribute */
6741 *product
, /* ppd-product attribute */
6742 *psversion
, /* ppd-psverion attribute */
6743 *type
, /* ppd-type attribute */
6744 *requested
; /* requested-attributes attribute */
6745 char command
[1024], /* cups-driverd command */
6746 options
[1024], /* Options to pass to command */
6747 device_str
[256],/* Escaped ppd-device-id string */
6749 /* Escaped ppd-natural-language */
6750 make_str
[256], /* Escaped ppd-make string */
6751 model_str
[256], /* Escaped ppd-make-and-model string */
6752 model_number_str
[256],
6753 /* ppd-model-number string */
6755 /* Escaped ppd-product string */
6757 /* Escaped ppd-psversion string */
6758 type_str
[256], /* Escaped ppd-type string */
6760 /* String for requested attributes */
6763 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
6769 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6771 send_http_error(con
, status
, NULL
);
6776 * Run cups-driverd command with the given options...
6779 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6780 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
6781 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
6783 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
6784 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
6786 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
6788 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
6789 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
6790 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
6791 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6795 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6797 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6800 url_encode_attr(device
, device_str
, sizeof(device_str
));
6802 device_str
[0] = '\0';
6805 url_encode_attr(language
, language_str
, sizeof(language_str
));
6807 language_str
[0] = '\0';
6810 url_encode_attr(make
, make_str
, sizeof(make_str
));
6815 url_encode_attr(model
, model_str
, sizeof(model_str
));
6817 model_str
[0] = '\0';
6820 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
6821 model_number
->values
[0].integer
);
6823 model_number_str
[0] = '\0';
6826 url_encode_attr(product
, product_str
, sizeof(product_str
));
6828 product_str
[0] = '\0';
6831 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
6833 psversion_str
[0] = '\0';
6836 url_encode_attr(type
, type_str
, sizeof(type_str
));
6840 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6841 snprintf(options
, sizeof(options
),
6842 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
6843 con
->request
->request
.op
.request_id
,
6844 limit
? limit
->values
[0].integer
: 0,
6846 device
? "%20" : "", device_str
,
6847 language
? "%20" : "", language_str
,
6848 make
? "%20" : "", make_str
,
6849 model
? "%20" : "", model_str
,
6850 model_number
? "%20" : "", model_number_str
,
6851 product
? "%20" : "", product_str
,
6852 psversion
? "%20" : "", psversion_str
,
6853 type
? "%20" : "", type_str
);
6855 if (cupsdSendCommand(con
, command
, options
, 0))
6858 * Command started successfully, don't send an IPP response here...
6861 ippDelete(con
->response
);
6862 con
->response
= NULL
;
6867 * Command failed, return "internal error" so the user knows something
6871 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6872 _("cups-driverd failed to execute."));
6878 * 'get_printer_attrs()' - Get printer attributes.
6882 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
6883 ipp_attribute_t
*uri
) /* I - Printer URI */
6885 http_status_t status
; /* Policy status */
6886 cups_ptype_t dtype
; /* Destination type (printer/class) */
6887 cupsd_printer_t
*printer
; /* Printer/class */
6888 cups_array_t
*ra
; /* Requested attributes array */
6891 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
6892 con
->http
.fd
, uri
->values
[0].string
.text
);
6895 * Is the destination valid?
6898 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6904 send_ipp_status(con
, IPP_NOT_FOUND
,
6905 _("The printer or class was not found."));
6913 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6915 send_http_error(con
, status
, printer
);
6920 * Send the attributes...
6923 ra
= create_requested_array(con
->request
);
6925 copy_printer_attrs(con
, printer
, ra
);
6927 cupsArrayDelete(ra
);
6929 con
->response
->request
.status
.status_code
= IPP_OK
;
6934 * 'get_printers()' - Get a list of printers or classes.
6938 get_printers(cupsd_client_t
*con
, /* I - Client connection */
6939 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
6941 http_status_t status
; /* Policy status */
6942 ipp_attribute_t
*attr
; /* Current attribute */
6943 int limit
; /* Max number of printers to return */
6944 int count
; /* Number of printers that match */
6945 cupsd_printer_t
*printer
; /* Current printer pointer */
6946 int printer_type
, /* printer-type attribute */
6947 printer_mask
; /* printer-type-mask attribute */
6948 char *location
; /* Location string */
6949 const char *username
; /* Current user */
6950 char *first_printer_name
; /* first-printer-name attribute */
6951 cups_array_t
*ra
; /* Requested attributes array */
6954 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
6955 con
->http
.fd
, type
);
6961 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6963 send_http_error(con
, status
, NULL
);
6968 * Check for printers...
6971 if (!Printers
|| !cupsArrayCount(Printers
))
6973 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
6978 * See if they want to limit the number of printers reported...
6981 if ((attr
= ippFindAttribute(con
->request
, "limit",
6982 IPP_TAG_INTEGER
)) != NULL
)
6983 limit
= attr
->values
[0].integer
;
6987 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
6988 IPP_TAG_NAME
)) != NULL
)
6989 first_printer_name
= attr
->values
[0].string
.text
;
6991 first_printer_name
= NULL
;
6994 * Support filtering...
6997 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
6998 IPP_TAG_ENUM
)) != NULL
)
6999 printer_type
= attr
->values
[0].integer
;
7003 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7004 IPP_TAG_ENUM
)) != NULL
)
7005 printer_mask
= attr
->values
[0].integer
;
7009 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7010 IPP_TAG_TEXT
)) != NULL
)
7011 location
= attr
->values
[0].string
.text
;
7015 if (con
->username
[0])
7016 username
= con
->username
;
7017 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7018 IPP_TAG_NAME
)) != NULL
)
7019 username
= attr
->values
[0].string
.text
;
7023 ra
= create_requested_array(con
->request
);
7026 * OK, build a list of printers for this printer...
7029 if (first_printer_name
)
7031 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7032 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7035 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7038 count
< limit
&& printer
;
7039 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7041 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7042 (printer
->type
& printer_mask
) == printer_type
&&
7043 (!location
|| !printer
->location
||
7044 !strcasecmp(printer
->location
, location
)))
7047 * If HideImplicitMembers is enabled, see if this printer or class
7048 * is a member of an implicit class...
7051 if (ImplicitClasses
&& HideImplicitMembers
&&
7052 printer
->in_implicit_class
)
7056 * If a username is specified, see if it is allowed or denied
7060 if (printer
->num_users
&& username
&& !user_allowed(printer
, username
))
7064 * Add the group separator as needed...
7068 ippAddSeparator(con
->response
);
7073 * Send the attributes...
7076 copy_printer_attrs(con
, printer
, ra
);
7080 cupsArrayDelete(ra
);
7082 con
->response
->request
.status
.status_code
= IPP_OK
;
7087 * 'get_subscription_attrs()' - Get subscription attributes.
7091 get_subscription_attrs(
7092 cupsd_client_t
*con
, /* I - Client connection */
7093 int sub_id
) /* I - Subscription ID */
7095 http_status_t status
; /* Policy status */
7096 cupsd_subscription_t
*sub
; /* Subscription */
7097 cups_array_t
*ra
; /* Requested attributes array */
7100 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7101 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7102 con
, con
->http
.fd
, sub_id
);
7105 * Is the subscription ID valid?
7108 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7111 * Bad subscription ID...
7114 send_ipp_status(con
, IPP_NOT_FOUND
,
7115 _("notify-subscription-id %d no good!"), sub_id
);
7123 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7125 con
, sub
->owner
)) != HTTP_OK
)
7127 send_http_error(con
, status
, sub
->dest
);
7132 * Copy the subscription attributes to the response using the
7133 * requested-attributes attribute that may be provided by the client.
7136 ra
= create_requested_array(con
->request
);
7138 copy_subscription_attrs(con
, sub
, ra
);
7140 cupsArrayDelete(ra
);
7142 con
->response
->request
.status
.status_code
= IPP_OK
;
7147 * 'get_subscriptions()' - Get subscriptions.
7151 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7152 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7154 http_status_t status
; /* Policy status */
7155 int count
; /* Number of subscriptions */
7156 int limit
; /* Limit */
7157 cupsd_subscription_t
*sub
; /* Subscription */
7158 cups_array_t
*ra
; /* Requested attributes array */
7159 ipp_attribute_t
*attr
; /* Attribute */
7160 cups_ptype_t dtype
; /* Destination type (printer/class) */
7161 char scheme
[HTTP_MAX_URI
],
7162 /* Scheme portion of URI */
7163 username
[HTTP_MAX_URI
],
7164 /* Username portion of URI */
7166 /* Host portion of URI */
7167 resource
[HTTP_MAX_URI
];
7168 /* Resource portion of URI */
7169 int port
; /* Port portion of URI */
7170 cupsd_job_t
*job
; /* Job pointer */
7171 cupsd_printer_t
*printer
; /* Printer */
7174 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7175 "get_subscriptions(con=%p[%d], uri=%s)",
7176 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
7179 * Is the destination valid?
7182 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7183 sizeof(scheme
), username
, sizeof(username
), host
,
7184 sizeof(host
), &port
, resource
, sizeof(resource
));
7186 if (!strcmp(resource
, "/") ||
7187 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7188 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7189 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7194 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7197 job
= cupsdFindJob(atoi(resource
+ 6));
7201 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
7206 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7212 send_ipp_status(con
, IPP_NOT_FOUND
,
7213 _("The printer or class was not found."));
7216 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7217 IPP_TAG_INTEGER
)) != NULL
)
7219 job
= cupsdFindJob(attr
->values
[0].integer
);
7223 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
7224 attr
->values
[0].integer
);
7235 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
7237 con
, NULL
)) != HTTP_OK
)
7239 send_http_error(con
, status
, printer
);
7244 * Copy the subscription attributes to the response using the
7245 * requested-attributes attribute that may be provided by the client.
7248 ra
= create_requested_array(con
->request
);
7250 if ((attr
= ippFindAttribute(con
->request
, "limit",
7251 IPP_TAG_INTEGER
)) != NULL
)
7252 limit
= attr
->values
[0].integer
;
7257 * See if we only want to see subscriptions for a specific user...
7260 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7261 IPP_TAG_BOOLEAN
)) != NULL
&&
7262 attr
->values
[0].boolean
)
7263 strlcpy(username
, get_username(con
), sizeof(username
));
7267 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7269 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7270 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7271 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
7273 ippAddSeparator(con
->response
);
7274 copy_subscription_attrs(con
, sub
, ra
);
7277 if (limit
&& count
>= limit
)
7281 cupsArrayDelete(ra
);
7284 con
->response
->request
.status
.status_code
= IPP_OK
;
7286 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7291 * 'get_username()' - Get the username associated with a request.
7294 static const char * /* O - Username */
7295 get_username(cupsd_client_t
*con
) /* I - Connection */
7297 ipp_attribute_t
*attr
; /* Attribute */
7300 if (con
->username
[0])
7301 return (con
->username
);
7302 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7303 IPP_TAG_NAME
)) != NULL
)
7304 return (attr
->values
[0].string
.text
);
7306 return ("anonymous");
7311 * 'hold_job()' - Hold a print job.
7315 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7316 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7318 ipp_attribute_t
*attr
, /* Current job-hold-until */
7319 *newattr
; /* New job-hold-until */
7320 int jobid
; /* Job ID */
7321 char method
[HTTP_MAX_URI
], /* Method portion of URI */
7322 username
[HTTP_MAX_URI
], /* Username portion of URI */
7323 host
[HTTP_MAX_URI
], /* Host portion of URI */
7324 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7325 int port
; /* Port portion of URI */
7326 cupsd_job_t
*job
; /* Job information */
7329 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
7330 uri
->values
[0].string
.text
);
7333 * See if we have a job URI or a printer URI...
7336 if (!strcmp(uri
->name
, "printer-uri"))
7339 * Got a printer URI; see if we also have a job-id attribute...
7342 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7343 IPP_TAG_INTEGER
)) == NULL
)
7345 send_ipp_status(con
, IPP_BAD_REQUEST
,
7346 _("Got a printer-uri attribute but no job-id!"));
7350 jobid
= attr
->values
[0].integer
;
7355 * Got a job URI; parse it to get the job ID...
7358 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
7359 sizeof(method
), username
, sizeof(username
), host
,
7360 sizeof(host
), &port
, resource
, sizeof(resource
));
7362 if (strncmp(resource
, "/jobs/", 6))
7368 send_ipp_status(con
, IPP_BAD_REQUEST
,
7369 _("Bad job-uri attribute \"%s\"!"),
7370 uri
->values
[0].string
.text
);
7374 jobid
= atoi(resource
+ 6);
7378 * See if the job exists...
7381 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7384 * Nope - return a "not found" error...
7387 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7392 * See if the job is owned by the requesting user...
7395 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7397 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7402 * Hold the job and return...
7407 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
7408 "Job held by user.");
7410 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
7411 IPP_TAG_KEYWORD
)) == NULL
)
7412 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7414 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7415 IPP_TAG_KEYWORD
)) == NULL
)
7416 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
7421 * Free the old hold value and copy the new one over...
7424 _cupsStrFree(attr
->values
[0].string
.text
);
7428 attr
->value_tag
= newattr
->value_tag
;
7429 attr
->values
[0].string
.text
=
7430 _cupsStrAlloc(newattr
->values
[0].string
.text
);
7434 attr
->value_tag
= IPP_TAG_KEYWORD
;
7435 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
7439 * Hold job until specified time...
7442 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
7444 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
7445 "Job job-hold-until value changed by user.");
7448 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Held by \"%s\".", jobid
,
7451 con
->response
->request
.status
.status_code
= IPP_OK
;
7456 * 'move_job()' - Move a job to a new destination.
7460 move_job(cupsd_client_t
*con
, /* I - Client connection */
7461 ipp_attribute_t
*uri
) /* I - Job URI */
7463 http_status_t status
; /* Policy status */
7464 ipp_attribute_t
*attr
; /* Current attribute */
7465 int jobid
; /* Job ID */
7466 cupsd_job_t
*job
; /* Current job */
7467 const char *src
; /* Source printer/class */
7468 cups_ptype_t stype
, /* Source type (printer or class) */
7469 dtype
; /* Destination type (printer/class) */
7470 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7471 username
[HTTP_MAX_URI
], /* Username portion of URI */
7472 host
[HTTP_MAX_URI
], /* Host portion of URI */
7473 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7474 int port
; /* Port portion of URI */
7475 cupsd_printer_t
*sprinter
, /* Source printer */
7476 *dprinter
; /* Destination printer */
7479 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
7480 uri
->values
[0].string
.text
);
7483 * Get the new printer or class...
7486 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7487 IPP_TAG_URI
)) == NULL
)
7490 * Need job-printer-uri...
7493 send_ipp_status(con
, IPP_BAD_REQUEST
,
7494 _("job-printer-uri attribute missing!"));
7498 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7504 send_ipp_status(con
, IPP_NOT_FOUND
,
7505 _("The printer or class was not found."));
7510 * See if we have a job URI or a printer URI...
7513 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7514 sizeof(scheme
), username
, sizeof(username
), host
,
7515 sizeof(host
), &port
, resource
, sizeof(resource
));
7517 if (!strcmp(uri
->name
, "printer-uri"))
7520 * Got a printer URI; see if we also have a job-id attribute...
7523 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7524 IPP_TAG_INTEGER
)) == NULL
)
7530 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
7531 &sprinter
)) == NULL
)
7537 send_ipp_status(con
, IPP_NOT_FOUND
,
7538 _("The printer or class was not found."));
7547 * Otherwise, just move a single job...
7550 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7553 * Nope - return a "not found" error...
7556 send_ipp_status(con
, IPP_NOT_FOUND
,
7557 _("Job #%d does not exist!"), attr
->values
[0].integer
);
7563 * Job found, initialize source pointers...
7574 * Got a job URI; parse it to get the job ID...
7577 if (strncmp(resource
, "/jobs/", 6))
7583 send_ipp_status(con
, IPP_BAD_REQUEST
,
7584 _("Bad job-uri attribute \"%s\"!"),
7585 uri
->values
[0].string
.text
);
7590 * See if the job exists...
7593 jobid
= atoi(resource
+ 6);
7595 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7598 * Nope - return a "not found" error...
7601 send_ipp_status(con
, IPP_NOT_FOUND
,
7602 _("Job #%d does not exist!"), jobid
);
7608 * Job found, initialize source pointers...
7617 * Check the policy of the destination printer...
7620 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
7621 job
? job
->username
: NULL
)) != HTTP_OK
)
7623 send_http_error(con
, status
, dprinter
);
7628 * Now move the job or jobs...
7634 * See if the job has been completed...
7637 if (job
->state_value
> IPP_JOB_STOPPED
)
7640 * Return a "not-possible" error...
7643 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7644 _("Job #%d is finished and cannot be altered!"),
7650 * See if the job is owned by the requesting user...
7653 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7655 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7660 * Move the job to a different printer or class...
7663 cupsdMoveJob(job
, dprinter
);
7668 * Got the source printer, now look through the jobs...
7671 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
7673 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
7676 * See if the job is pointing at the source printer or has not been
7680 if (strcasecmp(job
->dest
, src
) ||
7681 job
->state_value
> IPP_JOB_STOPPED
)
7685 * See if the job can be moved by the requesting user...
7688 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7692 * Move the job to a different printer or class...
7695 cupsdMoveJob(job
, dprinter
);
7700 * Start jobs if possible...
7706 * Return with "everything is OK" status...
7709 con
->response
->request
.status
.status_code
= IPP_OK
;
7714 * 'ppd_parse_line()' - Parse a PPD default line.
7717 static int /* O - 0 on success, -1 on failure */
7718 ppd_parse_line(const char *line
, /* I - Line */
7719 char *option
, /* O - Option name */
7720 int olen
, /* I - Size of option name */
7721 char *choice
, /* O - Choice name */
7722 int clen
) /* I - Size of choice name */
7725 * Verify this is a default option line...
7728 if (strncmp(line
, "*Default", 8))
7732 * Read the option name...
7735 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
7745 * Skip everything else up to the colon (:)...
7748 while (*line
&& *line
!= ':')
7757 * Now grab the option choice, skipping leading whitespace...
7760 while (isspace(*line
& 255))
7763 for (clen
--; isalnum(*line
& 255); line
++)
7773 * Return with no errors...
7781 * 'print_job()' - Print a file to a printer or class.
7785 print_job(cupsd_client_t
*con
, /* I - Client connection */
7786 ipp_attribute_t
*uri
) /* I - Printer URI */
7788 ipp_attribute_t
*attr
; /* Current attribute */
7789 ipp_attribute_t
*format
; /* Document-format attribute */
7790 const char *default_format
; /* document-format-default value */
7791 cupsd_job_t
*job
; /* New job */
7792 char filename
[1024]; /* Job filename */
7793 mime_type_t
*filetype
; /* Type of file */
7794 char super
[MIME_MAX_SUPER
], /* Supertype of file */
7795 type
[MIME_MAX_TYPE
], /* Subtype of file */
7796 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
7797 /* Textual name of mime type */
7798 cupsd_printer_t
*printer
; /* Printer data */
7799 struct stat fileinfo
; /* File information */
7800 int kbytes
; /* Size of file */
7801 int compression
; /* Document compression */
7804 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
7805 uri
->values
[0].string
.text
);
7808 * Validate print file attributes, for now just document-format and
7809 * compression (CUPS only supports "none" and "gzip")...
7812 compression
= CUPS_FILE_NONE
;
7814 if ((attr
= ippFindAttribute(con
->request
, "compression",
7815 IPP_TAG_KEYWORD
)) != NULL
)
7817 if (strcmp(attr
->values
[0].string
.text
, "none")
7819 && strcmp(attr
->values
[0].string
.text
, "gzip")
7820 #endif /* HAVE_LIBZ */
7823 send_ipp_status(con
, IPP_ATTRIBUTES
,
7824 _("Unsupported compression \"%s\"!"),
7825 attr
->values
[0].string
.text
);
7826 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
7827 "compression", NULL
, attr
->values
[0].string
.text
);
7832 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
7833 compression
= CUPS_FILE_GZIP
;
7834 #endif /* HAVE_LIBZ */
7838 * Do we have a file to print?
7843 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
7848 * Is the destination valid?
7851 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
7857 send_ipp_status(con
, IPP_NOT_FOUND
,
7858 _("The printer or class was not found."));
7863 * Is it a format we support?
7866 if ((format
= ippFindAttribute(con
->request
, "document-format",
7867 IPP_TAG_MIMETYPE
)) != NULL
)
7870 * Grab format from client...
7873 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
7876 send_ipp_status(con
, IPP_BAD_REQUEST
,
7877 _("Could not scan type \"%s\"!"),
7878 format
->values
[0].string
.text
);
7882 else if ((default_format
= cupsGetOption("document-format",
7883 printer
->num_options
,
7884 printer
->options
)) != NULL
)
7887 * Use default document format...
7890 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
7892 send_ipp_status(con
, IPP_BAD_REQUEST
,
7893 _("Could not scan type \"%s\"!"),
7904 strcpy(super
, "application");
7905 strcpy(type
, "octet-stream");
7908 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
7911 * Auto-type the file...
7914 ipp_attribute_t
*doc_name
; /* document-name attribute */
7917 cupsdLogMessage(CUPSD_LOG_DEBUG
, "print_job: auto-typing file...");
7919 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
7920 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
7921 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
7925 filetype
= mimeType(MimeDatabase
, super
, type
);
7928 filetype
= mimeType(MimeDatabase
, super
, type
);
7932 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
7935 * Replace the document-format attribute value with the auto-typed or
7939 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
7944 _cupsStrFree(format
->values
[0].string
.text
);
7946 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
7949 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
7950 "document-format", NULL
, mimetype
);
7954 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
7955 _("Unsupported format \'%s/%s\'!"), super
, type
);
7956 cupsdLogMessage(CUPSD_LOG_INFO
,
7957 "Hint: Do you have the raw file printing rules enabled?");
7960 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
7961 "document-format", NULL
, format
->values
[0].string
.text
);
7967 * Read any embedded job ticket info from PS files...
7970 if (!strcasecmp(filetype
->super
, "application") &&
7971 !strcasecmp(filetype
->type
, "postscript"))
7972 read_ps_job_ticket(con
);
7975 * Create the job object...
7978 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
7981 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Adding job file of type %s/%s.",
7982 job
->id
, filetype
->super
, filetype
->type
);
7985 * Update quota data...
7988 if (stat(con
->filename
, &fileinfo
))
7991 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
7993 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
7995 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
7996 IPP_TAG_INTEGER
)) != NULL
)
7997 attr
->values
[0].integer
+= kbytes
;
8000 * Add the job file...
8003 if (add_file(con
, job
, filetype
, compression
))
8006 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8008 rename(con
->filename
, filename
);
8009 cupsdClearString(&con
->filename
);
8012 * See if we need to add the ending sheet...
8015 if (cupsdTimeoutJob(job
))
8019 * Log and save the job...
8022 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Queued on \"%s\" by \"%s\".",
8023 job
->id
, job
->dest
, job
->username
);
8024 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] hold_until = %d", job
->id
,
8025 (int)job
->hold_until
);
8030 * Start the job if possible...
8038 * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
8040 * This function only gets called when printing a single PostScript
8041 * file using the Print-Job operation. It doesn't work for Create-Job +
8042 * Send-File, since the job attributes need to be set at job creation
8043 * time for banners to work. The embedded PS job ticket stuff is here
8044 * only to allow the Windows printer driver for CUPS to pass in JCL
8045 * options and IPP attributes which otherwise would be lost.
8047 * The format of a PS job ticket is simple:
8049 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8051 * %cupsJobTicket: attr1=value1
8052 * %cupsJobTicket: attr2=value2
8054 * %cupsJobTicket: attrN=valueN
8056 * Job ticket lines must appear immediately after the first line that
8057 * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
8058 * looking for job ticket info when it finds a line that does not begin
8059 * with "%cupsJobTicket:".
8061 * The maximum length of a job ticket line, including the prefix, is
8062 * 255 characters to conform with the Adobe DSC.
8064 * Read-only attributes are rejected with a notice to the error log in
8065 * case a malicious user tries anything. Since the job ticket is read
8066 * prior to attribute validation in print_job(), job ticket attributes
8067 * will go through the same validation as IPP attributes...
8071 read_ps_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8073 cups_file_t
*fp
; /* File to read from */
8074 char line
[256]; /* Line data */
8075 int num_options
; /* Number of options */
8076 cups_option_t
*options
; /* Options */
8077 ipp_t
*ticket
; /* New attributes */
8078 ipp_attribute_t
*attr
, /* Current attribute */
8079 *attr2
, /* Job attribute */
8080 *prev2
; /* Previous job attribute */
8084 * First open the print file...
8087 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8089 cupsdLogMessage(CUPSD_LOG_ERROR
,
8090 "read_ps_job_ticket: Unable to open PostScript print file "
8097 * Skip the first line...
8100 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8102 cupsdLogMessage(CUPSD_LOG_ERROR
,
8103 "read_ps_job_ticket: Unable to read from PostScript print "
8110 if (strncmp(line
, "%!PS-Adobe-", 11))
8113 * Not a DSC-compliant file, so no job ticket info will be available...
8121 * Read job ticket info from the file...
8127 while (cupsFileGets(fp
, line
, sizeof(line
)))
8130 * Stop at the first non-ticket line...
8133 if (strncmp(line
, "%cupsJobTicket:", 15))
8137 * Add the options to the option array...
8140 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8144 * Done with the file; see if we have any options...
8149 if (num_options
== 0)
8153 * OK, convert the options to an attribute list, and apply them to
8158 cupsEncodeOptions(ticket
, num_options
, options
);
8161 * See what the user wants to change.
8164 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8166 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8169 if (!strcmp(attr
->name
, "job-originating-host-name") ||
8170 !strcmp(attr
->name
, "job-originating-user-name") ||
8171 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8172 !strcmp(attr
->name
, "job-k-octets") ||
8173 !strcmp(attr
->name
, "job-id") ||
8174 !strncmp(attr
->name
, "job-state", 9) ||
8175 !strncmp(attr
->name
, "time-at-", 8))
8176 continue; /* Read-only attrs */
8178 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8179 IPP_TAG_ZERO
)) != NULL
)
8182 * Some other value; first free the old value...
8185 if (con
->request
->attrs
== attr2
)
8187 con
->request
->attrs
= attr2
->next
;
8192 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8193 if (prev2
->next
== attr2
)
8195 prev2
->next
= attr2
->next
;
8200 if (con
->request
->last
== attr2
)
8201 con
->request
->last
= prev2
;
8203 _ippFreeAttr(attr2
);
8207 * Add new option by copying it...
8210 copy_attribute(con
->request
, attr
, 0);
8214 * Then free the attribute list and option array...
8218 cupsFreeOptions(num_options
, options
);
8223 * 'reject_jobs()' - Reject print jobs to a printer.
8227 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8228 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8230 http_status_t status
; /* Policy status */
8231 cups_ptype_t dtype
; /* Destination type (printer/class) */
8232 cupsd_printer_t
*printer
; /* Printer data */
8233 ipp_attribute_t
*attr
; /* printer-state-message text */
8236 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8237 con
->http
.fd
, uri
->values
[0].string
.text
);
8240 * Is the destination valid?
8243 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8249 send_ipp_status(con
, IPP_NOT_FOUND
,
8250 _("The printer or class was not found."));
8258 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8260 send_http_error(con
, status
, printer
);
8265 * Reject jobs sent to the printer...
8268 printer
->accepting
= 0;
8270 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8271 IPP_TAG_TEXT
)) == NULL
)
8272 strcpy(printer
->state_message
, "Rejecting Jobs");
8274 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8275 sizeof(printer
->state_message
));
8277 cupsdAddPrinterHistory(printer
);
8279 if (dtype
& CUPS_PRINTER_CLASS
)
8281 cupsdSaveAllClasses();
8283 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8284 printer
->name
, get_username(con
));
8288 cupsdSaveAllPrinters();
8290 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8291 printer
->name
, get_username(con
));
8295 * Everything was ok, so return OK status...
8298 con
->response
->request
.status
.status_code
= IPP_OK
;
8303 * 'release_job()' - Release a held print job.
8307 release_job(cupsd_client_t
*con
, /* I - Client connection */
8308 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8310 ipp_attribute_t
*attr
; /* Current attribute */
8311 int jobid
; /* Job ID */
8312 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8313 username
[HTTP_MAX_URI
], /* Username portion of URI */
8314 host
[HTTP_MAX_URI
], /* Host portion of URI */
8315 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8316 int port
; /* Port portion of URI */
8317 cupsd_job_t
*job
; /* Job information */
8320 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8321 con
->http
.fd
, uri
->values
[0].string
.text
);
8324 * See if we have a job URI or a printer URI...
8327 if (!strcmp(uri
->name
, "printer-uri"))
8330 * Got a printer URI; see if we also have a job-id attribute...
8333 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8334 IPP_TAG_INTEGER
)) == NULL
)
8336 send_ipp_status(con
, IPP_BAD_REQUEST
,
8337 _("Got a printer-uri attribute but no job-id!"));
8341 jobid
= attr
->values
[0].integer
;
8346 * Got a job URI; parse it to get the job ID...
8349 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8350 sizeof(method
), username
, sizeof(username
), host
,
8351 sizeof(host
), &port
, resource
, sizeof(resource
));
8353 if (strncmp(resource
, "/jobs/", 6))
8359 send_ipp_status(con
, IPP_BAD_REQUEST
,
8360 _("Bad job-uri attribute \"%s\"!"),
8361 uri
->values
[0].string
.text
);
8365 jobid
= atoi(resource
+ 6);
8369 * See if the job exists...
8372 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8375 * Nope - return a "not found" error...
8378 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8383 * See if job is "held"...
8386 if (job
->state_value
!= IPP_JOB_HELD
)
8389 * Nope - return a "not possible" error...
8392 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
8397 * See if the job is owned by the requesting user...
8400 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8402 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8407 * Reset the job-hold-until value to "no-hold"...
8410 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8411 IPP_TAG_KEYWORD
)) == NULL
)
8412 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8416 _cupsStrFree(attr
->values
[0].string
.text
);
8418 attr
->value_tag
= IPP_TAG_KEYWORD
;
8419 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
8421 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
8422 "Job job-hold-until value changed by user.");
8426 * Release the job and return...
8429 cupsdReleaseJob(job
);
8431 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
8432 "Job released by user.");
8434 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Released by \"%s\".", jobid
,
8437 con
->response
->request
.status
.status_code
= IPP_OK
;
8442 * 'renew_subscription()' - Renew an existing subscription...
8447 cupsd_client_t
*con
, /* I - Client connection */
8448 int sub_id
) /* I - Subscription ID */
8450 http_status_t status
; /* Policy status */
8451 cupsd_subscription_t
*sub
; /* Subscription */
8452 ipp_attribute_t
*lease
; /* notify-lease-duration */
8455 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8456 "renew_subscription(con=%p[%d], sub_id=%d)",
8457 con
, con
->http
.fd
, sub_id
);
8460 * Is the subscription ID valid?
8463 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8466 * Bad subscription ID...
8469 send_ipp_status(con
, IPP_NOT_FOUND
,
8470 _("notify-subscription-id %d no good!"), sub_id
);
8477 * Job subscriptions cannot be renewed...
8480 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8481 _("Job subscriptions cannot be renewed!"));
8489 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8491 con
, sub
->owner
)) != HTTP_OK
)
8493 send_http_error(con
, status
, sub
->dest
);
8498 * Renew the subscription...
8501 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8504 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8506 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8508 cupsdLogMessage(CUPSD_LOG_INFO
,
8509 "renew_subscription: Limiting notify-lease-duration to "
8512 sub
->lease
= MaxLeaseDuration
;
8515 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
8517 cupsdSaveAllSubscriptions();
8519 con
->response
->request
.status
.status_code
= IPP_OK
;
8521 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
8522 "notify-lease-duration", sub
->lease
);
8527 * 'restart_job()' - Restart an old print job.
8531 restart_job(cupsd_client_t
*con
, /* I - Client connection */
8532 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8534 ipp_attribute_t
*attr
; /* Current attribute */
8535 int jobid
; /* Job ID */
8536 char method
[HTTP_MAX_URI
], /* Method portion of URI */
8537 username
[HTTP_MAX_URI
], /* Username portion of URI */
8538 host
[HTTP_MAX_URI
], /* Host portion of URI */
8539 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8540 int port
; /* Port portion of URI */
8541 cupsd_job_t
*job
; /* Job information */
8544 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
8545 con
->http
.fd
, uri
->values
[0].string
.text
);
8548 * See if we have a job URI or a printer URI...
8551 if (!strcmp(uri
->name
, "printer-uri"))
8554 * Got a printer URI; see if we also have a job-id attribute...
8557 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8558 IPP_TAG_INTEGER
)) == NULL
)
8560 send_ipp_status(con
, IPP_BAD_REQUEST
,
8561 _("Got a printer-uri attribute but no job-id!"));
8565 jobid
= attr
->values
[0].integer
;
8570 * Got a job URI; parse it to get the job ID...
8573 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8574 sizeof(method
), username
, sizeof(username
), host
,
8575 sizeof(host
), &port
, resource
, sizeof(resource
));
8577 if (strncmp(resource
, "/jobs/", 6))
8583 send_ipp_status(con
, IPP_BAD_REQUEST
,
8584 _("Bad job-uri attribute \"%s\"!"),
8585 uri
->values
[0].string
.text
);
8589 jobid
= atoi(resource
+ 6);
8593 * See if the job exists...
8596 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8599 * Nope - return a "not found" error...
8602 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
8607 * See if job is in any of the "completed" states...
8610 if (job
->state_value
<= IPP_JOB_PROCESSING
)
8613 * Nope - return a "not possible" error...
8616 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
8622 * See if we have retained the job files...
8627 if (!job
->attrs
|| job
->num_files
== 0)
8630 * Nope - return a "not possible" error...
8633 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8634 _("Job #%d cannot be restarted - no files!"), jobid
);
8639 * See if the job is owned by the requesting user...
8642 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8644 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8649 * Restart the job and return...
8652 cupsdRestartJob(job
);
8654 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Restarted by \"%s\".", jobid
,
8657 con
->response
->request
.status
.status_code
= IPP_OK
;
8662 * 'save_auth_info()' - Save authentication information for a job.
8667 cupsd_client_t
*con
, /* I - Client connection */
8668 cupsd_job_t
*job
, /* I - Job */
8669 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
8671 int i
; /* Looping var */
8672 char filename
[1024]; /* Job authentication filename */
8673 cups_file_t
*fp
; /* Job authentication file */
8674 char line
[2048]; /* Line for file */
8675 cupsd_printer_t
*dest
; /* Destination printer/class */
8679 * This function saves the in-memory authentication information for
8680 * a job so that it can be used to authenticate with a remote host.
8681 * The information is stored in a file that is readable only by the
8682 * root user. The fields are Base-64 encoded, each on a separate line,
8683 * followed by random number (up to 1024) of newlines to limit the
8684 * amount of information that is exposed.
8686 * Because of the potential for exposing of authentication information,
8687 * this functionality is only enabled when running cupsd as root.
8689 * This caching only works for the Basic and BasicDigest authentication
8690 * types. Digest authentication cannot be cached this way, and in
8691 * the future Kerberos authentication may make all of this obsolete.
8693 * Authentication information is saved whenever an authenticated
8694 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
8697 * This information is deleted after a job is completed or canceled,
8698 * so reprints may require subsequent re-authentication.
8704 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
8708 * Create the authentication file and change permissions...
8711 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
8712 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
8714 cupsdLogMessage(CUPSD_LOG_ERROR
,
8715 "Unable to save authentication info to \"%s\" - %s",
8716 filename
, strerror(errno
));
8720 fchown(cupsFileNumber(fp
), 0, 0);
8721 fchmod(cupsFileNumber(fp
), 0400);
8723 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
8726 * Write 1 to 3 auth values...
8729 cupsdClearString(&job
->auth_username
);
8730 cupsdClearString(&job
->auth_domain
);
8731 cupsdClearString(&job
->auth_password
);
8733 for (i
= 0; i
< auth_info
->num_values
; i
++)
8735 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
8736 strlen(auth_info
->values
[i
].string
.text
));
8737 cupsFilePrintf(fp
, "%s\n", line
);
8739 if (!strcmp(dest
->auth_info_required
[i
], "username"))
8740 cupsdSetStringf(&job
->auth_username
, "CUPSD_AUTH_USERNAME=%s",
8741 auth_info
->values
[i
].string
.text
);
8742 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
8743 cupsdSetStringf(&job
->auth_domain
, "CUPSD_AUTH_DOMAIN=%s",
8744 auth_info
->values
[i
].string
.text
);
8745 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
8746 cupsdSetStringf(&job
->auth_password
, "CUPSD_AUTH_PASSWORD=%s",
8747 auth_info
->values
[i
].string
.text
);
8750 else if (con
->username
[0])
8753 * Write the authenticated username...
8756 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
8757 cupsFilePrintf(fp
, "%s\n", line
);
8759 cupsdSetStringf(&job
->auth_username
, "CUPSD_AUTH_USERNAME=%s", con
->username
);
8760 cupsdClearString(&job
->auth_domain
);
8763 * Write the authenticated password...
8766 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
8767 cupsFilePrintf(fp
, "%s\n", line
);
8769 cupsdSetStringf(&job
->auth_password
, "CUPSD_AUTH_PASSWORD=%s", con
->password
);
8773 * Write a random number of newlines to the end of the file...
8776 for (i
= (rand() % 1024); i
>= 0; i
--)
8777 cupsFilePutChar(fp
, '\n');
8780 * Close the file and return...
8785 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8786 if (con
->gss_have_creds
)
8787 save_krb5_creds(con
, job
);
8788 else if (job
->ccname
)
8789 cupsdClearString(&(job
->ccname
));
8790 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8794 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8796 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
8800 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
8801 cupsd_job_t
*job
) /* I - Job */
8803 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
8804 cupsdLogMessage(CUPSD_LOG_INFO
,
8805 "Sorry, your version of Kerberos does not support delegated "
8810 krb5_error_code error
; /* Kerberos error code */
8811 OM_uint32 major_status
, /* Major status code */
8812 minor_status
; /* Minor status code */
8813 krb5_principal principal
; /* Kerberos principal */
8818 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
8822 if (krb5_init_context
== NULL
)
8824 # endif /* __APPLE__ */
8827 * We MUST create a file-based cache because memory-based caches are
8828 * only valid for the current process/address space.
8830 * Due to various bugs/features in different versions of Kerberos, we
8831 * need either the krb5_cc_new_unique() function or Heimdal's version
8832 * of krb5_cc_gen_new() to create a new FILE: credential cache that
8833 * can be passed to the backend. These functions create a temporary
8834 * file (typically in /tmp) containing the cached credentials, which
8835 * are removed when we have successfully printed a job.
8838 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
8839 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
8840 &(job
->ccache
))) != 0)
8841 # else /* HAVE_HEIMDAL */
8842 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
8843 &(job
->ccache
))) != 0)
8844 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
8846 cupsdLogMessage(CUPSD_LOG_ERROR
,
8847 "Unable to create new credentials cache (%d/%s)",
8848 error
, strerror(errno
));
8853 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
8855 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to parse kerberos username (%d/%s)",
8856 error
, strerror(errno
));
8857 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8862 if ((error
= krb5_cc_initialize(KerberosContext
, job
->ccache
, principal
)))
8864 cupsdLogMessage(CUPSD_LOG_ERROR
,
8865 "Unable to initialize credentials cache (%d/%s)", error
,
8867 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8868 krb5_free_principal(KerberosContext
, principal
);
8873 krb5_free_principal(KerberosContext
, principal
);
8876 * Copy the user's credentials to the new cache file...
8879 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
8882 if (GSS_ERROR(major_status
))
8884 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
8885 "Unable to import client credentials cache");
8886 krb5_cc_destroy(KerberosContext
, job
->ccache
);
8892 * Add the KRB5CCNAME environment variable to the job so that the
8893 * backend can use the credentials when printing.
8896 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
8897 krb5_cc_get_name(KerberosContext
, job
->ccache
));
8899 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] save_krb5_creds: %s", job
->id
,
8901 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
8903 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8907 * 'send_document()' - Send a file to a printer or class.
8911 send_document(cupsd_client_t
*con
, /* I - Client connection */
8912 ipp_attribute_t
*uri
) /* I - Printer URI */
8914 ipp_attribute_t
*attr
; /* Current attribute */
8915 ipp_attribute_t
*format
; /* Document-format attribute */
8916 const char *default_format
;/* document-format-default value */
8917 int jobid
; /* Job ID number */
8918 cupsd_job_t
*job
; /* Current job */
8919 char job_uri
[HTTP_MAX_URI
],
8921 method
[HTTP_MAX_URI
],
8922 /* Method portion of URI */
8923 username
[HTTP_MAX_URI
],
8924 /* Username portion of URI */
8926 /* Host portion of URI */
8927 resource
[HTTP_MAX_URI
];
8928 /* Resource portion of URI */
8929 int port
; /* Port portion of URI */
8930 mime_type_t
*filetype
; /* Type of file */
8931 char super
[MIME_MAX_SUPER
],
8932 /* Supertype of file */
8933 type
[MIME_MAX_TYPE
],
8934 /* Subtype of file */
8935 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8936 /* Textual name of mime type */
8937 char filename
[1024]; /* Job filename */
8938 cupsd_printer_t
*printer
; /* Current printer */
8939 struct stat fileinfo
; /* File information */
8940 int kbytes
; /* Size of file */
8941 int compression
; /* Type of compression */
8944 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
8945 con
->http
.fd
, uri
->values
[0].string
.text
);
8948 * See if we have a job URI or a printer URI...
8951 if (!strcmp(uri
->name
, "printer-uri"))
8954 * Got a printer URI; see if we also have a job-id attribute...
8957 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8958 IPP_TAG_INTEGER
)) == NULL
)
8960 send_ipp_status(con
, IPP_BAD_REQUEST
,
8961 _("Got a printer-uri attribute but no job-id!"));
8965 jobid
= attr
->values
[0].integer
;
8970 * Got a job URI; parse it to get the job ID...
8973 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
8974 sizeof(method
), username
, sizeof(username
), host
,
8975 sizeof(host
), &port
, resource
, sizeof(resource
));
8977 if (strncmp(resource
, "/jobs/", 6))
8983 send_ipp_status(con
, IPP_BAD_REQUEST
,
8984 _("Bad job-uri attribute \"%s\"!"),
8985 uri
->values
[0].string
.text
);
8989 jobid
= atoi(resource
+ 6);
8993 * See if the job exists...
8996 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8999 * Nope - return a "not found" error...
9002 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9006 printer
= cupsdFindDest(job
->dest
);
9009 * See if the job is owned by the requesting user...
9012 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9014 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9019 * OK, see if the client is sending the document compressed - CUPS
9020 * only supports "none" and "gzip".
9023 compression
= CUPS_FILE_NONE
;
9025 if ((attr
= ippFindAttribute(con
->request
, "compression",
9026 IPP_TAG_KEYWORD
)) != NULL
)
9028 if (strcmp(attr
->values
[0].string
.text
, "none")
9030 && strcmp(attr
->values
[0].string
.text
, "gzip")
9031 #endif /* HAVE_LIBZ */
9034 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
9035 attr
->values
[0].string
.text
);
9036 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9037 "compression", NULL
, attr
->values
[0].string
.text
);
9042 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9043 compression
= CUPS_FILE_GZIP
;
9044 #endif /* HAVE_LIBZ */
9048 * Do we have a file to print?
9053 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
9058 * Is it a format we support?
9061 if ((format
= ippFindAttribute(con
->request
, "document-format",
9062 IPP_TAG_MIMETYPE
)) != NULL
)
9065 * Grab format from client...
9068 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
9071 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9072 format
->values
[0].string
.text
);
9076 else if ((default_format
= cupsGetOption("document-format",
9077 printer
->num_options
,
9078 printer
->options
)) != NULL
)
9081 * Use default document format...
9084 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
9086 send_ipp_status(con
, IPP_BAD_REQUEST
,
9087 _("Could not scan type \"%s\"!"),
9095 * No document format attribute? Auto-type it!
9098 strcpy(super
, "application");
9099 strcpy(type
, "octet-stream");
9102 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9105 * Auto-type the file...
9108 ipp_attribute_t
*doc_name
; /* document-name attribute */
9111 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_document: auto-typing file...");
9113 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9114 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9115 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9119 filetype
= mimeType(MimeDatabase
, super
, type
);
9122 filetype
= mimeType(MimeDatabase
, super
, type
);
9126 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
9129 * Replace the document-format attribute value with the auto-typed or
9133 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9138 _cupsStrFree(format
->values
[0].string
.text
);
9140 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
9143 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9144 "document-format", NULL
, mimetype
);
9148 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9149 _("Unsupported format \'%s/%s\'!"), super
, type
);
9150 cupsdLogMessage(CUPSD_LOG_INFO
,
9151 "Hint: Do you have the raw file printing rules enabled?");
9154 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9155 "document-format", NULL
, format
->values
[0].string
.text
);
9160 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9162 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9165 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9166 _("Unsupported format \'%s\'!"), mimetype
);
9168 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9169 "document-format", NULL
, mimetype
);
9174 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9175 "send_document: request file type is %s/%s.",
9176 filetype
->super
, filetype
->type
);
9179 * Add the file to the job...
9184 if (add_file(con
, job
, filetype
, compression
))
9187 if (stat(con
->filename
, &fileinfo
))
9190 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9192 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9194 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
9195 IPP_TAG_INTEGER
)) != NULL
)
9196 attr
->values
[0].integer
+= kbytes
;
9198 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
9200 rename(con
->filename
, filename
);
9202 cupsdClearString(&con
->filename
);
9204 cupsdLogMessage(CUPSD_LOG_INFO
,
9205 "File of type %s/%s queued in job #%d by \"%s\".",
9206 filetype
->super
, filetype
->type
, job
->id
, job
->username
);
9209 * Start the job if this is the last document...
9212 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9213 IPP_TAG_BOOLEAN
)) != NULL
&&
9214 attr
->values
[0].boolean
)
9217 * See if we need to add the ending sheet...
9220 if (cupsdTimeoutJob(job
))
9223 if (job
->state_value
== IPP_JOB_STOPPED
)
9225 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9226 job
->state_value
= IPP_JOB_PENDING
;
9228 else if (job
->state_value
== IPP_JOB_HELD
)
9230 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9231 IPP_TAG_KEYWORD
)) == NULL
)
9232 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9234 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9236 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9237 job
->state_value
= IPP_JOB_PENDING
;
9244 * Start the job if possible... Since cupsdCheckJobs() can cancel a
9245 * job if it doesn't print, we need to re-find the job afterwards...
9252 job
= cupsdFindJob(jobid
);
9256 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9257 IPP_TAG_KEYWORD
)) == NULL
)
9258 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9260 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9262 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9263 job
->state_value
= IPP_JOB_HELD
;
9264 job
->hold_until
= time(NULL
) + 60;
9270 * Fill in the response info...
9273 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
9276 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9279 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9281 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9282 job
? job
->state_value
: IPP_JOB_CANCELED
);
9283 add_job_state_reasons(con
, job
);
9285 con
->response
->request
.status
.status_code
= IPP_OK
;
9290 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9295 cupsd_client_t
*con
, /* I - Client connection */
9296 http_status_t status
, /* I - HTTP status code */
9297 cupsd_printer_t
*printer
) /* I - Printer, if any */
9299 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: %s",
9300 ippOpString(con
->request
->request
.op
.operation_id
),
9301 httpStatus(status
));
9303 if (status
== HTTP_UNAUTHORIZED
&&
9304 printer
&& printer
->num_auth_info_required
> 0 &&
9305 !strcmp(printer
->auth_info_required
[0], "negotiate"))
9306 cupsdSendError(con
, status
, CUPSD_AUTH_NEGOTIATE
);
9309 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9310 cupsd_location_t
*auth
; /* Pointer to authentication element */
9313 if (printer
->type
& CUPS_PRINTER_CLASS
)
9314 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9316 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9318 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9319 auth
->type
== CUPSD_AUTH_NONE
)
9320 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
, IPP_PRINT_JOB
);
9322 cupsdSendError(con
, status
, auth
? auth
->type
: CUPSD_AUTH_NONE
);
9325 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
9327 ippDelete(con
->response
);
9328 con
->response
= NULL
;
9335 * 'send_ipp_status()' - Send a status back to the IPP client.
9339 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9340 ipp_status_t status
, /* I - IPP status code */
9341 const char *message
, /* I - Status message */
9342 ...) /* I - Additional args as needed */
9344 va_list ap
; /* Pointer to additional args */
9345 char formatted
[1024]; /* Formatted errror message */
9348 va_start(ap
, message
);
9349 vsnprintf(formatted
, sizeof(formatted
),
9350 _cupsLangString(con
->language
, message
), ap
);
9353 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9354 ippOpString(con
->request
->request
.op
.operation_id
),
9355 ippErrorString(status
), formatted
);
9357 con
->response
->request
.status
.status_code
= status
;
9359 if (ippFindAttribute(con
->response
, "attributes-charset",
9360 IPP_TAG_ZERO
) == NULL
)
9361 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9362 "attributes-charset", NULL
, DefaultCharset
);
9364 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9365 IPP_TAG_ZERO
) == NULL
)
9366 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9367 "attributes-natural-language", NULL
, DefaultLanguage
);
9369 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9370 "status-message", NULL
, formatted
);
9375 * 'set_default()' - Set the default destination...
9379 set_default(cupsd_client_t
*con
, /* I - Client connection */
9380 ipp_attribute_t
*uri
) /* I - Printer URI */
9382 http_status_t status
; /* Policy status */
9383 cups_ptype_t dtype
; /* Destination type (printer/class) */
9384 cupsd_printer_t
*printer
; /* Printer */
9387 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9388 con
->http
.fd
, uri
->values
[0].string
.text
);
9391 * Is the destination valid?
9394 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9400 send_ipp_status(con
, IPP_NOT_FOUND
,
9401 _("The printer or class was not found."));
9409 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9411 send_http_error(con
, status
, NULL
);
9416 * Set it as the default...
9419 DefaultPrinter
= printer
;
9421 cupsdSaveAllPrinters();
9422 cupsdSaveAllClasses();
9424 cupsdWritePrintcap();
9426 cupsdLogMessage(CUPSD_LOG_INFO
,
9427 "Default destination set to \"%s\" by \"%s\".",
9428 printer
->name
, get_username(con
));
9431 * Everything was ok, so return OK status...
9434 con
->response
->request
.status
.status_code
= IPP_OK
;
9439 * 'set_job_attrs()' - Set job attributes.
9443 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9444 ipp_attribute_t
*uri
) /* I - Job URI */
9446 ipp_attribute_t
*attr
, /* Current attribute */
9447 *attr2
; /* Job attribute */
9448 int jobid
; /* Job ID */
9449 cupsd_job_t
*job
; /* Current job */
9450 char method
[HTTP_MAX_URI
],
9451 /* Method portion of URI */
9452 username
[HTTP_MAX_URI
],
9453 /* Username portion of URI */
9455 /* Host portion of URI */
9456 resource
[HTTP_MAX_URI
];
9457 /* Resource portion of URI */
9458 int port
; /* Port portion of URI */
9459 int event
; /* Events? */
9462 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
9463 con
->http
.fd
, uri
->values
[0].string
.text
);
9466 * Start with "everything is OK" status...
9469 con
->response
->request
.status
.status_code
= IPP_OK
;
9472 * See if we have a job URI or a printer URI...
9475 if (!strcmp(uri
->name
, "printer-uri"))
9478 * Got a printer URI; see if we also have a job-id attribute...
9481 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9482 IPP_TAG_INTEGER
)) == NULL
)
9484 send_ipp_status(con
, IPP_BAD_REQUEST
,
9485 _("Got a printer-uri attribute but no job-id!"));
9489 jobid
= attr
->values
[0].integer
;
9494 * Got a job URI; parse it to get the job ID...
9497 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, method
,
9498 sizeof(method
), username
, sizeof(username
), host
,
9499 sizeof(host
), &port
, resource
, sizeof(resource
));
9501 if (strncmp(resource
, "/jobs/", 6))
9507 send_ipp_status(con
, IPP_BAD_REQUEST
,
9508 _("Bad job-uri attribute \"%s\"!"),
9509 uri
->values
[0].string
.text
);
9513 jobid
= atoi(resource
+ 6);
9517 * See if the job exists...
9520 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9523 * Nope - return a "not found" error...
9526 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9531 * See if the job has been completed...
9534 if (job
->state_value
> IPP_JOB_STOPPED
)
9537 * Return a "not-possible" error...
9540 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9541 _("Job #%d is finished and cannot be altered!"), jobid
);
9546 * See if the job is owned by the requesting user...
9549 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9551 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9556 * See what the user wants to change.
9563 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9565 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
9568 if (!strcmp(attr
->name
, "attributes-charset") ||
9569 !strcmp(attr
->name
, "attributes-natural-language") ||
9570 !strcmp(attr
->name
, "document-compression") ||
9571 !strcmp(attr
->name
, "document-format") ||
9572 !strcmp(attr
->name
, "job-detailed-status-messages") ||
9573 !strcmp(attr
->name
, "job-document-access-errors") ||
9574 !strcmp(attr
->name
, "job-id") ||
9575 !strcmp(attr
->name
, "job-k-octets") ||
9576 !strcmp(attr
->name
, "job-originating-host-name") ||
9577 !strcmp(attr
->name
, "job-originating-user-name") ||
9578 !strcmp(attr
->name
, "job-printer-up-time") ||
9579 !strcmp(attr
->name
, "job-printer-uri") ||
9580 !strcmp(attr
->name
, "job-sheets") ||
9581 !strcmp(attr
->name
, "job-state-message") ||
9582 !strcmp(attr
->name
, "job-state-reasons") ||
9583 !strcmp(attr
->name
, "job-uri") ||
9584 !strcmp(attr
->name
, "number-of-documents") ||
9585 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
9586 !strcmp(attr
->name
, "output-device-assigned") ||
9587 !strncmp(attr
->name
, "date-time-at-", 13) ||
9588 !strncmp(attr
->name
, "job-impressions", 15) ||
9589 !strncmp(attr
->name
, "job-k-octets", 12) ||
9590 !strncmp(attr
->name
, "job-media-sheets", 16) ||
9591 !strncmp(attr
->name
, "time-at-", 8))
9597 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
9598 _("%s cannot be changed."), attr
->name
);
9600 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9601 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9606 if (!strcmp(attr
->name
, "job-priority"))
9609 * Change the job priority...
9612 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9614 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
9616 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9617 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9619 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
9621 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9622 _("Job is completed and cannot be changed."));
9625 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9627 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
9628 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
9629 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
9632 else if (!strcmp(attr
->name
, "job-state"))
9635 * Change the job state...
9638 if (attr
->value_tag
!= IPP_TAG_ENUM
)
9640 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
9642 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
9643 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
9647 switch (attr
->values
[0].integer
)
9649 case IPP_JOB_PENDING
:
9651 if (job
->state_value
> IPP_JOB_HELD
)
9653 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9654 _("Job state cannot be changed."));
9657 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9659 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
9660 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
9662 event
|= CUPSD_EVENT_JOB_STATE
;
9666 case IPP_JOB_PROCESSING
:
9667 case IPP_JOB_STOPPED
:
9668 if (job
->state_value
!= attr
->values
[0].integer
)
9670 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9671 _("Job state cannot be changed."));
9676 case IPP_JOB_CANCELED
:
9677 case IPP_JOB_ABORTED
:
9678 case IPP_JOB_COMPLETED
:
9679 if (job
->state_value
> IPP_JOB_PROCESSING
)
9681 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9682 _("Job state cannot be changed."));
9685 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9686 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
9691 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
9693 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9694 IPP_TAG_ZERO
)) != NULL
)
9697 * Some other value; first free the old value...
9700 if (job
->attrs
->prev
)
9701 job
->attrs
->prev
->next
= attr2
->next
;
9703 job
->attrs
->attrs
= attr2
->next
;
9705 if (job
->attrs
->last
== attr2
)
9706 job
->attrs
->last
= job
->attrs
->prev
;
9708 _ippFreeAttr(attr2
);
9711 * Then copy the attribute...
9714 copy_attribute(job
->attrs
, attr
, 0);
9717 * See if the job-name or job-hold-until is being changed.
9720 if (!strcmp(attr
->name
, "job-hold-until"))
9722 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
9724 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
9725 cupsdReleaseJob(job
);
9729 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
9732 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
9735 * Delete the attribute...
9738 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
9739 IPP_TAG_ZERO
)) != NULL
)
9741 if (job
->attrs
->prev
)
9742 job
->attrs
->prev
->next
= attr2
->next
;
9744 job
->attrs
->attrs
= attr2
->next
;
9746 if (attr2
== job
->attrs
->last
)
9747 job
->attrs
->last
= job
->attrs
->prev
;
9749 _ippFreeAttr(attr2
);
9751 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9757 * Add new option by copying it...
9760 copy_attribute(job
->attrs
, attr
, 0);
9762 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
9773 * Send events as needed...
9776 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
9777 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
, job
->printer
, job
,
9778 "Job priority changed by user.");
9780 if (event
& CUPSD_EVENT_JOB_STATE
)
9781 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
9782 job
->state_value
== IPP_JOB_HELD
?
9783 "Job held by user." : "Job restarted by user.");
9785 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
9786 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, job
->printer
, job
,
9787 "Job options changed by user.");
9790 * Start jobs if possible...
9798 * 'set_printer_defaults()' - Set printer default options from a request.
9802 set_printer_defaults(
9803 cupsd_client_t
*con
, /* I - Client connection */
9804 cupsd_printer_t
*printer
) /* I - Printer */
9806 int i
; /* Looping var */
9807 ipp_attribute_t
*attr
; /* Current attribute */
9808 int namelen
; /* Length of attribute name */
9809 char name
[256], /* New attribute name */
9810 value
[256]; /* String version of integer attrs */
9813 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9816 * Skip non-printer attributes...
9819 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
9822 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
9824 if (!strcmp(attr
->name
, "job-sheets-default"))
9827 * Only allow keywords and names...
9830 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9834 * Only allow job-sheets-default to be set when running without a
9835 * system high classification level...
9841 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
9843 if (attr
->num_values
> 1)
9844 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
9846 cupsdSetString(&printer
->job_sheets
[1], "none");
9848 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
9850 cupsdFreePrinterUsers(printer
);
9852 printer
->deny_users
= 0;
9854 if (attr
->value_tag
== IPP_TAG_NAME
&&
9855 (attr
->num_values
> 1 ||
9856 strcmp(attr
->values
[0].string
.text
, "all")))
9858 for (i
= 0; i
< attr
->num_values
; i
++)
9859 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9862 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
9864 cupsdFreePrinterUsers(printer
);
9866 printer
->deny_users
= 1;
9868 if (attr
->value_tag
== IPP_TAG_NAME
&&
9869 (attr
->num_values
> 1 ||
9870 strcmp(attr
->values
[0].string
.text
, "none")))
9872 for (i
= 0; i
< attr
->num_values
; i
++)
9873 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
9876 else if (!strcmp(attr
->name
, "job-quota-period"))
9878 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9881 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
9882 attr
->values
[0].integer
);
9883 cupsdFreeQuotas(printer
);
9885 printer
->quota_period
= attr
->values
[0].integer
;
9887 else if (!strcmp(attr
->name
, "job-k-limit"))
9889 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9892 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
9893 attr
->values
[0].integer
);
9894 cupsdFreeQuotas(printer
);
9896 printer
->k_limit
= attr
->values
[0].integer
;
9898 else if (!strcmp(attr
->name
, "job-page-limit"))
9900 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9903 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
9904 attr
->values
[0].integer
);
9905 cupsdFreeQuotas(printer
);
9907 printer
->page_limit
= attr
->values
[0].integer
;
9909 else if (!strcmp(attr
->name
, "printer-op-policy"))
9911 cupsd_policy_t
*p
; /* Policy */
9914 if (attr
->value_tag
!= IPP_TAG_NAME
)
9917 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
9919 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9920 "Setting printer-op-policy to \"%s\"...",
9921 attr
->values
[0].string
.text
);
9922 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
9923 printer
->op_policy_ptr
= p
;
9927 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9928 _("Unknown printer-op-policy \"%s\"."),
9929 attr
->values
[0].string
.text
);
9933 else if (!strcmp(attr
->name
, "printer-error-policy"))
9935 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
9938 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
9939 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
9940 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
9942 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9943 _("Unknown printer-error-policy \"%s\"."),
9944 attr
->values
[0].string
.text
);
9948 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9949 "Setting printer-error-policy to \"%s\"...",
9950 attr
->values
[0].string
.text
);
9951 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
9955 * Skip any other non-default attributes...
9958 namelen
= strlen(attr
->name
);
9959 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
9960 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
9964 * OK, anything else must be a user-defined default...
9967 strlcpy(name
, attr
->name
, sizeof(name
));
9968 name
[namelen
- 8] = '\0'; /* Strip "-default" */
9970 switch (attr
->value_tag
)
9972 case IPP_TAG_DELETEATTR
:
9973 printer
->num_options
= cupsRemoveOption(name
,
9974 printer
->num_options
,
9975 &(printer
->options
));
9976 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9977 "Deleting %s", attr
->name
);
9981 case IPP_TAG_KEYWORD
:
9983 printer
->num_options
= cupsAddOption(name
,
9984 attr
->values
[0].string
.text
,
9985 printer
->num_options
,
9986 &(printer
->options
));
9987 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9988 "Setting %s to \"%s\"...", attr
->name
,
9989 attr
->values
[0].string
.text
);
9992 case IPP_TAG_BOOLEAN
:
9993 printer
->num_options
= cupsAddOption(name
,
9994 attr
->values
[0].boolean
?
9996 printer
->num_options
,
9997 &(printer
->options
));
9998 cupsdLogMessage(CUPSD_LOG_DEBUG
,
9999 "Setting %s to %s...", attr
->name
,
10000 attr
->values
[0].boolean
? "true" : "false");
10003 case IPP_TAG_INTEGER
:
10004 case IPP_TAG_ENUM
:
10005 sprintf(value
, "%d", attr
->values
[0].integer
);
10006 printer
->num_options
= cupsAddOption(name
, value
,
10007 printer
->num_options
,
10008 &(printer
->options
));
10009 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10010 "Setting %s to %s...", attr
->name
, value
);
10013 case IPP_TAG_RANGE
:
10014 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10015 attr
->values
[0].range
.upper
);
10016 printer
->num_options
= cupsAddOption(name
, value
,
10017 printer
->num_options
,
10018 &(printer
->options
));
10019 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10020 "Setting %s to %s...", attr
->name
, value
);
10023 case IPP_TAG_RESOLUTION
:
10024 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10025 attr
->values
[0].resolution
.yres
,
10026 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10028 printer
->num_options
= cupsAddOption(name
, value
,
10029 printer
->num_options
,
10030 &(printer
->options
));
10031 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10032 "Setting %s to %s...", attr
->name
, value
);
10036 /* Do nothing for other values */
10044 * 'start_printer()' - Start a printer.
10048 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10049 ipp_attribute_t
*uri
) /* I - Printer URI */
10051 http_status_t status
; /* Policy status */
10052 cups_ptype_t dtype
; /* Destination type (printer/class) */
10053 cupsd_printer_t
*printer
; /* Printer data */
10056 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10057 con
->http
.fd
, uri
->values
[0].string
.text
);
10060 * Is the destination valid?
10063 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10069 send_ipp_status(con
, IPP_NOT_FOUND
,
10070 _("The printer or class was not found."));
10078 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10080 send_http_error(con
, status
, printer
);
10085 * Start the printer...
10088 printer
->state_message
[0] = '\0';
10090 cupsdStartPrinter(printer
, 1);
10092 if (dtype
& CUPS_PRINTER_CLASS
)
10093 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10094 printer
->name
, get_username(con
));
10096 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10097 printer
->name
, get_username(con
));
10102 * Everything was ok, so return OK status...
10105 con
->response
->request
.status
.status_code
= IPP_OK
;
10110 * 'stop_printer()' - Stop a printer.
10114 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10115 ipp_attribute_t
*uri
) /* I - Printer URI */
10117 http_status_t status
; /* Policy status */
10118 cups_ptype_t dtype
; /* Destination type (printer/class) */
10119 cupsd_printer_t
*printer
; /* Printer data */
10120 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10123 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10124 con
->http
.fd
, uri
->values
[0].string
.text
);
10127 * Is the destination valid?
10130 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10136 send_ipp_status(con
, IPP_NOT_FOUND
,
10137 _("The printer or class was not found."));
10145 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10147 send_http_error(con
, status
, printer
);
10152 * Stop the printer...
10155 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10156 IPP_TAG_TEXT
)) == NULL
)
10157 strcpy(printer
->state_message
, "Paused");
10160 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10161 sizeof(printer
->state_message
));
10164 cupsdStopPrinter(printer
, 1);
10166 if (dtype
& CUPS_PRINTER_CLASS
)
10167 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10168 printer
->name
, get_username(con
));
10170 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10171 printer
->name
, get_username(con
));
10174 * Everything was ok, so return OK status...
10177 con
->response
->request
.status
.status_code
= IPP_OK
;
10182 * 'url_encode_attr()' - URL-encode a string attribute.
10186 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10187 char *buffer
,/* I - String buffer */
10188 int bufsize
)/* I - Size of buffer */
10190 int i
; /* Looping var */
10191 char *bufptr
, /* Pointer into buffer */
10192 *bufend
; /* End of buffer */
10195 strlcpy(buffer
, attr
->name
, bufsize
);
10196 bufptr
= buffer
+ strlen(buffer
);
10197 bufend
= buffer
+ bufsize
- 1;
10199 for (i
= 0; i
< attr
->num_values
; i
++)
10201 if (bufptr
>= bufend
)
10209 if (bufptr
>= bufend
)
10214 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
10215 bufptr
, bufend
- bufptr
+ 1);
10217 if (bufptr
>= bufend
)
10228 * 'url_encode_string()' - URL-encode a string.
10231 static char * /* O - End of string */
10232 url_encode_string(const char *s
, /* I - String */
10233 char *buffer
, /* I - String buffer */
10234 int bufsize
) /* I - Size of buffer */
10236 char *bufptr
, /* Pointer into buffer */
10237 *bufend
; /* End of buffer */
10238 static const char *hex
= "0123456789ABCDEF";
10243 bufend
= buffer
+ bufsize
- 1;
10245 while (*s
&& bufptr
< bufend
)
10247 if (*s
== ' ' || *s
== '%')
10249 if (bufptr
>= (bufend
- 2))
10253 *bufptr
++ = hex
[(*s
>> 4) & 15];
10254 *bufptr
++ = hex
[*s
& 15];
10258 else if (*s
== '\'' || *s
== '\\')
10260 if (bufptr
>= (bufend
- 1))
10277 * 'user_allowed()' - See if a user is allowed to print to a queue.
10280 static int /* O - 0 if not allowed, 1 if allowed */
10281 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
10282 const char *username
) /* I - Username */
10284 int i
; /* Looping var */
10285 struct passwd
*pw
; /* User password data */
10286 char baseuser
[256], /* Base username */
10287 *baseptr
; /* Pointer to "@" in base username */
10290 if (p
->num_users
== 0)
10293 if (!strcmp(username
, "root"))
10296 if (strchr(username
, '@'))
10299 * Strip @REALM for username check...
10302 strlcpy(baseuser
, username
, sizeof(baseuser
));
10304 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
10307 username
= baseuser
;
10310 pw
= getpwnam(username
);
10313 for (i
= 0; i
< p
->num_users
; i
++)
10315 if (p
->users
[i
][0] == '@')
10318 * Check group membership...
10321 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
10324 else if (!strcasecmp(username
, p
->users
[i
]))
10328 return ((i
< p
->num_users
) != p
->deny_users
);
10333 * 'validate_job()' - Validate printer options and destination.
10337 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10338 ipp_attribute_t
*uri
) /* I - Printer URI */
10340 http_status_t status
; /* Policy status */
10341 ipp_attribute_t
*attr
; /* Current attribute */
10342 ipp_attribute_t
*format
; /* Document-format attribute */
10343 cups_ptype_t dtype
; /* Destination type (printer/class) */
10344 char super
[MIME_MAX_SUPER
],
10345 /* Supertype of file */
10346 type
[MIME_MAX_TYPE
];
10347 /* Subtype of file */
10348 cupsd_printer_t
*printer
; /* Printer */
10351 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10352 con
->http
.fd
, uri
->values
[0].string
.text
);
10355 * OK, see if the client is sending the document compressed - CUPS
10356 * doesn't support compression yet...
10359 if ((attr
= ippFindAttribute(con
->request
, "compression",
10360 IPP_TAG_KEYWORD
)) != NULL
&&
10361 !strcmp(attr
->values
[0].string
.text
, "none"))
10363 send_ipp_status(con
, IPP_ATTRIBUTES
,
10364 _("Unsupported compression attribute %s!"),
10365 attr
->values
[0].string
.text
);
10366 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10367 "compression", NULL
, attr
->values
[0].string
.text
);
10372 * Is it a format we support?
10375 if ((format
= ippFindAttribute(con
->request
, "document-format",
10376 IPP_TAG_MIMETYPE
)) != NULL
)
10378 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
10381 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
10382 format
->values
[0].string
.text
);
10386 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10387 !mimeType(MimeDatabase
, super
, type
))
10389 cupsdLogMessage(CUPSD_LOG_INFO
,
10390 "Hint: Do you have the raw file printing rules enabled?");
10391 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10392 _("Unsupported format \"%s\"!"),
10393 format
->values
[0].string
.text
);
10394 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10395 "document-format", NULL
, format
->values
[0].string
.text
);
10401 * Is the destination valid?
10404 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10410 send_ipp_status(con
, IPP_NOT_FOUND
,
10411 _("The printer or class was not found."));
10419 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10421 send_http_error(con
, status
, printer
);
10426 * Everything was ok, so return OK status...
10429 con
->response
->request
.status
.status_code
= IPP_OK
;
10434 * 'validate_name()' - Make sure the printer name only contains valid chars.
10437 static int /* O - 0 if name is no good, 1 if good */
10438 validate_name(const char *name
) /* I - Name to check */
10440 const char *ptr
; /* Pointer into name */
10444 * Scan the whole name...
10447 for (ptr
= name
; *ptr
; ptr
++)
10448 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
10452 * All the characters are good; validate the length, too...
10455 return ((ptr
- name
) < 128);
10460 * 'validate_user()' - Validate the user for the request.
10463 static int /* O - 1 if permitted, 0 otherwise */
10464 validate_user(cupsd_job_t
*job
, /* I - Job */
10465 cupsd_client_t
*con
, /* I - Client connection */
10466 const char *owner
, /* I - Owner of job/resource */
10467 char *username
, /* O - Authenticated username */
10468 int userlen
) /* I - Length of username */
10470 cupsd_printer_t
*printer
; /* Printer for job */
10473 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
10474 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
10476 job
->id
, con
? con
->http
.fd
: 0,
10477 owner
? owner
: "(null)", username
, userlen
);
10480 * Validate input...
10483 if (!con
|| !owner
|| !username
|| userlen
<= 0)
10487 * Get the best authenticated username that is available.
10490 strlcpy(username
, get_username(con
), userlen
);
10493 * Check the username against the owner...
10496 printer
= cupsdFindDest(job
->dest
);
10498 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
10499 con
, owner
) == HTTP_OK
);
10504 * End of "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $".