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