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