]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/ipp.c
Merge changes from CUPS 1.4svn-r8305.
[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->attrs)
303 {
304 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
305 "%04X %s No attributes in request",
306 IPP_BAD_REQUEST, con->http.hostname);
307
308 send_ipp_status(con, IPP_BAD_REQUEST, _("No attributes in request!"));
309 }
310 else
311 {
312 /*
313 * Make sure that the attributes are provided in the correct order and
314 * don't repeat groups...
315 */
316
317 for (attr = con->request->attrs, group = attr->group_tag;
318 attr;
319 attr = attr->next)
320 if (attr->group_tag < group && attr->group_tag != IPP_TAG_ZERO)
321 {
322 /*
323 * Out of order; return an error...
324 */
325
326 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
327 "%04X %s Attribute groups are out of order",
328 IPP_BAD_REQUEST, con->http.hostname);
329
330 send_ipp_status(con, IPP_BAD_REQUEST,
331 _("Attribute groups are out of order (%x < %x)!"),
332 attr->group_tag, group);
333 break;
334 }
335 else
336 group = attr->group_tag;
337
338 if (!attr)
339 {
340 /*
341 * Then make sure that the first three attributes are:
342 *
343 * attributes-charset
344 * attributes-natural-language
345 * printer-uri/job-uri
346 */
347
348 attr = con->request->attrs;
349 if (attr && !strcmp(attr->name, "attributes-charset") &&
350 (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET)
351 charset = attr;
352 else
353 charset = NULL;
354
355 if (attr)
356 attr = attr->next;
357
358 if (attr && !strcmp(attr->name, "attributes-natural-language") &&
359 (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE)
360 language = attr;
361 else
362 language = NULL;
363
364 if ((attr = ippFindAttribute(con->request, "printer-uri",
365 IPP_TAG_URI)) != NULL)
366 uri = attr;
367 else if ((attr = ippFindAttribute(con->request, "job-uri",
368 IPP_TAG_URI)) != NULL)
369 uri = attr;
370 else if (con->request->request.op.operation_id == CUPS_GET_PPD)
371 uri = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME);
372 else
373 uri = NULL;
374
375 if (charset)
376 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
377 "attributes-charset", NULL,
378 charset->values[0].string.text);
379 else
380 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
381 "attributes-charset", NULL, DefaultCharset);
382
383 if (language)
384 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
385 "attributes-natural-language", NULL,
386 language->values[0].string.text);
387 else
388 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
389 "attributes-natural-language", NULL, DefaultLanguage);
390
391 if (charset &&
392 strcasecmp(charset->values[0].string.text, "us-ascii") &&
393 strcasecmp(charset->values[0].string.text, "utf-8"))
394 {
395 /*
396 * Bad character set...
397 */
398
399 cupsdLogMessage(CUPSD_LOG_ERROR, "Unsupported character set \"%s\"!",
400 charset->values[0].string.text);
401 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
402 "%04X %s Unsupported attributes-charset value \"%s\"",
403 IPP_CHARSET, con->http.hostname,
404 charset->values[0].string.text);
405 send_ipp_status(con, IPP_BAD_REQUEST,
406 _("Unsupported character set \"%s\"!"),
407 charset->values[0].string.text);
408 }
409 else if (!charset || !language ||
410 (!uri &&
411 con->request->request.op.operation_id != CUPS_GET_DEFAULT &&
412 con->request->request.op.operation_id != CUPS_GET_PRINTERS &&
413 con->request->request.op.operation_id != CUPS_GET_CLASSES &&
414 con->request->request.op.operation_id != CUPS_GET_DEVICES &&
415 con->request->request.op.operation_id != CUPS_GET_PPDS))
416 {
417 /*
418 * Return an error, since attributes-charset,
419 * attributes-natural-language, and printer-uri/job-uri are required
420 * for all operations.
421 */
422
423 if (!charset)
424 {
425 cupsdLogMessage(CUPSD_LOG_ERROR,
426 "Missing attributes-charset attribute!");
427
428 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
429 "%04X %s Missing attributes-charset attribute",
430 IPP_BAD_REQUEST, con->http.hostname);
431 }
432
433 if (!language)
434 {
435 cupsdLogMessage(CUPSD_LOG_ERROR,
436 "Missing attributes-natural-language attribute!");
437
438 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
439 "%04X %s Missing attributes-natural-language attribute",
440 IPP_BAD_REQUEST, con->http.hostname);
441 }
442
443 if (!uri)
444 {
445 cupsdLogMessage(CUPSD_LOG_ERROR,
446 "Missing printer-uri, job-uri, or ppd-name "
447 "attribute!");
448
449 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
450 "%04X %s Missing printer-uri, job-uri, or ppd-name "
451 "attribute", IPP_BAD_REQUEST, con->http.hostname);
452 }
453
454 cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow...");
455
456 for (attr = con->request->attrs; attr; attr = attr->next)
457 cupsdLogMessage(CUPSD_LOG_DEBUG,
458 "attr \"%s\": group_tag = %x, value_tag = %x",
459 attr->name ? attr->name : "(null)", attr->group_tag,
460 attr->value_tag);
461
462 cupsdLogMessage(CUPSD_LOG_DEBUG, "End of attributes...");
463
464 send_ipp_status(con, IPP_BAD_REQUEST,
465 _("Missing required attributes!"));
466 }
467 else
468 {
469 /*
470 * OK, all the checks pass so far; make sure requesting-user-name is
471 * not "root" from a remote host...
472 */
473
474 if ((username = ippFindAttribute(con->request, "requesting-user-name",
475 IPP_TAG_NAME)) != NULL)
476 {
477 /*
478 * Check for root user...
479 */
480
481 if (!strcmp(username->values[0].string.text, "root") &&
482 strcasecmp(con->http.hostname, "localhost") &&
483 strcmp(con->username, "root"))
484 {
485 /*
486 * Remote unauthenticated user masquerading as local root...
487 */
488
489 _cupsStrFree(username->values[0].string.text);
490 username->values[0].string.text = _cupsStrAlloc(RemoteRoot);
491 }
492 }
493
494 if ((attr = ippFindAttribute(con->request, "notify-subscription-id",
495 IPP_TAG_INTEGER)) != NULL)
496 sub_id = attr->values[0].integer;
497 else
498 sub_id = 0;
499
500 /*
501 * Then try processing the operation...
502 */
503
504 if (uri)
505 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s",
506 ippOpString(con->request->request.op.operation_id),
507 uri->values[0].string.text);
508 else
509 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s",
510 ippOpString(con->request->request.op.operation_id));
511
512 switch (con->request->request.op.operation_id)
513 {
514 case IPP_PRINT_JOB :
515 print_job(con, uri);
516 break;
517
518 case IPP_VALIDATE_JOB :
519 validate_job(con, uri);
520 break;
521
522 case IPP_CREATE_JOB :
523 create_job(con, uri);
524 break;
525
526 case IPP_SEND_DOCUMENT :
527 send_document(con, uri);
528 break;
529
530 case IPP_CANCEL_JOB :
531 cancel_job(con, uri);
532 break;
533
534 case IPP_GET_JOB_ATTRIBUTES :
535 get_job_attrs(con, uri);
536 break;
537
538 case IPP_GET_JOBS :
539 get_jobs(con, uri);
540 break;
541
542 case IPP_GET_PRINTER_ATTRIBUTES :
543 get_printer_attrs(con, uri);
544 break;
545
546 case IPP_GET_PRINTER_SUPPORTED_VALUES :
547 get_printer_supported(con, uri);
548 break;
549
550 case IPP_HOLD_JOB :
551 hold_job(con, uri);
552 break;
553
554 case IPP_RELEASE_JOB :
555 release_job(con, uri);
556 break;
557
558 case IPP_RESTART_JOB :
559 restart_job(con, uri);
560 break;
561
562 case IPP_PAUSE_PRINTER :
563 stop_printer(con, uri);
564 break;
565
566 case IPP_RESUME_PRINTER :
567 start_printer(con, uri);
568 break;
569
570 case IPP_PURGE_JOBS :
571 cancel_all_jobs(con, uri);
572 break;
573
574 case IPP_SET_JOB_ATTRIBUTES :
575 set_job_attrs(con, uri);
576 break;
577
578 case IPP_SET_PRINTER_ATTRIBUTES :
579 set_printer_attrs(con, uri);
580 break;
581
582 case IPP_HOLD_NEW_JOBS :
583 hold_new_jobs(con, uri);
584 break;
585
586 case IPP_RELEASE_HELD_NEW_JOBS :
587 release_held_new_jobs(con, uri);
588 break;
589
590 case CUPS_GET_DEFAULT :
591 get_default(con);
592 break;
593
594 case CUPS_GET_PRINTERS :
595 get_printers(con, 0);
596 break;
597
598 case CUPS_GET_CLASSES :
599 get_printers(con, CUPS_PRINTER_CLASS);
600 break;
601
602 case CUPS_ADD_PRINTER :
603 add_printer(con, uri);
604 break;
605
606 case CUPS_DELETE_PRINTER :
607 delete_printer(con, uri);
608 break;
609
610 case CUPS_ADD_CLASS :
611 add_class(con, uri);
612 break;
613
614 case CUPS_DELETE_CLASS :
615 delete_printer(con, uri);
616 break;
617
618 case CUPS_ACCEPT_JOBS :
619 case IPP_ENABLE_PRINTER :
620 accept_jobs(con, uri);
621 break;
622
623 case CUPS_REJECT_JOBS :
624 case IPP_DISABLE_PRINTER :
625 reject_jobs(con, uri);
626 break;
627
628 case CUPS_SET_DEFAULT :
629 set_default(con, uri);
630 break;
631
632 case CUPS_GET_DEVICES :
633 get_devices(con);
634 break;
635
636 case CUPS_GET_DOCUMENT :
637 get_document(con, uri);
638 break;
639
640 case CUPS_GET_PPD :
641 get_ppd(con, uri);
642 break;
643
644 case CUPS_GET_PPDS :
645 get_ppds(con);
646 break;
647
648 case CUPS_MOVE_JOB :
649 move_job(con, uri);
650 break;
651
652 case CUPS_AUTHENTICATE_JOB :
653 authenticate_job(con, uri);
654 break;
655
656 case IPP_CREATE_PRINTER_SUBSCRIPTION :
657 case IPP_CREATE_JOB_SUBSCRIPTION :
658 create_subscription(con, uri);
659 break;
660
661 case IPP_GET_SUBSCRIPTION_ATTRIBUTES :
662 get_subscription_attrs(con, sub_id);
663 break;
664
665 case IPP_GET_SUBSCRIPTIONS :
666 get_subscriptions(con, uri);
667 break;
668
669 case IPP_RENEW_SUBSCRIPTION :
670 renew_subscription(con, sub_id);
671 break;
672
673 case IPP_CANCEL_SUBSCRIPTION :
674 cancel_subscription(con, sub_id);
675 break;
676
677 case IPP_GET_NOTIFICATIONS :
678 get_notifications(con);
679 break;
680
681 default :
682 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
683 "%04X %s Operation %04X (%s) not supported",
684 IPP_OPERATION_NOT_SUPPORTED, con->http.hostname,
685 con->request->request.op.operation_id,
686 ippOpString(con->request->request.op.operation_id));
687
688 send_ipp_status(con, IPP_OPERATION_NOT_SUPPORTED,
689 _("%s not supported!"),
690 ippOpString(
691 con->request->request.op.operation_id));
692 break;
693 }
694 }
695 }
696 }
697
698 if (con->response)
699 {
700 /*
701 * Sending data from the scheduler...
702 */
703
704 cupsdLogMessage(con->response->request.status.status_code
705 >= IPP_BAD_REQUEST &&
706 con->response->request.status.status_code
707 != IPP_NOT_FOUND ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
708 "Returning IPP %s for %s (%s) from %s",
709 ippErrorString(con->response->request.status.status_code),
710 ippOpString(con->request->request.op.operation_id),
711 uri ? uri->values[0].string.text : "no URI",
712 con->http.hostname);
713
714 if (LogLevel == CUPSD_LOG_DEBUG2)
715 cupsdLogMessage(CUPSD_LOG_DEBUG2,
716 "cupsdProcessIPPRequest: ippLength(response)=%ld",
717 (long)ippLength(con->response));
718
719 if (cupsdSendHeader(con, HTTP_OK, "application/ipp", CUPSD_AUTH_NONE))
720 {
721 #ifdef CUPSD_USE_CHUNKING
722 /*
723 * Because older versions of CUPS (1.1.17 and older) and some IPP
724 * clients do not implement chunking properly, we cannot use
725 * chunking by default. This may become the default in future
726 * CUPS releases, or we might add a configuration directive for
727 * it.
728 */
729
730 if (con->http.version == HTTP_1_1)
731 {
732 if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n\r\n") < 0)
733 return (0);
734
735 if (cupsdFlushHeader(con) < 0)
736 return (0);
737
738 con->http.data_encoding = HTTP_ENCODE_CHUNKED;
739 }
740 else
741 #endif /* CUPSD_USE_CHUNKING */
742 {
743 size_t length; /* Length of response */
744
745
746 length = ippLength(con->response);
747
748 if (con->file >= 0 && !con->pipe_pid)
749 {
750 struct stat fileinfo; /* File information */
751
752
753 if (!fstat(con->file, &fileinfo))
754 length += fileinfo.st_size;
755 }
756
757 if (httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n\r\n",
758 CUPS_LLCAST length) < 0)
759 return (0);
760
761 if (cupsdFlushHeader(con) < 0)
762 return (0);
763
764 con->http.data_encoding = HTTP_ENCODE_LENGTH;
765 con->http.data_remaining = length;
766
767 if (con->http.data_remaining <= INT_MAX)
768 con->http._data_remaining = con->http.data_remaining;
769 else
770 con->http._data_remaining = INT_MAX;
771 }
772
773 cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient,
774 (cupsd_selfunc_t)cupsdWriteClient, con);
775
776 /*
777 * Tell the caller the response header was sent successfully...
778 */
779
780 return (1);
781 }
782 else
783 {
784 /*
785 * Tell the caller the response header could not be sent...
786 */
787
788 return (0);
789 }
790 }
791 else
792 {
793 /*
794 * Sending data from a subprocess like cups-deviced; tell the caller
795 * everything is A-OK so far...
796 */
797
798 return (1);
799 }
800 }
801
802
803 /*
804 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
805 */
806
807 int /* O - 0 on success, -1 on error */
808 cupsdTimeoutJob(cupsd_job_t *job) /* I - Job to timeout */
809 {
810 cupsd_printer_t *printer; /* Destination printer or class */
811 ipp_attribute_t *attr; /* job-sheets attribute */
812 int kbytes; /* Kilobytes in banner */
813
814
815 job->pending_timeout = 0;
816
817 /*
818 * See if we need to add the ending sheet...
819 */
820
821 printer = cupsdFindDest(job->dest);
822 attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
823
824 if (printer &&
825 !(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) &&
826 attr && attr->num_values > 1)
827 {
828 /*
829 * Yes...
830 */
831
832 cupsdLogJob(job, CUPSD_LOG_INFO, "Adding end banner page \"%s\".",
833 attr->values[1].string.text);
834
835 if ((kbytes = copy_banner(NULL, job, attr->values[1].string.text)) < 0)
836 return (-1);
837
838 cupsdUpdateQuota(printer, job->username, 0, kbytes);
839 }
840
841 return (0);
842 }
843
844
845 /*
846 * 'accept_jobs()' - Accept print jobs to a printer.
847 */
848
849 static void
850 accept_jobs(cupsd_client_t *con, /* I - Client connection */
851 ipp_attribute_t *uri) /* I - Printer or class URI */
852 {
853 http_status_t status; /* Policy status */
854 cups_ptype_t dtype; /* Destination type (printer/class) */
855 cupsd_printer_t *printer; /* Printer data */
856
857
858 cupsdLogMessage(CUPSD_LOG_DEBUG2, "accept_jobs(%p[%d], %s)", con,
859 con->http.fd, uri->values[0].string.text);
860
861 /*
862 * Is the destination valid?
863 */
864
865 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
866 {
867 /*
868 * Bad URI...
869 */
870
871 send_ipp_status(con, IPP_NOT_FOUND,
872 _("The printer or class was not found."));
873 return;
874 }
875
876 /*
877 * Check policy...
878 */
879
880 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
881 {
882 send_http_error(con, status, printer);
883 return;
884 }
885
886 /*
887 * Accept jobs sent to the printer...
888 */
889
890 printer->accepting = 1;
891 printer->state_message[0] = '\0';
892
893 cupsdAddPrinterHistory(printer);
894
895 if (dtype & CUPS_PRINTER_CLASS)
896 {
897 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
898
899 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" now accepting jobs (\"%s\").",
900 printer->name, get_username(con));
901 }
902 else
903 {
904 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
905
906 cupsdLogMessage(CUPSD_LOG_INFO,
907 "Printer \"%s\" now accepting jobs (\"%s\").",
908 printer->name, get_username(con));
909 }
910
911 /*
912 * Everything was ok, so return OK status...
913 */
914
915 con->response->request.status.status_code = IPP_OK;
916 }
917
918
919 /*
920 * 'add_class()' - Add a class to the system.
921 */
922
923 static void
924 add_class(cupsd_client_t *con, /* I - Client connection */
925 ipp_attribute_t *uri) /* I - URI of class */
926 {
927 http_status_t status; /* Policy status */
928 int i; /* Looping var */
929 char scheme[HTTP_MAX_URI], /* Method portion of URI */
930 username[HTTP_MAX_URI], /* Username portion of URI */
931 host[HTTP_MAX_URI], /* Host portion of URI */
932 resource[HTTP_MAX_URI]; /* Resource portion of URI */
933 int port; /* Port portion of URI */
934 cupsd_printer_t *pclass, /* Class */
935 *member; /* Member printer/class */
936 cups_ptype_t dtype; /* Destination type */
937 ipp_attribute_t *attr; /* Printer attribute */
938 int modify; /* Non-zero if we just modified */
939 char newname[IPP_MAX_NAME]; /* New class name */
940 int need_restart_job; /* Need to restart job? */
941
942
943 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_class(%p[%d], %s)", con,
944 con->http.fd, uri->values[0].string.text);
945
946 /*
947 * Do we have a valid URI?
948 */
949
950 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
951 sizeof(scheme), username, sizeof(username), host,
952 sizeof(host), &port, resource, sizeof(resource));
953
954
955 if (strncmp(resource, "/classes/", 9) || strlen(resource) == 9)
956 {
957 /*
958 * No, return an error...
959 */
960
961 send_ipp_status(con, IPP_BAD_REQUEST,
962 _("The printer-uri must be of the form "
963 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
964 return;
965 }
966
967 /*
968 * Do we have a valid printer name?
969 */
970
971 if (!validate_name(resource + 9))
972 {
973 /*
974 * No, return an error...
975 */
976
977 send_ipp_status(con, IPP_BAD_REQUEST,
978 _("The printer-uri \"%s\" contains invalid characters."),
979 uri->values[0].string.text);
980 return;
981 }
982
983 /*
984 * See if the class already exists; if not, create a new class...
985 */
986
987 if ((pclass = cupsdFindClass(resource + 9)) == NULL)
988 {
989 /*
990 * Class doesn't exist; see if we have a printer of the same name...
991 */
992
993 if ((pclass = cupsdFindPrinter(resource + 9)) != NULL &&
994 !(pclass->type & CUPS_PRINTER_DISCOVERED))
995 {
996 /*
997 * Yes, return an error...
998 */
999
1000 send_ipp_status(con, IPP_NOT_POSSIBLE,
1001 _("A printer named \"%s\" already exists!"),
1002 resource + 9);
1003 return;
1004 }
1005
1006 /*
1007 * No, check the default policy and then add the class...
1008 */
1009
1010 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
1011 {
1012 send_http_error(con, status, NULL);
1013 return;
1014 }
1015
1016 pclass = cupsdAddClass(resource + 9);
1017 modify = 0;
1018 }
1019 else if (pclass->type & CUPS_PRINTER_IMPLICIT)
1020 {
1021 /*
1022 * Check the default policy, then rename the implicit class to "AnyClass"
1023 * or remove it...
1024 */
1025
1026 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
1027 {
1028 send_http_error(con, status, NULL);
1029 return;
1030 }
1031
1032 if (ImplicitAnyClasses)
1033 {
1034 snprintf(newname, sizeof(newname), "Any%s", resource + 9);
1035 cupsdRenamePrinter(pclass, newname);
1036 }
1037 else
1038 cupsdDeletePrinter(pclass, 1);
1039
1040 /*
1041 * Add the class as a new local class...
1042 */
1043
1044 pclass = cupsdAddClass(resource + 9);
1045 modify = 0;
1046 }
1047 else if (pclass->type & CUPS_PRINTER_DISCOVERED)
1048 {
1049 /*
1050 * Check the default policy, then rename the remote class to "Class"...
1051 */
1052
1053 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
1054 {
1055 send_http_error(con, status, NULL);
1056 return;
1057 }
1058
1059 snprintf(newname, sizeof(newname), "%s@%s", resource + 9, pclass->hostname);
1060 cupsdRenamePrinter(pclass, newname);
1061
1062 /*
1063 * Add the class as a new local class...
1064 */
1065
1066 pclass = cupsdAddClass(resource + 9);
1067 modify = 0;
1068 }
1069 else if ((status = cupsdCheckPolicy(pclass->op_policy_ptr, con,
1070 NULL)) != HTTP_OK)
1071 {
1072 send_http_error(con, status, pclass);
1073 return;
1074 }
1075 else
1076 modify = 1;
1077
1078 /*
1079 * Look for attributes and copy them over as needed...
1080 */
1081
1082 need_restart_job = 0;
1083
1084 if ((attr = ippFindAttribute(con->request, "printer-location",
1085 IPP_TAG_TEXT)) != NULL)
1086 cupsdSetString(&pclass->location, attr->values[0].string.text);
1087
1088 if ((attr = ippFindAttribute(con->request, "printer-info",
1089 IPP_TAG_TEXT)) != NULL)
1090 cupsdSetString(&pclass->info, attr->values[0].string.text);
1091
1092 if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs",
1093 IPP_TAG_BOOLEAN)) != NULL)
1094 {
1095 cupsdLogMessage(CUPSD_LOG_INFO,
1096 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
1097 pclass->name, attr->values[0].boolean, pclass->accepting);
1098
1099 pclass->accepting = attr->values[0].boolean;
1100 cupsdAddPrinterHistory(pclass);
1101 }
1102
1103 if ((attr = ippFindAttribute(con->request, "printer-is-shared",
1104 IPP_TAG_BOOLEAN)) != NULL)
1105 {
1106 if (pclass->shared && !attr->values[0].boolean)
1107 cupsdDeregisterPrinter(pclass, 1);
1108
1109 cupsdLogMessage(CUPSD_LOG_INFO,
1110 "Setting %s printer-is-shared to %d (was %d.)",
1111 pclass->name, attr->values[0].boolean, pclass->shared);
1112
1113 pclass->shared = attr->values[0].boolean;
1114 }
1115
1116 if ((attr = ippFindAttribute(con->request, "printer-state",
1117 IPP_TAG_ENUM)) != NULL)
1118 {
1119 if (attr->values[0].integer != IPP_PRINTER_IDLE &&
1120 attr->values[0].integer != IPP_PRINTER_STOPPED)
1121 {
1122 send_ipp_status(con, IPP_BAD_REQUEST,
1123 _("Attempt to set %s printer-state to bad value %d!"),
1124 pclass->name, attr->values[0].integer);
1125 return;
1126 }
1127
1128 cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
1129 pclass->name, attr->values[0].integer, pclass->state);
1130
1131 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1132 cupsdStopPrinter(pclass, 0);
1133 else
1134 {
1135 cupsdSetPrinterState(pclass, (ipp_pstate_t)(attr->values[0].integer), 0);
1136 need_restart_job = 1;
1137 }
1138 }
1139 if ((attr = ippFindAttribute(con->request, "printer-state-message",
1140 IPP_TAG_TEXT)) != NULL)
1141 {
1142 strlcpy(pclass->state_message, attr->values[0].string.text,
1143 sizeof(pclass->state_message));
1144 cupsdAddPrinterHistory(pclass);
1145 }
1146 if ((attr = ippFindAttribute(con->request, "member-uris",
1147 IPP_TAG_URI)) != NULL)
1148 {
1149 /*
1150 * Clear the printer array as needed...
1151 */
1152
1153 need_restart_job = 1;
1154
1155 if (pclass->num_printers > 0)
1156 {
1157 free(pclass->printers);
1158 pclass->num_printers = 0;
1159 }
1160
1161 /*
1162 * Add each printer or class that is listed...
1163 */
1164
1165 for (i = 0; i < attr->num_values; i ++)
1166 {
1167 /*
1168 * Search for the printer or class URI...
1169 */
1170
1171 if (!cupsdValidateDest(attr->values[i].string.text, &dtype, &member))
1172 {
1173 /*
1174 * Bad URI...
1175 */
1176
1177 send_ipp_status(con, IPP_NOT_FOUND,
1178 _("The printer or class was not found."));
1179 return;
1180 }
1181
1182 /*
1183 * Add it to the class...
1184 */
1185
1186 cupsdAddPrinterToClass(pclass, member);
1187 }
1188 }
1189
1190 set_printer_defaults(con, pclass);
1191
1192 if ((attr = ippFindAttribute(con->request, "auth-info-required",
1193 IPP_TAG_KEYWORD)) != NULL)
1194 cupsdSetAuthInfoRequired(pclass, NULL, attr);
1195
1196 /*
1197 * Update the printer class attributes and return...
1198 */
1199
1200 cupsdSetPrinterAttrs(pclass);
1201 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
1202
1203 if (need_restart_job && pclass->job)
1204 {
1205 cupsd_job_t *job;
1206
1207 /*
1208 * Stop the current job and then restart it below...
1209 */
1210
1211 job = (cupsd_job_t *)pclass->job;
1212
1213 cupsdStopJob(job, 1);
1214
1215 job->state->values[0].integer = IPP_JOB_PENDING;
1216 job->state_value = IPP_JOB_PENDING;
1217 }
1218
1219 if (need_restart_job)
1220 cupsdCheckJobs();
1221
1222 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
1223
1224 if (modify)
1225 {
1226 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, pclass, NULL,
1227 "Class \"%s\" modified by \"%s\".", pclass->name,
1228 get_username(con));
1229
1230 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" modified by \"%s\".",
1231 pclass->name, get_username(con));
1232 }
1233 else
1234 {
1235 cupsdAddPrinterHistory(pclass);
1236
1237 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, pclass, NULL,
1238 "New class \"%s\" added by \"%s\".", pclass->name,
1239 get_username(con));
1240
1241 cupsdLogMessage(CUPSD_LOG_INFO, "New class \"%s\" added by \"%s\".",
1242 pclass->name, get_username(con));
1243 }
1244
1245 con->response->request.status.status_code = IPP_OK;
1246 }
1247
1248
1249 /*
1250 * 'add_file()' - Add a file to a job.
1251 */
1252
1253 static int /* O - 0 on success, -1 on error */
1254 add_file(cupsd_client_t *con, /* I - Connection to client */
1255 cupsd_job_t *job, /* I - Job to add to */
1256 mime_type_t *filetype, /* I - Type of file */
1257 int compression) /* I - Compression */
1258 {
1259 mime_type_t **filetypes; /* New filetypes array... */
1260 int *compressions; /* New compressions array... */
1261
1262
1263 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1264 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1265 "compression=%d)", con, con ? con->http.fd : -1, job->id,
1266 filetype->super, filetype->type, compression);
1267
1268 /*
1269 * Add the file to the job...
1270 */
1271
1272 if (job->num_files == 0)
1273 {
1274 compressions = (int *)malloc(sizeof(int));
1275 filetypes = (mime_type_t **)malloc(sizeof(mime_type_t *));
1276 }
1277 else
1278 {
1279 compressions = (int *)realloc(job->compressions,
1280 (job->num_files + 1) * sizeof(int));
1281 filetypes = (mime_type_t **)realloc(job->filetypes,
1282 (job->num_files + 1) *
1283 sizeof(mime_type_t *));
1284 }
1285
1286 if (!compressions || !filetypes)
1287 {
1288 cupsdCancelJob(job, 1, IPP_JOB_ABORTED);
1289
1290 if (con)
1291 send_ipp_status(con, IPP_INTERNAL_ERROR,
1292 _("Unable to allocate memory for file types!"));
1293
1294 return (-1);
1295 }
1296
1297 job->compressions = compressions;
1298 job->compressions[job->num_files] = compression;
1299 job->filetypes = filetypes;
1300 job->filetypes[job->num_files] = filetype;
1301
1302 job->num_files ++;
1303
1304 job->dirty = 1;
1305 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
1306
1307 return (0);
1308 }
1309
1310
1311 /*
1312 * 'add_job()' - Add a job to a print queue.
1313 */
1314
1315 static cupsd_job_t * /* O - Job object */
1316 add_job(cupsd_client_t *con, /* I - Client connection */
1317 cupsd_printer_t *printer, /* I - Destination printer */
1318 mime_type_t *filetype) /* I - First print file type, if any */
1319 {
1320 http_status_t status; /* Policy status */
1321 ipp_attribute_t *attr, /* Current attribute */
1322 *auth_info; /* auth-info attribute */
1323 const char *val; /* Default option value */
1324 int priority; /* Job priority */
1325 char *title; /* Job name/title */
1326 cupsd_job_t *job; /* Current job */
1327 char job_uri[HTTP_MAX_URI]; /* Job URI */
1328 int kbytes; /* Size of print file */
1329 int i; /* Looping var */
1330 int lowerpagerange; /* Page range bound */
1331
1332
1333 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1334 con, con->http.fd, printer, printer->name,
1335 filetype, filetype ? filetype->super : "none",
1336 filetype ? filetype->type : "none");
1337
1338 /*
1339 * Check remote printing to non-shared printer...
1340 */
1341
1342 if (!printer->shared &&
1343 strcasecmp(con->http.hostname, "localhost") &&
1344 strcasecmp(con->http.hostname, ServerName))
1345 {
1346 send_ipp_status(con, IPP_NOT_AUTHORIZED,
1347 _("The printer or class is not shared!"));
1348 return (NULL);
1349 }
1350
1351 /*
1352 * Check policy...
1353 */
1354
1355 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
1356
1357 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
1358 {
1359 send_http_error(con, status, printer);
1360 return (NULL);
1361 }
1362 else if (printer->num_auth_info_required > 0 &&
1363 strcmp(printer->auth_info_required[0], "none") &&
1364 !con->username[0] && !auth_info)
1365 {
1366 send_http_error(con, HTTP_UNAUTHORIZED, printer);
1367 return (NULL);
1368 }
1369 #ifdef HAVE_SSL
1370 else if (auth_info && !con->http.tls &&
1371 !httpAddrLocalhost(con->http.hostaddr))
1372 {
1373 /*
1374 * Require encryption of auth-info over non-local connections...
1375 */
1376
1377 send_http_error(con, HTTP_UPGRADE_REQUIRED, printer);
1378 return (NULL);
1379 }
1380 #endif /* HAVE_SSL */
1381
1382 /*
1383 * See if the printer is accepting jobs...
1384 */
1385
1386 if (!printer->accepting)
1387 {
1388 send_ipp_status(con, IPP_NOT_ACCEPTING,
1389 _("Destination \"%s\" is not accepting jobs."),
1390 printer->name);
1391 return (NULL);
1392 }
1393
1394 /*
1395 * Validate job template attributes; for now just document-format,
1396 * copies, number-up, and page-ranges...
1397 */
1398
1399 if (filetype && printer->filetypes &&
1400 !cupsArrayFind(printer->filetypes, filetype))
1401 {
1402 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
1403 /* MIME media type string */
1404
1405
1406 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
1407 filetype->type);
1408
1409 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
1410 _("Unsupported format \'%s\'!"), mimetype);
1411
1412 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
1413 "document-format", NULL, mimetype);
1414
1415 return (NULL);
1416 }
1417
1418 if ((attr = ippFindAttribute(con->request, "copies",
1419 IPP_TAG_INTEGER)) != NULL)
1420 {
1421 if (attr->values[0].integer < 1 || attr->values[0].integer > MaxCopies)
1422 {
1423 send_ipp_status(con, IPP_ATTRIBUTES, _("Bad copies value %d."),
1424 attr->values[0].integer);
1425 ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER,
1426 "copies", attr->values[0].integer);
1427 return (NULL);
1428 }
1429 }
1430
1431 if ((attr = ippFindAttribute(con->request, "job-sheets",
1432 IPP_TAG_ZERO)) != NULL)
1433 {
1434 if (attr->value_tag != IPP_TAG_KEYWORD &&
1435 attr->value_tag != IPP_TAG_NAME)
1436 {
1437 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value type!"));
1438 return (NULL);
1439 }
1440
1441 if (attr->num_values > 2)
1442 {
1443 send_ipp_status(con, IPP_BAD_REQUEST,
1444 _("Too many job-sheets values (%d > 2)!"),
1445 attr->num_values);
1446 return (NULL);
1447 }
1448
1449 for (i = 0; i < attr->num_values; i ++)
1450 if (strcmp(attr->values[i].string.text, "none") &&
1451 !cupsdFindBanner(attr->values[i].string.text))
1452 {
1453 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value \"%s\"!"),
1454 attr->values[i].string.text);
1455 return (NULL);
1456 }
1457 }
1458
1459 if ((attr = ippFindAttribute(con->request, "number-up",
1460 IPP_TAG_INTEGER)) != NULL)
1461 {
1462 if (attr->values[0].integer != 1 &&
1463 attr->values[0].integer != 2 &&
1464 attr->values[0].integer != 4 &&
1465 attr->values[0].integer != 6 &&
1466 attr->values[0].integer != 9 &&
1467 attr->values[0].integer != 16)
1468 {
1469 send_ipp_status(con, IPP_ATTRIBUTES, _("Bad number-up value %d."),
1470 attr->values[0].integer);
1471 ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER,
1472 "number-up", attr->values[0].integer);
1473 return (NULL);
1474 }
1475 }
1476
1477 if ((attr = ippFindAttribute(con->request, "page-ranges",
1478 IPP_TAG_RANGE)) != NULL)
1479 {
1480 for (i = 0, lowerpagerange = 1; i < attr->num_values; i ++)
1481 {
1482 if (attr->values[i].range.lower < lowerpagerange ||
1483 attr->values[i].range.lower > attr->values[i].range.upper)
1484 {
1485 send_ipp_status(con, IPP_BAD_REQUEST,
1486 _("Bad page-ranges values %d-%d."),
1487 attr->values[i].range.lower,
1488 attr->values[i].range.upper);
1489 return (NULL);
1490 }
1491
1492 lowerpagerange = attr->values[i].range.upper + 1;
1493 }
1494 }
1495
1496 /*
1497 * Make sure we aren't over our limit...
1498 */
1499
1500 if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs)
1501 cupsdCleanJobs();
1502
1503 if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs)
1504 {
1505 send_ipp_status(con, IPP_NOT_POSSIBLE,
1506 _("Too many active jobs."));
1507 return (NULL);
1508 }
1509
1510 if ((i = check_quotas(con, printer)) < 0)
1511 {
1512 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
1513 return (NULL);
1514 }
1515 else if (i == 0)
1516 {
1517 send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print."));
1518 return (NULL);
1519 }
1520
1521 /*
1522 * Create the job and set things up...
1523 */
1524
1525 if ((attr = ippFindAttribute(con->request, "job-priority",
1526 IPP_TAG_INTEGER)) != NULL)
1527 priority = attr->values[0].integer;
1528 else
1529 {
1530 if ((val = cupsGetOption("job-priority", printer->num_options,
1531 printer->options)) != NULL)
1532 priority = atoi(val);
1533 else
1534 priority = 50;
1535
1536 ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
1537 priority);
1538 }
1539
1540 if ((attr = ippFindAttribute(con->request, "job-name",
1541 IPP_TAG_NAME)) != NULL)
1542 title = attr->values[0].string.text;
1543 else
1544 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL,
1545 title = "Untitled");
1546
1547 if ((job = cupsdAddJob(priority, printer->name)) == NULL)
1548 {
1549 send_ipp_status(con, IPP_INTERNAL_ERROR,
1550 _("Unable to add job for destination \"%s\"!"),
1551 printer->name);
1552 return (NULL);
1553 }
1554
1555 job->dtype = printer->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
1556 CUPS_PRINTER_REMOTE);
1557 job->attrs = con->request;
1558 job->dirty = 1;
1559 con->request = ippNewRequest(job->attrs->request.op.operation_id);
1560
1561 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
1562
1563 add_job_uuid(con, job);
1564 apply_printer_defaults(printer, job);
1565
1566 attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME);
1567
1568 if (con->username[0])
1569 {
1570 cupsdSetString(&job->username, con->username);
1571
1572 if (attr)
1573 cupsdSetString(&attr->values[0].string.text, con->username);
1574 }
1575 else if (attr)
1576 {
1577 cupsdLogMessage(CUPSD_LOG_DEBUG,
1578 "add_job: requesting-user-name=\"%s\"",
1579 attr->values[0].string.text);
1580
1581 cupsdSetString(&job->username, attr->values[0].string.text);
1582 }
1583 else
1584 cupsdSetString(&job->username, "anonymous");
1585
1586 if (!attr)
1587 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
1588 "job-originating-user-name", NULL, job->username);
1589 else
1590 {
1591 attr->group_tag = IPP_TAG_JOB;
1592 _cupsStrFree(attr->name);
1593 attr->name = _cupsStrAlloc("job-originating-user-name");
1594 }
1595
1596 if (con->username[0] || auth_info)
1597 {
1598 save_auth_info(con, job, auth_info);
1599
1600 /*
1601 * Remove the auth-info attribute from the attribute data...
1602 */
1603
1604 if (auth_info)
1605 ippDeleteAttribute(job->attrs, auth_info);
1606 }
1607
1608 if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name",
1609 IPP_TAG_ZERO)) != NULL)
1610 {
1611 /*
1612 * Request contains a job-originating-host-name attribute; validate it...
1613 */
1614
1615 if (attr->value_tag != IPP_TAG_NAME ||
1616 attr->num_values != 1 ||
1617 strcmp(con->http.hostname, "localhost"))
1618 {
1619 /*
1620 * Can't override the value if we aren't connected via localhost.
1621 * Also, we can only have 1 value and it must be a name value.
1622 */
1623
1624 switch (attr->value_tag)
1625 {
1626 case IPP_TAG_STRING :
1627 case IPP_TAG_TEXTLANG :
1628 case IPP_TAG_NAMELANG :
1629 case IPP_TAG_TEXT :
1630 case IPP_TAG_NAME :
1631 case IPP_TAG_KEYWORD :
1632 case IPP_TAG_URI :
1633 case IPP_TAG_URISCHEME :
1634 case IPP_TAG_CHARSET :
1635 case IPP_TAG_LANGUAGE :
1636 case IPP_TAG_MIMETYPE :
1637 /*
1638 * Free old strings...
1639 */
1640
1641 for (i = 0; i < attr->num_values; i ++)
1642 {
1643 _cupsStrFree(attr->values[i].string.text);
1644 attr->values[i].string.text = NULL;
1645 if (attr->values[i].string.charset)
1646 {
1647 _cupsStrFree(attr->values[i].string.charset);
1648 attr->values[i].string.charset = NULL;
1649 }
1650 }
1651
1652 default :
1653 break;
1654 }
1655
1656 /*
1657 * Use the default connection hostname instead...
1658 */
1659
1660 attr->value_tag = IPP_TAG_NAME;
1661 attr->num_values = 1;
1662 attr->values[0].string.text = _cupsStrAlloc(con->http.hostname);
1663 }
1664
1665 attr->group_tag = IPP_TAG_JOB;
1666 }
1667 else
1668 {
1669 /*
1670 * No job-originating-host-name attribute, so use the hostname from
1671 * the connection...
1672 */
1673
1674 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
1675 "job-originating-host-name", NULL, con->http.hostname);
1676 }
1677
1678 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation",
1679 time(NULL));
1680 attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
1681 "time-at-processing", 0);
1682 attr->value_tag = IPP_TAG_NOVALUE;
1683 attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
1684 "time-at-completed", 0);
1685 attr->value_tag = IPP_TAG_NOVALUE;
1686
1687 /*
1688 * Add remaining job attributes...
1689 */
1690
1691 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
1692 job->state = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_ENUM,
1693 "job-state", IPP_JOB_STOPPED);
1694 job->state_value = (ipp_jstate_t)job->state->values[0].integer;
1695 job->sheets = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
1696 "job-media-sheets-completed", 0);
1697 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL,
1698 printer->uri);
1699 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL,
1700 title);
1701
1702 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
1703 IPP_TAG_INTEGER)) != NULL)
1704 attr->values[0].integer = 0;
1705 else
1706 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-k-octets", 0);
1707
1708 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
1709 IPP_TAG_KEYWORD)) == NULL)
1710 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
1711 if (!attr)
1712 {
1713 if ((val = cupsGetOption("job-hold-until", printer->num_options,
1714 printer->options)) == NULL)
1715 val = "no-hold";
1716
1717 attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1718 "job-hold-until", NULL, val);
1719 }
1720 if (attr && strcmp(attr->values[0].string.text, "no-hold") &&
1721 !(printer->type & CUPS_PRINTER_REMOTE))
1722 {
1723 /*
1724 * Hold job until specified time...
1725 */
1726
1727 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
1728
1729 job->state->values[0].integer = IPP_JOB_HELD;
1730 job->state_value = IPP_JOB_HELD;
1731 }
1732 else if (job->attrs->request.op.operation_id == IPP_CREATE_JOB)
1733 {
1734 job->hold_until = time(NULL) + MultipleOperationTimeout;
1735 job->state->values[0].integer = IPP_JOB_HELD;
1736 job->state_value = IPP_JOB_HELD;
1737 }
1738 else
1739 {
1740 job->state->values[0].integer = IPP_JOB_PENDING;
1741 job->state_value = IPP_JOB_PENDING;
1742 }
1743
1744 if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) ||
1745 Classification)
1746 {
1747 /*
1748 * Add job sheets options...
1749 */
1750
1751 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
1752 IPP_TAG_ZERO)) == NULL)
1753 {
1754 cupsdLogMessage(CUPSD_LOG_DEBUG,
1755 "Adding default job-sheets values \"%s,%s\"...",
1756 printer->job_sheets[0], printer->job_sheets[1]);
1757
1758 attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets",
1759 2, NULL, NULL);
1760 attr->values[0].string.text = _cupsStrRetain(printer->job_sheets[0]);
1761 attr->values[1].string.text = _cupsStrRetain(printer->job_sheets[1]);
1762 }
1763
1764 job->job_sheets = attr;
1765
1766 /*
1767 * Enforce classification level if set...
1768 */
1769
1770 if (Classification)
1771 {
1772 cupsdLogMessage(CUPSD_LOG_INFO,
1773 "Classification=\"%s\", ClassifyOverride=%d",
1774 Classification ? Classification : "(null)",
1775 ClassifyOverride);
1776
1777 if (ClassifyOverride)
1778 {
1779 if (!strcmp(attr->values[0].string.text, "none") &&
1780 (attr->num_values == 1 ||
1781 !strcmp(attr->values[1].string.text, "none")))
1782 {
1783 /*
1784 * Force the leading banner to have the classification on it...
1785 */
1786
1787 cupsdSetString(&attr->values[0].string.text, Classification);
1788
1789 cupsdLogJob(job, CUPSD_LOG_NOTICE, "CLASSIFICATION FORCED "
1790 "job-sheets=\"%s,none\", "
1791 "job-originating-user-name=\"%s\"",
1792 Classification, job->username);
1793 }
1794 else if (attr->num_values == 2 &&
1795 strcmp(attr->values[0].string.text,
1796 attr->values[1].string.text) &&
1797 strcmp(attr->values[0].string.text, "none") &&
1798 strcmp(attr->values[1].string.text, "none"))
1799 {
1800 /*
1801 * Can't put two different security markings on the same document!
1802 */
1803
1804 cupsdSetString(&attr->values[1].string.text, attr->values[0].string.text);
1805
1806 cupsdLogJob(job, CUPSD_LOG_NOTICE, "CLASSIFICATION FORCED "
1807 "job-sheets=\"%s,%s\", "
1808 "job-originating-user-name=\"%s\"",
1809 attr->values[0].string.text,
1810 attr->values[1].string.text, job->username);
1811 }
1812 else if (strcmp(attr->values[0].string.text, Classification) &&
1813 strcmp(attr->values[0].string.text, "none") &&
1814 (attr->num_values == 1 ||
1815 (strcmp(attr->values[1].string.text, Classification) &&
1816 strcmp(attr->values[1].string.text, "none"))))
1817 {
1818 if (attr->num_values == 1)
1819 cupsdLogJob(job, CUPSD_LOG_NOTICE,
1820 "CLASSIFICATION OVERRIDDEN "
1821 "job-sheets=\"%s\", "
1822 "job-originating-user-name=\"%s\"",
1823 attr->values[0].string.text, job->username);
1824 else
1825 cupsdLogJob(job, CUPSD_LOG_NOTICE,
1826 "CLASSIFICATION OVERRIDDEN "
1827 "job-sheets=\"%s,%s\",fffff "
1828 "job-originating-user-name=\"%s\"",
1829 attr->values[0].string.text,
1830 attr->values[1].string.text, job->username);
1831 }
1832 }
1833 else if (strcmp(attr->values[0].string.text, Classification) &&
1834 (attr->num_values == 1 ||
1835 strcmp(attr->values[1].string.text, Classification)))
1836 {
1837 /*
1838 * Force the banner to have the classification on it...
1839 */
1840
1841 if (attr->num_values > 1 &&
1842 !strcmp(attr->values[0].string.text, attr->values[1].string.text))
1843 {
1844 cupsdSetString(&(attr->values[0].string.text), Classification);
1845 cupsdSetString(&(attr->values[1].string.text), Classification);
1846 }
1847 else
1848 {
1849 if (attr->num_values == 1 ||
1850 strcmp(attr->values[0].string.text, "none"))
1851 cupsdSetString(&(attr->values[0].string.text), Classification);
1852
1853 if (attr->num_values > 1 &&
1854 strcmp(attr->values[1].string.text, "none"))
1855 cupsdSetString(&(attr->values[1].string.text), Classification);
1856 }
1857
1858 if (attr->num_values > 1)
1859 cupsdLogJob(job, CUPSD_LOG_NOTICE,
1860 "CLASSIFICATION FORCED "
1861 "job-sheets=\"%s,%s\", "
1862 "job-originating-user-name=\"%s\"",
1863 attr->values[0].string.text,
1864 attr->values[1].string.text, job->username);
1865 else
1866 cupsdLogJob(job, CUPSD_LOG_NOTICE,
1867 "CLASSIFICATION FORCED "
1868 "job-sheets=\"%s\", "
1869 "job-originating-user-name=\"%s\"",
1870 Classification, job->username);
1871 }
1872 }
1873
1874 /*
1875 * See if we need to add the starting sheet...
1876 */
1877
1878 if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
1879 {
1880 cupsdLogJob(job, CUPSD_LOG_INFO, "Adding start banner page \"%s\".",
1881 attr->values[0].string.text);
1882
1883 if ((kbytes = copy_banner(con, job, attr->values[0].string.text)) < 0)
1884 {
1885 cupsdDeleteJob(job);
1886 return (NULL);
1887 }
1888
1889 cupsdUpdateQuota(printer, job->username, 0, kbytes);
1890 }
1891 }
1892 else if ((attr = ippFindAttribute(job->attrs, "job-sheets",
1893 IPP_TAG_ZERO)) != NULL)
1894 job->sheets = attr;
1895
1896 /*
1897 * Fill in the response info...
1898 */
1899
1900 snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName,
1901 LocalPort, job->id);
1902
1903 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
1904 job_uri);
1905
1906 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
1907
1908 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
1909 job->state_value);
1910 add_job_state_reasons(con, job);
1911
1912 con->response->request.status.status_code = IPP_OK;
1913
1914 /*
1915 * Add any job subscriptions...
1916 */
1917
1918 add_job_subscriptions(con, job);
1919
1920 /*
1921 * Set all but the first two attributes to the job attributes group...
1922 */
1923
1924 for (attr = job->attrs->attrs->next->next; attr; attr = attr->next)
1925 attr->group_tag = IPP_TAG_JOB;
1926
1927 /*
1928 * Fire the "job created" event...
1929 */
1930
1931 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created.");
1932
1933 /*
1934 * Return the new job...
1935 */
1936
1937 return (job);
1938 }
1939
1940
1941 /*
1942 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1943 * upon the job and printer state...
1944 */
1945
1946 static void
1947 add_job_state_reasons(
1948 cupsd_client_t *con, /* I - Client connection */
1949 cupsd_job_t *job) /* I - Job info */
1950 {
1951 cupsd_printer_t *dest; /* Destination printer */
1952
1953
1954 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job_state_reasons(%p[%d], %d)",
1955 con, con->http.fd, job ? job->id : 0);
1956
1957 switch (job ? job->state_value : IPP_JOB_CANCELED)
1958 {
1959 case IPP_JOB_PENDING :
1960 dest = cupsdFindDest(job->dest);
1961
1962 if (dest && dest->state == IPP_PRINTER_STOPPED)
1963 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1964 "job-state-reasons", NULL, "printer-stopped");
1965 else
1966 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1967 "job-state-reasons", NULL, "none");
1968 break;
1969
1970 case IPP_JOB_HELD :
1971 if (ippFindAttribute(job->attrs, "job-hold-until",
1972 IPP_TAG_KEYWORD) != NULL ||
1973 ippFindAttribute(job->attrs, "job-hold-until",
1974 IPP_TAG_NAME) != NULL)
1975 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1976 "job-state-reasons", NULL, "job-hold-until-specified");
1977 else
1978 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1979 "job-state-reasons", NULL, "job-incoming");
1980 break;
1981
1982 case IPP_JOB_PROCESSING :
1983 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1984 "job-state-reasons", NULL, "job-printing");
1985 break;
1986
1987 case IPP_JOB_STOPPED :
1988 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1989 "job-state-reasons", NULL, "job-stopped");
1990 break;
1991
1992 case IPP_JOB_CANCELED :
1993 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1994 "job-state-reasons", NULL, "job-canceled-by-user");
1995 break;
1996
1997 case IPP_JOB_ABORTED :
1998 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1999 "job-state-reasons", NULL, "aborted-by-system");
2000 break;
2001
2002 case IPP_JOB_COMPLETED :
2003 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
2004 "job-state-reasons", NULL, "job-completed-successfully");
2005 break;
2006 }
2007 }
2008
2009
2010 /*
2011 * 'add_job_subscriptions()' - Add any subscriptions for a job.
2012 */
2013
2014 static void
2015 add_job_subscriptions(
2016 cupsd_client_t *con, /* I - Client connection */
2017 cupsd_job_t *job) /* I - Newly created job */
2018 {
2019 int i; /* Looping var */
2020 ipp_attribute_t *prev, /* Previous attribute */
2021 *next, /* Next attribute */
2022 *attr; /* Current attribute */
2023 cupsd_subscription_t *sub; /* Subscription object */
2024 const char *recipient, /* notify-recipient-uri */
2025 *pullmethod; /* notify-pull-method */
2026 ipp_attribute_t *user_data; /* notify-user-data */
2027 int interval; /* notify-time-interval */
2028 unsigned mask; /* notify-events */
2029
2030
2031 /*
2032 * Find the first subscription group attribute; return if we have
2033 * none...
2034 */
2035
2036 for (attr = job->attrs->attrs; attr; attr = attr->next)
2037 if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
2038 break;
2039
2040 if (!attr)
2041 return;
2042
2043 /*
2044 * Process the subscription attributes in the request...
2045 */
2046
2047 while (attr)
2048 {
2049 recipient = NULL;
2050 pullmethod = NULL;
2051 user_data = NULL;
2052 interval = 0;
2053 mask = CUPSD_EVENT_NONE;
2054
2055 while (attr && attr->group_tag != IPP_TAG_ZERO)
2056 {
2057 if (!strcmp(attr->name, "notify-recipient-uri") &&
2058 attr->value_tag == IPP_TAG_URI)
2059 {
2060 /*
2061 * Validate the recipient scheme against the ServerBin/notifier
2062 * directory...
2063 */
2064
2065 char notifier[1024], /* Notifier filename */
2066 scheme[HTTP_MAX_URI], /* Scheme portion of URI */
2067 userpass[HTTP_MAX_URI], /* Username portion of URI */
2068 host[HTTP_MAX_URI], /* Host portion of URI */
2069 resource[HTTP_MAX_URI]; /* Resource portion of URI */
2070 int port; /* Port portion of URI */
2071
2072
2073 recipient = attr->values[0].string.text;
2074
2075 if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient,
2076 scheme, sizeof(scheme), userpass, sizeof(userpass),
2077 host, sizeof(host), &port,
2078 resource, sizeof(resource)) < HTTP_URI_OK)
2079 {
2080 send_ipp_status(con, IPP_NOT_POSSIBLE,
2081 _("Bad notify-recipient-uri URI \"%s\"!"), recipient);
2082 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2083 "notify-status-code", IPP_URI_SCHEME);
2084 return;
2085 }
2086
2087 snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
2088 scheme);
2089 if (access(notifier, X_OK))
2090 {
2091 send_ipp_status(con, IPP_NOT_POSSIBLE,
2092 _("notify-recipient-uri URI \"%s\" uses unknown "
2093 "scheme!"), recipient);
2094 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2095 "notify-status-code", IPP_URI_SCHEME);
2096 return;
2097 }
2098
2099 if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient))
2100 {
2101 send_ipp_status(con, IPP_NOT_POSSIBLE,
2102 _("notify-recipient-uri URI \"%s\" is already used!"),
2103 recipient);
2104 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2105 "notify-status-code", IPP_ATTRIBUTES);
2106 return;
2107 }
2108 }
2109 else if (!strcmp(attr->name, "notify-pull-method") &&
2110 attr->value_tag == IPP_TAG_KEYWORD)
2111 {
2112 pullmethod = attr->values[0].string.text;
2113
2114 if (strcmp(pullmethod, "ippget"))
2115 {
2116 send_ipp_status(con, IPP_NOT_POSSIBLE,
2117 _("Bad notify-pull-method \"%s\"!"), pullmethod);
2118 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2119 "notify-status-code", IPP_ATTRIBUTES);
2120 return;
2121 }
2122 }
2123 else if (!strcmp(attr->name, "notify-charset") &&
2124 attr->value_tag == IPP_TAG_CHARSET &&
2125 strcmp(attr->values[0].string.text, "us-ascii") &&
2126 strcmp(attr->values[0].string.text, "utf-8"))
2127 {
2128 send_ipp_status(con, IPP_CHARSET,
2129 _("Character set \"%s\" not supported!"),
2130 attr->values[0].string.text);
2131 return;
2132 }
2133 else if (!strcmp(attr->name, "notify-natural-language") &&
2134 (attr->value_tag != IPP_TAG_LANGUAGE ||
2135 strcmp(attr->values[0].string.text, DefaultLanguage)))
2136 {
2137 send_ipp_status(con, IPP_CHARSET,
2138 _("Language \"%s\" not supported!"),
2139 attr->values[0].string.text);
2140 return;
2141 }
2142 else if (!strcmp(attr->name, "notify-user-data") &&
2143 attr->value_tag == IPP_TAG_STRING)
2144 {
2145 if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
2146 {
2147 send_ipp_status(con, IPP_REQUEST_VALUE,
2148 _("The notify-user-data value is too large "
2149 "(%d > 63 octets)!"),
2150 attr->values[0].unknown.length);
2151 return;
2152 }
2153
2154 user_data = attr;
2155 }
2156 else if (!strcmp(attr->name, "notify-events") &&
2157 attr->value_tag == IPP_TAG_KEYWORD)
2158 {
2159 for (i = 0; i < attr->num_values; i ++)
2160 mask |= cupsdEventValue(attr->values[i].string.text);
2161 }
2162 else if (!strcmp(attr->name, "notify-lease-duration"))
2163 {
2164 send_ipp_status(con, IPP_BAD_REQUEST,
2165 _("The notify-lease-duration attribute cannot be "
2166 "used with job subscriptions."));
2167 return;
2168 }
2169 else if (!strcmp(attr->name, "notify-time-interval") &&
2170 attr->value_tag == IPP_TAG_INTEGER)
2171 interval = attr->values[0].integer;
2172
2173 attr = attr->next;
2174 }
2175
2176 if (!recipient && !pullmethod)
2177 break;
2178
2179 if (mask == CUPSD_EVENT_NONE)
2180 mask = CUPSD_EVENT_JOB_COMPLETED;
2181
2182 if ((sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job,
2183 recipient, 0)) != NULL)
2184 {
2185 sub->interval = interval;
2186
2187 cupsdSetString(&sub->owner, job->username);
2188
2189 if (user_data)
2190 {
2191 sub->user_data_len = user_data->values[0].unknown.length;
2192 memcpy(sub->user_data, user_data->values[0].unknown.data,
2193 sub->user_data_len);
2194 }
2195
2196 ippAddSeparator(con->response);
2197 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
2198 "notify-subscription-id", sub->id);
2199 }
2200
2201 if (attr)
2202 attr = attr->next;
2203 }
2204
2205 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
2206
2207 /*
2208 * Remove all of the subscription attributes from the job request...
2209 *
2210 * TODO: Optimize this since subscription groups have to come at the
2211 * end of the request...
2212 */
2213
2214 for (attr = job->attrs->attrs, prev = NULL; attr; attr = next)
2215 {
2216 next = attr->next;
2217
2218 if (attr->group_tag == IPP_TAG_SUBSCRIPTION ||
2219 attr->group_tag == IPP_TAG_ZERO)
2220 {
2221 /*
2222 * Free and remove this attribute...
2223 */
2224
2225 _ippFreeAttr(attr);
2226
2227 if (prev)
2228 prev->next = next;
2229 else
2230 job->attrs->attrs = next;
2231 }
2232 else
2233 prev = attr;
2234 }
2235
2236 job->attrs->last = prev;
2237 job->attrs->current = prev;
2238 }
2239
2240
2241 /*
2242 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2243 *
2244 * See RFC 4122 for the definition of UUIDs and the format.
2245 */
2246
2247 static void
2248 add_job_uuid(cupsd_client_t *con, /* I - Client connection */
2249 cupsd_job_t *job) /* I - Job */
2250 {
2251 char uuid[1024]; /* job-uuid string */
2252 _cups_md5_state_t md5state; /* MD5 state */
2253 unsigned char md5sum[16]; /* MD5 digest/sum */
2254
2255
2256 /*
2257 * First see if the job already has a job-uuid attribute; if so, return...
2258 */
2259
2260 if (ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI))
2261 return;
2262
2263 /*
2264 * No job-uuid attribute, so build a version 3 UUID with the local job
2265 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
2266 * the ServerName, server name and port that the client connected to,
2267 * and local job ID...
2268 */
2269
2270 snprintf(uuid, sizeof(uuid), "%s:%s:%d:%d", ServerName, con->servername,
2271 con->serverport, job->id);
2272
2273 _cupsMD5Init(&md5state);
2274 _cupsMD5Append(&md5state, (unsigned char *)uuid, strlen(uuid));
2275 _cupsMD5Finish(&md5state, md5sum);
2276
2277 /*
2278 * Format the UUID URI using the MD5 sum and job ID.
2279 */
2280
2281 snprintf(uuid, sizeof(uuid),
2282 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2283 "%02x%02x%02x%02x%02x%02x",
2284 md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5],
2285 (md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40,
2286 md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13],
2287 md5sum[14], md5sum[15]);
2288
2289 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL, uuid);
2290 }
2291
2292
2293 /*
2294 * 'add_printer()' - Add a printer to the system.
2295 */
2296
2297 static void
2298 add_printer(cupsd_client_t *con, /* I - Client connection */
2299 ipp_attribute_t *uri) /* I - URI of printer */
2300 {
2301 http_status_t status; /* Policy status */
2302 int i; /* Looping var */
2303 char scheme[HTTP_MAX_URI], /* Method portion of URI */
2304 username[HTTP_MAX_URI], /* Username portion of URI */
2305 host[HTTP_MAX_URI], /* Host portion of URI */
2306 resource[HTTP_MAX_URI]; /* Resource portion of URI */
2307 int port; /* Port portion of URI */
2308 cupsd_printer_t *printer; /* Printer/class */
2309 ipp_attribute_t *attr; /* Printer attribute */
2310 cups_file_t *fp; /* Script/PPD file */
2311 char line[1024]; /* Line from file... */
2312 char srcfile[1024], /* Source Script/PPD file */
2313 dstfile[1024]; /* Destination Script/PPD file */
2314 int modify; /* Non-zero if we are modifying */
2315 char newname[IPP_MAX_NAME]; /* New printer name */
2316 int need_restart_job; /* Need to restart job? */
2317 int set_device_uri, /* Did we set the device URI? */
2318 set_port_monitor; /* Did we set the port monitor? */
2319
2320
2321 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con,
2322 con->http.fd, uri->values[0].string.text);
2323
2324 /*
2325 * Do we have a valid URI?
2326 */
2327
2328 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
2329 sizeof(scheme), username, sizeof(username), host,
2330 sizeof(host), &port, resource, sizeof(resource));
2331
2332 if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10)
2333 {
2334 /*
2335 * No, return an error...
2336 */
2337
2338 send_ipp_status(con, IPP_BAD_REQUEST,
2339 _("The printer-uri must be of the form "
2340 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2341 return;
2342 }
2343
2344 /*
2345 * Do we have a valid printer name?
2346 */
2347
2348 if (!validate_name(resource + 10))
2349 {
2350 /*
2351 * No, return an error...
2352 */
2353
2354 send_ipp_status(con, IPP_BAD_REQUEST,
2355 _("The printer-uri \"%s\" contains invalid characters."),
2356 uri->values[0].string.text);
2357 return;
2358 }
2359
2360 /*
2361 * See if the printer already exists; if not, create a new printer...
2362 */
2363
2364 if ((printer = cupsdFindPrinter(resource + 10)) == NULL)
2365 {
2366 /*
2367 * Printer doesn't exist; see if we have a class of the same name...
2368 */
2369
2370 if ((printer = cupsdFindClass(resource + 10)) != NULL &&
2371 !(printer->type & CUPS_PRINTER_DISCOVERED))
2372 {
2373 /*
2374 * Yes, return an error...
2375 */
2376
2377 send_ipp_status(con, IPP_NOT_POSSIBLE,
2378 _("A class named \"%s\" already exists!"),
2379 resource + 10);
2380 return;
2381 }
2382
2383 /*
2384 * No, check the default policy then add the printer...
2385 */
2386
2387 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
2388 {
2389 send_http_error(con, status, NULL);
2390 return;
2391 }
2392
2393 printer = cupsdAddPrinter(resource + 10);
2394 modify = 0;
2395 }
2396 else if (printer->type & CUPS_PRINTER_IMPLICIT)
2397 {
2398 /*
2399 * Check the default policy, then rename the implicit printer to
2400 * "AnyPrinter" or delete it...
2401 */
2402
2403 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
2404 {
2405 send_http_error(con, status, NULL);
2406 return;
2407 }
2408
2409 if (ImplicitAnyClasses)
2410 {
2411 snprintf(newname, sizeof(newname), "Any%s", resource + 10);
2412 cupsdRenamePrinter(printer, newname);
2413 }
2414 else
2415 cupsdDeletePrinter(printer, 1);
2416
2417 /*
2418 * Add the printer as a new local printer...
2419 */
2420
2421 printer = cupsdAddPrinter(resource + 10);
2422 modify = 0;
2423 }
2424 else if (printer->type & CUPS_PRINTER_DISCOVERED)
2425 {
2426 /*
2427 * Check the default policy, then rename the remote printer to
2428 * "Printer@server"...
2429 */
2430
2431 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
2432 {
2433 send_http_error(con, status, NULL);
2434 return;
2435 }
2436
2437 snprintf(newname, sizeof(newname), "%s@%s", resource + 10,
2438 printer->hostname);
2439 cupsdRenamePrinter(printer, newname);
2440
2441 /*
2442 * Add the printer as a new local printer...
2443 */
2444
2445 printer = cupsdAddPrinter(resource + 10);
2446 modify = 0;
2447 }
2448 else if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
2449 NULL)) != HTTP_OK)
2450 {
2451 send_http_error(con, status, printer);
2452 return;
2453 }
2454 else
2455 modify = 1;
2456
2457 /*
2458 * Look for attributes and copy them over as needed...
2459 */
2460
2461 need_restart_job = 0;
2462
2463 if ((attr = ippFindAttribute(con->request, "printer-location",
2464 IPP_TAG_TEXT)) != NULL)
2465 cupsdSetString(&printer->location, attr->values[0].string.text);
2466
2467 if ((attr = ippFindAttribute(con->request, "printer-info",
2468 IPP_TAG_TEXT)) != NULL)
2469 cupsdSetString(&printer->info, attr->values[0].string.text);
2470
2471 set_device_uri = 0;
2472
2473 if ((attr = ippFindAttribute(con->request, "device-uri",
2474 IPP_TAG_URI)) != NULL)
2475 {
2476 /*
2477 * Do we have a valid device URI?
2478 */
2479
2480 http_uri_status_t uri_status; /* URI separation status */
2481 char old_device_uri[1024];
2482 /* Old device URI */
2483
2484
2485 need_restart_job = 1;
2486
2487 uri_status = httpSeparateURI(HTTP_URI_CODING_ALL,
2488 attr->values[0].string.text,
2489 scheme, sizeof(scheme),
2490 username, sizeof(username),
2491 host, sizeof(host), &port,
2492 resource, sizeof(resource));
2493
2494 if (uri_status < HTTP_URI_OK)
2495 {
2496 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"!"),
2497 attr->values[0].string.text);
2498 cupsdLogMessage(CUPSD_LOG_DEBUG,
2499 "add_printer: httpSeparateURI returned %d", uri_status);
2500 return;
2501 }
2502
2503 if (!strcmp(scheme, "file"))
2504 {
2505 /*
2506 * See if the administrator has enabled file devices...
2507 */
2508
2509 if (!FileDevice && strcmp(resource, "/dev/null"))
2510 {
2511 /*
2512 * File devices are disabled and the URL is not file:/dev/null...
2513 */
2514
2515 send_ipp_status(con, IPP_NOT_POSSIBLE,
2516 _("File device URIs have been disabled! "
2517 "To enable, see the FileDevice directive in "
2518 "\"%s/cupsd.conf\"."),
2519 ServerRoot);
2520 return;
2521 }
2522 }
2523 else
2524 {
2525 /*
2526 * See if the backend exists and is executable...
2527 */
2528
2529 snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, scheme);
2530 if (access(srcfile, X_OK))
2531 {
2532 /*
2533 * Could not find device in list!
2534 */
2535
2536 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri scheme \"%s\"!"),
2537 scheme);
2538 return;
2539 }
2540 }
2541
2542 if (printer->sanitized_device_uri)
2543 strlcpy(old_device_uri, printer->sanitized_device_uri,
2544 sizeof(old_device_uri));
2545 else
2546 old_device_uri[0] = '\0';
2547
2548 cupsdSetDeviceURI(printer, attr->values[0].string.text);
2549
2550 cupsdLogMessage(CUPSD_LOG_INFO,
2551 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2552 printer->name, printer->sanitized_device_uri,
2553 old_device_uri);
2554
2555 set_device_uri = 1;
2556 }
2557
2558 set_port_monitor = 0;
2559
2560 if ((attr = ippFindAttribute(con->request, "port-monitor",
2561 IPP_TAG_NAME)) != NULL)
2562 {
2563 ipp_attribute_t *supported; /* port-monitor-supported attribute */
2564
2565
2566 need_restart_job = 1;
2567
2568 supported = ippFindAttribute(printer->ppd_attrs, "port-monitor-supported",
2569 IPP_TAG_NAME);
2570 if (supported)
2571 {
2572 for (i = 0; i < supported->num_values; i ++)
2573 if (!strcmp(supported->values[i].string.text,
2574 attr->values[0].string.text))
2575 break;
2576 }
2577
2578 if (!supported || i >= supported->num_values)
2579 {
2580 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"!"),
2581 attr->values[0].string.text);
2582 return;
2583 }
2584
2585 cupsdLogMessage(CUPSD_LOG_INFO,
2586 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2587 printer->name, attr->values[0].string.text,
2588 printer->port_monitor ? printer->port_monitor : "none");
2589
2590 if (strcmp(attr->values[0].string.text, "none"))
2591 cupsdSetString(&printer->port_monitor, attr->values[0].string.text);
2592 else
2593 cupsdClearString(&printer->port_monitor);
2594
2595 set_port_monitor = 1;
2596 }
2597
2598 if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs",
2599 IPP_TAG_BOOLEAN)) != NULL)
2600 {
2601 cupsdLogMessage(CUPSD_LOG_INFO,
2602 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2603 printer->name, attr->values[0].boolean, printer->accepting);
2604
2605 printer->accepting = attr->values[0].boolean;
2606 cupsdAddPrinterHistory(printer);
2607 }
2608
2609 if ((attr = ippFindAttribute(con->request, "printer-is-shared",
2610 IPP_TAG_BOOLEAN)) != NULL)
2611 {
2612 if (printer->shared && !attr->values[0].boolean)
2613 cupsdDeregisterPrinter(printer, 1);
2614
2615 cupsdLogMessage(CUPSD_LOG_INFO,
2616 "Setting %s printer-is-shared to %d (was %d.)",
2617 printer->name, attr->values[0].boolean, printer->shared);
2618
2619 printer->shared = attr->values[0].boolean;
2620 }
2621
2622 if ((attr = ippFindAttribute(con->request, "printer-state",
2623 IPP_TAG_ENUM)) != NULL)
2624 {
2625 if (attr->values[0].integer != IPP_PRINTER_IDLE &&
2626 attr->values[0].integer != IPP_PRINTER_STOPPED)
2627 {
2628 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d!"),
2629 attr->values[0].integer);
2630 return;
2631 }
2632
2633 cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
2634 printer->name, attr->values[0].integer, printer->state);
2635
2636 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
2637 cupsdStopPrinter(printer, 0);
2638 else
2639 {
2640 need_restart_job = 1;
2641 cupsdSetPrinterState(printer, (ipp_pstate_t)(attr->values[0].integer), 0);
2642 }
2643 }
2644 if ((attr = ippFindAttribute(con->request, "printer-state-message",
2645 IPP_TAG_TEXT)) != NULL)
2646 {
2647 strlcpy(printer->state_message, attr->values[0].string.text,
2648 sizeof(printer->state_message));
2649 cupsdAddPrinterHistory(printer);
2650 }
2651
2652 if ((attr = ippFindAttribute(con->request, "printer-state-reasons",
2653 IPP_TAG_KEYWORD)) != NULL)
2654 {
2655 if (attr->num_values >
2656 (int)(sizeof(printer->reasons) / sizeof(printer->reasons[0])))
2657 {
2658 send_ipp_status(con, IPP_NOT_POSSIBLE,
2659 _("Too many printer-state-reasons values (%d > %d)!"),
2660 attr->num_values,
2661 (int)(sizeof(printer->reasons) /
2662 sizeof(printer->reasons[0])));
2663 return;
2664 }
2665
2666 for (i = 0; i < printer->num_reasons; i ++)
2667 _cupsStrFree(printer->reasons[i]);
2668
2669 printer->num_reasons = 0;
2670 for (i = 0; i < attr->num_values; i ++)
2671 {
2672 if (!strcmp(attr->values[i].string.text, "none"))
2673 continue;
2674
2675 printer->reasons[printer->num_reasons] =
2676 _cupsStrRetain(attr->values[i].string.text);
2677
2678 if (!strcmp(printer->reasons[printer->num_reasons], "paused") &&
2679 printer->state != IPP_PRINTER_STOPPED)
2680 {
2681 cupsdLogMessage(CUPSD_LOG_INFO,
2682 "Setting %s printer-state to %d (was %d.)",
2683 printer->name, IPP_PRINTER_STOPPED, printer->state);
2684 cupsdStopPrinter(printer, 0);
2685 }
2686
2687 printer->num_reasons ++;
2688 }
2689
2690 if (PrintcapFormat == PRINTCAP_PLIST)
2691 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
2692 }
2693
2694 set_printer_defaults(con, printer);
2695
2696 if ((attr = ippFindAttribute(con->request, "auth-info-required",
2697 IPP_TAG_KEYWORD)) != NULL)
2698 cupsdSetAuthInfoRequired(printer, NULL, attr);
2699
2700 /*
2701 * See if we have all required attributes...
2702 */
2703
2704 if (!printer->device_uri)
2705 cupsdSetString(&printer->device_uri, "file:///dev/null");
2706
2707 /*
2708 * See if we have an interface script or PPD file attached to the request...
2709 */
2710
2711 if (con->filename)
2712 {
2713 need_restart_job = 1;
2714
2715 strlcpy(srcfile, con->filename, sizeof(srcfile));
2716
2717 if ((fp = cupsFileOpen(srcfile, "rb")))
2718 {
2719 /*
2720 * Yes; get the first line from it...
2721 */
2722
2723 line[0] = '\0';
2724 cupsFileGets(fp, line, sizeof(line));
2725 cupsFileClose(fp);
2726
2727 /*
2728 * Then see what kind of file it is...
2729 */
2730
2731 snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
2732 printer->name);
2733
2734 if (!strncmp(line, "*PPD-Adobe", 10))
2735 {
2736 /*
2737 * The new file is a PPD file, so remove any old interface script
2738 * that might be lying around...
2739 */
2740
2741 unlink(dstfile);
2742 }
2743 else
2744 {
2745 /*
2746 * This must be an interface script, so move the file over to the
2747 * interfaces directory and make it executable...
2748 */
2749
2750 if (copy_file(srcfile, dstfile))
2751 {
2752 send_ipp_status(con, IPP_INTERNAL_ERROR,
2753 _("Unable to copy interface script - %s!"),
2754 strerror(errno));
2755 return;
2756 }
2757
2758 cupsdLogMessage(CUPSD_LOG_DEBUG,
2759 "Copied interface script successfully!");
2760 chmod(dstfile, 0755);
2761 }
2762
2763 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2764 printer->name);
2765
2766 if (!strncmp(line, "*PPD-Adobe", 10))
2767 {
2768 /*
2769 * The new file is a PPD file, so move the file over to the
2770 * ppd directory and make it readable by all...
2771 */
2772
2773 if (copy_file(srcfile, dstfile))
2774 {
2775 send_ipp_status(con, IPP_INTERNAL_ERROR,
2776 _("Unable to copy PPD file - %s!"),
2777 strerror(errno));
2778 return;
2779 }
2780
2781 cupsdLogMessage(CUPSD_LOG_DEBUG,
2782 "Copied PPD file successfully!");
2783 chmod(dstfile, 0644);
2784
2785 #ifdef __APPLE__
2786 /*
2787 * (Re)register color profiles...
2788 */
2789
2790 if (!RunUser)
2791 {
2792 apple_unregister_profiles(printer);
2793 apple_register_profiles(printer);
2794 }
2795 #endif /* __APPLE__ */
2796 }
2797 else
2798 {
2799 /*
2800 * This must be an interface script, so remove any old PPD file that
2801 * may be lying around...
2802 */
2803
2804 unlink(dstfile);
2805 }
2806 }
2807 }
2808 else if ((attr = ippFindAttribute(con->request, "ppd-name",
2809 IPP_TAG_NAME)) != NULL)
2810 {
2811 need_restart_job = 1;
2812
2813 if (!strcmp(attr->values[0].string.text, "raw"))
2814 {
2815 /*
2816 * Raw driver, remove any existing PPD or interface script files.
2817 */
2818
2819 snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
2820 printer->name);
2821 unlink(dstfile);
2822
2823 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2824 printer->name);
2825 unlink(dstfile);
2826 }
2827 else
2828 {
2829 /*
2830 * PPD model file...
2831 */
2832
2833 snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
2834 printer->name);
2835 unlink(dstfile);
2836
2837 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2838 printer->name);
2839
2840 if (copy_model(con, attr->values[0].string.text, dstfile))
2841 {
2842 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file!"));
2843 return;
2844 }
2845
2846 cupsdLogMessage(CUPSD_LOG_DEBUG,
2847 "Copied PPD file successfully!");
2848 chmod(dstfile, 0644);
2849
2850 #ifdef __APPLE__
2851 /*
2852 * (Re)register color profiles...
2853 */
2854
2855 if (!RunUser)
2856 {
2857 apple_unregister_profiles(printer);
2858 apple_register_profiles(printer);
2859 }
2860 #endif /* __APPLE__ */
2861 }
2862 }
2863
2864 /*
2865 * If we set the device URI but not the port monitor, check which port
2866 * monitor to use by default...
2867 */
2868
2869 if (set_device_uri && !set_port_monitor)
2870 {
2871 ppd_file_t *ppd; /* PPD file */
2872 ppd_attr_t *ppdattr; /* cupsPortMonitor attribute */
2873
2874
2875 httpSeparateURI(HTTP_URI_CODING_ALL, printer->device_uri, scheme,
2876 sizeof(scheme), username, sizeof(username), host,
2877 sizeof(host), &port, resource, sizeof(resource));
2878
2879 snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot,
2880 printer->name);
2881 if ((ppd = ppdOpenFile(srcfile)) != NULL)
2882 {
2883 for (ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2884 ppdattr;
2885 ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
2886 if (!strcmp(scheme, ppdattr->spec))
2887 {
2888 cupsdLogMessage(CUPSD_LOG_INFO,
2889 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2890 printer->name, ppdattr->value,
2891 printer->port_monitor ? printer->port_monitor
2892 : "none");
2893
2894 if (strcmp(ppdattr->value, "none"))
2895 cupsdSetString(&printer->port_monitor, ppdattr->value);
2896 else
2897 cupsdClearString(&printer->port_monitor);
2898
2899 break;
2900 }
2901
2902 ppdClose(ppd);
2903 }
2904 }
2905
2906 /*
2907 * Update the printer attributes and return...
2908 */
2909
2910 cupsdSetPrinterAttrs(printer);
2911 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
2912
2913 if (need_restart_job && printer->job)
2914 {
2915 cupsd_job_t *job;
2916
2917 /*
2918 * Stop the current job and then restart it below...
2919 */
2920
2921 job = (cupsd_job_t *)printer->job;
2922
2923 cupsdStopJob(job, 1);
2924
2925 job->state->values[0].integer = IPP_JOB_PENDING;
2926 job->state_value = IPP_JOB_PENDING;
2927 }
2928
2929 if (need_restart_job)
2930 cupsdCheckJobs();
2931
2932 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
2933
2934 if (modify)
2935 {
2936 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL,
2937 "Printer \"%s\" modified by \"%s\".", printer->name,
2938 get_username(con));
2939
2940 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".",
2941 printer->name, get_username(con));
2942 }
2943 else
2944 {
2945 cupsdAddPrinterHistory(printer);
2946
2947 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, printer, NULL,
2948 "New printer \"%s\" added by \"%s\".", printer->name,
2949 get_username(con));
2950
2951 cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".",
2952 printer->name, get_username(con));
2953 }
2954
2955 con->response->request.status.status_code = IPP_OK;
2956 }
2957
2958
2959 /*
2960 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2961 * based upon the printer state...
2962 */
2963
2964 static void
2965 add_printer_state_reasons(
2966 cupsd_client_t *con, /* I - Client connection */
2967 cupsd_printer_t *p) /* I - Printer info */
2968 {
2969 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2970 "add_printer_state_reasons(%p[%d], %p[%s])",
2971 con, con->http.fd, p, p->name);
2972
2973 if (p->num_reasons == 0)
2974 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2975 "printer-state-reasons", NULL,
2976 p->state == IPP_PRINTER_STOPPED ? "paused" : "none");
2977 else
2978 ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2979 "printer-state-reasons", p->num_reasons, NULL,
2980 (const char * const *)p->reasons);
2981 }
2982
2983
2984 /*
2985 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2986 * the specified printer or class.
2987 */
2988
2989 static void
2990 add_queued_job_count(
2991 cupsd_client_t *con, /* I - Client connection */
2992 cupsd_printer_t *p) /* I - Printer or class */
2993 {
2994 int count; /* Number of jobs on destination */
2995
2996
2997 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])",
2998 con, con->http.fd, p, p->name);
2999
3000 count = cupsdGetPrinterJobCount(p->name);
3001
3002 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3003 "queued-job-count", count);
3004 }
3005
3006
3007 #ifdef __APPLE__
3008 /*
3009 * 'apple_init_profile()' - Initialize a color profile.
3010 */
3011
3012 static void
3013 apple_init_profile(
3014 ppd_file_t *ppd, /* I - PPD file */
3015 cups_array_t *languages, /* I - Languages in the PPD file */
3016 CMDeviceProfileInfo *profile, /* I - Profile record */
3017 unsigned id, /* I - Profile ID */
3018 const char *name, /* I - Profile name */
3019 const char *text, /* I - Profile UI text */
3020 const char *iccfile) /* I - ICC filename */
3021 {
3022 char url[1024]; /* URL for profile filename */
3023 CFMutableDictionaryRef dict; /* Dictionary for name */
3024 char *language; /* Current language */
3025 ppd_attr_t *attr; /* Profile attribute */
3026 CFStringRef cflang, /* Language string */
3027 cftext; /* Localized text */
3028
3029
3030 /*
3031 * Build the profile name dictionary...
3032 */
3033
3034 dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
3035 &kCFTypeDictionaryKeyCallBacks,
3036 &kCFTypeDictionaryValueCallBacks);
3037
3038 cftext = CFStringCreateWithCString(kCFAllocatorDefault, text,
3039 kCFStringEncodingUTF8);
3040
3041 if (cftext)
3042 {
3043 CFDictionarySetValue(dict, CFSTR("en"), cftext);
3044 CFRelease(cftext);
3045 }
3046
3047 if (languages)
3048 {
3049 /*
3050 * Find localized names for the color profiles...
3051 */
3052
3053 cupsArraySave(ppd->sorted_attrs);
3054
3055 for (language = (char *)cupsArrayFirst(languages);
3056 language;
3057 language = (char *)cupsArrayNext(languages))
3058 {
3059 if (iccfile)
3060 {
3061 if ((attr = _ppdLocalizedAttr(ppd, "cupsICCProfile", name,
3062 language)) == NULL)
3063 attr = _ppdLocalizedAttr(ppd, "APTiogaProfile", name, language);
3064 }
3065 else
3066 attr = _ppdLocalizedAttr(ppd, "ColorModel", name, language);
3067
3068 if (attr && attr->text[0])
3069 {
3070 cflang = CFStringCreateWithCString(kCFAllocatorDefault, language,
3071 kCFStringEncodingUTF8);
3072 cftext = CFStringCreateWithCString(kCFAllocatorDefault, attr->text,
3073 kCFStringEncodingUTF8);
3074
3075 if (cflang && cftext)
3076 CFDictionarySetValue(dict, cflang, cftext);
3077
3078 if (cflang)
3079 CFRelease(cflang);
3080
3081 if (cftext)
3082 CFRelease(cftext);
3083 }
3084 }
3085
3086 cupsArrayRestore(ppd->sorted_attrs);
3087 }
3088
3089 /*
3090 * Fill in the profile data...
3091 */
3092
3093 if (iccfile)
3094 httpAssembleURI(HTTP_URI_CODING_ALL, url, sizeof(url), "file", NULL, "", 0,
3095 iccfile);
3096
3097 profile->dataVersion = cmDeviceProfileInfoVersion1;
3098 profile->profileID = id;
3099 profile->profileLoc.locType = iccfile ? cmPathBasedProfile : cmNoProfileBase;
3100 profile->profileName = dict;
3101
3102 if (iccfile)
3103 strlcpy(profile->profileLoc.u.pathLoc.path, iccfile,
3104 sizeof(profile->profileLoc.u.pathLoc.path));
3105 }
3106
3107
3108 /*
3109 * 'apple_register_profiles()' - Register color profiles for a printer.
3110 */
3111
3112 static void
3113 apple_register_profiles(
3114 cupsd_printer_t *p) /* I - Printer */
3115 {
3116 int i; /* Looping var */
3117 char ppdfile[1024], /* PPD filename */
3118 iccfile[1024], /* ICC filename */
3119 selector[PPD_MAX_NAME];
3120 /* Profile selection string */
3121 ppd_file_t *ppd; /* PPD file */
3122 ppd_attr_t *attr, /* Profile attributes */
3123 *profileid_attr,/* cupsProfileID attribute */
3124 *q1_attr, /* ColorModel (or other) qualifier */
3125 *q2_attr, /* MediaType (or other) qualifier */
3126 *q3_attr; /* Resolution (or other) qualifier */
3127 char q_keyword[PPD_MAX_NAME];
3128 /* Qualifier keyword */
3129 const char *q1_choice, /* ColorModel (or other) choice */
3130 *q2_choice, /* MediaType (or other) choice */
3131 *q3_choice; /* Resolution (or other) choice */
3132 const char *profile_key; /* Profile keyword */
3133 ppd_option_t *cm_option; /* Color model option */
3134 ppd_choice_t *cm_choice; /* Color model choice */
3135 int num_profiles; /* Number of profiles */
3136 CMError error; /* Last error */
3137 unsigned device_id, /* Printer device ID */
3138 profile_id, /* Profile ID */
3139 default_profile_id = 0;
3140 /* Default profile ID */
3141 CFMutableDictionaryRef device_name; /* Printer device name dictionary */
3142 CFStringRef printer_name; /* Printer name string */
3143 CMDeviceScope scope = /* Scope of the registration */
3144 {
3145 kCFPreferencesAnyUser,
3146 kCFPreferencesCurrentHost
3147 };
3148 CMDeviceProfileArrayPtr profiles; /* Profiles */
3149 CMDeviceProfileInfo *profile; /* Current profile */
3150 cups_array_t *languages; /* Languages array */
3151
3152
3153 /*
3154 * Make sure ColorSync is available...
3155 */
3156
3157 if (CMRegisterColorDevice == NULL)
3158 return;
3159
3160 /*
3161 * Try opening the PPD file for this printer...
3162 */
3163
3164 snprintf(ppdfile, sizeof(ppdfile), "%s/ppd/%s.ppd", ServerRoot, p->name);
3165 if ((ppd = ppdOpenFile(ppdfile)) == NULL)
3166 return;
3167
3168 /*
3169 * See if we have any profiles...
3170 */
3171
3172 if ((attr = ppdFindAttr(ppd, "APTiogaProfile", NULL)) != NULL)
3173 profile_key = "APTiogaProfile";
3174 else
3175 {
3176 attr = ppdFindAttr(ppd, "cupsICCProfile", NULL);
3177 profile_key = "cupsICCProfile";
3178 }
3179
3180 for (num_profiles = 0; attr; attr = ppdFindNextAttr(ppd, profile_key, NULL))
3181 if (attr->spec[0] && attr->value && attr->value[0])
3182 {
3183 if (attr->value[0] != '/')
3184 snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir,
3185 attr->value);
3186 else
3187 strlcpy(iccfile, attr->value, sizeof(iccfile));
3188
3189 if (access(iccfile, 0))
3190 continue;
3191
3192 num_profiles ++;
3193 }
3194
3195
3196 /*
3197 * If we have profiles, add them...
3198 */
3199
3200 if (num_profiles > 0)
3201 {
3202 if (profile_key[0] == 'A')
3203 {
3204 /*
3205 * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile
3206 * attribute...
3207 */
3208
3209 if ((attr = ppdFindAttr(ppd, "DefaultAPTiogaProfile", NULL)) != NULL &&
3210 attr->value)
3211 default_profile_id = atoi(attr->value);
3212
3213 q1_choice = q2_choice = q3_choice = NULL;
3214 }
3215 else
3216 {
3217 /*
3218 * For CUPS PPDs, figure out the default profile selector values...
3219 */
3220
3221 if ((attr = ppdFindAttr(ppd, "cupsICCQualifier1", NULL)) != NULL &&
3222 attr->value && attr->value[0])
3223 {
3224 snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value);
3225 q1_attr = ppdFindAttr(ppd, q_keyword, NULL);
3226 }
3227 else if ((q1_attr = ppdFindAttr(ppd, "DefaultColorModel", NULL)) == NULL)
3228 q1_attr = ppdFindAttr(ppd, "DefaultColorSpace", NULL);
3229
3230 if (q1_attr && q1_attr->value && q1_attr->value[0])
3231 q1_choice = q1_attr->value;
3232 else
3233 q1_choice = "";
3234
3235 if ((attr = ppdFindAttr(ppd, "cupsICCQualifier2", NULL)) != NULL &&
3236 attr->value && attr->value[0])
3237 {
3238 snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value);
3239 q2_attr = ppdFindAttr(ppd, q_keyword, NULL);
3240 }
3241 else
3242 q2_attr = ppdFindAttr(ppd, "DefaultMediaType", NULL);
3243
3244 if (q2_attr && q2_attr->value && q2_attr->value[0])
3245 q2_choice = q2_attr->value;
3246 else
3247 q2_choice = NULL;
3248
3249 if ((attr = ppdFindAttr(ppd, "cupsICCQualifier3", NULL)) != NULL &&
3250 attr->value && attr->value[0])
3251 {
3252 snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value);
3253 q3_attr = ppdFindAttr(ppd, q_keyword, NULL);
3254 }
3255 else
3256 q3_attr = ppdFindAttr(ppd, "DefaultResolution", NULL);
3257
3258 if (q3_attr && q3_attr->value && q3_attr->value[0])
3259 q3_choice = q3_attr->value;
3260 else
3261 q3_choice = NULL;
3262 }
3263
3264 /*
3265 * Build the array of profiles...
3266 *
3267 * Note: This calloc actually requests slightly more memory than needed.
3268 */
3269
3270 if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL)
3271 {
3272 cupsdLogMessage(CUPSD_LOG_ERROR,
3273 "Unable to allocate memory for %d profiles!",
3274 num_profiles);
3275 ppdClose(ppd);
3276 return;
3277 }
3278
3279 profiles->profileCount = num_profiles;
3280 languages = _ppdGetLanguages(ppd);
3281
3282 for (profile = profiles->profiles,
3283 attr = ppdFindAttr(ppd, profile_key, NULL);
3284 attr;
3285 attr = ppdFindNextAttr(ppd, profile_key, NULL))
3286 if (attr->spec[0] && attr->value && attr->value[0])
3287 {
3288 /*
3289 * Add this profile...
3290 */
3291
3292 if (attr->value[0] != '/')
3293 snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir,
3294 attr->value);
3295 else
3296 strlcpy(iccfile, attr->value, sizeof(iccfile));
3297
3298 if (access(iccfile, 0))
3299 continue;
3300
3301 if (profile_key[0] == 'c')
3302 {
3303 cupsArraySave(ppd->sorted_attrs);
3304
3305 if ((profileid_attr = ppdFindAttr(ppd, "cupsProfileID",
3306 attr->spec)) != NULL &&
3307 profileid_attr->value && isdigit(profileid_attr->value[0] & 255))
3308 profile_id = (unsigned)strtoul(profileid_attr->value, NULL, 10);
3309 else
3310 profile_id = _ppdHashName(attr->spec);
3311
3312 cupsArrayRestore(ppd->sorted_attrs);
3313 }
3314 else
3315 profile_id = atoi(attr->spec);
3316
3317 apple_init_profile(ppd, languages, profile, profile_id, attr->spec,
3318 attr->text[0] ? attr->text : attr->spec, iccfile);
3319
3320 profile ++;
3321
3322 /*
3323 * See if this is the default profile...
3324 */
3325
3326 if (!default_profile_id)
3327 {
3328 if (q2_choice)
3329 {
3330 if (q3_choice)
3331 {
3332 snprintf(selector, sizeof(selector), "%s.%s.%s",
3333 q1_choice, q2_choice, q3_choice);
3334 if (!strcmp(selector, attr->spec))
3335 default_profile_id = profile_id;
3336 }
3337
3338 if (!default_profile_id)
3339 {
3340 snprintf(selector, sizeof(selector), "%s.%s.", q1_choice,
3341 q2_choice);
3342 if (!strcmp(selector, attr->spec))
3343 default_profile_id = profile_id;
3344 }
3345 }
3346
3347 if (!default_profile_id && q3_choice)
3348 {
3349 snprintf(selector, sizeof(selector), "%s..%s", q1_choice,
3350 q3_choice);
3351 if (!strcmp(selector, attr->spec))
3352 default_profile_id = profile_id;
3353 }
3354
3355 if (!default_profile_id)
3356 {
3357 snprintf(selector, sizeof(selector), "%s..", q1_choice);
3358 if (!strcmp(selector, attr->spec))
3359 default_profile_id = profile_id;
3360 }
3361 }
3362 }
3363
3364 _ppdFreeLanguages(languages);
3365 }
3366 else if ((cm_option = ppdFindOption(ppd, "ColorModel")) != NULL)
3367 {
3368 /*
3369 * Extract profiles from ColorModel option...
3370 */
3371
3372 const char *profile_name; /* Name of generic profile */
3373
3374
3375 num_profiles = cm_option->num_choices;
3376
3377 if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL)
3378 {
3379 cupsdLogMessage(CUPSD_LOG_ERROR,
3380 "Unable to allocate memory for %d profiles!",
3381 num_profiles);
3382 ppdClose(ppd);
3383 return;
3384 }
3385
3386 profiles->profileCount = num_profiles;
3387
3388 for (profile = profiles->profiles, i = cm_option->num_choices,
3389 cm_choice = cm_option->choices;
3390 i > 0;
3391 i --, cm_choice ++, profile ++)
3392 {
3393 if (!strcmp(cm_choice->choice, "Gray") ||
3394 !strcmp(cm_choice->choice, "Black"))
3395 profile_name = "Gray";
3396 else if (!strcmp(cm_choice->choice, "RGB") ||
3397 !strcmp(cm_choice->choice, "CMY"))
3398 profile_name = "RGB";
3399 else if (!strcmp(cm_choice->choice, "CMYK") ||
3400 !strcmp(cm_choice->choice, "KCMY"))
3401 profile_name = "CMYK";
3402 else
3403 profile_name = "DeviceN";
3404
3405 snprintf(selector, sizeof(selector), "%s..", profile_name);
3406 profile_id = _ppdHashName(selector);
3407
3408 apple_init_profile(ppd, NULL, profile, profile_id, cm_choice->choice,
3409 cm_choice->text, NULL);
3410
3411 if (cm_choice->marked)
3412 default_profile_id = profile_id;
3413 }
3414 }
3415 else
3416 {
3417 /*
3418 * Use the default colorspace...
3419 */
3420
3421 num_profiles = 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_N :
3451 default :
3452 apple_init_profile(ppd, NULL, profiles->profiles + 1,
3453 _ppdHashName("DeviceN.."), "DeviceN", "DeviceN",
3454 NULL);
3455 break;
3456 }
3457 }
3458
3459 if (num_profiles > 0)
3460 {
3461 /*
3462 * Make sure we have a default profile ID...
3463 */
3464
3465 if (!default_profile_id)
3466 default_profile_id = profiles->profiles[num_profiles - 1].profileID;
3467
3468 /*
3469 * Get the device ID hash and pathelogical name dictionary.
3470 */
3471
3472 cupsdLogMessage(CUPSD_LOG_INFO, "Registering ICC color profiles for \"%s\"",
3473 p->name);
3474
3475 device_id = _ppdHashName(p->name);
3476 device_name = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
3477 &kCFTypeDictionaryKeyCallBacks,
3478 &kCFTypeDictionaryValueCallBacks);
3479 printer_name = CFStringCreateWithCString(kCFAllocatorDefault,
3480 p->name, kCFStringEncodingUTF8);
3481
3482 if (device_name && printer_name)
3483 {
3484 CFDictionarySetValue(device_name, CFSTR("en"), printer_name);
3485
3486 /*
3487 * Register the device with ColorSync...
3488 */
3489
3490 error = CMRegisterColorDevice(cmPrinterDeviceClass, device_id,
3491 device_name, &scope);
3492
3493 /*
3494 * Register the profiles...
3495 */
3496
3497 if (error == noErr)
3498 error = CMSetDeviceFactoryProfiles(cmPrinterDeviceClass, device_id,
3499 default_profile_id, profiles);
3500 }
3501 else
3502 error = 1000;
3503
3504 /*
3505 * Clean up...
3506 */
3507
3508 if (error != noErr)
3509 cupsdLogMessage(CUPSD_LOG_ERROR,
3510 "Unable to register ICC color profiles for \"%s\" - %d",
3511 p->name, (int)error);
3512
3513 for (profile = profiles->profiles;
3514 num_profiles > 0;
3515 profile ++, num_profiles --)
3516 CFRelease(profile->profileName);
3517
3518 free(profiles);
3519
3520 if (printer_name)
3521 CFRelease(printer_name);
3522
3523 if (device_name)
3524 CFRelease(device_name);
3525 }
3526
3527 ppdClose(ppd);
3528 }
3529
3530
3531 /*
3532 * 'apple_unregister_profiles()' - Remove color profiles for the specified
3533 * printer.
3534 */
3535
3536 static void
3537 apple_unregister_profiles(
3538 cupsd_printer_t *p) /* I - Printer */
3539 {
3540 /*
3541 * Make sure ColorSync is available...
3542 */
3543
3544 if (CMUnregisterColorDevice != NULL)
3545 CMUnregisterColorDevice(cmPrinterDeviceClass, _ppdHashName(p->name));
3546 }
3547 #endif /* __APPLE__ */
3548
3549 /*
3550 * 'apply_printer_defaults()' - Apply printer default options to a job.
3551 */
3552
3553 static void
3554 apply_printer_defaults(
3555 cupsd_printer_t *printer, /* I - Printer */
3556 cupsd_job_t *job) /* I - Job */
3557 {
3558 int i, /* Looping var */
3559 num_options; /* Number of default options */
3560 cups_option_t *options, /* Default options */
3561 *option; /* Current option */
3562
3563
3564 /*
3565 * Collect all of the default options and add the missing ones to the
3566 * job object...
3567 */
3568
3569 for (i = printer->num_options, num_options = 0, options = NULL,
3570 option = printer->options;
3571 i > 0;
3572 i --, option ++)
3573 if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO))
3574 {
3575 num_options = cupsAddOption(option->name, option->value, num_options,
3576 &options);
3577 }
3578
3579 /*
3580 * Encode these options as attributes in the job object...
3581 */
3582
3583 cupsEncodeOptions2(job->attrs, num_options, options, IPP_TAG_JOB);
3584 cupsFreeOptions(num_options, options);
3585 }
3586
3587
3588 /*
3589 * 'authenticate_job()' - Set job authentication info.
3590 */
3591
3592 static void
3593 authenticate_job(cupsd_client_t *con, /* I - Client connection */
3594 ipp_attribute_t *uri) /* I - Job URI */
3595 {
3596 ipp_attribute_t *attr, /* job-id attribute */
3597 *auth_info; /* auth-info attribute */
3598 int jobid; /* Job ID */
3599 cupsd_job_t *job; /* Current job */
3600 char scheme[HTTP_MAX_URI],
3601 /* Method portion of URI */
3602 username[HTTP_MAX_URI],
3603 /* Username portion of URI */
3604 host[HTTP_MAX_URI],
3605 /* Host portion of URI */
3606 resource[HTTP_MAX_URI];
3607 /* Resource portion of URI */
3608 int port; /* Port portion of URI */
3609
3610
3611 cupsdLogMessage(CUPSD_LOG_DEBUG2, "authenticate_job(%p[%d], %s)",
3612 con, con->http.fd, uri->values[0].string.text);
3613
3614 /*
3615 * Start with "everything is OK" status...
3616 */
3617
3618 con->response->request.status.status_code = IPP_OK;
3619
3620 /*
3621 * See if we have a job URI or a printer URI...
3622 */
3623
3624 if (!strcmp(uri->name, "printer-uri"))
3625 {
3626 /*
3627 * Got a printer URI; see if we also have a job-id attribute...
3628 */
3629
3630 if ((attr = ippFindAttribute(con->request, "job-id",
3631 IPP_TAG_INTEGER)) == NULL)
3632 {
3633 send_ipp_status(con, IPP_BAD_REQUEST,
3634 _("Got a printer-uri attribute but no job-id!"));
3635 return;
3636 }
3637
3638 jobid = attr->values[0].integer;
3639 }
3640 else
3641 {
3642 /*
3643 * Got a job URI; parse it to get the job ID...
3644 */
3645
3646 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
3647 sizeof(scheme), username, sizeof(username), host,
3648 sizeof(host), &port, resource, sizeof(resource));
3649
3650 if (strncmp(resource, "/jobs/", 6))
3651 {
3652 /*
3653 * Not a valid URI!
3654 */
3655
3656 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri attribute \"%s\"!"),
3657 uri->values[0].string.text);
3658 return;
3659 }
3660
3661 jobid = atoi(resource + 6);
3662 }
3663
3664 /*
3665 * See if the job exists...
3666 */
3667
3668 if ((job = cupsdFindJob(jobid)) == NULL)
3669 {
3670 /*
3671 * Nope - return a "not found" error...
3672 */
3673
3674 send_ipp_status(con, IPP_NOT_FOUND,
3675 _("Job #%d does not exist!"), jobid);
3676 return;
3677 }
3678
3679 /*
3680 * See if the job has been completed...
3681 */
3682
3683 if (job->state_value != IPP_JOB_HELD)
3684 {
3685 /*
3686 * Return a "not-possible" error...
3687 */
3688
3689 send_ipp_status(con, IPP_NOT_POSSIBLE,
3690 _("Job #%d is not held for authentication!"),
3691 jobid);
3692 return;
3693 }
3694
3695 /*
3696 * See if we have already authenticated...
3697 */
3698
3699 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
3700
3701 if (!con->username[0] && !auth_info)
3702 {
3703 cupsd_printer_t *printer; /* Job destination */
3704
3705
3706 /*
3707 * No auth data. If we need to authenticate via Kerberos, send a
3708 * HTTP auth challenge, otherwise just return an IPP error...
3709 */
3710
3711 printer = cupsdFindDest(job->dest);
3712
3713 if (printer && printer->num_auth_info_required > 0 &&
3714 !strcmp(printer->auth_info_required[0], "negotiate"))
3715 send_http_error(con, HTTP_UNAUTHORIZED, printer);
3716 else
3717 send_ipp_status(con, IPP_NOT_AUTHORIZED,
3718 _("No authentication information provided!"));
3719 return;
3720 }
3721
3722 /*
3723 * See if the job is owned by the requesting user...
3724 */
3725
3726 if (!validate_user(job, con, job->username, username, sizeof(username)))
3727 {
3728 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
3729 return;
3730 }
3731
3732 /*
3733 * Save the authentication information for this job...
3734 */
3735
3736 save_auth_info(con, job, auth_info);
3737
3738 /*
3739 * Reset the job-hold-until value to "no-hold"...
3740 */
3741
3742 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
3743 IPP_TAG_KEYWORD)) == NULL)
3744 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
3745
3746 if (attr)
3747 {
3748 attr->value_tag = IPP_TAG_KEYWORD;
3749 cupsdSetString(&(attr->values[0].string.text), "no-hold");
3750 }
3751
3752 /*
3753 * Release the job and return...
3754 */
3755
3756 cupsdReleaseJob(job);
3757
3758 cupsdLogJob(job, CUPSD_LOG_INFO, "Authenticated by \"%s\".", con->username);
3759 }
3760
3761
3762 /*
3763 * 'cancel_all_jobs()' - Cancel all print jobs.
3764 */
3765
3766 static void
3767 cancel_all_jobs(cupsd_client_t *con, /* I - Client connection */
3768 ipp_attribute_t *uri) /* I - Job or Printer URI */
3769 {
3770 http_status_t status; /* Policy status */
3771 cups_ptype_t dtype; /* Destination type */
3772 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
3773 userpass[HTTP_MAX_URI], /* Username portion of URI */
3774 hostname[HTTP_MAX_URI], /* Host portion of URI */
3775 resource[HTTP_MAX_URI]; /* Resource portion of URI */
3776 int port; /* Port portion of URI */
3777 ipp_attribute_t *attr; /* Attribute in request */
3778 const char *username; /* Username */
3779 int purge; /* Purge? */
3780 cupsd_printer_t *printer; /* Printer */
3781
3782
3783 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_all_jobs(%p[%d], %s)", con,
3784 con->http.fd, uri->values[0].string.text);
3785
3786 /*
3787 * See if we have a printer URI...
3788 */
3789
3790 if (strcmp(uri->name, "printer-uri"))
3791 {
3792 send_ipp_status(con, IPP_BAD_REQUEST,
3793 _("The printer-uri attribute is required!"));
3794 return;
3795 }
3796
3797 /*
3798 * Get the username (if any) for the jobs we want to cancel (only if
3799 * "my-jobs" is specified...
3800 */
3801
3802 if ((attr = ippFindAttribute(con->request, "my-jobs",
3803 IPP_TAG_BOOLEAN)) != NULL &&
3804 attr->values[0].boolean)
3805 {
3806 if ((attr = ippFindAttribute(con->request, "requesting-user-name",
3807 IPP_TAG_NAME)) != NULL)
3808 username = attr->values[0].string.text;
3809 else
3810 {
3811 send_ipp_status(con, IPP_BAD_REQUEST,
3812 _("Missing requesting-user-name attribute!"));
3813 return;
3814 }
3815 }
3816 else
3817 username = NULL;
3818
3819 /*
3820 * Look for the "purge-jobs" attribute...
3821 */
3822
3823 if ((attr = ippFindAttribute(con->request, "purge-jobs",
3824 IPP_TAG_BOOLEAN)) != NULL)
3825 purge = attr->values[0].boolean;
3826 else
3827 purge = 1;
3828
3829 /*
3830 * And if the destination is valid...
3831 */
3832
3833 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
3834 {
3835 /*
3836 * Bad URI?
3837 */
3838
3839 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text,
3840 scheme, sizeof(scheme), userpass, sizeof(userpass),
3841 hostname, sizeof(hostname), &port,
3842 resource, sizeof(resource));
3843
3844 if ((!strncmp(resource, "/printers/", 10) && resource[10]) ||
3845 (!strncmp(resource, "/classes/", 9) && resource[9]))
3846 {
3847 send_ipp_status(con, IPP_NOT_FOUND,
3848 _("The printer or class was not found."));
3849 return;
3850 }
3851
3852 /*
3853 * Check policy...
3854 */
3855
3856 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
3857 {
3858 send_http_error(con, status, NULL);
3859 return;
3860 }
3861
3862 /*
3863 * Cancel all jobs on all printers...
3864 */
3865
3866 cupsdCancelJobs(NULL, username, purge);
3867
3868 cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".",
3869 purge ? "purged" : "canceled", get_username(con));
3870 }
3871 else
3872 {
3873 /*
3874 * Check policy...
3875 */
3876
3877 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
3878 NULL)) != HTTP_OK)
3879 {
3880 send_http_error(con, status, printer);
3881 return;
3882 }
3883
3884 /*
3885 * Cancel all of the jobs on the named printer...
3886 */
3887
3888 cupsdCancelJobs(printer->name, username, purge);
3889
3890 cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".",
3891 printer->name, purge ? "purged" : "canceled",
3892 get_username(con));
3893 }
3894
3895 con->response->request.status.status_code = IPP_OK;
3896 }
3897
3898
3899 /*
3900 * 'cancel_job()' - Cancel a print job.
3901 */
3902
3903 static void
3904 cancel_job(cupsd_client_t *con, /* I - Client connection */
3905 ipp_attribute_t *uri) /* I - Job or Printer URI */
3906 {
3907 ipp_attribute_t *attr; /* Current attribute */
3908 int jobid; /* Job ID */
3909 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
3910 username[HTTP_MAX_URI], /* Username portion of URI */
3911 host[HTTP_MAX_URI], /* Host portion of URI */
3912 resource[HTTP_MAX_URI]; /* Resource portion of URI */
3913 int port; /* Port portion of URI */
3914 cupsd_job_t *job; /* Job information */
3915 cups_ptype_t dtype; /* Destination type (printer/class) */
3916 cupsd_printer_t *printer; /* Printer data */
3917 int purge; /* Purge the job? */
3918
3919
3920 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con,
3921 con->http.fd, uri->values[0].string.text);
3922
3923 /*
3924 * See if we have a job URI or a printer URI...
3925 */
3926
3927 if (!strcmp(uri->name, "printer-uri"))
3928 {
3929 /*
3930 * Got a printer URI; see if we also have a job-id attribute...
3931 */
3932
3933 if ((attr = ippFindAttribute(con->request, "job-id",
3934 IPP_TAG_INTEGER)) == NULL)
3935 {
3936 send_ipp_status(con, IPP_BAD_REQUEST,
3937 _("Got a printer-uri attribute but no job-id!"));
3938 return;
3939 }
3940
3941 if ((jobid = attr->values[0].integer) == 0)
3942 {
3943 /*
3944 * Find the current job on the specified printer...
3945 */
3946
3947 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
3948 {
3949 /*
3950 * Bad URI...
3951 */
3952
3953 send_ipp_status(con, IPP_NOT_FOUND,
3954 _("The printer or class was not found."));
3955 return;
3956 }
3957
3958 /*
3959 * See if the printer is currently printing a job...
3960 */
3961
3962 if (printer->job)
3963 jobid = ((cupsd_job_t *)printer->job)->id;
3964 else
3965 {
3966 /*
3967 * No, see if there are any pending jobs...
3968 */
3969
3970 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
3971 job;
3972 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
3973 if (job->state_value <= IPP_JOB_PROCESSING &&
3974 !strcasecmp(job->dest, printer->name))
3975 break;
3976
3977 if (job)
3978 jobid = job->id;
3979 else
3980 {
3981 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
3982 job;
3983 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
3984 if (job->state_value == IPP_JOB_STOPPED &&
3985 !strcasecmp(job->dest, printer->name))
3986 break;
3987
3988 if (job)
3989 jobid = job->id;
3990 else
3991 {
3992 send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s!"),
3993 printer->name);
3994 return;
3995 }
3996 }
3997 }
3998 }
3999 }
4000 else
4001 {
4002 /*
4003 * Got a job URI; parse it to get the job ID...
4004 */
4005
4006 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
4007 sizeof(scheme), username, sizeof(username), host,
4008 sizeof(host), &port, resource, sizeof(resource));
4009
4010 if (strncmp(resource, "/jobs/", 6))
4011 {
4012 /*
4013 * Not a valid URI!
4014 */
4015
4016 send_ipp_status(con, IPP_BAD_REQUEST,
4017 _("Bad job-uri attribute \"%s\"!"),
4018 uri->values[0].string.text);
4019 return;
4020 }
4021
4022 jobid = atoi(resource + 6);
4023 }
4024
4025 /*
4026 * Look for the "purge-job" attribute...
4027 */
4028
4029 if ((attr = ippFindAttribute(con->request, "purge-job",
4030 IPP_TAG_BOOLEAN)) != NULL)
4031 purge = attr->values[0].boolean;
4032 else
4033 purge = 0;
4034
4035 /*
4036 * See if the job exists...
4037 */
4038
4039 if ((job = cupsdFindJob(jobid)) == NULL)
4040 {
4041 /*
4042 * Nope - return a "not found" error...
4043 */
4044
4045 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
4046 return;
4047 }
4048
4049 /*
4050 * See if the job is owned by the requesting user...
4051 */
4052
4053 if (!validate_user(job, con, job->username, username, sizeof(username)))
4054 {
4055 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
4056 return;
4057 }
4058
4059 /*
4060 * See if the job is already completed, canceled, or aborted; if so,
4061 * we can't cancel...
4062 */
4063
4064 if (job->state_value >= IPP_JOB_CANCELED && !purge)
4065 {
4066 switch (job->state_value)
4067 {
4068 case IPP_JOB_CANCELED :
4069 send_ipp_status(con, IPP_NOT_POSSIBLE,
4070 _("Job #%d is already canceled - can\'t cancel."),
4071 jobid);
4072 break;
4073
4074 case IPP_JOB_ABORTED :
4075 send_ipp_status(con, IPP_NOT_POSSIBLE,
4076 _("Job #%d is already aborted - can\'t cancel."),
4077 jobid);
4078 break;
4079
4080 default :
4081 send_ipp_status(con, IPP_NOT_POSSIBLE,
4082 _("Job #%d is already completed - can\'t cancel."),
4083 jobid);
4084 break;
4085 }
4086
4087 return;
4088 }
4089
4090 /*
4091 * Cancel the job and return...
4092 */
4093
4094 cupsdCancelJob(job, purge, IPP_JOB_CANCELED);
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, &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, "/"))
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 *newattr; /* New job-hold-until */
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 cupsdHoldJob(job);
8071
8072 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
8073 "Job held by user.");
8074
8075 if ((newattr = ippFindAttribute(con->request, "job-hold-until",
8076 IPP_TAG_KEYWORD)) == NULL)
8077 newattr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
8078
8079 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
8080 IPP_TAG_KEYWORD)) == NULL)
8081 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
8082
8083 if (attr)
8084 {
8085 /*
8086 * Free the old hold value and copy the new one over...
8087 */
8088
8089 _cupsStrFree(attr->values[0].string.text);
8090
8091 if (newattr)
8092 {
8093 attr->value_tag = newattr->value_tag;
8094 attr->values[0].string.text =
8095 _cupsStrRetain(newattr->values[0].string.text);
8096 }
8097 else
8098 {
8099 attr->value_tag = IPP_TAG_KEYWORD;
8100 attr->values[0].string.text = _cupsStrAlloc("indefinite");
8101 }
8102
8103 /*
8104 * Hold job until specified time...
8105 */
8106
8107 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
8108
8109 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
8110 "Job job-hold-until value changed by user.");
8111 }
8112
8113 cupsdLogJob(job, CUPSD_LOG_INFO, "Held by \"%s\".", username);
8114
8115 con->response->request.status.status_code = IPP_OK;
8116 }
8117
8118
8119 /*
8120 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
8121 */
8122
8123 static void
8124 hold_new_jobs(cupsd_client_t *con, /* I - Connection */
8125 ipp_attribute_t *uri) /* I - Printer URI */
8126 {
8127 http_status_t status; /* Policy status */
8128 cups_ptype_t dtype; /* Destination type (printer/class) */
8129 cupsd_printer_t *printer; /* Printer data */
8130
8131
8132 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_new_jobs(%p[%d], %s)", con,
8133 con->http.fd, uri->values[0].string.text);
8134
8135 /*
8136 * Is the destination valid?
8137 */
8138
8139 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8140 {
8141 /*
8142 * Bad URI...
8143 */
8144
8145 send_ipp_status(con, IPP_NOT_FOUND,
8146 _("The printer or class was not found."));
8147 return;
8148 }
8149
8150 /*
8151 * Check policy...
8152 */
8153
8154 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8155 {
8156 send_http_error(con, status, printer);
8157 return;
8158 }
8159
8160 /*
8161 * Hold pending/new jobs sent to the printer...
8162 */
8163
8164 printer->holding_new_jobs = 1;
8165
8166 cupsdSetPrinterReasons(printer, "+hold-new-jobs");
8167 cupsdAddPrinterHistory(printer);
8168
8169 if (dtype & CUPS_PRINTER_CLASS)
8170 cupsdLogMessage(CUPSD_LOG_INFO,
8171 "Class \"%s\" now holding pending/new jobs (\"%s\").",
8172 printer->name, get_username(con));
8173 else
8174 cupsdLogMessage(CUPSD_LOG_INFO,
8175 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
8176 printer->name, get_username(con));
8177
8178 /*
8179 * Everything was ok, so return OK status...
8180 */
8181
8182 con->response->request.status.status_code = IPP_OK;
8183 }
8184
8185
8186 /*
8187 * 'move_job()' - Move a job to a new destination.
8188 */
8189
8190 static void
8191 move_job(cupsd_client_t *con, /* I - Client connection */
8192 ipp_attribute_t *uri) /* I - Job URI */
8193 {
8194 http_status_t status; /* Policy status */
8195 ipp_attribute_t *attr; /* Current attribute */
8196 int jobid; /* Job ID */
8197 cupsd_job_t *job; /* Current job */
8198 const char *src; /* Source printer/class */
8199 cups_ptype_t stype, /* Source type (printer or class) */
8200 dtype; /* Destination type (printer/class) */
8201 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
8202 username[HTTP_MAX_URI], /* Username portion of URI */
8203 host[HTTP_MAX_URI], /* Host portion of URI */
8204 resource[HTTP_MAX_URI]; /* Resource portion of URI */
8205 int port; /* Port portion of URI */
8206 cupsd_printer_t *sprinter, /* Source printer */
8207 *dprinter; /* Destination printer */
8208
8209
8210 cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd,
8211 uri->values[0].string.text);
8212
8213 /*
8214 * Get the new printer or class...
8215 */
8216
8217 if ((attr = ippFindAttribute(con->request, "job-printer-uri",
8218 IPP_TAG_URI)) == NULL)
8219 {
8220 /*
8221 * Need job-printer-uri...
8222 */
8223
8224 send_ipp_status(con, IPP_BAD_REQUEST,
8225 _("job-printer-uri attribute missing!"));
8226 return;
8227 }
8228
8229 if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter))
8230 {
8231 /*
8232 * Bad URI...
8233 */
8234
8235 send_ipp_status(con, IPP_NOT_FOUND,
8236 _("The printer or class was not found."));
8237 return;
8238 }
8239
8240 /*
8241 * See if we have a job URI or a printer URI...
8242 */
8243
8244 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
8245 sizeof(scheme), username, sizeof(username), host,
8246 sizeof(host), &port, resource, sizeof(resource));
8247
8248 if (!strcmp(uri->name, "printer-uri"))
8249 {
8250 /*
8251 * Got a printer URI; see if we also have a job-id attribute...
8252 */
8253
8254 if ((attr = ippFindAttribute(con->request, "job-id",
8255 IPP_TAG_INTEGER)) == NULL)
8256 {
8257 /*
8258 * Move all jobs...
8259 */
8260
8261 if ((src = cupsdValidateDest(uri->values[0].string.text, &stype,
8262 &sprinter)) == NULL)
8263 {
8264 /*
8265 * Bad URI...
8266 */
8267
8268 send_ipp_status(con, IPP_NOT_FOUND,
8269 _("The printer or class was not found."));
8270 return;
8271 }
8272
8273 job = NULL;
8274 }
8275 else
8276 {
8277 /*
8278 * Otherwise, just move a single job...
8279 */
8280
8281 if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
8282 {
8283 /*
8284 * Nope - return a "not found" error...
8285 */
8286
8287 send_ipp_status(con, IPP_NOT_FOUND,
8288 _("Job #%d does not exist!"), attr->values[0].integer);
8289 return;
8290 }
8291 else
8292 {
8293 /*
8294 * Job found, initialize source pointers...
8295 */
8296
8297 src = NULL;
8298 sprinter = NULL;
8299 }
8300 }
8301 }
8302 else
8303 {
8304 /*
8305 * Got a job URI; parse it to get the job ID...
8306 */
8307
8308 if (strncmp(resource, "/jobs/", 6))
8309 {
8310 /*
8311 * Not a valid URI!
8312 */
8313
8314 send_ipp_status(con, IPP_BAD_REQUEST,
8315 _("Bad job-uri attribute \"%s\"!"),
8316 uri->values[0].string.text);
8317 return;
8318 }
8319
8320 /*
8321 * See if the job exists...
8322 */
8323
8324 jobid = atoi(resource + 6);
8325
8326 if ((job = cupsdFindJob(jobid)) == NULL)
8327 {
8328 /*
8329 * Nope - return a "not found" error...
8330 */
8331
8332 send_ipp_status(con, IPP_NOT_FOUND,
8333 _("Job #%d does not exist!"), jobid);
8334 return;
8335 }
8336 else
8337 {
8338 /*
8339 * Job found, initialize source pointers...
8340 */
8341
8342 src = NULL;
8343 sprinter = NULL;
8344 }
8345 }
8346
8347 /*
8348 * Check the policy of the destination printer...
8349 */
8350
8351 if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con,
8352 job ? job->username : NULL)) != HTTP_OK)
8353 {
8354 send_http_error(con, status, dprinter);
8355 return;
8356 }
8357
8358 /*
8359 * Now move the job or jobs...
8360 */
8361
8362 if (job)
8363 {
8364 /*
8365 * See if the job has been completed...
8366 */
8367
8368 if (job->state_value > IPP_JOB_STOPPED)
8369 {
8370 /*
8371 * Return a "not-possible" error...
8372 */
8373
8374 send_ipp_status(con, IPP_NOT_POSSIBLE,
8375 _("Job #%d is finished and cannot be altered!"),
8376 job->id);
8377 return;
8378 }
8379
8380 /*
8381 * See if the job is owned by the requesting user...
8382 */
8383
8384 if (!validate_user(job, con, job->username, username, sizeof(username)))
8385 {
8386 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
8387 return;
8388 }
8389
8390 /*
8391 * Move the job to a different printer or class...
8392 */
8393
8394 cupsdMoveJob(job, dprinter);
8395 }
8396 else
8397 {
8398 /*
8399 * Got the source printer, now look through the jobs...
8400 */
8401
8402 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
8403 job;
8404 job = (cupsd_job_t *)cupsArrayNext(Jobs))
8405 {
8406 /*
8407 * See if the job is pointing at the source printer or has not been
8408 * completed...
8409 */
8410
8411 if (strcasecmp(job->dest, src) ||
8412 job->state_value > IPP_JOB_STOPPED)
8413 continue;
8414
8415 /*
8416 * See if the job can be moved by the requesting user...
8417 */
8418
8419 if (!validate_user(job, con, job->username, username, sizeof(username)))
8420 continue;
8421
8422 /*
8423 * Move the job to a different printer or class...
8424 */
8425
8426 cupsdMoveJob(job, dprinter);
8427 }
8428 }
8429
8430 /*
8431 * Start jobs if possible...
8432 */
8433
8434 cupsdCheckJobs();
8435
8436 /*
8437 * Return with "everything is OK" status...
8438 */
8439
8440 con->response->request.status.status_code = IPP_OK;
8441 }
8442
8443
8444 /*
8445 * 'ppd_parse_line()' - Parse a PPD default line.
8446 */
8447
8448 static int /* O - 0 on success, -1 on failure */
8449 ppd_parse_line(const char *line, /* I - Line */
8450 char *option, /* O - Option name */
8451 int olen, /* I - Size of option name */
8452 char *choice, /* O - Choice name */
8453 int clen) /* I - Size of choice name */
8454 {
8455 /*
8456 * Verify this is a default option line...
8457 */
8458
8459 if (strncmp(line, "*Default", 8))
8460 return (-1);
8461
8462 /*
8463 * Read the option name...
8464 */
8465
8466 for (line += 8, olen --; isalnum(*line & 255); line ++)
8467 if (olen > 0)
8468 {
8469 *option++ = *line;
8470 olen --;
8471 }
8472
8473 *option = '\0';
8474
8475 /*
8476 * Skip everything else up to the colon (:)...
8477 */
8478
8479 while (*line && *line != ':')
8480 line ++;
8481
8482 if (!*line)
8483 return (-1);
8484
8485 line ++;
8486
8487 /*
8488 * Now grab the option choice, skipping leading whitespace...
8489 */
8490
8491 while (isspace(*line & 255))
8492 line ++;
8493
8494 for (clen --; isalnum(*line & 255); line ++)
8495 if (clen > 0)
8496 {
8497 *choice++ = *line;
8498 clen --;
8499 }
8500
8501 *choice = '\0';
8502
8503 /*
8504 * Return with no errors...
8505 */
8506
8507 return (0);
8508 }
8509
8510
8511 /*
8512 * 'print_job()' - Print a file to a printer or class.
8513 */
8514
8515 static void
8516 print_job(cupsd_client_t *con, /* I - Client connection */
8517 ipp_attribute_t *uri) /* I - Printer URI */
8518 {
8519 ipp_attribute_t *attr; /* Current attribute */
8520 ipp_attribute_t *format; /* Document-format attribute */
8521 const char *default_format; /* document-format-default value */
8522 cupsd_job_t *job; /* New job */
8523 char filename[1024]; /* Job filename */
8524 mime_type_t *filetype; /* Type of file */
8525 char super[MIME_MAX_SUPER], /* Supertype of file */
8526 type[MIME_MAX_TYPE], /* Subtype of file */
8527 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
8528 /* Textual name of mime type */
8529 cupsd_printer_t *printer; /* Printer data */
8530 struct stat fileinfo; /* File information */
8531 int kbytes; /* Size of file */
8532 int compression; /* Document compression */
8533
8534
8535 cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd,
8536 uri->values[0].string.text);
8537
8538 /*
8539 * Validate print file attributes, for now just document-format and
8540 * compression (CUPS only supports "none" and "gzip")...
8541 */
8542
8543 compression = CUPS_FILE_NONE;
8544
8545 if ((attr = ippFindAttribute(con->request, "compression",
8546 IPP_TAG_KEYWORD)) != NULL)
8547 {
8548 if (strcmp(attr->values[0].string.text, "none")
8549 #ifdef HAVE_LIBZ
8550 && strcmp(attr->values[0].string.text, "gzip")
8551 #endif /* HAVE_LIBZ */
8552 )
8553 {
8554 send_ipp_status(con, IPP_ATTRIBUTES,
8555 _("Unsupported compression \"%s\"!"),
8556 attr->values[0].string.text);
8557 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
8558 "compression", NULL, attr->values[0].string.text);
8559 return;
8560 }
8561
8562 #ifdef HAVE_LIBZ
8563 if (!strcmp(attr->values[0].string.text, "gzip"))
8564 compression = CUPS_FILE_GZIP;
8565 #endif /* HAVE_LIBZ */
8566 }
8567
8568 /*
8569 * Do we have a file to print?
8570 */
8571
8572 if (!con->filename)
8573 {
8574 send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!"));
8575 return;
8576 }
8577
8578 /*
8579 * Is the destination valid?
8580 */
8581
8582 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
8583 {
8584 /*
8585 * Bad URI...
8586 */
8587
8588 send_ipp_status(con, IPP_NOT_FOUND,
8589 _("The printer or class was not found."));
8590 return;
8591 }
8592
8593 /*
8594 * Is it a format we support?
8595 */
8596
8597 if ((format = ippFindAttribute(con->request, "document-format",
8598 IPP_TAG_MIMETYPE)) != NULL)
8599 {
8600 /*
8601 * Grab format from client...
8602 */
8603
8604 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super,
8605 type) != 2)
8606 {
8607 send_ipp_status(con, IPP_BAD_REQUEST,
8608 _("Could not scan type \"%s\"!"),
8609 format->values[0].string.text);
8610 return;
8611 }
8612 }
8613 else if ((default_format = cupsGetOption("document-format",
8614 printer->num_options,
8615 printer->options)) != NULL)
8616 {
8617 /*
8618 * Use default document format...
8619 */
8620
8621 if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
8622 {
8623 send_ipp_status(con, IPP_BAD_REQUEST,
8624 _("Could not scan type \"%s\"!"),
8625 default_format);
8626 return;
8627 }
8628 }
8629 else
8630 {
8631 /*
8632 * Auto-type it!
8633 */
8634
8635 strcpy(super, "application");
8636 strcpy(type, "octet-stream");
8637 }
8638
8639 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
8640 {
8641 /*
8642 * Auto-type the file...
8643 */
8644
8645 ipp_attribute_t *doc_name; /* document-name attribute */
8646
8647
8648 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file...");
8649
8650 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
8651 filetype = mimeFileType(MimeDatabase, con->filename,
8652 doc_name ? doc_name->values[0].string.text : NULL,
8653 &compression);
8654
8655 if (!filetype)
8656 filetype = mimeType(MimeDatabase, super, type);
8657
8658 cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.",
8659 filetype->super, filetype->type);
8660 }
8661 else
8662 filetype = mimeType(MimeDatabase, super, type);
8663
8664 if (filetype &&
8665 (!format ||
8666 (!strcmp(super, "application") && !strcmp(type, "octet-stream"))))
8667 {
8668 /*
8669 * Replace the document-format attribute value with the auto-typed or
8670 * default one.
8671 */
8672
8673 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
8674 filetype->type);
8675
8676 if (format)
8677 {
8678 _cupsStrFree(format->values[0].string.text);
8679
8680 format->values[0].string.text = _cupsStrAlloc(mimetype);
8681 }
8682 else
8683 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
8684 "document-format", NULL, mimetype);
8685 }
8686 else if (!filetype)
8687 {
8688 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
8689 _("Unsupported format \'%s/%s\'!"), super, type);
8690 cupsdLogMessage(CUPSD_LOG_INFO,
8691 "Hint: Do you have the raw file printing rules enabled?");
8692
8693 if (format)
8694 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
8695 "document-format", NULL, format->values[0].string.text);
8696
8697 return;
8698 }
8699
8700 /*
8701 * Read any embedded job ticket info from PS files...
8702 */
8703
8704 if (!strcasecmp(filetype->super, "application") &&
8705 (!strcasecmp(filetype->type, "postscript") ||
8706 !strcasecmp(filetype->type, "pdf")))
8707 read_job_ticket(con);
8708
8709 /*
8710 * Create the job object...
8711 */
8712
8713 if ((job = add_job(con, printer, filetype)) == NULL)
8714 return;
8715
8716 /*
8717 * Update quota data...
8718 */
8719
8720 if (stat(con->filename, &fileinfo))
8721 kbytes = 0;
8722 else
8723 kbytes = (fileinfo.st_size + 1023) / 1024;
8724
8725 cupsdUpdateQuota(printer, job->username, 0, kbytes);
8726
8727 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
8728 IPP_TAG_INTEGER)) != NULL)
8729 attr->values[0].integer += kbytes;
8730
8731 /*
8732 * Add the job file...
8733 */
8734
8735 if (add_file(con, job, filetype, compression))
8736 return;
8737
8738 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
8739 job->num_files);
8740 rename(con->filename, filename);
8741 cupsdClearString(&con->filename);
8742
8743 /*
8744 * See if we need to add the ending sheet...
8745 */
8746
8747 if (cupsdTimeoutJob(job))
8748 return;
8749
8750 /*
8751 * Log and save the job...
8752 */
8753
8754 cupsdLogJob(job, CUPSD_LOG_INFO,
8755 "File of type %s/%s queued by \"%s\".",
8756 filetype->super, filetype->type, job->username);
8757 cupsdLogJob(job, CUPSD_LOG_DEBUG, "hold_until=%d", (int)job->hold_until);
8758 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
8759 job->dest, job->username);
8760
8761 /*
8762 * Start the job if possible...
8763 */
8764
8765 cupsdCheckJobs();
8766 }
8767
8768
8769 /*
8770 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8771 *
8772 * This function only gets called when printing a single PDF or PostScript
8773 * file using the Print-Job operation. It doesn't work for Create-Job +
8774 * Send-File, since the job attributes need to be set at job creation
8775 * time for banners to work. The embedded job ticket stuff is here
8776 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8777 * options and IPP attributes which otherwise would be lost.
8778 *
8779 * The format of a job ticket is simple:
8780 *
8781 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8782 *
8783 * %cupsJobTicket: attr1=value1
8784 * %cupsJobTicket: attr2=value2
8785 * ...
8786 * %cupsJobTicket: attrN=valueN
8787 *
8788 * Job ticket lines must appear immediately after the first line that
8789 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8790 * stops looking for job ticket info when it finds a line that does not begin
8791 * with "%cupsJobTicket:".
8792 *
8793 * The maximum length of a job ticket line, including the prefix, is
8794 * 255 characters to conform with the Adobe DSC.
8795 *
8796 * Read-only attributes are rejected with a notice to the error log in
8797 * case a malicious user tries anything. Since the job ticket is read
8798 * prior to attribute validation in print_job(), job ticket attributes
8799 * will go through the same validation as IPP attributes...
8800 */
8801
8802 static void
8803 read_job_ticket(cupsd_client_t *con) /* I - Client connection */
8804 {
8805 cups_file_t *fp; /* File to read from */
8806 char line[256]; /* Line data */
8807 int num_options; /* Number of options */
8808 cups_option_t *options; /* Options */
8809 ipp_t *ticket; /* New attributes */
8810 ipp_attribute_t *attr, /* Current attribute */
8811 *attr2, /* Job attribute */
8812 *prev2; /* Previous job attribute */
8813
8814
8815 /*
8816 * First open the print file...
8817 */
8818
8819 if ((fp = cupsFileOpen(con->filename, "rb")) == NULL)
8820 {
8821 cupsdLogMessage(CUPSD_LOG_ERROR,
8822 "Unable to open print file for job ticket - %s",
8823 strerror(errno));
8824 return;
8825 }
8826
8827 /*
8828 * Skip the first line...
8829 */
8830
8831 if (cupsFileGets(fp, line, sizeof(line)) == NULL)
8832 {
8833 cupsdLogMessage(CUPSD_LOG_ERROR,
8834 "Unable to read from print file for job ticket - %s",
8835 strerror(errno));
8836 cupsFileClose(fp);
8837 return;
8838 }
8839
8840 if (strncmp(line, "%!PS-Adobe-", 11) && strncmp(line, "%PDF-", 5))
8841 {
8842 /*
8843 * Not a DSC-compliant file, so no job ticket info will be available...
8844 */
8845
8846 cupsFileClose(fp);
8847 return;
8848 }
8849
8850 /*
8851 * Read job ticket info from the file...
8852 */
8853
8854 num_options = 0;
8855 options = NULL;
8856
8857 while (cupsFileGets(fp, line, sizeof(line)))
8858 {
8859 /*
8860 * Stop at the first non-ticket line...
8861 */
8862
8863 if (strncmp(line, "%cupsJobTicket:", 15))
8864 break;
8865
8866 /*
8867 * Add the options to the option array...
8868 */
8869
8870 num_options = cupsParseOptions(line + 15, num_options, &options);
8871 }
8872
8873 /*
8874 * Done with the file; see if we have any options...
8875 */
8876
8877 cupsFileClose(fp);
8878
8879 if (num_options == 0)
8880 return;
8881
8882 /*
8883 * OK, convert the options to an attribute list, and apply them to
8884 * the request...
8885 */
8886
8887 ticket = ippNew();
8888 cupsEncodeOptions(ticket, num_options, options);
8889
8890 /*
8891 * See what the user wants to change.
8892 */
8893
8894 for (attr = ticket->attrs; attr; attr = attr->next)
8895 {
8896 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
8897 continue;
8898
8899 if (!strcmp(attr->name, "job-originating-host-name") ||
8900 !strcmp(attr->name, "job-originating-user-name") ||
8901 !strcmp(attr->name, "job-media-sheets-completed") ||
8902 !strcmp(attr->name, "job-k-octets") ||
8903 !strcmp(attr->name, "job-id") ||
8904 !strncmp(attr->name, "job-state", 9) ||
8905 !strncmp(attr->name, "time-at-", 8))
8906 continue; /* Read-only attrs */
8907
8908 if ((attr2 = ippFindAttribute(con->request, attr->name,
8909 IPP_TAG_ZERO)) != NULL)
8910 {
8911 /*
8912 * Some other value; first free the old value...
8913 */
8914
8915 if (con->request->attrs == attr2)
8916 {
8917 con->request->attrs = attr2->next;
8918 prev2 = NULL;
8919 }
8920 else
8921 {
8922 for (prev2 = con->request->attrs; prev2; prev2 = prev2->next)
8923 if (prev2->next == attr2)
8924 {
8925 prev2->next = attr2->next;
8926 break;
8927 }
8928 }
8929
8930 if (con->request->last == attr2)
8931 con->request->last = prev2;
8932
8933 _ippFreeAttr(attr2);
8934 }
8935
8936 /*
8937 * Add new option by copying it...
8938 */
8939
8940 copy_attribute(con->request, attr, 0);
8941 }
8942
8943 /*
8944 * Then free the attribute list and option array...
8945 */
8946
8947 ippDelete(ticket);
8948 cupsFreeOptions(num_options, options);
8949 }
8950
8951
8952 /*
8953 * 'reject_jobs()' - Reject print jobs to a printer.
8954 */
8955
8956 static void
8957 reject_jobs(cupsd_client_t *con, /* I - Client connection */
8958 ipp_attribute_t *uri) /* I - Printer or class URI */
8959 {
8960 http_status_t status; /* Policy status */
8961 cups_ptype_t dtype; /* Destination type (printer/class) */
8962 cupsd_printer_t *printer; /* Printer data */
8963 ipp_attribute_t *attr; /* printer-state-message text */
8964
8965
8966 cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con,
8967 con->http.fd, uri->values[0].string.text);
8968
8969 /*
8970 * Is the destination valid?
8971 */
8972
8973 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8974 {
8975 /*
8976 * Bad URI...
8977 */
8978
8979 send_ipp_status(con, IPP_NOT_FOUND,
8980 _("The printer or class was not found."));
8981 return;
8982 }
8983
8984 /*
8985 * Check policy...
8986 */
8987
8988 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8989 {
8990 send_http_error(con, status, printer);
8991 return;
8992 }
8993
8994 /*
8995 * Reject jobs sent to the printer...
8996 */
8997
8998 printer->accepting = 0;
8999
9000 if ((attr = ippFindAttribute(con->request, "printer-state-message",
9001 IPP_TAG_TEXT)) == NULL)
9002 strcpy(printer->state_message, "Rejecting Jobs");
9003 else
9004 strlcpy(printer->state_message, attr->values[0].string.text,
9005 sizeof(printer->state_message));
9006
9007 cupsdAddPrinterHistory(printer);
9008
9009 if (dtype & CUPS_PRINTER_CLASS)
9010 {
9011 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
9012
9013 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").",
9014 printer->name, get_username(con));
9015 }
9016 else
9017 {
9018 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
9019
9020 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").",
9021 printer->name, get_username(con));
9022 }
9023
9024 /*
9025 * Everything was ok, so return OK status...
9026 */
9027
9028 con->response->request.status.status_code = IPP_OK;
9029 }
9030
9031
9032 /*
9033 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
9034 */
9035
9036 static void
9037 release_held_new_jobs(
9038 cupsd_client_t *con, /* I - Connection */
9039 ipp_attribute_t *uri) /* I - Printer URI */
9040 {
9041 http_status_t status; /* Policy status */
9042 cups_ptype_t dtype; /* Destination type (printer/class) */
9043 cupsd_printer_t *printer; /* Printer data */
9044
9045
9046 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_held_new_jobs(%p[%d], %s)", con,
9047 con->http.fd, uri->values[0].string.text);
9048
9049 /*
9050 * Is the destination valid?
9051 */
9052
9053 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
9054 {
9055 /*
9056 * Bad URI...
9057 */
9058
9059 send_ipp_status(con, IPP_NOT_FOUND,
9060 _("The printer or class was not found."));
9061 return;
9062 }
9063
9064 /*
9065 * Check policy...
9066 */
9067
9068 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
9069 {
9070 send_http_error(con, status, printer);
9071 return;
9072 }
9073
9074 /*
9075 * Hold pending/new jobs sent to the printer...
9076 */
9077
9078 printer->holding_new_jobs = 0;
9079
9080 cupsdSetPrinterReasons(printer, "-hold-new-jobs");
9081 cupsdAddPrinterHistory(printer);
9082
9083 if (dtype & CUPS_PRINTER_CLASS)
9084 cupsdLogMessage(CUPSD_LOG_INFO,
9085 "Class \"%s\" now printing pending/new jobs (\"%s\").",
9086 printer->name, get_username(con));
9087 else
9088 cupsdLogMessage(CUPSD_LOG_INFO,
9089 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
9090 printer->name, get_username(con));
9091
9092 /*
9093 * Everything was ok, so return OK status...
9094 */
9095
9096 con->response->request.status.status_code = IPP_OK;
9097 }
9098
9099
9100 /*
9101 * 'release_job()' - Release a held print job.
9102 */
9103
9104 static void
9105 release_job(cupsd_client_t *con, /* I - Client connection */
9106 ipp_attribute_t *uri) /* I - Job or Printer URI */
9107 {
9108 ipp_attribute_t *attr; /* Current attribute */
9109 int jobid; /* Job ID */
9110 char scheme[HTTP_MAX_URI], /* Method portion of URI */
9111 username[HTTP_MAX_URI], /* Username portion of URI */
9112 host[HTTP_MAX_URI], /* Host portion of URI */
9113 resource[HTTP_MAX_URI]; /* Resource portion of URI */
9114 int port; /* Port portion of URI */
9115 cupsd_job_t *job; /* Job information */
9116
9117
9118 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con,
9119 con->http.fd, uri->values[0].string.text);
9120
9121 /*
9122 * See if we have a job URI or a printer URI...
9123 */
9124
9125 if (!strcmp(uri->name, "printer-uri"))
9126 {
9127 /*
9128 * Got a printer URI; see if we also have a job-id attribute...
9129 */
9130
9131 if ((attr = ippFindAttribute(con->request, "job-id",
9132 IPP_TAG_INTEGER)) == NULL)
9133 {
9134 send_ipp_status(con, IPP_BAD_REQUEST,
9135 _("Got a printer-uri attribute but no job-id!"));
9136 return;
9137 }
9138
9139 jobid = attr->values[0].integer;
9140 }
9141 else
9142 {
9143 /*
9144 * Got a job URI; parse it to get the job ID...
9145 */
9146
9147 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9148 sizeof(scheme), username, sizeof(username), host,
9149 sizeof(host), &port, resource, sizeof(resource));
9150
9151 if (strncmp(resource, "/jobs/", 6))
9152 {
9153 /*
9154 * Not a valid URI!
9155 */
9156
9157 send_ipp_status(con, IPP_BAD_REQUEST,
9158 _("Bad job-uri attribute \"%s\"!"),
9159 uri->values[0].string.text);
9160 return;
9161 }
9162
9163 jobid = atoi(resource + 6);
9164 }
9165
9166 /*
9167 * See if the job exists...
9168 */
9169
9170 if ((job = cupsdFindJob(jobid)) == NULL)
9171 {
9172 /*
9173 * Nope - return a "not found" error...
9174 */
9175
9176 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
9177 return;
9178 }
9179
9180 /*
9181 * See if job is "held"...
9182 */
9183
9184 if (job->state_value != IPP_JOB_HELD)
9185 {
9186 /*
9187 * Nope - return a "not possible" error...
9188 */
9189
9190 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held!"), jobid);
9191 return;
9192 }
9193
9194 /*
9195 * See if the job is owned by the requesting user...
9196 */
9197
9198 if (!validate_user(job, con, job->username, username, sizeof(username)))
9199 {
9200 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
9201 return;
9202 }
9203
9204 /*
9205 * Reset the job-hold-until value to "no-hold"...
9206 */
9207
9208 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9209 IPP_TAG_KEYWORD)) == NULL)
9210 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9211
9212 if (attr)
9213 {
9214 _cupsStrFree(attr->values[0].string.text);
9215
9216 attr->value_tag = IPP_TAG_KEYWORD;
9217 attr->values[0].string.text = _cupsStrAlloc("no-hold");
9218
9219 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
9220 "Job job-hold-until value changed by user.");
9221 }
9222
9223 /*
9224 * Release the job and return...
9225 */
9226
9227 cupsdReleaseJob(job);
9228
9229 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
9230 "Job released by user.");
9231
9232 cupsdLogJob(job, CUPSD_LOG_INFO, "Released by \"%s\".", username);
9233
9234 con->response->request.status.status_code = IPP_OK;
9235
9236 cupsdCheckJobs();
9237 }
9238
9239
9240 /*
9241 * 'renew_subscription()' - Renew an existing subscription...
9242 */
9243
9244 static void
9245 renew_subscription(
9246 cupsd_client_t *con, /* I - Client connection */
9247 int sub_id) /* I - Subscription ID */
9248 {
9249 http_status_t status; /* Policy status */
9250 cupsd_subscription_t *sub; /* Subscription */
9251 ipp_attribute_t *lease; /* notify-lease-duration */
9252
9253
9254 cupsdLogMessage(CUPSD_LOG_DEBUG2,
9255 "renew_subscription(con=%p[%d], sub_id=%d)",
9256 con, con->http.fd, sub_id);
9257
9258 /*
9259 * Is the subscription ID valid?
9260 */
9261
9262 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
9263 {
9264 /*
9265 * Bad subscription ID...
9266 */
9267
9268 send_ipp_status(con, IPP_NOT_FOUND,
9269 _("notify-subscription-id %d no good!"), sub_id);
9270 return;
9271 }
9272
9273 if (sub->job)
9274 {
9275 /*
9276 * Job subscriptions cannot be renewed...
9277 */
9278
9279 send_ipp_status(con, IPP_NOT_POSSIBLE,
9280 _("Job subscriptions cannot be renewed!"));
9281 return;
9282 }
9283
9284 /*
9285 * Check policy...
9286 */
9287
9288 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
9289 DefaultPolicyPtr,
9290 con, sub->owner)) != HTTP_OK)
9291 {
9292 send_http_error(con, status, sub->dest);
9293 return;
9294 }
9295
9296 /*
9297 * Renew the subscription...
9298 */
9299
9300 lease = ippFindAttribute(con->request, "notify-lease-duration",
9301 IPP_TAG_INTEGER);
9302
9303 sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration;
9304
9305 if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration))
9306 {
9307 cupsdLogMessage(CUPSD_LOG_INFO,
9308 "renew_subscription: Limiting notify-lease-duration to "
9309 "%d seconds.",
9310 MaxLeaseDuration);
9311 sub->lease = MaxLeaseDuration;
9312 }
9313
9314 sub->expire = sub->lease ? time(NULL) + sub->lease : 0;
9315
9316 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
9317
9318 con->response->request.status.status_code = IPP_OK;
9319
9320 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
9321 "notify-lease-duration", sub->lease);
9322 }
9323
9324
9325 /*
9326 * 'restart_job()' - Restart an old print job.
9327 */
9328
9329 static void
9330 restart_job(cupsd_client_t *con, /* I - Client connection */
9331 ipp_attribute_t *uri) /* I - Job or Printer URI */
9332 {
9333 ipp_attribute_t *attr; /* Current attribute */
9334 int jobid; /* Job ID */
9335 char scheme[HTTP_MAX_URI], /* Method portion of URI */
9336 username[HTTP_MAX_URI], /* Username portion of URI */
9337 host[HTTP_MAX_URI], /* Host portion of URI */
9338 resource[HTTP_MAX_URI]; /* Resource portion of URI */
9339 int port; /* Port portion of URI */
9340 cupsd_job_t *job; /* Job information */
9341
9342
9343 cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con,
9344 con->http.fd, uri->values[0].string.text);
9345
9346 /*
9347 * See if we have a job URI or a printer URI...
9348 */
9349
9350 if (!strcmp(uri->name, "printer-uri"))
9351 {
9352 /*
9353 * Got a printer URI; see if we also have a job-id attribute...
9354 */
9355
9356 if ((attr = ippFindAttribute(con->request, "job-id",
9357 IPP_TAG_INTEGER)) == NULL)
9358 {
9359 send_ipp_status(con, IPP_BAD_REQUEST,
9360 _("Got a printer-uri attribute but no job-id!"));
9361 return;
9362 }
9363
9364 jobid = attr->values[0].integer;
9365 }
9366 else
9367 {
9368 /*
9369 * Got a job URI; parse it to get the job ID...
9370 */
9371
9372 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9373 sizeof(scheme), username, sizeof(username), host,
9374 sizeof(host), &port, resource, sizeof(resource));
9375
9376 if (strncmp(resource, "/jobs/", 6))
9377 {
9378 /*
9379 * Not a valid URI!
9380 */
9381
9382 send_ipp_status(con, IPP_BAD_REQUEST,
9383 _("Bad job-uri attribute \"%s\"!"),
9384 uri->values[0].string.text);
9385 return;
9386 }
9387
9388 jobid = atoi(resource + 6);
9389 }
9390
9391 /*
9392 * See if the job exists...
9393 */
9394
9395 if ((job = cupsdFindJob(jobid)) == NULL)
9396 {
9397 /*
9398 * Nope - return a "not found" error...
9399 */
9400
9401 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
9402 return;
9403 }
9404
9405 /*
9406 * See if job is in any of the "completed" states...
9407 */
9408
9409 if (job->state_value <= IPP_JOB_PROCESSING)
9410 {
9411 /*
9412 * Nope - return a "not possible" error...
9413 */
9414
9415 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete!"),
9416 jobid);
9417 return;
9418 }
9419
9420 /*
9421 * See if we have retained the job files...
9422 */
9423
9424 cupsdLoadJob(job);
9425
9426 if (!job->attrs || job->num_files == 0)
9427 {
9428 /*
9429 * Nope - return a "not possible" error...
9430 */
9431
9432 send_ipp_status(con, IPP_NOT_POSSIBLE,
9433 _("Job #%d cannot be restarted - no files!"), jobid);
9434 return;
9435 }
9436
9437 /*
9438 * See if the job is owned by the requesting user...
9439 */
9440
9441 if (!validate_user(job, con, job->username, username, sizeof(username)))
9442 {
9443 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
9444 return;
9445 }
9446
9447 /*
9448 * Restart the job and return...
9449 */
9450
9451 cupsdRestartJob(job);
9452
9453 cupsdLogJob(job, CUPSD_LOG_INFO, "Restarted by \"%s\".", username);
9454
9455 con->response->request.status.status_code = IPP_OK;
9456 }
9457
9458
9459 /*
9460 * 'save_auth_info()' - Save authentication information for a job.
9461 */
9462
9463 static void
9464 save_auth_info(
9465 cupsd_client_t *con, /* I - Client connection */
9466 cupsd_job_t *job, /* I - Job */
9467 ipp_attribute_t *auth_info) /* I - auth-info attribute, if any */
9468 {
9469 int i; /* Looping var */
9470 char filename[1024]; /* Job authentication filename */
9471 cups_file_t *fp; /* Job authentication file */
9472 char line[2048]; /* Line for file */
9473 cupsd_printer_t *dest; /* Destination printer/class */
9474
9475
9476 /*
9477 * This function saves the in-memory authentication information for
9478 * a job so that it can be used to authenticate with a remote host.
9479 * The information is stored in a file that is readable only by the
9480 * root user. The fields are Base-64 encoded, each on a separate line,
9481 * followed by random number (up to 1024) of newlines to limit the
9482 * amount of information that is exposed.
9483 *
9484 * Because of the potential for exposing of authentication information,
9485 * this functionality is only enabled when running cupsd as root.
9486 *
9487 * This caching only works for the Basic and BasicDigest authentication
9488 * types. Digest authentication cannot be cached this way, and in
9489 * the future Kerberos authentication may make all of this obsolete.
9490 *
9491 * Authentication information is saved whenever an authenticated
9492 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9493 * performed.
9494 *
9495 * This information is deleted after a job is completed or canceled,
9496 * so reprints may require subsequent re-authentication.
9497 */
9498
9499 if (RunUser)
9500 return;
9501
9502 if ((dest = cupsdFindDest(job->dest)) == NULL)
9503 return;
9504
9505 /*
9506 * Create the authentication file and change permissions...
9507 */
9508
9509 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
9510 if ((fp = cupsFileOpen(filename, "w")) == NULL)
9511 {
9512 cupsdLogMessage(CUPSD_LOG_ERROR,
9513 "Unable to save authentication info to \"%s\" - %s",
9514 filename, strerror(errno));
9515 return;
9516 }
9517
9518 fchown(cupsFileNumber(fp), 0, 0);
9519 fchmod(cupsFileNumber(fp), 0400);
9520
9521 if (auth_info && auth_info->num_values == dest->num_auth_info_required)
9522 {
9523 /*
9524 * Write 1 to 3 auth values...
9525 */
9526
9527 cupsdClearString(&job->auth_username);
9528 cupsdClearString(&job->auth_domain);
9529 cupsdClearString(&job->auth_password);
9530
9531 for (i = 0; i < auth_info->num_values; i ++)
9532 {
9533 httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text,
9534 strlen(auth_info->values[i].string.text));
9535 cupsFilePrintf(fp, "%s\n", line);
9536
9537 if (!strcmp(dest->auth_info_required[i], "username"))
9538 cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s",
9539 auth_info->values[i].string.text);
9540 else if (!strcmp(dest->auth_info_required[i], "domain"))
9541 cupsdSetStringf(&job->auth_domain, "AUTH_DOMAIN=%s",
9542 auth_info->values[i].string.text);
9543 else if (!strcmp(dest->auth_info_required[i], "password"))
9544 cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s",
9545 auth_info->values[i].string.text);
9546 }
9547 }
9548 else if (con->username[0])
9549 {
9550 /*
9551 * Write the authenticated username...
9552 */
9553
9554 httpEncode64_2(line, sizeof(line), con->username, strlen(con->username));
9555 cupsFilePrintf(fp, "%s\n", line);
9556
9557 cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", con->username);
9558 cupsdClearString(&job->auth_domain);
9559
9560 /*
9561 * Write the authenticated password...
9562 */
9563
9564 httpEncode64_2(line, sizeof(line), con->password, strlen(con->password));
9565 cupsFilePrintf(fp, "%s\n", line);
9566
9567 cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", con->password);
9568 }
9569
9570 /*
9571 * Write a random number of newlines to the end of the file...
9572 */
9573
9574 for (i = (rand() % 1024); i >= 0; i --)
9575 cupsFilePutChar(fp, '\n');
9576
9577 /*
9578 * Close the file and return...
9579 */
9580
9581 cupsFileClose(fp);
9582
9583 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9584 if (con->gss_have_creds)
9585 save_krb5_creds(con, job);
9586 else if (job->ccname)
9587 cupsdClearString(&(job->ccname));
9588 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9589 }
9590
9591
9592 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9593 /*
9594 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
9595 */
9596
9597 static void
9598 save_krb5_creds(cupsd_client_t *con, /* I - Client connection */
9599 cupsd_job_t *job) /* I - Job */
9600 {
9601 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
9602 cupsdLogMessage(CUPSD_LOG_INFO,
9603 "Sorry, your version of Kerberos does not support delegated "
9604 "credentials!");
9605 return;
9606
9607 # else
9608 krb5_error_code error; /* Kerberos error code */
9609 OM_uint32 major_status, /* Major status code */
9610 minor_status; /* Minor status code */
9611 krb5_principal principal; /* Kerberos principal */
9612
9613
9614 # ifdef __APPLE__
9615 /*
9616 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
9617 * to use it...
9618 */
9619
9620 if (krb5_init_context == NULL)
9621 return;
9622 # endif /* __APPLE__ */
9623
9624 if (!KerberosInitialized)
9625 {
9626 /*
9627 * Setup a Kerberos context for the scheduler to use...
9628 */
9629
9630 KerberosInitialized = 1;
9631
9632 if (krb5_init_context(&KerberosContext))
9633 {
9634 KerberosContext = NULL;
9635
9636 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to initialize Kerberos context");
9637 return;
9638 }
9639 }
9640
9641 /*
9642 * We MUST create a file-based cache because memory-based caches are
9643 * only valid for the current process/address space.
9644 *
9645 * Due to various bugs/features in different versions of Kerberos, we
9646 * need either the krb5_cc_new_unique() function or Heimdal's version
9647 * of krb5_cc_gen_new() to create a new FILE: credential cache that
9648 * can be passed to the backend. These functions create a temporary
9649 * file (typically in /tmp) containing the cached credentials, which
9650 * are removed when we have successfully printed a job.
9651 */
9652
9653 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
9654 if ((error = krb5_cc_new_unique(KerberosContext, "FILE", NULL,
9655 &(job->ccache))) != 0)
9656 # else /* HAVE_HEIMDAL */
9657 if ((error = krb5_cc_gen_new(KerberosContext, &krb5_fcc_ops,
9658 &(job->ccache))) != 0)
9659 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
9660 {
9661 cupsdLogMessage(CUPSD_LOG_ERROR,
9662 "Unable to create new credentials cache (%d/%s)",
9663 error, strerror(errno));
9664 job->ccache = NULL;
9665 return;
9666 }
9667
9668 if ((error = krb5_parse_name(KerberosContext, con->username, &principal)) != 0)
9669 {
9670 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to parse kerberos username (%d/%s)",
9671 error, strerror(errno));
9672 krb5_cc_destroy(KerberosContext, job->ccache);
9673 job->ccache = NULL;
9674 return;
9675 }
9676
9677 if ((error = krb5_cc_initialize(KerberosContext, job->ccache, principal)))
9678 {
9679 cupsdLogMessage(CUPSD_LOG_ERROR,
9680 "Unable to initialize credentials cache (%d/%s)", error,
9681 strerror(errno));
9682 krb5_cc_destroy(KerberosContext, job->ccache);
9683 krb5_free_principal(KerberosContext, principal);
9684 job->ccache = NULL;
9685 return;
9686 }
9687
9688 krb5_free_principal(KerberosContext, principal);
9689
9690 /*
9691 * Copy the user's credentials to the new cache file...
9692 */
9693
9694 major_status = gss_krb5_copy_ccache(&minor_status, con->gss_delegated_cred,
9695 job->ccache);
9696
9697 if (GSS_ERROR(major_status))
9698 {
9699 cupsdLogGSSMessage(CUPSD_LOG_ERROR, major_status, minor_status,
9700 "Unable to import client credentials cache");
9701 krb5_cc_destroy(KerberosContext, job->ccache);
9702 job->ccache = NULL;
9703 return;
9704 }
9705
9706 /*
9707 * Add the KRB5CCNAME environment variable to the job so that the
9708 * backend can use the credentials when printing.
9709 */
9710
9711 cupsdSetStringf(&(job->ccname), "KRB5CCNAME=FILE:%s",
9712 krb5_cc_get_name(KerberosContext, job->ccache));
9713
9714 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "save_krb5_creds: %s", job->ccname);
9715 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
9716 }
9717 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9718
9719
9720 /*
9721 * 'send_document()' - Send a file to a printer or class.
9722 */
9723
9724 static void
9725 send_document(cupsd_client_t *con, /* I - Client connection */
9726 ipp_attribute_t *uri) /* I - Printer URI */
9727 {
9728 ipp_attribute_t *attr; /* Current attribute */
9729 ipp_attribute_t *format; /* Request's document-format attribute */
9730 ipp_attribute_t *jformat; /* Job's document-format attribute */
9731 const char *default_format;/* document-format-default value */
9732 int jobid; /* Job ID number */
9733 cupsd_job_t *job; /* Current job */
9734 char job_uri[HTTP_MAX_URI],
9735 /* Job URI */
9736 scheme[HTTP_MAX_URI],
9737 /* Method portion of URI */
9738 username[HTTP_MAX_URI],
9739 /* Username portion of URI */
9740 host[HTTP_MAX_URI],
9741 /* Host portion of URI */
9742 resource[HTTP_MAX_URI];
9743 /* Resource portion of URI */
9744 int port; /* Port portion of URI */
9745 mime_type_t *filetype; /* Type of file */
9746 char super[MIME_MAX_SUPER],
9747 /* Supertype of file */
9748 type[MIME_MAX_TYPE],
9749 /* Subtype of file */
9750 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
9751 /* Textual name of mime type */
9752 char filename[1024]; /* Job filename */
9753 cupsd_printer_t *printer; /* Current printer */
9754 struct stat fileinfo; /* File information */
9755 int kbytes; /* Size of file */
9756 int compression; /* Type of compression */
9757 int start_job; /* Start the job? */
9758
9759
9760 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con,
9761 con->http.fd, uri->values[0].string.text);
9762
9763 /*
9764 * See if we have a job URI or a printer URI...
9765 */
9766
9767 if (!strcmp(uri->name, "printer-uri"))
9768 {
9769 /*
9770 * Got a printer URI; see if we also have a job-id attribute...
9771 */
9772
9773 if ((attr = ippFindAttribute(con->request, "job-id",
9774 IPP_TAG_INTEGER)) == NULL)
9775 {
9776 send_ipp_status(con, IPP_BAD_REQUEST,
9777 _("Got a printer-uri attribute but no job-id!"));
9778 return;
9779 }
9780
9781 jobid = attr->values[0].integer;
9782 }
9783 else
9784 {
9785 /*
9786 * Got a job URI; parse it to get the job ID...
9787 */
9788
9789 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9790 sizeof(scheme), username, sizeof(username), host,
9791 sizeof(host), &port, resource, sizeof(resource));
9792
9793 if (strncmp(resource, "/jobs/", 6))
9794 {
9795 /*
9796 * Not a valid URI!
9797 */
9798
9799 send_ipp_status(con, IPP_BAD_REQUEST,
9800 _("Bad job-uri attribute \"%s\"!"),
9801 uri->values[0].string.text);
9802 return;
9803 }
9804
9805 jobid = atoi(resource + 6);
9806 }
9807
9808 /*
9809 * See if the job exists...
9810 */
9811
9812 if ((job = cupsdFindJob(jobid)) == NULL)
9813 {
9814 /*
9815 * Nope - return a "not found" error...
9816 */
9817
9818 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
9819 return;
9820 }
9821
9822 printer = cupsdFindDest(job->dest);
9823
9824 /*
9825 * See if the job is owned by the requesting user...
9826 */
9827
9828 if (!validate_user(job, con, job->username, username, sizeof(username)))
9829 {
9830 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
9831 return;
9832 }
9833
9834 /*
9835 * OK, see if the client is sending the document compressed - CUPS
9836 * only supports "none" and "gzip".
9837 */
9838
9839 compression = CUPS_FILE_NONE;
9840
9841 if ((attr = ippFindAttribute(con->request, "compression",
9842 IPP_TAG_KEYWORD)) != NULL)
9843 {
9844 if (strcmp(attr->values[0].string.text, "none")
9845 #ifdef HAVE_LIBZ
9846 && strcmp(attr->values[0].string.text, "gzip")
9847 #endif /* HAVE_LIBZ */
9848 )
9849 {
9850 send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"!"),
9851 attr->values[0].string.text);
9852 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
9853 "compression", NULL, attr->values[0].string.text);
9854 return;
9855 }
9856
9857 #ifdef HAVE_LIBZ
9858 if (!strcmp(attr->values[0].string.text, "gzip"))
9859 compression = CUPS_FILE_GZIP;
9860 #endif /* HAVE_LIBZ */
9861 }
9862
9863 /*
9864 * Do we have a file to print?
9865 */
9866
9867 if (!con->filename)
9868 {
9869 send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!"));
9870 return;
9871 }
9872
9873 /*
9874 * Is it a format we support?
9875 */
9876
9877 if ((format = ippFindAttribute(con->request, "document-format",
9878 IPP_TAG_MIMETYPE)) != NULL)
9879 {
9880 /*
9881 * Grab format from client...
9882 */
9883
9884 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
9885 super, type) != 2)
9886 {
9887 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"),
9888 format->values[0].string.text);
9889 return;
9890 }
9891 }
9892 else if ((default_format = cupsGetOption("document-format",
9893 printer->num_options,
9894 printer->options)) != NULL)
9895 {
9896 /*
9897 * Use default document format...
9898 */
9899
9900 if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
9901 {
9902 send_ipp_status(con, IPP_BAD_REQUEST,
9903 _("Could not scan type \"%s\"!"),
9904 default_format);
9905 return;
9906 }
9907 }
9908 else
9909 {
9910 /*
9911 * No document format attribute? Auto-type it!
9912 */
9913
9914 strcpy(super, "application");
9915 strcpy(type, "octet-stream");
9916 }
9917
9918 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
9919 {
9920 /*
9921 * Auto-type the file...
9922 */
9923
9924 ipp_attribute_t *doc_name; /* document-name attribute */
9925
9926
9927 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Auto-typing file...");
9928
9929 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
9930 filetype = mimeFileType(MimeDatabase, con->filename,
9931 doc_name ? doc_name->values[0].string.text : NULL,
9932 &compression);
9933
9934 if (!filetype)
9935 filetype = mimeType(MimeDatabase, super, type);
9936
9937 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Request file type is %s/%s.",
9938 filetype->super, filetype->type);
9939 }
9940 else
9941 filetype = mimeType(MimeDatabase, super, type);
9942
9943 if (filetype)
9944 {
9945 /*
9946 * Replace the document-format attribute value with the auto-typed or
9947 * default one.
9948 */
9949
9950 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9951 filetype->type);
9952
9953 if ((jformat = ippFindAttribute(job->attrs, "document-format",
9954 IPP_TAG_MIMETYPE)) != NULL)
9955 {
9956 _cupsStrFree(jformat->values[0].string.text);
9957
9958 jformat->values[0].string.text = _cupsStrAlloc(mimetype);
9959 }
9960 else
9961 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
9962 "document-format", NULL, mimetype);
9963 }
9964 else if (!filetype)
9965 {
9966 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9967 _("Unsupported format \'%s/%s\'!"), super, type);
9968 cupsdLogMessage(CUPSD_LOG_INFO,
9969 "Hint: Do you have the raw file printing rules enabled?");
9970
9971 if (format)
9972 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9973 "document-format", NULL, format->values[0].string.text);
9974
9975 return;
9976 }
9977
9978 if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype))
9979 {
9980 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9981 filetype->type);
9982
9983 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9984 _("Unsupported format \'%s\'!"), mimetype);
9985
9986 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9987 "document-format", NULL, mimetype);
9988
9989 return;
9990 }
9991
9992 /*
9993 * Add the file to the job...
9994 */
9995
9996 cupsdLoadJob(job);
9997
9998 if (add_file(con, job, filetype, compression))
9999 return;
10000
10001 if (stat(con->filename, &fileinfo))
10002 kbytes = 0;
10003 else
10004 kbytes = (fileinfo.st_size + 1023) / 1024;
10005
10006 cupsdUpdateQuota(printer, job->username, 0, kbytes);
10007
10008 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
10009 IPP_TAG_INTEGER)) != NULL)
10010 attr->values[0].integer += kbytes;
10011
10012 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
10013 job->num_files);
10014 rename(con->filename, filename);
10015
10016 cupsdClearString(&con->filename);
10017
10018 cupsdLogJob(job, CUPSD_LOG_INFO, "File of type %s/%s queued by \"%s\".",
10019 filetype->super, filetype->type, job->username);
10020
10021 /*
10022 * Start the job if this is the last document...
10023 */
10024
10025 if ((attr = ippFindAttribute(con->request, "last-document",
10026 IPP_TAG_BOOLEAN)) != NULL &&
10027 attr->values[0].boolean)
10028 {
10029 /*
10030 * See if we need to add the ending sheet...
10031 */
10032
10033 if (cupsdTimeoutJob(job))
10034 return;
10035
10036 if (job->state_value == IPP_JOB_STOPPED)
10037 {
10038 job->state->values[0].integer = IPP_JOB_PENDING;
10039 job->state_value = IPP_JOB_PENDING;
10040 }
10041 else if (job->state_value == IPP_JOB_HELD)
10042 {
10043 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
10044 IPP_TAG_KEYWORD)) == NULL)
10045 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
10046
10047 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
10048 {
10049 job->state->values[0].integer = IPP_JOB_PENDING;
10050 job->state_value = IPP_JOB_PENDING;
10051 }
10052 }
10053
10054 job->dirty = 1;
10055 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
10056
10057 start_job = 1;
10058 }
10059 else
10060 {
10061 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
10062 IPP_TAG_KEYWORD)) == NULL)
10063 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
10064
10065 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
10066 {
10067 job->state->values[0].integer = IPP_JOB_HELD;
10068 job->state_value = IPP_JOB_HELD;
10069 job->hold_until = time(NULL) + MultipleOperationTimeout;
10070 job->dirty = 1;
10071
10072 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
10073 }
10074
10075 start_job = 0;
10076 }
10077
10078 /*
10079 * Fill in the response info...
10080 */
10081
10082 snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName,
10083 LocalPort, jobid);
10084
10085 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
10086 job_uri);
10087
10088 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid);
10089
10090 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
10091 job->state_value);
10092 add_job_state_reasons(con, job);
10093
10094 con->response->request.status.status_code = IPP_OK;
10095
10096 /*
10097 * Start the job if necessary...
10098 */
10099
10100 if (start_job)
10101 cupsdCheckJobs();
10102 }
10103
10104
10105 /*
10106 * 'send_http_error()' - Send a HTTP error back to the IPP client.
10107 */
10108
10109 static void
10110 send_http_error(
10111 cupsd_client_t *con, /* I - Client connection */
10112 http_status_t status, /* I - HTTP status code */
10113 cupsd_printer_t *printer) /* I - Printer, if any */
10114 {
10115 ipp_attribute_t *uri; /* Request URI, if any */
10116
10117
10118 if ((uri = ippFindAttribute(con->request, "printer-uri",
10119 IPP_TAG_URI)) == NULL)
10120 uri = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI);
10121
10122 cupsdLogMessage(status == HTTP_FORBIDDEN ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
10123 "Returning HTTP %s for %s (%s) from %s",
10124 httpStatus(status),
10125 ippOpString(con->request->request.op.operation_id),
10126 uri ? uri->values[0].string.text : "no URI",
10127 con->http.hostname);
10128
10129 if (printer)
10130 {
10131 int auth_type; /* Type of authentication required */
10132
10133
10134 auth_type = CUPSD_AUTH_NONE;
10135
10136 if (status == HTTP_UNAUTHORIZED &&
10137 printer->num_auth_info_required > 0 &&
10138 !strcmp(printer->auth_info_required[0], "negotiate") &&
10139 con->request &&
10140 (con->request->request.op.operation_id == IPP_PRINT_JOB ||
10141 con->request->request.op.operation_id == IPP_CREATE_JOB ||
10142 con->request->request.op.operation_id == CUPS_AUTHENTICATE_JOB))
10143 {
10144 /*
10145 * Creating and authenticating jobs requires Kerberos...
10146 */
10147
10148 auth_type = CUPSD_AUTH_NEGOTIATE;
10149 }
10150 else
10151 {
10152 /*
10153 * Use policy/location-defined authentication requirements...
10154 */
10155
10156 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
10157 cupsd_location_t *auth; /* Pointer to authentication element */
10158
10159
10160 if (printer->type & CUPS_PRINTER_CLASS)
10161 snprintf(resource, sizeof(resource), "/classes/%s", printer->name);
10162 else
10163 snprintf(resource, sizeof(resource), "/printers/%s", printer->name);
10164
10165 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
10166 auth->type == CUPSD_AUTH_NONE)
10167 auth = cupsdFindPolicyOp(printer->op_policy_ptr,
10168 con->request ?
10169 con->request->request.op.operation_id :
10170 IPP_PRINT_JOB);
10171
10172 if (auth)
10173 {
10174 if (auth->type == CUPSD_AUTH_DEFAULT)
10175 auth_type = DefaultAuthType;
10176 else
10177 auth_type = auth->type;
10178 }
10179 }
10180
10181 cupsdSendError(con, status, auth_type);
10182 }
10183 else
10184 cupsdSendError(con, status, CUPSD_AUTH_NONE);
10185
10186 ippDelete(con->response);
10187 con->response = NULL;
10188
10189 return;
10190 }
10191
10192
10193 /*
10194 * 'send_ipp_status()' - Send a status back to the IPP client.
10195 */
10196
10197 static void
10198 send_ipp_status(cupsd_client_t *con, /* I - Client connection */
10199 ipp_status_t status, /* I - IPP status code */
10200 const char *message,/* I - Status message */
10201 ...) /* I - Additional args as needed */
10202 {
10203 va_list ap; /* Pointer to additional args */
10204 char formatted[1024]; /* Formatted errror message */
10205
10206
10207 va_start(ap, message);
10208 vsnprintf(formatted, sizeof(formatted),
10209 _cupsLangString(con->language, message), ap);
10210 va_end(ap);
10211
10212 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s",
10213 ippOpString(con->request->request.op.operation_id),
10214 ippErrorString(status), formatted);
10215
10216 con->response->request.status.status_code = status;
10217
10218 if (ippFindAttribute(con->response, "attributes-charset",
10219 IPP_TAG_ZERO) == NULL)
10220 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
10221 "attributes-charset", NULL, DefaultCharset);
10222
10223 if (ippFindAttribute(con->response, "attributes-natural-language",
10224 IPP_TAG_ZERO) == NULL)
10225 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
10226 "attributes-natural-language", NULL, DefaultLanguage);
10227
10228 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT,
10229 "status-message", NULL, formatted);
10230 }
10231
10232
10233 /*
10234 * 'set_default()' - Set the default destination...
10235 */
10236
10237 static void
10238 set_default(cupsd_client_t *con, /* I - Client connection */
10239 ipp_attribute_t *uri) /* I - Printer URI */
10240 {
10241 http_status_t status; /* Policy status */
10242 cups_ptype_t dtype; /* Destination type (printer/class) */
10243 cupsd_printer_t *printer, /* Printer */
10244 *oldprinter; /* Old default printer */
10245
10246
10247 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con,
10248 con->http.fd, uri->values[0].string.text);
10249
10250 /*
10251 * Is the destination valid?
10252 */
10253
10254 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10255 {
10256 /*
10257 * Bad URI...
10258 */
10259
10260 send_ipp_status(con, IPP_NOT_FOUND,
10261 _("The printer or class was not found."));
10262 return;
10263 }
10264
10265 /*
10266 * Check policy...
10267 */
10268
10269 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
10270 {
10271 send_http_error(con, status, NULL);
10272 return;
10273 }
10274
10275 /*
10276 * Set it as the default...
10277 */
10278
10279 oldprinter = DefaultPrinter;
10280 DefaultPrinter = printer;
10281
10282 if (oldprinter)
10283 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, oldprinter, NULL,
10284 "%s is no longer the default printer.", oldprinter->name);
10285
10286 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
10287 "%s is now the default printer.", printer->name);
10288
10289 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES |
10290 CUPSD_DIRTY_REMOTE | CUPSD_DIRTY_PRINTCAP);
10291
10292 cupsdLogMessage(CUPSD_LOG_INFO,
10293 "Default destination set to \"%s\" by \"%s\".",
10294 printer->name, get_username(con));
10295
10296 /*
10297 * Everything was ok, so return OK status...
10298 */
10299
10300 con->response->request.status.status_code = IPP_OK;
10301 }
10302
10303
10304 /*
10305 * 'set_job_attrs()' - Set job attributes.
10306 */
10307
10308 static void
10309 set_job_attrs(cupsd_client_t *con, /* I - Client connection */
10310 ipp_attribute_t *uri) /* I - Job URI */
10311 {
10312 ipp_attribute_t *attr, /* Current attribute */
10313 *attr2; /* Job attribute */
10314 int jobid; /* Job ID */
10315 cupsd_job_t *job; /* Current job */
10316 char scheme[HTTP_MAX_URI],
10317 /* Method portion of URI */
10318 username[HTTP_MAX_URI],
10319 /* Username portion of URI */
10320 host[HTTP_MAX_URI],
10321 /* Host portion of URI */
10322 resource[HTTP_MAX_URI];
10323 /* Resource portion of URI */
10324 int port; /* Port portion of URI */
10325 int event; /* Events? */
10326 int check_jobs; /* Check jobs? */
10327
10328
10329 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con,
10330 con->http.fd, uri->values[0].string.text);
10331
10332 /*
10333 * Start with "everything is OK" status...
10334 */
10335
10336 con->response->request.status.status_code = IPP_OK;
10337
10338 /*
10339 * See if we have a job URI or a printer URI...
10340 */
10341
10342 if (!strcmp(uri->name, "printer-uri"))
10343 {
10344 /*
10345 * Got a printer URI; see if we also have a job-id attribute...
10346 */
10347
10348 if ((attr = ippFindAttribute(con->request, "job-id",
10349 IPP_TAG_INTEGER)) == NULL)
10350 {
10351 send_ipp_status(con, IPP_BAD_REQUEST,
10352 _("Got a printer-uri attribute but no job-id!"));
10353 return;
10354 }
10355
10356 jobid = attr->values[0].integer;
10357 }
10358 else
10359 {
10360 /*
10361 * Got a job URI; parse it to get the job ID...
10362 */
10363
10364 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
10365 sizeof(scheme), username, sizeof(username), host,
10366 sizeof(host), &port, resource, sizeof(resource));
10367
10368 if (strncmp(resource, "/jobs/", 6))
10369 {
10370 /*
10371 * Not a valid URI!
10372 */
10373
10374 send_ipp_status(con, IPP_BAD_REQUEST,
10375 _("Bad job-uri attribute \"%s\"!"),
10376 uri->values[0].string.text);
10377 return;
10378 }
10379
10380 jobid = atoi(resource + 6);
10381 }
10382
10383 /*
10384 * See if the job exists...
10385 */
10386
10387 if ((job = cupsdFindJob(jobid)) == NULL)
10388 {
10389 /*
10390 * Nope - return a "not found" error...
10391 */
10392
10393 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
10394 return;
10395 }
10396
10397 /*
10398 * See if the job has been completed...
10399 */
10400
10401 if (job->state_value > IPP_JOB_STOPPED)
10402 {
10403 /*
10404 * Return a "not-possible" error...
10405 */
10406
10407 send_ipp_status(con, IPP_NOT_POSSIBLE,
10408 _("Job #%d is finished and cannot be altered!"), jobid);
10409 return;
10410 }
10411
10412 /*
10413 * See if the job is owned by the requesting user...
10414 */
10415
10416 if (!validate_user(job, con, job->username, username, sizeof(username)))
10417 {
10418 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
10419 return;
10420 }
10421
10422 /*
10423 * See what the user wants to change.
10424 */
10425
10426 cupsdLoadJob(job);
10427
10428 check_jobs = 0;
10429 event = 0;
10430
10431 for (attr = con->request->attrs; attr; attr = attr->next)
10432 {
10433 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
10434 continue;
10435
10436 if (!strcmp(attr->name, "attributes-charset") ||
10437 !strcmp(attr->name, "attributes-natural-language") ||
10438 !strcmp(attr->name, "document-compression") ||
10439 !strcmp(attr->name, "document-format") ||
10440 !strcmp(attr->name, "job-detailed-status-messages") ||
10441 !strcmp(attr->name, "job-document-access-errors") ||
10442 !strcmp(attr->name, "job-id") ||
10443 !strcmp(attr->name, "job-impressions-completed") ||
10444 !strcmp(attr->name, "job-k-octets") ||
10445 !strcmp(attr->name, "job-originating-host-name") ||
10446 !strcmp(attr->name, "job-originating-user-name") ||
10447 !strcmp(attr->name, "job-printer-up-time") ||
10448 !strcmp(attr->name, "job-printer-uri") ||
10449 !strcmp(attr->name, "job-sheets") ||
10450 !strcmp(attr->name, "job-state-message") ||
10451 !strcmp(attr->name, "job-state-reasons") ||
10452 !strcmp(attr->name, "job-uri") ||
10453 !strcmp(attr->name, "number-of-documents") ||
10454 !strcmp(attr->name, "number-of-intervening-jobs") ||
10455 !strcmp(attr->name, "output-device-assigned") ||
10456 !strncmp(attr->name, "date-time-at-", 13) ||
10457 !strncmp(attr->name, "job-k-octets", 12) ||
10458 !strncmp(attr->name, "job-media-sheets", 16) ||
10459 !strncmp(attr->name, "time-at-", 8))
10460 {
10461 /*
10462 * Read-only attrs!
10463 */
10464
10465 send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE,
10466 _("%s cannot be changed."), attr->name);
10467
10468 if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
10469 attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
10470
10471 continue;
10472 }
10473
10474 if (!strcmp(attr->name, "job-priority"))
10475 {
10476 /*
10477 * Change the job priority...
10478 */
10479
10480 if (attr->value_tag != IPP_TAG_INTEGER)
10481 {
10482 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value!"));
10483
10484 if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
10485 attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
10486 }
10487 else if (job->state_value >= IPP_JOB_PROCESSING)
10488 {
10489 send_ipp_status(con, IPP_NOT_POSSIBLE,
10490 _("Job is completed and cannot be changed."));
10491 return;
10492 }
10493 else if (con->response->request.status.status_code == IPP_OK)
10494 {
10495 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-priority to %d",
10496 attr->values[0].integer);
10497 cupsdSetJobPriority(job, attr->values[0].integer);
10498
10499 check_jobs = 1;
10500 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED |
10501 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED;
10502 }
10503 }
10504 else if (!strcmp(attr->name, "job-state"))
10505 {
10506 /*
10507 * Change the job state...
10508 */
10509
10510 if (attr->value_tag != IPP_TAG_ENUM)
10511 {
10512 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value!"));
10513
10514 if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
10515 attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
10516 }
10517 else
10518 {
10519 switch (attr->values[0].integer)
10520 {
10521 case IPP_JOB_PENDING :
10522 case IPP_JOB_HELD :
10523 if (job->state_value > IPP_JOB_HELD)
10524 {
10525 send_ipp_status(con, IPP_NOT_POSSIBLE,
10526 _("Job state cannot be changed."));
10527 return;
10528 }
10529 else if (con->response->request.status.status_code == IPP_OK)
10530 {
10531 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10532 attr->values[0].integer);
10533
10534 job->state->values[0].integer = attr->values[0].integer;
10535 job->state_value = (ipp_jstate_t)attr->values[0].integer;
10536
10537 event |= CUPSD_EVENT_JOB_STATE;
10538 check_jobs = 1;
10539 }
10540 break;
10541
10542 case IPP_JOB_PROCESSING :
10543 case IPP_JOB_STOPPED :
10544 if (job->state_value != attr->values[0].integer)
10545 {
10546 send_ipp_status(con, IPP_NOT_POSSIBLE,
10547 _("Job state cannot be changed."));
10548 return;
10549 }
10550 break;
10551
10552 case IPP_JOB_CANCELED :
10553 case IPP_JOB_ABORTED :
10554 case IPP_JOB_COMPLETED :
10555 if (job->state_value > IPP_JOB_PROCESSING)
10556 {
10557 send_ipp_status(con, IPP_NOT_POSSIBLE,
10558 _("Job state cannot be changed."));
10559 return;
10560 }
10561 else if (con->response->request.status.status_code == IPP_OK)
10562 {
10563 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10564 attr->values[0].integer);
10565 cupsdCancelJob(job, 0, (ipp_jstate_t)attr->values[0].integer);
10566
10567 check_jobs = 1;
10568 }
10569 break;
10570 }
10571 }
10572 }
10573 else if (con->response->request.status.status_code != IPP_OK)
10574 continue;
10575 else if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10576 IPP_TAG_ZERO)) != NULL)
10577 {
10578 /*
10579 * Some other value; first free the old value...
10580 */
10581
10582 if (job->attrs->prev)
10583 job->attrs->prev->next = attr2->next;
10584 else
10585 job->attrs->attrs = attr2->next;
10586
10587 if (job->attrs->last == attr2)
10588 job->attrs->last = job->attrs->prev;
10589
10590 _ippFreeAttr(attr2);
10591
10592 /*
10593 * Then copy the attribute...
10594 */
10595
10596 copy_attribute(job->attrs, attr, 0);
10597
10598 /*
10599 * See if the job-name or job-hold-until is being changed.
10600 */
10601
10602 if (!strcmp(attr->name, "job-hold-until"))
10603 {
10604 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s",
10605 attr->values[0].string.text);
10606 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
10607
10608 if (!strcmp(attr->values[0].string.text, "no-hold"))
10609 cupsdReleaseJob(job);
10610 else
10611 cupsdHoldJob(job);
10612
10613 check_jobs = 1;
10614 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
10615 }
10616 }
10617 else if (attr->value_tag == IPP_TAG_DELETEATTR)
10618 {
10619 /*
10620 * Delete the attribute...
10621 */
10622
10623 if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10624 IPP_TAG_ZERO)) != NULL)
10625 {
10626 if (job->attrs->prev)
10627 job->attrs->prev->next = attr2->next;
10628 else
10629 job->attrs->attrs = attr2->next;
10630
10631 if (attr2 == job->attrs->last)
10632 job->attrs->last = job->attrs->prev;
10633
10634 _ippFreeAttr(attr2);
10635
10636 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
10637 }
10638 }
10639 else
10640 {
10641 /*
10642 * Add new option by copying it...
10643 */
10644
10645 copy_attribute(job->attrs, attr, 0);
10646
10647 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
10648 }
10649 }
10650
10651 /*
10652 * Save the job...
10653 */
10654
10655 job->dirty = 1;
10656 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
10657
10658 /*
10659 * Send events as needed...
10660 */
10661
10662 if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED)
10663 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED,
10664 cupsdFindDest(job->dest), job,
10665 "Job priority changed by user.");
10666
10667 if (event & CUPSD_EVENT_JOB_STATE)
10668 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
10669 job->state_value == IPP_JOB_HELD ?
10670 "Job held by user." : "Job restarted by user.");
10671
10672 if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED)
10673 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
10674 "Job options changed by user.");
10675
10676 /*
10677 * Start jobs if possible...
10678 */
10679
10680 if (check_jobs)
10681 cupsdCheckJobs();
10682 }
10683
10684
10685 /*
10686 * 'set_printer_attrs()' - Set printer attributes.
10687 */
10688
10689 static void
10690 set_printer_attrs(cupsd_client_t *con, /* I - Client connection */
10691 ipp_attribute_t *uri) /* I - Printer */
10692 {
10693 http_status_t status; /* Policy status */
10694 cups_ptype_t dtype; /* Destination type (printer/class) */
10695 cupsd_printer_t *printer; /* Printer/class */
10696 ipp_attribute_t *attr; /* Printer attribute */
10697 int changed; /* Was anything changed? */
10698
10699
10700 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_attrs(%p[%d], %s)", con,
10701 con->http.fd, uri->values[0].string.text);
10702
10703 /*
10704 * Is the destination valid?
10705 */
10706
10707 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10708 {
10709 /*
10710 * Bad URI...
10711 */
10712
10713 send_ipp_status(con, IPP_NOT_FOUND,
10714 _("The printer or class was not found."));
10715 return;
10716 }
10717
10718 /*
10719 * Check policy...
10720 */
10721
10722 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10723 {
10724 send_http_error(con, status, printer);
10725 return;
10726 }
10727
10728 /*
10729 * Return a list of attributes that can be set via Set-Printer-Attributes.
10730 */
10731
10732 if ((attr = ippFindAttribute(con->request, "printer-location",
10733 IPP_TAG_TEXT)) != NULL)
10734 {
10735 cupsdSetString(&printer->location, attr->values[0].string.text);
10736 changed = 1;
10737 }
10738
10739 if ((attr = ippFindAttribute(con->request, "printer-info",
10740 IPP_TAG_TEXT)) != NULL)
10741 {
10742 cupsdSetString(&printer->info, attr->values[0].string.text);
10743 changed = 1;
10744 }
10745
10746 /*
10747 * Update the printer attributes and return...
10748 */
10749
10750 if (changed)
10751 {
10752 cupsdSetPrinterAttrs(printer);
10753 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
10754
10755 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL,
10756 "Printer \"%s\" description or location changed by \"%s\".",
10757 printer->name, get_username(con));
10758
10759 cupsdLogMessage(CUPSD_LOG_INFO,
10760 "Printer \"%s\" description or location changed by \"%s\".",
10761 printer->name, get_username(con));
10762 }
10763
10764 con->response->request.status.status_code = IPP_OK;
10765 }
10766
10767
10768 /*
10769 * 'set_printer_defaults()' - Set printer default options from a request.
10770 */
10771
10772 static void
10773 set_printer_defaults(
10774 cupsd_client_t *con, /* I - Client connection */
10775 cupsd_printer_t *printer) /* I - Printer */
10776 {
10777 int i; /* Looping var */
10778 ipp_attribute_t *attr; /* Current attribute */
10779 int namelen; /* Length of attribute name */
10780 char name[256], /* New attribute name */
10781 value[256]; /* String version of integer attrs */
10782
10783
10784 for (attr = con->request->attrs; attr; attr = attr->next)
10785 {
10786 /*
10787 * Skip non-printer attributes...
10788 */
10789
10790 if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
10791 continue;
10792
10793 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name);
10794
10795 if (!strcmp(attr->name, "job-sheets-default"))
10796 {
10797 /*
10798 * Only allow keywords and names...
10799 */
10800
10801 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10802 continue;
10803
10804 /*
10805 * Only allow job-sheets-default to be set when running without a
10806 * system high classification level...
10807 */
10808
10809 if (Classification)
10810 continue;
10811
10812 cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text);
10813
10814 if (attr->num_values > 1)
10815 cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text);
10816 else
10817 cupsdSetString(&printer->job_sheets[1], "none");
10818 }
10819 else if (!strcmp(attr->name, "requesting-user-name-allowed"))
10820 {
10821 cupsdFreePrinterUsers(printer);
10822
10823 printer->deny_users = 0;
10824
10825 if (attr->value_tag == IPP_TAG_NAME &&
10826 (attr->num_values > 1 ||
10827 strcmp(attr->values[0].string.text, "all")))
10828 {
10829 for (i = 0; i < attr->num_values; i ++)
10830 cupsdAddPrinterUser(printer, attr->values[i].string.text);
10831 }
10832 }
10833 else if (!strcmp(attr->name, "requesting-user-name-denied"))
10834 {
10835 cupsdFreePrinterUsers(printer);
10836
10837 printer->deny_users = 1;
10838
10839 if (attr->value_tag == IPP_TAG_NAME &&
10840 (attr->num_values > 1 ||
10841 strcmp(attr->values[0].string.text, "none")))
10842 {
10843 for (i = 0; i < attr->num_values; i ++)
10844 cupsdAddPrinterUser(printer, attr->values[i].string.text);
10845 }
10846 }
10847 else if (!strcmp(attr->name, "job-quota-period"))
10848 {
10849 if (attr->value_tag != IPP_TAG_INTEGER)
10850 continue;
10851
10852 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...",
10853 attr->values[0].integer);
10854 cupsdFreeQuotas(printer);
10855
10856 printer->quota_period = attr->values[0].integer;
10857 }
10858 else if (!strcmp(attr->name, "job-k-limit"))
10859 {
10860 if (attr->value_tag != IPP_TAG_INTEGER)
10861 continue;
10862
10863 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...",
10864 attr->values[0].integer);
10865 cupsdFreeQuotas(printer);
10866
10867 printer->k_limit = attr->values[0].integer;
10868 }
10869 else if (!strcmp(attr->name, "job-page-limit"))
10870 {
10871 if (attr->value_tag != IPP_TAG_INTEGER)
10872 continue;
10873
10874 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...",
10875 attr->values[0].integer);
10876 cupsdFreeQuotas(printer);
10877
10878 printer->page_limit = attr->values[0].integer;
10879 }
10880 else if (!strcmp(attr->name, "printer-op-policy"))
10881 {
10882 cupsd_policy_t *p; /* Policy */
10883
10884
10885 if (attr->value_tag != IPP_TAG_NAME)
10886 continue;
10887
10888 if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
10889 {
10890 cupsdLogMessage(CUPSD_LOG_DEBUG,
10891 "Setting printer-op-policy to \"%s\"...",
10892 attr->values[0].string.text);
10893 cupsdSetString(&printer->op_policy, attr->values[0].string.text);
10894 printer->op_policy_ptr = p;
10895 }
10896 else
10897 {
10898 send_ipp_status(con, IPP_NOT_POSSIBLE,
10899 _("Unknown printer-op-policy \"%s\"."),
10900 attr->values[0].string.text);
10901 return;
10902 }
10903 }
10904 else if (!strcmp(attr->name, "printer-error-policy"))
10905 {
10906 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10907 continue;
10908
10909 if (strcmp(attr->values[0].string.text, "abort-job") &&
10910 strcmp(attr->values[0].string.text, "retry-current-job") &&
10911 strcmp(attr->values[0].string.text, "retry-job") &&
10912 strcmp(attr->values[0].string.text, "stop-printer"))
10913 {
10914 send_ipp_status(con, IPP_NOT_POSSIBLE,
10915 _("Unknown printer-error-policy \"%s\"."),
10916 attr->values[0].string.text);
10917 return;
10918 }
10919
10920 cupsdLogMessage(CUPSD_LOG_DEBUG,
10921 "Setting printer-error-policy to \"%s\"...",
10922 attr->values[0].string.text);
10923 cupsdSetString(&printer->error_policy, attr->values[0].string.text);
10924 }
10925
10926 /*
10927 * Skip any other non-default attributes...
10928 */
10929
10930 namelen = strlen(attr->name);
10931 if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") ||
10932 namelen > (sizeof(name) - 1) || attr->num_values != 1)
10933 continue;
10934
10935 /*
10936 * OK, anything else must be a user-defined default...
10937 */
10938
10939 strlcpy(name, attr->name, sizeof(name));
10940 name[namelen - 8] = '\0'; /* Strip "-default" */
10941
10942 switch (attr->value_tag)
10943 {
10944 case IPP_TAG_DELETEATTR :
10945 printer->num_options = cupsRemoveOption(name,
10946 printer->num_options,
10947 &(printer->options));
10948 cupsdLogMessage(CUPSD_LOG_DEBUG,
10949 "Deleting %s", attr->name);
10950 break;
10951
10952 case IPP_TAG_NAME :
10953 case IPP_TAG_KEYWORD :
10954 case IPP_TAG_URI :
10955 printer->num_options = cupsAddOption(name,
10956 attr->values[0].string.text,
10957 printer->num_options,
10958 &(printer->options));
10959 cupsdLogMessage(CUPSD_LOG_DEBUG,
10960 "Setting %s to \"%s\"...", attr->name,
10961 attr->values[0].string.text);
10962 break;
10963
10964 case IPP_TAG_BOOLEAN :
10965 printer->num_options = cupsAddOption(name,
10966 attr->values[0].boolean ?
10967 "true" : "false",
10968 printer->num_options,
10969 &(printer->options));
10970 cupsdLogMessage(CUPSD_LOG_DEBUG,
10971 "Setting %s to %s...", attr->name,
10972 attr->values[0].boolean ? "true" : "false");
10973 break;
10974
10975 case IPP_TAG_INTEGER :
10976 case IPP_TAG_ENUM :
10977 sprintf(value, "%d", attr->values[0].integer);
10978 printer->num_options = cupsAddOption(name, value,
10979 printer->num_options,
10980 &(printer->options));
10981 cupsdLogMessage(CUPSD_LOG_DEBUG,
10982 "Setting %s to %s...", attr->name, value);
10983 break;
10984
10985 case IPP_TAG_RANGE :
10986 sprintf(value, "%d-%d", attr->values[0].range.lower,
10987 attr->values[0].range.upper);
10988 printer->num_options = cupsAddOption(name, value,
10989 printer->num_options,
10990 &(printer->options));
10991 cupsdLogMessage(CUPSD_LOG_DEBUG,
10992 "Setting %s to %s...", attr->name, value);
10993 break;
10994
10995 case IPP_TAG_RESOLUTION :
10996 sprintf(value, "%dx%d%s", attr->values[0].resolution.xres,
10997 attr->values[0].resolution.yres,
10998 attr->values[0].resolution.units == IPP_RES_PER_INCH ?
10999 "dpi" : "dpc");
11000 printer->num_options = cupsAddOption(name, value,
11001 printer->num_options,
11002 &(printer->options));
11003 cupsdLogMessage(CUPSD_LOG_DEBUG,
11004 "Setting %s to %s...", attr->name, value);
11005 break;
11006
11007 default :
11008 /* Do nothing for other values */
11009 break;
11010 }
11011 }
11012 }
11013
11014
11015 /*
11016 * 'start_printer()' - Start a printer.
11017 */
11018
11019 static void
11020 start_printer(cupsd_client_t *con, /* I - Client connection */
11021 ipp_attribute_t *uri) /* I - Printer URI */
11022 {
11023 http_status_t status; /* Policy status */
11024 cups_ptype_t dtype; /* Destination type (printer/class) */
11025 cupsd_printer_t *printer; /* Printer data */
11026
11027
11028 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con,
11029 con->http.fd, uri->values[0].string.text);
11030
11031 /*
11032 * Is the destination valid?
11033 */
11034
11035 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
11036 {
11037 /*
11038 * Bad URI...
11039 */
11040
11041 send_ipp_status(con, IPP_NOT_FOUND,
11042 _("The printer or class was not found."));
11043 return;
11044 }
11045
11046 /*
11047 * Check policy...
11048 */
11049
11050 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
11051 {
11052 send_http_error(con, status, printer);
11053 return;
11054 }
11055
11056 /*
11057 * Start the printer...
11058 */
11059
11060 printer->state_message[0] = '\0';
11061
11062 cupsdStartPrinter(printer, 1);
11063
11064 if (dtype & CUPS_PRINTER_CLASS)
11065 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".",
11066 printer->name, get_username(con));
11067 else
11068 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".",
11069 printer->name, get_username(con));
11070
11071 cupsdCheckJobs();
11072
11073 /*
11074 * Everything was ok, so return OK status...
11075 */
11076
11077 con->response->request.status.status_code = IPP_OK;
11078 }
11079
11080
11081 /*
11082 * 'stop_printer()' - Stop a printer.
11083 */
11084
11085 static void
11086 stop_printer(cupsd_client_t *con, /* I - Client connection */
11087 ipp_attribute_t *uri) /* I - Printer URI */
11088 {
11089 http_status_t status; /* Policy status */
11090 cups_ptype_t dtype; /* Destination type (printer/class) */
11091 cupsd_printer_t *printer; /* Printer data */
11092 ipp_attribute_t *attr; /* printer-state-message attribute */
11093
11094
11095 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con,
11096 con->http.fd, uri->values[0].string.text);
11097
11098 /*
11099 * Is the destination valid?
11100 */
11101
11102 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
11103 {
11104 /*
11105 * Bad URI...
11106 */
11107
11108 send_ipp_status(con, IPP_NOT_FOUND,
11109 _("The printer or class was not found."));
11110 return;
11111 }
11112
11113 /*
11114 * Check policy...
11115 */
11116
11117 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
11118 {
11119 send_http_error(con, status, printer);
11120 return;
11121 }
11122
11123 /*
11124 * Stop the printer...
11125 */
11126
11127 if ((attr = ippFindAttribute(con->request, "printer-state-message",
11128 IPP_TAG_TEXT)) == NULL)
11129 strcpy(printer->state_message, "Paused");
11130 else
11131 {
11132 strlcpy(printer->state_message, attr->values[0].string.text,
11133 sizeof(printer->state_message));
11134 }
11135
11136 cupsdStopPrinter(printer, 1);
11137
11138 if (dtype & CUPS_PRINTER_CLASS)
11139 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".",
11140 printer->name, get_username(con));
11141 else
11142 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".",
11143 printer->name, get_username(con));
11144
11145 /*
11146 * Everything was ok, so return OK status...
11147 */
11148
11149 con->response->request.status.status_code = IPP_OK;
11150 }
11151
11152
11153 /*
11154 * 'url_encode_attr()' - URL-encode a string attribute.
11155 */
11156
11157 static void
11158 url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */
11159 char *buffer,/* I - String buffer */
11160 int bufsize)/* I - Size of buffer */
11161 {
11162 int i; /* Looping var */
11163 char *bufptr, /* Pointer into buffer */
11164 *bufend; /* End of buffer */
11165
11166
11167 strlcpy(buffer, attr->name, bufsize);
11168 bufptr = buffer + strlen(buffer);
11169 bufend = buffer + bufsize - 1;
11170
11171 for (i = 0; i < attr->num_values; i ++)
11172 {
11173 if (bufptr >= bufend)
11174 break;
11175
11176 if (i)
11177 *bufptr++ = ',';
11178 else
11179 *bufptr++ = '=';
11180
11181 if (bufptr >= bufend)
11182 break;
11183
11184 *bufptr++ = '\'';
11185
11186 bufptr = url_encode_string(attr->values[i].string.text,
11187 bufptr, bufend - bufptr + 1);
11188
11189 if (bufptr >= bufend)
11190 break;
11191
11192 *bufptr++ = '\'';
11193 }
11194
11195 *bufptr = '\0';
11196 }
11197
11198
11199 /*
11200 * 'url_encode_string()' - URL-encode a string.
11201 */
11202
11203 static char * /* O - End of string */
11204 url_encode_string(const char *s, /* I - String */
11205 char *buffer, /* I - String buffer */
11206 int bufsize) /* I - Size of buffer */
11207 {
11208 char *bufptr, /* Pointer into buffer */
11209 *bufend; /* End of buffer */
11210 static const char *hex = "0123456789ABCDEF";
11211 /* Hex digits */
11212
11213
11214 bufptr = buffer;
11215 bufend = buffer + bufsize - 1;
11216
11217 while (*s && bufptr < bufend)
11218 {
11219 if (*s == ' ' || *s == '%')
11220 {
11221 if (bufptr >= (bufend - 2))
11222 break;
11223
11224 *bufptr++ = '%';
11225 *bufptr++ = hex[(*s >> 4) & 15];
11226 *bufptr++ = hex[*s & 15];
11227
11228 s ++;
11229 }
11230 else if (*s == '\'' || *s == '\\')
11231 {
11232 if (bufptr >= (bufend - 1))
11233 break;
11234
11235 *bufptr++ = '\\';
11236 *bufptr++ = *s++;
11237 }
11238 else
11239 *bufptr++ = *s++;
11240 }
11241
11242 *bufptr = '\0';
11243
11244 return (bufptr);
11245 }
11246
11247
11248 /*
11249 * 'user_allowed()' - See if a user is allowed to print to a queue.
11250 */
11251
11252 static int /* O - 0 if not allowed, 1 if allowed */
11253 user_allowed(cupsd_printer_t *p, /* I - Printer or class */
11254 const char *username) /* I - Username */
11255 {
11256 int i; /* Looping var */
11257 struct passwd *pw; /* User password data */
11258 char baseuser[256], /* Base username */
11259 *baseptr; /* Pointer to "@" in base username */
11260
11261
11262 if (p->num_users == 0)
11263 return (1);
11264
11265 if (!strcmp(username, "root"))
11266 return (1);
11267
11268 if (strchr(username, '@'))
11269 {
11270 /*
11271 * Strip @REALM for username check...
11272 */
11273
11274 strlcpy(baseuser, username, sizeof(baseuser));
11275
11276 if ((baseptr = strchr(baseuser, '@')) != NULL)
11277 *baseptr = '\0';
11278
11279 username = baseuser;
11280 }
11281
11282 pw = getpwnam(username);
11283 endpwent();
11284
11285 for (i = 0; i < p->num_users; i ++)
11286 {
11287 if (p->users[i][0] == '@')
11288 {
11289 /*
11290 * Check group membership...
11291 */
11292
11293 if (cupsdCheckGroup(username, pw, p->users[i] + 1))
11294 break;
11295 }
11296 else if (p->users[i][0] == '#')
11297 {
11298 /*
11299 * Check UUID...
11300 */
11301
11302 if (cupsdCheckGroup(username, pw, p->users[i]))
11303 break;
11304 }
11305 else if (!strcasecmp(username, p->users[i]))
11306 break;
11307 }
11308
11309 return ((i < p->num_users) != p->deny_users);
11310 }
11311
11312
11313 /*
11314 * 'validate_job()' - Validate printer options and destination.
11315 */
11316
11317 static void
11318 validate_job(cupsd_client_t *con, /* I - Client connection */
11319 ipp_attribute_t *uri) /* I - Printer URI */
11320 {
11321 http_status_t status; /* Policy status */
11322 ipp_attribute_t *attr; /* Current attribute */
11323 ipp_attribute_t *format; /* Document-format attribute */
11324 cups_ptype_t dtype; /* Destination type (printer/class) */
11325 char super[MIME_MAX_SUPER],
11326 /* Supertype of file */
11327 type[MIME_MAX_TYPE];
11328 /* Subtype of file */
11329 cupsd_printer_t *printer; /* Printer */
11330
11331
11332 cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con,
11333 con->http.fd, uri->values[0].string.text);
11334
11335 /*
11336 * OK, see if the client is sending the document compressed - CUPS
11337 * doesn't support compression yet...
11338 */
11339
11340 if ((attr = ippFindAttribute(con->request, "compression",
11341 IPP_TAG_KEYWORD)) != NULL &&
11342 !strcmp(attr->values[0].string.text, "none"))
11343 {
11344 send_ipp_status(con, IPP_ATTRIBUTES,
11345 _("Unsupported compression attribute %s!"),
11346 attr->values[0].string.text);
11347 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
11348 "compression", NULL, attr->values[0].string.text);
11349 return;
11350 }
11351
11352 /*
11353 * Is it a format we support?
11354 */
11355
11356 if ((format = ippFindAttribute(con->request, "document-format",
11357 IPP_TAG_MIMETYPE)) != NULL)
11358 {
11359 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
11360 super, type) != 2)
11361 {
11362 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"),
11363 format->values[0].string.text);
11364 return;
11365 }
11366
11367 if ((strcmp(super, "application") || strcmp(type, "octet-stream")) &&
11368 !mimeType(MimeDatabase, super, type))
11369 {
11370 cupsdLogMessage(CUPSD_LOG_INFO,
11371 "Hint: Do you have the raw file printing rules enabled?");
11372 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
11373 _("Unsupported format \"%s\"!"),
11374 format->values[0].string.text);
11375 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
11376 "document-format", NULL, format->values[0].string.text);
11377 return;
11378 }
11379 }
11380
11381 /*
11382 * Is the destination valid?
11383 */
11384
11385 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
11386 {
11387 /*
11388 * Bad URI...
11389 */
11390
11391 send_ipp_status(con, IPP_NOT_FOUND,
11392 _("The printer or class was not found."));
11393 return;
11394 }
11395
11396 /*
11397 * Check policy...
11398 */
11399
11400 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
11401 {
11402 send_http_error(con, status, printer);
11403 return;
11404 }
11405
11406 /*
11407 * Everything was ok, so return OK status...
11408 */
11409
11410 con->response->request.status.status_code = IPP_OK;
11411 }
11412
11413
11414 /*
11415 * 'validate_name()' - Make sure the printer name only contains valid chars.
11416 */
11417
11418 static int /* O - 0 if name is no good, 1 if good */
11419 validate_name(const char *name) /* I - Name to check */
11420 {
11421 const char *ptr; /* Pointer into name */
11422
11423
11424 /*
11425 * Scan the whole name...
11426 */
11427
11428 for (ptr = name; *ptr; ptr ++)
11429 if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
11430 return (0);
11431
11432 /*
11433 * All the characters are good; validate the length, too...
11434 */
11435
11436 return ((ptr - name) < 128);
11437 }
11438
11439
11440 /*
11441 * 'validate_user()' - Validate the user for the request.
11442 */
11443
11444 static int /* O - 1 if permitted, 0 otherwise */
11445 validate_user(cupsd_job_t *job, /* I - Job */
11446 cupsd_client_t *con, /* I - Client connection */
11447 const char *owner, /* I - Owner of job/resource */
11448 char *username, /* O - Authenticated username */
11449 int userlen) /* I - Length of username */
11450 {
11451 cupsd_printer_t *printer; /* Printer for job */
11452
11453
11454 cupsdLogMessage(CUPSD_LOG_DEBUG2,
11455 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
11456 "userlen=%d)",
11457 job->id, con ? con->http.fd : 0,
11458 owner ? owner : "(null)", username, userlen);
11459
11460 /*
11461 * Validate input...
11462 */
11463
11464 if (!con || !owner || !username || userlen <= 0)
11465 return (0);
11466
11467 /*
11468 * Get the best authenticated username that is available.
11469 */
11470
11471 strlcpy(username, get_username(con), userlen);
11472
11473 /*
11474 * Check the username against the owner...
11475 */
11476
11477 printer = cupsdFindDest(job->dest);
11478
11479 return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr,
11480 con, owner) == HTTP_OK);
11481 }
11482
11483
11484 /*
11485 * End of "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $".
11486 */