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