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