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