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