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