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