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