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