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