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