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