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