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