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