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