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