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