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