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