]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/ipp.c
Merge CUPS 1.4svn-r8052 (tentative 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->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", ServerRoot,
6344 printer->name);
6345 unlink(filename);
6346
6347 #ifdef __APPLE__
6348 /*
6349 * Unregister color profiles...
6350 */
6351
6352 apple_unregister_profiles(printer);
6353 #endif /* __APPLE__ */
6354
6355 if (dtype & CUPS_PRINTER_CLASS)
6356 {
6357 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".",
6358 printer->name, get_username(con));
6359
6360 cupsdDeletePrinter(printer, 0);
6361 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
6362 }
6363 else
6364 {
6365 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".",
6366 printer->name, get_username(con));
6367
6368 cupsdDeletePrinter(printer, 0);
6369 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
6370 }
6371
6372 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
6373
6374 /*
6375 * Return with no errors...
6376 */
6377
6378 con->response->request.status.status_code = IPP_OK;
6379 }
6380
6381
6382 /*
6383 * 'get_default()' - Get the default destination.
6384 */
6385
6386 static void
6387 get_default(cupsd_client_t *con) /* I - Client connection */
6388 {
6389 http_status_t status; /* Policy status */
6390 cups_array_t *ra; /* Requested attributes array */
6391
6392
6393 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->http.fd);
6394
6395 /*
6396 * Check policy...
6397 */
6398
6399 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6400 {
6401 send_http_error(con, status, NULL);
6402 return;
6403 }
6404
6405 if (DefaultPrinter)
6406 {
6407 ra = create_requested_array(con->request);
6408
6409 copy_printer_attrs(con, DefaultPrinter, ra);
6410
6411 cupsArrayDelete(ra);
6412
6413 con->response->request.status.status_code = IPP_OK;
6414 }
6415 else
6416 send_ipp_status(con, IPP_NOT_FOUND, _("No default printer"));
6417 }
6418
6419
6420 /*
6421 * 'get_devices()' - Get the list of available devices on the local system.
6422 */
6423
6424 static void
6425 get_devices(cupsd_client_t *con) /* I - Client connection */
6426 {
6427 http_status_t status; /* Policy status */
6428 ipp_attribute_t *limit, /* limit attribute */
6429 *timeout, /* timeout attribute */
6430 *requested, /* requested-attributes attribute */
6431 *exclude; /* exclude-schemes attribute */
6432 char command[1024], /* cups-deviced command */
6433 options[1024], /* Options to pass to command */
6434 requested_str[256],
6435 /* String for requested attributes */
6436 exclude_str[512];
6437 /* String for excluded attributes */
6438
6439
6440 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->http.fd);
6441
6442 /*
6443 * Check policy...
6444 */
6445
6446 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6447 {
6448 send_http_error(con, status, NULL);
6449 return;
6450 }
6451
6452 /*
6453 * Run cups-deviced command with the given options...
6454 */
6455
6456 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
6457 timeout = ippFindAttribute(con->request, "timeout", IPP_TAG_INTEGER);
6458 requested = ippFindAttribute(con->request, "requested-attributes",
6459 IPP_TAG_KEYWORD);
6460 exclude = ippFindAttribute(con->request, "exclude-schemes", IPP_TAG_NAME);
6461
6462 if (requested)
6463 url_encode_attr(requested, requested_str, sizeof(requested_str));
6464 else
6465 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
6466
6467 if (exclude)
6468 url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
6469 else
6470 exclude_str[0] = '\0';
6471
6472 snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin);
6473 snprintf(options, sizeof(options),
6474 "%d+%d+%d+%d+%s%s%s",
6475 con->request->request.op.request_id,
6476 limit ? limit->values[0].integer : 0,
6477 timeout ? timeout->values[0].integer : 10,
6478 (int)User,
6479 requested_str,
6480 exclude_str[0] ? "%20" : "", exclude_str);
6481
6482 if (cupsdSendCommand(con, command, options, 1))
6483 {
6484 /*
6485 * Command started successfully, don't send an IPP response here...
6486 */
6487
6488 ippDelete(con->response);
6489 con->response = NULL;
6490 }
6491 else
6492 {
6493 /*
6494 * Command failed, return "internal error" so the user knows something
6495 * went wrong...
6496 */
6497
6498 send_ipp_status(con, IPP_INTERNAL_ERROR,
6499 _("cups-deviced failed to execute."));
6500 }
6501 }
6502
6503
6504 /*
6505 * 'get_document()' - Get a copy of a job file.
6506 */
6507
6508 static void
6509 get_document(cupsd_client_t *con, /* I - Client connection */
6510 ipp_attribute_t *uri) /* I - Job URI */
6511 {
6512 http_status_t status; /* Policy status */
6513 ipp_attribute_t *attr; /* Current attribute */
6514 int jobid; /* Job ID */
6515 int docnum; /* Document number */
6516 cupsd_job_t *job; /* Current job */
6517 char scheme[HTTP_MAX_URI], /* Method portion of URI */
6518 username[HTTP_MAX_URI], /* Username portion of URI */
6519 host[HTTP_MAX_URI], /* Host portion of URI */
6520 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6521 int port; /* Port portion of URI */
6522 char filename[1024], /* Filename for document */
6523 format[1024]; /* Format for document */
6524
6525
6526 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_document(%p[%d], %s)", con,
6527 con->http.fd, uri->values[0].string.text);
6528
6529 /*
6530 * See if we have a job URI or a printer URI...
6531 */
6532
6533 if (!strcmp(uri->name, "printer-uri"))
6534 {
6535 /*
6536 * Got a printer URI; see if we also have a job-id attribute...
6537 */
6538
6539 if ((attr = ippFindAttribute(con->request, "job-id",
6540 IPP_TAG_INTEGER)) == NULL)
6541 {
6542 send_ipp_status(con, IPP_BAD_REQUEST,
6543 _("Got a printer-uri attribute but no job-id!"));
6544 return;
6545 }
6546
6547 jobid = attr->values[0].integer;
6548 }
6549 else
6550 {
6551 /*
6552 * Got a job URI; parse it to get the job ID...
6553 */
6554
6555 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6556 sizeof(scheme), username, sizeof(username), host,
6557 sizeof(host), &port, resource, sizeof(resource));
6558
6559 if (strncmp(resource, "/jobs/", 6))
6560 {
6561 /*
6562 * Not a valid URI!
6563 */
6564
6565 send_ipp_status(con, IPP_BAD_REQUEST,
6566 _("Bad job-uri attribute \"%s\"!"),
6567 uri->values[0].string.text);
6568 return;
6569 }
6570
6571 jobid = atoi(resource + 6);
6572 }
6573
6574 /*
6575 * See if the job exists...
6576 */
6577
6578 if ((job = cupsdFindJob(jobid)) == NULL)
6579 {
6580 /*
6581 * Nope - return a "not found" error...
6582 */
6583
6584 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
6585 return;
6586 }
6587
6588 /*
6589 * Check policy...
6590 */
6591
6592 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6593 {
6594 send_http_error(con, status, NULL);
6595 return;
6596 }
6597
6598 /*
6599 * Get the document number...
6600 */
6601
6602 if ((attr = ippFindAttribute(con->request, "document-number",
6603 IPP_TAG_INTEGER)) == NULL)
6604 {
6605 send_ipp_status(con, IPP_BAD_REQUEST,
6606 _("Missing document-number attribute!"));
6607 return;
6608 }
6609
6610 if ((docnum = attr->values[0].integer) < 1 || docnum > job->num_files ||
6611 attr->num_values > 1)
6612 {
6613 send_ipp_status(con, IPP_NOT_FOUND, _("Document %d not found in job %d."),
6614 docnum, jobid);
6615 return;
6616 }
6617
6618 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, jobid,
6619 docnum);
6620 if ((con->file = open(filename, O_RDONLY)) == -1)
6621 {
6622 cupsdLogMessage(CUPSD_LOG_ERROR,
6623 "Unable to open document %d in job %d - %s", docnum, jobid,
6624 strerror(errno));
6625 send_ipp_status(con, IPP_NOT_FOUND,
6626 _("Unable to open document %d in job %d!"), docnum, jobid);
6627 return;
6628 }
6629
6630 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
6631
6632 cupsdLoadJob(job);
6633
6634 snprintf(format, sizeof(format), "%s/%s", job->filetypes[docnum - 1]->super,
6635 job->filetypes[docnum - 1]->type);
6636
6637 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
6638 NULL, format);
6639 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "document-number",
6640 docnum);
6641 if ((attr = ippFindAttribute(job->attrs, "document-name",
6642 IPP_TAG_NAME)) != NULL)
6643 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "document-name",
6644 NULL, attr->values[0].string.text);
6645 }
6646
6647
6648 /*
6649 * 'get_job_attrs()' - Get job attributes.
6650 */
6651
6652 static void
6653 get_job_attrs(cupsd_client_t *con, /* I - Client connection */
6654 ipp_attribute_t *uri) /* I - Job URI */
6655 {
6656 http_status_t status; /* Policy status */
6657 ipp_attribute_t *attr; /* Current attribute */
6658 int jobid; /* Job ID */
6659 cupsd_job_t *job; /* Current job */
6660 char scheme[HTTP_MAX_URI], /* Method portion of URI */
6661 username[HTTP_MAX_URI], /* Username portion of URI */
6662 host[HTTP_MAX_URI], /* Host portion of URI */
6663 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6664 int port; /* Port portion of URI */
6665 cups_array_t *ra; /* Requested attributes array */
6666
6667
6668 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con,
6669 con->http.fd, uri->values[0].string.text);
6670
6671 /*
6672 * See if we have a job URI or a printer URI...
6673 */
6674
6675 if (!strcmp(uri->name, "printer-uri"))
6676 {
6677 /*
6678 * Got a printer URI; see if we also have a job-id attribute...
6679 */
6680
6681 if ((attr = ippFindAttribute(con->request, "job-id",
6682 IPP_TAG_INTEGER)) == NULL)
6683 {
6684 send_ipp_status(con, IPP_BAD_REQUEST,
6685 _("Got a printer-uri attribute but no job-id!"));
6686 return;
6687 }
6688
6689 jobid = attr->values[0].integer;
6690 }
6691 else
6692 {
6693 /*
6694 * Got a job URI; parse it to get the job ID...
6695 */
6696
6697 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6698 sizeof(scheme), username, sizeof(username), host,
6699 sizeof(host), &port, resource, sizeof(resource));
6700
6701 if (strncmp(resource, "/jobs/", 6))
6702 {
6703 /*
6704 * Not a valid URI!
6705 */
6706
6707 send_ipp_status(con, IPP_BAD_REQUEST,
6708 _("Bad job-uri attribute \"%s\"!"),
6709 uri->values[0].string.text);
6710 return;
6711 }
6712
6713 jobid = atoi(resource + 6);
6714 }
6715
6716 /*
6717 * See if the job exists...
6718 */
6719
6720 if ((job = cupsdFindJob(jobid)) == NULL)
6721 {
6722 /*
6723 * Nope - return a "not found" error...
6724 */
6725
6726 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
6727 return;
6728 }
6729
6730 /*
6731 * Check policy...
6732 */
6733
6734 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6735 {
6736 send_http_error(con, status, NULL);
6737 return;
6738 }
6739
6740 /*
6741 * Copy attributes...
6742 */
6743
6744 cupsdLoadJob(job);
6745
6746 ra = create_requested_array(con->request);
6747 copy_job_attrs(con, job, ra);
6748 cupsArrayDelete(ra);
6749
6750 con->response->request.status.status_code = IPP_OK;
6751 }
6752
6753
6754 /*
6755 * 'get_jobs()' - Get a list of jobs for the specified printer.
6756 */
6757
6758 static void
6759 get_jobs(cupsd_client_t *con, /* I - Client connection */
6760 ipp_attribute_t *uri) /* I - Printer URI */
6761 {
6762 http_status_t status; /* Policy status */
6763 ipp_attribute_t *attr; /* Current attribute */
6764 const char *dest; /* Destination */
6765 cups_ptype_t dtype; /* Destination type (printer/class) */
6766 cups_ptype_t dmask; /* Destination type mask */
6767 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
6768 username[HTTP_MAX_URI], /* Username portion of URI */
6769 host[HTTP_MAX_URI], /* Host portion of URI */
6770 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6771 int port; /* Port portion of URI */
6772 int completed; /* Completed jobs? */
6773 int first_job_id; /* First job ID */
6774 int limit; /* Maximum number of jobs to return */
6775 int count; /* Number of jobs that match */
6776 cupsd_job_t *job; /* Current job pointer */
6777 cupsd_printer_t *printer; /* Printer */
6778 cups_array_t *list; /* Which job list... */
6779 cups_array_t *ra; /* Requested attributes array */
6780
6781
6782 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->http.fd,
6783 uri->values[0].string.text);
6784
6785 /*
6786 * Is the destination valid?
6787 */
6788
6789 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6790 sizeof(scheme), username, sizeof(username), host,
6791 sizeof(host), &port, resource, sizeof(resource));
6792
6793 if (!strcmp(resource, "/") ||
6794 (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6))
6795 {
6796 dest = NULL;
6797 dtype = (cups_ptype_t)0;
6798 dmask = (cups_ptype_t)0;
6799 printer = NULL;
6800 }
6801 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
6802 {
6803 dest = NULL;
6804 dtype = (cups_ptype_t)0;
6805 dmask = CUPS_PRINTER_CLASS;
6806 printer = NULL;
6807 }
6808 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
6809 {
6810 dest = NULL;
6811 dtype = CUPS_PRINTER_CLASS;
6812 dmask = CUPS_PRINTER_CLASS;
6813 printer = NULL;
6814 }
6815 else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype,
6816 &printer)) == NULL)
6817 {
6818 /*
6819 * Bad URI...
6820 */
6821
6822 send_ipp_status(con, IPP_NOT_FOUND,
6823 _("The printer or class was not found."));
6824 return;
6825 }
6826 else
6827 {
6828 dtype &= CUPS_PRINTER_CLASS;
6829 dmask = CUPS_PRINTER_CLASS;
6830 }
6831
6832 /*
6833 * Check policy...
6834 */
6835
6836 if (printer)
6837 {
6838 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
6839 NULL)) != HTTP_OK)
6840 {
6841 send_http_error(con, status, printer);
6842 return;
6843 }
6844 }
6845 else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6846 {
6847 send_http_error(con, status, NULL);
6848 return;
6849 }
6850
6851 /*
6852 * See if the "which-jobs" attribute have been specified...
6853 */
6854
6855 if ((attr = ippFindAttribute(con->request, "which-jobs",
6856 IPP_TAG_KEYWORD)) != NULL &&
6857 !strcmp(attr->values[0].string.text, "completed"))
6858 {
6859 completed = 1;
6860 list = Jobs;
6861 }
6862 else if (attr && !strcmp(attr->values[0].string.text, "all"))
6863 {
6864 completed = 0;
6865 list = Jobs;
6866 }
6867 else if (attr && !strcmp(attr->values[0].string.text, "printing"))
6868 {
6869 completed = 0;
6870 list = PrintingJobs;
6871 }
6872 else
6873 {
6874 completed = 0;
6875 list = ActiveJobs;
6876 }
6877
6878 /*
6879 * See if they want to limit the number of jobs reported...
6880 */
6881
6882 if ((attr = ippFindAttribute(con->request, "limit",
6883 IPP_TAG_INTEGER)) != NULL)
6884 limit = attr->values[0].integer;
6885 else
6886 limit = 0;
6887
6888 if ((attr = ippFindAttribute(con->request, "first-job-id",
6889 IPP_TAG_INTEGER)) != NULL)
6890 first_job_id = attr->values[0].integer;
6891 else
6892 first_job_id = 1;
6893
6894 /*
6895 * See if we only want to see jobs for a specific user...
6896 */
6897
6898 if ((attr = ippFindAttribute(con->request, "my-jobs",
6899 IPP_TAG_BOOLEAN)) != NULL &&
6900 attr->values[0].boolean)
6901 strlcpy(username, get_username(con), sizeof(username));
6902 else
6903 username[0] = '\0';
6904
6905 ra = create_requested_array(con->request);
6906
6907 /*
6908 * OK, build a list of jobs for this printer...
6909 */
6910
6911 for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list);
6912 (limit <= 0 || count < limit) && job;
6913 job = (cupsd_job_t *)cupsArrayNext(list))
6914 {
6915 /*
6916 * Filter out jobs that don't match...
6917 */
6918
6919 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: job->id = %d", job->id);
6920
6921 if (!job->dest || !job->username)
6922 cupsdLoadJob(job);
6923
6924 if (!job->dest || !job->username)
6925 continue;
6926
6927 if ((dest && strcmp(job->dest, dest)) &&
6928 (!job->printer || !dest || strcmp(job->printer->name, dest)))
6929 continue;
6930 if ((job->dtype & dmask) != dtype &&
6931 (!job->printer || (job->printer->type & dmask) != dtype))
6932 continue;
6933 if (completed && job->state_value <= IPP_JOB_STOPPED)
6934 continue;
6935
6936 if (job->id < first_job_id)
6937 continue;
6938
6939 cupsdLoadJob(job);
6940
6941 if (!job->attrs)
6942 continue;
6943
6944 if (username[0] && strcasecmp(username, job->username))
6945 continue;
6946
6947 if (count > 0)
6948 ippAddSeparator(con->response);
6949
6950 count ++;
6951
6952 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count = %d", count);
6953
6954 copy_job_attrs(con, job, ra);
6955 }
6956
6957 cupsArrayDelete(ra);
6958
6959 con->response->request.status.status_code = IPP_OK;
6960 }
6961
6962
6963 /*
6964 * 'get_notifications()' - Get events for a subscription.
6965 */
6966
6967 static void
6968 get_notifications(cupsd_client_t *con) /* I - Client connection */
6969 {
6970 int i, j; /* Looping vars */
6971 http_status_t status; /* Policy status */
6972 cupsd_subscription_t *sub; /* Subscription */
6973 ipp_attribute_t *ids, /* notify-subscription-ids */
6974 *sequences; /* notify-sequence-numbers */
6975 int min_seq; /* Minimum sequence number */
6976 int interval; /* Poll interval */
6977
6978
6979 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])",
6980 con, con->http.fd);
6981
6982 /*
6983 * Get subscription attributes...
6984 */
6985
6986 ids = ippFindAttribute(con->request, "notify-subscription-ids",
6987 IPP_TAG_INTEGER);
6988 sequences = ippFindAttribute(con->request, "notify-sequence-numbers",
6989 IPP_TAG_INTEGER);
6990
6991 if (!ids)
6992 {
6993 send_ipp_status(con, IPP_BAD_REQUEST,
6994 _("Missing notify-subscription-ids attribute!"));
6995 return;
6996 }
6997
6998 /*
6999 * Are the subscription IDs valid?
7000 */
7001
7002 for (i = 0, interval = 60; i < ids->num_values; i ++)
7003 {
7004 if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL)
7005 {
7006 /*
7007 * Bad subscription ID...
7008 */
7009
7010 send_ipp_status(con, IPP_NOT_FOUND,
7011 _("notify-subscription-id %d no good!"),
7012 ids->values[i].integer);
7013 return;
7014 }
7015
7016 /*
7017 * Check policy...
7018 */
7019
7020 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
7021 DefaultPolicyPtr,
7022 con, sub->owner)) != HTTP_OK)
7023 {
7024 send_http_error(con, status, sub->dest);
7025 return;
7026 }
7027
7028 /*
7029 * Check the subscription type and update the interval accordingly.
7030 */
7031
7032 if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING &&
7033 interval > 10)
7034 interval = 10;
7035 else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED)
7036 interval = 0;
7037 else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING &&
7038 interval > 30)
7039 interval = 30;
7040 }
7041
7042 /*
7043 * Tell the client to poll again in N seconds...
7044 */
7045
7046 if (interval > 0)
7047 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
7048 "notify-get-interval", interval);
7049
7050 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
7051 "printer-up-time", time(NULL));
7052
7053 /*
7054 * Copy the subscription event attributes to the response.
7055 */
7056
7057 con->response->request.status.status_code =
7058 interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE;
7059
7060 for (i = 0; i < ids->num_values; i ++)
7061 {
7062 /*
7063 * Get the subscription and sequence number...
7064 */
7065
7066 sub = cupsdFindSubscription(ids->values[i].integer);
7067
7068 if (sequences && i < sequences->num_values)
7069 min_seq = sequences->values[i].integer;
7070 else
7071 min_seq = 1;
7072
7073 /*
7074 * If we don't have any new events, nothing to do here...
7075 */
7076
7077 if (min_seq > (sub->first_event_id + sub->num_events))
7078 continue;
7079
7080 /*
7081 * Otherwise copy all of the new events...
7082 */
7083
7084 if (sub->first_event_id > min_seq)
7085 j = 0;
7086 else
7087 j = min_seq - sub->first_event_id;
7088
7089 for (; j < sub->num_events; j ++)
7090 {
7091 ippAddSeparator(con->response);
7092
7093 copy_attrs(con->response, sub->events[j]->attrs, NULL,
7094 IPP_TAG_EVENT_NOTIFICATION, 0);
7095 }
7096 }
7097 }
7098
7099
7100 /*
7101 * 'get_ppd()' - Get a named PPD from the local system.
7102 */
7103
7104 static void
7105 get_ppd(cupsd_client_t *con, /* I - Client connection */
7106 ipp_attribute_t *uri) /* I - Printer URI or PPD name */
7107 {
7108 http_status_t status; /* Policy status */
7109 cupsd_printer_t *dest; /* Destination */
7110 cups_ptype_t dtype; /* Destination type */
7111
7112
7113 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con,
7114 con->http.fd, uri, uri->name, uri->values[0].string.text);
7115
7116 if (!strcmp(uri->name, "ppd-name"))
7117 {
7118 /*
7119 * Return a PPD file from cups-driverd...
7120 */
7121
7122 char command[1024], /* cups-driverd command */
7123 options[1024], /* Options to pass to command */
7124 ppd_name[1024]; /* ppd-name */
7125
7126
7127 /*
7128 * Check policy...
7129 */
7130
7131 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7132 {
7133 send_http_error(con, status, NULL);
7134 return;
7135 }
7136
7137 /*
7138 * Run cups-driverd command with the given options...
7139 */
7140
7141 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
7142 url_encode_string(uri->values[0].string.text, ppd_name, sizeof(ppd_name));
7143 snprintf(options, sizeof(options), "get+%d+%s",
7144 con->request->request.op.request_id, ppd_name);
7145
7146 if (cupsdSendCommand(con, command, options, 0))
7147 {
7148 /*
7149 * Command started successfully, don't send an IPP response here...
7150 */
7151
7152 ippDelete(con->response);
7153 con->response = NULL;
7154 }
7155 else
7156 {
7157 /*
7158 * Command failed, return "internal error" so the user knows something
7159 * went wrong...
7160 */
7161
7162 send_ipp_status(con, IPP_INTERNAL_ERROR,
7163 _("cups-driverd failed to execute."));
7164 }
7165 }
7166 else if (!strcmp(uri->name, "printer-uri") &&
7167 cupsdValidateDest(uri->values[0].string.text, &dtype, &dest))
7168 {
7169 int i; /* Looping var */
7170 char filename[1024]; /* PPD filename */
7171
7172
7173 /*
7174 * Check policy...
7175 */
7176
7177 if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK)
7178 {
7179 send_http_error(con, status, dest);
7180 return;
7181 }
7182
7183 /*
7184 * See if we need the PPD for a class or remote printer...
7185 */
7186
7187 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
7188 dest->name);
7189
7190 if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0))
7191 {
7192 con->response->request.status.status_code = CUPS_SEE_OTHER;
7193 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI,
7194 "printer-uri", NULL, dest->uri);
7195 return;
7196 }
7197 else if (dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
7198 {
7199 for (i = 0; i < dest->num_printers; i ++)
7200 if (!(dest->printers[i]->type &
7201 (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)))
7202 {
7203 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
7204 dest->printers[i]->name);
7205
7206 if (!access(filename, 0))
7207 break;
7208 }
7209
7210 if (i < dest->num_printers)
7211 dest = dest->printers[i];
7212 else
7213 {
7214 con->response->request.status.status_code = CUPS_SEE_OTHER;
7215 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI,
7216 "printer-uri", NULL, dest->printers[0]->uri);
7217 return;
7218 }
7219 }
7220
7221 /*
7222 * Found the printer with the PPD file, now see if there is one...
7223 */
7224
7225 if ((con->file = open(filename, O_RDONLY)) < 0)
7226 {
7227 send_ipp_status(con, IPP_NOT_FOUND,
7228 _("The PPD file \"%s\" could not be opened: %s"),
7229 uri->values[0].string.text, strerror(errno));
7230 return;
7231 }
7232
7233 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
7234
7235 con->pipe_pid = 0;
7236
7237 con->response->request.status.status_code = IPP_OK;
7238 }
7239 else
7240 send_ipp_status(con, IPP_NOT_FOUND,
7241 _("The PPD file \"%s\" could not be found."),
7242 uri->values[0].string.text);
7243 }
7244
7245
7246 /*
7247 * 'get_ppds()' - Get the list of PPD files on the local system.
7248 */
7249
7250 static void
7251 get_ppds(cupsd_client_t *con) /* I - Client connection */
7252 {
7253 http_status_t status; /* Policy status */
7254 ipp_attribute_t *limit, /* Limit attribute */
7255 *device, /* ppd-device-id attribute */
7256 *language, /* ppd-natural-language attribute */
7257 *make, /* ppd-make attribute */
7258 *model, /* ppd-make-and-model attribute */
7259 *model_number, /* ppd-model-number attribute */
7260 *product, /* ppd-product attribute */
7261 *psversion, /* ppd-psverion attribute */
7262 *type, /* ppd-type attribute */
7263 *requested; /* requested-attributes attribute */
7264 char command[1024], /* cups-driverd command */
7265 options[1024], /* Options to pass to command */
7266 device_str[256],/* Escaped ppd-device-id string */
7267 language_str[256],
7268 /* Escaped ppd-natural-language */
7269 make_str[256], /* Escaped ppd-make string */
7270 model_str[256], /* Escaped ppd-make-and-model string */
7271 model_number_str[256],
7272 /* ppd-model-number string */
7273 product_str[256],
7274 /* Escaped ppd-product string */
7275 psversion_str[256],
7276 /* Escaped ppd-psversion string */
7277 type_str[256], /* Escaped ppd-type string */
7278 requested_str[256];
7279 /* String for requested attributes */
7280
7281
7282 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->http.fd);
7283
7284 /*
7285 * Check policy...
7286 */
7287
7288 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7289 {
7290 send_http_error(con, status, NULL);
7291 return;
7292 }
7293
7294 /*
7295 * Run cups-driverd command with the given options...
7296 */
7297
7298 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
7299 device = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT);
7300 language = ippFindAttribute(con->request, "ppd-natural-language",
7301 IPP_TAG_LANGUAGE);
7302 make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT);
7303 model = ippFindAttribute(con->request, "ppd-make-and-model",
7304 IPP_TAG_TEXT);
7305 model_number = ippFindAttribute(con->request, "ppd-model-number",
7306 IPP_TAG_INTEGER);
7307 product = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT);
7308 psversion = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT);
7309 type = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD);
7310 requested = ippFindAttribute(con->request, "requested-attributes",
7311 IPP_TAG_KEYWORD);
7312
7313 if (requested)
7314 url_encode_attr(requested, requested_str, sizeof(requested_str));
7315 else
7316 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
7317
7318 if (device)
7319 url_encode_attr(device, device_str, sizeof(device_str));
7320 else
7321 device_str[0] = '\0';
7322
7323 if (language)
7324 url_encode_attr(language, language_str, sizeof(language_str));
7325 else
7326 language_str[0] = '\0';
7327
7328 if (make)
7329 url_encode_attr(make, make_str, sizeof(make_str));
7330 else
7331 make_str[0] = '\0';
7332
7333 if (model)
7334 url_encode_attr(model, model_str, sizeof(model_str));
7335 else
7336 model_str[0] = '\0';
7337
7338 if (model_number)
7339 snprintf(model_number_str, sizeof(model_number_str), "ppd-model-number=%d",
7340 model_number->values[0].integer);
7341 else
7342 model_number_str[0] = '\0';
7343
7344 if (product)
7345 url_encode_attr(product, product_str, sizeof(product_str));
7346 else
7347 product_str[0] = '\0';
7348
7349 if (psversion)
7350 url_encode_attr(psversion, psversion_str, sizeof(psversion_str));
7351 else
7352 psversion_str[0] = '\0';
7353
7354 if (type)
7355 url_encode_attr(type, type_str, sizeof(type_str));
7356 else
7357 type_str[0] = '\0';
7358
7359 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
7360 snprintf(options, sizeof(options),
7361 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7362 con->request->request.op.request_id,
7363 limit ? limit->values[0].integer : 0,
7364 requested_str,
7365 device ? "%20" : "", device_str,
7366 language ? "%20" : "", language_str,
7367 make ? "%20" : "", make_str,
7368 model ? "%20" : "", model_str,
7369 model_number ? "%20" : "", model_number_str,
7370 product ? "%20" : "", product_str,
7371 psversion ? "%20" : "", psversion_str,
7372 type ? "%20" : "", type_str);
7373
7374 if (cupsdSendCommand(con, command, options, 0))
7375 {
7376 /*
7377 * Command started successfully, don't send an IPP response here...
7378 */
7379
7380 ippDelete(con->response);
7381 con->response = NULL;
7382 }
7383 else
7384 {
7385 /*
7386 * Command failed, return "internal error" so the user knows something
7387 * went wrong...
7388 */
7389
7390 send_ipp_status(con, IPP_INTERNAL_ERROR,
7391 _("cups-driverd failed to execute."));
7392 }
7393 }
7394
7395
7396 /*
7397 * 'get_printer_attrs()' - Get printer attributes.
7398 */
7399
7400 static void
7401 get_printer_attrs(cupsd_client_t *con, /* I - Client connection */
7402 ipp_attribute_t *uri) /* I - Printer URI */
7403 {
7404 http_status_t status; /* Policy status */
7405 cups_ptype_t dtype; /* Destination type (printer/class) */
7406 cupsd_printer_t *printer; /* Printer/class */
7407 cups_array_t *ra; /* Requested attributes array */
7408
7409
7410 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con,
7411 con->http.fd, uri->values[0].string.text);
7412
7413 /*
7414 * Is the destination valid?
7415 */
7416
7417 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7418 {
7419 /*
7420 * Bad URI...
7421 */
7422
7423 send_ipp_status(con, IPP_NOT_FOUND,
7424 _("The printer or class was not found."));
7425 return;
7426 }
7427
7428 /*
7429 * Check policy...
7430 */
7431
7432 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
7433 {
7434 send_http_error(con, status, printer);
7435 return;
7436 }
7437
7438 /*
7439 * Send the attributes...
7440 */
7441
7442 ra = create_requested_array(con->request);
7443
7444 copy_printer_attrs(con, printer, ra);
7445
7446 cupsArrayDelete(ra);
7447
7448 con->response->request.status.status_code = IPP_OK;
7449 }
7450
7451
7452 /*
7453 * 'get_printers()' - Get a list of printers or classes.
7454 */
7455
7456 static void
7457 get_printers(cupsd_client_t *con, /* I - Client connection */
7458 int type) /* I - 0 or CUPS_PRINTER_CLASS */
7459 {
7460 http_status_t status; /* Policy status */
7461 ipp_attribute_t *attr; /* Current attribute */
7462 int limit; /* Max number of printers to return */
7463 int count; /* Number of printers that match */
7464 cupsd_printer_t *printer; /* Current printer pointer */
7465 int printer_type, /* printer-type attribute */
7466 printer_mask; /* printer-type-mask attribute */
7467 char *location; /* Location string */
7468 const char *username; /* Current user */
7469 char *first_printer_name; /* first-printer-name attribute */
7470 cups_array_t *ra; /* Requested attributes array */
7471
7472
7473 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con,
7474 con->http.fd, type);
7475
7476 /*
7477 * Check policy...
7478 */
7479
7480 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7481 {
7482 send_http_error(con, status, NULL);
7483 return;
7484 }
7485
7486 /*
7487 * Check for printers...
7488 */
7489
7490 if (!Printers || !cupsArrayCount(Printers))
7491 {
7492 send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added."));
7493 return;
7494 }
7495
7496 /*
7497 * See if they want to limit the number of printers reported...
7498 */
7499
7500 if ((attr = ippFindAttribute(con->request, "limit",
7501 IPP_TAG_INTEGER)) != NULL)
7502 limit = attr->values[0].integer;
7503 else
7504 limit = 10000000;
7505
7506 if ((attr = ippFindAttribute(con->request, "first-printer-name",
7507 IPP_TAG_NAME)) != NULL)
7508 first_printer_name = attr->values[0].string.text;
7509 else
7510 first_printer_name = NULL;
7511
7512 /*
7513 * Support filtering...
7514 */
7515
7516 if ((attr = ippFindAttribute(con->request, "printer-type",
7517 IPP_TAG_ENUM)) != NULL)
7518 printer_type = attr->values[0].integer;
7519 else
7520 printer_type = 0;
7521
7522 if ((attr = ippFindAttribute(con->request, "printer-type-mask",
7523 IPP_TAG_ENUM)) != NULL)
7524 printer_mask = attr->values[0].integer;
7525 else
7526 printer_mask = 0;
7527
7528 if ((attr = ippFindAttribute(con->request, "printer-location",
7529 IPP_TAG_TEXT)) != NULL)
7530 location = attr->values[0].string.text;
7531 else
7532 location = NULL;
7533
7534 if (con->username[0])
7535 username = con->username;
7536 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7537 IPP_TAG_NAME)) != NULL)
7538 username = attr->values[0].string.text;
7539 else
7540 username = NULL;
7541
7542 ra = create_requested_array(con->request);
7543
7544 /*
7545 * OK, build a list of printers for this printer...
7546 */
7547
7548 if (first_printer_name)
7549 {
7550 if ((printer = cupsdFindDest(first_printer_name)) == NULL)
7551 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
7552 }
7553 else
7554 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
7555
7556 for (count = 0;
7557 count < limit && printer;
7558 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
7559 {
7560 if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) &&
7561 (printer->type & printer_mask) == printer_type &&
7562 (!location ||
7563 (printer->location && !strcasecmp(printer->location, location))))
7564 {
7565 /*
7566 * If HideImplicitMembers is enabled, see if this printer or class
7567 * is a member of an implicit class...
7568 */
7569
7570 if (ImplicitClasses && HideImplicitMembers &&
7571 printer->in_implicit_class)
7572 continue;
7573
7574 /*
7575 * If a username is specified, see if it is allowed or denied
7576 * access...
7577 */
7578
7579 if (!(printer->type & CUPS_PRINTER_AUTHENTICATED) &&
7580 printer->num_users && username && !user_allowed(printer, username))
7581 continue;
7582
7583 /*
7584 * Add the group separator as needed...
7585 */
7586
7587 if (count > 0)
7588 ippAddSeparator(con->response);
7589
7590 count ++;
7591
7592 /*
7593 * Send the attributes...
7594 */
7595
7596 copy_printer_attrs(con, printer, ra);
7597 }
7598 }
7599
7600 cupsArrayDelete(ra);
7601
7602 con->response->request.status.status_code = IPP_OK;
7603 }
7604
7605
7606 /*
7607 * 'get_subscription_attrs()' - Get subscription attributes.
7608 */
7609
7610 static void
7611 get_subscription_attrs(
7612 cupsd_client_t *con, /* I - Client connection */
7613 int sub_id) /* I - Subscription ID */
7614 {
7615 http_status_t status; /* Policy status */
7616 cupsd_subscription_t *sub; /* Subscription */
7617 cups_array_t *ra; /* Requested attributes array */
7618
7619
7620 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7621 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7622 con, con->http.fd, sub_id);
7623
7624 /*
7625 * Is the subscription ID valid?
7626 */
7627
7628 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
7629 {
7630 /*
7631 * Bad subscription ID...
7632 */
7633
7634 send_ipp_status(con, IPP_NOT_FOUND,
7635 _("notify-subscription-id %d no good!"), sub_id);
7636 return;
7637 }
7638
7639 /*
7640 * Check policy...
7641 */
7642
7643 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
7644 DefaultPolicyPtr,
7645 con, sub->owner)) != HTTP_OK)
7646 {
7647 send_http_error(con, status, sub->dest);
7648 return;
7649 }
7650
7651 /*
7652 * Copy the subscription attributes to the response using the
7653 * requested-attributes attribute that may be provided by the client.
7654 */
7655
7656 ra = create_requested_array(con->request);
7657
7658 copy_subscription_attrs(con, sub, ra);
7659
7660 cupsArrayDelete(ra);
7661
7662 con->response->request.status.status_code = IPP_OK;
7663 }
7664
7665
7666 /*
7667 * 'get_subscriptions()' - Get subscriptions.
7668 */
7669
7670 static void
7671 get_subscriptions(cupsd_client_t *con, /* I - Client connection */
7672 ipp_attribute_t *uri) /* I - Printer/job URI */
7673 {
7674 http_status_t status; /* Policy status */
7675 int count; /* Number of subscriptions */
7676 int limit; /* Limit */
7677 cupsd_subscription_t *sub; /* Subscription */
7678 cups_array_t *ra; /* Requested attributes array */
7679 ipp_attribute_t *attr; /* Attribute */
7680 cups_ptype_t dtype; /* Destination type (printer/class) */
7681 char scheme[HTTP_MAX_URI],
7682 /* Scheme portion of URI */
7683 username[HTTP_MAX_URI],
7684 /* Username portion of URI */
7685 host[HTTP_MAX_URI],
7686 /* Host portion of URI */
7687 resource[HTTP_MAX_URI];
7688 /* Resource portion of URI */
7689 int port; /* Port portion of URI */
7690 cupsd_job_t *job; /* Job pointer */
7691 cupsd_printer_t *printer; /* Printer */
7692
7693
7694 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7695 "get_subscriptions(con=%p[%d], uri=%s)",
7696 con, con->http.fd, uri->values[0].string.text);
7697
7698 /*
7699 * Is the destination valid?
7700 */
7701
7702 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7703 sizeof(scheme), username, sizeof(username), host,
7704 sizeof(host), &port, resource, sizeof(resource));
7705
7706 if (!strcmp(resource, "/") ||
7707 (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) ||
7708 (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) ||
7709 (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9))
7710 {
7711 printer = NULL;
7712 job = NULL;
7713 }
7714 else if (!strncmp(resource, "/jobs/", 6) && resource[6])
7715 {
7716 printer = NULL;
7717 job = cupsdFindJob(atoi(resource + 6));
7718
7719 if (!job)
7720 {
7721 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%s does not exist!"),
7722 resource + 6);
7723 return;
7724 }
7725 }
7726 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7727 {
7728 /*
7729 * Bad URI...
7730 */
7731
7732 send_ipp_status(con, IPP_NOT_FOUND,
7733 _("The printer or class was not found."));
7734 return;
7735 }
7736 else if ((attr = ippFindAttribute(con->request, "notify-job-id",
7737 IPP_TAG_INTEGER)) != NULL)
7738 {
7739 job = cupsdFindJob(attr->values[0].integer);
7740
7741 if (!job)
7742 {
7743 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"),
7744 attr->values[0].integer);
7745 return;
7746 }
7747 }
7748 else
7749 job = NULL;
7750
7751 /*
7752 * Check policy...
7753 */
7754
7755 if ((status = cupsdCheckPolicy(printer ? printer->op_policy_ptr :
7756 DefaultPolicyPtr,
7757 con, NULL)) != HTTP_OK)
7758 {
7759 send_http_error(con, status, printer);
7760 return;
7761 }
7762
7763 /*
7764 * Copy the subscription attributes to the response using the
7765 * requested-attributes attribute that may be provided by the client.
7766 */
7767
7768 ra = create_requested_array(con->request);
7769
7770 if ((attr = ippFindAttribute(con->request, "limit",
7771 IPP_TAG_INTEGER)) != NULL)
7772 limit = attr->values[0].integer;
7773 else
7774 limit = 0;
7775
7776 /*
7777 * See if we only want to see subscriptions for a specific user...
7778 */
7779
7780 if ((attr = ippFindAttribute(con->request, "my-subscriptions",
7781 IPP_TAG_BOOLEAN)) != NULL &&
7782 attr->values[0].boolean)
7783 strlcpy(username, get_username(con), sizeof(username));
7784 else
7785 username[0] = '\0';
7786
7787 for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0;
7788 sub;
7789 sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
7790 if ((!printer || sub->dest == printer) && (!job || sub->job == job) &&
7791 (!username[0] || !strcasecmp(username, sub->owner)))
7792 {
7793 ippAddSeparator(con->response);
7794 copy_subscription_attrs(con, sub, ra);
7795
7796 count ++;
7797 if (limit && count >= limit)
7798 break;
7799 }
7800
7801 cupsArrayDelete(ra);
7802
7803 if (count)
7804 con->response->request.status.status_code = IPP_OK;
7805 else
7806 send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found."));
7807 }
7808
7809
7810 /*
7811 * 'get_username()' - Get the username associated with a request.
7812 */
7813
7814 static const char * /* O - Username */
7815 get_username(cupsd_client_t *con) /* I - Connection */
7816 {
7817 ipp_attribute_t *attr; /* Attribute */
7818
7819
7820 if (con->username[0])
7821 return (con->username);
7822 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7823 IPP_TAG_NAME)) != NULL)
7824 return (attr->values[0].string.text);
7825 else
7826 return ("anonymous");
7827 }
7828
7829
7830 /*
7831 * 'hold_job()' - Hold a print job.
7832 */
7833
7834 static void
7835 hold_job(cupsd_client_t *con, /* I - Client connection */
7836 ipp_attribute_t *uri) /* I - Job or Printer URI */
7837 {
7838 ipp_attribute_t *attr, /* Current job-hold-until */
7839 *newattr; /* New job-hold-until */
7840 int jobid; /* Job ID */
7841 char scheme[HTTP_MAX_URI], /* Method portion of URI */
7842 username[HTTP_MAX_URI], /* Username portion of URI */
7843 host[HTTP_MAX_URI], /* Host portion of URI */
7844 resource[HTTP_MAX_URI]; /* Resource portion of URI */
7845 int port; /* Port portion of URI */
7846 cupsd_job_t *job; /* Job information */
7847
7848
7849 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->http.fd,
7850 uri->values[0].string.text);
7851
7852 /*
7853 * See if we have a job URI or a printer URI...
7854 */
7855
7856 if (!strcmp(uri->name, "printer-uri"))
7857 {
7858 /*
7859 * Got a printer URI; see if we also have a job-id attribute...
7860 */
7861
7862 if ((attr = ippFindAttribute(con->request, "job-id",
7863 IPP_TAG_INTEGER)) == NULL)
7864 {
7865 send_ipp_status(con, IPP_BAD_REQUEST,
7866 _("Got a printer-uri attribute but no job-id!"));
7867 return;
7868 }
7869
7870 jobid = attr->values[0].integer;
7871 }
7872 else
7873 {
7874 /*
7875 * Got a job URI; parse it to get the job ID...
7876 */
7877
7878 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7879 sizeof(scheme), username, sizeof(username), host,
7880 sizeof(host), &port, resource, sizeof(resource));
7881
7882 if (strncmp(resource, "/jobs/", 6))
7883 {
7884 /*
7885 * Not a valid URI!
7886 */
7887
7888 send_ipp_status(con, IPP_BAD_REQUEST,
7889 _("Bad job-uri attribute \"%s\"!"),
7890 uri->values[0].string.text);
7891 return;
7892 }
7893
7894 jobid = atoi(resource + 6);
7895 }
7896
7897 /*
7898 * See if the job exists...
7899 */
7900
7901 if ((job = cupsdFindJob(jobid)) == NULL)
7902 {
7903 /*
7904 * Nope - return a "not found" error...
7905 */
7906
7907 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
7908 return;
7909 }
7910
7911 /*
7912 * See if the job is owned by the requesting user...
7913 */
7914
7915 if (!validate_user(job, con, job->username, username, sizeof(username)))
7916 {
7917 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
7918 return;
7919 }
7920
7921 /*
7922 * Hold the job and return...
7923 */
7924
7925 cupsdHoldJob(job);
7926
7927 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
7928 "Job held by user.");
7929
7930 if ((newattr = ippFindAttribute(con->request, "job-hold-until",
7931 IPP_TAG_KEYWORD)) == NULL)
7932 newattr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
7933
7934 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
7935 IPP_TAG_KEYWORD)) == NULL)
7936 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
7937
7938 if (attr)
7939 {
7940 /*
7941 * Free the old hold value and copy the new one over...
7942 */
7943
7944 _cupsStrFree(attr->values[0].string.text);
7945
7946 if (newattr)
7947 {
7948 attr->value_tag = newattr->value_tag;
7949 attr->values[0].string.text =
7950 _cupsStrAlloc(newattr->values[0].string.text);
7951 }
7952 else
7953 {
7954 attr->value_tag = IPP_TAG_KEYWORD;
7955 attr->values[0].string.text = _cupsStrAlloc("indefinite");
7956 }
7957
7958 /*
7959 * Hold job until specified time...
7960 */
7961
7962 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
7963
7964 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
7965 "Job job-hold-until value changed by user.");
7966 }
7967
7968 cupsdLogJob(job, CUPSD_LOG_INFO, "Held by \"%s\".", username);
7969
7970 con->response->request.status.status_code = IPP_OK;
7971 }
7972
7973
7974 /*
7975 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
7976 */
7977
7978 static void
7979 hold_new_jobs(cupsd_client_t *con, /* I - Connection */
7980 ipp_attribute_t *uri) /* I - Printer URI */
7981 {
7982 http_status_t status; /* Policy status */
7983 cups_ptype_t dtype; /* Destination type (printer/class) */
7984 cupsd_printer_t *printer; /* Printer data */
7985
7986
7987 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_new_jobs(%p[%d], %s)", con,
7988 con->http.fd, uri->values[0].string.text);
7989
7990 /*
7991 * Is the destination valid?
7992 */
7993
7994 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7995 {
7996 /*
7997 * Bad URI...
7998 */
7999
8000 send_ipp_status(con, IPP_NOT_FOUND,
8001 _("The printer or class was not found."));
8002 return;
8003 }
8004
8005 /*
8006 * Check policy...
8007 */
8008
8009 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8010 {
8011 send_http_error(con, status, printer);
8012 return;
8013 }
8014
8015 /*
8016 * Hold pending/new jobs sent to the printer...
8017 */
8018
8019 printer->holding_new_jobs = 1;
8020
8021 cupsdSetPrinterReasons(printer, "+hold-new-jobs");
8022 cupsdAddPrinterHistory(printer);
8023
8024 if (dtype & CUPS_PRINTER_CLASS)
8025 cupsdLogMessage(CUPSD_LOG_INFO,
8026 "Class \"%s\" now holding pending/new jobs (\"%s\").",
8027 printer->name, get_username(con));
8028 else
8029 cupsdLogMessage(CUPSD_LOG_INFO,
8030 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
8031 printer->name, get_username(con));
8032
8033 /*
8034 * Everything was ok, so return OK status...
8035 */
8036
8037 con->response->request.status.status_code = IPP_OK;
8038 }
8039
8040
8041 /*
8042 * 'move_job()' - Move a job to a new destination.
8043 */
8044
8045 static void
8046 move_job(cupsd_client_t *con, /* I - Client connection */
8047 ipp_attribute_t *uri) /* I - Job URI */
8048 {
8049 http_status_t status; /* Policy status */
8050 ipp_attribute_t *attr; /* Current attribute */
8051 int jobid; /* Job ID */
8052 cupsd_job_t *job; /* Current job */
8053 const char *src; /* Source printer/class */
8054 cups_ptype_t stype, /* Source type (printer or class) */
8055 dtype; /* Destination type (printer/class) */
8056 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
8057 username[HTTP_MAX_URI], /* Username portion of URI */
8058 host[HTTP_MAX_URI], /* Host portion of URI */
8059 resource[HTTP_MAX_URI]; /* Resource portion of URI */
8060 int port; /* Port portion of URI */
8061 cupsd_printer_t *sprinter, /* Source printer */
8062 *dprinter; /* Destination printer */
8063
8064
8065 cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd,
8066 uri->values[0].string.text);
8067
8068 /*
8069 * Get the new printer or class...
8070 */
8071
8072 if ((attr = ippFindAttribute(con->request, "job-printer-uri",
8073 IPP_TAG_URI)) == NULL)
8074 {
8075 /*
8076 * Need job-printer-uri...
8077 */
8078
8079 send_ipp_status(con, IPP_BAD_REQUEST,
8080 _("job-printer-uri attribute missing!"));
8081 return;
8082 }
8083
8084 if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter))
8085 {
8086 /*
8087 * Bad URI...
8088 */
8089
8090 send_ipp_status(con, IPP_NOT_FOUND,
8091 _("The printer or class was not found."));
8092 return;
8093 }
8094
8095 /*
8096 * See if we have a job URI or a printer URI...
8097 */
8098
8099 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
8100 sizeof(scheme), username, sizeof(username), host,
8101 sizeof(host), &port, resource, sizeof(resource));
8102
8103 if (!strcmp(uri->name, "printer-uri"))
8104 {
8105 /*
8106 * Got a printer URI; see if we also have a job-id attribute...
8107 */
8108
8109 if ((attr = ippFindAttribute(con->request, "job-id",
8110 IPP_TAG_INTEGER)) == NULL)
8111 {
8112 /*
8113 * Move all jobs...
8114 */
8115
8116 if ((src = cupsdValidateDest(uri->values[0].string.text, &stype,
8117 &sprinter)) == NULL)
8118 {
8119 /*
8120 * Bad URI...
8121 */
8122
8123 send_ipp_status(con, IPP_NOT_FOUND,
8124 _("The printer or class was not found."));
8125 return;
8126 }
8127
8128 job = NULL;
8129 }
8130 else
8131 {
8132 /*
8133 * Otherwise, just move a single job...
8134 */
8135
8136 if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
8137 {
8138 /*
8139 * Nope - return a "not found" error...
8140 */
8141
8142 send_ipp_status(con, IPP_NOT_FOUND,
8143 _("Job #%d does not exist!"), attr->values[0].integer);
8144 return;
8145 }
8146 else
8147 {
8148 /*
8149 * Job found, initialize source pointers...
8150 */
8151
8152 src = NULL;
8153 sprinter = NULL;
8154 }
8155 }
8156 }
8157 else
8158 {
8159 /*
8160 * Got a job URI; parse it to get the job ID...
8161 */
8162
8163 if (strncmp(resource, "/jobs/", 6))
8164 {
8165 /*
8166 * Not a valid URI!
8167 */
8168
8169 send_ipp_status(con, IPP_BAD_REQUEST,
8170 _("Bad job-uri attribute \"%s\"!"),
8171 uri->values[0].string.text);
8172 return;
8173 }
8174
8175 /*
8176 * See if the job exists...
8177 */
8178
8179 jobid = atoi(resource + 6);
8180
8181 if ((job = cupsdFindJob(jobid)) == NULL)
8182 {
8183 /*
8184 * Nope - return a "not found" error...
8185 */
8186
8187 send_ipp_status(con, IPP_NOT_FOUND,
8188 _("Job #%d does not exist!"), jobid);
8189 return;
8190 }
8191 else
8192 {
8193 /*
8194 * Job found, initialize source pointers...
8195 */
8196
8197 src = NULL;
8198 sprinter = NULL;
8199 }
8200 }
8201
8202 /*
8203 * Check the policy of the destination printer...
8204 */
8205
8206 if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con,
8207 job ? job->username : NULL)) != HTTP_OK)
8208 {
8209 send_http_error(con, status, dprinter);
8210 return;
8211 }
8212
8213 /*
8214 * Now move the job or jobs...
8215 */
8216
8217 if (job)
8218 {
8219 /*
8220 * See if the job has been completed...
8221 */
8222
8223 if (job->state_value > IPP_JOB_STOPPED)
8224 {
8225 /*
8226 * Return a "not-possible" error...
8227 */
8228
8229 send_ipp_status(con, IPP_NOT_POSSIBLE,
8230 _("Job #%d is finished and cannot be altered!"),
8231 job->id);
8232 return;
8233 }
8234
8235 /*
8236 * See if the job is owned by the requesting user...
8237 */
8238
8239 if (!validate_user(job, con, job->username, username, sizeof(username)))
8240 {
8241 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
8242 return;
8243 }
8244
8245 /*
8246 * Move the job to a different printer or class...
8247 */
8248
8249 cupsdMoveJob(job, dprinter);
8250 }
8251 else
8252 {
8253 /*
8254 * Got the source printer, now look through the jobs...
8255 */
8256
8257 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
8258 job;
8259 job = (cupsd_job_t *)cupsArrayNext(Jobs))
8260 {
8261 /*
8262 * See if the job is pointing at the source printer or has not been
8263 * completed...
8264 */
8265
8266 if (strcasecmp(job->dest, src) ||
8267 job->state_value > IPP_JOB_STOPPED)
8268 continue;
8269
8270 /*
8271 * See if the job can be moved by the requesting user...
8272 */
8273
8274 if (!validate_user(job, con, job->username, username, sizeof(username)))
8275 continue;
8276
8277 /*
8278 * Move the job to a different printer or class...
8279 */
8280
8281 cupsdMoveJob(job, dprinter);
8282 }
8283 }
8284
8285 /*
8286 * Start jobs if possible...
8287 */
8288
8289 cupsdCheckJobs();
8290
8291 /*
8292 * Return with "everything is OK" status...
8293 */
8294
8295 con->response->request.status.status_code = IPP_OK;
8296 }
8297
8298
8299 /*
8300 * 'ppd_parse_line()' - Parse a PPD default line.
8301 */
8302
8303 static int /* O - 0 on success, -1 on failure */
8304 ppd_parse_line(const char *line, /* I - Line */
8305 char *option, /* O - Option name */
8306 int olen, /* I - Size of option name */
8307 char *choice, /* O - Choice name */
8308 int clen) /* I - Size of choice name */
8309 {
8310 /*
8311 * Verify this is a default option line...
8312 */
8313
8314 if (strncmp(line, "*Default", 8))
8315 return (-1);
8316
8317 /*
8318 * Read the option name...
8319 */
8320
8321 for (line += 8, olen --; isalnum(*line & 255); line ++)
8322 if (olen > 0)
8323 {
8324 *option++ = *line;
8325 olen --;
8326 }
8327
8328 *option = '\0';
8329
8330 /*
8331 * Skip everything else up to the colon (:)...
8332 */
8333
8334 while (*line && *line != ':')
8335 line ++;
8336
8337 if (!*line)
8338 return (-1);
8339
8340 line ++;
8341
8342 /*
8343 * Now grab the option choice, skipping leading whitespace...
8344 */
8345
8346 while (isspace(*line & 255))
8347 line ++;
8348
8349 for (clen --; isalnum(*line & 255); line ++)
8350 if (clen > 0)
8351 {
8352 *choice++ = *line;
8353 clen --;
8354 }
8355
8356 *choice = '\0';
8357
8358 /*
8359 * Return with no errors...
8360 */
8361
8362 return (0);
8363 }
8364
8365
8366 /*
8367 * 'print_job()' - Print a file to a printer or class.
8368 */
8369
8370 static void
8371 print_job(cupsd_client_t *con, /* I - Client connection */
8372 ipp_attribute_t *uri) /* I - Printer URI */
8373 {
8374 ipp_attribute_t *attr; /* Current attribute */
8375 ipp_attribute_t *format; /* Document-format attribute */
8376 const char *default_format; /* document-format-default value */
8377 cupsd_job_t *job; /* New job */
8378 char filename[1024]; /* Job filename */
8379 mime_type_t *filetype; /* Type of file */
8380 char super[MIME_MAX_SUPER], /* Supertype of file */
8381 type[MIME_MAX_TYPE], /* Subtype of file */
8382 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
8383 /* Textual name of mime type */
8384 cupsd_printer_t *printer; /* Printer data */
8385 struct stat fileinfo; /* File information */
8386 int kbytes; /* Size of file */
8387 int compression; /* Document compression */
8388
8389
8390 cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd,
8391 uri->values[0].string.text);
8392
8393 /*
8394 * Validate print file attributes, for now just document-format and
8395 * compression (CUPS only supports "none" and "gzip")...
8396 */
8397
8398 compression = CUPS_FILE_NONE;
8399
8400 if ((attr = ippFindAttribute(con->request, "compression",
8401 IPP_TAG_KEYWORD)) != NULL)
8402 {
8403 if (strcmp(attr->values[0].string.text, "none")
8404 #ifdef HAVE_LIBZ
8405 && strcmp(attr->values[0].string.text, "gzip")
8406 #endif /* HAVE_LIBZ */
8407 )
8408 {
8409 send_ipp_status(con, IPP_ATTRIBUTES,
8410 _("Unsupported compression \"%s\"!"),
8411 attr->values[0].string.text);
8412 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
8413 "compression", NULL, attr->values[0].string.text);
8414 return;
8415 }
8416
8417 #ifdef HAVE_LIBZ
8418 if (!strcmp(attr->values[0].string.text, "gzip"))
8419 compression = CUPS_FILE_GZIP;
8420 #endif /* HAVE_LIBZ */
8421 }
8422
8423 /*
8424 * Do we have a file to print?
8425 */
8426
8427 if (!con->filename)
8428 {
8429 send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!"));
8430 return;
8431 }
8432
8433 /*
8434 * Is the destination valid?
8435 */
8436
8437 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
8438 {
8439 /*
8440 * Bad URI...
8441 */
8442
8443 send_ipp_status(con, IPP_NOT_FOUND,
8444 _("The printer or class was not found."));
8445 return;
8446 }
8447
8448 /*
8449 * Is it a format we support?
8450 */
8451
8452 if ((format = ippFindAttribute(con->request, "document-format",
8453 IPP_TAG_MIMETYPE)) != NULL)
8454 {
8455 /*
8456 * Grab format from client...
8457 */
8458
8459 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super,
8460 type) != 2)
8461 {
8462 send_ipp_status(con, IPP_BAD_REQUEST,
8463 _("Could not scan type \"%s\"!"),
8464 format->values[0].string.text);
8465 return;
8466 }
8467 }
8468 else if ((default_format = cupsGetOption("document-format",
8469 printer->num_options,
8470 printer->options)) != NULL)
8471 {
8472 /*
8473 * Use default document format...
8474 */
8475
8476 if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
8477 {
8478 send_ipp_status(con, IPP_BAD_REQUEST,
8479 _("Could not scan type \"%s\"!"),
8480 default_format);
8481 return;
8482 }
8483 }
8484 else
8485 {
8486 /*
8487 * Auto-type it!
8488 */
8489
8490 strcpy(super, "application");
8491 strcpy(type, "octet-stream");
8492 }
8493
8494 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
8495 {
8496 /*
8497 * Auto-type the file...
8498 */
8499
8500 ipp_attribute_t *doc_name; /* document-name attribute */
8501
8502
8503 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file...");
8504
8505 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
8506 filetype = mimeFileType(MimeDatabase, con->filename,
8507 doc_name ? doc_name->values[0].string.text : NULL,
8508 &compression);
8509
8510 if (!filetype)
8511 filetype = mimeType(MimeDatabase, super, type);
8512
8513 cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.",
8514 filetype->super, filetype->type);
8515 }
8516 else
8517 filetype = mimeType(MimeDatabase, super, type);
8518
8519 if (filetype &&
8520 (!format ||
8521 (!strcmp(super, "application") && !strcmp(type, "octet-stream"))))
8522 {
8523 /*
8524 * Replace the document-format attribute value with the auto-typed or
8525 * default one.
8526 */
8527
8528 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
8529 filetype->type);
8530
8531 if (format)
8532 {
8533 _cupsStrFree(format->values[0].string.text);
8534
8535 format->values[0].string.text = _cupsStrAlloc(mimetype);
8536 }
8537 else
8538 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
8539 "document-format", NULL, mimetype);
8540 }
8541 else if (!filetype)
8542 {
8543 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
8544 _("Unsupported format \'%s/%s\'!"), super, type);
8545 cupsdLogMessage(CUPSD_LOG_INFO,
8546 "Hint: Do you have the raw file printing rules enabled?");
8547
8548 if (format)
8549 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
8550 "document-format", NULL, format->values[0].string.text);
8551
8552 return;
8553 }
8554
8555 /*
8556 * Read any embedded job ticket info from PS files...
8557 */
8558
8559 if (!strcasecmp(filetype->super, "application") &&
8560 (!strcasecmp(filetype->type, "postscript") ||
8561 !strcasecmp(filetype->type, "pdf")))
8562 read_job_ticket(con);
8563
8564 /*
8565 * Create the job object...
8566 */
8567
8568 if ((job = add_job(con, printer, filetype)) == NULL)
8569 return;
8570
8571 /*
8572 * Update quota data...
8573 */
8574
8575 if (stat(con->filename, &fileinfo))
8576 kbytes = 0;
8577 else
8578 kbytes = (fileinfo.st_size + 1023) / 1024;
8579
8580 cupsdUpdateQuota(printer, job->username, 0, kbytes);
8581
8582 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
8583 IPP_TAG_INTEGER)) != NULL)
8584 attr->values[0].integer += kbytes;
8585
8586 /*
8587 * Add the job file...
8588 */
8589
8590 if (add_file(con, job, filetype, compression))
8591 return;
8592
8593 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
8594 job->num_files);
8595 rename(con->filename, filename);
8596 cupsdClearString(&con->filename);
8597
8598 /*
8599 * See if we need to add the ending sheet...
8600 */
8601
8602 if (cupsdTimeoutJob(job))
8603 return;
8604
8605 /*
8606 * Log and save the job...
8607 */
8608
8609 cupsdLogJob(job, CUPSD_LOG_INFO,
8610 "File of type %s/%s queued by \"%s\".",
8611 filetype->super, filetype->type, job->username);
8612 cupsdLogJob(job, CUPSD_LOG_DEBUG, "hold_until=%d", (int)job->hold_until);
8613 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
8614 job->dest, job->username);
8615
8616 /*
8617 * Start the job if possible...
8618 */
8619
8620 cupsdCheckJobs();
8621 }
8622
8623
8624 /*
8625 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8626 *
8627 * This function only gets called when printing a single PDF or PostScript
8628 * file using the Print-Job operation. It doesn't work for Create-Job +
8629 * Send-File, since the job attributes need to be set at job creation
8630 * time for banners to work. The embedded job ticket stuff is here
8631 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8632 * options and IPP attributes which otherwise would be lost.
8633 *
8634 * The format of a job ticket is simple:
8635 *
8636 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8637 *
8638 * %cupsJobTicket: attr1=value1
8639 * %cupsJobTicket: attr2=value2
8640 * ...
8641 * %cupsJobTicket: attrN=valueN
8642 *
8643 * Job ticket lines must appear immediately after the first line that
8644 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8645 * stops looking for job ticket info when it finds a line that does not begin
8646 * with "%cupsJobTicket:".
8647 *
8648 * The maximum length of a job ticket line, including the prefix, is
8649 * 255 characters to conform with the Adobe DSC.
8650 *
8651 * Read-only attributes are rejected with a notice to the error log in
8652 * case a malicious user tries anything. Since the job ticket is read
8653 * prior to attribute validation in print_job(), job ticket attributes
8654 * will go through the same validation as IPP attributes...
8655 */
8656
8657 static void
8658 read_job_ticket(cupsd_client_t *con) /* I - Client connection */
8659 {
8660 cups_file_t *fp; /* File to read from */
8661 char line[256]; /* Line data */
8662 int num_options; /* Number of options */
8663 cups_option_t *options; /* Options */
8664 ipp_t *ticket; /* New attributes */
8665 ipp_attribute_t *attr, /* Current attribute */
8666 *attr2, /* Job attribute */
8667 *prev2; /* Previous job attribute */
8668
8669
8670 /*
8671 * First open the print file...
8672 */
8673
8674 if ((fp = cupsFileOpen(con->filename, "rb")) == NULL)
8675 {
8676 cupsdLogMessage(CUPSD_LOG_ERROR,
8677 "Unable to open print file for job ticket - %s",
8678 strerror(errno));
8679 return;
8680 }
8681
8682 /*
8683 * Skip the first line...
8684 */
8685
8686 if (cupsFileGets(fp, line, sizeof(line)) == NULL)
8687 {
8688 cupsdLogMessage(CUPSD_LOG_ERROR,
8689 "Unable to read from print file for job ticket - %s",
8690 strerror(errno));
8691 cupsFileClose(fp);
8692 return;
8693 }
8694
8695 if (strncmp(line, "%!PS-Adobe-", 11) && strncmp(line, "%PDF-", 5))
8696 {
8697 /*
8698 * Not a DSC-compliant file, so no job ticket info will be available...
8699 */
8700
8701 cupsFileClose(fp);
8702 return;
8703 }
8704
8705 /*
8706 * Read job ticket info from the file...
8707 */
8708
8709 num_options = 0;
8710 options = NULL;
8711
8712 while (cupsFileGets(fp, line, sizeof(line)))
8713 {
8714 /*
8715 * Stop at the first non-ticket line...
8716 */
8717
8718 if (strncmp(line, "%cupsJobTicket:", 15))
8719 break;
8720
8721 /*
8722 * Add the options to the option array...
8723 */
8724
8725 num_options = cupsParseOptions(line + 15, num_options, &options);
8726 }
8727
8728 /*
8729 * Done with the file; see if we have any options...
8730 */
8731
8732 cupsFileClose(fp);
8733
8734 if (num_options == 0)
8735 return;
8736
8737 /*
8738 * OK, convert the options to an attribute list, and apply them to
8739 * the request...
8740 */
8741
8742 ticket = ippNew();
8743 cupsEncodeOptions(ticket, num_options, options);
8744
8745 /*
8746 * See what the user wants to change.
8747 */
8748
8749 for (attr = ticket->attrs; attr; attr = attr->next)
8750 {
8751 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
8752 continue;
8753
8754 if (!strcmp(attr->name, "job-originating-host-name") ||
8755 !strcmp(attr->name, "job-originating-user-name") ||
8756 !strcmp(attr->name, "job-media-sheets-completed") ||
8757 !strcmp(attr->name, "job-k-octets") ||
8758 !strcmp(attr->name, "job-id") ||
8759 !strncmp(attr->name, "job-state", 9) ||
8760 !strncmp(attr->name, "time-at-", 8))
8761 continue; /* Read-only attrs */
8762
8763 if ((attr2 = ippFindAttribute(con->request, attr->name,
8764 IPP_TAG_ZERO)) != NULL)
8765 {
8766 /*
8767 * Some other value; first free the old value...
8768 */
8769
8770 if (con->request->attrs == attr2)
8771 {
8772 con->request->attrs = attr2->next;
8773 prev2 = NULL;
8774 }
8775 else
8776 {
8777 for (prev2 = con->request->attrs; prev2; prev2 = prev2->next)
8778 if (prev2->next == attr2)
8779 {
8780 prev2->next = attr2->next;
8781 break;
8782 }
8783 }
8784
8785 if (con->request->last == attr2)
8786 con->request->last = prev2;
8787
8788 _ippFreeAttr(attr2);
8789 }
8790
8791 /*
8792 * Add new option by copying it...
8793 */
8794
8795 copy_attribute(con->request, attr, 0);
8796 }
8797
8798 /*
8799 * Then free the attribute list and option array...
8800 */
8801
8802 ippDelete(ticket);
8803 cupsFreeOptions(num_options, options);
8804 }
8805
8806
8807 /*
8808 * 'reject_jobs()' - Reject print jobs to a printer.
8809 */
8810
8811 static void
8812 reject_jobs(cupsd_client_t *con, /* I - Client connection */
8813 ipp_attribute_t *uri) /* I - Printer or class URI */
8814 {
8815 http_status_t status; /* Policy status */
8816 cups_ptype_t dtype; /* Destination type (printer/class) */
8817 cupsd_printer_t *printer; /* Printer data */
8818 ipp_attribute_t *attr; /* printer-state-message text */
8819
8820
8821 cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con,
8822 con->http.fd, uri->values[0].string.text);
8823
8824 /*
8825 * Is the destination valid?
8826 */
8827
8828 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8829 {
8830 /*
8831 * Bad URI...
8832 */
8833
8834 send_ipp_status(con, IPP_NOT_FOUND,
8835 _("The printer or class was not found."));
8836 return;
8837 }
8838
8839 /*
8840 * Check policy...
8841 */
8842
8843 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8844 {
8845 send_http_error(con, status, printer);
8846 return;
8847 }
8848
8849 /*
8850 * Reject jobs sent to the printer...
8851 */
8852
8853 printer->accepting = 0;
8854
8855 if ((attr = ippFindAttribute(con->request, "printer-state-message",
8856 IPP_TAG_TEXT)) == NULL)
8857 strcpy(printer->state_message, "Rejecting Jobs");
8858 else
8859 strlcpy(printer->state_message, attr->values[0].string.text,
8860 sizeof(printer->state_message));
8861
8862 cupsdAddPrinterHistory(printer);
8863
8864 if (dtype & CUPS_PRINTER_CLASS)
8865 {
8866 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
8867
8868 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").",
8869 printer->name, get_username(con));
8870 }
8871 else
8872 {
8873 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
8874
8875 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").",
8876 printer->name, get_username(con));
8877 }
8878
8879 /*
8880 * Everything was ok, so return OK status...
8881 */
8882
8883 con->response->request.status.status_code = IPP_OK;
8884 }
8885
8886
8887 /*
8888 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8889 */
8890
8891 static void
8892 release_held_new_jobs(
8893 cupsd_client_t *con, /* I - Connection */
8894 ipp_attribute_t *uri) /* I - Printer URI */
8895 {
8896 http_status_t status; /* Policy status */
8897 cups_ptype_t dtype; /* Destination type (printer/class) */
8898 cupsd_printer_t *printer; /* Printer data */
8899
8900
8901 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_held_new_jobs(%p[%d], %s)", con,
8902 con->http.fd, uri->values[0].string.text);
8903
8904 /*
8905 * Is the destination valid?
8906 */
8907
8908 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8909 {
8910 /*
8911 * Bad URI...
8912 */
8913
8914 send_ipp_status(con, IPP_NOT_FOUND,
8915 _("The printer or class was not found."));
8916 return;
8917 }
8918
8919 /*
8920 * Check policy...
8921 */
8922
8923 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8924 {
8925 send_http_error(con, status, printer);
8926 return;
8927 }
8928
8929 /*
8930 * Hold pending/new jobs sent to the printer...
8931 */
8932
8933 printer->holding_new_jobs = 0;
8934
8935 cupsdSetPrinterReasons(printer, "-hold-new-jobs");
8936 cupsdAddPrinterHistory(printer);
8937
8938 if (dtype & CUPS_PRINTER_CLASS)
8939 cupsdLogMessage(CUPSD_LOG_INFO,
8940 "Class \"%s\" now printing pending/new jobs (\"%s\").",
8941 printer->name, get_username(con));
8942 else
8943 cupsdLogMessage(CUPSD_LOG_INFO,
8944 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
8945 printer->name, get_username(con));
8946
8947 /*
8948 * Everything was ok, so return OK status...
8949 */
8950
8951 con->response->request.status.status_code = IPP_OK;
8952 }
8953
8954
8955 /*
8956 * 'release_job()' - Release a held print job.
8957 */
8958
8959 static void
8960 release_job(cupsd_client_t *con, /* I - Client connection */
8961 ipp_attribute_t *uri) /* I - Job or Printer URI */
8962 {
8963 ipp_attribute_t *attr; /* Current attribute */
8964 int jobid; /* Job ID */
8965 char scheme[HTTP_MAX_URI], /* Method portion of URI */
8966 username[HTTP_MAX_URI], /* Username portion of URI */
8967 host[HTTP_MAX_URI], /* Host portion of URI */
8968 resource[HTTP_MAX_URI]; /* Resource portion of URI */
8969 int port; /* Port portion of URI */
8970 cupsd_job_t *job; /* Job information */
8971
8972
8973 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con,
8974 con->http.fd, uri->values[0].string.text);
8975
8976 /*
8977 * See if we have a job URI or a printer URI...
8978 */
8979
8980 if (!strcmp(uri->name, "printer-uri"))
8981 {
8982 /*
8983 * Got a printer URI; see if we also have a job-id attribute...
8984 */
8985
8986 if ((attr = ippFindAttribute(con->request, "job-id",
8987 IPP_TAG_INTEGER)) == NULL)
8988 {
8989 send_ipp_status(con, IPP_BAD_REQUEST,
8990 _("Got a printer-uri attribute but no job-id!"));
8991 return;
8992 }
8993
8994 jobid = attr->values[0].integer;
8995 }
8996 else
8997 {
8998 /*
8999 * Got a job URI; parse it to get the job ID...
9000 */
9001
9002 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9003 sizeof(scheme), username, sizeof(username), host,
9004 sizeof(host), &port, resource, sizeof(resource));
9005
9006 if (strncmp(resource, "/jobs/", 6))
9007 {
9008 /*
9009 * Not a valid URI!
9010 */
9011
9012 send_ipp_status(con, IPP_BAD_REQUEST,
9013 _("Bad job-uri attribute \"%s\"!"),
9014 uri->values[0].string.text);
9015 return;
9016 }
9017
9018 jobid = atoi(resource + 6);
9019 }
9020
9021 /*
9022 * See if the job exists...
9023 */
9024
9025 if ((job = cupsdFindJob(jobid)) == NULL)
9026 {
9027 /*
9028 * Nope - return a "not found" error...
9029 */
9030
9031 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
9032 return;
9033 }
9034
9035 /*
9036 * See if job is "held"...
9037 */
9038
9039 if (job->state_value != IPP_JOB_HELD)
9040 {
9041 /*
9042 * Nope - return a "not possible" error...
9043 */
9044
9045 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held!"), jobid);
9046 return;
9047 }
9048
9049 /*
9050 * See if the job is owned by the requesting user...
9051 */
9052
9053 if (!validate_user(job, con, job->username, username, sizeof(username)))
9054 {
9055 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
9056 return;
9057 }
9058
9059 /*
9060 * Reset the job-hold-until value to "no-hold"...
9061 */
9062
9063 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9064 IPP_TAG_KEYWORD)) == NULL)
9065 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9066
9067 if (attr)
9068 {
9069 _cupsStrFree(attr->values[0].string.text);
9070
9071 attr->value_tag = IPP_TAG_KEYWORD;
9072 attr->values[0].string.text = _cupsStrAlloc("no-hold");
9073
9074 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
9075 "Job job-hold-until value changed by user.");
9076 }
9077
9078 /*
9079 * Release the job and return...
9080 */
9081
9082 cupsdReleaseJob(job);
9083
9084 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
9085 "Job released by user.");
9086
9087 cupsdLogJob(job, CUPSD_LOG_INFO, "Released by \"%s\".", username);
9088
9089 con->response->request.status.status_code = IPP_OK;
9090
9091 cupsdCheckJobs();
9092 }
9093
9094
9095 /*
9096 * 'renew_subscription()' - Renew an existing subscription...
9097 */
9098
9099 static void
9100 renew_subscription(
9101 cupsd_client_t *con, /* I - Client connection */
9102 int sub_id) /* I - Subscription ID */
9103 {
9104 http_status_t status; /* Policy status */
9105 cupsd_subscription_t *sub; /* Subscription */
9106 ipp_attribute_t *lease; /* notify-lease-duration */
9107
9108
9109 cupsdLogMessage(CUPSD_LOG_DEBUG2,
9110 "renew_subscription(con=%p[%d], sub_id=%d)",
9111 con, con->http.fd, sub_id);
9112
9113 /*
9114 * Is the subscription ID valid?
9115 */
9116
9117 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
9118 {
9119 /*
9120 * Bad subscription ID...
9121 */
9122
9123 send_ipp_status(con, IPP_NOT_FOUND,
9124 _("notify-subscription-id %d no good!"), sub_id);
9125 return;
9126 }
9127
9128 if (sub->job)
9129 {
9130 /*
9131 * Job subscriptions cannot be renewed...
9132 */
9133
9134 send_ipp_status(con, IPP_NOT_POSSIBLE,
9135 _("Job subscriptions cannot be renewed!"));
9136 return;
9137 }
9138
9139 /*
9140 * Check policy...
9141 */
9142
9143 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
9144 DefaultPolicyPtr,
9145 con, sub->owner)) != HTTP_OK)
9146 {
9147 send_http_error(con, status, sub->dest);
9148 return;
9149 }
9150
9151 /*
9152 * Renew the subscription...
9153 */
9154
9155 lease = ippFindAttribute(con->request, "notify-lease-duration",
9156 IPP_TAG_INTEGER);
9157
9158 sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration;
9159
9160 if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration))
9161 {
9162 cupsdLogMessage(CUPSD_LOG_INFO,
9163 "renew_subscription: Limiting notify-lease-duration to "
9164 "%d seconds.",
9165 MaxLeaseDuration);
9166 sub->lease = MaxLeaseDuration;
9167 }
9168
9169 sub->expire = sub->lease ? time(NULL) + sub->lease : 0;
9170
9171 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
9172
9173 con->response->request.status.status_code = IPP_OK;
9174
9175 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
9176 "notify-lease-duration", sub->lease);
9177 }
9178
9179
9180 /*
9181 * 'restart_job()' - Restart an old print job.
9182 */
9183
9184 static void
9185 restart_job(cupsd_client_t *con, /* I - Client connection */
9186 ipp_attribute_t *uri) /* I - Job or Printer URI */
9187 {
9188 ipp_attribute_t *attr; /* Current attribute */
9189 int jobid; /* Job ID */
9190 char scheme[HTTP_MAX_URI], /* Method portion of URI */
9191 username[HTTP_MAX_URI], /* Username portion of URI */
9192 host[HTTP_MAX_URI], /* Host portion of URI */
9193 resource[HTTP_MAX_URI]; /* Resource portion of URI */
9194 int port; /* Port portion of URI */
9195 cupsd_job_t *job; /* Job information */
9196
9197
9198 cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con,
9199 con->http.fd, uri->values[0].string.text);
9200
9201 /*
9202 * See if we have a job URI or a printer URI...
9203 */
9204
9205 if (!strcmp(uri->name, "printer-uri"))
9206 {
9207 /*
9208 * Got a printer URI; see if we also have a job-id attribute...
9209 */
9210
9211 if ((attr = ippFindAttribute(con->request, "job-id",
9212 IPP_TAG_INTEGER)) == NULL)
9213 {
9214 send_ipp_status(con, IPP_BAD_REQUEST,
9215 _("Got a printer-uri attribute but no job-id!"));
9216 return;
9217 }
9218
9219 jobid = attr->values[0].integer;
9220 }
9221 else
9222 {
9223 /*
9224 * Got a job URI; parse it to get the job ID...
9225 */
9226
9227 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9228 sizeof(scheme), username, sizeof(username), host,
9229 sizeof(host), &port, resource, sizeof(resource));
9230
9231 if (strncmp(resource, "/jobs/", 6))
9232 {
9233 /*
9234 * Not a valid URI!
9235 */
9236
9237 send_ipp_status(con, IPP_BAD_REQUEST,
9238 _("Bad job-uri attribute \"%s\"!"),
9239 uri->values[0].string.text);
9240 return;
9241 }
9242
9243 jobid = atoi(resource + 6);
9244 }
9245
9246 /*
9247 * See if the job exists...
9248 */
9249
9250 if ((job = cupsdFindJob(jobid)) == NULL)
9251 {
9252 /*
9253 * Nope - return a "not found" error...
9254 */
9255
9256 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
9257 return;
9258 }
9259
9260 /*
9261 * See if job is in any of the "completed" states...
9262 */
9263
9264 if (job->state_value <= IPP_JOB_PROCESSING)
9265 {
9266 /*
9267 * Nope - return a "not possible" error...
9268 */
9269
9270 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete!"),
9271 jobid);
9272 return;
9273 }
9274
9275 /*
9276 * See if we have retained the job files...
9277 */
9278
9279 cupsdLoadJob(job);
9280
9281 if (!job->attrs || job->num_files == 0)
9282 {
9283 /*
9284 * Nope - return a "not possible" error...
9285 */
9286
9287 send_ipp_status(con, IPP_NOT_POSSIBLE,
9288 _("Job #%d cannot be restarted - no files!"), jobid);
9289 return;
9290 }
9291
9292 /*
9293 * See if the job is owned by the requesting user...
9294 */
9295
9296 if (!validate_user(job, con, job->username, username, sizeof(username)))
9297 {
9298 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
9299 return;
9300 }
9301
9302 /*
9303 * Restart the job and return...
9304 */
9305
9306 cupsdRestartJob(job);
9307
9308 cupsdLogJob(job, CUPSD_LOG_INFO, "Restarted by \"%s\".", username);
9309
9310 con->response->request.status.status_code = IPP_OK;
9311 }
9312
9313
9314 /*
9315 * 'save_auth_info()' - Save authentication information for a job.
9316 */
9317
9318 static void
9319 save_auth_info(
9320 cupsd_client_t *con, /* I - Client connection */
9321 cupsd_job_t *job, /* I - Job */
9322 ipp_attribute_t *auth_info) /* I - auth-info attribute, if any */
9323 {
9324 int i; /* Looping var */
9325 char filename[1024]; /* Job authentication filename */
9326 cups_file_t *fp; /* Job authentication file */
9327 char line[2048]; /* Line for file */
9328 cupsd_printer_t *dest; /* Destination printer/class */
9329
9330
9331 /*
9332 * This function saves the in-memory authentication information for
9333 * a job so that it can be used to authenticate with a remote host.
9334 * The information is stored in a file that is readable only by the
9335 * root user. The fields are Base-64 encoded, each on a separate line,
9336 * followed by random number (up to 1024) of newlines to limit the
9337 * amount of information that is exposed.
9338 *
9339 * Because of the potential for exposing of authentication information,
9340 * this functionality is only enabled when running cupsd as root.
9341 *
9342 * This caching only works for the Basic and BasicDigest authentication
9343 * types. Digest authentication cannot be cached this way, and in
9344 * the future Kerberos authentication may make all of this obsolete.
9345 *
9346 * Authentication information is saved whenever an authenticated
9347 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9348 * performed.
9349 *
9350 * This information is deleted after a job is completed or canceled,
9351 * so reprints may require subsequent re-authentication.
9352 */
9353
9354 if (RunUser)
9355 return;
9356
9357 if ((dest = cupsdFindDest(job->dest)) == NULL)
9358 return;
9359
9360 /*
9361 * Create the authentication file and change permissions...
9362 */
9363
9364 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
9365 if ((fp = cupsFileOpen(filename, "w")) == NULL)
9366 {
9367 cupsdLogMessage(CUPSD_LOG_ERROR,
9368 "Unable to save authentication info to \"%s\" - %s",
9369 filename, strerror(errno));
9370 return;
9371 }
9372
9373 fchown(cupsFileNumber(fp), 0, 0);
9374 fchmod(cupsFileNumber(fp), 0400);
9375
9376 if (auth_info && auth_info->num_values == dest->num_auth_info_required)
9377 {
9378 /*
9379 * Write 1 to 3 auth values...
9380 */
9381
9382 cupsdClearString(&job->auth_username);
9383 cupsdClearString(&job->auth_domain);
9384 cupsdClearString(&job->auth_password);
9385
9386 for (i = 0; i < auth_info->num_values; i ++)
9387 {
9388 httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text,
9389 strlen(auth_info->values[i].string.text));
9390 cupsFilePrintf(fp, "%s\n", line);
9391
9392 if (!strcmp(dest->auth_info_required[i], "username"))
9393 cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s",
9394 auth_info->values[i].string.text);
9395 else if (!strcmp(dest->auth_info_required[i], "domain"))
9396 cupsdSetStringf(&job->auth_domain, "AUTH_DOMAIN=%s",
9397 auth_info->values[i].string.text);
9398 else if (!strcmp(dest->auth_info_required[i], "password"))
9399 cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s",
9400 auth_info->values[i].string.text);
9401 }
9402 }
9403 else if (con->username[0])
9404 {
9405 /*
9406 * Write the authenticated username...
9407 */
9408
9409 httpEncode64_2(line, sizeof(line), con->username, strlen(con->username));
9410 cupsFilePrintf(fp, "%s\n", line);
9411
9412 cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", con->username);
9413 cupsdClearString(&job->auth_domain);
9414
9415 /*
9416 * Write the authenticated password...
9417 */
9418
9419 httpEncode64_2(line, sizeof(line), con->password, strlen(con->password));
9420 cupsFilePrintf(fp, "%s\n", line);
9421
9422 cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", con->password);
9423 }
9424
9425 /*
9426 * Write a random number of newlines to the end of the file...
9427 */
9428
9429 for (i = (rand() % 1024); i >= 0; i --)
9430 cupsFilePutChar(fp, '\n');
9431
9432 /*
9433 * Close the file and return...
9434 */
9435
9436 cupsFileClose(fp);
9437
9438 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9439 if (con->gss_have_creds)
9440 save_krb5_creds(con, job);
9441 else if (job->ccname)
9442 cupsdClearString(&(job->ccname));
9443 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9444 }
9445
9446
9447 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9448 /*
9449 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
9450 */
9451
9452 static void
9453 save_krb5_creds(cupsd_client_t *con, /* I - Client connection */
9454 cupsd_job_t *job) /* I - Job */
9455 {
9456 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
9457 cupsdLogMessage(CUPSD_LOG_INFO,
9458 "Sorry, your version of Kerberos does not support delegated "
9459 "credentials!");
9460 return;
9461
9462 # else
9463 krb5_error_code error; /* Kerberos error code */
9464 OM_uint32 major_status, /* Major status code */
9465 minor_status; /* Minor status code */
9466 krb5_principal principal; /* Kerberos principal */
9467
9468
9469 # ifdef __APPLE__
9470 /*
9471 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
9472 * to use it...
9473 */
9474
9475 if (krb5_init_context == NULL)
9476 return;
9477 # endif /* __APPLE__ */
9478
9479 if (!KerberosInitialized)
9480 {
9481 /*
9482 * Setup a Kerberos context for the scheduler to use...
9483 */
9484
9485 KerberosInitialized = 1;
9486
9487 if (krb5_init_context(&KerberosContext))
9488 {
9489 KerberosContext = NULL;
9490
9491 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to initialize Kerberos context");
9492 return;
9493 }
9494 }
9495
9496 /*
9497 * We MUST create a file-based cache because memory-based caches are
9498 * only valid for the current process/address space.
9499 *
9500 * Due to various bugs/features in different versions of Kerberos, we
9501 * need either the krb5_cc_new_unique() function or Heimdal's version
9502 * of krb5_cc_gen_new() to create a new FILE: credential cache that
9503 * can be passed to the backend. These functions create a temporary
9504 * file (typically in /tmp) containing the cached credentials, which
9505 * are removed when we have successfully printed a job.
9506 */
9507
9508 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
9509 if ((error = krb5_cc_new_unique(KerberosContext, "FILE", NULL,
9510 &(job->ccache))) != 0)
9511 # else /* HAVE_HEIMDAL */
9512 if ((error = krb5_cc_gen_new(KerberosContext, &krb5_fcc_ops,
9513 &(job->ccache))) != 0)
9514 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
9515 {
9516 cupsdLogMessage(CUPSD_LOG_ERROR,
9517 "Unable to create new credentials cache (%d/%s)",
9518 error, strerror(errno));
9519 job->ccache = NULL;
9520 return;
9521 }
9522
9523 if ((error = krb5_parse_name(KerberosContext, con->username, &principal)) != 0)
9524 {
9525 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to parse kerberos username (%d/%s)",
9526 error, strerror(errno));
9527 krb5_cc_destroy(KerberosContext, job->ccache);
9528 job->ccache = NULL;
9529 return;
9530 }
9531
9532 if ((error = krb5_cc_initialize(KerberosContext, job->ccache, principal)))
9533 {
9534 cupsdLogMessage(CUPSD_LOG_ERROR,
9535 "Unable to initialize credentials cache (%d/%s)", error,
9536 strerror(errno));
9537 krb5_cc_destroy(KerberosContext, job->ccache);
9538 krb5_free_principal(KerberosContext, principal);
9539 job->ccache = NULL;
9540 return;
9541 }
9542
9543 krb5_free_principal(KerberosContext, principal);
9544
9545 /*
9546 * Copy the user's credentials to the new cache file...
9547 */
9548
9549 major_status = gss_krb5_copy_ccache(&minor_status, con->gss_delegated_cred,
9550 job->ccache);
9551
9552 if (GSS_ERROR(major_status))
9553 {
9554 cupsdLogGSSMessage(CUPSD_LOG_ERROR, major_status, minor_status,
9555 "Unable to import client credentials cache");
9556 krb5_cc_destroy(KerberosContext, job->ccache);
9557 job->ccache = NULL;
9558 return;
9559 }
9560
9561 /*
9562 * Add the KRB5CCNAME environment variable to the job so that the
9563 * backend can use the credentials when printing.
9564 */
9565
9566 cupsdSetStringf(&(job->ccname), "KRB5CCNAME=FILE:%s",
9567 krb5_cc_get_name(KerberosContext, job->ccache));
9568
9569 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "save_krb5_creds: %s", job->ccname);
9570 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
9571 }
9572 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9573
9574
9575 /*
9576 * 'send_document()' - Send a file to a printer or class.
9577 */
9578
9579 static void
9580 send_document(cupsd_client_t *con, /* I - Client connection */
9581 ipp_attribute_t *uri) /* I - Printer URI */
9582 {
9583 ipp_attribute_t *attr; /* Current attribute */
9584 ipp_attribute_t *format; /* Request's document-format attribute */
9585 ipp_attribute_t *jformat; /* Job's document-format attribute */
9586 const char *default_format;/* document-format-default value */
9587 int jobid; /* Job ID number */
9588 cupsd_job_t *job; /* Current job */
9589 char job_uri[HTTP_MAX_URI],
9590 /* Job URI */
9591 scheme[HTTP_MAX_URI],
9592 /* Method portion of URI */
9593 username[HTTP_MAX_URI],
9594 /* Username portion of URI */
9595 host[HTTP_MAX_URI],
9596 /* Host portion of URI */
9597 resource[HTTP_MAX_URI];
9598 /* Resource portion of URI */
9599 int port; /* Port portion of URI */
9600 mime_type_t *filetype; /* Type of file */
9601 char super[MIME_MAX_SUPER],
9602 /* Supertype of file */
9603 type[MIME_MAX_TYPE],
9604 /* Subtype of file */
9605 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
9606 /* Textual name of mime type */
9607 char filename[1024]; /* Job filename */
9608 cupsd_printer_t *printer; /* Current printer */
9609 struct stat fileinfo; /* File information */
9610 int kbytes; /* Size of file */
9611 int compression; /* Type of compression */
9612 int start_job; /* Start the job? */
9613
9614
9615 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con,
9616 con->http.fd, uri->values[0].string.text);
9617
9618 /*
9619 * See if we have a job URI or a printer URI...
9620 */
9621
9622 if (!strcmp(uri->name, "printer-uri"))
9623 {
9624 /*
9625 * Got a printer URI; see if we also have a job-id attribute...
9626 */
9627
9628 if ((attr = ippFindAttribute(con->request, "job-id",
9629 IPP_TAG_INTEGER)) == NULL)
9630 {
9631 send_ipp_status(con, IPP_BAD_REQUEST,
9632 _("Got a printer-uri attribute but no job-id!"));
9633 return;
9634 }
9635
9636 jobid = attr->values[0].integer;
9637 }
9638 else
9639 {
9640 /*
9641 * Got a job URI; parse it to get the job ID...
9642 */
9643
9644 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9645 sizeof(scheme), username, sizeof(username), host,
9646 sizeof(host), &port, resource, sizeof(resource));
9647
9648 if (strncmp(resource, "/jobs/", 6))
9649 {
9650 /*
9651 * Not a valid URI!
9652 */
9653
9654 send_ipp_status(con, IPP_BAD_REQUEST,
9655 _("Bad job-uri attribute \"%s\"!"),
9656 uri->values[0].string.text);
9657 return;
9658 }
9659
9660 jobid = atoi(resource + 6);
9661 }
9662
9663 /*
9664 * See if the job exists...
9665 */
9666
9667 if ((job = cupsdFindJob(jobid)) == NULL)
9668 {
9669 /*
9670 * Nope - return a "not found" error...
9671 */
9672
9673 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
9674 return;
9675 }
9676
9677 printer = cupsdFindDest(job->dest);
9678
9679 /*
9680 * See if the job is owned by the requesting user...
9681 */
9682
9683 if (!validate_user(job, con, job->username, username, sizeof(username)))
9684 {
9685 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
9686 return;
9687 }
9688
9689 /*
9690 * OK, see if the client is sending the document compressed - CUPS
9691 * only supports "none" and "gzip".
9692 */
9693
9694 compression = CUPS_FILE_NONE;
9695
9696 if ((attr = ippFindAttribute(con->request, "compression",
9697 IPP_TAG_KEYWORD)) != NULL)
9698 {
9699 if (strcmp(attr->values[0].string.text, "none")
9700 #ifdef HAVE_LIBZ
9701 && strcmp(attr->values[0].string.text, "gzip")
9702 #endif /* HAVE_LIBZ */
9703 )
9704 {
9705 send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"!"),
9706 attr->values[0].string.text);
9707 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
9708 "compression", NULL, attr->values[0].string.text);
9709 return;
9710 }
9711
9712 #ifdef HAVE_LIBZ
9713 if (!strcmp(attr->values[0].string.text, "gzip"))
9714 compression = CUPS_FILE_GZIP;
9715 #endif /* HAVE_LIBZ */
9716 }
9717
9718 /*
9719 * Do we have a file to print?
9720 */
9721
9722 if (!con->filename)
9723 {
9724 send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!"));
9725 return;
9726 }
9727
9728 /*
9729 * Is it a format we support?
9730 */
9731
9732 if ((format = ippFindAttribute(con->request, "document-format",
9733 IPP_TAG_MIMETYPE)) != NULL)
9734 {
9735 /*
9736 * Grab format from client...
9737 */
9738
9739 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
9740 super, type) != 2)
9741 {
9742 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"),
9743 format->values[0].string.text);
9744 return;
9745 }
9746 }
9747 else if ((default_format = cupsGetOption("document-format",
9748 printer->num_options,
9749 printer->options)) != NULL)
9750 {
9751 /*
9752 * Use default document format...
9753 */
9754
9755 if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
9756 {
9757 send_ipp_status(con, IPP_BAD_REQUEST,
9758 _("Could not scan type \"%s\"!"),
9759 default_format);
9760 return;
9761 }
9762 }
9763 else
9764 {
9765 /*
9766 * No document format attribute? Auto-type it!
9767 */
9768
9769 strcpy(super, "application");
9770 strcpy(type, "octet-stream");
9771 }
9772
9773 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
9774 {
9775 /*
9776 * Auto-type the file...
9777 */
9778
9779 ipp_attribute_t *doc_name; /* document-name attribute */
9780
9781
9782 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Auto-typing file...");
9783
9784 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
9785 filetype = mimeFileType(MimeDatabase, con->filename,
9786 doc_name ? doc_name->values[0].string.text : NULL,
9787 &compression);
9788
9789 if (!filetype)
9790 filetype = mimeType(MimeDatabase, super, type);
9791
9792 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Request file type is %s/%s.",
9793 filetype->super, filetype->type);
9794 }
9795 else
9796 filetype = mimeType(MimeDatabase, super, type);
9797
9798 if (filetype)
9799 {
9800 /*
9801 * Replace the document-format attribute value with the auto-typed or
9802 * default one.
9803 */
9804
9805 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9806 filetype->type);
9807
9808 if ((jformat = ippFindAttribute(job->attrs, "document-format",
9809 IPP_TAG_MIMETYPE)) != NULL)
9810 {
9811 _cupsStrFree(jformat->values[0].string.text);
9812
9813 jformat->values[0].string.text = _cupsStrAlloc(mimetype);
9814 }
9815 else
9816 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
9817 "document-format", NULL, mimetype);
9818 }
9819 else if (!filetype)
9820 {
9821 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9822 _("Unsupported format \'%s/%s\'!"), super, type);
9823 cupsdLogMessage(CUPSD_LOG_INFO,
9824 "Hint: Do you have the raw file printing rules enabled?");
9825
9826 if (format)
9827 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9828 "document-format", NULL, format->values[0].string.text);
9829
9830 return;
9831 }
9832
9833 if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype))
9834 {
9835 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9836 filetype->type);
9837
9838 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9839 _("Unsupported format \'%s\'!"), mimetype);
9840
9841 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9842 "document-format", NULL, mimetype);
9843
9844 return;
9845 }
9846
9847 /*
9848 * Add the file to the job...
9849 */
9850
9851 cupsdLoadJob(job);
9852
9853 if (add_file(con, job, filetype, compression))
9854 return;
9855
9856 if (stat(con->filename, &fileinfo))
9857 kbytes = 0;
9858 else
9859 kbytes = (fileinfo.st_size + 1023) / 1024;
9860
9861 cupsdUpdateQuota(printer, job->username, 0, kbytes);
9862
9863 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
9864 IPP_TAG_INTEGER)) != NULL)
9865 attr->values[0].integer += kbytes;
9866
9867 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
9868 job->num_files);
9869 rename(con->filename, filename);
9870
9871 cupsdClearString(&con->filename);
9872
9873 cupsdLogJob(job, CUPSD_LOG_INFO, "File of type %s/%s queued by \"%s\".",
9874 filetype->super, filetype->type, job->username);
9875
9876 /*
9877 * Start the job if this is the last document...
9878 */
9879
9880 if ((attr = ippFindAttribute(con->request, "last-document",
9881 IPP_TAG_BOOLEAN)) != NULL &&
9882 attr->values[0].boolean)
9883 {
9884 /*
9885 * See if we need to add the ending sheet...
9886 */
9887
9888 if (cupsdTimeoutJob(job))
9889 return;
9890
9891 if (job->state_value == IPP_JOB_STOPPED)
9892 {
9893 job->state->values[0].integer = IPP_JOB_PENDING;
9894 job->state_value = IPP_JOB_PENDING;
9895 }
9896 else if (job->state_value == IPP_JOB_HELD)
9897 {
9898 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9899 IPP_TAG_KEYWORD)) == NULL)
9900 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9901
9902 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
9903 {
9904 job->state->values[0].integer = IPP_JOB_PENDING;
9905 job->state_value = IPP_JOB_PENDING;
9906 }
9907 }
9908
9909 job->dirty = 1;
9910 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
9911
9912 start_job = 1;
9913 }
9914 else
9915 {
9916 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9917 IPP_TAG_KEYWORD)) == NULL)
9918 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9919
9920 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
9921 {
9922 job->state->values[0].integer = IPP_JOB_HELD;
9923 job->state_value = IPP_JOB_HELD;
9924 job->hold_until = time(NULL) + 60;
9925 job->dirty = 1;
9926
9927 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
9928 }
9929
9930 start_job = 0;
9931 }
9932
9933 /*
9934 * Fill in the response info...
9935 */
9936
9937 snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName,
9938 LocalPort, jobid);
9939
9940 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
9941 job_uri);
9942
9943 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid);
9944
9945 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
9946 job->state_value);
9947 add_job_state_reasons(con, job);
9948
9949 con->response->request.status.status_code = IPP_OK;
9950
9951 /*
9952 * Start the job if necessary...
9953 */
9954
9955 if (start_job)
9956 cupsdCheckJobs();
9957 }
9958
9959
9960 /*
9961 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9962 */
9963
9964 static void
9965 send_http_error(
9966 cupsd_client_t *con, /* I - Client connection */
9967 http_status_t status, /* I - HTTP status code */
9968 cupsd_printer_t *printer) /* I - Printer, if any */
9969 {
9970 ipp_attribute_t *uri; /* Request URI, if any */
9971
9972
9973 if ((uri = ippFindAttribute(con->request, "printer-uri",
9974 IPP_TAG_URI)) == NULL)
9975 uri = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI);
9976
9977 cupsdLogMessage(status == HTTP_FORBIDDEN ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
9978 "Returning HTTP %s for %s (%s) from %s",
9979 httpStatus(status),
9980 ippOpString(con->request->request.op.operation_id),
9981 uri ? uri->values[0].string.text : "no URI",
9982 con->http.hostname);
9983
9984 if (printer)
9985 {
9986 int auth_type; /* Type of authentication required */
9987
9988
9989 auth_type = CUPSD_AUTH_NONE;
9990
9991 if (status == HTTP_UNAUTHORIZED &&
9992 printer->num_auth_info_required > 0 &&
9993 !strcmp(printer->auth_info_required[0], "negotiate") &&
9994 con->request &&
9995 (con->request->request.op.operation_id == IPP_PRINT_JOB ||
9996 con->request->request.op.operation_id == IPP_CREATE_JOB ||
9997 con->request->request.op.operation_id == CUPS_AUTHENTICATE_JOB))
9998 {
9999 /*
10000 * Creating and authenticating jobs requires Kerberos...
10001 */
10002
10003 auth_type = CUPSD_AUTH_NEGOTIATE;
10004 }
10005 else
10006 {
10007 /*
10008 * Use policy/location-defined authentication requirements...
10009 */
10010
10011 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
10012 cupsd_location_t *auth; /* Pointer to authentication element */
10013
10014
10015 if (printer->type & CUPS_PRINTER_CLASS)
10016 snprintf(resource, sizeof(resource), "/classes/%s", printer->name);
10017 else
10018 snprintf(resource, sizeof(resource), "/printers/%s", printer->name);
10019
10020 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
10021 auth->type == CUPSD_AUTH_NONE)
10022 auth = cupsdFindPolicyOp(printer->op_policy_ptr,
10023 con->request ?
10024 con->request->request.op.operation_id :
10025 IPP_PRINT_JOB);
10026
10027 if (auth)
10028 {
10029 if (auth->type == CUPSD_AUTH_DEFAULT)
10030 auth_type = DefaultAuthType;
10031 else
10032 auth_type = auth->type;
10033 }
10034 }
10035
10036 cupsdSendError(con, status, auth_type);
10037 }
10038 else
10039 cupsdSendError(con, status, CUPSD_AUTH_NONE);
10040
10041 ippDelete(con->response);
10042 con->response = NULL;
10043
10044 return;
10045 }
10046
10047
10048 /*
10049 * 'send_ipp_status()' - Send a status back to the IPP client.
10050 */
10051
10052 static void
10053 send_ipp_status(cupsd_client_t *con, /* I - Client connection */
10054 ipp_status_t status, /* I - IPP status code */
10055 const char *message,/* I - Status message */
10056 ...) /* I - Additional args as needed */
10057 {
10058 va_list ap; /* Pointer to additional args */
10059 char formatted[1024]; /* Formatted errror message */
10060
10061
10062 va_start(ap, message);
10063 vsnprintf(formatted, sizeof(formatted),
10064 _cupsLangString(con->language, message), ap);
10065 va_end(ap);
10066
10067 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s",
10068 ippOpString(con->request->request.op.operation_id),
10069 ippErrorString(status), formatted);
10070
10071 con->response->request.status.status_code = status;
10072
10073 if (ippFindAttribute(con->response, "attributes-charset",
10074 IPP_TAG_ZERO) == NULL)
10075 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
10076 "attributes-charset", NULL, DefaultCharset);
10077
10078 if (ippFindAttribute(con->response, "attributes-natural-language",
10079 IPP_TAG_ZERO) == NULL)
10080 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
10081 "attributes-natural-language", NULL, DefaultLanguage);
10082
10083 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT,
10084 "status-message", NULL, formatted);
10085 }
10086
10087
10088 /*
10089 * 'set_default()' - Set the default destination...
10090 */
10091
10092 static void
10093 set_default(cupsd_client_t *con, /* I - Client connection */
10094 ipp_attribute_t *uri) /* I - Printer URI */
10095 {
10096 http_status_t status; /* Policy status */
10097 cups_ptype_t dtype; /* Destination type (printer/class) */
10098 cupsd_printer_t *printer, /* Printer */
10099 *oldprinter; /* Old default printer */
10100
10101
10102 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con,
10103 con->http.fd, uri->values[0].string.text);
10104
10105 /*
10106 * Is the destination valid?
10107 */
10108
10109 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10110 {
10111 /*
10112 * Bad URI...
10113 */
10114
10115 send_ipp_status(con, IPP_NOT_FOUND,
10116 _("The printer or class was not found."));
10117 return;
10118 }
10119
10120 /*
10121 * Check policy...
10122 */
10123
10124 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
10125 {
10126 send_http_error(con, status, NULL);
10127 return;
10128 }
10129
10130 /*
10131 * Set it as the default...
10132 */
10133
10134 oldprinter = DefaultPrinter;
10135 DefaultPrinter = printer;
10136
10137 if (oldprinter)
10138 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, oldprinter, NULL,
10139 "%s is no longer the default printer.", oldprinter->name);
10140
10141 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
10142 "%s is now the default printer.", printer->name);
10143
10144 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES |
10145 CUPSD_DIRTY_REMOTE | CUPSD_DIRTY_PRINTCAP);
10146
10147 cupsdLogMessage(CUPSD_LOG_INFO,
10148 "Default destination set to \"%s\" by \"%s\".",
10149 printer->name, get_username(con));
10150
10151 /*
10152 * Everything was ok, so return OK status...
10153 */
10154
10155 con->response->request.status.status_code = IPP_OK;
10156 }
10157
10158
10159 /*
10160 * 'set_job_attrs()' - Set job attributes.
10161 */
10162
10163 static void
10164 set_job_attrs(cupsd_client_t *con, /* I - Client connection */
10165 ipp_attribute_t *uri) /* I - Job URI */
10166 {
10167 ipp_attribute_t *attr, /* Current attribute */
10168 *attr2; /* Job attribute */
10169 int jobid; /* Job ID */
10170 cupsd_job_t *job; /* Current job */
10171 char scheme[HTTP_MAX_URI],
10172 /* Method portion of URI */
10173 username[HTTP_MAX_URI],
10174 /* Username portion of URI */
10175 host[HTTP_MAX_URI],
10176 /* Host portion of URI */
10177 resource[HTTP_MAX_URI];
10178 /* Resource portion of URI */
10179 int port; /* Port portion of URI */
10180 int event; /* Events? */
10181 int check_jobs; /* Check jobs? */
10182
10183
10184 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con,
10185 con->http.fd, uri->values[0].string.text);
10186
10187 /*
10188 * Start with "everything is OK" status...
10189 */
10190
10191 con->response->request.status.status_code = IPP_OK;
10192
10193 /*
10194 * See if we have a job URI or a printer URI...
10195 */
10196
10197 if (!strcmp(uri->name, "printer-uri"))
10198 {
10199 /*
10200 * Got a printer URI; see if we also have a job-id attribute...
10201 */
10202
10203 if ((attr = ippFindAttribute(con->request, "job-id",
10204 IPP_TAG_INTEGER)) == NULL)
10205 {
10206 send_ipp_status(con, IPP_BAD_REQUEST,
10207 _("Got a printer-uri attribute but no job-id!"));
10208 return;
10209 }
10210
10211 jobid = attr->values[0].integer;
10212 }
10213 else
10214 {
10215 /*
10216 * Got a job URI; parse it to get the job ID...
10217 */
10218
10219 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
10220 sizeof(scheme), username, sizeof(username), host,
10221 sizeof(host), &port, resource, sizeof(resource));
10222
10223 if (strncmp(resource, "/jobs/", 6))
10224 {
10225 /*
10226 * Not a valid URI!
10227 */
10228
10229 send_ipp_status(con, IPP_BAD_REQUEST,
10230 _("Bad job-uri attribute \"%s\"!"),
10231 uri->values[0].string.text);
10232 return;
10233 }
10234
10235 jobid = atoi(resource + 6);
10236 }
10237
10238 /*
10239 * See if the job exists...
10240 */
10241
10242 if ((job = cupsdFindJob(jobid)) == NULL)
10243 {
10244 /*
10245 * Nope - return a "not found" error...
10246 */
10247
10248 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
10249 return;
10250 }
10251
10252 /*
10253 * See if the job has been completed...
10254 */
10255
10256 if (job->state_value > IPP_JOB_STOPPED)
10257 {
10258 /*
10259 * Return a "not-possible" error...
10260 */
10261
10262 send_ipp_status(con, IPP_NOT_POSSIBLE,
10263 _("Job #%d is finished and cannot be altered!"), jobid);
10264 return;
10265 }
10266
10267 /*
10268 * See if the job is owned by the requesting user...
10269 */
10270
10271 if (!validate_user(job, con, job->username, username, sizeof(username)))
10272 {
10273 send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
10274 return;
10275 }
10276
10277 /*
10278 * See what the user wants to change.
10279 */
10280
10281 cupsdLoadJob(job);
10282
10283 check_jobs = 0;
10284 event = 0;
10285
10286 for (attr = con->request->attrs; attr; attr = attr->next)
10287 {
10288 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
10289 continue;
10290
10291 if (!strcmp(attr->name, "attributes-charset") ||
10292 !strcmp(attr->name, "attributes-natural-language") ||
10293 !strcmp(attr->name, "document-compression") ||
10294 !strcmp(attr->name, "document-format") ||
10295 !strcmp(attr->name, "job-detailed-status-messages") ||
10296 !strcmp(attr->name, "job-document-access-errors") ||
10297 !strcmp(attr->name, "job-id") ||
10298 !strcmp(attr->name, "job-impressions-completed") ||
10299 !strcmp(attr->name, "job-k-octets") ||
10300 !strcmp(attr->name, "job-originating-host-name") ||
10301 !strcmp(attr->name, "job-originating-user-name") ||
10302 !strcmp(attr->name, "job-printer-up-time") ||
10303 !strcmp(attr->name, "job-printer-uri") ||
10304 !strcmp(attr->name, "job-sheets") ||
10305 !strcmp(attr->name, "job-state-message") ||
10306 !strcmp(attr->name, "job-state-reasons") ||
10307 !strcmp(attr->name, "job-uri") ||
10308 !strcmp(attr->name, "number-of-documents") ||
10309 !strcmp(attr->name, "number-of-intervening-jobs") ||
10310 !strcmp(attr->name, "output-device-assigned") ||
10311 !strncmp(attr->name, "date-time-at-", 13) ||
10312 !strncmp(attr->name, "job-k-octets", 12) ||
10313 !strncmp(attr->name, "job-media-sheets", 16) ||
10314 !strncmp(attr->name, "time-at-", 8))
10315 {
10316 /*
10317 * Read-only attrs!
10318 */
10319
10320 send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE,
10321 _("%s cannot be changed."), attr->name);
10322
10323 if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
10324 attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
10325
10326 continue;
10327 }
10328
10329 if (!strcmp(attr->name, "job-priority"))
10330 {
10331 /*
10332 * Change the job priority...
10333 */
10334
10335 if (attr->value_tag != IPP_TAG_INTEGER)
10336 {
10337 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value!"));
10338
10339 if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
10340 attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
10341 }
10342 else if (job->state_value >= IPP_JOB_PROCESSING)
10343 {
10344 send_ipp_status(con, IPP_NOT_POSSIBLE,
10345 _("Job is completed and cannot be changed."));
10346 return;
10347 }
10348 else if (con->response->request.status.status_code == IPP_OK)
10349 {
10350 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-priority to %d",
10351 attr->values[0].integer);
10352 cupsdSetJobPriority(job, attr->values[0].integer);
10353
10354 check_jobs = 1;
10355 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED |
10356 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED;
10357 }
10358 }
10359 else if (!strcmp(attr->name, "job-state"))
10360 {
10361 /*
10362 * Change the job state...
10363 */
10364
10365 if (attr->value_tag != IPP_TAG_ENUM)
10366 {
10367 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value!"));
10368
10369 if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
10370 attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
10371 }
10372 else
10373 {
10374 switch (attr->values[0].integer)
10375 {
10376 case IPP_JOB_PENDING :
10377 case IPP_JOB_HELD :
10378 if (job->state_value > IPP_JOB_HELD)
10379 {
10380 send_ipp_status(con, IPP_NOT_POSSIBLE,
10381 _("Job state cannot be changed."));
10382 return;
10383 }
10384 else if (con->response->request.status.status_code == IPP_OK)
10385 {
10386 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10387 attr->values[0].integer);
10388
10389 job->state->values[0].integer = attr->values[0].integer;
10390 job->state_value = (ipp_jstate_t)attr->values[0].integer;
10391
10392 event |= CUPSD_EVENT_JOB_STATE;
10393 check_jobs = 1;
10394 }
10395 break;
10396
10397 case IPP_JOB_PROCESSING :
10398 case IPP_JOB_STOPPED :
10399 if (job->state_value != attr->values[0].integer)
10400 {
10401 send_ipp_status(con, IPP_NOT_POSSIBLE,
10402 _("Job state cannot be changed."));
10403 return;
10404 }
10405 break;
10406
10407 case IPP_JOB_CANCELED :
10408 case IPP_JOB_ABORTED :
10409 case IPP_JOB_COMPLETED :
10410 if (job->state_value > IPP_JOB_PROCESSING)
10411 {
10412 send_ipp_status(con, IPP_NOT_POSSIBLE,
10413 _("Job state cannot be changed."));
10414 return;
10415 }
10416 else if (con->response->request.status.status_code == IPP_OK)
10417 {
10418 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10419 attr->values[0].integer);
10420 cupsdCancelJob(job, 0, (ipp_jstate_t)attr->values[0].integer);
10421
10422 check_jobs = 1;
10423 }
10424 break;
10425 }
10426 }
10427 }
10428 else if (con->response->request.status.status_code != IPP_OK)
10429 continue;
10430 else if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10431 IPP_TAG_ZERO)) != NULL)
10432 {
10433 /*
10434 * Some other value; first free the old value...
10435 */
10436
10437 if (job->attrs->prev)
10438 job->attrs->prev->next = attr2->next;
10439 else
10440 job->attrs->attrs = attr2->next;
10441
10442 if (job->attrs->last == attr2)
10443 job->attrs->last = job->attrs->prev;
10444
10445 _ippFreeAttr(attr2);
10446
10447 /*
10448 * Then copy the attribute...
10449 */
10450
10451 copy_attribute(job->attrs, attr, 0);
10452
10453 /*
10454 * See if the job-name or job-hold-until is being changed.
10455 */
10456
10457 if (!strcmp(attr->name, "job-hold-until"))
10458 {
10459 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s",
10460 attr->values[0].string.text);
10461 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
10462
10463 if (!strcmp(attr->values[0].string.text, "no-hold"))
10464 cupsdReleaseJob(job);
10465 else
10466 cupsdHoldJob(job);
10467
10468 check_jobs = 1;
10469 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
10470 }
10471 }
10472 else if (attr->value_tag == IPP_TAG_DELETEATTR)
10473 {
10474 /*
10475 * Delete the attribute...
10476 */
10477
10478 if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10479 IPP_TAG_ZERO)) != NULL)
10480 {
10481 if (job->attrs->prev)
10482 job->attrs->prev->next = attr2->next;
10483 else
10484 job->attrs->attrs = attr2->next;
10485
10486 if (attr2 == job->attrs->last)
10487 job->attrs->last = job->attrs->prev;
10488
10489 _ippFreeAttr(attr2);
10490
10491 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
10492 }
10493 }
10494 else
10495 {
10496 /*
10497 * Add new option by copying it...
10498 */
10499
10500 copy_attribute(job->attrs, attr, 0);
10501
10502 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
10503 }
10504 }
10505
10506 /*
10507 * Save the job...
10508 */
10509
10510 job->dirty = 1;
10511 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
10512
10513 /*
10514 * Send events as needed...
10515 */
10516
10517 if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED)
10518 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED,
10519 cupsdFindDest(job->dest), job,
10520 "Job priority changed by user.");
10521
10522 if (event & CUPSD_EVENT_JOB_STATE)
10523 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
10524 job->state_value == IPP_JOB_HELD ?
10525 "Job held by user." : "Job restarted by user.");
10526
10527 if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED)
10528 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
10529 "Job options changed by user.");
10530
10531 /*
10532 * Start jobs if possible...
10533 */
10534
10535 if (check_jobs)
10536 cupsdCheckJobs();
10537 }
10538
10539
10540 /*
10541 * 'set_printer_defaults()' - Set printer default options from a request.
10542 */
10543
10544 static void
10545 set_printer_defaults(
10546 cupsd_client_t *con, /* I - Client connection */
10547 cupsd_printer_t *printer) /* I - Printer */
10548 {
10549 int i; /* Looping var */
10550 ipp_attribute_t *attr; /* Current attribute */
10551 int namelen; /* Length of attribute name */
10552 char name[256], /* New attribute name */
10553 value[256]; /* String version of integer attrs */
10554
10555
10556 for (attr = con->request->attrs; attr; attr = attr->next)
10557 {
10558 /*
10559 * Skip non-printer attributes...
10560 */
10561
10562 if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
10563 continue;
10564
10565 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name);
10566
10567 if (!strcmp(attr->name, "job-sheets-default"))
10568 {
10569 /*
10570 * Only allow keywords and names...
10571 */
10572
10573 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10574 continue;
10575
10576 /*
10577 * Only allow job-sheets-default to be set when running without a
10578 * system high classification level...
10579 */
10580
10581 if (Classification)
10582 continue;
10583
10584 cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text);
10585
10586 if (attr->num_values > 1)
10587 cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text);
10588 else
10589 cupsdSetString(&printer->job_sheets[1], "none");
10590 }
10591 else if (!strcmp(attr->name, "requesting-user-name-allowed"))
10592 {
10593 cupsdFreePrinterUsers(printer);
10594
10595 printer->deny_users = 0;
10596
10597 if (attr->value_tag == IPP_TAG_NAME &&
10598 (attr->num_values > 1 ||
10599 strcmp(attr->values[0].string.text, "all")))
10600 {
10601 for (i = 0; i < attr->num_values; i ++)
10602 cupsdAddPrinterUser(printer, attr->values[i].string.text);
10603 }
10604 }
10605 else if (!strcmp(attr->name, "requesting-user-name-denied"))
10606 {
10607 cupsdFreePrinterUsers(printer);
10608
10609 printer->deny_users = 1;
10610
10611 if (attr->value_tag == IPP_TAG_NAME &&
10612 (attr->num_values > 1 ||
10613 strcmp(attr->values[0].string.text, "none")))
10614 {
10615 for (i = 0; i < attr->num_values; i ++)
10616 cupsdAddPrinterUser(printer, attr->values[i].string.text);
10617 }
10618 }
10619 else if (!strcmp(attr->name, "job-quota-period"))
10620 {
10621 if (attr->value_tag != IPP_TAG_INTEGER)
10622 continue;
10623
10624 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...",
10625 attr->values[0].integer);
10626 cupsdFreeQuotas(printer);
10627
10628 printer->quota_period = attr->values[0].integer;
10629 }
10630 else if (!strcmp(attr->name, "job-k-limit"))
10631 {
10632 if (attr->value_tag != IPP_TAG_INTEGER)
10633 continue;
10634
10635 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...",
10636 attr->values[0].integer);
10637 cupsdFreeQuotas(printer);
10638
10639 printer->k_limit = attr->values[0].integer;
10640 }
10641 else if (!strcmp(attr->name, "job-page-limit"))
10642 {
10643 if (attr->value_tag != IPP_TAG_INTEGER)
10644 continue;
10645
10646 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...",
10647 attr->values[0].integer);
10648 cupsdFreeQuotas(printer);
10649
10650 printer->page_limit = attr->values[0].integer;
10651 }
10652 else if (!strcmp(attr->name, "printer-op-policy"))
10653 {
10654 cupsd_policy_t *p; /* Policy */
10655
10656
10657 if (attr->value_tag != IPP_TAG_NAME)
10658 continue;
10659
10660 if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
10661 {
10662 cupsdLogMessage(CUPSD_LOG_DEBUG,
10663 "Setting printer-op-policy to \"%s\"...",
10664 attr->values[0].string.text);
10665 cupsdSetString(&printer->op_policy, attr->values[0].string.text);
10666 printer->op_policy_ptr = p;
10667 }
10668 else
10669 {
10670 send_ipp_status(con, IPP_NOT_POSSIBLE,
10671 _("Unknown printer-op-policy \"%s\"."),
10672 attr->values[0].string.text);
10673 return;
10674 }
10675 }
10676 else if (!strcmp(attr->name, "printer-error-policy"))
10677 {
10678 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10679 continue;
10680
10681 if (strcmp(attr->values[0].string.text, "abort-job") &&
10682 strcmp(attr->values[0].string.text, "retry-current-job") &&
10683 strcmp(attr->values[0].string.text, "retry-job") &&
10684 strcmp(attr->values[0].string.text, "stop-printer"))
10685 {
10686 send_ipp_status(con, IPP_NOT_POSSIBLE,
10687 _("Unknown printer-error-policy \"%s\"."),
10688 attr->values[0].string.text);
10689 return;
10690 }
10691
10692 cupsdLogMessage(CUPSD_LOG_DEBUG,
10693 "Setting printer-error-policy to \"%s\"...",
10694 attr->values[0].string.text);
10695 cupsdSetString(&printer->error_policy, attr->values[0].string.text);
10696 }
10697
10698 /*
10699 * Skip any other non-default attributes...
10700 */
10701
10702 namelen = strlen(attr->name);
10703 if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") ||
10704 namelen > (sizeof(name) - 1) || attr->num_values != 1)
10705 continue;
10706
10707 /*
10708 * OK, anything else must be a user-defined default...
10709 */
10710
10711 strlcpy(name, attr->name, sizeof(name));
10712 name[namelen - 8] = '\0'; /* Strip "-default" */
10713
10714 switch (attr->value_tag)
10715 {
10716 case IPP_TAG_DELETEATTR :
10717 printer->num_options = cupsRemoveOption(name,
10718 printer->num_options,
10719 &(printer->options));
10720 cupsdLogMessage(CUPSD_LOG_DEBUG,
10721 "Deleting %s", attr->name);
10722 break;
10723
10724 case IPP_TAG_NAME :
10725 case IPP_TAG_KEYWORD :
10726 case IPP_TAG_URI :
10727 printer->num_options = cupsAddOption(name,
10728 attr->values[0].string.text,
10729 printer->num_options,
10730 &(printer->options));
10731 cupsdLogMessage(CUPSD_LOG_DEBUG,
10732 "Setting %s to \"%s\"...", attr->name,
10733 attr->values[0].string.text);
10734 break;
10735
10736 case IPP_TAG_BOOLEAN :
10737 printer->num_options = cupsAddOption(name,
10738 attr->values[0].boolean ?
10739 "true" : "false",
10740 printer->num_options,
10741 &(printer->options));
10742 cupsdLogMessage(CUPSD_LOG_DEBUG,
10743 "Setting %s to %s...", attr->name,
10744 attr->values[0].boolean ? "true" : "false");
10745 break;
10746
10747 case IPP_TAG_INTEGER :
10748 case IPP_TAG_ENUM :
10749 sprintf(value, "%d", attr->values[0].integer);
10750 printer->num_options = cupsAddOption(name, value,
10751 printer->num_options,
10752 &(printer->options));
10753 cupsdLogMessage(CUPSD_LOG_DEBUG,
10754 "Setting %s to %s...", attr->name, value);
10755 break;
10756
10757 case IPP_TAG_RANGE :
10758 sprintf(value, "%d-%d", attr->values[0].range.lower,
10759 attr->values[0].range.upper);
10760 printer->num_options = cupsAddOption(name, value,
10761 printer->num_options,
10762 &(printer->options));
10763 cupsdLogMessage(CUPSD_LOG_DEBUG,
10764 "Setting %s to %s...", attr->name, value);
10765 break;
10766
10767 case IPP_TAG_RESOLUTION :
10768 sprintf(value, "%dx%d%s", attr->values[0].resolution.xres,
10769 attr->values[0].resolution.yres,
10770 attr->values[0].resolution.units == IPP_RES_PER_INCH ?
10771 "dpi" : "dpc");
10772 printer->num_options = cupsAddOption(name, value,
10773 printer->num_options,
10774 &(printer->options));
10775 cupsdLogMessage(CUPSD_LOG_DEBUG,
10776 "Setting %s to %s...", attr->name, value);
10777 break;
10778
10779 default :
10780 /* Do nothing for other values */
10781 break;
10782 }
10783 }
10784 }
10785
10786
10787 /*
10788 * 'start_printer()' - Start a printer.
10789 */
10790
10791 static void
10792 start_printer(cupsd_client_t *con, /* I - Client connection */
10793 ipp_attribute_t *uri) /* I - Printer URI */
10794 {
10795 http_status_t status; /* Policy status */
10796 cups_ptype_t dtype; /* Destination type (printer/class) */
10797 cupsd_printer_t *printer; /* Printer data */
10798
10799
10800 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con,
10801 con->http.fd, uri->values[0].string.text);
10802
10803 /*
10804 * Is the destination valid?
10805 */
10806
10807 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10808 {
10809 /*
10810 * Bad URI...
10811 */
10812
10813 send_ipp_status(con, IPP_NOT_FOUND,
10814 _("The printer or class was not found."));
10815 return;
10816 }
10817
10818 /*
10819 * Check policy...
10820 */
10821
10822 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10823 {
10824 send_http_error(con, status, printer);
10825 return;
10826 }
10827
10828 /*
10829 * Start the printer...
10830 */
10831
10832 printer->state_message[0] = '\0';
10833
10834 cupsdStartPrinter(printer, 1);
10835
10836 if (dtype & CUPS_PRINTER_CLASS)
10837 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".",
10838 printer->name, get_username(con));
10839 else
10840 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".",
10841 printer->name, get_username(con));
10842
10843 cupsdCheckJobs();
10844
10845 /*
10846 * Everything was ok, so return OK status...
10847 */
10848
10849 con->response->request.status.status_code = IPP_OK;
10850 }
10851
10852
10853 /*
10854 * 'stop_printer()' - Stop a printer.
10855 */
10856
10857 static void
10858 stop_printer(cupsd_client_t *con, /* I - Client connection */
10859 ipp_attribute_t *uri) /* I - Printer URI */
10860 {
10861 http_status_t status; /* Policy status */
10862 cups_ptype_t dtype; /* Destination type (printer/class) */
10863 cupsd_printer_t *printer; /* Printer data */
10864 ipp_attribute_t *attr; /* printer-state-message attribute */
10865
10866
10867 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con,
10868 con->http.fd, uri->values[0].string.text);
10869
10870 /*
10871 * Is the destination valid?
10872 */
10873
10874 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10875 {
10876 /*
10877 * Bad URI...
10878 */
10879
10880 send_ipp_status(con, IPP_NOT_FOUND,
10881 _("The printer or class was not found."));
10882 return;
10883 }
10884
10885 /*
10886 * Check policy...
10887 */
10888
10889 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10890 {
10891 send_http_error(con, status, printer);
10892 return;
10893 }
10894
10895 /*
10896 * Stop the printer...
10897 */
10898
10899 if ((attr = ippFindAttribute(con->request, "printer-state-message",
10900 IPP_TAG_TEXT)) == NULL)
10901 strcpy(printer->state_message, "Paused");
10902 else
10903 {
10904 strlcpy(printer->state_message, attr->values[0].string.text,
10905 sizeof(printer->state_message));
10906 }
10907
10908 cupsdStopPrinter(printer, 1);
10909
10910 if (dtype & CUPS_PRINTER_CLASS)
10911 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".",
10912 printer->name, get_username(con));
10913 else
10914 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".",
10915 printer->name, get_username(con));
10916
10917 /*
10918 * Everything was ok, so return OK status...
10919 */
10920
10921 con->response->request.status.status_code = IPP_OK;
10922 }
10923
10924
10925 /*
10926 * 'url_encode_attr()' - URL-encode a string attribute.
10927 */
10928
10929 static void
10930 url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */
10931 char *buffer,/* I - String buffer */
10932 int bufsize)/* I - Size of buffer */
10933 {
10934 int i; /* Looping var */
10935 char *bufptr, /* Pointer into buffer */
10936 *bufend; /* End of buffer */
10937
10938
10939 strlcpy(buffer, attr->name, bufsize);
10940 bufptr = buffer + strlen(buffer);
10941 bufend = buffer + bufsize - 1;
10942
10943 for (i = 0; i < attr->num_values; i ++)
10944 {
10945 if (bufptr >= bufend)
10946 break;
10947
10948 if (i)
10949 *bufptr++ = ',';
10950 else
10951 *bufptr++ = '=';
10952
10953 if (bufptr >= bufend)
10954 break;
10955
10956 *bufptr++ = '\'';
10957
10958 bufptr = url_encode_string(attr->values[i].string.text,
10959 bufptr, bufend - bufptr + 1);
10960
10961 if (bufptr >= bufend)
10962 break;
10963
10964 *bufptr++ = '\'';
10965 }
10966
10967 *bufptr = '\0';
10968 }
10969
10970
10971 /*
10972 * 'url_encode_string()' - URL-encode a string.
10973 */
10974
10975 static char * /* O - End of string */
10976 url_encode_string(const char *s, /* I - String */
10977 char *buffer, /* I - String buffer */
10978 int bufsize) /* I - Size of buffer */
10979 {
10980 char *bufptr, /* Pointer into buffer */
10981 *bufend; /* End of buffer */
10982 static const char *hex = "0123456789ABCDEF";
10983 /* Hex digits */
10984
10985
10986 bufptr = buffer;
10987 bufend = buffer + bufsize - 1;
10988
10989 while (*s && bufptr < bufend)
10990 {
10991 if (*s == ' ' || *s == '%')
10992 {
10993 if (bufptr >= (bufend - 2))
10994 break;
10995
10996 *bufptr++ = '%';
10997 *bufptr++ = hex[(*s >> 4) & 15];
10998 *bufptr++ = hex[*s & 15];
10999
11000 s ++;
11001 }
11002 else if (*s == '\'' || *s == '\\')
11003 {
11004 if (bufptr >= (bufend - 1))
11005 break;
11006
11007 *bufptr++ = '\\';
11008 *bufptr++ = *s++;
11009 }
11010 else
11011 *bufptr++ = *s++;
11012 }
11013
11014 *bufptr = '\0';
11015
11016 return (bufptr);
11017 }
11018
11019
11020 /*
11021 * 'user_allowed()' - See if a user is allowed to print to a queue.
11022 */
11023
11024 static int /* O - 0 if not allowed, 1 if allowed */
11025 user_allowed(cupsd_printer_t *p, /* I - Printer or class */
11026 const char *username) /* I - Username */
11027 {
11028 int i; /* Looping var */
11029 struct passwd *pw; /* User password data */
11030 char baseuser[256], /* Base username */
11031 *baseptr; /* Pointer to "@" in base username */
11032
11033
11034 if (p->num_users == 0)
11035 return (1);
11036
11037 if (!strcmp(username, "root"))
11038 return (1);
11039
11040 if (strchr(username, '@'))
11041 {
11042 /*
11043 * Strip @REALM for username check...
11044 */
11045
11046 strlcpy(baseuser, username, sizeof(baseuser));
11047
11048 if ((baseptr = strchr(baseuser, '@')) != NULL)
11049 *baseptr = '\0';
11050
11051 username = baseuser;
11052 }
11053
11054 pw = getpwnam(username);
11055 endpwent();
11056
11057 for (i = 0; i < p->num_users; i ++)
11058 {
11059 if (p->users[i][0] == '@')
11060 {
11061 /*
11062 * Check group membership...
11063 */
11064
11065 if (cupsdCheckGroup(username, pw, p->users[i] + 1))
11066 break;
11067 }
11068 else if (p->users[i][0] == '#')
11069 {
11070 /*
11071 * Check UUID...
11072 */
11073
11074 if (cupsdCheckGroup(username, pw, p->users[i]))
11075 break;
11076 }
11077 else if (!strcasecmp(username, p->users[i]))
11078 break;
11079 }
11080
11081 return ((i < p->num_users) != p->deny_users);
11082 }
11083
11084
11085 /*
11086 * 'validate_job()' - Validate printer options and destination.
11087 */
11088
11089 static void
11090 validate_job(cupsd_client_t *con, /* I - Client connection */
11091 ipp_attribute_t *uri) /* I - Printer URI */
11092 {
11093 http_status_t status; /* Policy status */
11094 ipp_attribute_t *attr; /* Current attribute */
11095 ipp_attribute_t *format; /* Document-format attribute */
11096 cups_ptype_t dtype; /* Destination type (printer/class) */
11097 char super[MIME_MAX_SUPER],
11098 /* Supertype of file */
11099 type[MIME_MAX_TYPE];
11100 /* Subtype of file */
11101 cupsd_printer_t *printer; /* Printer */
11102
11103
11104 cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con,
11105 con->http.fd, uri->values[0].string.text);
11106
11107 /*
11108 * OK, see if the client is sending the document compressed - CUPS
11109 * doesn't support compression yet...
11110 */
11111
11112 if ((attr = ippFindAttribute(con->request, "compression",
11113 IPP_TAG_KEYWORD)) != NULL &&
11114 !strcmp(attr->values[0].string.text, "none"))
11115 {
11116 send_ipp_status(con, IPP_ATTRIBUTES,
11117 _("Unsupported compression attribute %s!"),
11118 attr->values[0].string.text);
11119 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
11120 "compression", NULL, attr->values[0].string.text);
11121 return;
11122 }
11123
11124 /*
11125 * Is it a format we support?
11126 */
11127
11128 if ((format = ippFindAttribute(con->request, "document-format",
11129 IPP_TAG_MIMETYPE)) != NULL)
11130 {
11131 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
11132 super, type) != 2)
11133 {
11134 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"),
11135 format->values[0].string.text);
11136 return;
11137 }
11138
11139 if ((strcmp(super, "application") || strcmp(type, "octet-stream")) &&
11140 !mimeType(MimeDatabase, super, type))
11141 {
11142 cupsdLogMessage(CUPSD_LOG_INFO,
11143 "Hint: Do you have the raw file printing rules enabled?");
11144 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
11145 _("Unsupported format \"%s\"!"),
11146 format->values[0].string.text);
11147 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
11148 "document-format", NULL, format->values[0].string.text);
11149 return;
11150 }
11151 }
11152
11153 /*
11154 * Is the destination valid?
11155 */
11156
11157 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
11158 {
11159 /*
11160 * Bad URI...
11161 */
11162
11163 send_ipp_status(con, IPP_NOT_FOUND,
11164 _("The printer or class was not found."));
11165 return;
11166 }
11167
11168 /*
11169 * Check policy...
11170 */
11171
11172 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
11173 {
11174 send_http_error(con, status, printer);
11175 return;
11176 }
11177
11178 /*
11179 * Everything was ok, so return OK status...
11180 */
11181
11182 con->response->request.status.status_code = IPP_OK;
11183 }
11184
11185
11186 /*
11187 * 'validate_name()' - Make sure the printer name only contains valid chars.
11188 */
11189
11190 static int /* O - 0 if name is no good, 1 if good */
11191 validate_name(const char *name) /* I - Name to check */
11192 {
11193 const char *ptr; /* Pointer into name */
11194
11195
11196 /*
11197 * Scan the whole name...
11198 */
11199
11200 for (ptr = name; *ptr; ptr ++)
11201 if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
11202 return (0);
11203
11204 /*
11205 * All the characters are good; validate the length, too...
11206 */
11207
11208 return ((ptr - name) < 128);
11209 }
11210
11211
11212 /*
11213 * 'validate_user()' - Validate the user for the request.
11214 */
11215
11216 static int /* O - 1 if permitted, 0 otherwise */
11217 validate_user(cupsd_job_t *job, /* I - Job */
11218 cupsd_client_t *con, /* I - Client connection */
11219 const char *owner, /* I - Owner of job/resource */
11220 char *username, /* O - Authenticated username */
11221 int userlen) /* I - Length of username */
11222 {
11223 cupsd_printer_t *printer; /* Printer for job */
11224
11225
11226 cupsdLogMessage(CUPSD_LOG_DEBUG2,
11227 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
11228 "userlen=%d)",
11229 job->id, con ? con->http.fd : 0,
11230 owner ? owner : "(null)", username, userlen);
11231
11232 /*
11233 * Validate input...
11234 */
11235
11236 if (!con || !owner || !username || userlen <= 0)
11237 return (0);
11238
11239 /*
11240 * Get the best authenticated username that is available.
11241 */
11242
11243 strlcpy(username, get_username(con), userlen);
11244
11245 /*
11246 * Check the username against the owner...
11247 */
11248
11249 printer = cupsdFindDest(job->dest);
11250
11251 return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr,
11252 con, owner) == HTTP_OK);
11253 }
11254
11255
11256 /*
11257 * End of "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $".
11258 */