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