]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/ipp.c
Update to CUPS trunk r6695.
[thirdparty/cups.git] / scheduler / ipp.c
1 /*
2 * "$Id: ipp.c 6689 2007-07-18 23:52:15Z mike $"
3 *
4 * IPP routines for the Common UNIX Printing System (CUPS) scheduler.
5 *
6 * Copyright 2007 by Apple Inc.
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.
11 *
12 * These coded instructions, statements, and computer programs are the
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/".
17 *
18 * Contents:
19 *
20 * cupsdProcessIPPRequest() - Process an incoming IPP request.
21 * cupsdTimeoutJob() - Timeout a job waiting on job files.
22 * accept_jobs() - Accept print jobs to a printer.
23 * add_class() - Add a class to the system.
24 * add_file() - Add a file to a job.
25 * add_job() - Add a job to a print queue.
26 * add_job_state_reasons() - Add the "job-state-reasons" attribute based
27 * upon the job and printer state...
28 * add_job_subscriptions() - Add any subcriptions for a job.
29 * add_job_uuid() - Add job-uuid attribute to a job.
30 * add_printer() - Add a printer to the system.
31 * add_printer_state_reasons() - Add the "printer-state-reasons" attribute
32 * based upon the printer state...
33 * add_queued_job_count() - Add the "queued-job-count" attribute for
34 * apply_printer_defaults() - Apply printer default options to a job.
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...
47 * copy_job_attrs() - Copy job attributes.
48 * copy_printer_attrs() - Copy printer attributes.
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.
57 * get_job_attrs() - Get job attributes.
58 * get_jobs() - Get a list of jobs for the specified printer.
59 * get_notifications() - Get events for a subscription.
60 * get_ppd() - Get a named PPD from the local system.
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.
67 * get_username() - Get the username associated with a request.
68 * hold_job() - Hold a print job.
69 * move_job() - Move a job to a new destination.
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.
78 * save_krb5_creds() - Save Kerberos credentials for a job.
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.
84 * set_printer_defaults() - Set printer default options from a request.
85 * start_printer() - Start a printer.
86 * stop_printer() - Stop a printer.
87 * url_encode_attr() - URL-encode a string attribute.
88 * url_encode_string() - URL-encode a string.
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
102 #ifdef HAVE_KRB5_H
103 # include <krb5.h>
104 #endif /* HAVE_KRB5_H */
105
106 #ifdef HAVE_LIBPAPER
107 # include <paper.h>
108 #endif /* HAVE_LIBPAPER */
109
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
117 extern int mbr_user_name_to_uuid(const char* name, uuid_t uu);
118 extern int mbr_group_name_to_uuid(const char* name, uuid_t uu);
119 extern int mbr_check_membership_by_id(uuid_t user, gid_t group, int* ismember);
120 # endif /* HAVE_MEMBERSHIPPRIV_H */
121 #endif /* __APPLE__ */
122
123
124 /*
125 * Local functions...
126 */
127
128 static void accept_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
129 static void add_class(cupsd_client_t *con, ipp_attribute_t *uri);
130 static int add_file(cupsd_client_t *con, cupsd_job_t *job,
131 mime_type_t *filetype, int compression);
132 static cupsd_job_t *add_job(cupsd_client_t *con, cupsd_printer_t *printer,
133 mime_type_t *filetype);
134 static void add_job_state_reasons(cupsd_client_t *con, cupsd_job_t *job);
135 static void add_job_subscriptions(cupsd_client_t *con, cupsd_job_t *job);
136 static void add_job_uuid(cupsd_client_t *con, cupsd_job_t *job);
137 static void add_printer(cupsd_client_t *con, ipp_attribute_t *uri);
138 static void add_printer_state_reasons(cupsd_client_t *con,
139 cupsd_printer_t *p);
140 static void add_queued_job_count(cupsd_client_t *con, cupsd_printer_t *p);
141 static void apply_printer_defaults(cupsd_printer_t *printer,
142 cupsd_job_t *job);
143 static void authenticate_job(cupsd_client_t *con, ipp_attribute_t *uri);
144 static void cancel_all_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
145 static void cancel_job(cupsd_client_t *con, ipp_attribute_t *uri);
146 static void cancel_subscription(cupsd_client_t *con, int id);
147 static int check_quotas(cupsd_client_t *con, cupsd_printer_t *p);
148 static ipp_attribute_t *copy_attribute(ipp_t *to, ipp_attribute_t *attr,
149 int quickcopy);
150 static void copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra,
151 ipp_tag_t group, int quickcopy);
152 static int copy_banner(cupsd_client_t *con, cupsd_job_t *job,
153 const char *name);
154 static int copy_file(const char *from, const char *to);
155 static int copy_model(cupsd_client_t *con, const char *from,
156 const char *to);
157 static void copy_job_attrs(cupsd_client_t *con,
158 cupsd_job_t *job,
159 cups_array_t *ra);
160 static void copy_printer_attrs(cupsd_client_t *con,
161 cupsd_printer_t *printer,
162 cups_array_t *ra);
163 static void copy_subscription_attrs(cupsd_client_t *con,
164 cupsd_subscription_t *sub,
165 cups_array_t *ra);
166 static void create_job(cupsd_client_t *con, ipp_attribute_t *uri);
167 static cups_array_t *create_requested_array(ipp_t *request);
168 static void create_subscription(cupsd_client_t *con, ipp_attribute_t *uri);
169 static void delete_printer(cupsd_client_t *con, ipp_attribute_t *uri);
170 static void get_default(cupsd_client_t *con);
171 static void get_devices(cupsd_client_t *con);
172 static void get_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
173 static void get_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
174 static void get_notifications(cupsd_client_t *con);
175 static void get_ppd(cupsd_client_t *con, ipp_attribute_t *uri);
176 static void get_ppds(cupsd_client_t *con);
177 static void get_printers(cupsd_client_t *con, int type);
178 static void get_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
179 static void get_subscription_attrs(cupsd_client_t *con, int sub_id);
180 static void get_subscriptions(cupsd_client_t *con, ipp_attribute_t *uri);
181 static const char *get_username(cupsd_client_t *con);
182 static void hold_job(cupsd_client_t *con, ipp_attribute_t *uri);
183 static void move_job(cupsd_client_t *con, ipp_attribute_t *uri);
184 static int ppd_parse_line(const char *line, char *option, int olen,
185 char *choice, int clen);
186 static void print_job(cupsd_client_t *con, ipp_attribute_t *uri);
187 static void read_ps_job_ticket(cupsd_client_t *con);
188 static void reject_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
189 static void release_job(cupsd_client_t *con, ipp_attribute_t *uri);
190 static void renew_subscription(cupsd_client_t *con, int sub_id);
191 static void restart_job(cupsd_client_t *con, ipp_attribute_t *uri);
192 static 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)
195 static void save_krb5_creds(cupsd_client_t *con, cupsd_job_t *job);
196 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
197 static void send_document(cupsd_client_t *con, ipp_attribute_t *uri);
198 static void send_http_error(cupsd_client_t *con, http_status_t status,
199 cupsd_printer_t *printer);
200 static 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 ;
206 static void set_default(cupsd_client_t *con, ipp_attribute_t *uri);
207 static void set_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
208 static void set_printer_defaults(cupsd_client_t *con,
209 cupsd_printer_t *printer);
210 static void start_printer(cupsd_client_t *con, ipp_attribute_t *uri);
211 static void stop_printer(cupsd_client_t *con, ipp_attribute_t *uri);
212 static void url_encode_attr(ipp_attribute_t *attr, char *buffer,
213 int bufsize);
214 static char *url_encode_string(const char *s, char *buffer, int bufsize);
215 static int user_allowed(cupsd_printer_t *p, const char *username);
216 static void validate_job(cupsd_client_t *con, ipp_attribute_t *uri);
217 static int validate_name(const char *name);
218 static int validate_user(cupsd_job_t *job, cupsd_client_t *con,
219 const char *owner, char *username,
220 int userlen);
221
222
223 /*
224 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
225 */
226
227 int /* O - 1 on success, 0 on failure */
228 cupsdProcessIPPRequest(
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
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;
256
257 /*
258 * Then validate the request header and required attributes...
259 */
260
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]);
277 }
278 else if (!con->request->attrs)
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;
294 attr;
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
314 if (!attr)
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") &&
326 (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET)
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") &&
335 (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE)
336 language = attr;
337 else
338 language = NULL;
339
340 if ((attr = ippFindAttribute(con->request, "printer-uri",
341 IPP_TAG_URI)) != NULL)
342 uri = attr;
343 else if ((attr = ippFindAttribute(con->request, "job-uri",
344 IPP_TAG_URI)) != NULL)
345 uri = attr;
346 else if (con->request->request.op.operation_id == CUPS_GET_PPD)
347 uri = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME);
348 else
349 uri = NULL;
350
351 if (charset)
352 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
353 "attributes-charset", NULL,
354 charset->values[0].string.text);
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
367 if (!charset || !language ||
368 (!uri &&
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,
404 "Missing printer-uri, job-uri, or ppd-name "
405 "attribute!");
406
407 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
408 "%04X %s Missing printer-uri, job-uri, or ppd-name "
409 "attribute", IPP_BAD_REQUEST, con->http.hostname);
410 }
411
412 cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow...");
413
414 for (attr = con->request->attrs; attr; attr = attr->next)
415 cupsdLogMessage(CUPSD_LOG_DEBUG,
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
447 _cupsStrFree(username->values[0].string.text);
448 username->values[0].string.text = _cupsStrAlloc(RemoteRoot);
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)
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));
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
578 case CUPS_GET_PPD :
579 get_ppd(con, uri);
580 break;
581
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 :
616 get_notifications(con);
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!"),
628 ippOpString(
629 con->request->request.op.operation_id));
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
647 if (cupsdSendHeader(con, HTTP_OK, "application/ipp", AUTH_NONE))
648 {
649 #ifdef CUPSD_USE_CHUNKING
650 /*
651 * Because older versions of CUPS (1.1.17 and older) and some IPP
652 * clients do not implement chunking properly, we cannot use
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
658 if (con->http.version == HTTP_1_1)
659 {
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);
665
666 con->http.data_encoding = HTTP_ENCODE_CHUNKED;
667 }
668 else
669 #endif /* CUPSD_USE_CHUNKING */
670 {
671 size_t length; /* Length of response */
672
673
674 length = ippLength(con->response);
675
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
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);
691
692 con->http.data_encoding = HTTP_ENCODE_LENGTH;
693 con->http.data_remaining = length;
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;
699 }
700
701 cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient,
702 (cupsd_selfunc_t)cupsdWriteClient, con);
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
731 /*
732 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
733 */
734
735 void
736 cupsdTimeoutJob(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
770 /*
771 * 'accept_jobs()' - Accept print jobs to a printer.
772 */
773
774 static void
775 accept_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 */
779 cups_ptype_t dtype; /* Destination type (printer/class) */
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
790 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
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 {
807 send_http_error(con, status, printer);
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)
821 {
822 cupsdSaveAllClasses();
823
824 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" now accepting jobs (\"%s\").",
825 printer->name, get_username(con));
826 }
827 else
828 {
829 cupsdSaveAllPrinters();
830
831 cupsdLogMessage(CUPSD_LOG_INFO,
832 "Printer \"%s\" now accepting jobs (\"%s\").",
833 printer->name, get_username(con));
834 }
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
848 static void
849 add_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 */
862 ipp_attribute_t *attr; /* Printer attribute */
863 int modify; /* Non-zero if we just modified */
864 char newname[IPP_MAX_NAME]; /* New class name */
865 int need_restart_job; /* Need to restart job? */
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
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));
878
879
880 if (strncmp(resource, "/classes/", 9) || strlen(resource) == 9)
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 {
914 send_http_error(con, status, NULL);
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 &&
929 !(pclass->type & CUPS_PRINTER_DISCOVERED))
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 {
956 snprintf(newname, sizeof(newname), "Any%s", resource + 9);
957 cupsdRenamePrinter(pclass, newname);
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 }
969 else if (pclass->type & CUPS_PRINTER_DISCOVERED)
970 {
971 /*
972 * Rename the remote class to "Class"...
973 */
974
975 snprintf(newname, sizeof(newname), "%s@%s", resource + 9, pclass->hostname);
976 cupsdRenamePrinter(pclass, newname);
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
992 need_restart_job = 0;
993
994 if ((attr = ippFindAttribute(con->request, "printer-location",
995 IPP_TAG_TEXT)) != NULL)
996 cupsdSetString(&pclass->location, attr->values[0].string.text);
997
998 if ((attr = ippFindAttribute(con->request, "printer-info",
999 IPP_TAG_TEXT)) != NULL)
1000 cupsdSetString(&pclass->info, attr->values[0].string.text);
1001
1002 if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs",
1003 IPP_TAG_BOOLEAN)) != NULL)
1004 {
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);
1008
1009 pclass->accepting = attr->values[0].boolean;
1010 cupsdAddPrinterHistory(pclass);
1011 }
1012
1013 if ((attr = ippFindAttribute(con->request, "printer-is-shared",
1014 IPP_TAG_BOOLEAN)) != NULL)
1015 {
1016 if (pclass->shared && !attr->values[0].boolean)
1017 cupsdDeregisterPrinter(pclass, 1);
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
1026 if ((attr = ippFindAttribute(con->request, "printer-state",
1027 IPP_TAG_ENUM)) != NULL)
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
1038 cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
1039 pclass->name, attr->values[0].integer, pclass->state);
1040
1041 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1042 cupsdStopPrinter(pclass, 0);
1043 else
1044 {
1045 cupsdSetPrinterState(pclass, (ipp_pstate_t)(attr->values[0].integer), 0);
1046 need_restart_job = 1;
1047 }
1048 }
1049 if ((attr = ippFindAttribute(con->request, "printer-state-message",
1050 IPP_TAG_TEXT)) != NULL)
1051 {
1052 strlcpy(pclass->state_message, attr->values[0].string.text,
1053 sizeof(pclass->state_message));
1054 cupsdAddPrinterHistory(pclass);
1055 }
1056 if ((attr = ippFindAttribute(con->request, "member-uris",
1057 IPP_TAG_URI)) != NULL)
1058 {
1059 /*
1060 * Clear the printer array as needed...
1061 */
1062
1063 need_restart_job = 1;
1064
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
1081 if (!cupsdValidateDest(attr->values[i].string.text, &dtype, &member))
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
1100 set_printer_defaults(con, pclass);
1101
1102 if ((attr = ippFindAttribute(con->request, "auth-info-required",
1103 IPP_TAG_KEYWORD)) != NULL)
1104 cupsdSetAuthInfoRequired(pclass, NULL, attr);
1105
1106 /*
1107 * Update the printer class attributes and return...
1108 */
1109
1110 cupsdSetPrinterAttrs(pclass);
1111 cupsdSaveAllClasses();
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);
1124
1125 job->state->values[0].integer = IPP_JOB_PENDING;
1126 job->state_value = IPP_JOB_PENDING;
1127 }
1128
1129 if (need_restart_job)
1130 cupsdCheckJobs();
1131
1132 cupsdWritePrintcap();
1133
1134 if (modify)
1135 {
1136 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, pclass, NULL,
1137 "Class \"%s\" modified by \"%s\".", pclass->name,
1138 get_username(con));
1139
1140 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" modified by \"%s\".",
1141 pclass->name, get_username(con));
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,
1149 get_username(con));
1150
1151 cupsdLogMessage(CUPSD_LOG_INFO, "New class \"%s\" added by \"%s\".",
1152 pclass->name, get_username(con));
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
1163 static int /* O - 0 on success, -1 on error */
1164 add_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,
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);
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
1196 if (!compressions || !filetypes)
1197 {
1198 cupsdCancelJob(job, 1, IPP_JOB_ABORTED);
1199
1200 if (con)
1201 send_ipp_status(con, IPP_INTERNAL_ERROR,
1202 _("Unable to allocate memory for file types!"));
1203
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 /*
1219 * 'add_job()' - Add a job to a print queue.
1220 */
1221
1222 static cupsd_job_t * /* O - Job object */
1223 add_job(cupsd_client_t *con, /* I - Client connection */
1224 cupsd_printer_t *printer, /* I - Destination printer */
1225 mime_type_t *filetype) /* I - First print file type, if any */
1226 {
1227 http_status_t status; /* Policy status */
1228 ipp_attribute_t *attr, /* Current attribute */
1229 *auth_info; /* auth-info attribute */
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 */
1234 char job_uri[HTTP_MAX_URI]; /* Job URI */
1235 int kbytes; /* Size of print file */
1236 int i; /* Looping var */
1237 int lowerpagerange; /* Page range bound */
1238
1239
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);
1243
1244 /*
1245 * Check remote printing to non-shared printer...
1246 */
1247
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 }
1256
1257 /*
1258 * Check policy...
1259 */
1260
1261 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
1262
1263 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
1264 {
1265 send_http_error(con, status, printer);
1266 return (NULL);
1267 }
1268 else if ((printer->type & CUPS_PRINTER_AUTHENTICATED) &&
1269 !con->username[0] && !auth_info)
1270 {
1271 send_http_error(con, HTTP_UNAUTHORIZED, printer);
1272 return (NULL);
1273 }
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
1282 send_http_error(con, HTTP_UPGRADE_REQUIRED, printer);
1283 return (NULL);
1284 }
1285 #endif /* HAVE_SSL */
1286
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."),
1295 printer->name);
1296 return (NULL);
1297 }
1298
1299 /*
1300 * Validate job template attributes; for now just document-format,
1301 * copies, number-up, and page-ranges...
1302 */
1303
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
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 }
1335
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
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 {
1359 if (attr->values[i].range.lower < lowerpagerange ||
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 }
1368
1369 lowerpagerange = attr->values[i].range.upper + 1;
1370 }
1371 }
1372
1373 /*
1374 * Make sure we aren't over our limit...
1375 */
1376
1377 if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs)
1378 cupsdCleanJobs();
1379
1380 if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs)
1381 {
1382 send_ipp_status(con, IPP_NOT_POSSIBLE,
1383 _("Too many active jobs."));
1384 return (NULL);
1385 }
1386
1387 if ((i = check_quotas(con, printer)) < 0)
1388 {
1389 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
1390 return (NULL);
1391 }
1392 else if (i == 0)
1393 {
1394 send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print."));
1395 return (NULL);
1396 }
1397
1398 /*
1399 * Create the job and set things up...
1400 */
1401
1402 if ((attr = ippFindAttribute(con->request, "job-priority",
1403 IPP_TAG_INTEGER)) != NULL)
1404 priority = attr->values[0].integer;
1405 else
1406 {
1407 if ((val = cupsGetOption("job-priority", printer->num_options,
1408 printer->options)) != NULL)
1409 priority = atoi(val);
1410 else
1411 priority = 50;
1412
1413 ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
1414 priority);
1415 }
1416
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");
1423
1424 if ((job = cupsdAddJob(priority, printer->name)) == NULL)
1425 {
1426 send_ipp_status(con, IPP_INTERNAL_ERROR,
1427 _("Unable to add job for destination \"%s\"!"),
1428 printer->name);
1429 return (NULL);
1430 }
1431
1432 job->dtype = printer->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
1433 CUPS_PRINTER_REMOTE);
1434 job->attrs = con->request;
1435 con->request = ippNewRequest(job->attrs->request.op.operation_id);
1436
1437 add_job_uuid(con, job);
1438 apply_printer_defaults(printer, job);
1439
1440 attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME);
1441
1442 if (con->username[0])
1443 {
1444 cupsdSetString(&job->username, con->username);
1445
1446 if (attr)
1447 cupsdSetString(&attr->values[0].string.text, con->username);
1448 }
1449 else if (attr)
1450 {
1451 cupsdLogMessage(CUPSD_LOG_DEBUG,
1452 "add_job: requesting-user-name=\"%s\"",
1453 attr->values[0].string.text);
1454
1455 cupsdSetString(&job->username, attr->values[0].string.text);
1456 }
1457 else
1458 cupsdSetString(&job->username, "anonymous");
1459
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;
1466 _cupsStrFree(attr->name);
1467 attr->name = _cupsStrAlloc("job-originating-user-name");
1468 }
1469
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
1492 if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name",
1493 IPP_TAG_ZERO)) != NULL)
1494 {
1495 /*
1496 * Request contains a job-originating-host-name attribute; validate it...
1497 */
1498
1499 if (attr->value_tag != IPP_TAG_NAME ||
1500 attr->num_values != 1 ||
1501 strcmp(con->http.hostname, "localhost"))
1502 {
1503 /*
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.
1506 */
1507
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 */
1524
1525 for (i = 0; i < attr->num_values; i ++)
1526 {
1527 _cupsStrFree(attr->values[i].string.text);
1528 attr->values[i].string.text = NULL;
1529 if (attr->values[i].string.charset)
1530 {
1531 _cupsStrFree(attr->values[i].string.charset);
1532 attr->values[i].string.charset = NULL;
1533 }
1534 }
1535
1536 default :
1537 break;
1538 }
1539
1540 /*
1541 * Use the default connection hostname instead...
1542 */
1543
1544 attr->value_tag = IPP_TAG_NAME;
1545 attr->num_values = 1;
1546 attr->values[0].string.text = _cupsStrAlloc(con->http.hostname);
1547 }
1548
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 */
1557
1558 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
1559 "job-originating-host-name", NULL, con->http.hostname);
1560 }
1561
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;
1570
1571 /*
1572 * Add remaining job attributes...
1573 */
1574
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);
1578 job->state_value = (ipp_jstate_t)job->state->values[0].integer;
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);
1585
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);
1592
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)
1597 {
1598 if ((val = cupsGetOption("job-hold-until", printer->num_options,
1599 printer->options)) == NULL)
1600 val = "no-hold";
1601
1602 attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1603 "job-hold-until", NULL, val);
1604 }
1605 if (attr && strcmp(attr->values[0].string.text, "no-hold") &&
1606 !(printer->type & CUPS_PRINTER_REMOTE))
1607 {
1608 /*
1609 * Hold job until specified time...
1610 */
1611
1612 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
1613
1614 job->state->values[0].integer = IPP_JOB_HELD;
1615 job->state_value = IPP_JOB_HELD;
1616 }
1617 else if (job->attrs->request.op.operation_id == IPP_CREATE_JOB)
1618 {
1619 job->hold_until = time(NULL) + 60;
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;
1627 }
1628
1629 if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) ||
1630 Classification)
1631 {
1632 /*
1633 * Add job sheets options...
1634 */
1635
1636 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
1637 IPP_TAG_ZERO)) == NULL)
1638 {
1639 cupsdLogMessage(CUPSD_LOG_DEBUG,
1640 "Adding default job-sheets values \"%s,%s\"...",
1641 printer->job_sheets[0], printer->job_sheets[1]);
1642
1643 attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets",
1644 2, NULL, NULL);
1645 attr->values[0].string.text = _cupsStrAlloc(printer->job_sheets[0]);
1646 attr->values[1].string.text = _cupsStrAlloc(printer->job_sheets[1]);
1647 }
1648
1649 job->job_sheets = attr;
1650
1651 /*
1652 * Enforce classification level if set...
1653 */
1654
1655 if (Classification)
1656 {
1657 cupsdLogMessage(CUPSD_LOG_INFO,
1658 "Classification=\"%s\", ClassifyOverride=%d",
1659 Classification ? Classification : "(null)",
1660 ClassifyOverride);
1661
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 */
1671
1672 cupsdSetString(&attr->values[0].string.text, Classification);
1673
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 */
1688
1689 cupsdSetString(&attr->values[1].string.text, attr->values[0].string.text);
1690
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)))
1721 {
1722 /*
1723 * Force the banner to have the classification on it...
1724 */
1725
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);
1737
1738 if (attr->num_values > 1 &&
1739 strcmp(attr->values[1].string.text, "none"))
1740 cupsdSetString(&(attr->values[1].string.text), Classification);
1741 }
1742
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);
1756 }
1757 }
1758
1759 /*
1760 * See if we need to add the starting sheet...
1761 */
1762
1763 if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
1764 {
1765 cupsdLogMessage(CUPSD_LOG_INFO,
1766 "[Job %d] Adding start banner page \"%s\".",
1767 job->id, attr->values[0].string.text);
1768
1769 kbytes = copy_banner(con, job, attr->values[0].string.text);
1770
1771 cupsdUpdateQuota(printer, job->username, 0, kbytes);
1772 }
1773 }
1774 else if ((attr = ippFindAttribute(job->attrs, "job-sheets",
1775 IPP_TAG_ZERO)) != NULL)
1776 job->sheets = attr;
1777
1778 /*
1779 * Fill in the response info...
1780 */
1781
1782 snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName,
1783 LocalPort, job->id);
1784
1785 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
1786 job_uri);
1787
1788 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
1789
1790 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
1791 job->state_value);
1792 add_job_state_reasons(con, job);
1793
1794 con->response->request.status.status_code = IPP_OK;
1795
1796 /*
1797 * Add any job subscriptions...
1798 */
1799
1800 add_job_subscriptions(con, job);
1801
1802 /*
1803 * Set all but the first two attributes to the job attributes group...
1804 */
1805
1806 for (attr = job->attrs->attrs->next->next; attr; attr = attr->next)
1807 attr->group_tag = IPP_TAG_JOB;
1808
1809 /*
1810 * Fire the "job created" event...
1811 */
1812
1813 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created.");
1814
1815 /*
1816 * Return the new job...
1817 */
1818
1819 return (job);
1820 }
1821
1822
1823 /*
1824 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1825 * upon the job and printer state...
1826 */
1827
1828 static void
1829 add_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 */
1834
1835
1836 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job_state_reasons(%p[%d], %d)",
1837 con, con->http.fd, job ? job->id : 0);
1838
1839 switch (job ? job->state_value : IPP_JOB_CANCELED)
1840 {
1841 case IPP_JOB_PENDING :
1842 dest = cupsdFindDest(job->dest);
1843
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;
1851
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;
1863
1864 case IPP_JOB_PROCESSING :
1865 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1866 "job-state-reasons", NULL, "job-printing");
1867 break;
1868
1869 case IPP_JOB_STOPPED :
1870 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1871 "job-state-reasons", NULL, "job-stopped");
1872 break;
1873
1874 case IPP_JOB_CANCELED :
1875 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1876 "job-state-reasons", NULL, "job-canceled-by-user");
1877 break;
1878
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;
1883
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;
1888 }
1889 }
1890
1891
1892 /*
1893 * 'add_job_subscriptions()' - Add any subcriptions for a job.
1894 */
1895
1896 static void
1897 add_job_subscriptions(
1898 cupsd_client_t *con, /* I - Client connection */
1899 cupsd_job_t *job) /* I - Newly created job */
1900 {
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 */
1911
1912
1913 /*
1914 * Find the first subscription group attribute; return if we have
1915 * none...
1916 */
1917
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;
1923
1924 if (!attr)
1925 return;
1926
1927 /*
1928 * Process the subscription attributes in the request...
1929 */
1930
1931 while (attr)
1932 {
1933 recipient = NULL;
1934 pullmethod = NULL;
1935 user_data = NULL;
1936 interval = 0;
1937 mask = CUPSD_EVENT_NONE;
1938
1939 while (attr && attr->group_tag != IPP_TAG_ZERO)
1940 {
1941 if (!strcmp(attr->name, "notify-recipient-uri") &&
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 }
1977
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;
1996
1997 attr = attr->next;
1998 }
1999
2000 if (!recipient && !pullmethod)
2001 break;
2002
2003 if (mask == CUPSD_EVENT_NONE)
2004 mask = CUPSD_EVENT_JOB_COMPLETED;
2005
2006 sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job, recipient,
2007 0);
2008
2009 sub->interval = interval;
2010
2011 cupsdSetString(&sub->owner, job->username);
2012
2013 if (user_data)
2014 {
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);
2018 }
2019
2020 ippAddSeparator(con->response);
2021 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
2022 "notify-subscription-id", sub->id);
2023
2024 if (attr)
2025 attr = attr->next;
2026 }
2027
2028 cupsdSaveAllSubscriptions();
2029
2030 /*
2031 * Remove all of the subscription attributes from the job request...
2032 */
2033
2034 for (attr = job->attrs->attrs, prev = NULL; attr; attr = next)
2035 {
2036 next = attr->next;
2037
2038 if (attr->group_tag == IPP_TAG_SUBSCRIPTION ||
2039 attr->group_tag == IPP_TAG_ZERO)
2040 {
2041 /*
2042 * Free and remove this attribute...
2043 */
2044
2045 _ippFreeAttr(attr);
2046
2047 if (prev)
2048 prev->next = next;
2049 else
2050 job->attrs->attrs = next;
2051 }
2052 else
2053 prev = attr;
2054 }
2055
2056 job->attrs->last = prev;
2057 job->attrs->current = prev;
2058 }
2059
2060
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
2067 static void
2068 add_job_uuid(cupsd_client_t *con, /* I - Client connection */
2069 cupsd_job_t *job) /* I - Job */
2070 {
2071 char uuid[1024]; /* job-uuid string */
2072 _cups_md5_state_t md5state; /* MD5 state */
2073 unsigned char md5sum[16]; /* MD5 digest/sum */
2074
2075
2076 /*
2077 * First see if the job already has a job-uuid attribute; if so, return...
2078 */
2079
2080 if (ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI))
2081 return;
2082
2083 /*
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...
2088 */
2089
2090 snprintf(uuid, sizeof(uuid), "%s:%s:%d:%d", ServerName, con->servername,
2091 con->serverport, job->id);
2092
2093 _cupsMD5Init(&md5state);
2094 _cupsMD5Append(&md5state, (unsigned char *)uuid, strlen(uuid));
2095 _cupsMD5Finish(&md5state, md5sum);
2096
2097 /*
2098 * Format the UUID URI using the MD5 sum and job ID.
2099 */
2100
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]);
2108
2109 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL, uuid);
2110 }
2111
2112
2113 /*
2114 * 'add_printer()' - Add a printer to the system.
2115 */
2116
2117 static void
2118 add_printer(cupsd_client_t *con, /* I - Client connection */
2119 ipp_attribute_t *uri) /* I - URI of printer */
2120 {
2121 http_status_t status; /* Policy status */
2122 int i; /* Looping var */
2123 char scheme[HTTP_MAX_URI], /* Method portion of URI */
2124 username[HTTP_MAX_URI], /* Username portion of URI */
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 */
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? */
2137 int set_device_uri, /* Did we set the device URI? */
2138 set_port_monitor; /* Did we set the port monitor? */
2139
2140
2141 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con,
2142 con->http.fd, uri->values[0].string.text);
2143
2144 /*
2145 * Do we have a valid URI?
2146 */
2147
2148 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
2149 sizeof(scheme), username, sizeof(username), host,
2150 sizeof(host), &port, resource, sizeof(resource));
2151
2152 if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10)
2153 {
2154 /*
2155 * No, return an error...
2156 */
2157
2158 send_ipp_status(con, IPP_BAD_REQUEST,
2159 _("The printer-uri must be of the form "
2160 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2161 return;
2162 }
2163
2164 /*
2165 * Do we have a valid printer name?
2166 */
2167
2168 if (!validate_name(resource + 10))
2169 {
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;
2178 }
2179
2180 /*
2181 * Check policy...
2182 */
2183
2184 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
2185 {
2186 send_http_error(con, status, NULL);
2187 return;
2188 }
2189
2190 /*
2191 * See if the printer already exists; if not, create a new printer...
2192 */
2193
2194 if ((printer = cupsdFindPrinter(resource + 10)) == NULL)
2195 {
2196 /*
2197 * Printer doesn't exist; see if we have a class of the same name...
2198 */
2199
2200 if ((printer = cupsdFindClass(resource + 10)) != NULL &&
2201 !(printer->type & CUPS_PRINTER_DISCOVERED))
2202 {
2203 /*
2204 * Yes, return an error...
2205 */
2206
2207 send_ipp_status(con, IPP_NOT_POSSIBLE,
2208 _("A class named \"%s\" already exists!"),
2209 resource + 10);
2210 return;
2211 }
2212
2213 /*
2214 * No, add the printer...
2215 */
2216
2217 printer = cupsdAddPrinter(resource + 10);
2218 modify = 0;
2219 }
2220 else if (printer->type & CUPS_PRINTER_IMPLICIT)
2221 {
2222 /*
2223 * Rename the implicit printer to "AnyPrinter" or delete it...
2224 */
2225
2226 if (ImplicitAnyClasses)
2227 {
2228 snprintf(newname, sizeof(newname), "Any%s", resource + 10);
2229 cupsdRenamePrinter(printer, newname);
2230 }
2231 else
2232 cupsdDeletePrinter(printer, 1);
2233
2234 /*
2235 * Add the printer as a new local printer...
2236 */
2237
2238 printer = cupsdAddPrinter(resource + 10);
2239 modify = 0;
2240 }
2241 else if (printer->type & CUPS_PRINTER_DISCOVERED)
2242 {
2243 /*
2244 * Rename the remote printer to "Printer@server"...
2245 */
2246
2247 snprintf(newname, sizeof(newname), "%s@%s", resource + 10,
2248 printer->hostname);
2249 cupsdRenamePrinter(printer, newname);
2250
2251 /*
2252 * Add the printer as a new local printer...
2253 */
2254
2255 printer = cupsdAddPrinter(resource + 10);
2256 modify = 0;
2257 }
2258 else
2259 modify = 1;
2260
2261 /*
2262 * Look for attributes and copy them over as needed...
2263 */
2264
2265 need_restart_job = 0;
2266
2267 if ((attr = ippFindAttribute(con->request, "printer-location",
2268 IPP_TAG_TEXT)) != NULL)
2269 cupsdSetString(&printer->location, attr->values[0].string.text);
2270
2271 if ((attr = ippFindAttribute(con->request, "printer-info",
2272 IPP_TAG_TEXT)) != NULL)
2273 cupsdSetString(&printer->info, attr->values[0].string.text);
2274
2275 set_device_uri = 0;
2276
2277 if ((attr = ippFindAttribute(con->request, "device-uri",
2278 IPP_TAG_URI)) != NULL)
2279 {
2280 /*
2281 * Do we have a valid device URI?
2282 */
2283
2284 need_restart_job = 1;
2285
2286 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text, scheme,
2287 sizeof(scheme), username, sizeof(username), host,
2288 sizeof(host), &port, resource, sizeof(resource));
2289
2290 if (!strcmp(scheme, "file"))
2291 {
2292 /*
2293 * See if the administrator has enabled file devices...
2294 */
2295
2296 if (!FileDevice && strcmp(resource, "/dev/null"))
2297 {
2298 /*
2299 * File devices are disabled and the URL is not file:/dev/null...
2300 */
2301
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);
2307 return;
2308 }
2309 }
2310 else
2311 {
2312 /*
2313 * See if the backend exists and is executable...
2314 */
2315
2316 snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, scheme);
2317 if (access(srcfile, X_OK))
2318 {
2319 /*
2320 * Could not find device in list!
2321 */
2322
2323 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"!"),
2324 attr->values[0].string.text);
2325 return;
2326 }
2327 }
2328
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)));
2336
2337 cupsdSetString(&printer->device_uri, attr->values[0].string.text);
2338 set_device_uri = 1;
2339 }
2340
2341 set_port_monitor = 0;
2342
2343 if ((attr = ippFindAttribute(con->request, "port-monitor",
2344 IPP_TAG_NAME)) != NULL)
2345 {
2346 ipp_attribute_t *supported; /* port-monitor-supported attribute */
2347
2348
2349 need_restart_job = 1;
2350
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;
2357
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 }
2364
2365 cupsdLogMessage(CUPSD_LOG_INFO,
2366 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2367 printer->name, attr->values[0].string.text,
2368 printer->port_monitor ? printer->port_monitor : "none");
2369
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);
2374
2375 set_port_monitor = 1;
2376 }
2377
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);
2384
2385 printer->accepting = attr->values[0].boolean;
2386 cupsdAddPrinterHistory(printer);
2387 }
2388
2389 if ((attr = ippFindAttribute(con->request, "printer-is-shared",
2390 IPP_TAG_BOOLEAN)) != NULL)
2391 {
2392 if (printer->shared && !attr->values[0].boolean)
2393 cupsdDeregisterPrinter(printer, 1);
2394
2395 cupsdLogMessage(CUPSD_LOG_INFO,
2396 "Setting %s printer-is-shared to %d (was %d.)",
2397 printer->name, attr->values[0].boolean, printer->shared);
2398
2399 printer->shared = attr->values[0].boolean;
2400 }
2401
2402 if ((attr = ippFindAttribute(con->request, "printer-state",
2403 IPP_TAG_ENUM)) != NULL)
2404 {
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 }
2412
2413 cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
2414 printer->name, attr->values[0].integer, printer->state);
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);
2430 }
2431
2432 set_printer_defaults(con, printer);
2433
2434 if ((attr = ippFindAttribute(con->request, "auth-info-required",
2435 IPP_TAG_KEYWORD)) != NULL)
2436 cupsdSetAuthInfoRequired(printer, NULL, attr);
2437
2438 /*
2439 * See if we have all required attributes...
2440 */
2441
2442 if (!printer->device_uri)
2443 cupsdSetString(&printer->device_uri, "file:///dev/null");
2444
2445 /*
2446 * See if we have an interface script or PPD file attached to the request...
2447 */
2448
2449 if (con->filename)
2450 {
2451 need_restart_job = 1;
2452
2453 strlcpy(srcfile, con->filename, sizeof(srcfile));
2454
2455 if ((fp = cupsFileOpen(srcfile, "rb")))
2456 {
2457 /*
2458 * Yes; get the first line from it...
2459 */
2460
2461 line[0] = '\0';
2462 cupsFileGets(fp, line, sizeof(line));
2463 cupsFileClose(fp);
2464
2465 /*
2466 * Then see what kind of file it is...
2467 */
2468
2469 snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
2470 printer->name);
2471
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 */
2478
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 */
2487
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 }
2502
2503 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2504 printer->name);
2505
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 */
2512
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 */
2533
2534 unlink(dstfile);
2535 }
2536 }
2537 }
2538 else if ((attr = ippFindAttribute(con->request, "ppd-name",
2539 IPP_TAG_NAME)) != NULL)
2540 {
2541 need_restart_job = 1;
2542
2543 if (!strcmp(attr->values[0].string.text, "raw"))
2544 {
2545 /*
2546 * Raw driver, remove any existing PPD or interface script files.
2547 */
2548
2549 snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
2550 printer->name);
2551 unlink(dstfile);
2552
2553 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2554 printer->name);
2555 unlink(dstfile);
2556 }
2557 else
2558 {
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 }
2581 }
2582 }
2583
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,
2611 printer->port_monitor ? printer->port_monitor
2612 : "none");
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
2626 /*
2627 * Update the printer attributes and return...
2628 */
2629
2630 cupsdSetPrinterAttrs(printer);
2631 cupsdSaveAllPrinters();
2632
2633 if (need_restart_job && printer->job)
2634 {
2635 cupsd_job_t *job;
2636
2637 /*
2638 * Stop the current job and then restart it below...
2639 */
2640
2641 job = (cupsd_job_t *)printer->job;
2642
2643 cupsdStopJob(job, 1);
2644
2645 job->state->values[0].integer = IPP_JOB_PENDING;
2646 job->state_value = IPP_JOB_PENDING;
2647 }
2648
2649 if (need_restart_job)
2650 cupsdCheckJobs();
2651
2652 cupsdWritePrintcap();
2653
2654 if (modify)
2655 {
2656 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL,
2657 "Printer \"%s\" modified by \"%s\".", printer->name,
2658 get_username(con));
2659
2660 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".",
2661 printer->name, get_username(con));
2662 }
2663 else
2664 {
2665 cupsdAddPrinterHistory(printer);
2666
2667 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, printer, NULL,
2668 "New printer \"%s\" added by \"%s\".", printer->name,
2669 get_username(con));
2670
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;
2676 }
2677
2678
2679 /*
2680 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2681 * based upon the printer state...
2682 */
2683
2684 static void
2685 add_printer_state_reasons(
2686 cupsd_client_t *con, /* I - Client connection */
2687 cupsd_printer_t *p) /* I - Printer info */
2688 {
2689 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2690 "add_printer_state_reasons(%p[%d], %p[%s])",
2691 con, con->http.fd, p, p->name);
2692
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 }
2702
2703
2704 /*
2705 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2706 * the specified printer or class.
2707 */
2708
2709 static void
2710 add_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 */
2715
2716
2717 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])",
2718 con, con->http.fd, p, p->name);
2719
2720 count = cupsdGetPrinterJobCount(p->name);
2721
2722 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2723 "queued-job-count", count);
2724 }
2725
2726
2727 /*
2728 * 'apply_printer_defaults()' - Apply printer default options to a job.
2729 */
2730
2731 static void
2732 apply_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 */
2739 *option; /* Current option */
2740
2741
2742 /*
2743 * Collect all of the default options and add the missing ones to the
2744 * job object...
2745 */
2746
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 }
2755
2756 /*
2757 * Encode these options as attributes in the job object...
2758 */
2759
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
2769 static void
2770 authenticate_job(cupsd_client_t *con, /* I - Client connection */
2771 ipp_attribute_t *uri) /* I - Job URI */
2772 {
2773 ipp_attribute_t *attr, /* job-id attribute */
2774 *auth_info; /* auth-info attribute */
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 */
2786
2787
2788 cupsdLogMessage(CUPSD_LOG_DEBUG2, "authenticate_job(%p[%d], %s)",
2789 con, con->http.fd, uri->values[0].string.text);
2790
2791 /*
2792 * Start with "everything is OK" status...
2793 */
2794
2795 con->response->request.status.status_code = IPP_OK;
2796
2797 /*
2798 * See if we have a job URI or a printer URI...
2799 */
2800
2801 if (!strcmp(uri->name, "printer-uri"))
2802 {
2803 /*
2804 * Got a printer URI; see if we also have a job-id attribute...
2805 */
2806
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 */
2822
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));
2826
2827 if (strncmp(resource, "/jobs/", 6))
2828 {
2829 /*
2830 * Not a valid URI!
2831 */
2832
2833 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri attribute \"%s\"!"),
2834 uri->values[0].string.text);
2835 return;
2836 }
2837
2838 jobid = atoi(resource + 6);
2839 }
2840
2841 /*
2842 * See if the job exists...
2843 */
2844
2845 if ((job = cupsdFindJob(jobid)) == NULL)
2846 {
2847 /*
2848 * Nope - return a "not found" error...
2849 */
2850
2851 send_ipp_status(con, IPP_NOT_FOUND,
2852 _("Job #%d does not exist!"), jobid);
2853 return;
2854 }
2855
2856 /*
2857 * See if the job has been completed...
2858 */
2859
2860 if (job->state_value != IPP_JOB_HELD)
2861 {
2862 /*
2863 * Return a "not-possible" error...
2864 */
2865
2866 send_ipp_status(con, IPP_NOT_POSSIBLE,
2867 _("Job #%d is not held for authentication!"),
2868 jobid);
2869 return;
2870 }
2871
2872 /*
2873 * See if we have already authenticated...
2874 */
2875
2876 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
2877
2878 if (!con->username[0] && !auth_info)
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 {
2891 send_http_error(con, HTTP_UNAUTHORIZED, NULL);
2892 return;
2893 }
2894
2895 /*
2896 * Save the authentication information for this job...
2897 */
2898
2899 save_auth_info(con, job, auth_info);
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");
2913 }
2914
2915 /*
2916 * Release the job and return...
2917 */
2918
2919 cupsdReleaseJob(job);
2920
2921 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Authenticated by \"%s\".", jobid,
2922 con->username);
2923 }
2924
2925
2926 /*
2927 * 'cancel_all_jobs()' - Cancel all print jobs.
2928 */
2929
2930 static void
2931 cancel_all_jobs(cupsd_client_t *con, /* I - Client connection */
2932 ipp_attribute_t *uri) /* I - Job or Printer URI */
2933 {
2934 http_status_t status; /* Policy status */
2935 cups_ptype_t dtype; /* Destination type */
2936 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
2937 userpass[HTTP_MAX_URI], /* Username portion of URI */
2938 hostname[HTTP_MAX_URI], /* Host portion of URI */
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 */
2945
2946
2947 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_all_jobs(%p[%d], %s)", con,
2948 con->http.fd, uri->values[0].string.text);
2949
2950 /*
2951 * See if we have a printer URI...
2952 */
2953
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 }
2960
2961 /*
2962 * Get the username (if any) for the jobs we want to cancel (only if
2963 * "my-jobs" is specified...
2964 */
2965
2966 if ((attr = ippFindAttribute(con->request, "my-jobs",
2967 IPP_TAG_BOOLEAN)) != NULL &&
2968 attr->values[0].boolean)
2969 {
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 }
2979 }
2980 else
2981 username = NULL;
2982
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;
2992
2993 /*
2994 * And if the destination is valid...
2995 */
2996
2997 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
2998 {
2999 /*
3000 * Bad URI?
3001 */
3002
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
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 }
3015
3016 /*
3017 * Check policy...
3018 */
3019
3020 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
3021 {
3022 send_http_error(con, status, NULL);
3023 return;
3024 }
3025
3026 /*
3027 * Cancel all jobs on all printers...
3028 */
3029
3030 cupsdCancelJobs(NULL, username, purge);
3031
3032 cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".",
3033 purge ? "purged" : "canceled", get_username(con));
3034 }
3035 else
3036 {
3037 /*
3038 * Check policy...
3039 */
3040
3041 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
3042 NULL)) != HTTP_OK)
3043 {
3044 send_http_error(con, status, printer);
3045 return;
3046 }
3047
3048 /*
3049 * Cancel all of the jobs on the named printer...
3050 */
3051
3052 cupsdCancelJobs(printer->name, username, purge);
3053
3054 cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".",
3055 printer->name, purge ? "purged" : "canceled",
3056 get_username(con));
3057 }
3058
3059 con->response->request.status.status_code = IPP_OK;
3060 }
3061
3062
3063 /*
3064 * 'cancel_job()' - Cancel a print job.
3065 */
3066
3067 static void
3068 cancel_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 */
3073 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
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 */
3079 cups_ptype_t dtype; /* Destination type (printer/class) */
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
3086 /*
3087 * See if we have a job URI or a printer URI...
3088 */
3089
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)
3105 {
3106 /*
3107 * Find the current job on the specified printer...
3108 */
3109
3110 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
3111 {
3112 /*
3113 * Bad URI...
3114 */
3115
3116 send_ipp_status(con, IPP_NOT_FOUND,
3117 _("The printer or class was not found."));
3118 return;
3119 }
3120
3121 /*
3122 * See if the printer is currently printing a job...
3123 */
3124
3125 if (printer->job)
3126 jobid = ((cupsd_job_t *)printer->job)->id;
3127 else
3128 {
3129 /*
3130 * No, see if there are any pending jobs...
3131 */
3132
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 &&
3137 !strcasecmp(job->dest, printer->name))
3138 break;
3139
3140 if (job)
3141 jobid = job->id;
3142 else
3143 {
3144 send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s!"),
3145 printer->name);
3146 return;
3147 }
3148 }
3149 }
3150 }
3151 else
3152 {
3153 /*
3154 * Got a job URI; parse it to get the job ID...
3155 */
3156
3157 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
3158 sizeof(scheme), username, sizeof(username), host,
3159 sizeof(host), &port, resource, sizeof(resource));
3160
3161 if (strncmp(resource, "/jobs/", 6))
3162 {
3163 /*
3164 * Not a valid URI!
3165 */
3166
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 {
3196 send_http_error(con, HTTP_UNAUTHORIZED, NULL);
3197 return;
3198 }
3199
3200 /*
3201 * See if the job is already completed, canceled, or aborted; if so,
3202 * we can't cancel...
3203 */
3204
3205 if (job->state_value >= IPP_JOB_CANCELED)
3206 {
3207 switch (job->state_value)
3208 {
3209 case IPP_JOB_CANCELED :
3210 send_ipp_status(con, IPP_NOT_POSSIBLE,
3211 _("Job #%d is already canceled - can\'t cancel."),
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
3228 return;
3229 }
3230
3231 /*
3232 * Cancel the job and return...
3233 */
3234
3235 cupsdCancelJob(job, 0, IPP_JOB_CANCELED);
3236 cupsdCheckJobs();
3237
3238 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Canceled by \"%s\".", jobid,
3239 username);
3240
3241 con->response->request.status.status_code = IPP_OK;
3242 }
3243
3244
3245 /*
3246 * 'cancel_subscription()' - Cancel a subscription.
3247 */
3248
3249 static void
3250 cancel_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 */
3256
3257
3258 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3259 "cancel_subscription(con=%p[%d], sub_id=%d)",
3260 con, con->http.fd, sub_id);
3261
3262 /*
3263 * Is the subscription ID valid?
3264 */
3265
3266 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
3267 {
3268 /*
3269 * Bad subscription ID...
3270 */
3271
3272 send_ipp_status(con, IPP_NOT_FOUND,
3273 _("notify-subscription-id %d no good!"), sub_id);
3274 return;
3275 }
3276
3277 /*
3278 * Check policy...
3279 */
3280
3281 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
3282 DefaultPolicyPtr,
3283 con, sub->owner)) != HTTP_OK)
3284 {
3285 send_http_error(con, status, sub->dest);
3286 return;
3287 }
3288
3289 /*
3290 * Cancel the subscription...
3291 */
3292
3293 cupsdDeleteSubscription(sub, 1);
3294
3295 con->response->request.status.status_code = IPP_OK;
3296 }
3297
3298
3299 /*
3300 * 'check_quotas()' - Check quotas for a printer and user.
3301 */
3302
3303 static int /* O - 1 if OK, 0 if not */
3304 check_quotas(cupsd_client_t *con, /* I - Client connection */
3305 cupsd_printer_t *p) /* I - Printer or class */
3306 {
3307 int i; /* Looping var */
3308 char username[33]; /* Username */
3309 cupsd_quota_t *q; /* Quota data */
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
3326 struct passwd *pw; /* User password data */
3327 #endif /* HAVE_MBR_UID_TO_UUID */
3328
3329
3330 cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])",
3331 con, con->http.fd, p, p->name);
3332
3333 /*
3334 * Check input...
3335 */
3336
3337 if (!con || !p)
3338 return (0);
3339
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)
3351 {
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 }
3376 }
3377
3378 /*
3379 * Check against users...
3380 */
3381
3382 if (p->num_users == 0 && p->k_limit == 0 && p->page_limit == 0)
3383 return (1);
3384
3385 if (p->num_users)
3386 {
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
3412 pw = getpwnam(username);
3413 endpwent();
3414 #endif /* HAVE_MBR_UID_TO_UUID */
3415
3416 for (i = 0; i < p->num_users; i ++)
3417 if (p->users[i][0] == '@')
3418 {
3419 /*
3420 * Check group membership...
3421 */
3422
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
3462 if (cupsdCheckGroup(username, pw, p->users[i] + 1))
3463 break;
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 }
3498 }
3499 #else
3500 else if (!strcasecmp(username, p->users[i]))
3501 break;
3502 #endif /* HAVE_MBR_UID_TO_UUID */
3503
3504 if ((i < p->num_users) == p->deny_users)
3505 {
3506 cupsdLogMessage(CUPSD_LOG_INFO,
3507 "Denying user \"%s\" access to printer \"%s\"...",
3508 username, p->name);
3509 return (0);
3510 }
3511 }
3512
3513 /*
3514 * Check quotas...
3515 */
3516
3517 #ifdef __APPLE__
3518 if (AppleQuotas && (q = cupsdFindQuota(p, username)) != NULL)
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 */
3540 return (-1);
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 */
3549 return (-1);
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 */
3558 return (-1);
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 */
3567 return (-1);
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__ */
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);
3584 return (-1);
3585 }
3586
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);
3592 return (-1);
3593 }
3594 }
3595
3596 /*
3597 * If we have gotten this far, we're done!
3598 */
3599
3600 return (1);
3601 }
3602
3603
3604 /*
3605 * 'copy_attribute()' - Copy a single attribute.
3606 */
3607
3608 static ipp_attribute_t * /* O - New attribute */
3609 copy_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? */
3613 {
3614 int i; /* Looping var */
3615 ipp_attribute_t *toattr; /* Destination attribute */
3616
3617
3618 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3619 "copy_attribute(%p, %p[%s,%x,%x])", to, attr,
3620 attr->name ? attr->name : "(null)", attr->group_tag,
3621 attr->value_tag);
3622
3623 switch (attr->value_tag & ~IPP_TAG_COPY)
3624 {
3625 case IPP_TAG_ZERO :
3626 toattr = ippAddSeparator(to);
3627 break;
3628
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);
3633
3634 for (i = 0; i < attr->num_values; i ++)
3635 toattr->values[i].integer = attr->values[i].integer;
3636 break;
3637
3638 case IPP_TAG_BOOLEAN :
3639 toattr = ippAddBooleans(to, attr->group_tag, attr->name,
3640 attr->num_values, NULL);
3641
3642 for (i = 0; i < attr->num_values; i ++)
3643 toattr->values[i].boolean = attr->values[i].boolean;
3644 break;
3645
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);
3658
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 ++)
3667 toattr->values[i].string.text =
3668 _cupsStrAlloc(attr->values[i].string.text);
3669 }
3670 break;
3671
3672 case IPP_TAG_DATE :
3673 toattr = ippAddDate(to, attr->group_tag, attr->name,
3674 attr->values[0].date);
3675 break;
3676
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);
3681
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;
3689
3690 case IPP_TAG_RANGE :
3691 toattr = ippAddRanges(to, attr->group_tag, attr->name,
3692 attr->num_values, NULL, NULL);
3693
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;
3700
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);
3706
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 =
3721 _cupsStrAlloc(attr->values[i].string.charset);
3722 else
3723 toattr->values[i].string.charset =
3724 toattr->values[0].string.charset;
3725
3726 toattr->values[i].string.text =
3727 _cupsStrAlloc(attr->values[i].string.text);
3728 }
3729 }
3730 break;
3731
3732 case IPP_TAG_BEGIN_COLLECTION :
3733 toattr = ippAddCollections(to, attr->group_tag, attr->name,
3734 attr->num_values, NULL);
3735
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 }
3742 break;
3743
3744 default :
3745 toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag,
3746 attr->name, attr->num_values, NULL);
3747
3748 for (i = 0; i < attr->num_values; i ++)
3749 {
3750 toattr->values[i].unknown.length = attr->values[i].unknown.length;
3751
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 }
3765
3766 return (toattr);
3767 }
3768
3769
3770 /*
3771 * 'copy_attrs()' - Copy attributes from one request to another.
3772 */
3773
3774 static void
3775 copy_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 */
3782
3783
3784 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3785 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3786 to, from, ra, group, quickcopy);
3787
3788 if (!to || !from)
3789 return;
3790
3791 for (fromattr = from->attrs; fromattr; fromattr = fromattr->next)
3792 {
3793 /*
3794 * Filter attributes as needed...
3795 */
3796
3797 if ((group != IPP_TAG_ZERO && fromattr->group_tag != group &&
3798 fromattr->group_tag != IPP_TAG_ZERO) || !fromattr->name)
3799 continue;
3800
3801 if (!ra || cupsArrayFind(ra, fromattr->name))
3802 copy_attribute(to, fromattr, quickcopy);
3803 }
3804 }
3805
3806
3807 /*
3808 * 'copy_banner()' - Copy a banner file to the requests directory for the
3809 * specified job.
3810 */
3811
3812 static int /* O - Size of banner file in kbytes */
3813 copy_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 */
3827
3828
3829 cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_banner(%p[%d], %p[%d], %s)",
3830 con, con ? con->http.fd : -1, job, job->id,
3831 name ? name : "(null)");
3832
3833 /*
3834 * Find the banner; return if not found or "none"...
3835 */
3836
3837 if (!name || !strcmp(name, "none") ||
3838 (banner = cupsdFindBanner(name)) == NULL)
3839 return (0);
3840
3841 /*
3842 * Open the banner and job files...
3843 */
3844
3845 if (add_file(con, job, banner->filetype, 0))
3846 return (0);
3847
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);
3861
3862 /*
3863 * Try the localized banner file under the subdirectory...
3864 */
3865
3866 strlcpy(attrname, job->attrs->attrs->next->values[0].string.text,
3867 sizeof(attrname));
3868 if (strlen(attrname) > 2 && attrname[2] == '-')
3869 {
3870 /*
3871 * Convert ll-cc to ll_CC...
3872 */
3873
3874 attrname[2] = '_';
3875 attrname[3] = toupper(attrname[3] & 255);
3876 attrname[4] = toupper(attrname[4] & 255);
3877 }
3878
3879 snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir,
3880 attrname, name);
3881
3882 if (access(filename, 0) && strlen(attrname) > 2)
3883 {
3884 /*
3885 * Wasn't able to find "ll_CC" locale file; try the non-national
3886 * localization banner directory.
3887 */
3888
3889 attrname[2] = '\0';
3890
3891 snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir,
3892 attrname, name);
3893 }
3894
3895 if (access(filename, 0))
3896 {
3897 /*
3898 * Use the non-localized banner file.
3899 */
3900
3901 snprintf(filename, sizeof(filename), "%s/banners/%s", DataDir, name);
3902 }
3903
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);
3913 }
3914
3915 /*
3916 * Parse the file to the end...
3917 */
3918
3919 while ((ch = cupsFileGetChar(in)) != EOF)
3920 if (ch == '{')
3921 {
3922 /*
3923 * Get an attribute name...
3924 */
3925
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;
3933
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 }
3945
3946 /*
3947 * See if it is defined...
3948 */
3949
3950 if (attrname[0] == '?')
3951 s = attrname + 1;
3952 else
3953 s = attrname;
3954
3955 if (!strcmp(s, "printer-name"))
3956 {
3957 cupsFilePuts(out, job->dest);
3958 continue;
3959 }
3960 else if ((attr = ippFindAttribute(job->attrs, s, IPP_TAG_ZERO)) == NULL)
3961 {
3962 /*
3963 * See if we have a leading question mark...
3964 */
3965
3966 if (attrname[0] != '?')
3967 {
3968 /*
3969 * Nope, write to file as-is; probably a PostScript procedure...
3970 */
3971
3972 cupsFilePrintf(out, "{%s}", attrname);
3973 }
3974
3975 continue;
3976 }
3977
3978 /*
3979 * Output value(s)...
3980 */
3981
3982 for (i = 0; i < attr->num_values; i ++)
3983 {
3984 if (i)
3985 cupsFilePutChar(out, ',');
3986
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;
3996
3997 case IPP_TAG_BOOLEAN :
3998 cupsFilePrintf(out, "%d", attr->values[i].boolean);
3999 break;
4000
4001 case IPP_TAG_NOVALUE :
4002 cupsFilePuts(out, "novalue");
4003 break;
4004
4005 case IPP_TAG_RANGE :
4006 cupsFilePrintf(out, "%d-%d", attr->values[i].range.lower,
4007 attr->values[i].range.upper);
4008 break;
4009
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;
4016
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 */
4029
4030 const char *p;
4031
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;
4048
4049 default :
4050 break; /* anti-compiler-warning-code */
4051 }
4052 }
4053 }
4054 else if (ch == '\\') /* Quoted char */
4055 {
4056 ch = cupsFileGetChar(in);
4057
4058 if (ch != '{') /* Only do special handling for \{ */
4059 cupsFilePutChar(out, '\\');
4060
4061 cupsFilePutChar(out, ch);
4062 }
4063 else
4064 cupsFilePutChar(out, ch);
4065
4066 cupsFileClose(in);
4067
4068 kbytes = (cupsFileTell(out) + 1023) / 1024;
4069
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);
4077 }
4078
4079
4080 /*
4081 * 'copy_file()' - Copy a PPD file or interface script...
4082 */
4083
4084 static int /* O - 0 = success, -1 = error */
4085 copy_file(const char *from, /* I - Source file */
4086 const char *to) /* I - Destination file */
4087 {
4088 cups_file_t *src, /* Source file */
4089 *dst; /* Destination file */
4090 int bytes; /* Bytes to read/write */
4091 char buffer[2048]; /* Copy buffer */
4092
4093
4094 cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_file(\"%s\", \"%s\")", from, to);
4095
4096 /*
4097 * Open the source and destination file for a copy...
4098 */
4099
4100 if ((src = cupsFileOpen(from, "rb")) == NULL)
4101 return (-1);
4102
4103 if ((dst = cupsFileOpen(to, "wb")) == NULL)
4104 {
4105 cupsFileClose(src);
4106 return (-1);
4107 }
4108
4109 /*
4110 * Copy the source file to the destination...
4111 */
4112
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 }
4120
4121 /*
4122 * Close both files and return...
4123 */
4124
4125 cupsFileClose(src);
4126
4127 return (cupsFileClose(dst));
4128 }
4129
4130
4131 /*
4132 * 'copy_model()' - Copy a PPD model file, substituting default values
4133 * as needed...
4134 */
4135
4136 static int /* O - 0 = success, -1 = error */
4137 copy_model(cupsd_client_t *con, /* I - Client connection */
4138 const char *from, /* I - Source file */
4139 const char *to) /* I - Destination file */
4140 {
4141 fd_set input; /* select() input set */
4142 struct timeval timeout; /* select() timeout */
4143 int maxfd; /* Max file descriptor for select() */
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 */
4152 ppd_file_t *ppd; /* PPD file */
4153 int bytes, /* Bytes from pipe */
4154 total; /* Total bytes from pipe */
4155 char buffer[2048]; /* Copy buffer */
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 */
4160 cups_option_t *defaults; /* Default options */
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 */
4169
4170
4171 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4172 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4173 con, from, to);
4174
4175 /*
4176 * Run cups-driverd to get the PPD file...
4177 */
4178
4179 argv[0] = "cups-driverd";
4180 argv[1] = "cat";
4181 argv[2] = (char *)from;
4182 argv[3] = NULL;
4183
4184 cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
4185
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
4194 cupsdLogMessage(CUPSD_LOG_DEBUG,
4195 "copy_model: Running \"cups-driverd cat %s\"...", from);
4196
4197 if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1],
4198 -1, -1, 0, &temppid))
4199 {
4200 close(tempfd);
4201 unlink(tempfile);
4202 return (-1);
4203 }
4204
4205 close(temppipe[1]);
4206
4207 /*
4208 * Wait up to 30 seconds for the PPD file to be copied...
4209 */
4210
4211 total = 0;
4212
4213 if (temppipe[0] > CGIPipes[0])
4214 maxfd = temppipe[0] + 1;
4215 else
4216 maxfd = CGIPipes[0] + 1;
4217
4218 for (;;)
4219 {
4220 /*
4221 * See if we have data ready...
4222 */
4223
4224 bytes = 0;
4225
4226 FD_ZERO(&input);
4227 FD_SET(temppipe[0], &input);
4228 FD_SET(CGIPipes[0], &input);
4229
4230 timeout.tv_sec = 30;
4231 timeout.tv_usec = 0;
4232
4233 if ((i = select(maxfd, &input, NULL, NULL, &timeout)) < 0)
4234 {
4235 if (errno == EINTR)
4236 continue;
4237 else
4238 break;
4239 }
4240 else if (i == 0)
4241 {
4242 /*
4243 * We have timed out...
4244 */
4245
4246 break;
4247 }
4248
4249 if (FD_ISSET(temppipe[0], &input))
4250 {
4251 /*
4252 * Read the PPD file from the pipe, and write it to the PPD file.
4253 */
4254
4255 if ((bytes = read(temppipe[0], buffer, sizeof(buffer))) > 0)
4256 {
4257 if (write(tempfd, buffer, bytes) < bytes)
4258 break;
4259
4260 total += bytes;
4261 }
4262 else
4263 break;
4264 }
4265
4266 if (FD_ISSET(CGIPipes[0], &input))
4267 cupsdUpdateCGI();
4268 }
4269
4270 close(temppipe[0]);
4271 close(tempfd);
4272
4273 if (!total)
4274 {
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);
4282 }
4283
4284 /*
4285 * Read the source file and see what page sizes are supported...
4286 */
4287
4288 if ((ppd = ppdOpenFile(tempfile)) == NULL)
4289 {
4290 unlink(tempfile);
4291 return (-1);
4292 }
4293
4294 have_letter = ppdPageSize(ppd, "Letter") != NULL;
4295 have_a4 = ppdPageSize(ppd, "A4") != NULL;
4296
4297 /*
4298 * Open the destination (if possible) and set the default options...
4299 */
4300
4301 num_defaults = 0;
4302 defaults = NULL;
4303 cups_protocol[0] = '\0';
4304
4305 if ((dst = cupsFileOpen(to, "rb")) != NULL)
4306 {
4307 /*
4308 * Read all of the default lines from the old PPD...
4309 */
4310
4311 while (cupsFileGets(dst, buffer, sizeof(buffer)))
4312 if (!strncmp(buffer, "*Default", 8))
4313 {
4314 /*
4315 * Add the default option...
4316 */
4317
4318 if (!ppd_parse_line(buffer, option, sizeof(option),
4319 choice, sizeof(choice)))
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,
4333 &defaults);
4334 }
4335 }
4336 else if (!strncmp(buffer, "*cupsProtocol:", 14))
4337 strlcpy(cups_protocol, buffer, sizeof(cups_protocol));
4338
4339 cupsFileClose(dst);
4340 }
4341 #ifdef HAVE_LIBPAPER
4342 else if ((paper_result = systempapername()) != NULL)
4343 {
4344 /*
4345 * Set the default media sizes from the systemwide default...
4346 */
4347
4348 strlcpy(system_paper, paper_result, sizeof(system_paper));
4349 system_paper[0] = toupper(system_paper[0] & 255);
4350
4351 if ((!strcmp(system_paper, "Letter") && have_letter) ||
4352 (!strcmp(system_paper, "A4") && have_a4))
4353 {
4354 num_defaults = cupsAddOption("PageSize", system_paper,
4355 num_defaults, &defaults);
4356 num_defaults = cupsAddOption("PageRegion", system_paper,
4357 num_defaults, &defaults);
4358 num_defaults = cupsAddOption("PaperDimension", system_paper,
4359 num_defaults, &defaults);
4360 num_defaults = cupsAddOption("ImageableArea", system_paper,
4361 num_defaults, &defaults);
4362 }
4363 }
4364 #endif /* HAVE_LIBPAPER */
4365 else
4366 {
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") ||
4380 !strncasecmp(DefaultLanguage, "en.", 3) ||
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 {
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);
4399 }
4400 }
4401 else if (have_a4)
4402 {
4403 /*
4404 * The rest default to "a4" size...
4405 */
4406
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);
4415 }
4416 }
4417
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
4431 /*
4432 * Open the destination file for a copy...
4433 */
4434
4435 if ((dst = cupsFileOpen(to, "wb")) == NULL)
4436 {
4437 cupsFreeOptions(num_defaults, defaults);
4438 cupsFileClose(src);
4439 unlink(tempfile);
4440 return (-1);
4441 }
4442
4443 /*
4444 * Copy the source file to the destination...
4445 */
4446
4447 while (cupsFileGets(src, buffer, sizeof(buffer)))
4448 {
4449 if (!strncmp(buffer, "*Default", 8))
4450 {
4451 /*
4452 * Check for an previous default option choice...
4453 */
4454
4455 if (!ppd_parse_line(buffer, option, sizeof(option),
4456 choice, sizeof(choice)))
4457 {
4458 const char *val; /* Default option value */
4459
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 }
4469 }
4470 }
4471
4472 cupsFilePrintf(dst, "%s\n", buffer);
4473 }
4474
4475 if (cups_protocol[0])
4476 cupsFilePrintf(dst, "%s\n", cups_protocol);
4477
4478 cupsFreeOptions(num_defaults, defaults);
4479
4480 /*
4481 * Close both files and return...
4482 */
4483
4484 cupsFileClose(src);
4485
4486 unlink(tempfile);
4487
4488 return (cupsFileClose(dst));
4489 }
4490
4491
4492 /*
4493 * 'copy_job_attrs()' - Copy job attributes.
4494 */
4495
4496 static void
4497 copy_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 */
4502
4503
4504 /*
4505 * Send the requested attributes for each job...
4506 */
4507
4508 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
4509 con->servername, con->serverport, "/jobs/%d",
4510 job->id);
4511
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);
4515
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);
4520
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));
4524
4525 if (!ra || cupsArrayFind(ra, "job-state-reasons"))
4526 add_job_state_reasons(con, job);
4527
4528 if (!ra || cupsArrayFind(ra, "job-uri"))
4529 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
4530 "job-uri", NULL, job_uri);
4531
4532 copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0);
4533 }
4534
4535
4536 /*
4537 * 'copy_printer_attrs()' - Copy printer attributes.
4538 */
4539
4540 static void
4541 copy_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 */
4551
4552
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 */
4557
4558 curtime = time(NULL);
4559
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__ */
4567
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
4578 if (!ra || cupsArrayFind(ra, "printer-current-time"))
4579 ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
4580 ippTimeToDate(curtime));
4581
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);
4585
4586 if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs"))
4587 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
4588 printer->accepting);
4589
4590 if (!ra || cupsArrayFind(ra, "printer-is-shared"))
4591 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared",
4592 printer->shared);
4593
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);
4605
4606 if (MaxPrinterHistory > 0 && printer->num_history > 0 &&
4607 cupsArrayFind(ra, "printer-state-history"))
4608 {
4609 /*
4610 * Printer history is only sent if specifically requested, so that
4611 * older CUPS/IPP clients won't barf on the collection attributes.
4612 */
4613
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);
4621 }
4622
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);
4626
4627 if (!ra || cupsArrayFind(ra, "printer-state-reasons"))
4628 add_printer_state_reasons(con, printer);
4629
4630 if (!ra || cupsArrayFind(ra, "printer-type"))
4631 {
4632 int type; /* printer-type value */
4633
4634
4635 /*
4636 * Add the CUPS-specific printer-type attribute...
4637 */
4638
4639 type = printer->type;
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);
4652 }
4653
4654 if (!ra || cupsArrayFind(ra, "printer-up-time"))
4655 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4656 "printer-up-time", curtime);
4657
4658 if ((!ra || cupsArrayFind(ra, "printer-uri-supported")) &&
4659 !ippFindAttribute(printer->attrs, "printer-uri-supported",
4660 IPP_TAG_URI))
4661 {
4662 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
4663 "ipp", NULL, con->servername, con->serverport,
4664 (printer->type & CUPS_PRINTER_CLASS) ?
4665 "/classes/%s" : "/printers/%s", printer->name);
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 }
4671
4672 if (!ra || cupsArrayFind(ra, "queued-job-count"))
4673 add_queued_job_count(con, printer);
4674
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 }
4678
4679
4680 /*
4681 * 'copy_subscription_attrs()' - Copy subscription attributes.
4682 */
4683
4684 static void
4685 copy_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 */
4696
4697
4698 /*
4699 * Copy the subscription attributes to the response using the
4700 * requested-attributes attribute that may be provided by the client.
4701 */
4702
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 */
4710
4711 ippAddString(con->response, IPP_TAG_SUBSCRIPTION,
4712 (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
4713 "notify-events", NULL, name);
4714 }
4715 else
4716 {
4717 /*
4718 * Complex event list...
4719 */
4720
4721 for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
4722 if (sub->mask & mask)
4723 count ++;
4724
4725 attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION,
4726 (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
4727 "notify-events", count, NULL, NULL);
4728
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);
4734
4735 count ++;
4736 }
4737 }
4738 }
4739
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);
4743
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);
4747
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);
4755 }
4756
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");
4763
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);
4767
4768 if (!ra || cupsArrayFind(ra, "notify-subscription-id"))
4769 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
4770 "notify-subscription-id", sub->id);
4771
4772 if (!ra || cupsArrayFind(ra, "notify-time-interval"))
4773 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
4774 "notify-time-interval", sub->interval);
4775
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 }
4780
4781
4782 /*
4783 * 'create_job()' - Print a file to a printer or class.
4784 */
4785
4786 static void
4787 create_job(cupsd_client_t *con, /* I - Client connection */
4788 ipp_attribute_t *uri) /* I - Printer URI */
4789 {
4790 cupsd_printer_t *printer; /* Printer */
4791 cupsd_job_t *job; /* New job */
4792
4793
4794 cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con,
4795 con->http.fd, uri->values[0].string.text);
4796
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
4812 /*
4813 * Create the job object...
4814 */
4815
4816 if ((job = add_job(con, printer, NULL)) == NULL)
4817 return;
4818
4819 job->pending_timeout = 1;
4820
4821 /*
4822 * Save and log the job...
4823 */
4824
4825 cupsdSaveJob(job);
4826
4827 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Queued on \"%s\" by \"%s\".",
4828 job->id, job->dest, job->username);
4829 }
4830
4831
4832 /*
4833 * 'create_requested_array()' - Create an array for the requested-attributes.
4834 */
4835
4836 static cups_array_t * /* O - Array of attributes or NULL */
4837 create_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");
4964 cupsArrayAdd(ra, "notify-events-default");
4965 cupsArrayAdd(ra, "notify-events-supported");
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");
4972 cupsArrayAdd(ra, "printer-alert");
4973 cupsArrayAdd(ra, "printer-alert-description");
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 }
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 }
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
5028 static void
5029 create_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 */
5036 cups_ptype_t dtype; /* Destination type (printer/class) */
5037 char scheme[HTTP_MAX_URI],
5038 /* Scheme portion of URI */
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 */
5050 const char *username, /* requesting-user-name or
5051 authenticated username */
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 */
5058 ipp_attribute_t *notify_events,/* notify-events(-default) */
5059 *notify_lease; /* notify-lease-duration(-default) */
5060
5061
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
5073 /*
5074 * Is the destination valid?
5075 */
5076
5077 cupsdLogMessage(CUPSD_LOG_DEBUG,
5078 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5079 con, con->http.fd, uri->values[0].string.text);
5080
5081 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
5082 sizeof(scheme), userpass, sizeof(userpass), host,
5083 sizeof(host), &port, resource, sizeof(resource));
5084
5085 if (!strcmp(resource, "/"))
5086 {
5087 dtype = (cups_ptype_t)0;
5088 printer = NULL;
5089 }
5090 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
5091 {
5092 dtype = (cups_ptype_t)0;
5093 printer = NULL;
5094 }
5095 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
5096 {
5097 dtype = CUPS_PRINTER_CLASS;
5098 printer = NULL;
5099 }
5100 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
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 {
5117 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
5118 NULL)) != HTTP_OK)
5119 {
5120 send_http_error(con, status, printer);
5121 return;
5122 }
5123 }
5124 else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5125 {
5126 send_http_error(con, status, NULL);
5127 return;
5128 }
5129
5130 /*
5131 * Get the user that is requesting the subscription...
5132 */
5133
5134 username = get_username(con);
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
5156 con->response->request.status.status_code = IPP_BAD_REQUEST;
5157
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
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
5185 while (attr && attr->group_tag != IPP_TAG_ZERO)
5186 {
5187 if (!strcmp(attr->name, "notify-recipient-uri") &&
5188 attr->value_tag == IPP_TAG_URI)
5189 {
5190 /*
5191 * Validate the recipient scheme against the ServerBin/notifier
5192 * directory...
5193 */
5194
5195 char notifier[1024]; /* Notifier filename */
5196
5197
5198 recipient = attr->values[0].string.text;
5199
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)
5204 {
5205 send_ipp_status(con, IPP_NOT_POSSIBLE,
5206 _("Bad notify-recipient-uri URI \"%s\"!"), recipient);
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,
5217 _("notify-recipient-uri URI \"%s\" uses unknown "
5218 "scheme!"), recipient);
5219 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5220 "notify-status-code", IPP_URI_SCHEME);
5221 return;
5222 }
5223 }
5224 else if (!strcmp(attr->name, "notify-pull-method") &&
5225 attr->value_tag == IPP_TAG_KEYWORD)
5226 {
5227 pullmethod = attr->values[0].string.text;
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 }
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)
5273 notify_events = attr;
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
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
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);
5299
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
5317 if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration))
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
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
5350 sub->interval = interval;
5351 sub->lease = lease;
5352 sub->expire = lease ? time(NULL) + lease : 0;
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
5367 con->response->request.status.status_code = IPP_OK;
5368
5369 if (attr)
5370 attr = attr->next;
5371 }
5372
5373 cupsdSaveAllSubscriptions();
5374
5375 }
5376
5377
5378 /*
5379 * 'delete_printer()' - Remove a printer or class from the system.
5380 */
5381
5382 static void
5383 delete_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 */
5387 cups_ptype_t dtype; /* Destination type (printer/class) */
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
5399 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
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 {
5416 send_http_error(con, status, NULL);
5417 return;
5418 }
5419
5420 /*
5421 * Remove old jobs...
5422 */
5423
5424 cupsdCancelJobs(printer->name, NULL, 1);
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",
5433 printer->name, get_username(con));
5434
5435 cupsdExpireSubscriptions(printer, NULL);
5436
5437 /*
5438 * Remove any old PPD or script files...
5439 */
5440
5441 snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
5442 printer->name);
5443 unlink(filename);
5444
5445 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
5446 printer->name);
5447 unlink(filename);
5448
5449 if (dtype & CUPS_PRINTER_CLASS)
5450 {
5451 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".",
5452 printer->name, get_username(con));
5453
5454 cupsdDeletePrinter(printer, 0);
5455 cupsdSaveAllClasses();
5456 }
5457 else
5458 {
5459 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".",
5460 printer->name, get_username(con));
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
5480 static void
5481 get_default(cupsd_client_t *con) /* I - Client connection */
5482 {
5483 http_status_t status; /* Policy status */
5484 cups_array_t *ra; /* Requested attributes array */
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 {
5495 send_http_error(con, status, NULL);
5496 return;
5497 }
5498
5499 if (DefaultPrinter)
5500 {
5501 ra = create_requested_array(con->request);
5502
5503 copy_printer_attrs(con, DefaultPrinter, ra);
5504
5505 cupsArrayDelete(ra);
5506
5507 con->response->request.status.status_code = IPP_OK;
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
5518 static void
5519 get_devices(cupsd_client_t *con) /* I - Client connection */
5520 {
5521 http_status_t status; /* Policy status */
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 */
5526 requested_str[256];
5527 /* String for requested attributes */
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 {
5538 send_http_error(con, status, NULL);
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)
5551 url_encode_attr(requested, requested_str, sizeof(requested_str));
5552 else
5553 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
5554
5555 snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin);
5556 snprintf(options, sizeof(options),
5557 "%d+%d+%d+%s",
5558 con->request->request.op.request_id,
5559 limit ? limit->values[0].integer : 0, (int)User,
5560 requested_str);
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 /*
5585 * 'get_job_attrs()' - Get job attributes.
5586 */
5587
5588 static void
5589 get_job_attrs(cupsd_client_t *con, /* I - Client connection */
5590 ipp_attribute_t *uri) /* I - Job URI */
5591 {
5592 http_status_t status; /* Policy status */
5593 ipp_attribute_t *attr; /* Current attribute */
5594 int jobid; /* Job ID */
5595 cupsd_job_t *job; /* Current job */
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 */
5601 cups_array_t *ra; /* Requested attributes array */
5602
5603
5604 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con,
5605 con->http.fd, uri->values[0].string.text);
5606
5607 /*
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
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));
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 {
5672 send_http_error(con, status, NULL);
5673 return;
5674 }
5675
5676 /*
5677 * Copy attributes...
5678 */
5679
5680 cupsdLoadJob(job);
5681
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
5694 static void
5695 get_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 */
5701 cups_ptype_t dtype; /* Destination type (printer/class) */
5702 cups_ptype_t dmask; /* Destination type mask */
5703 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
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?
5723 */
5724
5725 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
5726 sizeof(scheme), username, sizeof(username), host,
5727 sizeof(host), &port, resource, sizeof(resource));
5728
5729 if (!strcmp(resource, "/") ||
5730 (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6))
5731 {
5732 dest = NULL;
5733 dtype = (cups_ptype_t)0;
5734 dmask = (cups_ptype_t)0;
5735 printer = NULL;
5736 }
5737 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
5738 {
5739 dest = NULL;
5740 dtype = (cups_ptype_t)0;
5741 dmask = CUPS_PRINTER_CLASS;
5742 printer = NULL;
5743 }
5744 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
5745 {
5746 dest = NULL;
5747 dtype = CUPS_PRINTER_CLASS;
5748 dmask = CUPS_PRINTER_CLASS;
5749 printer = NULL;
5750 }
5751 else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype,
5752 &printer)) == NULL)
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
5763 {
5764 dtype &= CUPS_PRINTER_CLASS;
5765 dmask = CUPS_PRINTER_CLASS;
5766 }
5767
5768 /*
5769 * Check policy...
5770 */
5771
5772 if (printer)
5773 {
5774 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
5775 NULL)) != HTTP_OK)
5776 {
5777 send_http_error(con, status, printer);
5778 return;
5779 }
5780 }
5781 else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5782 {
5783 send_http_error(con, status, NULL);
5784 return;
5785 }
5786
5787 /*
5788 * See if the "which-jobs" attribute have been specified...
5789 */
5790
5791 if ((attr = ippFindAttribute(con->request, "which-jobs",
5792 IPP_TAG_KEYWORD)) != NULL &&
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
5813 if ((attr = ippFindAttribute(con->request, "limit",
5814 IPP_TAG_INTEGER)) != NULL)
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
5829 if ((attr = ippFindAttribute(con->request, "my-jobs",
5830 IPP_TAG_BOOLEAN)) != NULL &&
5831 attr->values[0].boolean)
5832 strlcpy(username, get_username(con), sizeof(username));
5833 else
5834 username[0] = '\0';
5835
5836 ra = create_requested_array(con->request);
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
5852 if ((dest && strcmp(job->dest, dest)) &&
5853 (!job->printer || !dest || strcmp(job->printer->name, dest)))
5854 continue;
5855 if ((job->dtype & dmask) != dtype &&
5856 (!job->printer || (job->printer->type & dmask) != dtype))
5857 continue;
5858 if (completed && job->state_value <= IPP_JOB_STOPPED)
5859 continue;
5860
5861 if (job->id < first_job_id)
5862 continue;
5863
5864 cupsdLoadJob(job);
5865
5866 if (!job->attrs)
5867 continue;
5868
5869 if (username[0] && strcasecmp(username, job->username))
5870 continue;
5871
5872 if (count > 0)
5873 ippAddSeparator(con->response);
5874
5875 count ++;
5876
5877 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count = %d", count);
5878
5879 copy_job_attrs(con, job, ra);
5880 }
5881
5882 cupsArrayDelete(ra);
5883
5884 con->response->request.status.status_code = IPP_OK;
5885 }
5886
5887
5888 /*
5889 * 'get_notifications()' - Get events for a subscription.
5890 */
5891
5892 static void
5893 get_notifications(cupsd_client_t *con) /* I - Client connection */
5894 {
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
5904 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])",
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 {
5949 send_http_error(con, status, sub->dest);
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
6014 for (; j < sub->num_events; j ++)
6015 {
6016 ippAddSeparator(con->response);
6017
6018 copy_attrs(con->response, sub->events[j]->attrs, NULL,
6019 IPP_TAG_EVENT_NOTIFICATION, 0);
6020 }
6021 }
6022 }
6023
6024
6025 /*
6026 * 'get_ppd()' - Get a named PPD from the local system.
6027 */
6028
6029 static void
6030 get_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
6112 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
6113 dest->name);
6114
6115 if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0))
6116 {
6117 con->response->request.status.status_code = CUPS_SEE_OTHER;
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 &
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 }
6134
6135 if (i < dest->num_printers)
6136 dest = dest->printers[i];
6137 else
6138 {
6139 con->response->request.status.status_code = CUPS_SEE_OTHER;
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
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"),
6154 uri->values[0].string.text, strerror(errno));
6155 return;
6156 }
6157
6158 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
6159
6160 con->pipe_pid = 0;
6161
6162 con->response->request.status.status_code = IPP_OK;
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
6171 /*
6172 * 'get_ppds()' - Get the list of PPD files on the local system.
6173 */
6174
6175 static void
6176 get_ppds(cupsd_client_t *con) /* I - Client connection */
6177 {
6178 http_status_t status; /* Policy status */
6179 ipp_attribute_t *limit, /* Limit attribute */
6180 *device, /* ppd-device-id attribute */
6181 *language, /* ppd-natural-language attribute */
6182 *make, /* ppd-make attribute */
6183 *model, /* ppd-make-and-model attribute */
6184 *model_number, /* ppd-model-number attribute */
6185 *product, /* ppd-product attribute */
6186 *psversion, /* ppd-psverion attribute */
6187 *type, /* ppd-type attribute */
6188 *requested; /* requested-attributes attribute */
6189 char command[1024], /* cups-driverd command */
6190 options[1024], /* Options to pass to command */
6191 device_str[256],/* Escaped ppd-device-id string */
6192 language_str[256],
6193 /* Escaped ppd-natural-language */
6194 make_str[256], /* Escaped ppd-make string */
6195 model_str[256], /* Escaped ppd-make-and-model string */
6196 model_number_str[256],
6197 /* ppd-model-number string */
6198 product_str[256],
6199 /* Escaped ppd-product string */
6200 psversion_str[256],
6201 /* Escaped ppd-psversion string */
6202 type_str[256], /* Escaped ppd-type string */
6203 requested_str[256];
6204 /* String for requested attributes */
6205
6206
6207 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->http.fd);
6208
6209 /*
6210 * Check policy...
6211 */
6212
6213 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6214 {
6215 send_http_error(con, status, NULL);
6216 return;
6217 }
6218
6219 /*
6220 * Run cups-driverd command with the given options...
6221 */
6222
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);
6237
6238 if (requested)
6239 url_encode_attr(requested, requested_str, sizeof(requested_str));
6240 else
6241 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
6242
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
6253 if (make)
6254 url_encode_attr(make, make_str, sizeof(make_str));
6255 else
6256 make_str[0] = '\0';
6257
6258 if (model)
6259 url_encode_attr(model, model_str, sizeof(model_str));
6260 else
6261 model_str[0] = '\0';
6262
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
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
6279 if (type)
6280 url_encode_attr(type, type_str, sizeof(type_str));
6281 else
6282 type_str[0] = '\0';
6283
6284 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
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",
6287 con->request->request.op.request_id,
6288 limit ? limit->values[0].integer : 0,
6289 requested_str,
6290 device ? "%20" : "", device_str,
6291 language ? "%20" : "", language_str,
6292 make ? "%20" : "", make_str,
6293 model ? "%20" : "", model_str,
6294 model_number ? "%20" : "", model_number_str,
6295 product ? "%20" : "", product_str,
6296 psversion ? "%20" : "", psversion_str,
6297 type ? "%20" : "", type_str);
6298
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 }
6318 }
6319
6320
6321 /*
6322 * 'get_printer_attrs()' - Get printer attributes.
6323 */
6324
6325 static void
6326 get_printer_attrs(cupsd_client_t *con, /* I - Client connection */
6327 ipp_attribute_t *uri) /* I - Printer URI */
6328 {
6329 http_status_t status; /* Policy status */
6330 cups_ptype_t dtype; /* Destination type (printer/class) */
6331 cupsd_printer_t *printer; /* Printer/class */
6332 cups_array_t *ra; /* Requested attributes array */
6333
6334
6335 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con,
6336 con->http.fd, uri->values[0].string.text);
6337
6338 /*
6339 * Is the destination valid?
6340 */
6341
6342 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
6343 {
6344 /*
6345 * Bad URI...
6346 */
6347
6348 send_ipp_status(con, IPP_NOT_FOUND,
6349 _("The printer or class was not found."));
6350 return;
6351 }
6352
6353 /*
6354 * Check policy...
6355 */
6356
6357 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
6358 {
6359 send_http_error(con, status, printer);
6360 return;
6361 }
6362
6363 /*
6364 * Send the attributes...
6365 */
6366
6367 ra = create_requested_array(con->request);
6368
6369 copy_printer_attrs(con, printer, ra);
6370
6371 cupsArrayDelete(ra);
6372
6373 con->response->request.status.status_code = IPP_OK;
6374 }
6375
6376
6377 /*
6378 * 'get_printers()' - Get a list of printers or classes.
6379 */
6380
6381 static void
6382 get_printers(cupsd_client_t *con, /* I - Client connection */
6383 int type) /* I - 0 or CUPS_PRINTER_CLASS */
6384 {
6385 http_status_t status; /* Policy status */
6386 ipp_attribute_t *attr; /* Current attribute */
6387 int limit; /* Max number of printers to return */
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 */
6396
6397
6398 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con,
6399 con->http.fd, type);
6400
6401 /*
6402 * Check policy...
6403 */
6404
6405 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6406 {
6407 send_http_error(con, status, NULL);
6408 return;
6409 }
6410
6411 /*
6412 * Check for printers...
6413 */
6414
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 */
6424
6425 if ((attr = ippFindAttribute(con->request, "limit",
6426 IPP_TAG_INTEGER)) != NULL)
6427 limit = attr->values[0].integer;
6428 else
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;
6436
6437 /*
6438 * Support filtering...
6439 */
6440
6441 if ((attr = ippFindAttribute(con->request, "printer-type",
6442 IPP_TAG_ENUM)) != NULL)
6443 printer_type = attr->values[0].integer;
6444 else
6445 printer_type = 0;
6446
6447 if ((attr = ippFindAttribute(con->request, "printer-type-mask",
6448 IPP_TAG_ENUM)) != NULL)
6449 printer_mask = attr->values[0].integer;
6450 else
6451 printer_mask = 0;
6452
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;
6458
6459 if (con->username[0])
6460 username = con->username;
6461 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
6462 IPP_TAG_NAME)) != NULL)
6463 username = attr->values[0].string.text;
6464 else
6465 username = NULL;
6466
6467 ra = create_requested_array(con->request);
6468
6469 /*
6470 * OK, build a list of printers for this printer...
6471 */
6472
6473 if (first_printer_name)
6474 {
6475 if ((printer = cupsdFindDest(first_printer_name)) == NULL)
6476 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
6477 }
6478 else
6479 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
6480
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)))
6489 {
6490 /*
6491 * If HideImplicitMembers is enabled, see if this printer or class
6492 * is a member of an implicit class...
6493 */
6494
6495 if (ImplicitClasses && HideImplicitMembers &&
6496 printer->in_implicit_class)
6497 continue;
6498
6499 /*
6500 * If a username is specified, see if it is allowed or denied
6501 * access...
6502 */
6503
6504 if (printer->num_users && username && !user_allowed(printer, username))
6505 continue;
6506
6507 /*
6508 * Add the group separator as needed...
6509 */
6510
6511 if (count > 0)
6512 ippAddSeparator(con->response);
6513
6514 count ++;
6515
6516 /*
6517 * Send the attributes...
6518 */
6519
6520 copy_printer_attrs(con, printer, ra);
6521 }
6522 }
6523
6524 cupsArrayDelete(ra);
6525
6526 con->response->request.status.status_code = IPP_OK;
6527 }
6528
6529
6530 /*
6531 * 'get_subscription_attrs()' - Get subscription attributes.
6532 */
6533
6534 static void
6535 get_subscription_attrs(
6536 cupsd_client_t *con, /* I - Client connection */
6537 int sub_id) /* I - Subscription ID */
6538 {
6539 http_status_t status; /* Policy status */
6540 cupsd_subscription_t *sub; /* Subscription */
6541 cups_array_t *ra; /* Requested attributes array */
6542
6543
6544 cupsdLogMessage(CUPSD_LOG_DEBUG2,
6545 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
6546 con, con->http.fd, sub_id);
6547
6548 /*
6549 * Is the subscription ID valid?
6550 */
6551
6552 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
6553 {
6554 /*
6555 * Bad subscription ID...
6556 */
6557
6558 send_ipp_status(con, IPP_NOT_FOUND,
6559 _("notify-subscription-id %d no good!"), sub_id);
6560 return;
6561 }
6562
6563 /*
6564 * Check policy...
6565 */
6566
6567 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
6568 DefaultPolicyPtr,
6569 con, sub->owner)) != HTTP_OK)
6570 {
6571 send_http_error(con, status, sub->dest);
6572 return;
6573 }
6574
6575 /*
6576 * Copy the subscription attributes to the response using the
6577 * requested-attributes attribute that may be provided by the client.
6578 */
6579
6580 ra = create_requested_array(con->request);
6581
6582 copy_subscription_attrs(con, sub, ra);
6583
6584 cupsArrayDelete(ra);
6585
6586 con->response->request.status.status_code = IPP_OK;
6587 }
6588
6589
6590 /*
6591 * 'get_subscriptions()' - Get subscriptions.
6592 */
6593
6594 static void
6595 get_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 */
6604 cups_ptype_t dtype; /* Destination type (printer/class) */
6605 char scheme[HTTP_MAX_URI],
6606 /* Scheme portion of URI */
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 */
6616
6617
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
6626 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6627 sizeof(scheme), username, sizeof(username), host,
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;
6637 }
6638 else if (!strncmp(resource, "/jobs/", 6) && resource[6])
6639 {
6640 printer = NULL;
6641 job = cupsdFindJob(atoi(resource + 6));
6642
6643 if (!job)
6644 {
6645 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%s does not exist!"),
6646 resource + 6);
6647 return;
6648 }
6649 }
6650 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
6651 {
6652 /*
6653 * Bad URI...
6654 */
6655
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);
6664
6665 if (!job)
6666 {
6667 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"),
6668 attr->values[0].integer);
6669 return;
6670 }
6671 }
6672 else
6673 job = NULL;
6674
6675 /*
6676 * Check policy...
6677 */
6678
6679 if ((status = cupsdCheckPolicy(printer ? printer->op_policy_ptr :
6680 DefaultPolicyPtr,
6681 con, NULL)) != HTTP_OK)
6682 {
6683 send_http_error(con, status, printer);
6684 return;
6685 }
6686
6687 /*
6688 * Copy the subscription attributes to the response using the
6689 * requested-attributes attribute that may be provided by the client.
6690 */
6691
6692 ra = create_requested_array(con->request);
6693
6694 if ((attr = ippFindAttribute(con->request, "limit",
6695 IPP_TAG_INTEGER)) != NULL)
6696 limit = attr->values[0].integer;
6697 else
6698 limit = 0;
6699
6700 /*
6701 * See if we only want to see subscriptions for a specific user...
6702 */
6703
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));
6708 else
6709 username[0] = '\0';
6710
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)))
6716 {
6717 ippAddSeparator(con->response);
6718 copy_subscription_attrs(con, sub, ra);
6719
6720 count ++;
6721 if (limit && count >= limit)
6722 break;
6723 }
6724
6725 cupsArrayDelete(ra);
6726
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 }
6732
6733
6734 /*
6735 * 'get_username()' - Get the username associated with a request.
6736 */
6737
6738 static const char * /* O - Username */
6739 get_username(cupsd_client_t *con) /* I - Connection */
6740 {
6741 ipp_attribute_t *attr; /* Attribute */
6742
6743
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");
6751 }
6752
6753
6754 /*
6755 * 'hold_job()' - Hold a print job.
6756 */
6757
6758 static void
6759 hold_job(cupsd_client_t *con, /* I - Client connection */
6760 ipp_attribute_t *uri) /* I - Job or Printer URI */
6761 {
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
6772
6773 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->http.fd,
6774 uri->values[0].string.text);
6775
6776 /*
6777 * See if we have a job URI or a printer URI...
6778 */
6779
6780 if (!strcmp(uri->name, "printer-uri"))
6781 {
6782 /*
6783 * Got a printer URI; see if we also have a job-id attribute...
6784 */
6785
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 }
6793
6794 jobid = attr->values[0].integer;
6795 }
6796 else
6797 {
6798 /*
6799 * Got a job URI; parse it to get the job ID...
6800 */
6801
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));
6805
6806 if (strncmp(resource, "/jobs/", 6))
6807 {
6808 /*
6809 * Not a valid URI!
6810 */
6811
6812 send_ipp_status(con, IPP_BAD_REQUEST,
6813 _("Bad job-uri attribute \"%s\"!"),
6814 uri->values[0].string.text);
6815 return;
6816 }
6817
6818 jobid = atoi(resource + 6);
6819 }
6820
6821 /*
6822 * See if the job exists...
6823 */
6824
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 }
6834
6835 /*
6836 * See if the job is owned by the requesting user...
6837 */
6838
6839 if (!validate_user(job, con, job->username, username, sizeof(username)))
6840 {
6841 send_http_error(con, HTTP_UNAUTHORIZED, NULL);
6842 return;
6843 }
6844
6845 /*
6846 * Hold the job and return...
6847 */
6848
6849 cupsdHoldJob(job);
6850
6851 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job,
6852 "Job held by user.");
6853
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);
6857
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);
6861
6862 if (attr)
6863 {
6864 /*
6865 * Free the old hold value and copy the new one over...
6866 */
6867
6868 _cupsStrFree(attr->values[0].string.text);
6869
6870 if (newattr)
6871 {
6872 attr->value_tag = newattr->value_tag;
6873 attr->values[0].string.text =
6874 _cupsStrAlloc(newattr->values[0].string.text);
6875 }
6876 else
6877 {
6878 attr->value_tag = IPP_TAG_KEYWORD;
6879 attr->values[0].string.text = _cupsStrAlloc("indefinite");
6880 }
6881
6882 /*
6883 * Hold job until specified time...
6884 */
6885
6886 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
6887
6888 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, job->printer, job,
6889 "Job job-hold-until value changed by user.");
6890 }
6891
6892 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Held by \"%s\".", jobid,
6893 username);
6894
6895 con->response->request.status.status_code = IPP_OK;
6896 }
6897
6898
6899 /*
6900 * 'move_job()' - Move a job to a new destination.
6901 */
6902
6903 static void
6904 move_job(cupsd_client_t *con, /* I - Client connection */
6905 ipp_attribute_t *uri) /* I - Job URI */
6906 {
6907 http_status_t status; /* Policy status */
6908 ipp_attribute_t *attr; /* Current attribute */
6909 int jobid; /* Job ID */
6910 cupsd_job_t *job; /* Current job */
6911 const char *src; /* Source printer/class */
6912 cups_ptype_t stype, /* Source type (printer or class) */
6913 dtype; /* Destination type (printer/class) */
6914 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
6915 username[HTTP_MAX_URI], /* Username portion of URI */
6916 host[HTTP_MAX_URI], /* Host portion of URI */
6917 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6918 int port; /* Port portion of URI */
6919 cupsd_printer_t *sprinter, /* Source printer */
6920 *dprinter; /* Destination printer */
6921
6922
6923 cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd,
6924 uri->values[0].string.text);
6925
6926 /*
6927 * Get the new printer or class...
6928 */
6929
6930 if ((attr = ippFindAttribute(con->request, "job-printer-uri",
6931 IPP_TAG_URI)) == NULL)
6932 {
6933 /*
6934 * Need job-printer-uri...
6935 */
6936
6937 send_ipp_status(con, IPP_BAD_REQUEST,
6938 _("job-printer-uri attribute missing!"));
6939 return;
6940 }
6941
6942 if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter))
6943 {
6944 /*
6945 * Bad URI...
6946 */
6947
6948 send_ipp_status(con, IPP_NOT_FOUND,
6949 _("The printer or class was not found."));
6950 return;
6951 }
6952
6953 /*
6954 * Check policy...
6955 */
6956
6957 if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con,
6958 NULL)) != HTTP_OK)
6959 {
6960 send_http_error(con, status, dprinter);
6961 return;
6962 }
6963
6964 /*
6965 * See if we have a job URI or a printer URI...
6966 */
6967
6968 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6969 sizeof(scheme), username, sizeof(username), host,
6970 sizeof(host), &port, resource, sizeof(resource));
6971
6972 if (!strcmp(uri->name, "printer-uri"))
6973 {
6974 /*
6975 * Got a printer URI; see if we also have a job-id attribute...
6976 */
6977
6978 if ((attr = ippFindAttribute(con->request, "job-id",
6979 IPP_TAG_INTEGER)) == NULL)
6980 {
6981 /*
6982 * Move all jobs...
6983 */
6984
6985 if ((src = cupsdValidateDest(uri->values[0].string.text, &stype,
6986 &sprinter)) == NULL)
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 }
7024 }
7025 }
7026 else
7027 {
7028 /*
7029 * Got a job URI; parse it to get the job ID...
7030 */
7031
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 }
7043
7044 /*
7045 * See if the job exists...
7046 */
7047
7048 jobid = atoi(resource + 6);
7049
7050 if ((job = cupsdFindJob(jobid)) == NULL)
7051 {
7052 /*
7053 * Nope - return a "not found" error...
7054 */
7055
7056 send_ipp_status(con, IPP_NOT_FOUND,
7057 _("Job #%d does not exist!"), jobid);
7058 return;
7059 }
7060 else
7061 {
7062 /*
7063 * Job found, initialize source pointers...
7064 */
7065
7066 src = NULL;
7067 sprinter = NULL;
7068 }
7069 }
7070
7071 /*
7072 * Now move the job or jobs...
7073 */
7074
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 }
7092
7093 /*
7094 * See if the job is owned by the requesting user...
7095 */
7096
7097 if (!validate_user(job, con, job->username, username, sizeof(username)))
7098 {
7099 send_http_error(con, HTTP_UNAUTHORIZED, NULL);
7100 return;
7101 }
7102
7103 /*
7104 * Move the job to a different printer or class...
7105 */
7106
7107 cupsdMoveJob(job, dprinter);
7108 }
7109 else
7110 {
7111 /*
7112 * Got the source printer, now look through the jobs...
7113 */
7114
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 */
7123
7124 if (strcasecmp(job->dest, src) ||
7125 job->state_value > IPP_JOB_STOPPED)
7126 continue;
7127
7128 /*
7129 * See if the job can be moved by the requesting user...
7130 */
7131
7132 if (!validate_user(job, con, job->username, username, sizeof(username)))
7133 continue;
7134
7135 /*
7136 * Move the job to a different printer or class...
7137 */
7138
7139 cupsdMoveJob(job, dprinter);
7140 }
7141 }
7142
7143 /*
7144 * Start jobs if possible...
7145 */
7146
7147 cupsdCheckJobs();
7148
7149 /*
7150 * Return with "everything is OK" status...
7151 */
7152
7153 con->response->request.status.status_code = IPP_OK;
7154 }
7155
7156
7157 /*
7158 * 'ppd_parse_line()' - Parse a PPD default line.
7159 */
7160
7161 static int /* O - 0 on success, -1 on failure */
7162 ppd_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 */
7171
7172 if (strncmp(line, "*Default", 8))
7173 return (-1);
7174
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 --;
7184 }
7185
7186 *option = '\0';
7187
7188 /*
7189 * Skip everything else up to the colon (:)...
7190 */
7191
7192 while (*line && *line != ':')
7193 line ++;
7194
7195 if (!*line)
7196 return (-1);
7197
7198 line ++;
7199
7200 /*
7201 * Now grab the option choice, skipping leading whitespace...
7202 */
7203
7204 while (isspace(*line & 255))
7205 line ++;
7206
7207 for (clen --; isalnum(*line & 255); line ++)
7208 if (clen > 0)
7209 {
7210 *choice++ = *line;
7211 clen --;
7212 }
7213
7214 *choice = '\0';
7215
7216 /*
7217 * Return with no errors...
7218 */
7219
7220 return (0);
7221 }
7222
7223
7224 /*
7225 * 'print_job()' - Print a file to a printer or class.
7226 */
7227
7228 static void
7229 print_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 */
7234 const char *default_format; /* document-format-default value */
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 */
7246
7247
7248 cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd,
7249 uri->values[0].string.text);
7250
7251 /*
7252 * Validate print file attributes, for now just document-format and
7253 * compression (CUPS only supports "none" and "gzip")...
7254 */
7255
7256 compression = CUPS_FILE_NONE;
7257
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 }
7274
7275 #ifdef HAVE_LIBZ
7276 if (!strcmp(attr->values[0].string.text, "gzip"))
7277 compression = CUPS_FILE_GZIP;
7278 #endif /* HAVE_LIBZ */
7279 }
7280
7281 /*
7282 * Do we have a file to print?
7283 */
7284
7285 if (!con->filename)
7286 {
7287 send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!"));
7288 return;
7289 }
7290
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
7306 /*
7307 * Is it a format we support?
7308 */
7309
7310 if ((format = ippFindAttribute(con->request, "document-format",
7311 IPP_TAG_MIMETYPE)) != NULL)
7312 {
7313 /*
7314 * Grab format from client...
7315 */
7316
7317 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super,
7318 type) != 2)
7319 {
7320 send_ipp_status(con, IPP_BAD_REQUEST,
7321 _("Could not scan type \"%s\"!"),
7322 format->values[0].string.text);
7323 return;
7324 }
7325 }
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 }
7342 else
7343 {
7344 /*
7345 * Auto-type it!
7346 */
7347
7348 strcpy(super, "application");
7349 strcpy(type, "octet-stream");
7350 }
7351
7352 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
7353 {
7354 /*
7355 * Auto-type the file...
7356 */
7357
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
7368 if (!filetype)
7369 filetype = mimeType(MimeDatabase, super, type);
7370 }
7371 else
7372 filetype = mimeType(MimeDatabase, super, type);
7373
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 */
7382
7383 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
7384 filetype->type);
7385
7386 if (format)
7387 {
7388 _cupsStrFree(format->values[0].string.text);
7389
7390 format->values[0].string.text = _cupsStrAlloc(mimetype);
7391 }
7392 else
7393 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
7394 "document-format", NULL, mimetype);
7395 }
7396 else if (!filetype)
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.",
7411 filetype->super, filetype->type);
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
7425 if ((job = add_job(con, printer, filetype)) == NULL)
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
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
7459 cupsdTimeoutJob(job);
7460
7461 /*
7462 * Log and save the job...
7463 */
7464
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,
7468 (int)job->hold_until);
7469
7470 cupsdSaveJob(job);
7471
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
7513 static void
7514 read_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
7553 if (strncmp(line, "%!PS-Adobe-", 11))
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
7570 while (cupsFileGets(fp, line, sizeof(line)))
7571 {
7572 /*
7573 * Stop at the first non-ticket line...
7574 */
7575
7576 if (strncmp(line, "%cupsJobTicket:", 15))
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
7607 for (attr = ticket->attrs; attr; attr = attr->next)
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
7621 if ((attr2 = ippFindAttribute(con->request, attr->name,
7622 IPP_TAG_ZERO)) != NULL)
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 {
7635 for (prev2 = con->request->attrs; prev2; prev2 = prev2->next)
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
7646 _ippFreeAttr(attr2);
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
7669 static void
7670 reject_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 */
7674 cups_ptype_t dtype; /* Destination type (printer/class) */
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
7686 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
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 {
7703 send_http_error(con, status, printer);
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\").",
7727 printer->name, get_username(con));
7728 }
7729 else
7730 {
7731 cupsdSaveAllPrinters();
7732
7733 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").",
7734 printer->name, get_username(con));
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
7749 static void
7750 release_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
7776 if ((attr = ippFindAttribute(con->request, "job-id",
7777 IPP_TAG_INTEGER)) == NULL)
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
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));
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
7829 if (job->state_value != IPP_JOB_HELD)
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 {
7845 send_http_error(con, HTTP_UNAUTHORIZED, NULL);
7846 return;
7847 }
7848
7849 /*
7850 * Reset the job-hold-until value to "no-hold"...
7851 */
7852
7853 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
7854 IPP_TAG_KEYWORD)) == NULL)
7855 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
7856
7857 if (attr)
7858 {
7859 _cupsStrFree(attr->values[0].string.text);
7860
7861 attr->value_tag = IPP_TAG_KEYWORD;
7862 attr->values[0].string.text = _cupsStrAlloc("no-hold");
7863
7864 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, job->printer, job,
7865 "Job job-hold-until value changed by user.");
7866 }
7867
7868 /*
7869 * Release the job and return...
7870 */
7871
7872 cupsdReleaseJob(job);
7873
7874 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job,
7875 "Job released by user.");
7876
7877 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Released by \"%s\".", jobid,
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
7888 static void
7889 renew_subscription(
7890 cupsd_client_t *con, /* I - Client connection */
7891 int sub_id) /* I - Subscription ID */
7892 {
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 {
7936 send_http_error(con, status, sub->dest);
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;
7963
7964 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
7965 "notify-lease-duration", sub->lease);
7966 }
7967
7968
7969 /*
7970 * 'restart_job()' - Restart an old print job.
7971 */
7972
7973 static void
7974 restart_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
8000 if ((attr = ippFindAttribute(con->request, "job-id",
8001 IPP_TAG_INTEGER)) == NULL)
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
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));
8019
8020 if (strncmp(resource, "/jobs/", 6))
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
8053 if (job->state_value <= IPP_JOB_PROCESSING)
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
8068 cupsdLoadJob(job);
8069
8070 if (!job->attrs || job->num_files == 0)
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 {
8087 send_http_error(con, HTTP_UNAUTHORIZED, NULL);
8088 return;
8089 }
8090
8091 /*
8092 * Restart the job and return...
8093 */
8094
8095 cupsdRestartJob(job);
8096
8097 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Restarted by \"%s\".", jobid,
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
8108 static void
8109 save_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 */
8113 {
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 */
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
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.
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
8147 if ((dest = cupsdFindDest(job->dest)) == NULL)
8148 return;
8149
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
8166 if (auth_info && auth_info->num_values == dest->num_auth_info_required)
8167 {
8168 /*
8169 * Write 1 to 3 auth values...
8170 */
8171
8172 cupsdClearString(&job->auth_username);
8173 cupsdClearString(&job->auth_domain);
8174 cupsdClearString(&job->auth_password);
8175
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);
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);
8191 }
8192 }
8193 else if (con->username[0])
8194 {
8195 /*
8196 * Write the authenticated username...
8197 */
8198
8199 httpEncode64_2(line, sizeof(line), con->username, strlen(con->username));
8200 cupsFilePrintf(fp, "%s\n", line);
8201
8202 cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", con->username);
8203 cupsdClearString(&job->auth_domain);
8204
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);
8211
8212 cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", con->password);
8213 }
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);
8227
8228 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
8229 if (con->gss_have_creds)
8230 save_krb5_creds(con, job);
8231 else if (job->ccname)
8232 cupsdClearString(&(job->ccname));
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
8242 static void
8243 save_krb5_creds(cupsd_client_t *con, /* I - Client connection */
8244 cupsd_job_t *job) /* I - Job */
8245 {
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
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
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
8277 /*
8278 * We MUST create a file-based cache because memory-based caches are
8279 * only valid for the current process/address space.
8280 */
8281
8282 # ifdef HAVE_KRB5_CC_RESOLVE
8283 if (krb5_cc_resolve(krb_context, "FILE:", &ccache))
8284 # elif defined(HAVE_HEIMDAL)
8285 if (krb5_cc_gen_new(krb_context, &krb5_fcc_ops, &ccache))
8286 # else
8287 if (krb5_cc_gen_new(krb_context, &ccache))
8288 # endif /* HAVE_HEIMDAL */
8289 {
8290 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create new credentials");
8291 return;
8292 }
8293
8294 major_status = gss_krb5_copy_ccache(&minor_status, con->gss_delegated_cred,
8295 ccache);
8296
8297 if (GSS_ERROR(major_status))
8298 {
8299 cupsdLogGSSMessage(CUPSD_LOG_ERROR, major_status, minor_status,
8300 "Unable to import client credentials cache");
8301 krb5_cc_destroy(krb_context, ccache);
8302 return;
8303 }
8304
8305 cupsdSetStringf(&(job->ccname), "KRB5CCNAME=FILE:%s",
8306 krb5_cc_get_name(krb_context, ccache));
8307
8308 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] save_krb5_creds: %s", job->id,
8309 job->ccname);
8310
8311 krb5_cc_close(krb_context, ccache);
8312 }
8313 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
8314
8315
8316 /*
8317 * 'send_document()' - Send a file to a printer or class.
8318 */
8319
8320 static void
8321 send_document(cupsd_client_t *con, /* I - Client connection */
8322 ipp_attribute_t *uri) /* I - Printer URI */
8323 {
8324 ipp_attribute_t *attr; /* Current attribute */
8325 ipp_attribute_t *format; /* Document-format attribute */
8326 const char *default_format;/* document-format-default value */
8327 int jobid; /* Job ID number */
8328 cupsd_job_t *job; /* Current job */
8329 char job_uri[HTTP_MAX_URI],
8330 /* Job URI */
8331 method[HTTP_MAX_URI],
8332 /* Method portion of URI */
8333 username[HTTP_MAX_URI],
8334 /* Username portion of URI */
8335 host[HTTP_MAX_URI],
8336 /* Host portion of URI */
8337 resource[HTTP_MAX_URI];
8338 /* Resource portion of URI */
8339 int port; /* Port portion of URI */
8340 mime_type_t *filetype; /* Type of file */
8341 char super[MIME_MAX_SUPER],
8342 /* Supertype of file */
8343 type[MIME_MAX_TYPE],
8344 /* Subtype of file */
8345 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
8346 /* Textual name of mime type */
8347 char filename[1024]; /* Job filename */
8348 cupsd_printer_t *printer; /* Current printer */
8349 struct stat fileinfo; /* File information */
8350 int kbytes; /* Size of file */
8351 int compression; /* Type of compression */
8352
8353
8354 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con,
8355 con->http.fd, uri->values[0].string.text);
8356
8357 /*
8358 * See if we have a job URI or a printer URI...
8359 */
8360
8361 if (!strcmp(uri->name, "printer-uri"))
8362 {
8363 /*
8364 * Got a printer URI; see if we also have a job-id attribute...
8365 */
8366
8367 if ((attr = ippFindAttribute(con->request, "job-id",
8368 IPP_TAG_INTEGER)) == NULL)
8369 {
8370 send_ipp_status(con, IPP_BAD_REQUEST,
8371 _("Got a printer-uri attribute but no job-id!"));
8372 return;
8373 }
8374
8375 jobid = attr->values[0].integer;
8376 }
8377 else
8378 {
8379 /*
8380 * Got a job URI; parse it to get the job ID...
8381 */
8382
8383 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
8384 sizeof(method), username, sizeof(username), host,
8385 sizeof(host), &port, resource, sizeof(resource));
8386
8387 if (strncmp(resource, "/jobs/", 6))
8388 {
8389 /*
8390 * Not a valid URI!
8391 */
8392
8393 send_ipp_status(con, IPP_BAD_REQUEST,
8394 _("Bad job-uri attribute \"%s\"!"),
8395 uri->values[0].string.text);
8396 return;
8397 }
8398
8399 jobid = atoi(resource + 6);
8400 }
8401
8402 /*
8403 * See if the job exists...
8404 */
8405
8406 if ((job = cupsdFindJob(jobid)) == NULL)
8407 {
8408 /*
8409 * Nope - return a "not found" error...
8410 */
8411
8412 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
8413 return;
8414 }
8415
8416 printer = cupsdFindDest(job->dest);
8417
8418 /*
8419 * See if the job is owned by the requesting user...
8420 */
8421
8422 if (!validate_user(job, con, job->username, username, sizeof(username)))
8423 {
8424 send_http_error(con, HTTP_UNAUTHORIZED, NULL);
8425 return;
8426 }
8427
8428 /*
8429 * OK, see if the client is sending the document compressed - CUPS
8430 * only supports "none" and "gzip".
8431 */
8432
8433 compression = CUPS_FILE_NONE;
8434
8435 if ((attr = ippFindAttribute(con->request, "compression",
8436 IPP_TAG_KEYWORD)) != NULL)
8437 {
8438 if (strcmp(attr->values[0].string.text, "none")
8439 #ifdef HAVE_LIBZ
8440 && strcmp(attr->values[0].string.text, "gzip")
8441 #endif /* HAVE_LIBZ */
8442 )
8443 {
8444 send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"!"),
8445 attr->values[0].string.text);
8446 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
8447 "compression", NULL, attr->values[0].string.text);
8448 return;
8449 }
8450
8451 #ifdef HAVE_LIBZ
8452 if (!strcmp(attr->values[0].string.text, "gzip"))
8453 compression = CUPS_FILE_GZIP;
8454 #endif /* HAVE_LIBZ */
8455 }
8456
8457 /*
8458 * Do we have a file to print?
8459 */
8460
8461 if (!con->filename)
8462 {
8463 send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!"));
8464 return;
8465 }
8466
8467 /*
8468 * Is it a format we support?
8469 */
8470
8471 if ((format = ippFindAttribute(con->request, "document-format",
8472 IPP_TAG_MIMETYPE)) != NULL)
8473 {
8474 /*
8475 * Grab format from client...
8476 */
8477
8478 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
8479 super, type) != 2)
8480 {
8481 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"),
8482 format->values[0].string.text);
8483 return;
8484 }
8485 }
8486 else if ((default_format = cupsGetOption("document-format",
8487 printer->num_options,
8488 printer->options)) != NULL)
8489 {
8490 /*
8491 * Use default document format...
8492 */
8493
8494 if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
8495 {
8496 send_ipp_status(con, IPP_BAD_REQUEST,
8497 _("Could not scan type \"%s\"!"),
8498 default_format);
8499 return;
8500 }
8501 }
8502 else
8503 {
8504 /*
8505 * No document format attribute? Auto-type it!
8506 */
8507
8508 strcpy(super, "application");
8509 strcpy(type, "octet-stream");
8510 }
8511
8512 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
8513 {
8514 /*
8515 * Auto-type the file...
8516 */
8517
8518 ipp_attribute_t *doc_name; /* document-name attribute */
8519
8520
8521 cupsdLogMessage(CUPSD_LOG_DEBUG, "send_document: auto-typing file...");
8522
8523 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
8524 filetype = mimeFileType(MimeDatabase, con->filename,
8525 doc_name ? doc_name->values[0].string.text : NULL,
8526 &compression);
8527
8528 if (!filetype)
8529 filetype = mimeType(MimeDatabase, super, type);
8530 }
8531 else
8532 filetype = mimeType(MimeDatabase, super, type);
8533
8534 if (filetype &&
8535 (!format ||
8536 (!strcmp(super, "application") && !strcmp(type, "octet-stream"))))
8537 {
8538 /*
8539 * Replace the document-format attribute value with the auto-typed or
8540 * default one.
8541 */
8542
8543 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
8544 filetype->type);
8545
8546 if (format)
8547 {
8548 _cupsStrFree(format->values[0].string.text);
8549
8550 format->values[0].string.text = _cupsStrAlloc(mimetype);
8551 }
8552 else
8553 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
8554 "document-format", NULL, mimetype);
8555 }
8556 else if (!filetype)
8557 {
8558 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
8559 _("Unsupported format \'%s/%s\'!"), super, type);
8560 cupsdLogMessage(CUPSD_LOG_INFO,
8561 "Hint: Do you have the raw file printing rules enabled?");
8562
8563 if (format)
8564 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
8565 "document-format", NULL, format->values[0].string.text);
8566
8567 return;
8568 }
8569
8570 if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype))
8571 {
8572 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
8573 filetype->type);
8574
8575 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
8576 _("Unsupported format \'%s\'!"), mimetype);
8577
8578 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
8579 "document-format", NULL, mimetype);
8580
8581 return;
8582 }
8583
8584 cupsdLogMessage(CUPSD_LOG_DEBUG,
8585 "send_document: request file type is %s/%s.",
8586 filetype->super, filetype->type);
8587
8588 /*
8589 * Add the file to the job...
8590 */
8591
8592 cupsdLoadJob(job);
8593
8594 if (add_file(con, job, filetype, compression))
8595 return;
8596
8597 if (stat(con->filename, &fileinfo))
8598 kbytes = 0;
8599 else
8600 kbytes = (fileinfo.st_size + 1023) / 1024;
8601
8602 cupsdUpdateQuota(printer, job->username, 0, kbytes);
8603
8604 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
8605 IPP_TAG_INTEGER)) != NULL)
8606 attr->values[0].integer += kbytes;
8607
8608 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
8609 job->num_files);
8610 rename(con->filename, filename);
8611
8612 cupsdClearString(&con->filename);
8613
8614 cupsdLogMessage(CUPSD_LOG_INFO,
8615 "File of type %s/%s queued in job #%d by \"%s\".",
8616 filetype->super, filetype->type, job->id, job->username);
8617
8618 /*
8619 * Start the job if this is the last document...
8620 */
8621
8622 if ((attr = ippFindAttribute(con->request, "last-document",
8623 IPP_TAG_BOOLEAN)) != NULL &&
8624 attr->values[0].boolean)
8625 {
8626 /*
8627 * See if we need to add the ending sheet...
8628 */
8629
8630 cupsdTimeoutJob(job);
8631
8632 if (job->state_value == IPP_JOB_STOPPED)
8633 {
8634 job->state->values[0].integer = IPP_JOB_PENDING;
8635 job->state_value = IPP_JOB_PENDING;
8636 }
8637 else if (job->state_value == IPP_JOB_HELD)
8638 {
8639 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
8640 IPP_TAG_KEYWORD)) == NULL)
8641 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
8642
8643 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
8644 {
8645 job->state->values[0].integer = IPP_JOB_PENDING;
8646 job->state_value = IPP_JOB_PENDING;
8647 }
8648 }
8649
8650 cupsdSaveJob(job);
8651
8652 /*
8653 * Start the job if possible... Since cupsdCheckJobs() can cancel a
8654 * job if it doesn't print, we need to re-find the job afterwards...
8655 */
8656
8657 jobid = job->id;
8658
8659 cupsdCheckJobs();
8660
8661 job = cupsdFindJob(jobid);
8662 }
8663 else
8664 {
8665 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
8666 IPP_TAG_KEYWORD)) == NULL)
8667 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
8668
8669 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
8670 {
8671 job->state->values[0].integer = IPP_JOB_HELD;
8672 job->state_value = IPP_JOB_HELD;
8673 job->hold_until = time(NULL) + 60;
8674 cupsdSaveJob(job);
8675 }
8676 }
8677
8678 /*
8679 * Fill in the response info...
8680 */
8681
8682 snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName,
8683 LocalPort, jobid);
8684
8685 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
8686 job_uri);
8687
8688 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid);
8689
8690 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
8691 job ? job->state_value : IPP_JOB_CANCELED);
8692 add_job_state_reasons(con, job);
8693
8694 con->response->request.status.status_code = IPP_OK;
8695 }
8696
8697
8698 /*
8699 * 'send_http_error()' - Send a HTTP error back to the IPP client.
8700 */
8701
8702 static void
8703 send_http_error(
8704 cupsd_client_t *con, /* I - Client connection */
8705 http_status_t status, /* I - HTTP status code */
8706 cupsd_printer_t *printer) /* I - Printer, if any */
8707 {
8708 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: %s",
8709 ippOpString(con->request->request.op.operation_id),
8710 httpStatus(status));
8711
8712 if (status == HTTP_UNAUTHORIZED &&
8713 printer && printer->num_auth_info_required > 0 &&
8714 !strcmp(printer->auth_info_required[0], "negotiate"))
8715 cupsdSendError(con, status, AUTH_NEGOTIATE);
8716 else
8717 cupsdSendError(con, status, AUTH_NONE);
8718
8719 ippDelete(con->response);
8720 con->response = NULL;
8721
8722 return;
8723 }
8724
8725
8726 /*
8727 * 'send_ipp_status()' - Send a status back to the IPP client.
8728 */
8729
8730 static void
8731 send_ipp_status(cupsd_client_t *con, /* I - Client connection */
8732 ipp_status_t status, /* I - IPP status code */
8733 const char *message, /* I - Status message */
8734 ...) /* I - Additional args as needed */
8735 {
8736 va_list ap; /* Pointer to additional args */
8737 char formatted[1024]; /* Formatted errror message */
8738
8739
8740 va_start(ap, message);
8741 vsnprintf(formatted, sizeof(formatted),
8742 _cupsLangString(con->language, message), ap);
8743 va_end(ap);
8744
8745 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s",
8746 ippOpString(con->request->request.op.operation_id),
8747 ippErrorString(status), formatted);
8748
8749 con->response->request.status.status_code = status;
8750
8751 if (ippFindAttribute(con->response, "attributes-charset",
8752 IPP_TAG_ZERO) == NULL)
8753 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
8754 "attributes-charset", NULL, DefaultCharset);
8755
8756 if (ippFindAttribute(con->response, "attributes-natural-language",
8757 IPP_TAG_ZERO) == NULL)
8758 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
8759 "attributes-natural-language", NULL, DefaultLanguage);
8760
8761 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT,
8762 "status-message", NULL, formatted);
8763 }
8764
8765
8766 /*
8767 * 'set_default()' - Set the default destination...
8768 */
8769
8770 static void
8771 set_default(cupsd_client_t *con, /* I - Client connection */
8772 ipp_attribute_t *uri) /* I - Printer URI */
8773 {
8774 http_status_t status; /* Policy status */
8775 cups_ptype_t dtype; /* Destination type (printer/class) */
8776 cupsd_printer_t *printer; /* Printer */
8777
8778
8779 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con,
8780 con->http.fd, uri->values[0].string.text);
8781
8782 /*
8783 * Is the destination valid?
8784 */
8785
8786 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8787 {
8788 /*
8789 * Bad URI...
8790 */
8791
8792 send_ipp_status(con, IPP_NOT_FOUND,
8793 _("The printer or class was not found."));
8794 return;
8795 }
8796
8797 /*
8798 * Check policy...
8799 */
8800
8801 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
8802 {
8803 send_http_error(con, status, NULL);
8804 return;
8805 }
8806
8807 /*
8808 * Set it as the default...
8809 */
8810
8811 DefaultPrinter = printer;
8812
8813 cupsdSaveAllPrinters();
8814 cupsdSaveAllClasses();
8815
8816 cupsdWritePrintcap();
8817
8818 cupsdLogMessage(CUPSD_LOG_INFO,
8819 "Default destination set to \"%s\" by \"%s\".",
8820 printer->name, get_username(con));
8821
8822 /*
8823 * Everything was ok, so return OK status...
8824 */
8825
8826 con->response->request.status.status_code = IPP_OK;
8827 }
8828
8829
8830 /*
8831 * 'set_job_attrs()' - Set job attributes.
8832 */
8833
8834 static void
8835 set_job_attrs(cupsd_client_t *con, /* I - Client connection */
8836 ipp_attribute_t *uri) /* I - Job URI */
8837 {
8838 ipp_attribute_t *attr, /* Current attribute */
8839 *attr2; /* Job attribute */
8840 int jobid; /* Job ID */
8841 cupsd_job_t *job; /* Current job */
8842 char method[HTTP_MAX_URI],
8843 /* Method portion of URI */
8844 username[HTTP_MAX_URI],
8845 /* Username portion of URI */
8846 host[HTTP_MAX_URI],
8847 /* Host portion of URI */
8848 resource[HTTP_MAX_URI];
8849 /* Resource portion of URI */
8850 int port; /* Port portion of URI */
8851 int event; /* Events? */
8852
8853
8854 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con,
8855 con->http.fd, uri->values[0].string.text);
8856
8857 /*
8858 * Start with "everything is OK" status...
8859 */
8860
8861 con->response->request.status.status_code = IPP_OK;
8862
8863 /*
8864 * See if we have a job URI or a printer URI...
8865 */
8866
8867 if (!strcmp(uri->name, "printer-uri"))
8868 {
8869 /*
8870 * Got a printer URI; see if we also have a job-id attribute...
8871 */
8872
8873 if ((attr = ippFindAttribute(con->request, "job-id",
8874 IPP_TAG_INTEGER)) == NULL)
8875 {
8876 send_ipp_status(con, IPP_BAD_REQUEST,
8877 _("Got a printer-uri attribute but no job-id!"));
8878 return;
8879 }
8880
8881 jobid = attr->values[0].integer;
8882 }
8883 else
8884 {
8885 /*
8886 * Got a job URI; parse it to get the job ID...
8887 */
8888
8889 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
8890 sizeof(method), username, sizeof(username), host,
8891 sizeof(host), &port, resource, sizeof(resource));
8892
8893 if (strncmp(resource, "/jobs/", 6))
8894 {
8895 /*
8896 * Not a valid URI!
8897 */
8898
8899 send_ipp_status(con, IPP_BAD_REQUEST,
8900 _("Bad job-uri attribute \"%s\"!"),
8901 uri->values[0].string.text);
8902 return;
8903 }
8904
8905 jobid = atoi(resource + 6);
8906 }
8907
8908 /*
8909 * See if the job exists...
8910 */
8911
8912 if ((job = cupsdFindJob(jobid)) == NULL)
8913 {
8914 /*
8915 * Nope - return a "not found" error...
8916 */
8917
8918 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
8919 return;
8920 }
8921
8922 /*
8923 * See if the job has been completed...
8924 */
8925
8926 if (job->state_value > IPP_JOB_STOPPED)
8927 {
8928 /*
8929 * Return a "not-possible" error...
8930 */
8931
8932 send_ipp_status(con, IPP_NOT_POSSIBLE,
8933 _("Job #%d is finished and cannot be altered!"), jobid);
8934 return;
8935 }
8936
8937 /*
8938 * See if the job is owned by the requesting user...
8939 */
8940
8941 if (!validate_user(job, con, job->username, username, sizeof(username)))
8942 {
8943 send_http_error(con, HTTP_UNAUTHORIZED, NULL);
8944 return;
8945 }
8946
8947 /*
8948 * See what the user wants to change.
8949 */
8950
8951 cupsdLoadJob(job);
8952
8953 event = 0;
8954
8955 for (attr = con->request->attrs; attr; attr = attr->next)
8956 {
8957 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
8958 continue;
8959
8960 if (!strcmp(attr->name, "attributes-charset") ||
8961 !strcmp(attr->name, "attributes-natural-language") ||
8962 !strcmp(attr->name, "document-compression") ||
8963 !strcmp(attr->name, "document-format") ||
8964 !strcmp(attr->name, "job-detailed-status-messages") ||
8965 !strcmp(attr->name, "job-document-access-errors") ||
8966 !strcmp(attr->name, "job-id") ||
8967 !strcmp(attr->name, "job-k-octets") ||
8968 !strcmp(attr->name, "job-originating-host-name") ||
8969 !strcmp(attr->name, "job-originating-user-name") ||
8970 !strcmp(attr->name, "job-printer-up-time") ||
8971 !strcmp(attr->name, "job-printer-uri") ||
8972 !strcmp(attr->name, "job-sheets") ||
8973 !strcmp(attr->name, "job-state-message") ||
8974 !strcmp(attr->name, "job-state-reasons") ||
8975 !strcmp(attr->name, "job-uri") ||
8976 !strcmp(attr->name, "number-of-documents") ||
8977 !strcmp(attr->name, "number-of-intervening-jobs") ||
8978 !strcmp(attr->name, "output-device-assigned") ||
8979 !strncmp(attr->name, "date-time-at-", 13) ||
8980 !strncmp(attr->name, "job-impressions", 15) ||
8981 !strncmp(attr->name, "job-k-octets", 12) ||
8982 !strncmp(attr->name, "job-media-sheets", 16) ||
8983 !strncmp(attr->name, "time-at-", 8))
8984 {
8985 /*
8986 * Read-only attrs!
8987 */
8988
8989 send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE,
8990 _("%s cannot be changed."), attr->name);
8991
8992 if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
8993 attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
8994
8995 continue;
8996 }
8997
8998 if (!strcmp(attr->name, "job-priority"))
8999 {
9000 /*
9001 * Change the job priority...
9002 */
9003
9004 if (attr->value_tag != IPP_TAG_INTEGER)
9005 {
9006 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value!"));
9007
9008 if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
9009 attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
9010 }
9011 else if (job->state_value >= IPP_JOB_PROCESSING)
9012 {
9013 send_ipp_status(con, IPP_NOT_POSSIBLE,
9014 _("Job is completed and cannot be changed."));
9015 return;
9016 }
9017 else if (con->response->request.status.status_code == IPP_OK)
9018 {
9019 cupsdSetJobPriority(job, attr->values[0].integer);
9020 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
9021 }
9022 }
9023 else if (!strcmp(attr->name, "job-state"))
9024 {
9025 /*
9026 * Change the job state...
9027 */
9028
9029 if (attr->value_tag != IPP_TAG_ENUM)
9030 {
9031 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value!"));
9032
9033 if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
9034 attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
9035 }
9036 else
9037 {
9038 switch (attr->values[0].integer)
9039 {
9040 case IPP_JOB_PENDING :
9041 case IPP_JOB_HELD :
9042 if (job->state_value > IPP_JOB_HELD)
9043 {
9044 send_ipp_status(con, IPP_NOT_POSSIBLE,
9045 _("Job state cannot be changed."));
9046 return;
9047 }
9048 else if (con->response->request.status.status_code == IPP_OK)
9049 {
9050 job->state->values[0].integer = attr->values[0].integer;
9051 job->state_value = (ipp_jstate_t)attr->values[0].integer;
9052
9053 event |= CUPSD_EVENT_JOB_STATE;
9054 }
9055 break;
9056
9057 case IPP_JOB_PROCESSING :
9058 case IPP_JOB_STOPPED :
9059 if (job->state_value != attr->values[0].integer)
9060 {
9061 send_ipp_status(con, IPP_NOT_POSSIBLE,
9062 _("Job state cannot be changed."));
9063 return;
9064 }
9065 break;
9066
9067 case IPP_JOB_CANCELED :
9068 case IPP_JOB_ABORTED :
9069 case IPP_JOB_COMPLETED :
9070 if (job->state_value > IPP_JOB_PROCESSING)
9071 {
9072 send_ipp_status(con, IPP_NOT_POSSIBLE,
9073 _("Job state cannot be changed."));
9074 return;
9075 }
9076 else if (con->response->request.status.status_code == IPP_OK)
9077 cupsdCancelJob(job, 0, (ipp_jstate_t)attr->values[0].integer);
9078 break;
9079 }
9080 }
9081 }
9082 else if (con->response->request.status.status_code != IPP_OK)
9083 continue;
9084 else if ((attr2 = ippFindAttribute(job->attrs, attr->name,
9085 IPP_TAG_ZERO)) != NULL)
9086 {
9087 /*
9088 * Some other value; first free the old value...
9089 */
9090
9091 if (job->attrs->prev)
9092 job->attrs->prev->next = attr2->next;
9093 else
9094 job->attrs->attrs = attr2->next;
9095
9096 if (job->attrs->last == attr2)
9097 job->attrs->last = job->attrs->prev;
9098
9099 _ippFreeAttr(attr2);
9100
9101 /*
9102 * Then copy the attribute...
9103 */
9104
9105 copy_attribute(job->attrs, attr, 0);
9106
9107 /*
9108 * See if the job-name or job-hold-until is being changed.
9109 */
9110
9111 if (!strcmp(attr->name, "job-hold-until"))
9112 {
9113 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
9114
9115 if (!strcmp(attr->values[0].string.text, "no-hold"))
9116 cupsdReleaseJob(job);
9117 else
9118 cupsdHoldJob(job);
9119
9120 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
9121 }
9122 }
9123 else if (attr->value_tag == IPP_TAG_DELETEATTR)
9124 {
9125 /*
9126 * Delete the attribute...
9127 */
9128
9129 if ((attr2 = ippFindAttribute(job->attrs, attr->name,
9130 IPP_TAG_ZERO)) != NULL)
9131 {
9132 if (job->attrs->prev)
9133 job->attrs->prev->next = attr2->next;
9134 else
9135 job->attrs->attrs = attr2->next;
9136
9137 if (attr2 == job->attrs->last)
9138 job->attrs->last = job->attrs->prev;
9139
9140 _ippFreeAttr(attr2);
9141
9142 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
9143 }
9144 }
9145 else
9146 {
9147 /*
9148 * Add new option by copying it...
9149 */
9150
9151 copy_attribute(job->attrs, attr, 0);
9152
9153 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
9154 }
9155 }
9156
9157 /*
9158 * Save the job...
9159 */
9160
9161 cupsdSaveJob(job);
9162
9163 /*
9164 * Send events as needed...
9165 */
9166
9167 if (event & CUPSD_EVENT_JOB_STATE)
9168 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job,
9169 job->state_value == IPP_JOB_HELD ?
9170 "Job held by user." : "Job restarted by user.");
9171
9172 if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED)
9173 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, job->printer, job,
9174 "Job options changed by user.");
9175
9176 /*
9177 * Start jobs if possible...
9178 */
9179
9180 cupsdCheckJobs();
9181 }
9182
9183
9184 /*
9185 * 'set_printer_defaults()' - Set printer default options from a request.
9186 */
9187
9188 static void
9189 set_printer_defaults(
9190 cupsd_client_t *con, /* I - Client connection */
9191 cupsd_printer_t *printer) /* I - Printer */
9192 {
9193 int i; /* Looping var */
9194 ipp_attribute_t *attr; /* Current attribute */
9195 int namelen; /* Length of attribute name */
9196 char name[256], /* New attribute name */
9197 value[256]; /* String version of integer attrs */
9198
9199
9200 for (attr = con->request->attrs; attr; attr = attr->next)
9201 {
9202 /*
9203 * Skip non-printer attributes...
9204 */
9205
9206 if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
9207 continue;
9208
9209 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name);
9210
9211 if (!strcmp(attr->name, "job-sheets-default"))
9212 {
9213 /*
9214 * Only allow keywords and names...
9215 */
9216
9217 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
9218 continue;
9219
9220 /*
9221 * Only allow job-sheets-default to be set when running without a
9222 * system high classification level...
9223 */
9224
9225 if (Classification)
9226 continue;
9227
9228 cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text);
9229
9230 if (attr->num_values > 1)
9231 cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text);
9232 else
9233 cupsdSetString(&printer->job_sheets[1], "none");
9234 }
9235 else if (!strcmp(attr->name, "requesting-user-name-allowed"))
9236 {
9237 cupsdFreePrinterUsers(printer);
9238
9239 printer->deny_users = 0;
9240
9241 if (attr->value_tag == IPP_TAG_NAME &&
9242 (attr->num_values > 1 ||
9243 strcmp(attr->values[0].string.text, "all")))
9244 {
9245 for (i = 0; i < attr->num_values; i ++)
9246 cupsdAddPrinterUser(printer, attr->values[i].string.text);
9247 }
9248 }
9249 else if (!strcmp(attr->name, "requesting-user-name-denied"))
9250 {
9251 cupsdFreePrinterUsers(printer);
9252
9253 printer->deny_users = 1;
9254
9255 if (attr->value_tag == IPP_TAG_NAME &&
9256 (attr->num_values > 1 ||
9257 strcmp(attr->values[0].string.text, "none")))
9258 {
9259 for (i = 0; i < attr->num_values; i ++)
9260 cupsdAddPrinterUser(printer, attr->values[i].string.text);
9261 }
9262 }
9263 else if (!strcmp(attr->name, "job-quota-period"))
9264 {
9265 if (attr->value_tag != IPP_TAG_INTEGER)
9266 continue;
9267
9268 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...",
9269 attr->values[0].integer);
9270 cupsdFreeQuotas(printer);
9271
9272 printer->quota_period = attr->values[0].integer;
9273 }
9274 else if (!strcmp(attr->name, "job-k-limit"))
9275 {
9276 if (attr->value_tag != IPP_TAG_INTEGER)
9277 continue;
9278
9279 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...",
9280 attr->values[0].integer);
9281 cupsdFreeQuotas(printer);
9282
9283 printer->k_limit = attr->values[0].integer;
9284 }
9285 else if (!strcmp(attr->name, "job-page-limit"))
9286 {
9287 if (attr->value_tag != IPP_TAG_INTEGER)
9288 continue;
9289
9290 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...",
9291 attr->values[0].integer);
9292 cupsdFreeQuotas(printer);
9293
9294 printer->page_limit = attr->values[0].integer;
9295 }
9296 else if (!strcmp(attr->name, "printer-op-policy"))
9297 {
9298 cupsd_policy_t *p; /* Policy */
9299
9300
9301 if (attr->value_tag != IPP_TAG_NAME)
9302 continue;
9303
9304 if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
9305 {
9306 cupsdLogMessage(CUPSD_LOG_DEBUG,
9307 "Setting printer-op-policy to \"%s\"...",
9308 attr->values[0].string.text);
9309 cupsdSetString(&printer->op_policy, attr->values[0].string.text);
9310 printer->op_policy_ptr = p;
9311 }
9312 else
9313 {
9314 send_ipp_status(con, IPP_NOT_POSSIBLE,
9315 _("Unknown printer-op-policy \"%s\"."),
9316 attr->values[0].string.text);
9317 return;
9318 }
9319 }
9320 else if (!strcmp(attr->name, "printer-error-policy"))
9321 {
9322 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
9323 continue;
9324
9325 if (strcmp(attr->values[0].string.text, "abort-job") &&
9326 strcmp(attr->values[0].string.text, "retry-job") &&
9327 strcmp(attr->values[0].string.text, "stop-printer"))
9328 {
9329 send_ipp_status(con, IPP_NOT_POSSIBLE,
9330 _("Unknown printer-error-policy \"%s\"."),
9331 attr->values[0].string.text);
9332 return;
9333 }
9334
9335 cupsdLogMessage(CUPSD_LOG_DEBUG,
9336 "Setting printer-error-policy to \"%s\"...",
9337 attr->values[0].string.text);
9338 cupsdSetString(&printer->error_policy, attr->values[0].string.text);
9339 }
9340
9341 /*
9342 * Skip any other non-default attributes...
9343 */
9344
9345 namelen = strlen(attr->name);
9346 if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") ||
9347 namelen > (sizeof(name) - 1) || attr->num_values != 1)
9348 continue;
9349
9350 /*
9351 * OK, anything else must be a user-defined default...
9352 */
9353
9354 strlcpy(name, attr->name, sizeof(name));
9355 name[namelen - 8] = '\0'; /* Strip "-default" */
9356
9357 switch (attr->value_tag)
9358 {
9359 case IPP_TAG_DELETEATTR :
9360 printer->num_options = cupsRemoveOption(name,
9361 printer->num_options,
9362 &(printer->options));
9363 cupsdLogMessage(CUPSD_LOG_DEBUG,
9364 "Deleting %s", attr->name);
9365 break;
9366
9367 case IPP_TAG_NAME :
9368 case IPP_TAG_KEYWORD :
9369 case IPP_TAG_URI :
9370 printer->num_options = cupsAddOption(name,
9371 attr->values[0].string.text,
9372 printer->num_options,
9373 &(printer->options));
9374 cupsdLogMessage(CUPSD_LOG_DEBUG,
9375 "Setting %s to \"%s\"...", attr->name,
9376 attr->values[0].string.text);
9377 break;
9378
9379 case IPP_TAG_BOOLEAN :
9380 printer->num_options = cupsAddOption(name,
9381 attr->values[0].boolean ?
9382 "true" : "false",
9383 printer->num_options,
9384 &(printer->options));
9385 cupsdLogMessage(CUPSD_LOG_DEBUG,
9386 "Setting %s to %s...", attr->name,
9387 attr->values[0].boolean ? "true" : "false");
9388 break;
9389
9390 case IPP_TAG_INTEGER :
9391 case IPP_TAG_ENUM :
9392 sprintf(value, "%d", attr->values[0].integer);
9393 printer->num_options = cupsAddOption(name, value,
9394 printer->num_options,
9395 &(printer->options));
9396 cupsdLogMessage(CUPSD_LOG_DEBUG,
9397 "Setting %s to %s...", attr->name, value);
9398 break;
9399
9400 case IPP_TAG_RANGE :
9401 sprintf(value, "%d-%d", attr->values[0].range.lower,
9402 attr->values[0].range.upper);
9403 printer->num_options = cupsAddOption(name, value,
9404 printer->num_options,
9405 &(printer->options));
9406 cupsdLogMessage(CUPSD_LOG_DEBUG,
9407 "Setting %s to %s...", attr->name, value);
9408 break;
9409
9410 case IPP_TAG_RESOLUTION :
9411 sprintf(value, "%dx%d%s", attr->values[0].resolution.xres,
9412 attr->values[0].resolution.yres,
9413 attr->values[0].resolution.units == IPP_RES_PER_INCH ?
9414 "dpi" : "dpc");
9415 printer->num_options = cupsAddOption(name, value,
9416 printer->num_options,
9417 &(printer->options));
9418 cupsdLogMessage(CUPSD_LOG_DEBUG,
9419 "Setting %s to %s...", attr->name, value);
9420 break;
9421
9422 default :
9423 /* Do nothing for other values */
9424 break;
9425 }
9426 }
9427 }
9428
9429
9430 /*
9431 * 'start_printer()' - Start a printer.
9432 */
9433
9434 static void
9435 start_printer(cupsd_client_t *con, /* I - Client connection */
9436 ipp_attribute_t *uri) /* I - Printer URI */
9437 {
9438 http_status_t status; /* Policy status */
9439 cups_ptype_t dtype; /* Destination type (printer/class) */
9440 cupsd_printer_t *printer; /* Printer data */
9441
9442
9443 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con,
9444 con->http.fd, uri->values[0].string.text);
9445
9446 /*
9447 * Is the destination valid?
9448 */
9449
9450 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
9451 {
9452 /*
9453 * Bad URI...
9454 */
9455
9456 send_ipp_status(con, IPP_NOT_FOUND,
9457 _("The printer or class was not found."));
9458 return;
9459 }
9460
9461 /*
9462 * Check policy...
9463 */
9464
9465 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
9466 {
9467 send_http_error(con, status, printer);
9468 return;
9469 }
9470
9471 /*
9472 * Start the printer...
9473 */
9474
9475 printer->state_message[0] = '\0';
9476
9477 cupsdStartPrinter(printer, 1);
9478
9479 if (dtype & CUPS_PRINTER_CLASS)
9480 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".",
9481 printer->name, get_username(con));
9482 else
9483 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".",
9484 printer->name, get_username(con));
9485
9486 cupsdCheckJobs();
9487
9488 /*
9489 * Everything was ok, so return OK status...
9490 */
9491
9492 con->response->request.status.status_code = IPP_OK;
9493 }
9494
9495
9496 /*
9497 * 'stop_printer()' - Stop a printer.
9498 */
9499
9500 static void
9501 stop_printer(cupsd_client_t *con, /* I - Client connection */
9502 ipp_attribute_t *uri) /* I - Printer URI */
9503 {
9504 http_status_t status; /* Policy status */
9505 cups_ptype_t dtype; /* Destination type (printer/class) */
9506 cupsd_printer_t *printer; /* Printer data */
9507 ipp_attribute_t *attr; /* printer-state-message attribute */
9508
9509
9510 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con,
9511 con->http.fd, uri->values[0].string.text);
9512
9513 /*
9514 * Is the destination valid?
9515 */
9516
9517 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
9518 {
9519 /*
9520 * Bad URI...
9521 */
9522
9523 send_ipp_status(con, IPP_NOT_FOUND,
9524 _("The printer or class was not found."));
9525 return;
9526 }
9527
9528 /*
9529 * Check policy...
9530 */
9531
9532 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
9533 {
9534 send_http_error(con, status, printer);
9535 return;
9536 }
9537
9538 /*
9539 * Stop the printer...
9540 */
9541
9542 if ((attr = ippFindAttribute(con->request, "printer-state-message",
9543 IPP_TAG_TEXT)) == NULL)
9544 strcpy(printer->state_message, "Paused");
9545 else
9546 {
9547 strlcpy(printer->state_message, attr->values[0].string.text,
9548 sizeof(printer->state_message));
9549 }
9550
9551 cupsdStopPrinter(printer, 1);
9552
9553 if (dtype & CUPS_PRINTER_CLASS)
9554 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".",
9555 printer->name, get_username(con));
9556 else
9557 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".",
9558 printer->name, get_username(con));
9559
9560 /*
9561 * Everything was ok, so return OK status...
9562 */
9563
9564 con->response->request.status.status_code = IPP_OK;
9565 }
9566
9567
9568 /*
9569 * 'url_encode_attr()' - URL-encode a string attribute.
9570 */
9571
9572 static void
9573 url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */
9574 char *buffer,/* I - String buffer */
9575 int bufsize)/* I - Size of buffer */
9576 {
9577 int i; /* Looping var */
9578 char *bufptr, /* Pointer into buffer */
9579 *bufend; /* End of buffer */
9580
9581
9582 strlcpy(buffer, attr->name, bufsize);
9583 bufptr = buffer + strlen(buffer);
9584 bufend = buffer + bufsize - 1;
9585
9586 for (i = 0; i < attr->num_values; i ++)
9587 {
9588 if (bufptr >= bufend)
9589 break;
9590
9591 if (i)
9592 *bufptr++ = ',';
9593 else
9594 *bufptr++ = '=';
9595
9596 if (bufptr >= bufend)
9597 break;
9598
9599 *bufptr++ = '\'';
9600
9601 bufptr = url_encode_string(attr->values[i].string.text,
9602 bufptr, bufend - bufptr + 1);
9603
9604 if (bufptr >= bufend)
9605 break;
9606
9607 *bufptr++ = '\'';
9608 }
9609
9610 *bufptr = '\0';
9611 }
9612
9613
9614 /*
9615 * 'url_encode_string()' - URL-encode a string.
9616 */
9617
9618 static char * /* O - End of string */
9619 url_encode_string(const char *s, /* I - String */
9620 char *buffer, /* I - String buffer */
9621 int bufsize) /* I - Size of buffer */
9622 {
9623 char *bufptr, /* Pointer into buffer */
9624 *bufend; /* End of buffer */
9625 static const char *hex = "0123456789ABCDEF";
9626 /* Hex digits */
9627
9628
9629 bufptr = buffer;
9630 bufend = buffer + bufsize - 1;
9631
9632 while (*s && bufptr < bufend)
9633 {
9634 if (*s == ' ' || *s == '%')
9635 {
9636 if (bufptr >= (bufend - 2))
9637 break;
9638
9639 *bufptr++ = '%';
9640 *bufptr++ = hex[(*s >> 4) & 15];
9641 *bufptr++ = hex[*s & 15];
9642
9643 s ++;
9644 }
9645 else if (*s == '\'' || *s == '\\')
9646 {
9647 if (bufptr >= (bufend - 1))
9648 break;
9649
9650 *bufptr++ = '\\';
9651 *bufptr++ = *s++;
9652 }
9653 else
9654 *bufptr++ = *s++;
9655 }
9656
9657 *bufptr = '\0';
9658
9659 return (bufptr);
9660 }
9661
9662
9663 /*
9664 * 'user_allowed()' - See if a user is allowed to print to a queue.
9665 */
9666
9667 static int /* O - 0 if not allowed, 1 if allowed */
9668 user_allowed(cupsd_printer_t *p, /* I - Printer or class */
9669 const char *username) /* I - Username */
9670 {
9671 int i; /* Looping var */
9672 struct passwd *pw; /* User password data */
9673
9674
9675 if (p->num_users == 0)
9676 return (1);
9677
9678 if (!strcmp(username, "root"))
9679 return (1);
9680
9681 pw = getpwnam(username);
9682 endpwent();
9683
9684 for (i = 0; i < p->num_users; i ++)
9685 {
9686 if (p->users[i][0] == '@')
9687 {
9688 /*
9689 * Check group membership...
9690 */
9691
9692 if (cupsdCheckGroup(username, pw, p->users[i] + 1))
9693 break;
9694 }
9695 else if (!strcasecmp(username, p->users[i]))
9696 break;
9697 }
9698
9699 return ((i < p->num_users) != p->deny_users);
9700 }
9701
9702
9703 /*
9704 * 'validate_job()' - Validate printer options and destination.
9705 */
9706
9707 static void
9708 validate_job(cupsd_client_t *con, /* I - Client connection */
9709 ipp_attribute_t *uri) /* I - Printer URI */
9710 {
9711 http_status_t status; /* Policy status */
9712 ipp_attribute_t *attr; /* Current attribute */
9713 ipp_attribute_t *format; /* Document-format attribute */
9714 cups_ptype_t dtype; /* Destination type (printer/class) */
9715 char super[MIME_MAX_SUPER],
9716 /* Supertype of file */
9717 type[MIME_MAX_TYPE];
9718 /* Subtype of file */
9719 cupsd_printer_t *printer; /* Printer */
9720
9721
9722 cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con,
9723 con->http.fd, uri->values[0].string.text);
9724
9725 /*
9726 * OK, see if the client is sending the document compressed - CUPS
9727 * doesn't support compression yet...
9728 */
9729
9730 if ((attr = ippFindAttribute(con->request, "compression",
9731 IPP_TAG_KEYWORD)) != NULL &&
9732 !strcmp(attr->values[0].string.text, "none"))
9733 {
9734 send_ipp_status(con, IPP_ATTRIBUTES,
9735 _("Unsupported compression attribute %s!"),
9736 attr->values[0].string.text);
9737 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
9738 "compression", NULL, attr->values[0].string.text);
9739 return;
9740 }
9741
9742 /*
9743 * Is it a format we support?
9744 */
9745
9746 if ((format = ippFindAttribute(con->request, "document-format",
9747 IPP_TAG_MIMETYPE)) != NULL)
9748 {
9749 if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
9750 super, type) != 2)
9751 {
9752 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"),
9753 format->values[0].string.text);
9754 return;
9755 }
9756
9757 if ((strcmp(super, "application") || strcmp(type, "octet-stream")) &&
9758 !mimeType(MimeDatabase, super, type))
9759 {
9760 cupsdLogMessage(CUPSD_LOG_INFO,
9761 "Hint: Do you have the raw file printing rules enabled?");
9762 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9763 _("Unsupported format \"%s\"!"),
9764 format->values[0].string.text);
9765 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9766 "document-format", NULL, format->values[0].string.text);
9767 return;
9768 }
9769 }
9770
9771 /*
9772 * Is the destination valid?
9773 */
9774
9775 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
9776 {
9777 /*
9778 * Bad URI...
9779 */
9780
9781 send_ipp_status(con, IPP_NOT_FOUND,
9782 _("The printer or class was not found."));
9783 return;
9784 }
9785
9786 /*
9787 * Check policy...
9788 */
9789
9790 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
9791 {
9792 send_http_error(con, status, printer);
9793 return;
9794 }
9795
9796 /*
9797 * Everything was ok, so return OK status...
9798 */
9799
9800 con->response->request.status.status_code = IPP_OK;
9801 }
9802
9803
9804 /*
9805 * 'validate_name()' - Make sure the printer name only contains valid chars.
9806 */
9807
9808 static int /* O - 0 if name is no good, 1 if good */
9809 validate_name(const char *name) /* I - Name to check */
9810 {
9811 const char *ptr; /* Pointer into name */
9812
9813
9814 /*
9815 * Scan the whole name...
9816 */
9817
9818 for (ptr = name; *ptr; ptr ++)
9819 if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
9820 return (0);
9821
9822 /*
9823 * All the characters are good; validate the length, too...
9824 */
9825
9826 return ((ptr - name) < 128);
9827 }
9828
9829
9830 /*
9831 * 'validate_user()' - Validate the user for the request.
9832 */
9833
9834 static int /* O - 1 if permitted, 0 otherwise */
9835 validate_user(cupsd_job_t *job, /* I - Job */
9836 cupsd_client_t *con, /* I - Client connection */
9837 const char *owner, /* I - Owner of job/resource */
9838 char *username, /* O - Authenticated username */
9839 int userlen) /* I - Length of username */
9840 {
9841 cupsd_printer_t *printer; /* Printer for job */
9842
9843
9844 cupsdLogMessage(CUPSD_LOG_DEBUG2,
9845 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
9846 "userlen=%d)",
9847 job ? job->id : 0, con->http.fd, owner ? owner : "(null)",
9848 username, userlen);
9849
9850 /*
9851 * Validate input...
9852 */
9853
9854 if (!con || !owner || !username || userlen <= 0)
9855 return (0);
9856
9857 /*
9858 * Get the best authenticated username that is available.
9859 */
9860
9861 strlcpy(username, get_username(con), userlen);
9862
9863 /*
9864 * Check the username against the owner...
9865 */
9866
9867 printer = cupsdFindDest(job->dest);
9868
9869 return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr,
9870 con, owner) == HTTP_OK);
9871 }
9872
9873
9874 /*
9875 * End of "$Id: ipp.c 6689 2007-07-18 23:52:15Z mike $".
9876 */