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