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