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