]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/ipp.c
Merge changes from CUPS 1.7svn-r10814.
[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 *
dcb445bc 6 * Copyright 2007-2012 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 {
4220952d 3273 if (!cupsdFindJob(job_ids->values[i].integer))
aaf19ab0
MS
3274 break;
3275 }
3276
3277 if (i < job_ids->num_values)
3278 {
84315f46 3279 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
aaf19ab0
MS
3280 job_ids->values[i].integer);
3281 return;
3282 }
ef416fc2 3283
aaf19ab0
MS
3284 for (i = 0; i < job_ids->num_values; i ++)
3285 {
3286 job = cupsdFindJob(job_ids->values[i].integer);
ef416fc2 3287
aaf19ab0
MS
3288 cupsdSetJobState(job, IPP_JOB_CANCELED, purge,
3289 purge == CUPSD_JOB_PURGE ? "Job purged by user." :
3290 "Job canceled by user.");
3291 }
3292
3293 cupsdLogMessage(CUPSD_LOG_INFO, "Selected jobs were %s by \"%s\".",
3294 purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
3295 get_username(con));
3296 }
3297 else
3298 {
3299 /*
3300 * Cancel all jobs on all printers...
3301 */
3302
3303 cupsdCancelJobs(NULL, username, purge);
3304
3305 cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".",
3306 purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
3307 get_username(con));
3308 }
ef416fc2 3309 }
b423cd4c 3310 else
ef416fc2 3311 {
3312 /*
b423cd4c 3313 * Check policy...
ef416fc2 3314 */
3315
f7deaa1a 3316 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
3317 NULL)) != HTTP_OK)
b423cd4c 3318 {
f899b121 3319 send_http_error(con, status, printer);
b423cd4c 3320 return;
3321 }
ef416fc2 3322
aaf19ab0
MS
3323 if (job_ids)
3324 {
3325 for (i = 0; i < job_ids->num_values; i ++)
3326 {
3327 if ((job = cupsdFindJob(job_ids->values[i].integer)) == NULL ||
88f9aafc 3328 _cups_strcasecmp(job->dest, printer->name))
aaf19ab0
MS
3329 break;
3330 }
b423cd4c 3331
aaf19ab0
MS
3332 if (i < job_ids->num_values)
3333 {
84315f46 3334 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
aaf19ab0
MS
3335 job_ids->values[i].integer);
3336 return;
3337 }
b423cd4c 3338
aaf19ab0
MS
3339 for (i = 0; i < job_ids->num_values; i ++)
3340 {
3341 job = cupsdFindJob(job_ids->values[i].integer);
3342
3343 cupsdSetJobState(job, IPP_JOB_CANCELED, purge,
3344 purge == CUPSD_JOB_PURGE ? "Job purged by user." :
3345 "Job canceled by user.");
3346 }
3347
3348 cupsdLogMessage(CUPSD_LOG_INFO, "Selected jobs were %s by \"%s\".",
3349 purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
3350 get_username(con));
3351 }
3352 else
3353 {
3354 /*
3355 * Cancel all of the jobs on the named printer...
3356 */
3357
3358 cupsdCancelJobs(printer->name, username, purge);
3359
3360 cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".",
3361 printer->name,
3362 purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
3363 get_username(con));
3364 }
ef416fc2 3365 }
3366
b423cd4c 3367 con->response->request.status.status_code = IPP_OK;
3368}
3369
3370
3371/*
3372 * 'cancel_job()' - Cancel a print job.
3373 */
3374
3375static void
3376cancel_job(cupsd_client_t *con, /* I - Client connection */
3377 ipp_attribute_t *uri) /* I - Job or Printer URI */
3378{
3379 ipp_attribute_t *attr; /* Current attribute */
3380 int jobid; /* Job ID */
f7deaa1a 3381 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
b423cd4c 3382 username[HTTP_MAX_URI], /* Username portion of URI */
3383 host[HTTP_MAX_URI], /* Host portion of URI */
3384 resource[HTTP_MAX_URI]; /* Resource portion of URI */
3385 int port; /* Port portion of URI */
3386 cupsd_job_t *job; /* Job information */
bc44d920 3387 cups_ptype_t dtype; /* Destination type (printer/class) */
b423cd4c 3388 cupsd_printer_t *printer; /* Printer data */
68b10830 3389 cupsd_jobaction_t purge; /* Purge the job? */
b423cd4c 3390
3391
3392 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con,
3393 con->http.fd, uri->values[0].string.text);
3394
ef416fc2 3395 /*
b423cd4c 3396 * See if we have a job URI or a printer URI...
ef416fc2 3397 */
3398
b423cd4c 3399 if (!strcmp(uri->name, "printer-uri"))
3400 {
3401 /*
3402 * Got a printer URI; see if we also have a job-id attribute...
3403 */
3404
3405 if ((attr = ippFindAttribute(con->request, "job-id",
3406 IPP_TAG_INTEGER)) == NULL)
3407 {
3408 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 3409 _("Got a printer-uri attribute but no job-id."));
b423cd4c 3410 return;
3411 }
3412
3413 if ((jobid = attr->values[0].integer) == 0)
ef416fc2 3414 {
3415 /*
b423cd4c 3416 * Find the current job on the specified printer...
ef416fc2 3417 */
3418
f7deaa1a 3419 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 3420 {
3421 /*
b423cd4c 3422 * Bad URI...
ef416fc2 3423 */
3424
b423cd4c 3425 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 3426 _("The printer or class does not exist."));
b423cd4c 3427 return;
ef416fc2 3428 }
3429
3430 /*
b9faaae1 3431 * See if there are any pending jobs...
ef416fc2 3432 */
3433
b9faaae1
MS
3434 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
3435 job;
3436 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
3437 if (job->state_value <= IPP_JOB_PROCESSING &&
88f9aafc 3438 !_cups_strcasecmp(job->dest, printer->name))
b9faaae1
MS
3439 break;
3440
3441 if (job)
3442 jobid = job->id;
ef416fc2 3443 else
ef416fc2 3444 {
3445 /*
b9faaae1 3446 * No, try stopped jobs...
ef416fc2 3447 */
f7deaa1a 3448
b9faaae1 3449 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
b423cd4c 3450 job;
3451 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
b9faaae1 3452 if (job->state_value == IPP_JOB_STOPPED &&
88f9aafc 3453 !_cups_strcasecmp(job->dest, printer->name))
b423cd4c 3454 break;
ef416fc2 3455
b423cd4c 3456 if (job)
3457 jobid = job->id;
3458 else
ef416fc2 3459 {
84315f46 3460 send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s."),
b9faaae1
MS
3461 printer->name);
3462 return;
b423cd4c 3463 }
3464 }
3465 }
3466 }
3467 else
3468 {
3469 /*
3470 * Got a job URI; parse it to get the job ID...
3471 */
ef416fc2 3472
f7deaa1a 3473 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
3474 sizeof(scheme), username, sizeof(username), host,
b423cd4c 3475 sizeof(host), &port, resource, sizeof(resource));
f7deaa1a 3476
b423cd4c 3477 if (strncmp(resource, "/jobs/", 6))
3478 {
3479 /*
3480 * Not a valid URI!
3481 */
ef416fc2 3482
84315f46 3483 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
b423cd4c 3484 uri->values[0].string.text);
3485 return;
3486 }
3487
3488 jobid = atoi(resource + 6);
3489 }
3490
0a682745
MS
3491 /*
3492 * Look for the "purge-job" attribute...
3493 */
3494
3495 if ((attr = ippFindAttribute(con->request, "purge-job",
3496 IPP_TAG_BOOLEAN)) != NULL)
68b10830 3497 purge = attr->values[0].boolean ? CUPSD_JOB_PURGE : CUPSD_JOB_DEFAULT;
0a682745 3498 else
68b10830 3499 purge = CUPSD_JOB_DEFAULT;
0a682745 3500
b423cd4c 3501 /*
3502 * See if the job exists...
3503 */
3504
3505 if ((job = cupsdFindJob(jobid)) == NULL)
3506 {
3507 /*
3508 * Nope - return a "not found" error...
3509 */
3510
84315f46 3511 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
b423cd4c 3512 return;
3513 }
3514
3515 /*
3516 * See if the job is owned by the requesting user...
3517 */
3518
3519 if (!validate_user(job, con, job->username, username, sizeof(username)))
3520 {
b0f6947b
MS
3521 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
3522 cupsdFindDest(job->dest));
b423cd4c 3523 return;
3524 }
3525
3526 /*
d09495fa 3527 * See if the job is already completed, canceled, or aborted; if so,
b423cd4c 3528 * we can't cancel...
3529 */
3530
68b10830 3531 if (job->state_value >= IPP_JOB_CANCELED && purge != CUPSD_JOB_PURGE)
b423cd4c 3532 {
e1d6a774 3533 switch (job->state_value)
3534 {
d09495fa 3535 case IPP_JOB_CANCELED :
e1d6a774 3536 send_ipp_status(con, IPP_NOT_POSSIBLE,
d09495fa 3537 _("Job #%d is already canceled - can\'t cancel."),
e1d6a774 3538 jobid);
3539 break;
3540
3541 case IPP_JOB_ABORTED :
3542 send_ipp_status(con, IPP_NOT_POSSIBLE,
3543 _("Job #%d is already aborted - can\'t cancel."),
3544 jobid);
3545 break;
3546
3547 default :
3548 send_ipp_status(con, IPP_NOT_POSSIBLE,
3549 _("Job #%d is already completed - can\'t cancel."),
3550 jobid);
3551 break;
3552 }
3553
b423cd4c 3554 return;
3555 }
ef416fc2 3556
b423cd4c 3557 /*
3558 * Cancel the job and return...
3559 */
ef416fc2 3560
b9faaae1 3561 cupsdSetJobState(job, IPP_JOB_CANCELED, purge,
68b10830
MS
3562 purge == CUPSD_JOB_PURGE ? "Job purged by \"%s\"" :
3563 "Job canceled by \"%s\"",
b9faaae1 3564 username);
b423cd4c 3565 cupsdCheckJobs();
ef416fc2 3566
68b10830 3567 if (purge == CUPSD_JOB_PURGE)
0a682745 3568 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Purged by \"%s\".", jobid,
75bd9771 3569 username);
0a682745
MS
3570 else
3571 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Canceled by \"%s\".", jobid,
75bd9771 3572 username);
ef416fc2 3573
b423cd4c 3574 con->response->request.status.status_code = IPP_OK;
3575}
ef416fc2 3576
ef416fc2 3577
b423cd4c 3578/*
3579 * 'cancel_subscription()' - Cancel a subscription.
3580 */
ef416fc2 3581
b423cd4c 3582static void
3583cancel_subscription(
3584 cupsd_client_t *con, /* I - Client connection */
3585 int sub_id) /* I - Subscription ID */
3586{
3587 http_status_t status; /* Policy status */
3588 cupsd_subscription_t *sub; /* Subscription */
ef416fc2 3589
ef416fc2 3590
b423cd4c 3591 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3592 "cancel_subscription(con=%p[%d], sub_id=%d)",
3593 con, con->http.fd, sub_id);
ef416fc2 3594
b423cd4c 3595 /*
3596 * Is the subscription ID valid?
3597 */
ef416fc2 3598
b423cd4c 3599 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
3600 {
3601 /*
3602 * Bad subscription ID...
3603 */
ef416fc2 3604
b423cd4c 3605 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 3606 _("Subscription #%d does not exist."), sub_id);
b423cd4c 3607 return;
3608 }
ef416fc2 3609
b423cd4c 3610 /*
3611 * Check policy...
3612 */
ef416fc2 3613
b423cd4c 3614 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
3615 DefaultPolicyPtr,
3616 con, sub->owner)) != HTTP_OK)
3617 {
f899b121 3618 send_http_error(con, status, sub->dest);
b423cd4c 3619 return;
3620 }
ef416fc2 3621
b423cd4c 3622 /*
3623 * Cancel the subscription...
3624 */
ef416fc2 3625
b423cd4c 3626 cupsdDeleteSubscription(sub, 1);
ef416fc2 3627
b423cd4c 3628 con->response->request.status.status_code = IPP_OK;
ef416fc2 3629}
3630
3631
3dfe78b3
MS
3632/*
3633 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
3634 */
3635
3636static int /* O - 1 if OK, 0 if not */
3637check_rss_recipient(
3638 const char *recipient) /* I - Recipient URI */
3639{
3640 cupsd_subscription_t *sub; /* Current subscription */
3641
3642
3643 for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions);
3644 sub;
3645 sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
3646 if (sub->recipient)
3647 {
3648 /*
3649 * Compare the URIs up to the first ?...
3650 */
3651
3652 const char *r1, *r2;
3653
3654 for (r1 = recipient, r2 = sub->recipient;
3655 *r1 == *r2 && *r1 && *r1 != '?' && *r2 && *r2 != '?';
3656 r1 ++, r2 ++);
3657
3658 if (*r1 == *r2)
3659 return (0);
3660 }
3661
3662 return (1);
3663}
3664
3665
ef416fc2 3666/*
b423cd4c 3667 * 'check_quotas()' - Check quotas for a printer and user.
ef416fc2 3668 */
3669
6c48a6ca
MS
3670static int /* O - 1 if OK, 0 if forbidden,
3671 -1 if limit reached */
b423cd4c 3672check_quotas(cupsd_client_t *con, /* I - Client connection */
3673 cupsd_printer_t *p) /* I - Printer or class */
ef416fc2 3674{
10d09e33
MS
3675 char username[33], /* Username */
3676 *name; /* Current user name */
b423cd4c 3677 cupsd_quota_t *q; /* Quota data */
7594b224 3678#ifdef HAVE_MBR_UID_TO_UUID
3679 /*
3680 * Use Apple membership APIs which require that all names represent
3681 * valid user account or group records accessible by the server.
3682 */
3683
3684 uuid_t usr_uuid; /* UUID for job requesting user */
3685 uuid_t usr2_uuid; /* UUID for ACL user name entry */
3686 uuid_t grp_uuid; /* UUID for ACL group name entry */
3687 int mbr_err; /* Error from membership function */
3688 int is_member; /* Is this user a member? */
3689#else
3690 /*
3691 * Use standard POSIX APIs for checking users and groups...
3692 */
3693
b423cd4c 3694 struct passwd *pw; /* User password data */
7594b224 3695#endif /* HAVE_MBR_UID_TO_UUID */
ef416fc2 3696
3697
b423cd4c 3698 cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])",
3699 con, con->http.fd, p, p->name);
ef416fc2 3700
b423cd4c 3701 /*
3702 * Figure out who is printing...
3703 */
3704
3705 strlcpy(username, get_username(con), sizeof(username));
3706
a2326b5b
MS
3707 if ((name = strchr(username, '@')) != NULL)
3708 *name = '\0'; /* Strip @REALM */
3709
b423cd4c 3710 /*
3711 * Check global active job limits for printers and users...
3712 */
3713
3714 if (MaxJobsPerPrinter)
ef416fc2 3715 {
b423cd4c 3716 /*
3717 * Check if there are too many pending jobs on this printer...
3718 */
3719
3720 if (cupsdGetPrinterJobCount(p->name) >= MaxJobsPerPrinter)
3721 {
3722 cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for printer \"%s\"...",
3723 p->name);
6c48a6ca 3724 return (-1);
b423cd4c 3725 }
3726 }
3727
3728 if (MaxJobsPerUser)
3729 {
3730 /*
3731 * Check if there are too many pending jobs for this user...
3732 */
3733
3734 if (cupsdGetUserJobCount(username) >= MaxJobsPerUser)
3735 {
3736 cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for user \"%s\"...",
3737 username);
6c48a6ca 3738 return (-1);
b423cd4c 3739 }
ef416fc2 3740 }
3741
3742 /*
b423cd4c 3743 * Check against users...
ef416fc2 3744 */
3745
10d09e33 3746 if (cupsArrayCount(p->users) == 0 && p->k_limit == 0 && p->page_limit == 0)
b423cd4c 3747 return (1);
3748
10d09e33 3749 if (cupsArrayCount(p->users))
b423cd4c 3750 {
7594b224 3751#ifdef HAVE_MBR_UID_TO_UUID
3752 /*
3753 * Get UUID for job requesting user...
3754 */
3755
3756 if (mbr_user_name_to_uuid((char *)username, usr_uuid))
3757 {
3758 /*
3759 * Unknown user...
3760 */
3761
3762 cupsdLogMessage(CUPSD_LOG_DEBUG,
3763 "check_quotas: UUID lookup failed for user \"%s\"",
3764 username);
3765 cupsdLogMessage(CUPSD_LOG_INFO,
3766 "Denying user \"%s\" access to printer \"%s\" "
3767 "(unknown user)...",
3768 username, p->name);
3769 return (0);
3770 }
3771#else
3772 /*
3773 * Get UID and GID of requesting user...
3774 */
3775
b423cd4c 3776 pw = getpwnam(username);
3777 endpwent();
7594b224 3778#endif /* HAVE_MBR_UID_TO_UUID */
b423cd4c 3779
10d09e33
MS
3780 for (name = (char *)cupsArrayFirst(p->users);
3781 name;
3782 name = (char *)cupsArrayNext(p->users))
3783 if (name[0] == '@')
b423cd4c 3784 {
3785 /*
3786 * Check group membership...
3787 */
3788
7594b224 3789#ifdef HAVE_MBR_UID_TO_UUID
10d09e33 3790 if (name[1] == '#')
c9fc04c6 3791 {
10d09e33 3792 if (uuid_parse(name + 2, grp_uuid))
c9fc04c6
MS
3793 uuid_clear(grp_uuid);
3794 }
10d09e33 3795 else if ((mbr_err = mbr_group_name_to_uuid(name + 1, grp_uuid)) != 0)
7594b224 3796 {
3797 /*
3798 * Invalid ACL entries are ignored for matching; just record a
3799 * warning in the log...
3800 */
3801
3802 cupsdLogMessage(CUPSD_LOG_DEBUG,
3803 "check_quotas: UUID lookup failed for ACL entry "
10d09e33 3804 "\"%s\" (err=%d)", name, mbr_err);
7594b224 3805 cupsdLogMessage(CUPSD_LOG_WARN,
3806 "Access control entry \"%s\" not a valid group name; "
10d09e33 3807 "entry ignored", name);
7594b224 3808 }
7594b224 3809
c9fc04c6
MS
3810 if ((mbr_err = mbr_check_membership(usr_uuid, grp_uuid,
3811 &is_member)) != 0)
3812 {
3813 /*
3814 * At this point, there should be no errors, but check anyways...
7594b224 3815 */
3816
c9fc04c6
MS
3817 cupsdLogMessage(CUPSD_LOG_DEBUG,
3818 "check_quotas: group \"%s\" membership check "
10d09e33 3819 "failed (err=%d)", name + 1, mbr_err);
c9fc04c6 3820 is_member = 0;
7594b224 3821 }
c9fc04c6
MS
3822
3823 /*
3824 * Stop if we found a match...
3825 */
3826
3827 if (is_member)
3828 break;
3829
7594b224 3830#else
10d09e33 3831 if (cupsdCheckGroup(username, pw, name + 1))
b423cd4c 3832 break;
7594b224 3833#endif /* HAVE_MBR_UID_TO_UUID */
3834 }
3835#ifdef HAVE_MBR_UID_TO_UUID
3836 else
3837 {
10d09e33 3838 if (name[0] == '#')
c9fc04c6 3839 {
10d09e33 3840 if (uuid_parse(name + 1, usr2_uuid))
c9fc04c6
MS
3841 uuid_clear(usr2_uuid);
3842 }
10d09e33 3843 else if ((mbr_err = mbr_user_name_to_uuid(name, usr2_uuid)) != 0)
7594b224 3844 {
3845 /*
3846 * Invalid ACL entries are ignored for matching; just record a
3847 * warning in the log...
3848 */
3849
3850 cupsdLogMessage(CUPSD_LOG_DEBUG,
3851 "check_quotas: UUID lookup failed for ACL entry "
10d09e33 3852 "\"%s\" (err=%d)", name, mbr_err);
7594b224 3853 cupsdLogMessage(CUPSD_LOG_WARN,
3854 "Access control entry \"%s\" not a valid user name; "
10d09e33 3855 "entry ignored", name);
7594b224 3856 }
7594b224 3857
d1c13e16 3858 if (!uuid_compare(usr_uuid, usr2_uuid))
c9fc04c6 3859 break;
b423cd4c 3860 }
7594b224 3861#else
88f9aafc 3862 else if (!_cups_strcasecmp(username, name))
b423cd4c 3863 break;
7594b224 3864#endif /* HAVE_MBR_UID_TO_UUID */
b423cd4c 3865
10d09e33 3866 if ((name != NULL) == p->deny_users)
ef416fc2 3867 {
b423cd4c 3868 cupsdLogMessage(CUPSD_LOG_INFO,
3869 "Denying user \"%s\" access to printer \"%s\"...",
3870 username, p->name);
3871 return (0);
ef416fc2 3872 }
b423cd4c 3873 }
ef416fc2 3874
3875 /*
b423cd4c 3876 * Check quotas...
ef416fc2 3877 */
3878
b423cd4c 3879 if (p->k_limit || p->page_limit)
3880 {
3881 if ((q = cupsdUpdateQuota(p, username, 0, 0)) == NULL)
3882 {
3883 cupsdLogMessage(CUPSD_LOG_ERROR,
4d301e69 3884 "Unable to allocate quota data for user \"%s\"",
b423cd4c 3885 username);
bc44d920 3886 return (-1);
b423cd4c 3887 }
ef416fc2 3888
b423cd4c 3889 if ((q->k_count >= p->k_limit && p->k_limit) ||
3890 (q->page_count >= p->page_limit && p->page_limit))
3891 {
3892 cupsdLogMessage(CUPSD_LOG_INFO, "User \"%s\" is over the quota limit...",
3893 username);
bc44d920 3894 return (-1);
b423cd4c 3895 }
3896 }
3897
3898 /*
3899 * If we have gotten this far, we're done!
3900 */
3901
3902 return (1);
ef416fc2 3903}
3904
3905
aaf19ab0
MS
3906/*
3907 * 'close_job()' - Close a multi-file job.
3908 */
3909
3910static void
3911close_job(cupsd_client_t *con, /* I - Client connection */
3912 ipp_attribute_t *uri) /* I - Printer URI */
3913{
3914 cupsd_job_t *job; /* Job */
aaf19ab0
MS
3915 ipp_attribute_t *attr; /* Attribute */
3916 char job_uri[HTTP_MAX_URI],
3917 /* Job URI */
3918 username[256]; /* User name */
3919
3920
3921 cupsdLogMessage(CUPSD_LOG_DEBUG2, "close_job(%p[%d], %s)", con,
3922 con->http.fd, uri->values[0].string.text);
3923
3924 /*
3925 * See if we have a job URI or a printer URI...
3926 */
3927
3928 if (strcmp(uri->name, "printer-uri"))
3929 {
3930 /*
3931 * job-uri is not supported by Close-Job!
3932 */
3933
3934 send_ipp_status(con, IPP_BAD_REQUEST,
3935 _("Close-Job doesn't support the job-uri attribute."));
3936 return;
3937 }
3938
3939 /*
3940 * Got a printer URI; see if we also have a job-id attribute...
3941 */
3942
3943 if ((attr = ippFindAttribute(con->request, "job-id",
3944 IPP_TAG_INTEGER)) == NULL)
3945 {
3946 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 3947 _("Got a printer-uri attribute but no job-id."));
aaf19ab0
MS
3948 return;
3949 }
3950
3951 if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
3952 {
3953 /*
3954 * Nope - return a "not found" error...
3955 */
3956
84315f46 3957 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
aaf19ab0
MS
3958 attr->values[0].integer);
3959 return;
3960 }
3961
aaf19ab0
MS
3962 /*
3963 * See if the job is owned by the requesting user...
3964 */
3965
3966 if (!validate_user(job, con, job->username, username, sizeof(username)))
3967 {
3968 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
3969 cupsdFindDest(job->dest));
3970 return;
3971 }
3972
3973 /*
3974 * Add any ending sheet...
3975 */
3976
3977 if (cupsdTimeoutJob(job))
3978 return;
3979
3980 if (job->state_value == IPP_JOB_STOPPED)
3981 {
3982 job->state->values[0].integer = IPP_JOB_PENDING;
3983 job->state_value = IPP_JOB_PENDING;
3984 }
3985 else if (job->state_value == IPP_JOB_HELD)
3986 {
3987 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
3988 IPP_TAG_KEYWORD)) == NULL)
3989 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
3990
3991 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
3992 {
3993 job->state->values[0].integer = IPP_JOB_PENDING;
3994 job->state_value = IPP_JOB_PENDING;
3995 }
3996 }
3997
3998 job->dirty = 1;
3999 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
4000
4001 /*
4002 * Fill in the response info...
4003 */
4004
83e08001
MS
4005 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
4006 con->servername, con->serverport, "/jobs/%d", job->id);
aaf19ab0
MS
4007 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
4008 job_uri);
4009
4010 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
4011
4012 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
4013 job->state_value);
4014
aaf19ab0
MS
4015 con->response->request.status.status_code = IPP_OK;
4016
4017 /*
4018 * Start the job if necessary...
4019 */
4020
4021 cupsdCheckJobs();
4022}
4023
4024
b423cd4c 4025/*
4026 * 'copy_attrs()' - Copy attributes from one request to another.
4027 */
4028
4029static void
4030copy_attrs(ipp_t *to, /* I - Destination request */
4031 ipp_t *from, /* I - Source request */
4032 cups_array_t *ra, /* I - Requested attributes */
4033 ipp_tag_t group, /* I - Group to copy */
10d09e33
MS
4034 int quickcopy, /* I - Do a quick copy? */
4035 cups_array_t *exclude) /* I - Attributes to exclude? */
b423cd4c 4036{
4037 ipp_attribute_t *fromattr; /* Source attribute */
ef416fc2 4038
ef416fc2 4039
b423cd4c 4040 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4041 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4042 to, from, ra, group, quickcopy);
ef416fc2 4043
b423cd4c 4044 if (!to || !from)
4045 return;
e00b005a 4046
b423cd4c 4047 for (fromattr = from->attrs; fromattr; fromattr = fromattr->next)
ef416fc2 4048 {
4049 /*
b423cd4c 4050 * Filter attributes as needed...
ef416fc2 4051 */
4052
2abf387c 4053 if ((group != IPP_TAG_ZERO && fromattr->group_tag != group &&
4054 fromattr->group_tag != IPP_TAG_ZERO) || !fromattr->name)
b423cd4c 4055 continue;
4056
88f9aafc
MS
4057 if (!strcmp(fromattr->name, "job-printer-uri"))
4058 continue;
4059
c8fef167 4060 if (exclude &&
10d09e33
MS
4061 (cupsArrayFind(exclude, fromattr->name) ||
4062 cupsArrayFind(exclude, "all")))
4063 {
4064 /*
4065 * We need to exclude this attribute for security reasons; we require the
88f9aafc
MS
4066 * job-id attribute regardless of the security settings for IPP
4067 * conformance.
4068 *
4069 * The job-printer-uri attribute is handled by copy_job_attrs().
10d09e33
MS
4070 *
4071 * Subscription attribute security is handled by copy_subscription_attrs().
4072 */
4073
88f9aafc 4074 if (strcmp(fromattr->name, "job-id"))
10d09e33
MS
4075 continue;
4076 }
4077
b423cd4c 4078 if (!ra || cupsArrayFind(ra, fromattr->name))
d2354e63
MS
4079 {
4080 /*
4081 * Don't send collection attributes by default to IPP/1.x clients
54afec33
MS
4082 * since many do not support collections. Also don't send
4083 * media-col-database unless specifically requested by the client.
d2354e63
MS
4084 */
4085
4086 if (fromattr->value_tag == IPP_TAG_BEGIN_COLLECTION &&
54afec33
MS
4087 !ra &&
4088 (to->request.status.version[0] == 1 ||
4089 !strcmp(fromattr->name, "media-col-database")))
d2354e63
MS
4090 continue;
4091
a2326b5b 4092 ippCopyAttribute(to, fromattr, quickcopy);
d2354e63 4093 }
ef416fc2 4094 }
b423cd4c 4095}
ef416fc2 4096
ef416fc2 4097
b423cd4c 4098/*
4099 * 'copy_banner()' - Copy a banner file to the requests directory for the
4100 * specified job.
4101 */
ef416fc2 4102
b423cd4c 4103static int /* O - Size of banner file in kbytes */
4104copy_banner(cupsd_client_t *con, /* I - Client connection */
4105 cupsd_job_t *job, /* I - Job information */
4106 const char *name) /* I - Name of banner */
4107{
4108 int i; /* Looping var */
4109 int kbytes; /* Size of banner file in kbytes */
4110 char filename[1024]; /* Job filename */
4111 cupsd_banner_t *banner; /* Pointer to banner */
4112 cups_file_t *in; /* Input file */
4113 cups_file_t *out; /* Output file */
4114 int ch; /* Character from file */
4115 char attrname[255], /* Name of attribute */
4116 *s; /* Pointer into name */
4117 ipp_attribute_t *attr; /* Attribute */
ef416fc2 4118
ef416fc2 4119
3dfe78b3
MS
4120 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4121 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
09a101d6 4122 con, con ? con->http.fd : -1, job, job->id,
4123 name ? name : "(null)");
ef416fc2 4124
b423cd4c 4125 /*
4126 * Find the banner; return if not found or "none"...
4127 */
ef416fc2 4128
b423cd4c 4129 if (!name || !strcmp(name, "none") ||
4130 (banner = cupsdFindBanner(name)) == NULL)
4131 return (0);
ef416fc2 4132
b423cd4c 4133 /*
4134 * Open the banner and job files...
4135 */
ef416fc2 4136
b423cd4c 4137 if (add_file(con, job, banner->filetype, 0))
91c84a35 4138 return (-1);
ef416fc2 4139
b423cd4c 4140 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
4141 job->num_files);
4142 if ((out = cupsFileOpen(filename, "w")) == NULL)
4143 {
4144 cupsdLogMessage(CUPSD_LOG_ERROR,
3dfe78b3 4145 "Unable to create banner job file %s - %s",
b423cd4c 4146 filename, strerror(errno));
4147 job->num_files --;
4148 return (0);
4149 }
4150
4151 fchmod(cupsFileNumber(out), 0640);
4152 fchown(cupsFileNumber(out), RunUser, Group);
ef416fc2 4153
4154 /*
b423cd4c 4155 * Try the localized banner file under the subdirectory...
ef416fc2 4156 */
4157
80ca4592 4158 strlcpy(attrname, job->attrs->attrs->next->values[0].string.text,
b423cd4c 4159 sizeof(attrname));
4160 if (strlen(attrname) > 2 && attrname[2] == '-')
ef416fc2 4161 {
4162 /*
b423cd4c 4163 * Convert ll-cc to ll_CC...
ef416fc2 4164 */
4165
b423cd4c 4166 attrname[2] = '_';
4167 attrname[3] = toupper(attrname[3] & 255);
4168 attrname[4] = toupper(attrname[4] & 255);
4169 }
ef416fc2 4170
b423cd4c 4171 snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir,
4172 attrname, name);
ef416fc2 4173
b423cd4c 4174 if (access(filename, 0) && strlen(attrname) > 2)
ef416fc2 4175 {
4176 /*
b423cd4c 4177 * Wasn't able to find "ll_CC" locale file; try the non-national
4178 * localization banner directory.
ef416fc2 4179 */
4180
b423cd4c 4181 attrname[2] = '\0';
ef416fc2 4182
b423cd4c 4183 snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir,
4184 attrname, name);
ef416fc2 4185 }
b423cd4c 4186
4187 if (access(filename, 0))
ef416fc2 4188 {
4189 /*
b423cd4c 4190 * Use the non-localized banner file.
ef416fc2 4191 */
4192
b423cd4c 4193 snprintf(filename, sizeof(filename), "%s/banners/%s", DataDir, name);
4194 }
ef416fc2 4195
b423cd4c 4196 if ((in = cupsFileOpen(filename, "r")) == NULL)
4197 {
4198 cupsFileClose(out);
4199 unlink(filename);
4200 cupsdLogMessage(CUPSD_LOG_ERROR,
3dfe78b3 4201 "Unable to open banner template file %s - %s",
b423cd4c 4202 filename, strerror(errno));
4203 job->num_files --;
4204 return (0);
ef416fc2 4205 }
4206
4207 /*
b423cd4c 4208 * Parse the file to the end...
ef416fc2 4209 */
4210
b423cd4c 4211 while ((ch = cupsFileGetChar(in)) != EOF)
4212 if (ch == '{')
4213 {
4214 /*
4215 * Get an attribute name...
4216 */
ef416fc2 4217
b423cd4c 4218 for (s = attrname; (ch = cupsFileGetChar(in)) != EOF;)
4219 if (!isalpha(ch & 255) && ch != '-' && ch != '?')
4220 break;
4221 else if (s < (attrname + sizeof(attrname) - 1))
4222 *s++ = ch;
4223 else
4224 break;
ef416fc2 4225
b423cd4c 4226 *s = '\0';
4227
4228 if (ch != '}')
4229 {
4230 /*
4231 * Ignore { followed by stuff that is not an attribute name...
4232 */
4233
4234 cupsFilePrintf(out, "{%s%c", attrname, ch);
4235 continue;
4236 }
ef416fc2 4237
ef416fc2 4238 /*
b423cd4c 4239 * See if it is defined...
ef416fc2 4240 */
4241
b423cd4c 4242 if (attrname[0] == '?')
4243 s = attrname + 1;
4244 else
4245 s = attrname;
ef416fc2 4246
b423cd4c 4247 if (!strcmp(s, "printer-name"))
4248 {
4249 cupsFilePuts(out, job->dest);
4250 continue;
ef416fc2 4251 }
b423cd4c 4252 else if ((attr = ippFindAttribute(job->attrs, s, IPP_TAG_ZERO)) == NULL)
4253 {
4254 /*
4255 * See if we have a leading question mark...
4256 */
ef416fc2 4257
b423cd4c 4258 if (attrname[0] != '?')
4259 {
4260 /*
4261 * Nope, write to file as-is; probably a PostScript procedure...
4262 */
ef416fc2 4263
b423cd4c 4264 cupsFilePrintf(out, "{%s}", attrname);
4265 }
ef416fc2 4266
b423cd4c 4267 continue;
4268 }
ef416fc2 4269
b423cd4c 4270 /*
4271 * Output value(s)...
4272 */
ef416fc2 4273
b423cd4c 4274 for (i = 0; i < attr->num_values; i ++)
4275 {
4276 if (i)
4277 cupsFilePutChar(out, ',');
ef416fc2 4278
b423cd4c 4279 switch (attr->value_tag)
4280 {
4281 case IPP_TAG_INTEGER :
4282 case IPP_TAG_ENUM :
4283 if (!strncmp(s, "time-at-", 8))
dfd5680b 4284 {
0268488e
MS
4285 struct timeval tv; /* Time value */
4286
4287 tv.tv_sec = attr->values[i].integer;
4288 tv.tv_usec = 0;
4289
dfd5680b
MS
4290 cupsFilePuts(out, cupsdGetDateTime(&tv, CUPSD_TIME_STANDARD));
4291 }
b423cd4c 4292 else
4293 cupsFilePrintf(out, "%d", attr->values[i].integer);
4294 break;
ef416fc2 4295
b423cd4c 4296 case IPP_TAG_BOOLEAN :
4297 cupsFilePrintf(out, "%d", attr->values[i].boolean);
4298 break;
ef416fc2 4299
b423cd4c 4300 case IPP_TAG_NOVALUE :
4301 cupsFilePuts(out, "novalue");
4302 break;
ef416fc2 4303
b423cd4c 4304 case IPP_TAG_RANGE :
4305 cupsFilePrintf(out, "%d-%d", attr->values[i].range.lower,
4306 attr->values[i].range.upper);
4307 break;
fa73b229 4308
b423cd4c 4309 case IPP_TAG_RESOLUTION :
4310 cupsFilePrintf(out, "%dx%d%s", attr->values[i].resolution.xres,
4311 attr->values[i].resolution.yres,
4312 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
3e7fe0ca 4313 "dpi" : "dpcm");
b423cd4c 4314 break;
fa73b229 4315
b423cd4c 4316 case IPP_TAG_URI :
4317 case IPP_TAG_STRING :
4318 case IPP_TAG_TEXT :
4319 case IPP_TAG_NAME :
4320 case IPP_TAG_KEYWORD :
4321 case IPP_TAG_CHARSET :
4322 case IPP_TAG_LANGUAGE :
88f9aafc 4323 if (!_cups_strcasecmp(banner->filetype->type, "postscript"))
b423cd4c 4324 {
4325 /*
4326 * Need to quote strings for PS banners...
4327 */
fa73b229 4328
b423cd4c 4329 const char *p;
fa73b229 4330
b423cd4c 4331 for (p = attr->values[i].string.text; *p; p ++)
4332 {
4333 if (*p == '(' || *p == ')' || *p == '\\')
4334 {
4335 cupsFilePutChar(out, '\\');
4336 cupsFilePutChar(out, *p);
4337 }
4338 else if (*p < 32 || *p > 126)
4339 cupsFilePrintf(out, "\\%03o", *p & 255);
4340 else
4341 cupsFilePutChar(out, *p);
4342 }
4343 }
4344 else
4345 cupsFilePuts(out, attr->values[i].string.text);
4346 break;
fa73b229 4347
b423cd4c 4348 default :
4349 break; /* anti-compiler-warning-code */
4350 }
4351 }
4352 }
4353 else if (ch == '\\') /* Quoted char */
4354 {
4355 ch = cupsFileGetChar(in);
fa73b229 4356
b423cd4c 4357 if (ch != '{') /* Only do special handling for \{ */
4358 cupsFilePutChar(out, '\\');
fa73b229 4359
b423cd4c 4360 cupsFilePutChar(out, ch);
4361 }
4362 else
4363 cupsFilePutChar(out, ch);
fa73b229 4364
b423cd4c 4365 cupsFileClose(in);
fa73b229 4366
b423cd4c 4367 kbytes = (cupsFileTell(out) + 1023) / 1024;
fa73b229 4368
b423cd4c 4369 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
4370 IPP_TAG_INTEGER)) != NULL)
4371 attr->values[0].integer += kbytes;
4372
4373 cupsFileClose(out);
4374
4375 return (kbytes);
fa73b229 4376}
4377
4378
4379/*
b423cd4c 4380 * 'copy_file()' - Copy a PPD file or interface script...
fa73b229 4381 */
4382
b423cd4c 4383static int /* O - 0 = success, -1 = error */
4384copy_file(const char *from, /* I - Source file */
4385 const char *to) /* I - Destination file */
fa73b229 4386{
b423cd4c 4387 cups_file_t *src, /* Source file */
4388 *dst; /* Destination file */
4389 int bytes; /* Bytes to read/write */
4390 char buffer[2048]; /* Copy buffer */
fa73b229 4391
4392
b423cd4c 4393 cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_file(\"%s\", \"%s\")", from, to);
4394
fa73b229 4395 /*
b423cd4c 4396 * Open the source and destination file for a copy...
fa73b229 4397 */
4398
b423cd4c 4399 if ((src = cupsFileOpen(from, "rb")) == NULL)
4400 return (-1);
fa73b229 4401
b423cd4c 4402 if ((dst = cupsFileOpen(to, "wb")) == NULL)
4403 {
4404 cupsFileClose(src);
4405 return (-1);
4406 }
fa73b229 4407
b423cd4c 4408 /*
4409 * Copy the source file to the destination...
4410 */
fa73b229 4411
b423cd4c 4412 while ((bytes = cupsFileRead(src, buffer, sizeof(buffer))) > 0)
4413 if (cupsFileWrite(dst, buffer, bytes) < bytes)
4414 {
4415 cupsFileClose(src);
4416 cupsFileClose(dst);
4417 return (-1);
4418 }
fa73b229 4419
b423cd4c 4420 /*
4421 * Close both files and return...
4422 */
fa73b229 4423
b423cd4c 4424 cupsFileClose(src);
fa73b229 4425
b423cd4c 4426 return (cupsFileClose(dst));
4427}
fa73b229 4428
fa73b229 4429
b423cd4c 4430/*
4431 * 'copy_model()' - Copy a PPD model file, substituting default values
4432 * as needed...
4433 */
e00b005a 4434
b423cd4c 4435static int /* O - 0 = success, -1 = error */
4436copy_model(cupsd_client_t *con, /* I - Client connection */
4437 const char *from, /* I - Source file */
4438 const char *to) /* I - Destination file */
4439{
f7deaa1a 4440 fd_set input; /* select() input set */
b423cd4c 4441 struct timeval timeout; /* select() timeout */
bc44d920 4442 int maxfd; /* Max file descriptor for select() */
b423cd4c 4443 char tempfile[1024]; /* Temporary PPD file */
4444 int tempfd; /* Temporary PPD file descriptor */
4445 int temppid; /* Process ID of cups-driverd */
4446 int temppipe[2]; /* Temporary pipes */
4447 char *argv[4], /* Command-line arguments */
4448 *envp[MAX_ENV]; /* Environment */
4449 cups_file_t *src, /* Source file */
4450 *dst; /* Destination file */
2abf387c 4451 ppd_file_t *ppd; /* PPD file */
b423cd4c 4452 int bytes, /* Bytes from pipe */
4453 total; /* Total bytes from pipe */
2abf387c 4454 char buffer[2048]; /* Copy buffer */
b423cd4c 4455 int i; /* Looping var */
4456 char option[PPD_MAX_NAME], /* Option name */
4457 choice[PPD_MAX_NAME]; /* Choice name */
54afec33 4458 ppd_size_t *size; /* Default size */
b423cd4c 4459 int num_defaults; /* Number of default options */
2abf387c 4460 cups_option_t *defaults; /* Default options */
b423cd4c 4461 char cups_protocol[PPD_MAX_LINE];
4462 /* cupsProtocol attribute */
e00b005a 4463
e00b005a 4464
b423cd4c 4465 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4466 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4467 con, from, to);
e00b005a 4468
b423cd4c 4469 /*
4470 * Run cups-driverd to get the PPD file...
4471 */
e00b005a 4472
b423cd4c 4473 argv[0] = "cups-driverd";
4474 argv[1] = "cat";
4475 argv[2] = (char *)from;
4476 argv[3] = NULL;
e00b005a 4477
b423cd4c 4478 cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
e00b005a 4479
b423cd4c 4480 snprintf(buffer, sizeof(buffer), "%s/daemon/cups-driverd", ServerBin);
4481 snprintf(tempfile, sizeof(tempfile), "%s/%d.ppd", TempDir, con->http.fd);
4482 tempfd = open(tempfile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
321d8d57 4483 if (tempfd < 0 || cupsdOpenPipe(temppipe))
b423cd4c 4484 return (-1);
4485
b423cd4c 4486 cupsdLogMessage(CUPSD_LOG_DEBUG,
4487 "copy_model: Running \"cups-driverd cat %s\"...", from);
fa73b229 4488
b423cd4c 4489 if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1],
38e73f87 4490 -1, -1, 0, DefaultProfile, NULL, &temppid))
fa73b229 4491 {
b423cd4c 4492 close(tempfd);
4493 unlink(tempfile);
a4924f6c 4494
b423cd4c 4495 return (-1);
fa73b229 4496 }
4497
b423cd4c 4498 close(temppipe[1]);
fa73b229 4499
b423cd4c 4500 /*
4501 * Wait up to 30 seconds for the PPD file to be copied...
4502 */
fa73b229 4503
b423cd4c 4504 total = 0;
fa73b229 4505
b423cd4c 4506 if (temppipe[0] > CGIPipes[0])
4507 maxfd = temppipe[0] + 1;
4508 else
4509 maxfd = CGIPipes[0] + 1;
ef416fc2 4510
b423cd4c 4511 for (;;)
4512 {
4513 /*
4514 * See if we have data ready...
4515 */
ef416fc2 4516
f7deaa1a 4517 FD_ZERO(&input);
4518 FD_SET(temppipe[0], &input);
4519 FD_SET(CGIPipes[0], &input);
ef416fc2 4520
b423cd4c 4521 timeout.tv_sec = 30;
4522 timeout.tv_usec = 0;
4523
f7deaa1a 4524 if ((i = select(maxfd, &input, NULL, NULL, &timeout)) < 0)
b423cd4c 4525 {
4526 if (errno == EINTR)
4527 continue;
4528 else
4529 break;
4530 }
4531 else if (i == 0)
ef416fc2 4532 {
4533 /*
b423cd4c 4534 * We have timed out...
ef416fc2 4535 */
4536
b423cd4c 4537 break;
ef416fc2 4538 }
b423cd4c 4539
f7deaa1a 4540 if (FD_ISSET(temppipe[0], &input))
ef416fc2 4541 {
4542 /*
b423cd4c 4543 * Read the PPD file from the pipe, and write it to the PPD file.
ef416fc2 4544 */
4545
b423cd4c 4546 if ((bytes = read(temppipe[0], buffer, sizeof(buffer))) > 0)
4547 {
4548 if (write(tempfd, buffer, bytes) < bytes)
4549 break;
ef416fc2 4550
b423cd4c 4551 total += bytes;
4552 }
4553 else
4554 break;
ef416fc2 4555 }
b423cd4c 4556
f7deaa1a 4557 if (FD_ISSET(CGIPipes[0], &input))
b423cd4c 4558 cupsdUpdateCGI();
ef416fc2 4559 }
4560
b423cd4c 4561 close(temppipe[0]);
4562 close(tempfd);
ef416fc2 4563
b423cd4c 4564 if (!total)
ef416fc2 4565 {
b423cd4c 4566 /*
4567 * No data from cups-deviced...
4568 */
4569
4d301e69 4570 cupsdLogMessage(CUPSD_LOG_ERROR, "copy_model: empty PPD file");
b423cd4c 4571 unlink(tempfile);
4572 return (-1);
ef416fc2 4573 }
4574
9c80ffa2
MS
4575 /*
4576 * Open the source file for a copy...
4577 */
4578
4579 if ((src = cupsFileOpen(tempfile, "rb")) == NULL)
4580 {
4581 unlink(tempfile);
4582 return (-1);
4583 }
4584
b423cd4c 4585 /*
4586 * Read the source file and see what page sizes are supported...
4587 */
fa73b229 4588
9c80ffa2 4589 if ((ppd = _ppdOpen(src, _PPD_LOCALIZATION_NONE)) == NULL)
b423cd4c 4590 {
9c80ffa2 4591 cupsFileClose(src);
b423cd4c 4592 unlink(tempfile);
4593 return (-1);
4594 }
fa73b229 4595
ef416fc2 4596 /*
b423cd4c 4597 * Open the destination (if possible) and set the default options...
ef416fc2 4598 */
4599
b423cd4c 4600 num_defaults = 0;
4601 defaults = NULL;
4602 cups_protocol[0] = '\0';
ef416fc2 4603
b423cd4c 4604 if ((dst = cupsFileOpen(to, "rb")) != NULL)
ef416fc2 4605 {
4606 /*
b423cd4c 4607 * Read all of the default lines from the old PPD...
ef416fc2 4608 */
4609
b423cd4c 4610 while (cupsFileGets(dst, buffer, sizeof(buffer)))
4611 if (!strncmp(buffer, "*Default", 8))
4612 {
4613 /*
4614 * Add the default option...
4615 */
ef416fc2 4616
b423cd4c 4617 if (!ppd_parse_line(buffer, option, sizeof(option),
4618 choice, sizeof(choice)))
2abf387c 4619 {
4620 ppd_option_t *ppdo; /* PPD option */
4621
4622
4623 /*
4624 * Only add the default if the default hasn't already been
4625 * set and the choice exists in the new PPD...
4626 */
4627
4628 if (!cupsGetOption(option, num_defaults, defaults) &&
4629 (ppdo = ppdFindOption(ppd, option)) != NULL &&
4630 ppdFindChoice(ppdo, choice))
4631 num_defaults = cupsAddOption(option, choice, num_defaults,
b423cd4c 4632 &defaults);
2abf387c 4633 }
b423cd4c 4634 }
4635 else if (!strncmp(buffer, "*cupsProtocol:", 14))
4636 strlcpy(cups_protocol, buffer, sizeof(cups_protocol));
ef416fc2 4637
b423cd4c 4638 cupsFileClose(dst);
ef416fc2 4639 }
54afec33 4640 else if ((size = ppdPageSize(ppd, DefaultPaperSize)) != NULL)
ef416fc2 4641 {
b423cd4c 4642 /*
4643 * Add the default media sizes...
b423cd4c 4644 */
4645
54afec33 4646 num_defaults = cupsAddOption("PageSize", size->name,
c5571a1d 4647 num_defaults, &defaults);
54afec33 4648 num_defaults = cupsAddOption("PageRegion", size->name,
c5571a1d 4649 num_defaults, &defaults);
54afec33 4650 num_defaults = cupsAddOption("PaperDimension", size->name,
c5571a1d 4651 num_defaults, &defaults);
54afec33 4652 num_defaults = cupsAddOption("ImageableArea", size->name,
c5571a1d 4653 num_defaults, &defaults);
ef416fc2 4654 }
4655
2abf387c 4656 ppdClose(ppd);
4657
ef416fc2 4658 /*
b423cd4c 4659 * Open the destination file for a copy...
ef416fc2 4660 */
4661
b423cd4c 4662 if ((dst = cupsFileOpen(to, "wb")) == NULL)
ef416fc2 4663 {
2abf387c 4664 cupsFreeOptions(num_defaults, defaults);
b423cd4c 4665 cupsFileClose(src);
4666 unlink(tempfile);
4667 return (-1);
ef416fc2 4668 }
4669
4670 /*
b423cd4c 4671 * Copy the source file to the destination...
ef416fc2 4672 */
4673
9c80ffa2
MS
4674 cupsFileRewind(src);
4675
b423cd4c 4676 while (cupsFileGets(src, buffer, sizeof(buffer)))
ef416fc2 4677 {
b423cd4c 4678 if (!strncmp(buffer, "*Default", 8))
ef416fc2 4679 {
b423cd4c 4680 /*
4681 * Check for an previous default option choice...
4682 */
ef416fc2 4683
b423cd4c 4684 if (!ppd_parse_line(buffer, option, sizeof(option),
4685 choice, sizeof(choice)))
ef416fc2 4686 {
2abf387c 4687 const char *val; /* Default option value */
ef416fc2 4688
2abf387c 4689
4690 if ((val = cupsGetOption(option, num_defaults, defaults)) != NULL)
4691 {
4692 /*
4693 * Substitute the previous choice...
4694 */
4695
4696 snprintf(buffer, sizeof(buffer), "*Default%s: %s", option, val);
4697 }
b423cd4c 4698 }
ef416fc2 4699 }
b423cd4c 4700
4701 cupsFilePrintf(dst, "%s\n", buffer);
ef416fc2 4702 }
4703
b423cd4c 4704 if (cups_protocol[0])
4705 cupsFilePrintf(dst, "%s\n", cups_protocol);
4706
2abf387c 4707 cupsFreeOptions(num_defaults, defaults);
b423cd4c 4708
ef416fc2 4709 /*
b423cd4c 4710 * Close both files and return...
ef416fc2 4711 */
4712
b423cd4c 4713 cupsFileClose(src);
4714
4715 unlink(tempfile);
4716
4717 return (cupsFileClose(dst));
4718}
4719
4720
4721/*
4722 * 'copy_job_attrs()' - Copy job attributes.
4723 */
ef416fc2 4724
b423cd4c 4725static void
4726copy_job_attrs(cupsd_client_t *con, /* I - Client connection */
4727 cupsd_job_t *job, /* I - Job */
10d09e33
MS
4728 cups_array_t *ra, /* I - Requested attributes array */
4729 cups_array_t *exclude) /* I - Private attributes array */
b423cd4c 4730{
4731 char job_uri[HTTP_MAX_URI]; /* Job URI */
ef416fc2 4732
ef416fc2 4733
4734 /*
b423cd4c 4735 * Send the requested attributes for each job...
ef416fc2 4736 */
4737
10d09e33
MS
4738 if (!cupsArrayFind(exclude, "all"))
4739 {
12f89d24
MS
4740 if ((!exclude || !cupsArrayFind(exclude, "number-of-documents")) &&
4741 (!ra || cupsArrayFind(ra, "number-of-documents")))
10d09e33 4742 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
12f89d24 4743 "number-of-documents", job->num_files);
2e4ff8af 4744
10d09e33
MS
4745 if ((!exclude || !cupsArrayFind(exclude, "job-media-progress")) &&
4746 (!ra || cupsArrayFind(ra, "job-media-progress")))
4747 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
4748 "job-media-progress", job->progress);
9a4f8274 4749
10d09e33
MS
4750 if ((!exclude || !cupsArrayFind(exclude, "job-more-info")) &&
4751 (!ra || cupsArrayFind(ra, "job-more-info")))
88f9aafc
MS
4752 {
4753 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "http",
4754 NULL, con->servername, con->serverport, "/jobs/%d",
4755 job->id);
10d09e33
MS
4756 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
4757 "job-more-info", NULL, job_uri);
88f9aafc 4758 }
ef416fc2 4759
10d09e33
MS
4760 if (job->state_value > IPP_JOB_PROCESSING &&
4761 (!exclude || !cupsArrayFind(exclude, "job-preserved")) &&
4762 (!ra || cupsArrayFind(ra, "job-preserved")))
4763 ippAddBoolean(con->response, IPP_TAG_JOB, "job-preserved",
4764 job->num_files > 0);
ef416fc2 4765
10d09e33
MS
4766 if ((!exclude || !cupsArrayFind(exclude, "job-printer-up-time")) &&
4767 (!ra || cupsArrayFind(ra, "job-printer-up-time")))
4768 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
4769 "job-printer-up-time", time(NULL));
4770 }
ef416fc2 4771
88f9aafc
MS
4772 if (!ra || cupsArrayFind(ra, "job-printer-uri"))
4773 {
4774 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
4775 con->servername, con->serverport,
a2326b5b
MS
4776 (job->dtype & CUPS_PRINTER_CLASS) ? "/classes/%s" :
4777 "/printers/%s",
88f9aafc
MS
4778 job->dest);
4779 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
4780 "job-printer-uri", NULL, job_uri);
4781 }
4782
b423cd4c 4783 if (!ra || cupsArrayFind(ra, "job-uri"))
88f9aafc
MS
4784 {
4785 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
4786 con->servername, con->serverport, "/jobs/%d",
4787 job->id);
b423cd4c 4788 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
4789 "job-uri", NULL, job_uri);
88f9aafc 4790 }
ef416fc2 4791
10d09e33 4792 copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0, exclude);
b423cd4c 4793}
ef416fc2 4794
ef416fc2 4795
b423cd4c 4796/*
4797 * 'copy_printer_attrs()' - Copy printer attributes.
4798 */
ef416fc2 4799
b423cd4c 4800static void
4801copy_printer_attrs(
4802 cupsd_client_t *con, /* I - Client connection */
4803 cupsd_printer_t *printer, /* I - Printer */
4804 cups_array_t *ra) /* I - Requested attributes array */
4805{
4806 char printer_uri[HTTP_MAX_URI];
4807 /* Printer URI */
7cf5915e
MS
4808 char printer_icons[HTTP_MAX_URI];
4809 /* Printer icons */
b423cd4c 4810 time_t curtime; /* Current time */
4811 int i; /* Looping var */
ef416fc2 4812
ef416fc2 4813
b423cd4c 4814 /*
4815 * Copy the printer attributes to the response using requested-attributes
4816 * and document-format attributes that may be provided by the client.
4817 */
ef416fc2 4818
b423cd4c 4819 curtime = time(NULL);
ef416fc2 4820
5a738aea
MS
4821 if (!ra || cupsArrayFind(ra, "marker-change-time"))
4822 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4823 "marker-change-time", printer->marker_time);
4824
dd1abb6b
MS
4825 if (printer->num_printers > 0 &&
4826 (!ra || cupsArrayFind(ra, "member-uris")))
4827 {
4828 ipp_attribute_t *member_uris; /* member-uris attribute */
4829 cupsd_printer_t *p2; /* Printer in class */
4830 ipp_attribute_t *p2_uri; /* printer-uri-supported for class printer */
4831
4832
4833 if ((member_uris = ippAddStrings(con->response, IPP_TAG_PRINTER,
4834 IPP_TAG_URI, "member-uris",
4835 printer->num_printers, NULL,
4836 NULL)) != NULL)
4837 {
4838 for (i = 0; i < printer->num_printers; i ++)
4839 {
4840 p2 = printer->printers[i];
4841
4842 if ((p2_uri = ippFindAttribute(p2->attrs, "printer-uri-supported",
4843 IPP_TAG_URI)) != NULL)
4844 member_uris->values[i].string.text =
426c6a59 4845 _cupsStrRetain(p2_uri->values[0].string.text);
dd1abb6b
MS
4846 else
4847 {
4848 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri,
4849 sizeof(printer_uri), "ipp", NULL, con->servername,
4850 con->serverport,
4851 (p2->type & CUPS_PRINTER_CLASS) ?
4852 "/classes/%s" : "/printers/%s", p2->name);
4853 member_uris->values[i].string.text = _cupsStrAlloc(printer_uri);
4854 }
4855 }
4856 }
4857 }
4858
323c5de1 4859 if (printer->alert && (!ra || cupsArrayFind(ra, "printer-alert")))
4860 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_STRING,
4861 "printer-alert", NULL, printer->alert);
4862
4863 if (printer->alert_description &&
4864 (!ra || cupsArrayFind(ra, "printer-alert-description")))
4865 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
4866 "printer-alert-description", NULL,
4867 printer->alert_description);
4868
b423cd4c 4869 if (!ra || cupsArrayFind(ra, "printer-current-time"))
4870 ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
4871 ippTimeToDate(curtime));
ef416fc2 4872
37e7e6e0 4873#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
d9bca400
MS
4874 if (!ra || cupsArrayFind(ra, "printer-dns-sd-name"))
4875 {
4876 if (printer->reg_name)
4877 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
4878 "printer-dns-sd-name", NULL, printer->reg_name);
4879 else
4880 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
4881 "printer-dns-sd-name", 0);
4882 }
37e7e6e0 4883#endif /* HAVE_DNSSD || HAVE_AVAHI */
d9bca400 4884
b423cd4c 4885 if (!ra || cupsArrayFind(ra, "printer-error-policy"))
4886 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
4887 "printer-error-policy", NULL, printer->error_policy);
ef416fc2 4888
f11a948a
MS
4889 if (!ra || cupsArrayFind(ra, "printer-error-policy-supported"))
4890 {
4891 static const char * const errors[] =/* printer-error-policy-supported values */
4892 {
4893 "abort-job",
4894 "retry-current-job",
4895 "retry-job",
4896 "stop-printer"
4897 };
4898
a2326b5b 4899 if (printer->type & CUPS_PRINTER_CLASS)
f11a948a
MS
4900 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
4901 "printer-error-policy-supported", NULL, "retry-current-job");
4902 else
4903 ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
4904 "printer-error-policy-supported",
4905 sizeof(errors) / sizeof(errors[0]), NULL, errors);
4906 }
4907
7cf5915e
MS
4908 if (!ra || cupsArrayFind(ra, "printer-icons"))
4909 {
4910 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_icons, sizeof(printer_icons),
4911 "http", NULL, con->servername, con->serverport,
4912 "/icons/%s.png", printer->name);
4913 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-icons",
4914 NULL, printer_icons);
4915 cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-icons=\"%s\"", printer_icons);
4916 }
4917
b423cd4c 4918 if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs"))
4919 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
4920 printer->accepting);
ef416fc2 4921
b423cd4c 4922 if (!ra || cupsArrayFind(ra, "printer-is-shared"))
4923 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared",
4924 printer->shared);
ef416fc2 4925
a2326b5b 4926 if (!ra || cupsArrayFind(ra, "printer-more-info"))
58dc1933
MS
4927 {
4928 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
4929 "http", NULL, con->servername, con->serverport,
4930 (printer->type & CUPS_PRINTER_CLASS) ?
4931 "/classes/%s" : "/printers/%s", printer->name);
4932 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
4933 "printer-more-info", NULL, printer_uri);
4934 }
4935
b423cd4c 4936 if (!ra || cupsArrayFind(ra, "printer-op-policy"))
4937 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
4938 "printer-op-policy", NULL, printer->op_policy);
4939
4940 if (!ra || cupsArrayFind(ra, "printer-state"))
4941 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
4942 printer->state);
4943
4944 if (!ra || cupsArrayFind(ra, "printer-state-change-time"))
4945 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4946 "printer-state-change-time", printer->state_time);
f7deaa1a 4947
b423cd4c 4948 if (!ra || cupsArrayFind(ra, "printer-state-message"))
4949 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
4950 "printer-state-message", NULL, printer->state_message);
ef416fc2 4951
b423cd4c 4952 if (!ra || cupsArrayFind(ra, "printer-state-reasons"))
4953 add_printer_state_reasons(con, printer);
ef416fc2 4954
b423cd4c 4955 if (!ra || cupsArrayFind(ra, "printer-type"))
4956 {
4957 int type; /* printer-type value */
ef416fc2 4958
ef416fc2 4959 /*
b423cd4c 4960 * Add the CUPS-specific printer-type attribute...
ef416fc2 4961 */
4962
09a101d6 4963 type = printer->type;
b423cd4c 4964
4965 if (printer == DefaultPrinter)
4966 type |= CUPS_PRINTER_DEFAULT;
4967
4968 if (!printer->accepting)
4969 type |= CUPS_PRINTER_REJECTING;
4970
4971 if (!printer->shared)
4972 type |= CUPS_PRINTER_NOT_SHARED;
4973
4974 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type",
4975 type);
ef416fc2 4976 }
ef416fc2 4977
b423cd4c 4978 if (!ra || cupsArrayFind(ra, "printer-up-time"))
4979 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4980 "printer-up-time", curtime);
ef416fc2 4981
a2326b5b 4982 if (!ra || cupsArrayFind(ra, "printer-uri-supported"))
ef416fc2 4983 {
b423cd4c 4984 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
4985 "ipp", NULL, con->servername, con->serverport,
8ca02f3c 4986 (printer->type & CUPS_PRINTER_CLASS) ?
4987 "/classes/%s" : "/printers/%s", printer->name);
b423cd4c 4988 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
4989 "printer-uri-supported", NULL, printer_uri);
4990 cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"",
4991 printer_uri);
4992 }
ef416fc2 4993
b423cd4c 4994 if (!ra || cupsArrayFind(ra, "queued-job-count"))
4995 add_queued_job_count(con, printer);
ef416fc2 4996
10d09e33 4997 copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0, NULL);
61cf44e2 4998 if (printer->ppd_attrs)
10d09e33
MS
4999 copy_attrs(con->response, printer->ppd_attrs, ra, IPP_TAG_ZERO, 0, NULL);
5000 copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY, NULL);
b423cd4c 5001}
ef416fc2 5002
ef416fc2 5003
b423cd4c 5004/*
5005 * 'copy_subscription_attrs()' - Copy subscription attributes.
5006 */
ef416fc2 5007
b423cd4c 5008static void
5009copy_subscription_attrs(
5010 cupsd_client_t *con, /* I - Client connection */
5011 cupsd_subscription_t *sub, /* I - Subscription */
10d09e33
MS
5012 cups_array_t *ra, /* I - Requested attributes array */
5013 cups_array_t *exclude) /* I - Private attributes array */
b423cd4c 5014{
5015 ipp_attribute_t *attr; /* Current attribute */
5016 char printer_uri[HTTP_MAX_URI];
5017 /* Printer URI */
5018 int count; /* Number of events */
5019 unsigned mask; /* Current event mask */
5020 const char *name; /* Current event name */
ef416fc2 5021
ef416fc2 5022
10d09e33
MS
5023 cupsdLogMessage(CUPSD_LOG_DEBUG2,
5024 "copy_subscription_attrs(con=%p, sub=%p, ra=%p, exclude=%p)",
5025 con, sub, ra, exclude);
5026
b423cd4c 5027 /*
5028 * Copy the subscription attributes to the response using the
5029 * requested-attributes attribute that may be provided by the client.
5030 */
ef416fc2 5031
10d09e33 5032 if (!exclude || !cupsArrayFind(exclude, "all"))
b423cd4c 5033 {
10d09e33
MS
5034 if ((!exclude || !cupsArrayFind(exclude, "notify-events")) &&
5035 (!ra || cupsArrayFind(ra, "notify-events")))
b423cd4c 5036 {
10d09e33 5037 cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_subscription_attrs: notify-events");
ef416fc2 5038
10d09e33
MS
5039 if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL)
5040 {
5041 /*
5042 * Simple event list...
5043 */
5044
5045 ippAddString(con->response, IPP_TAG_SUBSCRIPTION,
5046 (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
5047 "notify-events", NULL, name);
5048 }
5049 else
5050 {
5051 /*
5052 * Complex event list...
5053 */
ef416fc2 5054
10d09e33
MS
5055 for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
5056 if (sub->mask & mask)
5057 count ++;
ef416fc2 5058
10d09e33
MS
5059 attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION,
5060 (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
5061 "notify-events", count, NULL, NULL);
ef416fc2 5062
10d09e33
MS
5063 for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
5064 if (sub->mask & mask)
5065 {
5066 attr->values[count].string.text =
5067 (char *)cupsdEventName((cupsd_eventmask_t)mask);
ef416fc2 5068
10d09e33
MS
5069 count ++;
5070 }
5071 }
ef416fc2 5072 }
10d09e33
MS
5073
5074 if ((!exclude || !cupsArrayFind(exclude, "notify-lease-duration")) &&
5075 (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration"))))
5076 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5077 "notify-lease-duration", sub->lease);
5078
5079 if ((!exclude || !cupsArrayFind(exclude, "notify-recipient-uri")) &&
5080 (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri"))))
5081 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
5082 "notify-recipient-uri", NULL, sub->recipient);
5083 else if ((!exclude || !cupsArrayFind(exclude, "notify-pull-method")) &&
5084 (!ra || cupsArrayFind(ra, "notify-pull-method")))
5085 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
5086 "notify-pull-method", NULL, "ippget");
5087
5088 if ((!exclude || !cupsArrayFind(exclude, "notify-subscriber-user-name")) &&
5089 (!ra || cupsArrayFind(ra, "notify-subscriber-user-name")))
5090 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME,
5091 "notify-subscriber-user-name", NULL, sub->owner);
5092
5093 if ((!exclude || !cupsArrayFind(exclude, "notify-time-interval")) &&
5094 (!ra || cupsArrayFind(ra, "notify-time-interval")))
5095 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5096 "notify-time-interval", sub->interval);
5097
5098 if (sub->user_data_len > 0 &&
5099 (!exclude || !cupsArrayFind(exclude, "notify-user-data")) &&
5100 (!ra || cupsArrayFind(ra, "notify-user-data")))
5101 ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data",
5102 sub->user_data, sub->user_data_len);
b423cd4c 5103 }
ef416fc2 5104
b423cd4c 5105 if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id")))
5106 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5107 "notify-job-id", sub->job->id);
ef416fc2 5108
b423cd4c 5109 if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri")))
5110 {
5111 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
5112 "ipp", NULL, con->servername, con->serverport,
5113 "/printers/%s", sub->dest->name);
5114 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
5115 "notify-printer-uri", NULL, printer_uri);
ef416fc2 5116 }
ef416fc2 5117
b423cd4c 5118 if (!ra || cupsArrayFind(ra, "notify-subscription-id"))
5119 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5120 "notify-subscription-id", sub->id);
b423cd4c 5121}
ef416fc2 5122
ef416fc2 5123
b423cd4c 5124/*
5125 * 'create_job()' - Print a file to a printer or class.
5126 */
5127
5128static void
5129create_job(cupsd_client_t *con, /* I - Client connection */
5130 ipp_attribute_t *uri) /* I - Printer URI */
5131{
a29fd7dd 5132 int i; /* Looping var */
f7deaa1a 5133 cupsd_printer_t *printer; /* Printer */
5134 cupsd_job_t *job; /* New job */
a29fd7dd
MS
5135 static const char * const forbidden_attrs[] =
5136 { /* List of forbidden attributes */
5137 "compression",
5138 "document-format",
5139 "document-name",
5140 "document-natural-language"
5141 };
b423cd4c 5142
ef416fc2 5143
b423cd4c 5144 cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con,
5145 con->http.fd, uri->values[0].string.text);
ef416fc2 5146
f7deaa1a 5147 /*
5148 * Is the destination valid?
5149 */
5150
5151 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
5152 {
5153 /*
5154 * Bad URI...
5155 */
5156
5157 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 5158 _("The printer or class does not exist."));
f7deaa1a 5159 return;
5160 }
5161
a29fd7dd
MS
5162 /*
5163 * Check for invalid Create-Job attributes and log a warning or error depending
5164 * on whether cupsd is running in "strict conformance" mode...
5165 */
5166
5167 for (i = 0;
5168 i < (int)(sizeof(forbidden_attrs) / sizeof(forbidden_attrs[0]));
5169 i ++)
5170 if (ippFindAttribute(con->request, forbidden_attrs[i], IPP_TAG_ZERO))
5171 {
5172 if (StrictConformance)
5173 {
5174 send_ipp_status(con, IPP_BAD_REQUEST,
5175 _("The '%s' operation attribute cannot be supplied in a "
5176 "Create-Job request."), forbidden_attrs[i]);
5177 return;
5178 }
5179
5180 cupsdLogMessage(CUPSD_LOG_WARN,
5181 "Unexpected '%s' operation attribute in a Create-Job "
5182 "request.", forbidden_attrs[i]);
5183 }
5184
ef416fc2 5185 /*
b423cd4c 5186 * Create the job object...
ef416fc2 5187 */
5188
f7deaa1a 5189 if ((job = add_job(con, printer, NULL)) == NULL)
b423cd4c 5190 return;
ef416fc2 5191
09a101d6 5192 job->pending_timeout = 1;
5193
ef416fc2 5194 /*
5195 * Save and log the job...
5196 */
f7deaa1a 5197
75bd9771
MS
5198 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
5199 job->dest, job->username);
ef416fc2 5200}
5201
5202
5203/*
5204 * 'create_requested_array()' - Create an array for the requested-attributes.
5205 */
5206
5207static cups_array_t * /* O - Array of attributes or NULL */
5208create_requested_array(ipp_t *request) /* I - IPP request */
5209{
5210 int i; /* Looping var */
5211 ipp_attribute_t *requested; /* requested-attributes attribute */
5212 cups_array_t *ra; /* Requested attributes array */
5213 char *value; /* Current value */
5214
5215
5216 /*
5217 * Get the requested-attributes attribute, and return NULL if we don't
5218 * have one...
5219 */
5220
5221 if ((requested = ippFindAttribute(request, "requested-attributes",
5222 IPP_TAG_KEYWORD)) == NULL)
5223 return (NULL);
5224
5225 /*
5226 * If the attribute contains a single "all" keyword, return NULL...
5227 */
5228
5229 if (requested->num_values == 1 &&
5230 !strcmp(requested->values[0].string.text, "all"))
5231 return (NULL);
5232
5233 /*
5234 * Create an array using "strcmp" as the comparison function...
5235 */
5236
5237 ra = cupsArrayNew((cups_array_func_t)strcmp, NULL);
5238
5239 for (i = 0; i < requested->num_values; i ++)
5240 {
5241 value = requested->values[i].string.text;
5242
5243 if (!strcmp(value, "job-template"))
5244 {
dcb445bc 5245 /* Only includes the set of Job Template attributes supported by CUPS */
ef416fc2 5246 cupsArrayAdd(ra, "copies");
5247 cupsArrayAdd(ra, "copies-default");
5248 cupsArrayAdd(ra, "copies-supported");
5249 cupsArrayAdd(ra, "finishings");
5250 cupsArrayAdd(ra, "finishings-default");
5251 cupsArrayAdd(ra, "finishings-supported");
5252 cupsArrayAdd(ra, "job-hold-until");
5253 cupsArrayAdd(ra, "job-hold-until-default");
5254 cupsArrayAdd(ra, "job-hold-until-supported");
5255 cupsArrayAdd(ra, "job-priority");
5256 cupsArrayAdd(ra, "job-priority-default");
5257 cupsArrayAdd(ra, "job-priority-supported");
5258 cupsArrayAdd(ra, "job-sheets");
5259 cupsArrayAdd(ra, "job-sheets-default");
5260 cupsArrayAdd(ra, "job-sheets-supported");
5261 cupsArrayAdd(ra, "media");
dcb445bc
MS
5262 cupsArrayAdd(ra, "media-col");
5263 cupsArrayAdd(ra, "media-col-default");
ef416fc2 5264 cupsArrayAdd(ra, "media-default");
5265 cupsArrayAdd(ra, "media-supported");
5266 cupsArrayAdd(ra, "multiple-document-handling");
5267 cupsArrayAdd(ra, "multiple-document-handling-default");
5268 cupsArrayAdd(ra, "multiple-document-handling-supported");
5269 cupsArrayAdd(ra, "number-up");
5270 cupsArrayAdd(ra, "number-up-default");
dcb445bc
MS
5271 cupsArrayAdd(ra, "number-up-layout");
5272 cupsArrayAdd(ra, "number-up-layout-default");
5273 cupsArrayAdd(ra, "number-up-layout-supported");
ef416fc2 5274 cupsArrayAdd(ra, "number-up-supported");
5275 cupsArrayAdd(ra, "orientation-requested");
5276 cupsArrayAdd(ra, "orientation-requested-default");
5277 cupsArrayAdd(ra, "orientation-requested-supported");
dcb445bc
MS
5278 cupsArrayAdd(ra, "output-bin");
5279 cupsArrayAdd(ra, "output-bin-default");
5280 cupsArrayAdd(ra, "output-bin-supported");
5281 cupsArrayAdd(ra, "page-delivery");
5282 cupsArrayAdd(ra, "page-delivery-default");
5283 cupsArrayAdd(ra, "page-delivery-supported");
5284 cupsArrayAdd(ra, "page-order-received");
5285 cupsArrayAdd(ra, "page-order-received-default");
5286 cupsArrayAdd(ra, "page-order-received-supported");
ef416fc2 5287 cupsArrayAdd(ra, "page-ranges");
5288 cupsArrayAdd(ra, "page-ranges-supported");
dcb445bc
MS
5289 cupsArrayAdd(ra, "presentation-direction-number-up");
5290 cupsArrayAdd(ra, "presentation-direction-number-up-default");
5291 cupsArrayAdd(ra, "presentation-direction-number-up-supported");
5292 cupsArrayAdd(ra, "print-color-mode");
5293 cupsArrayAdd(ra, "print-color-mode-default");
5294 cupsArrayAdd(ra, "print-color-mode-supported");
5295 cupsArrayAdd(ra, "print-content-optimize");
5296 cupsArrayAdd(ra, "print-content-optimize-default");
5297 cupsArrayAdd(ra, "print-content-optimize-supported");
ef416fc2 5298 cupsArrayAdd(ra, "print-quality");
5299 cupsArrayAdd(ra, "print-quality-default");
5300 cupsArrayAdd(ra, "print-quality-supported");
dcb445bc
MS
5301 cupsArrayAdd(ra, "printer-resolution");
5302 cupsArrayAdd(ra, "printer-resolution-default");
5303 cupsArrayAdd(ra, "printer-resolution-supported");
5304 cupsArrayAdd(ra, "sheet-collate");
5305 cupsArrayAdd(ra, "sheet-collate-default");
5306 cupsArrayAdd(ra, "sheet-collate-supported");
ef416fc2 5307 cupsArrayAdd(ra, "sides");
5308 cupsArrayAdd(ra, "sides-default");
5309 cupsArrayAdd(ra, "sides-supported");
5310 }
5311 else if (!strcmp(value, "job-description"))
5312 {
dcb445bc 5313 /* Only includes the set of Job Description attributes supported by CUPS */
ef416fc2 5314 cupsArrayAdd(ra, "date-time-at-completed");
5315 cupsArrayAdd(ra, "date-time-at-creation");
5316 cupsArrayAdd(ra, "date-time-at-processing");
a469f8a5 5317 cupsArrayAdd(ra, "job-charge-info");
ef416fc2 5318 cupsArrayAdd(ra, "job-detailed-status-message");
5319 cupsArrayAdd(ra, "job-document-access-errors");
5320 cupsArrayAdd(ra, "job-id");
5321 cupsArrayAdd(ra, "job-impressions");
5322 cupsArrayAdd(ra, "job-impressions-completed");
5323 cupsArrayAdd(ra, "job-k-octets");
5324 cupsArrayAdd(ra, "job-k-octets-processed");
dcb445bc 5325 cupsArrayAdd(ra, "job-mandatory-attributes");
9a4f8274 5326 cupsArrayAdd(ra, "job-media-progress");
ef416fc2 5327 cupsArrayAdd(ra, "job-media-sheets");
5328 cupsArrayAdd(ra, "job-media-sheets-completed");
5329 cupsArrayAdd(ra, "job-message-from-operator");
5330 cupsArrayAdd(ra, "job-more-info");
5331 cupsArrayAdd(ra, "job-name");
5332 cupsArrayAdd(ra, "job-originating-user-name");
5333 cupsArrayAdd(ra, "job-printer-up-time");
5334 cupsArrayAdd(ra, "job-printer-uri");
5335 cupsArrayAdd(ra, "job-state");
5336 cupsArrayAdd(ra, "job-state-message");
5337 cupsArrayAdd(ra, "job-state-reasons");
5338 cupsArrayAdd(ra, "job-uri");
5339 cupsArrayAdd(ra, "number-of-documents");
5340 cupsArrayAdd(ra, "number-of-intervening-jobs");
5341 cupsArrayAdd(ra, "output-device-assigned");
5342 cupsArrayAdd(ra, "time-at-completed");
5343 cupsArrayAdd(ra, "time-at-creation");
5344 cupsArrayAdd(ra, "time-at-processing");
5345 }
5346 else if (!strcmp(value, "printer-description"))
5347 {
dcb445bc 5348 /* Only includes the set of Printer Description attributes supported by CUPS */
ef416fc2 5349 cupsArrayAdd(ra, "charset-configured");
5350 cupsArrayAdd(ra, "charset-supported");
5351 cupsArrayAdd(ra, "color-supported");
5352 cupsArrayAdd(ra, "compression-supported");
5353 cupsArrayAdd(ra, "document-format-default");
5354 cupsArrayAdd(ra, "document-format-supported");
5355 cupsArrayAdd(ra, "generated-natural-language-supported");
5356 cupsArrayAdd(ra, "ipp-versions-supported");
dcb445bc
MS
5357 cupsArrayAdd(ra, "job-creation-attributes-supported");
5358 cupsArrayAdd(ra, "job-ids-supported");
ef416fc2 5359 cupsArrayAdd(ra, "job-impressions-supported");
5360 cupsArrayAdd(ra, "job-k-octets-supported");
5361 cupsArrayAdd(ra, "job-media-sheets-supported");
1f6f3dbc 5362 cupsArrayAdd(ra, "job-settable-attributes-supported");
dcb445bc
MS
5363 cupsArrayAdd(ra, "jpeg-k-octets-supported");
5364 cupsArrayAdd(ra, "jpeg-x-dimension-supported");
5365 cupsArrayAdd(ra, "jpeg-y-dimension-supported");
5366 cupsArrayAdd(ra, "media-bottom-margin-supported");
5367 cupsArrayAdd(ra, "media-col-supported");
5368 cupsArrayAdd(ra, "media-key-supported");
5369 cupsArrayAdd(ra, "media-left-margin-supported");
5370 cupsArrayAdd(ra, "media-right-margin-supported");
5371 cupsArrayAdd(ra, "media-size-supported");
5372 cupsArrayAdd(ra, "media-source-supported");
5373 cupsArrayAdd(ra, "media-top-margin-supported");
5374 cupsArrayAdd(ra, "media-type-supported");
ef416fc2 5375 cupsArrayAdd(ra, "multiple-document-jobs-supported");
5376 cupsArrayAdd(ra, "multiple-operation-time-out");
5377 cupsArrayAdd(ra, "natural-language-configured");
ef416fc2 5378 cupsArrayAdd(ra, "notify-max-events-supported");
ef416fc2 5379 cupsArrayAdd(ra, "notify-schemes-supported");
5380 cupsArrayAdd(ra, "operations-supported");
5381 cupsArrayAdd(ra, "pages-per-minute");
5382 cupsArrayAdd(ra, "pages-per-minute-color");
dcb445bc 5383 cupsArrayAdd(ra, "pdf-k-octets-supported");
ef416fc2 5384 cupsArrayAdd(ra, "pdl-override-supported");
323c5de1 5385 cupsArrayAdd(ra, "printer-alert");
5386 cupsArrayAdd(ra, "printer-alert-description");
a469f8a5 5387 cupsArrayAdd(ra, "printer-charge-info-uri");
01ce6322 5388 cupsArrayAdd(ra, "printer-commands");
ef416fc2 5389 cupsArrayAdd(ra, "printer-current-time");
01ce6322 5390 cupsArrayAdd(ra, "printer-dns-sd-name");
ef416fc2 5391 cupsArrayAdd(ra, "printer-info");
5392 cupsArrayAdd(ra, "printer-is-accepting-jobs");
dcb445bc 5393 cupsArrayAdd(ra, "printer-is-shared");
ef416fc2 5394 cupsArrayAdd(ra, "printer-location");
5395 cupsArrayAdd(ra, "printer-make-and-model");
5396 cupsArrayAdd(ra, "printer-message-from-operator");
5397 cupsArrayAdd(ra, "printer-more-info");
5398 cupsArrayAdd(ra, "printer-more-info-manufacturer");
5399 cupsArrayAdd(ra, "printer-name");
dcb445bc 5400 cupsArrayAdd(ra, "printer-settable-attributes-supported");
ef416fc2 5401 cupsArrayAdd(ra, "printer-state");
dcb445bc
MS
5402 cupsArrayAdd(ra, "printer-state-change-date-time");
5403 cupsArrayAdd(ra, "printer-state-change-time");
ef416fc2 5404 cupsArrayAdd(ra, "printer-state-message");
5405 cupsArrayAdd(ra, "printer-state-reasons");
01ce6322 5406 cupsArrayAdd(ra, "printer-type");
ef416fc2 5407 cupsArrayAdd(ra, "printer-up-time");
5408 cupsArrayAdd(ra, "printer-uri-supported");
5409 cupsArrayAdd(ra, "queued-job-count");
5410 cupsArrayAdd(ra, "reference-uri-schemes-supported");
5411 cupsArrayAdd(ra, "uri-authentication-supported");
5412 cupsArrayAdd(ra, "uri-security-supported");
dcb445bc 5413 cupsArrayAdd(ra, "which-jobs-supported");
ef416fc2 5414 }
f7deaa1a 5415 else if (!strcmp(value, "printer-defaults"))
5416 {
5417 char *name; /* Option name */
5418
5419
5420 for (name = (char *)cupsArrayFirst(CommonDefaults);
5421 name;
5422 name = (char *)cupsArrayNext(CommonDefaults))
5423 cupsArrayAdd(ra, name);
5424 }
dcb445bc
MS
5425 else if (!strcmp(value, "subscription-description"))
5426 {
5427 /* Only includes the set of Subscription Description attributes supported by CUPS */
5428 cupsArrayAdd(ra, "notify-job-id");
5429 cupsArrayAdd(ra, "notify-lease-expiration-time");
5430 cupsArrayAdd(ra, "notify-printer-up-time");
5431 cupsArrayAdd(ra, "notify-printer-uri");
5432 cupsArrayAdd(ra, "notify-sequence-number");
5433 cupsArrayAdd(ra, "notify-subscriber-user-name");
5434 cupsArrayAdd(ra, "notify-subscription-id");
5435 }
ef416fc2 5436 else if (!strcmp(value, "subscription-template"))
5437 {
dcb445bc 5438 /* Only includes the set of Subscription Template attributes supported by CUPS */
ef416fc2 5439 cupsArrayAdd(ra, "notify-attributes");
dcb445bc 5440 cupsArrayAdd(ra, "notify-attributes-supported");
ef416fc2 5441 cupsArrayAdd(ra, "notify-charset");
5442 cupsArrayAdd(ra, "notify-events");
dcb445bc
MS
5443 cupsArrayAdd(ra, "notify-events-default");
5444 cupsArrayAdd(ra, "notify-events-supported");
ef416fc2 5445 cupsArrayAdd(ra, "notify-lease-duration");
dcb445bc
MS
5446 cupsArrayAdd(ra, "notify-lease-duration-default");
5447 cupsArrayAdd(ra, "notify-lease-duration-supported");
ef416fc2 5448 cupsArrayAdd(ra, "notify-natural-language");
5449 cupsArrayAdd(ra, "notify-pull-method");
dcb445bc 5450 cupsArrayAdd(ra, "notify-pull-method-supported");
ef416fc2 5451 cupsArrayAdd(ra, "notify-recipient-uri");
5452 cupsArrayAdd(ra, "notify-time-interval");
5453 cupsArrayAdd(ra, "notify-user-data");
5454 }
5455 else
5456 cupsArrayAdd(ra, value);
5457 }
5458
5459 return (ra);
5460}
5461
5462
5463/*
5464 * 'create_subscription()' - Create a notification subscription.
5465 */
5466
5467static void
5468create_subscription(
5469 cupsd_client_t *con, /* I - Client connection */
5470 ipp_attribute_t *uri) /* I - Printer URI */
5471{
5472 http_status_t status; /* Policy status */
5473 int i; /* Looping var */
5474 ipp_attribute_t *attr; /* Current attribute */
bc44d920 5475 cups_ptype_t dtype; /* Destination type (printer/class) */
ed486911 5476 char scheme[HTTP_MAX_URI],
5477 /* Scheme portion of URI */
ef416fc2 5478 userpass[HTTP_MAX_URI],
5479 /* Username portion of URI */
5480 host[HTTP_MAX_URI],
5481 /* Host portion of URI */
5482 resource[HTTP_MAX_URI];
5483 /* Resource portion of URI */
5484 int port; /* Port portion of URI */
5485 cupsd_printer_t *printer; /* Printer/class */
5486 cupsd_job_t *job; /* Job */
5487 int jobid; /* Job ID */
5488 cupsd_subscription_t *sub; /* Subscription object */
bc44d920 5489 const char *username, /* requesting-user-name or
5490 authenticated username */
ef416fc2 5491 *recipient, /* notify-recipient-uri */
5492 *pullmethod; /* notify-pull-method */
5493 ipp_attribute_t *user_data; /* notify-user-data */
5494 int interval, /* notify-time-interval */
5495 lease; /* notify-lease-duration */
5496 unsigned mask; /* notify-events */
f7deaa1a 5497 ipp_attribute_t *notify_events,/* notify-events(-default) */
5498 *notify_lease; /* notify-lease-duration(-default) */
ef416fc2 5499
5500
bd7854cb 5501#ifdef DEBUG
5502 for (attr = con->request->attrs; attr; attr = attr->next)
5503 {
5504 if (attr->group_tag != IPP_TAG_ZERO)
3dfe78b3 5505 cupsdLogMessage(CUPSD_LOG_DEBUG2, "g%04x v%04x %s", attr->group_tag,
bd7854cb 5506 attr->value_tag, attr->name);
5507 else
3dfe78b3 5508 cupsdLogMessage(CUPSD_LOG_DEBUG2, "----SEP----");
bd7854cb 5509 }
5510#endif /* DEBUG */
5511
ef416fc2 5512 /*
5513 * Is the destination valid?
5514 */
5515
fa73b229 5516 cupsdLogMessage(CUPSD_LOG_DEBUG,
5517 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5518 con, con->http.fd, uri->values[0].string.text);
5519
ed486911 5520 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
5521 sizeof(scheme), userpass, sizeof(userpass), host,
a4d04587 5522 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 5523
5524 if (!strcmp(resource, "/"))
5525 {
ef416fc2 5526 dtype = (cups_ptype_t)0;
5527 printer = NULL;
5528 }
5529 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
5530 {
ef416fc2 5531 dtype = (cups_ptype_t)0;
5532 printer = NULL;
5533 }
5534 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
5535 {
ef416fc2 5536 dtype = CUPS_PRINTER_CLASS;
5537 printer = NULL;
5538 }
f7deaa1a 5539 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 5540 {
5541 /*
5542 * Bad URI...
5543 */
5544
5545 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 5546 _("The printer or class does not exist."));
ef416fc2 5547 return;
5548 }
5549
5550 /*
5551 * Check policy...
5552 */
5553
5554 if (printer)
5555 {
f7deaa1a 5556 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
5557 NULL)) != HTTP_OK)
ef416fc2 5558 {
f899b121 5559 send_http_error(con, status, printer);
ef416fc2 5560 return;
5561 }
5562 }
5563 else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5564 {
f899b121 5565 send_http_error(con, status, NULL);
ef416fc2 5566 return;
5567 }
5568
5569 /*
5570 * Get the user that is requesting the subscription...
5571 */
5572
e00b005a 5573 username = get_username(con);
ef416fc2 5574
5575 /*
5576 * Find the first subscription group attribute; return if we have
5577 * none...
5578 */
5579
5580 for (attr = con->request->attrs; attr; attr = attr->next)
5581 if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
5582 break;
5583
5584 if (!attr)
5585 {
5586 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 5587 _("No subscription attributes in request."));
ef416fc2 5588 return;
5589 }
5590
5591 /*
5592 * Process the subscription attributes in the request...
5593 */
5594
fa73b229 5595 con->response->request.status.status_code = IPP_BAD_REQUEST;
5596
ef416fc2 5597 while (attr)
5598 {
5599 recipient = NULL;
5600 pullmethod = NULL;
5601 user_data = NULL;
5602 interval = 0;
5603 lease = DefaultLeaseDuration;
5604 jobid = 0;
5605 mask = CUPSD_EVENT_NONE;
5606
f7deaa1a 5607 if (printer)
5608 {
5609 notify_events = ippFindAttribute(printer->attrs, "notify-events-default",
5610 IPP_TAG_KEYWORD);
5611 notify_lease = ippFindAttribute(printer->attrs,
5612 "notify-lease-duration-default",
5613 IPP_TAG_INTEGER);
5614
5615 if (notify_lease)
5616 lease = notify_lease->values[0].integer;
5617 }
5618 else
5619 {
5620 notify_events = NULL;
5621 notify_lease = NULL;
5622 }
5623
ef416fc2 5624 while (attr && attr->group_tag != IPP_TAG_ZERO)
5625 {
7594b224 5626 if (!strcmp(attr->name, "notify-recipient-uri") &&
ef416fc2 5627 attr->value_tag == IPP_TAG_URI)
ed486911 5628 {
5629 /*
5630 * Validate the recipient scheme against the ServerBin/notifier
5631 * directory...
5632 */
5633
5634 char notifier[1024]; /* Notifier filename */
5635
5636
ef416fc2 5637 recipient = attr->values[0].string.text;
ed486911 5638
8ca02f3c 5639 if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient,
5640 scheme, sizeof(scheme), userpass, sizeof(userpass),
5641 host, sizeof(host), &port,
5642 resource, sizeof(resource)) < HTTP_URI_OK)
ed486911 5643 {
5644 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 5645 _("Bad notify-recipient-uri \"%s\"."), recipient);
ed486911 5646 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5647 "notify-status-code", IPP_URI_SCHEME);
5648 return;
5649 }
5650
5651 snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
5652 scheme);
5653 if (access(notifier, X_OK))
5654 {
5655 send_ipp_status(con, IPP_NOT_POSSIBLE,
bc44d920 5656 _("notify-recipient-uri URI \"%s\" uses unknown "
84315f46 5657 "scheme."), recipient);
ed486911 5658 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5659 "notify-status-code", IPP_URI_SCHEME);
5660 return;
5661 }
3dfe78b3
MS
5662
5663 if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient))
5664 {
5665 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 5666 _("notify-recipient-uri URI \"%s\" is already used."),
3dfe78b3
MS
5667 recipient);
5668 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5669 "notify-status-code", IPP_ATTRIBUTES);
5670 return;
5671 }
ed486911 5672 }
ef416fc2 5673 else if (!strcmp(attr->name, "notify-pull-method") &&
5674 attr->value_tag == IPP_TAG_KEYWORD)
ed486911 5675 {
ef416fc2 5676 pullmethod = attr->values[0].string.text;
ed486911 5677
5678 if (strcmp(pullmethod, "ippget"))
5679 {
5680 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 5681 _("Bad notify-pull-method \"%s\"."), pullmethod);
ed486911 5682 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5683 "notify-status-code", IPP_ATTRIBUTES);
5684 return;
5685 }
5686 }
ef416fc2 5687 else if (!strcmp(attr->name, "notify-charset") &&
5688 attr->value_tag == IPP_TAG_CHARSET &&
5689 strcmp(attr->values[0].string.text, "us-ascii") &&
5690 strcmp(attr->values[0].string.text, "utf-8"))
5691 {
5692 send_ipp_status(con, IPP_CHARSET,
84315f46 5693 _("Character set \"%s\" not supported."),
ef416fc2 5694 attr->values[0].string.text);
5695 return;
5696 }
5697 else if (!strcmp(attr->name, "notify-natural-language") &&
5698 (attr->value_tag != IPP_TAG_LANGUAGE ||
5699 strcmp(attr->values[0].string.text, DefaultLanguage)))
5700 {
5701 send_ipp_status(con, IPP_CHARSET,
84315f46 5702 _("Language \"%s\" not supported."),
ef416fc2 5703 attr->values[0].string.text);
5704 return;
5705 }
5706 else if (!strcmp(attr->name, "notify-user-data") &&
5707 attr->value_tag == IPP_TAG_STRING)
5708 {
5709 if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
5710 {
5711 send_ipp_status(con, IPP_REQUEST_VALUE,
5712 _("The notify-user-data value is too large "
84315f46 5713 "(%d > 63 octets)."),
ef416fc2 5714 attr->values[0].unknown.length);
5715 return;
5716 }
5717
5718 user_data = attr;
5719 }
5720 else if (!strcmp(attr->name, "notify-events") &&
5721 attr->value_tag == IPP_TAG_KEYWORD)
f7deaa1a 5722 notify_events = attr;
ef416fc2 5723 else if (!strcmp(attr->name, "notify-lease-duration") &&
5724 attr->value_tag == IPP_TAG_INTEGER)
5725 lease = attr->values[0].integer;
5726 else if (!strcmp(attr->name, "notify-time-interval") &&
5727 attr->value_tag == IPP_TAG_INTEGER)
5728 interval = attr->values[0].integer;
5729 else if (!strcmp(attr->name, "notify-job-id") &&
5730 attr->value_tag == IPP_TAG_INTEGER)
5731 jobid = attr->values[0].integer;
5732
5733 attr = attr->next;
5734 }
5735
f7deaa1a 5736 if (notify_events)
5737 {
5738 for (i = 0; i < notify_events->num_values; i ++)
5739 mask |= cupsdEventValue(notify_events->values[i].string.text);
5740 }
5741
bd7854cb 5742 if (recipient)
5743 cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient);
5744 if (pullmethod)
5745 cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod);
5746 cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease);
5747 cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval);
fa73b229 5748
ef416fc2 5749 if (!recipient && !pullmethod)
5750 break;
5751
5752 if (mask == CUPSD_EVENT_NONE)
5753 {
5754 if (jobid)
5755 mask = CUPSD_EVENT_JOB_COMPLETED;
5756 else if (printer)
5757 mask = CUPSD_EVENT_PRINTER_STATE_CHANGED;
5758 else
5759 {
5760 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 5761 _("notify-events not specified."));
ef416fc2 5762 return;
5763 }
5764 }
5765
bd7854cb 5766 if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration))
ef416fc2 5767 {
5768 cupsdLogMessage(CUPSD_LOG_INFO,
5769 "create_subscription: Limiting notify-lease-duration to "
5770 "%d seconds.",
5771 MaxLeaseDuration);
5772 lease = MaxLeaseDuration;
5773 }
5774
5775 if (jobid)
5776 {
5777 if ((job = cupsdFindJob(jobid)) == NULL)
5778 {
84315f46
MS
5779 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
5780 jobid);
ef416fc2 5781 return;
5782 }
5783 }
5784 else
5785 job = NULL;
5786
52f6f666
MS
5787 if ((sub = cupsdAddSubscription(mask, printer, job, recipient, 0)) == NULL)
5788 {
5789 send_ipp_status(con, IPP_TOO_MANY_SUBSCRIPTIONS,
5790 _("There are too many subscriptions."));
5791 return;
5792 }
ef416fc2 5793
fa73b229 5794 if (job)
84315f46 5795 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription #%d for job %d.",
fa73b229 5796 sub->id, job->id);
5797 else if (printer)
5798 cupsdLogMessage(CUPSD_LOG_DEBUG,
84315f46 5799 "Added subscription #%d for printer \"%s\".",
fa73b229 5800 sub->id, printer->name);
5801 else
84315f46 5802 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription #%d for server.",
fa73b229 5803 sub->id);
5804
ef416fc2 5805 sub->interval = interval;
5806 sub->lease = lease;
bd7854cb 5807 sub->expire = lease ? time(NULL) + lease : 0;
ef416fc2 5808
5809 cupsdSetString(&sub->owner, username);
5810
5811 if (user_data)
5812 {
5813 sub->user_data_len = user_data->values[0].unknown.length;
5814 memcpy(sub->user_data, user_data->values[0].unknown.data,
5815 sub->user_data_len);
5816 }
5817
5818 ippAddSeparator(con->response);
5819 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5820 "notify-subscription-id", sub->id);
5821
fa73b229 5822 con->response->request.status.status_code = IPP_OK;
5823
ef416fc2 5824 if (attr)
5825 attr = attr->next;
5826 }
5827
3dfe78b3 5828 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
ef416fc2 5829}
5830
5831
5832/*
5833 * 'delete_printer()' - Remove a printer or class from the system.
5834 */
5835
5836static void
5837delete_printer(cupsd_client_t *con, /* I - Client connection */
5838 ipp_attribute_t *uri) /* I - URI of printer or class */
5839{
5840 http_status_t status; /* Policy status */
bc44d920 5841 cups_ptype_t dtype; /* Destination type (printer/class) */
ef416fc2 5842 cupsd_printer_t *printer; /* Printer/class */
5843 char filename[1024]; /* Script/PPD filename */
5844
5845
5846 cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con,
5847 con->http.fd, uri->values[0].string.text);
5848
5849 /*
5850 * Do we have a valid URI?
5851 */
5852
f7deaa1a 5853 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 5854 {
5855 /*
5856 * Bad URI...
5857 */
5858
5859 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 5860 _("The printer or class does not exist."));
ef416fc2 5861 return;
5862 }
5863
5864 /*
5865 * Check policy...
5866 */
5867
5868 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5869 {
f899b121 5870 send_http_error(con, status, NULL);
ef416fc2 5871 return;
5872 }
5873
5874 /*
5875 * Remove old jobs...
5876 */
5877
f7deaa1a 5878 cupsdCancelJobs(printer->name, NULL, 1);
ef416fc2 5879
5880 /*
5881 * Remove old subscriptions and send a "deleted printer" event...
5882 */
5883
5884 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL,
5885 "%s \"%s\" deleted by \"%s\".",
5886 (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
f7deaa1a 5887 printer->name, get_username(con));
ef416fc2 5888
5889 cupsdExpireSubscriptions(printer, NULL);
f7deaa1a 5890
ef416fc2 5891 /*
5892 * Remove any old PPD or script files...
5893 */
5894
f7deaa1a 5895 snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
5896 printer->name);
ef416fc2 5897 unlink(filename);
5898
f7deaa1a 5899 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
5900 printer->name);
ef416fc2 5901 unlink(filename);
5902
7cf5915e 5903 snprintf(filename, sizeof(filename), "%s/%s.png", CacheDir, printer->name);
61cf44e2
MS
5904 unlink(filename);
5905
f14324a7 5906 snprintf(filename, sizeof(filename), "%s/%s.data", CacheDir, printer->name);
54afec33
MS
5907 unlink(filename);
5908
568fa3fa
MS
5909 /*
5910 * Unregister color profiles...
5911 */
5912
a29fd7dd 5913 cupsdUnregisterColor(printer);
568fa3fa 5914
ef416fc2 5915 if (dtype & CUPS_PRINTER_CLASS)
5916 {
f7deaa1a 5917 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".",
5918 printer->name, get_username(con));
ef416fc2 5919
5920 cupsdDeletePrinter(printer, 0);
3dfe78b3 5921 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
ef416fc2 5922 }
5923 else
5924 {
f7deaa1a 5925 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".",
5926 printer->name, get_username(con));
ef416fc2 5927
f8b3a85b
MS
5928 if (cupsdDeletePrinter(printer, 0))
5929 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
5930
3dfe78b3 5931 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
ef416fc2 5932 }
5933
3dfe78b3 5934 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
ef416fc2 5935
5936 /*
5937 * Return with no errors...
5938 */
5939
5940 con->response->request.status.status_code = IPP_OK;
5941}
5942
5943
5944/*
5945 * 'get_default()' - Get the default destination.
5946 */
5947
5948static void
5949get_default(cupsd_client_t *con) /* I - Client connection */
5950{
fa73b229 5951 http_status_t status; /* Policy status */
5952 cups_array_t *ra; /* Requested attributes array */
ef416fc2 5953
5954
5955 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->http.fd);
5956
5957 /*
5958 * Check policy...
5959 */
5960
5961 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5962 {
f899b121 5963 send_http_error(con, status, NULL);
ef416fc2 5964 return;
5965 }
5966
fa73b229 5967 if (DefaultPrinter)
ef416fc2 5968 {
fa73b229 5969 ra = create_requested_array(con->request);
ef416fc2 5970
fa73b229 5971 copy_printer_attrs(con, DefaultPrinter, ra);
ef416fc2 5972
fa73b229 5973 cupsArrayDelete(ra);
ef416fc2 5974
fa73b229 5975 con->response->request.status.status_code = IPP_OK;
ef416fc2 5976 }
5977 else
84315f46 5978 send_ipp_status(con, IPP_NOT_FOUND, _("No default printer."));
ef416fc2 5979}
5980
5981
5982/*
5983 * 'get_devices()' - Get the list of available devices on the local system.
5984 */
5985
5986static void
5987get_devices(cupsd_client_t *con) /* I - Client connection */
5988{
5989 http_status_t status; /* Policy status */
ae71f5de
MS
5990 ipp_attribute_t *limit, /* limit attribute */
5991 *timeout, /* timeout attribute */
5992 *requested, /* requested-attributes attribute */
ed6e7faf
MS
5993 *exclude, /* exclude-schemes attribute */
5994 *include; /* include-schemes attribute */
ef416fc2 5995 char command[1024], /* cups-deviced command */
ed6e7faf 5996 options[2048], /* Options to pass to command */
ae71f5de 5997 requested_str[256],
89d46774 5998 /* String for requested attributes */
ed6e7faf
MS
5999 exclude_str[512],
6000 /* String for excluded schemes */
6001 include_str[512];
6002 /* String for included schemes */
ef416fc2 6003
6004
6005 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->http.fd);
6006
6007 /*
6008 * Check policy...
6009 */
6010
6011 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6012 {
f899b121 6013 send_http_error(con, status, NULL);
ef416fc2 6014 return;
6015 }
6016
6017 /*
6018 * Run cups-deviced command with the given options...
6019 */
6020
ae71f5de
MS
6021 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
6022 timeout = ippFindAttribute(con->request, "timeout", IPP_TAG_INTEGER);
ef416fc2 6023 requested = ippFindAttribute(con->request, "requested-attributes",
6024 IPP_TAG_KEYWORD);
ae71f5de 6025 exclude = ippFindAttribute(con->request, "exclude-schemes", IPP_TAG_NAME);
ed6e7faf 6026 include = ippFindAttribute(con->request, "include-schemes", IPP_TAG_NAME);
ef416fc2 6027
6028 if (requested)
89d46774 6029 url_encode_attr(requested, requested_str, sizeof(requested_str));
ef416fc2 6030 else
89d46774 6031 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
ef416fc2 6032
ae71f5de
MS
6033 if (exclude)
6034 url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
6035 else
6036 exclude_str[0] = '\0';
6037
ed6e7faf
MS
6038 if (include)
6039 url_encode_attr(include, include_str, sizeof(include_str));
6040 else
6041 include_str[0] = '\0';
6042
ef416fc2 6043 snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin);
6044 snprintf(options, sizeof(options),
ed6e7faf 6045 "%d+%d+%d+%d+%s%s%s%s%s",
ef416fc2 6046 con->request->request.op.request_id,
ae71f5de 6047 limit ? limit->values[0].integer : 0,
7a0cbd5e 6048 timeout ? timeout->values[0].integer : 15,
ae71f5de
MS
6049 (int)User,
6050 requested_str,
ed6e7faf
MS
6051 exclude_str[0] ? "%20" : "", exclude_str,
6052 include_str[0] ? "%20" : "", include_str);
ef416fc2 6053
6054 if (cupsdSendCommand(con, command, options, 1))
6055 {
6056 /*
6057 * Command started successfully, don't send an IPP response here...
6058 */
6059
6060 ippDelete(con->response);
6061 con->response = NULL;
6062 }
6063 else
6064 {
6065 /*
6066 * Command failed, return "internal error" so the user knows something
6067 * went wrong...
6068 */
6069
6070 send_ipp_status(con, IPP_INTERNAL_ERROR,
6071 _("cups-deviced failed to execute."));
6072 }
6073}
6074
6075
2e4ff8af
MS
6076/*
6077 * 'get_document()' - Get a copy of a job file.
6078 */
6079
6080static void
6081get_document(cupsd_client_t *con, /* I - Client connection */
6082 ipp_attribute_t *uri) /* I - Job URI */
6083{
6084 http_status_t status; /* Policy status */
6085 ipp_attribute_t *attr; /* Current attribute */
6086 int jobid; /* Job ID */
6087 int docnum; /* Document number */
6088 cupsd_job_t *job; /* Current job */
61cf44e2 6089 char scheme[HTTP_MAX_URI], /* Method portion of URI */
2e4ff8af
MS
6090 username[HTTP_MAX_URI], /* Username portion of URI */
6091 host[HTTP_MAX_URI], /* Host portion of URI */
6092 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6093 int port; /* Port portion of URI */
6094 char filename[1024], /* Filename for document */
6095 format[1024]; /* Format for document */
6096
6097
6098 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_document(%p[%d], %s)", con,
6099 con->http.fd, uri->values[0].string.text);
6100
6101 /*
6102 * See if we have a job URI or a printer URI...
6103 */
6104
6105 if (!strcmp(uri->name, "printer-uri"))
6106 {
6107 /*
6108 * Got a printer URI; see if we also have a job-id attribute...
6109 */
6110
6111 if ((attr = ippFindAttribute(con->request, "job-id",
6112 IPP_TAG_INTEGER)) == NULL)
6113 {
6114 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 6115 _("Got a printer-uri attribute but no job-id."));
2e4ff8af
MS
6116 return;
6117 }
6118
6119 jobid = attr->values[0].integer;
6120 }
6121 else
6122 {
6123 /*
6124 * Got a job URI; parse it to get the job ID...
6125 */
6126
61cf44e2
MS
6127 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6128 sizeof(scheme), username, sizeof(username), host,
2e4ff8af
MS
6129 sizeof(host), &port, resource, sizeof(resource));
6130
6131 if (strncmp(resource, "/jobs/", 6))
6132 {
6133 /*
6134 * Not a valid URI!
6135 */
6136
84315f46 6137 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
2e4ff8af
MS
6138 uri->values[0].string.text);
6139 return;
6140 }
6141
6142 jobid = atoi(resource + 6);
6143 }
6144
6145 /*
6146 * See if the job exists...
6147 */
6148
6149 if ((job = cupsdFindJob(jobid)) == NULL)
6150 {
6151 /*
6152 * Nope - return a "not found" error...
6153 */
6154
84315f46 6155 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
2e4ff8af
MS
6156 return;
6157 }
6158
6159 /*
6160 * Check policy...
6161 */
6162
e60ec91f
MS
6163 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con,
6164 job->username)) != HTTP_OK)
2e4ff8af
MS
6165 {
6166 send_http_error(con, status, NULL);
6167 return;
6168 }
6169
6170 /*
6171 * Get the document number...
6172 */
6173
6174 if ((attr = ippFindAttribute(con->request, "document-number",
6175 IPP_TAG_INTEGER)) == NULL)
6176 {
6177 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 6178 _("Missing document-number attribute."));
2e4ff8af
MS
6179 return;
6180 }
6181
6182 if ((docnum = attr->values[0].integer) < 1 || docnum > job->num_files ||
6183 attr->num_values > 1)
6184 {
84315f46
MS
6185 send_ipp_status(con, IPP_NOT_FOUND,
6186 _("Document #%d does not exist in job #%d."), docnum,
6187 jobid);
2e4ff8af
MS
6188 return;
6189 }
6190
6191 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, jobid,
6192 docnum);
6193 if ((con->file = open(filename, O_RDONLY)) == -1)
6194 {
6195 cupsdLogMessage(CUPSD_LOG_ERROR,
6196 "Unable to open document %d in job %d - %s", docnum, jobid,
6197 strerror(errno));
6198 send_ipp_status(con, IPP_NOT_FOUND,
84315f46
MS
6199 _("Unable to open document #%d in job #%d."), docnum,
6200 jobid);
2e4ff8af
MS
6201 return;
6202 }
6203
6204 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
6205
6206 cupsdLoadJob(job);
6207
6208 snprintf(format, sizeof(format), "%s/%s", job->filetypes[docnum - 1]->super,
6209 job->filetypes[docnum - 1]->type);
6210
6211 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
6212 NULL, format);
6213 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "document-number",
6214 docnum);
6215 if ((attr = ippFindAttribute(job->attrs, "document-name",
6216 IPP_TAG_NAME)) != NULL)
6217 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "document-name",
6218 NULL, attr->values[0].string.text);
6219}
6220
6221
ef416fc2 6222/*
fa73b229 6223 * 'get_job_attrs()' - Get job attributes.
ef416fc2 6224 */
6225
6226static void
fa73b229 6227get_job_attrs(cupsd_client_t *con, /* I - Client connection */
6228 ipp_attribute_t *uri) /* I - Job URI */
ef416fc2 6229{
6230 http_status_t status; /* Policy status */
fa73b229 6231 ipp_attribute_t *attr; /* Current attribute */
6232 int jobid; /* Job ID */
6233 cupsd_job_t *job; /* Current job */
ef55b745 6234 cupsd_printer_t *printer; /* Current printer */
10d09e33
MS
6235 cupsd_policy_t *policy; /* Current security policy */
6236 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
ef416fc2 6237 username[HTTP_MAX_URI], /* Username portion of URI */
6238 host[HTTP_MAX_URI], /* Host portion of URI */
6239 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6240 int port; /* Port portion of URI */
10d09e33
MS
6241 cups_array_t *ra, /* Requested attributes array */
6242 *exclude; /* Private attributes array */
ef416fc2 6243
6244
fa73b229 6245 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con,
6246 con->http.fd, uri->values[0].string.text);
ef416fc2 6247
6248 /*
fa73b229 6249 * See if we have a job URI or a printer URI...
6250 */
6251
6252 if (!strcmp(uri->name, "printer-uri"))
6253 {
6254 /*
6255 * Got a printer URI; see if we also have a job-id attribute...
6256 */
6257
6258 if ((attr = ippFindAttribute(con->request, "job-id",
6259 IPP_TAG_INTEGER)) == NULL)
6260 {
6261 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 6262 _("Got a printer-uri attribute but no job-id."));
fa73b229 6263 return;
6264 }
6265
6266 jobid = attr->values[0].integer;
6267 }
6268 else
6269 {
6270 /*
6271 * Got a job URI; parse it to get the job ID...
6272 */
6273
61cf44e2
MS
6274 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6275 sizeof(scheme), username, sizeof(username), host,
a4d04587 6276 sizeof(host), &port, resource, sizeof(resource));
fa73b229 6277
6278 if (strncmp(resource, "/jobs/", 6))
6279 {
6280 /*
6281 * Not a valid URI!
6282 */
6283
84315f46 6284 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
fa73b229 6285 uri->values[0].string.text);
6286 return;
6287 }
6288
6289 jobid = atoi(resource + 6);
6290 }
6291
6292 /*
6293 * See if the job exists...
6294 */
6295
6296 if ((job = cupsdFindJob(jobid)) == NULL)
6297 {
6298 /*
6299 * Nope - return a "not found" error...
6300 */
6301
84315f46 6302 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
fa73b229 6303 return;
6304 }
6305
6306 /*
6307 * Check policy...
6308 */
6309
ef55b745
MS
6310 if ((printer = job->printer) == NULL)
6311 printer = cupsdFindDest(job->dest);
6312
6313 if (printer)
10d09e33
MS
6314 policy = printer->op_policy_ptr;
6315 else
6316 policy = DefaultPolicyPtr;
6317
6318 if ((status = cupsdCheckPolicy(policy, con, job->username)) != HTTP_OK)
fa73b229 6319 {
f899b121 6320 send_http_error(con, status, NULL);
fa73b229 6321 return;
6322 }
6323
10d09e33
MS
6324 exclude = cupsdGetPrivateAttrs(policy, con, printer, job->username);
6325
fa73b229 6326 /*
6327 * Copy attributes...
6328 */
6329
bd7854cb 6330 cupsdLoadJob(job);
6331
fa73b229 6332 ra = create_requested_array(con->request);
10d09e33 6333 copy_job_attrs(con, job, ra, exclude);
fa73b229 6334 cupsArrayDelete(ra);
6335
6336 con->response->request.status.status_code = IPP_OK;
6337}
6338
6339
6340/*
6341 * 'get_jobs()' - Get a list of jobs for the specified printer.
6342 */
6343
6344static void
6345get_jobs(cupsd_client_t *con, /* I - Client connection */
6346 ipp_attribute_t *uri) /* I - Printer URI */
6347{
6348 http_status_t status; /* Policy status */
6349 ipp_attribute_t *attr; /* Current attribute */
6350 const char *dest; /* Destination */
bc44d920 6351 cups_ptype_t dtype; /* Destination type (printer/class) */
fa73b229 6352 cups_ptype_t dmask; /* Destination type mask */
f7deaa1a 6353 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
fa73b229 6354 username[HTTP_MAX_URI], /* Username portion of URI */
6355 host[HTTP_MAX_URI], /* Host portion of URI */
6356 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6357 int port; /* Port portion of URI */
aaf19ab0
MS
6358 int job_comparison; /* Job comparison */
6359 ipp_jstate_t job_state; /* job-state value */
fa73b229 6360 int first_job_id; /* First job ID */
6361 int limit; /* Maximum number of jobs to return */
6362 int count; /* Number of jobs that match */
aaf19ab0 6363 ipp_attribute_t *job_ids; /* job-ids attribute */
fa73b229 6364 cupsd_job_t *job; /* Current job pointer */
6365 cupsd_printer_t *printer; /* Printer */
6366 cups_array_t *list; /* Which job list... */
10d09e33
MS
6367 cups_array_t *ra, /* Requested attributes array */
6368 *exclude; /* Private attributes array */
6369 cupsd_policy_t *policy; /* Current policy */
fa73b229 6370
6371
6372 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->http.fd,
6373 uri->values[0].string.text);
6374
6375 /*
6376 * Is the destination valid?
ef416fc2 6377 */
6378
4b3f67ff
MS
6379 if (strcmp(uri->name, "printer-uri"))
6380 {
84315f46 6381 send_ipp_status(con, IPP_BAD_REQUEST, _("No printer-uri in request."));
4b3f67ff
MS
6382 return;
6383 }
6384
f7deaa1a 6385 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6386 sizeof(scheme), username, sizeof(username), host,
a4d04587 6387 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 6388
b9faaae1 6389 if (!strcmp(resource, "/") || !strcmp(resource, "/jobs"))
ef416fc2 6390 {
6391 dest = NULL;
6392 dtype = (cups_ptype_t)0;
6393 dmask = (cups_ptype_t)0;
6394 printer = NULL;
6395 }
fa73b229 6396 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
ef416fc2 6397 {
6398 dest = NULL;
6399 dtype = (cups_ptype_t)0;
6400 dmask = CUPS_PRINTER_CLASS;
6401 printer = NULL;
6402 }
fa73b229 6403 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
ef416fc2 6404 {
6405 dest = NULL;
6406 dtype = CUPS_PRINTER_CLASS;
6407 dmask = CUPS_PRINTER_CLASS;
6408 printer = NULL;
6409 }
f7deaa1a 6410 else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype,
6411 &printer)) == NULL)
ef416fc2 6412 {
6413 /*
6414 * Bad URI...
6415 */
6416
6417 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 6418 _("The printer or class does not exist."));
ef416fc2 6419 return;
6420 }
6421 else
b86bc4cf 6422 {
6423 dtype &= CUPS_PRINTER_CLASS;
ef416fc2 6424 dmask = CUPS_PRINTER_CLASS;
b86bc4cf 6425 }
ef416fc2 6426
6427 /*
6428 * Check policy...
6429 */
6430
6431 if (printer)
10d09e33
MS
6432 policy = printer->op_policy_ptr;
6433 else
6434 policy = DefaultPolicyPtr;
6435
6436 if ((status = cupsdCheckPolicy(policy, con, NULL)) != HTTP_OK)
ef416fc2 6437 {
f899b121 6438 send_http_error(con, status, NULL);
ef416fc2 6439 return;
6440 }
6441
aaf19ab0
MS
6442 job_ids = ippFindAttribute(con->request, "job-ids", IPP_TAG_INTEGER);
6443
ef416fc2 6444 /*
6445 * See if the "which-jobs" attribute have been specified...
6446 */
6447
fa73b229 6448 if ((attr = ippFindAttribute(con->request, "which-jobs",
aaf19ab0
MS
6449 IPP_TAG_KEYWORD)) != NULL && job_ids)
6450 {
6451 send_ipp_status(con, IPP_CONFLICT,
6452 _("The %s attribute cannot be provided with job-ids."),
6453 "which-jobs");
6454 return;
6455 }
6456 else if (!attr || !strcmp(attr->values[0].string.text, "not-completed"))
6457 {
6458 job_comparison = -1;
6459 job_state = IPP_JOB_STOPPED;
6460 list = Jobs;
6461 }
6462 else if (!strcmp(attr->values[0].string.text, "completed"))
6463 {
6464 job_comparison = 1;
6465 job_state = IPP_JOB_CANCELED;
6466 list = Jobs;
6467 }
6468 else if (!strcmp(attr->values[0].string.text, "aborted"))
6469 {
6470 job_comparison = 0;
6471 job_state = IPP_JOB_ABORTED;
6472 list = Jobs;
6473 }
6474 else if (!strcmp(attr->values[0].string.text, "all"))
6475 {
6476 job_comparison = 1;
6477 job_state = IPP_JOB_PENDING;
6478 list = Jobs;
6479 }
6480 else if (!strcmp(attr->values[0].string.text, "canceled"))
6481 {
6482 job_comparison = 0;
6483 job_state = IPP_JOB_CANCELED;
6484 list = Jobs;
6485 }
6486 else if (!strcmp(attr->values[0].string.text, "pending"))
ef416fc2 6487 {
aaf19ab0
MS
6488 job_comparison = 0;
6489 job_state = IPP_JOB_PENDING;
6490 list = ActiveJobs;
ef416fc2 6491 }
aaf19ab0 6492 else if (!strcmp(attr->values[0].string.text, "pending-held"))
ef416fc2 6493 {
aaf19ab0
MS
6494 job_comparison = 0;
6495 job_state = IPP_JOB_HELD;
6496 list = ActiveJobs;
ef416fc2 6497 }
aaf19ab0 6498 else if (!strcmp(attr->values[0].string.text, "processing"))
3dfe78b3 6499 {
aaf19ab0
MS
6500 job_comparison = 0;
6501 job_state = IPP_JOB_PROCESSING;
6502 list = PrintingJobs;
6503 }
6504 else if (!strcmp(attr->values[0].string.text, "processing-stopped"))
6505 {
6506 job_comparison = 0;
6507 job_state = IPP_JOB_STOPPED;
6508 list = ActiveJobs;
3dfe78b3 6509 }
ef416fc2 6510 else
6511 {
aaf19ab0
MS
6512 send_ipp_status(con, IPP_ATTRIBUTES,
6513 _("The which-jobs value \"%s\" is not supported."),
6514 attr->values[0].string.text);
6515 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
6516 "which-jobs", NULL, attr->values[0].string.text);
6517 return;
ef416fc2 6518 }
6519
6520 /*
6521 * See if they want to limit the number of jobs reported...
6522 */
6523
fa73b229 6524 if ((attr = ippFindAttribute(con->request, "limit",
6525 IPP_TAG_INTEGER)) != NULL)
aaf19ab0
MS
6526 {
6527 if (job_ids)
6528 {
6529 send_ipp_status(con, IPP_CONFLICT,
6530 _("The %s attribute cannot be provided with job-ids."),
6531 "limit");
6532 return;
6533 }
6534
ef416fc2 6535 limit = attr->values[0].integer;
aaf19ab0 6536 }
ef416fc2 6537 else
3dfe78b3 6538 limit = 0;
ef416fc2 6539
6540 if ((attr = ippFindAttribute(con->request, "first-job-id",
6541 IPP_TAG_INTEGER)) != NULL)
aaf19ab0
MS
6542 {
6543 if (job_ids)
6544 {
6545 send_ipp_status(con, IPP_CONFLICT,
6546 _("The %s attribute cannot be provided with job-ids."),
6547 "first-job-id");
6548 return;
6549 }
6550
ef416fc2 6551 first_job_id = attr->values[0].integer;
aaf19ab0 6552 }
ef416fc2 6553 else
6554 first_job_id = 1;
6555
6556 /*
6557 * See if we only want to see jobs for a specific user...
6558 */
6559
fa73b229 6560 if ((attr = ippFindAttribute(con->request, "my-jobs",
aaf19ab0
MS
6561 IPP_TAG_BOOLEAN)) != NULL && job_ids)
6562 {
6563 send_ipp_status(con, IPP_CONFLICT,
6564 _("The %s attribute cannot be provided with job-ids."),
6565 "my-jobs");
6566 return;
6567 }
6568 else if (attr && attr->values[0].boolean)
e00b005a 6569 strlcpy(username, get_username(con), sizeof(username));
ef416fc2 6570 else
6571 username[0] = '\0';
6572
5a662dc0
MS
6573 if ((ra = create_requested_array(con->request)) == NULL &&
6574 !ippFindAttribute(con->request, "requested-attributes", IPP_TAG_KEYWORD))
6575 {
6576 /*
6577 * IPP conformance - Get-Jobs has a default requested-attributes value of
6578 * "job-id" and "job-uri".
6579 */
6580
6581 ra = cupsArrayNew((cups_array_func_t)strcmp, NULL);
6582 cupsArrayAdd(ra, "job-id");
6583 cupsArrayAdd(ra, "job-uri");
6584 }
ef416fc2 6585
6586 /*
6587 * OK, build a list of jobs for this printer...
6588 */
6589
aaf19ab0 6590 if (job_ids)
ef416fc2 6591 {
aaf19ab0 6592 int i; /* Looping var */
ef416fc2 6593
aaf19ab0
MS
6594 for (i = 0; i < job_ids->num_values; i ++)
6595 {
4220952d 6596 if (!cupsdFindJob(job_ids->values[i].integer))
aaf19ab0
MS
6597 break;
6598 }
ef416fc2 6599
aaf19ab0
MS
6600 if (i < job_ids->num_values)
6601 {
84315f46 6602 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
aaf19ab0
MS
6603 job_ids->values[i].integer);
6604 return;
6605 }
0a682745 6606
aaf19ab0
MS
6607 for (i = 0; i < job_ids->num_values; i ++)
6608 {
6609 job = cupsdFindJob(job_ids->values[i].integer);
0a682745 6610
aaf19ab0 6611 cupsdLoadJob(job);
ef416fc2 6612
aaf19ab0
MS
6613 if (!job->attrs)
6614 {
6615 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d",
6616 job->id);
6617 continue;
6618 }
ef416fc2 6619
aaf19ab0
MS
6620 if (i > 0)
6621 ippAddSeparator(con->response);
bd7854cb 6622
10d09e33
MS
6623 exclude = cupsdGetPrivateAttrs(job->printer ?
6624 job->printer->op_policy_ptr :
6625 policy, con, job->printer,
6626 job->username);
6627
6628 copy_job_attrs(con, job, ra, exclude);
d1c13e16 6629 }
aaf19ab0
MS
6630 }
6631 else
6632 {
6633 for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list);
6634 (limit <= 0 || count < limit) && job;
6635 job = (cupsd_job_t *)cupsArrayNext(list))
6636 {
6637 /*
6638 * Filter out jobs that don't match...
6639 */
bd7854cb 6640
aaf19ab0
MS
6641 cupsdLogMessage(CUPSD_LOG_DEBUG2,
6642 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
6643 "state_value=%d, attrs=%p", job->id, job->dest,
6644 job->username, job->state_value, job->attrs);
7594b224 6645
aaf19ab0
MS
6646 if (!job->dest || !job->username)
6647 cupsdLoadJob(job);
fa73b229 6648
aaf19ab0
MS
6649 if (!job->dest || !job->username)
6650 continue;
ef416fc2 6651
aaf19ab0
MS
6652 if ((dest && strcmp(job->dest, dest)) &&
6653 (!job->printer || !dest || strcmp(job->printer->name, dest)))
6654 continue;
6655 if ((job->dtype & dmask) != dtype &&
6656 (!job->printer || (job->printer->type & dmask) != dtype))
6657 continue;
ef416fc2 6658
aaf19ab0
MS
6659 if ((job_comparison < 0 && job->state_value > job_state) ||
6660 (job_comparison == 0 && job->state_value != job_state) ||
6661 (job_comparison > 0 && job->state_value < job_state))
6662 continue;
6663
6664 if (job->id < first_job_id)
6665 continue;
6666
6667 cupsdLoadJob(job);
6668
6669 if (!job->attrs)
6670 {
6671 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d",
6672 job->id);
6673 continue;
6674 }
6675
88f9aafc 6676 if (username[0] && _cups_strcasecmp(username, job->username))
aaf19ab0
MS
6677 continue;
6678
6679 if (count > 0)
6680 ippAddSeparator(con->response);
6681
6682 count ++;
6683
10d09e33
MS
6684 exclude = cupsdGetPrivateAttrs(job->printer ?
6685 job->printer->op_policy_ptr :
6686 policy, con, job->printer,
6687 job->username);
6688
6689 copy_job_attrs(con, job, ra, exclude);
aaf19ab0
MS
6690 }
6691
6692 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count=%d", count);
6693 }
d1c13e16 6694
fa73b229 6695 cupsArrayDelete(ra);
ef416fc2 6696
fa73b229 6697 con->response->request.status.status_code = IPP_OK;
ef416fc2 6698}
6699
6700
6701/*
6702 * 'get_notifications()' - Get events for a subscription.
6703 */
6704
6705static void
bd7854cb 6706get_notifications(cupsd_client_t *con) /* I - Client connection */
ef416fc2 6707{
bd7854cb 6708 int i, j; /* Looping vars */
6709 http_status_t status; /* Policy status */
6710 cupsd_subscription_t *sub; /* Subscription */
6711 ipp_attribute_t *ids, /* notify-subscription-ids */
6712 *sequences; /* notify-sequence-numbers */
6713 int min_seq; /* Minimum sequence number */
6714 int interval; /* Poll interval */
6715
6716
f7deaa1a 6717 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])",
bd7854cb 6718 con, con->http.fd);
6719
6720 /*
6721 * Get subscription attributes...
6722 */
6723
6724 ids = ippFindAttribute(con->request, "notify-subscription-ids",
6725 IPP_TAG_INTEGER);
6726 sequences = ippFindAttribute(con->request, "notify-sequence-numbers",
6727 IPP_TAG_INTEGER);
6728
6729 if (!ids)
6730 {
6731 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 6732 _("Missing notify-subscription-ids attribute."));
bd7854cb 6733 return;
6734 }
6735
6736 /*
6737 * Are the subscription IDs valid?
6738 */
6739
6740 for (i = 0, interval = 60; i < ids->num_values; i ++)
6741 {
6742 if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL)
6743 {
6744 /*
6745 * Bad subscription ID...
6746 */
6747
84315f46 6748 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
bd7854cb 6749 ids->values[i].integer);
6750 return;
6751 }
6752
6753 /*
6754 * Check policy...
6755 */
6756
6757 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
6758 DefaultPolicyPtr,
6759 con, sub->owner)) != HTTP_OK)
6760 {
f899b121 6761 send_http_error(con, status, sub->dest);
bd7854cb 6762 return;
6763 }
6764
6765 /*
6766 * Check the subscription type and update the interval accordingly.
6767 */
6768
6769 if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING &&
6770 interval > 10)
6771 interval = 10;
6772 else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED)
6773 interval = 0;
6774 else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING &&
6775 interval > 30)
6776 interval = 30;
6777 }
6778
6779 /*
6780 * Tell the client to poll again in N seconds...
6781 */
6782
6783 if (interval > 0)
6784 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
6785 "notify-get-interval", interval);
6786
6787 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
6788 "printer-up-time", time(NULL));
6789
6790 /*
6791 * Copy the subscription event attributes to the response.
6792 */
6793
6794 con->response->request.status.status_code =
6795 interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE;
6796
6797 for (i = 0; i < ids->num_values; i ++)
6798 {
6799 /*
6800 * Get the subscription and sequence number...
6801 */
6802
6803 sub = cupsdFindSubscription(ids->values[i].integer);
6804
6805 if (sequences && i < sequences->num_values)
6806 min_seq = sequences->values[i].integer;
6807 else
6808 min_seq = 1;
6809
6810 /*
6811 * If we don't have any new events, nothing to do here...
6812 */
6813
10d09e33 6814 if (min_seq > (sub->first_event_id + cupsArrayCount(sub->events)))
bd7854cb 6815 continue;
6816
6817 /*
6818 * Otherwise copy all of the new events...
6819 */
6820
6821 if (sub->first_event_id > min_seq)
6822 j = 0;
6823 else
6824 j = min_seq - sub->first_event_id;
6825
10d09e33 6826 for (; j < cupsArrayCount(sub->events); j ++)
b423cd4c 6827 {
6828 ippAddSeparator(con->response);
ef416fc2 6829
10d09e33
MS
6830 copy_attrs(con->response,
6831 ((cupsd_event_t *)cupsArrayIndex(sub->events, j))->attrs, NULL,
6832 IPP_TAG_EVENT_NOTIFICATION, 0, NULL);
b423cd4c 6833 }
6834 }
6835}
ef416fc2 6836
ef416fc2 6837
b94498cf 6838/*
6839 * 'get_ppd()' - Get a named PPD from the local system.
6840 */
6841
6842static void
6843get_ppd(cupsd_client_t *con, /* I - Client connection */
6844 ipp_attribute_t *uri) /* I - Printer URI or PPD name */
6845{
6846 http_status_t status; /* Policy status */
6847 cupsd_printer_t *dest; /* Destination */
6848 cups_ptype_t dtype; /* Destination type */
6849
6850
6851 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con,
6852 con->http.fd, uri, uri->name, uri->values[0].string.text);
6853
6854 if (!strcmp(uri->name, "ppd-name"))
6855 {
6856 /*
6857 * Return a PPD file from cups-driverd...
6858 */
6859
6860 char command[1024], /* cups-driverd command */
6861 options[1024], /* Options to pass to command */
6862 ppd_name[1024]; /* ppd-name */
6863
6864
6865 /*
6866 * Check policy...
6867 */
6868
6869 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6870 {
6871 send_http_error(con, status, NULL);
6872 return;
6873 }
6874
6875 /*
6876 * Run cups-driverd command with the given options...
6877 */
6878
6879 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
6880 url_encode_string(uri->values[0].string.text, ppd_name, sizeof(ppd_name));
6881 snprintf(options, sizeof(options), "get+%d+%s",
6882 con->request->request.op.request_id, ppd_name);
6883
6884 if (cupsdSendCommand(con, command, options, 0))
6885 {
6886 /*
6887 * Command started successfully, don't send an IPP response here...
6888 */
6889
6890 ippDelete(con->response);
6891 con->response = NULL;
6892 }
6893 else
6894 {
6895 /*
6896 * Command failed, return "internal error" so the user knows something
6897 * went wrong...
6898 */
6899
6900 send_ipp_status(con, IPP_INTERNAL_ERROR,
6901 _("cups-driverd failed to execute."));
6902 }
6903 }
6904 else if (!strcmp(uri->name, "printer-uri") &&
6905 cupsdValidateDest(uri->values[0].string.text, &dtype, &dest))
6906 {
6907 int i; /* Looping var */
6908 char filename[1024]; /* PPD filename */
6909
6910
6911 /*
6912 * Check policy...
6913 */
6914
6915 if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK)
6916 {
2fb76298 6917 send_http_error(con, status, dest);
b94498cf 6918 return;
6919 }
6920
6921 /*
6922 * See if we need the PPD for a class or remote printer...
6923 */
6924
bc44d920 6925 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
6926 dest->name);
6927
6928 if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0))
b94498cf 6929 {
3d8365b8 6930 con->response->request.status.status_code = CUPS_SEE_OTHER;
b94498cf 6931 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI,
6932 "printer-uri", NULL, dest->uri);
6933 return;
6934 }
a2326b5b 6935 else if (dtype & CUPS_PRINTER_CLASS)
b94498cf 6936 {
6937 for (i = 0; i < dest->num_printers; i ++)
a2326b5b 6938 if (!(dest->printers[i]->type & CUPS_PRINTER_CLASS))
bc44d920 6939 {
6940 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
6941 dest->printers[i]->name);
6942
6943 if (!access(filename, 0))
6944 break;
6945 }
b94498cf 6946
6947 if (i < dest->num_printers)
6948 dest = dest->printers[i];
6949 else
6950 {
3d8365b8 6951 con->response->request.status.status_code = CUPS_SEE_OTHER;
b94498cf 6952 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI,
6953 "printer-uri", NULL, dest->printers[0]->uri);
6954 return;
6955 }
6956 }
6957
6958 /*
6959 * Found the printer with the PPD file, now see if there is one...
6960 */
6961
b94498cf 6962 if ((con->file = open(filename, O_RDONLY)) < 0)
6963 {
6964 send_ipp_status(con, IPP_NOT_FOUND,
6965 _("The PPD file \"%s\" could not be opened: %s"),
3d8365b8 6966 uri->values[0].string.text, strerror(errno));
b94498cf 6967 return;
6968 }
6969
6970 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
6971
6972 con->pipe_pid = 0;
6973
3d8365b8 6974 con->response->request.status.status_code = IPP_OK;
b94498cf 6975 }
6976 else
6977 send_ipp_status(con, IPP_NOT_FOUND,
6978 _("The PPD file \"%s\" could not be found."),
6979 uri->values[0].string.text);
6980}
6981
6982
b423cd4c 6983/*
6984 * 'get_ppds()' - Get the list of PPD files on the local system.
6985 */
ef416fc2 6986
b423cd4c 6987static void
6988get_ppds(cupsd_client_t *con) /* I - Client connection */
6989{
6990 http_status_t status; /* Policy status */
b423cd4c 6991 ipp_attribute_t *limit, /* Limit attribute */
b94498cf 6992 *device, /* ppd-device-id attribute */
6993 *language, /* ppd-natural-language attribute */
b423cd4c 6994 *make, /* ppd-make attribute */
b94498cf 6995 *model, /* ppd-make-and-model attribute */
3d8365b8 6996 *model_number, /* ppd-model-number attribute */
b94498cf 6997 *product, /* ppd-product attribute */
6998 *psversion, /* ppd-psverion attribute */
3d8365b8 6999 *type, /* ppd-type attribute */
ed6e7faf
MS
7000 *requested, /* requested-attributes attribute */
7001 *exclude, /* exclude-schemes attribute */
7002 *include; /* include-schemes attribute */
b94498cf 7003 char command[1024], /* cups-driverd command */
ed6e7faf 7004 options[4096], /* Options to pass to command */
b94498cf 7005 device_str[256],/* Escaped ppd-device-id string */
7006 language_str[256],
bc44d920 7007 /* Escaped ppd-natural-language */
b94498cf 7008 make_str[256], /* Escaped ppd-make string */
7009 model_str[256], /* Escaped ppd-make-and-model string */
3d8365b8 7010 model_number_str[256],
7011 /* ppd-model-number string */
b94498cf 7012 product_str[256],
7013 /* Escaped ppd-product string */
7014 psversion_str[256],
7015 /* Escaped ppd-psversion string */
3d8365b8 7016 type_str[256], /* Escaped ppd-type string */
ed6e7faf 7017 requested_str[256],
89d46774 7018 /* String for requested attributes */
ed6e7faf
MS
7019 exclude_str[512],
7020 /* String for excluded schemes */
7021 include_str[512];
7022 /* String for included schemes */
ef416fc2 7023
ef416fc2 7024
b423cd4c 7025 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->http.fd);
ef416fc2 7026
7027 /*
b423cd4c 7028 * Check policy...
ef416fc2 7029 */
7030
b423cd4c 7031 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
ef416fc2 7032 {
f899b121 7033 send_http_error(con, status, NULL);
b423cd4c 7034 return;
ef416fc2 7035 }
ef416fc2 7036
b423cd4c 7037 /*
7038 * Run cups-driverd command with the given options...
7039 */
7040
3d8365b8 7041 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
7042 device = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT);
7043 language = ippFindAttribute(con->request, "ppd-natural-language",
7044 IPP_TAG_LANGUAGE);
7045 make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT);
7046 model = ippFindAttribute(con->request, "ppd-make-and-model",
7047 IPP_TAG_TEXT);
7048 model_number = ippFindAttribute(con->request, "ppd-model-number",
7049 IPP_TAG_INTEGER);
7050 product = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT);
7051 psversion = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT);
7052 type = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD);
7053 requested = ippFindAttribute(con->request, "requested-attributes",
7054 IPP_TAG_KEYWORD);
8b450588
MS
7055 exclude = ippFindAttribute(con->request, "exclude-schemes",
7056 IPP_TAG_NAME);
7057 include = ippFindAttribute(con->request, "include-schemes",
7058 IPP_TAG_NAME);
b423cd4c 7059
7060 if (requested)
89d46774 7061 url_encode_attr(requested, requested_str, sizeof(requested_str));
7062 else
7063 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
ef416fc2 7064
b94498cf 7065 if (device)
7066 url_encode_attr(device, device_str, sizeof(device_str));
7067 else
7068 device_str[0] = '\0';
7069
7070 if (language)
7071 url_encode_attr(language, language_str, sizeof(language_str));
7072 else
7073 language_str[0] = '\0';
7074
89d46774 7075 if (make)
7076 url_encode_attr(make, make_str, sizeof(make_str));
b423cd4c 7077 else
89d46774 7078 make_str[0] = '\0';
fa73b229 7079
b94498cf 7080 if (model)
7081 url_encode_attr(model, model_str, sizeof(model_str));
7082 else
7083 model_str[0] = '\0';
7084
3d8365b8 7085 if (model_number)
7086 snprintf(model_number_str, sizeof(model_number_str), "ppd-model-number=%d",
7087 model_number->values[0].integer);
7088 else
7089 model_number_str[0] = '\0';
7090
b94498cf 7091 if (product)
7092 url_encode_attr(product, product_str, sizeof(product_str));
7093 else
7094 product_str[0] = '\0';
7095
7096 if (psversion)
7097 url_encode_attr(psversion, psversion_str, sizeof(psversion_str));
7098 else
7099 psversion_str[0] = '\0';
7100
3d8365b8 7101 if (type)
7102 url_encode_attr(type, type_str, sizeof(type_str));
7103 else
7104 type_str[0] = '\0';
7105
ed6e7faf
MS
7106 if (exclude)
7107 url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
7108 else
7109 exclude_str[0] = '\0';
7110
7111 if (include)
7112 url_encode_attr(include, include_str, sizeof(include_str));
7113 else
7114 include_str[0] = '\0';
7115
b423cd4c 7116 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
3d8365b8 7117 snprintf(options, sizeof(options),
ed6e7faf 7118 "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 7119 con->request->request.op.request_id,
7120 limit ? limit->values[0].integer : 0,
b94498cf 7121 requested_str,
7122 device ? "%20" : "", device_str,
7123 language ? "%20" : "", language_str,
7124 make ? "%20" : "", make_str,
7125 model ? "%20" : "", model_str,
3d8365b8 7126 model_number ? "%20" : "", model_number_str,
b94498cf 7127 product ? "%20" : "", product_str,
3d8365b8 7128 psversion ? "%20" : "", psversion_str,
ed6e7faf
MS
7129 type ? "%20" : "", type_str,
7130 exclude_str[0] ? "%20" : "", exclude_str,
7131 include_str[0] ? "%20" : "", include_str);
ef416fc2 7132
b423cd4c 7133 if (cupsdSendCommand(con, command, options, 0))
7134 {
7135 /*
7136 * Command started successfully, don't send an IPP response here...
7137 */
7138
7139 ippDelete(con->response);
7140 con->response = NULL;
7141 }
7142 else
7143 {
7144 /*
7145 * Command failed, return "internal error" so the user knows something
7146 * went wrong...
7147 */
7148
7149 send_ipp_status(con, IPP_INTERNAL_ERROR,
7150 _("cups-driverd failed to execute."));
7151 }
ef416fc2 7152}
7153
7154
7155/*
b423cd4c 7156 * 'get_printer_attrs()' - Get printer attributes.
ef416fc2 7157 */
7158
7159static void
b423cd4c 7160get_printer_attrs(cupsd_client_t *con, /* I - Client connection */
7161 ipp_attribute_t *uri) /* I - Printer URI */
ef416fc2 7162{
7163 http_status_t status; /* Policy status */
bc44d920 7164 cups_ptype_t dtype; /* Destination type (printer/class) */
b423cd4c 7165 cupsd_printer_t *printer; /* Printer/class */
ef416fc2 7166 cups_array_t *ra; /* Requested attributes array */
7167
7168
b423cd4c 7169 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con,
7170 con->http.fd, uri->values[0].string.text);
ef416fc2 7171
7172 /*
b423cd4c 7173 * Is the destination valid?
ef416fc2 7174 */
7175
f7deaa1a 7176 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 7177 {
7178 /*
b423cd4c 7179 * Bad URI...
ef416fc2 7180 */
7181
7182 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 7183 _("The printer or class does not exist."));
ef416fc2 7184 return;
7185 }
7186
7187 /*
7188 * Check policy...
7189 */
7190
b423cd4c 7191 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
ef416fc2 7192 {
f899b121 7193 send_http_error(con, status, printer);
ef416fc2 7194 return;
7195 }
7196
7197 /*
b423cd4c 7198 * Send the attributes...
ef416fc2 7199 */
7200
7201 ra = create_requested_array(con->request);
7202
b423cd4c 7203 copy_printer_attrs(con, printer, ra);
ef416fc2 7204
7205 cupsArrayDelete(ra);
7206
7207 con->response->request.status.status_code = IPP_OK;
7208}
7209
7210
c168a833
MS
7211/*
7212 * 'get_printer_supported()' - Get printer supported values.
7213 */
7214
7215static void
7216get_printer_supported(
7217 cupsd_client_t *con, /* I - Client connection */
7218 ipp_attribute_t *uri) /* I - Printer URI */
7219{
7220 http_status_t status; /* Policy status */
7221 cups_ptype_t dtype; /* Destination type (printer/class) */
7222 cupsd_printer_t *printer; /* Printer/class */
7223
7224
7225 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_supported(%p[%d], %s)", con,
7226 con->http.fd, uri->values[0].string.text);
7227
7228 /*
7229 * Is the destination valid?
7230 */
7231
7232 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7233 {
7234 /*
7235 * Bad URI...
7236 */
7237
7238 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 7239 _("The printer or class does not exist."));
c168a833
MS
7240 return;
7241 }
7242
7243 /*
7244 * Check policy...
7245 */
7246
7247 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
7248 {
7249 send_http_error(con, status, printer);
7250 return;
7251 }
7252
7253 /*
7254 * Return a list of attributes that can be set via Set-Printer-Attributes.
7255 */
7256
7257 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7258 "printer-info", 0);
7259 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7260 "printer-location", 0);
7261
7262 con->response->request.status.status_code = IPP_OK;
7263}
7264
7265
ef416fc2 7266/*
b423cd4c 7267 * 'get_printers()' - Get a list of printers or classes.
ef416fc2 7268 */
7269
7270static void
b423cd4c 7271get_printers(cupsd_client_t *con, /* I - Client connection */
7272 int type) /* I - 0 or CUPS_PRINTER_CLASS */
ef416fc2 7273{
b423cd4c 7274 http_status_t status; /* Policy status */
7275 ipp_attribute_t *attr; /* Current attribute */
bc44d920 7276 int limit; /* Max number of printers to return */
b423cd4c 7277 int count; /* Number of printers that match */
7278 cupsd_printer_t *printer; /* Current printer pointer */
7279 int printer_type, /* printer-type attribute */
7280 printer_mask; /* printer-type-mask attribute */
7281 char *location; /* Location string */
7282 const char *username; /* Current user */
7283 char *first_printer_name; /* first-printer-name attribute */
7284 cups_array_t *ra; /* Requested attributes array */
e07d4801 7285 int local; /* Local connection? */
ef416fc2 7286
ef416fc2 7287
b423cd4c 7288 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con,
7289 con->http.fd, type);
ef416fc2 7290
7291 /*
7292 * Check policy...
7293 */
7294
b423cd4c 7295 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
ef416fc2 7296 {
f899b121 7297 send_http_error(con, status, NULL);
ef416fc2 7298 return;
7299 }
7300
7301 /*
b423cd4c 7302 * Check for printers...
ef416fc2 7303 */
7304
b423cd4c 7305 if (!Printers || !cupsArrayCount(Printers))
7306 {
7307 send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added."));
7308 return;
7309 }
7310
7311 /*
7312 * See if they want to limit the number of printers reported...
7313 */
ef416fc2 7314
fa73b229 7315 if ((attr = ippFindAttribute(con->request, "limit",
7316 IPP_TAG_INTEGER)) != NULL)
ef416fc2 7317 limit = attr->values[0].integer;
7318 else
b423cd4c 7319 limit = 10000000;
7320
7321 if ((attr = ippFindAttribute(con->request, "first-printer-name",
7322 IPP_TAG_NAME)) != NULL)
7323 first_printer_name = attr->values[0].string.text;
7324 else
7325 first_printer_name = NULL;
ef416fc2 7326
7327 /*
b423cd4c 7328 * Support filtering...
ef416fc2 7329 */
7330
b423cd4c 7331 if ((attr = ippFindAttribute(con->request, "printer-type",
7332 IPP_TAG_ENUM)) != NULL)
7333 printer_type = attr->values[0].integer;
ef416fc2 7334 else
b423cd4c 7335 printer_type = 0;
ef416fc2 7336
b423cd4c 7337 if ((attr = ippFindAttribute(con->request, "printer-type-mask",
7338 IPP_TAG_ENUM)) != NULL)
7339 printer_mask = attr->values[0].integer;
ef416fc2 7340 else
b423cd4c 7341 printer_mask = 0;
e00b005a 7342
e07d4801
MS
7343 local = httpAddrLocalhost(&(con->clientaddr));
7344
b423cd4c 7345 if ((attr = ippFindAttribute(con->request, "printer-location",
7346 IPP_TAG_TEXT)) != NULL)
7347 location = attr->values[0].string.text;
7348 else
7349 location = NULL;
e00b005a 7350
7351 if (con->username[0])
b423cd4c 7352 username = con->username;
e00b005a 7353 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7354 IPP_TAG_NAME)) != NULL)
b423cd4c 7355 username = attr->values[0].string.text;
e00b005a 7356 else
b423cd4c 7357 username = NULL;
ef416fc2 7358
b423cd4c 7359 ra = create_requested_array(con->request);
ef416fc2 7360
7361 /*
b423cd4c 7362 * OK, build a list of printers for this printer...
ef416fc2 7363 */
7364
b423cd4c 7365 if (first_printer_name)
ef416fc2 7366 {
b423cd4c 7367 if ((printer = cupsdFindDest(first_printer_name)) == NULL)
7368 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
ef416fc2 7369 }
7370 else
b423cd4c 7371 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
ef416fc2 7372
b423cd4c 7373 for (count = 0;
7374 count < limit && printer;
7375 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
7376 {
e07d4801
MS
7377 if (!local && !printer->shared)
7378 continue;
7379
b423cd4c 7380 if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) &&
7381 (printer->type & printer_mask) == printer_type &&
b19ccc9e 7382 (!location ||
88f9aafc 7383 (printer->location && !_cups_strcasecmp(printer->location, location))))
ef416fc2 7384 {
b423cd4c 7385 /*
7386 * If a username is specified, see if it is allowed or denied
7387 * access...
7388 */
ef416fc2 7389
10d09e33
MS
7390 if (cupsArrayCount(printer->users) && username &&
7391 !user_allowed(printer, username))
b423cd4c 7392 continue;
ef416fc2 7393
b423cd4c 7394 /*
7395 * Add the group separator as needed...
7396 */
ef416fc2 7397
b423cd4c 7398 if (count > 0)
7399 ippAddSeparator(con->response);
ef416fc2 7400
b423cd4c 7401 count ++;
ef416fc2 7402
b423cd4c 7403 /*
7404 * Send the attributes...
7405 */
ef416fc2 7406
b423cd4c 7407 copy_printer_attrs(con, printer, ra);
7408 }
ef416fc2 7409 }
7410
b423cd4c 7411 cupsArrayDelete(ra);
ef416fc2 7412
7413 con->response->request.status.status_code = IPP_OK;
7414}
7415
7416
7417/*
b423cd4c 7418 * 'get_subscription_attrs()' - Get subscription attributes.
ef416fc2 7419 */
7420
7421static void
b423cd4c 7422get_subscription_attrs(
7423 cupsd_client_t *con, /* I - Client connection */
7424 int sub_id) /* I - Subscription ID */
ef416fc2 7425{
b423cd4c 7426 http_status_t status; /* Policy status */
7427 cupsd_subscription_t *sub; /* Subscription */
10d09e33
MS
7428 cupsd_policy_t *policy; /* Current security policy */
7429 cups_array_t *ra, /* Requested attributes array */
7430 *exclude; /* Private attributes array */
ef416fc2 7431
7432
b423cd4c 7433 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7434 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7435 con, con->http.fd, sub_id);
ef416fc2 7436
fa73b229 7437 /*
b423cd4c 7438 * Is the subscription ID valid?
fa73b229 7439 */
7440
b423cd4c 7441 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
fa73b229 7442 {
7443 /*
b423cd4c 7444 * Bad subscription ID...
fa73b229 7445 */
7446
84315f46
MS
7447 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
7448 sub_id);
fa73b229 7449 return;
7450 }
7451
7452 /*
7453 * Check policy...
7454 */
7455
10d09e33
MS
7456 if (sub->dest)
7457 policy = sub->dest->op_policy_ptr;
7458 else
7459 policy = DefaultPolicyPtr;
7460
7461 if ((status = cupsdCheckPolicy(policy, con, sub->owner)) != HTTP_OK)
fa73b229 7462 {
f899b121 7463 send_http_error(con, status, sub->dest);
fa73b229 7464 return;
7465 }
7466
10d09e33
MS
7467 exclude = cupsdGetPrivateAttrs(policy, con, sub->dest, sub->owner);
7468
ef416fc2 7469 /*
b423cd4c 7470 * Copy the subscription attributes to the response using the
7471 * requested-attributes attribute that may be provided by the client.
ef416fc2 7472 */
7473
b423cd4c 7474 ra = create_requested_array(con->request);
fa73b229 7475
10d09e33 7476 copy_subscription_attrs(con, sub, ra, exclude);
ef416fc2 7477
b423cd4c 7478 cupsArrayDelete(ra);
fa73b229 7479
b423cd4c 7480 con->response->request.status.status_code = IPP_OK;
7481}
fa73b229 7482
fa73b229 7483
b423cd4c 7484/*
7485 * 'get_subscriptions()' - Get subscriptions.
7486 */
ef416fc2 7487
b423cd4c 7488static void
7489get_subscriptions(cupsd_client_t *con, /* I - Client connection */
7490 ipp_attribute_t *uri) /* I - Printer/job URI */
7491{
7492 http_status_t status; /* Policy status */
7493 int count; /* Number of subscriptions */
7494 int limit; /* Limit */
7495 cupsd_subscription_t *sub; /* Subscription */
7496 cups_array_t *ra; /* Requested attributes array */
7497 ipp_attribute_t *attr; /* Attribute */
bc44d920 7498 cups_ptype_t dtype; /* Destination type (printer/class) */
f7deaa1a 7499 char scheme[HTTP_MAX_URI],
7500 /* Scheme portion of URI */
b423cd4c 7501 username[HTTP_MAX_URI],
7502 /* Username portion of URI */
7503 host[HTTP_MAX_URI],
7504 /* Host portion of URI */
7505 resource[HTTP_MAX_URI];
7506 /* Resource portion of URI */
7507 int port; /* Port portion of URI */
7508 cupsd_job_t *job; /* Job pointer */
7509 cupsd_printer_t *printer; /* Printer */
10d09e33
MS
7510 cupsd_policy_t *policy; /* Policy */
7511 cups_array_t *exclude; /* Private attributes array */
fa73b229 7512
fa73b229 7513
b423cd4c 7514 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7515 "get_subscriptions(con=%p[%d], uri=%s)",
7516 con, con->http.fd, uri->values[0].string.text);
7517
7518 /*
7519 * Is the destination valid?
7520 */
7521
f7deaa1a 7522 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7523 sizeof(scheme), username, sizeof(username), host,
b423cd4c 7524 sizeof(host), &port, resource, sizeof(resource));
7525
7526 if (!strcmp(resource, "/") ||
7527 (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) ||
7528 (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) ||
7529 (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9))
7530 {
7531 printer = NULL;
7532 job = NULL;
ef416fc2 7533 }
b423cd4c 7534 else if (!strncmp(resource, "/jobs/", 6) && resource[6])
ef416fc2 7535 {
b423cd4c 7536 printer = NULL;
7537 job = cupsdFindJob(atoi(resource + 6));
ef416fc2 7538
b423cd4c 7539 if (!job)
ef416fc2 7540 {
84315f46
MS
7541 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
7542 atoi(resource + 6));
ef416fc2 7543 return;
7544 }
b423cd4c 7545 }
f7deaa1a 7546 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
b423cd4c 7547 {
fa73b229 7548 /*
b423cd4c 7549 * Bad URI...
fa73b229 7550 */
7551
b423cd4c 7552 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 7553 _("The printer or class does not exist."));
b423cd4c 7554 return;
7555 }
7556 else if ((attr = ippFindAttribute(con->request, "notify-job-id",
7557 IPP_TAG_INTEGER)) != NULL)
7558 {
7559 job = cupsdFindJob(attr->values[0].integer);
ef416fc2 7560
b423cd4c 7561 if (!job)
fa73b229 7562 {
84315f46 7563 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
b423cd4c 7564 attr->values[0].integer);
fa73b229 7565 return;
7566 }
ef416fc2 7567 }
b423cd4c 7568 else
7569 job = NULL;
ef416fc2 7570
7571 /*
b423cd4c 7572 * Check policy...
ef416fc2 7573 */
7574
10d09e33
MS
7575 if (printer)
7576 policy = printer->op_policy_ptr;
7577 else
7578 policy = DefaultPolicyPtr;
7579
7580 if ((status = cupsdCheckPolicy(policy, con, NULL)) != HTTP_OK)
ef416fc2 7581 {
f899b121 7582 send_http_error(con, status, printer);
b423cd4c 7583 return;
7584 }
ef416fc2 7585
b423cd4c 7586 /*
7587 * Copy the subscription attributes to the response using the
7588 * requested-attributes attribute that may be provided by the client.
7589 */
ef416fc2 7590
b423cd4c 7591 ra = create_requested_array(con->request);
ef416fc2 7592
b423cd4c 7593 if ((attr = ippFindAttribute(con->request, "limit",
7594 IPP_TAG_INTEGER)) != NULL)
7595 limit = attr->values[0].integer;
7596 else
7597 limit = 0;
ef416fc2 7598
b423cd4c 7599 /*
7600 * See if we only want to see subscriptions for a specific user...
7601 */
ef416fc2 7602
b423cd4c 7603 if ((attr = ippFindAttribute(con->request, "my-subscriptions",
7604 IPP_TAG_BOOLEAN)) != NULL &&
7605 attr->values[0].boolean)
7606 strlcpy(username, get_username(con), sizeof(username));
fa73b229 7607 else
b423cd4c 7608 username[0] = '\0';
ef416fc2 7609
b423cd4c 7610 for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0;
7611 sub;
7612 sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
7613 if ((!printer || sub->dest == printer) && (!job || sub->job == job) &&
88f9aafc 7614 (!username[0] || !_cups_strcasecmp(username, sub->owner)))
fa73b229 7615 {
b423cd4c 7616 ippAddSeparator(con->response);
10d09e33
MS
7617
7618 exclude = cupsdGetPrivateAttrs(sub->dest ? sub->dest->op_policy_ptr :
7619 policy, con, sub->dest,
7620 sub->owner);
7621
7622 copy_subscription_attrs(con, sub, ra, exclude);
ef416fc2 7623
b423cd4c 7624 count ++;
7625 if (limit && count >= limit)
7626 break;
7627 }
ef416fc2 7628
b423cd4c 7629 cupsArrayDelete(ra);
fa73b229 7630
b423cd4c 7631 if (count)
7632 con->response->request.status.status_code = IPP_OK;
7633 else
7634 send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found."));
7635}
ef416fc2 7636
ef416fc2 7637
b423cd4c 7638/*
7639 * 'get_username()' - Get the username associated with a request.
7640 */
ef416fc2 7641
b423cd4c 7642static const char * /* O - Username */
7643get_username(cupsd_client_t *con) /* I - Connection */
7644{
7645 ipp_attribute_t *attr; /* Attribute */
ef416fc2 7646
ef416fc2 7647
b423cd4c 7648 if (con->username[0])
7649 return (con->username);
7650 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7651 IPP_TAG_NAME)) != NULL)
7652 return (attr->values[0].string.text);
7653 else
7654 return ("anonymous");
ef416fc2 7655}
7656
7657
7658/*
b423cd4c 7659 * 'hold_job()' - Hold a print job.
ef416fc2 7660 */
7661
b423cd4c 7662static void
7663hold_job(cupsd_client_t *con, /* I - Client connection */
7664 ipp_attribute_t *uri) /* I - Job or Printer URI */
ef416fc2 7665{
b9faaae1
MS
7666 ipp_attribute_t *attr; /* Current job-hold-until */
7667 const char *when; /* New value */
b423cd4c 7668 int jobid; /* Job ID */
61cf44e2 7669 char scheme[HTTP_MAX_URI], /* Method portion of URI */
b423cd4c 7670 username[HTTP_MAX_URI], /* Username portion of URI */
7671 host[HTTP_MAX_URI], /* Host portion of URI */
7672 resource[HTTP_MAX_URI]; /* Resource portion of URI */
7673 int port; /* Port portion of URI */
7674 cupsd_job_t *job; /* Job information */
7675
ef416fc2 7676
b423cd4c 7677 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->http.fd,
7678 uri->values[0].string.text);
ef416fc2 7679
7680 /*
b423cd4c 7681 * See if we have a job URI or a printer URI...
ef416fc2 7682 */
7683
b423cd4c 7684 if (!strcmp(uri->name, "printer-uri"))
7685 {
7686 /*
7687 * Got a printer URI; see if we also have a job-id attribute...
7688 */
ef416fc2 7689
b423cd4c 7690 if ((attr = ippFindAttribute(con->request, "job-id",
7691 IPP_TAG_INTEGER)) == NULL)
7692 {
7693 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 7694 _("Got a printer-uri attribute but no job-id."));
b423cd4c 7695 return;
7696 }
ef416fc2 7697
b423cd4c 7698 jobid = attr->values[0].integer;
7699 }
ef416fc2 7700 else
ef416fc2 7701 {
b423cd4c 7702 /*
7703 * Got a job URI; parse it to get the job ID...
7704 */
ef416fc2 7705
61cf44e2
MS
7706 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7707 sizeof(scheme), username, sizeof(username), host,
b423cd4c 7708 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 7709
b423cd4c 7710 if (strncmp(resource, "/jobs/", 6))
7711 {
7712 /*
7713 * Not a valid URI!
7714 */
ef416fc2 7715
b423cd4c 7716 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 7717 _("Bad job-uri \"%s\"."),
b423cd4c 7718 uri->values[0].string.text);
7719 return;
7720 }
ef416fc2 7721
b423cd4c 7722 jobid = atoi(resource + 6);
7723 }
ef416fc2 7724
ef416fc2 7725 /*
b423cd4c 7726 * See if the job exists...
ef416fc2 7727 */
7728
b423cd4c 7729 if ((job = cupsdFindJob(jobid)) == NULL)
7730 {
7731 /*
7732 * Nope - return a "not found" error...
7733 */
7734
84315f46 7735 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
b423cd4c 7736 return;
7737 }
ef416fc2 7738
7739 /*
b423cd4c 7740 * See if the job is owned by the requesting user...
ef416fc2 7741 */
7742
b423cd4c 7743 if (!validate_user(job, con, job->username, username, sizeof(username)))
7744 {
b0f6947b
MS
7745 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
7746 cupsdFindDest(job->dest));
b423cd4c 7747 return;
7748 }
ef416fc2 7749
f99f3698
MS
7750 /*
7751 * See if the job is in a state that allows holding...
7752 */
7753
7754 if (job->state_value > IPP_JOB_STOPPED)
7755 {
7756 /*
7757 * Return a "not-possible" error...
7758 */
7759
7760 send_ipp_status(con, IPP_NOT_POSSIBLE,
7761 _("Job #%d is finished and cannot be altered."),
7762 job->id);
7763 return;
7764 }
7765
ef416fc2 7766 /*
b423cd4c 7767 * Hold the job and return...
ef416fc2 7768 */
7769
b9faaae1
MS
7770 if ((attr = ippFindAttribute(con->request, "job-hold-until",
7771 IPP_TAG_KEYWORD)) == NULL)
7772 attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
ef416fc2 7773
b423cd4c 7774 if (attr)
7775 {
b9faaae1 7776 when = attr->values[0].string.text;
d09495fa 7777
01ce6322 7778 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
b9faaae1 7779 "Job job-hold-until value changed by user.");
b423cd4c 7780 }
b9faaae1
MS
7781 else
7782 when = "indefinite";
ef416fc2 7783
b9faaae1
MS
7784 cupsdSetJobHoldUntil(job, when, 1);
7785 cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT, "Job held by \"%s\".",
7786 username);
b423cd4c 7787
7788 con->response->request.status.status_code = IPP_OK;
ef416fc2 7789}
7790
7791
61cf44e2
MS
7792/*
7793 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
7794 */
7795
7796static void
7797hold_new_jobs(cupsd_client_t *con, /* I - Connection */
7798 ipp_attribute_t *uri) /* I - Printer URI */
7799{
7800 http_status_t status; /* Policy status */
7801 cups_ptype_t dtype; /* Destination type (printer/class) */
7802 cupsd_printer_t *printer; /* Printer data */
7803
7804
7805 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_new_jobs(%p[%d], %s)", con,
7806 con->http.fd, uri->values[0].string.text);
7807
7808 /*
7809 * Is the destination valid?
7810 */
7811
7812 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7813 {
7814 /*
7815 * Bad URI...
7816 */
7817
7818 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 7819 _("The printer or class does not exist."));
61cf44e2
MS
7820 return;
7821 }
7822
7823 /*
7824 * Check policy...
7825 */
7826
7827 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
7828 {
7829 send_http_error(con, status, printer);
7830 return;
7831 }
7832
7833 /*
7834 * Hold pending/new jobs sent to the printer...
7835 */
7836
7837 printer->holding_new_jobs = 1;
7838
7839 cupsdSetPrinterReasons(printer, "+hold-new-jobs");
61cf44e2
MS
7840
7841 if (dtype & CUPS_PRINTER_CLASS)
7842 cupsdLogMessage(CUPSD_LOG_INFO,
7843 "Class \"%s\" now holding pending/new jobs (\"%s\").",
7844 printer->name, get_username(con));
7845 else
7846 cupsdLogMessage(CUPSD_LOG_INFO,
7847 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
7848 printer->name, get_username(con));
7849
7850 /*
7851 * Everything was ok, so return OK status...
7852 */
7853
7854 con->response->request.status.status_code = IPP_OK;
7855}
7856
7857
ef416fc2 7858/*
b423cd4c 7859 * 'move_job()' - Move a job to a new destination.
ef416fc2 7860 */
7861
7862static void
b423cd4c 7863move_job(cupsd_client_t *con, /* I - Client connection */
7864 ipp_attribute_t *uri) /* I - Job URI */
ef416fc2 7865{
7866 http_status_t status; /* Policy status */
7867 ipp_attribute_t *attr; /* Current attribute */
b423cd4c 7868 int jobid; /* Job ID */
ef416fc2 7869 cupsd_job_t *job; /* Current job */
d09495fa 7870 const char *src; /* Source printer/class */
b423cd4c 7871 cups_ptype_t stype, /* Source type (printer or class) */
bc44d920 7872 dtype; /* Destination type (printer/class) */
f7deaa1a 7873 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
ef416fc2 7874 username[HTTP_MAX_URI], /* Username portion of URI */
7875 host[HTTP_MAX_URI], /* Host portion of URI */
b423cd4c 7876 resource[HTTP_MAX_URI]; /* Resource portion of URI */
ef416fc2 7877 int port; /* Port portion of URI */
b423cd4c 7878 cupsd_printer_t *sprinter, /* Source printer */
7879 *dprinter; /* Destination printer */
ef416fc2 7880
7881
b423cd4c 7882 cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd,
ef416fc2 7883 uri->values[0].string.text);
7884
7885 /*
b423cd4c 7886 * Get the new printer or class...
ef416fc2 7887 */
7888
b423cd4c 7889 if ((attr = ippFindAttribute(con->request, "job-printer-uri",
7890 IPP_TAG_URI)) == NULL)
ef416fc2 7891 {
b423cd4c 7892 /*
7893 * Need job-printer-uri...
7894 */
ef416fc2 7895
b423cd4c 7896 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 7897 _("job-printer-uri attribute missing."));
b423cd4c 7898 return;
ef416fc2 7899 }
f7faf1f5 7900
f7deaa1a 7901 if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter))
ef416fc2 7902 {
b423cd4c 7903 /*
7904 * Bad URI...
7905 */
ef416fc2 7906
b423cd4c 7907 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 7908 _("The printer or class does not exist."));
b423cd4c 7909 return;
ef416fc2 7910 }
7911
ef416fc2 7912 /*
b423cd4c 7913 * See if we have a job URI or a printer URI...
ef416fc2 7914 */
7915
f7deaa1a 7916 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7917 sizeof(scheme), username, sizeof(username), host,
b423cd4c 7918 sizeof(host), &port, resource, sizeof(resource));
7919
7920 if (!strcmp(uri->name, "printer-uri"))
ef416fc2 7921 {
7922 /*
b423cd4c 7923 * Got a printer URI; see if we also have a job-id attribute...
ef416fc2 7924 */
7925
b423cd4c 7926 if ((attr = ippFindAttribute(con->request, "job-id",
7927 IPP_TAG_INTEGER)) == NULL)
ef416fc2 7928 {
b423cd4c 7929 /*
7930 * Move all jobs...
7931 */
7932
f7deaa1a 7933 if ((src = cupsdValidateDest(uri->values[0].string.text, &stype,
7934 &sprinter)) == NULL)
b423cd4c 7935 {
7936 /*
7937 * Bad URI...
7938 */
7939
7940 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 7941 _("The printer or class does not exist."));
b423cd4c 7942 return;
7943 }
7944
7945 job = NULL;
7946 }
7947 else
7948 {
7949 /*
7950 * Otherwise, just move a single job...
7951 */
7952
7953 if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
7954 {
7955 /*
7956 * Nope - return a "not found" error...
7957 */
7958
7959 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 7960 _("Job #%d does not exist."), attr->values[0].integer);
b423cd4c 7961 return;
7962 }
7963 else
7964 {
7965 /*
7966 * Job found, initialize source pointers...
7967 */
7968
7969 src = NULL;
7970 sprinter = NULL;
7971 }
ef416fc2 7972 }
7973 }
7974 else
7975 {
7976 /*
b423cd4c 7977 * Got a job URI; parse it to get the job ID...
ef416fc2 7978 */
7979
b423cd4c 7980 if (strncmp(resource, "/jobs/", 6))
7981 {
7982 /*
7983 * Not a valid URI!
7984 */
7985
84315f46 7986 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
b423cd4c 7987 uri->values[0].string.text);
7988 return;
7989 }
ef416fc2 7990
ef416fc2 7991 /*
b423cd4c 7992 * See if the job exists...
ef416fc2 7993 */
7994
b423cd4c 7995 jobid = atoi(resource + 6);
ef416fc2 7996
b423cd4c 7997 if ((job = cupsdFindJob(jobid)) == NULL)
ef416fc2 7998 {
7999 /*
b423cd4c 8000 * Nope - return a "not found" error...
ef416fc2 8001 */
8002
84315f46 8003 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
b423cd4c 8004 return;
ef416fc2 8005 }
8006 else
b423cd4c 8007 {
8008 /*
8009 * Job found, initialize source pointers...
8010 */
ef416fc2 8011
b423cd4c 8012 src = NULL;
8013 sprinter = NULL;
8014 }
ef416fc2 8015 }
8016
ac884b6a
MS
8017 /*
8018 * Check the policy of the destination printer...
8019 */
8020
8021 if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con,
8022 job ? job->username : NULL)) != HTTP_OK)
8023 {
8024 send_http_error(con, status, dprinter);
8025 return;
8026 }
8027
ef416fc2 8028 /*
b423cd4c 8029 * Now move the job or jobs...
ef416fc2 8030 */
8031
b423cd4c 8032 if (job)
8033 {
8034 /*
8035 * See if the job has been completed...
8036 */
8037
8038 if (job->state_value > IPP_JOB_STOPPED)
8039 {
8040 /*
8041 * Return a "not-possible" error...
8042 */
8043
8044 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 8045 _("Job #%d is finished and cannot be altered."),
b423cd4c 8046 job->id);
8047 return;
8048 }
ef416fc2 8049
b423cd4c 8050 /*
8051 * See if the job is owned by the requesting user...
8052 */
ef416fc2 8053
b423cd4c 8054 if (!validate_user(job, con, job->username, username, sizeof(username)))
8055 {
b0f6947b
MS
8056 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
8057 cupsdFindDest(job->dest));
b423cd4c 8058 return;
8059 }
ef416fc2 8060
ef416fc2 8061 /*
b423cd4c 8062 * Move the job to a different printer or class...
ef416fc2 8063 */
8064
e53920b9 8065 cupsdMoveJob(job, dprinter);
ef416fc2 8066 }
b423cd4c 8067 else
8068 {
8069 /*
8070 * Got the source printer, now look through the jobs...
8071 */
ef416fc2 8072
b423cd4c 8073 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
8074 job;
8075 job = (cupsd_job_t *)cupsArrayNext(Jobs))
8076 {
8077 /*
8078 * See if the job is pointing at the source printer or has not been
8079 * completed...
8080 */
ef416fc2 8081
88f9aafc 8082 if (_cups_strcasecmp(job->dest, src) ||
b423cd4c 8083 job->state_value > IPP_JOB_STOPPED)
8084 continue;
ef416fc2 8085
b423cd4c 8086 /*
8087 * See if the job can be moved by the requesting user...
8088 */
ef416fc2 8089
b423cd4c 8090 if (!validate_user(job, con, job->username, username, sizeof(username)))
8091 continue;
ef416fc2 8092
b423cd4c 8093 /*
8094 * Move the job to a different printer or class...
8095 */
ef416fc2 8096
e53920b9 8097 cupsdMoveJob(job, dprinter);
b423cd4c 8098 }
ef416fc2 8099 }
8100
8101 /*
b423cd4c 8102 * Start jobs if possible...
ef416fc2 8103 */
8104
b423cd4c 8105 cupsdCheckJobs();
ef416fc2 8106
8107 /*
b423cd4c 8108 * Return with "everything is OK" status...
ef416fc2 8109 */
8110
b423cd4c 8111 con->response->request.status.status_code = IPP_OK;
8112}
ef416fc2 8113
ef416fc2 8114
b423cd4c 8115/*
8116 * 'ppd_parse_line()' - Parse a PPD default line.
8117 */
ef416fc2 8118
b423cd4c 8119static int /* O - 0 on success, -1 on failure */
8120ppd_parse_line(const char *line, /* I - Line */
8121 char *option, /* O - Option name */
8122 int olen, /* I - Size of option name */
8123 char *choice, /* O - Choice name */
8124 int clen) /* I - Size of choice name */
8125{
8126 /*
8127 * Verify this is a default option line...
8128 */
ef416fc2 8129
b423cd4c 8130 if (strncmp(line, "*Default", 8))
8131 return (-1);
ef416fc2 8132
b423cd4c 8133 /*
8134 * Read the option name...
8135 */
8136
18ecb428
MS
8137 for (line += 8, olen --;
8138 *line > ' ' && *line < 0x7f && *line != ':' && *line != '/';
8139 line ++)
b423cd4c 8140 if (olen > 0)
8141 {
8142 *option++ = *line;
8143 olen --;
ef416fc2 8144 }
8145
b423cd4c 8146 *option = '\0';
ef416fc2 8147
b423cd4c 8148 /*
8149 * Skip everything else up to the colon (:)...
8150 */
ef416fc2 8151
b423cd4c 8152 while (*line && *line != ':')
8153 line ++;
ef416fc2 8154
b423cd4c 8155 if (!*line)
8156 return (-1);
ef416fc2 8157
b423cd4c 8158 line ++;
ef416fc2 8159
b423cd4c 8160 /*
8161 * Now grab the option choice, skipping leading whitespace...
8162 */
ef416fc2 8163
b423cd4c 8164 while (isspace(*line & 255))
8165 line ++;
ef416fc2 8166
18ecb428
MS
8167 for (clen --;
8168 *line > ' ' && *line < 0x7f && *line != ':' && *line != '/';
8169 line ++)
b423cd4c 8170 if (clen > 0)
8171 {
8172 *choice++ = *line;
8173 clen --;
8174 }
ef416fc2 8175
b423cd4c 8176 *choice = '\0';
ef416fc2 8177
b423cd4c 8178 /*
8179 * Return with no errors...
8180 */
ef416fc2 8181
b423cd4c 8182 return (0);
8183}
ef416fc2 8184
ef416fc2 8185
b423cd4c 8186/*
8187 * 'print_job()' - Print a file to a printer or class.
8188 */
ef416fc2 8189
b423cd4c 8190static void
8191print_job(cupsd_client_t *con, /* I - Client connection */
8192 ipp_attribute_t *uri) /* I - Printer URI */
8193{
8194 ipp_attribute_t *attr; /* Current attribute */
8195 ipp_attribute_t *format; /* Document-format attribute */
f7deaa1a 8196 const char *default_format; /* document-format-default value */
b423cd4c 8197 cupsd_job_t *job; /* New job */
8198 char filename[1024]; /* Job filename */
8199 mime_type_t *filetype; /* Type of file */
8200 char super[MIME_MAX_SUPER], /* Supertype of file */
8201 type[MIME_MAX_TYPE], /* Subtype of file */
8202 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
8203 /* Textual name of mime type */
8204 cupsd_printer_t *printer; /* Printer data */
8205 struct stat fileinfo; /* File information */
8206 int kbytes; /* Size of file */
8207 int compression; /* Document compression */
ef416fc2 8208
ef416fc2 8209
b423cd4c 8210 cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd,
8211 uri->values[0].string.text);
ef416fc2 8212
b423cd4c 8213 /*
8214 * Validate print file attributes, for now just document-format and
8215 * compression (CUPS only supports "none" and "gzip")...
8216 */
ef416fc2 8217
b423cd4c 8218 compression = CUPS_FILE_NONE;
ef416fc2 8219
b423cd4c 8220 if ((attr = ippFindAttribute(con->request, "compression",
8221 IPP_TAG_KEYWORD)) != NULL)
8222 {
8223 if (strcmp(attr->values[0].string.text, "none")
8224#ifdef HAVE_LIBZ
8225 && strcmp(attr->values[0].string.text, "gzip")
8226#endif /* HAVE_LIBZ */
8227 )
8228 {
8229 send_ipp_status(con, IPP_ATTRIBUTES,
84315f46 8230 _("Unsupported compression \"%s\"."),
b423cd4c 8231 attr->values[0].string.text);
8232 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
8233 "compression", NULL, attr->values[0].string.text);
8234 return;
8235 }
ef416fc2 8236
b423cd4c 8237#ifdef HAVE_LIBZ
8238 if (!strcmp(attr->values[0].string.text, "gzip"))
8239 compression = CUPS_FILE_GZIP;
8240#endif /* HAVE_LIBZ */
8241 }
ef416fc2 8242
b423cd4c 8243 /*
8244 * Do we have a file to print?
8245 */
ef416fc2 8246
b423cd4c 8247 if (!con->filename)
8248 {
84315f46 8249 send_ipp_status(con, IPP_BAD_REQUEST, _("No file in print request."));
b423cd4c 8250 return;
8251 }
ef416fc2 8252
f7deaa1a 8253 /*
8254 * Is the destination valid?
8255 */
8256
8257 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
8258 {
8259 /*
8260 * Bad URI...
8261 */
8262
8263 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 8264 _("The printer or class does not exist."));
f7deaa1a 8265 return;
8266 }
8267
b423cd4c 8268 /*
8269 * Is it a format we support?
8270 */
ef416fc2 8271
b423cd4c 8272 if ((format = ippFindAttribute(con->request, "document-format",
8273 IPP_TAG_MIMETYPE)) != NULL)
8274 {
8275 /*
8276 * Grab format from client...
8277 */
8278
f7deaa1a 8279 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super,
8280 type) != 2)
b423cd4c 8281 {
8282 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 8283 _("Bad document-format \"%s\"."),
b423cd4c 8284 format->values[0].string.text);
8285 return;
ef416fc2 8286 }
b423cd4c 8287 }
f7deaa1a 8288 else if ((default_format = cupsGetOption("document-format",
8289 printer->num_options,
8290 printer->options)) != NULL)
8291 {
8292 /*
8293 * Use default document format...
8294 */
8295
8296 if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
8297 {
8298 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 8299 _("Bad document-format \"%s\"."),
f7deaa1a 8300 default_format);
8301 return;
8302 }
8303 }
b423cd4c 8304 else
8305 {
8306 /*
f7deaa1a 8307 * Auto-type it!
b423cd4c 8308 */
8309
5a9febac
MS
8310 strlcpy(super, "application", sizeof(super));
8311 strlcpy(type, "octet-stream", sizeof(type));
b423cd4c 8312 }
ef416fc2 8313
b423cd4c 8314 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
8315 {
ef416fc2 8316 /*
b423cd4c 8317 * Auto-type the file...
ef416fc2 8318 */
8319
b423cd4c 8320 ipp_attribute_t *doc_name; /* document-name attribute */
8321
8322
a0f6818e 8323 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file...");
b423cd4c 8324
8325 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
8326 filetype = mimeFileType(MimeDatabase, con->filename,
8327 doc_name ? doc_name->values[0].string.text : NULL,
8328 &compression);
8329
f7deaa1a 8330 if (!filetype)
8331 filetype = mimeType(MimeDatabase, super, type);
a0f6818e
MS
8332
8333 cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.",
8334 filetype->super, filetype->type);
f7deaa1a 8335 }
8336 else
8337 filetype = mimeType(MimeDatabase, super, type);
ef416fc2 8338
f7deaa1a 8339 if (filetype &&
8340 (!format ||
8341 (!strcmp(super, "application") && !strcmp(type, "octet-stream"))))
8342 {
8343 /*
8344 * Replace the document-format attribute value with the auto-typed or
8345 * default one.
8346 */
b423cd4c 8347
f7deaa1a 8348 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
8349 filetype->type);
ed486911 8350
f7deaa1a 8351 if (format)
8352 {
8353 _cupsStrFree(format->values[0].string.text);
8354
8355 format->values[0].string.text = _cupsStrAlloc(mimetype);
ef416fc2 8356 }
b423cd4c 8357 else
f7deaa1a 8358 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
8359 "document-format", NULL, mimetype);
ef416fc2 8360 }
f7deaa1a 8361 else if (!filetype)
b423cd4c 8362 {
8363 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
84315f46 8364 _("Unsupported document-format \"%s\"."),
c7017ecc
MS
8365 format ? format->values[0].string.text :
8366 "application/octet-stream");
b423cd4c 8367 cupsdLogMessage(CUPSD_LOG_INFO,
8368 "Hint: Do you have the raw file printing rules enabled?");
8369
8370 if (format)
8371 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
8372 "document-format", NULL, format->values[0].string.text);
8373
8374 return;
8375 }
8376
b423cd4c 8377 /*
8378 * Read any embedded job ticket info from PS files...
8379 */
8380
88f9aafc
MS
8381 if (!_cups_strcasecmp(filetype->super, "application") &&
8382 (!_cups_strcasecmp(filetype->type, "postscript") ||
8383 !_cups_strcasecmp(filetype->type, "pdf")))
c5571a1d 8384 read_job_ticket(con);
b423cd4c 8385
8386 /*
8387 * Create the job object...
8388 */
8389
f7deaa1a 8390 if ((job = add_job(con, printer, filetype)) == NULL)
b423cd4c 8391 return;
8392
8393 /*
8394 * Update quota data...
8395 */
8396
8397 if (stat(con->filename, &fileinfo))
8398 kbytes = 0;
8399 else
8400 kbytes = (fileinfo.st_size + 1023) / 1024;
8401
8402 cupsdUpdateQuota(printer, job->username, 0, kbytes);
8403
8404 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
8405 IPP_TAG_INTEGER)) != NULL)
8406 attr->values[0].integer += kbytes;
8407
ef416fc2 8408 /*
8409 * Add the job file...
8410 */
8411
8412 if (add_file(con, job, filetype, compression))
8413 return;
8414
8415 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
8416 job->num_files);
8417 rename(con->filename, filename);
8418 cupsdClearString(&con->filename);
8419
8420 /*
8421 * See if we need to add the ending sheet...
8422 */
8423
91c84a35
MS
8424 if (cupsdTimeoutJob(job))
8425 return;
ef416fc2 8426
ef416fc2 8427 /*
8428 * Log and save the job...
8429 */
8430
75bd9771
MS
8431 cupsdLogJob(job, CUPSD_LOG_INFO,
8432 "File of type %s/%s queued by \"%s\".",
8433 filetype->super, filetype->type, job->username);
8434 cupsdLogJob(job, CUPSD_LOG_DEBUG, "hold_until=%d", (int)job->hold_until);
b19ccc9e
MS
8435 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
8436 job->dest, job->username);
ef416fc2 8437
ef416fc2 8438 /*
8439 * Start the job if possible...
8440 */
8441
8442 cupsdCheckJobs();
8443}
8444
8445
8446/*
c5571a1d 8447 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
ef416fc2 8448 *
c5571a1d 8449 * This function only gets called when printing a single PDF or PostScript
ef416fc2 8450 * file using the Print-Job operation. It doesn't work for Create-Job +
8451 * Send-File, since the job attributes need to be set at job creation
c5571a1d
MS
8452 * time for banners to work. The embedded job ticket stuff is here
8453 * primarily to allow the Windows printer driver for CUPS to pass in JCL
ef416fc2 8454 * options and IPP attributes which otherwise would be lost.
8455 *
c5571a1d 8456 * The format of a job ticket is simple:
ef416fc2 8457 *
8458 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8459 *
8460 * %cupsJobTicket: attr1=value1
8461 * %cupsJobTicket: attr2=value2
8462 * ...
8463 * %cupsJobTicket: attrN=valueN
8464 *
8465 * Job ticket lines must appear immediately after the first line that
c5571a1d
MS
8466 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8467 * stops looking for job ticket info when it finds a line that does not begin
ef416fc2 8468 * with "%cupsJobTicket:".
8469 *
8470 * The maximum length of a job ticket line, including the prefix, is
8471 * 255 characters to conform with the Adobe DSC.
8472 *
8473 * Read-only attributes are rejected with a notice to the error log in
8474 * case a malicious user tries anything. Since the job ticket is read
8475 * prior to attribute validation in print_job(), job ticket attributes
8476 * will go through the same validation as IPP attributes...
8477 */
8478
8479static void
c5571a1d 8480read_job_ticket(cupsd_client_t *con) /* I - Client connection */
ef416fc2 8481{
8482 cups_file_t *fp; /* File to read from */
8483 char line[256]; /* Line data */
8484 int num_options; /* Number of options */
8485 cups_option_t *options; /* Options */
8486 ipp_t *ticket; /* New attributes */
8487 ipp_attribute_t *attr, /* Current attribute */
8488 *attr2, /* Job attribute */
8489 *prev2; /* Previous job attribute */
8490
8491
8492 /*
8493 * First open the print file...
8494 */
8495
8496 if ((fp = cupsFileOpen(con->filename, "rb")) == NULL)
8497 {
8498 cupsdLogMessage(CUPSD_LOG_ERROR,
c5571a1d 8499 "Unable to open print file for job ticket - %s",
ef416fc2 8500 strerror(errno));
8501 return;
8502 }
8503
8504 /*
8505 * Skip the first line...
8506 */
8507
8508 if (cupsFileGets(fp, line, sizeof(line)) == NULL)
8509 {
8510 cupsdLogMessage(CUPSD_LOG_ERROR,
c5571a1d 8511 "Unable to read from print file for job ticket - %s",
ef416fc2 8512 strerror(errno));
8513 cupsFileClose(fp);
8514 return;
8515 }
8516
c5571a1d 8517 if (strncmp(line, "%!PS-Adobe-", 11) && strncmp(line, "%PDF-", 5))
ef416fc2 8518 {
8519 /*
8520 * Not a DSC-compliant file, so no job ticket info will be available...
8521 */
8522
8523 cupsFileClose(fp);
8524 return;
8525 }
8526
8527 /*
8528 * Read job ticket info from the file...
8529 */
8530
8531 num_options = 0;
8532 options = NULL;
8533
fa73b229 8534 while (cupsFileGets(fp, line, sizeof(line)))
ef416fc2 8535 {
8536 /*
8537 * Stop at the first non-ticket line...
8538 */
8539
fa73b229 8540 if (strncmp(line, "%cupsJobTicket:", 15))
ef416fc2 8541 break;
8542
8543 /*
8544 * Add the options to the option array...
8545 */
8546
8547 num_options = cupsParseOptions(line + 15, num_options, &options);
8548 }
8549
8550 /*
8551 * Done with the file; see if we have any options...
8552 */
8553
8554 cupsFileClose(fp);
8555
8556 if (num_options == 0)
8557 return;
8558
8559 /*
8560 * OK, convert the options to an attribute list, and apply them to
8561 * the request...
8562 */
8563
8564 ticket = ippNew();
8565 cupsEncodeOptions(ticket, num_options, options);
8566
8567 /*
8568 * See what the user wants to change.
8569 */
8570
fa73b229 8571 for (attr = ticket->attrs; attr; attr = attr->next)
ef416fc2 8572 {
8573 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
8574 continue;
8575
8576 if (!strcmp(attr->name, "job-originating-host-name") ||
8577 !strcmp(attr->name, "job-originating-user-name") ||
8578 !strcmp(attr->name, "job-media-sheets-completed") ||
8579 !strcmp(attr->name, "job-k-octets") ||
8580 !strcmp(attr->name, "job-id") ||
8581 !strncmp(attr->name, "job-state", 9) ||
8582 !strncmp(attr->name, "time-at-", 8))
8583 continue; /* Read-only attrs */
8584
fa73b229 8585 if ((attr2 = ippFindAttribute(con->request, attr->name,
8586 IPP_TAG_ZERO)) != NULL)
ef416fc2 8587 {
8588 /*
8589 * Some other value; first free the old value...
8590 */
8591
8592 if (con->request->attrs == attr2)
8593 {
8594 con->request->attrs = attr2->next;
8595 prev2 = NULL;
8596 }
8597 else
8598 {
fa73b229 8599 for (prev2 = con->request->attrs; prev2; prev2 = prev2->next)
ef416fc2 8600 if (prev2->next == attr2)
8601 {
8602 prev2->next = attr2->next;
8603 break;
8604 }
8605 }
8606
8607 if (con->request->last == attr2)
8608 con->request->last = prev2;
8609
a2326b5b 8610 ippDeleteAttribute(NULL, attr2);
ef416fc2 8611 }
8612
8613 /*
8614 * Add new option by copying it...
8615 */
8616
a2326b5b 8617 ippCopyAttribute(con->request, attr, 0);
ef416fc2 8618 }
8619
8620 /*
8621 * Then free the attribute list and option array...
8622 */
8623
8624 ippDelete(ticket);
8625 cupsFreeOptions(num_options, options);
8626}
8627
8628
8629/*
8630 * 'reject_jobs()' - Reject print jobs to a printer.
8631 */
8632
8633static void
8634reject_jobs(cupsd_client_t *con, /* I - Client connection */
8635 ipp_attribute_t *uri) /* I - Printer or class URI */
8636{
8637 http_status_t status; /* Policy status */
bc44d920 8638 cups_ptype_t dtype; /* Destination type (printer/class) */
ef416fc2 8639 cupsd_printer_t *printer; /* Printer data */
8640 ipp_attribute_t *attr; /* printer-state-message text */
8641
8642
8643 cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con,
8644 con->http.fd, uri->values[0].string.text);
8645
8646 /*
8647 * Is the destination valid?
8648 */
8649
f7deaa1a 8650 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 8651 {
8652 /*
8653 * Bad URI...
8654 */
8655
8656 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 8657 _("The printer or class does not exist."));
ef416fc2 8658 return;
8659 }
8660
8661 /*
8662 * Check policy...
8663 */
8664
8665 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8666 {
f899b121 8667 send_http_error(con, status, printer);
ef416fc2 8668 return;
8669 }
8670
8671 /*
8672 * Reject jobs sent to the printer...
8673 */
8674
8675 printer->accepting = 0;
8676
8677 if ((attr = ippFindAttribute(con->request, "printer-state-message",
8678 IPP_TAG_TEXT)) == NULL)
5a9febac
MS
8679 strlcpy(printer->state_message, "Rejecting Jobs",
8680 sizeof(printer->state_message));
ef416fc2 8681 else
8682 strlcpy(printer->state_message, attr->values[0].string.text,
8683 sizeof(printer->state_message));
8684
f8b3a85b
MS
8685 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
8686 "No longer accepting jobs.");
8687
ef416fc2 8688 if (dtype & CUPS_PRINTER_CLASS)
8689 {
3dfe78b3 8690 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
ef416fc2 8691
8692 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").",
f7deaa1a 8693 printer->name, get_username(con));
ef416fc2 8694 }
8695 else
8696 {
3dfe78b3 8697 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
ef416fc2 8698
8699 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").",
f7deaa1a 8700 printer->name, get_username(con));
ef416fc2 8701 }
8702
8703 /*
8704 * Everything was ok, so return OK status...
8705 */
8706
8707 con->response->request.status.status_code = IPP_OK;
8708}
8709
8710
61cf44e2
MS
8711/*
8712 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8713 */
8714
8715static void
8716release_held_new_jobs(
8717 cupsd_client_t *con, /* I - Connection */
8718 ipp_attribute_t *uri) /* I - Printer URI */
8719{
8720 http_status_t status; /* Policy status */
8721 cups_ptype_t dtype; /* Destination type (printer/class) */
8722 cupsd_printer_t *printer; /* Printer data */
8723
8724
8725 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_held_new_jobs(%p[%d], %s)", con,
8726 con->http.fd, uri->values[0].string.text);
8727
8728 /*
8729 * Is the destination valid?
8730 */
8731
8732 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8733 {
8734 /*
8735 * Bad URI...
8736 */
8737
8738 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 8739 _("The printer or class does not exist."));
61cf44e2
MS
8740 return;
8741 }
8742
8743 /*
8744 * Check policy...
8745 */
8746
8747 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8748 {
8749 send_http_error(con, status, printer);
8750 return;
8751 }
8752
8753 /*
8754 * Hold pending/new jobs sent to the printer...
8755 */
8756
8757 printer->holding_new_jobs = 0;
8758
8759 cupsdSetPrinterReasons(printer, "-hold-new-jobs");
61cf44e2
MS
8760
8761 if (dtype & CUPS_PRINTER_CLASS)
8762 cupsdLogMessage(CUPSD_LOG_INFO,
8763 "Class \"%s\" now printing pending/new jobs (\"%s\").",
8764 printer->name, get_username(con));
8765 else
8766 cupsdLogMessage(CUPSD_LOG_INFO,
8767 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
8768 printer->name, get_username(con));
8769
8770 /*
8771 * Everything was ok, so return OK status...
8772 */
8773
8774 con->response->request.status.status_code = IPP_OK;
8775}
8776
8777
ef416fc2 8778/*
8779 * 'release_job()' - Release a held print job.
8780 */
8781
8782static void
8783release_job(cupsd_client_t *con, /* I - Client connection */
8784 ipp_attribute_t *uri) /* I - Job or Printer URI */
8785{
8786 ipp_attribute_t *attr; /* Current attribute */
8787 int jobid; /* Job ID */
61cf44e2 8788 char scheme[HTTP_MAX_URI], /* Method portion of URI */
ef416fc2 8789 username[HTTP_MAX_URI], /* Username portion of URI */
8790 host[HTTP_MAX_URI], /* Host portion of URI */
8791 resource[HTTP_MAX_URI]; /* Resource portion of URI */
8792 int port; /* Port portion of URI */
8793 cupsd_job_t *job; /* Job information */
8794
8795
8796 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con,
8797 con->http.fd, uri->values[0].string.text);
8798
8799 /*
8800 * See if we have a job URI or a printer URI...
8801 */
8802
8803 if (!strcmp(uri->name, "printer-uri"))
8804 {
8805 /*
8806 * Got a printer URI; see if we also have a job-id attribute...
8807 */
8808
fa73b229 8809 if ((attr = ippFindAttribute(con->request, "job-id",
8810 IPP_TAG_INTEGER)) == NULL)
ef416fc2 8811 {
8812 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 8813 _("Got a printer-uri attribute but no job-id."));
ef416fc2 8814 return;
8815 }
8816
8817 jobid = attr->values[0].integer;
8818 }
8819 else
8820 {
8821 /*
8822 * Got a job URI; parse it to get the job ID...
8823 */
8824
61cf44e2
MS
8825 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
8826 sizeof(scheme), username, sizeof(username), host,
a4d04587 8827 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 8828
8829 if (strncmp(resource, "/jobs/", 6))
8830 {
8831 /*
8832 * Not a valid URI!
8833 */
8834
84315f46 8835 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
ef416fc2 8836 uri->values[0].string.text);
8837 return;
8838 }
8839
8840 jobid = atoi(resource + 6);
8841 }
8842
8843 /*
8844 * See if the job exists...
8845 */
8846
8847 if ((job = cupsdFindJob(jobid)) == NULL)
8848 {
8849 /*
8850 * Nope - return a "not found" error...
8851 */
8852
84315f46 8853 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
ef416fc2 8854 return;
8855 }
8856
8857 /*
8858 * See if job is "held"...
8859 */
8860
bd7854cb 8861 if (job->state_value != IPP_JOB_HELD)
ef416fc2 8862 {
8863 /*
8864 * Nope - return a "not possible" error...
8865 */
8866
84315f46 8867 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held."), jobid);
ef416fc2 8868 return;
8869 }
8870
8871 /*
8872 * See if the job is owned by the requesting user...
8873 */
8874
8875 if (!validate_user(job, con, job->username, username, sizeof(username)))
8876 {
b0f6947b
MS
8877 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
8878 cupsdFindDest(job->dest));
ef416fc2 8879 return;
8880 }
8881
8882 /*
8883 * Reset the job-hold-until value to "no-hold"...
8884 */
8885
fa73b229 8886 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
8887 IPP_TAG_KEYWORD)) == NULL)
ef416fc2 8888 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
8889
fa73b229 8890 if (attr)
ef416fc2 8891 {
757d2cad 8892 _cupsStrFree(attr->values[0].string.text);
4400e98d 8893
ef416fc2 8894 attr->value_tag = IPP_TAG_KEYWORD;
757d2cad 8895 attr->values[0].string.text = _cupsStrAlloc("no-hold");
d09495fa 8896
01ce6322 8897 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
d09495fa 8898 "Job job-hold-until value changed by user.");
3e7fe0ca 8899 ippSetString(job->attrs, &job->reasons, 0, "none");
ef416fc2 8900 }
8901
8902 /*
8903 * Release the job and return...
8904 */
8905
8906 cupsdReleaseJob(job);
8907
01ce6322 8908 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
d09495fa 8909 "Job released by user.");
8910
75bd9771 8911 cupsdLogJob(job, CUPSD_LOG_INFO, "Released by \"%s\".", username);
ef416fc2 8912
8913 con->response->request.status.status_code = IPP_OK;
005dd1eb
MS
8914
8915 cupsdCheckJobs();
ef416fc2 8916}
8917
8918
8919/*
8920 * 'renew_subscription()' - Renew an existing subscription...
8921 */
8922
8923static void
8924renew_subscription(
8925 cupsd_client_t *con, /* I - Client connection */
8926 int sub_id) /* I - Subscription ID */
8927{
bd7854cb 8928 http_status_t status; /* Policy status */
8929 cupsd_subscription_t *sub; /* Subscription */
8930 ipp_attribute_t *lease; /* notify-lease-duration */
8931
8932
8933 cupsdLogMessage(CUPSD_LOG_DEBUG2,
8934 "renew_subscription(con=%p[%d], sub_id=%d)",
8935 con, con->http.fd, sub_id);
8936
8937 /*
8938 * Is the subscription ID valid?
8939 */
8940
8941 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
8942 {
8943 /*
8944 * Bad subscription ID...
8945 */
8946
84315f46
MS
8947 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
8948 sub_id);
bd7854cb 8949 return;
8950 }
8951
8952 if (sub->job)
8953 {
8954 /*
8955 * Job subscriptions cannot be renewed...
8956 */
8957
8958 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 8959 _("Job subscriptions cannot be renewed."));
bd7854cb 8960 return;
8961 }
8962
8963 /*
8964 * Check policy...
8965 */
8966
8967 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
8968 DefaultPolicyPtr,
8969 con, sub->owner)) != HTTP_OK)
8970 {
f899b121 8971 send_http_error(con, status, sub->dest);
bd7854cb 8972 return;
8973 }
8974
8975 /*
8976 * Renew the subscription...
8977 */
8978
8979 lease = ippFindAttribute(con->request, "notify-lease-duration",
8980 IPP_TAG_INTEGER);
8981
8982 sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration;
8983
8984 if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration))
8985 {
8986 cupsdLogMessage(CUPSD_LOG_INFO,
8987 "renew_subscription: Limiting notify-lease-duration to "
8988 "%d seconds.",
8989 MaxLeaseDuration);
8990 sub->lease = MaxLeaseDuration;
8991 }
8992
8993 sub->expire = sub->lease ? time(NULL) + sub->lease : 0;
8994
3dfe78b3 8995 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
bd7854cb 8996
8997 con->response->request.status.status_code = IPP_OK;
b94498cf 8998
8999 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
9000 "notify-lease-duration", sub->lease);
ef416fc2 9001}
9002
9003
9004/*
9005 * 'restart_job()' - Restart an old print job.
9006 */
9007
9008static void
9009restart_job(cupsd_client_t *con, /* I - Client connection */
9010 ipp_attribute_t *uri) /* I - Job or Printer URI */
9011{
9012 ipp_attribute_t *attr; /* Current attribute */
9013 int jobid; /* Job ID */
238c3832 9014 cupsd_job_t *job; /* Job information */
61cf44e2 9015 char scheme[HTTP_MAX_URI], /* Method portion of URI */
ef416fc2 9016 username[HTTP_MAX_URI], /* Username portion of URI */
9017 host[HTTP_MAX_URI], /* Host portion of URI */
9018 resource[HTTP_MAX_URI]; /* Resource portion of URI */
9019 int port; /* Port portion of URI */
ef416fc2 9020
9021
9022 cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con,
9023 con->http.fd, uri->values[0].string.text);
9024
9025 /*
9026 * See if we have a job URI or a printer URI...
9027 */
9028
9029 if (!strcmp(uri->name, "printer-uri"))
9030 {
9031 /*
9032 * Got a printer URI; see if we also have a job-id attribute...
9033 */
9034
fa73b229 9035 if ((attr = ippFindAttribute(con->request, "job-id",
9036 IPP_TAG_INTEGER)) == NULL)
ef416fc2 9037 {
9038 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 9039 _("Got a printer-uri attribute but no job-id."));
ef416fc2 9040 return;
9041 }
9042
9043 jobid = attr->values[0].integer;
9044 }
9045 else
9046 {
9047 /*
9048 * Got a job URI; parse it to get the job ID...
9049 */
9050
61cf44e2
MS
9051 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9052 sizeof(scheme), username, sizeof(username), host,
a4d04587 9053 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 9054
fa73b229 9055 if (strncmp(resource, "/jobs/", 6))
ef416fc2 9056 {
9057 /*
9058 * Not a valid URI!
9059 */
9060
84315f46 9061 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
ef416fc2 9062 uri->values[0].string.text);
9063 return;
9064 }
9065
9066 jobid = atoi(resource + 6);
9067 }
9068
9069 /*
9070 * See if the job exists...
9071 */
9072
9073 if ((job = cupsdFindJob(jobid)) == NULL)
9074 {
9075 /*
9076 * Nope - return a "not found" error...
9077 */
9078
84315f46 9079 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
ef416fc2 9080 return;
9081 }
9082
9083 /*
9084 * See if job is in any of the "completed" states...
9085 */
9086
bd7854cb 9087 if (job->state_value <= IPP_JOB_PROCESSING)
ef416fc2 9088 {
9089 /*
9090 * Nope - return a "not possible" error...
9091 */
9092
84315f46 9093 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete."),
ef416fc2 9094 jobid);
9095 return;
9096 }
9097
9098 /*
9099 * See if we have retained the job files...
9100 */
9101
bd7854cb 9102 cupsdLoadJob(job);
9103
07725fee 9104 if (!job->attrs || job->num_files == 0)
ef416fc2 9105 {
9106 /*
9107 * Nope - return a "not possible" error...
9108 */
9109
9110 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 9111 _("Job #%d cannot be restarted - no files."), jobid);
ef416fc2 9112 return;
9113 }
9114
9115 /*
9116 * See if the job is owned by the requesting user...
9117 */
9118
9119 if (!validate_user(job, con, job->username, username, sizeof(username)))
9120 {
b0f6947b
MS
9121 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9122 cupsdFindDest(job->dest));
ef416fc2 9123 return;
9124 }
9125
9126 /*
238c3832 9127 * See if the job-hold-until attribute is specified...
ef416fc2 9128 */
9129
238c3832
MS
9130 if ((attr = ippFindAttribute(con->request, "job-hold-until",
9131 IPP_TAG_KEYWORD)) == NULL)
9132 attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
9133
9134 if (attr && strcmp(attr->values[0].string.text, "no-hold"))
9135 {
9136 /*
9137 * Return the job to a held state...
9138 */
9139
9140 cupsdLogJob(job, CUPSD_LOG_DEBUG,
9141 "Restarted by \"%s\" with job-hold-until=%s.",
9142 username, attr->values[0].string.text);
9143 cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
9144
9145 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE,
9146 NULL, job, "Job restarted by user with job-hold-until=%s",
9147 attr->values[0].string.text);
9148 }
9149 else
9150 {
9151 /*
9152 * Restart the job...
9153 */
9154
9155 cupsdRestartJob(job);
f11a948a 9156 cupsdCheckJobs();
238c3832 9157 }
ef416fc2 9158
75bd9771 9159 cupsdLogJob(job, CUPSD_LOG_INFO, "Restarted by \"%s\".", username);
ef416fc2 9160
9161 con->response->request.status.status_code = IPP_OK;
9162}
9163
9164
9165/*
9166 * 'save_auth_info()' - Save authentication information for a job.
9167 */
9168
9169static void
f7deaa1a 9170save_auth_info(
9171 cupsd_client_t *con, /* I - Client connection */
9172 cupsd_job_t *job, /* I - Job */
9173 ipp_attribute_t *auth_info) /* I - auth-info attribute, if any */
ef416fc2 9174{
09a101d6 9175 int i; /* Looping var */
9176 char filename[1024]; /* Job authentication filename */
9177 cups_file_t *fp; /* Job authentication file */
85dda01c 9178 char line[65536]; /* Line for file */
09a101d6 9179 cupsd_printer_t *dest; /* Destination printer/class */
ef416fc2 9180
9181
9182 /*
9183 * This function saves the in-memory authentication information for
9184 * a job so that it can be used to authenticate with a remote host.
9185 * The information is stored in a file that is readable only by the
f7deaa1a 9186 * root user. The fields are Base-64 encoded, each on a separate line,
9187 * followed by random number (up to 1024) of newlines to limit the
9188 * amount of information that is exposed.
ef416fc2 9189 *
9190 * Because of the potential for exposing of authentication information,
9191 * this functionality is only enabled when running cupsd as root.
9192 *
9193 * This caching only works for the Basic and BasicDigest authentication
9194 * types. Digest authentication cannot be cached this way, and in
9195 * the future Kerberos authentication may make all of this obsolete.
9196 *
9197 * Authentication information is saved whenever an authenticated
9198 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9199 * performed.
9200 *
9201 * This information is deleted after a job is completed or canceled,
9202 * so reprints may require subsequent re-authentication.
9203 */
9204
9205 if (RunUser)
9206 return;
9207
09a101d6 9208 if ((dest = cupsdFindDest(job->dest)) == NULL)
9209 return;
9210
ef416fc2 9211 /*
9212 * Create the authentication file and change permissions...
9213 */
9214
9215 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
9216 if ((fp = cupsFileOpen(filename, "w")) == NULL)
9217 {
9218 cupsdLogMessage(CUPSD_LOG_ERROR,
9219 "Unable to save authentication info to \"%s\" - %s",
9220 filename, strerror(errno));
9221 return;
9222 }
9223
9224 fchown(cupsFileNumber(fp), 0, 0);
9225 fchmod(cupsFileNumber(fp), 0400);
9226
dcb445bc
MS
9227 cupsFilePuts(fp, "CUPSD-AUTH-V2\n");
9228
88f9aafc
MS
9229 for (i = 0;
9230 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
9231 i ++)
9232 cupsdClearString(job->auth_env + i);
9233
09a101d6 9234 if (auth_info && auth_info->num_values == dest->num_auth_info_required)
f7deaa1a 9235 {
9236 /*
09a101d6 9237 * Write 1 to 3 auth values...
f7deaa1a 9238 */
ef416fc2 9239
88f9aafc
MS
9240 for (i = 0;
9241 i < auth_info->num_values &&
9242 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
9243 i ++)
f7deaa1a 9244 {
9245 httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text,
9246 strlen(auth_info->values[i].string.text));
dcb445bc 9247 cupsFilePutConf(fp, dest->auth_info_required[i], line);
09a101d6 9248
9249 if (!strcmp(dest->auth_info_required[i], "username"))
88f9aafc 9250 cupsdSetStringf(job->auth_env + i, "AUTH_USERNAME=%s",
09a101d6 9251 auth_info->values[i].string.text);
9252 else if (!strcmp(dest->auth_info_required[i], "domain"))
88f9aafc 9253 cupsdSetStringf(job->auth_env + i, "AUTH_DOMAIN=%s",
09a101d6 9254 auth_info->values[i].string.text);
9255 else if (!strcmp(dest->auth_info_required[i], "password"))
88f9aafc
MS
9256 cupsdSetStringf(job->auth_env + i, "AUTH_PASSWORD=%s",
9257 auth_info->values[i].string.text);
9258 else if (!strcmp(dest->auth_info_required[i], "negotiate"))
9259 cupsdSetStringf(job->auth_env + i, "AUTH_NEGOTIATE=%s",
9260 auth_info->values[i].string.text);
9261 else
dcb445bc 9262 i --;
f7deaa1a 9263 }
9264 }
dcb445bc
MS
9265 else if (auth_info && auth_info->num_values == 2 &&
9266 dest->num_auth_info_required == 1 &&
9267 !strcmp(dest->auth_info_required[0], "negotiate"))
9268 {
9269 /*
9270 * Allow fallback to username+password for Kerberized queues...
9271 */
9272
9273 httpEncode64_2(line, sizeof(line), auth_info->values[0].string.text,
9274 strlen(auth_info->values[0].string.text));
9275 cupsFilePutConf(fp, "username", line);
9276
9277 cupsdSetStringf(job->auth_env + 0, "AUTH_USERNAME=%s",
9278 auth_info->values[0].string.text);
9279
9280 httpEncode64_2(line, sizeof(line), auth_info->values[1].string.text,
9281 strlen(auth_info->values[1].string.text));
9282 cupsFilePutConf(fp, "password", line);
9283
9284 cupsdSetStringf(job->auth_env + 1, "AUTH_PASSWORD=%s",
9285 auth_info->values[1].string.text);
9286 }
09a101d6 9287 else if (con->username[0])
f7deaa1a 9288 {
9289 /*
9290 * Write the authenticated username...
9291 */
ef416fc2 9292
f7deaa1a 9293 httpEncode64_2(line, sizeof(line), con->username, strlen(con->username));
dcb445bc 9294 cupsFilePutConf(fp, "username", line);
ef416fc2 9295
88f9aafc 9296 cupsdSetStringf(job->auth_env + 0, "AUTH_USERNAME=%s", con->username);
09a101d6 9297
f7deaa1a 9298 /*
9299 * Write the authenticated password...
9300 */
9301
9302 httpEncode64_2(line, sizeof(line), con->password, strlen(con->password));
dcb445bc 9303 cupsFilePutConf(fp, "password", line);
09a101d6 9304
88f9aafc 9305 cupsdSetStringf(job->auth_env + 1, "AUTH_PASSWORD=%s", con->password);
f7deaa1a 9306 }
ef416fc2 9307
07ed0e9a 9308#ifdef HAVE_GSSAPI
eac3a0a0 9309 if (con->gss_uid > 0)
07ed0e9a 9310 {
dcb445bc 9311 cupsFilePrintf(fp, "uid %d\n", (int)con->gss_uid);
07ed0e9a
MS
9312 cupsdSetStringf(&job->auth_uid, "AUTH_UID=%d", (int)con->gss_uid);
9313 }
9314#endif /* HAVE_GSSAPI */
9315
ef416fc2 9316 /*
9317 * Write a random number of newlines to the end of the file...
9318 */
9319
41681883 9320 for (i = (CUPS_RAND() % 1024); i >= 0; i --)
ef416fc2 9321 cupsFilePutChar(fp, '\n');
9322
9323 /*
9324 * Close the file and return...
9325 */
9326
9327 cupsFileClose(fp);
9328}
9329
9330
9331/*
9332 * 'send_document()' - Send a file to a printer or class.
9333 */
9334
9335static void
9336send_document(cupsd_client_t *con, /* I - Client connection */
9337 ipp_attribute_t *uri) /* I - Printer URI */
9338{
9339 ipp_attribute_t *attr; /* Current attribute */
a0f6818e
MS
9340 ipp_attribute_t *format; /* Request's document-format attribute */
9341 ipp_attribute_t *jformat; /* Job's document-format attribute */
f7deaa1a 9342 const char *default_format;/* document-format-default value */
ef416fc2 9343 int jobid; /* Job ID number */
9344 cupsd_job_t *job; /* Current job */
9345 char job_uri[HTTP_MAX_URI],
9346 /* Job URI */
61cf44e2 9347 scheme[HTTP_MAX_URI],
ef416fc2 9348 /* Method portion of URI */
9349 username[HTTP_MAX_URI],
9350 /* Username portion of URI */
9351 host[HTTP_MAX_URI],
9352 /* Host portion of URI */
9353 resource[HTTP_MAX_URI];
9354 /* Resource portion of URI */
9355 int port; /* Port portion of URI */
9356 mime_type_t *filetype; /* Type of file */
9357 char super[MIME_MAX_SUPER],
9358 /* Supertype of file */
9359 type[MIME_MAX_TYPE],
9360 /* Subtype of file */
9361 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
9362 /* Textual name of mime type */
9363 char filename[1024]; /* Job filename */
9364 cupsd_printer_t *printer; /* Current printer */
9365 struct stat fileinfo; /* File information */
9366 int kbytes; /* Size of file */
9367 int compression; /* Type of compression */
75bd9771 9368 int start_job; /* Start the job? */
ef416fc2 9369
9370
9371 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con,
9372 con->http.fd, uri->values[0].string.text);
9373
9374 /*
9375 * See if we have a job URI or a printer URI...
9376 */
9377
9378 if (!strcmp(uri->name, "printer-uri"))
9379 {
9380 /*
9381 * Got a printer URI; see if we also have a job-id attribute...
9382 */
9383
fa73b229 9384 if ((attr = ippFindAttribute(con->request, "job-id",
9385 IPP_TAG_INTEGER)) == NULL)
ef416fc2 9386 {
9387 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 9388 _("Got a printer-uri attribute but no job-id."));
ef416fc2 9389 return;
9390 }
9391
9392 jobid = attr->values[0].integer;
9393 }
9394 else
9395 {
9396 /*
9397 * Got a job URI; parse it to get the job ID...
9398 */
9399
61cf44e2
MS
9400 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9401 sizeof(scheme), username, sizeof(username), host,
a4d04587 9402 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 9403
9404 if (strncmp(resource, "/jobs/", 6))
9405 {
9406 /*
9407 * Not a valid URI!
9408 */
9409
84315f46 9410 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
ef416fc2 9411 uri->values[0].string.text);
9412 return;
9413 }
9414
9415 jobid = atoi(resource + 6);
9416 }
9417
9418 /*
9419 * See if the job exists...
9420 */
9421
9422 if ((job = cupsdFindJob(jobid)) == NULL)
9423 {
9424 /*
9425 * Nope - return a "not found" error...
9426 */
9427
84315f46 9428 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
ef416fc2 9429 return;
9430 }
9431
f301802f 9432 printer = cupsdFindDest(job->dest);
9433
ef416fc2 9434 /*
9435 * See if the job is owned by the requesting user...
9436 */
9437
9438 if (!validate_user(job, con, job->username, username, sizeof(username)))
9439 {
b0f6947b
MS
9440 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9441 cupsdFindDest(job->dest));
ef416fc2 9442 return;
9443 }
9444
9445 /*
9446 * OK, see if the client is sending the document compressed - CUPS
9447 * only supports "none" and "gzip".
9448 */
9449
9450 compression = CUPS_FILE_NONE;
9451
fa73b229 9452 if ((attr = ippFindAttribute(con->request, "compression",
9453 IPP_TAG_KEYWORD)) != NULL)
ef416fc2 9454 {
9455 if (strcmp(attr->values[0].string.text, "none")
9456#ifdef HAVE_LIBZ
9457 && strcmp(attr->values[0].string.text, "gzip")
9458#endif /* HAVE_LIBZ */
9459 )
9460 {
84315f46 9461 send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"."),
ef416fc2 9462 attr->values[0].string.text);
9463 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
9464 "compression", NULL, attr->values[0].string.text);
9465 return;
9466 }
9467
9468#ifdef HAVE_LIBZ
9469 if (!strcmp(attr->values[0].string.text, "gzip"))
9470 compression = CUPS_FILE_GZIP;
9471#endif /* HAVE_LIBZ */
9472 }
9473
9474 /*
9475 * Do we have a file to print?
9476 */
9477
83e08001
MS
9478 if ((attr = ippFindAttribute(con->request, "last-document",
9479 IPP_TAG_BOOLEAN)) == NULL)
9480 {
9481 send_ipp_status(con, IPP_BAD_REQUEST,
9482 _("Missing last-document attribute in request."));
9483 return;
9484 }
9485
ef416fc2 9486 if (!con->filename)
9487 {
6d2f911b
MS
9488 /*
9489 * Check for an empty request with "last-document" set to true, which is
9490 * used to close an "open" job by RFC 2911, section 3.3.2.
9491 */
9492
83e08001 9493 if (job->num_files > 0 && attr->values[0].boolean)
6d2f911b
MS
9494 goto last_document;
9495
84315f46 9496 send_ipp_status(con, IPP_BAD_REQUEST, _("No file in print request."));
ef416fc2 9497 return;
9498 }
9499
9500 /*
9501 * Is it a format we support?
9502 */
9503
9504 if ((format = ippFindAttribute(con->request, "document-format",
9505 IPP_TAG_MIMETYPE)) != NULL)
9506 {
9507 /*
9508 * Grab format from client...
9509 */
9510
bc44d920 9511 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
9512 super, type) != 2)
ef416fc2 9513 {
84315f46 9514 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"."),
ef416fc2 9515 format->values[0].string.text);
9516 return;
9517 }
9518 }
f7deaa1a 9519 else if ((default_format = cupsGetOption("document-format",
9520 printer->num_options,
9521 printer->options)) != NULL)
9522 {
9523 /*
9524 * Use default document format...
9525 */
9526
9527 if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
9528 {
9529 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 9530 _("Bad document-format-default \"%s\"."), default_format);
f7deaa1a 9531 return;
9532 }
9533 }
ef416fc2 9534 else
9535 {
9536 /*
9537 * No document format attribute? Auto-type it!
9538 */
9539
5a9febac
MS
9540 strlcpy(super, "application", sizeof(super));
9541 strlcpy(type, "octet-stream", sizeof(type));
ef416fc2 9542 }
9543
fa73b229 9544 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
ef416fc2 9545 {
9546 /*
9547 * Auto-type the file...
9548 */
9549
bd7854cb 9550 ipp_attribute_t *doc_name; /* document-name attribute */
9551
9552
75bd9771 9553 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Auto-typing file...");
ef416fc2 9554
bd7854cb 9555 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
9556 filetype = mimeFileType(MimeDatabase, con->filename,
9557 doc_name ? doc_name->values[0].string.text : NULL,
9558 &compression);
ef416fc2 9559
f7deaa1a 9560 if (!filetype)
f7faf1f5 9561 filetype = mimeType(MimeDatabase, super, type);
a0f6818e 9562
0268488e
MS
9563 if (filetype)
9564 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Request file type is %s/%s.",
9565 filetype->super, filetype->type);
ed486911 9566 }
f7faf1f5 9567 else
9568 filetype = mimeType(MimeDatabase, super, type);
9569
49d87452 9570 if (filetype)
f7deaa1a 9571 {
9572 /*
9573 * Replace the document-format attribute value with the auto-typed or
9574 * default one.
9575 */
9576
9577 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9578 filetype->type);
9579
49d87452
MS
9580 if ((jformat = ippFindAttribute(job->attrs, "document-format",
9581 IPP_TAG_MIMETYPE)) != NULL)
f7deaa1a 9582 {
a0f6818e 9583 _cupsStrFree(jformat->values[0].string.text);
f7deaa1a 9584
a0f6818e 9585 jformat->values[0].string.text = _cupsStrAlloc(mimetype);
f7deaa1a 9586 }
9587 else
a0f6818e 9588 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
f7deaa1a 9589 "document-format", NULL, mimetype);
9590 }
9591 else if (!filetype)
ef416fc2 9592 {
9593 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
84315f46 9594 _("Unsupported document-format \"%s/%s\"."), super, type);
ef416fc2 9595 cupsdLogMessage(CUPSD_LOG_INFO,
9596 "Hint: Do you have the raw file printing rules enabled?");
9597
9598 if (format)
9599 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9600 "document-format", NULL, format->values[0].string.text);
9601
9602 return;
9603 }
9604
80ca4592 9605 if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype))
9606 {
9607 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9608 filetype->type);
9609
9610 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
84315f46 9611 _("Unsupported document-format \"%s\"."), mimetype);
80ca4592 9612
9613 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9614 "document-format", NULL, mimetype);
9615
9616 return;
9617 }
9618
ef416fc2 9619 /*
9620 * Add the file to the job...
9621 */
9622
bd7854cb 9623 cupsdLoadJob(job);
9624
ef416fc2 9625 if (add_file(con, job, filetype, compression))
9626 return;
9627
ef416fc2 9628 if (stat(con->filename, &fileinfo))
9629 kbytes = 0;
9630 else
9631 kbytes = (fileinfo.st_size + 1023) / 1024;
9632
9633 cupsdUpdateQuota(printer, job->username, 0, kbytes);
9634
fa73b229 9635 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
9636 IPP_TAG_INTEGER)) != NULL)
ef416fc2 9637 attr->values[0].integer += kbytes;
9638
9639 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
9640 job->num_files);
9641 rename(con->filename, filename);
9642
9643 cupsdClearString(&con->filename);
9644
75bd9771
MS
9645 cupsdLogJob(job, CUPSD_LOG_INFO, "File of type %s/%s queued by \"%s\".",
9646 filetype->super, filetype->type, job->username);
ef416fc2 9647
9648 /*
9649 * Start the job if this is the last document...
9650 */
9651
6d2f911b
MS
9652 last_document:
9653
fa73b229 9654 if ((attr = ippFindAttribute(con->request, "last-document",
9655 IPP_TAG_BOOLEAN)) != NULL &&
ef416fc2 9656 attr->values[0].boolean)
9657 {
9658 /*
9659 * See if we need to add the ending sheet...
9660 */
9661
91c84a35
MS
9662 if (cupsdTimeoutJob(job))
9663 return;
ef416fc2 9664
bd7854cb 9665 if (job->state_value == IPP_JOB_STOPPED)
9666 {
ef416fc2 9667 job->state->values[0].integer = IPP_JOB_PENDING;
bd7854cb 9668 job->state_value = IPP_JOB_PENDING;
12f89d24
MS
9669
9670 ippSetString(job->attrs, &job->reasons, 0, "none");
bd7854cb 9671 }
9672 else if (job->state_value == IPP_JOB_HELD)
ef416fc2 9673 {
fa73b229 9674 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9675 IPP_TAG_KEYWORD)) == NULL)
ef416fc2 9676 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9677
fa73b229 9678 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
bd7854cb 9679 {
ef416fc2 9680 job->state->values[0].integer = IPP_JOB_PENDING;
bd7854cb 9681 job->state_value = IPP_JOB_PENDING;
12f89d24
MS
9682
9683 ippSetString(job->attrs, &job->reasons, 0, "none");
bd7854cb 9684 }
12f89d24
MS
9685 else
9686 ippSetString(job->attrs, &job->reasons, 0, "job-hold-until-specified");
ef416fc2 9687 }
9688
3dfe78b3
MS
9689 job->dirty = 1;
9690 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
ef416fc2 9691
75bd9771 9692 start_job = 1;
ef416fc2 9693 }
9694 else
9695 {
fa73b229 9696 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9697 IPP_TAG_KEYWORD)) == NULL)
ef416fc2 9698 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9699
fa73b229 9700 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
ef416fc2 9701 {
9702 job->state->values[0].integer = IPP_JOB_HELD;
bd7854cb 9703 job->state_value = IPP_JOB_HELD;
dfd5680b 9704 job->hold_until = time(NULL) + MultipleOperationTimeout;
3dfe78b3 9705
12f89d24
MS
9706 ippSetString(job->attrs, &job->reasons, 0, "job-incoming");
9707
9708 job->dirty = 1;
3dfe78b3 9709 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
ef416fc2 9710 }
75bd9771
MS
9711
9712 start_job = 0;
ef416fc2 9713 }
9714
9715 /*
9716 * Fill in the response info...
9717 */
9718
83e08001
MS
9719 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
9720 con->servername, con->serverport, "/jobs/%d", jobid);
ef416fc2 9721 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
9722 job_uri);
9723
9724 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid);
9725
9726 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
75bd9771 9727 job->state_value);
12f89d24
MS
9728 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons",
9729 NULL, job->reasons->values[0].string.text);
ef416fc2 9730
9731 con->response->request.status.status_code = IPP_OK;
75bd9771
MS
9732
9733 /*
9734 * Start the job if necessary...
9735 */
9736
9737 if (start_job)
9738 cupsdCheckJobs();
ef416fc2 9739}
9740
9741
9742/*
9743 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9744 */
9745
9746static void
f899b121 9747send_http_error(
9748 cupsd_client_t *con, /* I - Client connection */
9749 http_status_t status, /* I - HTTP status code */
9750 cupsd_printer_t *printer) /* I - Printer, if any */
ef416fc2 9751{
9f5eb9be
MS
9752 ipp_attribute_t *uri; /* Request URI, if any */
9753
9754
9755 if ((uri = ippFindAttribute(con->request, "printer-uri",
9756 IPP_TAG_URI)) == NULL)
9757 uri = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI);
9758
9759 cupsdLogMessage(status == HTTP_FORBIDDEN ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
9760 "Returning HTTP %s for %s (%s) from %s",
9761 httpStatus(status),
321d8d57
MS
9762 con->request ?
9763 ippOpString(con->request->request.op.operation_id) :
9764 "no operation-id",
9f5eb9be
MS
9765 uri ? uri->values[0].string.text : "no URI",
9766 con->http.hostname);
ef416fc2 9767
5f64df29
MS
9768 if (printer)
9769 {
db0bd74a 9770 int auth_type; /* Type of authentication required */
2fb76298
MS
9771
9772
5f64df29
MS
9773 auth_type = CUPSD_AUTH_NONE;
9774
9775 if (status == HTTP_UNAUTHORIZED &&
9776 printer->num_auth_info_required > 0 &&
9777 !strcmp(printer->auth_info_required[0], "negotiate") &&
9778 con->request &&
9779 (con->request->request.op.operation_id == IPP_PRINT_JOB ||
9780 con->request->request.op.operation_id == IPP_CREATE_JOB ||
9781 con->request->request.op.operation_id == CUPS_AUTHENTICATE_JOB))
9782 {
9783 /*
9784 * Creating and authenticating jobs requires Kerberos...
9785 */
9786
9787 auth_type = CUPSD_AUTH_NEGOTIATE;
9788 }
db0bd74a 9789 else
5f64df29
MS
9790 {
9791 /*
9792 * Use policy/location-defined authentication requirements...
9793 */
9794
9795 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
9796 cupsd_location_t *auth; /* Pointer to authentication element */
9797
9798
9799 if (printer->type & CUPS_PRINTER_CLASS)
9800 snprintf(resource, sizeof(resource), "/classes/%s", printer->name);
9801 else
9802 snprintf(resource, sizeof(resource), "/printers/%s", printer->name);
9803
9804 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
9805 auth->type == CUPSD_AUTH_NONE)
9806 auth = cupsdFindPolicyOp(printer->op_policy_ptr,
9807 con->request ?
9808 con->request->request.op.operation_id :
9809 IPP_PRINT_JOB);
9810
9811 if (auth)
9812 {
9813 if (auth->type == CUPSD_AUTH_DEFAULT)
dcb445bc 9814 auth_type = cupsdDefaultAuthType();
5f64df29
MS
9815 else
9816 auth_type = auth->type;
9817 }
9818 }
2fb76298 9819
db0bd74a 9820 cupsdSendError(con, status, auth_type);
2fb76298 9821 }
f899b121 9822 else
5bd77a73 9823 cupsdSendError(con, status, CUPSD_AUTH_NONE);
ef416fc2 9824
9825 ippDelete(con->response);
9826 con->response = NULL;
9827
9828 return;
9829}
9830
9831
9832/*
9833 * 'send_ipp_status()' - Send a status back to the IPP client.
9834 */
9835
9836static void
9837send_ipp_status(cupsd_client_t *con, /* I - Client connection */
1f0275e3
MS
9838 ipp_status_t status, /* I - IPP status code */
9839 const char *message,/* I - Status message */
9840 ...) /* I - Additional args as needed */
ef416fc2 9841{
9842 va_list ap; /* Pointer to additional args */
9843 char formatted[1024]; /* Formatted errror message */
9844
9845
3d8365b8 9846 va_start(ap, message);
9847 vsnprintf(formatted, sizeof(formatted),
9848 _cupsLangString(con->language, message), ap);
9849 va_end(ap);
ef416fc2 9850
3d8365b8 9851 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s",
9852 ippOpString(con->request->request.op.operation_id),
9853 ippErrorString(status), formatted);
ef416fc2 9854
9855 con->response->request.status.status_code = status;
9856
fa73b229 9857 if (ippFindAttribute(con->response, "attributes-charset",
9858 IPP_TAG_ZERO) == NULL)
ef416fc2 9859 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
f8b3a85b 9860 "attributes-charset", NULL, "utf-8");
ef416fc2 9861
9862 if (ippFindAttribute(con->response, "attributes-natural-language",
9863 IPP_TAG_ZERO) == NULL)
9864 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
9865 "attributes-natural-language", NULL, DefaultLanguage);
9866
3d8365b8 9867 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT,
9868 "status-message", NULL, formatted);
ef416fc2 9869}
9870
9871
9872/*
9873 * 'set_default()' - Set the default destination...
9874 */
9875
9876static void
9877set_default(cupsd_client_t *con, /* I - Client connection */
9878 ipp_attribute_t *uri) /* I - Printer URI */
9879{
9880 http_status_t status; /* Policy status */
bc44d920 9881 cups_ptype_t dtype; /* Destination type (printer/class) */
dd1abb6b
MS
9882 cupsd_printer_t *printer, /* Printer */
9883 *oldprinter; /* Old default printer */
ef416fc2 9884
9885
9886 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con,
9887 con->http.fd, uri->values[0].string.text);
9888
9889 /*
9890 * Is the destination valid?
9891 */
9892
f7deaa1a 9893 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 9894 {
9895 /*
9896 * Bad URI...
9897 */
9898
9899 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 9900 _("The printer or class does not exist."));
ef416fc2 9901 return;
9902 }
9903
9904 /*
9905 * Check policy...
9906 */
9907
9908 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
9909 {
f899b121 9910 send_http_error(con, status, NULL);
ef416fc2 9911 return;
9912 }
9913
9914 /*
9915 * Set it as the default...
9916 */
9917
dd1abb6b 9918 oldprinter = DefaultPrinter;
ef416fc2 9919 DefaultPrinter = printer;
9920
dd1abb6b
MS
9921 if (oldprinter)
9922 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, oldprinter, NULL,
9923 "%s is no longer the default printer.", oldprinter->name);
9924
9925 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
9926 "%s is now the default printer.", printer->name);
9927
3dfe78b3 9928 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES |
a2326b5b 9929 CUPSD_DIRTY_PRINTCAP);
ef416fc2 9930
9931 cupsdLogMessage(CUPSD_LOG_INFO,
f7deaa1a 9932 "Default destination set to \"%s\" by \"%s\".",
9933 printer->name, get_username(con));
ef416fc2 9934
9935 /*
9936 * Everything was ok, so return OK status...
9937 */
9938
9939 con->response->request.status.status_code = IPP_OK;
9940}
9941
9942
9943/*
9944 * 'set_job_attrs()' - Set job attributes.
9945 */
9946
9947static void
9948set_job_attrs(cupsd_client_t *con, /* I - Client connection */
9949 ipp_attribute_t *uri) /* I - Job URI */
9950{
9951 ipp_attribute_t *attr, /* Current attribute */
9952 *attr2; /* Job attribute */
9953 int jobid; /* Job ID */
9954 cupsd_job_t *job; /* Current job */
61cf44e2 9955 char scheme[HTTP_MAX_URI],
ef416fc2 9956 /* Method portion of URI */
9957 username[HTTP_MAX_URI],
9958 /* Username portion of URI */
9959 host[HTTP_MAX_URI],
9960 /* Host portion of URI */
9961 resource[HTTP_MAX_URI];
9962 /* Resource portion of URI */
9963 int port; /* Port portion of URI */
d09495fa 9964 int event; /* Events? */
005dd1eb 9965 int check_jobs; /* Check jobs? */
ef416fc2 9966
9967
9968 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con,
9969 con->http.fd, uri->values[0].string.text);
9970
9971 /*
9972 * Start with "everything is OK" status...
9973 */
9974
9975 con->response->request.status.status_code = IPP_OK;
9976
9977 /*
9978 * See if we have a job URI or a printer URI...
9979 */
9980
fa73b229 9981 if (!strcmp(uri->name, "printer-uri"))
ef416fc2 9982 {
9983 /*
9984 * Got a printer URI; see if we also have a job-id attribute...
9985 */
9986
fa73b229 9987 if ((attr = ippFindAttribute(con->request, "job-id",
9988 IPP_TAG_INTEGER)) == NULL)
ef416fc2 9989 {
9990 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 9991 _("Got a printer-uri attribute but no job-id."));
ef416fc2 9992 return;
9993 }
9994
9995 jobid = attr->values[0].integer;
9996 }
9997 else
9998 {
9999 /*
10000 * Got a job URI; parse it to get the job ID...
10001 */
10002
61cf44e2
MS
10003 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
10004 sizeof(scheme), username, sizeof(username), host,
a4d04587 10005 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 10006
fa73b229 10007 if (strncmp(resource, "/jobs/", 6))
ef416fc2 10008 {
10009 /*
10010 * Not a valid URI!
10011 */
10012
84315f46 10013 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
ef416fc2 10014 uri->values[0].string.text);
10015 return;
10016 }
10017
10018 jobid = atoi(resource + 6);
10019 }
10020
10021 /*
10022 * See if the job exists...
10023 */
10024
10025 if ((job = cupsdFindJob(jobid)) == NULL)
10026 {
10027 /*
10028 * Nope - return a "not found" error...
10029 */
10030
84315f46 10031 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
ef416fc2 10032 return;
10033 }
10034
10035 /*
10036 * See if the job has been completed...
10037 */
10038
bd7854cb 10039 if (job->state_value > IPP_JOB_STOPPED)
ef416fc2 10040 {
10041 /*
10042 * Return a "not-possible" error...
10043 */
10044
10045 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 10046 _("Job #%d is finished and cannot be altered."), jobid);
ef416fc2 10047 return;
10048 }
10049
10050 /*
10051 * See if the job is owned by the requesting user...
10052 */
10053
10054 if (!validate_user(job, con, job->username, username, sizeof(username)))
10055 {
b0f6947b
MS
10056 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
10057 cupsdFindDest(job->dest));
ef416fc2 10058 return;
10059 }
10060
10061 /*
10062 * See what the user wants to change.
10063 */
10064
bd7854cb 10065 cupsdLoadJob(job);
10066
005dd1eb
MS
10067 check_jobs = 0;
10068 event = 0;
d09495fa 10069
fa73b229 10070 for (attr = con->request->attrs; attr; attr = attr->next)
ef416fc2 10071 {
10072 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
10073 continue;
10074
10075 if (!strcmp(attr->name, "attributes-charset") ||
10076 !strcmp(attr->name, "attributes-natural-language") ||
10077 !strcmp(attr->name, "document-compression") ||
10078 !strcmp(attr->name, "document-format") ||
10079 !strcmp(attr->name, "job-detailed-status-messages") ||
10080 !strcmp(attr->name, "job-document-access-errors") ||
10081 !strcmp(attr->name, "job-id") ||
5d6412a9 10082 !strcmp(attr->name, "job-impressions-completed") ||
ef416fc2 10083 !strcmp(attr->name, "job-k-octets") ||
10084 !strcmp(attr->name, "job-originating-host-name") ||
10085 !strcmp(attr->name, "job-originating-user-name") ||
10086 !strcmp(attr->name, "job-printer-up-time") ||
10087 !strcmp(attr->name, "job-printer-uri") ||
10088 !strcmp(attr->name, "job-sheets") ||
10089 !strcmp(attr->name, "job-state-message") ||
10090 !strcmp(attr->name, "job-state-reasons") ||
10091 !strcmp(attr->name, "job-uri") ||
10092 !strcmp(attr->name, "number-of-documents") ||
10093 !strcmp(attr->name, "number-of-intervening-jobs") ||
10094 !strcmp(attr->name, "output-device-assigned") ||
10095 !strncmp(attr->name, "date-time-at-", 13) ||
ef416fc2 10096 !strncmp(attr->name, "job-k-octets", 12) ||
10097 !strncmp(attr->name, "job-media-sheets", 16) ||
10098 !strncmp(attr->name, "time-at-", 8))
10099 {
10100 /*
10101 * Read-only attrs!
10102 */
10103
10104 send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE,
10105 _("%s cannot be changed."), attr->name);
10106
a2326b5b
MS
10107 attr2 = ippCopyAttribute(con->response, attr, 0);
10108 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
ef416fc2 10109 continue;
10110 }
10111
10112 if (!strcmp(attr->name, "job-priority"))
10113 {
10114 /*
10115 * Change the job priority...
10116 */
10117
10118 if (attr->value_tag != IPP_TAG_INTEGER)
10119 {
84315f46 10120 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value."));
ef416fc2 10121
a2326b5b
MS
10122 attr2 = ippCopyAttribute(con->response, attr, 0);
10123 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
ef416fc2 10124 }
bd7854cb 10125 else if (job->state_value >= IPP_JOB_PROCESSING)
ef416fc2 10126 {
10127 send_ipp_status(con, IPP_NOT_POSSIBLE,
10128 _("Job is completed and cannot be changed."));
10129 return;
10130 }
10131 else if (con->response->request.status.status_code == IPP_OK)
d09495fa 10132 {
005dd1eb
MS
10133 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-priority to %d",
10134 attr->values[0].integer);
ef416fc2 10135 cupsdSetJobPriority(job, attr->values[0].integer);
005dd1eb
MS
10136
10137 check_jobs = 1;
10138 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED |
10139 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED;
d09495fa 10140 }
ef416fc2 10141 }
10142 else if (!strcmp(attr->name, "job-state"))
10143 {
10144 /*
10145 * Change the job state...
10146 */
10147
10148 if (attr->value_tag != IPP_TAG_ENUM)
10149 {
84315f46 10150 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value."));
ef416fc2 10151
a2326b5b
MS
10152 attr2 = ippCopyAttribute(con->response, attr, 0);
10153 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
ef416fc2 10154 }
10155 else
10156 {
10157 switch (attr->values[0].integer)
10158 {
10159 case IPP_JOB_PENDING :
10160 case IPP_JOB_HELD :
bd7854cb 10161 if (job->state_value > IPP_JOB_HELD)
ef416fc2 10162 {
10163 send_ipp_status(con, IPP_NOT_POSSIBLE,
10164 _("Job state cannot be changed."));
10165 return;
10166 }
10167 else if (con->response->request.status.status_code == IPP_OK)
bd7854cb 10168 {
005dd1eb
MS
10169 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10170 attr->values[0].integer);
b9faaae1
MS
10171 cupsdSetJobState(job, attr->values[0].integer,
10172 CUPSD_JOB_DEFAULT,
10173 "Job state changed by \"%s\"", username);
005dd1eb 10174 check_jobs = 1;
bd7854cb 10175 }
ef416fc2 10176 break;
10177
10178 case IPP_JOB_PROCESSING :
10179 case IPP_JOB_STOPPED :
bd7854cb 10180 if (job->state_value != attr->values[0].integer)
ef416fc2 10181 {
10182 send_ipp_status(con, IPP_NOT_POSSIBLE,
10183 _("Job state cannot be changed."));
10184 return;
10185 }
10186 break;
10187
d09495fa 10188 case IPP_JOB_CANCELED :
ef416fc2 10189 case IPP_JOB_ABORTED :
10190 case IPP_JOB_COMPLETED :
bd7854cb 10191 if (job->state_value > IPP_JOB_PROCESSING)
ef416fc2 10192 {
10193 send_ipp_status(con, IPP_NOT_POSSIBLE,
10194 _("Job state cannot be changed."));
10195 return;
10196 }
10197 else if (con->response->request.status.status_code == IPP_OK)
005dd1eb
MS
10198 {
10199 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10200 attr->values[0].integer);
b9faaae1
MS
10201 cupsdSetJobState(job, (ipp_jstate_t)attr->values[0].integer,
10202 CUPSD_JOB_DEFAULT,
10203 "Job state changed by \"%s\"", username);
005dd1eb
MS
10204 check_jobs = 1;
10205 }
ef416fc2 10206 break;
10207 }
10208 }
10209 }
10210 else if (con->response->request.status.status_code != IPP_OK)
10211 continue;
fa73b229 10212 else if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10213 IPP_TAG_ZERO)) != NULL)
ef416fc2 10214 {
10215 /*
10216 * Some other value; first free the old value...
10217 */
10218
10219 if (job->attrs->prev)
10220 job->attrs->prev->next = attr2->next;
10221 else
10222 job->attrs->attrs = attr2->next;
10223
10224 if (job->attrs->last == attr2)
10225 job->attrs->last = job->attrs->prev;
10226
a2326b5b 10227 ippDeleteAttribute(NULL, attr2);
ef416fc2 10228
10229 /*
10230 * Then copy the attribute...
10231 */
10232
a2326b5b 10233 ippCopyAttribute(job->attrs, attr, 0);
ef416fc2 10234
10235 /*
10236 * See if the job-name or job-hold-until is being changed.
10237 */
10238
10239 if (!strcmp(attr->name, "job-hold-until"))
10240 {
005dd1eb
MS
10241 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s",
10242 attr->values[0].string.text);
b9faaae1 10243 cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
ef416fc2 10244
10245 if (!strcmp(attr->values[0].string.text, "no-hold"))
b9faaae1 10246 {
ef416fc2 10247 cupsdReleaseJob(job);
b9faaae1
MS
10248 check_jobs = 1;
10249 }
ef416fc2 10250 else
b9faaae1
MS
10251 cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT,
10252 "Job held by \"%s\".", username);
d09495fa 10253
b9faaae1 10254 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
ef416fc2 10255 }
10256 }
10257 else if (attr->value_tag == IPP_TAG_DELETEATTR)
10258 {
10259 /*
10260 * Delete the attribute...
10261 */
10262
10263 if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10264 IPP_TAG_ZERO)) != NULL)
10265 {
10266 if (job->attrs->prev)
10267 job->attrs->prev->next = attr2->next;
10268 else
10269 job->attrs->attrs = attr2->next;
10270
10271 if (attr2 == job->attrs->last)
10272 job->attrs->last = job->attrs->prev;
10273
a2326b5b 10274 ippDeleteAttribute(NULL, attr2);
d09495fa 10275
10276 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
ef416fc2 10277 }
10278 }
10279 else
10280 {
10281 /*
10282 * Add new option by copying it...
10283 */
10284
a2326b5b 10285 ippCopyAttribute(job->attrs, attr, 0);
d09495fa 10286
10287 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
ef416fc2 10288 }
10289 }
10290
10291 /*
10292 * Save the job...
10293 */
10294
3dfe78b3
MS
10295 job->dirty = 1;
10296 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
ef416fc2 10297
d09495fa 10298 /*
10299 * Send events as needed...
10300 */
10301
d9bca400 10302 if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED)
01ce6322
MS
10303 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED,
10304 cupsdFindDest(job->dest), job,
d9bca400
MS
10305 "Job priority changed by user.");
10306
d09495fa 10307 if (event & CUPSD_EVENT_JOB_STATE)
01ce6322 10308 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
d09495fa 10309 job->state_value == IPP_JOB_HELD ?
10310 "Job held by user." : "Job restarted by user.");
10311
10312 if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED)
01ce6322 10313 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
d09495fa 10314 "Job options changed by user.");
10315
ef416fc2 10316 /*
10317 * Start jobs if possible...
10318 */
10319
005dd1eb
MS
10320 if (check_jobs)
10321 cupsdCheckJobs();
ef416fc2 10322}
10323
10324
c168a833
MS
10325/*
10326 * 'set_printer_attrs()' - Set printer attributes.
10327 */
10328
10329static void
10330set_printer_attrs(cupsd_client_t *con, /* I - Client connection */
10331 ipp_attribute_t *uri) /* I - Printer */
10332{
10333 http_status_t status; /* Policy status */
10334 cups_ptype_t dtype; /* Destination type (printer/class) */
10335 cupsd_printer_t *printer; /* Printer/class */
10336 ipp_attribute_t *attr; /* Printer attribute */
bf3816c7 10337 int changed = 0; /* Was anything changed? */
c168a833
MS
10338
10339
10340 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_attrs(%p[%d], %s)", con,
10341 con->http.fd, uri->values[0].string.text);
10342
10343 /*
10344 * Is the destination valid?
10345 */
10346
10347 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10348 {
10349 /*
10350 * Bad URI...
10351 */
10352
10353 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 10354 _("The printer or class does not exist."));
c168a833
MS
10355 return;
10356 }
10357
10358 /*
10359 * Check policy...
10360 */
10361
10362 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10363 {
10364 send_http_error(con, status, printer);
10365 return;
10366 }
10367
10368 /*
10369 * Return a list of attributes that can be set via Set-Printer-Attributes.
10370 */
10371
10372 if ((attr = ippFindAttribute(con->request, "printer-location",
10373 IPP_TAG_TEXT)) != NULL)
10374 {
10375 cupsdSetString(&printer->location, attr->values[0].string.text);
10376 changed = 1;
10377 }
10378
10379 if ((attr = ippFindAttribute(con->request, "printer-info",
10380 IPP_TAG_TEXT)) != NULL)
10381 {
10382 cupsdSetString(&printer->info, attr->values[0].string.text);
10383 changed = 1;
10384 }
10385
10386 /*
10387 * Update the printer attributes and return...
10388 */
10389
10390 if (changed)
10391 {
10392 cupsdSetPrinterAttrs(printer);
10393 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
10394
10395 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL,
10396 "Printer \"%s\" description or location changed by \"%s\".",
10397 printer->name, get_username(con));
10398
10399 cupsdLogMessage(CUPSD_LOG_INFO,
10400 "Printer \"%s\" description or location changed by \"%s\".",
10401 printer->name, get_username(con));
10402 }
10403
10404 con->response->request.status.status_code = IPP_OK;
10405}
10406
10407
b423cd4c 10408/*
10409 * 'set_printer_defaults()' - Set printer default options from a request.
10410 */
10411
10412static void
10413set_printer_defaults(
10414 cupsd_client_t *con, /* I - Client connection */
10415 cupsd_printer_t *printer) /* I - Printer */
10416{
10417 int i; /* Looping var */
10418 ipp_attribute_t *attr; /* Current attribute */
10419 int namelen; /* Length of attribute name */
10420 char name[256], /* New attribute name */
10421 value[256]; /* String version of integer attrs */
10422
10423
10424 for (attr = con->request->attrs; attr; attr = attr->next)
10425 {
10426 /*
10427 * Skip non-printer attributes...
10428 */
10429
10430 if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
10431 continue;
10432
10433 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name);
10434
10435 if (!strcmp(attr->name, "job-sheets-default"))
10436 {
10437 /*
10438 * Only allow keywords and names...
10439 */
10440
10441 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10442 continue;
10443
10444 /*
10445 * Only allow job-sheets-default to be set when running without a
10446 * system high classification level...
10447 */
10448
10449 if (Classification)
10450 continue;
10451
10452 cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text);
10453
10454 if (attr->num_values > 1)
10455 cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text);
10456 else
10457 cupsdSetString(&printer->job_sheets[1], "none");
10458 }
10459 else if (!strcmp(attr->name, "requesting-user-name-allowed"))
10460 {
10d09e33 10461 cupsdFreeStrings(&(printer->users));
b423cd4c 10462
10463 printer->deny_users = 0;
10464
10465 if (attr->value_tag == IPP_TAG_NAME &&
10466 (attr->num_values > 1 ||
10467 strcmp(attr->values[0].string.text, "all")))
10468 {
10469 for (i = 0; i < attr->num_values; i ++)
10d09e33 10470 cupsdAddString(&(printer->users), attr->values[i].string.text);
b423cd4c 10471 }
10472 }
10473 else if (!strcmp(attr->name, "requesting-user-name-denied"))
10474 {
10d09e33 10475 cupsdFreeStrings(&(printer->users));
b423cd4c 10476
10477 printer->deny_users = 1;
10478
10479 if (attr->value_tag == IPP_TAG_NAME &&
10480 (attr->num_values > 1 ||
10481 strcmp(attr->values[0].string.text, "none")))
10482 {
10483 for (i = 0; i < attr->num_values; i ++)
10d09e33 10484 cupsdAddString(&(printer->users), attr->values[i].string.text);
b423cd4c 10485 }
10486 }
10487 else if (!strcmp(attr->name, "job-quota-period"))
10488 {
10489 if (attr->value_tag != IPP_TAG_INTEGER)
10490 continue;
10491
10492 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...",
10493 attr->values[0].integer);
10494 cupsdFreeQuotas(printer);
10495
10496 printer->quota_period = attr->values[0].integer;
10497 }
10498 else if (!strcmp(attr->name, "job-k-limit"))
10499 {
10500 if (attr->value_tag != IPP_TAG_INTEGER)
10501 continue;
10502
10503 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...",
10504 attr->values[0].integer);
10505 cupsdFreeQuotas(printer);
10506
10507 printer->k_limit = attr->values[0].integer;
10508 }
10509 else if (!strcmp(attr->name, "job-page-limit"))
10510 {
10511 if (attr->value_tag != IPP_TAG_INTEGER)
10512 continue;
10513
10514 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...",
10515 attr->values[0].integer);
10516 cupsdFreeQuotas(printer);
10517
10518 printer->page_limit = attr->values[0].integer;
10519 }
10520 else if (!strcmp(attr->name, "printer-op-policy"))
10521 {
10522 cupsd_policy_t *p; /* Policy */
10523
10524
10525 if (attr->value_tag != IPP_TAG_NAME)
10526 continue;
10527
10528 if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
10529 {
10530 cupsdLogMessage(CUPSD_LOG_DEBUG,
10531 "Setting printer-op-policy to \"%s\"...",
10532 attr->values[0].string.text);
10533 cupsdSetString(&printer->op_policy, attr->values[0].string.text);
10534 printer->op_policy_ptr = p;
10535 }
10536 else
10537 {
10538 send_ipp_status(con, IPP_NOT_POSSIBLE,
10539 _("Unknown printer-op-policy \"%s\"."),
10540 attr->values[0].string.text);
10541 return;
10542 }
10543 }
10544 else if (!strcmp(attr->name, "printer-error-policy"))
10545 {
10546 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10547 continue;
10548
f11a948a 10549 if (strcmp(attr->values[0].string.text, "retry-current-job") &&
a2326b5b 10550 ((printer->type & CUPS_PRINTER_CLASS) ||
f11a948a
MS
10551 (strcmp(attr->values[0].string.text, "abort-job") &&
10552 strcmp(attr->values[0].string.text, "retry-job") &&
10553 strcmp(attr->values[0].string.text, "stop-printer"))))
b423cd4c 10554 {
10555 send_ipp_status(con, IPP_NOT_POSSIBLE,
10556 _("Unknown printer-error-policy \"%s\"."),
10557 attr->values[0].string.text);
10558 return;
10559 }
10560
10561 cupsdLogMessage(CUPSD_LOG_DEBUG,
10562 "Setting printer-error-policy to \"%s\"...",
10563 attr->values[0].string.text);
10564 cupsdSetString(&printer->error_policy, attr->values[0].string.text);
10565 }
b423cd4c 10566
10567 /*
10568 * Skip any other non-default attributes...
10569 */
10570
10571 namelen = strlen(attr->name);
10572 if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") ||
10573 namelen > (sizeof(name) - 1) || attr->num_values != 1)
10574 continue;
10575
10576 /*
10577 * OK, anything else must be a user-defined default...
10578 */
10579
10580 strlcpy(name, attr->name, sizeof(name));
10581 name[namelen - 8] = '\0'; /* Strip "-default" */
10582
10583 switch (attr->value_tag)
10584 {
10585 case IPP_TAG_DELETEATTR :
10586 printer->num_options = cupsRemoveOption(name,
10587 printer->num_options,
10588 &(printer->options));
10589 cupsdLogMessage(CUPSD_LOG_DEBUG,
10590 "Deleting %s", attr->name);
10591 break;
10592
10593 case IPP_TAG_NAME :
5a9febac 10594 case IPP_TAG_TEXT :
b423cd4c 10595 case IPP_TAG_KEYWORD :
10596 case IPP_TAG_URI :
10597 printer->num_options = cupsAddOption(name,
10598 attr->values[0].string.text,
10599 printer->num_options,
10600 &(printer->options));
10601 cupsdLogMessage(CUPSD_LOG_DEBUG,
10602 "Setting %s to \"%s\"...", attr->name,
10603 attr->values[0].string.text);
10604 break;
10605
10606 case IPP_TAG_BOOLEAN :
10607 printer->num_options = cupsAddOption(name,
10608 attr->values[0].boolean ?
10609 "true" : "false",
10610 printer->num_options,
10611 &(printer->options));
10612 cupsdLogMessage(CUPSD_LOG_DEBUG,
10613 "Setting %s to %s...", attr->name,
10614 attr->values[0].boolean ? "true" : "false");
10615 break;
10616
10617 case IPP_TAG_INTEGER :
10618 case IPP_TAG_ENUM :
10619 sprintf(value, "%d", attr->values[0].integer);
10620 printer->num_options = cupsAddOption(name, value,
10621 printer->num_options,
10622 &(printer->options));
10623 cupsdLogMessage(CUPSD_LOG_DEBUG,
10624 "Setting %s to %s...", attr->name, value);
10625 break;
10626
10627 case IPP_TAG_RANGE :
10628 sprintf(value, "%d-%d", attr->values[0].range.lower,
10629 attr->values[0].range.upper);
10630 printer->num_options = cupsAddOption(name, value,
10631 printer->num_options,
10632 &(printer->options));
10633 cupsdLogMessage(CUPSD_LOG_DEBUG,
10634 "Setting %s to %s...", attr->name, value);
10635 break;
10636
10637 case IPP_TAG_RESOLUTION :
10638 sprintf(value, "%dx%d%s", attr->values[0].resolution.xres,
10639 attr->values[0].resolution.yres,
10640 attr->values[0].resolution.units == IPP_RES_PER_INCH ?
3e7fe0ca 10641 "dpi" : "dpcm");
b423cd4c 10642 printer->num_options = cupsAddOption(name, value,
10643 printer->num_options,
10644 &(printer->options));
10645 cupsdLogMessage(CUPSD_LOG_DEBUG,
10646 "Setting %s to %s...", attr->name, value);
10647 break;
10648
10649 default :
10650 /* Do nothing for other values */
10651 break;
10652 }
10653 }
10654}
10655
10656
ef416fc2 10657/*
10658 * 'start_printer()' - Start a printer.
10659 */
10660
10661static void
10662start_printer(cupsd_client_t *con, /* I - Client connection */
10663 ipp_attribute_t *uri) /* I - Printer URI */
10664{
10d09e33 10665 int i; /* Temporary variable */
ef416fc2 10666 http_status_t status; /* Policy status */
bc44d920 10667 cups_ptype_t dtype; /* Destination type (printer/class) */
ef416fc2 10668 cupsd_printer_t *printer; /* Printer data */
10669
10670
10671 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con,
10672 con->http.fd, uri->values[0].string.text);
10673
10674 /*
10675 * Is the destination valid?
10676 */
10677
f7deaa1a 10678 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 10679 {
10680 /*
10681 * Bad URI...
10682 */
10683
10684 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 10685 _("The printer or class does not exist."));
ef416fc2 10686 return;
10687 }
10688
10689 /*
10690 * Check policy...
10691 */
10692
10693 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10694 {
f899b121 10695 send_http_error(con, status, printer);
ef416fc2 10696 return;
10697 }
10698
10699 /*
10700 * Start the printer...
10701 */
10702
10703 printer->state_message[0] = '\0';
10704
10705 cupsdStartPrinter(printer, 1);
10706
10707 if (dtype & CUPS_PRINTER_CLASS)
f7deaa1a 10708 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".",
10709 printer->name, get_username(con));
e00b005a 10710 else
f7deaa1a 10711 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".",
10712 printer->name, get_username(con));
ef416fc2 10713
10714 cupsdCheckJobs();
10715
10d09e33
MS
10716 /*
10717 * Check quotas...
10718 */
10719
10720 if ((i = check_quotas(con, printer)) < 0)
10721 {
10722 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
10723 return;
10724 }
10725 else if (i == 0)
10726 {
10727 send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print."));
10728 return;
10729 }
10730
ef416fc2 10731 /*
10732 * Everything was ok, so return OK status...
10733 */
10734
10735 con->response->request.status.status_code = IPP_OK;
10736}
10737
10738
10739/*
10740 * 'stop_printer()' - Stop a printer.
10741 */
10742
10743static void
10744stop_printer(cupsd_client_t *con, /* I - Client connection */
10745 ipp_attribute_t *uri) /* I - Printer URI */
10746{
10747 http_status_t status; /* Policy status */
bc44d920 10748 cups_ptype_t dtype; /* Destination type (printer/class) */
ef416fc2 10749 cupsd_printer_t *printer; /* Printer data */
10750 ipp_attribute_t *attr; /* printer-state-message attribute */
10751
10752
10753 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con,
10754 con->http.fd, uri->values[0].string.text);
10755
10756 /*
10757 * Is the destination valid?
10758 */
10759
f7deaa1a 10760 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 10761 {
10762 /*
10763 * Bad URI...
10764 */
10765
10766 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 10767 _("The printer or class does not exist."));
ef416fc2 10768 return;
10769 }
10770
10771 /*
10772 * Check policy...
10773 */
10774
10775 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10776 {
f899b121 10777 send_http_error(con, status, printer);
ef416fc2 10778 return;
10779 }
10780
10781 /*
10782 * Stop the printer...
10783 */
10784
10785 if ((attr = ippFindAttribute(con->request, "printer-state-message",
10786 IPP_TAG_TEXT)) == NULL)
5a9febac 10787 strlcpy(printer->state_message, "Paused", sizeof(printer->state_message));
ef416fc2 10788 else
10789 {
10790 strlcpy(printer->state_message, attr->values[0].string.text,
10791 sizeof(printer->state_message));
10792 }
10793
10794 cupsdStopPrinter(printer, 1);
10795
10796 if (dtype & CUPS_PRINTER_CLASS)
f7deaa1a 10797 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".",
10798 printer->name, get_username(con));
ef416fc2 10799 else
f7deaa1a 10800 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".",
10801 printer->name, get_username(con));
ef416fc2 10802
10803 /*
10804 * Everything was ok, so return OK status...
10805 */
10806
10807 con->response->request.status.status_code = IPP_OK;
10808}
10809
10810
89d46774 10811/*
10812 * 'url_encode_attr()' - URL-encode a string attribute.
10813 */
10814
10815static void
10816url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */
10817 char *buffer,/* I - String buffer */
10818 int bufsize)/* I - Size of buffer */
10819{
10820 int i; /* Looping var */
10821 char *bufptr, /* Pointer into buffer */
b94498cf 10822 *bufend; /* End of buffer */
89d46774 10823
10824
10825 strlcpy(buffer, attr->name, bufsize);
10826 bufptr = buffer + strlen(buffer);
10827 bufend = buffer + bufsize - 1;
10828
10829 for (i = 0; i < attr->num_values; i ++)
10830 {
10831 if (bufptr >= bufend)
10832 break;
10833
10834 if (i)
10835 *bufptr++ = ',';
10836 else
10837 *bufptr++ = '=';
10838
10839 if (bufptr >= bufend)
10840 break;
10841
10842 *bufptr++ = '\'';
10843
b94498cf 10844 bufptr = url_encode_string(attr->values[i].string.text,
10845 bufptr, bufend - bufptr + 1);
89d46774 10846
10847 if (bufptr >= bufend)
10848 break;
10849
10850 *bufptr++ = '\'';
10851 }
10852
10853 *bufptr = '\0';
10854}
10855
10856
b94498cf 10857/*
10858 * 'url_encode_string()' - URL-encode a string.
10859 */
10860
10861static char * /* O - End of string */
10862url_encode_string(const char *s, /* I - String */
10863 char *buffer, /* I - String buffer */
10864 int bufsize) /* I - Size of buffer */
10865{
10866 char *bufptr, /* Pointer into buffer */
10867 *bufend; /* End of buffer */
10868 static const char *hex = "0123456789ABCDEF";
10869 /* Hex digits */
10870
10871
10872 bufptr = buffer;
10873 bufend = buffer + bufsize - 1;
10874
10875 while (*s && bufptr < bufend)
10876 {
f701418f 10877 if (*s == ' ' || *s == '%' || *s == '+')
b94498cf 10878 {
10879 if (bufptr >= (bufend - 2))
10880 break;
10881
10882 *bufptr++ = '%';
10883 *bufptr++ = hex[(*s >> 4) & 15];
10884 *bufptr++ = hex[*s & 15];
10885
10886 s ++;
10887 }
10888 else if (*s == '\'' || *s == '\\')
10889 {
10890 if (bufptr >= (bufend - 1))
10891 break;
10892
10893 *bufptr++ = '\\';
10894 *bufptr++ = *s++;
10895 }
10896 else
10897 *bufptr++ = *s++;
10898 }
10899
10900 *bufptr = '\0';
10901
10902 return (bufptr);
10903}
10904
10905
ef416fc2 10906/*
10907 * 'user_allowed()' - See if a user is allowed to print to a queue.
10908 */
10909
10910static int /* O - 0 if not allowed, 1 if allowed */
10911user_allowed(cupsd_printer_t *p, /* I - Printer or class */
10912 const char *username) /* I - Username */
10913{
ef416fc2 10914 struct passwd *pw; /* User password data */
5bd77a73 10915 char baseuser[256], /* Base username */
10d09e33
MS
10916 *baseptr, /* Pointer to "@" in base username */
10917 *name; /* Current user name */
ef416fc2 10918
10919
10d09e33 10920 if (cupsArrayCount(p->users) == 0)
ef416fc2 10921 return (1);
10922
10923 if (!strcmp(username, "root"))
10924 return (1);
10925
5bd77a73
MS
10926 if (strchr(username, '@'))
10927 {
10928 /*
10929 * Strip @REALM for username check...
10930 */
10931
10932 strlcpy(baseuser, username, sizeof(baseuser));
10933
10934 if ((baseptr = strchr(baseuser, '@')) != NULL)
10935 *baseptr = '\0';
10936
10937 username = baseuser;
10938 }
10939
ef416fc2 10940 pw = getpwnam(username);
10941 endpwent();
10942
10d09e33
MS
10943 for (name = (char *)cupsArrayFirst(p->users);
10944 name;
10945 name = (char *)cupsArrayNext(p->users))
ef416fc2 10946 {
10d09e33 10947 if (name[0] == '@')
ef416fc2 10948 {
10949 /*
10950 * Check group membership...
10951 */
10952
10d09e33 10953 if (cupsdCheckGroup(username, pw, name + 1))
ef416fc2 10954 break;
10955 }
10d09e33 10956 else if (name[0] == '#')
8922323b
MS
10957 {
10958 /*
10959 * Check UUID...
10960 */
10961
10d09e33 10962 if (cupsdCheckGroup(username, pw, name))
8922323b
MS
10963 break;
10964 }
88f9aafc 10965 else if (!_cups_strcasecmp(username, name))
ef416fc2 10966 break;
10967 }
10968
10d09e33 10969 return ((name != NULL) != p->deny_users);
ef416fc2 10970}
10971
10972
10973/*
10974 * 'validate_job()' - Validate printer options and destination.
10975 */
10976
10977static void
10978validate_job(cupsd_client_t *con, /* I - Client connection */
10979 ipp_attribute_t *uri) /* I - Printer URI */
10980{
10981 http_status_t status; /* Policy status */
c7017ecc
MS
10982 ipp_attribute_t *attr, /* Current attribute */
10983 *auth_info; /* auth-info attribute */
5a9febac
MS
10984 ipp_attribute_t *format, /* Document-format attribute */
10985 *name; /* Job-name attribute */
bc44d920 10986 cups_ptype_t dtype; /* Destination type (printer/class) */
ef416fc2 10987 char super[MIME_MAX_SUPER],
10988 /* Supertype of file */
10989 type[MIME_MAX_TYPE];
10990 /* Subtype of file */
10991 cupsd_printer_t *printer; /* Printer */
10992
10993
10994 cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con,
10995 con->http.fd, uri->values[0].string.text);
10996
10997 /*
10998 * OK, see if the client is sending the document compressed - CUPS
10999 * doesn't support compression yet...
11000 */
11001
fa73b229 11002 if ((attr = ippFindAttribute(con->request, "compression",
c7017ecc 11003 IPP_TAG_KEYWORD)) != NULL)
ef416fc2 11004 {
c7017ecc
MS
11005 if (strcmp(attr->values[0].string.text, "none")
11006#ifdef HAVE_LIBZ
11007 && strcmp(attr->values[0].string.text, "gzip")
11008#endif /* HAVE_LIBZ */
11009 )
11010 {
11011 send_ipp_status(con, IPP_ATTRIBUTES,
5a9febac 11012 _("Unsupported 'compression' value \"%s\"."),
c7017ecc
MS
11013 attr->values[0].string.text);
11014 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
11015 "compression", NULL, attr->values[0].string.text);
11016 return;
11017 }
ef416fc2 11018 }
11019
11020 /*
11021 * Is it a format we support?
11022 */
11023
11024 if ((format = ippFindAttribute(con->request, "document-format",
11025 IPP_TAG_MIMETYPE)) != NULL)
11026 {
bc44d920 11027 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
11028 super, type) != 2)
ef416fc2 11029 {
5a9febac
MS
11030 send_ipp_status(con, IPP_BAD_REQUEST,
11031 _("Bad 'document-format' value \"%s\"."),
ef416fc2 11032 format->values[0].string.text);
11033 return;
11034 }
11035
fa73b229 11036 if ((strcmp(super, "application") || strcmp(type, "octet-stream")) &&
11037 !mimeType(MimeDatabase, super, type))
ef416fc2 11038 {
11039 cupsdLogMessage(CUPSD_LOG_INFO,
11040 "Hint: Do you have the raw file printing rules enabled?");
11041 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
5a9febac 11042 _("Unsupported 'document-format' value \"%s\"."),
ef416fc2 11043 format->values[0].string.text);
11044 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
11045 "document-format", NULL, format->values[0].string.text);
11046 return;
11047 }
11048 }
11049
5a9febac
MS
11050 /*
11051 * Is the job-name valid?
11052 */
11053
11054 if ((name = ippFindAttribute(con->request, "job-name", IPP_TAG_ZERO)) != NULL)
11055 {
11056 int bad_name = 0; /* Is the job-name value bad? */
11057
11058 if ((name->value_tag != IPP_TAG_NAME && name->value_tag != IPP_TAG_NAMELANG) ||
11059 name->num_values != 1)
11060 {
11061 bad_name = 1;
11062 }
11063 else
11064 {
11065 /*
11066 * Validate that job-name conforms to RFC 5198 (Network Unicode) and
11067 * IPP Everywhere requirements for "name" values...
11068 */
11069
11070 const unsigned char *nameptr; /* Pointer into "job-name" attribute */
11071
11072 for (nameptr = (unsigned char *)name->values[0].string.text;
11073 *nameptr;
11074 nameptr ++)
11075 {
11076 if (*nameptr < ' ' && *nameptr != '\t')
11077 break;
11078 else if (*nameptr == 0x7f)
11079 break;
11080 else if ((*nameptr & 0xe0) == 0xc0)
11081 {
11082 if ((nameptr[1] & 0xc0) != 0x80)
11083 break;
11084
11085 nameptr ++;
11086 }
11087 else if ((*nameptr & 0xf0) == 0xe0)
11088 {
11089 if ((nameptr[1] & 0xc0) != 0x80 ||
11090 (nameptr[2] & 0xc0) != 0x80)
11091 break;
11092
11093 nameptr += 2;
11094 }
11095 else if ((*nameptr & 0xf8) == 0xf0)
11096 {
11097 if ((nameptr[1] & 0xc0) != 0x80 ||
11098 (nameptr[2] & 0xc0) != 0x80 ||
11099 (nameptr[3] & 0xc0) != 0x80)
11100 break;
11101
11102 nameptr += 3;
11103 }
11104 else if (*nameptr & 0x80)
11105 break;
11106 }
11107
11108 if (*nameptr)
11109 bad_name = 1;
11110 }
11111
11112 if (bad_name)
11113 {
11114 if (StrictConformance)
11115 {
11116 send_ipp_status(con, IPP_ATTRIBUTES,
11117 _("Unsupported 'job-name' value."));
11118 ippCopyAttribute(con->response, name, 0);
11119 return;
11120 }
11121 else
11122 {
11123 cupsdLogMessage(CUPSD_LOG_WARN,
11124 "Unsupported 'job-name' value, deleting from request.");
11125 ippDeleteAttribute(con->request, name);
11126 }
11127 }
11128 }
11129
ef416fc2 11130 /*
11131 * Is the destination valid?
11132 */
11133
f7deaa1a 11134 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 11135 {
11136 /*
11137 * Bad URI...
11138 */
11139
11140 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 11141 _("The printer or class does not exist."));
ef416fc2 11142 return;
11143 }
11144
11145 /*
11146 * Check policy...
11147 */
11148
c7017ecc
MS
11149 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
11150
ef416fc2 11151 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
11152 {
f899b121 11153 send_http_error(con, status, printer);
ef416fc2 11154 return;
11155 }
c7017ecc
MS
11156 else if (printer->num_auth_info_required == 1 &&
11157 !strcmp(printer->auth_info_required[0], "negotiate") &&
11158 !con->username[0])
11159 {
11160 send_http_error(con, HTTP_UNAUTHORIZED, printer);
11161 return;
11162 }
11163#ifdef HAVE_SSL
11164 else if (auth_info && !con->http.tls &&
11165 !httpAddrLocalhost(con->http.hostaddr))
11166 {
11167 /*
11168 * Require encryption of auth-info over non-local connections...
11169 */
11170
11171 send_http_error(con, HTTP_UPGRADE_REQUIRED, printer);
11172 return;
11173 }
11174#endif /* HAVE_SSL */
ef416fc2 11175
11176 /*
11177 * Everything was ok, so return OK status...
11178 */
11179
11180 con->response->request.status.status_code = IPP_OK;
11181}
11182
11183
11184/*
11185 * 'validate_name()' - Make sure the printer name only contains valid chars.
11186 */
11187
bc44d920 11188static int /* O - 0 if name is no good, 1 if good */
ef416fc2 11189validate_name(const char *name) /* I - Name to check */
11190{
11191 const char *ptr; /* Pointer into name */
11192
11193
11194 /*
11195 * Scan the whole name...
11196 */
11197
11198 for (ptr = name; *ptr; ptr ++)
f7deaa1a 11199 if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
ef416fc2 11200 return (0);
11201
11202 /*
11203 * All the characters are good; validate the length, too...
11204 */
11205
11206 return ((ptr - name) < 128);
11207}
11208
11209
11210/*
11211 * 'validate_user()' - Validate the user for the request.
11212 */
11213
11214static int /* O - 1 if permitted, 0 otherwise */
11215validate_user(cupsd_job_t *job, /* I - Job */
11216 cupsd_client_t *con, /* I - Client connection */
11217 const char *owner, /* I - Owner of job/resource */
11218 char *username, /* O - Authenticated username */
11219 int userlen) /* I - Length of username */
11220{
ef416fc2 11221 cupsd_printer_t *printer; /* Printer for job */
11222
11223
11224 cupsdLogMessage(CUPSD_LOG_DEBUG2,
11225 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
11226 "userlen=%d)",
91c84a35
MS
11227 job->id, con ? con->http.fd : 0,
11228 owner ? owner : "(null)", username, userlen);
ef416fc2 11229
11230 /*
11231 * Validate input...
11232 */
11233
11234 if (!con || !owner || !username || userlen <= 0)
11235 return (0);
11236
11237 /*
11238 * Get the best authenticated username that is available.
11239 */
11240
e00b005a 11241 strlcpy(username, get_username(con), userlen);
ef416fc2 11242
11243 /*
11244 * Check the username against the owner...
11245 */
11246
80ca4592 11247 printer = cupsdFindDest(job->dest);
ef416fc2 11248
11249 return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr,
11250 con, owner) == HTTP_OK);
11251}
11252
11253
11254/*
b19ccc9e 11255 * End of "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $".
ef416fc2 11256 */