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