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