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