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