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