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