]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/ipp.c
Merge changes from CUPS 1.4svn-r8033.
[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 scheme \"%s\"!"),
2508 scheme);
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 if ((attr = ippFindAttribute(con->request, "printer-state-reasons",
2624 IPP_TAG_KEYWORD)) != NULL)
2625 {
2626 if (attr->num_values >
2627 (int)(sizeof(printer->reasons) / sizeof(printer->reasons[0])))
2628 {
2629 send_ipp_status(con, IPP_NOT_POSSIBLE,
2630 _("Too many printer-state-reasons values (%d > %d)!"),
2631 attr->num_values,
2632 (int)(sizeof(printer->reasons) /
2633 sizeof(printer->reasons[0])));
2634 return;
2635 }
2636
2637 for (i = 0; i < printer->num_reasons; i ++)
2638 _cupsStrFree(printer->reasons[i]);
2639
2640 printer->num_reasons = 0;
2641 for (i = 0; i < attr->num_values; i ++)
2642 {
2643 if (!strcmp(attr->values[i].string.text, "none"))
2644 continue;
2645
2646 printer->reasons[printer->num_reasons] =
2647 _cupsStrAlloc(attr->values[i].string.text);
2648
2649 if (!strcmp(printer->reasons[printer->num_reasons], "paused") &&
2650 printer->state != IPP_PRINTER_STOPPED)
2651 {
2652 cupsdLogMessage(CUPSD_LOG_INFO,
2653 "Setting %s printer-state to %d (was %d.)",
2654 printer->name, IPP_PRINTER_STOPPED, printer->state);
2655 cupsdStopPrinter(printer, 0);
2656 }
2657
2658 printer->num_reasons ++;
2659 }
2660
2661 if (PrintcapFormat == PRINTCAP_PLIST)
2662 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
2663 }
2664
2665 set_printer_defaults(con, printer);
2666
2667 if ((attr = ippFindAttribute(con->request, "auth-info-required",
2668 IPP_TAG_KEYWORD)) != NULL)
2669 cupsdSetAuthInfoRequired(printer, NULL, attr);
2670
2671 /*
2672 * See if we have all required attributes...
2673 */
2674
2675 if (!printer->device_uri)
2676 cupsdSetString(&printer->device_uri, "file:///dev/null");
2677
2678 /*
2679 * See if we have an interface script or PPD file attached to the request...
2680 */
2681
2682 if (con->filename)
2683 {
2684 need_restart_job = 1;
2685
2686 strlcpy(srcfile, con->filename, sizeof(srcfile));
2687
2688 if ((fp = cupsFileOpen(srcfile, "rb")))
2689 {
2690 /*
2691 * Yes; get the first line from it...
2692 */
2693
2694 line[0] = '\0';
2695 cupsFileGets(fp, line, sizeof(line));
2696 cupsFileClose(fp);
2697
2698 /*
2699 * Then see what kind of file it is...
2700 */
2701
2702 snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
2703 printer->name);
2704
2705 if (!strncmp(line, "*PPD-Adobe", 10))
2706 {
2707 /*
2708 * The new file is a PPD file, so remove any old interface script
2709 * that might be lying around...
2710 */
2711
2712 unlink(dstfile);
2713 }
2714 else
2715 {
2716 /*
2717 * This must be an interface script, so move the file over to the
2718 * interfaces directory and make it executable...
2719 */
2720
2721 if (copy_file(srcfile, dstfile))
2722 {
2723 send_ipp_status(con, IPP_INTERNAL_ERROR,
2724 _("Unable to copy interface script - %s!"),
2725 strerror(errno));
2726 return;
2727 }
2728
2729 cupsdLogMessage(CUPSD_LOG_DEBUG,
2730 "Copied interface script successfully!");
2731 chmod(dstfile, 0755);
2732 }
2733
2734 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2735 printer->name);
2736
2737 if (!strncmp(line, "*PPD-Adobe", 10))
2738 {
2739 /*
2740 * The new file is a PPD file, so move the file over to the
2741 * ppd directory and make it readable by all...
2742 */
2743
2744 if (copy_file(srcfile, dstfile))
2745 {
2746 send_ipp_status(con, IPP_INTERNAL_ERROR,
2747 _("Unable to copy PPD file - %s!"),
2748 strerror(errno));
2749 return;
2750 }
2751
2752 cupsdLogMessage(CUPSD_LOG_DEBUG,
2753 "Copied PPD file successfully!");
2754 chmod(dstfile, 0644);
2755
2756 #ifdef __APPLE__
2757 /*
2758 * (Re)register color profiles...
2759 */
2760
2761 if (!RunUser)
2762 {
2763 apple_unregister_profiles(printer);
2764 apple_register_profiles(printer);
2765 }
2766 #endif /* __APPLE__ */
2767 }
2768 else
2769 {
2770 /*
2771 * This must be an interface script, so remove any old PPD file that
2772 * may be lying around...
2773 */
2774
2775 unlink(dstfile);
2776 }
2777 }
2778 }
2779 else if ((attr = ippFindAttribute(con->request, "ppd-name",
2780 IPP_TAG_NAME)) != NULL)
2781 {
2782 need_restart_job = 1;
2783
2784 if (!strcmp(attr->values[0].string.text, "raw"))
2785 {
2786 /*
2787 * Raw driver, remove any existing PPD or interface script files.
2788 */
2789
2790 snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
2791 printer->name);
2792 unlink(dstfile);
2793
2794 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2795 printer->name);
2796 unlink(dstfile);
2797 }
2798 else
2799 {
2800 /*
2801 * PPD model file...
2802 */
2803
2804 snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
2805 printer->name);
2806 unlink(dstfile);
2807
2808 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2809 printer->name);
2810
2811 if (copy_model(con, attr->values[0].string.text, dstfile))
2812 {
2813 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file!"));
2814 return;
2815 }
2816
2817 cupsdLogMessage(CUPSD_LOG_DEBUG,
2818 "Copied PPD file successfully!");
2819 chmod(dstfile, 0644);
2820
2821 #ifdef __APPLE__
2822 /*
2823 * (Re)register color profiles...
2824 */
2825
2826 if (!RunUser)
2827 {
2828 apple_unregister_profiles(printer);
2829 apple_register_profiles(printer);
2830 }
2831 #endif /* __APPLE__ */
2832 }
2833 }
2834
2835 /*
2836 * If we set the device URI but not the port monitor, check which port
2837 * monitor to use by default...
2838 */
2839
2840 if (set_device_uri && !set_port_monitor)
2841 {
2842 ppd_file_t *ppd; /* PPD file */
2843 ppd_attr_t *ppdattr; /* cupsPortMonitor attribute */
2844
2845
2846 httpSeparateURI(HTTP_URI_CODING_ALL, printer->device_uri, scheme,
2847 sizeof(scheme), username, sizeof(username), host,
2848 sizeof(host), &port, resource, sizeof(resource));
2849
2850 snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot,
2851 printer->name);
2852 if ((ppd = ppdOpenFile(srcfile)) != NULL)
2853 {
2854 for (ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2855 ppdattr;
2856 ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
2857 if (!strcmp(scheme, ppdattr->spec))
2858 {
2859 cupsdLogMessage(CUPSD_LOG_INFO,
2860 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2861 printer->name, ppdattr->value,
2862 printer->port_monitor ? printer->port_monitor
2863 : "none");
2864
2865 if (strcmp(ppdattr->value, "none"))
2866 cupsdSetString(&printer->port_monitor, ppdattr->value);
2867 else
2868 cupsdClearString(&printer->port_monitor);
2869
2870 break;
2871 }
2872
2873 ppdClose(ppd);
2874 }
2875 }
2876
2877 /*
2878 * Update the printer attributes and return...
2879 */
2880
2881 cupsdSetPrinterAttrs(printer);
2882 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
2883
2884 if (need_restart_job && printer->job)
2885 {
2886 cupsd_job_t *job;
2887
2888 /*
2889 * Stop the current job and then restart it below...
2890 */
2891
2892 job = (cupsd_job_t *)printer->job;
2893
2894 cupsdStopJob(job, 1);
2895
2896 job->state->values[0].integer = IPP_JOB_PENDING;
2897 job->state_value = IPP_JOB_PENDING;
2898 }
2899
2900 if (need_restart_job)
2901 cupsdCheckJobs();
2902
2903 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
2904
2905 if (modify)
2906 {
2907 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL,
2908 "Printer \"%s\" modified by \"%s\".", printer->name,
2909 get_username(con));
2910
2911 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".",
2912 printer->name, get_username(con));
2913 }
2914 else
2915 {
2916 cupsdAddPrinterHistory(printer);
2917
2918 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, printer, NULL,
2919 "New printer \"%s\" added by \"%s\".", printer->name,
2920 get_username(con));
2921
2922 cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".",
2923 printer->name, get_username(con));
2924 }
2925
2926 con->response->request.status.status_code = IPP_OK;
2927 }
2928
2929
2930 /*
2931 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2932 * based upon the printer state...
2933 */
2934
2935 static void
2936 add_printer_state_reasons(
2937 cupsd_client_t *con, /* I - Client connection */
2938 cupsd_printer_t *p) /* I - Printer info */
2939 {
2940 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2941 "add_printer_state_reasons(%p[%d], %p[%s])",
2942 con, con->http.fd, p, p->name);
2943
2944 if (p->num_reasons == 0)
2945 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2946 "printer-state-reasons", NULL,
2947 p->state == IPP_PRINTER_STOPPED ? "paused" : "none");
2948 else
2949 ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2950 "printer-state-reasons", p->num_reasons, NULL,
2951 (const char * const *)p->reasons);
2952 }
2953
2954
2955 /*
2956 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2957 * the specified printer or class.
2958 */
2959
2960 static void
2961 add_queued_job_count(
2962 cupsd_client_t *con, /* I - Client connection */
2963 cupsd_printer_t *p) /* I - Printer or class */
2964 {
2965 int count; /* Number of jobs on destination */
2966
2967
2968 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])",
2969 con, con->http.fd, p, p->name);
2970
2971 count = cupsdGetPrinterJobCount(p->name);
2972
2973 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2974 "queued-job-count", count);
2975 }
2976
2977
2978 #ifdef __APPLE__
2979 /*
2980 * 'apple_init_profile()' - Initialize a color profile.
2981 */
2982
2983 static void
2984 apple_init_profile(
2985 ppd_file_t *ppd, /* I - PPD file */
2986 cups_array_t *languages, /* I - Languages in the PPD file */
2987 CMDeviceProfileInfo *profile, /* I - Profile record */
2988 unsigned id, /* I - Profile ID */
2989 const char *name, /* I - Profile name */
2990 const char *text, /* I - Profile UI text */
2991 const char *iccfile) /* I - ICC filename */
2992 {
2993 char url[1024]; /* URL for profile filename */
2994 CFMutableDictionaryRef dict; /* Dictionary for name */
2995 char *language; /* Current language */
2996 ppd_attr_t *attr; /* Profile attribute */
2997 CFStringRef cflang, /* Language string */
2998 cftext; /* Localized text */
2999
3000
3001 /*
3002 * Build the profile name dictionary...
3003 */
3004
3005 dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
3006 &kCFTypeDictionaryKeyCallBacks,
3007 &kCFTypeDictionaryValueCallBacks);
3008
3009 cftext = CFStringCreateWithCString(kCFAllocatorDefault, text,
3010 kCFStringEncodingUTF8);
3011
3012 if (cftext)
3013 {
3014 CFDictionarySetValue(dict, CFSTR("en"), cftext);
3015 CFRelease(cftext);
3016 }
3017
3018 if (languages)
3019 {
3020 /*
3021 * Find localized names for the color profiles...
3022 */
3023
3024 cupsArraySave(ppd->sorted_attrs);
3025
3026 for (language = (char *)cupsArrayFirst(languages);
3027 language;
3028 language = (char *)cupsArrayNext(languages))
3029 {
3030 if (iccfile)
3031 {
3032 if ((attr = _ppdLocalizedAttr(ppd, "cupsICCProfile", name,
3033 language)) == NULL)
3034 attr = _ppdLocalizedAttr(ppd, "APTiogaProfile", name, language);
3035 }
3036 else
3037 attr = _ppdLocalizedAttr(ppd, "ColorModel", name, language);
3038
3039 if (attr && attr->text[0])
3040 {
3041 cflang = CFStringCreateWithCString(kCFAllocatorDefault, language,
3042 kCFStringEncodingUTF8);
3043 cftext = CFStringCreateWithCString(kCFAllocatorDefault, attr->text,
3044 kCFStringEncodingUTF8);
3045
3046 if (cflang && cftext)
3047 CFDictionarySetValue(dict, cflang, cftext);
3048
3049 if (cflang)
3050 CFRelease(cflang);
3051
3052 if (cftext)
3053 CFRelease(cftext);
3054 }
3055 }
3056
3057 cupsArrayRestore(ppd->sorted_attrs);
3058 }
3059
3060 /*
3061 * Fill in the profile data...
3062 */
3063
3064 if (iccfile)
3065 httpAssembleURI(HTTP_URI_CODING_ALL, url, sizeof(url), "file", NULL, "", 0,
3066 iccfile);
3067
3068 profile->dataVersion = cmDeviceProfileInfoVersion1;
3069 profile->profileID = id;
3070 profile->profileLoc.locType = iccfile ? cmPathBasedProfile : cmNoProfileBase;
3071 profile->profileName = dict;
3072
3073 if (iccfile)
3074 strlcpy(profile->profileLoc.u.pathLoc.path, iccfile,
3075 sizeof(profile->profileLoc.u.pathLoc.path));
3076 }
3077
3078
3079 /*
3080 * 'apple_register_profiles()' - Register color profiles for a printer.
3081 */
3082
3083 static void
3084 apple_register_profiles(
3085 cupsd_printer_t *p) /* I - Printer */
3086 {
3087 int i; /* Looping var */
3088 char ppdfile[1024], /* PPD filename */
3089 iccfile[1024], /* ICC filename */
3090 selector[PPD_MAX_NAME];
3091 /* Profile selection string */
3092 ppd_file_t *ppd; /* PPD file */
3093 ppd_attr_t *attr, /* Profile attributes */
3094 *profileid_attr,/* cupsProfileID attribute */
3095 *q1_attr, /* ColorModel (or other) qualifier */
3096 *q2_attr, /* MediaType (or other) qualifier */
3097 *q3_attr; /* Resolution (or other) qualifier */
3098 char q_keyword[PPD_MAX_NAME];
3099 /* Qualifier keyword */
3100 const char *q1_choice, /* ColorModel (or other) choice */
3101 *q2_choice, /* MediaType (or other) choice */
3102 *q3_choice; /* Resolution (or other) choice */
3103 const char *profile_key; /* Profile keyword */
3104 ppd_option_t *cm_option; /* Color model option */
3105 ppd_choice_t *cm_choice; /* Color model choice */
3106 int num_profiles; /* Number of profiles */
3107 CMError error; /* Last error */
3108 unsigned device_id, /* Printer device ID */
3109 profile_id, /* Profile ID */
3110 default_profile_id = 0;
3111 /* Default profile ID */
3112 CFMutableDictionaryRef device_name; /* Printer device name dictionary */
3113 CFStringRef printer_name; /* Printer name string */
3114 CMDeviceScope scope = /* Scope of the registration */
3115 {
3116 kCFPreferencesAnyUser,
3117 kCFPreferencesCurrentHost
3118 };
3119 CMDeviceProfileArrayPtr profiles; /* Profiles */
3120 CMDeviceProfileInfo *profile; /* Current profile */
3121 cups_array_t *languages; /* Languages array */
3122
3123
3124 /*
3125 * Make sure ColorSync is available...
3126 */
3127
3128 if (CMRegisterColorDevice == NULL)
3129 return;
3130
3131 /*
3132 * Try opening the PPD file for this printer...
3133 */
3134
3135 snprintf(ppdfile, sizeof(ppdfile), "%s/ppd/%s.ppd", ServerRoot, p->name);
3136 if ((ppd = ppdOpenFile(ppdfile)) == NULL)
3137 return;
3138
3139 /*
3140 * See if we have any profiles...
3141 */
3142
3143 if ((attr = ppdFindAttr(ppd, "APTiogaProfile", NULL)) != NULL)
3144 profile_key = "APTiogaProfile";
3145 else
3146 {
3147 attr = ppdFindAttr(ppd, "cupsICCProfile", NULL);
3148 profile_key = "cupsICCProfile";
3149 }
3150
3151 for (num_profiles = 0; attr; attr = ppdFindNextAttr(ppd, profile_key, NULL))
3152 if (attr->spec[0] && attr->value && attr->value[0])
3153 {
3154 if (attr->value[0] != '/')
3155 snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir,
3156 attr->value);
3157 else
3158 strlcpy(iccfile, attr->value, sizeof(iccfile));
3159
3160 if (access(iccfile, 0))
3161 continue;
3162
3163 num_profiles ++;
3164 }
3165
3166
3167 /*
3168 * If we have profiles, add them...
3169 */
3170
3171 if (num_profiles > 0)
3172 {
3173 if (profile_key[0] == 'A')
3174 {
3175 /*
3176 * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile
3177 * attribute...
3178 */
3179
3180 if ((attr = ppdFindAttr(ppd, "DefaultAPTiogaProfile", NULL)) != NULL &&
3181 attr->value)
3182 default_profile_id = atoi(attr->value);
3183
3184 q1_choice = q2_choice = q3_choice = NULL;
3185 }
3186 else
3187 {
3188 /*
3189 * For CUPS PPDs, figure out the default profile selector values...
3190 */
3191
3192 if ((attr = ppdFindAttr(ppd, "cupsICCQualifier1", NULL)) != NULL &&
3193 attr->value && attr->value[0])
3194 {
3195 snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value);
3196 q1_attr = ppdFindAttr(ppd, q_keyword, NULL);
3197 }
3198 else if ((q1_attr = ppdFindAttr(ppd, "DefaultColorModel", NULL)) == NULL)
3199 q1_attr = ppdFindAttr(ppd, "DefaultColorSpace", NULL);
3200
3201 if (q1_attr && q1_attr->value && q1_attr->value[0])
3202 q1_choice = q1_attr->value;
3203 else
3204 q1_choice = "";
3205
3206 if ((attr = ppdFindAttr(ppd, "cupsICCQualifier2", NULL)) != NULL &&
3207 attr->value && attr->value[0])
3208 {
3209 snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value);
3210 q2_attr = ppdFindAttr(ppd, q_keyword, NULL);
3211 }
3212 else
3213 q2_attr = ppdFindAttr(ppd, "DefaultMediaType", NULL);
3214
3215 if (q2_attr && q2_attr->value && q2_attr->value[0])
3216 q2_choice = q2_attr->value;
3217 else
3218 q2_choice = NULL;
3219
3220 if ((attr = ppdFindAttr(ppd, "cupsICCQualifier3", NULL)) != NULL &&
3221 attr->value && attr->value[0])
3222 {
3223 snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value);
3224 q3_attr = ppdFindAttr(ppd, q_keyword, NULL);
3225 }
3226 else
3227 q3_attr = ppdFindAttr(ppd, "DefaultResolution", NULL);
3228
3229 if (q3_attr && q3_attr->value && q3_attr->value[0])
3230 q3_choice = q3_attr->value;
3231 else
3232 q3_choice = NULL;
3233 }
3234
3235 /*
3236 * Build the array of profiles...
3237 *
3238 * Note: This calloc actually requests slightly more memory than needed.
3239 */
3240
3241 if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL)
3242 {
3243 cupsdLogMessage(CUPSD_LOG_ERROR,
3244 "Unable to allocate memory for %d profiles!",
3245 num_profiles);
3246 ppdClose(ppd);
3247 return;
3248 }
3249
3250 profiles->profileCount = num_profiles;
3251 languages = _ppdGetLanguages(ppd);
3252
3253 for (profile = profiles->profiles,
3254 attr = ppdFindAttr(ppd, profile_key, NULL);
3255 attr;
3256 attr = ppdFindNextAttr(ppd, profile_key, NULL))
3257 if (attr->spec[0] && attr->value && attr->value[0])
3258 {
3259 /*
3260 * Add this profile...
3261 */
3262
3263 if (attr->value[0] != '/')
3264 snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir,
3265 attr->value);
3266 else
3267 strlcpy(iccfile, attr->value, sizeof(iccfile));
3268
3269 if (access(iccfile, 0))
3270 continue;
3271
3272 if (profile_key[0] == 'c')
3273 {
3274 cupsArraySave(ppd->sorted_attrs);
3275
3276 if ((profileid_attr = ppdFindAttr(ppd, "cupsProfileID",
3277 attr->spec)) != NULL &&
3278 profileid_attr->value && isdigit(profileid_attr->value[0] & 255))
3279 profile_id = (unsigned)strtoul(profileid_attr->value, NULL, 10);
3280 else
3281 profile_id = _ppdHashName(attr->spec);
3282
3283 cupsArrayRestore(ppd->sorted_attrs);
3284 }
3285 else
3286 profile_id = atoi(attr->spec);
3287
3288 apple_init_profile(ppd, languages, profile, profile_id, attr->spec,
3289 attr->text[0] ? attr->text : attr->spec, iccfile);
3290
3291 profile ++;
3292
3293 /*
3294 * See if this is the default profile...
3295 */
3296
3297 if (!default_profile_id)
3298 {
3299 if (q2_choice)
3300 {
3301 if (q3_choice)
3302 {
3303 snprintf(selector, sizeof(selector), "%s.%s.%s",
3304 q1_choice, q2_choice, q3_choice);
3305 if (!strcmp(selector, attr->spec))
3306 default_profile_id = profile_id;
3307 }
3308
3309 if (!default_profile_id)
3310 {
3311 snprintf(selector, sizeof(selector), "%s.%s.", q1_choice,
3312 q2_choice);
3313 if (!strcmp(selector, attr->spec))
3314 default_profile_id = profile_id;
3315 }
3316 }
3317
3318 if (!default_profile_id && q3_choice)
3319 {
3320 snprintf(selector, sizeof(selector), "%s..%s", q1_choice,
3321 q3_choice);
3322 if (!strcmp(selector, attr->spec))
3323 default_profile_id = profile_id;
3324 }
3325
3326 if (!default_profile_id)
3327 {
3328 snprintf(selector, sizeof(selector), "%s..", q1_choice);
3329 if (!strcmp(selector, attr->spec))
3330 default_profile_id = profile_id;
3331 }
3332 }
3333 }
3334
3335 _ppdFreeLanguages(languages);
3336 }
3337 else if ((cm_option = ppdFindOption(ppd, "ColorModel")) != NULL)
3338 {
3339 /*
3340 * Extract profiles from ColorModel option...
3341 */
3342
3343 const char *profile_name; /* Name of generic profile */
3344
3345
3346 num_profiles = cm_option->num_choices;
3347
3348 if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL)
3349 {
3350 cupsdLogMessage(CUPSD_LOG_ERROR,
3351 "Unable to allocate memory for %d profiles!",
3352 num_profiles);
3353 ppdClose(ppd);
3354 return;
3355 }
3356
3357 profiles->profileCount = num_profiles;
3358
3359 for (profile = profiles->profiles, i = cm_option->num_choices,
3360 cm_choice = cm_option->choices;
3361 i > 0;
3362 i --, cm_choice ++, profile ++)
3363 {
3364 if (!strcmp(cm_choice->choice, "Gray") ||
3365 !strcmp(cm_choice->choice, "Black"))
3366 profile_name = "Gray";
3367 else if (!strcmp(cm_choice->choice, "RGB") ||
3368 !strcmp(cm_choice->choice, "CMY"))
3369 profile_name = "RGB";
3370 else if (!strcmp(cm_choice->choice, "CMYK") ||
3371 !strcmp(cm_choice->choice, "KCMY"))
3372 profile_name = "CMYK";
3373 else
3374 profile_name = "DeviceN";
3375
3376 snprintf(selector, sizeof(selector), "%s..", profile_name);
3377 profile_id = _ppdHashName(selector);
3378
3379 apple_init_profile(ppd, NULL, profile, profile_id, cm_choice->choice,
3380 cm_choice->text, NULL);
3381
3382 if (cm_choice->marked)
3383 default_profile_id = profile_id;
3384 }
3385 }
3386 else
3387 {
3388 /*
3389 * Use the default colorspace...
3390 */
3391
3392 num_profiles = 1 + ppd->colorspace != PPD_CS_GRAY;
3393
3394 if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL)
3395 {
3396 cupsdLogMessage(CUPSD_LOG_ERROR,
3397 "Unable to allocate memory for %d profiles!",
3398 num_profiles);
3399 ppdClose(ppd);
3400 return;
3401 }
3402
3403 profiles->profileCount = num_profiles;
3404
3405 apple_init_profile(ppd, NULL, profiles->profiles, _ppdHashName("Gray.."),
3406 "Gray", "Gray", NULL);
3407
3408 switch (ppd->colorspace)
3409 {
3410 case PPD_CS_RGB :
3411 case PPD_CS_CMY :
3412 apple_init_profile(ppd, NULL, profiles->profiles + 1,
3413 _ppdHashName("RGB.."), "RGB", "RGB", NULL);
3414 break;
3415 case PPD_CS_RGBK :
3416 case PPD_CS_CMYK :
3417 apple_init_profile(ppd, NULL, profiles->profiles + 1,
3418 _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL);
3419 break;
3420
3421 case PPD_CS_N :
3422 apple_init_profile(ppd, NULL, profiles->profiles + 1,
3423 _ppdHashName("DeviceN.."), "DeviceN", "DeviceN",
3424 NULL);
3425 break;
3426
3427 default :
3428 break;
3429 }
3430 }
3431
3432 if (num_profiles > 0)
3433 {
3434 /*
3435 * Make sure we have a default profile ID...
3436 */
3437
3438 if (!default_profile_id)
3439 default_profile_id = profiles->profiles[num_profiles - 1].profileID;
3440
3441 /*
3442 * Get the device ID hash and pathelogical name dictionary.
3443 */
3444
3445 cupsdLogMessage(CUPSD_LOG_INFO, "Registering ICC color profiles for \"%s\"",
3446 p->name);
3447
3448 device_id = _ppdHashName(p->name);
3449 device_name = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
3450 &kCFTypeDictionaryKeyCallBacks,
3451 &kCFTypeDictionaryValueCallBacks);
3452 printer_name = CFStringCreateWithCString(kCFAllocatorDefault,
3453 p->name, kCFStringEncodingUTF8);
3454
3455 if (device_name && printer_name)
3456 {
3457 CFDictionarySetValue(device_name, CFSTR("en"), printer_name);
3458
3459 /*
3460 * Register the device with ColorSync...
3461 */
3462
3463 error = CMRegisterColorDevice(cmPrinterDeviceClass, device_id,
3464 device_name, &scope);
3465
3466 /*
3467 * Register the profiles...
3468 */
3469
3470 if (error == noErr)
3471 error = CMSetDeviceFactoryProfiles(cmPrinterDeviceClass, device_id,
3472 default_profile_id, profiles);
3473 }
3474 else
3475 error = 1000;
3476
3477 /*
3478 * Clean up...
3479 */
3480
3481 if (error != noErr)
3482 cupsdLogMessage(CUPSD_LOG_ERROR,
3483 "Unable to register ICC color profiles for \"%s\" - %d",
3484 p->name, (int)error);
3485
3486 for (profile = profiles->profiles;
3487 num_profiles > 0;
3488 profile ++, num_profiles --)
3489 CFRelease(profile->profileName);
3490
3491 free(profiles);
3492
3493 if (printer_name)
3494 CFRelease(printer_name);
3495
3496 if (device_name)
3497 CFRelease(device_name);
3498 }
3499
3500 ppdClose(ppd);
3501 }
3502
3503
3504 /*
3505 * 'apple_unregister_profiles()' - Remove color profiles for the specified
3506 * printer.
3507 */
3508
3509 static void
3510 apple_unregister_profiles(
3511 cupsd_printer_t *p) /* I - Printer */
3512 {
3513 /*
3514 * Make sure ColorSync is available...
3515 */
3516
3517 if (CMUnregisterColorDevice != NULL)
3518 CMUnregisterColorDevice(cmPrinterDeviceClass, _ppdHashName(p->name));
3519 }
3520 #endif /* __APPLE__ */
3521
3522 /*
3523 * 'apply_printer_defaults()' - Apply printer default options to a job.
3524 */
3525
3526 static void
3527 apply_printer_defaults(
3528 cupsd_printer_t *printer, /* I - Printer */
3529 cupsd_job_t *job) /* I - Job */
3530 {
3531 int i, /* Looping var */
3532 num_options; /* Number of default options */
3533 cups_option_t *options, /* Default options */
3534 *option; /* Current option */
3535
3536
3537 /*
3538 * Collect all of the default options and add the missing ones to the
3539 * job object...
3540 */
3541
3542 for (i = printer->num_options, num_options = 0, options = NULL,
3543 option = printer->options;
3544 i > 0;
3545 i --, option ++)
3546 if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO))
3547 {
3548 num_options = cupsAddOption(option->name, option->value, num_options,
3549 &options);
3550 }
3551
3552 /*
3553 * Encode these options as attributes in the job object...
3554 */
3555
3556 cupsEncodeOptions2(job->attrs, num_options, options, IPP_TAG_JOB);
3557 cupsFreeOptions(num_options, options);
3558 }
3559
3560
3561 /*
3562 * 'authenticate_job()' - Set job authentication info.
3563 */
3564
3565 static void
3566 authenticate_job(cupsd_client_t *con, /* I - Client connection */
3567 ipp_attribute_t *uri) /* I - Job URI */
3568 {
3569 ipp_attribute_t *attr, /* job-id attribute */
3570 *auth_info; /* auth-info attribute */
3571 int jobid; /* Job ID */
3572 cupsd_job_t *job; /* Current job */
3573 char method[HTTP_MAX_URI],
3574 /* Method portion of URI */
3575 username[HTTP_MAX_URI],
3576 /* Username portion of URI */
3577 host[HTTP_MAX_URI],
3578 /* Host portion of URI */
3579 resource[HTTP_MAX_URI];
3580 /* Resource portion of URI */
3581 int port; /* Port portion of URI */
3582
3583
3584 cupsdLogMessage(CUPSD_LOG_DEBUG2, "authenticate_job(%p[%d], %s)",
3585 con, con->http.fd, uri->values[0].string.text);
3586
3587 /*
3588 * Start with "everything is OK" status...
3589 */
3590
3591 con->response->request.status.status_code = IPP_OK;
3592
3593 /*
3594 * See if we have a job URI or a printer URI...
3595 */
3596
3597 if (!strcmp(uri->name, "printer-uri"))
3598 {
3599 /*
3600 * Got a printer URI; see if we also have a job-id attribute...
3601 */
3602
3603 if ((attr = ippFindAttribute(con->request, "job-id",
3604 IPP_TAG_INTEGER)) == NULL)
3605 {
3606 send_ipp_status(con, IPP_BAD_REQUEST,
3607 _("Got a printer-uri attribute but no job-id!"));
3608 return;
3609 }
3610
3611 jobid = attr->values[0].integer;
3612 }
3613 else
3614 {
3615 /*
3616 * Got a job URI; parse it to get the job ID...
3617 */
3618
3619 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
3620 sizeof(method), username, sizeof(username), host,
3621 sizeof(host), &port, resource, sizeof(resource));
3622
3623 if (strncmp(resource, "/jobs/", 6))
3624 {
3625 /*
3626 * Not a valid URI!
3627 */
3628
3629 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri attribute \"%s\"!"),
3630 uri->values[0].string.text);
3631 return;
3632 }
3633
3634 jobid = atoi(resource + 6);
3635 }
3636
3637 /*
3638 * See if the job exists...
3639 */
3640
3641 if ((job = cupsdFindJob(jobid)) == NULL)
3642 {
3643 /*
3644 * Nope - return a "not found" error...
3645 */
3646
3647 send_ipp_status(con, IPP_NOT_FOUND,
3648 _("Job #%d does not exist!"), jobid);
3649 return;
3650 }
3651
3652 /*
3653 * See if the job has been completed...
3654 */
3655
3656 if (job->state_value != IPP_JOB_HELD)
3657 {
3658 /*
3659 * Return a "not-possible" error...
3660 */
3661
3662 send_ipp_status(con, IPP_NOT_POSSIBLE,
3663 _("Job #%d is not held for authentication!"),
3664 jobid);
3665 return;
3666 }
3667
3668 /*
3669 * See if we have already authenticated...
3670 */
3671
3672 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
3673
3674 if (!con->username[0] && !auth_info)
3675 {
3676 cupsd_printer_t *printer; /* Job destination */
3677
3678
3679 /*
3680 * No auth data. If we need to authenticate via Kerberos, send a
3681 * HTTP auth challenge, otherwise just return an IPP error...
3682 */
3683
3684 printer = cupsdFindDest(job->dest);
3685
3686 if (printer && printer->num_auth_info_required > 0 &&
3687 !strcmp(printer->auth_info_required[0], "negotiate"))
3688 send_http_error(con, HTTP_UNAUTHORIZED, printer);
3689 else
3690 send_ipp_status(con, IPP_NOT_AUTHORIZED,
3691 _("No authentication information provided!"));
3692 return;
3693 }
3694
3695 /*
3696 * See if the job is owned by the requesting user...
3697 */
3698
3699 if (!validate_user(job, con, job->username, username, sizeof(username)))
3700 {
3701 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
3702 return;
3703 }
3704
3705 /*
3706 * Save the authentication information for this job...
3707 */
3708
3709 save_auth_info(con, job, auth_info);
3710
3711 /*
3712 * Reset the job-hold-until value to "no-hold"...
3713 */
3714
3715 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
3716 IPP_TAG_KEYWORD)) == NULL)
3717 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
3718
3719 if (attr)
3720 {
3721 attr->value_tag = IPP_TAG_KEYWORD;
3722 cupsdSetString(&(attr->values[0].string.text), "no-hold");
3723 }
3724
3725 /*
3726 * Release the job and return...
3727 */
3728
3729 cupsdReleaseJob(job);
3730
3731 cupsdLogJob(job, CUPSD_LOG_INFO, "Authenticated by \"%s\".", con->username);
3732 }
3733
3734
3735 /*
3736 * 'cancel_all_jobs()' - Cancel all print jobs.
3737 */
3738
3739 static void
3740 cancel_all_jobs(cupsd_client_t *con, /* I - Client connection */
3741 ipp_attribute_t *uri) /* I - Job or Printer URI */
3742 {
3743 http_status_t status; /* Policy status */
3744 cups_ptype_t dtype; /* Destination type */
3745 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
3746 userpass[HTTP_MAX_URI], /* Username portion of URI */
3747 hostname[HTTP_MAX_URI], /* Host portion of URI */
3748 resource[HTTP_MAX_URI]; /* Resource portion of URI */
3749 int port; /* Port portion of URI */
3750 ipp_attribute_t *attr; /* Attribute in request */
3751 const char *username; /* Username */
3752 int purge; /* Purge? */
3753 cupsd_printer_t *printer; /* Printer */
3754
3755
3756 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_all_jobs(%p[%d], %s)", con,
3757 con->http.fd, uri->values[0].string.text);
3758
3759 /*
3760 * See if we have a printer URI...
3761 */
3762
3763 if (strcmp(uri->name, "printer-uri"))
3764 {
3765 send_ipp_status(con, IPP_BAD_REQUEST,
3766 _("The printer-uri attribute is required!"));
3767 return;
3768 }
3769
3770 /*
3771 * Get the username (if any) for the jobs we want to cancel (only if
3772 * "my-jobs" is specified...
3773 */
3774
3775 if ((attr = ippFindAttribute(con->request, "my-jobs",
3776 IPP_TAG_BOOLEAN)) != NULL &&
3777 attr->values[0].boolean)
3778 {
3779 if ((attr = ippFindAttribute(con->request, "requesting-user-name",
3780 IPP_TAG_NAME)) != NULL)
3781 username = attr->values[0].string.text;
3782 else
3783 {
3784 send_ipp_status(con, IPP_BAD_REQUEST,
3785 _("Missing requesting-user-name attribute!"));
3786 return;
3787 }
3788 }
3789 else
3790 username = NULL;
3791
3792 /*
3793 * Look for the "purge-jobs" attribute...
3794 */
3795
3796 if ((attr = ippFindAttribute(con->request, "purge-jobs",
3797 IPP_TAG_BOOLEAN)) != NULL)
3798 purge = attr->values[0].boolean;
3799 else
3800 purge = 1;
3801
3802 /*
3803 * And if the destination is valid...
3804 */
3805
3806 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
3807 {
3808 /*
3809 * Bad URI?
3810 */
3811
3812 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text,
3813 scheme, sizeof(scheme), userpass, sizeof(userpass),
3814 hostname, sizeof(hostname), &port,
3815 resource, sizeof(resource));
3816
3817 if ((!strncmp(resource, "/printers/", 10) && resource[10]) ||
3818 (!strncmp(resource, "/classes/", 9) && resource[9]))
3819 {
3820 send_ipp_status(con, IPP_NOT_FOUND,
3821 _("The printer or class was not found."));
3822 return;
3823 }
3824
3825 /*
3826 * Check policy...
3827 */
3828
3829 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
3830 {
3831 send_http_error(con, status, NULL);
3832 return;
3833 }
3834
3835 /*
3836 * Cancel all jobs on all printers...
3837 */
3838
3839 cupsdCancelJobs(NULL, username, purge);
3840
3841 cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".",
3842 purge ? "purged" : "canceled", get_username(con));
3843 }
3844 else
3845 {
3846 /*
3847 * Check policy...
3848 */
3849
3850 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
3851 NULL)) != HTTP_OK)
3852 {
3853 send_http_error(con, status, printer);
3854 return;
3855 }
3856
3857 /*
3858 * Cancel all of the jobs on the named printer...
3859 */
3860
3861 cupsdCancelJobs(printer->name, username, purge);
3862
3863 cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".",
3864 printer->name, purge ? "purged" : "canceled",
3865 get_username(con));
3866 }
3867
3868 con->response->request.status.status_code = IPP_OK;
3869 }
3870
3871
3872 /*
3873 * 'cancel_job()' - Cancel a print job.
3874 */
3875
3876 static void
3877 cancel_job(cupsd_client_t *con, /* I - Client connection */
3878 ipp_attribute_t *uri) /* I - Job or Printer URI */
3879 {
3880 ipp_attribute_t *attr; /* Current attribute */
3881 int jobid; /* Job ID */
3882 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
3883 username[HTTP_MAX_URI], /* Username portion of URI */
3884 host[HTTP_MAX_URI], /* Host portion of URI */
3885 resource[HTTP_MAX_URI]; /* Resource portion of URI */
3886 int port; /* Port portion of URI */
3887 cupsd_job_t *job; /* Job information */
3888 cups_ptype_t dtype; /* Destination type (printer/class) */
3889 cupsd_printer_t *printer; /* Printer data */
3890 int purge; /* Purge the job? */
3891
3892
3893 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con,
3894 con->http.fd, uri->values[0].string.text);
3895
3896 /*
3897 * See if we have a job URI or a printer URI...
3898 */
3899
3900 if (!strcmp(uri->name, "printer-uri"))
3901 {
3902 /*
3903 * Got a printer URI; see if we also have a job-id attribute...
3904 */
3905
3906 if ((attr = ippFindAttribute(con->request, "job-id",
3907 IPP_TAG_INTEGER)) == NULL)
3908 {
3909 send_ipp_status(con, IPP_BAD_REQUEST,
3910 _("Got a printer-uri attribute but no job-id!"));
3911 return;
3912 }
3913
3914 if ((jobid = attr->values[0].integer) == 0)
3915 {
3916 /*
3917 * Find the current job on the specified printer...
3918 */
3919
3920 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
3921 {
3922 /*
3923 * Bad URI...
3924 */
3925
3926 send_ipp_status(con, IPP_NOT_FOUND,
3927 _("The printer or class was not found."));
3928 return;
3929 }
3930
3931 /*
3932 * See if the printer is currently printing a job...
3933 */
3934
3935 if (printer->job)
3936 jobid = ((cupsd_job_t *)printer->job)->id;
3937 else
3938 {
3939 /*
3940 * No, see if there are any pending jobs...
3941 */
3942
3943 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
3944 job;
3945 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
3946 if (job->state_value <= IPP_JOB_PROCESSING &&
3947 !strcasecmp(job->dest, printer->name))
3948 break;
3949
3950 if (job)
3951 jobid = job->id;
3952 else
3953 {
3954 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
3955 job;
3956 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
3957 if (job->state_value == IPP_JOB_STOPPED &&
3958 !strcasecmp(job->dest, printer->name))
3959 break;
3960
3961 if (job)
3962 jobid = job->id;
3963 else
3964 {
3965 send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s!"),
3966 printer->name);
3967 return;
3968 }
3969 }
3970 }
3971 }
3972 }
3973 else
3974 {
3975 /*
3976 * Got a job URI; parse it to get the job ID...
3977 */
3978
3979 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
3980 sizeof(scheme), username, sizeof(username), host,
3981 sizeof(host), &port, resource, sizeof(resource));
3982
3983 if (strncmp(resource, "/jobs/", 6))
3984 {
3985 /*
3986 * Not a valid URI!
3987 */
3988
3989 send_ipp_status(con, IPP_BAD_REQUEST,
3990 _("Bad job-uri attribute \"%s\"!"),
3991 uri->values[0].string.text);
3992 return;
3993 }
3994
3995 jobid = atoi(resource + 6);
3996 }
3997
3998 /*
3999 * Look for the "purge-job" attribute...
4000 */
4001
4002 if ((attr = ippFindAttribute(con->request, "purge-job",
4003 IPP_TAG_BOOLEAN)) != NULL)
4004 purge = attr->values[0].boolean;
4005 else
4006 purge = 0;
4007
4008 /*
4009 * See if the job exists...
4010 */
4011
4012 if ((job = cupsdFindJob(jobid)) == NULL)
4013 {
4014 /*
4015 * Nope - return a "not found" error...
4016 */
4017
4018 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
4019 return;
4020 }
4021
4022 /*
4023 * See if the job is owned by the requesting user...
4024 */
4025
4026 if (!validate_user(job, con, job->username, username, sizeof(username)))
4027 {
4028 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
4029 return;
4030 }
4031
4032 /*
4033 * See if the job is already completed, canceled, or aborted; if so,
4034 * we can't cancel...
4035 */
4036
4037 if (job->state_value >= IPP_JOB_CANCELED && !purge)
4038 {
4039 switch (job->state_value)
4040 {
4041 case IPP_JOB_CANCELED :
4042 send_ipp_status(con, IPP_NOT_POSSIBLE,
4043 _("Job #%d is already canceled - can\'t cancel."),
4044 jobid);
4045 break;
4046
4047 case IPP_JOB_ABORTED :
4048 send_ipp_status(con, IPP_NOT_POSSIBLE,
4049 _("Job #%d is already aborted - can\'t cancel."),
4050 jobid);
4051 break;
4052
4053 default :
4054 send_ipp_status(con, IPP_NOT_POSSIBLE,
4055 _("Job #%d is already completed - can\'t cancel."),
4056 jobid);
4057 break;
4058 }
4059
4060 return;
4061 }
4062
4063 /*
4064 * Cancel the job and return...
4065 */
4066
4067 cupsdCancelJob(job, purge, IPP_JOB_CANCELED);
4068 cupsdCheckJobs();
4069
4070 if (purge)
4071 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Purged by \"%s\".", jobid,
4072 username);
4073 else
4074 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Canceled by \"%s\".", jobid,
4075 username);
4076
4077 con->response->request.status.status_code = IPP_OK;
4078 }
4079
4080
4081 /*
4082 * 'cancel_subscription()' - Cancel a subscription.
4083 */
4084
4085 static void
4086 cancel_subscription(
4087 cupsd_client_t *con, /* I - Client connection */
4088 int sub_id) /* I - Subscription ID */
4089 {
4090 http_status_t status; /* Policy status */
4091 cupsd_subscription_t *sub; /* Subscription */
4092
4093
4094 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4095 "cancel_subscription(con=%p[%d], sub_id=%d)",
4096 con, con->http.fd, sub_id);
4097
4098 /*
4099 * Is the subscription ID valid?
4100 */
4101
4102 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
4103 {
4104 /*
4105 * Bad subscription ID...
4106 */
4107
4108 send_ipp_status(con, IPP_NOT_FOUND,
4109 _("notify-subscription-id %d no good!"), sub_id);
4110 return;
4111 }
4112
4113 /*
4114 * Check policy...
4115 */
4116
4117 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
4118 DefaultPolicyPtr,
4119 con, sub->owner)) != HTTP_OK)
4120 {
4121 send_http_error(con, status, sub->dest);
4122 return;
4123 }
4124
4125 /*
4126 * Cancel the subscription...
4127 */
4128
4129 cupsdDeleteSubscription(sub, 1);
4130
4131 con->response->request.status.status_code = IPP_OK;
4132 }
4133
4134
4135 /*
4136 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
4137 */
4138
4139 static int /* O - 1 if OK, 0 if not */
4140 check_rss_recipient(
4141 const char *recipient) /* I - Recipient URI */
4142 {
4143 cupsd_subscription_t *sub; /* Current subscription */
4144
4145
4146 for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions);
4147 sub;
4148 sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
4149 if (sub->recipient)
4150 {
4151 /*
4152 * Compare the URIs up to the first ?...
4153 */
4154
4155 const char *r1, *r2;
4156
4157 for (r1 = recipient, r2 = sub->recipient;
4158 *r1 == *r2 && *r1 && *r1 != '?' && *r2 && *r2 != '?';
4159 r1 ++, r2 ++);
4160
4161 if (*r1 == *r2)
4162 return (0);
4163 }
4164
4165 return (1);
4166 }
4167
4168
4169 /*
4170 * 'check_quotas()' - Check quotas for a printer and user.
4171 */
4172
4173 static int /* O - 1 if OK, 0 if not */
4174 check_quotas(cupsd_client_t *con, /* I - Client connection */
4175 cupsd_printer_t *p) /* I - Printer or class */
4176 {
4177 int i; /* Looping var */
4178 char username[33]; /* Username */
4179 cupsd_quota_t *q; /* Quota data */
4180 #ifdef HAVE_MBR_UID_TO_UUID
4181 /*
4182 * Use Apple membership APIs which require that all names represent
4183 * valid user account or group records accessible by the server.
4184 */
4185
4186 uuid_t usr_uuid; /* UUID for job requesting user */
4187 uuid_t usr2_uuid; /* UUID for ACL user name entry */
4188 uuid_t grp_uuid; /* UUID for ACL group name entry */
4189 int mbr_err; /* Error from membership function */
4190 int is_member; /* Is this user a member? */
4191 #else
4192 /*
4193 * Use standard POSIX APIs for checking users and groups...
4194 */
4195
4196 struct passwd *pw; /* User password data */
4197 #endif /* HAVE_MBR_UID_TO_UUID */
4198
4199
4200 cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])",
4201 con, con->http.fd, p, p->name);
4202
4203 /*
4204 * Figure out who is printing...
4205 */
4206
4207 strlcpy(username, get_username(con), sizeof(username));
4208
4209 /*
4210 * Check global active job limits for printers and users...
4211 */
4212
4213 if (MaxJobsPerPrinter)
4214 {
4215 /*
4216 * Check if there are too many pending jobs on this printer...
4217 */
4218
4219 if (cupsdGetPrinterJobCount(p->name) >= MaxJobsPerPrinter)
4220 {
4221 cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for printer \"%s\"...",
4222 p->name);
4223 return (0);
4224 }
4225 }
4226
4227 if (MaxJobsPerUser)
4228 {
4229 /*
4230 * Check if there are too many pending jobs for this user...
4231 */
4232
4233 if (cupsdGetUserJobCount(username) >= MaxJobsPerUser)
4234 {
4235 cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for user \"%s\"...",
4236 username);
4237 return (0);
4238 }
4239 }
4240
4241 /*
4242 * Check against users...
4243 */
4244
4245 if (p->num_users == 0 && p->k_limit == 0 && p->page_limit == 0)
4246 return (1);
4247
4248 if (p->num_users)
4249 {
4250 #ifdef HAVE_MBR_UID_TO_UUID
4251 /*
4252 * Get UUID for job requesting user...
4253 */
4254
4255 if (mbr_user_name_to_uuid((char *)username, usr_uuid))
4256 {
4257 /*
4258 * Unknown user...
4259 */
4260
4261 cupsdLogMessage(CUPSD_LOG_DEBUG,
4262 "check_quotas: UUID lookup failed for user \"%s\"",
4263 username);
4264 cupsdLogMessage(CUPSD_LOG_INFO,
4265 "Denying user \"%s\" access to printer \"%s\" "
4266 "(unknown user)...",
4267 username, p->name);
4268 return (0);
4269 }
4270 #else
4271 /*
4272 * Get UID and GID of requesting user...
4273 */
4274
4275 pw = getpwnam(username);
4276 endpwent();
4277 #endif /* HAVE_MBR_UID_TO_UUID */
4278
4279 for (i = 0; i < p->num_users; i ++)
4280 if (p->users[i][0] == '@')
4281 {
4282 /*
4283 * Check group membership...
4284 */
4285
4286 #ifdef HAVE_MBR_UID_TO_UUID
4287 if (p->users[i][1] == '#')
4288 {
4289 if (uuid_parse((char *)p->users[i] + 2, grp_uuid))
4290 uuid_clear(grp_uuid);
4291 }
4292 else if ((mbr_err = mbr_group_name_to_uuid((char *)p->users[i] + 1,
4293 grp_uuid)) != 0)
4294 {
4295 /*
4296 * Invalid ACL entries are ignored for matching; just record a
4297 * warning in the log...
4298 */
4299
4300 cupsdLogMessage(CUPSD_LOG_DEBUG,
4301 "check_quotas: UUID lookup failed for ACL entry "
4302 "\"%s\" (err=%d)", p->users[i], mbr_err);
4303 cupsdLogMessage(CUPSD_LOG_WARN,
4304 "Access control entry \"%s\" not a valid group name; "
4305 "entry ignored", p->users[i]);
4306 }
4307
4308 if ((mbr_err = mbr_check_membership(usr_uuid, grp_uuid,
4309 &is_member)) != 0)
4310 {
4311 /*
4312 * At this point, there should be no errors, but check anyways...
4313 */
4314
4315 cupsdLogMessage(CUPSD_LOG_DEBUG,
4316 "check_quotas: group \"%s\" membership check "
4317 "failed (err=%d)", p->users[i] + 1, mbr_err);
4318 is_member = 0;
4319 }
4320
4321 /*
4322 * Stop if we found a match...
4323 */
4324
4325 if (is_member)
4326 break;
4327
4328 #else
4329 if (cupsdCheckGroup(username, pw, p->users[i] + 1))
4330 break;
4331 #endif /* HAVE_MBR_UID_TO_UUID */
4332 }
4333 #ifdef HAVE_MBR_UID_TO_UUID
4334 else
4335 {
4336 if (p->users[i][0] == '#')
4337 {
4338 if (uuid_parse((char *)p->users[i] + 1, usr2_uuid))
4339 uuid_clear(usr2_uuid);
4340 }
4341 else if ((mbr_err = mbr_user_name_to_uuid((char *)p->users[i],
4342 usr2_uuid)) != 0)
4343 {
4344 /*
4345 * Invalid ACL entries are ignored for matching; just record a
4346 * warning in the log...
4347 */
4348
4349 cupsdLogMessage(CUPSD_LOG_DEBUG,
4350 "check_quotas: UUID lookup failed for ACL entry "
4351 "\"%s\" (err=%d)", p->users[i], mbr_err);
4352 cupsdLogMessage(CUPSD_LOG_WARN,
4353 "Access control entry \"%s\" not a valid user name; "
4354 "entry ignored", p->users[i]);
4355 }
4356
4357 if ((mbr_err = mbr_check_membership(usr_uuid, usr2_uuid,
4358 &is_member)) != 0)
4359 {
4360 cupsdLogMessage(CUPSD_LOG_DEBUG,
4361 "check_quotas: User \"%s\" identity check failed "
4362 "(err=%d)", p->users[i], mbr_err);
4363 is_member = 0;
4364 }
4365
4366 if (is_member)
4367 break;
4368 }
4369 #else
4370 else if (!strcasecmp(username, p->users[i]))
4371 break;
4372 #endif /* HAVE_MBR_UID_TO_UUID */
4373
4374 if ((i < p->num_users) == p->deny_users)
4375 {
4376 cupsdLogMessage(CUPSD_LOG_INFO,
4377 "Denying user \"%s\" access to printer \"%s\"...",
4378 username, p->name);
4379 return (0);
4380 }
4381 }
4382
4383 /*
4384 * Check quotas...
4385 */
4386
4387 #ifdef __APPLE__
4388 if (AppleQuotas && (q = cupsdFindQuota(p, username)) != NULL)
4389 {
4390 /*
4391 * TODO: Define these special page count values as constants!
4392 */
4393
4394 if (q->page_count == -4) /* special case: unlimited user */
4395 {
4396 cupsdLogMessage(CUPSD_LOG_INFO,
4397 "User \"%s\" request approved for printer %s (%s): "
4398 "unlimited quota.",
4399 username, p->name, p->info);
4400 q->page_count = 0; /* allow user to print */
4401 return (1);
4402 }
4403 else if (q->page_count == -3) /* quota exceeded */
4404 {
4405 cupsdLogMessage(CUPSD_LOG_INFO,
4406 "User \"%s\" request denied for printer %s (%s): "
4407 "quota limit exceeded.",
4408 username, p->name, p->info);
4409 q->page_count = 2; /* force quota exceeded failure */
4410 return (-1);
4411 }
4412 else if (q->page_count == -2) /* quota disabled for user */
4413 {
4414 cupsdLogMessage(CUPSD_LOG_INFO,
4415 "User \"%s\" request denied for printer %s (%s): "
4416 "printing disabled for user.",
4417 username, p->name, p->info);
4418 q->page_count = 2; /* force quota exceeded failure */
4419 return (-1);
4420 }
4421 else if (q->page_count == -1) /* quota access error */
4422 {
4423 cupsdLogMessage(CUPSD_LOG_INFO,
4424 "User \"%s\" request denied for printer %s (%s): "
4425 "unable to determine quota limit.",
4426 username, p->name, p->info);
4427 q->page_count = 2; /* force quota exceeded failure */
4428 return (-1);
4429 }
4430 else if (q->page_count < 0) /* user not found or other error */
4431 {
4432 cupsdLogMessage(CUPSD_LOG_INFO,
4433 "User \"%s\" request denied for printer %s (%s): "
4434 "user disabled / missing quota.",
4435 username, p->name, p->info);
4436 q->page_count = 2; /* force quota exceeded failure */
4437 return (-1);
4438 }
4439 else /* page within user limits */
4440 {
4441 q->page_count = 0; /* allow user to print */
4442 return (1);
4443 }
4444 }
4445 else
4446 #endif /* __APPLE__ */
4447 if (p->k_limit || p->page_limit)
4448 {
4449 if ((q = cupsdUpdateQuota(p, username, 0, 0)) == NULL)
4450 {
4451 cupsdLogMessage(CUPSD_LOG_ERROR,
4452 "Unable to allocate quota data for user \"%s\"!",
4453 username);
4454 return (-1);
4455 }
4456
4457 if ((q->k_count >= p->k_limit && p->k_limit) ||
4458 (q->page_count >= p->page_limit && p->page_limit))
4459 {
4460 cupsdLogMessage(CUPSD_LOG_INFO, "User \"%s\" is over the quota limit...",
4461 username);
4462 return (-1);
4463 }
4464 }
4465
4466 /*
4467 * If we have gotten this far, we're done!
4468 */
4469
4470 return (1);
4471 }
4472
4473
4474 /*
4475 * 'copy_attribute()' - Copy a single attribute.
4476 */
4477
4478 static ipp_attribute_t * /* O - New attribute */
4479 copy_attribute(
4480 ipp_t *to, /* O - Destination request/response */
4481 ipp_attribute_t *attr, /* I - Attribute to copy */
4482 int quickcopy) /* I - Do a quick copy? */
4483 {
4484 int i; /* Looping var */
4485 ipp_attribute_t *toattr; /* Destination attribute */
4486
4487
4488 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4489 "copy_attribute(%p, %p[%s,%x,%x])", to, attr,
4490 attr->name ? attr->name : "(null)", attr->group_tag,
4491 attr->value_tag);
4492
4493 switch (attr->value_tag & ~IPP_TAG_COPY)
4494 {
4495 case IPP_TAG_ZERO :
4496 toattr = ippAddSeparator(to);
4497 break;
4498
4499 case IPP_TAG_INTEGER :
4500 case IPP_TAG_ENUM :
4501 toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag,
4502 attr->name, attr->num_values, NULL);
4503
4504 for (i = 0; i < attr->num_values; i ++)
4505 toattr->values[i].integer = attr->values[i].integer;
4506 break;
4507
4508 case IPP_TAG_BOOLEAN :
4509 toattr = ippAddBooleans(to, attr->group_tag, attr->name,
4510 attr->num_values, NULL);
4511
4512 for (i = 0; i < attr->num_values; i ++)
4513 toattr->values[i].boolean = attr->values[i].boolean;
4514 break;
4515
4516 case IPP_TAG_STRING :
4517 case IPP_TAG_TEXT :
4518 case IPP_TAG_NAME :
4519 case IPP_TAG_KEYWORD :
4520 case IPP_TAG_URI :
4521 case IPP_TAG_URISCHEME :
4522 case IPP_TAG_CHARSET :
4523 case IPP_TAG_LANGUAGE :
4524 case IPP_TAG_MIMETYPE :
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 toattr->values[i].string.text = attr->values[i].string.text;
4533 }
4534 else
4535 {
4536 for (i = 0; i < attr->num_values; i ++)
4537 toattr->values[i].string.text =
4538 _cupsStrAlloc(attr->values[i].string.text);
4539 }
4540 break;
4541
4542 case IPP_TAG_DATE :
4543 toattr = ippAddDate(to, attr->group_tag, attr->name,
4544 attr->values[0].date);
4545 break;
4546
4547 case IPP_TAG_RESOLUTION :
4548 toattr = ippAddResolutions(to, attr->group_tag, attr->name,
4549 attr->num_values, IPP_RES_PER_INCH,
4550 NULL, NULL);
4551
4552 for (i = 0; i < attr->num_values; i ++)
4553 {
4554 toattr->values[i].resolution.xres = attr->values[i].resolution.xres;
4555 toattr->values[i].resolution.yres = attr->values[i].resolution.yres;
4556 toattr->values[i].resolution.units = attr->values[i].resolution.units;
4557 }
4558 break;
4559
4560 case IPP_TAG_RANGE :
4561 toattr = ippAddRanges(to, attr->group_tag, attr->name,
4562 attr->num_values, NULL, NULL);
4563
4564 for (i = 0; i < attr->num_values; i ++)
4565 {
4566 toattr->values[i].range.lower = attr->values[i].range.lower;
4567 toattr->values[i].range.upper = attr->values[i].range.upper;
4568 }
4569 break;
4570
4571 case IPP_TAG_TEXTLANG :
4572 case IPP_TAG_NAMELANG :
4573 toattr = ippAddStrings(to, attr->group_tag,
4574 (ipp_tag_t)(attr->value_tag | quickcopy),
4575 attr->name, attr->num_values, NULL, NULL);
4576
4577 if (quickcopy)
4578 {
4579 for (i = 0; i < attr->num_values; i ++)
4580 {
4581 toattr->values[i].string.charset = attr->values[i].string.charset;
4582 toattr->values[i].string.text = attr->values[i].string.text;
4583 }
4584 }
4585 else
4586 {
4587 for (i = 0; i < attr->num_values; i ++)
4588 {
4589 if (!i)
4590 toattr->values[i].string.charset =
4591 _cupsStrAlloc(attr->values[i].string.charset);
4592 else
4593 toattr->values[i].string.charset =
4594 toattr->values[0].string.charset;
4595
4596 toattr->values[i].string.text =
4597 _cupsStrAlloc(attr->values[i].string.text);
4598 }
4599 }
4600 break;
4601
4602 case IPP_TAG_BEGIN_COLLECTION :
4603 toattr = ippAddCollections(to, attr->group_tag, attr->name,
4604 attr->num_values, NULL);
4605
4606 for (i = 0; i < attr->num_values; i ++)
4607 {
4608 toattr->values[i].collection = ippNew();
4609 copy_attrs(toattr->values[i].collection, attr->values[i].collection,
4610 NULL, IPP_TAG_ZERO, 0);
4611 }
4612 break;
4613
4614 default :
4615 toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag,
4616 attr->name, attr->num_values, NULL);
4617
4618 for (i = 0; i < attr->num_values; i ++)
4619 {
4620 toattr->values[i].unknown.length = attr->values[i].unknown.length;
4621
4622 if (toattr->values[i].unknown.length > 0)
4623 {
4624 if ((toattr->values[i].unknown.data =
4625 malloc(toattr->values[i].unknown.length)) == NULL)
4626 toattr->values[i].unknown.length = 0;
4627 else
4628 memcpy(toattr->values[i].unknown.data,
4629 attr->values[i].unknown.data,
4630 toattr->values[i].unknown.length);
4631 }
4632 }
4633 break; /* anti-compiler-warning-code */
4634 }
4635
4636 return (toattr);
4637 }
4638
4639
4640 /*
4641 * 'copy_attrs()' - Copy attributes from one request to another.
4642 */
4643
4644 static void
4645 copy_attrs(ipp_t *to, /* I - Destination request */
4646 ipp_t *from, /* I - Source request */
4647 cups_array_t *ra, /* I - Requested attributes */
4648 ipp_tag_t group, /* I - Group to copy */
4649 int quickcopy) /* I - Do a quick copy? */
4650 {
4651 ipp_attribute_t *fromattr; /* Source attribute */
4652
4653
4654 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4655 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4656 to, from, ra, group, quickcopy);
4657
4658 if (!to || !from)
4659 return;
4660
4661 for (fromattr = from->attrs; fromattr; fromattr = fromattr->next)
4662 {
4663 /*
4664 * Filter attributes as needed...
4665 */
4666
4667 if ((group != IPP_TAG_ZERO && fromattr->group_tag != group &&
4668 fromattr->group_tag != IPP_TAG_ZERO) || !fromattr->name)
4669 continue;
4670
4671 if (!ra || cupsArrayFind(ra, fromattr->name))
4672 copy_attribute(to, fromattr, quickcopy);
4673 }
4674 }
4675
4676
4677 /*
4678 * 'copy_banner()' - Copy a banner file to the requests directory for the
4679 * specified job.
4680 */
4681
4682 static int /* O - Size of banner file in kbytes */
4683 copy_banner(cupsd_client_t *con, /* I - Client connection */
4684 cupsd_job_t *job, /* I - Job information */
4685 const char *name) /* I - Name of banner */
4686 {
4687 int i; /* Looping var */
4688 int kbytes; /* Size of banner file in kbytes */
4689 char filename[1024]; /* Job filename */
4690 cupsd_banner_t *banner; /* Pointer to banner */
4691 cups_file_t *in; /* Input file */
4692 cups_file_t *out; /* Output file */
4693 int ch; /* Character from file */
4694 char attrname[255], /* Name of attribute */
4695 *s; /* Pointer into name */
4696 ipp_attribute_t *attr; /* Attribute */
4697
4698
4699 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4700 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4701 con, con ? con->http.fd : -1, job, job->id,
4702 name ? name : "(null)");
4703
4704 /*
4705 * Find the banner; return if not found or "none"...
4706 */
4707
4708 if (!name || !strcmp(name, "none") ||
4709 (banner = cupsdFindBanner(name)) == NULL)
4710 return (0);
4711
4712 /*
4713 * Open the banner and job files...
4714 */
4715
4716 if (add_file(con, job, banner->filetype, 0))
4717 return (-1);
4718
4719 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
4720 job->num_files);
4721 if ((out = cupsFileOpen(filename, "w")) == NULL)
4722 {
4723 cupsdLogMessage(CUPSD_LOG_ERROR,
4724 "Unable to create banner job file %s - %s",
4725 filename, strerror(errno));
4726 job->num_files --;
4727 return (0);
4728 }
4729
4730 fchmod(cupsFileNumber(out), 0640);
4731 fchown(cupsFileNumber(out), RunUser, Group);
4732
4733 /*
4734 * Try the localized banner file under the subdirectory...
4735 */
4736
4737 strlcpy(attrname, job->attrs->attrs->next->values[0].string.text,
4738 sizeof(attrname));
4739 if (strlen(attrname) > 2 && attrname[2] == '-')
4740 {
4741 /*
4742 * Convert ll-cc to ll_CC...
4743 */
4744
4745 attrname[2] = '_';
4746 attrname[3] = toupper(attrname[3] & 255);
4747 attrname[4] = toupper(attrname[4] & 255);
4748 }
4749
4750 snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir,
4751 attrname, name);
4752
4753 if (access(filename, 0) && strlen(attrname) > 2)
4754 {
4755 /*
4756 * Wasn't able to find "ll_CC" locale file; try the non-national
4757 * localization banner directory.
4758 */
4759
4760 attrname[2] = '\0';
4761
4762 snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir,
4763 attrname, name);
4764 }
4765
4766 if (access(filename, 0))
4767 {
4768 /*
4769 * Use the non-localized banner file.
4770 */
4771
4772 snprintf(filename, sizeof(filename), "%s/banners/%s", DataDir, name);
4773 }
4774
4775 if ((in = cupsFileOpen(filename, "r")) == NULL)
4776 {
4777 cupsFileClose(out);
4778 unlink(filename);
4779 cupsdLogMessage(CUPSD_LOG_ERROR,
4780 "Unable to open banner template file %s - %s",
4781 filename, strerror(errno));
4782 job->num_files --;
4783 return (0);
4784 }
4785
4786 /*
4787 * Parse the file to the end...
4788 */
4789
4790 while ((ch = cupsFileGetChar(in)) != EOF)
4791 if (ch == '{')
4792 {
4793 /*
4794 * Get an attribute name...
4795 */
4796
4797 for (s = attrname; (ch = cupsFileGetChar(in)) != EOF;)
4798 if (!isalpha(ch & 255) && ch != '-' && ch != '?')
4799 break;
4800 else if (s < (attrname + sizeof(attrname) - 1))
4801 *s++ = ch;
4802 else
4803 break;
4804
4805 *s = '\0';
4806
4807 if (ch != '}')
4808 {
4809 /*
4810 * Ignore { followed by stuff that is not an attribute name...
4811 */
4812
4813 cupsFilePrintf(out, "{%s%c", attrname, ch);
4814 continue;
4815 }
4816
4817 /*
4818 * See if it is defined...
4819 */
4820
4821 if (attrname[0] == '?')
4822 s = attrname + 1;
4823 else
4824 s = attrname;
4825
4826 if (!strcmp(s, "printer-name"))
4827 {
4828 cupsFilePuts(out, job->dest);
4829 continue;
4830 }
4831 else if ((attr = ippFindAttribute(job->attrs, s, IPP_TAG_ZERO)) == NULL)
4832 {
4833 /*
4834 * See if we have a leading question mark...
4835 */
4836
4837 if (attrname[0] != '?')
4838 {
4839 /*
4840 * Nope, write to file as-is; probably a PostScript procedure...
4841 */
4842
4843 cupsFilePrintf(out, "{%s}", attrname);
4844 }
4845
4846 continue;
4847 }
4848
4849 /*
4850 * Output value(s)...
4851 */
4852
4853 for (i = 0; i < attr->num_values; i ++)
4854 {
4855 if (i)
4856 cupsFilePutChar(out, ',');
4857
4858 switch (attr->value_tag)
4859 {
4860 case IPP_TAG_INTEGER :
4861 case IPP_TAG_ENUM :
4862 if (!strncmp(s, "time-at-", 8))
4863 cupsFilePuts(out, cupsdGetDateTime(attr->values[i].integer));
4864 else
4865 cupsFilePrintf(out, "%d", attr->values[i].integer);
4866 break;
4867
4868 case IPP_TAG_BOOLEAN :
4869 cupsFilePrintf(out, "%d", attr->values[i].boolean);
4870 break;
4871
4872 case IPP_TAG_NOVALUE :
4873 cupsFilePuts(out, "novalue");
4874 break;
4875
4876 case IPP_TAG_RANGE :
4877 cupsFilePrintf(out, "%d-%d", attr->values[i].range.lower,
4878 attr->values[i].range.upper);
4879 break;
4880
4881 case IPP_TAG_RESOLUTION :
4882 cupsFilePrintf(out, "%dx%d%s", attr->values[i].resolution.xres,
4883 attr->values[i].resolution.yres,
4884 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
4885 "dpi" : "dpc");
4886 break;
4887
4888 case IPP_TAG_URI :
4889 case IPP_TAG_STRING :
4890 case IPP_TAG_TEXT :
4891 case IPP_TAG_NAME :
4892 case IPP_TAG_KEYWORD :
4893 case IPP_TAG_CHARSET :
4894 case IPP_TAG_LANGUAGE :
4895 if (!strcasecmp(banner->filetype->type, "postscript"))
4896 {
4897 /*
4898 * Need to quote strings for PS banners...
4899 */
4900
4901 const char *p;
4902
4903 for (p = attr->values[i].string.text; *p; p ++)
4904 {
4905 if (*p == '(' || *p == ')' || *p == '\\')
4906 {
4907 cupsFilePutChar(out, '\\');
4908 cupsFilePutChar(out, *p);
4909 }
4910 else if (*p < 32 || *p > 126)
4911 cupsFilePrintf(out, "\\%03o", *p & 255);
4912 else
4913 cupsFilePutChar(out, *p);
4914 }
4915 }
4916 else
4917 cupsFilePuts(out, attr->values[i].string.text);
4918 break;
4919
4920 default :
4921 break; /* anti-compiler-warning-code */
4922 }
4923 }
4924 }
4925 else if (ch == '\\') /* Quoted char */
4926 {
4927 ch = cupsFileGetChar(in);
4928
4929 if (ch != '{') /* Only do special handling for \{ */
4930 cupsFilePutChar(out, '\\');
4931
4932 cupsFilePutChar(out, ch);
4933 }
4934 else
4935 cupsFilePutChar(out, ch);
4936
4937 cupsFileClose(in);
4938
4939 kbytes = (cupsFileTell(out) + 1023) / 1024;
4940
4941 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
4942 IPP_TAG_INTEGER)) != NULL)
4943 attr->values[0].integer += kbytes;
4944
4945 cupsFileClose(out);
4946
4947 return (kbytes);
4948 }
4949
4950
4951 /*
4952 * 'copy_file()' - Copy a PPD file or interface script...
4953 */
4954
4955 static int /* O - 0 = success, -1 = error */
4956 copy_file(const char *from, /* I - Source file */
4957 const char *to) /* I - Destination file */
4958 {
4959 cups_file_t *src, /* Source file */
4960 *dst; /* Destination file */
4961 int bytes; /* Bytes to read/write */
4962 char buffer[2048]; /* Copy buffer */
4963
4964
4965 cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_file(\"%s\", \"%s\")", from, to);
4966
4967 /*
4968 * Open the source and destination file for a copy...
4969 */
4970
4971 if ((src = cupsFileOpen(from, "rb")) == NULL)
4972 return (-1);
4973
4974 if ((dst = cupsFileOpen(to, "wb")) == NULL)
4975 {
4976 cupsFileClose(src);
4977 return (-1);
4978 }
4979
4980 /*
4981 * Copy the source file to the destination...
4982 */
4983
4984 while ((bytes = cupsFileRead(src, buffer, sizeof(buffer))) > 0)
4985 if (cupsFileWrite(dst, buffer, bytes) < bytes)
4986 {
4987 cupsFileClose(src);
4988 cupsFileClose(dst);
4989 return (-1);
4990 }
4991
4992 /*
4993 * Close both files and return...
4994 */
4995
4996 cupsFileClose(src);
4997
4998 return (cupsFileClose(dst));
4999 }
5000
5001
5002 /*
5003 * 'copy_model()' - Copy a PPD model file, substituting default values
5004 * as needed...
5005 */
5006
5007 static int /* O - 0 = success, -1 = error */
5008 copy_model(cupsd_client_t *con, /* I - Client connection */
5009 const char *from, /* I - Source file */
5010 const char *to) /* I - Destination file */
5011 {
5012 fd_set input; /* select() input set */
5013 struct timeval timeout; /* select() timeout */
5014 int maxfd; /* Max file descriptor for select() */
5015 char tempfile[1024]; /* Temporary PPD file */
5016 int tempfd; /* Temporary PPD file descriptor */
5017 int temppid; /* Process ID of cups-driverd */
5018 int temppipe[2]; /* Temporary pipes */
5019 char *argv[4], /* Command-line arguments */
5020 *envp[MAX_ENV]; /* Environment */
5021 cups_file_t *src, /* Source file */
5022 *dst; /* Destination file */
5023 ppd_file_t *ppd; /* PPD file */
5024 int bytes, /* Bytes from pipe */
5025 total; /* Total bytes from pipe */
5026 char buffer[2048]; /* Copy buffer */
5027 int i; /* Looping var */
5028 char option[PPD_MAX_NAME], /* Option name */
5029 choice[PPD_MAX_NAME]; /* Choice name */
5030 int num_defaults; /* Number of default options */
5031 cups_option_t *defaults; /* Default options */
5032 char cups_protocol[PPD_MAX_LINE];
5033 /* cupsProtocol attribute */
5034
5035
5036 cupsdLogMessage(CUPSD_LOG_DEBUG2,
5037 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
5038 con, from, to);
5039
5040 /*
5041 * Run cups-driverd to get the PPD file...
5042 */
5043
5044 argv[0] = "cups-driverd";
5045 argv[1] = "cat";
5046 argv[2] = (char *)from;
5047 argv[3] = NULL;
5048
5049 cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
5050
5051 snprintf(buffer, sizeof(buffer), "%s/daemon/cups-driverd", ServerBin);
5052 snprintf(tempfile, sizeof(tempfile), "%s/%d.ppd", TempDir, con->http.fd);
5053 tempfd = open(tempfile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5054 if (tempfd < 0)
5055 return (-1);
5056
5057 cupsdOpenPipe(temppipe);
5058
5059 cupsdLogMessage(CUPSD_LOG_DEBUG,
5060 "copy_model: Running \"cups-driverd cat %s\"...", from);
5061
5062 if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1],
5063 -1, -1, 0, DefaultProfile, &temppid))
5064 {
5065 close(tempfd);
5066 unlink(tempfile);
5067
5068 return (-1);
5069 }
5070
5071 close(temppipe[1]);
5072
5073 /*
5074 * Wait up to 30 seconds for the PPD file to be copied...
5075 */
5076
5077 total = 0;
5078
5079 if (temppipe[0] > CGIPipes[0])
5080 maxfd = temppipe[0] + 1;
5081 else
5082 maxfd = CGIPipes[0] + 1;
5083
5084 for (;;)
5085 {
5086 /*
5087 * See if we have data ready...
5088 */
5089
5090 FD_ZERO(&input);
5091 FD_SET(temppipe[0], &input);
5092 FD_SET(CGIPipes[0], &input);
5093
5094 timeout.tv_sec = 30;
5095 timeout.tv_usec = 0;
5096
5097 if ((i = select(maxfd, &input, NULL, NULL, &timeout)) < 0)
5098 {
5099 if (errno == EINTR)
5100 continue;
5101 else
5102 break;
5103 }
5104 else if (i == 0)
5105 {
5106 /*
5107 * We have timed out...
5108 */
5109
5110 break;
5111 }
5112
5113 if (FD_ISSET(temppipe[0], &input))
5114 {
5115 /*
5116 * Read the PPD file from the pipe, and write it to the PPD file.
5117 */
5118
5119 if ((bytes = read(temppipe[0], buffer, sizeof(buffer))) > 0)
5120 {
5121 if (write(tempfd, buffer, bytes) < bytes)
5122 break;
5123
5124 total += bytes;
5125 }
5126 else
5127 break;
5128 }
5129
5130 if (FD_ISSET(CGIPipes[0], &input))
5131 cupsdUpdateCGI();
5132 }
5133
5134 close(temppipe[0]);
5135 close(tempfd);
5136
5137 if (!total)
5138 {
5139 /*
5140 * No data from cups-deviced...
5141 */
5142
5143 cupsdLogMessage(CUPSD_LOG_ERROR, "copy_model: empty PPD file!");
5144 unlink(tempfile);
5145 return (-1);
5146 }
5147
5148 /*
5149 * Read the source file and see what page sizes are supported...
5150 */
5151
5152 if ((ppd = ppdOpenFile(tempfile)) == NULL)
5153 {
5154 unlink(tempfile);
5155 return (-1);
5156 }
5157
5158 /*
5159 * Open the destination (if possible) and set the default options...
5160 */
5161
5162 num_defaults = 0;
5163 defaults = NULL;
5164 cups_protocol[0] = '\0';
5165
5166 if ((dst = cupsFileOpen(to, "rb")) != NULL)
5167 {
5168 /*
5169 * Read all of the default lines from the old PPD...
5170 */
5171
5172 while (cupsFileGets(dst, buffer, sizeof(buffer)))
5173 if (!strncmp(buffer, "*Default", 8))
5174 {
5175 /*
5176 * Add the default option...
5177 */
5178
5179 if (!ppd_parse_line(buffer, option, sizeof(option),
5180 choice, sizeof(choice)))
5181 {
5182 ppd_option_t *ppdo; /* PPD option */
5183
5184
5185 /*
5186 * Only add the default if the default hasn't already been
5187 * set and the choice exists in the new PPD...
5188 */
5189
5190 if (!cupsGetOption(option, num_defaults, defaults) &&
5191 (ppdo = ppdFindOption(ppd, option)) != NULL &&
5192 ppdFindChoice(ppdo, choice))
5193 num_defaults = cupsAddOption(option, choice, num_defaults,
5194 &defaults);
5195 }
5196 }
5197 else if (!strncmp(buffer, "*cupsProtocol:", 14))
5198 strlcpy(cups_protocol, buffer, sizeof(cups_protocol));
5199
5200 cupsFileClose(dst);
5201 }
5202 else if (ppdPageSize(ppd, DefaultPaperSize))
5203 {
5204 /*
5205 * Add the default media sizes...
5206 */
5207
5208 num_defaults = cupsAddOption("PageSize", DefaultPaperSize,
5209 num_defaults, &defaults);
5210 num_defaults = cupsAddOption("PageRegion", DefaultPaperSize,
5211 num_defaults, &defaults);
5212 num_defaults = cupsAddOption("PaperDimension", DefaultPaperSize,
5213 num_defaults, &defaults);
5214 num_defaults = cupsAddOption("ImageableArea", DefaultPaperSize,
5215 num_defaults, &defaults);
5216 }
5217
5218 ppdClose(ppd);
5219
5220 /*
5221 * Open the source file for a copy...
5222 */
5223
5224 if ((src = cupsFileOpen(tempfile, "rb")) == NULL)
5225 {
5226 cupsFreeOptions(num_defaults, defaults);
5227 unlink(tempfile);
5228 return (-1);
5229 }
5230
5231 /*
5232 * Open the destination file for a copy...
5233 */
5234
5235 if ((dst = cupsFileOpen(to, "wb")) == NULL)
5236 {
5237 cupsFreeOptions(num_defaults, defaults);
5238 cupsFileClose(src);
5239 unlink(tempfile);
5240 return (-1);
5241 }
5242
5243 /*
5244 * Copy the source file to the destination...
5245 */
5246
5247 while (cupsFileGets(src, buffer, sizeof(buffer)))
5248 {
5249 if (!strncmp(buffer, "*Default", 8))
5250 {
5251 /*
5252 * Check for an previous default option choice...
5253 */
5254
5255 if (!ppd_parse_line(buffer, option, sizeof(option),
5256 choice, sizeof(choice)))
5257 {
5258 const char *val; /* Default option value */
5259
5260
5261 if ((val = cupsGetOption(option, num_defaults, defaults)) != NULL)
5262 {
5263 /*
5264 * Substitute the previous choice...
5265 */
5266
5267 snprintf(buffer, sizeof(buffer), "*Default%s: %s", option, val);
5268 }
5269 }
5270 }
5271
5272 cupsFilePrintf(dst, "%s\n", buffer);
5273 }
5274
5275 if (cups_protocol[0])
5276 cupsFilePrintf(dst, "%s\n", cups_protocol);
5277
5278 cupsFreeOptions(num_defaults, defaults);
5279
5280 /*
5281 * Close both files and return...
5282 */
5283
5284 cupsFileClose(src);
5285
5286 unlink(tempfile);
5287
5288 return (cupsFileClose(dst));
5289 }
5290
5291
5292 /*
5293 * 'copy_job_attrs()' - Copy job attributes.
5294 */
5295
5296 static void
5297 copy_job_attrs(cupsd_client_t *con, /* I - Client connection */
5298 cupsd_job_t *job, /* I - Job */
5299 cups_array_t *ra) /* I - Requested attributes array */
5300 {
5301 char job_uri[HTTP_MAX_URI]; /* Job URI */
5302
5303
5304 /*
5305 * Send the requested attributes for each job...
5306 */
5307
5308 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
5309 con->servername, con->serverport, "/jobs/%d",
5310 job->id);
5311
5312 if (!ra || cupsArrayFind(ra, "document-count"))
5313 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
5314 "document-count", job->num_files);
5315
5316 if (!ra || cupsArrayFind(ra, "job-media-progress"))
5317 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
5318 "job-media-progress", job->progress);
5319
5320 if (!ra || cupsArrayFind(ra, "job-more-info"))
5321 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
5322 "job-more-info", NULL, job_uri);
5323
5324 if (job->state_value > IPP_JOB_PROCESSING &&
5325 (!ra || cupsArrayFind(ra, "job-preserved")))
5326 ippAddBoolean(con->response, IPP_TAG_JOB, "job-preserved",
5327 job->num_files > 0);
5328
5329 if (!ra || cupsArrayFind(ra, "job-printer-up-time"))
5330 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
5331 "job-printer-up-time", time(NULL));
5332
5333 if (!ra || cupsArrayFind(ra, "job-state-reasons"))
5334 add_job_state_reasons(con, job);
5335
5336 if (!ra || cupsArrayFind(ra, "job-uri"))
5337 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
5338 "job-uri", NULL, job_uri);
5339
5340 copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0);
5341 }
5342
5343
5344 /*
5345 * 'copy_printer_attrs()' - Copy printer attributes.
5346 */
5347
5348 static void
5349 copy_printer_attrs(
5350 cupsd_client_t *con, /* I - Client connection */
5351 cupsd_printer_t *printer, /* I - Printer */
5352 cups_array_t *ra) /* I - Requested attributes array */
5353 {
5354 char printer_uri[HTTP_MAX_URI];
5355 /* Printer URI */
5356 time_t curtime; /* Current time */
5357 int i; /* Looping var */
5358 ipp_attribute_t *history; /* History collection */
5359
5360
5361 /*
5362 * Copy the printer attributes to the response using requested-attributes
5363 * and document-format attributes that may be provided by the client.
5364 */
5365
5366 curtime = time(NULL);
5367
5368 #ifdef __APPLE__
5369 if ((!ra || cupsArrayFind(ra, "com.apple.print.recoverable-message")) &&
5370 printer->recoverable)
5371 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
5372 "com.apple.print.recoverable-message", NULL,
5373 printer->recoverable);
5374 #endif /* __APPLE__ */
5375
5376 if (!ra || cupsArrayFind(ra, "marker-change-time"))
5377 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5378 "marker-change-time", printer->marker_time);
5379
5380 if (printer->num_printers > 0 &&
5381 (!ra || cupsArrayFind(ra, "member-uris")))
5382 {
5383 ipp_attribute_t *member_uris; /* member-uris attribute */
5384 cupsd_printer_t *p2; /* Printer in class */
5385 ipp_attribute_t *p2_uri; /* printer-uri-supported for class printer */
5386
5387
5388 if ((member_uris = ippAddStrings(con->response, IPP_TAG_PRINTER,
5389 IPP_TAG_URI, "member-uris",
5390 printer->num_printers, NULL,
5391 NULL)) != NULL)
5392 {
5393 for (i = 0; i < printer->num_printers; i ++)
5394 {
5395 p2 = printer->printers[i];
5396
5397 if ((p2_uri = ippFindAttribute(p2->attrs, "printer-uri-supported",
5398 IPP_TAG_URI)) != NULL)
5399 member_uris->values[i].string.text =
5400 _cupsStrAlloc(p2_uri->values[0].string.text);
5401 else
5402 {
5403 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri,
5404 sizeof(printer_uri), "ipp", NULL, con->servername,
5405 con->serverport,
5406 (p2->type & CUPS_PRINTER_CLASS) ?
5407 "/classes/%s" : "/printers/%s", p2->name);
5408 member_uris->values[i].string.text = _cupsStrAlloc(printer_uri);
5409 }
5410 }
5411 }
5412 }
5413
5414 if (printer->alert && (!ra || cupsArrayFind(ra, "printer-alert")))
5415 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_STRING,
5416 "printer-alert", NULL, printer->alert);
5417
5418 if (printer->alert_description &&
5419 (!ra || cupsArrayFind(ra, "printer-alert-description")))
5420 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
5421 "printer-alert-description", NULL,
5422 printer->alert_description);
5423
5424 if (!ra || cupsArrayFind(ra, "printer-current-time"))
5425 ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
5426 ippTimeToDate(curtime));
5427
5428 #ifdef HAVE_DNSSD
5429 if (!ra || cupsArrayFind(ra, "printer-dns-sd-name"))
5430 {
5431 if (printer->reg_name)
5432 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
5433 "printer-dns-sd-name", NULL, printer->reg_name);
5434 else
5435 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
5436 "printer-dns-sd-name", 0);
5437 }
5438 #endif /* HAVE_DNSSD */
5439
5440 if (!ra || cupsArrayFind(ra, "printer-error-policy"))
5441 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
5442 "printer-error-policy", NULL, printer->error_policy);
5443
5444 if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs"))
5445 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
5446 printer->accepting);
5447
5448 if (!ra || cupsArrayFind(ra, "printer-is-shared"))
5449 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared",
5450 printer->shared);
5451
5452 if ((!ra || cupsArrayFind(ra, "printer-more-info")) &&
5453 !(printer->type & CUPS_PRINTER_DISCOVERED))
5454 {
5455 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
5456 "http", NULL, con->servername, con->serverport,
5457 (printer->type & CUPS_PRINTER_CLASS) ?
5458 "/classes/%s" : "/printers/%s", printer->name);
5459 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
5460 "printer-more-info", NULL, printer_uri);
5461 }
5462
5463 if (!ra || cupsArrayFind(ra, "printer-op-policy"))
5464 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
5465 "printer-op-policy", NULL, printer->op_policy);
5466
5467 if (!ra || cupsArrayFind(ra, "printer-state"))
5468 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
5469 printer->state);
5470
5471 if (!ra || cupsArrayFind(ra, "printer-state-change-time"))
5472 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5473 "printer-state-change-time", printer->state_time);
5474
5475 if (MaxPrinterHistory > 0 && printer->num_history > 0 &&
5476 cupsArrayFind(ra, "printer-state-history"))
5477 {
5478 /*
5479 * Printer history is only sent if specifically requested, so that
5480 * older CUPS/IPP clients won't barf on the collection attributes.
5481 */
5482
5483 history = ippAddCollections(con->response, IPP_TAG_PRINTER,
5484 "printer-state-history",
5485 printer->num_history, NULL);
5486
5487 for (i = 0; i < printer->num_history; i ++)
5488 copy_attrs(history->values[i].collection = ippNew(), printer->history[i],
5489 NULL, IPP_TAG_ZERO, 0);
5490 }
5491
5492 if (!ra || cupsArrayFind(ra, "printer-state-message"))
5493 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
5494 "printer-state-message", NULL, printer->state_message);
5495
5496 if (!ra || cupsArrayFind(ra, "printer-state-reasons"))
5497 add_printer_state_reasons(con, printer);
5498
5499 if (!ra || cupsArrayFind(ra, "printer-type"))
5500 {
5501 int type; /* printer-type value */
5502
5503
5504 /*
5505 * Add the CUPS-specific printer-type attribute...
5506 */
5507
5508 type = printer->type;
5509
5510 if (printer == DefaultPrinter)
5511 type |= CUPS_PRINTER_DEFAULT;
5512
5513 if (!printer->accepting)
5514 type |= CUPS_PRINTER_REJECTING;
5515
5516 if (!printer->shared)
5517 type |= CUPS_PRINTER_NOT_SHARED;
5518
5519 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type",
5520 type);
5521 }
5522
5523 if (!ra || cupsArrayFind(ra, "printer-up-time"))
5524 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5525 "printer-up-time", curtime);
5526
5527 if ((!ra || cupsArrayFind(ra, "printer-uri-supported")) &&
5528 !(printer->type & CUPS_PRINTER_DISCOVERED))
5529 {
5530 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
5531 "ipp", NULL, con->servername, con->serverport,
5532 (printer->type & CUPS_PRINTER_CLASS) ?
5533 "/classes/%s" : "/printers/%s", printer->name);
5534 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
5535 "printer-uri-supported", NULL, printer_uri);
5536 cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"",
5537 printer_uri);
5538 }
5539
5540 if (!ra || cupsArrayFind(ra, "queued-job-count"))
5541 add_queued_job_count(con, printer);
5542
5543 copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0);
5544 copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY);
5545 }
5546
5547
5548 /*
5549 * 'copy_subscription_attrs()' - Copy subscription attributes.
5550 */
5551
5552 static void
5553 copy_subscription_attrs(
5554 cupsd_client_t *con, /* I - Client connection */
5555 cupsd_subscription_t *sub, /* I - Subscription */
5556 cups_array_t *ra) /* I - Requested attributes array */
5557 {
5558 ipp_attribute_t *attr; /* Current attribute */
5559 char printer_uri[HTTP_MAX_URI];
5560 /* Printer URI */
5561 int count; /* Number of events */
5562 unsigned mask; /* Current event mask */
5563 const char *name; /* Current event name */
5564
5565
5566 /*
5567 * Copy the subscription attributes to the response using the
5568 * requested-attributes attribute that may be provided by the client.
5569 */
5570
5571 if (!ra || cupsArrayFind(ra, "notify-events"))
5572 {
5573 if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL)
5574 {
5575 /*
5576 * Simple event list...
5577 */
5578
5579 ippAddString(con->response, IPP_TAG_SUBSCRIPTION,
5580 (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
5581 "notify-events", NULL, name);
5582 }
5583 else
5584 {
5585 /*
5586 * Complex event list...
5587 */
5588
5589 for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
5590 if (sub->mask & mask)
5591 count ++;
5592
5593 attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION,
5594 (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
5595 "notify-events", count, NULL, NULL);
5596
5597 for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
5598 if (sub->mask & mask)
5599 {
5600 attr->values[count].string.text =
5601 (char *)cupsdEventName((cupsd_eventmask_t)mask);
5602
5603 count ++;
5604 }
5605 }
5606 }
5607
5608 if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id")))
5609 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5610 "notify-job-id", sub->job->id);
5611
5612 if (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration")))
5613 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5614 "notify-lease-duration", sub->lease);
5615
5616 if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri")))
5617 {
5618 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
5619 "ipp", NULL, con->servername, con->serverport,
5620 "/printers/%s", sub->dest->name);
5621 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
5622 "notify-printer-uri", NULL, printer_uri);
5623 }
5624
5625 if (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri")))
5626 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
5627 "notify-recipient-uri", NULL, sub->recipient);
5628 else if (!ra || cupsArrayFind(ra, "notify-pull-method"))
5629 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
5630 "notify-pull-method", NULL, "ippget");
5631
5632 if (!ra || cupsArrayFind(ra, "notify-subscriber-user-name"))
5633 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME,
5634 "notify-subscriber-user-name", NULL, sub->owner);
5635
5636 if (!ra || cupsArrayFind(ra, "notify-subscription-id"))
5637 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5638 "notify-subscription-id", sub->id);
5639
5640 if (!ra || cupsArrayFind(ra, "notify-time-interval"))
5641 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5642 "notify-time-interval", sub->interval);
5643
5644 if (sub->user_data_len > 0 && (!ra || cupsArrayFind(ra, "notify-user-data")))
5645 ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data",
5646 sub->user_data, sub->user_data_len);
5647 }
5648
5649
5650 /*
5651 * 'create_job()' - Print a file to a printer or class.
5652 */
5653
5654 static void
5655 create_job(cupsd_client_t *con, /* I - Client connection */
5656 ipp_attribute_t *uri) /* I - Printer URI */
5657 {
5658 cupsd_printer_t *printer; /* Printer */
5659 cupsd_job_t *job; /* New job */
5660
5661
5662 cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con,
5663 con->http.fd, uri->values[0].string.text);
5664
5665 /*
5666 * Is the destination valid?
5667 */
5668
5669 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
5670 {
5671 /*
5672 * Bad URI...
5673 */
5674
5675 send_ipp_status(con, IPP_NOT_FOUND,
5676 _("The printer or class was not found."));
5677 return;
5678 }
5679
5680 /*
5681 * Create the job object...
5682 */
5683
5684 if ((job = add_job(con, printer, NULL)) == NULL)
5685 return;
5686
5687 job->pending_timeout = 1;
5688
5689 /*
5690 * Save and log the job...
5691 */
5692
5693 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
5694 job->dest, job->username);
5695 }
5696
5697
5698 /*
5699 * 'create_requested_array()' - Create an array for the requested-attributes.
5700 */
5701
5702 static cups_array_t * /* O - Array of attributes or NULL */
5703 create_requested_array(ipp_t *request) /* I - IPP request */
5704 {
5705 int i; /* Looping var */
5706 ipp_attribute_t *requested; /* requested-attributes attribute */
5707 cups_array_t *ra; /* Requested attributes array */
5708 char *value; /* Current value */
5709
5710
5711 /*
5712 * Get the requested-attributes attribute, and return NULL if we don't
5713 * have one...
5714 */
5715
5716 if ((requested = ippFindAttribute(request, "requested-attributes",
5717 IPP_TAG_KEYWORD)) == NULL)
5718 return (NULL);
5719
5720 /*
5721 * If the attribute contains a single "all" keyword, return NULL...
5722 */
5723
5724 if (requested->num_values == 1 &&
5725 !strcmp(requested->values[0].string.text, "all"))
5726 return (NULL);
5727
5728 /*
5729 * Create an array using "strcmp" as the comparison function...
5730 */
5731
5732 ra = cupsArrayNew((cups_array_func_t)strcmp, NULL);
5733
5734 for (i = 0; i < requested->num_values; i ++)
5735 {
5736 value = requested->values[i].string.text;
5737
5738 if (!strcmp(value, "job-template"))
5739 {
5740 cupsArrayAdd(ra, "copies");
5741 cupsArrayAdd(ra, "copies-default");
5742 cupsArrayAdd(ra, "copies-supported");
5743 cupsArrayAdd(ra, "finishings");
5744 cupsArrayAdd(ra, "finishings-default");
5745 cupsArrayAdd(ra, "finishings-supported");
5746 cupsArrayAdd(ra, "job-hold-until");
5747 cupsArrayAdd(ra, "job-hold-until-default");
5748 cupsArrayAdd(ra, "job-hold-until-supported");
5749 cupsArrayAdd(ra, "job-priority");
5750 cupsArrayAdd(ra, "job-priority-default");
5751 cupsArrayAdd(ra, "job-priority-supported");
5752 cupsArrayAdd(ra, "job-sheets");
5753 cupsArrayAdd(ra, "job-sheets-default");
5754 cupsArrayAdd(ra, "job-sheets-supported");
5755 cupsArrayAdd(ra, "media");
5756 cupsArrayAdd(ra, "media-default");
5757 cupsArrayAdd(ra, "media-supported");
5758 cupsArrayAdd(ra, "multiple-document-handling");
5759 cupsArrayAdd(ra, "multiple-document-handling-default");
5760 cupsArrayAdd(ra, "multiple-document-handling-supported");
5761 cupsArrayAdd(ra, "number-up");
5762 cupsArrayAdd(ra, "number-up-default");
5763 cupsArrayAdd(ra, "number-up-supported");
5764 cupsArrayAdd(ra, "orientation-requested");
5765 cupsArrayAdd(ra, "orientation-requested-default");
5766 cupsArrayAdd(ra, "orientation-requested-supported");
5767 cupsArrayAdd(ra, "page-ranges");
5768 cupsArrayAdd(ra, "page-ranges-supported");
5769 cupsArrayAdd(ra, "printer-resolution");
5770 cupsArrayAdd(ra, "printer-resolution-default");
5771 cupsArrayAdd(ra, "printer-resolution-supported");
5772 cupsArrayAdd(ra, "print-quality");
5773 cupsArrayAdd(ra, "print-quality-default");
5774 cupsArrayAdd(ra, "print-quality-supported");
5775 cupsArrayAdd(ra, "sides");
5776 cupsArrayAdd(ra, "sides-default");
5777 cupsArrayAdd(ra, "sides-supported");
5778 }
5779 else if (!strcmp(value, "job-description"))
5780 {
5781 cupsArrayAdd(ra, "date-time-at-completed");
5782 cupsArrayAdd(ra, "date-time-at-creation");
5783 cupsArrayAdd(ra, "date-time-at-processing");
5784 cupsArrayAdd(ra, "job-detailed-status-message");
5785 cupsArrayAdd(ra, "job-document-access-errors");
5786 cupsArrayAdd(ra, "job-id");
5787 cupsArrayAdd(ra, "job-impressions");
5788 cupsArrayAdd(ra, "job-impressions-completed");
5789 cupsArrayAdd(ra, "job-k-octets");
5790 cupsArrayAdd(ra, "job-k-octets-processed");
5791 cupsArrayAdd(ra, "job-media-progress");
5792 cupsArrayAdd(ra, "job-media-sheets");
5793 cupsArrayAdd(ra, "job-media-sheets-completed");
5794 cupsArrayAdd(ra, "job-message-from-operator");
5795 cupsArrayAdd(ra, "job-more-info");
5796 cupsArrayAdd(ra, "job-name");
5797 cupsArrayAdd(ra, "job-originating-user-name");
5798 cupsArrayAdd(ra, "job-printer-up-time");
5799 cupsArrayAdd(ra, "job-printer-uri");
5800 cupsArrayAdd(ra, "job-state");
5801 cupsArrayAdd(ra, "job-state-message");
5802 cupsArrayAdd(ra, "job-state-reasons");
5803 cupsArrayAdd(ra, "job-uri");
5804 cupsArrayAdd(ra, "number-of-documents");
5805 cupsArrayAdd(ra, "number-of-intervening-jobs");
5806 cupsArrayAdd(ra, "output-device-assigned");
5807 cupsArrayAdd(ra, "time-at-completed");
5808 cupsArrayAdd(ra, "time-at-creation");
5809 cupsArrayAdd(ra, "time-at-processing");
5810 }
5811 else if (!strcmp(value, "printer-description"))
5812 {
5813 cupsArrayAdd(ra, "charset-configured");
5814 cupsArrayAdd(ra, "charset-supported");
5815 cupsArrayAdd(ra, "color-supported");
5816 cupsArrayAdd(ra, "compression-supported");
5817 cupsArrayAdd(ra, "document-format-default");
5818 cupsArrayAdd(ra, "document-format-supported");
5819 cupsArrayAdd(ra, "generated-natural-language-supported");
5820 cupsArrayAdd(ra, "ipp-versions-supported");
5821 cupsArrayAdd(ra, "job-impressions-supported");
5822 cupsArrayAdd(ra, "job-k-octets-supported");
5823 cupsArrayAdd(ra, "job-media-sheets-supported");
5824 cupsArrayAdd(ra, "job-settable-attributes-supported");
5825 cupsArrayAdd(ra, "multiple-document-jobs-supported");
5826 cupsArrayAdd(ra, "multiple-operation-time-out");
5827 cupsArrayAdd(ra, "natural-language-configured");
5828 cupsArrayAdd(ra, "notify-attributes-supported");
5829 cupsArrayAdd(ra, "notify-lease-duration-default");
5830 cupsArrayAdd(ra, "notify-lease-duration-supported");
5831 cupsArrayAdd(ra, "notify-max-events-supported");
5832 cupsArrayAdd(ra, "notify-events-default");
5833 cupsArrayAdd(ra, "notify-events-supported");
5834 cupsArrayAdd(ra, "notify-pull-method-supported");
5835 cupsArrayAdd(ra, "notify-schemes-supported");
5836 cupsArrayAdd(ra, "operations-supported");
5837 cupsArrayAdd(ra, "pages-per-minute");
5838 cupsArrayAdd(ra, "pages-per-minute-color");
5839 cupsArrayAdd(ra, "pdl-override-supported");
5840 cupsArrayAdd(ra, "printer-alert");
5841 cupsArrayAdd(ra, "printer-alert-description");
5842 cupsArrayAdd(ra, "printer-commands");
5843 cupsArrayAdd(ra, "printer-current-time");
5844 cupsArrayAdd(ra, "printer-driver-installer");
5845 cupsArrayAdd(ra, "printer-dns-sd-name");
5846 cupsArrayAdd(ra, "printer-info");
5847 cupsArrayAdd(ra, "printer-is-accepting-jobs");
5848 cupsArrayAdd(ra, "printer-location");
5849 cupsArrayAdd(ra, "printer-make-and-model");
5850 cupsArrayAdd(ra, "printer-message-from-operator");
5851 cupsArrayAdd(ra, "printer-more-info");
5852 cupsArrayAdd(ra, "printer-more-info-manufacturer");
5853 cupsArrayAdd(ra, "printer-name");
5854 cupsArrayAdd(ra, "printer-state");
5855 cupsArrayAdd(ra, "printer-state-message");
5856 cupsArrayAdd(ra, "printer-state-reasons");
5857 cupsArrayAdd(ra, "printer-settable-attributes-supported");
5858 cupsArrayAdd(ra, "printer-type");
5859 cupsArrayAdd(ra, "printer-up-time");
5860 cupsArrayAdd(ra, "printer-uri-supported");
5861 cupsArrayAdd(ra, "queued-job-count");
5862 cupsArrayAdd(ra, "reference-uri-schemes-supported");
5863 cupsArrayAdd(ra, "uri-authentication-supported");
5864 cupsArrayAdd(ra, "uri-security-supported");
5865 }
5866 else if (!strcmp(value, "printer-defaults"))
5867 {
5868 char *name; /* Option name */
5869
5870
5871 for (name = (char *)cupsArrayFirst(CommonDefaults);
5872 name;
5873 name = (char *)cupsArrayNext(CommonDefaults))
5874 cupsArrayAdd(ra, name);
5875 }
5876 else if (!strcmp(value, "subscription-template"))
5877 {
5878 cupsArrayAdd(ra, "notify-attributes");
5879 cupsArrayAdd(ra, "notify-charset");
5880 cupsArrayAdd(ra, "notify-events");
5881 cupsArrayAdd(ra, "notify-lease-duration");
5882 cupsArrayAdd(ra, "notify-natural-language");
5883 cupsArrayAdd(ra, "notify-pull-method");
5884 cupsArrayAdd(ra, "notify-recipient-uri");
5885 cupsArrayAdd(ra, "notify-time-interval");
5886 cupsArrayAdd(ra, "notify-user-data");
5887 }
5888 else
5889 cupsArrayAdd(ra, value);
5890 }
5891
5892 return (ra);
5893 }
5894
5895
5896 /*
5897 * 'create_subscription()' - Create a notification subscription.
5898 */
5899
5900 static void
5901 create_subscription(
5902 cupsd_client_t *con, /* I - Client connection */
5903 ipp_attribute_t *uri) /* I - Printer URI */
5904 {
5905 http_status_t status; /* Policy status */
5906 int i; /* Looping var */
5907 ipp_attribute_t *attr; /* Current attribute */
5908 cups_ptype_t dtype; /* Destination type (printer/class) */
5909 char scheme[HTTP_MAX_URI],
5910 /* Scheme portion of URI */
5911 userpass[HTTP_MAX_URI],
5912 /* Username portion of URI */
5913 host[HTTP_MAX_URI],
5914 /* Host portion of URI */
5915 resource[HTTP_MAX_URI];
5916 /* Resource portion of URI */
5917 int port; /* Port portion of URI */
5918 cupsd_printer_t *printer; /* Printer/class */
5919 cupsd_job_t *job; /* Job */
5920 int jobid; /* Job ID */
5921 cupsd_subscription_t *sub; /* Subscription object */
5922 const char *username, /* requesting-user-name or
5923 authenticated username */
5924 *recipient, /* notify-recipient-uri */
5925 *pullmethod; /* notify-pull-method */
5926 ipp_attribute_t *user_data; /* notify-user-data */
5927 int interval, /* notify-time-interval */
5928 lease; /* notify-lease-duration */
5929 unsigned mask; /* notify-events */
5930 ipp_attribute_t *notify_events,/* notify-events(-default) */
5931 *notify_lease; /* notify-lease-duration(-default) */
5932
5933
5934 #ifdef DEBUG
5935 for (attr = con->request->attrs; attr; attr = attr->next)
5936 {
5937 if (attr->group_tag != IPP_TAG_ZERO)
5938 cupsdLogMessage(CUPSD_LOG_DEBUG2, "g%04x v%04x %s", attr->group_tag,
5939 attr->value_tag, attr->name);
5940 else
5941 cupsdLogMessage(CUPSD_LOG_DEBUG2, "----SEP----");
5942 }
5943 #endif /* DEBUG */
5944
5945 /*
5946 * Is the destination valid?
5947 */
5948
5949 cupsdLogMessage(CUPSD_LOG_DEBUG,
5950 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5951 con, con->http.fd, uri->values[0].string.text);
5952
5953 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
5954 sizeof(scheme), userpass, sizeof(userpass), host,
5955 sizeof(host), &port, resource, sizeof(resource));
5956
5957 if (!strcmp(resource, "/"))
5958 {
5959 dtype = (cups_ptype_t)0;
5960 printer = NULL;
5961 }
5962 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
5963 {
5964 dtype = (cups_ptype_t)0;
5965 printer = NULL;
5966 }
5967 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
5968 {
5969 dtype = CUPS_PRINTER_CLASS;
5970 printer = NULL;
5971 }
5972 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
5973 {
5974 /*
5975 * Bad URI...
5976 */
5977
5978 send_ipp_status(con, IPP_NOT_FOUND,
5979 _("The printer or class was not found."));
5980 return;
5981 }
5982
5983 /*
5984 * Check policy...
5985 */
5986
5987 if (printer)
5988 {
5989 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
5990 NULL)) != HTTP_OK)
5991 {
5992 send_http_error(con, status, printer);
5993 return;
5994 }
5995 }
5996 else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5997 {
5998 send_http_error(con, status, NULL);
5999 return;
6000 }
6001
6002 /*
6003 * Get the user that is requesting the subscription...
6004 */
6005
6006 username = get_username(con);
6007
6008 /*
6009 * Find the first subscription group attribute; return if we have
6010 * none...
6011 */
6012
6013 for (attr = con->request->attrs; attr; attr = attr->next)
6014 if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
6015 break;
6016
6017 if (!attr)
6018 {
6019 send_ipp_status(con, IPP_BAD_REQUEST,
6020 _("No subscription attributes in request!"));
6021 return;
6022 }
6023
6024 /*
6025 * Process the subscription attributes in the request...
6026 */
6027
6028 con->response->request.status.status_code = IPP_BAD_REQUEST;
6029
6030 while (attr)
6031 {
6032 recipient = NULL;
6033 pullmethod = NULL;
6034 user_data = NULL;
6035 interval = 0;
6036 lease = DefaultLeaseDuration;
6037 jobid = 0;
6038 mask = CUPSD_EVENT_NONE;
6039
6040 if (printer)
6041 {
6042 notify_events = ippFindAttribute(printer->attrs, "notify-events-default",
6043 IPP_TAG_KEYWORD);
6044 notify_lease = ippFindAttribute(printer->attrs,
6045 "notify-lease-duration-default",
6046 IPP_TAG_INTEGER);
6047
6048 if (notify_lease)
6049 lease = notify_lease->values[0].integer;
6050 }
6051 else
6052 {
6053 notify_events = NULL;
6054 notify_lease = NULL;
6055 }
6056
6057 while (attr && attr->group_tag != IPP_TAG_ZERO)
6058 {
6059 if (!strcmp(attr->name, "notify-recipient-uri") &&
6060 attr->value_tag == IPP_TAG_URI)
6061 {
6062 /*
6063 * Validate the recipient scheme against the ServerBin/notifier
6064 * directory...
6065 */
6066
6067 char notifier[1024]; /* Notifier filename */
6068
6069
6070 recipient = attr->values[0].string.text;
6071
6072 if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient,
6073 scheme, sizeof(scheme), userpass, sizeof(userpass),
6074 host, sizeof(host), &port,
6075 resource, sizeof(resource)) < HTTP_URI_OK)
6076 {
6077 send_ipp_status(con, IPP_NOT_POSSIBLE,
6078 _("Bad notify-recipient-uri URI \"%s\"!"), recipient);
6079 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
6080 "notify-status-code", IPP_URI_SCHEME);
6081 return;
6082 }
6083
6084 snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
6085 scheme);
6086 if (access(notifier, X_OK))
6087 {
6088 send_ipp_status(con, IPP_NOT_POSSIBLE,
6089 _("notify-recipient-uri URI \"%s\" uses unknown "
6090 "scheme!"), recipient);
6091 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
6092 "notify-status-code", IPP_URI_SCHEME);
6093 return;
6094 }
6095
6096 if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient))
6097 {
6098 send_ipp_status(con, IPP_NOT_POSSIBLE,
6099 _("notify-recipient-uri URI \"%s\" is already used!"),
6100 recipient);
6101 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
6102 "notify-status-code", IPP_ATTRIBUTES);
6103 return;
6104 }
6105 }
6106 else if (!strcmp(attr->name, "notify-pull-method") &&
6107 attr->value_tag == IPP_TAG_KEYWORD)
6108 {
6109 pullmethod = attr->values[0].string.text;
6110
6111 if (strcmp(pullmethod, "ippget"))
6112 {
6113 send_ipp_status(con, IPP_NOT_POSSIBLE,
6114 _("Bad notify-pull-method \"%s\"!"), pullmethod);
6115 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
6116 "notify-status-code", IPP_ATTRIBUTES);
6117 return;
6118 }
6119 }
6120 else if (!strcmp(attr->name, "notify-charset") &&
6121 attr->value_tag == IPP_TAG_CHARSET &&
6122 strcmp(attr->values[0].string.text, "us-ascii") &&
6123 strcmp(attr->values[0].string.text, "utf-8"))
6124 {
6125 send_ipp_status(con, IPP_CHARSET,
6126 _("Character set \"%s\" not supported!"),
6127 attr->values[0].string.text);
6128 return;
6129 }
6130 else if (!strcmp(attr->name, "notify-natural-language") &&
6131 (attr->value_tag != IPP_TAG_LANGUAGE ||
6132 strcmp(attr->values[0].string.text, DefaultLanguage)))
6133 {
6134 send_ipp_status(con, IPP_CHARSET,
6135 _("Language \"%s\" not supported!"),
6136 attr->values[0].string.text);
6137 return;
6138 }
6139 else if (!strcmp(attr->name, "notify-user-data") &&
6140 attr->value_tag == IPP_TAG_STRING)
6141 {
6142 if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
6143 {
6144 send_ipp_status(con, IPP_REQUEST_VALUE,
6145 _("The notify-user-data value is too large "
6146 "(%d > 63 octets)!"),
6147 attr->values[0].unknown.length);
6148 return;
6149 }
6150
6151 user_data = attr;
6152 }
6153 else if (!strcmp(attr->name, "notify-events") &&
6154 attr->value_tag == IPP_TAG_KEYWORD)
6155 notify_events = attr;
6156 else if (!strcmp(attr->name, "notify-lease-duration") &&
6157 attr->value_tag == IPP_TAG_INTEGER)
6158 lease = attr->values[0].integer;
6159 else if (!strcmp(attr->name, "notify-time-interval") &&
6160 attr->value_tag == IPP_TAG_INTEGER)
6161 interval = attr->values[0].integer;
6162 else if (!strcmp(attr->name, "notify-job-id") &&
6163 attr->value_tag == IPP_TAG_INTEGER)
6164 jobid = attr->values[0].integer;
6165
6166 attr = attr->next;
6167 }
6168
6169 if (notify_events)
6170 {
6171 for (i = 0; i < notify_events->num_values; i ++)
6172 mask |= cupsdEventValue(notify_events->values[i].string.text);
6173 }
6174
6175 if (recipient)
6176 cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient);
6177 if (pullmethod)
6178 cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod);
6179 cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease);
6180 cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval);
6181
6182 if (!recipient && !pullmethod)
6183 break;
6184
6185 if (mask == CUPSD_EVENT_NONE)
6186 {
6187 if (jobid)
6188 mask = CUPSD_EVENT_JOB_COMPLETED;
6189 else if (printer)
6190 mask = CUPSD_EVENT_PRINTER_STATE_CHANGED;
6191 else
6192 {
6193 send_ipp_status(con, IPP_BAD_REQUEST,
6194 _("notify-events not specified!"));
6195 return;
6196 }
6197 }
6198
6199 if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration))
6200 {
6201 cupsdLogMessage(CUPSD_LOG_INFO,
6202 "create_subscription: Limiting notify-lease-duration to "
6203 "%d seconds.",
6204 MaxLeaseDuration);
6205 lease = MaxLeaseDuration;
6206 }
6207
6208 if (jobid)
6209 {
6210 if ((job = cupsdFindJob(jobid)) == NULL)
6211 {
6212 send_ipp_status(con, IPP_NOT_FOUND, _("Job %d not found!"), jobid);
6213 return;
6214 }
6215 }
6216 else
6217 job = NULL;
6218
6219 sub = cupsdAddSubscription(mask, printer, job, recipient, 0);
6220
6221 if (job)
6222 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for job %d",
6223 sub->id, job->id);
6224 else if (printer)
6225 cupsdLogMessage(CUPSD_LOG_DEBUG,
6226 "Added subscription %d for printer \"%s\"",
6227 sub->id, printer->name);
6228 else
6229 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for server",
6230 sub->id);
6231
6232 sub->interval = interval;
6233 sub->lease = lease;
6234 sub->expire = lease ? time(NULL) + lease : 0;
6235
6236 cupsdSetString(&sub->owner, username);
6237
6238 if (user_data)
6239 {
6240 sub->user_data_len = user_data->values[0].unknown.length;
6241 memcpy(sub->user_data, user_data->values[0].unknown.data,
6242 sub->user_data_len);
6243 }
6244
6245 ippAddSeparator(con->response);
6246 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
6247 "notify-subscription-id", sub->id);
6248
6249 con->response->request.status.status_code = IPP_OK;
6250
6251 if (attr)
6252 attr = attr->next;
6253 }
6254
6255 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
6256 }
6257
6258
6259 /*
6260 * 'delete_printer()' - Remove a printer or class from the system.
6261 */
6262
6263 static void
6264 delete_printer(cupsd_client_t *con, /* I - Client connection */
6265 ipp_attribute_t *uri) /* I - URI of printer or class */
6266 {
6267 http_status_t status; /* Policy status */
6268 cups_ptype_t dtype; /* Destination type (printer/class) */
6269 cupsd_printer_t *printer; /* Printer/class */
6270 char filename[1024]; /* Script/PPD filename */
6271
6272
6273 cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con,
6274 con->http.fd, uri->values[0].string.text);
6275
6276 /*
6277 * Do we have a valid URI?
6278 */
6279
6280 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
6281 {
6282 /*
6283 * Bad URI...
6284 */
6285
6286 send_ipp_status(con, IPP_NOT_FOUND,
6287 _("The printer or class was not found."));
6288 return;
6289 }
6290
6291 /*
6292 * Check policy...
6293 */
6294
6295 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6296 {
6297 send_http_error(con, status, NULL);
6298 return;
6299 }
6300
6301 /*
6302 * Remove old jobs...
6303 */
6304
6305 cupsdCancelJobs(printer->name, NULL, 1);
6306
6307 /*
6308 * Remove old subscriptions and send a "deleted printer" event...
6309 */
6310
6311 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL,
6312 "%s \"%s\" deleted by \"%s\".",
6313 (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
6314 printer->name, get_username(con));
6315
6316 cupsdExpireSubscriptions(printer, NULL);
6317
6318 /*
6319 * Remove any old PPD or script files...
6320 */
6321
6322 snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
6323 printer->name);
6324 unlink(filename);
6325
6326 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
6327 printer->name);
6328 unlink(filename);
6329
6330 #ifdef __APPLE__
6331 /*
6332 * Unregister color profiles...
6333 */
6334
6335 apple_unregister_profiles(printer);
6336 #endif /* __APPLE__ */
6337
6338 if (dtype & CUPS_PRINTER_CLASS)
6339 {
6340 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".",
6341 printer->name, get_username(con));
6342
6343 cupsdDeletePrinter(printer, 0);
6344 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
6345 }
6346 else
6347 {
6348 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".",
6349 printer->name, get_username(con));
6350
6351 cupsdDeletePrinter(printer, 0);
6352 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
6353 }
6354
6355 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
6356
6357 /*
6358 * Return with no errors...
6359 */
6360
6361 con->response->request.status.status_code = IPP_OK;
6362 }
6363
6364
6365 /*
6366 * 'get_default()' - Get the default destination.
6367 */
6368
6369 static void
6370 get_default(cupsd_client_t *con) /* I - Client connection */
6371 {
6372 http_status_t status; /* Policy status */
6373 cups_array_t *ra; /* Requested attributes array */
6374
6375
6376 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->http.fd);
6377
6378 /*
6379 * Check policy...
6380 */
6381
6382 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6383 {
6384 send_http_error(con, status, NULL);
6385 return;
6386 }
6387
6388 if (DefaultPrinter)
6389 {
6390 ra = create_requested_array(con->request);
6391
6392 copy_printer_attrs(con, DefaultPrinter, ra);
6393
6394 cupsArrayDelete(ra);
6395
6396 con->response->request.status.status_code = IPP_OK;
6397 }
6398 else
6399 send_ipp_status(con, IPP_NOT_FOUND, _("No default printer"));
6400 }
6401
6402
6403 /*
6404 * 'get_devices()' - Get the list of available devices on the local system.
6405 */
6406
6407 static void
6408 get_devices(cupsd_client_t *con) /* I - Client connection */
6409 {
6410 http_status_t status; /* Policy status */
6411 ipp_attribute_t *limit, /* limit attribute */
6412 *timeout, /* timeout attribute */
6413 *requested, /* requested-attributes attribute */
6414 *exclude; /* exclude-schemes attribute */
6415 char command[1024], /* cups-deviced command */
6416 options[1024], /* Options to pass to command */
6417 requested_str[256],
6418 /* String for requested attributes */
6419 exclude_str[512];
6420 /* String for excluded attributes */
6421
6422
6423 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->http.fd);
6424
6425 /*
6426 * Check policy...
6427 */
6428
6429 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6430 {
6431 send_http_error(con, status, NULL);
6432 return;
6433 }
6434
6435 /*
6436 * Run cups-deviced command with the given options...
6437 */
6438
6439 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
6440 timeout = ippFindAttribute(con->request, "timeout", IPP_TAG_INTEGER);
6441 requested = ippFindAttribute(con->request, "requested-attributes",
6442 IPP_TAG_KEYWORD);
6443 exclude = ippFindAttribute(con->request, "exclude-schemes", IPP_TAG_NAME);
6444
6445 if (requested)
6446 url_encode_attr(requested, requested_str, sizeof(requested_str));
6447 else
6448 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
6449
6450 if (exclude)
6451 url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
6452 else
6453 exclude_str[0] = '\0';
6454
6455 snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin);
6456 snprintf(options, sizeof(options),
6457 "%d+%d+%d+%d+%s%s%s",
6458 con->request->request.op.request_id,
6459 limit ? limit->values[0].integer : 0,
6460 timeout ? timeout->values[0].integer : 10,
6461 (int)User,
6462 requested_str,
6463 exclude_str[0] ? "%20" : "", exclude_str);
6464
6465 if (cupsdSendCommand(con, command, options, 1))
6466 {
6467 /*
6468 * Command started successfully, don't send an IPP response here...
6469 */
6470
6471 ippDelete(con->response);
6472 con->response = NULL;
6473 }
6474 else
6475 {
6476 /*
6477 * Command failed, return "internal error" so the user knows something
6478 * went wrong...
6479 */
6480
6481 send_ipp_status(con, IPP_INTERNAL_ERROR,
6482 _("cups-deviced failed to execute."));
6483 }
6484 }
6485
6486
6487 /*
6488 * 'get_document()' - Get a copy of a job file.
6489 */
6490
6491 static void
6492 get_document(cupsd_client_t *con, /* I - Client connection */
6493 ipp_attribute_t *uri) /* I - Job URI */
6494 {
6495 http_status_t status; /* Policy status */
6496 ipp_attribute_t *attr; /* Current attribute */
6497 int jobid; /* Job ID */
6498 int docnum; /* Document number */
6499 cupsd_job_t *job; /* Current job */
6500 char method[HTTP_MAX_URI], /* Method portion of URI */
6501 username[HTTP_MAX_URI], /* Username portion of URI */
6502 host[HTTP_MAX_URI], /* Host portion of URI */
6503 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6504 int port; /* Port portion of URI */
6505 char filename[1024], /* Filename for document */
6506 format[1024]; /* Format for document */
6507
6508
6509 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_document(%p[%d], %s)", con,
6510 con->http.fd, uri->values[0].string.text);
6511
6512 /*
6513 * See if we have a job URI or a printer URI...
6514 */
6515
6516 if (!strcmp(uri->name, "printer-uri"))
6517 {
6518 /*
6519 * Got a printer URI; see if we also have a job-id attribute...
6520 */
6521
6522 if ((attr = ippFindAttribute(con->request, "job-id",
6523 IPP_TAG_INTEGER)) == NULL)
6524 {
6525 send_ipp_status(con, IPP_BAD_REQUEST,
6526 _("Got a printer-uri attribute but no job-id!"));
6527 return;
6528 }
6529
6530 jobid = attr->values[0].integer;
6531 }
6532 else
6533 {
6534 /*
6535 * Got a job URI; parse it to get the job ID...
6536 */
6537
6538 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
6539 sizeof(method), username, sizeof(username), host,
6540 sizeof(host), &port, resource, sizeof(resource));
6541
6542 if (strncmp(resource, "/jobs/", 6))
6543 {
6544 /*
6545 * Not a valid URI!
6546 */
6547
6548 send_ipp_status(con, IPP_BAD_REQUEST,
6549 _("Bad job-uri attribute \"%s\"!"),
6550 uri->values[0].string.text);
6551 return;
6552 }
6553
6554 jobid = atoi(resource + 6);
6555 }
6556
6557 /*
6558 * See if the job exists...
6559 */
6560
6561 if ((job = cupsdFindJob(jobid)) == NULL)
6562 {
6563 /*
6564 * Nope - return a "not found" error...
6565 */
6566
6567 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
6568 return;
6569 }
6570
6571 /*
6572 * Check policy...
6573 */
6574
6575 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6576 {
6577 send_http_error(con, status, NULL);
6578 return;
6579 }
6580
6581 /*
6582 * Get the document number...
6583 */
6584
6585 if ((attr = ippFindAttribute(con->request, "document-number",
6586 IPP_TAG_INTEGER)) == NULL)
6587 {
6588 send_ipp_status(con, IPP_BAD_REQUEST,
6589 _("Missing document-number attribute!"));
6590 return;
6591 }
6592
6593 if ((docnum = attr->values[0].integer) < 1 || docnum > job->num_files ||
6594 attr->num_values > 1)
6595 {
6596 send_ipp_status(con, IPP_NOT_FOUND, _("Document %d not found in job %d."),
6597 docnum, jobid);
6598 return;
6599 }
6600
6601 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, jobid,
6602 docnum);
6603 if ((con->file = open(filename, O_RDONLY)) == -1)
6604 {
6605 cupsdLogMessage(CUPSD_LOG_ERROR,
6606 "Unable to open document %d in job %d - %s", docnum, jobid,
6607 strerror(errno));
6608 send_ipp_status(con, IPP_NOT_FOUND,
6609 _("Unable to open document %d in job %d!"), docnum, jobid);
6610 return;
6611 }
6612
6613 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
6614
6615 cupsdLoadJob(job);
6616
6617 snprintf(format, sizeof(format), "%s/%s", job->filetypes[docnum - 1]->super,
6618 job->filetypes[docnum - 1]->type);
6619
6620 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
6621 NULL, format);
6622 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "document-number",
6623 docnum);
6624 if ((attr = ippFindAttribute(job->attrs, "document-name",
6625 IPP_TAG_NAME)) != NULL)
6626 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "document-name",
6627 NULL, attr->values[0].string.text);
6628 }
6629
6630
6631 /*
6632 * 'get_job_attrs()' - Get job attributes.
6633 */
6634
6635 static void
6636 get_job_attrs(cupsd_client_t *con, /* I - Client connection */
6637 ipp_attribute_t *uri) /* I - Job URI */
6638 {
6639 http_status_t status; /* Policy status */
6640 ipp_attribute_t *attr; /* Current attribute */
6641 int jobid; /* Job ID */
6642 cupsd_job_t *job; /* Current job */
6643 char method[HTTP_MAX_URI], /* Method portion of URI */
6644 username[HTTP_MAX_URI], /* Username portion of URI */
6645 host[HTTP_MAX_URI], /* Host portion of URI */
6646 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6647 int port; /* Port portion of URI */
6648 cups_array_t *ra; /* Requested attributes array */
6649
6650
6651 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con,
6652 con->http.fd, uri->values[0].string.text);
6653
6654 /*
6655 * See if we have a job URI or a printer URI...
6656 */
6657
6658 if (!strcmp(uri->name, "printer-uri"))
6659 {
6660 /*
6661 * Got a printer URI; see if we also have a job-id attribute...
6662 */
6663
6664 if ((attr = ippFindAttribute(con->request, "job-id",
6665 IPP_TAG_INTEGER)) == NULL)
6666 {
6667 send_ipp_status(con, IPP_BAD_REQUEST,
6668 _("Got a printer-uri attribute but no job-id!"));
6669 return;
6670 }
6671
6672 jobid = attr->values[0].integer;
6673 }
6674 else
6675 {
6676 /*
6677 * Got a job URI; parse it to get the job ID...
6678 */
6679
6680 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
6681 sizeof(method), username, sizeof(username), host,
6682 sizeof(host), &port, resource, sizeof(resource));
6683
6684 if (strncmp(resource, "/jobs/", 6))
6685 {
6686 /*
6687 * Not a valid URI!
6688 */
6689
6690 send_ipp_status(con, IPP_BAD_REQUEST,
6691 _("Bad job-uri attribute \"%s\"!"),
6692 uri->values[0].string.text);
6693 return;
6694 }
6695
6696 jobid = atoi(resource + 6);
6697 }
6698
6699 /*
6700 * See if the job exists...
6701 */
6702
6703 if ((job = cupsdFindJob(jobid)) == NULL)
6704 {
6705 /*
6706 * Nope - return a "not found" error...
6707 */
6708
6709 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
6710 return;
6711 }
6712
6713 /*
6714 * Check policy...
6715 */
6716
6717 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6718 {
6719 send_http_error(con, status, NULL);
6720 return;
6721 }
6722
6723 /*
6724 * Copy attributes...
6725 */
6726
6727 cupsdLoadJob(job);
6728
6729 ra = create_requested_array(con->request);
6730 copy_job_attrs(con, job, ra);
6731 cupsArrayDelete(ra);
6732
6733 con->response->request.status.status_code = IPP_OK;
6734 }
6735
6736
6737 /*
6738 * 'get_jobs()' - Get a list of jobs for the specified printer.
6739 */
6740
6741 static void
6742 get_jobs(cupsd_client_t *con, /* I - Client connection */
6743 ipp_attribute_t *uri) /* I - Printer URI */
6744 {
6745 http_status_t status; /* Policy status */
6746 ipp_attribute_t *attr; /* Current attribute */
6747 const char *dest; /* Destination */
6748 cups_ptype_t dtype; /* Destination type (printer/class) */
6749 cups_ptype_t dmask; /* Destination type mask */
6750 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
6751 username[HTTP_MAX_URI], /* Username portion of URI */
6752 host[HTTP_MAX_URI], /* Host portion of URI */
6753 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6754 int port; /* Port portion of URI */
6755 int completed; /* Completed jobs? */
6756 int first_job_id; /* First job ID */
6757 int limit; /* Maximum number of jobs to return */
6758 int count; /* Number of jobs that match */
6759 cupsd_job_t *job; /* Current job pointer */
6760 cupsd_printer_t *printer; /* Printer */
6761 cups_array_t *list; /* Which job list... */
6762 cups_array_t *ra; /* Requested attributes array */
6763
6764
6765 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->http.fd,
6766 uri->values[0].string.text);
6767
6768 /*
6769 * Is the destination valid?
6770 */
6771
6772 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6773 sizeof(scheme), username, sizeof(username), host,
6774 sizeof(host), &port, resource, sizeof(resource));
6775
6776 if (!strcmp(resource, "/") ||
6777 (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6))
6778 {
6779 dest = NULL;
6780 dtype = (cups_ptype_t)0;
6781 dmask = (cups_ptype_t)0;
6782 printer = NULL;
6783 }
6784 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
6785 {
6786 dest = NULL;
6787 dtype = (cups_ptype_t)0;
6788 dmask = CUPS_PRINTER_CLASS;
6789 printer = NULL;
6790 }
6791 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
6792 {
6793 dest = NULL;
6794 dtype = CUPS_PRINTER_CLASS;
6795 dmask = CUPS_PRINTER_CLASS;
6796 printer = NULL;
6797 }
6798 else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype,
6799 &printer)) == NULL)
6800 {
6801 /*
6802 * Bad URI...
6803 */
6804
6805 send_ipp_status(con, IPP_NOT_FOUND,
6806 _("The printer or class was not found."));
6807 return;
6808 }
6809 else
6810 {
6811 dtype &= CUPS_PRINTER_CLASS;
6812 dmask = CUPS_PRINTER_CLASS;
6813 }
6814
6815 /*
6816 * Check policy...
6817 */
6818
6819 if (printer)
6820 {
6821 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
6822 NULL)) != HTTP_OK)
6823 {
6824 send_http_error(con, status, printer);
6825 return;
6826 }
6827 }
6828 else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6829 {
6830 send_http_error(con, status, NULL);
6831 return;
6832 }
6833
6834 /*
6835 * See if the "which-jobs" attribute have been specified...
6836 */
6837
6838 if ((attr = ippFindAttribute(con->request, "which-jobs",
6839 IPP_TAG_KEYWORD)) != NULL &&
6840 !strcmp(attr->values[0].string.text, "completed"))
6841 {
6842 completed = 1;
6843 list = Jobs;
6844 }
6845 else if (attr && !strcmp(attr->values[0].string.text, "all"))
6846 {
6847 completed = 0;
6848 list = Jobs;
6849 }
6850 else if (attr && !strcmp(attr->values[0].string.text, "printing"))
6851 {
6852 completed = 0;
6853 list = PrintingJobs;
6854 }
6855 else
6856 {
6857 completed = 0;
6858 list = ActiveJobs;
6859 }
6860
6861 /*
6862 * See if they want to limit the number of jobs reported...
6863 */
6864
6865 if ((attr = ippFindAttribute(con->request, "limit",
6866 IPP_TAG_INTEGER)) != NULL)
6867 limit = attr->values[0].integer;
6868 else
6869 limit = 0;
6870
6871 if ((attr = ippFindAttribute(con->request, "first-job-id",
6872 IPP_TAG_INTEGER)) != NULL)
6873 first_job_id = attr->values[0].integer;
6874 else
6875 first_job_id = 1;
6876
6877 /*
6878 * See if we only want to see jobs for a specific user...
6879 */
6880
6881 if ((attr = ippFindAttribute(con->request, "my-jobs",
6882 IPP_TAG_BOOLEAN)) != NULL &&
6883 attr->values[0].boolean)
6884 strlcpy(username, get_username(con), sizeof(username));
6885 else
6886 username[0] = '\0';
6887
6888 ra = create_requested_array(con->request);
6889
6890 /*
6891 * OK, build a list of jobs for this printer...
6892 */
6893
6894 for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list);
6895 (limit <= 0 || count < limit) && job;
6896 job = (cupsd_job_t *)cupsArrayNext(list))
6897 {
6898 /*
6899 * Filter out jobs that don't match...
6900 */
6901
6902 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: job->id = %d", job->id);
6903
6904 if (!job->dest || !job->username)
6905 cupsdLoadJob(job);
6906
6907 if (!job->dest || !job->username)
6908 continue;
6909
6910 if ((dest && strcmp(job->dest, dest)) &&
6911 (!job->printer || !dest || strcmp(job->printer->name, dest)))
6912 continue;
6913 if ((job->dtype & dmask) != dtype &&
6914 (!job->printer || (job->printer->type & dmask) != dtype))
6915 continue;
6916 if (completed && job->state_value <= IPP_JOB_STOPPED)
6917 continue;
6918
6919 if (job->id < first_job_id)
6920 continue;
6921
6922 cupsdLoadJob(job);
6923
6924 if (!job->attrs)
6925 continue;
6926
6927 if (username[0] && strcasecmp(username, job->username))
6928 continue;
6929
6930 if (count > 0)
6931 ippAddSeparator(con->response);
6932
6933 count ++;
6934
6935 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count = %d", count);
6936
6937 copy_job_attrs(con, job, ra);
6938 }
6939
6940 cupsArrayDelete(ra);
6941
6942 con->response->request.status.status_code = IPP_OK;
6943 }
6944
6945
6946 /*
6947 * 'get_notifications()' - Get events for a subscription.
6948 */
6949
6950 static void
6951 get_notifications(cupsd_client_t *con) /* I - Client connection */
6952 {
6953 int i, j; /* Looping vars */
6954 http_status_t status; /* Policy status */
6955 cupsd_subscription_t *sub; /* Subscription */
6956 ipp_attribute_t *ids, /* notify-subscription-ids */
6957 *sequences; /* notify-sequence-numbers */
6958 int min_seq; /* Minimum sequence number */
6959 int interval; /* Poll interval */
6960
6961
6962 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])",
6963 con, con->http.fd);
6964
6965 /*
6966 * Get subscription attributes...
6967 */
6968
6969 ids = ippFindAttribute(con->request, "notify-subscription-ids",
6970 IPP_TAG_INTEGER);
6971 sequences = ippFindAttribute(con->request, "notify-sequence-numbers",
6972 IPP_TAG_INTEGER);
6973
6974 if (!ids)
6975 {
6976 send_ipp_status(con, IPP_BAD_REQUEST,
6977 _("Missing notify-subscription-ids attribute!"));
6978 return;
6979 }
6980
6981 /*
6982 * Are the subscription IDs valid?
6983 */
6984
6985 for (i = 0, interval = 60; i < ids->num_values; i ++)
6986 {
6987 if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL)
6988 {
6989 /*
6990 * Bad subscription ID...
6991 */
6992
6993 send_ipp_status(con, IPP_NOT_FOUND,
6994 _("notify-subscription-id %d no good!"),
6995 ids->values[i].integer);
6996 return;
6997 }
6998
6999 /*
7000 * Check policy...
7001 */
7002
7003 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
7004 DefaultPolicyPtr,
7005 con, sub->owner)) != HTTP_OK)
7006 {
7007 send_http_error(con, status, sub->dest);
7008 return;
7009 }
7010
7011 /*
7012 * Check the subscription type and update the interval accordingly.
7013 */
7014
7015 if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING &&
7016 interval > 10)
7017 interval = 10;
7018 else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED)
7019 interval = 0;
7020 else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING &&
7021 interval > 30)
7022 interval = 30;
7023 }
7024
7025 /*
7026 * Tell the client to poll again in N seconds...
7027 */
7028
7029 if (interval > 0)
7030 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
7031 "notify-get-interval", interval);
7032
7033 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
7034 "printer-up-time", time(NULL));
7035
7036 /*
7037 * Copy the subscription event attributes to the response.
7038 */
7039
7040 con->response->request.status.status_code =
7041 interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE;
7042
7043 for (i = 0; i < ids->num_values; i ++)
7044 {
7045 /*
7046 * Get the subscription and sequence number...
7047 */
7048
7049 sub = cupsdFindSubscription(ids->values[i].integer);
7050
7051 if (sequences && i < sequences->num_values)
7052 min_seq = sequences->values[i].integer;
7053 else
7054 min_seq = 1;
7055
7056 /*
7057 * If we don't have any new events, nothing to do here...
7058 */
7059
7060 if (min_seq > (sub->first_event_id + sub->num_events))
7061 continue;
7062
7063 /*
7064 * Otherwise copy all of the new events...
7065 */
7066
7067 if (sub->first_event_id > min_seq)
7068 j = 0;
7069 else
7070 j = min_seq - sub->first_event_id;
7071
7072 for (; j < sub->num_events; j ++)
7073 {
7074 ippAddSeparator(con->response);
7075
7076 copy_attrs(con->response, sub->events[j]->attrs, NULL,
7077 IPP_TAG_EVENT_NOTIFICATION, 0);
7078 }
7079 }
7080 }
7081
7082
7083 /*
7084 * 'get_ppd()' - Get a named PPD from the local system.
7085 */
7086
7087 static void
7088 get_ppd(cupsd_client_t *con, /* I - Client connection */
7089 ipp_attribute_t *uri) /* I - Printer URI or PPD name */
7090 {
7091 http_status_t status; /* Policy status */
7092 cupsd_printer_t *dest; /* Destination */
7093 cups_ptype_t dtype; /* Destination type */
7094
7095
7096 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con,
7097 con->http.fd, uri, uri->name, uri->values[0].string.text);
7098
7099 if (!strcmp(uri->name, "ppd-name"))
7100 {
7101 /*
7102 * Return a PPD file from cups-driverd...
7103 */
7104
7105 char command[1024], /* cups-driverd command */
7106 options[1024], /* Options to pass to command */
7107 ppd_name[1024]; /* ppd-name */
7108
7109
7110 /*
7111 * Check policy...
7112 */
7113
7114 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7115 {
7116 send_http_error(con, status, NULL);
7117 return;
7118 }
7119
7120 /*
7121 * Run cups-driverd command with the given options...
7122 */
7123
7124 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
7125 url_encode_string(uri->values[0].string.text, ppd_name, sizeof(ppd_name));
7126 snprintf(options, sizeof(options), "get+%d+%s",
7127 con->request->request.op.request_id, ppd_name);
7128
7129 if (cupsdSendCommand(con, command, options, 0))
7130 {
7131 /*
7132 * Command started successfully, don't send an IPP response here...
7133 */
7134
7135 ippDelete(con->response);
7136 con->response = NULL;
7137 }
7138 else
7139 {
7140 /*
7141 * Command failed, return "internal error" so the user knows something
7142 * went wrong...
7143 */
7144
7145 send_ipp_status(con, IPP_INTERNAL_ERROR,
7146 _("cups-driverd failed to execute."));
7147 }
7148 }
7149 else if (!strcmp(uri->name, "printer-uri") &&
7150 cupsdValidateDest(uri->values[0].string.text, &dtype, &dest))
7151 {
7152 int i; /* Looping var */
7153 char filename[1024]; /* PPD filename */
7154
7155
7156 /*
7157 * Check policy...
7158 */
7159
7160 if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK)
7161 {
7162 send_http_error(con, status, dest);
7163 return;
7164 }
7165
7166 /*
7167 * See if we need the PPD for a class or remote printer...
7168 */
7169
7170 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
7171 dest->name);
7172
7173 if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0))
7174 {
7175 con->response->request.status.status_code = CUPS_SEE_OTHER;
7176 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI,
7177 "printer-uri", NULL, dest->uri);
7178 return;
7179 }
7180 else if (dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
7181 {
7182 for (i = 0; i < dest->num_printers; i ++)
7183 if (!(dest->printers[i]->type &
7184 (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)))
7185 {
7186 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
7187 dest->printers[i]->name);
7188
7189 if (!access(filename, 0))
7190 break;
7191 }
7192
7193 if (i < dest->num_printers)
7194 dest = dest->printers[i];
7195 else
7196 {
7197 con->response->request.status.status_code = CUPS_SEE_OTHER;
7198 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI,
7199 "printer-uri", NULL, dest->printers[0]->uri);
7200 return;
7201 }
7202 }
7203
7204 /*
7205 * Found the printer with the PPD file, now see if there is one...
7206 */
7207
7208 if ((con->file = open(filename, O_RDONLY)) < 0)
7209 {
7210 send_ipp_status(con, IPP_NOT_FOUND,
7211 _("The PPD file \"%s\" could not be opened: %s"),
7212 uri->values[0].string.text, strerror(errno));
7213 return;
7214 }
7215
7216 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
7217
7218 con->pipe_pid = 0;
7219
7220 con->response->request.status.status_code = IPP_OK;
7221 }
7222 else
7223 send_ipp_status(con, IPP_NOT_FOUND,
7224 _("The PPD file \"%s\" could not be found."),
7225 uri->values[0].string.text);
7226 }
7227
7228
7229 /*
7230 * 'get_ppds()' - Get the list of PPD files on the local system.
7231 */
7232
7233 static void
7234 get_ppds(cupsd_client_t *con) /* I - Client connection */
7235 {
7236 http_status_t status; /* Policy status */
7237 ipp_attribute_t *limit, /* Limit attribute */
7238 *device, /* ppd-device-id attribute */
7239 *language, /* ppd-natural-language attribute */
7240 *make, /* ppd-make attribute */
7241 *model, /* ppd-make-and-model attribute */
7242 *model_number, /* ppd-model-number attribute */
7243 *product, /* ppd-product attribute */
7244 *psversion, /* ppd-psverion attribute */
7245 *type, /* ppd-type attribute */
7246 *requested; /* requested-attributes attribute */
7247 char command[1024], /* cups-driverd command */
7248 options[1024], /* Options to pass to command */
7249 device_str[256],/* Escaped ppd-device-id string */
7250 language_str[256],
7251 /* Escaped ppd-natural-language */
7252 make_str[256], /* Escaped ppd-make string */
7253 model_str[256], /* Escaped ppd-make-and-model string */
7254 model_number_str[256],
7255 /* ppd-model-number string */
7256 product_str[256],
7257 /* Escaped ppd-product string */
7258 psversion_str[256],
7259 /* Escaped ppd-psversion string */
7260 type_str[256], /* Escaped ppd-type string */
7261 requested_str[256];
7262 /* String for requested attributes */
7263
7264
7265 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->http.fd);
7266
7267 /*
7268 * Check policy...
7269 */
7270
7271 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7272 {
7273 send_http_error(con, status, NULL);
7274 return;
7275 }
7276
7277 /*
7278 * Run cups-driverd command with the given options...
7279 */
7280
7281 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
7282 device = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT);
7283 language = ippFindAttribute(con->request, "ppd-natural-language",
7284 IPP_TAG_LANGUAGE);
7285 make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT);
7286 model = ippFindAttribute(con->request, "ppd-make-and-model",
7287 IPP_TAG_TEXT);
7288 model_number = ippFindAttribute(con->request, "ppd-model-number",
7289 IPP_TAG_INTEGER);
7290 product = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT);
7291 psversion = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT);
7292 type = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD);
7293 requested = ippFindAttribute(con->request, "requested-attributes",
7294 IPP_TAG_KEYWORD);
7295
7296 if (requested)
7297 url_encode_attr(requested, requested_str, sizeof(requested_str));
7298 else
7299 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
7300
7301 if (device)
7302 url_encode_attr(device, device_str, sizeof(device_str));
7303 else
7304 device_str[0] = '\0';
7305
7306 if (language)
7307 url_encode_attr(language, language_str, sizeof(language_str));
7308 else
7309 language_str[0] = '\0';
7310
7311 if (make)
7312 url_encode_attr(make, make_str, sizeof(make_str));
7313 else
7314 make_str[0] = '\0';
7315
7316 if (model)
7317 url_encode_attr(model, model_str, sizeof(model_str));
7318 else
7319 model_str[0] = '\0';
7320
7321 if (model_number)
7322 snprintf(model_number_str, sizeof(model_number_str), "ppd-model-number=%d",
7323 model_number->values[0].integer);
7324 else
7325 model_number_str[0] = '\0';
7326
7327 if (product)
7328 url_encode_attr(product, product_str, sizeof(product_str));
7329 else
7330 product_str[0] = '\0';
7331
7332 if (psversion)
7333 url_encode_attr(psversion, psversion_str, sizeof(psversion_str));
7334 else
7335 psversion_str[0] = '\0';
7336
7337 if (type)
7338 url_encode_attr(type, type_str, sizeof(type_str));
7339 else
7340 type_str[0] = '\0';
7341
7342 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
7343 snprintf(options, sizeof(options),
7344 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7345 con->request->request.op.request_id,
7346 limit ? limit->values[0].integer : 0,
7347 requested_str,
7348 device ? "%20" : "", device_str,
7349 language ? "%20" : "", language_str,
7350 make ? "%20" : "", make_str,
7351 model ? "%20" : "", model_str,
7352 model_number ? "%20" : "", model_number_str,
7353 product ? "%20" : "", product_str,
7354 psversion ? "%20" : "", psversion_str,
7355 type ? "%20" : "", type_str);
7356
7357 if (cupsdSendCommand(con, command, options, 0))
7358 {
7359 /*
7360 * Command started successfully, don't send an IPP response here...
7361 */
7362
7363 ippDelete(con->response);
7364 con->response = NULL;
7365 }
7366 else
7367 {
7368 /*
7369 * Command failed, return "internal error" so the user knows something
7370 * went wrong...
7371 */
7372
7373 send_ipp_status(con, IPP_INTERNAL_ERROR,
7374 _("cups-driverd failed to execute."));
7375 }
7376 }
7377
7378
7379 /*
7380 * 'get_printer_attrs()' - Get printer attributes.
7381 */
7382
7383 static void
7384 get_printer_attrs(cupsd_client_t *con, /* I - Client connection */
7385 ipp_attribute_t *uri) /* I - Printer URI */
7386 {
7387 http_status_t status; /* Policy status */
7388 cups_ptype_t dtype; /* Destination type (printer/class) */
7389 cupsd_printer_t *printer; /* Printer/class */
7390 cups_array_t *ra; /* Requested attributes array */
7391
7392
7393 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con,
7394 con->http.fd, uri->values[0].string.text);
7395
7396 /*
7397 * Is the destination valid?
7398 */
7399
7400 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7401 {
7402 /*
7403 * Bad URI...
7404 */
7405
7406 send_ipp_status(con, IPP_NOT_FOUND,
7407 _("The printer or class was not found."));
7408 return;
7409 }
7410
7411 /*
7412 * Check policy...
7413 */
7414
7415 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
7416 {
7417 send_http_error(con, status, printer);
7418 return;
7419 }
7420
7421 /*
7422 * Send the attributes...
7423 */
7424
7425 ra = create_requested_array(con->request);
7426
7427 copy_printer_attrs(con, printer, ra);
7428
7429 cupsArrayDelete(ra);
7430
7431 con->response->request.status.status_code = IPP_OK;
7432 }
7433
7434
7435 /*
7436 * 'get_printers()' - Get a list of printers or classes.
7437 */
7438
7439 static void
7440 get_printers(cupsd_client_t *con, /* I - Client connection */
7441 int type) /* I - 0 or CUPS_PRINTER_CLASS */
7442 {
7443 http_status_t status; /* Policy status */
7444 ipp_attribute_t *attr; /* Current attribute */
7445 int limit; /* Max number of printers to return */
7446 int count; /* Number of printers that match */
7447 cupsd_printer_t *printer; /* Current printer pointer */
7448 int printer_type, /* printer-type attribute */
7449 printer_mask; /* printer-type-mask attribute */
7450 char *location; /* Location string */
7451 const char *username; /* Current user */
7452 char *first_printer_name; /* first-printer-name attribute */
7453 cups_array_t *ra; /* Requested attributes array */
7454
7455
7456 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con,
7457 con->http.fd, type);
7458
7459 /*
7460 * Check policy...
7461 */
7462
7463 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7464 {
7465 send_http_error(con, status, NULL);
7466 return;
7467 }
7468
7469 /*
7470 * Check for printers...
7471 */
7472
7473 if (!Printers || !cupsArrayCount(Printers))
7474 {
7475 send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added."));
7476 return;
7477 }
7478
7479 /*
7480 * See if they want to limit the number of printers reported...
7481 */
7482
7483 if ((attr = ippFindAttribute(con->request, "limit",
7484 IPP_TAG_INTEGER)) != NULL)
7485 limit = attr->values[0].integer;
7486 else
7487 limit = 10000000;
7488
7489 if ((attr = ippFindAttribute(con->request, "first-printer-name",
7490 IPP_TAG_NAME)) != NULL)
7491 first_printer_name = attr->values[0].string.text;
7492 else
7493 first_printer_name = NULL;
7494
7495 /*
7496 * Support filtering...
7497 */
7498
7499 if ((attr = ippFindAttribute(con->request, "printer-type",
7500 IPP_TAG_ENUM)) != NULL)
7501 printer_type = attr->values[0].integer;
7502 else
7503 printer_type = 0;
7504
7505 if ((attr = ippFindAttribute(con->request, "printer-type-mask",
7506 IPP_TAG_ENUM)) != NULL)
7507 printer_mask = attr->values[0].integer;
7508 else
7509 printer_mask = 0;
7510
7511 if ((attr = ippFindAttribute(con->request, "printer-location",
7512 IPP_TAG_TEXT)) != NULL)
7513 location = attr->values[0].string.text;
7514 else
7515 location = NULL;
7516
7517 if (con->username[0])
7518 username = con->username;
7519 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7520 IPP_TAG_NAME)) != NULL)
7521 username = attr->values[0].string.text;
7522 else
7523 username = NULL;
7524
7525 ra = create_requested_array(con->request);
7526
7527 /*
7528 * OK, build a list of printers for this printer...
7529 */
7530
7531 if (first_printer_name)
7532 {
7533 if ((printer = cupsdFindDest(first_printer_name)) == NULL)
7534 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
7535 }
7536 else
7537 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
7538
7539 for (count = 0;
7540 count < limit && printer;
7541 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
7542 {
7543 if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) &&
7544 (printer->type & printer_mask) == printer_type &&
7545 (!location ||
7546 (printer->location && !strcasecmp(printer->location, location))))
7547 {
7548 /*
7549 * If HideImplicitMembers is enabled, see if this printer or class
7550 * is a member of an implicit class...
7551 */
7552
7553 if (ImplicitClasses && HideImplicitMembers &&
7554 printer->in_implicit_class)
7555 continue;
7556
7557 /*
7558 * If a username is specified, see if it is allowed or denied
7559 * access...
7560 */
7561
7562 if (!(printer->type & CUPS_PRINTER_AUTHENTICATED) &&
7563 printer->num_users && username && !user_allowed(printer, username))
7564 continue;
7565
7566 /*
7567 * Add the group separator as needed...
7568 */
7569
7570 if (count > 0)
7571 ippAddSeparator(con->response);
7572
7573 count ++;
7574
7575 /*
7576 * Send the attributes...
7577 */
7578
7579 copy_printer_attrs(con, printer, ra);
7580 }
7581 }
7582
7583 cupsArrayDelete(ra);
7584
7585 con->response->request.status.status_code = IPP_OK;
7586 }
7587
7588
7589 /*
7590 * 'get_subscription_attrs()' - Get subscription attributes.
7591 */
7592
7593 static void
7594 get_subscription_attrs(
7595 cupsd_client_t *con, /* I - Client connection */
7596 int sub_id) /* I - Subscription ID */
7597 {
7598 http_status_t status; /* Policy status */
7599 cupsd_subscription_t *sub; /* Subscription */
7600 cups_array_t *ra; /* Requested attributes array */
7601
7602
7603 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7604 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7605 con, con->http.fd, sub_id);
7606
7607 /*
7608 * Is the subscription ID valid?
7609 */
7610
7611 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
7612 {
7613 /*
7614 * Bad subscription ID...
7615 */
7616
7617 send_ipp_status(con, IPP_NOT_FOUND,
7618 _("notify-subscription-id %d no good!"), sub_id);
7619 return;
7620 }
7621
7622 /*
7623 * Check policy...
7624 */
7625
7626 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
7627 DefaultPolicyPtr,
7628 con, sub->owner)) != HTTP_OK)
7629 {
7630 send_http_error(con, status, sub->dest);
7631 return;
7632 }
7633
7634 /*
7635 * Copy the subscription attributes to the response using the
7636 * requested-attributes attribute that may be provided by the client.
7637 */
7638
7639 ra = create_requested_array(con->request);
7640
7641 copy_subscription_attrs(con, sub, ra);
7642
7643 cupsArrayDelete(ra);
7644
7645 con->response->request.status.status_code = IPP_OK;
7646 }
7647
7648
7649 /*
7650 * 'get_subscriptions()' - Get subscriptions.
7651 */
7652
7653 static void
7654 get_subscriptions(cupsd_client_t *con, /* I - Client connection */
7655 ipp_attribute_t *uri) /* I - Printer/job URI */
7656 {
7657 http_status_t status; /* Policy status */
7658 int count; /* Number of subscriptions */
7659 int limit; /* Limit */
7660 cupsd_subscription_t *sub; /* Subscription */
7661 cups_array_t *ra; /* Requested attributes array */
7662 ipp_attribute_t *attr; /* Attribute */
7663 cups_ptype_t dtype; /* Destination type (printer/class) */
7664 char scheme[HTTP_MAX_URI],
7665 /* Scheme portion of URI */
7666 username[HTTP_MAX_URI],
7667 /* Username portion of URI */
7668 host[HTTP_MAX_URI],
7669 /* Host portion of URI */
7670 resource[HTTP_MAX_URI];
7671 /* Resource portion of URI */
7672 int port; /* Port portion of URI */
7673 cupsd_job_t *job; /* Job pointer */
7674 cupsd_printer_t *printer; /* Printer */
7675
7676
7677 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7678 "get_subscriptions(con=%p[%d], uri=%s)",
7679 con, con->http.fd, uri->values[0].string.text);
7680
7681 /*
7682 * Is the destination valid?
7683 */
7684
7685 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7686 sizeof(scheme), username, sizeof(username), host,
7687 sizeof(host), &port, resource, sizeof(resource));
7688
7689 if (!strcmp(resource, "/") ||
7690 (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) ||
7691 (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) ||
7692 (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9))
7693 {
7694 printer = NULL;
7695 job = NULL;
7696 }
7697 else if (!strncmp(resource, "/jobs/", 6) && resource[6])
7698 {
7699 printer = NULL;
7700 job = cupsdFindJob(atoi(resource + 6));
7701
7702 if (!job)
7703 {
7704 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%s does not exist!"),
7705 resource + 6);
7706 return;
7707 }
7708 }
7709 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7710 {
7711 /*
7712 * Bad URI...
7713 */
7714
7715 send_ipp_status(con, IPP_NOT_FOUND,
7716 _("The printer or class was not found."));
7717 return;
7718 }
7719 else if ((attr = ippFindAttribute(con->request, "notify-job-id",
7720 IPP_TAG_INTEGER)) != NULL)
7721 {
7722 job = cupsdFindJob(attr->values[0].integer);
7723
7724 if (!job)
7725 {
7726 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"),
7727 attr->values[0].integer);
7728 return;
7729 }
7730 }
7731 else
7732 job = NULL;
7733
7734 /*
7735 * Check policy...
7736 */
7737
7738 if ((status = cupsdCheckPolicy(printer ? printer->op_policy_ptr :
7739 DefaultPolicyPtr,
7740 con, NULL)) != HTTP_OK)
7741 {
7742 send_http_error(con, status, printer);
7743 return;
7744 }
7745
7746 /*
7747 * Copy the subscription attributes to the response using the
7748 * requested-attributes attribute that may be provided by the client.
7749 */
7750
7751 ra = create_requested_array(con->request);
7752
7753 if ((attr = ippFindAttribute(con->request, "limit",
7754 IPP_TAG_INTEGER)) != NULL)
7755 limit = attr->values[0].integer;
7756 else
7757 limit = 0;
7758
7759 /*
7760 * See if we only want to see subscriptions for a specific user...
7761 */
7762
7763 if ((attr = ippFindAttribute(con->request, "my-subscriptions",
7764 IPP_TAG_BOOLEAN)) != NULL &&
7765 attr->values[0].boolean)
7766 strlcpy(username, get_username(con), sizeof(username));
7767 else
7768 username[0] = '\0';
7769
7770 for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0;
7771 sub;
7772 sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
7773 if ((!printer || sub->dest == printer) && (!job || sub->job == job) &&
7774 (!username[0] || !strcasecmp(username, sub->owner)))
7775 {
7776 ippAddSeparator(con->response);
7777 copy_subscription_attrs(con, sub, ra);
7778
7779 count ++;
7780 if (limit && count >= limit)
7781 break;
7782 }
7783
7784 cupsArrayDelete(ra);
7785
7786 if (count)
7787 con->response->request.status.status_code = IPP_OK;
7788 else
7789 send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found."));
7790 }
7791
7792
7793 /*
7794 * 'get_username()' - Get the username associated with a request.
7795 */
7796
7797 static const char * /* O - Username */
7798 get_username(cupsd_client_t *con) /* I - Connection */
7799 {
7800 ipp_attribute_t *attr; /* Attribute */
7801
7802
7803 if (con->username[0])
7804 return (con->username);
7805 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7806 IPP_TAG_NAME)) != NULL)
7807 return (attr->values[0].string.text);
7808 else
7809 return ("anonymous");
7810 }
7811
7812
7813 /*
7814 * 'hold_job()' - Hold a print job.
7815 */
7816
7817 static void
7818 hold_job(cupsd_client_t *con, /* I - Client connection */
7819 ipp_attribute_t *uri) /* I - Job or Printer URI */
7820 {
7821 ipp_attribute_t *attr, /* Current job-hold-until */
7822 *newattr; /* New job-hold-until */
7823 int jobid; /* Job ID */
7824 char method[HTTP_MAX_URI], /* Method portion of URI */
7825 username[HTTP_MAX_URI], /* Username portion of URI */
7826 host[HTTP_MAX_URI], /* Host portion of URI */
7827 resource[HTTP_MAX_URI]; /* Resource portion of URI */
7828 int port; /* Port portion of URI */
7829 cupsd_job_t *job; /* Job information */
7830
7831
7832 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->http.fd,
7833 uri->values[0].string.text);
7834
7835 /*
7836 * See if we have a job URI or a printer URI...
7837 */
7838
7839 if (!strcmp(uri->name, "printer-uri"))
7840 {
7841 /*
7842 * Got a printer URI; see if we also have a job-id attribute...
7843 */
7844
7845 if ((attr = ippFindAttribute(con->request, "job-id",
7846 IPP_TAG_INTEGER)) == NULL)
7847 {
7848 send_ipp_status(con, IPP_BAD_REQUEST,
7849 _("Got a printer-uri attribute but no job-id!"));
7850 return;
7851 }
7852
7853 jobid = attr->values[0].integer;
7854 }
7855 else
7856 {
7857 /*
7858 * Got a job URI; parse it to get the job ID...
7859 */
7860
7861 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
7862 sizeof(method), username, sizeof(username), host,
7863 sizeof(host), &port, resource, sizeof(resource));
7864
7865 if (strncmp(resource, "/jobs/", 6))
7866 {
7867 /*
7868 * Not a valid URI!
7869 */
7870
7871 send_ipp_status(con, IPP_BAD_REQUEST,
7872 _("Bad job-uri attribute \"%s\"!"),
7873 uri->values[0].string.text);
7874 return;
7875 }
7876
7877 jobid = atoi(resource + 6);
7878 }
7879
7880 /*
7881 * See if the job exists...
7882 */
7883
7884 if ((job = cupsdFindJob(jobid)) == NULL)
7885 {
7886 /*
7887 * Nope - return a "not found" error...
7888 */
7889
7890 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
7891 return;
7892 }
7893
7894 /*
7895 * See if the job is owned by the requesting user...
7896 */
7897
7898 if (!validate_user(job, con, job->username, username, sizeof(username)))
7899 {
7900 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
7901 return;
7902 }
7903
7904 /*
7905 * Hold the job and return...
7906 */
7907
7908 cupsdHoldJob(job);
7909
7910 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
7911 "Job held by user.");
7912
7913 if ((newattr = ippFindAttribute(con->request, "job-hold-until",
7914 IPP_TAG_KEYWORD)) == NULL)
7915 newattr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
7916
7917 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
7918 IPP_TAG_KEYWORD)) == NULL)
7919 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
7920
7921 if (attr)
7922 {
7923 /*
7924 * Free the old hold value and copy the new one over...
7925 */
7926
7927 _cupsStrFree(attr->values[0].string.text);
7928
7929 if (newattr)
7930 {
7931 attr->value_tag = newattr->value_tag;
7932 attr->values[0].string.text =
7933 _cupsStrAlloc(newattr->values[0].string.text);
7934 }
7935 else
7936 {
7937 attr->value_tag = IPP_TAG_KEYWORD;
7938 attr->values[0].string.text = _cupsStrAlloc("indefinite");
7939 }
7940
7941 /*
7942 * Hold job until specified time...
7943 */
7944
7945 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
7946
7947 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
7948 "Job job-hold-until value changed by user.");
7949 }
7950
7951 cupsdLogJob(job, CUPSD_LOG_INFO, "Held by \"%s\".", username);
7952
7953 con->response->request.status.status_code = IPP_OK;
7954 }
7955
7956
7957 /*
7958 * 'move_job()' - Move a job to a new destination.
7959 */
7960
7961 static void
7962 move_job(cupsd_client_t *con, /* I - Client connection */
7963 ipp_attribute_t *uri) /* I - Job URI */
7964 {
7965 http_status_t status; /* Policy status */
7966 ipp_attribute_t *attr; /* Current attribute */
7967 int jobid; /* Job ID */
7968 cupsd_job_t *job; /* Current job */
7969 const char *src; /* Source printer/class */
7970 cups_ptype_t stype, /* Source type (printer or class) */
7971 dtype; /* Destination type (printer/class) */
7972 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
7973 username[HTTP_MAX_URI], /* Username portion of URI */
7974 host[HTTP_MAX_URI], /* Host portion of URI */
7975 resource[HTTP_MAX_URI]; /* Resource portion of URI */
7976 int port; /* Port portion of URI */
7977 cupsd_printer_t *sprinter, /* Source printer */
7978 *dprinter; /* Destination printer */
7979
7980
7981 cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd,
7982 uri->values[0].string.text);
7983
7984 /*
7985 * Get the new printer or class...
7986 */
7987
7988 if ((attr = ippFindAttribute(con->request, "job-printer-uri",
7989 IPP_TAG_URI)) == NULL)
7990 {
7991 /*
7992 * Need job-printer-uri...
7993 */
7994
7995 send_ipp_status(con, IPP_BAD_REQUEST,
7996 _("job-printer-uri attribute missing!"));
7997 return;
7998 }
7999
8000 if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter))
8001 {
8002 /*
8003 * Bad URI...
8004 */
8005
8006 send_ipp_status(con, IPP_NOT_FOUND,
8007 _("The printer or class was not found."));
8008 return;
8009 }
8010
8011 /*
8012 * See if we have a job URI or a printer URI...
8013 */
8014
8015 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
8016 sizeof(scheme), username, sizeof(username), host,
8017 sizeof(host), &port, resource, sizeof(resource));
8018
8019 if (!strcmp(uri->name, "printer-uri"))
8020 {
8021 /*
8022 * Got a printer URI; see if we also have a job-id attribute...
8023 */
8024
8025 if ((attr = ippFindAttribute(con->request, "job-id",
8026 IPP_TAG_INTEGER)) == NULL)
8027 {
8028 /*
8029 * Move all jobs...
8030 */
8031
8032 if ((src = cupsdValidateDest(uri->values[0].string.text, &stype,
8033 &sprinter)) == NULL)
8034 {
8035 /*
8036 * Bad URI...
8037 */
8038
8039 send_ipp_status(con, IPP_NOT_FOUND,
8040 _("The printer or class was not found."));
8041 return;
8042 }
8043
8044 job = NULL;
8045 }
8046 else
8047 {
8048 /*
8049 * Otherwise, just move a single job...
8050 */
8051
8052 if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
8053 {
8054 /*
8055 * Nope - return a "not found" error...
8056 */
8057
8058 send_ipp_status(con, IPP_NOT_FOUND,
8059 _("Job #%d does not exist!"), attr->values[0].integer);
8060 return;
8061 }
8062 else
8063 {
8064 /*
8065 * Job found, initialize source pointers...
8066 */
8067
8068 src = NULL;
8069 sprinter = NULL;
8070 }
8071 }
8072 }
8073 else
8074 {
8075 /*
8076 * Got a job URI; parse it to get the job ID...
8077 */
8078
8079 if (strncmp(resource, "/jobs/", 6))
8080 {
8081 /*
8082 * Not a valid URI!
8083 */
8084
8085 send_ipp_status(con, IPP_BAD_REQUEST,
8086 _("Bad job-uri attribute \"%s\"!"),
8087 uri->values[0].string.text);
8088 return;
8089 }
8090
8091 /*
8092 * See if the job exists...
8093 */
8094
8095 jobid = atoi(resource + 6);
8096
8097 if ((job = cupsdFindJob(jobid)) == NULL)
8098 {
8099 /*
8100 * Nope - return a "not found" error...
8101 */
8102
8103 send_ipp_status(con, IPP_NOT_FOUND,
8104 _("Job #%d does not exist!"), jobid);
8105 return;
8106 }
8107 else
8108 {
8109 /*
8110 * Job found, initialize source pointers...
8111 */
8112
8113 src = NULL;
8114 sprinter = NULL;
8115 }
8116 }
8117
8118 /*
8119 * Check the policy of the destination printer...
8120 */
8121
8122 if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con,
8123 job ? job->username : NULL)) != HTTP_OK)
8124 {
8125 send_http_error(con, status, dprinter);
8126 return;
8127 }
8128
8129 /*
8130 * Now move the job or jobs...
8131 */
8132
8133 if (job)
8134 {
8135 /*
8136 * See if the job has been completed...
8137 */
8138
8139 if (job->state_value > IPP_JOB_STOPPED)
8140 {
8141 /*
8142 * Return a "not-possible" error...
8143 */
8144
8145 send_ipp_status(con, IPP_NOT_POSSIBLE,
8146 _("Job #%d is finished and cannot be altered!"),
8147 job->id);
8148 return;
8149 }
8150
8151 /*
8152 * See if the job is owned by the requesting user...
8153 */
8154
8155 if (!validate_user(job, con, job->username, username, sizeof(username)))
8156 {
8157 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
8158 return;
8159 }
8160
8161 /*
8162 * Move the job to a different printer or class...
8163 */
8164
8165 cupsdMoveJob(job, dprinter);
8166 }
8167 else
8168 {
8169 /*
8170 * Got the source printer, now look through the jobs...
8171 */
8172
8173 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
8174 job;
8175 job = (cupsd_job_t *)cupsArrayNext(Jobs))
8176 {
8177 /*
8178 * See if the job is pointing at the source printer or has not been
8179 * completed...
8180 */
8181
8182 if (strcasecmp(job->dest, src) ||
8183 job->state_value > IPP_JOB_STOPPED)
8184 continue;
8185
8186 /*
8187 * See if the job can be moved by the requesting user...
8188 */
8189
8190 if (!validate_user(job, con, job->username, username, sizeof(username)))
8191 continue;
8192
8193 /*
8194 * Move the job to a different printer or class...
8195 */
8196
8197 cupsdMoveJob(job, dprinter);
8198 }
8199 }
8200
8201 /*
8202 * Start jobs if possible...
8203 */
8204
8205 cupsdCheckJobs();
8206
8207 /*
8208 * Return with "everything is OK" status...
8209 */
8210
8211 con->response->request.status.status_code = IPP_OK;
8212 }
8213
8214
8215 /*
8216 * 'ppd_parse_line()' - Parse a PPD default line.
8217 */
8218
8219 static int /* O - 0 on success, -1 on failure */
8220 ppd_parse_line(const char *line, /* I - Line */
8221 char *option, /* O - Option name */
8222 int olen, /* I - Size of option name */
8223 char *choice, /* O - Choice name */
8224 int clen) /* I - Size of choice name */
8225 {
8226 /*
8227 * Verify this is a default option line...
8228 */
8229
8230 if (strncmp(line, "*Default", 8))
8231 return (-1);
8232
8233 /*
8234 * Read the option name...
8235 */
8236
8237 for (line += 8, olen --; isalnum(*line & 255); line ++)
8238 if (olen > 0)
8239 {
8240 *option++ = *line;
8241 olen --;
8242 }
8243
8244 *option = '\0';
8245
8246 /*
8247 * Skip everything else up to the colon (:)...
8248 */
8249
8250 while (*line && *line != ':')
8251 line ++;
8252
8253 if (!*line)
8254 return (-1);
8255
8256 line ++;
8257
8258 /*
8259 * Now grab the option choice, skipping leading whitespace...
8260 */
8261
8262 while (isspace(*line & 255))
8263 line ++;
8264
8265 for (clen --; isalnum(*line & 255); line ++)
8266 if (clen > 0)
8267 {
8268 *choice++ = *line;
8269 clen --;
8270 }
8271
8272 *choice = '\0';
8273
8274 /*
8275 * Return with no errors...
8276 */
8277
8278 return (0);
8279 }
8280
8281
8282 /*
8283 * 'print_job()' - Print a file to a printer or class.
8284 */
8285
8286 static void
8287 print_job(cupsd_client_t *con, /* I - Client connection */
8288 ipp_attribute_t *uri) /* I - Printer URI */
8289 {
8290 ipp_attribute_t *attr; /* Current attribute */
8291 ipp_attribute_t *format; /* Document-format attribute */
8292 const char *default_format; /* document-format-default value */
8293 cupsd_job_t *job; /* New job */
8294 char filename[1024]; /* Job filename */
8295 mime_type_t *filetype; /* Type of file */
8296 char super[MIME_MAX_SUPER], /* Supertype of file */
8297 type[MIME_MAX_TYPE], /* Subtype of file */
8298 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
8299 /* Textual name of mime type */
8300 cupsd_printer_t *printer; /* Printer data */
8301 struct stat fileinfo; /* File information */
8302 int kbytes; /* Size of file */
8303 int compression; /* Document compression */
8304
8305
8306 cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd,
8307 uri->values[0].string.text);
8308
8309 /*
8310 * Validate print file attributes, for now just document-format and
8311 * compression (CUPS only supports "none" and "gzip")...
8312 */
8313
8314 compression = CUPS_FILE_NONE;
8315
8316 if ((attr = ippFindAttribute(con->request, "compression",
8317 IPP_TAG_KEYWORD)) != NULL)
8318 {
8319 if (strcmp(attr->values[0].string.text, "none")
8320 #ifdef HAVE_LIBZ
8321 && strcmp(attr->values[0].string.text, "gzip")
8322 #endif /* HAVE_LIBZ */
8323 )
8324 {
8325 send_ipp_status(con, IPP_ATTRIBUTES,
8326 _("Unsupported compression \"%s\"!"),
8327 attr->values[0].string.text);
8328 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
8329 "compression", NULL, attr->values[0].string.text);
8330 return;
8331 }
8332
8333 #ifdef HAVE_LIBZ
8334 if (!strcmp(attr->values[0].string.text, "gzip"))
8335 compression = CUPS_FILE_GZIP;
8336 #endif /* HAVE_LIBZ */
8337 }
8338
8339 /*
8340 * Do we have a file to print?
8341 */
8342
8343 if (!con->filename)
8344 {
8345 send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!"));
8346 return;
8347 }
8348
8349 /*
8350 * Is the destination valid?
8351 */
8352
8353 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
8354 {
8355 /*
8356 * Bad URI...
8357 */
8358
8359 send_ipp_status(con, IPP_NOT_FOUND,
8360 _("The printer or class was not found."));
8361 return;
8362 }
8363
8364 /*
8365 * Is it a format we support?
8366 */
8367
8368 if ((format = ippFindAttribute(con->request, "document-format",
8369 IPP_TAG_MIMETYPE)) != NULL)
8370 {
8371 /*
8372 * Grab format from client...
8373 */
8374
8375 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super,
8376 type) != 2)
8377 {
8378 send_ipp_status(con, IPP_BAD_REQUEST,
8379 _("Could not scan type \"%s\"!"),
8380 format->values[0].string.text);
8381 return;
8382 }
8383 }
8384 else if ((default_format = cupsGetOption("document-format",
8385 printer->num_options,
8386 printer->options)) != NULL)
8387 {
8388 /*
8389 * Use default document format...
8390 */
8391
8392 if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
8393 {
8394 send_ipp_status(con, IPP_BAD_REQUEST,
8395 _("Could not scan type \"%s\"!"),
8396 default_format);
8397 return;
8398 }
8399 }
8400 else
8401 {
8402 /*
8403 * Auto-type it!
8404 */
8405
8406 strcpy(super, "application");
8407 strcpy(type, "octet-stream");
8408 }
8409
8410 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
8411 {
8412 /*
8413 * Auto-type the file...
8414 */
8415
8416 ipp_attribute_t *doc_name; /* document-name attribute */
8417
8418
8419 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file...");
8420
8421 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
8422 filetype = mimeFileType(MimeDatabase, con->filename,
8423 doc_name ? doc_name->values[0].string.text : NULL,
8424 &compression);
8425
8426 if (!filetype)
8427 filetype = mimeType(MimeDatabase, super, type);
8428
8429 cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.",
8430 filetype->super, filetype->type);
8431 }
8432 else
8433 filetype = mimeType(MimeDatabase, super, type);
8434
8435 if (filetype &&
8436 (!format ||
8437 (!strcmp(super, "application") && !strcmp(type, "octet-stream"))))
8438 {
8439 /*
8440 * Replace the document-format attribute value with the auto-typed or
8441 * default one.
8442 */
8443
8444 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
8445 filetype->type);
8446
8447 if (format)
8448 {
8449 _cupsStrFree(format->values[0].string.text);
8450
8451 format->values[0].string.text = _cupsStrAlloc(mimetype);
8452 }
8453 else
8454 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
8455 "document-format", NULL, mimetype);
8456 }
8457 else if (!filetype)
8458 {
8459 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
8460 _("Unsupported format \'%s/%s\'!"), super, type);
8461 cupsdLogMessage(CUPSD_LOG_INFO,
8462 "Hint: Do you have the raw file printing rules enabled?");
8463
8464 if (format)
8465 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
8466 "document-format", NULL, format->values[0].string.text);
8467
8468 return;
8469 }
8470
8471 /*
8472 * Read any embedded job ticket info from PS files...
8473 */
8474
8475 if (!strcasecmp(filetype->super, "application") &&
8476 (!strcasecmp(filetype->type, "postscript") ||
8477 !strcasecmp(filetype->type, "pdf")))
8478 read_job_ticket(con);
8479
8480 /*
8481 * Create the job object...
8482 */
8483
8484 if ((job = add_job(con, printer, filetype)) == NULL)
8485 return;
8486
8487 /*
8488 * Update quota data...
8489 */
8490
8491 if (stat(con->filename, &fileinfo))
8492 kbytes = 0;
8493 else
8494 kbytes = (fileinfo.st_size + 1023) / 1024;
8495
8496 cupsdUpdateQuota(printer, job->username, 0, kbytes);
8497
8498 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
8499 IPP_TAG_INTEGER)) != NULL)
8500 attr->values[0].integer += kbytes;
8501
8502 /*
8503 * Add the job file...
8504 */
8505
8506 if (add_file(con, job, filetype, compression))
8507 return;
8508
8509 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
8510 job->num_files);
8511 rename(con->filename, filename);
8512 cupsdClearString(&con->filename);
8513
8514 /*
8515 * See if we need to add the ending sheet...
8516 */
8517
8518 if (cupsdTimeoutJob(job))
8519 return;
8520
8521 /*
8522 * Log and save the job...
8523 */
8524
8525 cupsdLogJob(job, CUPSD_LOG_INFO,
8526 "File of type %s/%s queued by \"%s\".",
8527 filetype->super, filetype->type, job->username);
8528 cupsdLogJob(job, CUPSD_LOG_DEBUG, "hold_until=%d", (int)job->hold_until);
8529 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
8530 job->dest, job->username);
8531
8532 /*
8533 * Start the job if possible...
8534 */
8535
8536 cupsdCheckJobs();
8537 }
8538
8539
8540 /*
8541 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8542 *
8543 * This function only gets called when printing a single PDF or PostScript
8544 * file using the Print-Job operation. It doesn't work for Create-Job +
8545 * Send-File, since the job attributes need to be set at job creation
8546 * time for banners to work. The embedded job ticket stuff is here
8547 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8548 * options and IPP attributes which otherwise would be lost.
8549 *
8550 * The format of a job ticket is simple:
8551 *
8552 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8553 *
8554 * %cupsJobTicket: attr1=value1
8555 * %cupsJobTicket: attr2=value2
8556 * ...
8557 * %cupsJobTicket: attrN=valueN
8558 *
8559 * Job ticket lines must appear immediately after the first line that
8560 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8561 * stops looking for job ticket info when it finds a line that does not begin
8562 * with "%cupsJobTicket:".
8563 *
8564 * The maximum length of a job ticket line, including the prefix, is
8565 * 255 characters to conform with the Adobe DSC.
8566 *
8567 * Read-only attributes are rejected with a notice to the error log in
8568 * case a malicious user tries anything. Since the job ticket is read
8569 * prior to attribute validation in print_job(), job ticket attributes
8570 * will go through the same validation as IPP attributes...
8571 */
8572
8573 static void
8574 read_job_ticket(cupsd_client_t *con) /* I - Client connection */
8575 {
8576 cups_file_t *fp; /* File to read from */
8577 char line[256]; /* Line data */
8578 int num_options; /* Number of options */
8579 cups_option_t *options; /* Options */
8580 ipp_t *ticket; /* New attributes */
8581 ipp_attribute_t *attr, /* Current attribute */
8582 *attr2, /* Job attribute */
8583 *prev2; /* Previous job attribute */
8584
8585
8586 /*
8587 * First open the print file...
8588 */
8589
8590 if ((fp = cupsFileOpen(con->filename, "rb")) == NULL)
8591 {
8592 cupsdLogMessage(CUPSD_LOG_ERROR,
8593 "Unable to open print file for job ticket - %s",
8594 strerror(errno));
8595 return;
8596 }
8597
8598 /*
8599 * Skip the first line...
8600 */
8601
8602 if (cupsFileGets(fp, line, sizeof(line)) == NULL)
8603 {
8604 cupsdLogMessage(CUPSD_LOG_ERROR,
8605 "Unable to read from print file for job ticket - %s",
8606 strerror(errno));
8607 cupsFileClose(fp);
8608 return;
8609 }
8610
8611 if (strncmp(line, "%!PS-Adobe-", 11) && strncmp(line, "%PDF-", 5))
8612 {
8613 /*
8614 * Not a DSC-compliant file, so no job ticket info will be available...
8615 */
8616
8617 cupsFileClose(fp);
8618 return;
8619 }
8620
8621 /*
8622 * Read job ticket info from the file...
8623 */
8624
8625 num_options = 0;
8626 options = NULL;
8627
8628 while (cupsFileGets(fp, line, sizeof(line)))
8629 {
8630 /*
8631 * Stop at the first non-ticket line...
8632 */
8633
8634 if (strncmp(line, "%cupsJobTicket:", 15))
8635 break;
8636
8637 /*
8638 * Add the options to the option array...
8639 */
8640
8641 num_options = cupsParseOptions(line + 15, num_options, &options);
8642 }
8643
8644 /*
8645 * Done with the file; see if we have any options...
8646 */
8647
8648 cupsFileClose(fp);
8649
8650 if (num_options == 0)
8651 return;
8652
8653 /*
8654 * OK, convert the options to an attribute list, and apply them to
8655 * the request...
8656 */
8657
8658 ticket = ippNew();
8659 cupsEncodeOptions(ticket, num_options, options);
8660
8661 /*
8662 * See what the user wants to change.
8663 */
8664
8665 for (attr = ticket->attrs; attr; attr = attr->next)
8666 {
8667 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
8668 continue;
8669
8670 if (!strcmp(attr->name, "job-originating-host-name") ||
8671 !strcmp(attr->name, "job-originating-user-name") ||
8672 !strcmp(attr->name, "job-media-sheets-completed") ||
8673 !strcmp(attr->name, "job-k-octets") ||
8674 !strcmp(attr->name, "job-id") ||
8675 !strncmp(attr->name, "job-state", 9) ||
8676 !strncmp(attr->name, "time-at-", 8))
8677 continue; /* Read-only attrs */
8678
8679 if ((attr2 = ippFindAttribute(con->request, attr->name,
8680 IPP_TAG_ZERO)) != NULL)
8681 {
8682 /*
8683 * Some other value; first free the old value...
8684 */
8685
8686 if (con->request->attrs == attr2)
8687 {
8688 con->request->attrs = attr2->next;
8689 prev2 = NULL;
8690 }
8691 else
8692 {
8693 for (prev2 = con->request->attrs; prev2; prev2 = prev2->next)
8694 if (prev2->next == attr2)
8695 {
8696 prev2->next = attr2->next;
8697 break;
8698 }
8699 }
8700
8701 if (con->request->last == attr2)
8702 con->request->last = prev2;
8703
8704 _ippFreeAttr(attr2);
8705 }
8706
8707 /*
8708 * Add new option by copying it...
8709 */
8710
8711 copy_attribute(con->request, attr, 0);
8712 }
8713
8714 /*
8715 * Then free the attribute list and option array...
8716 */
8717
8718 ippDelete(ticket);
8719 cupsFreeOptions(num_options, options);
8720 }
8721
8722
8723 /*
8724 * 'reject_jobs()' - Reject print jobs to a printer.
8725 */
8726
8727 static void
8728 reject_jobs(cupsd_client_t *con, /* I - Client connection */
8729 ipp_attribute_t *uri) /* I - Printer or class URI */
8730 {
8731 http_status_t status; /* Policy status */
8732 cups_ptype_t dtype; /* Destination type (printer/class) */
8733 cupsd_printer_t *printer; /* Printer data */
8734 ipp_attribute_t *attr; /* printer-state-message text */
8735
8736
8737 cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con,
8738 con->http.fd, uri->values[0].string.text);
8739
8740 /*
8741 * Is the destination valid?
8742 */
8743
8744 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8745 {
8746 /*
8747 * Bad URI...
8748 */
8749
8750 send_ipp_status(con, IPP_NOT_FOUND,
8751 _("The printer or class was not found."));
8752 return;
8753 }
8754
8755 /*
8756 * Check policy...
8757 */
8758
8759 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8760 {
8761 send_http_error(con, status, printer);
8762 return;
8763 }
8764
8765 /*
8766 * Reject jobs sent to the printer...
8767 */
8768
8769 printer->accepting = 0;
8770
8771 if ((attr = ippFindAttribute(con->request, "printer-state-message",
8772 IPP_TAG_TEXT)) == NULL)
8773 strcpy(printer->state_message, "Rejecting Jobs");
8774 else
8775 strlcpy(printer->state_message, attr->values[0].string.text,
8776 sizeof(printer->state_message));
8777
8778 cupsdAddPrinterHistory(printer);
8779
8780 if (dtype & CUPS_PRINTER_CLASS)
8781 {
8782 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
8783
8784 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").",
8785 printer->name, get_username(con));
8786 }
8787 else
8788 {
8789 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
8790
8791 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").",
8792 printer->name, get_username(con));
8793 }
8794
8795 /*
8796 * Everything was ok, so return OK status...
8797 */
8798
8799 con->response->request.status.status_code = IPP_OK;
8800 }
8801
8802
8803 /*
8804 * 'release_job()' - Release a held print job.
8805 */
8806
8807 static void
8808 release_job(cupsd_client_t *con, /* I - Client connection */
8809 ipp_attribute_t *uri) /* I - Job or Printer URI */
8810 {
8811 ipp_attribute_t *attr; /* Current attribute */
8812 int jobid; /* Job ID */
8813 char method[HTTP_MAX_URI], /* Method portion of URI */
8814 username[HTTP_MAX_URI], /* Username portion of URI */
8815 host[HTTP_MAX_URI], /* Host portion of URI */
8816 resource[HTTP_MAX_URI]; /* Resource portion of URI */
8817 int port; /* Port portion of URI */
8818 cupsd_job_t *job; /* Job information */
8819
8820
8821 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con,
8822 con->http.fd, uri->values[0].string.text);
8823
8824 /*
8825 * See if we have a job URI or a printer URI...
8826 */
8827
8828 if (!strcmp(uri->name, "printer-uri"))
8829 {
8830 /*
8831 * Got a printer URI; see if we also have a job-id attribute...
8832 */
8833
8834 if ((attr = ippFindAttribute(con->request, "job-id",
8835 IPP_TAG_INTEGER)) == NULL)
8836 {
8837 send_ipp_status(con, IPP_BAD_REQUEST,
8838 _("Got a printer-uri attribute but no job-id!"));
8839 return;
8840 }
8841
8842 jobid = attr->values[0].integer;
8843 }
8844 else
8845 {
8846 /*
8847 * Got a job URI; parse it to get the job ID...
8848 */
8849
8850 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
8851 sizeof(method), username, sizeof(username), host,
8852 sizeof(host), &port, resource, sizeof(resource));
8853
8854 if (strncmp(resource, "/jobs/", 6))
8855 {
8856 /*
8857 * Not a valid URI!
8858 */
8859
8860 send_ipp_status(con, IPP_BAD_REQUEST,
8861 _("Bad job-uri attribute \"%s\"!"),
8862 uri->values[0].string.text);
8863 return;
8864 }
8865
8866 jobid = atoi(resource + 6);
8867 }
8868
8869 /*
8870 * See if the job exists...
8871 */
8872
8873 if ((job = cupsdFindJob(jobid)) == NULL)
8874 {
8875 /*
8876 * Nope - return a "not found" error...
8877 */
8878
8879 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
8880 return;
8881 }
8882
8883 /*
8884 * See if job is "held"...
8885 */
8886
8887 if (job->state_value != IPP_JOB_HELD)
8888 {
8889 /*
8890 * Nope - return a "not possible" error...
8891 */
8892
8893 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held!"), jobid);
8894 return;
8895 }
8896
8897 /*
8898 * See if the job is owned by the requesting user...
8899 */
8900
8901 if (!validate_user(job, con, job->username, username, sizeof(username)))
8902 {
8903 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
8904 return;
8905 }
8906
8907 /*
8908 * Reset the job-hold-until value to "no-hold"...
8909 */
8910
8911 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
8912 IPP_TAG_KEYWORD)) == NULL)
8913 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
8914
8915 if (attr)
8916 {
8917 _cupsStrFree(attr->values[0].string.text);
8918
8919 attr->value_tag = IPP_TAG_KEYWORD;
8920 attr->values[0].string.text = _cupsStrAlloc("no-hold");
8921
8922 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
8923 "Job job-hold-until value changed by user.");
8924 }
8925
8926 /*
8927 * Release the job and return...
8928 */
8929
8930 cupsdReleaseJob(job);
8931
8932 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
8933 "Job released by user.");
8934
8935 cupsdLogJob(job, CUPSD_LOG_INFO, "Released by \"%s\".", username);
8936
8937 con->response->request.status.status_code = IPP_OK;
8938
8939 cupsdCheckJobs();
8940 }
8941
8942
8943 /*
8944 * 'renew_subscription()' - Renew an existing subscription...
8945 */
8946
8947 static void
8948 renew_subscription(
8949 cupsd_client_t *con, /* I - Client connection */
8950 int sub_id) /* I - Subscription ID */
8951 {
8952 http_status_t status; /* Policy status */
8953 cupsd_subscription_t *sub; /* Subscription */
8954 ipp_attribute_t *lease; /* notify-lease-duration */
8955
8956
8957 cupsdLogMessage(CUPSD_LOG_DEBUG2,
8958 "renew_subscription(con=%p[%d], sub_id=%d)",
8959 con, con->http.fd, sub_id);
8960
8961 /*
8962 * Is the subscription ID valid?
8963 */
8964
8965 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
8966 {
8967 /*
8968 * Bad subscription ID...
8969 */
8970
8971 send_ipp_status(con, IPP_NOT_FOUND,
8972 _("notify-subscription-id %d no good!"), sub_id);
8973 return;
8974 }
8975
8976 if (sub->job)
8977 {
8978 /*
8979 * Job subscriptions cannot be renewed...
8980 */
8981
8982 send_ipp_status(con, IPP_NOT_POSSIBLE,
8983 _("Job subscriptions cannot be renewed!"));
8984 return;
8985 }
8986
8987 /*
8988 * Check policy...
8989 */
8990
8991 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
8992 DefaultPolicyPtr,
8993 con, sub->owner)) != HTTP_OK)
8994 {
8995 send_http_error(con, status, sub->dest);
8996 return;
8997 }
8998
8999 /*
9000 * Renew the subscription...
9001 */
9002
9003 lease = ippFindAttribute(con->request, "notify-lease-duration",
9004 IPP_TAG_INTEGER);
9005
9006 sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration;
9007
9008 if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration))
9009 {
9010 cupsdLogMessage(CUPSD_LOG_INFO,
9011 "renew_subscription: Limiting notify-lease-duration to "
9012 "%d seconds.",
9013 MaxLeaseDuration);
9014 sub->lease = MaxLeaseDuration;
9015 }
9016
9017 sub->expire = sub->lease ? time(NULL) + sub->lease : 0;
9018
9019 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
9020
9021 con->response->request.status.status_code = IPP_OK;
9022
9023 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
9024 "notify-lease-duration", sub->lease);
9025 }
9026
9027
9028 /*
9029 * 'restart_job()' - Restart an old print job.
9030 */
9031
9032 static void
9033 restart_job(cupsd_client_t *con, /* I - Client connection */
9034 ipp_attribute_t *uri) /* I - Job or Printer URI */
9035 {
9036 ipp_attribute_t *attr; /* Current attribute */
9037 int jobid; /* Job ID */
9038 char method[HTTP_MAX_URI], /* Method portion of URI */
9039 username[HTTP_MAX_URI], /* Username portion of URI */
9040 host[HTTP_MAX_URI], /* Host portion of URI */
9041 resource[HTTP_MAX_URI]; /* Resource portion of URI */
9042 int port; /* Port portion of URI */
9043 cupsd_job_t *job; /* Job information */
9044
9045
9046 cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con,
9047 con->http.fd, uri->values[0].string.text);
9048
9049 /*
9050 * See if we have a job URI or a printer URI...
9051 */
9052
9053 if (!strcmp(uri->name, "printer-uri"))
9054 {
9055 /*
9056 * Got a printer URI; see if we also have a job-id attribute...
9057 */
9058
9059 if ((attr = ippFindAttribute(con->request, "job-id",
9060 IPP_TAG_INTEGER)) == NULL)
9061 {
9062 send_ipp_status(con, IPP_BAD_REQUEST,
9063 _("Got a printer-uri attribute but no job-id!"));
9064 return;
9065 }
9066
9067 jobid = attr->values[0].integer;
9068 }
9069 else
9070 {
9071 /*
9072 * Got a job URI; parse it to get the job ID...
9073 */
9074
9075 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
9076 sizeof(method), username, sizeof(username), host,
9077 sizeof(host), &port, resource, sizeof(resource));
9078
9079 if (strncmp(resource, "/jobs/", 6))
9080 {
9081 /*
9082 * Not a valid URI!
9083 */
9084
9085 send_ipp_status(con, IPP_BAD_REQUEST,
9086 _("Bad job-uri attribute \"%s\"!"),
9087 uri->values[0].string.text);
9088 return;
9089 }
9090
9091 jobid = atoi(resource + 6);
9092 }
9093
9094 /*
9095 * See if the job exists...
9096 */
9097
9098 if ((job = cupsdFindJob(jobid)) == NULL)
9099 {
9100 /*
9101 * Nope - return a "not found" error...
9102 */
9103
9104 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
9105 return;
9106 }
9107
9108 /*
9109 * See if job is in any of the "completed" states...
9110 */
9111
9112 if (job->state_value <= IPP_JOB_PROCESSING)
9113 {
9114 /*
9115 * Nope - return a "not possible" error...
9116 */
9117
9118 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete!"),
9119 jobid);
9120 return;
9121 }
9122
9123 /*
9124 * See if we have retained the job files...
9125 */
9126
9127 cupsdLoadJob(job);
9128
9129 if (!job->attrs || job->num_files == 0)
9130 {
9131 /*
9132 * Nope - return a "not possible" error...
9133 */
9134
9135 send_ipp_status(con, IPP_NOT_POSSIBLE,
9136 _("Job #%d cannot be restarted - no files!"), jobid);
9137 return;
9138 }
9139
9140 /*
9141 * See if the job is owned by the requesting user...
9142 */
9143
9144 if (!validate_user(job, con, job->username, username, sizeof(username)))
9145 {
9146 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
9147 return;
9148 }
9149
9150 /*
9151 * Restart the job and return...
9152 */
9153
9154 cupsdRestartJob(job);
9155
9156 cupsdLogJob(job, CUPSD_LOG_INFO, "Restarted by \"%s\".", username);
9157
9158 con->response->request.status.status_code = IPP_OK;
9159 }
9160
9161
9162 /*
9163 * 'save_auth_info()' - Save authentication information for a job.
9164 */
9165
9166 static void
9167 save_auth_info(
9168 cupsd_client_t *con, /* I - Client connection */
9169 cupsd_job_t *job, /* I - Job */
9170 ipp_attribute_t *auth_info) /* I - auth-info attribute, if any */
9171 {
9172 int i; /* Looping var */
9173 char filename[1024]; /* Job authentication filename */
9174 cups_file_t *fp; /* Job authentication file */
9175 char line[2048]; /* Line for file */
9176 cupsd_printer_t *dest; /* Destination printer/class */
9177
9178
9179 /*
9180 * This function saves the in-memory authentication information for
9181 * a job so that it can be used to authenticate with a remote host.
9182 * The information is stored in a file that is readable only by the
9183 * root user. The fields are Base-64 encoded, each on a separate line,
9184 * followed by random number (up to 1024) of newlines to limit the
9185 * amount of information that is exposed.
9186 *
9187 * Because of the potential for exposing of authentication information,
9188 * this functionality is only enabled when running cupsd as root.
9189 *
9190 * This caching only works for the Basic and BasicDigest authentication
9191 * types. Digest authentication cannot be cached this way, and in
9192 * the future Kerberos authentication may make all of this obsolete.
9193 *
9194 * Authentication information is saved whenever an authenticated
9195 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9196 * performed.
9197 *
9198 * This information is deleted after a job is completed or canceled,
9199 * so reprints may require subsequent re-authentication.
9200 */
9201
9202 if (RunUser)
9203 return;
9204
9205 if ((dest = cupsdFindDest(job->dest)) == NULL)
9206 return;
9207
9208 /*
9209 * Create the authentication file and change permissions...
9210 */
9211
9212 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
9213 if ((fp = cupsFileOpen(filename, "w")) == NULL)
9214 {
9215 cupsdLogMessage(CUPSD_LOG_ERROR,
9216 "Unable to save authentication info to \"%s\" - %s",
9217 filename, strerror(errno));
9218 return;
9219 }
9220
9221 fchown(cupsFileNumber(fp), 0, 0);
9222 fchmod(cupsFileNumber(fp), 0400);
9223
9224 if (auth_info && auth_info->num_values == dest->num_auth_info_required)
9225 {
9226 /*
9227 * Write 1 to 3 auth values...
9228 */
9229
9230 cupsdClearString(&job->auth_username);
9231 cupsdClearString(&job->auth_domain);
9232 cupsdClearString(&job->auth_password);
9233
9234 for (i = 0; i < auth_info->num_values; i ++)
9235 {
9236 httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text,
9237 strlen(auth_info->values[i].string.text));
9238 cupsFilePrintf(fp, "%s\n", line);
9239
9240 if (!strcmp(dest->auth_info_required[i], "username"))
9241 cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s",
9242 auth_info->values[i].string.text);
9243 else if (!strcmp(dest->auth_info_required[i], "domain"))
9244 cupsdSetStringf(&job->auth_domain, "AUTH_DOMAIN=%s",
9245 auth_info->values[i].string.text);
9246 else if (!strcmp(dest->auth_info_required[i], "password"))
9247 cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s",
9248 auth_info->values[i].string.text);
9249 }
9250 }
9251 else if (con->username[0])
9252 {
9253 /*
9254 * Write the authenticated username...
9255 */
9256
9257 httpEncode64_2(line, sizeof(line), con->username, strlen(con->username));
9258 cupsFilePrintf(fp, "%s\n", line);
9259
9260 cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", con->username);
9261 cupsdClearString(&job->auth_domain);
9262
9263 /*
9264 * Write the authenticated password...
9265 */
9266
9267 httpEncode64_2(line, sizeof(line), con->password, strlen(con->password));
9268 cupsFilePrintf(fp, "%s\n", line);
9269
9270 cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", con->password);
9271 }
9272
9273 /*
9274 * Write a random number of newlines to the end of the file...
9275 */
9276
9277 for (i = (rand() % 1024); i >= 0; i --)
9278 cupsFilePutChar(fp, '\n');
9279
9280 /*
9281 * Close the file and return...
9282 */
9283
9284 cupsFileClose(fp);
9285
9286 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9287 if (con->gss_have_creds)
9288 save_krb5_creds(con, job);
9289 else if (job->ccname)
9290 cupsdClearString(&(job->ccname));
9291 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9292 }
9293
9294
9295 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9296 /*
9297 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
9298 */
9299
9300 static void
9301 save_krb5_creds(cupsd_client_t *con, /* I - Client connection */
9302 cupsd_job_t *job) /* I - Job */
9303 {
9304 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
9305 cupsdLogMessage(CUPSD_LOG_INFO,
9306 "Sorry, your version of Kerberos does not support delegated "
9307 "credentials!");
9308 return;
9309
9310 # else
9311 krb5_error_code error; /* Kerberos error code */
9312 OM_uint32 major_status, /* Major status code */
9313 minor_status; /* Minor status code */
9314 krb5_principal principal; /* Kerberos principal */
9315
9316
9317 # ifdef __APPLE__
9318 /*
9319 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
9320 * to use it...
9321 */
9322
9323 if (krb5_init_context == NULL)
9324 return;
9325 # endif /* __APPLE__ */
9326
9327 if (!KerberosInitialized)
9328 {
9329 /*
9330 * Setup a Kerberos context for the scheduler to use...
9331 */
9332
9333 KerberosInitialized = 1;
9334
9335 if (krb5_init_context(&KerberosContext))
9336 {
9337 KerberosContext = NULL;
9338
9339 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to initialize Kerberos context");
9340 return;
9341 }
9342 }
9343
9344 /*
9345 * We MUST create a file-based cache because memory-based caches are
9346 * only valid for the current process/address space.
9347 *
9348 * Due to various bugs/features in different versions of Kerberos, we
9349 * need either the krb5_cc_new_unique() function or Heimdal's version
9350 * of krb5_cc_gen_new() to create a new FILE: credential cache that
9351 * can be passed to the backend. These functions create a temporary
9352 * file (typically in /tmp) containing the cached credentials, which
9353 * are removed when we have successfully printed a job.
9354 */
9355
9356 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
9357 if ((error = krb5_cc_new_unique(KerberosContext, "FILE", NULL,
9358 &(job->ccache))) != 0)
9359 # else /* HAVE_HEIMDAL */
9360 if ((error = krb5_cc_gen_new(KerberosContext, &krb5_fcc_ops,
9361 &(job->ccache))) != 0)
9362 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
9363 {
9364 cupsdLogMessage(CUPSD_LOG_ERROR,
9365 "Unable to create new credentials cache (%d/%s)",
9366 error, strerror(errno));
9367 job->ccache = NULL;
9368 return;
9369 }
9370
9371 if ((error = krb5_parse_name(KerberosContext, con->username, &principal)) != 0)
9372 {
9373 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to parse kerberos username (%d/%s)",
9374 error, strerror(errno));
9375 krb5_cc_destroy(KerberosContext, job->ccache);
9376 job->ccache = NULL;
9377 return;
9378 }
9379
9380 if ((error = krb5_cc_initialize(KerberosContext, job->ccache, principal)))
9381 {
9382 cupsdLogMessage(CUPSD_LOG_ERROR,
9383 "Unable to initialize credentials cache (%d/%s)", error,
9384 strerror(errno));
9385 krb5_cc_destroy(KerberosContext, job->ccache);
9386 krb5_free_principal(KerberosContext, principal);
9387 job->ccache = NULL;
9388 return;
9389 }
9390
9391 krb5_free_principal(KerberosContext, principal);
9392
9393 /*
9394 * Copy the user's credentials to the new cache file...
9395 */
9396
9397 major_status = gss_krb5_copy_ccache(&minor_status, con->gss_delegated_cred,
9398 job->ccache);
9399
9400 if (GSS_ERROR(major_status))
9401 {
9402 cupsdLogGSSMessage(CUPSD_LOG_ERROR, major_status, minor_status,
9403 "Unable to import client credentials cache");
9404 krb5_cc_destroy(KerberosContext, job->ccache);
9405 job->ccache = NULL;
9406 return;
9407 }
9408
9409 /*
9410 * Add the KRB5CCNAME environment variable to the job so that the
9411 * backend can use the credentials when printing.
9412 */
9413
9414 cupsdSetStringf(&(job->ccname), "KRB5CCNAME=FILE:%s",
9415 krb5_cc_get_name(KerberosContext, job->ccache));
9416
9417 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "save_krb5_creds: %s", job->ccname);
9418 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
9419 }
9420 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9421
9422
9423 /*
9424 * 'send_document()' - Send a file to a printer or class.
9425 */
9426
9427 static void
9428 send_document(cupsd_client_t *con, /* I - Client connection */
9429 ipp_attribute_t *uri) /* I - Printer URI */
9430 {
9431 ipp_attribute_t *attr; /* Current attribute */
9432 ipp_attribute_t *format; /* Request's document-format attribute */
9433 ipp_attribute_t *jformat; /* Job's document-format attribute */
9434 const char *default_format;/* document-format-default value */
9435 int jobid; /* Job ID number */
9436 cupsd_job_t *job; /* Current job */
9437 char job_uri[HTTP_MAX_URI],
9438 /* Job URI */
9439 method[HTTP_MAX_URI],
9440 /* Method portion of URI */
9441 username[HTTP_MAX_URI],
9442 /* Username portion of URI */
9443 host[HTTP_MAX_URI],
9444 /* Host portion of URI */
9445 resource[HTTP_MAX_URI];
9446 /* Resource portion of URI */
9447 int port; /* Port portion of URI */
9448 mime_type_t *filetype; /* Type of file */
9449 char super[MIME_MAX_SUPER],
9450 /* Supertype of file */
9451 type[MIME_MAX_TYPE],
9452 /* Subtype of file */
9453 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
9454 /* Textual name of mime type */
9455 char filename[1024]; /* Job filename */
9456 cupsd_printer_t *printer; /* Current printer */
9457 struct stat fileinfo; /* File information */
9458 int kbytes; /* Size of file */
9459 int compression; /* Type of compression */
9460 int start_job; /* Start the job? */
9461
9462
9463 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con,
9464 con->http.fd, uri->values[0].string.text);
9465
9466 /*
9467 * See if we have a job URI or a printer URI...
9468 */
9469
9470 if (!strcmp(uri->name, "printer-uri"))
9471 {
9472 /*
9473 * Got a printer URI; see if we also have a job-id attribute...
9474 */
9475
9476 if ((attr = ippFindAttribute(con->request, "job-id",
9477 IPP_TAG_INTEGER)) == NULL)
9478 {
9479 send_ipp_status(con, IPP_BAD_REQUEST,
9480 _("Got a printer-uri attribute but no job-id!"));
9481 return;
9482 }
9483
9484 jobid = attr->values[0].integer;
9485 }
9486 else
9487 {
9488 /*
9489 * Got a job URI; parse it to get the job ID...
9490 */
9491
9492 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
9493 sizeof(method), username, sizeof(username), host,
9494 sizeof(host), &port, resource, sizeof(resource));
9495
9496 if (strncmp(resource, "/jobs/", 6))
9497 {
9498 /*
9499 * Not a valid URI!
9500 */
9501
9502 send_ipp_status(con, IPP_BAD_REQUEST,
9503 _("Bad job-uri attribute \"%s\"!"),
9504 uri->values[0].string.text);
9505 return;
9506 }
9507
9508 jobid = atoi(resource + 6);
9509 }
9510
9511 /*
9512 * See if the job exists...
9513 */
9514
9515 if ((job = cupsdFindJob(jobid)) == NULL)
9516 {
9517 /*
9518 * Nope - return a "not found" error...
9519 */
9520
9521 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
9522 return;
9523 }
9524
9525 printer = cupsdFindDest(job->dest);
9526
9527 /*
9528 * See if the job is owned by the requesting user...
9529 */
9530
9531 if (!validate_user(job, con, job->username, username, sizeof(username)))
9532 {
9533 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
9534 return;
9535 }
9536
9537 /*
9538 * OK, see if the client is sending the document compressed - CUPS
9539 * only supports "none" and "gzip".
9540 */
9541
9542 compression = CUPS_FILE_NONE;
9543
9544 if ((attr = ippFindAttribute(con->request, "compression",
9545 IPP_TAG_KEYWORD)) != NULL)
9546 {
9547 if (strcmp(attr->values[0].string.text, "none")
9548 #ifdef HAVE_LIBZ
9549 && strcmp(attr->values[0].string.text, "gzip")
9550 #endif /* HAVE_LIBZ */
9551 )
9552 {
9553 send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"!"),
9554 attr->values[0].string.text);
9555 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
9556 "compression", NULL, attr->values[0].string.text);
9557 return;
9558 }
9559
9560 #ifdef HAVE_LIBZ
9561 if (!strcmp(attr->values[0].string.text, "gzip"))
9562 compression = CUPS_FILE_GZIP;
9563 #endif /* HAVE_LIBZ */
9564 }
9565
9566 /*
9567 * Do we have a file to print?
9568 */
9569
9570 if (!con->filename)
9571 {
9572 send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!"));
9573 return;
9574 }
9575
9576 /*
9577 * Is it a format we support?
9578 */
9579
9580 if ((format = ippFindAttribute(con->request, "document-format",
9581 IPP_TAG_MIMETYPE)) != NULL)
9582 {
9583 /*
9584 * Grab format from client...
9585 */
9586
9587 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
9588 super, type) != 2)
9589 {
9590 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"),
9591 format->values[0].string.text);
9592 return;
9593 }
9594 }
9595 else if ((default_format = cupsGetOption("document-format",
9596 printer->num_options,
9597 printer->options)) != NULL)
9598 {
9599 /*
9600 * Use default document format...
9601 */
9602
9603 if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
9604 {
9605 send_ipp_status(con, IPP_BAD_REQUEST,
9606 _("Could not scan type \"%s\"!"),
9607 default_format);
9608 return;
9609 }
9610 }
9611 else
9612 {
9613 /*
9614 * No document format attribute? Auto-type it!
9615 */
9616
9617 strcpy(super, "application");
9618 strcpy(type, "octet-stream");
9619 }
9620
9621 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
9622 {
9623 /*
9624 * Auto-type the file...
9625 */
9626
9627 ipp_attribute_t *doc_name; /* document-name attribute */
9628
9629
9630 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Auto-typing file...");
9631
9632 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
9633 filetype = mimeFileType(MimeDatabase, con->filename,
9634 doc_name ? doc_name->values[0].string.text : NULL,
9635 &compression);
9636
9637 if (!filetype)
9638 filetype = mimeType(MimeDatabase, super, type);
9639
9640 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Request file type is %s/%s.",
9641 filetype->super, filetype->type);
9642 }
9643 else
9644 filetype = mimeType(MimeDatabase, super, type);
9645
9646 if (filetype)
9647 {
9648 /*
9649 * Replace the document-format attribute value with the auto-typed or
9650 * default one.
9651 */
9652
9653 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9654 filetype->type);
9655
9656 if ((jformat = ippFindAttribute(job->attrs, "document-format",
9657 IPP_TAG_MIMETYPE)) != NULL)
9658 {
9659 _cupsStrFree(jformat->values[0].string.text);
9660
9661 jformat->values[0].string.text = _cupsStrAlloc(mimetype);
9662 }
9663 else
9664 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
9665 "document-format", NULL, mimetype);
9666 }
9667 else if (!filetype)
9668 {
9669 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9670 _("Unsupported format \'%s/%s\'!"), super, type);
9671 cupsdLogMessage(CUPSD_LOG_INFO,
9672 "Hint: Do you have the raw file printing rules enabled?");
9673
9674 if (format)
9675 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9676 "document-format", NULL, format->values[0].string.text);
9677
9678 return;
9679 }
9680
9681 if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype))
9682 {
9683 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9684 filetype->type);
9685
9686 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9687 _("Unsupported format \'%s\'!"), mimetype);
9688
9689 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9690 "document-format", NULL, mimetype);
9691
9692 return;
9693 }
9694
9695 /*
9696 * Add the file to the job...
9697 */
9698
9699 cupsdLoadJob(job);
9700
9701 if (add_file(con, job, filetype, compression))
9702 return;
9703
9704 if (stat(con->filename, &fileinfo))
9705 kbytes = 0;
9706 else
9707 kbytes = (fileinfo.st_size + 1023) / 1024;
9708
9709 cupsdUpdateQuota(printer, job->username, 0, kbytes);
9710
9711 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
9712 IPP_TAG_INTEGER)) != NULL)
9713 attr->values[0].integer += kbytes;
9714
9715 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
9716 job->num_files);
9717 rename(con->filename, filename);
9718
9719 cupsdClearString(&con->filename);
9720
9721 cupsdLogJob(job, CUPSD_LOG_INFO, "File of type %s/%s queued by \"%s\".",
9722 filetype->super, filetype->type, job->username);
9723
9724 /*
9725 * Start the job if this is the last document...
9726 */
9727
9728 if ((attr = ippFindAttribute(con->request, "last-document",
9729 IPP_TAG_BOOLEAN)) != NULL &&
9730 attr->values[0].boolean)
9731 {
9732 /*
9733 * See if we need to add the ending sheet...
9734 */
9735
9736 if (cupsdTimeoutJob(job))
9737 return;
9738
9739 if (job->state_value == IPP_JOB_STOPPED)
9740 {
9741 job->state->values[0].integer = IPP_JOB_PENDING;
9742 job->state_value = IPP_JOB_PENDING;
9743 }
9744 else if (job->state_value == IPP_JOB_HELD)
9745 {
9746 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9747 IPP_TAG_KEYWORD)) == NULL)
9748 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9749
9750 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
9751 {
9752 job->state->values[0].integer = IPP_JOB_PENDING;
9753 job->state_value = IPP_JOB_PENDING;
9754 }
9755 }
9756
9757 job->dirty = 1;
9758 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
9759
9760 start_job = 1;
9761 }
9762 else
9763 {
9764 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9765 IPP_TAG_KEYWORD)) == NULL)
9766 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9767
9768 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
9769 {
9770 job->state->values[0].integer = IPP_JOB_HELD;
9771 job->state_value = IPP_JOB_HELD;
9772 job->hold_until = time(NULL) + 60;
9773 job->dirty = 1;
9774
9775 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
9776 }
9777
9778 start_job = 0;
9779 }
9780
9781 /*
9782 * Fill in the response info...
9783 */
9784
9785 snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName,
9786 LocalPort, jobid);
9787
9788 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
9789 job_uri);
9790
9791 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid);
9792
9793 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
9794 job->state_value);
9795 add_job_state_reasons(con, job);
9796
9797 con->response->request.status.status_code = IPP_OK;
9798
9799 /*
9800 * Start the job if necessary...
9801 */
9802
9803 if (start_job)
9804 cupsdCheckJobs();
9805 }
9806
9807
9808 /*
9809 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9810 */
9811
9812 static void
9813 send_http_error(
9814 cupsd_client_t *con, /* I - Client connection */
9815 http_status_t status, /* I - HTTP status code */
9816 cupsd_printer_t *printer) /* I - Printer, if any */
9817 {
9818 ipp_attribute_t *uri; /* Request URI, if any */
9819
9820
9821 if ((uri = ippFindAttribute(con->request, "printer-uri",
9822 IPP_TAG_URI)) == NULL)
9823 uri = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI);
9824
9825 cupsdLogMessage(status == HTTP_FORBIDDEN ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
9826 "Returning HTTP %s for %s (%s) from %s",
9827 httpStatus(status),
9828 ippOpString(con->request->request.op.operation_id),
9829 uri ? uri->values[0].string.text : "no URI",
9830 con->http.hostname);
9831
9832 if (printer)
9833 {
9834 int auth_type; /* Type of authentication required */
9835
9836
9837 auth_type = CUPSD_AUTH_NONE;
9838
9839 if (status == HTTP_UNAUTHORIZED &&
9840 printer->num_auth_info_required > 0 &&
9841 !strcmp(printer->auth_info_required[0], "negotiate") &&
9842 con->request &&
9843 (con->request->request.op.operation_id == IPP_PRINT_JOB ||
9844 con->request->request.op.operation_id == IPP_CREATE_JOB ||
9845 con->request->request.op.operation_id == CUPS_AUTHENTICATE_JOB))
9846 {
9847 /*
9848 * Creating and authenticating jobs requires Kerberos...
9849 */
9850
9851 auth_type = CUPSD_AUTH_NEGOTIATE;
9852 }
9853 else
9854 {
9855 /*
9856 * Use policy/location-defined authentication requirements...
9857 */
9858
9859 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
9860 cupsd_location_t *auth; /* Pointer to authentication element */
9861
9862
9863 if (printer->type & CUPS_PRINTER_CLASS)
9864 snprintf(resource, sizeof(resource), "/classes/%s", printer->name);
9865 else
9866 snprintf(resource, sizeof(resource), "/printers/%s", printer->name);
9867
9868 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
9869 auth->type == CUPSD_AUTH_NONE)
9870 auth = cupsdFindPolicyOp(printer->op_policy_ptr,
9871 con->request ?
9872 con->request->request.op.operation_id :
9873 IPP_PRINT_JOB);
9874
9875 if (auth)
9876 {
9877 if (auth->type == CUPSD_AUTH_DEFAULT)
9878 auth_type = DefaultAuthType;
9879 else
9880 auth_type = auth->type;
9881 }
9882 }
9883
9884 cupsdSendError(con, status, auth_type);
9885 }
9886 else
9887 cupsdSendError(con, status, CUPSD_AUTH_NONE);
9888
9889 ippDelete(con->response);
9890 con->response = NULL;
9891
9892 return;
9893 }
9894
9895
9896 /*
9897 * 'send_ipp_status()' - Send a status back to the IPP client.
9898 */
9899
9900 static void
9901 send_ipp_status(cupsd_client_t *con, /* I - Client connection */
9902 ipp_status_t status, /* I - IPP status code */
9903 const char *message,/* I - Status message */
9904 ...) /* I - Additional args as needed */
9905 {
9906 va_list ap; /* Pointer to additional args */
9907 char formatted[1024]; /* Formatted errror message */
9908
9909
9910 va_start(ap, message);
9911 vsnprintf(formatted, sizeof(formatted),
9912 _cupsLangString(con->language, message), ap);
9913 va_end(ap);
9914
9915 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s",
9916 ippOpString(con->request->request.op.operation_id),
9917 ippErrorString(status), formatted);
9918
9919 con->response->request.status.status_code = status;
9920
9921 if (ippFindAttribute(con->response, "attributes-charset",
9922 IPP_TAG_ZERO) == NULL)
9923 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
9924 "attributes-charset", NULL, DefaultCharset);
9925
9926 if (ippFindAttribute(con->response, "attributes-natural-language",
9927 IPP_TAG_ZERO) == NULL)
9928 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
9929 "attributes-natural-language", NULL, DefaultLanguage);
9930
9931 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT,
9932 "status-message", NULL, formatted);
9933 }
9934
9935
9936 /*
9937 * 'set_default()' - Set the default destination...
9938 */
9939
9940 static void
9941 set_default(cupsd_client_t *con, /* I - Client connection */
9942 ipp_attribute_t *uri) /* I - Printer URI */
9943 {
9944 http_status_t status; /* Policy status */
9945 cups_ptype_t dtype; /* Destination type (printer/class) */
9946 cupsd_printer_t *printer, /* Printer */
9947 *oldprinter; /* Old default printer */
9948
9949
9950 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con,
9951 con->http.fd, uri->values[0].string.text);
9952
9953 /*
9954 * Is the destination valid?
9955 */
9956
9957 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
9958 {
9959 /*
9960 * Bad URI...
9961 */
9962
9963 send_ipp_status(con, IPP_NOT_FOUND,
9964 _("The printer or class was not found."));
9965 return;
9966 }
9967
9968 /*
9969 * Check policy...
9970 */
9971
9972 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
9973 {
9974 send_http_error(con, status, NULL);
9975 return;
9976 }
9977
9978 /*
9979 * Set it as the default...
9980 */
9981
9982 oldprinter = DefaultPrinter;
9983 DefaultPrinter = printer;
9984
9985 if (oldprinter)
9986 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, oldprinter, NULL,
9987 "%s is no longer the default printer.", oldprinter->name);
9988
9989 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
9990 "%s is now the default printer.", printer->name);
9991
9992 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES |
9993 CUPSD_DIRTY_REMOTE | CUPSD_DIRTY_PRINTCAP);
9994
9995 cupsdLogMessage(CUPSD_LOG_INFO,
9996 "Default destination set to \"%s\" by \"%s\".",
9997 printer->name, get_username(con));
9998
9999 /*
10000 * Everything was ok, so return OK status...
10001 */
10002
10003 con->response->request.status.status_code = IPP_OK;
10004 }
10005
10006
10007 /*
10008 * 'set_job_attrs()' - Set job attributes.
10009 */
10010
10011 static void
10012 set_job_attrs(cupsd_client_t *con, /* I - Client connection */
10013 ipp_attribute_t *uri) /* I - Job URI */
10014 {
10015 ipp_attribute_t *attr, /* Current attribute */
10016 *attr2; /* Job attribute */
10017 int jobid; /* Job ID */
10018 cupsd_job_t *job; /* Current job */
10019 char method[HTTP_MAX_URI],
10020 /* Method portion of URI */
10021 username[HTTP_MAX_URI],
10022 /* Username portion of URI */
10023 host[HTTP_MAX_URI],
10024 /* Host portion of URI */
10025 resource[HTTP_MAX_URI];
10026 /* Resource portion of URI */
10027 int port; /* Port portion of URI */
10028 int event; /* Events? */
10029 int check_jobs; /* Check jobs? */
10030
10031
10032 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con,
10033 con->http.fd, uri->values[0].string.text);
10034
10035 /*
10036 * Start with "everything is OK" status...
10037 */
10038
10039 con->response->request.status.status_code = IPP_OK;
10040
10041 /*
10042 * See if we have a job URI or a printer URI...
10043 */
10044
10045 if (!strcmp(uri->name, "printer-uri"))
10046 {
10047 /*
10048 * Got a printer URI; see if we also have a job-id attribute...
10049 */
10050
10051 if ((attr = ippFindAttribute(con->request, "job-id",
10052 IPP_TAG_INTEGER)) == NULL)
10053 {
10054 send_ipp_status(con, IPP_BAD_REQUEST,
10055 _("Got a printer-uri attribute but no job-id!"));
10056 return;
10057 }
10058
10059 jobid = attr->values[0].integer;
10060 }
10061 else
10062 {
10063 /*
10064 * Got a job URI; parse it to get the job ID...
10065 */
10066
10067 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
10068 sizeof(method), username, sizeof(username), host,
10069 sizeof(host), &port, resource, sizeof(resource));
10070
10071 if (strncmp(resource, "/jobs/", 6))
10072 {
10073 /*
10074 * Not a valid URI!
10075 */
10076
10077 send_ipp_status(con, IPP_BAD_REQUEST,
10078 _("Bad job-uri attribute \"%s\"!"),
10079 uri->values[0].string.text);
10080 return;
10081 }
10082
10083 jobid = atoi(resource + 6);
10084 }
10085
10086 /*
10087 * See if the job exists...
10088 */
10089
10090 if ((job = cupsdFindJob(jobid)) == NULL)
10091 {
10092 /*
10093 * Nope - return a "not found" error...
10094 */
10095
10096 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
10097 return;
10098 }
10099
10100 /*
10101 * See if the job has been completed...
10102 */
10103
10104 if (job->state_value > IPP_JOB_STOPPED)
10105 {
10106 /*
10107 * Return a "not-possible" error...
10108 */
10109
10110 send_ipp_status(con, IPP_NOT_POSSIBLE,
10111 _("Job #%d is finished and cannot be altered!"), jobid);
10112 return;
10113 }
10114
10115 /*
10116 * See if the job is owned by the requesting user...
10117 */
10118
10119 if (!validate_user(job, con, job->username, username, sizeof(username)))
10120 {
10121 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
10122 return;
10123 }
10124
10125 /*
10126 * See what the user wants to change.
10127 */
10128
10129 cupsdLoadJob(job);
10130
10131 check_jobs = 0;
10132 event = 0;
10133
10134 for (attr = con->request->attrs; attr; attr = attr->next)
10135 {
10136 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
10137 continue;
10138
10139 if (!strcmp(attr->name, "attributes-charset") ||
10140 !strcmp(attr->name, "attributes-natural-language") ||
10141 !strcmp(attr->name, "document-compression") ||
10142 !strcmp(attr->name, "document-format") ||
10143 !strcmp(attr->name, "job-detailed-status-messages") ||
10144 !strcmp(attr->name, "job-document-access-errors") ||
10145 !strcmp(attr->name, "job-id") ||
10146 !strcmp(attr->name, "job-impressions-completed") ||
10147 !strcmp(attr->name, "job-k-octets") ||
10148 !strcmp(attr->name, "job-originating-host-name") ||
10149 !strcmp(attr->name, "job-originating-user-name") ||
10150 !strcmp(attr->name, "job-printer-up-time") ||
10151 !strcmp(attr->name, "job-printer-uri") ||
10152 !strcmp(attr->name, "job-sheets") ||
10153 !strcmp(attr->name, "job-state-message") ||
10154 !strcmp(attr->name, "job-state-reasons") ||
10155 !strcmp(attr->name, "job-uri") ||
10156 !strcmp(attr->name, "number-of-documents") ||
10157 !strcmp(attr->name, "number-of-intervening-jobs") ||
10158 !strcmp(attr->name, "output-device-assigned") ||
10159 !strncmp(attr->name, "date-time-at-", 13) ||
10160 !strncmp(attr->name, "job-k-octets", 12) ||
10161 !strncmp(attr->name, "job-media-sheets", 16) ||
10162 !strncmp(attr->name, "time-at-", 8))
10163 {
10164 /*
10165 * Read-only attrs!
10166 */
10167
10168 send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE,
10169 _("%s cannot be changed."), attr->name);
10170
10171 if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
10172 attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
10173
10174 continue;
10175 }
10176
10177 if (!strcmp(attr->name, "job-priority"))
10178 {
10179 /*
10180 * Change the job priority...
10181 */
10182
10183 if (attr->value_tag != IPP_TAG_INTEGER)
10184 {
10185 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value!"));
10186
10187 if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
10188 attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
10189 }
10190 else if (job->state_value >= IPP_JOB_PROCESSING)
10191 {
10192 send_ipp_status(con, IPP_NOT_POSSIBLE,
10193 _("Job is completed and cannot be changed."));
10194 return;
10195 }
10196 else if (con->response->request.status.status_code == IPP_OK)
10197 {
10198 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-priority to %d",
10199 attr->values[0].integer);
10200 cupsdSetJobPriority(job, attr->values[0].integer);
10201
10202 check_jobs = 1;
10203 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED |
10204 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED;
10205 }
10206 }
10207 else if (!strcmp(attr->name, "job-state"))
10208 {
10209 /*
10210 * Change the job state...
10211 */
10212
10213 if (attr->value_tag != IPP_TAG_ENUM)
10214 {
10215 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value!"));
10216
10217 if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
10218 attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
10219 }
10220 else
10221 {
10222 switch (attr->values[0].integer)
10223 {
10224 case IPP_JOB_PENDING :
10225 case IPP_JOB_HELD :
10226 if (job->state_value > IPP_JOB_HELD)
10227 {
10228 send_ipp_status(con, IPP_NOT_POSSIBLE,
10229 _("Job state cannot be changed."));
10230 return;
10231 }
10232 else if (con->response->request.status.status_code == IPP_OK)
10233 {
10234 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10235 attr->values[0].integer);
10236
10237 job->state->values[0].integer = attr->values[0].integer;
10238 job->state_value = (ipp_jstate_t)attr->values[0].integer;
10239
10240 event |= CUPSD_EVENT_JOB_STATE;
10241 check_jobs = 1;
10242 }
10243 break;
10244
10245 case IPP_JOB_PROCESSING :
10246 case IPP_JOB_STOPPED :
10247 if (job->state_value != attr->values[0].integer)
10248 {
10249 send_ipp_status(con, IPP_NOT_POSSIBLE,
10250 _("Job state cannot be changed."));
10251 return;
10252 }
10253 break;
10254
10255 case IPP_JOB_CANCELED :
10256 case IPP_JOB_ABORTED :
10257 case IPP_JOB_COMPLETED :
10258 if (job->state_value > IPP_JOB_PROCESSING)
10259 {
10260 send_ipp_status(con, IPP_NOT_POSSIBLE,
10261 _("Job state cannot be changed."));
10262 return;
10263 }
10264 else if (con->response->request.status.status_code == IPP_OK)
10265 {
10266 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10267 attr->values[0].integer);
10268 cupsdCancelJob(job, 0, (ipp_jstate_t)attr->values[0].integer);
10269
10270 check_jobs = 1;
10271 }
10272 break;
10273 }
10274 }
10275 }
10276 else if (con->response->request.status.status_code != IPP_OK)
10277 continue;
10278 else if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10279 IPP_TAG_ZERO)) != NULL)
10280 {
10281 /*
10282 * Some other value; first free the old value...
10283 */
10284
10285 if (job->attrs->prev)
10286 job->attrs->prev->next = attr2->next;
10287 else
10288 job->attrs->attrs = attr2->next;
10289
10290 if (job->attrs->last == attr2)
10291 job->attrs->last = job->attrs->prev;
10292
10293 _ippFreeAttr(attr2);
10294
10295 /*
10296 * Then copy the attribute...
10297 */
10298
10299 copy_attribute(job->attrs, attr, 0);
10300
10301 /*
10302 * See if the job-name or job-hold-until is being changed.
10303 */
10304
10305 if (!strcmp(attr->name, "job-hold-until"))
10306 {
10307 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s",
10308 attr->values[0].string.text);
10309 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
10310
10311 if (!strcmp(attr->values[0].string.text, "no-hold"))
10312 cupsdReleaseJob(job);
10313 else
10314 cupsdHoldJob(job);
10315
10316 check_jobs = 1;
10317 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
10318 }
10319 }
10320 else if (attr->value_tag == IPP_TAG_DELETEATTR)
10321 {
10322 /*
10323 * Delete the attribute...
10324 */
10325
10326 if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10327 IPP_TAG_ZERO)) != NULL)
10328 {
10329 if (job->attrs->prev)
10330 job->attrs->prev->next = attr2->next;
10331 else
10332 job->attrs->attrs = attr2->next;
10333
10334 if (attr2 == job->attrs->last)
10335 job->attrs->last = job->attrs->prev;
10336
10337 _ippFreeAttr(attr2);
10338
10339 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
10340 }
10341 }
10342 else
10343 {
10344 /*
10345 * Add new option by copying it...
10346 */
10347
10348 copy_attribute(job->attrs, attr, 0);
10349
10350 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
10351 }
10352 }
10353
10354 /*
10355 * Save the job...
10356 */
10357
10358 job->dirty = 1;
10359 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
10360
10361 /*
10362 * Send events as needed...
10363 */
10364
10365 if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED)
10366 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED,
10367 cupsdFindDest(job->dest), job,
10368 "Job priority changed by user.");
10369
10370 if (event & CUPSD_EVENT_JOB_STATE)
10371 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
10372 job->state_value == IPP_JOB_HELD ?
10373 "Job held by user." : "Job restarted by user.");
10374
10375 if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED)
10376 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
10377 "Job options changed by user.");
10378
10379 /*
10380 * Start jobs if possible...
10381 */
10382
10383 if (check_jobs)
10384 cupsdCheckJobs();
10385 }
10386
10387
10388 /*
10389 * 'set_printer_defaults()' - Set printer default options from a request.
10390 */
10391
10392 static void
10393 set_printer_defaults(
10394 cupsd_client_t *con, /* I - Client connection */
10395 cupsd_printer_t *printer) /* I - Printer */
10396 {
10397 int i; /* Looping var */
10398 ipp_attribute_t *attr; /* Current attribute */
10399 int namelen; /* Length of attribute name */
10400 char name[256], /* New attribute name */
10401 value[256]; /* String version of integer attrs */
10402
10403
10404 for (attr = con->request->attrs; attr; attr = attr->next)
10405 {
10406 /*
10407 * Skip non-printer attributes...
10408 */
10409
10410 if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
10411 continue;
10412
10413 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name);
10414
10415 if (!strcmp(attr->name, "job-sheets-default"))
10416 {
10417 /*
10418 * Only allow keywords and names...
10419 */
10420
10421 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10422 continue;
10423
10424 /*
10425 * Only allow job-sheets-default to be set when running without a
10426 * system high classification level...
10427 */
10428
10429 if (Classification)
10430 continue;
10431
10432 cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text);
10433
10434 if (attr->num_values > 1)
10435 cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text);
10436 else
10437 cupsdSetString(&printer->job_sheets[1], "none");
10438 }
10439 else if (!strcmp(attr->name, "requesting-user-name-allowed"))
10440 {
10441 cupsdFreePrinterUsers(printer);
10442
10443 printer->deny_users = 0;
10444
10445 if (attr->value_tag == IPP_TAG_NAME &&
10446 (attr->num_values > 1 ||
10447 strcmp(attr->values[0].string.text, "all")))
10448 {
10449 for (i = 0; i < attr->num_values; i ++)
10450 cupsdAddPrinterUser(printer, attr->values[i].string.text);
10451 }
10452 }
10453 else if (!strcmp(attr->name, "requesting-user-name-denied"))
10454 {
10455 cupsdFreePrinterUsers(printer);
10456
10457 printer->deny_users = 1;
10458
10459 if (attr->value_tag == IPP_TAG_NAME &&
10460 (attr->num_values > 1 ||
10461 strcmp(attr->values[0].string.text, "none")))
10462 {
10463 for (i = 0; i < attr->num_values; i ++)
10464 cupsdAddPrinterUser(printer, attr->values[i].string.text);
10465 }
10466 }
10467 else if (!strcmp(attr->name, "job-quota-period"))
10468 {
10469 if (attr->value_tag != IPP_TAG_INTEGER)
10470 continue;
10471
10472 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...",
10473 attr->values[0].integer);
10474 cupsdFreeQuotas(printer);
10475
10476 printer->quota_period = attr->values[0].integer;
10477 }
10478 else if (!strcmp(attr->name, "job-k-limit"))
10479 {
10480 if (attr->value_tag != IPP_TAG_INTEGER)
10481 continue;
10482
10483 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...",
10484 attr->values[0].integer);
10485 cupsdFreeQuotas(printer);
10486
10487 printer->k_limit = attr->values[0].integer;
10488 }
10489 else if (!strcmp(attr->name, "job-page-limit"))
10490 {
10491 if (attr->value_tag != IPP_TAG_INTEGER)
10492 continue;
10493
10494 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...",
10495 attr->values[0].integer);
10496 cupsdFreeQuotas(printer);
10497
10498 printer->page_limit = attr->values[0].integer;
10499 }
10500 else if (!strcmp(attr->name, "printer-op-policy"))
10501 {
10502 cupsd_policy_t *p; /* Policy */
10503
10504
10505 if (attr->value_tag != IPP_TAG_NAME)
10506 continue;
10507
10508 if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
10509 {
10510 cupsdLogMessage(CUPSD_LOG_DEBUG,
10511 "Setting printer-op-policy to \"%s\"...",
10512 attr->values[0].string.text);
10513 cupsdSetString(&printer->op_policy, attr->values[0].string.text);
10514 printer->op_policy_ptr = p;
10515 }
10516 else
10517 {
10518 send_ipp_status(con, IPP_NOT_POSSIBLE,
10519 _("Unknown printer-op-policy \"%s\"."),
10520 attr->values[0].string.text);
10521 return;
10522 }
10523 }
10524 else if (!strcmp(attr->name, "printer-error-policy"))
10525 {
10526 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10527 continue;
10528
10529 if (strcmp(attr->values[0].string.text, "abort-job") &&
10530 strcmp(attr->values[0].string.text, "retry-current-job") &&
10531 strcmp(attr->values[0].string.text, "retry-job") &&
10532 strcmp(attr->values[0].string.text, "stop-printer"))
10533 {
10534 send_ipp_status(con, IPP_NOT_POSSIBLE,
10535 _("Unknown printer-error-policy \"%s\"."),
10536 attr->values[0].string.text);
10537 return;
10538 }
10539
10540 cupsdLogMessage(CUPSD_LOG_DEBUG,
10541 "Setting printer-error-policy to \"%s\"...",
10542 attr->values[0].string.text);
10543 cupsdSetString(&printer->error_policy, attr->values[0].string.text);
10544 }
10545
10546 /*
10547 * Skip any other non-default attributes...
10548 */
10549
10550 namelen = strlen(attr->name);
10551 if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") ||
10552 namelen > (sizeof(name) - 1) || attr->num_values != 1)
10553 continue;
10554
10555 /*
10556 * OK, anything else must be a user-defined default...
10557 */
10558
10559 strlcpy(name, attr->name, sizeof(name));
10560 name[namelen - 8] = '\0'; /* Strip "-default" */
10561
10562 switch (attr->value_tag)
10563 {
10564 case IPP_TAG_DELETEATTR :
10565 printer->num_options = cupsRemoveOption(name,
10566 printer->num_options,
10567 &(printer->options));
10568 cupsdLogMessage(CUPSD_LOG_DEBUG,
10569 "Deleting %s", attr->name);
10570 break;
10571
10572 case IPP_TAG_NAME :
10573 case IPP_TAG_KEYWORD :
10574 case IPP_TAG_URI :
10575 printer->num_options = cupsAddOption(name,
10576 attr->values[0].string.text,
10577 printer->num_options,
10578 &(printer->options));
10579 cupsdLogMessage(CUPSD_LOG_DEBUG,
10580 "Setting %s to \"%s\"...", attr->name,
10581 attr->values[0].string.text);
10582 break;
10583
10584 case IPP_TAG_BOOLEAN :
10585 printer->num_options = cupsAddOption(name,
10586 attr->values[0].boolean ?
10587 "true" : "false",
10588 printer->num_options,
10589 &(printer->options));
10590 cupsdLogMessage(CUPSD_LOG_DEBUG,
10591 "Setting %s to %s...", attr->name,
10592 attr->values[0].boolean ? "true" : "false");
10593 break;
10594
10595 case IPP_TAG_INTEGER :
10596 case IPP_TAG_ENUM :
10597 sprintf(value, "%d", attr->values[0].integer);
10598 printer->num_options = cupsAddOption(name, value,
10599 printer->num_options,
10600 &(printer->options));
10601 cupsdLogMessage(CUPSD_LOG_DEBUG,
10602 "Setting %s to %s...", attr->name, value);
10603 break;
10604
10605 case IPP_TAG_RANGE :
10606 sprintf(value, "%d-%d", attr->values[0].range.lower,
10607 attr->values[0].range.upper);
10608 printer->num_options = cupsAddOption(name, value,
10609 printer->num_options,
10610 &(printer->options));
10611 cupsdLogMessage(CUPSD_LOG_DEBUG,
10612 "Setting %s to %s...", attr->name, value);
10613 break;
10614
10615 case IPP_TAG_RESOLUTION :
10616 sprintf(value, "%dx%d%s", attr->values[0].resolution.xres,
10617 attr->values[0].resolution.yres,
10618 attr->values[0].resolution.units == IPP_RES_PER_INCH ?
10619 "dpi" : "dpc");
10620 printer->num_options = cupsAddOption(name, value,
10621 printer->num_options,
10622 &(printer->options));
10623 cupsdLogMessage(CUPSD_LOG_DEBUG,
10624 "Setting %s to %s...", attr->name, value);
10625 break;
10626
10627 default :
10628 /* Do nothing for other values */
10629 break;
10630 }
10631 }
10632 }
10633
10634
10635 /*
10636 * 'start_printer()' - Start a printer.
10637 */
10638
10639 static void
10640 start_printer(cupsd_client_t *con, /* I - Client connection */
10641 ipp_attribute_t *uri) /* I - Printer URI */
10642 {
10643 http_status_t status; /* Policy status */
10644 cups_ptype_t dtype; /* Destination type (printer/class) */
10645 cupsd_printer_t *printer; /* Printer data */
10646
10647
10648 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con,
10649 con->http.fd, uri->values[0].string.text);
10650
10651 /*
10652 * Is the destination valid?
10653 */
10654
10655 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10656 {
10657 /*
10658 * Bad URI...
10659 */
10660
10661 send_ipp_status(con, IPP_NOT_FOUND,
10662 _("The printer or class was not found."));
10663 return;
10664 }
10665
10666 /*
10667 * Check policy...
10668 */
10669
10670 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10671 {
10672 send_http_error(con, status, printer);
10673 return;
10674 }
10675
10676 /*
10677 * Start the printer...
10678 */
10679
10680 printer->state_message[0] = '\0';
10681
10682 cupsdStartPrinter(printer, 1);
10683
10684 if (dtype & CUPS_PRINTER_CLASS)
10685 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".",
10686 printer->name, get_username(con));
10687 else
10688 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".",
10689 printer->name, get_username(con));
10690
10691 cupsdCheckJobs();
10692
10693 /*
10694 * Everything was ok, so return OK status...
10695 */
10696
10697 con->response->request.status.status_code = IPP_OK;
10698 }
10699
10700
10701 /*
10702 * 'stop_printer()' - Stop a printer.
10703 */
10704
10705 static void
10706 stop_printer(cupsd_client_t *con, /* I - Client connection */
10707 ipp_attribute_t *uri) /* I - Printer URI */
10708 {
10709 http_status_t status; /* Policy status */
10710 cups_ptype_t dtype; /* Destination type (printer/class) */
10711 cupsd_printer_t *printer; /* Printer data */
10712 ipp_attribute_t *attr; /* printer-state-message attribute */
10713
10714
10715 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con,
10716 con->http.fd, uri->values[0].string.text);
10717
10718 /*
10719 * Is the destination valid?
10720 */
10721
10722 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10723 {
10724 /*
10725 * Bad URI...
10726 */
10727
10728 send_ipp_status(con, IPP_NOT_FOUND,
10729 _("The printer or class was not found."));
10730 return;
10731 }
10732
10733 /*
10734 * Check policy...
10735 */
10736
10737 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10738 {
10739 send_http_error(con, status, printer);
10740 return;
10741 }
10742
10743 /*
10744 * Stop the printer...
10745 */
10746
10747 if ((attr = ippFindAttribute(con->request, "printer-state-message",
10748 IPP_TAG_TEXT)) == NULL)
10749 strcpy(printer->state_message, "Paused");
10750 else
10751 {
10752 strlcpy(printer->state_message, attr->values[0].string.text,
10753 sizeof(printer->state_message));
10754 }
10755
10756 cupsdStopPrinter(printer, 1);
10757
10758 if (dtype & CUPS_PRINTER_CLASS)
10759 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".",
10760 printer->name, get_username(con));
10761 else
10762 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".",
10763 printer->name, get_username(con));
10764
10765 /*
10766 * Everything was ok, so return OK status...
10767 */
10768
10769 con->response->request.status.status_code = IPP_OK;
10770 }
10771
10772
10773 /*
10774 * 'url_encode_attr()' - URL-encode a string attribute.
10775 */
10776
10777 static void
10778 url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */
10779 char *buffer,/* I - String buffer */
10780 int bufsize)/* I - Size of buffer */
10781 {
10782 int i; /* Looping var */
10783 char *bufptr, /* Pointer into buffer */
10784 *bufend; /* End of buffer */
10785
10786
10787 strlcpy(buffer, attr->name, bufsize);
10788 bufptr = buffer + strlen(buffer);
10789 bufend = buffer + bufsize - 1;
10790
10791 for (i = 0; i < attr->num_values; i ++)
10792 {
10793 if (bufptr >= bufend)
10794 break;
10795
10796 if (i)
10797 *bufptr++ = ',';
10798 else
10799 *bufptr++ = '=';
10800
10801 if (bufptr >= bufend)
10802 break;
10803
10804 *bufptr++ = '\'';
10805
10806 bufptr = url_encode_string(attr->values[i].string.text,
10807 bufptr, bufend - bufptr + 1);
10808
10809 if (bufptr >= bufend)
10810 break;
10811
10812 *bufptr++ = '\'';
10813 }
10814
10815 *bufptr = '\0';
10816 }
10817
10818
10819 /*
10820 * 'url_encode_string()' - URL-encode a string.
10821 */
10822
10823 static char * /* O - End of string */
10824 url_encode_string(const char *s, /* I - String */
10825 char *buffer, /* I - String buffer */
10826 int bufsize) /* I - Size of buffer */
10827 {
10828 char *bufptr, /* Pointer into buffer */
10829 *bufend; /* End of buffer */
10830 static const char *hex = "0123456789ABCDEF";
10831 /* Hex digits */
10832
10833
10834 bufptr = buffer;
10835 bufend = buffer + bufsize - 1;
10836
10837 while (*s && bufptr < bufend)
10838 {
10839 if (*s == ' ' || *s == '%')
10840 {
10841 if (bufptr >= (bufend - 2))
10842 break;
10843
10844 *bufptr++ = '%';
10845 *bufptr++ = hex[(*s >> 4) & 15];
10846 *bufptr++ = hex[*s & 15];
10847
10848 s ++;
10849 }
10850 else if (*s == '\'' || *s == '\\')
10851 {
10852 if (bufptr >= (bufend - 1))
10853 break;
10854
10855 *bufptr++ = '\\';
10856 *bufptr++ = *s++;
10857 }
10858 else
10859 *bufptr++ = *s++;
10860 }
10861
10862 *bufptr = '\0';
10863
10864 return (bufptr);
10865 }
10866
10867
10868 /*
10869 * 'user_allowed()' - See if a user is allowed to print to a queue.
10870 */
10871
10872 static int /* O - 0 if not allowed, 1 if allowed */
10873 user_allowed(cupsd_printer_t *p, /* I - Printer or class */
10874 const char *username) /* I - Username */
10875 {
10876 int i; /* Looping var */
10877 struct passwd *pw; /* User password data */
10878 char baseuser[256], /* Base username */
10879 *baseptr; /* Pointer to "@" in base username */
10880
10881
10882 if (p->num_users == 0)
10883 return (1);
10884
10885 if (!strcmp(username, "root"))
10886 return (1);
10887
10888 if (strchr(username, '@'))
10889 {
10890 /*
10891 * Strip @REALM for username check...
10892 */
10893
10894 strlcpy(baseuser, username, sizeof(baseuser));
10895
10896 if ((baseptr = strchr(baseuser, '@')) != NULL)
10897 *baseptr = '\0';
10898
10899 username = baseuser;
10900 }
10901
10902 pw = getpwnam(username);
10903 endpwent();
10904
10905 for (i = 0; i < p->num_users; i ++)
10906 {
10907 if (p->users[i][0] == '@')
10908 {
10909 /*
10910 * Check group membership...
10911 */
10912
10913 if (cupsdCheckGroup(username, pw, p->users[i] + 1))
10914 break;
10915 }
10916 else if (p->users[i][0] == '#')
10917 {
10918 /*
10919 * Check UUID...
10920 */
10921
10922 if (cupsdCheckGroup(username, pw, p->users[i]))
10923 break;
10924 }
10925 else if (!strcasecmp(username, p->users[i]))
10926 break;
10927 }
10928
10929 return ((i < p->num_users) != p->deny_users);
10930 }
10931
10932
10933 /*
10934 * 'validate_job()' - Validate printer options and destination.
10935 */
10936
10937 static void
10938 validate_job(cupsd_client_t *con, /* I - Client connection */
10939 ipp_attribute_t *uri) /* I - Printer URI */
10940 {
10941 http_status_t status; /* Policy status */
10942 ipp_attribute_t *attr; /* Current attribute */
10943 ipp_attribute_t *format; /* Document-format attribute */
10944 cups_ptype_t dtype; /* Destination type (printer/class) */
10945 char super[MIME_MAX_SUPER],
10946 /* Supertype of file */
10947 type[MIME_MAX_TYPE];
10948 /* Subtype of file */
10949 cupsd_printer_t *printer; /* Printer */
10950
10951
10952 cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con,
10953 con->http.fd, uri->values[0].string.text);
10954
10955 /*
10956 * OK, see if the client is sending the document compressed - CUPS
10957 * doesn't support compression yet...
10958 */
10959
10960 if ((attr = ippFindAttribute(con->request, "compression",
10961 IPP_TAG_KEYWORD)) != NULL &&
10962 !strcmp(attr->values[0].string.text, "none"))
10963 {
10964 send_ipp_status(con, IPP_ATTRIBUTES,
10965 _("Unsupported compression attribute %s!"),
10966 attr->values[0].string.text);
10967 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
10968 "compression", NULL, attr->values[0].string.text);
10969 return;
10970 }
10971
10972 /*
10973 * Is it a format we support?
10974 */
10975
10976 if ((format = ippFindAttribute(con->request, "document-format",
10977 IPP_TAG_MIMETYPE)) != NULL)
10978 {
10979 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
10980 super, type) != 2)
10981 {
10982 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"),
10983 format->values[0].string.text);
10984 return;
10985 }
10986
10987 if ((strcmp(super, "application") || strcmp(type, "octet-stream")) &&
10988 !mimeType(MimeDatabase, super, type))
10989 {
10990 cupsdLogMessage(CUPSD_LOG_INFO,
10991 "Hint: Do you have the raw file printing rules enabled?");
10992 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
10993 _("Unsupported format \"%s\"!"),
10994 format->values[0].string.text);
10995 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
10996 "document-format", NULL, format->values[0].string.text);
10997 return;
10998 }
10999 }
11000
11001 /*
11002 * Is the destination valid?
11003 */
11004
11005 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
11006 {
11007 /*
11008 * Bad URI...
11009 */
11010
11011 send_ipp_status(con, IPP_NOT_FOUND,
11012 _("The printer or class was not found."));
11013 return;
11014 }
11015
11016 /*
11017 * Check policy...
11018 */
11019
11020 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
11021 {
11022 send_http_error(con, status, printer);
11023 return;
11024 }
11025
11026 /*
11027 * Everything was ok, so return OK status...
11028 */
11029
11030 con->response->request.status.status_code = IPP_OK;
11031 }
11032
11033
11034 /*
11035 * 'validate_name()' - Make sure the printer name only contains valid chars.
11036 */
11037
11038 static int /* O - 0 if name is no good, 1 if good */
11039 validate_name(const char *name) /* I - Name to check */
11040 {
11041 const char *ptr; /* Pointer into name */
11042
11043
11044 /*
11045 * Scan the whole name...
11046 */
11047
11048 for (ptr = name; *ptr; ptr ++)
11049 if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
11050 return (0);
11051
11052 /*
11053 * All the characters are good; validate the length, too...
11054 */
11055
11056 return ((ptr - name) < 128);
11057 }
11058
11059
11060 /*
11061 * 'validate_user()' - Validate the user for the request.
11062 */
11063
11064 static int /* O - 1 if permitted, 0 otherwise */
11065 validate_user(cupsd_job_t *job, /* I - Job */
11066 cupsd_client_t *con, /* I - Client connection */
11067 const char *owner, /* I - Owner of job/resource */
11068 char *username, /* O - Authenticated username */
11069 int userlen) /* I - Length of username */
11070 {
11071 cupsd_printer_t *printer; /* Printer for job */
11072
11073
11074 cupsdLogMessage(CUPSD_LOG_DEBUG2,
11075 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
11076 "userlen=%d)",
11077 job->id, con ? con->http.fd : 0,
11078 owner ? owner : "(null)", username, userlen);
11079
11080 /*
11081 * Validate input...
11082 */
11083
11084 if (!con || !owner || !username || userlen <= 0)
11085 return (0);
11086
11087 /*
11088 * Get the best authenticated username that is available.
11089 */
11090
11091 strlcpy(username, get_username(con), userlen);
11092
11093 /*
11094 * Check the username against the owner...
11095 */
11096
11097 printer = cupsdFindDest(job->dest);
11098
11099 return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr,
11100 con, owner) == HTTP_OK);
11101 }
11102
11103
11104 /*
11105 * End of "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $".
11106 */