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