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