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