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