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