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