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