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