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