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