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