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