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