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