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