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