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