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