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