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