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