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