]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/ipp.c
fc61141285bb685078b12f695115df5796d0207c
[thirdparty/cups.git] / scheduler / ipp.c
1 /*
2 * "$Id$"
3 *
4 * IPP routines for the CUPS scheduler.
5 *
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
9 * This file contains Kerberos support code, copyright 2006 by
10 * Jelmer Vernooij.
11 *
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/".
17 */
18
19 /*
20 * Include necessary headers...
21 */
22
23 #include "cupsd.h"
24 #include <cups/ppd-private.h>
25
26 #ifdef __APPLE__
27 /*# include <ApplicationServices/ApplicationServices.h>
28 extern CFUUIDRef ColorSyncCreateUUIDFromUInt32(unsigned id);
29 # include <CoreFoundation/CoreFoundation.h>*/
30 # ifdef HAVE_MEMBERSHIP_H
31 # include <membership.h>
32 # endif /* HAVE_MEMBERSHIP_H */
33 # ifdef HAVE_MEMBERSHIPPRIV_H
34 # include <membershipPriv.h>
35 # else
36 extern int mbr_user_name_to_uuid(const char* name, uuid_t uu);
37 extern int mbr_group_name_to_uuid(const char* name, uuid_t uu);
38 extern int mbr_check_membership_by_id(uuid_t user, gid_t group, int* ismember);
39 # endif /* HAVE_MEMBERSHIPPRIV_H */
40 #endif /* __APPLE__ */
41
42
43 /*
44 * Local functions...
45 */
46
47 static void accept_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
48 static void add_class(cupsd_client_t *con, ipp_attribute_t *uri);
49 static int add_file(cupsd_client_t *con, cupsd_job_t *job,
50 mime_type_t *filetype, int compression);
51 static cupsd_job_t *add_job(cupsd_client_t *con, cupsd_printer_t *printer,
52 mime_type_t *filetype);
53 static void add_job_subscriptions(cupsd_client_t *con, cupsd_job_t *job);
54 static void add_job_uuid(cupsd_job_t *job);
55 static void add_printer(cupsd_client_t *con, ipp_attribute_t *uri);
56 static void add_printer_state_reasons(cupsd_client_t *con,
57 cupsd_printer_t *p);
58 static void add_queued_job_count(cupsd_client_t *con, cupsd_printer_t *p);
59 static void apply_printer_defaults(cupsd_printer_t *printer,
60 cupsd_job_t *job);
61 static void authenticate_job(cupsd_client_t *con, ipp_attribute_t *uri);
62 static void cancel_all_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
63 static void cancel_job(cupsd_client_t *con, ipp_attribute_t *uri);
64 static void cancel_subscription(cupsd_client_t *con, int id);
65 static int check_rss_recipient(const char *recipient);
66 static int check_quotas(cupsd_client_t *con, cupsd_printer_t *p);
67 static void close_job(cupsd_client_t *con, ipp_attribute_t *uri);
68 static void copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra,
69 ipp_tag_t group, int quickcopy,
70 cups_array_t *exclude);
71 static int copy_banner(cupsd_client_t *con, cupsd_job_t *job,
72 const char *name);
73 static int copy_file(const char *from, const char *to);
74 static int copy_model(cupsd_client_t *con, const char *from,
75 const char *to);
76 static void copy_job_attrs(cupsd_client_t *con,
77 cupsd_job_t *job,
78 cups_array_t *ra, cups_array_t *exclude);
79 static void copy_printer_attrs(cupsd_client_t *con,
80 cupsd_printer_t *printer,
81 cups_array_t *ra);
82 static void copy_subscription_attrs(cupsd_client_t *con,
83 cupsd_subscription_t *sub,
84 cups_array_t *ra,
85 cups_array_t *exclude);
86 static void create_job(cupsd_client_t *con, ipp_attribute_t *uri);
87 static cups_array_t *create_requested_array(ipp_t *request);
88 static void create_subscription(cupsd_client_t *con, ipp_attribute_t *uri);
89 static void delete_printer(cupsd_client_t *con, ipp_attribute_t *uri);
90 static void get_default(cupsd_client_t *con);
91 static void get_devices(cupsd_client_t *con);
92 static void get_document(cupsd_client_t *con, ipp_attribute_t *uri);
93 static void get_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
94 static void get_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
95 static void get_notifications(cupsd_client_t *con);
96 static void get_ppd(cupsd_client_t *con, ipp_attribute_t *uri);
97 static void get_ppds(cupsd_client_t *con);
98 static void get_printers(cupsd_client_t *con, int type);
99 static void get_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
100 static void get_printer_supported(cupsd_client_t *con, ipp_attribute_t *uri);
101 static void get_subscription_attrs(cupsd_client_t *con, int sub_id);
102 static void get_subscriptions(cupsd_client_t *con, ipp_attribute_t *uri);
103 static const char *get_username(cupsd_client_t *con);
104 static void hold_job(cupsd_client_t *con, ipp_attribute_t *uri);
105 static void hold_new_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
106 static void move_job(cupsd_client_t *con, ipp_attribute_t *uri);
107 static int ppd_parse_line(const char *line, char *option, int olen,
108 char *choice, int clen);
109 static void print_job(cupsd_client_t *con, ipp_attribute_t *uri);
110 static void read_job_ticket(cupsd_client_t *con);
111 static void reject_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
112 static void release_held_new_jobs(cupsd_client_t *con,
113 ipp_attribute_t *uri);
114 static void release_job(cupsd_client_t *con, ipp_attribute_t *uri);
115 static void renew_subscription(cupsd_client_t *con, int sub_id);
116 static void restart_job(cupsd_client_t *con, ipp_attribute_t *uri);
117 static void save_auth_info(cupsd_client_t *con, cupsd_job_t *job,
118 ipp_attribute_t *auth_info);
119 static void send_document(cupsd_client_t *con, ipp_attribute_t *uri);
120 static void send_http_error(cupsd_client_t *con, http_status_t status,
121 cupsd_printer_t *printer);
122 static void send_ipp_status(cupsd_client_t *con, ipp_status_t status,
123 const char *message, ...)
124 __attribute__((__format__(__printf__, 3, 4)));
125 static void set_default(cupsd_client_t *con, ipp_attribute_t *uri);
126 static void set_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
127 static void set_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
128 static void set_printer_defaults(cupsd_client_t *con,
129 cupsd_printer_t *printer);
130 static void start_printer(cupsd_client_t *con, ipp_attribute_t *uri);
131 static void stop_printer(cupsd_client_t *con, ipp_attribute_t *uri);
132 static void url_encode_attr(ipp_attribute_t *attr, char *buffer,
133 int bufsize);
134 static char *url_encode_string(const char *s, char *buffer, int bufsize);
135 static int user_allowed(cupsd_printer_t *p, const char *username);
136 static void validate_job(cupsd_client_t *con, ipp_attribute_t *uri);
137 static int validate_name(const char *name);
138 static int validate_user(cupsd_job_t *job, cupsd_client_t *con,
139 const char *owner, char *username,
140 int userlen);
141
142
143 /*
144 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
145 */
146
147 int /* O - 1 on success, 0 on failure */
148 cupsdProcessIPPRequest(
149 cupsd_client_t *con) /* I - Client connection */
150 {
151 ipp_tag_t group; /* Current group tag */
152 ipp_attribute_t *attr; /* Current attribute */
153 ipp_attribute_t *charset; /* Character set attribute */
154 ipp_attribute_t *language; /* Language attribute */
155 ipp_attribute_t *uri = NULL; /* Printer or job URI attribute */
156 ipp_attribute_t *username; /* requesting-user-name attr */
157 int sub_id; /* Subscription ID */
158
159
160 cupsdLogMessage(CUPSD_LOG_DEBUG2,
161 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
162 con, con->number, con->request->request.op.operation_id);
163
164 /*
165 * First build an empty response message for this request...
166 */
167
168 con->response = ippNew();
169
170 con->response->request.status.version[0] =
171 con->request->request.op.version[0];
172 con->response->request.status.version[1] =
173 con->request->request.op.version[1];
174 con->response->request.status.request_id =
175 con->request->request.op.request_id;
176
177 /*
178 * Then validate the request header and required attributes...
179 */
180
181 if (con->request->request.any.version[0] != 1 &&
182 con->request->request.any.version[0] != 2)
183 {
184 /*
185 * Return an error, since we only support IPP 1.x and 2.x.
186 */
187
188 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
189 "%04X %s Bad request version number %d.%d",
190 IPP_VERSION_NOT_SUPPORTED, con->http->hostname,
191 con->request->request.any.version[0],
192 con->request->request.any.version[1]);
193
194 send_ipp_status(con, IPP_VERSION_NOT_SUPPORTED,
195 _("Bad request version number %d.%d."),
196 con->request->request.any.version[0],
197 con->request->request.any.version[1]);
198 }
199 else if (con->request->request.any.request_id < 1)
200 {
201 /*
202 * Return an error, since request IDs must be between 1 and 2^31-1
203 */
204
205 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
206 "%04X %s Bad request ID %d",
207 IPP_BAD_REQUEST, con->http->hostname,
208 con->request->request.any.request_id);
209
210 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad request ID %d."),
211 con->request->request.any.request_id);
212 }
213 else if (!con->request->attrs)
214 {
215 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
216 "%04X %s No attributes in request",
217 IPP_BAD_REQUEST, con->http->hostname);
218
219 send_ipp_status(con, IPP_BAD_REQUEST, _("No attributes in request."));
220 }
221 else
222 {
223 /*
224 * Make sure that the attributes are provided in the correct order and
225 * don't repeat groups...
226 */
227
228 for (attr = con->request->attrs, group = attr->group_tag;
229 attr;
230 attr = attr->next)
231 if (attr->group_tag < group && attr->group_tag != IPP_TAG_ZERO)
232 {
233 /*
234 * Out of order; return an error...
235 */
236
237 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
238 "%04X %s Attribute groups are out of order",
239 IPP_BAD_REQUEST, con->http->hostname);
240
241 send_ipp_status(con, IPP_BAD_REQUEST,
242 _("Attribute groups are out of order (%x < %x)."),
243 attr->group_tag, group);
244 break;
245 }
246 else
247 group = attr->group_tag;
248
249 if (!attr)
250 {
251 /*
252 * Then make sure that the first three attributes are:
253 *
254 * attributes-charset
255 * attributes-natural-language
256 * printer-uri/job-uri
257 */
258
259 attr = con->request->attrs;
260 if (attr && attr->name &&
261 !strcmp(attr->name, "attributes-charset") &&
262 (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET)
263 charset = attr;
264 else
265 charset = NULL;
266
267 if (attr)
268 attr = attr->next;
269
270 if (attr && attr->name &&
271 !strcmp(attr->name, "attributes-natural-language") &&
272 (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE)
273 {
274 language = attr;
275
276 /*
277 * Reset language for this request if different from Accept-Language.
278 */
279
280 if (!con->language ||
281 strcmp(attr->values[0].string.text, con->language->language))
282 {
283 cupsLangFree(con->language);
284 con->language = cupsLangGet(attr->values[0].string.text);
285 }
286 }
287 else
288 language = NULL;
289
290 if ((attr = ippFindAttribute(con->request, "printer-uri",
291 IPP_TAG_URI)) != NULL)
292 uri = attr;
293 else if ((attr = ippFindAttribute(con->request, "job-uri",
294 IPP_TAG_URI)) != NULL)
295 uri = attr;
296 else if (con->request->request.op.operation_id == CUPS_GET_PPD)
297 uri = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME);
298 else
299 uri = NULL;
300
301 if (charset)
302 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
303 "attributes-charset", NULL,
304 charset->values[0].string.text);
305 else
306 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
307 "attributes-charset", NULL, "utf-8");
308
309 if (language)
310 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
311 "attributes-natural-language", NULL,
312 language->values[0].string.text);
313 else
314 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
315 "attributes-natural-language", NULL, DefaultLanguage);
316
317 if (charset &&
318 _cups_strcasecmp(charset->values[0].string.text, "us-ascii") &&
319 _cups_strcasecmp(charset->values[0].string.text, "utf-8"))
320 {
321 /*
322 * Bad character set...
323 */
324
325 cupsdLogMessage(CUPSD_LOG_ERROR, "Unsupported character set \"%s\"",
326 charset->values[0].string.text);
327 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
328 "%04X %s Unsupported attributes-charset value \"%s\"",
329 IPP_CHARSET, con->http->hostname,
330 charset->values[0].string.text);
331 send_ipp_status(con, IPP_BAD_REQUEST,
332 _("Unsupported character set \"%s\"."),
333 charset->values[0].string.text);
334 }
335 else if (!charset || !language ||
336 (!uri &&
337 con->request->request.op.operation_id != CUPS_GET_DEFAULT &&
338 con->request->request.op.operation_id != CUPS_GET_PRINTERS &&
339 con->request->request.op.operation_id != CUPS_GET_CLASSES &&
340 con->request->request.op.operation_id != CUPS_GET_DEVICES &&
341 con->request->request.op.operation_id != CUPS_GET_PPDS))
342 {
343 /*
344 * Return an error, since attributes-charset,
345 * attributes-natural-language, and printer-uri/job-uri are required
346 * for all operations.
347 */
348
349 if (!charset)
350 {
351 cupsdLogMessage(CUPSD_LOG_ERROR,
352 "Missing attributes-charset attribute");
353
354 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
355 "%04X %s Missing attributes-charset attribute",
356 IPP_BAD_REQUEST, con->http->hostname);
357 }
358
359 if (!language)
360 {
361 cupsdLogMessage(CUPSD_LOG_ERROR,
362 "Missing attributes-natural-language attribute");
363
364 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
365 "%04X %s Missing attributes-natural-language attribute",
366 IPP_BAD_REQUEST, con->http->hostname);
367 }
368
369 if (!uri)
370 {
371 cupsdLogMessage(CUPSD_LOG_ERROR,
372 "Missing printer-uri, job-uri, or ppd-name "
373 "attribute");
374
375 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
376 "%04X %s Missing printer-uri, job-uri, or ppd-name "
377 "attribute", IPP_BAD_REQUEST, con->http->hostname);
378 }
379
380 cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow...");
381
382 for (attr = con->request->attrs; attr; attr = attr->next)
383 cupsdLogMessage(CUPSD_LOG_DEBUG,
384 "attr \"%s\": group_tag = %x, value_tag = %x",
385 attr->name ? attr->name : "(null)", attr->group_tag,
386 attr->value_tag);
387
388 cupsdLogMessage(CUPSD_LOG_DEBUG, "End of attributes...");
389
390 send_ipp_status(con, IPP_BAD_REQUEST,
391 _("Missing required attributes."));
392 }
393 else
394 {
395 /*
396 * OK, all the checks pass so far; make sure requesting-user-name is
397 * not "root" from a remote host...
398 */
399
400 if ((username = ippFindAttribute(con->request, "requesting-user-name",
401 IPP_TAG_NAME)) != NULL)
402 {
403 /*
404 * Check for root user...
405 */
406
407 if (!strcmp(username->values[0].string.text, "root") &&
408 _cups_strcasecmp(con->http->hostname, "localhost") &&
409 strcmp(con->username, "root"))
410 {
411 /*
412 * Remote unauthenticated user masquerading as local root...
413 */
414
415 _cupsStrFree(username->values[0].string.text);
416 username->values[0].string.text = _cupsStrAlloc(RemoteRoot);
417 }
418 }
419
420 if ((attr = ippFindAttribute(con->request, "notify-subscription-id",
421 IPP_TAG_INTEGER)) != NULL)
422 sub_id = attr->values[0].integer;
423 else
424 sub_id = 0;
425
426 /*
427 * Then try processing the operation...
428 */
429
430 if (uri)
431 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s",
432 ippOpString(con->request->request.op.operation_id),
433 uri->values[0].string.text);
434 else
435 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s",
436 ippOpString(con->request->request.op.operation_id));
437
438 switch (con->request->request.op.operation_id)
439 {
440 case IPP_PRINT_JOB :
441 print_job(con, uri);
442 break;
443
444 case IPP_VALIDATE_JOB :
445 validate_job(con, uri);
446 break;
447
448 case IPP_CREATE_JOB :
449 create_job(con, uri);
450 break;
451
452 case IPP_SEND_DOCUMENT :
453 send_document(con, uri);
454 break;
455
456 case IPP_CANCEL_JOB :
457 cancel_job(con, uri);
458 break;
459
460 case IPP_GET_JOB_ATTRIBUTES :
461 get_job_attrs(con, uri);
462 break;
463
464 case IPP_GET_JOBS :
465 get_jobs(con, uri);
466 break;
467
468 case IPP_GET_PRINTER_ATTRIBUTES :
469 get_printer_attrs(con, uri);
470 break;
471
472 case IPP_GET_PRINTER_SUPPORTED_VALUES :
473 get_printer_supported(con, uri);
474 break;
475
476 case IPP_HOLD_JOB :
477 hold_job(con, uri);
478 break;
479
480 case IPP_RELEASE_JOB :
481 release_job(con, uri);
482 break;
483
484 case IPP_RESTART_JOB :
485 restart_job(con, uri);
486 break;
487
488 case IPP_PAUSE_PRINTER :
489 stop_printer(con, uri);
490 break;
491
492 case IPP_RESUME_PRINTER :
493 start_printer(con, uri);
494 break;
495
496 case IPP_PURGE_JOBS :
497 case IPP_CANCEL_JOBS :
498 case IPP_CANCEL_MY_JOBS :
499 cancel_all_jobs(con, uri);
500 break;
501
502 case IPP_SET_JOB_ATTRIBUTES :
503 set_job_attrs(con, uri);
504 break;
505
506 case IPP_SET_PRINTER_ATTRIBUTES :
507 set_printer_attrs(con, uri);
508 break;
509
510 case IPP_HOLD_NEW_JOBS :
511 hold_new_jobs(con, uri);
512 break;
513
514 case IPP_RELEASE_HELD_NEW_JOBS :
515 release_held_new_jobs(con, uri);
516 break;
517
518 case IPP_CLOSE_JOB :
519 close_job(con, uri);
520 break;
521
522 case CUPS_GET_DEFAULT :
523 get_default(con);
524 break;
525
526 case CUPS_GET_PRINTERS :
527 get_printers(con, 0);
528 break;
529
530 case CUPS_GET_CLASSES :
531 get_printers(con, CUPS_PRINTER_CLASS);
532 break;
533
534 case CUPS_ADD_PRINTER :
535 add_printer(con, uri);
536 break;
537
538 case CUPS_DELETE_PRINTER :
539 delete_printer(con, uri);
540 break;
541
542 case CUPS_ADD_CLASS :
543 add_class(con, uri);
544 break;
545
546 case CUPS_DELETE_CLASS :
547 delete_printer(con, uri);
548 break;
549
550 case CUPS_ACCEPT_JOBS :
551 case IPP_ENABLE_PRINTER :
552 accept_jobs(con, uri);
553 break;
554
555 case CUPS_REJECT_JOBS :
556 case IPP_DISABLE_PRINTER :
557 reject_jobs(con, uri);
558 break;
559
560 case CUPS_SET_DEFAULT :
561 set_default(con, uri);
562 break;
563
564 case CUPS_GET_DEVICES :
565 get_devices(con);
566 break;
567
568 case CUPS_GET_DOCUMENT :
569 get_document(con, uri);
570 break;
571
572 case CUPS_GET_PPD :
573 get_ppd(con, uri);
574 break;
575
576 case CUPS_GET_PPDS :
577 get_ppds(con);
578 break;
579
580 case CUPS_MOVE_JOB :
581 move_job(con, uri);
582 break;
583
584 case CUPS_AUTHENTICATE_JOB :
585 authenticate_job(con, uri);
586 break;
587
588 case IPP_CREATE_PRINTER_SUBSCRIPTION :
589 case IPP_CREATE_JOB_SUBSCRIPTION :
590 create_subscription(con, uri);
591 break;
592
593 case IPP_GET_SUBSCRIPTION_ATTRIBUTES :
594 get_subscription_attrs(con, sub_id);
595 break;
596
597 case IPP_GET_SUBSCRIPTIONS :
598 get_subscriptions(con, uri);
599 break;
600
601 case IPP_RENEW_SUBSCRIPTION :
602 renew_subscription(con, sub_id);
603 break;
604
605 case IPP_CANCEL_SUBSCRIPTION :
606 cancel_subscription(con, sub_id);
607 break;
608
609 case IPP_GET_NOTIFICATIONS :
610 get_notifications(con);
611 break;
612
613 default :
614 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
615 "%04X %s Operation %04X (%s) not supported",
616 IPP_OPERATION_NOT_SUPPORTED, con->http->hostname,
617 con->request->request.op.operation_id,
618 ippOpString(con->request->request.op.operation_id));
619
620 send_ipp_status(con, IPP_OPERATION_NOT_SUPPORTED,
621 _("%s not supported."),
622 ippOpString(
623 con->request->request.op.operation_id));
624 break;
625 }
626 }
627 }
628 }
629
630 if (con->response)
631 {
632 /*
633 * Sending data from the scheduler...
634 */
635
636 cupsdLogMessage(con->response->request.status.status_code
637 >= IPP_BAD_REQUEST &&
638 con->response->request.status.status_code
639 != IPP_NOT_FOUND ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
640 "[Client %d] Returning IPP %s for %s (%s) from %s",
641 con->number,
642 ippErrorString(con->response->request.status.status_code),
643 ippOpString(con->request->request.op.operation_id),
644 uri ? uri->values[0].string.text : "no URI",
645 con->http->hostname);
646
647 httpClearFields(con->http);
648
649 #ifdef CUPSD_USE_CHUNKING
650 /*
651 * Because older versions of CUPS (1.1.17 and older) and some IPP
652 * clients do not implement chunking properly, we cannot use
653 * chunking by default. This may become the default in future
654 * CUPS releases, or we might add a configuration directive for
655 * it.
656 */
657
658 if (con->http->version == HTTP_1_1)
659 {
660 cupsdLogMessage(CUPSD_LOG_DEBUG,
661 "[Client %d] Transfer-Encoding: chunked",
662 con->number);
663
664 cupsdSetLength(con->http, 0);
665 }
666 else
667 #endif /* CUPSD_USE_CHUNKING */
668 {
669 size_t length; /* Length of response */
670
671
672 length = ippLength(con->response);
673
674 if (con->file >= 0 && !con->pipe_pid)
675 {
676 struct stat fileinfo; /* File information */
677
678 if (!fstat(con->file, &fileinfo))
679 length += fileinfo.st_size;
680 }
681
682 cupsdLogMessage(CUPSD_LOG_DEBUG,
683 "[Client %d] Content-Length: " CUPS_LLFMT,
684 con->number, CUPS_LLCAST length);
685 httpSetLength(con->http, length);
686 }
687
688 if (cupsdSendHeader(con, HTTP_OK, "application/ipp", CUPSD_AUTH_NONE))
689 {
690 /*
691 * Tell the caller the response header was sent successfully...
692 */
693
694 cupsdAddSelect(httpGetFd(con->http), (cupsd_selfunc_t)cupsdReadClient,
695 (cupsd_selfunc_t)cupsdWriteClient, con);
696
697 return (1);
698 }
699 else
700 {
701 /*
702 * Tell the caller the response header could not be sent...
703 */
704
705 return (0);
706 }
707 }
708 else
709 {
710 /*
711 * Sending data from a subprocess like cups-deviced; tell the caller
712 * everything is A-OK so far...
713 */
714
715 return (1);
716 }
717 }
718
719
720 /*
721 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
722 */
723
724 int /* O - 0 on success, -1 on error */
725 cupsdTimeoutJob(cupsd_job_t *job) /* I - Job to timeout */
726 {
727 cupsd_printer_t *printer; /* Destination printer or class */
728 ipp_attribute_t *attr; /* job-sheets attribute */
729 int kbytes; /* Kilobytes in banner */
730
731
732 job->pending_timeout = 0;
733
734 /*
735 * See if we need to add the ending sheet...
736 */
737
738 if (!cupsdLoadJob(job))
739 return (-1);
740
741 printer = cupsdFindDest(job->dest);
742 attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
743
744 if (printer && !(printer->type & CUPS_PRINTER_REMOTE) &&
745 attr && attr->num_values > 1)
746 {
747 /*
748 * Yes...
749 */
750
751 cupsdLogJob(job, CUPSD_LOG_INFO, "Adding end banner page \"%s\".",
752 attr->values[1].string.text);
753
754 if ((kbytes = copy_banner(NULL, job, attr->values[1].string.text)) < 0)
755 return (-1);
756
757 cupsdUpdateQuota(printer, job->username, 0, kbytes);
758 }
759
760 return (0);
761 }
762
763
764 /*
765 * 'accept_jobs()' - Accept print jobs to a printer.
766 */
767
768 static void
769 accept_jobs(cupsd_client_t *con, /* I - Client connection */
770 ipp_attribute_t *uri) /* I - Printer or class URI */
771 {
772 http_status_t status; /* Policy status */
773 cups_ptype_t dtype; /* Destination type (printer/class) */
774 cupsd_printer_t *printer; /* Printer data */
775
776
777 cupsdLogMessage(CUPSD_LOG_DEBUG2, "accept_jobs(%p[%d], %s)", con,
778 con->number, uri->values[0].string.text);
779
780 /*
781 * Is the destination valid?
782 */
783
784 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
785 {
786 /*
787 * Bad URI...
788 */
789
790 send_ipp_status(con, IPP_NOT_FOUND,
791 _("The printer or class does not exist."));
792 return;
793 }
794
795 /*
796 * Check policy...
797 */
798
799 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
800 {
801 send_http_error(con, status, printer);
802 return;
803 }
804
805 /*
806 * Accept jobs sent to the printer...
807 */
808
809 printer->accepting = 1;
810 printer->state_message[0] = '\0';
811
812 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
813 "Now accepting jobs.");
814
815 if (dtype & CUPS_PRINTER_CLASS)
816 {
817 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
818
819 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" now accepting jobs (\"%s\").",
820 printer->name, get_username(con));
821 }
822 else
823 {
824 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
825
826 cupsdLogMessage(CUPSD_LOG_INFO,
827 "Printer \"%s\" now accepting jobs (\"%s\").",
828 printer->name, get_username(con));
829 }
830
831 /*
832 * Everything was ok, so return OK status...
833 */
834
835 con->response->request.status.status_code = IPP_OK;
836 }
837
838
839 /*
840 * 'add_class()' - Add a class to the system.
841 */
842
843 static void
844 add_class(cupsd_client_t *con, /* I - Client connection */
845 ipp_attribute_t *uri) /* I - URI of class */
846 {
847 http_status_t status; /* Policy status */
848 int i; /* Looping var */
849 char scheme[HTTP_MAX_URI], /* Method portion of URI */
850 username[HTTP_MAX_URI], /* Username portion of URI */
851 host[HTTP_MAX_URI], /* Host portion of URI */
852 resource[HTTP_MAX_URI]; /* Resource portion of URI */
853 int port; /* Port portion of URI */
854 cupsd_printer_t *pclass, /* Class */
855 *member; /* Member printer/class */
856 cups_ptype_t dtype; /* Destination type */
857 ipp_attribute_t *attr; /* Printer attribute */
858 int modify; /* Non-zero if we just modified */
859 int need_restart_job; /* Need to restart job? */
860
861
862 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_class(%p[%d], %s)", con,
863 con->number, uri->values[0].string.text);
864
865 /*
866 * Do we have a valid URI?
867 */
868
869 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
870 sizeof(scheme), username, sizeof(username), host,
871 sizeof(host), &port, resource, sizeof(resource));
872
873
874 if (strncmp(resource, "/classes/", 9) || strlen(resource) == 9)
875 {
876 /*
877 * No, return an error...
878 */
879
880 send_ipp_status(con, IPP_BAD_REQUEST,
881 _("The printer-uri must be of the form "
882 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
883 return;
884 }
885
886 /*
887 * Do we have a valid printer name?
888 */
889
890 if (!validate_name(resource + 9))
891 {
892 /*
893 * No, return an error...
894 */
895
896 send_ipp_status(con, IPP_BAD_REQUEST,
897 _("The printer-uri \"%s\" contains invalid characters."),
898 uri->values[0].string.text);
899 return;
900 }
901
902 /*
903 * See if the class already exists; if not, create a new class...
904 */
905
906 if ((pclass = cupsdFindClass(resource + 9)) == NULL)
907 {
908 /*
909 * Class doesn't exist; see if we have a printer of the same name...
910 */
911
912 if ((pclass = cupsdFindPrinter(resource + 9)) != NULL)
913 {
914 /*
915 * Yes, return an error...
916 */
917
918 send_ipp_status(con, IPP_NOT_POSSIBLE,
919 _("A printer named \"%s\" already exists."),
920 resource + 9);
921 return;
922 }
923
924 /*
925 * No, check the default policy and then add the class...
926 */
927
928 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
929 {
930 send_http_error(con, status, NULL);
931 return;
932 }
933
934 pclass = cupsdAddClass(resource + 9);
935 modify = 0;
936 }
937 else if ((status = cupsdCheckPolicy(pclass->op_policy_ptr, con,
938 NULL)) != HTTP_OK)
939 {
940 send_http_error(con, status, pclass);
941 return;
942 }
943 else
944 modify = 1;
945
946 /*
947 * Look for attributes and copy them over as needed...
948 */
949
950 need_restart_job = 0;
951
952 if ((attr = ippFindAttribute(con->request, "printer-location",
953 IPP_TAG_TEXT)) != NULL)
954 cupsdSetString(&pclass->location, attr->values[0].string.text);
955
956 if ((attr = ippFindAttribute(con->request, "printer-info",
957 IPP_TAG_TEXT)) != NULL)
958 cupsdSetString(&pclass->info, attr->values[0].string.text);
959
960 if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs",
961 IPP_TAG_BOOLEAN)) != NULL &&
962 attr->values[0].boolean != pclass->accepting)
963 {
964 cupsdLogMessage(CUPSD_LOG_INFO,
965 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
966 pclass->name, attr->values[0].boolean, pclass->accepting);
967
968 pclass->accepting = attr->values[0].boolean;
969
970 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, pclass, NULL, "%s accepting jobs.",
971 pclass->accepting ? "Now" : "No longer");
972 }
973
974 if ((attr = ippFindAttribute(con->request, "printer-is-shared",
975 IPP_TAG_BOOLEAN)) != NULL)
976 {
977 if (pclass->shared && !attr->values[0].boolean)
978 cupsdDeregisterPrinter(pclass, 1);
979
980 cupsdLogMessage(CUPSD_LOG_INFO,
981 "Setting %s printer-is-shared to %d (was %d.)",
982 pclass->name, attr->values[0].boolean, pclass->shared);
983
984 pclass->shared = attr->values[0].boolean;
985 }
986
987 if ((attr = ippFindAttribute(con->request, "printer-state",
988 IPP_TAG_ENUM)) != NULL)
989 {
990 if (attr->values[0].integer != IPP_PRINTER_IDLE &&
991 attr->values[0].integer != IPP_PRINTER_STOPPED)
992 {
993 send_ipp_status(con, IPP_BAD_REQUEST,
994 _("Attempt to set %s printer-state to bad value %d."),
995 pclass->name, attr->values[0].integer);
996 return;
997 }
998
999 cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
1000 pclass->name, attr->values[0].integer, pclass->state);
1001
1002 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1003 cupsdStopPrinter(pclass, 0);
1004 else
1005 {
1006 cupsdSetPrinterState(pclass, (ipp_pstate_t)(attr->values[0].integer), 0);
1007 need_restart_job = 1;
1008 }
1009 }
1010 if ((attr = ippFindAttribute(con->request, "printer-state-message",
1011 IPP_TAG_TEXT)) != NULL)
1012 {
1013 strlcpy(pclass->state_message, attr->values[0].string.text,
1014 sizeof(pclass->state_message));
1015
1016 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, pclass, NULL, "%s",
1017 pclass->state_message);
1018 }
1019 if ((attr = ippFindAttribute(con->request, "member-uris",
1020 IPP_TAG_URI)) != NULL)
1021 {
1022 /*
1023 * Clear the printer array as needed...
1024 */
1025
1026 need_restart_job = 1;
1027
1028 if (pclass->num_printers > 0)
1029 {
1030 free(pclass->printers);
1031 pclass->num_printers = 0;
1032 }
1033
1034 /*
1035 * Add each printer or class that is listed...
1036 */
1037
1038 for (i = 0; i < attr->num_values; i ++)
1039 {
1040 /*
1041 * Search for the printer or class URI...
1042 */
1043
1044 if (!cupsdValidateDest(attr->values[i].string.text, &dtype, &member))
1045 {
1046 /*
1047 * Bad URI...
1048 */
1049
1050 send_ipp_status(con, IPP_NOT_FOUND,
1051 _("The printer or class does not exist."));
1052 return;
1053 }
1054 else if (dtype & CUPS_PRINTER_CLASS)
1055 {
1056 send_ipp_status(con, IPP_BAD_REQUEST,
1057 _("Nested classes are not allowed."));
1058 return;
1059 }
1060
1061 /*
1062 * Add it to the class...
1063 */
1064
1065 cupsdAddPrinterToClass(pclass, member);
1066 }
1067 }
1068
1069 set_printer_defaults(con, pclass);
1070
1071 if ((attr = ippFindAttribute(con->request, "auth-info-required",
1072 IPP_TAG_KEYWORD)) != NULL)
1073 cupsdSetAuthInfoRequired(pclass, NULL, attr);
1074
1075 /*
1076 * Update the printer class attributes and return...
1077 */
1078
1079 cupsdSetPrinterAttrs(pclass);
1080 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
1081
1082 if (need_restart_job && pclass->job)
1083 {
1084 /*
1085 * Reset the current job to a "pending" status...
1086 */
1087
1088 cupsdSetJobState(pclass->job, IPP_JOB_PENDING, CUPSD_JOB_FORCE,
1089 "Job restarted because the class was modified.");
1090 }
1091
1092 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
1093
1094 if (modify)
1095 {
1096 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED,
1097 pclass, NULL, "Class \"%s\" modified by \"%s\".",
1098 pclass->name, get_username(con));
1099
1100 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" modified by \"%s\".",
1101 pclass->name, get_username(con));
1102 }
1103 else
1104 {
1105 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED,
1106 pclass, NULL, "New class \"%s\" added by \"%s\".",
1107 pclass->name, get_username(con));
1108
1109 cupsdLogMessage(CUPSD_LOG_INFO, "New class \"%s\" added by \"%s\".",
1110 pclass->name, get_username(con));
1111 }
1112
1113 con->response->request.status.status_code = IPP_OK;
1114 }
1115
1116
1117 /*
1118 * 'add_file()' - Add a file to a job.
1119 */
1120
1121 static int /* O - 0 on success, -1 on error */
1122 add_file(cupsd_client_t *con, /* I - Connection to client */
1123 cupsd_job_t *job, /* I - Job to add to */
1124 mime_type_t *filetype, /* I - Type of file */
1125 int compression) /* I - Compression */
1126 {
1127 mime_type_t **filetypes; /* New filetypes array... */
1128 int *compressions; /* New compressions array... */
1129
1130
1131 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1132 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1133 "compression=%d)", con, con ? con->number : -1, job->id,
1134 filetype->super, filetype->type, compression);
1135
1136 /*
1137 * Add the file to the job...
1138 */
1139
1140 if (job->num_files == 0)
1141 {
1142 compressions = (int *)malloc(sizeof(int));
1143 filetypes = (mime_type_t **)malloc(sizeof(mime_type_t *));
1144 }
1145 else
1146 {
1147 compressions = (int *)realloc(job->compressions,
1148 (job->num_files + 1) * sizeof(int));
1149 filetypes = (mime_type_t **)realloc(job->filetypes,
1150 (job->num_files + 1) *
1151 sizeof(mime_type_t *));
1152 }
1153
1154 if (compressions)
1155 job->compressions = compressions;
1156
1157 if (filetypes)
1158 job->filetypes = filetypes;
1159
1160 if (!compressions || !filetypes)
1161 {
1162 cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_PURGE,
1163 "Job aborted because the scheduler ran out of memory.");
1164
1165 if (con)
1166 send_ipp_status(con, IPP_INTERNAL_ERROR,
1167 _("Unable to allocate memory for file types."));
1168
1169 return (-1);
1170 }
1171
1172 job->compressions[job->num_files] = compression;
1173 job->filetypes[job->num_files] = filetype;
1174
1175 job->num_files ++;
1176
1177 job->dirty = 1;
1178 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
1179
1180 return (0);
1181 }
1182
1183
1184 /*
1185 * 'add_job()' - Add a job to a print queue.
1186 */
1187
1188 static cupsd_job_t * /* O - Job object */
1189 add_job(cupsd_client_t *con, /* I - Client connection */
1190 cupsd_printer_t *printer, /* I - Destination printer */
1191 mime_type_t *filetype) /* I - First print file type, if any */
1192 {
1193 http_status_t status; /* Policy status */
1194 ipp_attribute_t *attr, /* Current attribute */
1195 *auth_info; /* auth-info attribute */
1196 const char *mandatory; /* Current mandatory job attribute */
1197 const char *val; /* Default option value */
1198 int priority; /* Job priority */
1199 cupsd_job_t *job; /* Current job */
1200 char job_uri[HTTP_MAX_URI]; /* Job URI */
1201 int kbytes; /* Size of print file */
1202 int i; /* Looping var */
1203 int lowerpagerange; /* Page range bound */
1204 int exact; /* Did we have an exact match? */
1205 ipp_attribute_t *media_col, /* media-col attribute */
1206 *media_margin; /* media-*-margin attribute */
1207 ipp_t *unsup_col; /* media-col in unsupported response */
1208 static const char * const readonly[] =/* List of read-only attributes */
1209 {
1210 "job-id",
1211 "job-k-octets-completed",
1212 "job-impressions-completed",
1213 "job-media-sheets-completed",
1214 "job-state",
1215 "job-state-message",
1216 "job-state-reasons",
1217 "time-at-completed",
1218 "time-at-creation",
1219 "time-at-processing"
1220 };
1221
1222
1223 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1224 con, con->number, printer, printer->name,
1225 filetype, filetype ? filetype->super : "none",
1226 filetype ? filetype->type : "none");
1227
1228 /*
1229 * Check remote printing to non-shared printer...
1230 */
1231
1232 if (!printer->shared &&
1233 _cups_strcasecmp(con->http->hostname, "localhost") &&
1234 _cups_strcasecmp(con->http->hostname, ServerName))
1235 {
1236 send_ipp_status(con, IPP_NOT_AUTHORIZED,
1237 _("The printer or class is not shared."));
1238 return (NULL);
1239 }
1240
1241 /*
1242 * Check policy...
1243 */
1244
1245 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
1246
1247 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
1248 {
1249 send_http_error(con, status, printer);
1250 return (NULL);
1251 }
1252 else if (printer->num_auth_info_required == 1 &&
1253 !strcmp(printer->auth_info_required[0], "negotiate") &&
1254 !con->username[0])
1255 {
1256 send_http_error(con, HTTP_UNAUTHORIZED, printer);
1257 return (NULL);
1258 }
1259 #ifdef HAVE_SSL
1260 else if (auth_info && !con->http->tls &&
1261 !httpAddrLocalhost(con->http->hostaddr))
1262 {
1263 /*
1264 * Require encryption of auth-info over non-local connections...
1265 */
1266
1267 send_http_error(con, HTTP_UPGRADE_REQUIRED, printer);
1268 return (NULL);
1269 }
1270 #endif /* HAVE_SSL */
1271
1272 /*
1273 * See if the printer is accepting jobs...
1274 */
1275
1276 if (!printer->accepting)
1277 {
1278 send_ipp_status(con, IPP_NOT_ACCEPTING,
1279 _("Destination \"%s\" is not accepting jobs."),
1280 printer->name);
1281 return (NULL);
1282 }
1283
1284 /*
1285 * Validate job template attributes; for now just document-format,
1286 * copies, job-sheets, number-up, page-ranges, mandatory attributes, and
1287 * media...
1288 */
1289
1290 for (i = 0; i < (int)(sizeof(readonly) / sizeof(readonly[0])); i ++)
1291 {
1292 if ((attr = ippFindAttribute(con->request, readonly[i],
1293 IPP_TAG_ZERO)) != NULL)
1294 {
1295 ippDeleteAttribute(con->request, attr);
1296
1297 if (StrictConformance)
1298 {
1299 send_ipp_status(con, IPP_BAD_REQUEST,
1300 _("The '%s' Job Description attribute cannot be "
1301 "supplied in a job creation request."), readonly[i]);
1302 return (NULL);
1303 }
1304
1305 cupsdLogMessage(CUPSD_LOG_INFO,
1306 "Unexpected '%s' Job Description attribute in a job "
1307 "creation request.", readonly[i]);
1308 }
1309 }
1310
1311 if (printer->pc)
1312 {
1313 for (mandatory = (char *)cupsArrayFirst(printer->pc->mandatory);
1314 mandatory;
1315 mandatory = (char *)cupsArrayNext(printer->pc->mandatory))
1316 {
1317 if (!ippFindAttribute(con->request, mandatory, IPP_TAG_ZERO))
1318 {
1319 /*
1320 * Missing a required attribute...
1321 */
1322
1323 send_ipp_status(con, IPP_CONFLICT,
1324 _("The \"%s\" attribute is required for print jobs."),
1325 mandatory);
1326 return (NULL);
1327 }
1328 }
1329 }
1330
1331 if (filetype && printer->filetypes &&
1332 !cupsArrayFind(printer->filetypes, filetype))
1333 {
1334 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
1335 /* MIME media type string */
1336
1337
1338 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
1339 filetype->type);
1340
1341 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
1342 _("Unsupported format \"%s\"."), mimetype);
1343
1344 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
1345 "document-format", NULL, mimetype);
1346
1347 return (NULL);
1348 }
1349
1350 if ((attr = ippFindAttribute(con->request, "copies",
1351 IPP_TAG_INTEGER)) != NULL)
1352 {
1353 if (attr->values[0].integer < 1 || attr->values[0].integer > MaxCopies)
1354 {
1355 send_ipp_status(con, IPP_ATTRIBUTES, _("Bad copies value %d."),
1356 attr->values[0].integer);
1357 ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER,
1358 "copies", attr->values[0].integer);
1359 return (NULL);
1360 }
1361 }
1362
1363 if ((attr = ippFindAttribute(con->request, "job-sheets",
1364 IPP_TAG_ZERO)) != NULL)
1365 {
1366 if (attr->value_tag != IPP_TAG_KEYWORD &&
1367 attr->value_tag != IPP_TAG_NAME)
1368 {
1369 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value type."));
1370 return (NULL);
1371 }
1372
1373 if (attr->num_values > 2)
1374 {
1375 send_ipp_status(con, IPP_BAD_REQUEST,
1376 _("Too many job-sheets values (%d > 2)."),
1377 attr->num_values);
1378 return (NULL);
1379 }
1380
1381 for (i = 0; i < attr->num_values; i ++)
1382 if (strcmp(attr->values[i].string.text, "none") &&
1383 !cupsdFindBanner(attr->values[i].string.text))
1384 {
1385 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value \"%s\"."),
1386 attr->values[i].string.text);
1387 return (NULL);
1388 }
1389 }
1390
1391 if ((attr = ippFindAttribute(con->request, "number-up",
1392 IPP_TAG_INTEGER)) != NULL)
1393 {
1394 if (attr->values[0].integer != 1 &&
1395 attr->values[0].integer != 2 &&
1396 attr->values[0].integer != 4 &&
1397 attr->values[0].integer != 6 &&
1398 attr->values[0].integer != 9 &&
1399 attr->values[0].integer != 16)
1400 {
1401 send_ipp_status(con, IPP_ATTRIBUTES, _("Bad number-up value %d."),
1402 attr->values[0].integer);
1403 ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER,
1404 "number-up", attr->values[0].integer);
1405 return (NULL);
1406 }
1407 }
1408
1409 if ((attr = ippFindAttribute(con->request, "page-ranges",
1410 IPP_TAG_RANGE)) != NULL)
1411 {
1412 for (i = 0, lowerpagerange = 1; i < attr->num_values; i ++)
1413 {
1414 if (attr->values[i].range.lower < lowerpagerange ||
1415 attr->values[i].range.lower > attr->values[i].range.upper)
1416 {
1417 send_ipp_status(con, IPP_BAD_REQUEST,
1418 _("Bad page-ranges values %d-%d."),
1419 attr->values[i].range.lower,
1420 attr->values[i].range.upper);
1421 return (NULL);
1422 }
1423
1424 lowerpagerange = attr->values[i].range.upper + 1;
1425 }
1426 }
1427
1428 /*
1429 * Do media selection as needed...
1430 */
1431
1432 if (!ippFindAttribute(con->request, "PageRegion", IPP_TAG_ZERO) &&
1433 !ippFindAttribute(con->request, "PageSize", IPP_TAG_ZERO) &&
1434 _ppdCacheGetPageSize(printer->pc, con->request, NULL, &exact))
1435 {
1436 if (!exact &&
1437 (media_col = ippFindAttribute(con->request, "media-col",
1438 IPP_TAG_BEGIN_COLLECTION)) != NULL)
1439 {
1440 send_ipp_status(con, IPP_OK_SUBST, _("Unsupported margins."));
1441
1442 unsup_col = ippNew();
1443 if ((media_margin = ippFindAttribute(media_col->values[0].collection,
1444 "media-bottom-margin",
1445 IPP_TAG_INTEGER)) != NULL)
1446 ippAddInteger(unsup_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1447 "media-bottom-margin", media_margin->values[0].integer);
1448
1449 if ((media_margin = ippFindAttribute(media_col->values[0].collection,
1450 "media-left-margin",
1451 IPP_TAG_INTEGER)) != NULL)
1452 ippAddInteger(unsup_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1453 "media-left-margin", media_margin->values[0].integer);
1454
1455 if ((media_margin = ippFindAttribute(media_col->values[0].collection,
1456 "media-right-margin",
1457 IPP_TAG_INTEGER)) != NULL)
1458 ippAddInteger(unsup_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1459 "media-right-margin", media_margin->values[0].integer);
1460
1461 if ((media_margin = ippFindAttribute(media_col->values[0].collection,
1462 "media-top-margin",
1463 IPP_TAG_INTEGER)) != NULL)
1464 ippAddInteger(unsup_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1465 "media-top-margin", media_margin->values[0].integer);
1466
1467 ippAddCollection(con->response, IPP_TAG_UNSUPPORTED_GROUP, "media-col",
1468 unsup_col);
1469 ippDelete(unsup_col);
1470 }
1471 }
1472
1473 /*
1474 * Make sure we aren't over our limit...
1475 */
1476
1477 if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs)
1478 cupsdCleanJobs();
1479
1480 if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs)
1481 {
1482 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Too many active jobs."));
1483 return (NULL);
1484 }
1485
1486 if ((i = check_quotas(con, printer)) < 0)
1487 {
1488 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
1489 return (NULL);
1490 }
1491 else if (i == 0)
1492 {
1493 send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print."));
1494 return (NULL);
1495 }
1496
1497 /*
1498 * Create the job and set things up...
1499 */
1500
1501 if ((attr = ippFindAttribute(con->request, "job-priority",
1502 IPP_TAG_INTEGER)) != NULL)
1503 priority = attr->values[0].integer;
1504 else
1505 {
1506 if ((val = cupsGetOption("job-priority", printer->num_options,
1507 printer->options)) != NULL)
1508 priority = atoi(val);
1509 else
1510 priority = 50;
1511
1512 ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
1513 priority);
1514 }
1515
1516 if ((attr = ippFindAttribute(con->request, "job-name", IPP_TAG_ZERO)) == NULL)
1517 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL,
1518 "Untitled");
1519 else if ((attr->value_tag != IPP_TAG_NAME &&
1520 attr->value_tag != IPP_TAG_NAMELANG) ||
1521 attr->num_values != 1)
1522 {
1523 send_ipp_status(con, IPP_ATTRIBUTES,
1524 _("Bad job-name value: Wrong type or count."));
1525 if ((attr = ippCopyAttribute(con->response, attr, 0)) != NULL)
1526 attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
1527 return (NULL);
1528 }
1529 else if (!ippValidateAttribute(attr))
1530 {
1531 send_ipp_status(con, IPP_ATTRIBUTES, _("Bad job-name value: %s"),
1532 cupsLastErrorString());
1533 if ((attr = ippCopyAttribute(con->response, attr, 0)) != NULL)
1534 attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
1535 return (NULL);
1536 }
1537
1538 if ((job = cupsdAddJob(priority, printer->name)) == NULL)
1539 {
1540 send_ipp_status(con, IPP_INTERNAL_ERROR,
1541 _("Unable to add job for destination \"%s\"."),
1542 printer->name);
1543 return (NULL);
1544 }
1545
1546 job->dtype = printer->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE);
1547 job->attrs = con->request;
1548 job->dirty = 1;
1549 con->request = ippNewRequest(job->attrs->request.op.operation_id);
1550
1551 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
1552
1553 add_job_uuid(job);
1554 apply_printer_defaults(printer, job);
1555
1556 attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME);
1557
1558 if (con->username[0])
1559 {
1560 cupsdSetString(&job->username, con->username);
1561
1562 if (attr)
1563 cupsdSetString(&attr->values[0].string.text, con->username);
1564 }
1565 else if (attr)
1566 {
1567 cupsdLogMessage(CUPSD_LOG_DEBUG,
1568 "add_job: requesting-user-name=\"%s\"",
1569 attr->values[0].string.text);
1570
1571 cupsdSetString(&job->username, attr->values[0].string.text);
1572 }
1573 else
1574 cupsdSetString(&job->username, "anonymous");
1575
1576 if (!attr)
1577 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
1578 "job-originating-user-name", NULL, job->username);
1579 else
1580 {
1581 attr->group_tag = IPP_TAG_JOB;
1582 _cupsStrFree(attr->name);
1583 attr->name = _cupsStrAlloc("job-originating-user-name");
1584 }
1585
1586 if (con->username[0] || auth_info)
1587 {
1588 save_auth_info(con, job, auth_info);
1589
1590 /*
1591 * Remove the auth-info attribute from the attribute data...
1592 */
1593
1594 if (auth_info)
1595 ippDeleteAttribute(job->attrs, auth_info);
1596 }
1597
1598 if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name",
1599 IPP_TAG_ZERO)) != NULL)
1600 {
1601 /*
1602 * Request contains a job-originating-host-name attribute; validate it...
1603 */
1604
1605 if (attr->value_tag != IPP_TAG_NAME ||
1606 attr->num_values != 1 ||
1607 strcmp(con->http->hostname, "localhost"))
1608 {
1609 /*
1610 * Can't override the value if we aren't connected via localhost.
1611 * Also, we can only have 1 value and it must be a name value.
1612 */
1613
1614 switch (attr->value_tag)
1615 {
1616 case IPP_TAG_STRING :
1617 case IPP_TAG_TEXTLANG :
1618 case IPP_TAG_NAMELANG :
1619 case IPP_TAG_TEXT :
1620 case IPP_TAG_NAME :
1621 case IPP_TAG_KEYWORD :
1622 case IPP_TAG_URI :
1623 case IPP_TAG_URISCHEME :
1624 case IPP_TAG_CHARSET :
1625 case IPP_TAG_LANGUAGE :
1626 case IPP_TAG_MIMETYPE :
1627 /*
1628 * Free old strings...
1629 */
1630
1631 for (i = 0; i < attr->num_values; i ++)
1632 {
1633 _cupsStrFree(attr->values[i].string.text);
1634 attr->values[i].string.text = NULL;
1635 if (attr->values[i].string.language)
1636 {
1637 _cupsStrFree(attr->values[i].string.language);
1638 attr->values[i].string.language = NULL;
1639 }
1640 }
1641
1642 default :
1643 break;
1644 }
1645
1646 /*
1647 * Use the default connection hostname instead...
1648 */
1649
1650 attr->value_tag = IPP_TAG_NAME;
1651 attr->num_values = 1;
1652 attr->values[0].string.text = _cupsStrAlloc(con->http->hostname);
1653 }
1654
1655 attr->group_tag = IPP_TAG_JOB;
1656 }
1657 else
1658 {
1659 /*
1660 * No job-originating-host-name attribute, so use the hostname from
1661 * the connection...
1662 */
1663
1664 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
1665 "job-originating-host-name", NULL, con->http->hostname);
1666 }
1667
1668 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation",
1669 time(NULL));
1670 attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
1671 "time-at-processing", 0);
1672 attr->value_tag = IPP_TAG_NOVALUE;
1673 attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
1674 "time-at-completed", 0);
1675 attr->value_tag = IPP_TAG_NOVALUE;
1676
1677 /*
1678 * Add remaining job attributes...
1679 */
1680
1681 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
1682 job->state = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_ENUM,
1683 "job-state", IPP_JOB_STOPPED);
1684 job->state_value = (ipp_jstate_t)job->state->values[0].integer;
1685 job->reasons = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1686 "job-state-reasons", NULL, "job-incoming");
1687 job->sheets = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
1688 "job-media-sheets-completed", 0);
1689 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL,
1690 printer->uri);
1691
1692 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
1693 IPP_TAG_INTEGER)) != NULL)
1694 attr->values[0].integer = 0;
1695 else
1696 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-k-octets", 0);
1697
1698 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
1699 IPP_TAG_KEYWORD)) == NULL)
1700 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
1701 if (!attr)
1702 {
1703 if ((val = cupsGetOption("job-hold-until", printer->num_options,
1704 printer->options)) == NULL)
1705 val = "no-hold";
1706
1707 attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1708 "job-hold-until", NULL, val);
1709 }
1710 if (attr && strcmp(attr->values[0].string.text, "no-hold"))
1711 {
1712 /*
1713 * Hold job until specified time...
1714 */
1715
1716 cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
1717
1718 job->state->values[0].integer = IPP_JOB_HELD;
1719 job->state_value = IPP_JOB_HELD;
1720
1721 ippSetString(job->attrs, &job->reasons, 0, "job-hold-until-specified");
1722 }
1723 else if (job->attrs->request.op.operation_id == IPP_CREATE_JOB)
1724 {
1725 job->hold_until = time(NULL) + MultipleOperationTimeout;
1726 job->state->values[0].integer = IPP_JOB_HELD;
1727 job->state_value = IPP_JOB_HELD;
1728 }
1729 else
1730 {
1731 job->state->values[0].integer = IPP_JOB_PENDING;
1732 job->state_value = IPP_JOB_PENDING;
1733
1734 ippSetString(job->attrs, &job->reasons, 0, "none");
1735 }
1736
1737 if (!(printer->type & CUPS_PRINTER_REMOTE) || Classification)
1738 {
1739 /*
1740 * Add job sheets options...
1741 */
1742
1743 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
1744 IPP_TAG_ZERO)) == NULL)
1745 {
1746 cupsdLogMessage(CUPSD_LOG_DEBUG,
1747 "Adding default job-sheets values \"%s,%s\"...",
1748 printer->job_sheets[0], printer->job_sheets[1]);
1749
1750 attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets",
1751 2, NULL, NULL);
1752 attr->values[0].string.text = _cupsStrRetain(printer->job_sheets[0]);
1753 attr->values[1].string.text = _cupsStrRetain(printer->job_sheets[1]);
1754 }
1755
1756 job->job_sheets = attr;
1757
1758 /*
1759 * Enforce classification level if set...
1760 */
1761
1762 if (Classification)
1763 {
1764 cupsdLogMessage(CUPSD_LOG_INFO,
1765 "Classification=\"%s\", ClassifyOverride=%d",
1766 Classification ? Classification : "(null)",
1767 ClassifyOverride);
1768
1769 if (ClassifyOverride)
1770 {
1771 if (!strcmp(attr->values[0].string.text, "none") &&
1772 (attr->num_values == 1 ||
1773 !strcmp(attr->values[1].string.text, "none")))
1774 {
1775 /*
1776 * Force the leading banner to have the classification on it...
1777 */
1778
1779 cupsdSetString(&attr->values[0].string.text, Classification);
1780
1781 cupsdLogJob(job, CUPSD_LOG_NOTICE, "CLASSIFICATION FORCED "
1782 "job-sheets=\"%s,none\", "
1783 "job-originating-user-name=\"%s\"",
1784 Classification, job->username);
1785 }
1786 else if (attr->num_values == 2 &&
1787 strcmp(attr->values[0].string.text,
1788 attr->values[1].string.text) &&
1789 strcmp(attr->values[0].string.text, "none") &&
1790 strcmp(attr->values[1].string.text, "none"))
1791 {
1792 /*
1793 * Can't put two different security markings on the same document!
1794 */
1795
1796 cupsdSetString(&attr->values[1].string.text, attr->values[0].string.text);
1797
1798 cupsdLogJob(job, CUPSD_LOG_NOTICE, "CLASSIFICATION FORCED "
1799 "job-sheets=\"%s,%s\", "
1800 "job-originating-user-name=\"%s\"",
1801 attr->values[0].string.text,
1802 attr->values[1].string.text, job->username);
1803 }
1804 else if (strcmp(attr->values[0].string.text, Classification) &&
1805 strcmp(attr->values[0].string.text, "none") &&
1806 (attr->num_values == 1 ||
1807 (strcmp(attr->values[1].string.text, Classification) &&
1808 strcmp(attr->values[1].string.text, "none"))))
1809 {
1810 if (attr->num_values == 1)
1811 cupsdLogJob(job, CUPSD_LOG_NOTICE,
1812 "CLASSIFICATION OVERRIDDEN "
1813 "job-sheets=\"%s\", "
1814 "job-originating-user-name=\"%s\"",
1815 attr->values[0].string.text, job->username);
1816 else
1817 cupsdLogJob(job, CUPSD_LOG_NOTICE,
1818 "CLASSIFICATION OVERRIDDEN "
1819 "job-sheets=\"%s,%s\",fffff "
1820 "job-originating-user-name=\"%s\"",
1821 attr->values[0].string.text,
1822 attr->values[1].string.text, job->username);
1823 }
1824 }
1825 else if (strcmp(attr->values[0].string.text, Classification) &&
1826 (attr->num_values == 1 ||
1827 strcmp(attr->values[1].string.text, Classification)))
1828 {
1829 /*
1830 * Force the banner to have the classification on it...
1831 */
1832
1833 if (attr->num_values > 1 &&
1834 !strcmp(attr->values[0].string.text, attr->values[1].string.text))
1835 {
1836 cupsdSetString(&(attr->values[0].string.text), Classification);
1837 cupsdSetString(&(attr->values[1].string.text), Classification);
1838 }
1839 else
1840 {
1841 if (attr->num_values == 1 ||
1842 strcmp(attr->values[0].string.text, "none"))
1843 cupsdSetString(&(attr->values[0].string.text), Classification);
1844
1845 if (attr->num_values > 1 &&
1846 strcmp(attr->values[1].string.text, "none"))
1847 cupsdSetString(&(attr->values[1].string.text), Classification);
1848 }
1849
1850 if (attr->num_values > 1)
1851 cupsdLogJob(job, CUPSD_LOG_NOTICE,
1852 "CLASSIFICATION FORCED "
1853 "job-sheets=\"%s,%s\", "
1854 "job-originating-user-name=\"%s\"",
1855 attr->values[0].string.text,
1856 attr->values[1].string.text, job->username);
1857 else
1858 cupsdLogJob(job, CUPSD_LOG_NOTICE,
1859 "CLASSIFICATION FORCED "
1860 "job-sheets=\"%s\", "
1861 "job-originating-user-name=\"%s\"",
1862 Classification, job->username);
1863 }
1864 }
1865
1866 /*
1867 * See if we need to add the starting sheet...
1868 */
1869
1870 if (!(printer->type & CUPS_PRINTER_REMOTE))
1871 {
1872 cupsdLogJob(job, CUPSD_LOG_INFO, "Adding start banner page \"%s\".",
1873 attr->values[0].string.text);
1874
1875 if ((kbytes = copy_banner(con, job, attr->values[0].string.text)) < 0)
1876 {
1877 cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_PURGE,
1878 "Aborting job because the start banner could not be "
1879 "copied.");
1880 return (NULL);
1881 }
1882
1883 cupsdUpdateQuota(printer, job->username, 0, kbytes);
1884 }
1885 }
1886 else if ((attr = ippFindAttribute(job->attrs, "job-sheets",
1887 IPP_TAG_ZERO)) != NULL)
1888 job->job_sheets = attr;
1889
1890 /*
1891 * Fill in the response info...
1892 */
1893
1894 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
1895 con->clientname, con->clientport, "/jobs/%d", job->id);
1896 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
1897 job_uri);
1898
1899 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
1900
1901 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
1902 job->state_value);
1903 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons",
1904 NULL, job->reasons->values[0].string.text);
1905
1906 con->response->request.status.status_code = IPP_OK;
1907
1908 /*
1909 * Add any job subscriptions...
1910 */
1911
1912 add_job_subscriptions(con, job);
1913
1914 /*
1915 * Set all but the first two attributes to the job attributes group...
1916 */
1917
1918 for (attr = job->attrs->attrs->next->next; attr; attr = attr->next)
1919 attr->group_tag = IPP_TAG_JOB;
1920
1921 /*
1922 * Fire the "job created" event...
1923 */
1924
1925 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created.");
1926
1927 /*
1928 * Return the new job...
1929 */
1930
1931 return (job);
1932 }
1933
1934
1935 /*
1936 * 'add_job_subscriptions()' - Add any subscriptions for a job.
1937 */
1938
1939 static void
1940 add_job_subscriptions(
1941 cupsd_client_t *con, /* I - Client connection */
1942 cupsd_job_t *job) /* I - Newly created job */
1943 {
1944 int i; /* Looping var */
1945 ipp_attribute_t *prev, /* Previous attribute */
1946 *next, /* Next attribute */
1947 *attr; /* Current attribute */
1948 cupsd_subscription_t *sub; /* Subscription object */
1949 const char *recipient, /* notify-recipient-uri */
1950 *pullmethod; /* notify-pull-method */
1951 ipp_attribute_t *user_data; /* notify-user-data */
1952 int interval; /* notify-time-interval */
1953 unsigned mask; /* notify-events */
1954
1955
1956 /*
1957 * Find the first subscription group attribute; return if we have
1958 * none...
1959 */
1960
1961 for (attr = job->attrs->attrs; attr; attr = attr->next)
1962 if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
1963 break;
1964
1965 if (!attr)
1966 return;
1967
1968 /*
1969 * Process the subscription attributes in the request...
1970 */
1971
1972 while (attr)
1973 {
1974 recipient = NULL;
1975 pullmethod = NULL;
1976 user_data = NULL;
1977 interval = 0;
1978 mask = CUPSD_EVENT_NONE;
1979
1980 while (attr && attr->group_tag != IPP_TAG_ZERO)
1981 {
1982 if (!strcmp(attr->name, "notify-recipient-uri") &&
1983 attr->value_tag == IPP_TAG_URI)
1984 {
1985 /*
1986 * Validate the recipient scheme against the ServerBin/notifier
1987 * directory...
1988 */
1989
1990 char notifier[1024], /* Notifier filename */
1991 scheme[HTTP_MAX_URI], /* Scheme portion of URI */
1992 userpass[HTTP_MAX_URI], /* Username portion of URI */
1993 host[HTTP_MAX_URI], /* Host portion of URI */
1994 resource[HTTP_MAX_URI]; /* Resource portion of URI */
1995 int port; /* Port portion of URI */
1996
1997
1998 recipient = attr->values[0].string.text;
1999
2000 if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient,
2001 scheme, sizeof(scheme), userpass, sizeof(userpass),
2002 host, sizeof(host), &port,
2003 resource, sizeof(resource)) < HTTP_URI_OK)
2004 {
2005 send_ipp_status(con, IPP_NOT_POSSIBLE,
2006 _("Bad notify-recipient-uri \"%s\"."), recipient);
2007 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2008 "notify-status-code", IPP_URI_SCHEME);
2009 return;
2010 }
2011
2012 snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
2013 scheme);
2014 if (access(notifier, X_OK))
2015 {
2016 send_ipp_status(con, IPP_NOT_POSSIBLE,
2017 _("notify-recipient-uri URI \"%s\" uses unknown "
2018 "scheme."), recipient);
2019 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2020 "notify-status-code", IPP_URI_SCHEME);
2021 return;
2022 }
2023
2024 if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient))
2025 {
2026 send_ipp_status(con, IPP_NOT_POSSIBLE,
2027 _("notify-recipient-uri URI \"%s\" is already used."),
2028 recipient);
2029 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2030 "notify-status-code", IPP_ATTRIBUTES);
2031 return;
2032 }
2033 }
2034 else if (!strcmp(attr->name, "notify-pull-method") &&
2035 attr->value_tag == IPP_TAG_KEYWORD)
2036 {
2037 pullmethod = attr->values[0].string.text;
2038
2039 if (strcmp(pullmethod, "ippget"))
2040 {
2041 send_ipp_status(con, IPP_NOT_POSSIBLE,
2042 _("Bad notify-pull-method \"%s\"."), pullmethod);
2043 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2044 "notify-status-code", IPP_ATTRIBUTES);
2045 return;
2046 }
2047 }
2048 else if (!strcmp(attr->name, "notify-charset") &&
2049 attr->value_tag == IPP_TAG_CHARSET &&
2050 strcmp(attr->values[0].string.text, "us-ascii") &&
2051 strcmp(attr->values[0].string.text, "utf-8"))
2052 {
2053 send_ipp_status(con, IPP_CHARSET,
2054 _("Character set \"%s\" not supported."),
2055 attr->values[0].string.text);
2056 return;
2057 }
2058 else if (!strcmp(attr->name, "notify-natural-language") &&
2059 (attr->value_tag != IPP_TAG_LANGUAGE ||
2060 strcmp(attr->values[0].string.text, DefaultLanguage)))
2061 {
2062 send_ipp_status(con, IPP_CHARSET,
2063 _("Language \"%s\" not supported."),
2064 attr->values[0].string.text);
2065 return;
2066 }
2067 else if (!strcmp(attr->name, "notify-user-data") &&
2068 attr->value_tag == IPP_TAG_STRING)
2069 {
2070 if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
2071 {
2072 send_ipp_status(con, IPP_REQUEST_VALUE,
2073 _("The notify-user-data value is too large "
2074 "(%d > 63 octets)."),
2075 attr->values[0].unknown.length);
2076 return;
2077 }
2078
2079 user_data = attr;
2080 }
2081 else if (!strcmp(attr->name, "notify-events") &&
2082 attr->value_tag == IPP_TAG_KEYWORD)
2083 {
2084 for (i = 0; i < attr->num_values; i ++)
2085 mask |= cupsdEventValue(attr->values[i].string.text);
2086 }
2087 else if (!strcmp(attr->name, "notify-lease-duration"))
2088 {
2089 send_ipp_status(con, IPP_BAD_REQUEST,
2090 _("The notify-lease-duration attribute cannot be "
2091 "used with job subscriptions."));
2092 return;
2093 }
2094 else if (!strcmp(attr->name, "notify-time-interval") &&
2095 attr->value_tag == IPP_TAG_INTEGER)
2096 interval = attr->values[0].integer;
2097
2098 attr = attr->next;
2099 }
2100
2101 if (!recipient && !pullmethod)
2102 break;
2103
2104 if (mask == CUPSD_EVENT_NONE)
2105 mask = CUPSD_EVENT_JOB_COMPLETED;
2106
2107 if ((sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job,
2108 recipient, 0)) != NULL)
2109 {
2110 sub->interval = interval;
2111
2112 cupsdSetString(&sub->owner, job->username);
2113
2114 if (user_data)
2115 {
2116 sub->user_data_len = user_data->values[0].unknown.length;
2117 memcpy(sub->user_data, user_data->values[0].unknown.data,
2118 sub->user_data_len);
2119 }
2120
2121 ippAddSeparator(con->response);
2122 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
2123 "notify-subscription-id", sub->id);
2124
2125 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for job %d",
2126 sub->id, job->id);
2127 }
2128
2129 if (attr)
2130 attr = attr->next;
2131 }
2132
2133 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
2134
2135 /*
2136 * Remove all of the subscription attributes from the job request...
2137 *
2138 * TODO: Optimize this since subscription groups have to come at the
2139 * end of the request...
2140 */
2141
2142 for (attr = job->attrs->attrs, prev = NULL; attr; attr = next)
2143 {
2144 next = attr->next;
2145
2146 if (attr->group_tag == IPP_TAG_SUBSCRIPTION ||
2147 attr->group_tag == IPP_TAG_ZERO)
2148 {
2149 /*
2150 * Free and remove this attribute...
2151 */
2152
2153 ippDeleteAttribute(NULL, attr);
2154
2155 if (prev)
2156 prev->next = next;
2157 else
2158 job->attrs->attrs = next;
2159 }
2160 else
2161 prev = attr;
2162 }
2163
2164 job->attrs->last = prev;
2165 job->attrs->current = prev;
2166 }
2167
2168
2169 /*
2170 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2171 *
2172 * See RFC 4122 for the definition of UUIDs and the format.
2173 */
2174
2175 static void
2176 add_job_uuid(cupsd_job_t *job) /* I - Job */
2177 {
2178 char uuid[64]; /* job-uuid string */
2179
2180
2181 /*
2182 * Add a job-uuid attribute if none exists...
2183 */
2184
2185 if (!ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI))
2186 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL,
2187 _httpAssembleUUID(ServerName, RemotePort, job->dest, job->id,
2188 uuid, sizeof(uuid)));
2189 }
2190
2191
2192 /*
2193 * 'add_printer()' - Add a printer to the system.
2194 */
2195
2196 static void
2197 add_printer(cupsd_client_t *con, /* I - Client connection */
2198 ipp_attribute_t *uri) /* I - URI of printer */
2199 {
2200 http_status_t status; /* Policy status */
2201 int i; /* Looping var */
2202 char scheme[HTTP_MAX_URI], /* Method portion of URI */
2203 username[HTTP_MAX_URI], /* Username portion of URI */
2204 host[HTTP_MAX_URI], /* Host portion of URI */
2205 resource[HTTP_MAX_URI]; /* Resource portion of URI */
2206 int port; /* Port portion of URI */
2207 cupsd_printer_t *printer; /* Printer/class */
2208 ipp_attribute_t *attr; /* Printer attribute */
2209 cups_file_t *fp; /* Script/PPD file */
2210 char line[1024]; /* Line from file... */
2211 char srcfile[1024], /* Source Script/PPD file */
2212 dstfile[1024]; /* Destination Script/PPD file */
2213 int modify; /* Non-zero if we are modifying */
2214 int changed_driver, /* Changed the PPD/interface script? */
2215 need_restart_job, /* Need to restart job? */
2216 set_device_uri, /* Did we set the device URI? */
2217 set_port_monitor; /* Did we set the port monitor? */
2218
2219
2220 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con,
2221 con->number, uri->values[0].string.text);
2222
2223 /*
2224 * Do we have a valid URI?
2225 */
2226
2227 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
2228 sizeof(scheme), username, sizeof(username), host,
2229 sizeof(host), &port, resource, sizeof(resource));
2230
2231 if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10)
2232 {
2233 /*
2234 * No, return an error...
2235 */
2236
2237 send_ipp_status(con, IPP_BAD_REQUEST,
2238 _("The printer-uri must be of the form "
2239 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2240 return;
2241 }
2242
2243 /*
2244 * Do we have a valid printer name?
2245 */
2246
2247 if (!validate_name(resource + 10))
2248 {
2249 /*
2250 * No, return an error...
2251 */
2252
2253 send_ipp_status(con, IPP_BAD_REQUEST,
2254 _("The printer-uri \"%s\" contains invalid characters."),
2255 uri->values[0].string.text);
2256 return;
2257 }
2258
2259 /*
2260 * See if the printer already exists; if not, create a new printer...
2261 */
2262
2263 if ((printer = cupsdFindPrinter(resource + 10)) == NULL)
2264 {
2265 /*
2266 * Printer doesn't exist; see if we have a class of the same name...
2267 */
2268
2269 if ((printer = cupsdFindClass(resource + 10)) != NULL)
2270 {
2271 /*
2272 * Yes, return an error...
2273 */
2274
2275 send_ipp_status(con, IPP_NOT_POSSIBLE,
2276 _("A class named \"%s\" already exists."),
2277 resource + 10);
2278 return;
2279 }
2280
2281 /*
2282 * No, check the default policy then add the printer...
2283 */
2284
2285 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
2286 {
2287 send_http_error(con, status, NULL);
2288 return;
2289 }
2290
2291 printer = cupsdAddPrinter(resource + 10);
2292 modify = 0;
2293 }
2294 else if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
2295 NULL)) != HTTP_OK)
2296 {
2297 send_http_error(con, status, printer);
2298 return;
2299 }
2300 else
2301 modify = 1;
2302
2303 /*
2304 * Look for attributes and copy them over as needed...
2305 */
2306
2307 changed_driver = 0;
2308 need_restart_job = 0;
2309
2310 if ((attr = ippFindAttribute(con->request, "printer-location",
2311 IPP_TAG_TEXT)) != NULL)
2312 cupsdSetString(&printer->location, attr->values[0].string.text);
2313
2314 if ((attr = ippFindAttribute(con->request, "printer-info",
2315 IPP_TAG_TEXT)) != NULL)
2316 cupsdSetString(&printer->info, attr->values[0].string.text);
2317
2318 set_device_uri = 0;
2319
2320 if ((attr = ippFindAttribute(con->request, "device-uri",
2321 IPP_TAG_URI)) != NULL)
2322 {
2323 /*
2324 * Do we have a valid device URI?
2325 */
2326
2327 http_uri_status_t uri_status; /* URI separation status */
2328 char old_device_uri[1024];
2329 /* Old device URI */
2330 static const char * const uri_status_strings[] =
2331 {
2332 "URI too large.",
2333 "Bad arguments to function.",
2334 "Bad resource path.",
2335 "Bad port number.",
2336 "Bad hostname/address.",
2337 "Bad username/password.",
2338 "Bad URI scheme.",
2339 "Bad URI.",
2340 "OK",
2341 "Missing URI scheme.",
2342 "Unknown URI scheme",
2343 "Missing resource path."
2344 };
2345
2346
2347 need_restart_job = 1;
2348
2349 uri_status = httpSeparateURI(HTTP_URI_CODING_ALL,
2350 attr->values[0].string.text,
2351 scheme, sizeof(scheme),
2352 username, sizeof(username),
2353 host, sizeof(host), &port,
2354 resource, sizeof(resource));
2355
2356 cupsdLogMessage(CUPSD_LOG_DEBUG,
2357 "%s device-uri: %s", printer->name,
2358 uri_status_strings[uri_status - HTTP_URI_STATUS_OVERFLOW]);
2359
2360 if (uri_status < HTTP_URI_OK)
2361 {
2362 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"."),
2363 attr->values[0].string.text);
2364 return;
2365 }
2366
2367 if (!strcmp(scheme, "file"))
2368 {
2369 /*
2370 * See if the administrator has enabled file devices...
2371 */
2372
2373 if (!FileDevice && strcmp(resource, "/dev/null"))
2374 {
2375 /*
2376 * File devices are disabled and the URL is not file:/dev/null...
2377 */
2378
2379 send_ipp_status(con, IPP_NOT_POSSIBLE,
2380 _("File device URIs have been disabled. "
2381 "To enable, see the FileDevice directive in "
2382 "\"%s/cups-files.conf\"."),
2383 ServerRoot);
2384 return;
2385 }
2386 }
2387 else
2388 {
2389 /*
2390 * See if the backend exists and is executable...
2391 */
2392
2393 snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, scheme);
2394 if (access(srcfile, X_OK))
2395 {
2396 /*
2397 * Could not find device in list!
2398 */
2399
2400 send_ipp_status(con, IPP_NOT_POSSIBLE,
2401 _("Bad device-uri scheme \"%s\"."), scheme);
2402 return;
2403 }
2404 }
2405
2406 if (printer->sanitized_device_uri)
2407 strlcpy(old_device_uri, printer->sanitized_device_uri,
2408 sizeof(old_device_uri));
2409 else
2410 old_device_uri[0] = '\0';
2411
2412 cupsdSetDeviceURI(printer, attr->values[0].string.text);
2413
2414 cupsdLogMessage(CUPSD_LOG_INFO,
2415 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2416 printer->name, printer->sanitized_device_uri,
2417 old_device_uri);
2418
2419 set_device_uri = 1;
2420 }
2421
2422 set_port_monitor = 0;
2423
2424 if ((attr = ippFindAttribute(con->request, "port-monitor",
2425 IPP_TAG_NAME)) != NULL)
2426 {
2427 ipp_attribute_t *supported; /* port-monitor-supported attribute */
2428
2429
2430 need_restart_job = 1;
2431
2432 supported = ippFindAttribute(printer->ppd_attrs, "port-monitor-supported",
2433 IPP_TAG_NAME);
2434 if (supported)
2435 {
2436 for (i = 0; i < supported->num_values; i ++)
2437 if (!strcmp(supported->values[i].string.text,
2438 attr->values[0].string.text))
2439 break;
2440 }
2441
2442 if (!supported || i >= supported->num_values)
2443 {
2444 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"."),
2445 attr->values[0].string.text);
2446 return;
2447 }
2448
2449 cupsdLogMessage(CUPSD_LOG_INFO,
2450 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2451 printer->name, attr->values[0].string.text,
2452 printer->port_monitor ? printer->port_monitor : "none");
2453
2454 if (strcmp(attr->values[0].string.text, "none"))
2455 cupsdSetString(&printer->port_monitor, attr->values[0].string.text);
2456 else
2457 cupsdClearString(&printer->port_monitor);
2458
2459 set_port_monitor = 1;
2460 }
2461
2462 if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs",
2463 IPP_TAG_BOOLEAN)) != NULL &&
2464 attr->values[0].boolean != printer->accepting)
2465 {
2466 cupsdLogMessage(CUPSD_LOG_INFO,
2467 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2468 printer->name, attr->values[0].boolean, printer->accepting);
2469
2470 printer->accepting = attr->values[0].boolean;
2471
2472 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
2473 "%s accepting jobs.",
2474 printer->accepting ? "Now" : "No longer");
2475 }
2476
2477 if ((attr = ippFindAttribute(con->request, "printer-is-shared",
2478 IPP_TAG_BOOLEAN)) != NULL)
2479 {
2480 if (attr->values[0].boolean &&
2481 printer->num_auth_info_required == 1 &&
2482 !strcmp(printer->auth_info_required[0], "negotiate"))
2483 {
2484 send_ipp_status(con, IPP_BAD_REQUEST,
2485 _("Cannot share a remote Kerberized printer."));
2486 return;
2487 }
2488
2489 if (printer->shared && !attr->values[0].boolean)
2490 cupsdDeregisterPrinter(printer, 1);
2491
2492 cupsdLogMessage(CUPSD_LOG_INFO,
2493 "Setting %s printer-is-shared to %d (was %d.)",
2494 printer->name, attr->values[0].boolean, printer->shared);
2495
2496 printer->shared = attr->values[0].boolean;
2497 }
2498
2499 if ((attr = ippFindAttribute(con->request, "printer-state",
2500 IPP_TAG_ENUM)) != NULL)
2501 {
2502 if (attr->values[0].integer != IPP_PRINTER_IDLE &&
2503 attr->values[0].integer != IPP_PRINTER_STOPPED)
2504 {
2505 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d."),
2506 attr->values[0].integer);
2507 return;
2508 }
2509
2510 cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
2511 printer->name, attr->values[0].integer, printer->state);
2512
2513 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
2514 cupsdStopPrinter(printer, 0);
2515 else
2516 {
2517 need_restart_job = 1;
2518 cupsdSetPrinterState(printer, (ipp_pstate_t)(attr->values[0].integer), 0);
2519 }
2520 }
2521
2522 if ((attr = ippFindAttribute(con->request, "printer-state-message",
2523 IPP_TAG_TEXT)) != NULL)
2524 {
2525 strlcpy(printer->state_message, attr->values[0].string.text,
2526 sizeof(printer->state_message));
2527
2528 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL, "%s",
2529 printer->state_message);
2530 }
2531
2532 if ((attr = ippFindAttribute(con->request, "printer-state-reasons",
2533 IPP_TAG_KEYWORD)) != NULL)
2534 {
2535 if (attr->num_values >
2536 (int)(sizeof(printer->reasons) / sizeof(printer->reasons[0])))
2537 {
2538 send_ipp_status(con, IPP_NOT_POSSIBLE,
2539 _("Too many printer-state-reasons values (%d > %d)."),
2540 attr->num_values,
2541 (int)(sizeof(printer->reasons) /
2542 sizeof(printer->reasons[0])));
2543 return;
2544 }
2545
2546 for (i = 0; i < printer->num_reasons; i ++)
2547 _cupsStrFree(printer->reasons[i]);
2548
2549 printer->num_reasons = 0;
2550 for (i = 0; i < attr->num_values; i ++)
2551 {
2552 if (!strcmp(attr->values[i].string.text, "none"))
2553 continue;
2554
2555 printer->reasons[printer->num_reasons] =
2556 _cupsStrRetain(attr->values[i].string.text);
2557 printer->num_reasons ++;
2558
2559 if (!strcmp(attr->values[i].string.text, "paused") &&
2560 printer->state != IPP_PRINTER_STOPPED)
2561 {
2562 cupsdLogMessage(CUPSD_LOG_INFO,
2563 "Setting %s printer-state to %d (was %d.)",
2564 printer->name, IPP_PRINTER_STOPPED, printer->state);
2565 cupsdStopPrinter(printer, 0);
2566 }
2567 }
2568
2569 if (PrintcapFormat == PRINTCAP_PLIST)
2570 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
2571
2572 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
2573 "Printer \"%s\" state changed.", printer->name);
2574 }
2575
2576 set_printer_defaults(con, printer);
2577
2578 if ((attr = ippFindAttribute(con->request, "auth-info-required",
2579 IPP_TAG_KEYWORD)) != NULL)
2580 cupsdSetAuthInfoRequired(printer, NULL, attr);
2581
2582 /*
2583 * See if we have all required attributes...
2584 */
2585
2586 if (!printer->device_uri)
2587 cupsdSetString(&printer->device_uri, "file:///dev/null");
2588
2589 /*
2590 * See if we have an interface script or PPD file attached to the request...
2591 */
2592
2593 if (con->filename)
2594 {
2595 need_restart_job = 1;
2596 changed_driver = 1;
2597
2598 strlcpy(srcfile, con->filename, sizeof(srcfile));
2599
2600 if ((fp = cupsFileOpen(srcfile, "rb")))
2601 {
2602 /*
2603 * Yes; get the first line from it...
2604 */
2605
2606 line[0] = '\0';
2607 cupsFileGets(fp, line, sizeof(line));
2608 cupsFileClose(fp);
2609
2610 /*
2611 * Then see what kind of file it is...
2612 */
2613
2614 snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
2615 printer->name);
2616
2617 if (!strncmp(line, "*PPD-Adobe", 10))
2618 {
2619 /*
2620 * The new file is a PPD file, so remove any old interface script
2621 * that might be lying around...
2622 */
2623
2624 unlink(dstfile);
2625 }
2626 else
2627 {
2628 /*
2629 * This must be an interface script, so move the file over to the
2630 * interfaces directory and make it executable...
2631 */
2632
2633 if (copy_file(srcfile, dstfile))
2634 {
2635 send_ipp_status(con, IPP_INTERNAL_ERROR,
2636 _("Unable to copy interface script - %s"),
2637 strerror(errno));
2638 return;
2639 }
2640
2641 cupsdLogMessage(CUPSD_LOG_DEBUG,
2642 "Copied interface script successfully");
2643 chmod(dstfile, 0755);
2644 }
2645
2646 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2647 printer->name);
2648
2649 if (!strncmp(line, "*PPD-Adobe", 10))
2650 {
2651 /*
2652 * The new file is a PPD file, so move the file over to the
2653 * ppd directory and make it readable by all...
2654 */
2655
2656 if (copy_file(srcfile, dstfile))
2657 {
2658 send_ipp_status(con, IPP_INTERNAL_ERROR,
2659 _("Unable to copy PPD file - %s"),
2660 strerror(errno));
2661 return;
2662 }
2663
2664 cupsdLogMessage(CUPSD_LOG_DEBUG,
2665 "Copied PPD file successfully");
2666 chmod(dstfile, 0644);
2667 }
2668 else
2669 {
2670 /*
2671 * This must be an interface script, so remove any old PPD file that
2672 * may be lying around...
2673 */
2674
2675 unlink(dstfile);
2676 }
2677 }
2678 }
2679 else if ((attr = ippFindAttribute(con->request, "ppd-name",
2680 IPP_TAG_NAME)) != NULL)
2681 {
2682 need_restart_job = 1;
2683 changed_driver = 1;
2684
2685 if (!strcmp(attr->values[0].string.text, "raw"))
2686 {
2687 /*
2688 * Raw driver, remove any existing PPD or interface script files.
2689 */
2690
2691 snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
2692 printer->name);
2693 unlink(dstfile);
2694
2695 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2696 printer->name);
2697 unlink(dstfile);
2698 }
2699 else
2700 {
2701 /*
2702 * PPD model file...
2703 */
2704
2705 snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
2706 printer->name);
2707 unlink(dstfile);
2708
2709 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2710 printer->name);
2711
2712 if (copy_model(con, attr->values[0].string.text, dstfile))
2713 {
2714 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file."));
2715 return;
2716 }
2717
2718 cupsdLogMessage(CUPSD_LOG_DEBUG,
2719 "Copied PPD file successfully");
2720 chmod(dstfile, 0644);
2721 }
2722 }
2723
2724 if (changed_driver)
2725 {
2726 /*
2727 * If we changed the PPD/interface script, then remove the printer's cache
2728 * file and clear the printer-state-reasons...
2729 */
2730
2731 char cache_name[1024]; /* Cache filename for printer attrs */
2732
2733 snprintf(cache_name, sizeof(cache_name), "%s/%s.data", CacheDir,
2734 printer->name);
2735 unlink(cache_name);
2736
2737 cupsdSetPrinterReasons(printer, "none");
2738
2739 /*
2740 * (Re)register color profiles...
2741 */
2742
2743 cupsdRegisterColor(printer);
2744 }
2745
2746 /*
2747 * If we set the device URI but not the port monitor, check which port
2748 * monitor to use by default...
2749 */
2750
2751 if (set_device_uri && !set_port_monitor)
2752 {
2753 ppd_file_t *ppd; /* PPD file */
2754 ppd_attr_t *ppdattr; /* cupsPortMonitor attribute */
2755
2756
2757 httpSeparateURI(HTTP_URI_CODING_ALL, printer->device_uri, scheme,
2758 sizeof(scheme), username, sizeof(username), host,
2759 sizeof(host), &port, resource, sizeof(resource));
2760
2761 snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot,
2762 printer->name);
2763 if ((ppd = _ppdOpenFile(srcfile, _PPD_LOCALIZATION_NONE)) != NULL)
2764 {
2765 for (ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2766 ppdattr;
2767 ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
2768 if (!strcmp(scheme, ppdattr->spec))
2769 {
2770 cupsdLogMessage(CUPSD_LOG_INFO,
2771 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2772 printer->name, ppdattr->value,
2773 printer->port_monitor ? printer->port_monitor
2774 : "none");
2775
2776 if (strcmp(ppdattr->value, "none"))
2777 cupsdSetString(&printer->port_monitor, ppdattr->value);
2778 else
2779 cupsdClearString(&printer->port_monitor);
2780
2781 break;
2782 }
2783
2784 ppdClose(ppd);
2785 }
2786 }
2787
2788 /*
2789 * Update the printer attributes and return...
2790 */
2791
2792 cupsdSetPrinterAttrs(printer);
2793 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
2794
2795 if (need_restart_job && printer->job)
2796 {
2797 /*
2798 * Restart the current job...
2799 */
2800
2801 cupsdSetJobState(printer->job, IPP_JOB_PENDING, CUPSD_JOB_FORCE,
2802 "Job restarted because the printer was modified.");
2803 }
2804
2805 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
2806
2807 if (modify)
2808 {
2809 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED,
2810 printer, NULL, "Printer \"%s\" modified by \"%s\".",
2811 printer->name, get_username(con));
2812
2813 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".",
2814 printer->name, get_username(con));
2815 }
2816 else
2817 {
2818 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED,
2819 printer, NULL, "New printer \"%s\" added by \"%s\".",
2820 printer->name, get_username(con));
2821
2822 cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".",
2823 printer->name, get_username(con));
2824 }
2825
2826 con->response->request.status.status_code = IPP_OK;
2827 }
2828
2829
2830 /*
2831 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2832 * based upon the printer state...
2833 */
2834
2835 static void
2836 add_printer_state_reasons(
2837 cupsd_client_t *con, /* I - Client connection */
2838 cupsd_printer_t *p) /* I - Printer info */
2839 {
2840 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2841 "add_printer_state_reasons(%p[%d], %p[%s])",
2842 con, con->number, p, p->name);
2843
2844 if (p->num_reasons == 0)
2845 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2846 "printer-state-reasons", NULL, "none");
2847 else
2848 ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2849 "printer-state-reasons", p->num_reasons, NULL,
2850 (const char * const *)p->reasons);
2851 }
2852
2853
2854 /*
2855 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2856 * the specified printer or class.
2857 */
2858
2859 static void
2860 add_queued_job_count(
2861 cupsd_client_t *con, /* I - Client connection */
2862 cupsd_printer_t *p) /* I - Printer or class */
2863 {
2864 int count; /* Number of jobs on destination */
2865
2866
2867 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])",
2868 con, con->number, p, p->name);
2869
2870 count = cupsdGetPrinterJobCount(p->name);
2871
2872 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2873 "queued-job-count", count);
2874 }
2875
2876
2877 /*
2878 * 'apply_printer_defaults()' - Apply printer default options to a job.
2879 */
2880
2881 static void
2882 apply_printer_defaults(
2883 cupsd_printer_t *printer, /* I - Printer */
2884 cupsd_job_t *job) /* I - Job */
2885 {
2886 int i, /* Looping var */
2887 num_options; /* Number of default options */
2888 cups_option_t *options, /* Default options */
2889 *option; /* Current option */
2890
2891
2892 /*
2893 * Collect all of the default options and add the missing ones to the
2894 * job object...
2895 */
2896
2897 for (i = printer->num_options, num_options = 0, options = NULL,
2898 option = printer->options;
2899 i > 0;
2900 i --, option ++)
2901 if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO))
2902 {
2903 num_options = cupsAddOption(option->name, option->value, num_options,
2904 &options);
2905 }
2906
2907 /*
2908 * Encode these options as attributes in the job object...
2909 */
2910
2911 cupsEncodeOptions2(job->attrs, num_options, options, IPP_TAG_JOB);
2912 cupsFreeOptions(num_options, options);
2913 }
2914
2915
2916 /*
2917 * 'authenticate_job()' - Set job authentication info.
2918 */
2919
2920 static void
2921 authenticate_job(cupsd_client_t *con, /* I - Client connection */
2922 ipp_attribute_t *uri) /* I - Job URI */
2923 {
2924 ipp_attribute_t *attr, /* job-id attribute */
2925 *auth_info; /* auth-info attribute */
2926 int jobid; /* Job ID */
2927 cupsd_job_t *job; /* Current job */
2928 char scheme[HTTP_MAX_URI],
2929 /* Method portion of URI */
2930 username[HTTP_MAX_URI],
2931 /* Username portion of URI */
2932 host[HTTP_MAX_URI],
2933 /* Host portion of URI */
2934 resource[HTTP_MAX_URI];
2935 /* Resource portion of URI */
2936 int port; /* Port portion of URI */
2937
2938
2939 cupsdLogMessage(CUPSD_LOG_DEBUG2, "authenticate_job(%p[%d], %s)",
2940 con, con->number, uri->values[0].string.text);
2941
2942 /*
2943 * Start with "everything is OK" status...
2944 */
2945
2946 con->response->request.status.status_code = IPP_OK;
2947
2948 /*
2949 * See if we have a job URI or a printer URI...
2950 */
2951
2952 if (!strcmp(uri->name, "printer-uri"))
2953 {
2954 /*
2955 * Got a printer URI; see if we also have a job-id attribute...
2956 */
2957
2958 if ((attr = ippFindAttribute(con->request, "job-id",
2959 IPP_TAG_INTEGER)) == NULL)
2960 {
2961 send_ipp_status(con, IPP_BAD_REQUEST,
2962 _("Got a printer-uri attribute but no job-id."));
2963 return;
2964 }
2965
2966 jobid = attr->values[0].integer;
2967 }
2968 else
2969 {
2970 /*
2971 * Got a job URI; parse it to get the job ID...
2972 */
2973
2974 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
2975 sizeof(scheme), username, sizeof(username), host,
2976 sizeof(host), &port, resource, sizeof(resource));
2977
2978 if (strncmp(resource, "/jobs/", 6))
2979 {
2980 /*
2981 * Not a valid URI!
2982 */
2983
2984 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
2985 uri->values[0].string.text);
2986 return;
2987 }
2988
2989 jobid = atoi(resource + 6);
2990 }
2991
2992 /*
2993 * See if the job exists...
2994 */
2995
2996 if ((job = cupsdFindJob(jobid)) == NULL)
2997 {
2998 /*
2999 * Nope - return a "not found" error...
3000 */
3001
3002 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
3003 return;
3004 }
3005
3006 /*
3007 * See if the job has been completed...
3008 */
3009
3010 if (job->state_value != IPP_JOB_HELD)
3011 {
3012 /*
3013 * Return a "not-possible" error...
3014 */
3015
3016 send_ipp_status(con, IPP_NOT_POSSIBLE,
3017 _("Job #%d is not held for authentication."),
3018 jobid);
3019 return;
3020 }
3021
3022 /*
3023 * See if we have already authenticated...
3024 */
3025
3026 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
3027
3028 if (!con->username[0] && !auth_info)
3029 {
3030 cupsd_printer_t *printer; /* Job destination */
3031
3032 /*
3033 * No auth data. If we need to authenticate via Kerberos, send a
3034 * HTTP auth challenge, otherwise just return an IPP error...
3035 */
3036
3037 printer = cupsdFindDest(job->dest);
3038
3039 if (printer && printer->num_auth_info_required > 0 &&
3040 !strcmp(printer->auth_info_required[0], "negotiate"))
3041 send_http_error(con, HTTP_UNAUTHORIZED, printer);
3042 else
3043 send_ipp_status(con, IPP_NOT_AUTHORIZED,
3044 _("No authentication information provided."));
3045 return;
3046 }
3047
3048 /*
3049 * See if the job is owned by the requesting user...
3050 */
3051
3052 if (!validate_user(job, con, job->username, username, sizeof(username)))
3053 {
3054 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
3055 cupsdFindDest(job->dest));
3056 return;
3057 }
3058
3059 /*
3060 * Save the authentication information for this job...
3061 */
3062
3063 save_auth_info(con, job, auth_info);
3064
3065 /*
3066 * Reset the job-hold-until value to "no-hold"...
3067 */
3068
3069 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
3070 IPP_TAG_KEYWORD)) == NULL)
3071 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
3072
3073 if (attr)
3074 {
3075 attr->value_tag = IPP_TAG_KEYWORD;
3076 cupsdSetString(&(attr->values[0].string.text), "no-hold");
3077 }
3078
3079 /*
3080 * Release the job and return...
3081 */
3082
3083 cupsdReleaseJob(job);
3084
3085 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, NULL, job, "Job authenticated by user");
3086
3087 cupsdLogJob(job, CUPSD_LOG_INFO, "Authenticated by \"%s\".", con->username);
3088
3089 cupsdCheckJobs();
3090 }
3091
3092
3093 /*
3094 * 'cancel_all_jobs()' - Cancel all or selected print jobs.
3095 */
3096
3097 static void
3098 cancel_all_jobs(cupsd_client_t *con, /* I - Client connection */
3099 ipp_attribute_t *uri) /* I - Job or Printer URI */
3100 {
3101 int i; /* Looping var */
3102 http_status_t status; /* Policy status */
3103 cups_ptype_t dtype; /* Destination type */
3104 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
3105 userpass[HTTP_MAX_URI], /* Username portion of URI */
3106 hostname[HTTP_MAX_URI], /* Host portion of URI */
3107 resource[HTTP_MAX_URI]; /* Resource portion of URI */
3108 int port; /* Port portion of URI */
3109 ipp_attribute_t *attr; /* Attribute in request */
3110 const char *username = NULL; /* Username */
3111 cupsd_jobaction_t purge = CUPSD_JOB_DEFAULT;
3112 /* Purge? */
3113 cupsd_printer_t *printer; /* Printer */
3114 ipp_attribute_t *job_ids; /* job-ids attribute */
3115 cupsd_job_t *job; /* Job */
3116
3117
3118 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_all_jobs(%p[%d], %s)", con,
3119 con->number, uri->values[0].string.text);
3120
3121 /*
3122 * Get the jobs to cancel/purge...
3123 */
3124
3125 switch (con->request->request.op.operation_id)
3126 {
3127 case IPP_PURGE_JOBS :
3128 /*
3129 * Get the username (if any) for the jobs we want to cancel (only if
3130 * "my-jobs" is specified...
3131 */
3132
3133 if ((attr = ippFindAttribute(con->request, "my-jobs",
3134 IPP_TAG_BOOLEAN)) != NULL &&
3135 attr->values[0].boolean)
3136 {
3137 if ((attr = ippFindAttribute(con->request, "requesting-user-name",
3138 IPP_TAG_NAME)) != NULL)
3139 username = attr->values[0].string.text;
3140 else
3141 {
3142 send_ipp_status(con, IPP_BAD_REQUEST,
3143 _("Missing requesting-user-name attribute."));
3144 return;
3145 }
3146 }
3147
3148 /*
3149 * Look for the "purge-jobs" attribute...
3150 */
3151
3152 if ((attr = ippFindAttribute(con->request, "purge-jobs",
3153 IPP_TAG_BOOLEAN)) != NULL)
3154 purge = attr->values[0].boolean ? CUPSD_JOB_PURGE : CUPSD_JOB_DEFAULT;
3155 else
3156 purge = CUPSD_JOB_PURGE;
3157 break;
3158
3159 case IPP_CANCEL_MY_JOBS :
3160 if (con->username[0])
3161 username = con->username;
3162 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
3163 IPP_TAG_NAME)) != NULL)
3164 username = attr->values[0].string.text;
3165 else
3166 {
3167 send_ipp_status(con, IPP_BAD_REQUEST,
3168 _("Missing requesting-user-name attribute."));
3169 return;
3170 }
3171
3172 default :
3173 break;
3174 }
3175
3176 job_ids = ippFindAttribute(con->request, "job-ids", IPP_TAG_INTEGER);
3177
3178 /*
3179 * See if we have a printer URI...
3180 */
3181
3182 if (strcmp(uri->name, "printer-uri"))
3183 {
3184 send_ipp_status(con, IPP_BAD_REQUEST,
3185 _("The printer-uri attribute is required."));
3186 return;
3187 }
3188
3189 /*
3190 * And if the destination is valid...
3191 */
3192
3193 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
3194 {
3195 /*
3196 * Bad URI?
3197 */
3198
3199 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text,
3200 scheme, sizeof(scheme), userpass, sizeof(userpass),
3201 hostname, sizeof(hostname), &port,
3202 resource, sizeof(resource));
3203
3204 if ((!strncmp(resource, "/printers/", 10) && resource[10]) ||
3205 (!strncmp(resource, "/classes/", 9) && resource[9]))
3206 {
3207 send_ipp_status(con, IPP_NOT_FOUND,
3208 _("The printer or class does not exist."));
3209 return;
3210 }
3211
3212 /*
3213 * Check policy...
3214 */
3215
3216 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
3217 {
3218 send_http_error(con, status, NULL);
3219 return;
3220 }
3221
3222 if (job_ids)
3223 {
3224 for (i = 0; i < job_ids->num_values; i ++)
3225 {
3226 if ((job = cupsdFindJob(job_ids->values[i].integer)) == NULL)
3227 break;
3228
3229 if (con->request->request.op.operation_id == IPP_CANCEL_MY_JOBS &&
3230 _cups_strcasecmp(job->username, username))
3231 break;
3232 }
3233
3234 if (i < job_ids->num_values)
3235 {
3236 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
3237 job_ids->values[i].integer);
3238 return;
3239 }
3240
3241 for (i = 0; i < job_ids->num_values; i ++)
3242 {
3243 job = cupsdFindJob(job_ids->values[i].integer);
3244
3245 cupsdSetJobState(job, IPP_JOB_CANCELED, purge,
3246 purge == CUPSD_JOB_PURGE ? "Job purged by user." :
3247 "Job canceled by user.");
3248 }
3249
3250 cupsdLogMessage(CUPSD_LOG_INFO, "Selected jobs were %s by \"%s\".",
3251 purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
3252 get_username(con));
3253 }
3254 else
3255 {
3256 /*
3257 * Cancel all jobs on all printers...
3258 */
3259
3260 cupsdCancelJobs(NULL, username, purge);
3261
3262 cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".",
3263 purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
3264 get_username(con));
3265 }
3266 }
3267 else
3268 {
3269 /*
3270 * Check policy...
3271 */
3272
3273 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
3274 NULL)) != HTTP_OK)
3275 {
3276 send_http_error(con, status, printer);
3277 return;
3278 }
3279
3280 if (job_ids)
3281 {
3282 for (i = 0; i < job_ids->num_values; i ++)
3283 {
3284 if ((job = cupsdFindJob(job_ids->values[i].integer)) == NULL ||
3285 _cups_strcasecmp(job->dest, printer->name))
3286 break;
3287
3288 if (con->request->request.op.operation_id == IPP_CANCEL_MY_JOBS &&
3289 _cups_strcasecmp(job->username, username))
3290 break;
3291 }
3292
3293 if (i < job_ids->num_values)
3294 {
3295 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
3296 job_ids->values[i].integer);
3297 return;
3298 }
3299
3300 for (i = 0; i < job_ids->num_values; i ++)
3301 {
3302 job = cupsdFindJob(job_ids->values[i].integer);
3303
3304 cupsdSetJobState(job, IPP_JOB_CANCELED, purge,
3305 purge == CUPSD_JOB_PURGE ? "Job purged by user." :
3306 "Job canceled by user.");
3307 }
3308
3309 cupsdLogMessage(CUPSD_LOG_INFO, "Selected jobs were %s by \"%s\".",
3310 purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
3311 get_username(con));
3312 }
3313 else
3314 {
3315 /*
3316 * Cancel all of the jobs on the named printer...
3317 */
3318
3319 cupsdCancelJobs(printer->name, username, purge);
3320
3321 cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".",
3322 printer->name,
3323 purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
3324 get_username(con));
3325 }
3326 }
3327
3328 con->response->request.status.status_code = IPP_OK;
3329 }
3330
3331
3332 /*
3333 * 'cancel_job()' - Cancel a print job.
3334 */
3335
3336 static void
3337 cancel_job(cupsd_client_t *con, /* I - Client connection */
3338 ipp_attribute_t *uri) /* I - Job or Printer URI */
3339 {
3340 ipp_attribute_t *attr; /* Current attribute */
3341 int jobid; /* Job ID */
3342 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
3343 username[HTTP_MAX_URI], /* Username portion of URI */
3344 host[HTTP_MAX_URI], /* Host portion of URI */
3345 resource[HTTP_MAX_URI]; /* Resource portion of URI */
3346 int port; /* Port portion of URI */
3347 cupsd_job_t *job; /* Job information */
3348 cups_ptype_t dtype; /* Destination type (printer/class) */
3349 cupsd_printer_t *printer; /* Printer data */
3350 cupsd_jobaction_t purge; /* Purge the job? */
3351
3352
3353 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con,
3354 con->number, uri->values[0].string.text);
3355
3356 /*
3357 * See if we have a job URI or a printer URI...
3358 */
3359
3360 if (!strcmp(uri->name, "printer-uri"))
3361 {
3362 /*
3363 * Got a printer URI; see if we also have a job-id attribute...
3364 */
3365
3366 if ((attr = ippFindAttribute(con->request, "job-id",
3367 IPP_TAG_INTEGER)) == NULL)
3368 {
3369 send_ipp_status(con, IPP_BAD_REQUEST,
3370 _("Got a printer-uri attribute but no job-id."));
3371 return;
3372 }
3373
3374 if ((jobid = attr->values[0].integer) == 0)
3375 {
3376 /*
3377 * Find the current job on the specified printer...
3378 */
3379
3380 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
3381 {
3382 /*
3383 * Bad URI...
3384 */
3385
3386 send_ipp_status(con, IPP_NOT_FOUND,
3387 _("The printer or class does not exist."));
3388 return;
3389 }
3390
3391 /*
3392 * See if there are any pending jobs...
3393 */
3394
3395 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
3396 job;
3397 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
3398 if (job->state_value <= IPP_JOB_PROCESSING &&
3399 !_cups_strcasecmp(job->dest, printer->name))
3400 break;
3401
3402 if (job)
3403 jobid = job->id;
3404 else
3405 {
3406 /*
3407 * No, try stopped jobs...
3408 */
3409
3410 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
3411 job;
3412 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
3413 if (job->state_value == IPP_JOB_STOPPED &&
3414 !_cups_strcasecmp(job->dest, printer->name))
3415 break;
3416
3417 if (job)
3418 jobid = job->id;
3419 else
3420 {
3421 send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s."),
3422 printer->name);
3423 return;
3424 }
3425 }
3426 }
3427 }
3428 else
3429 {
3430 /*
3431 * Got a job URI; parse it to get the job ID...
3432 */
3433
3434 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
3435 sizeof(scheme), username, sizeof(username), host,
3436 sizeof(host), &port, resource, sizeof(resource));
3437
3438 if (strncmp(resource, "/jobs/", 6))
3439 {
3440 /*
3441 * Not a valid URI!
3442 */
3443
3444 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
3445 uri->values[0].string.text);
3446 return;
3447 }
3448
3449 jobid = atoi(resource + 6);
3450 }
3451
3452 /*
3453 * Look for the "purge-job" attribute...
3454 */
3455
3456 if ((attr = ippFindAttribute(con->request, "purge-job",
3457 IPP_TAG_BOOLEAN)) != NULL)
3458 purge = attr->values[0].boolean ? CUPSD_JOB_PURGE : CUPSD_JOB_DEFAULT;
3459 else
3460 purge = CUPSD_JOB_DEFAULT;
3461
3462 /*
3463 * See if the job exists...
3464 */
3465
3466 if ((job = cupsdFindJob(jobid)) == NULL)
3467 {
3468 /*
3469 * Nope - return a "not found" error...
3470 */
3471
3472 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
3473 return;
3474 }
3475
3476 /*
3477 * See if the job is owned by the requesting user...
3478 */
3479
3480 if (!validate_user(job, con, job->username, username, sizeof(username)))
3481 {
3482 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
3483 cupsdFindDest(job->dest));
3484 return;
3485 }
3486
3487 /*
3488 * See if the job is already completed, canceled, or aborted; if so,
3489 * we can't cancel...
3490 */
3491
3492 if (job->state_value >= IPP_JOB_CANCELED && purge != CUPSD_JOB_PURGE)
3493 {
3494 switch (job->state_value)
3495 {
3496 case IPP_JOB_CANCELED :
3497 send_ipp_status(con, IPP_NOT_POSSIBLE,
3498 _("Job #%d is already canceled - can\'t cancel."),
3499 jobid);
3500 break;
3501
3502 case IPP_JOB_ABORTED :
3503 send_ipp_status(con, IPP_NOT_POSSIBLE,
3504 _("Job #%d is already aborted - can\'t cancel."),
3505 jobid);
3506 break;
3507
3508 default :
3509 send_ipp_status(con, IPP_NOT_POSSIBLE,
3510 _("Job #%d is already completed - can\'t cancel."),
3511 jobid);
3512 break;
3513 }
3514
3515 return;
3516 }
3517
3518 /*
3519 * Cancel the job and return...
3520 */
3521
3522 cupsdSetJobState(job, IPP_JOB_CANCELED, purge,
3523 purge == CUPSD_JOB_PURGE ? "Job purged by \"%s\"" :
3524 "Job canceled by \"%s\"",
3525 username);
3526 cupsdCheckJobs();
3527
3528 if (purge == CUPSD_JOB_PURGE)
3529 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Purged by \"%s\".", jobid,
3530 username);
3531 else
3532 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Canceled by \"%s\".", jobid,
3533 username);
3534
3535 con->response->request.status.status_code = IPP_OK;
3536 }
3537
3538
3539 /*
3540 * 'cancel_subscription()' - Cancel a subscription.
3541 */
3542
3543 static void
3544 cancel_subscription(
3545 cupsd_client_t *con, /* I - Client connection */
3546 int sub_id) /* I - Subscription ID */
3547 {
3548 http_status_t status; /* Policy status */
3549 cupsd_subscription_t *sub; /* Subscription */
3550
3551
3552 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3553 "cancel_subscription(con=%p[%d], sub_id=%d)",
3554 con, con->number, sub_id);
3555
3556 /*
3557 * Is the subscription ID valid?
3558 */
3559
3560 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
3561 {
3562 /*
3563 * Bad subscription ID...
3564 */
3565
3566 send_ipp_status(con, IPP_NOT_FOUND,
3567 _("Subscription #%d does not exist."), sub_id);
3568 return;
3569 }
3570
3571 /*
3572 * Check policy...
3573 */
3574
3575 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
3576 DefaultPolicyPtr,
3577 con, sub->owner)) != HTTP_OK)
3578 {
3579 send_http_error(con, status, sub->dest);
3580 return;
3581 }
3582
3583 /*
3584 * Cancel the subscription...
3585 */
3586
3587 cupsdDeleteSubscription(sub, 1);
3588
3589 con->response->request.status.status_code = IPP_OK;
3590 }
3591
3592
3593 /*
3594 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
3595 */
3596
3597 static int /* O - 1 if OK, 0 if not */
3598 check_rss_recipient(
3599 const char *recipient) /* I - Recipient URI */
3600 {
3601 cupsd_subscription_t *sub; /* Current subscription */
3602
3603
3604 for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions);
3605 sub;
3606 sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
3607 if (sub->recipient)
3608 {
3609 /*
3610 * Compare the URIs up to the first ?...
3611 */
3612
3613 const char *r1, *r2;
3614
3615 for (r1 = recipient, r2 = sub->recipient;
3616 *r1 == *r2 && *r1 && *r1 != '?' && *r2 && *r2 != '?';
3617 r1 ++, r2 ++);
3618
3619 if (*r1 == *r2)
3620 return (0);
3621 }
3622
3623 return (1);
3624 }
3625
3626
3627 /*
3628 * 'check_quotas()' - Check quotas for a printer and user.
3629 */
3630
3631 static int /* O - 1 if OK, 0 if forbidden,
3632 -1 if limit reached */
3633 check_quotas(cupsd_client_t *con, /* I - Client connection */
3634 cupsd_printer_t *p) /* I - Printer or class */
3635 {
3636 char username[33], /* Username */
3637 *name; /* Current user name */
3638 cupsd_quota_t *q; /* Quota data */
3639 #ifdef HAVE_MBR_UID_TO_UUID
3640 /*
3641 * Use Apple membership APIs which require that all names represent
3642 * valid user account or group records accessible by the server.
3643 */
3644
3645 uuid_t usr_uuid; /* UUID for job requesting user */
3646 uuid_t usr2_uuid; /* UUID for ACL user name entry */
3647 uuid_t grp_uuid; /* UUID for ACL group name entry */
3648 int mbr_err; /* Error from membership function */
3649 int is_member; /* Is this user a member? */
3650 #else
3651 /*
3652 * Use standard POSIX APIs for checking users and groups...
3653 */
3654
3655 struct passwd *pw; /* User password data */
3656 #endif /* HAVE_MBR_UID_TO_UUID */
3657
3658
3659 cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])",
3660 con, con->number, p, p->name);
3661
3662 /*
3663 * Figure out who is printing...
3664 */
3665
3666 strlcpy(username, get_username(con), sizeof(username));
3667
3668 if ((name = strchr(username, '@')) != NULL)
3669 *name = '\0'; /* Strip @REALM */
3670
3671 /*
3672 * Check global active job limits for printers and users...
3673 */
3674
3675 if (MaxJobsPerPrinter)
3676 {
3677 /*
3678 * Check if there are too many pending jobs on this printer...
3679 */
3680
3681 if (cupsdGetPrinterJobCount(p->name) >= MaxJobsPerPrinter)
3682 {
3683 cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for printer \"%s\"...",
3684 p->name);
3685 return (-1);
3686 }
3687 }
3688
3689 if (MaxJobsPerUser)
3690 {
3691 /*
3692 * Check if there are too many pending jobs for this user...
3693 */
3694
3695 if (cupsdGetUserJobCount(username) >= MaxJobsPerUser)
3696 {
3697 cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for user \"%s\"...",
3698 username);
3699 return (-1);
3700 }
3701 }
3702
3703 /*
3704 * Check against users...
3705 */
3706
3707 if (cupsArrayCount(p->users) == 0 && p->k_limit == 0 && p->page_limit == 0)
3708 return (1);
3709
3710 if (cupsArrayCount(p->users))
3711 {
3712 #ifdef HAVE_MBR_UID_TO_UUID
3713 /*
3714 * Get UUID for job requesting user...
3715 */
3716
3717 if (mbr_user_name_to_uuid((char *)username, usr_uuid))
3718 {
3719 /*
3720 * Unknown user...
3721 */
3722
3723 cupsdLogMessage(CUPSD_LOG_DEBUG,
3724 "check_quotas: UUID lookup failed for user \"%s\"",
3725 username);
3726 cupsdLogMessage(CUPSD_LOG_INFO,
3727 "Denying user \"%s\" access to printer \"%s\" "
3728 "(unknown user)...",
3729 username, p->name);
3730 return (0);
3731 }
3732 #else
3733 /*
3734 * Get UID and GID of requesting user...
3735 */
3736
3737 pw = getpwnam(username);
3738 endpwent();
3739 #endif /* HAVE_MBR_UID_TO_UUID */
3740
3741 for (name = (char *)cupsArrayFirst(p->users);
3742 name;
3743 name = (char *)cupsArrayNext(p->users))
3744 if (name[0] == '@')
3745 {
3746 /*
3747 * Check group membership...
3748 */
3749
3750 #ifdef HAVE_MBR_UID_TO_UUID
3751 if (name[1] == '#')
3752 {
3753 if (uuid_parse(name + 2, grp_uuid))
3754 uuid_clear(grp_uuid);
3755 }
3756 else if ((mbr_err = mbr_group_name_to_uuid(name + 1, grp_uuid)) != 0)
3757 {
3758 /*
3759 * Invalid ACL entries are ignored for matching; just record a
3760 * warning in the log...
3761 */
3762
3763 cupsdLogMessage(CUPSD_LOG_DEBUG,
3764 "check_quotas: UUID lookup failed for ACL entry "
3765 "\"%s\" (err=%d)", name, mbr_err);
3766 cupsdLogMessage(CUPSD_LOG_WARN,
3767 "Access control entry \"%s\" not a valid group name; "
3768 "entry ignored", name);
3769 }
3770
3771 if ((mbr_err = mbr_check_membership(usr_uuid, grp_uuid,
3772 &is_member)) != 0)
3773 {
3774 /*
3775 * At this point, there should be no errors, but check anyways...
3776 */
3777
3778 cupsdLogMessage(CUPSD_LOG_DEBUG,
3779 "check_quotas: group \"%s\" membership check "
3780 "failed (err=%d)", name + 1, mbr_err);
3781 is_member = 0;
3782 }
3783
3784 /*
3785 * Stop if we found a match...
3786 */
3787
3788 if (is_member)
3789 break;
3790
3791 #else
3792 if (cupsdCheckGroup(username, pw, name + 1))
3793 break;
3794 #endif /* HAVE_MBR_UID_TO_UUID */
3795 }
3796 #ifdef HAVE_MBR_UID_TO_UUID
3797 else
3798 {
3799 if (name[0] == '#')
3800 {
3801 if (uuid_parse(name + 1, usr2_uuid))
3802 uuid_clear(usr2_uuid);
3803 }
3804 else if ((mbr_err = mbr_user_name_to_uuid(name, usr2_uuid)) != 0)
3805 {
3806 /*
3807 * Invalid ACL entries are ignored for matching; just record a
3808 * warning in the log...
3809 */
3810
3811 cupsdLogMessage(CUPSD_LOG_DEBUG,
3812 "check_quotas: UUID lookup failed for ACL entry "
3813 "\"%s\" (err=%d)", name, mbr_err);
3814 cupsdLogMessage(CUPSD_LOG_WARN,
3815 "Access control entry \"%s\" not a valid user name; "
3816 "entry ignored", name);
3817 }
3818
3819 if (!uuid_compare(usr_uuid, usr2_uuid))
3820 break;
3821 }
3822 #else
3823 else if (!_cups_strcasecmp(username, name))
3824 break;
3825 #endif /* HAVE_MBR_UID_TO_UUID */
3826
3827 if ((name != NULL) == p->deny_users)
3828 {
3829 cupsdLogMessage(CUPSD_LOG_INFO,
3830 "Denying user \"%s\" access to printer \"%s\"...",
3831 username, p->name);
3832 return (0);
3833 }
3834 }
3835
3836 /*
3837 * Check quotas...
3838 */
3839
3840 if (p->k_limit || p->page_limit)
3841 {
3842 if ((q = cupsdUpdateQuota(p, username, 0, 0)) == NULL)
3843 {
3844 cupsdLogMessage(CUPSD_LOG_ERROR,
3845 "Unable to allocate quota data for user \"%s\"",
3846 username);
3847 return (-1);
3848 }
3849
3850 if ((q->k_count >= p->k_limit && p->k_limit) ||
3851 (q->page_count >= p->page_limit && p->page_limit))
3852 {
3853 cupsdLogMessage(CUPSD_LOG_INFO, "User \"%s\" is over the quota limit...",
3854 username);
3855 return (-1);
3856 }
3857 }
3858
3859 /*
3860 * If we have gotten this far, we're done!
3861 */
3862
3863 return (1);
3864 }
3865
3866
3867 /*
3868 * 'close_job()' - Close a multi-file job.
3869 */
3870
3871 static void
3872 close_job(cupsd_client_t *con, /* I - Client connection */
3873 ipp_attribute_t *uri) /* I - Printer URI */
3874 {
3875 cupsd_job_t *job; /* Job */
3876 ipp_attribute_t *attr; /* Attribute */
3877 char job_uri[HTTP_MAX_URI],
3878 /* Job URI */
3879 username[256]; /* User name */
3880
3881
3882 cupsdLogMessage(CUPSD_LOG_DEBUG2, "close_job(%p[%d], %s)", con,
3883 con->number, uri->values[0].string.text);
3884
3885 /*
3886 * See if we have a job URI or a printer URI...
3887 */
3888
3889 if (strcmp(uri->name, "printer-uri"))
3890 {
3891 /*
3892 * job-uri is not supported by Close-Job!
3893 */
3894
3895 send_ipp_status(con, IPP_BAD_REQUEST,
3896 _("Close-Job doesn't support the job-uri attribute."));
3897 return;
3898 }
3899
3900 /*
3901 * Got a printer URI; see if we also have a job-id attribute...
3902 */
3903
3904 if ((attr = ippFindAttribute(con->request, "job-id",
3905 IPP_TAG_INTEGER)) == NULL)
3906 {
3907 send_ipp_status(con, IPP_BAD_REQUEST,
3908 _("Got a printer-uri attribute but no job-id."));
3909 return;
3910 }
3911
3912 if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
3913 {
3914 /*
3915 * Nope - return a "not found" error...
3916 */
3917
3918 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
3919 attr->values[0].integer);
3920 return;
3921 }
3922
3923 /*
3924 * See if the job is owned by the requesting user...
3925 */
3926
3927 if (!validate_user(job, con, job->username, username, sizeof(username)))
3928 {
3929 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
3930 cupsdFindDest(job->dest));
3931 return;
3932 }
3933
3934 /*
3935 * Add any ending sheet...
3936 */
3937
3938 if (cupsdTimeoutJob(job))
3939 return;
3940
3941 if (job->state_value == IPP_JOB_STOPPED)
3942 {
3943 job->state->values[0].integer = IPP_JOB_PENDING;
3944 job->state_value = IPP_JOB_PENDING;
3945 }
3946 else if (job->state_value == IPP_JOB_HELD)
3947 {
3948 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
3949 IPP_TAG_KEYWORD)) == NULL)
3950 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
3951
3952 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
3953 {
3954 job->state->values[0].integer = IPP_JOB_PENDING;
3955 job->state_value = IPP_JOB_PENDING;
3956 }
3957 }
3958
3959 job->dirty = 1;
3960 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
3961
3962 /*
3963 * Fill in the response info...
3964 */
3965
3966 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
3967 con->clientname, con->clientport, "/jobs/%d", job->id);
3968 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
3969 job_uri);
3970
3971 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
3972
3973 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
3974 job->state_value);
3975
3976 con->response->request.status.status_code = IPP_OK;
3977
3978 /*
3979 * Start the job if necessary...
3980 */
3981
3982 cupsdCheckJobs();
3983 }
3984
3985
3986 /*
3987 * 'copy_attrs()' - Copy attributes from one request to another.
3988 */
3989
3990 static void
3991 copy_attrs(ipp_t *to, /* I - Destination request */
3992 ipp_t *from, /* I - Source request */
3993 cups_array_t *ra, /* I - Requested attributes */
3994 ipp_tag_t group, /* I - Group to copy */
3995 int quickcopy, /* I - Do a quick copy? */
3996 cups_array_t *exclude) /* I - Attributes to exclude? */
3997 {
3998 ipp_attribute_t *fromattr; /* Source attribute */
3999
4000
4001 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4002 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4003 to, from, ra, group, quickcopy);
4004
4005 if (!to || !from)
4006 return;
4007
4008 for (fromattr = from->attrs; fromattr; fromattr = fromattr->next)
4009 {
4010 /*
4011 * Filter attributes as needed...
4012 */
4013
4014 if ((group != IPP_TAG_ZERO && fromattr->group_tag != group &&
4015 fromattr->group_tag != IPP_TAG_ZERO) || !fromattr->name)
4016 continue;
4017
4018 if (!strcmp(fromattr->name, "document-password") ||
4019 !strcmp(fromattr->name, "job-authorization-uri") ||
4020 !strcmp(fromattr->name, "job-password") ||
4021 !strcmp(fromattr->name, "job-password-encryption") ||
4022 !strcmp(fromattr->name, "job-printer-uri"))
4023 continue;
4024
4025 if (exclude &&
4026 (cupsArrayFind(exclude, fromattr->name) ||
4027 cupsArrayFind(exclude, "all")))
4028 {
4029 /*
4030 * We need to exclude this attribute for security reasons; we require the
4031 * job-id attribute regardless of the security settings for IPP
4032 * conformance.
4033 *
4034 * The job-printer-uri attribute is handled by copy_job_attrs().
4035 *
4036 * Subscription attribute security is handled by copy_subscription_attrs().
4037 */
4038
4039 if (strcmp(fromattr->name, "job-id"))
4040 continue;
4041 }
4042
4043 if (!ra || cupsArrayFind(ra, fromattr->name))
4044 {
4045 /*
4046 * Don't send collection attributes by default to IPP/1.x clients
4047 * since many do not support collections. Also don't send
4048 * media-col-database unless specifically requested by the client.
4049 */
4050
4051 if (fromattr->value_tag == IPP_TAG_BEGIN_COLLECTION &&
4052 !ra &&
4053 (to->request.status.version[0] == 1 ||
4054 !strcmp(fromattr->name, "media-col-database")))
4055 continue;
4056
4057 ippCopyAttribute(to, fromattr, quickcopy);
4058 }
4059 }
4060 }
4061
4062
4063 /*
4064 * 'copy_banner()' - Copy a banner file to the requests directory for the
4065 * specified job.
4066 */
4067
4068 static int /* O - Size of banner file in kbytes */
4069 copy_banner(cupsd_client_t *con, /* I - Client connection */
4070 cupsd_job_t *job, /* I - Job information */
4071 const char *name) /* I - Name of banner */
4072 {
4073 int i; /* Looping var */
4074 int kbytes; /* Size of banner file in kbytes */
4075 char filename[1024]; /* Job filename */
4076 cupsd_banner_t *banner; /* Pointer to banner */
4077 cups_file_t *in; /* Input file */
4078 cups_file_t *out; /* Output file */
4079 int ch; /* Character from file */
4080 char attrname[255], /* Name of attribute */
4081 *s; /* Pointer into name */
4082 ipp_attribute_t *attr; /* Attribute */
4083
4084
4085 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4086 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4087 con, con ? con->number : -1, job, job->id,
4088 name ? name : "(null)");
4089
4090 /*
4091 * Find the banner; return if not found or "none"...
4092 */
4093
4094 if (!name || !strcmp(name, "none") ||
4095 (banner = cupsdFindBanner(name)) == NULL)
4096 return (0);
4097
4098 /*
4099 * Open the banner and job files...
4100 */
4101
4102 if (add_file(con, job, banner->filetype, 0))
4103 return (-1);
4104
4105 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
4106 job->num_files);
4107 if ((out = cupsFileOpen(filename, "w")) == NULL)
4108 {
4109 cupsdLogMessage(CUPSD_LOG_ERROR,
4110 "Unable to create banner job file %s - %s",
4111 filename, strerror(errno));
4112 job->num_files --;
4113 return (0);
4114 }
4115
4116 fchmod(cupsFileNumber(out), 0640);
4117 fchown(cupsFileNumber(out), RunUser, Group);
4118
4119 /*
4120 * Try the localized banner file under the subdirectory...
4121 */
4122
4123 strlcpy(attrname, job->attrs->attrs->next->values[0].string.text,
4124 sizeof(attrname));
4125 if (strlen(attrname) > 2 && attrname[2] == '-')
4126 {
4127 /*
4128 * Convert ll-cc to ll_CC...
4129 */
4130
4131 attrname[2] = '_';
4132 attrname[3] = toupper(attrname[3] & 255);
4133 attrname[4] = toupper(attrname[4] & 255);
4134 }
4135
4136 snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir,
4137 attrname, name);
4138
4139 if (access(filename, 0) && strlen(attrname) > 2)
4140 {
4141 /*
4142 * Wasn't able to find "ll_CC" locale file; try the non-national
4143 * localization banner directory.
4144 */
4145
4146 attrname[2] = '\0';
4147
4148 snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir,
4149 attrname, name);
4150 }
4151
4152 if (access(filename, 0))
4153 {
4154 /*
4155 * Use the non-localized banner file.
4156 */
4157
4158 snprintf(filename, sizeof(filename), "%s/banners/%s", DataDir, name);
4159 }
4160
4161 if ((in = cupsFileOpen(filename, "r")) == NULL)
4162 {
4163 cupsFileClose(out);
4164 unlink(filename);
4165 cupsdLogMessage(CUPSD_LOG_ERROR,
4166 "Unable to open banner template file %s - %s",
4167 filename, strerror(errno));
4168 job->num_files --;
4169 return (0);
4170 }
4171
4172 /*
4173 * Parse the file to the end...
4174 */
4175
4176 while ((ch = cupsFileGetChar(in)) != EOF)
4177 if (ch == '{')
4178 {
4179 /*
4180 * Get an attribute name...
4181 */
4182
4183 for (s = attrname; (ch = cupsFileGetChar(in)) != EOF;)
4184 if (!isalpha(ch & 255) && ch != '-' && ch != '?')
4185 break;
4186 else if (s < (attrname + sizeof(attrname) - 1))
4187 *s++ = ch;
4188 else
4189 break;
4190
4191 *s = '\0';
4192
4193 if (ch != '}')
4194 {
4195 /*
4196 * Ignore { followed by stuff that is not an attribute name...
4197 */
4198
4199 cupsFilePrintf(out, "{%s%c", attrname, ch);
4200 continue;
4201 }
4202
4203 /*
4204 * See if it is defined...
4205 */
4206
4207 if (attrname[0] == '?')
4208 s = attrname + 1;
4209 else
4210 s = attrname;
4211
4212 if (!strcmp(s, "printer-name"))
4213 {
4214 cupsFilePuts(out, job->dest);
4215 continue;
4216 }
4217 else if ((attr = ippFindAttribute(job->attrs, s, IPP_TAG_ZERO)) == NULL)
4218 {
4219 /*
4220 * See if we have a leading question mark...
4221 */
4222
4223 if (attrname[0] != '?')
4224 {
4225 /*
4226 * Nope, write to file as-is; probably a PostScript procedure...
4227 */
4228
4229 cupsFilePrintf(out, "{%s}", attrname);
4230 }
4231
4232 continue;
4233 }
4234
4235 /*
4236 * Output value(s)...
4237 */
4238
4239 for (i = 0; i < attr->num_values; i ++)
4240 {
4241 if (i)
4242 cupsFilePutChar(out, ',');
4243
4244 switch (attr->value_tag)
4245 {
4246 case IPP_TAG_INTEGER :
4247 case IPP_TAG_ENUM :
4248 if (!strncmp(s, "time-at-", 8))
4249 {
4250 struct timeval tv; /* Time value */
4251
4252 tv.tv_sec = attr->values[i].integer;
4253 tv.tv_usec = 0;
4254
4255 cupsFilePuts(out, cupsdGetDateTime(&tv, CUPSD_TIME_STANDARD));
4256 }
4257 else
4258 cupsFilePrintf(out, "%d", attr->values[i].integer);
4259 break;
4260
4261 case IPP_TAG_BOOLEAN :
4262 cupsFilePrintf(out, "%d", attr->values[i].boolean);
4263 break;
4264
4265 case IPP_TAG_NOVALUE :
4266 cupsFilePuts(out, "novalue");
4267 break;
4268
4269 case IPP_TAG_RANGE :
4270 cupsFilePrintf(out, "%d-%d", attr->values[i].range.lower,
4271 attr->values[i].range.upper);
4272 break;
4273
4274 case IPP_TAG_RESOLUTION :
4275 cupsFilePrintf(out, "%dx%d%s", attr->values[i].resolution.xres,
4276 attr->values[i].resolution.yres,
4277 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
4278 "dpi" : "dpcm");
4279 break;
4280
4281 case IPP_TAG_URI :
4282 case IPP_TAG_STRING :
4283 case IPP_TAG_TEXT :
4284 case IPP_TAG_NAME :
4285 case IPP_TAG_KEYWORD :
4286 case IPP_TAG_CHARSET :
4287 case IPP_TAG_LANGUAGE :
4288 if (!_cups_strcasecmp(banner->filetype->type, "postscript"))
4289 {
4290 /*
4291 * Need to quote strings for PS banners...
4292 */
4293
4294 const char *p;
4295
4296 for (p = attr->values[i].string.text; *p; p ++)
4297 {
4298 if (*p == '(' || *p == ')' || *p == '\\')
4299 {
4300 cupsFilePutChar(out, '\\');
4301 cupsFilePutChar(out, *p);
4302 }
4303 else if (*p < 32 || *p > 126)
4304 cupsFilePrintf(out, "\\%03o", *p & 255);
4305 else
4306 cupsFilePutChar(out, *p);
4307 }
4308 }
4309 else
4310 cupsFilePuts(out, attr->values[i].string.text);
4311 break;
4312
4313 default :
4314 break; /* anti-compiler-warning-code */
4315 }
4316 }
4317 }
4318 else if (ch == '\\') /* Quoted char */
4319 {
4320 ch = cupsFileGetChar(in);
4321
4322 if (ch != '{') /* Only do special handling for \{ */
4323 cupsFilePutChar(out, '\\');
4324
4325 cupsFilePutChar(out, ch);
4326 }
4327 else
4328 cupsFilePutChar(out, ch);
4329
4330 cupsFileClose(in);
4331
4332 kbytes = (cupsFileTell(out) + 1023) / 1024;
4333
4334 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
4335 IPP_TAG_INTEGER)) != NULL)
4336 attr->values[0].integer += kbytes;
4337
4338 cupsFileClose(out);
4339
4340 return (kbytes);
4341 }
4342
4343
4344 /*
4345 * 'copy_file()' - Copy a PPD file or interface script...
4346 */
4347
4348 static int /* O - 0 = success, -1 = error */
4349 copy_file(const char *from, /* I - Source file */
4350 const char *to) /* I - Destination file */
4351 {
4352 cups_file_t *src, /* Source file */
4353 *dst; /* Destination file */
4354 int bytes; /* Bytes to read/write */
4355 char buffer[2048]; /* Copy buffer */
4356
4357
4358 cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_file(\"%s\", \"%s\")", from, to);
4359
4360 /*
4361 * Open the source and destination file for a copy...
4362 */
4363
4364 if ((src = cupsFileOpen(from, "rb")) == NULL)
4365 return (-1);
4366
4367 if ((dst = cupsFileOpen(to, "wb")) == NULL)
4368 {
4369 cupsFileClose(src);
4370 return (-1);
4371 }
4372
4373 /*
4374 * Copy the source file to the destination...
4375 */
4376
4377 while ((bytes = cupsFileRead(src, buffer, sizeof(buffer))) > 0)
4378 if (cupsFileWrite(dst, buffer, bytes) < bytes)
4379 {
4380 cupsFileClose(src);
4381 cupsFileClose(dst);
4382 return (-1);
4383 }
4384
4385 /*
4386 * Close both files and return...
4387 */
4388
4389 cupsFileClose(src);
4390
4391 return (cupsFileClose(dst));
4392 }
4393
4394
4395 /*
4396 * 'copy_model()' - Copy a PPD model file, substituting default values
4397 * as needed...
4398 */
4399
4400 static int /* O - 0 = success, -1 = error */
4401 copy_model(cupsd_client_t *con, /* I - Client connection */
4402 const char *from, /* I - Source file */
4403 const char *to) /* I - Destination file */
4404 {
4405 fd_set input; /* select() input set */
4406 struct timeval timeout; /* select() timeout */
4407 int maxfd; /* Max file descriptor for select() */
4408 char tempfile[1024]; /* Temporary PPD file */
4409 int tempfd; /* Temporary PPD file descriptor */
4410 int temppid; /* Process ID of cups-driverd */
4411 int temppipe[2]; /* Temporary pipes */
4412 char *argv[4], /* Command-line arguments */
4413 *envp[MAX_ENV]; /* Environment */
4414 cups_file_t *src, /* Source file */
4415 *dst; /* Destination file */
4416 ppd_file_t *ppd; /* PPD file */
4417 int bytes, /* Bytes from pipe */
4418 total; /* Total bytes from pipe */
4419 char buffer[2048]; /* Copy buffer */
4420 int i; /* Looping var */
4421 char option[PPD_MAX_NAME], /* Option name */
4422 choice[PPD_MAX_NAME]; /* Choice name */
4423 ppd_size_t *size; /* Default size */
4424 int num_defaults; /* Number of default options */
4425 cups_option_t *defaults; /* Default options */
4426 char cups_protocol[PPD_MAX_LINE];
4427 /* cupsProtocol attribute */
4428
4429
4430 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4431 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4432 con, from, to);
4433
4434 /*
4435 * Run cups-driverd to get the PPD file...
4436 */
4437
4438 argv[0] = "cups-driverd";
4439 argv[1] = "cat";
4440 argv[2] = (char *)from;
4441 argv[3] = NULL;
4442
4443 cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
4444
4445 snprintf(buffer, sizeof(buffer), "%s/daemon/cups-driverd", ServerBin);
4446 snprintf(tempfile, sizeof(tempfile), "%s/%d.ppd", TempDir, con->number);
4447 tempfd = open(tempfile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
4448 if (tempfd < 0 || cupsdOpenPipe(temppipe))
4449 return (-1);
4450
4451 cupsdLogMessage(CUPSD_LOG_DEBUG,
4452 "copy_model: Running \"cups-driverd cat %s\"...", from);
4453
4454 if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1],
4455 -1, -1, 0, DefaultProfile, NULL, &temppid))
4456 {
4457 close(tempfd);
4458 unlink(tempfile);
4459
4460 return (-1);
4461 }
4462
4463 close(temppipe[1]);
4464
4465 /*
4466 * Wait up to 30 seconds for the PPD file to be copied...
4467 */
4468
4469 total = 0;
4470
4471 if (temppipe[0] > CGIPipes[0])
4472 maxfd = temppipe[0] + 1;
4473 else
4474 maxfd = CGIPipes[0] + 1;
4475
4476 for (;;)
4477 {
4478 /*
4479 * See if we have data ready...
4480 */
4481
4482 FD_ZERO(&input);
4483 FD_SET(temppipe[0], &input);
4484 FD_SET(CGIPipes[0], &input);
4485
4486 timeout.tv_sec = 30;
4487 timeout.tv_usec = 0;
4488
4489 if ((i = select(maxfd, &input, NULL, NULL, &timeout)) < 0)
4490 {
4491 if (errno == EINTR)
4492 continue;
4493 else
4494 break;
4495 }
4496 else if (i == 0)
4497 {
4498 /*
4499 * We have timed out...
4500 */
4501
4502 break;
4503 }
4504
4505 if (FD_ISSET(temppipe[0], &input))
4506 {
4507 /*
4508 * Read the PPD file from the pipe, and write it to the PPD file.
4509 */
4510
4511 if ((bytes = read(temppipe[0], buffer, sizeof(buffer))) > 0)
4512 {
4513 if (write(tempfd, buffer, bytes) < bytes)
4514 break;
4515
4516 total += bytes;
4517 }
4518 else
4519 break;
4520 }
4521
4522 if (FD_ISSET(CGIPipes[0], &input))
4523 cupsdUpdateCGI();
4524 }
4525
4526 close(temppipe[0]);
4527 close(tempfd);
4528
4529 if (!total)
4530 {
4531 /*
4532 * No data from cups-deviced...
4533 */
4534
4535 cupsdLogMessage(CUPSD_LOG_ERROR, "copy_model: empty PPD file");
4536 unlink(tempfile);
4537 return (-1);
4538 }
4539
4540 /*
4541 * Open the source file for a copy...
4542 */
4543
4544 if ((src = cupsFileOpen(tempfile, "rb")) == NULL)
4545 {
4546 unlink(tempfile);
4547 return (-1);
4548 }
4549
4550 /*
4551 * Read the source file and see what page sizes are supported...
4552 */
4553
4554 if ((ppd = _ppdOpen(src, _PPD_LOCALIZATION_NONE)) == NULL)
4555 {
4556 cupsFileClose(src);
4557 unlink(tempfile);
4558 return (-1);
4559 }
4560
4561 /*
4562 * Open the destination (if possible) and set the default options...
4563 */
4564
4565 num_defaults = 0;
4566 defaults = NULL;
4567 cups_protocol[0] = '\0';
4568
4569 if ((dst = cupsFileOpen(to, "rb")) != NULL)
4570 {
4571 /*
4572 * Read all of the default lines from the old PPD...
4573 */
4574
4575 while (cupsFileGets(dst, buffer, sizeof(buffer)))
4576 if (!strncmp(buffer, "*Default", 8))
4577 {
4578 /*
4579 * Add the default option...
4580 */
4581
4582 if (!ppd_parse_line(buffer, option, sizeof(option),
4583 choice, sizeof(choice)))
4584 {
4585 ppd_option_t *ppdo; /* PPD option */
4586
4587
4588 /*
4589 * Only add the default if the default hasn't already been
4590 * set and the choice exists in the new PPD...
4591 */
4592
4593 if (!cupsGetOption(option, num_defaults, defaults) &&
4594 (ppdo = ppdFindOption(ppd, option)) != NULL &&
4595 ppdFindChoice(ppdo, choice))
4596 num_defaults = cupsAddOption(option, choice, num_defaults,
4597 &defaults);
4598 }
4599 }
4600 else if (!strncmp(buffer, "*cupsProtocol:", 14))
4601 strlcpy(cups_protocol, buffer, sizeof(cups_protocol));
4602
4603 cupsFileClose(dst);
4604 }
4605 else if ((size = ppdPageSize(ppd, DefaultPaperSize)) != NULL)
4606 {
4607 /*
4608 * Add the default media sizes...
4609 */
4610
4611 num_defaults = cupsAddOption("PageSize", size->name,
4612 num_defaults, &defaults);
4613 num_defaults = cupsAddOption("PageRegion", size->name,
4614 num_defaults, &defaults);
4615 num_defaults = cupsAddOption("PaperDimension", size->name,
4616 num_defaults, &defaults);
4617 num_defaults = cupsAddOption("ImageableArea", size->name,
4618 num_defaults, &defaults);
4619 }
4620
4621 ppdClose(ppd);
4622
4623 /*
4624 * Open the destination file for a copy...
4625 */
4626
4627 if ((dst = cupsFileOpen(to, "wb")) == NULL)
4628 {
4629 cupsFreeOptions(num_defaults, defaults);
4630 cupsFileClose(src);
4631 unlink(tempfile);
4632 return (-1);
4633 }
4634
4635 /*
4636 * Copy the source file to the destination...
4637 */
4638
4639 cupsFileRewind(src);
4640
4641 while (cupsFileGets(src, buffer, sizeof(buffer)))
4642 {
4643 if (!strncmp(buffer, "*Default", 8))
4644 {
4645 /*
4646 * Check for an previous default option choice...
4647 */
4648
4649 if (!ppd_parse_line(buffer, option, sizeof(option),
4650 choice, sizeof(choice)))
4651 {
4652 const char *val; /* Default option value */
4653
4654
4655 if ((val = cupsGetOption(option, num_defaults, defaults)) != NULL)
4656 {
4657 /*
4658 * Substitute the previous choice...
4659 */
4660
4661 snprintf(buffer, sizeof(buffer), "*Default%s: %s", option, val);
4662 }
4663 }
4664 }
4665
4666 cupsFilePrintf(dst, "%s\n", buffer);
4667 }
4668
4669 if (cups_protocol[0])
4670 cupsFilePrintf(dst, "%s\n", cups_protocol);
4671
4672 cupsFreeOptions(num_defaults, defaults);
4673
4674 /*
4675 * Close both files and return...
4676 */
4677
4678 cupsFileClose(src);
4679
4680 unlink(tempfile);
4681
4682 return (cupsFileClose(dst));
4683 }
4684
4685
4686 /*
4687 * 'copy_job_attrs()' - Copy job attributes.
4688 */
4689
4690 static void
4691 copy_job_attrs(cupsd_client_t *con, /* I - Client connection */
4692 cupsd_job_t *job, /* I - Job */
4693 cups_array_t *ra, /* I - Requested attributes array */
4694 cups_array_t *exclude) /* I - Private attributes array */
4695 {
4696 char job_uri[HTTP_MAX_URI]; /* Job URI */
4697
4698
4699 /*
4700 * Send the requested attributes for each job...
4701 */
4702
4703 if (!cupsArrayFind(exclude, "all"))
4704 {
4705 if ((!exclude || !cupsArrayFind(exclude, "number-of-documents")) &&
4706 (!ra || cupsArrayFind(ra, "number-of-documents")))
4707 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
4708 "number-of-documents", job->num_files);
4709
4710 if ((!exclude || !cupsArrayFind(exclude, "job-media-progress")) &&
4711 (!ra || cupsArrayFind(ra, "job-media-progress")))
4712 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
4713 "job-media-progress", job->progress);
4714
4715 if ((!exclude || !cupsArrayFind(exclude, "job-more-info")) &&
4716 (!ra || cupsArrayFind(ra, "job-more-info")))
4717 {
4718 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "http",
4719 NULL, con->clientname, con->clientport, "/jobs/%d",
4720 job->id);
4721 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
4722 "job-more-info", NULL, job_uri);
4723 }
4724
4725 if (job->state_value > IPP_JOB_PROCESSING &&
4726 (!exclude || !cupsArrayFind(exclude, "job-preserved")) &&
4727 (!ra || cupsArrayFind(ra, "job-preserved")))
4728 ippAddBoolean(con->response, IPP_TAG_JOB, "job-preserved",
4729 job->num_files > 0);
4730
4731 if ((!exclude || !cupsArrayFind(exclude, "job-printer-up-time")) &&
4732 (!ra || cupsArrayFind(ra, "job-printer-up-time")))
4733 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
4734 "job-printer-up-time", time(NULL));
4735 }
4736
4737 if (!ra || cupsArrayFind(ra, "job-printer-uri"))
4738 {
4739 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
4740 con->clientname, con->clientport,
4741 (job->dtype & CUPS_PRINTER_CLASS) ? "/classes/%s" :
4742 "/printers/%s",
4743 job->dest);
4744 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
4745 "job-printer-uri", NULL, job_uri);
4746 }
4747
4748 if (!ra || cupsArrayFind(ra, "job-uri"))
4749 {
4750 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
4751 con->clientname, con->clientport, "/jobs/%d",
4752 job->id);
4753 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
4754 "job-uri", NULL, job_uri);
4755 }
4756
4757 copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0, exclude);
4758 }
4759
4760
4761 /*
4762 * 'copy_printer_attrs()' - Copy printer attributes.
4763 */
4764
4765 static void
4766 copy_printer_attrs(
4767 cupsd_client_t *con, /* I - Client connection */
4768 cupsd_printer_t *printer, /* I - Printer */
4769 cups_array_t *ra) /* I - Requested attributes array */
4770 {
4771 char printer_uri[HTTP_MAX_URI];
4772 /* Printer URI */
4773 char printer_icons[HTTP_MAX_URI];
4774 /* Printer icons */
4775 time_t curtime; /* Current time */
4776 int i; /* Looping var */
4777
4778
4779 /*
4780 * Copy the printer attributes to the response using requested-attributes
4781 * and document-format attributes that may be provided by the client.
4782 */
4783
4784 curtime = time(NULL);
4785
4786 if (!ra || cupsArrayFind(ra, "marker-change-time"))
4787 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4788 "marker-change-time", printer->marker_time);
4789
4790 if (printer->num_printers > 0 &&
4791 (!ra || cupsArrayFind(ra, "member-uris")))
4792 {
4793 ipp_attribute_t *member_uris; /* member-uris attribute */
4794 cupsd_printer_t *p2; /* Printer in class */
4795 ipp_attribute_t *p2_uri; /* printer-uri-supported for class printer */
4796
4797
4798 if ((member_uris = ippAddStrings(con->response, IPP_TAG_PRINTER,
4799 IPP_TAG_URI, "member-uris",
4800 printer->num_printers, NULL,
4801 NULL)) != NULL)
4802 {
4803 for (i = 0; i < printer->num_printers; i ++)
4804 {
4805 p2 = printer->printers[i];
4806
4807 if ((p2_uri = ippFindAttribute(p2->attrs, "printer-uri-supported",
4808 IPP_TAG_URI)) != NULL)
4809 member_uris->values[i].string.text =
4810 _cupsStrRetain(p2_uri->values[0].string.text);
4811 else
4812 {
4813 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri,
4814 sizeof(printer_uri), "ipp", NULL, con->clientname,
4815 con->clientport,
4816 (p2->type & CUPS_PRINTER_CLASS) ?
4817 "/classes/%s" : "/printers/%s", p2->name);
4818 member_uris->values[i].string.text = _cupsStrAlloc(printer_uri);
4819 }
4820 }
4821 }
4822 }
4823
4824 if (printer->alert && (!ra || cupsArrayFind(ra, "printer-alert")))
4825 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_STRING,
4826 "printer-alert", NULL, printer->alert);
4827
4828 if (printer->alert_description &&
4829 (!ra || cupsArrayFind(ra, "printer-alert-description")))
4830 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
4831 "printer-alert-description", NULL,
4832 printer->alert_description);
4833
4834 if (!ra || cupsArrayFind(ra, "printer-current-time"))
4835 ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
4836 ippTimeToDate(curtime));
4837
4838 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
4839 if (!ra || cupsArrayFind(ra, "printer-dns-sd-name"))
4840 {
4841 if (printer->reg_name)
4842 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
4843 "printer-dns-sd-name", NULL, printer->reg_name);
4844 else
4845 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
4846 "printer-dns-sd-name", 0);
4847 }
4848 #endif /* HAVE_DNSSD || HAVE_AVAHI */
4849
4850 if (!ra || cupsArrayFind(ra, "printer-error-policy"))
4851 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
4852 "printer-error-policy", NULL, printer->error_policy);
4853
4854 if (!ra || cupsArrayFind(ra, "printer-error-policy-supported"))
4855 {
4856 static const char * const errors[] =/* printer-error-policy-supported values */
4857 {
4858 "abort-job",
4859 "retry-current-job",
4860 "retry-job",
4861 "stop-printer"
4862 };
4863
4864 if (printer->type & CUPS_PRINTER_CLASS)
4865 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
4866 "printer-error-policy-supported", NULL, "retry-current-job");
4867 else
4868 ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
4869 "printer-error-policy-supported",
4870 sizeof(errors) / sizeof(errors[0]), NULL, errors);
4871 }
4872
4873 if (!ra || cupsArrayFind(ra, "printer-icons"))
4874 {
4875 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_icons, sizeof(printer_icons),
4876 "http", NULL, con->clientname, con->clientport,
4877 "/icons/%s.png", printer->name);
4878 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-icons",
4879 NULL, printer_icons);
4880 cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-icons=\"%s\"", printer_icons);
4881 }
4882
4883 if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs"))
4884 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
4885 printer->accepting);
4886
4887 if (!ra || cupsArrayFind(ra, "printer-is-shared"))
4888 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared",
4889 printer->shared);
4890
4891 if (!ra || cupsArrayFind(ra, "printer-more-info"))
4892 {
4893 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
4894 "http", NULL, con->clientname, con->clientport,
4895 (printer->type & CUPS_PRINTER_CLASS) ?
4896 "/classes/%s" : "/printers/%s", printer->name);
4897 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
4898 "printer-more-info", NULL, printer_uri);
4899 }
4900
4901 if (!ra || cupsArrayFind(ra, "printer-op-policy"))
4902 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
4903 "printer-op-policy", NULL, printer->op_policy);
4904
4905 if (!ra || cupsArrayFind(ra, "printer-state"))
4906 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
4907 printer->state);
4908
4909 if (!ra || cupsArrayFind(ra, "printer-state-change-time"))
4910 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4911 "printer-state-change-time", printer->state_time);
4912
4913 if (!ra || cupsArrayFind(ra, "printer-state-message"))
4914 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
4915 "printer-state-message", NULL, printer->state_message);
4916
4917 if (!ra || cupsArrayFind(ra, "printer-state-reasons"))
4918 add_printer_state_reasons(con, printer);
4919
4920 if (!ra || cupsArrayFind(ra, "printer-type"))
4921 {
4922 int type; /* printer-type value */
4923
4924 /*
4925 * Add the CUPS-specific printer-type attribute...
4926 */
4927
4928 type = printer->type;
4929
4930 if (printer == DefaultPrinter)
4931 type |= CUPS_PRINTER_DEFAULT;
4932
4933 if (!printer->accepting)
4934 type |= CUPS_PRINTER_REJECTING;
4935
4936 if (!printer->shared)
4937 type |= CUPS_PRINTER_NOT_SHARED;
4938
4939 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type",
4940 type);
4941 }
4942
4943 if (!ra || cupsArrayFind(ra, "printer-up-time"))
4944 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4945 "printer-up-time", curtime);
4946
4947 if (!ra || cupsArrayFind(ra, "printer-uri-supported"))
4948 {
4949 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
4950 "ipp", NULL, con->clientname, con->clientport,
4951 (printer->type & CUPS_PRINTER_CLASS) ?
4952 "/classes/%s" : "/printers/%s", printer->name);
4953 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
4954 "printer-uri-supported", NULL, printer_uri);
4955 cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"",
4956 printer_uri);
4957 }
4958
4959 if (!ra || cupsArrayFind(ra, "queued-job-count"))
4960 add_queued_job_count(con, printer);
4961
4962 copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0, NULL);
4963 if (printer->ppd_attrs)
4964 copy_attrs(con->response, printer->ppd_attrs, ra, IPP_TAG_ZERO, 0, NULL);
4965 copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY, NULL);
4966 }
4967
4968
4969 /*
4970 * 'copy_subscription_attrs()' - Copy subscription attributes.
4971 */
4972
4973 static void
4974 copy_subscription_attrs(
4975 cupsd_client_t *con, /* I - Client connection */
4976 cupsd_subscription_t *sub, /* I - Subscription */
4977 cups_array_t *ra, /* I - Requested attributes array */
4978 cups_array_t *exclude) /* I - Private attributes array */
4979 {
4980 ipp_attribute_t *attr; /* Current attribute */
4981 char printer_uri[HTTP_MAX_URI];
4982 /* Printer URI */
4983 int count; /* Number of events */
4984 unsigned mask; /* Current event mask */
4985 const char *name; /* Current event name */
4986
4987
4988 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4989 "copy_subscription_attrs(con=%p, sub=%p, ra=%p, exclude=%p)",
4990 con, sub, ra, exclude);
4991
4992 /*
4993 * Copy the subscription attributes to the response using the
4994 * requested-attributes attribute that may be provided by the client.
4995 */
4996
4997 if (!exclude || !cupsArrayFind(exclude, "all"))
4998 {
4999 if ((!exclude || !cupsArrayFind(exclude, "notify-events")) &&
5000 (!ra || cupsArrayFind(ra, "notify-events")))
5001 {
5002 cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_subscription_attrs: notify-events");
5003
5004 if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL)
5005 {
5006 /*
5007 * Simple event list...
5008 */
5009
5010 ippAddString(con->response, IPP_TAG_SUBSCRIPTION,
5011 (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
5012 "notify-events", NULL, name);
5013 }
5014 else
5015 {
5016 /*
5017 * Complex event list...
5018 */
5019
5020 for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
5021 if (sub->mask & mask)
5022 count ++;
5023
5024 attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION,
5025 (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
5026 "notify-events", count, NULL, NULL);
5027
5028 for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
5029 if (sub->mask & mask)
5030 {
5031 attr->values[count].string.text =
5032 (char *)cupsdEventName((cupsd_eventmask_t)mask);
5033
5034 count ++;
5035 }
5036 }
5037 }
5038
5039 if ((!exclude || !cupsArrayFind(exclude, "notify-lease-duration")) &&
5040 (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration"))))
5041 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5042 "notify-lease-duration", sub->lease);
5043
5044 if ((!exclude || !cupsArrayFind(exclude, "notify-recipient-uri")) &&
5045 (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri"))))
5046 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
5047 "notify-recipient-uri", NULL, sub->recipient);
5048 else if ((!exclude || !cupsArrayFind(exclude, "notify-pull-method")) &&
5049 (!ra || cupsArrayFind(ra, "notify-pull-method")))
5050 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
5051 "notify-pull-method", NULL, "ippget");
5052
5053 if ((!exclude || !cupsArrayFind(exclude, "notify-subscriber-user-name")) &&
5054 (!ra || cupsArrayFind(ra, "notify-subscriber-user-name")))
5055 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME,
5056 "notify-subscriber-user-name", NULL, sub->owner);
5057
5058 if ((!exclude || !cupsArrayFind(exclude, "notify-time-interval")) &&
5059 (!ra || cupsArrayFind(ra, "notify-time-interval")))
5060 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5061 "notify-time-interval", sub->interval);
5062
5063 if (sub->user_data_len > 0 &&
5064 (!exclude || !cupsArrayFind(exclude, "notify-user-data")) &&
5065 (!ra || cupsArrayFind(ra, "notify-user-data")))
5066 ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data",
5067 sub->user_data, sub->user_data_len);
5068 }
5069
5070 if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id")))
5071 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5072 "notify-job-id", sub->job->id);
5073
5074 if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri")))
5075 {
5076 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
5077 "ipp", NULL, con->clientname, con->clientport,
5078 "/printers/%s", sub->dest->name);
5079 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
5080 "notify-printer-uri", NULL, printer_uri);
5081 }
5082
5083 if (!ra || cupsArrayFind(ra, "notify-subscription-id"))
5084 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5085 "notify-subscription-id", sub->id);
5086 }
5087
5088
5089 /*
5090 * 'create_job()' - Print a file to a printer or class.
5091 */
5092
5093 static void
5094 create_job(cupsd_client_t *con, /* I - Client connection */
5095 ipp_attribute_t *uri) /* I - Printer URI */
5096 {
5097 int i; /* Looping var */
5098 cupsd_printer_t *printer; /* Printer */
5099 cupsd_job_t *job; /* New job */
5100 static const char * const forbidden_attrs[] =
5101 { /* List of forbidden attributes */
5102 "compression",
5103 "document-format",
5104 "document-name",
5105 "document-natural-language"
5106 };
5107
5108
5109 cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con,
5110 con->number, uri->values[0].string.text);
5111
5112 /*
5113 * Is the destination valid?
5114 */
5115
5116 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
5117 {
5118 /*
5119 * Bad URI...
5120 */
5121
5122 send_ipp_status(con, IPP_NOT_FOUND,
5123 _("The printer or class does not exist."));
5124 return;
5125 }
5126
5127 /*
5128 * Check for invalid Create-Job attributes and log a warning or error depending
5129 * on whether cupsd is running in "strict conformance" mode...
5130 */
5131
5132 for (i = 0;
5133 i < (int)(sizeof(forbidden_attrs) / sizeof(forbidden_attrs[0]));
5134 i ++)
5135 if (ippFindAttribute(con->request, forbidden_attrs[i], IPP_TAG_ZERO))
5136 {
5137 if (StrictConformance)
5138 {
5139 send_ipp_status(con, IPP_BAD_REQUEST,
5140 _("The '%s' operation attribute cannot be supplied in a "
5141 "Create-Job request."), forbidden_attrs[i]);
5142 return;
5143 }
5144
5145 cupsdLogMessage(CUPSD_LOG_WARN,
5146 "Unexpected '%s' operation attribute in a Create-Job "
5147 "request.", forbidden_attrs[i]);
5148 }
5149
5150 /*
5151 * Create the job object...
5152 */
5153
5154 if ((job = add_job(con, printer, NULL)) == NULL)
5155 return;
5156
5157 job->pending_timeout = 1;
5158
5159 /*
5160 * Save and log the job...
5161 */
5162
5163 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
5164 job->dest, job->username);
5165 }
5166
5167
5168 /*
5169 * 'create_requested_array()' - Create an array for the requested-attributes.
5170 */
5171
5172 static cups_array_t * /* O - Array of attributes or NULL */
5173 create_requested_array(ipp_t *request) /* I - IPP request */
5174 {
5175 cups_array_t *ra; /* Requested attributes array */
5176
5177
5178 /*
5179 * Create the array for standard attributes...
5180 */
5181
5182 ra = ippCreateRequestedArray(request);
5183
5184 /*
5185 * Add CUPS defaults as needed...
5186 */
5187
5188 if (cupsArrayFind(ra, "printer-defaults"))
5189 {
5190 /*
5191 * Include user-set defaults...
5192 */
5193
5194 char *name; /* Option name */
5195
5196 cupsArrayRemove(ra, "printer-defaults");
5197
5198 for (name = (char *)cupsArrayFirst(CommonDefaults);
5199 name;
5200 name = (char *)cupsArrayNext(CommonDefaults))
5201 if (!cupsArrayFind(ra, name))
5202 cupsArrayAdd(ra, name);
5203 }
5204
5205 return (ra);
5206 }
5207
5208
5209 /*
5210 * 'create_subscription()' - Create a notification subscription.
5211 */
5212
5213 static void
5214 create_subscription(
5215 cupsd_client_t *con, /* I - Client connection */
5216 ipp_attribute_t *uri) /* I - Printer URI */
5217 {
5218 http_status_t status; /* Policy status */
5219 int i; /* Looping var */
5220 ipp_attribute_t *attr; /* Current attribute */
5221 cups_ptype_t dtype; /* Destination type (printer/class) */
5222 char scheme[HTTP_MAX_URI],
5223 /* Scheme portion of URI */
5224 userpass[HTTP_MAX_URI],
5225 /* Username portion of URI */
5226 host[HTTP_MAX_URI],
5227 /* Host portion of URI */
5228 resource[HTTP_MAX_URI];
5229 /* Resource portion of URI */
5230 int port; /* Port portion of URI */
5231 cupsd_printer_t *printer; /* Printer/class */
5232 cupsd_job_t *job; /* Job */
5233 int jobid; /* Job ID */
5234 cupsd_subscription_t *sub; /* Subscription object */
5235 const char *username, /* requesting-user-name or
5236 authenticated username */
5237 *recipient, /* notify-recipient-uri */
5238 *pullmethod; /* notify-pull-method */
5239 ipp_attribute_t *user_data; /* notify-user-data */
5240 int interval, /* notify-time-interval */
5241 lease; /* notify-lease-duration */
5242 unsigned mask; /* notify-events */
5243 ipp_attribute_t *notify_events,/* notify-events(-default) */
5244 *notify_lease; /* notify-lease-duration(-default) */
5245
5246
5247 #ifdef DEBUG
5248 for (attr = con->request->attrs; attr; attr = attr->next)
5249 {
5250 if (attr->group_tag != IPP_TAG_ZERO)
5251 cupsdLogMessage(CUPSD_LOG_DEBUG2, "g%04x v%04x %s", attr->group_tag,
5252 attr->value_tag, attr->name);
5253 else
5254 cupsdLogMessage(CUPSD_LOG_DEBUG2, "----SEP----");
5255 }
5256 #endif /* DEBUG */
5257
5258 /*
5259 * Is the destination valid?
5260 */
5261
5262 cupsdLogMessage(CUPSD_LOG_DEBUG,
5263 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5264 con, con->number, uri->values[0].string.text);
5265
5266 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
5267 sizeof(scheme), userpass, sizeof(userpass), host,
5268 sizeof(host), &port, resource, sizeof(resource));
5269
5270 if (!strcmp(resource, "/"))
5271 {
5272 dtype = (cups_ptype_t)0;
5273 printer = NULL;
5274 }
5275 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
5276 {
5277 dtype = (cups_ptype_t)0;
5278 printer = NULL;
5279 }
5280 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
5281 {
5282 dtype = CUPS_PRINTER_CLASS;
5283 printer = NULL;
5284 }
5285 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
5286 {
5287 /*
5288 * Bad URI...
5289 */
5290
5291 send_ipp_status(con, IPP_NOT_FOUND,
5292 _("The printer or class does not exist."));
5293 return;
5294 }
5295
5296 /*
5297 * Check policy...
5298 */
5299
5300 if (printer)
5301 {
5302 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
5303 NULL)) != HTTP_OK)
5304 {
5305 send_http_error(con, status, printer);
5306 return;
5307 }
5308 }
5309 else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5310 {
5311 send_http_error(con, status, NULL);
5312 return;
5313 }
5314
5315 /*
5316 * Get the user that is requesting the subscription...
5317 */
5318
5319 username = get_username(con);
5320
5321 /*
5322 * Find the first subscription group attribute; return if we have
5323 * none...
5324 */
5325
5326 for (attr = con->request->attrs; attr; attr = attr->next)
5327 if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
5328 break;
5329
5330 if (!attr)
5331 {
5332 send_ipp_status(con, IPP_BAD_REQUEST,
5333 _("No subscription attributes in request."));
5334 return;
5335 }
5336
5337 /*
5338 * Process the subscription attributes in the request...
5339 */
5340
5341 con->response->request.status.status_code = IPP_BAD_REQUEST;
5342
5343 while (attr)
5344 {
5345 recipient = NULL;
5346 pullmethod = NULL;
5347 user_data = NULL;
5348 interval = 0;
5349 lease = DefaultLeaseDuration;
5350 jobid = 0;
5351 mask = CUPSD_EVENT_NONE;
5352
5353 if (printer)
5354 {
5355 notify_events = ippFindAttribute(printer->attrs, "notify-events-default",
5356 IPP_TAG_KEYWORD);
5357 notify_lease = ippFindAttribute(printer->attrs,
5358 "notify-lease-duration-default",
5359 IPP_TAG_INTEGER);
5360
5361 if (notify_lease)
5362 lease = notify_lease->values[0].integer;
5363 }
5364 else
5365 {
5366 notify_events = NULL;
5367 notify_lease = NULL;
5368 }
5369
5370 while (attr && attr->group_tag != IPP_TAG_ZERO)
5371 {
5372 if (!strcmp(attr->name, "notify-recipient-uri") &&
5373 attr->value_tag == IPP_TAG_URI)
5374 {
5375 /*
5376 * Validate the recipient scheme against the ServerBin/notifier
5377 * directory...
5378 */
5379
5380 char notifier[1024]; /* Notifier filename */
5381
5382
5383 recipient = attr->values[0].string.text;
5384
5385 if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient,
5386 scheme, sizeof(scheme), userpass, sizeof(userpass),
5387 host, sizeof(host), &port,
5388 resource, sizeof(resource)) < HTTP_URI_OK)
5389 {
5390 send_ipp_status(con, IPP_NOT_POSSIBLE,
5391 _("Bad notify-recipient-uri \"%s\"."), recipient);
5392 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5393 "notify-status-code", IPP_URI_SCHEME);
5394 return;
5395 }
5396
5397 snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
5398 scheme);
5399 if (access(notifier, X_OK))
5400 {
5401 send_ipp_status(con, IPP_NOT_POSSIBLE,
5402 _("notify-recipient-uri URI \"%s\" uses unknown "
5403 "scheme."), recipient);
5404 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5405 "notify-status-code", IPP_URI_SCHEME);
5406 return;
5407 }
5408
5409 if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient))
5410 {
5411 send_ipp_status(con, IPP_NOT_POSSIBLE,
5412 _("notify-recipient-uri URI \"%s\" is already used."),
5413 recipient);
5414 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5415 "notify-status-code", IPP_ATTRIBUTES);
5416 return;
5417 }
5418 }
5419 else if (!strcmp(attr->name, "notify-pull-method") &&
5420 attr->value_tag == IPP_TAG_KEYWORD)
5421 {
5422 pullmethod = attr->values[0].string.text;
5423
5424 if (strcmp(pullmethod, "ippget"))
5425 {
5426 send_ipp_status(con, IPP_NOT_POSSIBLE,
5427 _("Bad notify-pull-method \"%s\"."), pullmethod);
5428 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5429 "notify-status-code", IPP_ATTRIBUTES);
5430 return;
5431 }
5432 }
5433 else if (!strcmp(attr->name, "notify-charset") &&
5434 attr->value_tag == IPP_TAG_CHARSET &&
5435 strcmp(attr->values[0].string.text, "us-ascii") &&
5436 strcmp(attr->values[0].string.text, "utf-8"))
5437 {
5438 send_ipp_status(con, IPP_CHARSET,
5439 _("Character set \"%s\" not supported."),
5440 attr->values[0].string.text);
5441 return;
5442 }
5443 else if (!strcmp(attr->name, "notify-natural-language") &&
5444 (attr->value_tag != IPP_TAG_LANGUAGE ||
5445 strcmp(attr->values[0].string.text, DefaultLanguage)))
5446 {
5447 send_ipp_status(con, IPP_CHARSET,
5448 _("Language \"%s\" not supported."),
5449 attr->values[0].string.text);
5450 return;
5451 }
5452 else if (!strcmp(attr->name, "notify-user-data") &&
5453 attr->value_tag == IPP_TAG_STRING)
5454 {
5455 if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
5456 {
5457 send_ipp_status(con, IPP_REQUEST_VALUE,
5458 _("The notify-user-data value is too large "
5459 "(%d > 63 octets)."),
5460 attr->values[0].unknown.length);
5461 return;
5462 }
5463
5464 user_data = attr;
5465 }
5466 else if (!strcmp(attr->name, "notify-events") &&
5467 attr->value_tag == IPP_TAG_KEYWORD)
5468 notify_events = attr;
5469 else if (!strcmp(attr->name, "notify-lease-duration") &&
5470 attr->value_tag == IPP_TAG_INTEGER)
5471 lease = attr->values[0].integer;
5472 else if (!strcmp(attr->name, "notify-time-interval") &&
5473 attr->value_tag == IPP_TAG_INTEGER)
5474 interval = attr->values[0].integer;
5475 else if (!strcmp(attr->name, "notify-job-id") &&
5476 attr->value_tag == IPP_TAG_INTEGER)
5477 jobid = attr->values[0].integer;
5478
5479 attr = attr->next;
5480 }
5481
5482 if (notify_events)
5483 {
5484 for (i = 0; i < notify_events->num_values; i ++)
5485 mask |= cupsdEventValue(notify_events->values[i].string.text);
5486 }
5487
5488 if (recipient)
5489 cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient);
5490 if (pullmethod)
5491 cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod);
5492 cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease);
5493 cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval);
5494
5495 if (!recipient && !pullmethod)
5496 break;
5497
5498 if (mask == CUPSD_EVENT_NONE)
5499 {
5500 if (jobid)
5501 mask = CUPSD_EVENT_JOB_COMPLETED;
5502 else if (printer)
5503 mask = CUPSD_EVENT_PRINTER_STATE_CHANGED;
5504 else
5505 {
5506 send_ipp_status(con, IPP_BAD_REQUEST,
5507 _("notify-events not specified."));
5508 return;
5509 }
5510 }
5511
5512 if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration))
5513 {
5514 cupsdLogMessage(CUPSD_LOG_INFO,
5515 "create_subscription: Limiting notify-lease-duration to "
5516 "%d seconds.",
5517 MaxLeaseDuration);
5518 lease = MaxLeaseDuration;
5519 }
5520
5521 if (jobid)
5522 {
5523 if ((job = cupsdFindJob(jobid)) == NULL)
5524 {
5525 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
5526 jobid);
5527 return;
5528 }
5529 }
5530 else
5531 job = NULL;
5532
5533 if ((sub = cupsdAddSubscription(mask, printer, job, recipient, 0)) == NULL)
5534 {
5535 send_ipp_status(con, IPP_TOO_MANY_SUBSCRIPTIONS,
5536 _("There are too many subscriptions."));
5537 return;
5538 }
5539
5540 if (job)
5541 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription #%d for job %d.",
5542 sub->id, job->id);
5543 else if (printer)
5544 cupsdLogMessage(CUPSD_LOG_DEBUG,
5545 "Added subscription #%d for printer \"%s\".",
5546 sub->id, printer->name);
5547 else
5548 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription #%d for server.",
5549 sub->id);
5550
5551 sub->interval = interval;
5552 sub->lease = lease;
5553 sub->expire = lease ? time(NULL) + lease : 0;
5554
5555 cupsdSetString(&sub->owner, username);
5556
5557 if (user_data)
5558 {
5559 sub->user_data_len = user_data->values[0].unknown.length;
5560 memcpy(sub->user_data, user_data->values[0].unknown.data,
5561 sub->user_data_len);
5562 }
5563
5564 ippAddSeparator(con->response);
5565 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5566 "notify-subscription-id", sub->id);
5567
5568 con->response->request.status.status_code = IPP_OK;
5569
5570 if (attr)
5571 attr = attr->next;
5572 }
5573
5574 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
5575 }
5576
5577
5578 /*
5579 * 'delete_printer()' - Remove a printer or class from the system.
5580 */
5581
5582 static void
5583 delete_printer(cupsd_client_t *con, /* I - Client connection */
5584 ipp_attribute_t *uri) /* I - URI of printer or class */
5585 {
5586 http_status_t status; /* Policy status */
5587 cups_ptype_t dtype; /* Destination type (printer/class) */
5588 cupsd_printer_t *printer; /* Printer/class */
5589 char filename[1024]; /* Script/PPD filename */
5590
5591
5592 cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con,
5593 con->number, uri->values[0].string.text);
5594
5595 /*
5596 * Do we have a valid URI?
5597 */
5598
5599 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
5600 {
5601 /*
5602 * Bad URI...
5603 */
5604
5605 send_ipp_status(con, IPP_NOT_FOUND,
5606 _("The printer or class does not exist."));
5607 return;
5608 }
5609
5610 /*
5611 * Check policy...
5612 */
5613
5614 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5615 {
5616 send_http_error(con, status, NULL);
5617 return;
5618 }
5619
5620 /*
5621 * Remove old jobs...
5622 */
5623
5624 cupsdCancelJobs(printer->name, NULL, 1);
5625
5626 /*
5627 * Remove old subscriptions and send a "deleted printer" event...
5628 */
5629
5630 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL,
5631 "%s \"%s\" deleted by \"%s\".",
5632 (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
5633 printer->name, get_username(con));
5634
5635 cupsdExpireSubscriptions(printer, NULL);
5636
5637 /*
5638 * Remove any old PPD or script files...
5639 */
5640
5641 snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
5642 printer->name);
5643 unlink(filename);
5644 snprintf(filename, sizeof(filename), "%s/interfaces/%s.O", ServerRoot,
5645 printer->name);
5646 unlink(filename);
5647
5648 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
5649 printer->name);
5650 unlink(filename);
5651 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd.O", ServerRoot,
5652 printer->name);
5653 unlink(filename);
5654
5655 snprintf(filename, sizeof(filename), "%s/%s.png", CacheDir, printer->name);
5656 unlink(filename);
5657
5658 snprintf(filename, sizeof(filename), "%s/%s.data", CacheDir, printer->name);
5659 unlink(filename);
5660
5661 /*
5662 * Unregister color profiles...
5663 */
5664
5665 cupsdUnregisterColor(printer);
5666
5667 if (dtype & CUPS_PRINTER_CLASS)
5668 {
5669 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".",
5670 printer->name, get_username(con));
5671
5672 cupsdDeletePrinter(printer, 0);
5673 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
5674 }
5675 else
5676 {
5677 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".",
5678 printer->name, get_username(con));
5679
5680 if (cupsdDeletePrinter(printer, 0))
5681 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
5682
5683 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5684 }
5685
5686 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
5687
5688 /*
5689 * Return with no errors...
5690 */
5691
5692 con->response->request.status.status_code = IPP_OK;
5693 }
5694
5695
5696 /*
5697 * 'get_default()' - Get the default destination.
5698 */
5699
5700 static void
5701 get_default(cupsd_client_t *con) /* I - Client connection */
5702 {
5703 http_status_t status; /* Policy status */
5704 cups_array_t *ra; /* Requested attributes array */
5705
5706
5707 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->number);
5708
5709 /*
5710 * Check policy...
5711 */
5712
5713 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5714 {
5715 send_http_error(con, status, NULL);
5716 return;
5717 }
5718
5719 if (DefaultPrinter)
5720 {
5721 ra = create_requested_array(con->request);
5722
5723 copy_printer_attrs(con, DefaultPrinter, ra);
5724
5725 cupsArrayDelete(ra);
5726
5727 con->response->request.status.status_code = IPP_OK;
5728 }
5729 else
5730 send_ipp_status(con, IPP_NOT_FOUND, _("No default printer."));
5731 }
5732
5733
5734 /*
5735 * 'get_devices()' - Get the list of available devices on the local system.
5736 */
5737
5738 static void
5739 get_devices(cupsd_client_t *con) /* I - Client connection */
5740 {
5741 http_status_t status; /* Policy status */
5742 ipp_attribute_t *limit, /* limit attribute */
5743 *timeout, /* timeout attribute */
5744 *requested, /* requested-attributes attribute */
5745 *exclude, /* exclude-schemes attribute */
5746 *include; /* include-schemes attribute */
5747 char command[1024], /* cups-deviced command */
5748 options[2048], /* Options to pass to command */
5749 requested_str[256],
5750 /* String for requested attributes */
5751 exclude_str[512],
5752 /* String for excluded schemes */
5753 include_str[512];
5754 /* String for included schemes */
5755
5756
5757 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->number);
5758
5759 /*
5760 * Check policy...
5761 */
5762
5763 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5764 {
5765 send_http_error(con, status, NULL);
5766 return;
5767 }
5768
5769 /*
5770 * Run cups-deviced command with the given options...
5771 */
5772
5773 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
5774 timeout = ippFindAttribute(con->request, "timeout", IPP_TAG_INTEGER);
5775 requested = ippFindAttribute(con->request, "requested-attributes",
5776 IPP_TAG_KEYWORD);
5777 exclude = ippFindAttribute(con->request, "exclude-schemes", IPP_TAG_NAME);
5778 include = ippFindAttribute(con->request, "include-schemes", IPP_TAG_NAME);
5779
5780 if (requested)
5781 url_encode_attr(requested, requested_str, sizeof(requested_str));
5782 else
5783 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
5784
5785 if (exclude)
5786 url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
5787 else
5788 exclude_str[0] = '\0';
5789
5790 if (include)
5791 url_encode_attr(include, include_str, sizeof(include_str));
5792 else
5793 include_str[0] = '\0';
5794
5795 snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin);
5796 snprintf(options, sizeof(options),
5797 "%d+%d+%d+%d+%s%s%s%s%s",
5798 con->request->request.op.request_id,
5799 limit ? limit->values[0].integer : 0,
5800 timeout ? timeout->values[0].integer : 15,
5801 (int)User,
5802 requested_str,
5803 exclude_str[0] ? "%20" : "", exclude_str,
5804 include_str[0] ? "%20" : "", include_str);
5805
5806 if (cupsdSendCommand(con, command, options, 1))
5807 {
5808 /*
5809 * Command started successfully, don't send an IPP response here...
5810 */
5811
5812 ippDelete(con->response);
5813 con->response = NULL;
5814 }
5815 else
5816 {
5817 /*
5818 * Command failed, return "internal error" so the user knows something
5819 * went wrong...
5820 */
5821
5822 send_ipp_status(con, IPP_INTERNAL_ERROR,
5823 _("cups-deviced failed to execute."));
5824 }
5825 }
5826
5827
5828 /*
5829 * 'get_document()' - Get a copy of a job file.
5830 */
5831
5832 static void
5833 get_document(cupsd_client_t *con, /* I - Client connection */
5834 ipp_attribute_t *uri) /* I - Job URI */
5835 {
5836 http_status_t status; /* Policy status */
5837 ipp_attribute_t *attr; /* Current attribute */
5838 int jobid; /* Job ID */
5839 int docnum; /* Document number */
5840 cupsd_job_t *job; /* Current job */
5841 char scheme[HTTP_MAX_URI], /* Method portion of URI */
5842 username[HTTP_MAX_URI], /* Username portion of URI */
5843 host[HTTP_MAX_URI], /* Host portion of URI */
5844 resource[HTTP_MAX_URI]; /* Resource portion of URI */
5845 int port; /* Port portion of URI */
5846 char filename[1024], /* Filename for document */
5847 format[1024]; /* Format for document */
5848
5849
5850 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_document(%p[%d], %s)", con,
5851 con->number, uri->values[0].string.text);
5852
5853 /*
5854 * See if we have a job URI or a printer URI...
5855 */
5856
5857 if (!strcmp(uri->name, "printer-uri"))
5858 {
5859 /*
5860 * Got a printer URI; see if we also have a job-id attribute...
5861 */
5862
5863 if ((attr = ippFindAttribute(con->request, "job-id",
5864 IPP_TAG_INTEGER)) == NULL)
5865 {
5866 send_ipp_status(con, IPP_BAD_REQUEST,
5867 _("Got a printer-uri attribute but no job-id."));
5868 return;
5869 }
5870
5871 jobid = attr->values[0].integer;
5872 }
5873 else
5874 {
5875 /*
5876 * Got a job URI; parse it to get the job ID...
5877 */
5878
5879 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
5880 sizeof(scheme), username, sizeof(username), host,
5881 sizeof(host), &port, resource, sizeof(resource));
5882
5883 if (strncmp(resource, "/jobs/", 6))
5884 {
5885 /*
5886 * Not a valid URI!
5887 */
5888
5889 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
5890 uri->values[0].string.text);
5891 return;
5892 }
5893
5894 jobid = atoi(resource + 6);
5895 }
5896
5897 /*
5898 * See if the job exists...
5899 */
5900
5901 if ((job = cupsdFindJob(jobid)) == NULL)
5902 {
5903 /*
5904 * Nope - return a "not found" error...
5905 */
5906
5907 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
5908 return;
5909 }
5910
5911 /*
5912 * Check policy...
5913 */
5914
5915 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con,
5916 job->username)) != HTTP_OK)
5917 {
5918 send_http_error(con, status, NULL);
5919 return;
5920 }
5921
5922 /*
5923 * Get the document number...
5924 */
5925
5926 if ((attr = ippFindAttribute(con->request, "document-number",
5927 IPP_TAG_INTEGER)) == NULL)
5928 {
5929 send_ipp_status(con, IPP_BAD_REQUEST,
5930 _("Missing document-number attribute."));
5931 return;
5932 }
5933
5934 if ((docnum = attr->values[0].integer) < 1 || docnum > job->num_files ||
5935 attr->num_values > 1)
5936 {
5937 send_ipp_status(con, IPP_NOT_FOUND,
5938 _("Document #%d does not exist in job #%d."), docnum,
5939 jobid);
5940 return;
5941 }
5942
5943 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, jobid,
5944 docnum);
5945 if ((con->file = open(filename, O_RDONLY)) == -1)
5946 {
5947 cupsdLogMessage(CUPSD_LOG_ERROR,
5948 "Unable to open document %d in job %d - %s", docnum, jobid,
5949 strerror(errno));
5950 send_ipp_status(con, IPP_NOT_FOUND,
5951 _("Unable to open document #%d in job #%d."), docnum,
5952 jobid);
5953 return;
5954 }
5955
5956 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
5957
5958 cupsdLoadJob(job);
5959
5960 snprintf(format, sizeof(format), "%s/%s", job->filetypes[docnum - 1]->super,
5961 job->filetypes[docnum - 1]->type);
5962
5963 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
5964 NULL, format);
5965 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "document-number",
5966 docnum);
5967 if ((attr = ippFindAttribute(job->attrs, "document-name",
5968 IPP_TAG_NAME)) != NULL)
5969 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "document-name",
5970 NULL, attr->values[0].string.text);
5971 }
5972
5973
5974 /*
5975 * 'get_job_attrs()' - Get job attributes.
5976 */
5977
5978 static void
5979 get_job_attrs(cupsd_client_t *con, /* I - Client connection */
5980 ipp_attribute_t *uri) /* I - Job URI */
5981 {
5982 http_status_t status; /* Policy status */
5983 ipp_attribute_t *attr; /* Current attribute */
5984 int jobid; /* Job ID */
5985 cupsd_job_t *job; /* Current job */
5986 cupsd_printer_t *printer; /* Current printer */
5987 cupsd_policy_t *policy; /* Current security policy */
5988 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
5989 username[HTTP_MAX_URI], /* Username portion of URI */
5990 host[HTTP_MAX_URI], /* Host portion of URI */
5991 resource[HTTP_MAX_URI]; /* Resource portion of URI */
5992 int port; /* Port portion of URI */
5993 cups_array_t *ra, /* Requested attributes array */
5994 *exclude; /* Private attributes array */
5995
5996
5997 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con,
5998 con->number, uri->values[0].string.text);
5999
6000 /*
6001 * See if we have a job URI or a printer URI...
6002 */
6003
6004 if (!strcmp(uri->name, "printer-uri"))
6005 {
6006 /*
6007 * Got a printer URI; see if we also have a job-id attribute...
6008 */
6009
6010 if ((attr = ippFindAttribute(con->request, "job-id",
6011 IPP_TAG_INTEGER)) == NULL)
6012 {
6013 send_ipp_status(con, IPP_BAD_REQUEST,
6014 _("Got a printer-uri attribute but no job-id."));
6015 return;
6016 }
6017
6018 jobid = attr->values[0].integer;
6019 }
6020 else
6021 {
6022 /*
6023 * Got a job URI; parse it to get the job ID...
6024 */
6025
6026 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6027 sizeof(scheme), username, sizeof(username), host,
6028 sizeof(host), &port, resource, sizeof(resource));
6029
6030 if (strncmp(resource, "/jobs/", 6))
6031 {
6032 /*
6033 * Not a valid URI!
6034 */
6035
6036 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
6037 uri->values[0].string.text);
6038 return;
6039 }
6040
6041 jobid = atoi(resource + 6);
6042 }
6043
6044 /*
6045 * See if the job exists...
6046 */
6047
6048 if ((job = cupsdFindJob(jobid)) == NULL)
6049 {
6050 /*
6051 * Nope - return a "not found" error...
6052 */
6053
6054 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
6055 return;
6056 }
6057
6058 /*
6059 * Check policy...
6060 */
6061
6062 if ((printer = job->printer) == NULL)
6063 printer = cupsdFindDest(job->dest);
6064
6065 if (printer)
6066 policy = printer->op_policy_ptr;
6067 else
6068 policy = DefaultPolicyPtr;
6069
6070 if ((status = cupsdCheckPolicy(policy, con, job->username)) != HTTP_OK)
6071 {
6072 send_http_error(con, status, NULL);
6073 return;
6074 }
6075
6076 exclude = cupsdGetPrivateAttrs(policy, con, printer, job->username);
6077
6078 /*
6079 * Copy attributes...
6080 */
6081
6082 cupsdLoadJob(job);
6083
6084 ra = create_requested_array(con->request);
6085 copy_job_attrs(con, job, ra, exclude);
6086 cupsArrayDelete(ra);
6087
6088 con->response->request.status.status_code = IPP_OK;
6089 }
6090
6091
6092 /*
6093 * 'get_jobs()' - Get a list of jobs for the specified printer.
6094 */
6095
6096 static void
6097 get_jobs(cupsd_client_t *con, /* I - Client connection */
6098 ipp_attribute_t *uri) /* I - Printer URI */
6099 {
6100 http_status_t status; /* Policy status */
6101 ipp_attribute_t *attr; /* Current attribute */
6102 const char *dest; /* Destination */
6103 cups_ptype_t dtype; /* Destination type (printer/class) */
6104 cups_ptype_t dmask; /* Destination type mask */
6105 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
6106 username[HTTP_MAX_URI], /* Username portion of URI */
6107 host[HTTP_MAX_URI], /* Host portion of URI */
6108 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6109 int port; /* Port portion of URI */
6110 int job_comparison; /* Job comparison */
6111 ipp_jstate_t job_state; /* job-state value */
6112 int first_job_id; /* First job ID */
6113 int limit; /* Maximum number of jobs to return */
6114 int count; /* Number of jobs that match */
6115 ipp_attribute_t *job_ids; /* job-ids attribute */
6116 cupsd_job_t *job; /* Current job pointer */
6117 cupsd_printer_t *printer; /* Printer */
6118 cups_array_t *list; /* Which job list... */
6119 cups_array_t *ra, /* Requested attributes array */
6120 *exclude; /* Private attributes array */
6121 cupsd_policy_t *policy; /* Current policy */
6122
6123
6124 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->number,
6125 uri->values[0].string.text);
6126
6127 /*
6128 * Is the destination valid?
6129 */
6130
6131 if (strcmp(uri->name, "printer-uri"))
6132 {
6133 send_ipp_status(con, IPP_BAD_REQUEST, _("No printer-uri in request."));
6134 return;
6135 }
6136
6137 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6138 sizeof(scheme), username, sizeof(username), host,
6139 sizeof(host), &port, resource, sizeof(resource));
6140
6141 if (!strcmp(resource, "/") || !strcmp(resource, "/jobs"))
6142 {
6143 dest = NULL;
6144 dtype = (cups_ptype_t)0;
6145 dmask = (cups_ptype_t)0;
6146 printer = NULL;
6147 }
6148 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
6149 {
6150 dest = NULL;
6151 dtype = (cups_ptype_t)0;
6152 dmask = CUPS_PRINTER_CLASS;
6153 printer = NULL;
6154 }
6155 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
6156 {
6157 dest = NULL;
6158 dtype = CUPS_PRINTER_CLASS;
6159 dmask = CUPS_PRINTER_CLASS;
6160 printer = NULL;
6161 }
6162 else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype,
6163 &printer)) == NULL)
6164 {
6165 /*
6166 * Bad URI...
6167 */
6168
6169 send_ipp_status(con, IPP_NOT_FOUND,
6170 _("The printer or class does not exist."));
6171 return;
6172 }
6173 else
6174 {
6175 dtype &= CUPS_PRINTER_CLASS;
6176 dmask = CUPS_PRINTER_CLASS;
6177 }
6178
6179 /*
6180 * Check policy...
6181 */
6182
6183 if (printer)
6184 policy = printer->op_policy_ptr;
6185 else
6186 policy = DefaultPolicyPtr;
6187
6188 if ((status = cupsdCheckPolicy(policy, con, NULL)) != HTTP_OK)
6189 {
6190 send_http_error(con, status, NULL);
6191 return;
6192 }
6193
6194 job_ids = ippFindAttribute(con->request, "job-ids", IPP_TAG_INTEGER);
6195
6196 /*
6197 * See if the "which-jobs" attribute have been specified...
6198 */
6199
6200 if ((attr = ippFindAttribute(con->request, "which-jobs",
6201 IPP_TAG_KEYWORD)) != NULL && job_ids)
6202 {
6203 send_ipp_status(con, IPP_CONFLICT,
6204 _("The %s attribute cannot be provided with job-ids."),
6205 "which-jobs");
6206 return;
6207 }
6208 else if (!attr || !strcmp(attr->values[0].string.text, "not-completed"))
6209 {
6210 job_comparison = -1;
6211 job_state = IPP_JOB_STOPPED;
6212 list = Jobs;
6213 }
6214 else if (!strcmp(attr->values[0].string.text, "completed"))
6215 {
6216 job_comparison = 1;
6217 job_state = IPP_JOB_CANCELED;
6218 list = Jobs;
6219 }
6220 else if (!strcmp(attr->values[0].string.text, "aborted"))
6221 {
6222 job_comparison = 0;
6223 job_state = IPP_JOB_ABORTED;
6224 list = Jobs;
6225 }
6226 else if (!strcmp(attr->values[0].string.text, "all"))
6227 {
6228 job_comparison = 1;
6229 job_state = IPP_JOB_PENDING;
6230 list = Jobs;
6231 }
6232 else if (!strcmp(attr->values[0].string.text, "canceled"))
6233 {
6234 job_comparison = 0;
6235 job_state = IPP_JOB_CANCELED;
6236 list = Jobs;
6237 }
6238 else if (!strcmp(attr->values[0].string.text, "pending"))
6239 {
6240 job_comparison = 0;
6241 job_state = IPP_JOB_PENDING;
6242 list = ActiveJobs;
6243 }
6244 else if (!strcmp(attr->values[0].string.text, "pending-held"))
6245 {
6246 job_comparison = 0;
6247 job_state = IPP_JOB_HELD;
6248 list = ActiveJobs;
6249 }
6250 else if (!strcmp(attr->values[0].string.text, "processing"))
6251 {
6252 job_comparison = 0;
6253 job_state = IPP_JOB_PROCESSING;
6254 list = PrintingJobs;
6255 }
6256 else if (!strcmp(attr->values[0].string.text, "processing-stopped"))
6257 {
6258 job_comparison = 0;
6259 job_state = IPP_JOB_STOPPED;
6260 list = ActiveJobs;
6261 }
6262 else
6263 {
6264 send_ipp_status(con, IPP_ATTRIBUTES,
6265 _("The which-jobs value \"%s\" is not supported."),
6266 attr->values[0].string.text);
6267 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
6268 "which-jobs", NULL, attr->values[0].string.text);
6269 return;
6270 }
6271
6272 /*
6273 * See if they want to limit the number of jobs reported...
6274 */
6275
6276 if ((attr = ippFindAttribute(con->request, "limit",
6277 IPP_TAG_INTEGER)) != NULL)
6278 {
6279 if (job_ids)
6280 {
6281 send_ipp_status(con, IPP_CONFLICT,
6282 _("The %s attribute cannot be provided with job-ids."),
6283 "limit");
6284 return;
6285 }
6286
6287 limit = attr->values[0].integer;
6288 }
6289 else
6290 limit = 0;
6291
6292 if ((attr = ippFindAttribute(con->request, "first-job-id",
6293 IPP_TAG_INTEGER)) != NULL)
6294 {
6295 if (job_ids)
6296 {
6297 send_ipp_status(con, IPP_CONFLICT,
6298 _("The %s attribute cannot be provided with job-ids."),
6299 "first-job-id");
6300 return;
6301 }
6302
6303 first_job_id = attr->values[0].integer;
6304 }
6305 else
6306 first_job_id = 1;
6307
6308 /*
6309 * See if we only want to see jobs for a specific user...
6310 */
6311
6312 if ((attr = ippFindAttribute(con->request, "my-jobs",
6313 IPP_TAG_BOOLEAN)) != NULL && job_ids)
6314 {
6315 send_ipp_status(con, IPP_CONFLICT,
6316 _("The %s attribute cannot be provided with job-ids."),
6317 "my-jobs");
6318 return;
6319 }
6320 else if (attr && attr->values[0].boolean)
6321 strlcpy(username, get_username(con), sizeof(username));
6322 else
6323 username[0] = '\0';
6324
6325 ra = create_requested_array(con->request);
6326
6327 /*
6328 * OK, build a list of jobs for this printer...
6329 */
6330
6331 if (job_ids)
6332 {
6333 int i; /* Looping var */
6334
6335 for (i = 0; i < job_ids->num_values; i ++)
6336 {
6337 if (!cupsdFindJob(job_ids->values[i].integer))
6338 break;
6339 }
6340
6341 if (i < job_ids->num_values)
6342 {
6343 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
6344 job_ids->values[i].integer);
6345 return;
6346 }
6347
6348 for (i = 0; i < job_ids->num_values; i ++)
6349 {
6350 job = cupsdFindJob(job_ids->values[i].integer);
6351
6352 cupsdLoadJob(job);
6353
6354 if (!job->attrs)
6355 {
6356 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d",
6357 job->id);
6358 continue;
6359 }
6360
6361 if (i > 0)
6362 ippAddSeparator(con->response);
6363
6364 exclude = cupsdGetPrivateAttrs(job->printer ?
6365 job->printer->op_policy_ptr :
6366 policy, con, job->printer,
6367 job->username);
6368
6369 copy_job_attrs(con, job, ra, exclude);
6370 }
6371 }
6372 else
6373 {
6374 for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list);
6375 (limit <= 0 || count < limit) && job;
6376 job = (cupsd_job_t *)cupsArrayNext(list))
6377 {
6378 /*
6379 * Filter out jobs that don't match...
6380 */
6381
6382 cupsdLogMessage(CUPSD_LOG_DEBUG2,
6383 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
6384 "state_value=%d, attrs=%p", job->id, job->dest,
6385 job->username, job->state_value, job->attrs);
6386
6387 if (!job->dest || !job->username)
6388 cupsdLoadJob(job);
6389
6390 if (!job->dest || !job->username)
6391 continue;
6392
6393 if ((dest && strcmp(job->dest, dest)) &&
6394 (!job->printer || !dest || strcmp(job->printer->name, dest)))
6395 continue;
6396 if ((job->dtype & dmask) != dtype &&
6397 (!job->printer || (job->printer->type & dmask) != dtype))
6398 continue;
6399
6400 if ((job_comparison < 0 && job->state_value > job_state) ||
6401 (job_comparison == 0 && job->state_value != job_state) ||
6402 (job_comparison > 0 && job->state_value < job_state))
6403 continue;
6404
6405 if (job->id < first_job_id)
6406 continue;
6407
6408 cupsdLoadJob(job);
6409
6410 if (!job->attrs)
6411 {
6412 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d",
6413 job->id);
6414 continue;
6415 }
6416
6417 if (username[0] && _cups_strcasecmp(username, job->username))
6418 continue;
6419
6420 if (count > 0)
6421 ippAddSeparator(con->response);
6422
6423 count ++;
6424
6425 exclude = cupsdGetPrivateAttrs(job->printer ?
6426 job->printer->op_policy_ptr :
6427 policy, con, job->printer,
6428 job->username);
6429
6430 copy_job_attrs(con, job, ra, exclude);
6431 }
6432
6433 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count=%d", count);
6434 }
6435
6436 cupsArrayDelete(ra);
6437
6438 con->response->request.status.status_code = IPP_OK;
6439 }
6440
6441
6442 /*
6443 * 'get_notifications()' - Get events for a subscription.
6444 */
6445
6446 static void
6447 get_notifications(cupsd_client_t *con) /* I - Client connection */
6448 {
6449 int i, j; /* Looping vars */
6450 http_status_t status; /* Policy status */
6451 cupsd_subscription_t *sub; /* Subscription */
6452 ipp_attribute_t *ids, /* notify-subscription-ids */
6453 *sequences; /* notify-sequence-numbers */
6454 int min_seq; /* Minimum sequence number */
6455 int interval; /* Poll interval */
6456
6457
6458 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])",
6459 con, con->number);
6460
6461 /*
6462 * Get subscription attributes...
6463 */
6464
6465 ids = ippFindAttribute(con->request, "notify-subscription-ids",
6466 IPP_TAG_INTEGER);
6467 sequences = ippFindAttribute(con->request, "notify-sequence-numbers",
6468 IPP_TAG_INTEGER);
6469
6470 if (!ids)
6471 {
6472 send_ipp_status(con, IPP_BAD_REQUEST,
6473 _("Missing notify-subscription-ids attribute."));
6474 return;
6475 }
6476
6477 /*
6478 * Are the subscription IDs valid?
6479 */
6480
6481 for (i = 0, interval = 60; i < ids->num_values; i ++)
6482 {
6483 if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL)
6484 {
6485 /*
6486 * Bad subscription ID...
6487 */
6488
6489 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
6490 ids->values[i].integer);
6491 return;
6492 }
6493
6494 /*
6495 * Check policy...
6496 */
6497
6498 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
6499 DefaultPolicyPtr,
6500 con, sub->owner)) != HTTP_OK)
6501 {
6502 send_http_error(con, status, sub->dest);
6503 return;
6504 }
6505
6506 /*
6507 * Check the subscription type and update the interval accordingly.
6508 */
6509
6510 if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING &&
6511 interval > 10)
6512 interval = 10;
6513 else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED)
6514 interval = 0;
6515 else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING &&
6516 interval > 30)
6517 interval = 30;
6518 }
6519
6520 /*
6521 * Tell the client to poll again in N seconds...
6522 */
6523
6524 if (interval > 0)
6525 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
6526 "notify-get-interval", interval);
6527
6528 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
6529 "printer-up-time", time(NULL));
6530
6531 /*
6532 * Copy the subscription event attributes to the response.
6533 */
6534
6535 con->response->request.status.status_code =
6536 interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE;
6537
6538 for (i = 0; i < ids->num_values; i ++)
6539 {
6540 /*
6541 * Get the subscription and sequence number...
6542 */
6543
6544 sub = cupsdFindSubscription(ids->values[i].integer);
6545
6546 if (sequences && i < sequences->num_values)
6547 min_seq = sequences->values[i].integer;
6548 else
6549 min_seq = 1;
6550
6551 /*
6552 * If we don't have any new events, nothing to do here...
6553 */
6554
6555 if (min_seq > (sub->first_event_id + cupsArrayCount(sub->events)))
6556 continue;
6557
6558 /*
6559 * Otherwise copy all of the new events...
6560 */
6561
6562 if (sub->first_event_id > min_seq)
6563 j = 0;
6564 else
6565 j = min_seq - sub->first_event_id;
6566
6567 for (; j < cupsArrayCount(sub->events); j ++)
6568 {
6569 ippAddSeparator(con->response);
6570
6571 copy_attrs(con->response,
6572 ((cupsd_event_t *)cupsArrayIndex(sub->events, j))->attrs, NULL,
6573 IPP_TAG_EVENT_NOTIFICATION, 0, NULL);
6574 }
6575 }
6576 }
6577
6578
6579 /*
6580 * 'get_ppd()' - Get a named PPD from the local system.
6581 */
6582
6583 static void
6584 get_ppd(cupsd_client_t *con, /* I - Client connection */
6585 ipp_attribute_t *uri) /* I - Printer URI or PPD name */
6586 {
6587 http_status_t status; /* Policy status */
6588 cupsd_printer_t *dest; /* Destination */
6589 cups_ptype_t dtype; /* Destination type */
6590
6591
6592 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con,
6593 con->number, uri, uri->name, uri->values[0].string.text);
6594
6595 if (!strcmp(uri->name, "ppd-name"))
6596 {
6597 /*
6598 * Return a PPD file from cups-driverd...
6599 */
6600
6601 char command[1024], /* cups-driverd command */
6602 options[1024], /* Options to pass to command */
6603 ppd_name[1024]; /* ppd-name */
6604
6605
6606 /*
6607 * Check policy...
6608 */
6609
6610 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6611 {
6612 send_http_error(con, status, NULL);
6613 return;
6614 }
6615
6616 /*
6617 * Run cups-driverd command with the given options...
6618 */
6619
6620 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
6621 url_encode_string(uri->values[0].string.text, ppd_name, sizeof(ppd_name));
6622 snprintf(options, sizeof(options), "get+%d+%s",
6623 con->request->request.op.request_id, ppd_name);
6624
6625 if (cupsdSendCommand(con, command, options, 0))
6626 {
6627 /*
6628 * Command started successfully, don't send an IPP response here...
6629 */
6630
6631 ippDelete(con->response);
6632 con->response = NULL;
6633 }
6634 else
6635 {
6636 /*
6637 * Command failed, return "internal error" so the user knows something
6638 * went wrong...
6639 */
6640
6641 send_ipp_status(con, IPP_INTERNAL_ERROR,
6642 _("cups-driverd failed to execute."));
6643 }
6644 }
6645 else if (!strcmp(uri->name, "printer-uri") &&
6646 cupsdValidateDest(uri->values[0].string.text, &dtype, &dest))
6647 {
6648 int i; /* Looping var */
6649 char filename[1024]; /* PPD filename */
6650
6651
6652 /*
6653 * Check policy...
6654 */
6655
6656 if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK)
6657 {
6658 send_http_error(con, status, dest);
6659 return;
6660 }
6661
6662 /*
6663 * See if we need the PPD for a class or remote printer...
6664 */
6665
6666 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
6667 dest->name);
6668
6669 if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0))
6670 {
6671 con->response->request.status.status_code = CUPS_SEE_OTHER;
6672 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI,
6673 "printer-uri", NULL, dest->uri);
6674 return;
6675 }
6676 else if (dtype & CUPS_PRINTER_CLASS)
6677 {
6678 for (i = 0; i < dest->num_printers; i ++)
6679 if (!(dest->printers[i]->type & CUPS_PRINTER_CLASS))
6680 {
6681 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
6682 dest->printers[i]->name);
6683
6684 if (!access(filename, 0))
6685 break;
6686 }
6687
6688 if (i < dest->num_printers)
6689 dest = dest->printers[i];
6690 else
6691 {
6692 con->response->request.status.status_code = CUPS_SEE_OTHER;
6693 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI,
6694 "printer-uri", NULL, dest->printers[0]->uri);
6695 return;
6696 }
6697 }
6698
6699 /*
6700 * Found the printer with the PPD file, now see if there is one...
6701 */
6702
6703 if ((con->file = open(filename, O_RDONLY)) < 0)
6704 {
6705 send_ipp_status(con, IPP_NOT_FOUND,
6706 _("The PPD file \"%s\" could not be opened: %s"),
6707 uri->values[0].string.text, strerror(errno));
6708 return;
6709 }
6710
6711 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
6712
6713 con->pipe_pid = 0;
6714
6715 con->response->request.status.status_code = IPP_OK;
6716 }
6717 else
6718 send_ipp_status(con, IPP_NOT_FOUND,
6719 _("The PPD file \"%s\" could not be found."),
6720 uri->values[0].string.text);
6721 }
6722
6723
6724 /*
6725 * 'get_ppds()' - Get the list of PPD files on the local system.
6726 */
6727
6728 static void
6729 get_ppds(cupsd_client_t *con) /* I - Client connection */
6730 {
6731 http_status_t status; /* Policy status */
6732 ipp_attribute_t *limit, /* Limit attribute */
6733 *device, /* ppd-device-id attribute */
6734 *language, /* ppd-natural-language attribute */
6735 *make, /* ppd-make attribute */
6736 *model, /* ppd-make-and-model attribute */
6737 *model_number, /* ppd-model-number attribute */
6738 *product, /* ppd-product attribute */
6739 *psversion, /* ppd-psverion attribute */
6740 *type, /* ppd-type attribute */
6741 *requested, /* requested-attributes attribute */
6742 *exclude, /* exclude-schemes attribute */
6743 *include; /* include-schemes attribute */
6744 char command[1024], /* cups-driverd command */
6745 options[4096], /* Options to pass to command */
6746 device_str[256],/* Escaped ppd-device-id string */
6747 language_str[256],
6748 /* Escaped ppd-natural-language */
6749 make_str[256], /* Escaped ppd-make string */
6750 model_str[256], /* Escaped ppd-make-and-model string */
6751 model_number_str[256],
6752 /* ppd-model-number string */
6753 product_str[256],
6754 /* Escaped ppd-product string */
6755 psversion_str[256],
6756 /* Escaped ppd-psversion string */
6757 type_str[256], /* Escaped ppd-type string */
6758 requested_str[256],
6759 /* String for requested attributes */
6760 exclude_str[512],
6761 /* String for excluded schemes */
6762 include_str[512];
6763 /* String for included schemes */
6764
6765
6766 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->number);
6767
6768 /*
6769 * Check policy...
6770 */
6771
6772 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6773 {
6774 send_http_error(con, status, NULL);
6775 return;
6776 }
6777
6778 /*
6779 * Run cups-driverd command with the given options...
6780 */
6781
6782 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
6783 device = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT);
6784 language = ippFindAttribute(con->request, "ppd-natural-language",
6785 IPP_TAG_LANGUAGE);
6786 make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT);
6787 model = ippFindAttribute(con->request, "ppd-make-and-model",
6788 IPP_TAG_TEXT);
6789 model_number = ippFindAttribute(con->request, "ppd-model-number",
6790 IPP_TAG_INTEGER);
6791 product = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT);
6792 psversion = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT);
6793 type = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD);
6794 requested = ippFindAttribute(con->request, "requested-attributes",
6795 IPP_TAG_KEYWORD);
6796 exclude = ippFindAttribute(con->request, "exclude-schemes",
6797 IPP_TAG_NAME);
6798 include = ippFindAttribute(con->request, "include-schemes",
6799 IPP_TAG_NAME);
6800
6801 if (requested)
6802 url_encode_attr(requested, requested_str, sizeof(requested_str));
6803 else
6804 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
6805
6806 if (device)
6807 url_encode_attr(device, device_str, sizeof(device_str));
6808 else
6809 device_str[0] = '\0';
6810
6811 if (language)
6812 url_encode_attr(language, language_str, sizeof(language_str));
6813 else
6814 language_str[0] = '\0';
6815
6816 if (make)
6817 url_encode_attr(make, make_str, sizeof(make_str));
6818 else
6819 make_str[0] = '\0';
6820
6821 if (model)
6822 url_encode_attr(model, model_str, sizeof(model_str));
6823 else
6824 model_str[0] = '\0';
6825
6826 if (model_number)
6827 snprintf(model_number_str, sizeof(model_number_str), "ppd-model-number=%d",
6828 model_number->values[0].integer);
6829 else
6830 model_number_str[0] = '\0';
6831
6832 if (product)
6833 url_encode_attr(product, product_str, sizeof(product_str));
6834 else
6835 product_str[0] = '\0';
6836
6837 if (psversion)
6838 url_encode_attr(psversion, psversion_str, sizeof(psversion_str));
6839 else
6840 psversion_str[0] = '\0';
6841
6842 if (type)
6843 url_encode_attr(type, type_str, sizeof(type_str));
6844 else
6845 type_str[0] = '\0';
6846
6847 if (exclude)
6848 url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
6849 else
6850 exclude_str[0] = '\0';
6851
6852 if (include)
6853 url_encode_attr(include, include_str, sizeof(include_str));
6854 else
6855 include_str[0] = '\0';
6856
6857 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
6858 snprintf(options, sizeof(options),
6859 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
6860 con->request->request.op.request_id,
6861 limit ? limit->values[0].integer : 0,
6862 requested_str,
6863 device ? "%20" : "", device_str,
6864 language ? "%20" : "", language_str,
6865 make ? "%20" : "", make_str,
6866 model ? "%20" : "", model_str,
6867 model_number ? "%20" : "", model_number_str,
6868 product ? "%20" : "", product_str,
6869 psversion ? "%20" : "", psversion_str,
6870 type ? "%20" : "", type_str,
6871 exclude_str[0] ? "%20" : "", exclude_str,
6872 include_str[0] ? "%20" : "", include_str);
6873
6874 if (cupsdSendCommand(con, command, options, 0))
6875 {
6876 /*
6877 * Command started successfully, don't send an IPP response here...
6878 */
6879
6880 ippDelete(con->response);
6881 con->response = NULL;
6882 }
6883 else
6884 {
6885 /*
6886 * Command failed, return "internal error" so the user knows something
6887 * went wrong...
6888 */
6889
6890 send_ipp_status(con, IPP_INTERNAL_ERROR,
6891 _("cups-driverd failed to execute."));
6892 }
6893 }
6894
6895
6896 /*
6897 * 'get_printer_attrs()' - Get printer attributes.
6898 */
6899
6900 static void
6901 get_printer_attrs(cupsd_client_t *con, /* I - Client connection */
6902 ipp_attribute_t *uri) /* I - Printer URI */
6903 {
6904 http_status_t status; /* Policy status */
6905 cups_ptype_t dtype; /* Destination type (printer/class) */
6906 cupsd_printer_t *printer; /* Printer/class */
6907 cups_array_t *ra; /* Requested attributes array */
6908
6909
6910 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con,
6911 con->number, uri->values[0].string.text);
6912
6913 /*
6914 * Is the destination valid?
6915 */
6916
6917 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
6918 {
6919 /*
6920 * Bad URI...
6921 */
6922
6923 send_ipp_status(con, IPP_NOT_FOUND,
6924 _("The printer or class does not exist."));
6925 return;
6926 }
6927
6928 /*
6929 * Check policy...
6930 */
6931
6932 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
6933 {
6934 send_http_error(con, status, printer);
6935 return;
6936 }
6937
6938 /*
6939 * Send the attributes...
6940 */
6941
6942 ra = create_requested_array(con->request);
6943
6944 copy_printer_attrs(con, printer, ra);
6945
6946 cupsArrayDelete(ra);
6947
6948 con->response->request.status.status_code = IPP_OK;
6949 }
6950
6951
6952 /*
6953 * 'get_printer_supported()' - Get printer supported values.
6954 */
6955
6956 static void
6957 get_printer_supported(
6958 cupsd_client_t *con, /* I - Client connection */
6959 ipp_attribute_t *uri) /* I - Printer URI */
6960 {
6961 http_status_t status; /* Policy status */
6962 cups_ptype_t dtype; /* Destination type (printer/class) */
6963 cupsd_printer_t *printer; /* Printer/class */
6964
6965
6966 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_supported(%p[%d], %s)", con,
6967 con->number, uri->values[0].string.text);
6968
6969 /*
6970 * Is the destination valid?
6971 */
6972
6973 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
6974 {
6975 /*
6976 * Bad URI...
6977 */
6978
6979 send_ipp_status(con, IPP_NOT_FOUND,
6980 _("The printer or class does not exist."));
6981 return;
6982 }
6983
6984 /*
6985 * Check policy...
6986 */
6987
6988 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
6989 {
6990 send_http_error(con, status, printer);
6991 return;
6992 }
6993
6994 /*
6995 * Return a list of attributes that can be set via Set-Printer-Attributes.
6996 */
6997
6998 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
6999 "printer-info", 0);
7000 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7001 "printer-location", 0);
7002
7003 con->response->request.status.status_code = IPP_OK;
7004 }
7005
7006
7007 /*
7008 * 'get_printers()' - Get a list of printers or classes.
7009 */
7010
7011 static void
7012 get_printers(cupsd_client_t *con, /* I - Client connection */
7013 int type) /* I - 0 or CUPS_PRINTER_CLASS */
7014 {
7015 http_status_t status; /* Policy status */
7016 ipp_attribute_t *attr; /* Current attribute */
7017 int limit; /* Max number of printers to return */
7018 int count; /* Number of printers that match */
7019 cupsd_printer_t *printer; /* Current printer pointer */
7020 int printer_type, /* printer-type attribute */
7021 printer_mask; /* printer-type-mask attribute */
7022 char *location; /* Location string */
7023 const char *username; /* Current user */
7024 char *first_printer_name; /* first-printer-name attribute */
7025 cups_array_t *ra; /* Requested attributes array */
7026 int local; /* Local connection? */
7027
7028
7029 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con,
7030 con->number, type);
7031
7032 /*
7033 * Check policy...
7034 */
7035
7036 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7037 {
7038 send_http_error(con, status, NULL);
7039 return;
7040 }
7041
7042 /*
7043 * Check for printers...
7044 */
7045
7046 if (!Printers || !cupsArrayCount(Printers))
7047 {
7048 send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added."));
7049 return;
7050 }
7051
7052 /*
7053 * See if they want to limit the number of printers reported...
7054 */
7055
7056 if ((attr = ippFindAttribute(con->request, "limit",
7057 IPP_TAG_INTEGER)) != NULL)
7058 limit = attr->values[0].integer;
7059 else
7060 limit = 10000000;
7061
7062 if ((attr = ippFindAttribute(con->request, "first-printer-name",
7063 IPP_TAG_NAME)) != NULL)
7064 first_printer_name = attr->values[0].string.text;
7065 else
7066 first_printer_name = NULL;
7067
7068 /*
7069 * Support filtering...
7070 */
7071
7072 if ((attr = ippFindAttribute(con->request, "printer-type",
7073 IPP_TAG_ENUM)) != NULL)
7074 printer_type = attr->values[0].integer;
7075 else
7076 printer_type = 0;
7077
7078 if ((attr = ippFindAttribute(con->request, "printer-type-mask",
7079 IPP_TAG_ENUM)) != NULL)
7080 printer_mask = attr->values[0].integer;
7081 else
7082 printer_mask = 0;
7083
7084 local = httpAddrLocalhost(&(con->clientaddr));
7085
7086 if ((attr = ippFindAttribute(con->request, "printer-location",
7087 IPP_TAG_TEXT)) != NULL)
7088 location = attr->values[0].string.text;
7089 else
7090 location = NULL;
7091
7092 if (con->username[0])
7093 username = con->username;
7094 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7095 IPP_TAG_NAME)) != NULL)
7096 username = attr->values[0].string.text;
7097 else
7098 username = NULL;
7099
7100 ra = create_requested_array(con->request);
7101
7102 /*
7103 * OK, build a list of printers for this printer...
7104 */
7105
7106 if (first_printer_name)
7107 {
7108 if ((printer = cupsdFindDest(first_printer_name)) == NULL)
7109 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
7110 }
7111 else
7112 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
7113
7114 for (count = 0;
7115 count < limit && printer;
7116 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
7117 {
7118 if (!local && !printer->shared)
7119 continue;
7120
7121 if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) &&
7122 (printer->type & printer_mask) == printer_type &&
7123 (!location ||
7124 (printer->location && !_cups_strcasecmp(printer->location, location))))
7125 {
7126 /*
7127 * If a username is specified, see if it is allowed or denied
7128 * access...
7129 */
7130
7131 if (cupsArrayCount(printer->users) && username &&
7132 !user_allowed(printer, username))
7133 continue;
7134
7135 /*
7136 * Add the group separator as needed...
7137 */
7138
7139 if (count > 0)
7140 ippAddSeparator(con->response);
7141
7142 count ++;
7143
7144 /*
7145 * Send the attributes...
7146 */
7147
7148 copy_printer_attrs(con, printer, ra);
7149 }
7150 }
7151
7152 cupsArrayDelete(ra);
7153
7154 con->response->request.status.status_code = IPP_OK;
7155 }
7156
7157
7158 /*
7159 * 'get_subscription_attrs()' - Get subscription attributes.
7160 */
7161
7162 static void
7163 get_subscription_attrs(
7164 cupsd_client_t *con, /* I - Client connection */
7165 int sub_id) /* I - Subscription ID */
7166 {
7167 http_status_t status; /* Policy status */
7168 cupsd_subscription_t *sub; /* Subscription */
7169 cupsd_policy_t *policy; /* Current security policy */
7170 cups_array_t *ra, /* Requested attributes array */
7171 *exclude; /* Private attributes array */
7172
7173
7174 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7175 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7176 con, con->number, sub_id);
7177
7178 /*
7179 * Is the subscription ID valid?
7180 */
7181
7182 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
7183 {
7184 /*
7185 * Bad subscription ID...
7186 */
7187
7188 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
7189 sub_id);
7190 return;
7191 }
7192
7193 /*
7194 * Check policy...
7195 */
7196
7197 if (sub->dest)
7198 policy = sub->dest->op_policy_ptr;
7199 else
7200 policy = DefaultPolicyPtr;
7201
7202 if ((status = cupsdCheckPolicy(policy, con, sub->owner)) != HTTP_OK)
7203 {
7204 send_http_error(con, status, sub->dest);
7205 return;
7206 }
7207
7208 exclude = cupsdGetPrivateAttrs(policy, con, sub->dest, sub->owner);
7209
7210 /*
7211 * Copy the subscription attributes to the response using the
7212 * requested-attributes attribute that may be provided by the client.
7213 */
7214
7215 ra = create_requested_array(con->request);
7216
7217 copy_subscription_attrs(con, sub, ra, exclude);
7218
7219 cupsArrayDelete(ra);
7220
7221 con->response->request.status.status_code = IPP_OK;
7222 }
7223
7224
7225 /*
7226 * 'get_subscriptions()' - Get subscriptions.
7227 */
7228
7229 static void
7230 get_subscriptions(cupsd_client_t *con, /* I - Client connection */
7231 ipp_attribute_t *uri) /* I - Printer/job URI */
7232 {
7233 http_status_t status; /* Policy status */
7234 int count; /* Number of subscriptions */
7235 int limit; /* Limit */
7236 cupsd_subscription_t *sub; /* Subscription */
7237 cups_array_t *ra; /* Requested attributes array */
7238 ipp_attribute_t *attr; /* Attribute */
7239 cups_ptype_t dtype; /* Destination type (printer/class) */
7240 char scheme[HTTP_MAX_URI],
7241 /* Scheme portion of URI */
7242 username[HTTP_MAX_URI],
7243 /* Username portion of URI */
7244 host[HTTP_MAX_URI],
7245 /* Host portion of URI */
7246 resource[HTTP_MAX_URI];
7247 /* Resource portion of URI */
7248 int port; /* Port portion of URI */
7249 cupsd_job_t *job; /* Job pointer */
7250 cupsd_printer_t *printer; /* Printer */
7251 cupsd_policy_t *policy; /* Policy */
7252 cups_array_t *exclude; /* Private attributes array */
7253
7254
7255 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7256 "get_subscriptions(con=%p[%d], uri=%s)",
7257 con, con->number, uri->values[0].string.text);
7258
7259 /*
7260 * Is the destination valid?
7261 */
7262
7263 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7264 sizeof(scheme), username, sizeof(username), host,
7265 sizeof(host), &port, resource, sizeof(resource));
7266
7267 if (!strcmp(resource, "/") ||
7268 (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) ||
7269 (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) ||
7270 (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9))
7271 {
7272 printer = NULL;
7273 job = NULL;
7274 }
7275 else if (!strncmp(resource, "/jobs/", 6) && resource[6])
7276 {
7277 printer = NULL;
7278 job = cupsdFindJob(atoi(resource + 6));
7279
7280 if (!job)
7281 {
7282 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
7283 atoi(resource + 6));
7284 return;
7285 }
7286 }
7287 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7288 {
7289 /*
7290 * Bad URI...
7291 */
7292
7293 send_ipp_status(con, IPP_NOT_FOUND,
7294 _("The printer or class does not exist."));
7295 return;
7296 }
7297 else if ((attr = ippFindAttribute(con->request, "notify-job-id",
7298 IPP_TAG_INTEGER)) != NULL)
7299 {
7300 job = cupsdFindJob(attr->values[0].integer);
7301
7302 if (!job)
7303 {
7304 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
7305 attr->values[0].integer);
7306 return;
7307 }
7308 }
7309 else
7310 job = NULL;
7311
7312 /*
7313 * Check policy...
7314 */
7315
7316 if (printer)
7317 policy = printer->op_policy_ptr;
7318 else
7319 policy = DefaultPolicyPtr;
7320
7321 if ((status = cupsdCheckPolicy(policy, con, NULL)) != HTTP_OK)
7322 {
7323 send_http_error(con, status, printer);
7324 return;
7325 }
7326
7327 /*
7328 * Copy the subscription attributes to the response using the
7329 * requested-attributes attribute that may be provided by the client.
7330 */
7331
7332 ra = create_requested_array(con->request);
7333
7334 if ((attr = ippFindAttribute(con->request, "limit",
7335 IPP_TAG_INTEGER)) != NULL)
7336 limit = attr->values[0].integer;
7337 else
7338 limit = 0;
7339
7340 /*
7341 * See if we only want to see subscriptions for a specific user...
7342 */
7343
7344 if ((attr = ippFindAttribute(con->request, "my-subscriptions",
7345 IPP_TAG_BOOLEAN)) != NULL &&
7346 attr->values[0].boolean)
7347 strlcpy(username, get_username(con), sizeof(username));
7348 else
7349 username[0] = '\0';
7350
7351 for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0;
7352 sub;
7353 sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
7354 if ((!printer || sub->dest == printer) && (!job || sub->job == job) &&
7355 (!username[0] || !_cups_strcasecmp(username, sub->owner)))
7356 {
7357 ippAddSeparator(con->response);
7358
7359 exclude = cupsdGetPrivateAttrs(sub->dest ? sub->dest->op_policy_ptr :
7360 policy, con, sub->dest,
7361 sub->owner);
7362
7363 copy_subscription_attrs(con, sub, ra, exclude);
7364
7365 count ++;
7366 if (limit && count >= limit)
7367 break;
7368 }
7369
7370 cupsArrayDelete(ra);
7371
7372 if (count)
7373 con->response->request.status.status_code = IPP_OK;
7374 else
7375 send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found."));
7376 }
7377
7378
7379 /*
7380 * 'get_username()' - Get the username associated with a request.
7381 */
7382
7383 static const char * /* O - Username */
7384 get_username(cupsd_client_t *con) /* I - Connection */
7385 {
7386 ipp_attribute_t *attr; /* Attribute */
7387
7388
7389 if (con->username[0])
7390 return (con->username);
7391 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7392 IPP_TAG_NAME)) != NULL)
7393 return (attr->values[0].string.text);
7394 else
7395 return ("anonymous");
7396 }
7397
7398
7399 /*
7400 * 'hold_job()' - Hold a print job.
7401 */
7402
7403 static void
7404 hold_job(cupsd_client_t *con, /* I - Client connection */
7405 ipp_attribute_t *uri) /* I - Job or Printer URI */
7406 {
7407 ipp_attribute_t *attr; /* Current job-hold-until */
7408 const char *when; /* New value */
7409 int jobid; /* Job ID */
7410 char scheme[HTTP_MAX_URI], /* Method portion of URI */
7411 username[HTTP_MAX_URI], /* Username portion of URI */
7412 host[HTTP_MAX_URI], /* Host portion of URI */
7413 resource[HTTP_MAX_URI]; /* Resource portion of URI */
7414 int port; /* Port portion of URI */
7415 cupsd_job_t *job; /* Job information */
7416
7417
7418 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->number,
7419 uri->values[0].string.text);
7420
7421 /*
7422 * See if we have a job URI or a printer URI...
7423 */
7424
7425 if (!strcmp(uri->name, "printer-uri"))
7426 {
7427 /*
7428 * Got a printer URI; see if we also have a job-id attribute...
7429 */
7430
7431 if ((attr = ippFindAttribute(con->request, "job-id",
7432 IPP_TAG_INTEGER)) == NULL)
7433 {
7434 send_ipp_status(con, IPP_BAD_REQUEST,
7435 _("Got a printer-uri attribute but no job-id."));
7436 return;
7437 }
7438
7439 jobid = attr->values[0].integer;
7440 }
7441 else
7442 {
7443 /*
7444 * Got a job URI; parse it to get the job ID...
7445 */
7446
7447 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7448 sizeof(scheme), username, sizeof(username), host,
7449 sizeof(host), &port, resource, sizeof(resource));
7450
7451 if (strncmp(resource, "/jobs/", 6))
7452 {
7453 /*
7454 * Not a valid URI!
7455 */
7456
7457 send_ipp_status(con, IPP_BAD_REQUEST,
7458 _("Bad job-uri \"%s\"."),
7459 uri->values[0].string.text);
7460 return;
7461 }
7462
7463 jobid = atoi(resource + 6);
7464 }
7465
7466 /*
7467 * See if the job exists...
7468 */
7469
7470 if ((job = cupsdFindJob(jobid)) == NULL)
7471 {
7472 /*
7473 * Nope - return a "not found" error...
7474 */
7475
7476 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
7477 return;
7478 }
7479
7480 /*
7481 * See if the job is owned by the requesting user...
7482 */
7483
7484 if (!validate_user(job, con, job->username, username, sizeof(username)))
7485 {
7486 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
7487 cupsdFindDest(job->dest));
7488 return;
7489 }
7490
7491 /*
7492 * See if the job is in a state that allows holding...
7493 */
7494
7495 if (job->state_value > IPP_JOB_STOPPED)
7496 {
7497 /*
7498 * Return a "not-possible" error...
7499 */
7500
7501 send_ipp_status(con, IPP_NOT_POSSIBLE,
7502 _("Job #%d is finished and cannot be altered."),
7503 job->id);
7504 return;
7505 }
7506
7507 /*
7508 * Hold the job and return...
7509 */
7510
7511 if ((attr = ippFindAttribute(con->request, "job-hold-until",
7512 IPP_TAG_KEYWORD)) == NULL)
7513 attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
7514
7515 if (attr)
7516 {
7517 when = attr->values[0].string.text;
7518
7519 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
7520 "Job job-hold-until value changed by user.");
7521 }
7522 else
7523 when = "indefinite";
7524
7525 cupsdSetJobHoldUntil(job, when, 1);
7526 cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT, "Job held by \"%s\".",
7527 username);
7528
7529 con->response->request.status.status_code = IPP_OK;
7530 }
7531
7532
7533 /*
7534 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
7535 */
7536
7537 static void
7538 hold_new_jobs(cupsd_client_t *con, /* I - Connection */
7539 ipp_attribute_t *uri) /* I - Printer URI */
7540 {
7541 http_status_t status; /* Policy status */
7542 cups_ptype_t dtype; /* Destination type (printer/class) */
7543 cupsd_printer_t *printer; /* Printer data */
7544
7545
7546 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_new_jobs(%p[%d], %s)", con,
7547 con->number, uri->values[0].string.text);
7548
7549 /*
7550 * Is the destination valid?
7551 */
7552
7553 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7554 {
7555 /*
7556 * Bad URI...
7557 */
7558
7559 send_ipp_status(con, IPP_NOT_FOUND,
7560 _("The printer or class does not exist."));
7561 return;
7562 }
7563
7564 /*
7565 * Check policy...
7566 */
7567
7568 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
7569 {
7570 send_http_error(con, status, printer);
7571 return;
7572 }
7573
7574 /*
7575 * Hold pending/new jobs sent to the printer...
7576 */
7577
7578 printer->holding_new_jobs = 1;
7579
7580 cupsdSetPrinterReasons(printer, "+hold-new-jobs");
7581
7582 if (dtype & CUPS_PRINTER_CLASS)
7583 cupsdLogMessage(CUPSD_LOG_INFO,
7584 "Class \"%s\" now holding pending/new jobs (\"%s\").",
7585 printer->name, get_username(con));
7586 else
7587 cupsdLogMessage(CUPSD_LOG_INFO,
7588 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
7589 printer->name, get_username(con));
7590
7591 /*
7592 * Everything was ok, so return OK status...
7593 */
7594
7595 con->response->request.status.status_code = IPP_OK;
7596 }
7597
7598
7599 /*
7600 * 'move_job()' - Move a job to a new destination.
7601 */
7602
7603 static void
7604 move_job(cupsd_client_t *con, /* I - Client connection */
7605 ipp_attribute_t *uri) /* I - Job URI */
7606 {
7607 http_status_t status; /* Policy status */
7608 ipp_attribute_t *attr; /* Current attribute */
7609 int jobid; /* Job ID */
7610 cupsd_job_t *job; /* Current job */
7611 const char *src; /* Source printer/class */
7612 cups_ptype_t stype, /* Source type (printer or class) */
7613 dtype; /* Destination type (printer/class) */
7614 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
7615 username[HTTP_MAX_URI], /* Username portion of URI */
7616 host[HTTP_MAX_URI], /* Host portion of URI */
7617 resource[HTTP_MAX_URI]; /* Resource portion of URI */
7618 int port; /* Port portion of URI */
7619 cupsd_printer_t *sprinter, /* Source printer */
7620 *dprinter; /* Destination printer */
7621
7622
7623 cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->number,
7624 uri->values[0].string.text);
7625
7626 /*
7627 * Get the new printer or class...
7628 */
7629
7630 if ((attr = ippFindAttribute(con->request, "job-printer-uri",
7631 IPP_TAG_URI)) == NULL)
7632 {
7633 /*
7634 * Need job-printer-uri...
7635 */
7636
7637 send_ipp_status(con, IPP_BAD_REQUEST,
7638 _("job-printer-uri attribute missing."));
7639 return;
7640 }
7641
7642 if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter))
7643 {
7644 /*
7645 * Bad URI...
7646 */
7647
7648 send_ipp_status(con, IPP_NOT_FOUND,
7649 _("The printer or class does not exist."));
7650 return;
7651 }
7652
7653 /*
7654 * See if we have a job URI or a printer URI...
7655 */
7656
7657 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7658 sizeof(scheme), username, sizeof(username), host,
7659 sizeof(host), &port, resource, sizeof(resource));
7660
7661 if (!strcmp(uri->name, "printer-uri"))
7662 {
7663 /*
7664 * Got a printer URI; see if we also have a job-id attribute...
7665 */
7666
7667 if ((attr = ippFindAttribute(con->request, "job-id",
7668 IPP_TAG_INTEGER)) == NULL)
7669 {
7670 /*
7671 * Move all jobs...
7672 */
7673
7674 if ((src = cupsdValidateDest(uri->values[0].string.text, &stype,
7675 &sprinter)) == NULL)
7676 {
7677 /*
7678 * Bad URI...
7679 */
7680
7681 send_ipp_status(con, IPP_NOT_FOUND,
7682 _("The printer or class does not exist."));
7683 return;
7684 }
7685
7686 job = NULL;
7687 }
7688 else
7689 {
7690 /*
7691 * Otherwise, just move a single job...
7692 */
7693
7694 if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
7695 {
7696 /*
7697 * Nope - return a "not found" error...
7698 */
7699
7700 send_ipp_status(con, IPP_NOT_FOUND,
7701 _("Job #%d does not exist."), attr->values[0].integer);
7702 return;
7703 }
7704 else
7705 {
7706 /*
7707 * Job found, initialize source pointers...
7708 */
7709
7710 src = NULL;
7711 sprinter = NULL;
7712 }
7713 }
7714 }
7715 else
7716 {
7717 /*
7718 * Got a job URI; parse it to get the job ID...
7719 */
7720
7721 if (strncmp(resource, "/jobs/", 6))
7722 {
7723 /*
7724 * Not a valid URI!
7725 */
7726
7727 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
7728 uri->values[0].string.text);
7729 return;
7730 }
7731
7732 /*
7733 * See if the job exists...
7734 */
7735
7736 jobid = atoi(resource + 6);
7737
7738 if ((job = cupsdFindJob(jobid)) == NULL)
7739 {
7740 /*
7741 * Nope - return a "not found" error...
7742 */
7743
7744 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
7745 return;
7746 }
7747 else
7748 {
7749 /*
7750 * Job found, initialize source pointers...
7751 */
7752
7753 src = NULL;
7754 sprinter = NULL;
7755 }
7756 }
7757
7758 /*
7759 * Check the policy of the destination printer...
7760 */
7761
7762 if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con,
7763 job ? job->username : NULL)) != HTTP_OK)
7764 {
7765 send_http_error(con, status, dprinter);
7766 return;
7767 }
7768
7769 /*
7770 * Now move the job or jobs...
7771 */
7772
7773 if (job)
7774 {
7775 /*
7776 * See if the job has been completed...
7777 */
7778
7779 if (job->state_value > IPP_JOB_STOPPED)
7780 {
7781 /*
7782 * Return a "not-possible" error...
7783 */
7784
7785 send_ipp_status(con, IPP_NOT_POSSIBLE,
7786 _("Job #%d is finished and cannot be altered."),
7787 job->id);
7788 return;
7789 }
7790
7791 /*
7792 * See if the job is owned by the requesting user...
7793 */
7794
7795 if (!validate_user(job, con, job->username, username, sizeof(username)))
7796 {
7797 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
7798 cupsdFindDest(job->dest));
7799 return;
7800 }
7801
7802 /*
7803 * Move the job to a different printer or class...
7804 */
7805
7806 cupsdMoveJob(job, dprinter);
7807 }
7808 else
7809 {
7810 /*
7811 * Got the source printer, now look through the jobs...
7812 */
7813
7814 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
7815 job;
7816 job = (cupsd_job_t *)cupsArrayNext(Jobs))
7817 {
7818 /*
7819 * See if the job is pointing at the source printer or has not been
7820 * completed...
7821 */
7822
7823 if (_cups_strcasecmp(job->dest, src) ||
7824 job->state_value > IPP_JOB_STOPPED)
7825 continue;
7826
7827 /*
7828 * See if the job can be moved by the requesting user...
7829 */
7830
7831 if (!validate_user(job, con, job->username, username, sizeof(username)))
7832 continue;
7833
7834 /*
7835 * Move the job to a different printer or class...
7836 */
7837
7838 cupsdMoveJob(job, dprinter);
7839 }
7840 }
7841
7842 /*
7843 * Start jobs if possible...
7844 */
7845
7846 cupsdCheckJobs();
7847
7848 /*
7849 * Return with "everything is OK" status...
7850 */
7851
7852 con->response->request.status.status_code = IPP_OK;
7853 }
7854
7855
7856 /*
7857 * 'ppd_parse_line()' - Parse a PPD default line.
7858 */
7859
7860 static int /* O - 0 on success, -1 on failure */
7861 ppd_parse_line(const char *line, /* I - Line */
7862 char *option, /* O - Option name */
7863 int olen, /* I - Size of option name */
7864 char *choice, /* O - Choice name */
7865 int clen) /* I - Size of choice name */
7866 {
7867 /*
7868 * Verify this is a default option line...
7869 */
7870
7871 if (strncmp(line, "*Default", 8))
7872 return (-1);
7873
7874 /*
7875 * Read the option name...
7876 */
7877
7878 for (line += 8, olen --;
7879 *line > ' ' && *line < 0x7f && *line != ':' && *line != '/';
7880 line ++)
7881 if (olen > 0)
7882 {
7883 *option++ = *line;
7884 olen --;
7885 }
7886
7887 *option = '\0';
7888
7889 /*
7890 * Skip everything else up to the colon (:)...
7891 */
7892
7893 while (*line && *line != ':')
7894 line ++;
7895
7896 if (!*line)
7897 return (-1);
7898
7899 line ++;
7900
7901 /*
7902 * Now grab the option choice, skipping leading whitespace...
7903 */
7904
7905 while (isspace(*line & 255))
7906 line ++;
7907
7908 for (clen --;
7909 *line > ' ' && *line < 0x7f && *line != ':' && *line != '/';
7910 line ++)
7911 if (clen > 0)
7912 {
7913 *choice++ = *line;
7914 clen --;
7915 }
7916
7917 *choice = '\0';
7918
7919 /*
7920 * Return with no errors...
7921 */
7922
7923 return (0);
7924 }
7925
7926
7927 /*
7928 * 'print_job()' - Print a file to a printer or class.
7929 */
7930
7931 static void
7932 print_job(cupsd_client_t *con, /* I - Client connection */
7933 ipp_attribute_t *uri) /* I - Printer URI */
7934 {
7935 ipp_attribute_t *attr; /* Current attribute */
7936 ipp_attribute_t *format; /* Document-format attribute */
7937 const char *default_format; /* document-format-default value */
7938 cupsd_job_t *job; /* New job */
7939 char filename[1024]; /* Job filename */
7940 mime_type_t *filetype; /* Type of file */
7941 char super[MIME_MAX_SUPER], /* Supertype of file */
7942 type[MIME_MAX_TYPE], /* Subtype of file */
7943 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
7944 /* Textual name of mime type */
7945 cupsd_printer_t *printer; /* Printer data */
7946 struct stat fileinfo; /* File information */
7947 int kbytes; /* Size of file */
7948 int compression; /* Document compression */
7949
7950
7951 cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->number,
7952 uri->values[0].string.text);
7953
7954 /*
7955 * Validate print file attributes, for now just document-format and
7956 * compression (CUPS only supports "none" and "gzip")...
7957 */
7958
7959 compression = CUPS_FILE_NONE;
7960
7961 if ((attr = ippFindAttribute(con->request, "compression",
7962 IPP_TAG_KEYWORD)) != NULL)
7963 {
7964 if (strcmp(attr->values[0].string.text, "none")
7965 #ifdef HAVE_LIBZ
7966 && strcmp(attr->values[0].string.text, "gzip")
7967 #endif /* HAVE_LIBZ */
7968 )
7969 {
7970 send_ipp_status(con, IPP_ATTRIBUTES,
7971 _("Unsupported compression \"%s\"."),
7972 attr->values[0].string.text);
7973 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
7974 "compression", NULL, attr->values[0].string.text);
7975 return;
7976 }
7977
7978 #ifdef HAVE_LIBZ
7979 if (!strcmp(attr->values[0].string.text, "gzip"))
7980 compression = CUPS_FILE_GZIP;
7981 #endif /* HAVE_LIBZ */
7982 }
7983
7984 /*
7985 * Do we have a file to print?
7986 */
7987
7988 if (!con->filename)
7989 {
7990 send_ipp_status(con, IPP_BAD_REQUEST, _("No file in print request."));
7991 return;
7992 }
7993
7994 /*
7995 * Is the destination valid?
7996 */
7997
7998 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
7999 {
8000 /*
8001 * Bad URI...
8002 */
8003
8004 send_ipp_status(con, IPP_NOT_FOUND,
8005 _("The printer or class does not exist."));
8006 return;
8007 }
8008
8009 /*
8010 * Is it a format we support?
8011 */
8012
8013 if ((format = ippFindAttribute(con->request, "document-format",
8014 IPP_TAG_MIMETYPE)) != NULL)
8015 {
8016 /*
8017 * Grab format from client...
8018 */
8019
8020 if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]", super,
8021 type) != 2)
8022 {
8023 send_ipp_status(con, IPP_BAD_REQUEST,
8024 _("Bad document-format \"%s\"."),
8025 format->values[0].string.text);
8026 return;
8027 }
8028 }
8029 else if ((default_format = cupsGetOption("document-format",
8030 printer->num_options,
8031 printer->options)) != NULL)
8032 {
8033 /*
8034 * Use default document format...
8035 */
8036
8037 if (sscanf(default_format, "%15[^/]/%255[^;]", super, type) != 2)
8038 {
8039 send_ipp_status(con, IPP_BAD_REQUEST,
8040 _("Bad document-format \"%s\"."),
8041 default_format);
8042 return;
8043 }
8044 }
8045 else
8046 {
8047 /*
8048 * Auto-type it!
8049 */
8050
8051 strlcpy(super, "application", sizeof(super));
8052 strlcpy(type, "octet-stream", sizeof(type));
8053 }
8054
8055 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
8056 {
8057 /*
8058 * Auto-type the file...
8059 */
8060
8061 ipp_attribute_t *doc_name; /* document-name attribute */
8062
8063
8064 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file...");
8065
8066 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
8067 filetype = mimeFileType(MimeDatabase, con->filename,
8068 doc_name ? doc_name->values[0].string.text : NULL,
8069 &compression);
8070
8071 if (!filetype)
8072 filetype = mimeType(MimeDatabase, super, type);
8073
8074 cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.",
8075 filetype->super, filetype->type);
8076 }
8077 else
8078 filetype = mimeType(MimeDatabase, super, type);
8079
8080 if (filetype &&
8081 (!format ||
8082 (!strcmp(super, "application") && !strcmp(type, "octet-stream"))))
8083 {
8084 /*
8085 * Replace the document-format attribute value with the auto-typed or
8086 * default one.
8087 */
8088
8089 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
8090 filetype->type);
8091
8092 if (format)
8093 {
8094 _cupsStrFree(format->values[0].string.text);
8095
8096 format->values[0].string.text = _cupsStrAlloc(mimetype);
8097 }
8098 else
8099 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
8100 "document-format", NULL, mimetype);
8101 }
8102 else if (!filetype)
8103 {
8104 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
8105 _("Unsupported document-format \"%s\"."),
8106 format ? format->values[0].string.text :
8107 "application/octet-stream");
8108 cupsdLogMessage(CUPSD_LOG_INFO,
8109 "Hint: Do you have the raw file printing rules enabled?");
8110
8111 if (format)
8112 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
8113 "document-format", NULL, format->values[0].string.text);
8114
8115 return;
8116 }
8117
8118 /*
8119 * Read any embedded job ticket info from PS files...
8120 */
8121
8122 if (!_cups_strcasecmp(filetype->super, "application") &&
8123 (!_cups_strcasecmp(filetype->type, "postscript") ||
8124 !_cups_strcasecmp(filetype->type, "pdf")))
8125 read_job_ticket(con);
8126
8127 /*
8128 * Create the job object...
8129 */
8130
8131 if ((job = add_job(con, printer, filetype)) == NULL)
8132 return;
8133
8134 /*
8135 * Update quota data...
8136 */
8137
8138 if (stat(con->filename, &fileinfo))
8139 kbytes = 0;
8140 else
8141 kbytes = (fileinfo.st_size + 1023) / 1024;
8142
8143 cupsdUpdateQuota(printer, job->username, 0, kbytes);
8144
8145 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
8146 IPP_TAG_INTEGER)) != NULL)
8147 attr->values[0].integer += kbytes;
8148
8149 /*
8150 * Add the job file...
8151 */
8152
8153 if (add_file(con, job, filetype, compression))
8154 return;
8155
8156 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
8157 job->num_files);
8158 rename(con->filename, filename);
8159 cupsdClearString(&con->filename);
8160
8161 /*
8162 * See if we need to add the ending sheet...
8163 */
8164
8165 if (cupsdTimeoutJob(job))
8166 return;
8167
8168 /*
8169 * Log and save the job...
8170 */
8171
8172 cupsdLogJob(job, CUPSD_LOG_INFO,
8173 "File of type %s/%s queued by \"%s\".",
8174 filetype->super, filetype->type, job->username);
8175 cupsdLogJob(job, CUPSD_LOG_DEBUG, "hold_until=%d", (int)job->hold_until);
8176 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
8177 job->dest, job->username);
8178
8179 /*
8180 * Start the job if possible...
8181 */
8182
8183 cupsdCheckJobs();
8184 }
8185
8186
8187 /*
8188 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8189 *
8190 * This function only gets called when printing a single PDF or PostScript
8191 * file using the Print-Job operation. It doesn't work for Create-Job +
8192 * Send-File, since the job attributes need to be set at job creation
8193 * time for banners to work. The embedded job ticket stuff is here
8194 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8195 * options and IPP attributes which otherwise would be lost.
8196 *
8197 * The format of a job ticket is simple:
8198 *
8199 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8200 *
8201 * %cupsJobTicket: attr1=value1
8202 * %cupsJobTicket: attr2=value2
8203 * ...
8204 * %cupsJobTicket: attrN=valueN
8205 *
8206 * Job ticket lines must appear immediately after the first line that
8207 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8208 * stops looking for job ticket info when it finds a line that does not begin
8209 * with "%cupsJobTicket:".
8210 *
8211 * The maximum length of a job ticket line, including the prefix, is
8212 * 255 characters to conform with the Adobe DSC.
8213 *
8214 * Read-only attributes are rejected with a notice to the error log in
8215 * case a malicious user tries anything. Since the job ticket is read
8216 * prior to attribute validation in print_job(), job ticket attributes
8217 * will go through the same validation as IPP attributes...
8218 */
8219
8220 static void
8221 read_job_ticket(cupsd_client_t *con) /* I - Client connection */
8222 {
8223 cups_file_t *fp; /* File to read from */
8224 char line[256]; /* Line data */
8225 int num_options; /* Number of options */
8226 cups_option_t *options; /* Options */
8227 ipp_t *ticket; /* New attributes */
8228 ipp_attribute_t *attr, /* Current attribute */
8229 *attr2, /* Job attribute */
8230 *prev2; /* Previous job attribute */
8231
8232
8233 /*
8234 * First open the print file...
8235 */
8236
8237 if ((fp = cupsFileOpen(con->filename, "rb")) == NULL)
8238 {
8239 cupsdLogMessage(CUPSD_LOG_ERROR,
8240 "Unable to open print file for job ticket - %s",
8241 strerror(errno));
8242 return;
8243 }
8244
8245 /*
8246 * Skip the first line...
8247 */
8248
8249 if (cupsFileGets(fp, line, sizeof(line)) == NULL)
8250 {
8251 cupsdLogMessage(CUPSD_LOG_ERROR,
8252 "Unable to read from print file for job ticket - %s",
8253 strerror(errno));
8254 cupsFileClose(fp);
8255 return;
8256 }
8257
8258 if (strncmp(line, "%!PS-Adobe-", 11) && strncmp(line, "%PDF-", 5))
8259 {
8260 /*
8261 * Not a DSC-compliant file, so no job ticket info will be available...
8262 */
8263
8264 cupsFileClose(fp);
8265 return;
8266 }
8267
8268 /*
8269 * Read job ticket info from the file...
8270 */
8271
8272 num_options = 0;
8273 options = NULL;
8274
8275 while (cupsFileGets(fp, line, sizeof(line)))
8276 {
8277 /*
8278 * Stop at the first non-ticket line...
8279 */
8280
8281 if (strncmp(line, "%cupsJobTicket:", 15))
8282 break;
8283
8284 /*
8285 * Add the options to the option array...
8286 */
8287
8288 num_options = cupsParseOptions(line + 15, num_options, &options);
8289 }
8290
8291 /*
8292 * Done with the file; see if we have any options...
8293 */
8294
8295 cupsFileClose(fp);
8296
8297 if (num_options == 0)
8298 return;
8299
8300 /*
8301 * OK, convert the options to an attribute list, and apply them to
8302 * the request...
8303 */
8304
8305 ticket = ippNew();
8306 cupsEncodeOptions(ticket, num_options, options);
8307
8308 /*
8309 * See what the user wants to change.
8310 */
8311
8312 for (attr = ticket->attrs; attr; attr = attr->next)
8313 {
8314 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
8315 continue;
8316
8317 if (!strcmp(attr->name, "job-originating-host-name") ||
8318 !strcmp(attr->name, "job-originating-user-name") ||
8319 !strcmp(attr->name, "job-media-sheets-completed") ||
8320 !strcmp(attr->name, "job-k-octets") ||
8321 !strcmp(attr->name, "job-id") ||
8322 !strncmp(attr->name, "job-state", 9) ||
8323 !strncmp(attr->name, "time-at-", 8))
8324 continue; /* Read-only attrs */
8325
8326 if ((attr2 = ippFindAttribute(con->request, attr->name,
8327 IPP_TAG_ZERO)) != NULL)
8328 {
8329 /*
8330 * Some other value; first free the old value...
8331 */
8332
8333 if (con->request->attrs == attr2)
8334 {
8335 con->request->attrs = attr2->next;
8336 prev2 = NULL;
8337 }
8338 else
8339 {
8340 for (prev2 = con->request->attrs; prev2; prev2 = prev2->next)
8341 if (prev2->next == attr2)
8342 {
8343 prev2->next = attr2->next;
8344 break;
8345 }
8346 }
8347
8348 if (con->request->last == attr2)
8349 con->request->last = prev2;
8350
8351 ippDeleteAttribute(NULL, attr2);
8352 }
8353
8354 /*
8355 * Add new option by copying it...
8356 */
8357
8358 ippCopyAttribute(con->request, attr, 0);
8359 }
8360
8361 /*
8362 * Then free the attribute list and option array...
8363 */
8364
8365 ippDelete(ticket);
8366 cupsFreeOptions(num_options, options);
8367 }
8368
8369
8370 /*
8371 * 'reject_jobs()' - Reject print jobs to a printer.
8372 */
8373
8374 static void
8375 reject_jobs(cupsd_client_t *con, /* I - Client connection */
8376 ipp_attribute_t *uri) /* I - Printer or class URI */
8377 {
8378 http_status_t status; /* Policy status */
8379 cups_ptype_t dtype; /* Destination type (printer/class) */
8380 cupsd_printer_t *printer; /* Printer data */
8381 ipp_attribute_t *attr; /* printer-state-message text */
8382
8383
8384 cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con,
8385 con->number, uri->values[0].string.text);
8386
8387 /*
8388 * Is the destination valid?
8389 */
8390
8391 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8392 {
8393 /*
8394 * Bad URI...
8395 */
8396
8397 send_ipp_status(con, IPP_NOT_FOUND,
8398 _("The printer or class does not exist."));
8399 return;
8400 }
8401
8402 /*
8403 * Check policy...
8404 */
8405
8406 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8407 {
8408 send_http_error(con, status, printer);
8409 return;
8410 }
8411
8412 /*
8413 * Reject jobs sent to the printer...
8414 */
8415
8416 printer->accepting = 0;
8417
8418 if ((attr = ippFindAttribute(con->request, "printer-state-message",
8419 IPP_TAG_TEXT)) == NULL)
8420 strlcpy(printer->state_message, "Rejecting Jobs",
8421 sizeof(printer->state_message));
8422 else
8423 strlcpy(printer->state_message, attr->values[0].string.text,
8424 sizeof(printer->state_message));
8425
8426 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
8427 "No longer accepting jobs.");
8428
8429 if (dtype & CUPS_PRINTER_CLASS)
8430 {
8431 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
8432
8433 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").",
8434 printer->name, get_username(con));
8435 }
8436 else
8437 {
8438 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
8439
8440 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").",
8441 printer->name, get_username(con));
8442 }
8443
8444 /*
8445 * Everything was ok, so return OK status...
8446 */
8447
8448 con->response->request.status.status_code = IPP_OK;
8449 }
8450
8451
8452 /*
8453 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8454 */
8455
8456 static void
8457 release_held_new_jobs(
8458 cupsd_client_t *con, /* I - Connection */
8459 ipp_attribute_t *uri) /* I - Printer URI */
8460 {
8461 http_status_t status; /* Policy status */
8462 cups_ptype_t dtype; /* Destination type (printer/class) */
8463 cupsd_printer_t *printer; /* Printer data */
8464
8465
8466 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_held_new_jobs(%p[%d], %s)", con,
8467 con->number, uri->values[0].string.text);
8468
8469 /*
8470 * Is the destination valid?
8471 */
8472
8473 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8474 {
8475 /*
8476 * Bad URI...
8477 */
8478
8479 send_ipp_status(con, IPP_NOT_FOUND,
8480 _("The printer or class does not exist."));
8481 return;
8482 }
8483
8484 /*
8485 * Check policy...
8486 */
8487
8488 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8489 {
8490 send_http_error(con, status, printer);
8491 return;
8492 }
8493
8494 /*
8495 * Hold pending/new jobs sent to the printer...
8496 */
8497
8498 printer->holding_new_jobs = 0;
8499
8500 cupsdSetPrinterReasons(printer, "-hold-new-jobs");
8501
8502 if (dtype & CUPS_PRINTER_CLASS)
8503 cupsdLogMessage(CUPSD_LOG_INFO,
8504 "Class \"%s\" now printing pending/new jobs (\"%s\").",
8505 printer->name, get_username(con));
8506 else
8507 cupsdLogMessage(CUPSD_LOG_INFO,
8508 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
8509 printer->name, get_username(con));
8510
8511 /*
8512 * Everything was ok, so return OK status...
8513 */
8514
8515 con->response->request.status.status_code = IPP_OK;
8516 }
8517
8518
8519 /*
8520 * 'release_job()' - Release a held print job.
8521 */
8522
8523 static void
8524 release_job(cupsd_client_t *con, /* I - Client connection */
8525 ipp_attribute_t *uri) /* I - Job or Printer URI */
8526 {
8527 ipp_attribute_t *attr; /* Current attribute */
8528 int jobid; /* Job ID */
8529 char scheme[HTTP_MAX_URI], /* Method portion of URI */
8530 username[HTTP_MAX_URI], /* Username portion of URI */
8531 host[HTTP_MAX_URI], /* Host portion of URI */
8532 resource[HTTP_MAX_URI]; /* Resource portion of URI */
8533 int port; /* Port portion of URI */
8534 cupsd_job_t *job; /* Job information */
8535
8536
8537 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con,
8538 con->number, uri->values[0].string.text);
8539
8540 /*
8541 * See if we have a job URI or a printer URI...
8542 */
8543
8544 if (!strcmp(uri->name, "printer-uri"))
8545 {
8546 /*
8547 * Got a printer URI; see if we also have a job-id attribute...
8548 */
8549
8550 if ((attr = ippFindAttribute(con->request, "job-id",
8551 IPP_TAG_INTEGER)) == NULL)
8552 {
8553 send_ipp_status(con, IPP_BAD_REQUEST,
8554 _("Got a printer-uri attribute but no job-id."));
8555 return;
8556 }
8557
8558 jobid = attr->values[0].integer;
8559 }
8560 else
8561 {
8562 /*
8563 * Got a job URI; parse it to get the job ID...
8564 */
8565
8566 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
8567 sizeof(scheme), username, sizeof(username), host,
8568 sizeof(host), &port, resource, sizeof(resource));
8569
8570 if (strncmp(resource, "/jobs/", 6))
8571 {
8572 /*
8573 * Not a valid URI!
8574 */
8575
8576 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
8577 uri->values[0].string.text);
8578 return;
8579 }
8580
8581 jobid = atoi(resource + 6);
8582 }
8583
8584 /*
8585 * See if the job exists...
8586 */
8587
8588 if ((job = cupsdFindJob(jobid)) == NULL)
8589 {
8590 /*
8591 * Nope - return a "not found" error...
8592 */
8593
8594 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
8595 return;
8596 }
8597
8598 /*
8599 * See if job is "held"...
8600 */
8601
8602 if (job->state_value != IPP_JOB_HELD)
8603 {
8604 /*
8605 * Nope - return a "not possible" error...
8606 */
8607
8608 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held."), jobid);
8609 return;
8610 }
8611
8612 /*
8613 * See if the job is owned by the requesting user...
8614 */
8615
8616 if (!validate_user(job, con, job->username, username, sizeof(username)))
8617 {
8618 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
8619 cupsdFindDest(job->dest));
8620 return;
8621 }
8622
8623 /*
8624 * Reset the job-hold-until value to "no-hold"...
8625 */
8626
8627 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
8628 IPP_TAG_KEYWORD)) == NULL)
8629 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
8630
8631 if (attr)
8632 {
8633 _cupsStrFree(attr->values[0].string.text);
8634
8635 attr->value_tag = IPP_TAG_KEYWORD;
8636 attr->values[0].string.text = _cupsStrAlloc("no-hold");
8637
8638 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
8639 "Job job-hold-until value changed by user.");
8640 ippSetString(job->attrs, &job->reasons, 0, "none");
8641 }
8642
8643 /*
8644 * Release the job and return...
8645 */
8646
8647 cupsdReleaseJob(job);
8648
8649 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
8650 "Job released by user.");
8651
8652 cupsdLogJob(job, CUPSD_LOG_INFO, "Released by \"%s\".", username);
8653
8654 con->response->request.status.status_code = IPP_OK;
8655
8656 cupsdCheckJobs();
8657 }
8658
8659
8660 /*
8661 * 'renew_subscription()' - Renew an existing subscription...
8662 */
8663
8664 static void
8665 renew_subscription(
8666 cupsd_client_t *con, /* I - Client connection */
8667 int sub_id) /* I - Subscription ID */
8668 {
8669 http_status_t status; /* Policy status */
8670 cupsd_subscription_t *sub; /* Subscription */
8671 ipp_attribute_t *lease; /* notify-lease-duration */
8672
8673
8674 cupsdLogMessage(CUPSD_LOG_DEBUG2,
8675 "renew_subscription(con=%p[%d], sub_id=%d)",
8676 con, con->number, sub_id);
8677
8678 /*
8679 * Is the subscription ID valid?
8680 */
8681
8682 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
8683 {
8684 /*
8685 * Bad subscription ID...
8686 */
8687
8688 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
8689 sub_id);
8690 return;
8691 }
8692
8693 if (sub->job)
8694 {
8695 /*
8696 * Job subscriptions cannot be renewed...
8697 */
8698
8699 send_ipp_status(con, IPP_NOT_POSSIBLE,
8700 _("Job subscriptions cannot be renewed."));
8701 return;
8702 }
8703
8704 /*
8705 * Check policy...
8706 */
8707
8708 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
8709 DefaultPolicyPtr,
8710 con, sub->owner)) != HTTP_OK)
8711 {
8712 send_http_error(con, status, sub->dest);
8713 return;
8714 }
8715
8716 /*
8717 * Renew the subscription...
8718 */
8719
8720 lease = ippFindAttribute(con->request, "notify-lease-duration",
8721 IPP_TAG_INTEGER);
8722
8723 sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration;
8724
8725 if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration))
8726 {
8727 cupsdLogMessage(CUPSD_LOG_INFO,
8728 "renew_subscription: Limiting notify-lease-duration to "
8729 "%d seconds.",
8730 MaxLeaseDuration);
8731 sub->lease = MaxLeaseDuration;
8732 }
8733
8734 sub->expire = sub->lease ? time(NULL) + sub->lease : 0;
8735
8736 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
8737
8738 con->response->request.status.status_code = IPP_OK;
8739
8740 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
8741 "notify-lease-duration", sub->lease);
8742 }
8743
8744
8745 /*
8746 * 'restart_job()' - Restart an old print job.
8747 */
8748
8749 static void
8750 restart_job(cupsd_client_t *con, /* I - Client connection */
8751 ipp_attribute_t *uri) /* I - Job or Printer URI */
8752 {
8753 ipp_attribute_t *attr; /* Current attribute */
8754 int jobid; /* Job ID */
8755 cupsd_job_t *job; /* Job information */
8756 char scheme[HTTP_MAX_URI], /* Method portion of URI */
8757 username[HTTP_MAX_URI], /* Username portion of URI */
8758 host[HTTP_MAX_URI], /* Host portion of URI */
8759 resource[HTTP_MAX_URI]; /* Resource portion of URI */
8760 int port; /* Port portion of URI */
8761
8762
8763 cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con,
8764 con->number, uri->values[0].string.text);
8765
8766 /*
8767 * See if we have a job URI or a printer URI...
8768 */
8769
8770 if (!strcmp(uri->name, "printer-uri"))
8771 {
8772 /*
8773 * Got a printer URI; see if we also have a job-id attribute...
8774 */
8775
8776 if ((attr = ippFindAttribute(con->request, "job-id",
8777 IPP_TAG_INTEGER)) == NULL)
8778 {
8779 send_ipp_status(con, IPP_BAD_REQUEST,
8780 _("Got a printer-uri attribute but no job-id."));
8781 return;
8782 }
8783
8784 jobid = attr->values[0].integer;
8785 }
8786 else
8787 {
8788 /*
8789 * Got a job URI; parse it to get the job ID...
8790 */
8791
8792 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
8793 sizeof(scheme), username, sizeof(username), host,
8794 sizeof(host), &port, resource, sizeof(resource));
8795
8796 if (strncmp(resource, "/jobs/", 6))
8797 {
8798 /*
8799 * Not a valid URI!
8800 */
8801
8802 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
8803 uri->values[0].string.text);
8804 return;
8805 }
8806
8807 jobid = atoi(resource + 6);
8808 }
8809
8810 /*
8811 * See if the job exists...
8812 */
8813
8814 if ((job = cupsdFindJob(jobid)) == NULL)
8815 {
8816 /*
8817 * Nope - return a "not found" error...
8818 */
8819
8820 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
8821 return;
8822 }
8823
8824 /*
8825 * See if job is in any of the "completed" states...
8826 */
8827
8828 if (job->state_value <= IPP_JOB_PROCESSING)
8829 {
8830 /*
8831 * Nope - return a "not possible" error...
8832 */
8833
8834 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete."),
8835 jobid);
8836 return;
8837 }
8838
8839 /*
8840 * See if we have retained the job files...
8841 */
8842
8843 cupsdLoadJob(job);
8844
8845 if (!job->attrs || job->num_files == 0)
8846 {
8847 /*
8848 * Nope - return a "not possible" error...
8849 */
8850
8851 send_ipp_status(con, IPP_NOT_POSSIBLE,
8852 _("Job #%d cannot be restarted - no files."), jobid);
8853 return;
8854 }
8855
8856 /*
8857 * See if the job is owned by the requesting user...
8858 */
8859
8860 if (!validate_user(job, con, job->username, username, sizeof(username)))
8861 {
8862 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
8863 cupsdFindDest(job->dest));
8864 return;
8865 }
8866
8867 /*
8868 * See if the job-hold-until attribute is specified...
8869 */
8870
8871 if ((attr = ippFindAttribute(con->request, "job-hold-until",
8872 IPP_TAG_KEYWORD)) == NULL)
8873 attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
8874
8875 if (attr && strcmp(attr->values[0].string.text, "no-hold"))
8876 {
8877 /*
8878 * Return the job to a held state...
8879 */
8880
8881 cupsdLogJob(job, CUPSD_LOG_DEBUG,
8882 "Restarted by \"%s\" with job-hold-until=%s.",
8883 username, attr->values[0].string.text);
8884 cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
8885
8886 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE,
8887 NULL, job, "Job restarted by user with job-hold-until=%s",
8888 attr->values[0].string.text);
8889 }
8890 else
8891 {
8892 /*
8893 * Restart the job...
8894 */
8895
8896 cupsdRestartJob(job);
8897 cupsdCheckJobs();
8898 }
8899
8900 cupsdLogJob(job, CUPSD_LOG_INFO, "Restarted by \"%s\".", username);
8901
8902 con->response->request.status.status_code = IPP_OK;
8903 }
8904
8905
8906 /*
8907 * 'save_auth_info()' - Save authentication information for a job.
8908 */
8909
8910 static void
8911 save_auth_info(
8912 cupsd_client_t *con, /* I - Client connection */
8913 cupsd_job_t *job, /* I - Job */
8914 ipp_attribute_t *auth_info) /* I - auth-info attribute, if any */
8915 {
8916 int i; /* Looping var */
8917 char filename[1024]; /* Job authentication filename */
8918 cups_file_t *fp; /* Job authentication file */
8919 char line[65536]; /* Line for file */
8920 cupsd_printer_t *dest; /* Destination printer/class */
8921
8922
8923 /*
8924 * This function saves the in-memory authentication information for
8925 * a job so that it can be used to authenticate with a remote host.
8926 * The information is stored in a file that is readable only by the
8927 * root user. The fields are Base-64 encoded, each on a separate line,
8928 * followed by random number (up to 1024) of newlines to limit the
8929 * amount of information that is exposed.
8930 *
8931 * Because of the potential for exposing of authentication information,
8932 * this functionality is only enabled when running cupsd as root.
8933 *
8934 * This caching only works for the Basic and BasicDigest authentication
8935 * types. Digest authentication cannot be cached this way, and in
8936 * the future Kerberos authentication may make all of this obsolete.
8937 *
8938 * Authentication information is saved whenever an authenticated
8939 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
8940 * performed.
8941 *
8942 * This information is deleted after a job is completed or canceled,
8943 * so reprints may require subsequent re-authentication.
8944 */
8945
8946 if (RunUser)
8947 return;
8948
8949 if ((dest = cupsdFindDest(job->dest)) == NULL)
8950 return;
8951
8952 /*
8953 * Create the authentication file and change permissions...
8954 */
8955
8956 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
8957 if ((fp = cupsFileOpen(filename, "w")) == NULL)
8958 {
8959 cupsdLogMessage(CUPSD_LOG_ERROR,
8960 "Unable to save authentication info to \"%s\" - %s",
8961 filename, strerror(errno));
8962 return;
8963 }
8964
8965 fchown(cupsFileNumber(fp), 0, 0);
8966 fchmod(cupsFileNumber(fp), 0400);
8967
8968 cupsFilePuts(fp, "CUPSD-AUTH-V3\n");
8969
8970 for (i = 0;
8971 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
8972 i ++)
8973 cupsdClearString(job->auth_env + i);
8974
8975 if (auth_info && auth_info->num_values == dest->num_auth_info_required)
8976 {
8977 /*
8978 * Write 1 to 3 auth values...
8979 */
8980
8981 for (i = 0;
8982 i < auth_info->num_values &&
8983 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
8984 i ++)
8985 {
8986 if (strcmp(dest->auth_info_required[i], "negotiate"))
8987 {
8988 httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text,
8989 strlen(auth_info->values[i].string.text));
8990 cupsFilePutConf(fp, dest->auth_info_required[i], line);
8991 }
8992 else
8993 cupsFilePutConf(fp, dest->auth_info_required[i],
8994 auth_info->values[i].string.text);
8995
8996 if (!strcmp(dest->auth_info_required[i], "username"))
8997 cupsdSetStringf(job->auth_env + i, "AUTH_USERNAME=%s",
8998 auth_info->values[i].string.text);
8999 else if (!strcmp(dest->auth_info_required[i], "domain"))
9000 cupsdSetStringf(job->auth_env + i, "AUTH_DOMAIN=%s",
9001 auth_info->values[i].string.text);
9002 else if (!strcmp(dest->auth_info_required[i], "password"))
9003 cupsdSetStringf(job->auth_env + i, "AUTH_PASSWORD=%s",
9004 auth_info->values[i].string.text);
9005 else if (!strcmp(dest->auth_info_required[i], "negotiate"))
9006 cupsdSetStringf(job->auth_env + i, "AUTH_NEGOTIATE=%s",
9007 auth_info->values[i].string.text);
9008 else
9009 i --;
9010 }
9011 }
9012 else if (auth_info && auth_info->num_values == 2 &&
9013 dest->num_auth_info_required == 1 &&
9014 !strcmp(dest->auth_info_required[0], "negotiate"))
9015 {
9016 /*
9017 * Allow fallback to username+password for Kerberized queues...
9018 */
9019
9020 httpEncode64_2(line, sizeof(line), auth_info->values[0].string.text,
9021 strlen(auth_info->values[0].string.text));
9022 cupsFilePutConf(fp, "username", line);
9023
9024 cupsdSetStringf(job->auth_env + 0, "AUTH_USERNAME=%s",
9025 auth_info->values[0].string.text);
9026
9027 httpEncode64_2(line, sizeof(line), auth_info->values[1].string.text,
9028 strlen(auth_info->values[1].string.text));
9029 cupsFilePutConf(fp, "password", line);
9030
9031 cupsdSetStringf(job->auth_env + 1, "AUTH_PASSWORD=%s",
9032 auth_info->values[1].string.text);
9033 }
9034 else if (con->username[0])
9035 {
9036 /*
9037 * Write the authenticated username...
9038 */
9039
9040 httpEncode64_2(line, sizeof(line), con->username, strlen(con->username));
9041 cupsFilePutConf(fp, "username", line);
9042
9043 cupsdSetStringf(job->auth_env + 0, "AUTH_USERNAME=%s", con->username);
9044
9045 /*
9046 * Write the authenticated password...
9047 */
9048
9049 httpEncode64_2(line, sizeof(line), con->password, strlen(con->password));
9050 cupsFilePutConf(fp, "password", line);
9051
9052 cupsdSetStringf(job->auth_env + 1, "AUTH_PASSWORD=%s", con->password);
9053 }
9054
9055 #ifdef HAVE_GSSAPI
9056 if (con->gss_uid > 0)
9057 {
9058 cupsFilePrintf(fp, "uid %d\n", (int)con->gss_uid);
9059 cupsdSetStringf(&job->auth_uid, "AUTH_UID=%d", (int)con->gss_uid);
9060 }
9061 #endif /* HAVE_GSSAPI */
9062
9063 /*
9064 * Write a random number of newlines to the end of the file...
9065 */
9066
9067 for (i = (CUPS_RAND() % 1024); i >= 0; i --)
9068 cupsFilePutChar(fp, '\n');
9069
9070 /*
9071 * Close the file and return...
9072 */
9073
9074 cupsFileClose(fp);
9075 }
9076
9077
9078 /*
9079 * 'send_document()' - Send a file to a printer or class.
9080 */
9081
9082 static void
9083 send_document(cupsd_client_t *con, /* I - Client connection */
9084 ipp_attribute_t *uri) /* I - Printer URI */
9085 {
9086 ipp_attribute_t *attr; /* Current attribute */
9087 ipp_attribute_t *format; /* Request's document-format attribute */
9088 ipp_attribute_t *jformat; /* Job's document-format attribute */
9089 const char *default_format;/* document-format-default value */
9090 int jobid; /* Job ID number */
9091 cupsd_job_t *job; /* Current job */
9092 char job_uri[HTTP_MAX_URI],
9093 /* Job URI */
9094 scheme[HTTP_MAX_URI],
9095 /* Method portion of URI */
9096 username[HTTP_MAX_URI],
9097 /* Username portion of URI */
9098 host[HTTP_MAX_URI],
9099 /* Host portion of URI */
9100 resource[HTTP_MAX_URI];
9101 /* Resource portion of URI */
9102 int port; /* Port portion of URI */
9103 mime_type_t *filetype; /* Type of file */
9104 char super[MIME_MAX_SUPER],
9105 /* Supertype of file */
9106 type[MIME_MAX_TYPE],
9107 /* Subtype of file */
9108 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
9109 /* Textual name of mime type */
9110 char filename[1024]; /* Job filename */
9111 cupsd_printer_t *printer; /* Current printer */
9112 struct stat fileinfo; /* File information */
9113 int kbytes; /* Size of file */
9114 int compression; /* Type of compression */
9115 int start_job; /* Start the job? */
9116
9117
9118 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con,
9119 con->number, uri->values[0].string.text);
9120
9121 /*
9122 * See if we have a job URI or a printer URI...
9123 */
9124
9125 if (!strcmp(uri->name, "printer-uri"))
9126 {
9127 /*
9128 * Got a printer URI; see if we also have a job-id attribute...
9129 */
9130
9131 if ((attr = ippFindAttribute(con->request, "job-id",
9132 IPP_TAG_INTEGER)) == NULL)
9133 {
9134 send_ipp_status(con, IPP_BAD_REQUEST,
9135 _("Got a printer-uri attribute but no job-id."));
9136 return;
9137 }
9138
9139 jobid = attr->values[0].integer;
9140 }
9141 else
9142 {
9143 /*
9144 * Got a job URI; parse it to get the job ID...
9145 */
9146
9147 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9148 sizeof(scheme), username, sizeof(username), host,
9149 sizeof(host), &port, resource, sizeof(resource));
9150
9151 if (strncmp(resource, "/jobs/", 6))
9152 {
9153 /*
9154 * Not a valid URI!
9155 */
9156
9157 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
9158 uri->values[0].string.text);
9159 return;
9160 }
9161
9162 jobid = atoi(resource + 6);
9163 }
9164
9165 /*
9166 * See if the job exists...
9167 */
9168
9169 if ((job = cupsdFindJob(jobid)) == NULL)
9170 {
9171 /*
9172 * Nope - return a "not found" error...
9173 */
9174
9175 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
9176 return;
9177 }
9178
9179 printer = cupsdFindDest(job->dest);
9180
9181 /*
9182 * See if the job is owned by the requesting user...
9183 */
9184
9185 if (!validate_user(job, con, job->username, username, sizeof(username)))
9186 {
9187 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9188 cupsdFindDest(job->dest));
9189 return;
9190 }
9191
9192 /*
9193 * OK, see if the client is sending the document compressed - CUPS
9194 * only supports "none" and "gzip".
9195 */
9196
9197 compression = CUPS_FILE_NONE;
9198
9199 if ((attr = ippFindAttribute(con->request, "compression",
9200 IPP_TAG_KEYWORD)) != NULL)
9201 {
9202 if (strcmp(attr->values[0].string.text, "none")
9203 #ifdef HAVE_LIBZ
9204 && strcmp(attr->values[0].string.text, "gzip")
9205 #endif /* HAVE_LIBZ */
9206 )
9207 {
9208 send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"."),
9209 attr->values[0].string.text);
9210 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
9211 "compression", NULL, attr->values[0].string.text);
9212 return;
9213 }
9214
9215 #ifdef HAVE_LIBZ
9216 if (!strcmp(attr->values[0].string.text, "gzip"))
9217 compression = CUPS_FILE_GZIP;
9218 #endif /* HAVE_LIBZ */
9219 }
9220
9221 /*
9222 * Do we have a file to print?
9223 */
9224
9225 if ((attr = ippFindAttribute(con->request, "last-document",
9226 IPP_TAG_BOOLEAN)) == NULL)
9227 {
9228 send_ipp_status(con, IPP_BAD_REQUEST,
9229 _("Missing last-document attribute in request."));
9230 return;
9231 }
9232
9233 if (!con->filename)
9234 {
9235 /*
9236 * Check for an empty request with "last-document" set to true, which is
9237 * used to close an "open" job by RFC 2911, section 3.3.2.
9238 */
9239
9240 if (job->num_files > 0 && attr->values[0].boolean)
9241 goto last_document;
9242
9243 send_ipp_status(con, IPP_BAD_REQUEST, _("No file in print request."));
9244 return;
9245 }
9246
9247 /*
9248 * Is it a format we support?
9249 */
9250
9251 if ((format = ippFindAttribute(con->request, "document-format",
9252 IPP_TAG_MIMETYPE)) != NULL)
9253 {
9254 /*
9255 * Grab format from client...
9256 */
9257
9258 if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]",
9259 super, type) != 2)
9260 {
9261 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"."),
9262 format->values[0].string.text);
9263 return;
9264 }
9265 }
9266 else if ((default_format = cupsGetOption("document-format",
9267 printer->num_options,
9268 printer->options)) != NULL)
9269 {
9270 /*
9271 * Use default document format...
9272 */
9273
9274 if (sscanf(default_format, "%15[^/]/%255[^;]", super, type) != 2)
9275 {
9276 send_ipp_status(con, IPP_BAD_REQUEST,
9277 _("Bad document-format-default \"%s\"."), default_format);
9278 return;
9279 }
9280 }
9281 else
9282 {
9283 /*
9284 * No document format attribute? Auto-type it!
9285 */
9286
9287 strlcpy(super, "application", sizeof(super));
9288 strlcpy(type, "octet-stream", sizeof(type));
9289 }
9290
9291 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
9292 {
9293 /*
9294 * Auto-type the file...
9295 */
9296
9297 ipp_attribute_t *doc_name; /* document-name attribute */
9298
9299
9300 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Auto-typing file...");
9301
9302 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
9303 filetype = mimeFileType(MimeDatabase, con->filename,
9304 doc_name ? doc_name->values[0].string.text : NULL,
9305 &compression);
9306
9307 if (!filetype)
9308 filetype = mimeType(MimeDatabase, super, type);
9309
9310 if (filetype)
9311 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Request file type is %s/%s.",
9312 filetype->super, filetype->type);
9313 }
9314 else
9315 filetype = mimeType(MimeDatabase, super, type);
9316
9317 if (filetype)
9318 {
9319 /*
9320 * Replace the document-format attribute value with the auto-typed or
9321 * default one.
9322 */
9323
9324 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9325 filetype->type);
9326
9327 if ((jformat = ippFindAttribute(job->attrs, "document-format",
9328 IPP_TAG_MIMETYPE)) != NULL)
9329 {
9330 _cupsStrFree(jformat->values[0].string.text);
9331
9332 jformat->values[0].string.text = _cupsStrAlloc(mimetype);
9333 }
9334 else
9335 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
9336 "document-format", NULL, mimetype);
9337 }
9338 else if (!filetype)
9339 {
9340 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9341 _("Unsupported document-format \"%s/%s\"."), super, type);
9342 cupsdLogMessage(CUPSD_LOG_INFO,
9343 "Hint: Do you have the raw file printing rules enabled?");
9344
9345 if (format)
9346 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9347 "document-format", NULL, format->values[0].string.text);
9348
9349 return;
9350 }
9351
9352 if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype))
9353 {
9354 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9355 filetype->type);
9356
9357 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9358 _("Unsupported document-format \"%s\"."), mimetype);
9359
9360 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9361 "document-format", NULL, mimetype);
9362
9363 return;
9364 }
9365
9366 /*
9367 * Add the file to the job...
9368 */
9369
9370 cupsdLoadJob(job);
9371
9372 if (add_file(con, job, filetype, compression))
9373 return;
9374
9375 if (stat(con->filename, &fileinfo))
9376 kbytes = 0;
9377 else
9378 kbytes = (fileinfo.st_size + 1023) / 1024;
9379
9380 cupsdUpdateQuota(printer, job->username, 0, kbytes);
9381
9382 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
9383 IPP_TAG_INTEGER)) != NULL)
9384 attr->values[0].integer += kbytes;
9385
9386 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
9387 job->num_files);
9388 rename(con->filename, filename);
9389
9390 cupsdClearString(&con->filename);
9391
9392 cupsdLogJob(job, CUPSD_LOG_INFO, "File of type %s/%s queued by \"%s\".",
9393 filetype->super, filetype->type, job->username);
9394
9395 /*
9396 * Start the job if this is the last document...
9397 */
9398
9399 last_document:
9400
9401 if ((attr = ippFindAttribute(con->request, "last-document",
9402 IPP_TAG_BOOLEAN)) != NULL &&
9403 attr->values[0].boolean)
9404 {
9405 /*
9406 * See if we need to add the ending sheet...
9407 */
9408
9409 if (cupsdTimeoutJob(job))
9410 return;
9411
9412 if (job->state_value == IPP_JOB_STOPPED)
9413 {
9414 job->state->values[0].integer = IPP_JOB_PENDING;
9415 job->state_value = IPP_JOB_PENDING;
9416
9417 ippSetString(job->attrs, &job->reasons, 0, "none");
9418 }
9419 else if (job->state_value == IPP_JOB_HELD)
9420 {
9421 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9422 IPP_TAG_KEYWORD)) == NULL)
9423 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9424
9425 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
9426 {
9427 job->state->values[0].integer = IPP_JOB_PENDING;
9428 job->state_value = IPP_JOB_PENDING;
9429
9430 ippSetString(job->attrs, &job->reasons, 0, "none");
9431 }
9432 else
9433 ippSetString(job->attrs, &job->reasons, 0, "job-hold-until-specified");
9434 }
9435
9436 job->dirty = 1;
9437 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
9438
9439 start_job = 1;
9440 }
9441 else
9442 {
9443 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9444 IPP_TAG_KEYWORD)) == NULL)
9445 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9446
9447 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
9448 {
9449 job->state->values[0].integer = IPP_JOB_HELD;
9450 job->state_value = IPP_JOB_HELD;
9451 job->hold_until = time(NULL) + MultipleOperationTimeout;
9452
9453 ippSetString(job->attrs, &job->reasons, 0, "job-incoming");
9454
9455 job->dirty = 1;
9456 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
9457 }
9458
9459 start_job = 0;
9460 }
9461
9462 /*
9463 * Fill in the response info...
9464 */
9465
9466 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
9467 con->clientname, con->clientport, "/jobs/%d", jobid);
9468 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
9469 job_uri);
9470
9471 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid);
9472
9473 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
9474 job->state_value);
9475 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons",
9476 NULL, job->reasons->values[0].string.text);
9477
9478 con->response->request.status.status_code = IPP_OK;
9479
9480 /*
9481 * Start the job if necessary...
9482 */
9483
9484 if (start_job)
9485 cupsdCheckJobs();
9486 }
9487
9488
9489 /*
9490 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9491 */
9492
9493 static void
9494 send_http_error(
9495 cupsd_client_t *con, /* I - Client connection */
9496 http_status_t status, /* I - HTTP status code */
9497 cupsd_printer_t *printer) /* I - Printer, if any */
9498 {
9499 ipp_attribute_t *uri; /* Request URI, if any */
9500
9501
9502 if ((uri = ippFindAttribute(con->request, "printer-uri",
9503 IPP_TAG_URI)) == NULL)
9504 uri = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI);
9505
9506 cupsdLogMessage(status == HTTP_FORBIDDEN ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
9507 "[Client %d] Returning HTTP %s for %s (%s) from %s",
9508 con->number, httpStatus(status),
9509 con->request ?
9510 ippOpString(con->request->request.op.operation_id) :
9511 "no operation-id",
9512 uri ? uri->values[0].string.text : "no URI",
9513 con->http->hostname);
9514
9515 if (printer)
9516 {
9517 int auth_type; /* Type of authentication required */
9518
9519
9520 auth_type = CUPSD_AUTH_NONE;
9521
9522 if (status == HTTP_UNAUTHORIZED &&
9523 printer->num_auth_info_required > 0 &&
9524 !strcmp(printer->auth_info_required[0], "negotiate") &&
9525 con->request &&
9526 (con->request->request.op.operation_id == IPP_PRINT_JOB ||
9527 con->request->request.op.operation_id == IPP_CREATE_JOB ||
9528 con->request->request.op.operation_id == CUPS_AUTHENTICATE_JOB))
9529 {
9530 /*
9531 * Creating and authenticating jobs requires Kerberos...
9532 */
9533
9534 auth_type = CUPSD_AUTH_NEGOTIATE;
9535 }
9536 else
9537 {
9538 /*
9539 * Use policy/location-defined authentication requirements...
9540 */
9541
9542 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
9543 cupsd_location_t *auth; /* Pointer to authentication element */
9544
9545
9546 if (printer->type & CUPS_PRINTER_CLASS)
9547 snprintf(resource, sizeof(resource), "/classes/%s", printer->name);
9548 else
9549 snprintf(resource, sizeof(resource), "/printers/%s", printer->name);
9550
9551 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
9552 auth->type == CUPSD_AUTH_NONE)
9553 auth = cupsdFindPolicyOp(printer->op_policy_ptr,
9554 con->request ?
9555 con->request->request.op.operation_id :
9556 IPP_PRINT_JOB);
9557
9558 if (auth)
9559 {
9560 if (auth->type == CUPSD_AUTH_DEFAULT)
9561 auth_type = cupsdDefaultAuthType();
9562 else
9563 auth_type = auth->type;
9564 }
9565 }
9566
9567 cupsdSendError(con, status, auth_type);
9568 }
9569 else
9570 cupsdSendError(con, status, CUPSD_AUTH_NONE);
9571
9572 ippDelete(con->response);
9573 con->response = NULL;
9574
9575 return;
9576 }
9577
9578
9579 /*
9580 * 'send_ipp_status()' - Send a status back to the IPP client.
9581 */
9582
9583 static void
9584 send_ipp_status(cupsd_client_t *con, /* I - Client connection */
9585 ipp_status_t status, /* I - IPP status code */
9586 const char *message,/* I - Status message */
9587 ...) /* I - Additional args as needed */
9588 {
9589 va_list ap; /* Pointer to additional args */
9590 char formatted[1024]; /* Formatted errror message */
9591
9592
9593 va_start(ap, message);
9594 vsnprintf(formatted, sizeof(formatted),
9595 _cupsLangString(con->language, message), ap);
9596 va_end(ap);
9597
9598 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s",
9599 ippOpString(con->request->request.op.operation_id),
9600 ippErrorString(status), formatted);
9601
9602 con->response->request.status.status_code = status;
9603
9604 if (ippFindAttribute(con->response, "attributes-charset",
9605 IPP_TAG_ZERO) == NULL)
9606 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
9607 "attributes-charset", NULL, "utf-8");
9608
9609 if (ippFindAttribute(con->response, "attributes-natural-language",
9610 IPP_TAG_ZERO) == NULL)
9611 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
9612 "attributes-natural-language", NULL, DefaultLanguage);
9613
9614 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT,
9615 "status-message", NULL, formatted);
9616 }
9617
9618
9619 /*
9620 * 'set_default()' - Set the default destination...
9621 */
9622
9623 static void
9624 set_default(cupsd_client_t *con, /* I - Client connection */
9625 ipp_attribute_t *uri) /* I - Printer URI */
9626 {
9627 http_status_t status; /* Policy status */
9628 cups_ptype_t dtype; /* Destination type (printer/class) */
9629 cupsd_printer_t *printer, /* Printer */
9630 *oldprinter; /* Old default printer */
9631
9632
9633 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con,
9634 con->number, uri->values[0].string.text);
9635
9636 /*
9637 * Is the destination valid?
9638 */
9639
9640 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
9641 {
9642 /*
9643 * Bad URI...
9644 */
9645
9646 send_ipp_status(con, IPP_NOT_FOUND,
9647 _("The printer or class does not exist."));
9648 return;
9649 }
9650
9651 /*
9652 * Check policy...
9653 */
9654
9655 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
9656 {
9657 send_http_error(con, status, NULL);
9658 return;
9659 }
9660
9661 /*
9662 * Set it as the default...
9663 */
9664
9665 oldprinter = DefaultPrinter;
9666 DefaultPrinter = printer;
9667
9668 if (oldprinter)
9669 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, oldprinter, NULL,
9670 "%s is no longer the default printer.", oldprinter->name);
9671
9672 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
9673 "%s is now the default printer.", printer->name);
9674
9675 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES |
9676 CUPSD_DIRTY_PRINTCAP);
9677
9678 cupsdLogMessage(CUPSD_LOG_INFO,
9679 "Default destination set to \"%s\" by \"%s\".",
9680 printer->name, get_username(con));
9681
9682 /*
9683 * Everything was ok, so return OK status...
9684 */
9685
9686 con->response->request.status.status_code = IPP_OK;
9687 }
9688
9689
9690 /*
9691 * 'set_job_attrs()' - Set job attributes.
9692 */
9693
9694 static void
9695 set_job_attrs(cupsd_client_t *con, /* I - Client connection */
9696 ipp_attribute_t *uri) /* I - Job URI */
9697 {
9698 ipp_attribute_t *attr, /* Current attribute */
9699 *attr2; /* Job attribute */
9700 int jobid; /* Job ID */
9701 cupsd_job_t *job; /* Current job */
9702 char scheme[HTTP_MAX_URI],
9703 /* Method portion of URI */
9704 username[HTTP_MAX_URI],
9705 /* Username portion of URI */
9706 host[HTTP_MAX_URI],
9707 /* Host portion of URI */
9708 resource[HTTP_MAX_URI];
9709 /* Resource portion of URI */
9710 int port; /* Port portion of URI */
9711 int event; /* Events? */
9712 int check_jobs; /* Check jobs? */
9713
9714
9715 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con,
9716 con->number, uri->values[0].string.text);
9717
9718 /*
9719 * Start with "everything is OK" status...
9720 */
9721
9722 con->response->request.status.status_code = IPP_OK;
9723
9724 /*
9725 * See if we have a job URI or a printer URI...
9726 */
9727
9728 if (!strcmp(uri->name, "printer-uri"))
9729 {
9730 /*
9731 * Got a printer URI; see if we also have a job-id attribute...
9732 */
9733
9734 if ((attr = ippFindAttribute(con->request, "job-id",
9735 IPP_TAG_INTEGER)) == NULL)
9736 {
9737 send_ipp_status(con, IPP_BAD_REQUEST,
9738 _("Got a printer-uri attribute but no job-id."));
9739 return;
9740 }
9741
9742 jobid = attr->values[0].integer;
9743 }
9744 else
9745 {
9746 /*
9747 * Got a job URI; parse it to get the job ID...
9748 */
9749
9750 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9751 sizeof(scheme), username, sizeof(username), host,
9752 sizeof(host), &port, resource, sizeof(resource));
9753
9754 if (strncmp(resource, "/jobs/", 6))
9755 {
9756 /*
9757 * Not a valid URI!
9758 */
9759
9760 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
9761 uri->values[0].string.text);
9762 return;
9763 }
9764
9765 jobid = atoi(resource + 6);
9766 }
9767
9768 /*
9769 * See if the job exists...
9770 */
9771
9772 if ((job = cupsdFindJob(jobid)) == NULL)
9773 {
9774 /*
9775 * Nope - return a "not found" error...
9776 */
9777
9778 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
9779 return;
9780 }
9781
9782 /*
9783 * See if the job has been completed...
9784 */
9785
9786 if (job->state_value > IPP_JOB_STOPPED)
9787 {
9788 /*
9789 * Return a "not-possible" error...
9790 */
9791
9792 send_ipp_status(con, IPP_NOT_POSSIBLE,
9793 _("Job #%d is finished and cannot be altered."), jobid);
9794 return;
9795 }
9796
9797 /*
9798 * See if the job is owned by the requesting user...
9799 */
9800
9801 if (!validate_user(job, con, job->username, username, sizeof(username)))
9802 {
9803 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9804 cupsdFindDest(job->dest));
9805 return;
9806 }
9807
9808 /*
9809 * See what the user wants to change.
9810 */
9811
9812 cupsdLoadJob(job);
9813
9814 check_jobs = 0;
9815 event = 0;
9816
9817 for (attr = con->request->attrs; attr; attr = attr->next)
9818 {
9819 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
9820 continue;
9821
9822 if (!strcmp(attr->name, "attributes-charset") ||
9823 !strcmp(attr->name, "attributes-natural-language") ||
9824 !strcmp(attr->name, "document-compression") ||
9825 !strcmp(attr->name, "document-format") ||
9826 !strcmp(attr->name, "job-detailed-status-messages") ||
9827 !strcmp(attr->name, "job-document-access-errors") ||
9828 !strcmp(attr->name, "job-id") ||
9829 !strcmp(attr->name, "job-impressions-completed") ||
9830 !strcmp(attr->name, "job-k-octets") ||
9831 !strcmp(attr->name, "job-originating-host-name") ||
9832 !strcmp(attr->name, "job-originating-user-name") ||
9833 !strcmp(attr->name, "job-printer-up-time") ||
9834 !strcmp(attr->name, "job-printer-uri") ||
9835 !strcmp(attr->name, "job-sheets") ||
9836 !strcmp(attr->name, "job-state-message") ||
9837 !strcmp(attr->name, "job-state-reasons") ||
9838 !strcmp(attr->name, "job-uri") ||
9839 !strcmp(attr->name, "number-of-documents") ||
9840 !strcmp(attr->name, "number-of-intervening-jobs") ||
9841 !strcmp(attr->name, "output-device-assigned") ||
9842 !strncmp(attr->name, "date-time-at-", 13) ||
9843 !strncmp(attr->name, "job-k-octets", 12) ||
9844 !strncmp(attr->name, "job-media-sheets", 16) ||
9845 !strncmp(attr->name, "time-at-", 8))
9846 {
9847 /*
9848 * Read-only attrs!
9849 */
9850
9851 send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE,
9852 _("%s cannot be changed."), attr->name);
9853
9854 attr2 = ippCopyAttribute(con->response, attr, 0);
9855 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
9856 continue;
9857 }
9858
9859 if (!strcmp(attr->name, "job-priority"))
9860 {
9861 /*
9862 * Change the job priority...
9863 */
9864
9865 if (attr->value_tag != IPP_TAG_INTEGER)
9866 {
9867 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value."));
9868
9869 attr2 = ippCopyAttribute(con->response, attr, 0);
9870 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
9871 }
9872 else if (job->state_value >= IPP_JOB_PROCESSING)
9873 {
9874 send_ipp_status(con, IPP_NOT_POSSIBLE,
9875 _("Job is completed and cannot be changed."));
9876 return;
9877 }
9878 else if (con->response->request.status.status_code == IPP_OK)
9879 {
9880 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-priority to %d",
9881 attr->values[0].integer);
9882 cupsdSetJobPriority(job, attr->values[0].integer);
9883
9884 check_jobs = 1;
9885 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED |
9886 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED;
9887 }
9888 }
9889 else if (!strcmp(attr->name, "job-state"))
9890 {
9891 /*
9892 * Change the job state...
9893 */
9894
9895 if (attr->value_tag != IPP_TAG_ENUM)
9896 {
9897 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value."));
9898
9899 attr2 = ippCopyAttribute(con->response, attr, 0);
9900 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
9901 }
9902 else
9903 {
9904 switch (attr->values[0].integer)
9905 {
9906 case IPP_JOB_PENDING :
9907 case IPP_JOB_HELD :
9908 if (job->state_value > IPP_JOB_HELD)
9909 {
9910 send_ipp_status(con, IPP_NOT_POSSIBLE,
9911 _("Job state cannot be changed."));
9912 return;
9913 }
9914 else if (con->response->request.status.status_code == IPP_OK)
9915 {
9916 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
9917 attr->values[0].integer);
9918 cupsdSetJobState(job, attr->values[0].integer,
9919 CUPSD_JOB_DEFAULT,
9920 "Job state changed by \"%s\"", username);
9921 check_jobs = 1;
9922 }
9923 break;
9924
9925 case IPP_JOB_PROCESSING :
9926 case IPP_JOB_STOPPED :
9927 if (job->state_value != attr->values[0].integer)
9928 {
9929 send_ipp_status(con, IPP_NOT_POSSIBLE,
9930 _("Job state cannot be changed."));
9931 return;
9932 }
9933 break;
9934
9935 case IPP_JOB_CANCELED :
9936 case IPP_JOB_ABORTED :
9937 case IPP_JOB_COMPLETED :
9938 if (job->state_value > IPP_JOB_PROCESSING)
9939 {
9940 send_ipp_status(con, IPP_NOT_POSSIBLE,
9941 _("Job state cannot be changed."));
9942 return;
9943 }
9944 else if (con->response->request.status.status_code == IPP_OK)
9945 {
9946 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
9947 attr->values[0].integer);
9948 cupsdSetJobState(job, (ipp_jstate_t)attr->values[0].integer,
9949 CUPSD_JOB_DEFAULT,
9950 "Job state changed by \"%s\"", username);
9951 check_jobs = 1;
9952 }
9953 break;
9954 }
9955 }
9956 }
9957 else if (con->response->request.status.status_code != IPP_OK)
9958 continue;
9959 else if ((attr2 = ippFindAttribute(job->attrs, attr->name,
9960 IPP_TAG_ZERO)) != NULL)
9961 {
9962 /*
9963 * Some other value; first free the old value...
9964 */
9965
9966 if (job->attrs->prev)
9967 job->attrs->prev->next = attr2->next;
9968 else
9969 job->attrs->attrs = attr2->next;
9970
9971 if (job->attrs->last == attr2)
9972 job->attrs->last = job->attrs->prev;
9973
9974 ippDeleteAttribute(NULL, attr2);
9975
9976 /*
9977 * Then copy the attribute...
9978 */
9979
9980 ippCopyAttribute(job->attrs, attr, 0);
9981
9982 /*
9983 * See if the job-name or job-hold-until is being changed.
9984 */
9985
9986 if (!strcmp(attr->name, "job-hold-until"))
9987 {
9988 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s",
9989 attr->values[0].string.text);
9990 cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
9991
9992 if (!strcmp(attr->values[0].string.text, "no-hold"))
9993 {
9994 cupsdReleaseJob(job);
9995 check_jobs = 1;
9996 }
9997 else
9998 cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT,
9999 "Job held by \"%s\".", username);
10000
10001 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
10002 }
10003 }
10004 else if (attr->value_tag == IPP_TAG_DELETEATTR)
10005 {
10006 /*
10007 * Delete the attribute...
10008 */
10009
10010 if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10011 IPP_TAG_ZERO)) != NULL)
10012 {
10013 if (job->attrs->prev)
10014 job->attrs->prev->next = attr2->next;
10015 else
10016 job->attrs->attrs = attr2->next;
10017
10018 if (attr2 == job->attrs->last)
10019 job->attrs->last = job->attrs->prev;
10020
10021 ippDeleteAttribute(NULL, attr2);
10022
10023 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
10024 }
10025 }
10026 else
10027 {
10028 /*
10029 * Add new option by copying it...
10030 */
10031
10032 ippCopyAttribute(job->attrs, attr, 0);
10033
10034 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
10035 }
10036 }
10037
10038 /*
10039 * Save the job...
10040 */
10041
10042 job->dirty = 1;
10043 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
10044
10045 /*
10046 * Send events as needed...
10047 */
10048
10049 if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED)
10050 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED,
10051 cupsdFindDest(job->dest), job,
10052 "Job priority changed by user.");
10053
10054 if (event & CUPSD_EVENT_JOB_STATE)
10055 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
10056 job->state_value == IPP_JOB_HELD ?
10057 "Job held by user." : "Job restarted by user.");
10058
10059 if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED)
10060 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
10061 "Job options changed by user.");
10062
10063 /*
10064 * Start jobs if possible...
10065 */
10066
10067 if (check_jobs)
10068 cupsdCheckJobs();
10069 }
10070
10071
10072 /*
10073 * 'set_printer_attrs()' - Set printer attributes.
10074 */
10075
10076 static void
10077 set_printer_attrs(cupsd_client_t *con, /* I - Client connection */
10078 ipp_attribute_t *uri) /* I - Printer */
10079 {
10080 http_status_t status; /* Policy status */
10081 cups_ptype_t dtype; /* Destination type (printer/class) */
10082 cupsd_printer_t *printer; /* Printer/class */
10083 ipp_attribute_t *attr; /* Printer attribute */
10084 int changed = 0; /* Was anything changed? */
10085
10086
10087 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_attrs(%p[%d], %s)", con,
10088 con->number, uri->values[0].string.text);
10089
10090 /*
10091 * Is the destination valid?
10092 */
10093
10094 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10095 {
10096 /*
10097 * Bad URI...
10098 */
10099
10100 send_ipp_status(con, IPP_NOT_FOUND,
10101 _("The printer or class does not exist."));
10102 return;
10103 }
10104
10105 /*
10106 * Check policy...
10107 */
10108
10109 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10110 {
10111 send_http_error(con, status, printer);
10112 return;
10113 }
10114
10115 /*
10116 * Return a list of attributes that can be set via Set-Printer-Attributes.
10117 */
10118
10119 if ((attr = ippFindAttribute(con->request, "printer-location",
10120 IPP_TAG_TEXT)) != NULL)
10121 {
10122 cupsdSetString(&printer->location, attr->values[0].string.text);
10123 changed = 1;
10124 }
10125
10126 if ((attr = ippFindAttribute(con->request, "printer-info",
10127 IPP_TAG_TEXT)) != NULL)
10128 {
10129 cupsdSetString(&printer->info, attr->values[0].string.text);
10130 changed = 1;
10131 }
10132
10133 /*
10134 * Update the printer attributes and return...
10135 */
10136
10137 if (changed)
10138 {
10139 cupsdSetPrinterAttrs(printer);
10140 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
10141
10142 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL,
10143 "Printer \"%s\" description or location changed by \"%s\".",
10144 printer->name, get_username(con));
10145
10146 cupsdLogMessage(CUPSD_LOG_INFO,
10147 "Printer \"%s\" description or location changed by \"%s\".",
10148 printer->name, get_username(con));
10149 }
10150
10151 con->response->request.status.status_code = IPP_OK;
10152 }
10153
10154
10155 /*
10156 * 'set_printer_defaults()' - Set printer default options from a request.
10157 */
10158
10159 static void
10160 set_printer_defaults(
10161 cupsd_client_t *con, /* I - Client connection */
10162 cupsd_printer_t *printer) /* I - Printer */
10163 {
10164 int i; /* Looping var */
10165 ipp_attribute_t *attr; /* Current attribute */
10166 int namelen; /* Length of attribute name */
10167 char name[256], /* New attribute name */
10168 value[256]; /* String version of integer attrs */
10169
10170
10171 for (attr = con->request->attrs; attr; attr = attr->next)
10172 {
10173 /*
10174 * Skip non-printer attributes...
10175 */
10176
10177 if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
10178 continue;
10179
10180 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name);
10181
10182 if (!strcmp(attr->name, "job-sheets-default"))
10183 {
10184 /*
10185 * Only allow keywords and names...
10186 */
10187
10188 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10189 continue;
10190
10191 /*
10192 * Only allow job-sheets-default to be set when running without a
10193 * system high classification level...
10194 */
10195
10196 if (Classification)
10197 continue;
10198
10199 cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text);
10200
10201 if (attr->num_values > 1)
10202 cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text);
10203 else
10204 cupsdSetString(&printer->job_sheets[1], "none");
10205 }
10206 else if (!strcmp(attr->name, "requesting-user-name-allowed"))
10207 {
10208 cupsdFreeStrings(&(printer->users));
10209
10210 printer->deny_users = 0;
10211
10212 if (attr->value_tag == IPP_TAG_NAME &&
10213 (attr->num_values > 1 ||
10214 strcmp(attr->values[0].string.text, "all")))
10215 {
10216 for (i = 0; i < attr->num_values; i ++)
10217 cupsdAddString(&(printer->users), attr->values[i].string.text);
10218 }
10219 }
10220 else if (!strcmp(attr->name, "requesting-user-name-denied"))
10221 {
10222 cupsdFreeStrings(&(printer->users));
10223
10224 printer->deny_users = 1;
10225
10226 if (attr->value_tag == IPP_TAG_NAME &&
10227 (attr->num_values > 1 ||
10228 strcmp(attr->values[0].string.text, "none")))
10229 {
10230 for (i = 0; i < attr->num_values; i ++)
10231 cupsdAddString(&(printer->users), attr->values[i].string.text);
10232 }
10233 }
10234 else if (!strcmp(attr->name, "job-quota-period"))
10235 {
10236 if (attr->value_tag != IPP_TAG_INTEGER)
10237 continue;
10238
10239 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...",
10240 attr->values[0].integer);
10241 cupsdFreeQuotas(printer);
10242
10243 printer->quota_period = attr->values[0].integer;
10244 }
10245 else if (!strcmp(attr->name, "job-k-limit"))
10246 {
10247 if (attr->value_tag != IPP_TAG_INTEGER)
10248 continue;
10249
10250 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...",
10251 attr->values[0].integer);
10252 cupsdFreeQuotas(printer);
10253
10254 printer->k_limit = attr->values[0].integer;
10255 }
10256 else if (!strcmp(attr->name, "job-page-limit"))
10257 {
10258 if (attr->value_tag != IPP_TAG_INTEGER)
10259 continue;
10260
10261 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...",
10262 attr->values[0].integer);
10263 cupsdFreeQuotas(printer);
10264
10265 printer->page_limit = attr->values[0].integer;
10266 }
10267 else if (!strcmp(attr->name, "printer-op-policy"))
10268 {
10269 cupsd_policy_t *p; /* Policy */
10270
10271
10272 if (attr->value_tag != IPP_TAG_NAME)
10273 continue;
10274
10275 if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
10276 {
10277 cupsdLogMessage(CUPSD_LOG_DEBUG,
10278 "Setting printer-op-policy to \"%s\"...",
10279 attr->values[0].string.text);
10280 cupsdSetString(&printer->op_policy, attr->values[0].string.text);
10281 printer->op_policy_ptr = p;
10282 }
10283 else
10284 {
10285 send_ipp_status(con, IPP_NOT_POSSIBLE,
10286 _("Unknown printer-op-policy \"%s\"."),
10287 attr->values[0].string.text);
10288 return;
10289 }
10290 }
10291 else if (!strcmp(attr->name, "printer-error-policy"))
10292 {
10293 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10294 continue;
10295
10296 if (strcmp(attr->values[0].string.text, "retry-current-job") &&
10297 ((printer->type & CUPS_PRINTER_CLASS) ||
10298 (strcmp(attr->values[0].string.text, "abort-job") &&
10299 strcmp(attr->values[0].string.text, "retry-job") &&
10300 strcmp(attr->values[0].string.text, "stop-printer"))))
10301 {
10302 send_ipp_status(con, IPP_NOT_POSSIBLE,
10303 _("Unknown printer-error-policy \"%s\"."),
10304 attr->values[0].string.text);
10305 return;
10306 }
10307
10308 cupsdLogMessage(CUPSD_LOG_DEBUG,
10309 "Setting printer-error-policy to \"%s\"...",
10310 attr->values[0].string.text);
10311 cupsdSetString(&printer->error_policy, attr->values[0].string.text);
10312 }
10313
10314 /*
10315 * Skip any other non-default attributes...
10316 */
10317
10318 namelen = strlen(attr->name);
10319 if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") ||
10320 namelen > (sizeof(name) - 1) || attr->num_values != 1)
10321 continue;
10322
10323 /*
10324 * OK, anything else must be a user-defined default...
10325 */
10326
10327 strlcpy(name, attr->name, sizeof(name));
10328 name[namelen - 8] = '\0'; /* Strip "-default" */
10329
10330 switch (attr->value_tag)
10331 {
10332 case IPP_TAG_DELETEATTR :
10333 printer->num_options = cupsRemoveOption(name,
10334 printer->num_options,
10335 &(printer->options));
10336 cupsdLogMessage(CUPSD_LOG_DEBUG,
10337 "Deleting %s", attr->name);
10338 break;
10339
10340 case IPP_TAG_NAME :
10341 case IPP_TAG_TEXT :
10342 case IPP_TAG_KEYWORD :
10343 case IPP_TAG_URI :
10344 printer->num_options = cupsAddOption(name,
10345 attr->values[0].string.text,
10346 printer->num_options,
10347 &(printer->options));
10348 cupsdLogMessage(CUPSD_LOG_DEBUG,
10349 "Setting %s to \"%s\"...", attr->name,
10350 attr->values[0].string.text);
10351 break;
10352
10353 case IPP_TAG_BOOLEAN :
10354 printer->num_options = cupsAddOption(name,
10355 attr->values[0].boolean ?
10356 "true" : "false",
10357 printer->num_options,
10358 &(printer->options));
10359 cupsdLogMessage(CUPSD_LOG_DEBUG,
10360 "Setting %s to %s...", attr->name,
10361 attr->values[0].boolean ? "true" : "false");
10362 break;
10363
10364 case IPP_TAG_INTEGER :
10365 case IPP_TAG_ENUM :
10366 sprintf(value, "%d", attr->values[0].integer);
10367 printer->num_options = cupsAddOption(name, value,
10368 printer->num_options,
10369 &(printer->options));
10370 cupsdLogMessage(CUPSD_LOG_DEBUG,
10371 "Setting %s to %s...", attr->name, value);
10372 break;
10373
10374 case IPP_TAG_RANGE :
10375 sprintf(value, "%d-%d", attr->values[0].range.lower,
10376 attr->values[0].range.upper);
10377 printer->num_options = cupsAddOption(name, value,
10378 printer->num_options,
10379 &(printer->options));
10380 cupsdLogMessage(CUPSD_LOG_DEBUG,
10381 "Setting %s to %s...", attr->name, value);
10382 break;
10383
10384 case IPP_TAG_RESOLUTION :
10385 sprintf(value, "%dx%d%s", attr->values[0].resolution.xres,
10386 attr->values[0].resolution.yres,
10387 attr->values[0].resolution.units == IPP_RES_PER_INCH ?
10388 "dpi" : "dpcm");
10389 printer->num_options = cupsAddOption(name, value,
10390 printer->num_options,
10391 &(printer->options));
10392 cupsdLogMessage(CUPSD_LOG_DEBUG,
10393 "Setting %s to %s...", attr->name, value);
10394 break;
10395
10396 default :
10397 /* Do nothing for other values */
10398 break;
10399 }
10400 }
10401 }
10402
10403
10404 /*
10405 * 'start_printer()' - Start a printer.
10406 */
10407
10408 static void
10409 start_printer(cupsd_client_t *con, /* I - Client connection */
10410 ipp_attribute_t *uri) /* I - Printer URI */
10411 {
10412 int i; /* Temporary variable */
10413 http_status_t status; /* Policy status */
10414 cups_ptype_t dtype; /* Destination type (printer/class) */
10415 cupsd_printer_t *printer; /* Printer data */
10416
10417
10418 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con,
10419 con->number, uri->values[0].string.text);
10420
10421 /*
10422 * Is the destination valid?
10423 */
10424
10425 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10426 {
10427 /*
10428 * Bad URI...
10429 */
10430
10431 send_ipp_status(con, IPP_NOT_FOUND,
10432 _("The printer or class does not exist."));
10433 return;
10434 }
10435
10436 /*
10437 * Check policy...
10438 */
10439
10440 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10441 {
10442 send_http_error(con, status, printer);
10443 return;
10444 }
10445
10446 /*
10447 * Start the printer...
10448 */
10449
10450 printer->state_message[0] = '\0';
10451
10452 cupsdStartPrinter(printer, 1);
10453
10454 if (dtype & CUPS_PRINTER_CLASS)
10455 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".",
10456 printer->name, get_username(con));
10457 else
10458 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".",
10459 printer->name, get_username(con));
10460
10461 cupsdCheckJobs();
10462
10463 /*
10464 * Check quotas...
10465 */
10466
10467 if ((i = check_quotas(con, printer)) < 0)
10468 {
10469 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
10470 return;
10471 }
10472 else if (i == 0)
10473 {
10474 send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print."));
10475 return;
10476 }
10477
10478 /*
10479 * Everything was ok, so return OK status...
10480 */
10481
10482 con->response->request.status.status_code = IPP_OK;
10483 }
10484
10485
10486 /*
10487 * 'stop_printer()' - Stop a printer.
10488 */
10489
10490 static void
10491 stop_printer(cupsd_client_t *con, /* I - Client connection */
10492 ipp_attribute_t *uri) /* I - Printer URI */
10493 {
10494 http_status_t status; /* Policy status */
10495 cups_ptype_t dtype; /* Destination type (printer/class) */
10496 cupsd_printer_t *printer; /* Printer data */
10497 ipp_attribute_t *attr; /* printer-state-message attribute */
10498
10499
10500 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con,
10501 con->number, uri->values[0].string.text);
10502
10503 /*
10504 * Is the destination valid?
10505 */
10506
10507 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10508 {
10509 /*
10510 * Bad URI...
10511 */
10512
10513 send_ipp_status(con, IPP_NOT_FOUND,
10514 _("The printer or class does not exist."));
10515 return;
10516 }
10517
10518 /*
10519 * Check policy...
10520 */
10521
10522 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10523 {
10524 send_http_error(con, status, printer);
10525 return;
10526 }
10527
10528 /*
10529 * Stop the printer...
10530 */
10531
10532 if ((attr = ippFindAttribute(con->request, "printer-state-message",
10533 IPP_TAG_TEXT)) == NULL)
10534 strlcpy(printer->state_message, "Paused", sizeof(printer->state_message));
10535 else
10536 {
10537 strlcpy(printer->state_message, attr->values[0].string.text,
10538 sizeof(printer->state_message));
10539 }
10540
10541 cupsdStopPrinter(printer, 1);
10542
10543 if (dtype & CUPS_PRINTER_CLASS)
10544 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".",
10545 printer->name, get_username(con));
10546 else
10547 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".",
10548 printer->name, get_username(con));
10549
10550 /*
10551 * Everything was ok, so return OK status...
10552 */
10553
10554 con->response->request.status.status_code = IPP_OK;
10555 }
10556
10557
10558 /*
10559 * 'url_encode_attr()' - URL-encode a string attribute.
10560 */
10561
10562 static void
10563 url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */
10564 char *buffer,/* I - String buffer */
10565 int bufsize)/* I - Size of buffer */
10566 {
10567 int i; /* Looping var */
10568 char *bufptr, /* Pointer into buffer */
10569 *bufend; /* End of buffer */
10570
10571
10572 strlcpy(buffer, attr->name, bufsize);
10573 bufptr = buffer + strlen(buffer);
10574 bufend = buffer + bufsize - 1;
10575
10576 for (i = 0; i < attr->num_values; i ++)
10577 {
10578 if (bufptr >= bufend)
10579 break;
10580
10581 if (i)
10582 *bufptr++ = ',';
10583 else
10584 *bufptr++ = '=';
10585
10586 if (bufptr >= bufend)
10587 break;
10588
10589 *bufptr++ = '\'';
10590
10591 bufptr = url_encode_string(attr->values[i].string.text,
10592 bufptr, bufend - bufptr + 1);
10593
10594 if (bufptr >= bufend)
10595 break;
10596
10597 *bufptr++ = '\'';
10598 }
10599
10600 *bufptr = '\0';
10601 }
10602
10603
10604 /*
10605 * 'url_encode_string()' - URL-encode a string.
10606 */
10607
10608 static char * /* O - End of string */
10609 url_encode_string(const char *s, /* I - String */
10610 char *buffer, /* I - String buffer */
10611 int bufsize) /* I - Size of buffer */
10612 {
10613 char *bufptr, /* Pointer into buffer */
10614 *bufend; /* End of buffer */
10615 static const char *hex = "0123456789ABCDEF";
10616 /* Hex digits */
10617
10618
10619 bufptr = buffer;
10620 bufend = buffer + bufsize - 1;
10621
10622 while (*s && bufptr < bufend)
10623 {
10624 if (*s == ' ' || *s == '%' || *s == '+')
10625 {
10626 if (bufptr >= (bufend - 2))
10627 break;
10628
10629 *bufptr++ = '%';
10630 *bufptr++ = hex[(*s >> 4) & 15];
10631 *bufptr++ = hex[*s & 15];
10632
10633 s ++;
10634 }
10635 else if (*s == '\'' || *s == '\\')
10636 {
10637 if (bufptr >= (bufend - 1))
10638 break;
10639
10640 *bufptr++ = '\\';
10641 *bufptr++ = *s++;
10642 }
10643 else
10644 *bufptr++ = *s++;
10645 }
10646
10647 *bufptr = '\0';
10648
10649 return (bufptr);
10650 }
10651
10652
10653 /*
10654 * 'user_allowed()' - See if a user is allowed to print to a queue.
10655 */
10656
10657 static int /* O - 0 if not allowed, 1 if allowed */
10658 user_allowed(cupsd_printer_t *p, /* I - Printer or class */
10659 const char *username) /* I - Username */
10660 {
10661 struct passwd *pw; /* User password data */
10662 char baseuser[256], /* Base username */
10663 *baseptr, /* Pointer to "@" in base username */
10664 *name; /* Current user name */
10665
10666
10667 if (cupsArrayCount(p->users) == 0)
10668 return (1);
10669
10670 if (!strcmp(username, "root"))
10671 return (1);
10672
10673 if (strchr(username, '@'))
10674 {
10675 /*
10676 * Strip @REALM for username check...
10677 */
10678
10679 strlcpy(baseuser, username, sizeof(baseuser));
10680
10681 if ((baseptr = strchr(baseuser, '@')) != NULL)
10682 *baseptr = '\0';
10683
10684 username = baseuser;
10685 }
10686
10687 pw = getpwnam(username);
10688 endpwent();
10689
10690 for (name = (char *)cupsArrayFirst(p->users);
10691 name;
10692 name = (char *)cupsArrayNext(p->users))
10693 {
10694 if (name[0] == '@')
10695 {
10696 /*
10697 * Check group membership...
10698 */
10699
10700 if (cupsdCheckGroup(username, pw, name + 1))
10701 break;
10702 }
10703 else if (name[0] == '#')
10704 {
10705 /*
10706 * Check UUID...
10707 */
10708
10709 if (cupsdCheckGroup(username, pw, name))
10710 break;
10711 }
10712 else if (!_cups_strcasecmp(username, name))
10713 break;
10714 }
10715
10716 return ((name != NULL) != p->deny_users);
10717 }
10718
10719
10720 /*
10721 * 'validate_job()' - Validate printer options and destination.
10722 */
10723
10724 static void
10725 validate_job(cupsd_client_t *con, /* I - Client connection */
10726 ipp_attribute_t *uri) /* I - Printer URI */
10727 {
10728 http_status_t status; /* Policy status */
10729 ipp_attribute_t *attr, /* Current attribute */
10730 *auth_info; /* auth-info attribute */
10731 ipp_attribute_t *format, /* Document-format attribute */
10732 *name; /* Job-name attribute */
10733 cups_ptype_t dtype; /* Destination type (printer/class) */
10734 char super[MIME_MAX_SUPER],
10735 /* Supertype of file */
10736 type[MIME_MAX_TYPE];
10737 /* Subtype of file */
10738 cupsd_printer_t *printer; /* Printer */
10739
10740
10741 cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con,
10742 con->number, uri->values[0].string.text);
10743
10744 /*
10745 * OK, see if the client is sending the document compressed - CUPS
10746 * doesn't support compression yet...
10747 */
10748
10749 if ((attr = ippFindAttribute(con->request, "compression",
10750 IPP_TAG_KEYWORD)) != NULL)
10751 {
10752 if (strcmp(attr->values[0].string.text, "none")
10753 #ifdef HAVE_LIBZ
10754 && strcmp(attr->values[0].string.text, "gzip")
10755 #endif /* HAVE_LIBZ */
10756 )
10757 {
10758 send_ipp_status(con, IPP_ATTRIBUTES,
10759 _("Unsupported 'compression' value \"%s\"."),
10760 attr->values[0].string.text);
10761 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
10762 "compression", NULL, attr->values[0].string.text);
10763 return;
10764 }
10765 }
10766
10767 /*
10768 * Is it a format we support?
10769 */
10770
10771 if ((format = ippFindAttribute(con->request, "document-format",
10772 IPP_TAG_MIMETYPE)) != NULL)
10773 {
10774 if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]",
10775 super, type) != 2)
10776 {
10777 send_ipp_status(con, IPP_BAD_REQUEST,
10778 _("Bad 'document-format' value \"%s\"."),
10779 format->values[0].string.text);
10780 return;
10781 }
10782
10783 if ((strcmp(super, "application") || strcmp(type, "octet-stream")) &&
10784 !mimeType(MimeDatabase, super, type))
10785 {
10786 cupsdLogMessage(CUPSD_LOG_INFO,
10787 "Hint: Do you have the raw file printing rules enabled?");
10788 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
10789 _("Unsupported 'document-format' value \"%s\"."),
10790 format->values[0].string.text);
10791 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
10792 "document-format", NULL, format->values[0].string.text);
10793 return;
10794 }
10795 }
10796
10797 /*
10798 * Is the job-name valid?
10799 */
10800
10801 if ((name = ippFindAttribute(con->request, "job-name", IPP_TAG_ZERO)) != NULL)
10802 {
10803 int bad_name = 0; /* Is the job-name value bad? */
10804
10805 if ((name->value_tag != IPP_TAG_NAME && name->value_tag != IPP_TAG_NAMELANG) ||
10806 name->num_values != 1)
10807 {
10808 bad_name = 1;
10809 }
10810 else
10811 {
10812 /*
10813 * Validate that job-name conforms to RFC 5198 (Network Unicode) and
10814 * IPP Everywhere requirements for "name" values...
10815 */
10816
10817 const unsigned char *nameptr; /* Pointer into "job-name" attribute */
10818
10819 for (nameptr = (unsigned char *)name->values[0].string.text;
10820 *nameptr;
10821 nameptr ++)
10822 {
10823 if (*nameptr < ' ' && *nameptr != '\t')
10824 break;
10825 else if (*nameptr == 0x7f)
10826 break;
10827 else if ((*nameptr & 0xe0) == 0xc0)
10828 {
10829 if ((nameptr[1] & 0xc0) != 0x80)
10830 break;
10831
10832 nameptr ++;
10833 }
10834 else if ((*nameptr & 0xf0) == 0xe0)
10835 {
10836 if ((nameptr[1] & 0xc0) != 0x80 ||
10837 (nameptr[2] & 0xc0) != 0x80)
10838 break;
10839
10840 nameptr += 2;
10841 }
10842 else if ((*nameptr & 0xf8) == 0xf0)
10843 {
10844 if ((nameptr[1] & 0xc0) != 0x80 ||
10845 (nameptr[2] & 0xc0) != 0x80 ||
10846 (nameptr[3] & 0xc0) != 0x80)
10847 break;
10848
10849 nameptr += 3;
10850 }
10851 else if (*nameptr & 0x80)
10852 break;
10853 }
10854
10855 if (*nameptr)
10856 bad_name = 1;
10857 }
10858
10859 if (bad_name)
10860 {
10861 if (StrictConformance)
10862 {
10863 send_ipp_status(con, IPP_ATTRIBUTES,
10864 _("Unsupported 'job-name' value."));
10865 ippCopyAttribute(con->response, name, 0);
10866 return;
10867 }
10868 else
10869 {
10870 cupsdLogMessage(CUPSD_LOG_WARN,
10871 "Unsupported 'job-name' value, deleting from request.");
10872 ippDeleteAttribute(con->request, name);
10873 }
10874 }
10875 }
10876
10877 /*
10878 * Is the destination valid?
10879 */
10880
10881 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10882 {
10883 /*
10884 * Bad URI...
10885 */
10886
10887 send_ipp_status(con, IPP_NOT_FOUND,
10888 _("The printer or class does not exist."));
10889 return;
10890 }
10891
10892 /*
10893 * Check policy...
10894 */
10895
10896 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
10897
10898 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10899 {
10900 send_http_error(con, status, printer);
10901 return;
10902 }
10903 else if (printer->num_auth_info_required == 1 &&
10904 !strcmp(printer->auth_info_required[0], "negotiate") &&
10905 !con->username[0])
10906 {
10907 send_http_error(con, HTTP_UNAUTHORIZED, printer);
10908 return;
10909 }
10910 #ifdef HAVE_SSL
10911 else if (auth_info && !con->http->tls &&
10912 !httpAddrLocalhost(con->http->hostaddr))
10913 {
10914 /*
10915 * Require encryption of auth-info over non-local connections...
10916 */
10917
10918 send_http_error(con, HTTP_UPGRADE_REQUIRED, printer);
10919 return;
10920 }
10921 #endif /* HAVE_SSL */
10922
10923 /*
10924 * Everything was ok, so return OK status...
10925 */
10926
10927 con->response->request.status.status_code = IPP_OK;
10928 }
10929
10930
10931 /*
10932 * 'validate_name()' - Make sure the printer name only contains valid chars.
10933 */
10934
10935 static int /* O - 0 if name is no good, 1 if good */
10936 validate_name(const char *name) /* I - Name to check */
10937 {
10938 const char *ptr; /* Pointer into name */
10939
10940
10941 /*
10942 * Scan the whole name...
10943 */
10944
10945 for (ptr = name; *ptr; ptr ++)
10946 if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
10947 return (0);
10948
10949 /*
10950 * All the characters are good; validate the length, too...
10951 */
10952
10953 return ((ptr - name) < 128);
10954 }
10955
10956
10957 /*
10958 * 'validate_user()' - Validate the user for the request.
10959 */
10960
10961 static int /* O - 1 if permitted, 0 otherwise */
10962 validate_user(cupsd_job_t *job, /* I - Job */
10963 cupsd_client_t *con, /* I - Client connection */
10964 const char *owner, /* I - Owner of job/resource */
10965 char *username, /* O - Authenticated username */
10966 int userlen) /* I - Length of username */
10967 {
10968 cupsd_printer_t *printer; /* Printer for job */
10969
10970
10971 cupsdLogMessage(CUPSD_LOG_DEBUG2,
10972 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
10973 "userlen=%d)",
10974 job->id, con ? con->number : 0,
10975 owner ? owner : "(null)", username, userlen);
10976
10977 /*
10978 * Validate input...
10979 */
10980
10981 if (!con || !owner || !username || userlen <= 0)
10982 return (0);
10983
10984 /*
10985 * Get the best authenticated username that is available.
10986 */
10987
10988 strlcpy(username, get_username(con), userlen);
10989
10990 /*
10991 * Check the username against the owner...
10992 */
10993
10994 printer = cupsdFindDest(job->dest);
10995
10996 return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr,
10997 con, owner) == HTTP_OK);
10998 }
10999
11000
11001 /*
11002 * End of "$Id$".
11003 */