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