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