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