]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/ipp.c
Fix another CUPS-Create-Local-Printer crash (Issue #5290)
[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 _cupsRWLockWrite(&printer->lock);
5364
5365 if ((!printer->info || !*(printer->info)) && (attr = ippFindAttribute(response, "printer-info", IPP_TAG_TEXT)) != NULL)
5366 cupsdSetString(&printer->info, ippGetString(attr, 0, NULL));
5367
5368 if ((!printer->location || !*(printer->location)) && (attr = ippFindAttribute(response, "printer-location", IPP_TAG_TEXT)) != NULL)
5369 cupsdSetString(&printer->location, ippGetString(attr, 0, NULL));
5370
5371 if ((!printer->geo_location || !*(printer->geo_location)) && (attr = ippFindAttribute(response, "printer-geo-location", IPP_TAG_URI)) != NULL)
5372 cupsdSetString(&printer->geo_location, ippGetString(attr, 0, NULL));
5373
5374 _cupsRWUnlock(&printer->lock);
5375
5376 if ((from = cupsFileOpen(fromppd, "r")) == NULL)
5377 {
5378 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to read generated PPD: %s", printer->name, strerror(errno));
5379 return (NULL);
5380 }
5381
5382 snprintf(toppd, sizeof(toppd), "%s/ppd/%s.ppd", ServerRoot, printer->name);
5383 if ((to = cupsdCreateConfFile(toppd, ConfigFilePerm)) == NULL)
5384 {
5385 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to create PPD for printer: %s", printer->name, strerror(errno));
5386 cupsFileClose(from);
5387 return (NULL);
5388 }
5389
5390 while (cupsFileGets(from, line, sizeof(line)))
5391 cupsFilePrintf(to, "%s\n", line);
5392
5393 cupsFileClose(from);
5394 if (!cupsdCloseCreatedConfFile(to, toppd))
5395 {
5396 printer->config_time = time(NULL);
5397 printer->state = IPP_PSTATE_IDLE;
5398 printer->accepting = 1;
5399
5400 cupsdSetPrinterAttrs(printer);
5401
5402 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL, "Printer \"%s\" is now available.", printer->name);
5403 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" is now available.", printer->name);
5404 }
5405 }
5406 else
5407 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: PPD creation failed: %s", printer->name, cupsLastErrorString());
5408
5409 return (NULL);
5410 }
5411
5412
5413 /*
5414 * 'create_local_printer()' - Create a local (temporary) print queue.
5415 */
5416
5417 static void
5418 create_local_printer(
5419 cupsd_client_t *con) /* I - Client connection */
5420 {
5421 ipp_attribute_t *device_uri, /* device-uri attribute */
5422 *printer_geo_location, /* printer-geo-location attribute */
5423 *printer_info, /* printer-info attribute */
5424 *printer_location, /* printer-location attribute */
5425 *printer_name; /* printer-name attribute */
5426 cupsd_printer_t *printer; /* New printer */
5427 http_status_t status; /* Policy status */
5428 char name[128], /* Sanitized printer name */
5429 *nameptr, /* Pointer into name */
5430 uri[1024]; /* printer-uri-supported value */
5431 const char *ptr; /* Pointer into attribute value */
5432
5433
5434 /*
5435 * Require local access to create a local printer...
5436 */
5437
5438 if (!httpAddrLocalhost(httpGetAddress(con->http)))
5439 {
5440 send_ipp_status(con, IPP_STATUS_ERROR_FORBIDDEN, _("Only local users can create a local printer."));
5441 return;
5442 }
5443
5444 /*
5445 * Check any other policy limits...
5446 */
5447
5448 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5449 {
5450 send_http_error(con, status, NULL);
5451 return;
5452 }
5453
5454 /*
5455 * Grab needed attributes...
5456 */
5457
5458 if ((printer_name = ippFindAttribute(con->request, "printer-name", IPP_TAG_ZERO)) == NULL || ippGetGroupTag(printer_name) != IPP_TAG_PRINTER || ippGetValueTag(printer_name) != IPP_TAG_NAME)
5459 {
5460 if (!printer_name)
5461 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Missing required attribute \"%s\"."), "printer-name");
5462 else if (ippGetGroupTag(printer_name) != IPP_TAG_PRINTER)
5463 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is in the wrong group."), "printer-name");
5464 else
5465 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is the wrong value type."), "printer-name");
5466
5467 return;
5468 }
5469
5470 for (nameptr = name, ptr = ippGetString(printer_name, 0, NULL); *ptr && nameptr < (name + sizeof(name) - 1); ptr ++)
5471 {
5472 /*
5473 * Sanitize the printer name...
5474 */
5475
5476 if (_cups_isalnum(*ptr))
5477 *nameptr++ = *ptr;
5478 else if (nameptr == name || nameptr[-1] != '_')
5479 *nameptr++ = '_';
5480 }
5481
5482 *nameptr = '\0';
5483
5484 if ((device_uri = ippFindAttribute(con->request, "device-uri", IPP_TAG_ZERO)) == NULL || ippGetGroupTag(device_uri) != IPP_TAG_PRINTER || ippGetValueTag(device_uri) != IPP_TAG_URI)
5485 {
5486 if (!device_uri)
5487 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Missing required attribute \"%s\"."), "device-uri");
5488 else if (ippGetGroupTag(device_uri) != IPP_TAG_PRINTER)
5489 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is in the wrong group."), "device-uri");
5490 else
5491 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is the wrong value type."), "device-uri");
5492
5493 return;
5494 }
5495
5496 printer_geo_location = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI);
5497 printer_info = ippFindAttribute(con->request, "printer-info", IPP_TAG_TEXT);
5498 printer_location = ippFindAttribute(con->request, "printer-location", IPP_TAG_TEXT);
5499
5500 /*
5501 * See if the printer already exists...
5502 */
5503
5504 if ((printer = cupsdFindDest(name)) != NULL)
5505 {
5506 send_ipp_status(con, IPP_STATUS_ERROR_NOT_POSSIBLE, _("Printer \"%s\" already exists."), name);
5507 goto add_printer_attributes;
5508 }
5509
5510 /*
5511 * Create the printer...
5512 */
5513
5514 if ((printer = cupsdAddPrinter(name)) == NULL)
5515 {
5516 send_ipp_status(con, IPP_STATUS_ERROR_INTERNAL, _("Unable to create printer."));
5517 return;
5518 }
5519
5520 printer->shared = 0;
5521 printer->temporary = 1;
5522
5523 cupsdSetDeviceURI(printer, ippGetString(device_uri, 0, NULL));
5524
5525 if (printer_geo_location)
5526 cupsdSetString(&printer->geo_location, ippGetString(printer_geo_location, 0, NULL));
5527 if (printer_info)
5528 cupsdSetString(&printer->info, ippGetString(printer_info, 0, NULL));
5529 if (printer_location)
5530 cupsdSetString(&printer->location, ippGetString(printer_location, 0, NULL));
5531
5532 cupsdSetPrinterAttrs(printer);
5533
5534 /*
5535 * Run a background thread to create the PPD...
5536 */
5537
5538 _cupsThreadCreate((_cups_thread_func_t)create_local_bg_thread, printer);
5539
5540 /*
5541 * Return printer attributes...
5542 */
5543
5544 send_ipp_status(con, IPP_STATUS_OK, _("Local printer created."));
5545
5546 add_printer_attributes:
5547
5548 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", (char)printer->accepting);
5549 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", printer->state);
5550 add_printer_state_reasons(con, printer);
5551
5552 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), httpIsEncrypted(con->http) ? "ipps" : "ipp", NULL, con->clientname, con->clientport, "/printers/%s", printer->name);
5553 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-uri-supported", NULL, uri);
5554 }
5555
5556
5557 /*
5558 * 'create_requested_array()' - Create an array for the requested-attributes.
5559 */
5560
5561 static cups_array_t * /* O - Array of attributes or NULL */
5562 create_requested_array(ipp_t *request) /* I - IPP request */
5563 {
5564 cups_array_t *ra; /* Requested attributes array */
5565
5566
5567 /*
5568 * Create the array for standard attributes...
5569 */
5570
5571 ra = ippCreateRequestedArray(request);
5572
5573 /*
5574 * Add CUPS defaults as needed...
5575 */
5576
5577 if (cupsArrayFind(ra, "printer-defaults"))
5578 {
5579 /*
5580 * Include user-set defaults...
5581 */
5582
5583 char *name; /* Option name */
5584
5585 cupsArrayRemove(ra, "printer-defaults");
5586
5587 for (name = (char *)cupsArrayFirst(CommonDefaults);
5588 name;
5589 name = (char *)cupsArrayNext(CommonDefaults))
5590 if (!cupsArrayFind(ra, name))
5591 cupsArrayAdd(ra, name);
5592 }
5593
5594 return (ra);
5595 }
5596
5597
5598 /*
5599 * 'create_subscriptions()' - Create one or more notification subscriptions.
5600 */
5601
5602 static void
5603 create_subscriptions(
5604 cupsd_client_t *con, /* I - Client connection */
5605 ipp_attribute_t *uri) /* I - Printer URI */
5606 {
5607 http_status_t status; /* Policy status */
5608 int i; /* Looping var */
5609 ipp_attribute_t *attr; /* Current attribute */
5610 cups_ptype_t dtype; /* Destination type (printer/class) */
5611 char scheme[HTTP_MAX_URI],
5612 /* Scheme portion of URI */
5613 userpass[HTTP_MAX_URI],
5614 /* Username portion of URI */
5615 host[HTTP_MAX_URI],
5616 /* Host portion of URI */
5617 resource[HTTP_MAX_URI];
5618 /* Resource portion of URI */
5619 int port; /* Port portion of URI */
5620 cupsd_printer_t *printer; /* Printer/class */
5621 cupsd_job_t *job; /* Job */
5622 int jobid; /* Job ID */
5623 cupsd_subscription_t *sub; /* Subscription object */
5624 const char *username, /* requesting-user-name or
5625 authenticated username */
5626 *recipient, /* notify-recipient-uri */
5627 *pullmethod; /* notify-pull-method */
5628 ipp_attribute_t *user_data; /* notify-user-data */
5629 int interval, /* notify-time-interval */
5630 lease; /* notify-lease-duration */
5631 unsigned mask; /* notify-events */
5632 ipp_attribute_t *notify_events,/* notify-events(-default) */
5633 *notify_lease; /* notify-lease-duration(-default) */
5634
5635
5636 #ifdef DEBUG
5637 for (attr = con->request->attrs; attr; attr = attr->next)
5638 {
5639 if (attr->group_tag != IPP_TAG_ZERO)
5640 cupsdLogMessage(CUPSD_LOG_DEBUG2, "g%04x v%04x %s", attr->group_tag,
5641 attr->value_tag, attr->name);
5642 else
5643 cupsdLogMessage(CUPSD_LOG_DEBUG2, "----SEP----");
5644 }
5645 #endif /* DEBUG */
5646
5647 /*
5648 * Is the destination valid?
5649 */
5650
5651 cupsdLogMessage(CUPSD_LOG_DEBUG, "create_subscriptions(con=%p(%d), uri=\"%s\")", con, con->number, uri->values[0].string.text);
5652
5653 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
5654 sizeof(scheme), userpass, sizeof(userpass), host,
5655 sizeof(host), &port, resource, sizeof(resource));
5656
5657 if (!strcmp(resource, "/"))
5658 {
5659 dtype = (cups_ptype_t)0;
5660 printer = NULL;
5661 }
5662 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
5663 {
5664 dtype = (cups_ptype_t)0;
5665 printer = NULL;
5666 }
5667 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
5668 {
5669 dtype = CUPS_PRINTER_CLASS;
5670 printer = NULL;
5671 }
5672 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
5673 {
5674 /*
5675 * Bad URI...
5676 */
5677
5678 send_ipp_status(con, IPP_NOT_FOUND,
5679 _("The printer or class does not exist."));
5680 return;
5681 }
5682
5683 /*
5684 * Check policy...
5685 */
5686
5687 if (printer)
5688 {
5689 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
5690 NULL)) != HTTP_OK)
5691 {
5692 send_http_error(con, status, printer);
5693 return;
5694 }
5695 }
5696 else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5697 {
5698 send_http_error(con, status, NULL);
5699 return;
5700 }
5701
5702 /*
5703 * Get the user that is requesting the subscription...
5704 */
5705
5706 username = get_username(con);
5707
5708 /*
5709 * Find the first subscription group attribute; return if we have
5710 * none...
5711 */
5712
5713 for (attr = con->request->attrs; attr; attr = attr->next)
5714 if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
5715 break;
5716
5717 if (!attr)
5718 {
5719 send_ipp_status(con, IPP_BAD_REQUEST,
5720 _("No subscription attributes in request."));
5721 return;
5722 }
5723
5724 /*
5725 * Process the subscription attributes in the request...
5726 */
5727
5728 con->response->request.status.status_code = IPP_BAD_REQUEST;
5729
5730 while (attr)
5731 {
5732 recipient = NULL;
5733 pullmethod = NULL;
5734 user_data = NULL;
5735 interval = 0;
5736 lease = DefaultLeaseDuration;
5737 jobid = 0;
5738 mask = CUPSD_EVENT_NONE;
5739
5740 if (printer)
5741 {
5742 notify_events = ippFindAttribute(printer->attrs, "notify-events-default",
5743 IPP_TAG_KEYWORD);
5744 notify_lease = ippFindAttribute(printer->attrs,
5745 "notify-lease-duration-default",
5746 IPP_TAG_INTEGER);
5747
5748 if (notify_lease)
5749 lease = notify_lease->values[0].integer;
5750 }
5751 else
5752 {
5753 notify_events = NULL;
5754 notify_lease = NULL;
5755 }
5756
5757 while (attr && attr->group_tag != IPP_TAG_ZERO)
5758 {
5759 if (!strcmp(attr->name, "notify-recipient-uri") &&
5760 attr->value_tag == IPP_TAG_URI)
5761 {
5762 /*
5763 * Validate the recipient scheme against the ServerBin/notifier
5764 * directory...
5765 */
5766
5767 char notifier[1024]; /* Notifier filename */
5768
5769
5770 recipient = attr->values[0].string.text;
5771
5772 if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient,
5773 scheme, sizeof(scheme), userpass, sizeof(userpass),
5774 host, sizeof(host), &port,
5775 resource, sizeof(resource)) < HTTP_URI_OK)
5776 {
5777 send_ipp_status(con, IPP_NOT_POSSIBLE,
5778 _("Bad notify-recipient-uri \"%s\"."), recipient);
5779 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5780 "notify-status-code", IPP_URI_SCHEME);
5781 return;
5782 }
5783
5784 snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
5785 scheme);
5786 if (access(notifier, X_OK))
5787 {
5788 send_ipp_status(con, IPP_NOT_POSSIBLE,
5789 _("notify-recipient-uri URI \"%s\" uses unknown "
5790 "scheme."), recipient);
5791 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5792 "notify-status-code", IPP_URI_SCHEME);
5793 return;
5794 }
5795
5796 if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient))
5797 {
5798 send_ipp_status(con, IPP_NOT_POSSIBLE,
5799 _("notify-recipient-uri URI \"%s\" is already used."),
5800 recipient);
5801 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5802 "notify-status-code", IPP_ATTRIBUTES);
5803 return;
5804 }
5805 }
5806 else if (!strcmp(attr->name, "notify-pull-method") &&
5807 attr->value_tag == IPP_TAG_KEYWORD)
5808 {
5809 pullmethod = attr->values[0].string.text;
5810
5811 if (strcmp(pullmethod, "ippget"))
5812 {
5813 send_ipp_status(con, IPP_NOT_POSSIBLE,
5814 _("Bad notify-pull-method \"%s\"."), pullmethod);
5815 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5816 "notify-status-code", IPP_ATTRIBUTES);
5817 return;
5818 }
5819 }
5820 else if (!strcmp(attr->name, "notify-charset") &&
5821 attr->value_tag == IPP_TAG_CHARSET &&
5822 strcmp(attr->values[0].string.text, "us-ascii") &&
5823 strcmp(attr->values[0].string.text, "utf-8"))
5824 {
5825 send_ipp_status(con, IPP_CHARSET,
5826 _("Character set \"%s\" not supported."),
5827 attr->values[0].string.text);
5828 return;
5829 }
5830 else if (!strcmp(attr->name, "notify-natural-language") &&
5831 (attr->value_tag != IPP_TAG_LANGUAGE ||
5832 strcmp(attr->values[0].string.text, DefaultLanguage)))
5833 {
5834 send_ipp_status(con, IPP_CHARSET,
5835 _("Language \"%s\" not supported."),
5836 attr->values[0].string.text);
5837 return;
5838 }
5839 else if (!strcmp(attr->name, "notify-user-data") &&
5840 attr->value_tag == IPP_TAG_STRING)
5841 {
5842 if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
5843 {
5844 send_ipp_status(con, IPP_REQUEST_VALUE,
5845 _("The notify-user-data value is too large "
5846 "(%d > 63 octets)."),
5847 attr->values[0].unknown.length);
5848 return;
5849 }
5850
5851 user_data = attr;
5852 }
5853 else if (!strcmp(attr->name, "notify-events") &&
5854 attr->value_tag == IPP_TAG_KEYWORD)
5855 notify_events = attr;
5856 else if (!strcmp(attr->name, "notify-lease-duration") &&
5857 attr->value_tag == IPP_TAG_INTEGER)
5858 lease = attr->values[0].integer;
5859 else if (!strcmp(attr->name, "notify-time-interval") &&
5860 attr->value_tag == IPP_TAG_INTEGER)
5861 interval = attr->values[0].integer;
5862 else if (!strcmp(attr->name, "notify-job-id") &&
5863 attr->value_tag == IPP_TAG_INTEGER)
5864 jobid = attr->values[0].integer;
5865
5866 attr = attr->next;
5867 }
5868
5869 if (notify_events)
5870 {
5871 for (i = 0; i < notify_events->num_values; i ++)
5872 mask |= cupsdEventValue(notify_events->values[i].string.text);
5873 }
5874
5875 if (recipient)
5876 cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient);
5877 if (pullmethod)
5878 cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod);
5879 cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease);
5880 cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval);
5881
5882 if (!recipient && !pullmethod)
5883 break;
5884
5885 if (mask == CUPSD_EVENT_NONE)
5886 {
5887 if (jobid)
5888 mask = CUPSD_EVENT_JOB_COMPLETED;
5889 else if (printer)
5890 mask = CUPSD_EVENT_PRINTER_STATE_CHANGED;
5891 else
5892 {
5893 send_ipp_status(con, IPP_BAD_REQUEST,
5894 _("notify-events not specified."));
5895 return;
5896 }
5897 }
5898
5899 if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration))
5900 {
5901 cupsdLogMessage(CUPSD_LOG_INFO,
5902 "create_subscriptions: Limiting notify-lease-duration to "
5903 "%d seconds.",
5904 MaxLeaseDuration);
5905 lease = MaxLeaseDuration;
5906 }
5907
5908 if (jobid)
5909 {
5910 if ((job = cupsdFindJob(jobid)) == NULL)
5911 {
5912 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
5913 jobid);
5914 return;
5915 }
5916 }
5917 else
5918 job = NULL;
5919
5920 if ((sub = cupsdAddSubscription(mask, printer, job, recipient, 0)) == NULL)
5921 {
5922 send_ipp_status(con, IPP_TOO_MANY_SUBSCRIPTIONS,
5923 _("There are too many subscriptions."));
5924 return;
5925 }
5926
5927 if (job)
5928 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription #%d for job %d.",
5929 sub->id, job->id);
5930 else if (printer)
5931 cupsdLogMessage(CUPSD_LOG_DEBUG,
5932 "Added subscription #%d for printer \"%s\".",
5933 sub->id, printer->name);
5934 else
5935 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription #%d for server.",
5936 sub->id);
5937
5938 sub->interval = interval;
5939 sub->lease = lease;
5940 sub->expire = lease ? time(NULL) + lease : 0;
5941
5942 cupsdSetString(&sub->owner, username);
5943
5944 if (user_data)
5945 {
5946 sub->user_data_len = user_data->values[0].unknown.length;
5947 memcpy(sub->user_data, user_data->values[0].unknown.data,
5948 (size_t)sub->user_data_len);
5949 }
5950
5951 ippAddSeparator(con->response);
5952 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5953 "notify-subscription-id", sub->id);
5954
5955 con->response->request.status.status_code = IPP_OK;
5956
5957 if (attr)
5958 attr = attr->next;
5959 }
5960
5961 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
5962 }
5963
5964
5965 /*
5966 * 'delete_printer()' - Remove a printer or class from the system.
5967 */
5968
5969 static void
5970 delete_printer(cupsd_client_t *con, /* I - Client connection */
5971 ipp_attribute_t *uri) /* I - URI of printer or class */
5972 {
5973 http_status_t status; /* Policy status */
5974 cups_ptype_t dtype; /* Destination type (printer/class) */
5975 cupsd_printer_t *printer; /* Printer/class */
5976 char filename[1024]; /* Script/PPD filename */
5977 int temporary; /* Temporary queue? */
5978
5979
5980 cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con,
5981 con->number, uri->values[0].string.text);
5982
5983 /*
5984 * Do we have a valid URI?
5985 */
5986
5987 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
5988 {
5989 /*
5990 * Bad URI...
5991 */
5992
5993 send_ipp_status(con, IPP_NOT_FOUND,
5994 _("The printer or class does not exist."));
5995 return;
5996 }
5997
5998 /*
5999 * Check policy...
6000 */
6001
6002 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6003 {
6004 send_http_error(con, status, NULL);
6005 return;
6006 }
6007
6008 /*
6009 * Remove old jobs...
6010 */
6011
6012 cupsdCancelJobs(printer->name, NULL, 1);
6013
6014 /*
6015 * Remove old subscriptions and send a "deleted printer" event...
6016 */
6017
6018 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL,
6019 "%s \"%s\" deleted by \"%s\".",
6020 (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
6021 printer->name, get_username(con));
6022
6023 cupsdExpireSubscriptions(printer, NULL);
6024
6025 /*
6026 * Remove any old PPD or script files...
6027 */
6028
6029 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
6030 printer->name);
6031 unlink(filename);
6032 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd.O", ServerRoot,
6033 printer->name);
6034 unlink(filename);
6035
6036 snprintf(filename, sizeof(filename), "%s/%s.png", CacheDir, printer->name);
6037 unlink(filename);
6038
6039 snprintf(filename, sizeof(filename), "%s/%s.data", CacheDir, printer->name);
6040 unlink(filename);
6041
6042 /*
6043 * Unregister color profiles...
6044 */
6045
6046 cupsdUnregisterColor(printer);
6047
6048 temporary = printer->temporary;
6049
6050 if (dtype & CUPS_PRINTER_CLASS)
6051 {
6052 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".",
6053 printer->name, get_username(con));
6054
6055 cupsdDeletePrinter(printer, 0);
6056 if (!temporary)
6057 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
6058 }
6059 else
6060 {
6061 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".",
6062 printer->name, get_username(con));
6063
6064 if (cupsdDeletePrinter(printer, 0) && !temporary)
6065 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
6066
6067 if (!temporary)
6068 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
6069 }
6070
6071 if (!temporary)
6072 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
6073
6074 /*
6075 * Return with no errors...
6076 */
6077
6078 con->response->request.status.status_code = IPP_OK;
6079 }
6080
6081
6082 /*
6083 * 'get_default()' - Get the default destination.
6084 */
6085
6086 static void
6087 get_default(cupsd_client_t *con) /* I - Client connection */
6088 {
6089 http_status_t status; /* Policy status */
6090 cups_array_t *ra; /* Requested attributes array */
6091
6092
6093 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->number);
6094
6095 /*
6096 * Check policy...
6097 */
6098
6099 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6100 {
6101 send_http_error(con, status, NULL);
6102 return;
6103 }
6104
6105 if (DefaultPrinter)
6106 {
6107 ra = create_requested_array(con->request);
6108
6109 copy_printer_attrs(con, DefaultPrinter, ra);
6110
6111 cupsArrayDelete(ra);
6112
6113 con->response->request.status.status_code = IPP_OK;
6114 }
6115 else
6116 send_ipp_status(con, IPP_NOT_FOUND, _("No default printer."));
6117 }
6118
6119
6120 /*
6121 * 'get_devices()' - Get the list of available devices on the local system.
6122 */
6123
6124 static void
6125 get_devices(cupsd_client_t *con) /* I - Client connection */
6126 {
6127 http_status_t status; /* Policy status */
6128 ipp_attribute_t *limit, /* limit attribute */
6129 *timeout, /* timeout attribute */
6130 *requested, /* requested-attributes attribute */
6131 *exclude, /* exclude-schemes attribute */
6132 *include; /* include-schemes attribute */
6133 char command[1024], /* cups-deviced command */
6134 options[2048], /* Options to pass to command */
6135 requested_str[256],
6136 /* String for requested attributes */
6137 exclude_str[512],
6138 /* String for excluded schemes */
6139 include_str[512];
6140 /* String for included schemes */
6141
6142
6143 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->number);
6144
6145 /*
6146 * Check policy...
6147 */
6148
6149 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6150 {
6151 send_http_error(con, status, NULL);
6152 return;
6153 }
6154
6155 /*
6156 * Run cups-deviced command with the given options...
6157 */
6158
6159 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
6160 timeout = ippFindAttribute(con->request, "timeout", IPP_TAG_INTEGER);
6161 requested = ippFindAttribute(con->request, "requested-attributes",
6162 IPP_TAG_KEYWORD);
6163 exclude = ippFindAttribute(con->request, "exclude-schemes", IPP_TAG_NAME);
6164 include = ippFindAttribute(con->request, "include-schemes", IPP_TAG_NAME);
6165
6166 if (requested)
6167 url_encode_attr(requested, requested_str, sizeof(requested_str));
6168 else
6169 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
6170
6171 if (exclude)
6172 url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
6173 else
6174 exclude_str[0] = '\0';
6175
6176 if (include)
6177 url_encode_attr(include, include_str, sizeof(include_str));
6178 else
6179 include_str[0] = '\0';
6180
6181 snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin);
6182 snprintf(options, sizeof(options),
6183 "%d+%d+%d+%d+%s%s%s%s%s",
6184 con->request->request.op.request_id,
6185 limit ? limit->values[0].integer : 0,
6186 timeout ? timeout->values[0].integer : 15,
6187 (int)User,
6188 requested_str,
6189 exclude_str[0] ? "%20" : "", exclude_str,
6190 include_str[0] ? "%20" : "", include_str);
6191
6192 if (cupsdSendCommand(con, command, options, 1))
6193 {
6194 /*
6195 * Command started successfully, don't send an IPP response here...
6196 */
6197
6198 ippDelete(con->response);
6199 con->response = NULL;
6200 }
6201 else
6202 {
6203 /*
6204 * Command failed, return "internal error" so the user knows something
6205 * went wrong...
6206 */
6207
6208 send_ipp_status(con, IPP_INTERNAL_ERROR,
6209 _("cups-deviced failed to execute."));
6210 }
6211 }
6212
6213
6214 /*
6215 * 'get_document()' - Get a copy of a job file.
6216 */
6217
6218 static void
6219 get_document(cupsd_client_t *con, /* I - Client connection */
6220 ipp_attribute_t *uri) /* I - Job URI */
6221 {
6222 http_status_t status; /* Policy status */
6223 ipp_attribute_t *attr; /* Current attribute */
6224 int jobid; /* Job ID */
6225 int docnum; /* Document number */
6226 cupsd_job_t *job; /* Current job */
6227 char scheme[HTTP_MAX_URI], /* Method portion of URI */
6228 username[HTTP_MAX_URI], /* Username portion of URI */
6229 host[HTTP_MAX_URI], /* Host portion of URI */
6230 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6231 int port; /* Port portion of URI */
6232 char filename[1024], /* Filename for document */
6233 format[1024]; /* Format for document */
6234
6235
6236 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_document(%p[%d], %s)", con,
6237 con->number, uri->values[0].string.text);
6238
6239 /*
6240 * See if we have a job URI or a printer URI...
6241 */
6242
6243 if (!strcmp(uri->name, "printer-uri"))
6244 {
6245 /*
6246 * Got a printer URI; see if we also have a job-id attribute...
6247 */
6248
6249 if ((attr = ippFindAttribute(con->request, "job-id",
6250 IPP_TAG_INTEGER)) == NULL)
6251 {
6252 send_ipp_status(con, IPP_BAD_REQUEST,
6253 _("Got a printer-uri attribute but no job-id."));
6254 return;
6255 }
6256
6257 jobid = attr->values[0].integer;
6258 }
6259 else
6260 {
6261 /*
6262 * Got a job URI; parse it to get the job ID...
6263 */
6264
6265 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6266 sizeof(scheme), username, sizeof(username), host,
6267 sizeof(host), &port, resource, sizeof(resource));
6268
6269 if (strncmp(resource, "/jobs/", 6))
6270 {
6271 /*
6272 * Not a valid URI!
6273 */
6274
6275 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
6276 uri->values[0].string.text);
6277 return;
6278 }
6279
6280 jobid = atoi(resource + 6);
6281 }
6282
6283 /*
6284 * See if the job exists...
6285 */
6286
6287 if ((job = cupsdFindJob(jobid)) == NULL)
6288 {
6289 /*
6290 * Nope - return a "not found" error...
6291 */
6292
6293 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
6294 return;
6295 }
6296
6297 /*
6298 * Check policy...
6299 */
6300
6301 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con,
6302 job->username)) != HTTP_OK)
6303 {
6304 send_http_error(con, status, NULL);
6305 return;
6306 }
6307
6308 /*
6309 * Get the document number...
6310 */
6311
6312 if ((attr = ippFindAttribute(con->request, "document-number",
6313 IPP_TAG_INTEGER)) == NULL)
6314 {
6315 send_ipp_status(con, IPP_BAD_REQUEST,
6316 _("Missing document-number attribute."));
6317 return;
6318 }
6319
6320 if ((docnum = attr->values[0].integer) < 1 || docnum > job->num_files ||
6321 attr->num_values > 1)
6322 {
6323 send_ipp_status(con, IPP_NOT_FOUND,
6324 _("Document #%d does not exist in job #%d."), docnum,
6325 jobid);
6326 return;
6327 }
6328
6329 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, jobid,
6330 docnum);
6331 if ((con->file = open(filename, O_RDONLY)) == -1)
6332 {
6333 cupsdLogMessage(CUPSD_LOG_ERROR,
6334 "Unable to open document %d in job %d - %s", docnum, jobid,
6335 strerror(errno));
6336 send_ipp_status(con, IPP_NOT_FOUND,
6337 _("Unable to open document #%d in job #%d."), docnum,
6338 jobid);
6339 return;
6340 }
6341
6342 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
6343
6344 cupsdLoadJob(job);
6345
6346 snprintf(format, sizeof(format), "%s/%s", job->filetypes[docnum - 1]->super,
6347 job->filetypes[docnum - 1]->type);
6348
6349 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
6350 NULL, format);
6351 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "document-number",
6352 docnum);
6353 if ((attr = ippFindAttribute(job->attrs, "document-name",
6354 IPP_TAG_NAME)) != NULL)
6355 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "document-name",
6356 NULL, attr->values[0].string.text);
6357 }
6358
6359
6360 /*
6361 * 'get_job_attrs()' - Get job attributes.
6362 */
6363
6364 static void
6365 get_job_attrs(cupsd_client_t *con, /* I - Client connection */
6366 ipp_attribute_t *uri) /* I - Job URI */
6367 {
6368 http_status_t status; /* Policy status */
6369 ipp_attribute_t *attr; /* Current attribute */
6370 int jobid; /* Job ID */
6371 cupsd_job_t *job; /* Current job */
6372 cupsd_printer_t *printer; /* Current printer */
6373 cupsd_policy_t *policy; /* Current security policy */
6374 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
6375 username[HTTP_MAX_URI], /* Username portion of URI */
6376 host[HTTP_MAX_URI], /* Host portion of URI */
6377 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6378 int port; /* Port portion of URI */
6379 cups_array_t *ra, /* Requested attributes array */
6380 *exclude; /* Private attributes array */
6381
6382
6383 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con,
6384 con->number, uri->values[0].string.text);
6385
6386 /*
6387 * See if we have a job URI or a printer URI...
6388 */
6389
6390 if (!strcmp(uri->name, "printer-uri"))
6391 {
6392 /*
6393 * Got a printer URI; see if we also have a job-id attribute...
6394 */
6395
6396 if ((attr = ippFindAttribute(con->request, "job-id",
6397 IPP_TAG_INTEGER)) == NULL)
6398 {
6399 send_ipp_status(con, IPP_BAD_REQUEST,
6400 _("Got a printer-uri attribute but no job-id."));
6401 return;
6402 }
6403
6404 jobid = attr->values[0].integer;
6405 }
6406 else
6407 {
6408 /*
6409 * Got a job URI; parse it to get the job ID...
6410 */
6411
6412 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6413 sizeof(scheme), username, sizeof(username), host,
6414 sizeof(host), &port, resource, sizeof(resource));
6415
6416 if (strncmp(resource, "/jobs/", 6))
6417 {
6418 /*
6419 * Not a valid URI!
6420 */
6421
6422 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
6423 uri->values[0].string.text);
6424 return;
6425 }
6426
6427 jobid = atoi(resource + 6);
6428 }
6429
6430 /*
6431 * See if the job exists...
6432 */
6433
6434 if ((job = cupsdFindJob(jobid)) == NULL)
6435 {
6436 /*
6437 * Nope - return a "not found" error...
6438 */
6439
6440 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
6441 return;
6442 }
6443
6444 /*
6445 * Check policy...
6446 */
6447
6448 if ((printer = job->printer) == NULL)
6449 printer = cupsdFindDest(job->dest);
6450
6451 if (printer)
6452 policy = printer->op_policy_ptr;
6453 else
6454 policy = DefaultPolicyPtr;
6455
6456 if ((status = cupsdCheckPolicy(policy, con, job->username)) != HTTP_OK)
6457 {
6458 send_http_error(con, status, NULL);
6459 return;
6460 }
6461
6462 exclude = cupsdGetPrivateAttrs(policy, con, printer, job->username);
6463
6464 /*
6465 * Copy attributes...
6466 */
6467
6468 cupsdLoadJob(job);
6469
6470 ra = create_requested_array(con->request);
6471 copy_job_attrs(con, job, ra, exclude);
6472 cupsArrayDelete(ra);
6473
6474 con->response->request.status.status_code = IPP_OK;
6475 }
6476
6477
6478 /*
6479 * 'get_jobs()' - Get a list of jobs for the specified printer.
6480 */
6481
6482 static void
6483 get_jobs(cupsd_client_t *con, /* I - Client connection */
6484 ipp_attribute_t *uri) /* I - Printer URI */
6485 {
6486 http_status_t status; /* Policy status */
6487 ipp_attribute_t *attr; /* Current attribute */
6488 const char *dest; /* Destination */
6489 cups_ptype_t dtype; /* Destination type (printer/class) */
6490 cups_ptype_t dmask; /* Destination type mask */
6491 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
6492 username[HTTP_MAX_URI], /* Username portion of URI */
6493 host[HTTP_MAX_URI], /* Host portion of URI */
6494 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6495 int port; /* Port portion of URI */
6496 int job_comparison; /* Job comparison */
6497 ipp_jstate_t job_state; /* job-state value */
6498 int first_job_id = 1, /* First job ID */
6499 first_index = 1, /* First index */
6500 limit = 0, /* Maximum number of jobs to return */
6501 count, /* Number of jobs that match */
6502 need_load_job = 0; /* Do we need to load the job? */
6503 const char *job_attr; /* Job attribute requested */
6504 ipp_attribute_t *job_ids; /* job-ids attribute */
6505 cupsd_job_t *job; /* Current job pointer */
6506 cupsd_printer_t *printer; /* Printer */
6507 cups_array_t *list; /* Which job list... */
6508 int delete_list = 0; /* Delete the list afterwards? */
6509 cups_array_t *ra, /* Requested attributes array */
6510 *exclude; /* Private attributes array */
6511 cupsd_policy_t *policy; /* Current policy */
6512
6513
6514 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->number,
6515 uri->values[0].string.text);
6516
6517 /*
6518 * Is the destination valid?
6519 */
6520
6521 if (strcmp(uri->name, "printer-uri"))
6522 {
6523 send_ipp_status(con, IPP_BAD_REQUEST, _("No printer-uri in request."));
6524 return;
6525 }
6526
6527 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6528 sizeof(scheme), username, sizeof(username), host,
6529 sizeof(host), &port, resource, sizeof(resource));
6530
6531 if (!strcmp(resource, "/") || !strcmp(resource, "/jobs"))
6532 {
6533 dest = NULL;
6534 dtype = (cups_ptype_t)0;
6535 dmask = (cups_ptype_t)0;
6536 printer = NULL;
6537 }
6538 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
6539 {
6540 dest = NULL;
6541 dtype = (cups_ptype_t)0;
6542 dmask = CUPS_PRINTER_CLASS;
6543 printer = NULL;
6544 }
6545 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
6546 {
6547 dest = NULL;
6548 dtype = CUPS_PRINTER_CLASS;
6549 dmask = CUPS_PRINTER_CLASS;
6550 printer = NULL;
6551 }
6552 else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype,
6553 &printer)) == NULL)
6554 {
6555 /*
6556 * Bad URI...
6557 */
6558
6559 send_ipp_status(con, IPP_NOT_FOUND,
6560 _("The printer or class does not exist."));
6561 return;
6562 }
6563 else
6564 {
6565 dtype &= CUPS_PRINTER_CLASS;
6566 dmask = CUPS_PRINTER_CLASS;
6567 }
6568
6569 /*
6570 * Check policy...
6571 */
6572
6573 if (printer)
6574 policy = printer->op_policy_ptr;
6575 else
6576 policy = DefaultPolicyPtr;
6577
6578 if ((status = cupsdCheckPolicy(policy, con, NULL)) != HTTP_OK)
6579 {
6580 send_http_error(con, status, NULL);
6581 return;
6582 }
6583
6584 job_ids = ippFindAttribute(con->request, "job-ids", IPP_TAG_INTEGER);
6585
6586 /*
6587 * See if the "which-jobs" attribute have been specified...
6588 */
6589
6590 if ((attr = ippFindAttribute(con->request, "which-jobs",
6591 IPP_TAG_KEYWORD)) != NULL && job_ids)
6592 {
6593 send_ipp_status(con, IPP_CONFLICT,
6594 _("The %s attribute cannot be provided with job-ids."),
6595 "which-jobs");
6596 return;
6597 }
6598 else if (!attr || !strcmp(attr->values[0].string.text, "not-completed"))
6599 {
6600 job_comparison = -1;
6601 job_state = IPP_JOB_STOPPED;
6602 list = ActiveJobs;
6603 }
6604 else if (!strcmp(attr->values[0].string.text, "completed"))
6605 {
6606 job_comparison = 1;
6607 job_state = IPP_JOB_CANCELED;
6608 list = cupsdGetCompletedJobs(printer);
6609 delete_list = 1;
6610 }
6611 else if (!strcmp(attr->values[0].string.text, "aborted"))
6612 {
6613 job_comparison = 0;
6614 job_state = IPP_JOB_ABORTED;
6615 list = cupsdGetCompletedJobs(printer);
6616 delete_list = 1;
6617 }
6618 else if (!strcmp(attr->values[0].string.text, "all"))
6619 {
6620 job_comparison = 1;
6621 job_state = IPP_JOB_PENDING;
6622 list = Jobs;
6623 }
6624 else if (!strcmp(attr->values[0].string.text, "canceled"))
6625 {
6626 job_comparison = 0;
6627 job_state = IPP_JOB_CANCELED;
6628 list = cupsdGetCompletedJobs(printer);
6629 delete_list = 1;
6630 }
6631 else if (!strcmp(attr->values[0].string.text, "pending"))
6632 {
6633 job_comparison = 0;
6634 job_state = IPP_JOB_PENDING;
6635 list = ActiveJobs;
6636 }
6637 else if (!strcmp(attr->values[0].string.text, "pending-held"))
6638 {
6639 job_comparison = 0;
6640 job_state = IPP_JOB_HELD;
6641 list = ActiveJobs;
6642 }
6643 else if (!strcmp(attr->values[0].string.text, "processing"))
6644 {
6645 job_comparison = 0;
6646 job_state = IPP_JOB_PROCESSING;
6647 list = PrintingJobs;
6648 }
6649 else if (!strcmp(attr->values[0].string.text, "processing-stopped"))
6650 {
6651 job_comparison = 0;
6652 job_state = IPP_JOB_STOPPED;
6653 list = ActiveJobs;
6654 }
6655 else
6656 {
6657 send_ipp_status(con, IPP_ATTRIBUTES,
6658 _("The which-jobs value \"%s\" is not supported."),
6659 attr->values[0].string.text);
6660 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
6661 "which-jobs", NULL, attr->values[0].string.text);
6662 return;
6663 }
6664
6665 /*
6666 * See if they want to limit the number of jobs reported...
6667 */
6668
6669 if ((attr = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER)) != NULL)
6670 {
6671 if (job_ids)
6672 {
6673 send_ipp_status(con, IPP_CONFLICT,
6674 _("The %s attribute cannot be provided with job-ids."),
6675 "limit");
6676 return;
6677 }
6678
6679 limit = attr->values[0].integer;
6680 }
6681
6682 if ((attr = ippFindAttribute(con->request, "first-index", IPP_TAG_INTEGER)) != NULL)
6683 {
6684 if (job_ids)
6685 {
6686 send_ipp_status(con, IPP_CONFLICT,
6687 _("The %s attribute cannot be provided with job-ids."),
6688 "first-index");
6689 return;
6690 }
6691
6692 first_index = attr->values[0].integer;
6693 }
6694 else if ((attr = ippFindAttribute(con->request, "first-job-id", IPP_TAG_INTEGER)) != NULL)
6695 {
6696 if (job_ids)
6697 {
6698 send_ipp_status(con, IPP_CONFLICT,
6699 _("The %s attribute cannot be provided with job-ids."),
6700 "first-job-id");
6701 return;
6702 }
6703
6704 first_job_id = attr->values[0].integer;
6705 }
6706
6707 /*
6708 * See if we only want to see jobs for a specific user...
6709 */
6710
6711 if ((attr = ippFindAttribute(con->request, "my-jobs", IPP_TAG_BOOLEAN)) != NULL && job_ids)
6712 {
6713 send_ipp_status(con, IPP_CONFLICT,
6714 _("The %s attribute cannot be provided with job-ids."),
6715 "my-jobs");
6716 return;
6717 }
6718 else if (attr && attr->values[0].boolean)
6719 strlcpy(username, get_username(con), sizeof(username));
6720 else
6721 username[0] = '\0';
6722
6723 ra = create_requested_array(con->request);
6724 for (job_attr = (char *)cupsArrayFirst(ra); job_attr; job_attr = (char *)cupsArrayNext(ra))
6725 if (strcmp(job_attr, "job-id") &&
6726 strcmp(job_attr, "job-k-octets") &&
6727 strcmp(job_attr, "job-media-progress") &&
6728 strcmp(job_attr, "job-more-info") &&
6729 strcmp(job_attr, "job-name") &&
6730 strcmp(job_attr, "job-originating-user-name") &&
6731 strcmp(job_attr, "job-preserved") &&
6732 strcmp(job_attr, "job-printer-up-time") &&
6733 strcmp(job_attr, "job-printer-uri") &&
6734 strcmp(job_attr, "job-state") &&
6735 strcmp(job_attr, "job-state-reasons") &&
6736 strcmp(job_attr, "job-uri") &&
6737 strcmp(job_attr, "time-at-completed") &&
6738 strcmp(job_attr, "time-at-creation") &&
6739 strcmp(job_attr, "number-of-documents"))
6740 {
6741 need_load_job = 1;
6742 break;
6743 }
6744
6745 if (need_load_job && (limit == 0 || limit > 500) && (list == Jobs || delete_list))
6746 {
6747 /*
6748 * Limit expensive Get-Jobs for job history to 500 jobs...
6749 */
6750
6751 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "limit", 500);
6752
6753 if (limit)
6754 ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER, "limit", limit);
6755
6756 limit = 500;
6757
6758 cupsdLogClient(con, CUPSD_LOG_INFO, "Limiting Get-Jobs response to %d jobs.", limit);
6759 }
6760
6761 /*
6762 * OK, build a list of jobs for this printer...
6763 */
6764
6765 if (job_ids)
6766 {
6767 int i; /* Looping var */
6768
6769 for (i = 0; i < job_ids->num_values; i ++)
6770 {
6771 if (!cupsdFindJob(job_ids->values[i].integer))
6772 break;
6773 }
6774
6775 if (i < job_ids->num_values)
6776 {
6777 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
6778 job_ids->values[i].integer);
6779 return;
6780 }
6781
6782 for (i = 0; i < job_ids->num_values; i ++)
6783 {
6784 job = cupsdFindJob(job_ids->values[i].integer);
6785
6786 if (need_load_job && !job->attrs)
6787 {
6788 cupsdLoadJob(job);
6789
6790 if (!job->attrs)
6791 {
6792 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d", job->id);
6793 continue;
6794 }
6795 }
6796
6797 if (i > 0)
6798 ippAddSeparator(con->response);
6799
6800 exclude = cupsdGetPrivateAttrs(job->printer ?
6801 job->printer->op_policy_ptr :
6802 policy, con, job->printer,
6803 job->username);
6804
6805 copy_job_attrs(con, job, ra, exclude);
6806 }
6807 }
6808 else
6809 {
6810 if (first_index > 1)
6811 job = (cupsd_job_t *)cupsArrayIndex(list, first_index - 1);
6812 else
6813 job = (cupsd_job_t *)cupsArrayFirst(list);
6814
6815 for (count = 0; (limit <= 0 || count < limit) && job; job = (cupsd_job_t *)cupsArrayNext(list))
6816 {
6817 /*
6818 * Filter out jobs that don't match...
6819 */
6820
6821 cupsdLogMessage(CUPSD_LOG_DEBUG2,
6822 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
6823 "state_value=%d, attrs=%p", job->id, job->dest,
6824 job->username, job->state_value, job->attrs);
6825
6826 if (!job->dest || !job->username)
6827 cupsdLoadJob(job);
6828
6829 if (!job->dest || !job->username)
6830 continue;
6831
6832 if ((dest && strcmp(job->dest, dest)) &&
6833 (!job->printer || !dest || strcmp(job->printer->name, dest)))
6834 continue;
6835 if ((job->dtype & dmask) != dtype &&
6836 (!job->printer || (job->printer->type & dmask) != dtype))
6837 continue;
6838
6839 if ((job_comparison < 0 && job->state_value > job_state) ||
6840 (job_comparison == 0 && job->state_value != job_state) ||
6841 (job_comparison > 0 && job->state_value < job_state))
6842 continue;
6843
6844 if (job->id < first_job_id)
6845 continue;
6846
6847 if (need_load_job && !job->attrs)
6848 {
6849 cupsdLoadJob(job);
6850
6851 if (!job->attrs)
6852 {
6853 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d", job->id);
6854 continue;
6855 }
6856 }
6857
6858 if (username[0] && _cups_strcasecmp(username, job->username))
6859 continue;
6860
6861 if (count > 0)
6862 ippAddSeparator(con->response);
6863
6864 count ++;
6865
6866 exclude = cupsdGetPrivateAttrs(job->printer ?
6867 job->printer->op_policy_ptr :
6868 policy, con, job->printer,
6869 job->username);
6870
6871 copy_job_attrs(con, job, ra, exclude);
6872 }
6873
6874 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count=%d", count);
6875 }
6876
6877 cupsArrayDelete(ra);
6878
6879 if (delete_list)
6880 cupsArrayDelete(list);
6881
6882 con->response->request.status.status_code = IPP_OK;
6883 }
6884
6885
6886 /*
6887 * 'get_notifications()' - Get events for a subscription.
6888 */
6889
6890 static void
6891 get_notifications(cupsd_client_t *con) /* I - Client connection */
6892 {
6893 int i, j; /* Looping vars */
6894 http_status_t status; /* Policy status */
6895 cupsd_subscription_t *sub; /* Subscription */
6896 ipp_attribute_t *ids, /* notify-subscription-ids */
6897 *sequences; /* notify-sequence-numbers */
6898 int min_seq; /* Minimum sequence number */
6899 int interval; /* Poll interval */
6900
6901
6902 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])",
6903 con, con->number);
6904
6905 /*
6906 * Get subscription attributes...
6907 */
6908
6909 ids = ippFindAttribute(con->request, "notify-subscription-ids",
6910 IPP_TAG_INTEGER);
6911 sequences = ippFindAttribute(con->request, "notify-sequence-numbers",
6912 IPP_TAG_INTEGER);
6913
6914 if (!ids)
6915 {
6916 send_ipp_status(con, IPP_BAD_REQUEST,
6917 _("Missing notify-subscription-ids attribute."));
6918 return;
6919 }
6920
6921 /*
6922 * Are the subscription IDs valid?
6923 */
6924
6925 for (i = 0, interval = 60; i < ids->num_values; i ++)
6926 {
6927 if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL)
6928 {
6929 /*
6930 * Bad subscription ID...
6931 */
6932
6933 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
6934 ids->values[i].integer);
6935 return;
6936 }
6937
6938 /*
6939 * Check policy...
6940 */
6941
6942 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
6943 DefaultPolicyPtr,
6944 con, sub->owner)) != HTTP_OK)
6945 {
6946 send_http_error(con, status, sub->dest);
6947 return;
6948 }
6949
6950 /*
6951 * Check the subscription type and update the interval accordingly.
6952 */
6953
6954 if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING &&
6955 interval > 10)
6956 interval = 10;
6957 else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED)
6958 interval = 0;
6959 else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING &&
6960 interval > 30)
6961 interval = 30;
6962 }
6963
6964 /*
6965 * Tell the client to poll again in N seconds...
6966 */
6967
6968 if (interval > 0)
6969 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
6970 "notify-get-interval", interval);
6971
6972 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
6973 "printer-up-time", time(NULL));
6974
6975 /*
6976 * Copy the subscription event attributes to the response.
6977 */
6978
6979 con->response->request.status.status_code =
6980 interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE;
6981
6982 for (i = 0; i < ids->num_values; i ++)
6983 {
6984 /*
6985 * Get the subscription and sequence number...
6986 */
6987
6988 sub = cupsdFindSubscription(ids->values[i].integer);
6989
6990 if (sequences && i < sequences->num_values)
6991 min_seq = sequences->values[i].integer;
6992 else
6993 min_seq = 1;
6994
6995 /*
6996 * If we don't have any new events, nothing to do here...
6997 */
6998
6999 if (min_seq > (sub->first_event_id + cupsArrayCount(sub->events)))
7000 continue;
7001
7002 /*
7003 * Otherwise copy all of the new events...
7004 */
7005
7006 if (sub->first_event_id > min_seq)
7007 j = 0;
7008 else
7009 j = min_seq - sub->first_event_id;
7010
7011 for (; j < cupsArrayCount(sub->events); j ++)
7012 {
7013 ippAddSeparator(con->response);
7014
7015 copy_attrs(con->response,
7016 ((cupsd_event_t *)cupsArrayIndex(sub->events, j))->attrs, NULL,
7017 IPP_TAG_EVENT_NOTIFICATION, 0, NULL);
7018 }
7019 }
7020 }
7021
7022
7023 /*
7024 * 'get_ppd()' - Get a named PPD from the local system.
7025 */
7026
7027 static void
7028 get_ppd(cupsd_client_t *con, /* I - Client connection */
7029 ipp_attribute_t *uri) /* I - Printer URI or PPD name */
7030 {
7031 http_status_t status; /* Policy status */
7032 cupsd_printer_t *dest; /* Destination */
7033 cups_ptype_t dtype; /* Destination type */
7034
7035
7036 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con,
7037 con->number, uri, uri->name, uri->values[0].string.text);
7038
7039 if (!strcmp(ippGetName(uri), "ppd-name"))
7040 {
7041 /*
7042 * Return a PPD file from cups-driverd...
7043 */
7044
7045 const char *ppd_name = ippGetString(uri, 0, NULL);
7046 /* ppd-name value */
7047 char command[1024], /* cups-driverd command */
7048 options[1024], /* Options to pass to command */
7049 oppd_name[1024]; /* Escaped ppd-name */
7050
7051 /*
7052 * Check policy...
7053 */
7054
7055 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7056 {
7057 send_http_error(con, status, NULL);
7058 return;
7059 }
7060
7061 /*
7062 * Check ppd-name value...
7063 */
7064
7065 if (strstr(ppd_name, "../"))
7066 {
7067 send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Invalid ppd-name value."));
7068 return;
7069 }
7070
7071 /*
7072 * Run cups-driverd command with the given options...
7073 */
7074
7075 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
7076 url_encode_string(ppd_name, oppd_name, sizeof(oppd_name));
7077 snprintf(options, sizeof(options), "get+%d+%s", ippGetRequestId(con->request), oppd_name);
7078
7079 if (cupsdSendCommand(con, command, options, 0))
7080 {
7081 /*
7082 * Command started successfully, don't send an IPP response here...
7083 */
7084
7085 ippDelete(con->response);
7086 con->response = NULL;
7087 }
7088 else
7089 {
7090 /*
7091 * Command failed, return "internal error" so the user knows something
7092 * went wrong...
7093 */
7094
7095 send_ipp_status(con, IPP_INTERNAL_ERROR, _("cups-driverd failed to execute."));
7096 }
7097 }
7098 else if (!strcmp(ippGetName(uri), "printer-uri") && cupsdValidateDest(ippGetString(uri, 0, NULL), &dtype, &dest))
7099 {
7100 int i; /* Looping var */
7101 char filename[1024]; /* PPD filename */
7102
7103 /*
7104 * Check policy...
7105 */
7106
7107 if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK)
7108 {
7109 send_http_error(con, status, dest);
7110 return;
7111 }
7112
7113 /*
7114 * See if we need the PPD for a class or remote printer...
7115 */
7116
7117 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, dest->name);
7118
7119 if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0))
7120 {
7121 send_ipp_status(con, IPP_STATUS_CUPS_SEE_OTHER, _("See remote printer."));
7122 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, dest->uri);
7123 return;
7124 }
7125 else if (dtype & CUPS_PRINTER_CLASS)
7126 {
7127 for (i = 0; i < dest->num_printers; i ++)
7128 if (!(dest->printers[i]->type & CUPS_PRINTER_CLASS))
7129 {
7130 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, dest->printers[i]->name);
7131
7132 if (!access(filename, 0))
7133 break;
7134 }
7135
7136 if (i < dest->num_printers)
7137 dest = dest->printers[i];
7138 else
7139 {
7140 send_ipp_status(con, IPP_STATUS_CUPS_SEE_OTHER, _("See remote printer."));
7141 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, dest->printers[0]->uri);
7142 return;
7143 }
7144 }
7145
7146 /*
7147 * Found the printer with the PPD file, now see if there is one...
7148 */
7149
7150 if ((con->file = open(filename, O_RDONLY)) < 0)
7151 {
7152 send_ipp_status(con, IPP_STATUS_ERROR_NOT_FOUND, _("The PPD file \"%s\" could not be opened: %s"), ippGetString(uri, 0, NULL), strerror(errno));
7153 return;
7154 }
7155
7156 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
7157
7158 con->pipe_pid = 0;
7159
7160 ippSetStatusCode(con->response, IPP_STATUS_OK);
7161 }
7162 else
7163 send_ipp_status(con, IPP_STATUS_ERROR_NOT_FOUND, _("The PPD file \"%s\" could not be found."), ippGetString(uri, 0, NULL));
7164 }
7165
7166
7167 /*
7168 * 'get_ppds()' - Get the list of PPD files on the local system.
7169 */
7170
7171 static void
7172 get_ppds(cupsd_client_t *con) /* I - Client connection */
7173 {
7174 http_status_t status; /* Policy status */
7175 ipp_attribute_t *limit, /* Limit attribute */
7176 *device, /* ppd-device-id attribute */
7177 *language, /* ppd-natural-language attribute */
7178 *make, /* ppd-make attribute */
7179 *model, /* ppd-make-and-model attribute */
7180 *model_number, /* ppd-model-number attribute */
7181 *product, /* ppd-product attribute */
7182 *psversion, /* ppd-psverion attribute */
7183 *type, /* ppd-type attribute */
7184 *requested, /* requested-attributes attribute */
7185 *exclude, /* exclude-schemes attribute */
7186 *include; /* include-schemes attribute */
7187 char command[1024], /* cups-driverd command */
7188 options[4096], /* Options to pass to command */
7189 device_str[256],/* Escaped ppd-device-id string */
7190 language_str[256],
7191 /* Escaped ppd-natural-language */
7192 make_str[256], /* Escaped ppd-make string */
7193 model_str[256], /* Escaped ppd-make-and-model string */
7194 model_number_str[256],
7195 /* ppd-model-number string */
7196 product_str[256],
7197 /* Escaped ppd-product string */
7198 psversion_str[256],
7199 /* Escaped ppd-psversion string */
7200 type_str[256], /* Escaped ppd-type string */
7201 requested_str[256],
7202 /* String for requested attributes */
7203 exclude_str[512],
7204 /* String for excluded schemes */
7205 include_str[512];
7206 /* String for included schemes */
7207
7208
7209 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->number);
7210
7211 /*
7212 * Check policy...
7213 */
7214
7215 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7216 {
7217 send_http_error(con, status, NULL);
7218 return;
7219 }
7220
7221 /*
7222 * Run cups-driverd command with the given options...
7223 */
7224
7225 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
7226 device = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT);
7227 language = ippFindAttribute(con->request, "ppd-natural-language",
7228 IPP_TAG_LANGUAGE);
7229 make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT);
7230 model = ippFindAttribute(con->request, "ppd-make-and-model",
7231 IPP_TAG_TEXT);
7232 model_number = ippFindAttribute(con->request, "ppd-model-number",
7233 IPP_TAG_INTEGER);
7234 product = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT);
7235 psversion = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT);
7236 type = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD);
7237 requested = ippFindAttribute(con->request, "requested-attributes",
7238 IPP_TAG_KEYWORD);
7239 exclude = ippFindAttribute(con->request, "exclude-schemes",
7240 IPP_TAG_NAME);
7241 include = ippFindAttribute(con->request, "include-schemes",
7242 IPP_TAG_NAME);
7243
7244 if (requested)
7245 url_encode_attr(requested, requested_str, sizeof(requested_str));
7246 else
7247 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
7248
7249 if (device)
7250 url_encode_attr(device, device_str, sizeof(device_str));
7251 else
7252 device_str[0] = '\0';
7253
7254 if (language)
7255 url_encode_attr(language, language_str, sizeof(language_str));
7256 else
7257 language_str[0] = '\0';
7258
7259 if (make)
7260 url_encode_attr(make, make_str, sizeof(make_str));
7261 else
7262 make_str[0] = '\0';
7263
7264 if (model)
7265 url_encode_attr(model, model_str, sizeof(model_str));
7266 else
7267 model_str[0] = '\0';
7268
7269 if (model_number)
7270 snprintf(model_number_str, sizeof(model_number_str), "ppd-model-number=%d",
7271 model_number->values[0].integer);
7272 else
7273 model_number_str[0] = '\0';
7274
7275 if (product)
7276 url_encode_attr(product, product_str, sizeof(product_str));
7277 else
7278 product_str[0] = '\0';
7279
7280 if (psversion)
7281 url_encode_attr(psversion, psversion_str, sizeof(psversion_str));
7282 else
7283 psversion_str[0] = '\0';
7284
7285 if (type)
7286 url_encode_attr(type, type_str, sizeof(type_str));
7287 else
7288 type_str[0] = '\0';
7289
7290 if (exclude)
7291 url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
7292 else
7293 exclude_str[0] = '\0';
7294
7295 if (include)
7296 url_encode_attr(include, include_str, sizeof(include_str));
7297 else
7298 include_str[0] = '\0';
7299
7300 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
7301 snprintf(options, sizeof(options),
7302 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7303 con->request->request.op.request_id,
7304 limit ? limit->values[0].integer : 0,
7305 requested_str,
7306 device ? "%20" : "", device_str,
7307 language ? "%20" : "", language_str,
7308 make ? "%20" : "", make_str,
7309 model ? "%20" : "", model_str,
7310 model_number ? "%20" : "", model_number_str,
7311 product ? "%20" : "", product_str,
7312 psversion ? "%20" : "", psversion_str,
7313 type ? "%20" : "", type_str,
7314 exclude_str[0] ? "%20" : "", exclude_str,
7315 include_str[0] ? "%20" : "", include_str);
7316
7317 if (cupsdSendCommand(con, command, options, 0))
7318 {
7319 /*
7320 * Command started successfully, don't send an IPP response here...
7321 */
7322
7323 ippDelete(con->response);
7324 con->response = NULL;
7325 }
7326 else
7327 {
7328 /*
7329 * Command failed, return "internal error" so the user knows something
7330 * went wrong...
7331 */
7332
7333 send_ipp_status(con, IPP_INTERNAL_ERROR,
7334 _("cups-driverd failed to execute."));
7335 }
7336 }
7337
7338
7339 /*
7340 * 'get_printer_attrs()' - Get printer attributes.
7341 */
7342
7343 static void
7344 get_printer_attrs(cupsd_client_t *con, /* I - Client connection */
7345 ipp_attribute_t *uri) /* I - Printer URI */
7346 {
7347 http_status_t status; /* Policy status */
7348 cups_ptype_t dtype; /* Destination type (printer/class) */
7349 cupsd_printer_t *printer; /* Printer/class */
7350 cups_array_t *ra; /* Requested attributes array */
7351
7352
7353 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con,
7354 con->number, uri->values[0].string.text);
7355
7356 /*
7357 * Is the destination valid?
7358 */
7359
7360 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7361 {
7362 /*
7363 * Bad URI...
7364 */
7365
7366 send_ipp_status(con, IPP_NOT_FOUND,
7367 _("The printer or class does not exist."));
7368 return;
7369 }
7370
7371 /*
7372 * Check policy...
7373 */
7374
7375 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
7376 {
7377 send_http_error(con, status, printer);
7378 return;
7379 }
7380
7381 /*
7382 * Send the attributes...
7383 */
7384
7385 ra = create_requested_array(con->request);
7386
7387 copy_printer_attrs(con, printer, ra);
7388
7389 cupsArrayDelete(ra);
7390
7391 con->response->request.status.status_code = IPP_OK;
7392 }
7393
7394
7395 /*
7396 * 'get_printer_supported()' - Get printer supported values.
7397 */
7398
7399 static void
7400 get_printer_supported(
7401 cupsd_client_t *con, /* I - Client connection */
7402 ipp_attribute_t *uri) /* I - Printer URI */
7403 {
7404 http_status_t status; /* Policy status */
7405 cups_ptype_t dtype; /* Destination type (printer/class) */
7406 cupsd_printer_t *printer; /* Printer/class */
7407
7408
7409 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_supported(%p[%d], %s)", con,
7410 con->number, uri->values[0].string.text);
7411
7412 /*
7413 * Is the destination valid?
7414 */
7415
7416 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7417 {
7418 /*
7419 * Bad URI...
7420 */
7421
7422 send_ipp_status(con, IPP_NOT_FOUND,
7423 _("The printer or class does not exist."));
7424 return;
7425 }
7426
7427 /*
7428 * Check policy...
7429 */
7430
7431 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
7432 {
7433 send_http_error(con, status, printer);
7434 return;
7435 }
7436
7437 /*
7438 * Return a list of attributes that can be set via Set-Printer-Attributes.
7439 */
7440
7441 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7442 "printer-geo-location", 0);
7443 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7444 "printer-info", 0);
7445 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7446 "printer-location", 0);
7447 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7448 "printer-organization", 0);
7449 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7450 "printer-organizational-unit", 0);
7451
7452 con->response->request.status.status_code = IPP_OK;
7453 }
7454
7455
7456 /*
7457 * 'get_printers()' - Get a list of printers or classes.
7458 */
7459
7460 static void
7461 get_printers(cupsd_client_t *con, /* I - Client connection */
7462 int type) /* I - 0 or CUPS_PRINTER_CLASS */
7463 {
7464 http_status_t status; /* Policy status */
7465 ipp_attribute_t *attr; /* Current attribute */
7466 int limit; /* Max number of printers to return */
7467 int count; /* Number of printers that match */
7468 cupsd_printer_t *printer; /* Current printer pointer */
7469 cups_ptype_t printer_type, /* printer-type attribute */
7470 printer_mask; /* printer-type-mask attribute */
7471 char *location; /* Location string */
7472 const char *username; /* Current user */
7473 char *first_printer_name; /* first-printer-name attribute */
7474 cups_array_t *ra; /* Requested attributes array */
7475 int local; /* Local connection? */
7476
7477
7478 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con,
7479 con->number, type);
7480
7481 /*
7482 * Check policy...
7483 */
7484
7485 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7486 {
7487 send_http_error(con, status, NULL);
7488 return;
7489 }
7490
7491 /*
7492 * Check for printers...
7493 */
7494
7495 if (!Printers || !cupsArrayCount(Printers))
7496 {
7497 send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added."));
7498 return;
7499 }
7500
7501 /*
7502 * See if they want to limit the number of printers reported...
7503 */
7504
7505 if ((attr = ippFindAttribute(con->request, "limit",
7506 IPP_TAG_INTEGER)) != NULL)
7507 limit = attr->values[0].integer;
7508 else
7509 limit = 10000000;
7510
7511 if ((attr = ippFindAttribute(con->request, "first-printer-name",
7512 IPP_TAG_NAME)) != NULL)
7513 first_printer_name = attr->values[0].string.text;
7514 else
7515 first_printer_name = NULL;
7516
7517 /*
7518 * Support filtering...
7519 */
7520
7521 if ((attr = ippFindAttribute(con->request, "printer-type",
7522 IPP_TAG_ENUM)) != NULL)
7523 printer_type = (cups_ptype_t)attr->values[0].integer;
7524 else
7525 printer_type = (cups_ptype_t)0;
7526
7527 if ((attr = ippFindAttribute(con->request, "printer-type-mask",
7528 IPP_TAG_ENUM)) != NULL)
7529 printer_mask = (cups_ptype_t)attr->values[0].integer;
7530 else
7531 printer_mask = (cups_ptype_t)0;
7532
7533 local = httpAddrLocalhost(&(con->clientaddr));
7534
7535 if ((attr = ippFindAttribute(con->request, "printer-location",
7536 IPP_TAG_TEXT)) != NULL)
7537 location = attr->values[0].string.text;
7538 else
7539 location = NULL;
7540
7541 if (con->username[0])
7542 username = con->username;
7543 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7544 IPP_TAG_NAME)) != NULL)
7545 username = attr->values[0].string.text;
7546 else
7547 username = NULL;
7548
7549 ra = create_requested_array(con->request);
7550
7551 /*
7552 * OK, build a list of printers for this printer...
7553 */
7554
7555 if (first_printer_name)
7556 {
7557 if ((printer = cupsdFindDest(first_printer_name)) == NULL)
7558 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
7559 }
7560 else
7561 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
7562
7563 for (count = 0;
7564 count < limit && printer;
7565 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
7566 {
7567 if (!local && !printer->shared)
7568 continue;
7569
7570 if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) &&
7571 (printer->type & printer_mask) == printer_type &&
7572 (!location ||
7573 (printer->location && !_cups_strcasecmp(printer->location, location))))
7574 {
7575 /*
7576 * If a username is specified, see if it is allowed or denied
7577 * access...
7578 */
7579
7580 if (cupsArrayCount(printer->users) && username &&
7581 !user_allowed(printer, username))
7582 continue;
7583
7584 /*
7585 * Add the group separator as needed...
7586 */
7587
7588 if (count > 0)
7589 ippAddSeparator(con->response);
7590
7591 count ++;
7592
7593 /*
7594 * Send the attributes...
7595 */
7596
7597 copy_printer_attrs(con, printer, ra);
7598 }
7599 }
7600
7601 cupsArrayDelete(ra);
7602
7603 con->response->request.status.status_code = IPP_OK;
7604 }
7605
7606
7607 /*
7608 * 'get_subscription_attrs()' - Get subscription attributes.
7609 */
7610
7611 static void
7612 get_subscription_attrs(
7613 cupsd_client_t *con, /* I - Client connection */
7614 int sub_id) /* I - Subscription ID */
7615 {
7616 http_status_t status; /* Policy status */
7617 cupsd_subscription_t *sub; /* Subscription */
7618 cupsd_policy_t *policy; /* Current security policy */
7619 cups_array_t *ra, /* Requested attributes array */
7620 *exclude; /* Private attributes array */
7621
7622
7623 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7624 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7625 con, con->number, sub_id);
7626
7627 /*
7628 * Expire subscriptions as needed...
7629 */
7630
7631 cupsdExpireSubscriptions(NULL, NULL);
7632
7633 /*
7634 * Is the subscription ID valid?
7635 */
7636
7637 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
7638 {
7639 /*
7640 * Bad subscription ID...
7641 */
7642
7643 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
7644 sub_id);
7645 return;
7646 }
7647
7648 /*
7649 * Check policy...
7650 */
7651
7652 if (sub->dest)
7653 policy = sub->dest->op_policy_ptr;
7654 else
7655 policy = DefaultPolicyPtr;
7656
7657 if ((status = cupsdCheckPolicy(policy, con, sub->owner)) != HTTP_OK)
7658 {
7659 send_http_error(con, status, sub->dest);
7660 return;
7661 }
7662
7663 exclude = cupsdGetPrivateAttrs(policy, con, sub->dest, sub->owner);
7664
7665 /*
7666 * Copy the subscription attributes to the response using the
7667 * requested-attributes attribute that may be provided by the client.
7668 */
7669
7670 ra = create_requested_array(con->request);
7671
7672 copy_subscription_attrs(con, sub, ra, exclude);
7673
7674 cupsArrayDelete(ra);
7675
7676 con->response->request.status.status_code = IPP_OK;
7677 }
7678
7679
7680 /*
7681 * 'get_subscriptions()' - Get subscriptions.
7682 */
7683
7684 static void
7685 get_subscriptions(cupsd_client_t *con, /* I - Client connection */
7686 ipp_attribute_t *uri) /* I - Printer/job URI */
7687 {
7688 http_status_t status; /* Policy status */
7689 int count; /* Number of subscriptions */
7690 int limit; /* Limit */
7691 cupsd_subscription_t *sub; /* Subscription */
7692 cups_array_t *ra; /* Requested attributes array */
7693 ipp_attribute_t *attr; /* Attribute */
7694 cups_ptype_t dtype; /* Destination type (printer/class) */
7695 char scheme[HTTP_MAX_URI],
7696 /* Scheme portion of URI */
7697 username[HTTP_MAX_URI],
7698 /* Username portion of URI */
7699 host[HTTP_MAX_URI],
7700 /* Host portion of URI */
7701 resource[HTTP_MAX_URI];
7702 /* Resource portion of URI */
7703 int port; /* Port portion of URI */
7704 cupsd_job_t *job; /* Job pointer */
7705 cupsd_printer_t *printer; /* Printer */
7706 cupsd_policy_t *policy; /* Policy */
7707 cups_array_t *exclude; /* Private attributes array */
7708
7709
7710 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7711 "get_subscriptions(con=%p[%d], uri=%s)",
7712 con, con->number, uri->values[0].string.text);
7713
7714 /*
7715 * Is the destination valid?
7716 */
7717
7718 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7719 sizeof(scheme), username, sizeof(username), host,
7720 sizeof(host), &port, resource, sizeof(resource));
7721
7722 if (!strcmp(resource, "/") ||
7723 (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) ||
7724 (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) ||
7725 (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9))
7726 {
7727 printer = NULL;
7728 job = NULL;
7729 }
7730 else if (!strncmp(resource, "/jobs/", 6) && resource[6])
7731 {
7732 printer = NULL;
7733 job = cupsdFindJob(atoi(resource + 6));
7734
7735 if (!job)
7736 {
7737 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
7738 atoi(resource + 6));
7739 return;
7740 }
7741 }
7742 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7743 {
7744 /*
7745 * Bad URI...
7746 */
7747
7748 send_ipp_status(con, IPP_NOT_FOUND,
7749 _("The printer or class does not exist."));
7750 return;
7751 }
7752 else if ((attr = ippFindAttribute(con->request, "notify-job-id",
7753 IPP_TAG_INTEGER)) != NULL)
7754 {
7755 job = cupsdFindJob(attr->values[0].integer);
7756
7757 if (!job)
7758 {
7759 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
7760 attr->values[0].integer);
7761 return;
7762 }
7763 }
7764 else
7765 job = NULL;
7766
7767 /*
7768 * Check policy...
7769 */
7770
7771 if (printer)
7772 policy = printer->op_policy_ptr;
7773 else
7774 policy = DefaultPolicyPtr;
7775
7776 if ((status = cupsdCheckPolicy(policy, con, NULL)) != HTTP_OK)
7777 {
7778 send_http_error(con, status, printer);
7779 return;
7780 }
7781
7782 /*
7783 * Expire subscriptions as needed...
7784 */
7785
7786 cupsdExpireSubscriptions(NULL, NULL);
7787
7788 /*
7789 * Copy the subscription attributes to the response using the
7790 * requested-attributes attribute that may be provided by the client.
7791 */
7792
7793 ra = create_requested_array(con->request);
7794
7795 if ((attr = ippFindAttribute(con->request, "limit",
7796 IPP_TAG_INTEGER)) != NULL)
7797 limit = attr->values[0].integer;
7798 else
7799 limit = 0;
7800
7801 /*
7802 * See if we only want to see subscriptions for a specific user...
7803 */
7804
7805 if ((attr = ippFindAttribute(con->request, "my-subscriptions",
7806 IPP_TAG_BOOLEAN)) != NULL &&
7807 attr->values[0].boolean)
7808 strlcpy(username, get_username(con), sizeof(username));
7809 else
7810 username[0] = '\0';
7811
7812 for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0;
7813 sub;
7814 sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
7815 if ((!printer || sub->dest == printer) && (!job || sub->job == job) &&
7816 (!username[0] || !_cups_strcasecmp(username, sub->owner)))
7817 {
7818 ippAddSeparator(con->response);
7819
7820 exclude = cupsdGetPrivateAttrs(sub->dest ? sub->dest->op_policy_ptr :
7821 policy, con, sub->dest,
7822 sub->owner);
7823
7824 copy_subscription_attrs(con, sub, ra, exclude);
7825
7826 count ++;
7827 if (limit && count >= limit)
7828 break;
7829 }
7830
7831 cupsArrayDelete(ra);
7832
7833 if (count)
7834 con->response->request.status.status_code = IPP_OK;
7835 else
7836 send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found."));
7837 }
7838
7839
7840 /*
7841 * 'get_username()' - Get the username associated with a request.
7842 */
7843
7844 static const char * /* O - Username */
7845 get_username(cupsd_client_t *con) /* I - Connection */
7846 {
7847 ipp_attribute_t *attr; /* Attribute */
7848
7849
7850 if (con->username[0])
7851 return (con->username);
7852 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7853 IPP_TAG_NAME)) != NULL)
7854 return (attr->values[0].string.text);
7855 else
7856 return ("anonymous");
7857 }
7858
7859
7860 /*
7861 * 'hold_job()' - Hold a print job.
7862 */
7863
7864 static void
7865 hold_job(cupsd_client_t *con, /* I - Client connection */
7866 ipp_attribute_t *uri) /* I - Job or Printer URI */
7867 {
7868 ipp_attribute_t *attr; /* Current job-hold-until */
7869 const char *when; /* New value */
7870 int jobid; /* Job ID */
7871 char scheme[HTTP_MAX_URI], /* Method portion of URI */
7872 username[HTTP_MAX_URI], /* Username portion of URI */
7873 host[HTTP_MAX_URI], /* Host portion of URI */
7874 resource[HTTP_MAX_URI]; /* Resource portion of URI */
7875 int port; /* Port portion of URI */
7876 cupsd_job_t *job; /* Job information */
7877
7878
7879 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->number,
7880 uri->values[0].string.text);
7881
7882 /*
7883 * See if we have a job URI or a printer URI...
7884 */
7885
7886 if (!strcmp(uri->name, "printer-uri"))
7887 {
7888 /*
7889 * Got a printer URI; see if we also have a job-id attribute...
7890 */
7891
7892 if ((attr = ippFindAttribute(con->request, "job-id",
7893 IPP_TAG_INTEGER)) == NULL)
7894 {
7895 send_ipp_status(con, IPP_BAD_REQUEST,
7896 _("Got a printer-uri attribute but no job-id."));
7897 return;
7898 }
7899
7900 jobid = attr->values[0].integer;
7901 }
7902 else
7903 {
7904 /*
7905 * Got a job URI; parse it to get the job ID...
7906 */
7907
7908 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7909 sizeof(scheme), username, sizeof(username), host,
7910 sizeof(host), &port, resource, sizeof(resource));
7911
7912 if (strncmp(resource, "/jobs/", 6))
7913 {
7914 /*
7915 * Not a valid URI!
7916 */
7917
7918 send_ipp_status(con, IPP_BAD_REQUEST,
7919 _("Bad job-uri \"%s\"."),
7920 uri->values[0].string.text);
7921 return;
7922 }
7923
7924 jobid = atoi(resource + 6);
7925 }
7926
7927 /*
7928 * See if the job exists...
7929 */
7930
7931 if ((job = cupsdFindJob(jobid)) == NULL)
7932 {
7933 /*
7934 * Nope - return a "not found" error...
7935 */
7936
7937 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
7938 return;
7939 }
7940
7941 /*
7942 * See if the job is owned by the requesting user...
7943 */
7944
7945 if (!validate_user(job, con, job->username, username, sizeof(username)))
7946 {
7947 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
7948 cupsdFindDest(job->dest));
7949 return;
7950 }
7951
7952 /*
7953 * See if the job is in a state that allows holding...
7954 */
7955
7956 if (job->state_value > IPP_JOB_STOPPED)
7957 {
7958 /*
7959 * Return a "not-possible" error...
7960 */
7961
7962 send_ipp_status(con, IPP_NOT_POSSIBLE,
7963 _("Job #%d is finished and cannot be altered."),
7964 job->id);
7965 return;
7966 }
7967
7968 /*
7969 * Hold the job and return...
7970 */
7971
7972 if ((attr = ippFindAttribute(con->request, "job-hold-until",
7973 IPP_TAG_KEYWORD)) == NULL)
7974 attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
7975
7976 if (attr)
7977 {
7978 when = attr->values[0].string.text;
7979
7980 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
7981 "Job job-hold-until value changed by user.");
7982 }
7983 else
7984 when = "indefinite";
7985
7986 cupsdSetJobHoldUntil(job, when, 1);
7987 cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT, "Job held by \"%s\".",
7988 username);
7989
7990 con->response->request.status.status_code = IPP_OK;
7991 }
7992
7993
7994 /*
7995 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
7996 */
7997
7998 static void
7999 hold_new_jobs(cupsd_client_t *con, /* I - Connection */
8000 ipp_attribute_t *uri) /* I - Printer URI */
8001 {
8002 http_status_t status; /* Policy status */
8003 cups_ptype_t dtype; /* Destination type (printer/class) */
8004 cupsd_printer_t *printer; /* Printer data */
8005
8006
8007 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_new_jobs(%p[%d], %s)", con,
8008 con->number, uri->values[0].string.text);
8009
8010 /*
8011 * Is the destination valid?
8012 */
8013
8014 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8015 {
8016 /*
8017 * Bad URI...
8018 */
8019
8020 send_ipp_status(con, IPP_NOT_FOUND,
8021 _("The printer or class does not exist."));
8022 return;
8023 }
8024
8025 /*
8026 * Check policy...
8027 */
8028
8029 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8030 {
8031 send_http_error(con, status, printer);
8032 return;
8033 }
8034
8035 /*
8036 * Hold pending/new jobs sent to the printer...
8037 */
8038
8039 printer->holding_new_jobs = 1;
8040
8041 cupsdSetPrinterReasons(printer, "+hold-new-jobs");
8042
8043 if (dtype & CUPS_PRINTER_CLASS)
8044 cupsdLogMessage(CUPSD_LOG_INFO,
8045 "Class \"%s\" now holding pending/new jobs (\"%s\").",
8046 printer->name, get_username(con));
8047 else
8048 cupsdLogMessage(CUPSD_LOG_INFO,
8049 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
8050 printer->name, get_username(con));
8051
8052 /*
8053 * Everything was ok, so return OK status...
8054 */
8055
8056 con->response->request.status.status_code = IPP_OK;
8057 }
8058
8059
8060 /*
8061 * 'move_job()' - Move a job to a new destination.
8062 */
8063
8064 static void
8065 move_job(cupsd_client_t *con, /* I - Client connection */
8066 ipp_attribute_t *uri) /* I - Job URI */
8067 {
8068 http_status_t status; /* Policy status */
8069 ipp_attribute_t *attr; /* Current attribute */
8070 int jobid; /* Job ID */
8071 cupsd_job_t *job; /* Current job */
8072 const char *src; /* Source printer/class */
8073 cups_ptype_t stype, /* Source type (printer or class) */
8074 dtype; /* Destination type (printer/class) */
8075 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
8076 username[HTTP_MAX_URI], /* Username portion of URI */
8077 host[HTTP_MAX_URI], /* Host portion of URI */
8078 resource[HTTP_MAX_URI]; /* Resource portion of URI */
8079 int port; /* Port portion of URI */
8080 cupsd_printer_t *sprinter, /* Source printer */
8081 *dprinter; /* Destination printer */
8082
8083
8084 cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->number,
8085 uri->values[0].string.text);
8086
8087 /*
8088 * Get the new printer or class...
8089 */
8090
8091 if ((attr = ippFindAttribute(con->request, "job-printer-uri",
8092 IPP_TAG_URI)) == NULL)
8093 {
8094 /*
8095 * Need job-printer-uri...
8096 */
8097
8098 send_ipp_status(con, IPP_BAD_REQUEST,
8099 _("job-printer-uri attribute missing."));
8100 return;
8101 }
8102
8103 if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter))
8104 {
8105 /*
8106 * Bad URI...
8107 */
8108
8109 send_ipp_status(con, IPP_NOT_FOUND,
8110 _("The printer or class does not exist."));
8111 return;
8112 }
8113
8114 /*
8115 * See if we have a job URI or a printer URI...
8116 */
8117
8118 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
8119 sizeof(scheme), username, sizeof(username), host,
8120 sizeof(host), &port, resource, sizeof(resource));
8121
8122 if (!strcmp(uri->name, "printer-uri"))
8123 {
8124 /*
8125 * Got a printer URI; see if we also have a job-id attribute...
8126 */
8127
8128 if ((attr = ippFindAttribute(con->request, "job-id",
8129 IPP_TAG_INTEGER)) == NULL)
8130 {
8131 /*
8132 * Move all jobs...
8133 */
8134
8135 if ((src = cupsdValidateDest(uri->values[0].string.text, &stype,
8136 &sprinter)) == NULL)
8137 {
8138 /*
8139 * Bad URI...
8140 */
8141
8142 send_ipp_status(con, IPP_NOT_FOUND,
8143 _("The printer or class does not exist."));
8144 return;
8145 }
8146
8147 job = NULL;
8148 }
8149 else
8150 {
8151 /*
8152 * Otherwise, just move a single job...
8153 */
8154
8155 if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
8156 {
8157 /*
8158 * Nope - return a "not found" error...
8159 */
8160
8161 send_ipp_status(con, IPP_NOT_FOUND,
8162 _("Job #%d does not exist."), attr->values[0].integer);
8163 return;
8164 }
8165 else
8166 {
8167 /*
8168 * Job found, initialize source pointers...
8169 */
8170
8171 src = NULL;
8172 sprinter = NULL;
8173 }
8174 }
8175 }
8176 else
8177 {
8178 /*
8179 * Got a job URI; parse it to get the job ID...
8180 */
8181
8182 if (strncmp(resource, "/jobs/", 6))
8183 {
8184 /*
8185 * Not a valid URI!
8186 */
8187
8188 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
8189 uri->values[0].string.text);
8190 return;
8191 }
8192
8193 /*
8194 * See if the job exists...
8195 */
8196
8197 jobid = atoi(resource + 6);
8198
8199 if ((job = cupsdFindJob(jobid)) == NULL)
8200 {
8201 /*
8202 * Nope - return a "not found" error...
8203 */
8204
8205 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
8206 return;
8207 }
8208 else
8209 {
8210 /*
8211 * Job found, initialize source pointers...
8212 */
8213
8214 src = NULL;
8215 sprinter = NULL;
8216 }
8217 }
8218
8219 /*
8220 * Check the policy of the destination printer...
8221 */
8222
8223 if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con,
8224 job ? job->username : NULL)) != HTTP_OK)
8225 {
8226 send_http_error(con, status, dprinter);
8227 return;
8228 }
8229
8230 /*
8231 * Now move the job or jobs...
8232 */
8233
8234 if (job)
8235 {
8236 /*
8237 * See if the job has been completed...
8238 */
8239
8240 if (job->state_value > IPP_JOB_STOPPED)
8241 {
8242 /*
8243 * Return a "not-possible" error...
8244 */
8245
8246 send_ipp_status(con, IPP_NOT_POSSIBLE,
8247 _("Job #%d is finished and cannot be altered."),
8248 job->id);
8249 return;
8250 }
8251
8252 /*
8253 * See if the job is owned by the requesting user...
8254 */
8255
8256 if (!validate_user(job, con, job->username, username, sizeof(username)))
8257 {
8258 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
8259 cupsdFindDest(job->dest));
8260 return;
8261 }
8262
8263 /*
8264 * Move the job to a different printer or class...
8265 */
8266
8267 cupsdMoveJob(job, dprinter);
8268 }
8269 else
8270 {
8271 /*
8272 * Got the source printer, now look through the jobs...
8273 */
8274
8275 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
8276 job;
8277 job = (cupsd_job_t *)cupsArrayNext(Jobs))
8278 {
8279 /*
8280 * See if the job is pointing at the source printer or has not been
8281 * completed...
8282 */
8283
8284 if (_cups_strcasecmp(job->dest, src) ||
8285 job->state_value > IPP_JOB_STOPPED)
8286 continue;
8287
8288 /*
8289 * See if the job can be moved by the requesting user...
8290 */
8291
8292 if (!validate_user(job, con, job->username, username, sizeof(username)))
8293 continue;
8294
8295 /*
8296 * Move the job to a different printer or class...
8297 */
8298
8299 cupsdMoveJob(job, dprinter);
8300 }
8301 }
8302
8303 /*
8304 * Start jobs if possible...
8305 */
8306
8307 cupsdCheckJobs();
8308
8309 /*
8310 * Return with "everything is OK" status...
8311 */
8312
8313 con->response->request.status.status_code = IPP_OK;
8314 }
8315
8316
8317 /*
8318 * 'ppd_parse_line()' - Parse a PPD default line.
8319 */
8320
8321 static int /* O - 0 on success, -1 on failure */
8322 ppd_parse_line(const char *line, /* I - Line */
8323 char *option, /* O - Option name */
8324 int olen, /* I - Size of option name */
8325 char *choice, /* O - Choice name */
8326 int clen) /* I - Size of choice name */
8327 {
8328 /*
8329 * Verify this is a default option line...
8330 */
8331
8332 if (strncmp(line, "*Default", 8))
8333 return (-1);
8334
8335 /*
8336 * Read the option name...
8337 */
8338
8339 for (line += 8, olen --;
8340 *line > ' ' && *line < 0x7f && *line != ':' && *line != '/';
8341 line ++)
8342 if (olen > 0)
8343 {
8344 *option++ = *line;
8345 olen --;
8346 }
8347
8348 *option = '\0';
8349
8350 /*
8351 * Skip everything else up to the colon (:)...
8352 */
8353
8354 while (*line && *line != ':')
8355 line ++;
8356
8357 if (!*line)
8358 return (-1);
8359
8360 line ++;
8361
8362 /*
8363 * Now grab the option choice, skipping leading whitespace...
8364 */
8365
8366 while (isspace(*line & 255))
8367 line ++;
8368
8369 for (clen --;
8370 *line > ' ' && *line < 0x7f && *line != ':' && *line != '/';
8371 line ++)
8372 if (clen > 0)
8373 {
8374 *choice++ = *line;
8375 clen --;
8376 }
8377
8378 *choice = '\0';
8379
8380 /*
8381 * Return with no errors...
8382 */
8383
8384 return (0);
8385 }
8386
8387
8388 /*
8389 * 'print_job()' - Print a file to a printer or class.
8390 */
8391
8392 static void
8393 print_job(cupsd_client_t *con, /* I - Client connection */
8394 ipp_attribute_t *uri) /* I - Printer URI */
8395 {
8396 ipp_attribute_t *attr; /* Current attribute */
8397 ipp_attribute_t *doc_name; /* document-name attribute */
8398 ipp_attribute_t *format; /* Document-format attribute */
8399 const char *default_format; /* document-format-default value */
8400 cupsd_job_t *job; /* New job */
8401 char filename[1024]; /* Job filename */
8402 mime_type_t *filetype; /* Type of file */
8403 char super[MIME_MAX_SUPER], /* Supertype of file */
8404 type[MIME_MAX_TYPE], /* Subtype of file */
8405 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
8406 /* Textual name of mime type */
8407 cupsd_printer_t *printer; /* Printer data */
8408 struct stat fileinfo; /* File information */
8409 int kbytes; /* Size of file */
8410 int compression; /* Document compression */
8411
8412
8413 cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->number,
8414 uri->values[0].string.text);
8415
8416 /*
8417 * Validate print file attributes, for now just document-format and
8418 * compression (CUPS only supports "none" and "gzip")...
8419 */
8420
8421 compression = CUPS_FILE_NONE;
8422
8423 if ((attr = ippFindAttribute(con->request, "compression",
8424 IPP_TAG_KEYWORD)) != NULL)
8425 {
8426 if (strcmp(attr->values[0].string.text, "none")
8427 #ifdef HAVE_LIBZ
8428 && strcmp(attr->values[0].string.text, "gzip")
8429 #endif /* HAVE_LIBZ */
8430 )
8431 {
8432 send_ipp_status(con, IPP_ATTRIBUTES,
8433 _("Unsupported compression \"%s\"."),
8434 attr->values[0].string.text);
8435 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
8436 "compression", NULL, attr->values[0].string.text);
8437 return;
8438 }
8439
8440 #ifdef HAVE_LIBZ
8441 if (!strcmp(attr->values[0].string.text, "gzip"))
8442 compression = CUPS_FILE_GZIP;
8443 #endif /* HAVE_LIBZ */
8444 }
8445
8446 /*
8447 * Do we have a file to print?
8448 */
8449
8450 if (!con->filename)
8451 {
8452 send_ipp_status(con, IPP_BAD_REQUEST, _("No file in print request."));
8453 return;
8454 }
8455
8456 /*
8457 * Is the destination valid?
8458 */
8459
8460 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
8461 {
8462 /*
8463 * Bad URI...
8464 */
8465
8466 send_ipp_status(con, IPP_NOT_FOUND,
8467 _("The printer or class does not exist."));
8468 return;
8469 }
8470
8471 /*
8472 * Is it a format we support?
8473 */
8474
8475 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
8476 if (doc_name)
8477 ippSetName(con->request, &doc_name, "document-name-supplied");
8478
8479 if ((format = ippFindAttribute(con->request, "document-format",
8480 IPP_TAG_MIMETYPE)) != NULL)
8481 {
8482 /*
8483 * Grab format from client...
8484 */
8485
8486 if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]", super,
8487 type) != 2)
8488 {
8489 send_ipp_status(con, IPP_BAD_REQUEST,
8490 _("Bad document-format \"%s\"."),
8491 format->values[0].string.text);
8492 return;
8493 }
8494
8495 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-supplied", NULL, ippGetString(format, 0, NULL));
8496 }
8497 else if ((default_format = cupsGetOption("document-format",
8498 printer->num_options,
8499 printer->options)) != NULL)
8500 {
8501 /*
8502 * Use default document format...
8503 */
8504
8505 if (sscanf(default_format, "%15[^/]/%255[^;]", super, type) != 2)
8506 {
8507 send_ipp_status(con, IPP_BAD_REQUEST,
8508 _("Bad document-format \"%s\"."),
8509 default_format);
8510 return;
8511 }
8512 }
8513 else
8514 {
8515 /*
8516 * Auto-type it!
8517 */
8518
8519 strlcpy(super, "application", sizeof(super));
8520 strlcpy(type, "octet-stream", sizeof(type));
8521 }
8522
8523 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
8524 {
8525 /*
8526 * Auto-type the file...
8527 */
8528
8529 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file...");
8530
8531
8532 filetype = mimeFileType(MimeDatabase, con->filename,
8533 doc_name ? doc_name->values[0].string.text : NULL,
8534 &compression);
8535
8536 if (!filetype)
8537 filetype = mimeType(MimeDatabase, super, type);
8538
8539 cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.",
8540 filetype->super, filetype->type);
8541
8542 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, filetype->type);
8543 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-detected", NULL, mimetype);
8544 }
8545 else
8546 filetype = mimeType(MimeDatabase, super, type);
8547
8548 if (filetype &&
8549 (!format ||
8550 (!strcmp(super, "application") && !strcmp(type, "octet-stream"))))
8551 {
8552 /*
8553 * Replace the document-format attribute value with the auto-typed or
8554 * default one.
8555 */
8556
8557 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
8558 filetype->type);
8559
8560 if (format)
8561 ippSetString(con->request, &format, 0, mimetype);
8562 else
8563 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
8564 "document-format", NULL, mimetype);
8565 }
8566 else if (!filetype)
8567 {
8568 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
8569 _("Unsupported document-format \"%s\"."),
8570 format ? format->values[0].string.text :
8571 "application/octet-stream");
8572 cupsdLogMessage(CUPSD_LOG_INFO,
8573 "Hint: Do you have the raw file printing rules enabled?");
8574
8575 if (format)
8576 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
8577 "document-format", NULL, format->values[0].string.text);
8578
8579 return;
8580 }
8581
8582 /*
8583 * Read any embedded job ticket info from PS files...
8584 */
8585
8586 if (!_cups_strcasecmp(filetype->super, "application") &&
8587 (!_cups_strcasecmp(filetype->type, "postscript") ||
8588 !_cups_strcasecmp(filetype->type, "pdf")))
8589 read_job_ticket(con);
8590
8591 /*
8592 * Create the job object...
8593 */
8594
8595 if ((job = add_job(con, printer, filetype)) == NULL)
8596 return;
8597
8598 /*
8599 * Update quota data...
8600 */
8601
8602 if (stat(con->filename, &fileinfo))
8603 kbytes = 0;
8604 else
8605 kbytes = (fileinfo.st_size + 1023) / 1024;
8606
8607 cupsdUpdateQuota(printer, job->username, 0, kbytes);
8608
8609 job->koctets += kbytes;
8610
8611 if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL)
8612 attr->values[0].integer += kbytes;
8613
8614 /*
8615 * Add the job file...
8616 */
8617
8618 if (add_file(con, job, filetype, compression))
8619 return;
8620
8621 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, job->num_files);
8622 if (rename(con->filename, filename))
8623 {
8624 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to rename job document file \"%s\": %s", filename, strerror(errno));
8625
8626 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to rename job document file."));
8627 return;
8628 }
8629
8630 cupsdClearString(&con->filename);
8631
8632 /*
8633 * See if we need to add the ending sheet...
8634 */
8635
8636 if (cupsdTimeoutJob(job))
8637 return;
8638
8639 /*
8640 * Log and save the job...
8641 */
8642
8643 cupsdLogJob(job, CUPSD_LOG_INFO,
8644 "File of type %s/%s queued by \"%s\".",
8645 filetype->super, filetype->type, job->username);
8646 cupsdLogJob(job, CUPSD_LOG_DEBUG, "hold_until=%d", (int)job->hold_until);
8647 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
8648 job->dest, job->username);
8649
8650 /*
8651 * Start the job if possible...
8652 */
8653
8654 cupsdCheckJobs();
8655 }
8656
8657
8658 /*
8659 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8660 *
8661 * This function only gets called when printing a single PDF or PostScript
8662 * file using the Print-Job operation. It doesn't work for Create-Job +
8663 * Send-File, since the job attributes need to be set at job creation
8664 * time for banners to work. The embedded job ticket stuff is here
8665 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8666 * options and IPP attributes which otherwise would be lost.
8667 *
8668 * The format of a job ticket is simple:
8669 *
8670 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8671 *
8672 * %cupsJobTicket: attr1=value1
8673 * %cupsJobTicket: attr2=value2
8674 * ...
8675 * %cupsJobTicket: attrN=valueN
8676 *
8677 * Job ticket lines must appear immediately after the first line that
8678 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8679 * stops looking for job ticket info when it finds a line that does not begin
8680 * with "%cupsJobTicket:".
8681 *
8682 * The maximum length of a job ticket line, including the prefix, is
8683 * 255 characters to conform with the Adobe DSC.
8684 *
8685 * Read-only attributes are rejected with a notice to the error log in
8686 * case a malicious user tries anything. Since the job ticket is read
8687 * prior to attribute validation in print_job(), job ticket attributes
8688 * will go through the same validation as IPP attributes...
8689 */
8690
8691 static void
8692 read_job_ticket(cupsd_client_t *con) /* I - Client connection */
8693 {
8694 cups_file_t *fp; /* File to read from */
8695 char line[256]; /* Line data */
8696 int num_options; /* Number of options */
8697 cups_option_t *options; /* Options */
8698 ipp_t *ticket; /* New attributes */
8699 ipp_attribute_t *attr, /* Current attribute */
8700 *attr2, /* Job attribute */
8701 *prev2; /* Previous job attribute */
8702
8703
8704 /*
8705 * First open the print file...
8706 */
8707
8708 if ((fp = cupsFileOpen(con->filename, "rb")) == NULL)
8709 {
8710 cupsdLogMessage(CUPSD_LOG_ERROR,
8711 "Unable to open print file for job ticket - %s",
8712 strerror(errno));
8713 return;
8714 }
8715
8716 /*
8717 * Skip the first line...
8718 */
8719
8720 if (cupsFileGets(fp, line, sizeof(line)) == NULL)
8721 {
8722 cupsdLogMessage(CUPSD_LOG_ERROR,
8723 "Unable to read from print file for job ticket - %s",
8724 strerror(errno));
8725 cupsFileClose(fp);
8726 return;
8727 }
8728
8729 if (strncmp(line, "%!PS-Adobe-", 11) && strncmp(line, "%PDF-", 5))
8730 {
8731 /*
8732 * Not a DSC-compliant file, so no job ticket info will be available...
8733 */
8734
8735 cupsFileClose(fp);
8736 return;
8737 }
8738
8739 /*
8740 * Read job ticket info from the file...
8741 */
8742
8743 num_options = 0;
8744 options = NULL;
8745
8746 while (cupsFileGets(fp, line, sizeof(line)))
8747 {
8748 /*
8749 * Stop at the first non-ticket line...
8750 */
8751
8752 if (strncmp(line, "%cupsJobTicket:", 15))
8753 break;
8754
8755 /*
8756 * Add the options to the option array...
8757 */
8758
8759 num_options = cupsParseOptions(line + 15, num_options, &options);
8760 }
8761
8762 /*
8763 * Done with the file; see if we have any options...
8764 */
8765
8766 cupsFileClose(fp);
8767
8768 if (num_options == 0)
8769 return;
8770
8771 /*
8772 * OK, convert the options to an attribute list, and apply them to
8773 * the request...
8774 */
8775
8776 ticket = ippNew();
8777 cupsEncodeOptions(ticket, num_options, options);
8778
8779 /*
8780 * See what the user wants to change.
8781 */
8782
8783 for (attr = ticket->attrs; attr; attr = attr->next)
8784 {
8785 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
8786 continue;
8787
8788 if (!strncmp(attr->name, "date-time-at-", 13) ||
8789 !strcmp(attr->name, "job-impressions-completed") ||
8790 !strcmp(attr->name, "job-media-sheets-completed") ||
8791 !strncmp(attr->name, "job-k-octets", 12) ||
8792 !strcmp(attr->name, "job-id") ||
8793 !strcmp(attr->name, "job-originating-host-name") ||
8794 !strcmp(attr->name, "job-originating-user-name") ||
8795 !strcmp(attr->name, "job-pages-completed") ||
8796 !strcmp(attr->name, "job-printer-uri") ||
8797 !strncmp(attr->name, "job-state", 9) ||
8798 !strcmp(attr->name, "job-uri") ||
8799 !strncmp(attr->name, "time-at-", 8))
8800 continue; /* Read-only attrs */
8801
8802 if ((attr2 = ippFindAttribute(con->request, attr->name,
8803 IPP_TAG_ZERO)) != NULL)
8804 {
8805 /*
8806 * Some other value; first free the old value...
8807 */
8808
8809 if (con->request->attrs == attr2)
8810 {
8811 con->request->attrs = attr2->next;
8812 prev2 = NULL;
8813 }
8814 else
8815 {
8816 for (prev2 = con->request->attrs; prev2; prev2 = prev2->next)
8817 if (prev2->next == attr2)
8818 {
8819 prev2->next = attr2->next;
8820 break;
8821 }
8822 }
8823
8824 if (con->request->last == attr2)
8825 con->request->last = prev2;
8826
8827 ippDeleteAttribute(NULL, attr2);
8828 }
8829
8830 /*
8831 * Add new option by copying it...
8832 */
8833
8834 ippCopyAttribute(con->request, attr, 0);
8835 }
8836
8837 /*
8838 * Then free the attribute list and option array...
8839 */
8840
8841 ippDelete(ticket);
8842 cupsFreeOptions(num_options, options);
8843 }
8844
8845
8846 /*
8847 * 'reject_jobs()' - Reject print jobs to a printer.
8848 */
8849
8850 static void
8851 reject_jobs(cupsd_client_t *con, /* I - Client connection */
8852 ipp_attribute_t *uri) /* I - Printer or class URI */
8853 {
8854 http_status_t status; /* Policy status */
8855 cups_ptype_t dtype; /* Destination type (printer/class) */
8856 cupsd_printer_t *printer; /* Printer data */
8857 ipp_attribute_t *attr; /* printer-state-message text */
8858
8859
8860 cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con,
8861 con->number, uri->values[0].string.text);
8862
8863 /*
8864 * Is the destination valid?
8865 */
8866
8867 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8868 {
8869 /*
8870 * Bad URI...
8871 */
8872
8873 send_ipp_status(con, IPP_NOT_FOUND,
8874 _("The printer or class does not exist."));
8875 return;
8876 }
8877
8878 /*
8879 * Check policy...
8880 */
8881
8882 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8883 {
8884 send_http_error(con, status, printer);
8885 return;
8886 }
8887
8888 /*
8889 * Reject jobs sent to the printer...
8890 */
8891
8892 printer->accepting = 0;
8893
8894 if ((attr = ippFindAttribute(con->request, "printer-state-message",
8895 IPP_TAG_TEXT)) == NULL)
8896 strlcpy(printer->state_message, "Rejecting Jobs",
8897 sizeof(printer->state_message));
8898 else
8899 strlcpy(printer->state_message, attr->values[0].string.text,
8900 sizeof(printer->state_message));
8901
8902 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
8903 "No longer accepting jobs.");
8904
8905 if (dtype & CUPS_PRINTER_CLASS)
8906 {
8907 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
8908
8909 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").",
8910 printer->name, get_username(con));
8911 }
8912 else
8913 {
8914 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
8915
8916 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").",
8917 printer->name, get_username(con));
8918 }
8919
8920 /*
8921 * Everything was ok, so return OK status...
8922 */
8923
8924 con->response->request.status.status_code = IPP_OK;
8925 }
8926
8927
8928 /*
8929 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8930 */
8931
8932 static void
8933 release_held_new_jobs(
8934 cupsd_client_t *con, /* I - Connection */
8935 ipp_attribute_t *uri) /* I - Printer URI */
8936 {
8937 http_status_t status; /* Policy status */
8938 cups_ptype_t dtype; /* Destination type (printer/class) */
8939 cupsd_printer_t *printer; /* Printer data */
8940
8941
8942 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_held_new_jobs(%p[%d], %s)", con,
8943 con->number, uri->values[0].string.text);
8944
8945 /*
8946 * Is the destination valid?
8947 */
8948
8949 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8950 {
8951 /*
8952 * Bad URI...
8953 */
8954
8955 send_ipp_status(con, IPP_NOT_FOUND,
8956 _("The printer or class does not exist."));
8957 return;
8958 }
8959
8960 /*
8961 * Check policy...
8962 */
8963
8964 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8965 {
8966 send_http_error(con, status, printer);
8967 return;
8968 }
8969
8970 /*
8971 * Hold pending/new jobs sent to the printer...
8972 */
8973
8974 printer->holding_new_jobs = 0;
8975
8976 cupsdSetPrinterReasons(printer, "-hold-new-jobs");
8977
8978 if (dtype & CUPS_PRINTER_CLASS)
8979 cupsdLogMessage(CUPSD_LOG_INFO,
8980 "Class \"%s\" now printing pending/new jobs (\"%s\").",
8981 printer->name, get_username(con));
8982 else
8983 cupsdLogMessage(CUPSD_LOG_INFO,
8984 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
8985 printer->name, get_username(con));
8986
8987 cupsdCheckJobs();
8988
8989 /*
8990 * Everything was ok, so return OK status...
8991 */
8992
8993 con->response->request.status.status_code = IPP_OK;
8994 }
8995
8996
8997 /*
8998 * 'release_job()' - Release a held print job.
8999 */
9000
9001 static void
9002 release_job(cupsd_client_t *con, /* I - Client connection */
9003 ipp_attribute_t *uri) /* I - Job or Printer URI */
9004 {
9005 ipp_attribute_t *attr; /* Current attribute */
9006 int jobid; /* Job ID */
9007 char scheme[HTTP_MAX_URI], /* Method portion of URI */
9008 username[HTTP_MAX_URI], /* Username portion of URI */
9009 host[HTTP_MAX_URI], /* Host portion of URI */
9010 resource[HTTP_MAX_URI]; /* Resource portion of URI */
9011 int port; /* Port portion of URI */
9012 cupsd_job_t *job; /* Job information */
9013
9014
9015 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con,
9016 con->number, uri->values[0].string.text);
9017
9018 /*
9019 * See if we have a job URI or a printer URI...
9020 */
9021
9022 if (!strcmp(uri->name, "printer-uri"))
9023 {
9024 /*
9025 * Got a printer URI; see if we also have a job-id attribute...
9026 */
9027
9028 if ((attr = ippFindAttribute(con->request, "job-id",
9029 IPP_TAG_INTEGER)) == NULL)
9030 {
9031 send_ipp_status(con, IPP_BAD_REQUEST,
9032 _("Got a printer-uri attribute but no job-id."));
9033 return;
9034 }
9035
9036 jobid = attr->values[0].integer;
9037 }
9038 else
9039 {
9040 /*
9041 * Got a job URI; parse it to get the job ID...
9042 */
9043
9044 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9045 sizeof(scheme), username, sizeof(username), host,
9046 sizeof(host), &port, resource, sizeof(resource));
9047
9048 if (strncmp(resource, "/jobs/", 6))
9049 {
9050 /*
9051 * Not a valid URI!
9052 */
9053
9054 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
9055 uri->values[0].string.text);
9056 return;
9057 }
9058
9059 jobid = atoi(resource + 6);
9060 }
9061
9062 /*
9063 * See if the job exists...
9064 */
9065
9066 if ((job = cupsdFindJob(jobid)) == NULL)
9067 {
9068 /*
9069 * Nope - return a "not found" error...
9070 */
9071
9072 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
9073 return;
9074 }
9075
9076 /*
9077 * See if job is "held"...
9078 */
9079
9080 if (job->state_value != IPP_JOB_HELD)
9081 {
9082 /*
9083 * Nope - return a "not possible" error...
9084 */
9085
9086 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held."), jobid);
9087 return;
9088 }
9089
9090 /*
9091 * See if the job is owned by the requesting user...
9092 */
9093
9094 if (!validate_user(job, con, job->username, username, sizeof(username)))
9095 {
9096 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9097 cupsdFindDest(job->dest));
9098 return;
9099 }
9100
9101 /*
9102 * Reset the job-hold-until value to "no-hold"...
9103 */
9104
9105 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9106 IPP_TAG_KEYWORD)) == NULL)
9107 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9108
9109 if (attr)
9110 {
9111 ippSetValueTag(job->attrs, &attr, IPP_TAG_KEYWORD);
9112 ippSetString(job->attrs, &attr, 0, "no-hold");
9113
9114 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
9115 "Job job-hold-until value changed by user.");
9116 ippSetString(job->attrs, &job->reasons, 0, "none");
9117 }
9118
9119 /*
9120 * Release the job and return...
9121 */
9122
9123 cupsdReleaseJob(job);
9124
9125 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
9126 "Job released by user.");
9127
9128 cupsdLogJob(job, CUPSD_LOG_INFO, "Released by \"%s\".", username);
9129
9130 con->response->request.status.status_code = IPP_OK;
9131
9132 cupsdCheckJobs();
9133 }
9134
9135
9136 /*
9137 * 'renew_subscription()' - Renew an existing subscription...
9138 */
9139
9140 static void
9141 renew_subscription(
9142 cupsd_client_t *con, /* I - Client connection */
9143 int sub_id) /* I - Subscription ID */
9144 {
9145 http_status_t status; /* Policy status */
9146 cupsd_subscription_t *sub; /* Subscription */
9147 ipp_attribute_t *lease; /* notify-lease-duration */
9148
9149
9150 cupsdLogMessage(CUPSD_LOG_DEBUG2,
9151 "renew_subscription(con=%p[%d], sub_id=%d)",
9152 con, con->number, sub_id);
9153
9154 /*
9155 * Is the subscription ID valid?
9156 */
9157
9158 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
9159 {
9160 /*
9161 * Bad subscription ID...
9162 */
9163
9164 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
9165 sub_id);
9166 return;
9167 }
9168
9169 if (sub->job)
9170 {
9171 /*
9172 * Job subscriptions cannot be renewed...
9173 */
9174
9175 send_ipp_status(con, IPP_NOT_POSSIBLE,
9176 _("Job subscriptions cannot be renewed."));
9177 return;
9178 }
9179
9180 /*
9181 * Check policy...
9182 */
9183
9184 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
9185 DefaultPolicyPtr,
9186 con, sub->owner)) != HTTP_OK)
9187 {
9188 send_http_error(con, status, sub->dest);
9189 return;
9190 }
9191
9192 /*
9193 * Renew the subscription...
9194 */
9195
9196 lease = ippFindAttribute(con->request, "notify-lease-duration",
9197 IPP_TAG_INTEGER);
9198
9199 sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration;
9200
9201 if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration))
9202 {
9203 cupsdLogMessage(CUPSD_LOG_INFO,
9204 "renew_subscription: Limiting notify-lease-duration to "
9205 "%d seconds.",
9206 MaxLeaseDuration);
9207 sub->lease = MaxLeaseDuration;
9208 }
9209
9210 sub->expire = sub->lease ? time(NULL) + sub->lease : 0;
9211
9212 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
9213
9214 con->response->request.status.status_code = IPP_OK;
9215
9216 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
9217 "notify-lease-duration", sub->lease);
9218 }
9219
9220
9221 /*
9222 * 'restart_job()' - Restart an old print job.
9223 */
9224
9225 static void
9226 restart_job(cupsd_client_t *con, /* I - Client connection */
9227 ipp_attribute_t *uri) /* I - Job or Printer URI */
9228 {
9229 ipp_attribute_t *attr; /* Current attribute */
9230 int jobid; /* Job ID */
9231 cupsd_job_t *job; /* Job information */
9232 char scheme[HTTP_MAX_URI], /* Method portion of URI */
9233 username[HTTP_MAX_URI], /* Username portion of URI */
9234 host[HTTP_MAX_URI], /* Host portion of URI */
9235 resource[HTTP_MAX_URI]; /* Resource portion of URI */
9236 int port; /* Port portion of URI */
9237
9238
9239 cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con,
9240 con->number, uri->values[0].string.text);
9241
9242 /*
9243 * See if we have a job URI or a printer URI...
9244 */
9245
9246 if (!strcmp(uri->name, "printer-uri"))
9247 {
9248 /*
9249 * Got a printer URI; see if we also have a job-id attribute...
9250 */
9251
9252 if ((attr = ippFindAttribute(con->request, "job-id",
9253 IPP_TAG_INTEGER)) == NULL)
9254 {
9255 send_ipp_status(con, IPP_BAD_REQUEST,
9256 _("Got a printer-uri attribute but no job-id."));
9257 return;
9258 }
9259
9260 jobid = attr->values[0].integer;
9261 }
9262 else
9263 {
9264 /*
9265 * Got a job URI; parse it to get the job ID...
9266 */
9267
9268 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9269 sizeof(scheme), username, sizeof(username), host,
9270 sizeof(host), &port, resource, sizeof(resource));
9271
9272 if (strncmp(resource, "/jobs/", 6))
9273 {
9274 /*
9275 * Not a valid URI!
9276 */
9277
9278 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
9279 uri->values[0].string.text);
9280 return;
9281 }
9282
9283 jobid = atoi(resource + 6);
9284 }
9285
9286 /*
9287 * See if the job exists...
9288 */
9289
9290 if ((job = cupsdFindJob(jobid)) == NULL)
9291 {
9292 /*
9293 * Nope - return a "not found" error...
9294 */
9295
9296 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
9297 return;
9298 }
9299
9300 /*
9301 * See if job is in any of the "completed" states...
9302 */
9303
9304 if (job->state_value <= IPP_JOB_PROCESSING)
9305 {
9306 /*
9307 * Nope - return a "not possible" error...
9308 */
9309
9310 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete."),
9311 jobid);
9312 return;
9313 }
9314
9315 /*
9316 * See if we have retained the job files...
9317 */
9318
9319 cupsdLoadJob(job);
9320
9321 if (!job->attrs || job->num_files == 0)
9322 {
9323 /*
9324 * Nope - return a "not possible" error...
9325 */
9326
9327 send_ipp_status(con, IPP_NOT_POSSIBLE,
9328 _("Job #%d cannot be restarted - no files."), jobid);
9329 return;
9330 }
9331
9332 /*
9333 * See if the job is owned by the requesting user...
9334 */
9335
9336 if (!validate_user(job, con, job->username, username, sizeof(username)))
9337 {
9338 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9339 cupsdFindDest(job->dest));
9340 return;
9341 }
9342
9343 /*
9344 * See if the job-hold-until attribute is specified...
9345 */
9346
9347 if ((attr = ippFindAttribute(con->request, "job-hold-until",
9348 IPP_TAG_KEYWORD)) == NULL)
9349 attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
9350
9351 if (attr && strcmp(attr->values[0].string.text, "no-hold"))
9352 {
9353 /*
9354 * Return the job to a held state...
9355 */
9356
9357 cupsdLogJob(job, CUPSD_LOG_DEBUG,
9358 "Restarted by \"%s\" with job-hold-until=%s.",
9359 username, attr->values[0].string.text);
9360 cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
9361
9362 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE,
9363 NULL, job, "Job restarted by user with job-hold-until=%s",
9364 attr->values[0].string.text);
9365 }
9366 else
9367 {
9368 /*
9369 * Restart the job...
9370 */
9371
9372 cupsdRestartJob(job);
9373 cupsdCheckJobs();
9374 }
9375
9376 cupsdLogJob(job, CUPSD_LOG_INFO, "Restarted by \"%s\".", username);
9377
9378 con->response->request.status.status_code = IPP_OK;
9379 }
9380
9381
9382 /*
9383 * 'save_auth_info()' - Save authentication information for a job.
9384 */
9385
9386 static void
9387 save_auth_info(
9388 cupsd_client_t *con, /* I - Client connection */
9389 cupsd_job_t *job, /* I - Job */
9390 ipp_attribute_t *auth_info) /* I - auth-info attribute, if any */
9391 {
9392 int i; /* Looping var */
9393 char filename[1024]; /* Job authentication filename */
9394 cups_file_t *fp; /* Job authentication file */
9395 char line[65536]; /* Line for file */
9396 cupsd_printer_t *dest; /* Destination printer/class */
9397
9398
9399 /*
9400 * This function saves the in-memory authentication information for
9401 * a job so that it can be used to authenticate with a remote host.
9402 * The information is stored in a file that is readable only by the
9403 * root user. The fields are Base-64 encoded, each on a separate line,
9404 * followed by random number (up to 1024) of newlines to limit the
9405 * amount of information that is exposed.
9406 *
9407 * Because of the potential for exposing of authentication information,
9408 * this functionality is only enabled when running cupsd as root.
9409 *
9410 * This caching only works for the Basic and BasicDigest authentication
9411 * types. Digest authentication cannot be cached this way, and in
9412 * the future Kerberos authentication may make all of this obsolete.
9413 *
9414 * Authentication information is saved whenever an authenticated
9415 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9416 * performed.
9417 *
9418 * This information is deleted after a job is completed or canceled,
9419 * so reprints may require subsequent re-authentication.
9420 */
9421
9422 if (RunUser)
9423 return;
9424
9425 if ((dest = cupsdFindDest(job->dest)) == NULL)
9426 return;
9427
9428 /*
9429 * Create the authentication file and change permissions...
9430 */
9431
9432 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
9433 if ((fp = cupsFileOpen(filename, "w")) == NULL)
9434 {
9435 cupsdLogMessage(CUPSD_LOG_ERROR,
9436 "Unable to save authentication info to \"%s\" - %s",
9437 filename, strerror(errno));
9438 return;
9439 }
9440
9441 fchown(cupsFileNumber(fp), 0, 0);
9442 fchmod(cupsFileNumber(fp), 0400);
9443
9444 cupsFilePuts(fp, "CUPSD-AUTH-V3\n");
9445
9446 for (i = 0;
9447 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
9448 i ++)
9449 cupsdClearString(job->auth_env + i);
9450
9451 if (auth_info && auth_info->num_values == dest->num_auth_info_required)
9452 {
9453 /*
9454 * Write 1 to 3 auth values...
9455 */
9456
9457 for (i = 0;
9458 i < auth_info->num_values &&
9459 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
9460 i ++)
9461 {
9462 if (strcmp(dest->auth_info_required[i], "negotiate"))
9463 {
9464 httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text, (int)strlen(auth_info->values[i].string.text));
9465 cupsFilePutConf(fp, dest->auth_info_required[i], line);
9466 }
9467 else
9468 cupsFilePutConf(fp, dest->auth_info_required[i],
9469 auth_info->values[i].string.text);
9470
9471 if (!strcmp(dest->auth_info_required[i], "username"))
9472 cupsdSetStringf(job->auth_env + i, "AUTH_USERNAME=%s",
9473 auth_info->values[i].string.text);
9474 else if (!strcmp(dest->auth_info_required[i], "domain"))
9475 cupsdSetStringf(job->auth_env + i, "AUTH_DOMAIN=%s",
9476 auth_info->values[i].string.text);
9477 else if (!strcmp(dest->auth_info_required[i], "password"))
9478 cupsdSetStringf(job->auth_env + i, "AUTH_PASSWORD=%s",
9479 auth_info->values[i].string.text);
9480 else if (!strcmp(dest->auth_info_required[i], "negotiate"))
9481 cupsdSetStringf(job->auth_env + i, "AUTH_NEGOTIATE=%s",
9482 auth_info->values[i].string.text);
9483 else
9484 i --;
9485 }
9486 }
9487 else if (auth_info && auth_info->num_values == 2 &&
9488 dest->num_auth_info_required == 1 &&
9489 !strcmp(dest->auth_info_required[0], "negotiate"))
9490 {
9491 /*
9492 * Allow fallback to username+password for Kerberized queues...
9493 */
9494
9495 httpEncode64_2(line, sizeof(line), auth_info->values[0].string.text, (int)strlen(auth_info->values[0].string.text));
9496 cupsFilePutConf(fp, "username", line);
9497
9498 cupsdSetStringf(job->auth_env + 0, "AUTH_USERNAME=%s",
9499 auth_info->values[0].string.text);
9500
9501 httpEncode64_2(line, sizeof(line), auth_info->values[1].string.text, (int)strlen(auth_info->values[1].string.text));
9502 cupsFilePutConf(fp, "password", line);
9503
9504 cupsdSetStringf(job->auth_env + 1, "AUTH_PASSWORD=%s",
9505 auth_info->values[1].string.text);
9506 }
9507 else if (con->username[0])
9508 {
9509 /*
9510 * Write the authenticated username...
9511 */
9512
9513 httpEncode64_2(line, sizeof(line), con->username, (int)strlen(con->username));
9514 cupsFilePutConf(fp, "username", line);
9515
9516 cupsdSetStringf(job->auth_env + 0, "AUTH_USERNAME=%s", con->username);
9517
9518 /*
9519 * Write the authenticated password...
9520 */
9521
9522 httpEncode64_2(line, sizeof(line), con->password, (int)strlen(con->password));
9523 cupsFilePutConf(fp, "password", line);
9524
9525 cupsdSetStringf(job->auth_env + 1, "AUTH_PASSWORD=%s", con->password);
9526 }
9527
9528 #ifdef HAVE_GSSAPI
9529 if (con->gss_uid > 0)
9530 {
9531 cupsFilePrintf(fp, "uid %d\n", (int)con->gss_uid);
9532 cupsdSetStringf(&job->auth_uid, "AUTH_UID=%d", (int)con->gss_uid);
9533 }
9534 #endif /* HAVE_GSSAPI */
9535
9536 /*
9537 * Write a random number of newlines to the end of the file...
9538 */
9539
9540 for (i = (CUPS_RAND() % 1024); i >= 0; i --)
9541 cupsFilePutChar(fp, '\n');
9542
9543 /*
9544 * Close the file and return...
9545 */
9546
9547 cupsFileClose(fp);
9548 }
9549
9550
9551 /*
9552 * 'send_document()' - Send a file to a printer or class.
9553 */
9554
9555 static void
9556 send_document(cupsd_client_t *con, /* I - Client connection */
9557 ipp_attribute_t *uri) /* I - Printer URI */
9558 {
9559 ipp_attribute_t *attr; /* Current attribute */
9560 ipp_attribute_t *format; /* Request's document-format attribute */
9561 ipp_attribute_t *jformat; /* Job's document-format attribute */
9562 const char *default_format;/* document-format-default value */
9563 int jobid; /* Job ID number */
9564 cupsd_job_t *job; /* Current job */
9565 char job_uri[HTTP_MAX_URI],
9566 /* Job URI */
9567 scheme[HTTP_MAX_URI],
9568 /* Method portion of URI */
9569 username[HTTP_MAX_URI],
9570 /* Username portion of URI */
9571 host[HTTP_MAX_URI],
9572 /* Host portion of URI */
9573 resource[HTTP_MAX_URI];
9574 /* Resource portion of URI */
9575 int port; /* Port portion of URI */
9576 mime_type_t *filetype; /* Type of file */
9577 char super[MIME_MAX_SUPER],
9578 /* Supertype of file */
9579 type[MIME_MAX_TYPE],
9580 /* Subtype of file */
9581 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
9582 /* Textual name of mime type */
9583 char filename[1024]; /* Job filename */
9584 cupsd_printer_t *printer; /* Current printer */
9585 struct stat fileinfo; /* File information */
9586 int kbytes; /* Size of file */
9587 int compression; /* Type of compression */
9588 int start_job; /* Start the job? */
9589
9590
9591 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con,
9592 con->number, uri->values[0].string.text);
9593
9594 /*
9595 * See if we have a job URI or a printer URI...
9596 */
9597
9598 if (!strcmp(uri->name, "printer-uri"))
9599 {
9600 /*
9601 * Got a printer URI; see if we also have a job-id attribute...
9602 */
9603
9604 if ((attr = ippFindAttribute(con->request, "job-id",
9605 IPP_TAG_INTEGER)) == NULL)
9606 {
9607 send_ipp_status(con, IPP_BAD_REQUEST,
9608 _("Got a printer-uri attribute but no job-id."));
9609 return;
9610 }
9611
9612 jobid = attr->values[0].integer;
9613 }
9614 else
9615 {
9616 /*
9617 * Got a job URI; parse it to get the job ID...
9618 */
9619
9620 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9621 sizeof(scheme), username, sizeof(username), host,
9622 sizeof(host), &port, resource, sizeof(resource));
9623
9624 if (strncmp(resource, "/jobs/", 6))
9625 {
9626 /*
9627 * Not a valid URI!
9628 */
9629
9630 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
9631 uri->values[0].string.text);
9632 return;
9633 }
9634
9635 jobid = atoi(resource + 6);
9636 }
9637
9638 /*
9639 * See if the job exists...
9640 */
9641
9642 if ((job = cupsdFindJob(jobid)) == NULL)
9643 {
9644 /*
9645 * Nope - return a "not found" error...
9646 */
9647
9648 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
9649 return;
9650 }
9651
9652 printer = cupsdFindDest(job->dest);
9653
9654 /*
9655 * See if the job is owned by the requesting user...
9656 */
9657
9658 if (!validate_user(job, con, job->username, username, sizeof(username)))
9659 {
9660 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9661 cupsdFindDest(job->dest));
9662 return;
9663 }
9664
9665 /*
9666 * OK, see if the client is sending the document compressed - CUPS
9667 * only supports "none" and "gzip".
9668 */
9669
9670 compression = CUPS_FILE_NONE;
9671
9672 if ((attr = ippFindAttribute(con->request, "compression",
9673 IPP_TAG_KEYWORD)) != NULL)
9674 {
9675 if (strcmp(attr->values[0].string.text, "none")
9676 #ifdef HAVE_LIBZ
9677 && strcmp(attr->values[0].string.text, "gzip")
9678 #endif /* HAVE_LIBZ */
9679 )
9680 {
9681 send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"."),
9682 attr->values[0].string.text);
9683 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
9684 "compression", NULL, attr->values[0].string.text);
9685 return;
9686 }
9687
9688 #ifdef HAVE_LIBZ
9689 if (!strcmp(attr->values[0].string.text, "gzip"))
9690 compression = CUPS_FILE_GZIP;
9691 #endif /* HAVE_LIBZ */
9692 }
9693
9694 /*
9695 * Do we have a file to print?
9696 */
9697
9698 if ((attr = ippFindAttribute(con->request, "last-document",
9699 IPP_TAG_BOOLEAN)) == NULL)
9700 {
9701 send_ipp_status(con, IPP_BAD_REQUEST,
9702 _("Missing last-document attribute in request."));
9703 return;
9704 }
9705
9706 if (!con->filename)
9707 {
9708 /*
9709 * Check for an empty request with "last-document" set to true, which is
9710 * used to close an "open" job by RFC 2911, section 3.3.2.
9711 */
9712
9713 if (job->num_files > 0 && attr->values[0].boolean)
9714 goto last_document;
9715
9716 send_ipp_status(con, IPP_BAD_REQUEST, _("No file in print request."));
9717 return;
9718 }
9719
9720 /*
9721 * Is it a format we support?
9722 */
9723
9724 cupsdLoadJob(job);
9725
9726 if ((format = ippFindAttribute(con->request, "document-format",
9727 IPP_TAG_MIMETYPE)) != NULL)
9728 {
9729 /*
9730 * Grab format from client...
9731 */
9732
9733 if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]",
9734 super, type) != 2)
9735 {
9736 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"."),
9737 format->values[0].string.text);
9738 return;
9739 }
9740
9741 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-supplied", NULL, ippGetString(format, 0, NULL));
9742 }
9743 else if ((default_format = cupsGetOption("document-format",
9744 printer->num_options,
9745 printer->options)) != NULL)
9746 {
9747 /*
9748 * Use default document format...
9749 */
9750
9751 if (sscanf(default_format, "%15[^/]/%255[^;]", super, type) != 2)
9752 {
9753 send_ipp_status(con, IPP_BAD_REQUEST,
9754 _("Bad document-format-default \"%s\"."), default_format);
9755 return;
9756 }
9757 }
9758 else
9759 {
9760 /*
9761 * No document format attribute? Auto-type it!
9762 */
9763
9764 strlcpy(super, "application", sizeof(super));
9765 strlcpy(type, "octet-stream", sizeof(type));
9766 }
9767
9768 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
9769 {
9770 /*
9771 * Auto-type the file...
9772 */
9773
9774 ipp_attribute_t *doc_name; /* document-name attribute */
9775
9776
9777 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Auto-typing file...");
9778
9779 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
9780 filetype = mimeFileType(MimeDatabase, con->filename,
9781 doc_name ? doc_name->values[0].string.text : NULL,
9782 &compression);
9783
9784 if (!filetype)
9785 filetype = mimeType(MimeDatabase, super, type);
9786
9787 if (filetype)
9788 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Request file type is %s/%s.",
9789 filetype->super, filetype->type);
9790
9791 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, filetype->type);
9792 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-detected", NULL, mimetype);
9793 }
9794 else
9795 filetype = mimeType(MimeDatabase, super, type);
9796
9797 if (filetype)
9798 {
9799 /*
9800 * Replace the document-format attribute value with the auto-typed or
9801 * default one.
9802 */
9803
9804 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9805 filetype->type);
9806
9807 if ((jformat = ippFindAttribute(job->attrs, "document-format",
9808 IPP_TAG_MIMETYPE)) != NULL)
9809 ippSetString(job->attrs, &jformat, 0, mimetype);
9810 else
9811 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
9812 "document-format", NULL, mimetype);
9813 }
9814 else if (!filetype)
9815 {
9816 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9817 _("Unsupported document-format \"%s/%s\"."), super, type);
9818 cupsdLogMessage(CUPSD_LOG_INFO,
9819 "Hint: Do you have the raw file printing rules enabled?");
9820
9821 if (format)
9822 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9823 "document-format", NULL, format->values[0].string.text);
9824
9825 return;
9826 }
9827
9828 if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype))
9829 {
9830 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9831 filetype->type);
9832
9833 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9834 _("Unsupported document-format \"%s\"."), mimetype);
9835
9836 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9837 "document-format", NULL, mimetype);
9838
9839 return;
9840 }
9841
9842 /*
9843 * Add the file to the job...
9844 */
9845
9846 if (add_file(con, job, filetype, compression))
9847 return;
9848
9849 if ((attr = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME)) != NULL)
9850 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "document-name-supplied", NULL, ippGetString(attr, 0, NULL));
9851
9852 if (stat(con->filename, &fileinfo))
9853 kbytes = 0;
9854 else
9855 kbytes = (fileinfo.st_size + 1023) / 1024;
9856
9857 cupsdUpdateQuota(printer, job->username, 0, kbytes);
9858
9859 job->koctets += kbytes;
9860
9861 if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL)
9862 attr->values[0].integer += kbytes;
9863
9864 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, job->num_files);
9865 if (rename(con->filename, filename))
9866 {
9867 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to rename job document file \"%s\": %s", filename, strerror(errno));
9868
9869 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to rename job document file."));
9870 return;
9871 }
9872
9873 cupsdClearString(&con->filename);
9874
9875 cupsdLogJob(job, CUPSD_LOG_INFO, "File of type %s/%s queued by \"%s\".",
9876 filetype->super, filetype->type, job->username);
9877
9878 /*
9879 * Start the job if this is the last document...
9880 */
9881
9882 last_document:
9883
9884 if ((attr = ippFindAttribute(con->request, "last-document",
9885 IPP_TAG_BOOLEAN)) != NULL &&
9886 attr->values[0].boolean)
9887 {
9888 /*
9889 * See if we need to add the ending sheet...
9890 */
9891
9892 if (cupsdTimeoutJob(job))
9893 return;
9894
9895 if (job->state_value == IPP_JOB_STOPPED)
9896 {
9897 job->state->values[0].integer = IPP_JOB_PENDING;
9898 job->state_value = IPP_JOB_PENDING;
9899
9900 ippSetString(job->attrs, &job->reasons, 0, "none");
9901 }
9902 else if (job->state_value == IPP_JOB_HELD)
9903 {
9904 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9905 IPP_TAG_KEYWORD)) == NULL)
9906 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9907
9908 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
9909 {
9910 job->state->values[0].integer = IPP_JOB_PENDING;
9911 job->state_value = IPP_JOB_PENDING;
9912
9913 ippSetString(job->attrs, &job->reasons, 0, "none");
9914 }
9915 else
9916 ippSetString(job->attrs, &job->reasons, 0, "job-hold-until-specified");
9917 }
9918
9919 job->dirty = 1;
9920 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
9921
9922 start_job = 1;
9923 }
9924 else
9925 {
9926 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9927 IPP_TAG_KEYWORD)) == NULL)
9928 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9929
9930 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
9931 {
9932 job->state->values[0].integer = IPP_JOB_HELD;
9933 job->state_value = IPP_JOB_HELD;
9934 job->hold_until = time(NULL) + MultipleOperationTimeout;
9935
9936 ippSetString(job->attrs, &job->reasons, 0, "job-incoming");
9937
9938 job->dirty = 1;
9939 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
9940 }
9941
9942 start_job = 0;
9943 }
9944
9945 /*
9946 * Fill in the response info...
9947 */
9948
9949 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
9950 con->clientname, con->clientport, "/jobs/%d", jobid);
9951 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
9952 job_uri);
9953
9954 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid);
9955
9956 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
9957 job->state_value);
9958 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons",
9959 NULL, job->reasons->values[0].string.text);
9960
9961 con->response->request.status.status_code = IPP_OK;
9962
9963 /*
9964 * Start the job if necessary...
9965 */
9966
9967 if (start_job)
9968 cupsdCheckJobs();
9969 }
9970
9971
9972 /*
9973 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9974 */
9975
9976 static void
9977 send_http_error(
9978 cupsd_client_t *con, /* I - Client connection */
9979 http_status_t status, /* I - HTTP status code */
9980 cupsd_printer_t *printer) /* I - Printer, if any */
9981 {
9982 ipp_attribute_t *uri; /* Request URI, if any */
9983
9984
9985 if ((uri = ippFindAttribute(con->request, "printer-uri",
9986 IPP_TAG_URI)) == NULL)
9987 uri = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI);
9988
9989 cupsdLogMessage(status == HTTP_FORBIDDEN ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
9990 "[Client %d] Returning HTTP %s for %s (%s) from %s",
9991 con->number, httpStatus(status),
9992 con->request ?
9993 ippOpString(con->request->request.op.operation_id) :
9994 "no operation-id",
9995 uri ? uri->values[0].string.text : "no URI",
9996 con->http->hostname);
9997
9998 if (printer)
9999 {
10000 int auth_type; /* Type of authentication required */
10001
10002
10003 auth_type = CUPSD_AUTH_NONE;
10004
10005 if (status == HTTP_UNAUTHORIZED &&
10006 printer->num_auth_info_required > 0 &&
10007 !strcmp(printer->auth_info_required[0], "negotiate") &&
10008 con->request &&
10009 (con->request->request.op.operation_id == IPP_PRINT_JOB ||
10010 con->request->request.op.operation_id == IPP_CREATE_JOB ||
10011 con->request->request.op.operation_id == CUPS_AUTHENTICATE_JOB))
10012 {
10013 /*
10014 * Creating and authenticating jobs requires Kerberos...
10015 */
10016
10017 auth_type = CUPSD_AUTH_NEGOTIATE;
10018 }
10019 else
10020 {
10021 /*
10022 * Use policy/location-defined authentication requirements...
10023 */
10024
10025 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
10026 cupsd_location_t *auth; /* Pointer to authentication element */
10027
10028
10029 if (printer->type & CUPS_PRINTER_CLASS)
10030 snprintf(resource, sizeof(resource), "/classes/%s", printer->name);
10031 else
10032 snprintf(resource, sizeof(resource), "/printers/%s", printer->name);
10033
10034 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
10035 auth->type == CUPSD_AUTH_NONE)
10036 auth = cupsdFindPolicyOp(printer->op_policy_ptr,
10037 con->request ?
10038 con->request->request.op.operation_id :
10039 IPP_PRINT_JOB);
10040
10041 if (auth)
10042 {
10043 if (auth->type == CUPSD_AUTH_DEFAULT)
10044 auth_type = cupsdDefaultAuthType();
10045 else
10046 auth_type = auth->type;
10047 }
10048 }
10049
10050 cupsdSendError(con, status, auth_type);
10051 }
10052 else
10053 cupsdSendError(con, status, CUPSD_AUTH_NONE);
10054
10055 ippDelete(con->response);
10056 con->response = NULL;
10057
10058 return;
10059 }
10060
10061
10062 /*
10063 * 'send_ipp_status()' - Send a status back to the IPP client.
10064 */
10065
10066 static void
10067 send_ipp_status(cupsd_client_t *con, /* I - Client connection */
10068 ipp_status_t status, /* I - IPP status code */
10069 const char *message,/* I - Status message */
10070 ...) /* I - Additional args as needed */
10071 {
10072 va_list ap; /* Pointer to additional args */
10073 char formatted[1024]; /* Formatted errror message */
10074
10075
10076 va_start(ap, message);
10077 vsnprintf(formatted, sizeof(formatted),
10078 _cupsLangString(con->language, message), ap);
10079 va_end(ap);
10080
10081 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s",
10082 ippOpString(con->request->request.op.operation_id),
10083 ippErrorString(status), formatted);
10084
10085 con->response->request.status.status_code = status;
10086
10087 if (ippFindAttribute(con->response, "attributes-charset",
10088 IPP_TAG_ZERO) == NULL)
10089 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
10090 "attributes-charset", NULL, "utf-8");
10091
10092 if (ippFindAttribute(con->response, "attributes-natural-language",
10093 IPP_TAG_ZERO) == NULL)
10094 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
10095 "attributes-natural-language", NULL, DefaultLanguage);
10096
10097 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT,
10098 "status-message", NULL, formatted);
10099 }
10100
10101
10102 /*
10103 * 'set_default()' - Set the default destination...
10104 */
10105
10106 static void
10107 set_default(cupsd_client_t *con, /* I - Client connection */
10108 ipp_attribute_t *uri) /* I - Printer URI */
10109 {
10110 http_status_t status; /* Policy status */
10111 cups_ptype_t dtype; /* Destination type (printer/class) */
10112 cupsd_printer_t *printer, /* Printer */
10113 *oldprinter; /* Old default printer */
10114
10115
10116 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con,
10117 con->number, uri->values[0].string.text);
10118
10119 /*
10120 * Is the destination valid?
10121 */
10122
10123 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10124 {
10125 /*
10126 * Bad URI...
10127 */
10128
10129 send_ipp_status(con, IPP_NOT_FOUND,
10130 _("The printer or class does not exist."));
10131 return;
10132 }
10133
10134 /*
10135 * Check policy...
10136 */
10137
10138 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
10139 {
10140 send_http_error(con, status, NULL);
10141 return;
10142 }
10143
10144 /*
10145 * Set it as the default...
10146 */
10147
10148 oldprinter = DefaultPrinter;
10149 DefaultPrinter = printer;
10150
10151 if (oldprinter)
10152 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, oldprinter, NULL,
10153 "%s is no longer the default printer.", oldprinter->name);
10154
10155 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
10156 "%s is now the default printer.", printer->name);
10157
10158 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES |
10159 CUPSD_DIRTY_PRINTCAP);
10160
10161 cupsdLogMessage(CUPSD_LOG_INFO,
10162 "Default destination set to \"%s\" by \"%s\".",
10163 printer->name, get_username(con));
10164
10165 /*
10166 * Everything was ok, so return OK status...
10167 */
10168
10169 con->response->request.status.status_code = IPP_OK;
10170 }
10171
10172
10173 /*
10174 * 'set_job_attrs()' - Set job attributes.
10175 */
10176
10177 static void
10178 set_job_attrs(cupsd_client_t *con, /* I - Client connection */
10179 ipp_attribute_t *uri) /* I - Job URI */
10180 {
10181 ipp_attribute_t *attr, /* Current attribute */
10182 *attr2; /* Job attribute */
10183 int jobid; /* Job ID */
10184 cupsd_job_t *job; /* Current job */
10185 char scheme[HTTP_MAX_URI],
10186 /* Method portion of URI */
10187 username[HTTP_MAX_URI],
10188 /* Username portion of URI */
10189 host[HTTP_MAX_URI],
10190 /* Host portion of URI */
10191 resource[HTTP_MAX_URI];
10192 /* Resource portion of URI */
10193 int port; /* Port portion of URI */
10194 int event; /* Events? */
10195 int check_jobs; /* Check jobs? */
10196
10197
10198 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con,
10199 con->number, uri->values[0].string.text);
10200
10201 /*
10202 * Start with "everything is OK" status...
10203 */
10204
10205 con->response->request.status.status_code = IPP_OK;
10206
10207 /*
10208 * See if we have a job URI or a printer URI...
10209 */
10210
10211 if (!strcmp(uri->name, "printer-uri"))
10212 {
10213 /*
10214 * Got a printer URI; see if we also have a job-id attribute...
10215 */
10216
10217 if ((attr = ippFindAttribute(con->request, "job-id",
10218 IPP_TAG_INTEGER)) == NULL)
10219 {
10220 send_ipp_status(con, IPP_BAD_REQUEST,
10221 _("Got a printer-uri attribute but no job-id."));
10222 return;
10223 }
10224
10225 jobid = attr->values[0].integer;
10226 }
10227 else
10228 {
10229 /*
10230 * Got a job URI; parse it to get the job ID...
10231 */
10232
10233 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
10234 sizeof(scheme), username, sizeof(username), host,
10235 sizeof(host), &port, resource, sizeof(resource));
10236
10237 if (strncmp(resource, "/jobs/", 6))
10238 {
10239 /*
10240 * Not a valid URI!
10241 */
10242
10243 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
10244 uri->values[0].string.text);
10245 return;
10246 }
10247
10248 jobid = atoi(resource + 6);
10249 }
10250
10251 /*
10252 * See if the job exists...
10253 */
10254
10255 if ((job = cupsdFindJob(jobid)) == NULL)
10256 {
10257 /*
10258 * Nope - return a "not found" error...
10259 */
10260
10261 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
10262 return;
10263 }
10264
10265 /*
10266 * See if the job has been completed...
10267 */
10268
10269 if (job->state_value > IPP_JOB_STOPPED)
10270 {
10271 /*
10272 * Return a "not-possible" error...
10273 */
10274
10275 send_ipp_status(con, IPP_NOT_POSSIBLE,
10276 _("Job #%d is finished and cannot be altered."), jobid);
10277 return;
10278 }
10279
10280 /*
10281 * See if the job is owned by the requesting user...
10282 */
10283
10284 if (!validate_user(job, con, job->username, username, sizeof(username)))
10285 {
10286 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
10287 cupsdFindDest(job->dest));
10288 return;
10289 }
10290
10291 /*
10292 * See what the user wants to change.
10293 */
10294
10295 cupsdLoadJob(job);
10296
10297 check_jobs = 0;
10298 event = 0;
10299
10300 for (attr = con->request->attrs; attr; attr = attr->next)
10301 {
10302 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
10303 continue;
10304
10305 if (!strcmp(attr->name, "attributes-charset") ||
10306 !strcmp(attr->name, "attributes-natural-language") ||
10307 !strncmp(attr->name, "date-time-at-", 13) ||
10308 !strncmp(attr->name, "document-compression", 20) ||
10309 !strncmp(attr->name, "document-format", 15) ||
10310 !strcmp(attr->name, "job-detailed-status-messages") ||
10311 !strcmp(attr->name, "job-document-access-errors") ||
10312 !strcmp(attr->name, "job-id") ||
10313 !strcmp(attr->name, "job-impressions-completed") ||
10314 !strcmp(attr->name, "job-k-octets-completed") ||
10315 !strcmp(attr->name, "job-media-sheets-completed") ||
10316 !strcmp(attr->name, "job-originating-host-name") ||
10317 !strcmp(attr->name, "job-originating-user-name") ||
10318 !strcmp(attr->name, "job-pages-completed") ||
10319 !strcmp(attr->name, "job-printer-up-time") ||
10320 !strcmp(attr->name, "job-printer-uri") ||
10321 !strcmp(attr->name, "job-sheets") ||
10322 !strcmp(attr->name, "job-state-message") ||
10323 !strcmp(attr->name, "job-state-reasons") ||
10324 !strcmp(attr->name, "job-uri") ||
10325 !strcmp(attr->name, "number-of-documents") ||
10326 !strcmp(attr->name, "number-of-intervening-jobs") ||
10327 !strcmp(attr->name, "output-device-assigned") ||
10328 !strncmp(attr->name, "time-at-", 8))
10329 {
10330 /*
10331 * Read-only attrs!
10332 */
10333
10334 send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE,
10335 _("%s cannot be changed."), attr->name);
10336
10337 attr2 = ippCopyAttribute(con->response, attr, 0);
10338 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
10339 continue;
10340 }
10341
10342 if (!strcmp(attr->name, "job-priority"))
10343 {
10344 /*
10345 * Change the job priority...
10346 */
10347
10348 if (attr->value_tag != IPP_TAG_INTEGER)
10349 {
10350 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value."));
10351
10352 attr2 = ippCopyAttribute(con->response, attr, 0);
10353 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
10354 }
10355 else if (job->state_value >= IPP_JOB_PROCESSING)
10356 {
10357 send_ipp_status(con, IPP_NOT_POSSIBLE,
10358 _("Job is completed and cannot be changed."));
10359 return;
10360 }
10361 else if (con->response->request.status.status_code == IPP_OK)
10362 {
10363 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-priority to %d",
10364 attr->values[0].integer);
10365 cupsdSetJobPriority(job, attr->values[0].integer);
10366
10367 check_jobs = 1;
10368 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED |
10369 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED;
10370 }
10371 }
10372 else if (!strcmp(attr->name, "job-state"))
10373 {
10374 /*
10375 * Change the job state...
10376 */
10377
10378 if (attr->value_tag != IPP_TAG_ENUM)
10379 {
10380 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value."));
10381
10382 attr2 = ippCopyAttribute(con->response, attr, 0);
10383 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
10384 }
10385 else
10386 {
10387 switch (attr->values[0].integer)
10388 {
10389 case IPP_JOB_PENDING :
10390 case IPP_JOB_HELD :
10391 if (job->state_value > IPP_JOB_HELD)
10392 {
10393 send_ipp_status(con, IPP_NOT_POSSIBLE,
10394 _("Job state cannot be changed."));
10395 return;
10396 }
10397 else if (con->response->request.status.status_code == IPP_OK)
10398 {
10399 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10400 attr->values[0].integer);
10401 cupsdSetJobState(job, (ipp_jstate_t)attr->values[0].integer, CUPSD_JOB_DEFAULT, "Job state changed by \"%s\"", username);
10402 check_jobs = 1;
10403 }
10404 break;
10405
10406 case IPP_JOB_PROCESSING :
10407 case IPP_JOB_STOPPED :
10408 if (job->state_value != attr->values[0].integer)
10409 {
10410 send_ipp_status(con, IPP_NOT_POSSIBLE,
10411 _("Job state cannot be changed."));
10412 return;
10413 }
10414 break;
10415
10416 case IPP_JOB_CANCELED :
10417 case IPP_JOB_ABORTED :
10418 case IPP_JOB_COMPLETED :
10419 if (job->state_value > IPP_JOB_PROCESSING)
10420 {
10421 send_ipp_status(con, IPP_NOT_POSSIBLE,
10422 _("Job state cannot be changed."));
10423 return;
10424 }
10425 else if (con->response->request.status.status_code == IPP_OK)
10426 {
10427 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10428 attr->values[0].integer);
10429 cupsdSetJobState(job, (ipp_jstate_t)attr->values[0].integer,
10430 CUPSD_JOB_DEFAULT,
10431 "Job state changed by \"%s\"", username);
10432 check_jobs = 1;
10433 }
10434 break;
10435 }
10436 }
10437 }
10438 else if (con->response->request.status.status_code != IPP_OK)
10439 continue;
10440 else if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10441 IPP_TAG_ZERO)) != NULL)
10442 {
10443 /*
10444 * Some other value; first free the old value...
10445 */
10446
10447 if (job->attrs->prev)
10448 job->attrs->prev->next = attr2->next;
10449 else
10450 job->attrs->attrs = attr2->next;
10451
10452 if (job->attrs->last == attr2)
10453 job->attrs->last = job->attrs->prev;
10454
10455 ippDeleteAttribute(NULL, attr2);
10456
10457 /*
10458 * Then copy the attribute...
10459 */
10460
10461 ippCopyAttribute(job->attrs, attr, 0);
10462
10463 /*
10464 * See if the job-name or job-hold-until is being changed.
10465 */
10466
10467 if (!strcmp(attr->name, "job-hold-until"))
10468 {
10469 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s",
10470 attr->values[0].string.text);
10471 cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
10472
10473 if (!strcmp(attr->values[0].string.text, "no-hold"))
10474 {
10475 cupsdReleaseJob(job);
10476 check_jobs = 1;
10477 }
10478 else
10479 cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT,
10480 "Job held by \"%s\".", username);
10481
10482 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
10483 }
10484 }
10485 else if (attr->value_tag == IPP_TAG_DELETEATTR)
10486 {
10487 /*
10488 * Delete the attribute...
10489 */
10490
10491 if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10492 IPP_TAG_ZERO)) != NULL)
10493 {
10494 if (job->attrs->prev)
10495 job->attrs->prev->next = attr2->next;
10496 else
10497 job->attrs->attrs = attr2->next;
10498
10499 if (attr2 == job->attrs->last)
10500 job->attrs->last = job->attrs->prev;
10501
10502 ippDeleteAttribute(NULL, attr2);
10503
10504 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
10505 }
10506 }
10507 else
10508 {
10509 /*
10510 * Add new option by copying it...
10511 */
10512
10513 ippCopyAttribute(job->attrs, attr, 0);
10514
10515 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
10516 }
10517 }
10518
10519 /*
10520 * Save the job...
10521 */
10522
10523 job->dirty = 1;
10524 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
10525
10526 /*
10527 * Send events as needed...
10528 */
10529
10530 if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED)
10531 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED,
10532 cupsdFindDest(job->dest), job,
10533 "Job priority changed by user.");
10534
10535 if (event & CUPSD_EVENT_JOB_STATE)
10536 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
10537 job->state_value == IPP_JOB_HELD ?
10538 "Job held by user." : "Job restarted by user.");
10539
10540 if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED)
10541 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
10542 "Job options changed by user.");
10543
10544 /*
10545 * Start jobs if possible...
10546 */
10547
10548 if (check_jobs)
10549 cupsdCheckJobs();
10550 }
10551
10552
10553 /*
10554 * 'set_printer_attrs()' - Set printer attributes.
10555 */
10556
10557 static void
10558 set_printer_attrs(cupsd_client_t *con, /* I - Client connection */
10559 ipp_attribute_t *uri) /* I - Printer */
10560 {
10561 http_status_t status; /* Policy status */
10562 cups_ptype_t dtype; /* Destination type (printer/class) */
10563 cupsd_printer_t *printer; /* Printer/class */
10564 ipp_attribute_t *attr; /* Printer attribute */
10565 int changed = 0; /* Was anything changed? */
10566
10567
10568 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_attrs(%p[%d], %s)", con,
10569 con->number, uri->values[0].string.text);
10570
10571 /*
10572 * Is the destination valid?
10573 */
10574
10575 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10576 {
10577 /*
10578 * Bad URI...
10579 */
10580
10581 send_ipp_status(con, IPP_NOT_FOUND,
10582 _("The printer or class does not exist."));
10583 return;
10584 }
10585
10586 /*
10587 * Check policy...
10588 */
10589
10590 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10591 {
10592 send_http_error(con, status, printer);
10593 return;
10594 }
10595
10596 /*
10597 * Return a list of attributes that can be set via Set-Printer-Attributes.
10598 */
10599
10600 if ((attr = ippFindAttribute(con->request, "printer-location",
10601 IPP_TAG_TEXT)) != NULL)
10602 {
10603 cupsdSetString(&printer->location, attr->values[0].string.text);
10604 changed = 1;
10605 }
10606
10607 if ((attr = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI)) != NULL && !strncmp(attr->values[0].string.text, "geo:", 4))
10608 {
10609 cupsdSetString(&printer->geo_location, attr->values[0].string.text);
10610 changed = 1;
10611 }
10612
10613 if ((attr = ippFindAttribute(con->request, "printer-organization", IPP_TAG_TEXT)) != NULL)
10614 {
10615 cupsdSetString(&printer->organization, attr->values[0].string.text);
10616 changed = 1;
10617 }
10618
10619 if ((attr = ippFindAttribute(con->request, "printer-organizational-unit", IPP_TAG_TEXT)) != NULL)
10620 {
10621 cupsdSetString(&printer->organizational_unit, attr->values[0].string.text);
10622 changed = 1;
10623 }
10624
10625 if ((attr = ippFindAttribute(con->request, "printer-info",
10626 IPP_TAG_TEXT)) != NULL)
10627 {
10628 cupsdSetString(&printer->info, attr->values[0].string.text);
10629 changed = 1;
10630 }
10631
10632 /*
10633 * Update the printer attributes and return...
10634 */
10635
10636 if (changed)
10637 {
10638 printer->config_time = time(NULL);
10639
10640 cupsdSetPrinterAttrs(printer);
10641 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
10642
10643 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL,
10644 "Printer \"%s\" description or location changed by \"%s\".",
10645 printer->name, get_username(con));
10646
10647 cupsdLogMessage(CUPSD_LOG_INFO,
10648 "Printer \"%s\" description or location changed by \"%s\".",
10649 printer->name, get_username(con));
10650 }
10651
10652 con->response->request.status.status_code = IPP_OK;
10653 }
10654
10655
10656 /*
10657 * 'set_printer_defaults()' - Set printer default options from a request.
10658 */
10659
10660 static int /* O - 1 on success, 0 on failure */
10661 set_printer_defaults(
10662 cupsd_client_t *con, /* I - Client connection */
10663 cupsd_printer_t *printer) /* I - Printer */
10664 {
10665 int i; /* Looping var */
10666 ipp_attribute_t *attr; /* Current attribute */
10667 size_t namelen; /* Length of attribute name */
10668 char name[256], /* New attribute name */
10669 value[256]; /* String version of integer attrs */
10670
10671
10672 for (attr = con->request->attrs; attr; attr = attr->next)
10673 {
10674 /*
10675 * Skip non-printer attributes...
10676 */
10677
10678 if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
10679 continue;
10680
10681 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name);
10682
10683 if (!strcmp(attr->name, "job-sheets-default"))
10684 {
10685 /*
10686 * Only allow keywords and names...
10687 */
10688
10689 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10690 continue;
10691
10692 /*
10693 * Only allow job-sheets-default to be set when running without a
10694 * system high classification level...
10695 */
10696
10697 if (Classification)
10698 continue;
10699
10700 cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text);
10701
10702 if (attr->num_values > 1)
10703 cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text);
10704 else
10705 cupsdSetString(&printer->job_sheets[1], "none");
10706 }
10707 else if (!strcmp(attr->name, "requesting-user-name-allowed"))
10708 {
10709 cupsdFreeStrings(&(printer->users));
10710
10711 printer->deny_users = 0;
10712
10713 if (attr->value_tag == IPP_TAG_NAME &&
10714 (attr->num_values > 1 ||
10715 strcmp(attr->values[0].string.text, "all")))
10716 {
10717 for (i = 0; i < attr->num_values; i ++)
10718 cupsdAddString(&(printer->users), attr->values[i].string.text);
10719 }
10720 }
10721 else if (!strcmp(attr->name, "requesting-user-name-denied"))
10722 {
10723 cupsdFreeStrings(&(printer->users));
10724
10725 printer->deny_users = 1;
10726
10727 if (attr->value_tag == IPP_TAG_NAME &&
10728 (attr->num_values > 1 ||
10729 strcmp(attr->values[0].string.text, "none")))
10730 {
10731 for (i = 0; i < attr->num_values; i ++)
10732 cupsdAddString(&(printer->users), attr->values[i].string.text);
10733 }
10734 }
10735 else if (!strcmp(attr->name, "job-quota-period"))
10736 {
10737 if (attr->value_tag != IPP_TAG_INTEGER)
10738 continue;
10739
10740 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...",
10741 attr->values[0].integer);
10742 cupsdFreeQuotas(printer);
10743
10744 printer->quota_period = attr->values[0].integer;
10745 }
10746 else if (!strcmp(attr->name, "job-k-limit"))
10747 {
10748 if (attr->value_tag != IPP_TAG_INTEGER)
10749 continue;
10750
10751 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...",
10752 attr->values[0].integer);
10753 cupsdFreeQuotas(printer);
10754
10755 printer->k_limit = attr->values[0].integer;
10756 }
10757 else if (!strcmp(attr->name, "job-page-limit"))
10758 {
10759 if (attr->value_tag != IPP_TAG_INTEGER)
10760 continue;
10761
10762 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...",
10763 attr->values[0].integer);
10764 cupsdFreeQuotas(printer);
10765
10766 printer->page_limit = attr->values[0].integer;
10767 }
10768 else if (!strcmp(attr->name, "printer-op-policy"))
10769 {
10770 cupsd_policy_t *p; /* Policy */
10771
10772
10773 if (attr->value_tag != IPP_TAG_NAME)
10774 continue;
10775
10776 if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
10777 {
10778 cupsdLogMessage(CUPSD_LOG_DEBUG,
10779 "Setting printer-op-policy to \"%s\"...",
10780 attr->values[0].string.text);
10781 cupsdSetString(&printer->op_policy, attr->values[0].string.text);
10782 printer->op_policy_ptr = p;
10783 }
10784 else
10785 {
10786 send_ipp_status(con, IPP_NOT_POSSIBLE,
10787 _("Unknown printer-op-policy \"%s\"."),
10788 attr->values[0].string.text);
10789 return (0);
10790 }
10791 }
10792 else if (!strcmp(attr->name, "printer-error-policy"))
10793 {
10794 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10795 continue;
10796
10797 if (strcmp(attr->values[0].string.text, "retry-current-job") &&
10798 ((printer->type & CUPS_PRINTER_CLASS) ||
10799 (strcmp(attr->values[0].string.text, "abort-job") &&
10800 strcmp(attr->values[0].string.text, "retry-job") &&
10801 strcmp(attr->values[0].string.text, "stop-printer"))))
10802 {
10803 send_ipp_status(con, IPP_NOT_POSSIBLE,
10804 _("Unknown printer-error-policy \"%s\"."),
10805 attr->values[0].string.text);
10806 return (0);
10807 }
10808
10809 cupsdLogMessage(CUPSD_LOG_DEBUG,
10810 "Setting printer-error-policy to \"%s\"...",
10811 attr->values[0].string.text);
10812 cupsdSetString(&printer->error_policy, attr->values[0].string.text);
10813 }
10814
10815 /*
10816 * Skip any other non-default attributes...
10817 */
10818
10819 namelen = strlen(attr->name);
10820 if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") ||
10821 namelen > (sizeof(name) - 1) || attr->num_values != 1)
10822 continue;
10823
10824 /*
10825 * OK, anything else must be a user-defined default...
10826 */
10827
10828 strlcpy(name, attr->name, sizeof(name));
10829 name[namelen - 8] = '\0'; /* Strip "-default" */
10830
10831 switch (attr->value_tag)
10832 {
10833 case IPP_TAG_DELETEATTR :
10834 printer->num_options = cupsRemoveOption(name,
10835 printer->num_options,
10836 &(printer->options));
10837 cupsdLogMessage(CUPSD_LOG_DEBUG,
10838 "Deleting %s", attr->name);
10839 break;
10840
10841 case IPP_TAG_NAME :
10842 case IPP_TAG_TEXT :
10843 case IPP_TAG_KEYWORD :
10844 case IPP_TAG_URI :
10845 printer->num_options = cupsAddOption(name,
10846 attr->values[0].string.text,
10847 printer->num_options,
10848 &(printer->options));
10849 cupsdLogMessage(CUPSD_LOG_DEBUG,
10850 "Setting %s to \"%s\"...", attr->name,
10851 attr->values[0].string.text);
10852 break;
10853
10854 case IPP_TAG_BOOLEAN :
10855 printer->num_options = cupsAddOption(name,
10856 attr->values[0].boolean ?
10857 "true" : "false",
10858 printer->num_options,
10859 &(printer->options));
10860 cupsdLogMessage(CUPSD_LOG_DEBUG,
10861 "Setting %s to %s...", attr->name,
10862 attr->values[0].boolean ? "true" : "false");
10863 break;
10864
10865 case IPP_TAG_INTEGER :
10866 case IPP_TAG_ENUM :
10867 sprintf(value, "%d", attr->values[0].integer);
10868 printer->num_options = cupsAddOption(name, value,
10869 printer->num_options,
10870 &(printer->options));
10871 cupsdLogMessage(CUPSD_LOG_DEBUG,
10872 "Setting %s to %s...", attr->name, value);
10873 break;
10874
10875 case IPP_TAG_RANGE :
10876 sprintf(value, "%d-%d", attr->values[0].range.lower,
10877 attr->values[0].range.upper);
10878 printer->num_options = cupsAddOption(name, value,
10879 printer->num_options,
10880 &(printer->options));
10881 cupsdLogMessage(CUPSD_LOG_DEBUG,
10882 "Setting %s to %s...", attr->name, value);
10883 break;
10884
10885 case IPP_TAG_RESOLUTION :
10886 sprintf(value, "%dx%d%s", attr->values[0].resolution.xres,
10887 attr->values[0].resolution.yres,
10888 attr->values[0].resolution.units == IPP_RES_PER_INCH ?
10889 "dpi" : "dpcm");
10890 printer->num_options = cupsAddOption(name, value,
10891 printer->num_options,
10892 &(printer->options));
10893 cupsdLogMessage(CUPSD_LOG_DEBUG,
10894 "Setting %s to %s...", attr->name, value);
10895 break;
10896
10897 default :
10898 /* Do nothing for other values */
10899 break;
10900 }
10901 }
10902
10903 return (1);
10904 }
10905
10906
10907 /*
10908 * 'start_printer()' - Start a printer.
10909 */
10910
10911 static void
10912 start_printer(cupsd_client_t *con, /* I - Client connection */
10913 ipp_attribute_t *uri) /* I - Printer URI */
10914 {
10915 int i; /* Temporary variable */
10916 http_status_t status; /* Policy status */
10917 cups_ptype_t dtype; /* Destination type (printer/class) */
10918 cupsd_printer_t *printer; /* Printer data */
10919
10920
10921 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con,
10922 con->number, uri->values[0].string.text);
10923
10924 /*
10925 * Is the destination valid?
10926 */
10927
10928 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10929 {
10930 /*
10931 * Bad URI...
10932 */
10933
10934 send_ipp_status(con, IPP_NOT_FOUND,
10935 _("The printer or class does not exist."));
10936 return;
10937 }
10938
10939 /*
10940 * Check policy...
10941 */
10942
10943 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10944 {
10945 send_http_error(con, status, printer);
10946 return;
10947 }
10948
10949 /*
10950 * Start the printer...
10951 */
10952
10953 printer->state_message[0] = '\0';
10954
10955 cupsdStartPrinter(printer, 1);
10956
10957 if (dtype & CUPS_PRINTER_CLASS)
10958 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".",
10959 printer->name, get_username(con));
10960 else
10961 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".",
10962 printer->name, get_username(con));
10963
10964 cupsdCheckJobs();
10965
10966 /*
10967 * Check quotas...
10968 */
10969
10970 if ((i = check_quotas(con, printer)) < 0)
10971 {
10972 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
10973 return;
10974 }
10975 else if (i == 0)
10976 {
10977 send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print."));
10978 return;
10979 }
10980
10981 /*
10982 * Everything was ok, so return OK status...
10983 */
10984
10985 con->response->request.status.status_code = IPP_OK;
10986 }
10987
10988
10989 /*
10990 * 'stop_printer()' - Stop a printer.
10991 */
10992
10993 static void
10994 stop_printer(cupsd_client_t *con, /* I - Client connection */
10995 ipp_attribute_t *uri) /* I - Printer URI */
10996 {
10997 http_status_t status; /* Policy status */
10998 cups_ptype_t dtype; /* Destination type (printer/class) */
10999 cupsd_printer_t *printer; /* Printer data */
11000 ipp_attribute_t *attr; /* printer-state-message attribute */
11001
11002
11003 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con,
11004 con->number, uri->values[0].string.text);
11005
11006 /*
11007 * Is the destination valid?
11008 */
11009
11010 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
11011 {
11012 /*
11013 * Bad URI...
11014 */
11015
11016 send_ipp_status(con, IPP_NOT_FOUND,
11017 _("The printer or class does not exist."));
11018 return;
11019 }
11020
11021 /*
11022 * Check policy...
11023 */
11024
11025 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
11026 {
11027 send_http_error(con, status, printer);
11028 return;
11029 }
11030
11031 /*
11032 * Stop the printer...
11033 */
11034
11035 if ((attr = ippFindAttribute(con->request, "printer-state-message",
11036 IPP_TAG_TEXT)) == NULL)
11037 strlcpy(printer->state_message, "Paused", sizeof(printer->state_message));
11038 else
11039 {
11040 strlcpy(printer->state_message, attr->values[0].string.text,
11041 sizeof(printer->state_message));
11042 }
11043
11044 cupsdStopPrinter(printer, 1);
11045
11046 if (dtype & CUPS_PRINTER_CLASS)
11047 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".",
11048 printer->name, get_username(con));
11049 else
11050 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".",
11051 printer->name, get_username(con));
11052
11053 /*
11054 * Everything was ok, so return OK status...
11055 */
11056
11057 con->response->request.status.status_code = IPP_OK;
11058 }
11059
11060
11061 /*
11062 * 'url_encode_attr()' - URL-encode a string attribute.
11063 */
11064
11065 static void
11066 url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */
11067 char *buffer,/* I - String buffer */
11068 size_t bufsize)/* I - Size of buffer */
11069 {
11070 int i; /* Looping var */
11071 char *bufptr, /* Pointer into buffer */
11072 *bufend; /* End of buffer */
11073
11074
11075 strlcpy(buffer, attr->name, bufsize);
11076 bufptr = buffer + strlen(buffer);
11077 bufend = buffer + bufsize - 1;
11078
11079 for (i = 0; i < attr->num_values; i ++)
11080 {
11081 if (bufptr >= bufend)
11082 break;
11083
11084 if (i)
11085 *bufptr++ = ',';
11086 else
11087 *bufptr++ = '=';
11088
11089 if (bufptr >= bufend)
11090 break;
11091
11092 *bufptr++ = '\'';
11093
11094 bufptr = url_encode_string(attr->values[i].string.text, bufptr, (size_t)(bufend - bufptr + 1));
11095
11096 if (bufptr >= bufend)
11097 break;
11098
11099 *bufptr++ = '\'';
11100 }
11101
11102 *bufptr = '\0';
11103 }
11104
11105
11106 /*
11107 * 'url_encode_string()' - URL-encode a string.
11108 */
11109
11110 static char * /* O - End of string */
11111 url_encode_string(const char *s, /* I - String */
11112 char *buffer, /* I - String buffer */
11113 size_t bufsize) /* I - Size of buffer */
11114 {
11115 char *bufptr, /* Pointer into buffer */
11116 *bufend; /* End of buffer */
11117 static const char *hex = "0123456789ABCDEF";
11118 /* Hex digits */
11119
11120
11121 bufptr = buffer;
11122 bufend = buffer + bufsize - 1;
11123
11124 while (*s && bufptr < bufend)
11125 {
11126 if (*s == ' ' || *s == '%' || *s == '+')
11127 {
11128 if (bufptr >= (bufend - 2))
11129 break;
11130
11131 *bufptr++ = '%';
11132 *bufptr++ = hex[(*s >> 4) & 15];
11133 *bufptr++ = hex[*s & 15];
11134
11135 s ++;
11136 }
11137 else if (*s == '\'' || *s == '\\')
11138 {
11139 if (bufptr >= (bufend - 1))
11140 break;
11141
11142 *bufptr++ = '\\';
11143 *bufptr++ = *s++;
11144 }
11145 else
11146 *bufptr++ = *s++;
11147 }
11148
11149 *bufptr = '\0';
11150
11151 return (bufptr);
11152 }
11153
11154
11155 /*
11156 * 'user_allowed()' - See if a user is allowed to print to a queue.
11157 */
11158
11159 static int /* O - 0 if not allowed, 1 if allowed */
11160 user_allowed(cupsd_printer_t *p, /* I - Printer or class */
11161 const char *username) /* I - Username */
11162 {
11163 struct passwd *pw; /* User password data */
11164 char baseuser[256], /* Base username */
11165 *baseptr, /* Pointer to "@" in base username */
11166 *name; /* Current user name */
11167
11168
11169 if (cupsArrayCount(p->users) == 0)
11170 return (1);
11171
11172 if (!strcmp(username, "root"))
11173 return (1);
11174
11175 if (strchr(username, '@'))
11176 {
11177 /*
11178 * Strip @REALM for username check...
11179 */
11180
11181 strlcpy(baseuser, username, sizeof(baseuser));
11182
11183 if ((baseptr = strchr(baseuser, '@')) != NULL)
11184 *baseptr = '\0';
11185
11186 username = baseuser;
11187 }
11188
11189 pw = getpwnam(username);
11190 endpwent();
11191
11192 for (name = (char *)cupsArrayFirst(p->users);
11193 name;
11194 name = (char *)cupsArrayNext(p->users))
11195 {
11196 if (name[0] == '@')
11197 {
11198 /*
11199 * Check group membership...
11200 */
11201
11202 if (cupsdCheckGroup(username, pw, name + 1))
11203 break;
11204 }
11205 else if (name[0] == '#')
11206 {
11207 /*
11208 * Check UUID...
11209 */
11210
11211 if (cupsdCheckGroup(username, pw, name))
11212 break;
11213 }
11214 else if (!_cups_strcasecmp(username, name))
11215 break;
11216 }
11217
11218 return ((name != NULL) != p->deny_users);
11219 }
11220
11221
11222 /*
11223 * 'validate_job()' - Validate printer options and destination.
11224 */
11225
11226 static void
11227 validate_job(cupsd_client_t *con, /* I - Client connection */
11228 ipp_attribute_t *uri) /* I - Printer URI */
11229 {
11230 http_status_t status; /* Policy status */
11231 ipp_attribute_t *attr; /* Current attribute */
11232 #ifdef HAVE_SSL
11233 ipp_attribute_t *auth_info; /* auth-info attribute */
11234 #endif /* HAVE_SSL */
11235 ipp_attribute_t *format, /* Document-format attribute */
11236 *name; /* Job-name attribute */
11237 cups_ptype_t dtype; /* Destination type (printer/class) */
11238 char super[MIME_MAX_SUPER],
11239 /* Supertype of file */
11240 type[MIME_MAX_TYPE];
11241 /* Subtype of file */
11242 cupsd_printer_t *printer; /* Printer */
11243
11244
11245 cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con,
11246 con->number, uri->values[0].string.text);
11247
11248 /*
11249 * OK, see if the client is sending the document compressed - CUPS
11250 * doesn't support compression yet...
11251 */
11252
11253 if ((attr = ippFindAttribute(con->request, "compression",
11254 IPP_TAG_KEYWORD)) != NULL)
11255 {
11256 if (strcmp(attr->values[0].string.text, "none")
11257 #ifdef HAVE_LIBZ
11258 && strcmp(attr->values[0].string.text, "gzip")
11259 #endif /* HAVE_LIBZ */
11260 )
11261 {
11262 send_ipp_status(con, IPP_ATTRIBUTES,
11263 _("Unsupported 'compression' value \"%s\"."),
11264 attr->values[0].string.text);
11265 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
11266 "compression", NULL, attr->values[0].string.text);
11267 return;
11268 }
11269 }
11270
11271 /*
11272 * Is it a format we support?
11273 */
11274
11275 if ((format = ippFindAttribute(con->request, "document-format",
11276 IPP_TAG_MIMETYPE)) != NULL)
11277 {
11278 if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]",
11279 super, type) != 2)
11280 {
11281 send_ipp_status(con, IPP_BAD_REQUEST,
11282 _("Bad 'document-format' value \"%s\"."),
11283 format->values[0].string.text);
11284 return;
11285 }
11286
11287 if ((strcmp(super, "application") || strcmp(type, "octet-stream")) &&
11288 !mimeType(MimeDatabase, super, type))
11289 {
11290 cupsdLogMessage(CUPSD_LOG_INFO,
11291 "Hint: Do you have the raw file printing rules enabled?");
11292 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
11293 _("Unsupported 'document-format' value \"%s\"."),
11294 format->values[0].string.text);
11295 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
11296 "document-format", NULL, format->values[0].string.text);
11297 return;
11298 }
11299 }
11300
11301 /*
11302 * Is the job-name valid?
11303 */
11304
11305 if ((name = ippFindAttribute(con->request, "job-name", IPP_TAG_ZERO)) != NULL)
11306 {
11307 int bad_name = 0; /* Is the job-name value bad? */
11308
11309 if ((name->value_tag != IPP_TAG_NAME && name->value_tag != IPP_TAG_NAMELANG) ||
11310 name->num_values != 1)
11311 {
11312 bad_name = 1;
11313 }
11314 else
11315 {
11316 /*
11317 * Validate that job-name conforms to RFC 5198 (Network Unicode) and
11318 * IPP Everywhere requirements for "name" values...
11319 */
11320
11321 const unsigned char *nameptr; /* Pointer into "job-name" attribute */
11322
11323 for (nameptr = (unsigned char *)name->values[0].string.text;
11324 *nameptr;
11325 nameptr ++)
11326 {
11327 if (*nameptr < ' ' && *nameptr != '\t')
11328 break;
11329 else if (*nameptr == 0x7f)
11330 break;
11331 else if ((*nameptr & 0xe0) == 0xc0)
11332 {
11333 if ((nameptr[1] & 0xc0) != 0x80)
11334 break;
11335
11336 nameptr ++;
11337 }
11338 else if ((*nameptr & 0xf0) == 0xe0)
11339 {
11340 if ((nameptr[1] & 0xc0) != 0x80 ||
11341 (nameptr[2] & 0xc0) != 0x80)
11342 break;
11343
11344 nameptr += 2;
11345 }
11346 else if ((*nameptr & 0xf8) == 0xf0)
11347 {
11348 if ((nameptr[1] & 0xc0) != 0x80 ||
11349 (nameptr[2] & 0xc0) != 0x80 ||
11350 (nameptr[3] & 0xc0) != 0x80)
11351 break;
11352
11353 nameptr += 3;
11354 }
11355 else if (*nameptr & 0x80)
11356 break;
11357 }
11358
11359 if (*nameptr)
11360 bad_name = 1;
11361 }
11362
11363 if (bad_name)
11364 {
11365 if (StrictConformance)
11366 {
11367 send_ipp_status(con, IPP_ATTRIBUTES,
11368 _("Unsupported 'job-name' value."));
11369 ippCopyAttribute(con->response, name, 0);
11370 return;
11371 }
11372 else
11373 {
11374 cupsdLogMessage(CUPSD_LOG_WARN,
11375 "Unsupported 'job-name' value, deleting from request.");
11376 ippDeleteAttribute(con->request, name);
11377 }
11378 }
11379 }
11380
11381 /*
11382 * Is the destination valid?
11383 */
11384
11385 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
11386 {
11387 /*
11388 * Bad URI...
11389 */
11390
11391 send_ipp_status(con, IPP_NOT_FOUND,
11392 _("The printer or class does not exist."));
11393 return;
11394 }
11395
11396 /*
11397 * Check policy...
11398 */
11399
11400 #ifdef HAVE_SSL
11401 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
11402 #endif /* HAVE_SSL */
11403
11404 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
11405 {
11406 send_http_error(con, status, printer);
11407 return;
11408 }
11409 else if (printer->num_auth_info_required == 1 &&
11410 !strcmp(printer->auth_info_required[0], "negotiate") &&
11411 !con->username[0])
11412 {
11413 send_http_error(con, HTTP_UNAUTHORIZED, printer);
11414 return;
11415 }
11416 #ifdef HAVE_SSL
11417 else if (auth_info && !con->http->tls &&
11418 !httpAddrLocalhost(con->http->hostaddr))
11419 {
11420 /*
11421 * Require encryption of auth-info over non-local connections...
11422 */
11423
11424 send_http_error(con, HTTP_UPGRADE_REQUIRED, printer);
11425 return;
11426 }
11427 #endif /* HAVE_SSL */
11428
11429 /*
11430 * Everything was ok, so return OK status...
11431 */
11432
11433 con->response->request.status.status_code = IPP_OK;
11434 }
11435
11436
11437 /*
11438 * 'validate_name()' - Make sure the printer name only contains valid chars.
11439 */
11440
11441 static int /* O - 0 if name is no good, 1 if good */
11442 validate_name(const char *name) /* I - Name to check */
11443 {
11444 const char *ptr; /* Pointer into name */
11445
11446
11447 /*
11448 * Scan the whole name...
11449 */
11450
11451 for (ptr = name; *ptr; ptr ++)
11452 if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
11453 return (0);
11454
11455 /*
11456 * All the characters are good; validate the length, too...
11457 */
11458
11459 return ((ptr - name) < 128);
11460 }
11461
11462
11463 /*
11464 * 'validate_user()' - Validate the user for the request.
11465 */
11466
11467 static int /* O - 1 if permitted, 0 otherwise */
11468 validate_user(cupsd_job_t *job, /* I - Job */
11469 cupsd_client_t *con, /* I - Client connection */
11470 const char *owner, /* I - Owner of job/resource */
11471 char *username, /* O - Authenticated username */
11472 size_t userlen) /* I - Length of username */
11473 {
11474 cupsd_printer_t *printer; /* Printer for job */
11475
11476
11477 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);
11478
11479 /*
11480 * Validate input...
11481 */
11482
11483 if (!con || !owner || !username || userlen <= 0)
11484 return (0);
11485
11486 /*
11487 * Get the best authenticated username that is available.
11488 */
11489
11490 strlcpy(username, get_username(con), userlen);
11491
11492 /*
11493 * Check the username against the owner...
11494 */
11495
11496 printer = cupsdFindDest(job->dest);
11497
11498 return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr,
11499 con, owner) == HTTP_OK);
11500 }