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