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