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