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