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