]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/ipp.c
Generalize the input validation of some kinds of attributes.
[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;
aefefc35 256 if (attr && attr->name && !strcmp(attr->name, "attributes-charset") && (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET)
ef416fc2 257 charset = attr;
258 else
259 charset = NULL;
260
261 if (attr)
262 attr = attr->next;
263
aefefc35 264 if (attr && attr->name && !strcmp(attr->name, "attributes-natural-language") && (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE)
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
aefefc35 282 if ((attr = ippFindAttribute(con->request, "printer-uri", IPP_TAG_URI)) != NULL)
ef416fc2 283 uri = attr;
aefefc35 284 else if ((attr = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI)) != NULL)
ef416fc2 285 uri = attr;
b94498cf 286 else if (con->request->request.op.operation_id == CUPS_GET_PPD)
287 uri = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME);
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);
2036 if (access(notifier, X_OK))
2037 {
2038 send_ipp_status(con, IPP_NOT_POSSIBLE,
2039 _("notify-recipient-uri URI \"%s\" uses unknown "
84315f46 2040 "scheme."), recipient);
3dfe78b3
MS
2041 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2042 "notify-status-code", IPP_URI_SCHEME);
2043 return;
2044 }
2045
2046 if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient))
2047 {
2048 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 2049 _("notify-recipient-uri URI \"%s\" is already used."),
3dfe78b3
MS
2050 recipient);
2051 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2052 "notify-status-code", IPP_ATTRIBUTES);
2053 return;
2054 }
2055 }
b423cd4c 2056 else if (!strcmp(attr->name, "notify-pull-method") &&
2057 attr->value_tag == IPP_TAG_KEYWORD)
3dfe78b3 2058 {
b423cd4c 2059 pullmethod = attr->values[0].string.text;
3dfe78b3
MS
2060
2061 if (strcmp(pullmethod, "ippget"))
2062 {
2063 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 2064 _("Bad notify-pull-method \"%s\"."), pullmethod);
3dfe78b3
MS
2065 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2066 "notify-status-code", IPP_ATTRIBUTES);
2067 return;
2068 }
2069 }
b423cd4c 2070 else if (!strcmp(attr->name, "notify-charset") &&
2071 attr->value_tag == IPP_TAG_CHARSET &&
2072 strcmp(attr->values[0].string.text, "us-ascii") &&
2073 strcmp(attr->values[0].string.text, "utf-8"))
2074 {
2075 send_ipp_status(con, IPP_CHARSET,
84315f46 2076 _("Character set \"%s\" not supported."),
b423cd4c 2077 attr->values[0].string.text);
2078 return;
2079 }
2080 else if (!strcmp(attr->name, "notify-natural-language") &&
2081 (attr->value_tag != IPP_TAG_LANGUAGE ||
2082 strcmp(attr->values[0].string.text, DefaultLanguage)))
2083 {
2084 send_ipp_status(con, IPP_CHARSET,
84315f46 2085 _("Language \"%s\" not supported."),
b423cd4c 2086 attr->values[0].string.text);
2087 return;
2088 }
2089 else if (!strcmp(attr->name, "notify-user-data") &&
2090 attr->value_tag == IPP_TAG_STRING)
2091 {
2092 if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
2093 {
2094 send_ipp_status(con, IPP_REQUEST_VALUE,
2095 _("The notify-user-data value is too large "
84315f46 2096 "(%d > 63 octets)."),
b423cd4c 2097 attr->values[0].unknown.length);
2098 return;
2099 }
ef416fc2 2100
b423cd4c 2101 user_data = attr;
2102 }
2103 else if (!strcmp(attr->name, "notify-events") &&
2104 attr->value_tag == IPP_TAG_KEYWORD)
2105 {
2106 for (i = 0; i < attr->num_values; i ++)
2107 mask |= cupsdEventValue(attr->values[i].string.text);
2108 }
2109 else if (!strcmp(attr->name, "notify-lease-duration"))
2110 {
2111 send_ipp_status(con, IPP_BAD_REQUEST,
2112 _("The notify-lease-duration attribute cannot be "
2113 "used with job subscriptions."));
2114 return;
2115 }
2116 else if (!strcmp(attr->name, "notify-time-interval") &&
2117 attr->value_tag == IPP_TAG_INTEGER)
2118 interval = attr->values[0].integer;
ef416fc2 2119
b423cd4c 2120 attr = attr->next;
2121 }
ef416fc2 2122
b423cd4c 2123 if (!recipient && !pullmethod)
2124 break;
ef416fc2 2125
b423cd4c 2126 if (mask == CUPSD_EVENT_NONE)
2127 mask = CUPSD_EVENT_JOB_COMPLETED;
ef416fc2 2128
52f6f666
MS
2129 if ((sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job,
2130 recipient, 0)) != NULL)
2131 {
2132 sub->interval = interval;
ef416fc2 2133
52f6f666 2134 cupsdSetString(&sub->owner, job->username);
ef416fc2 2135
52f6f666
MS
2136 if (user_data)
2137 {
2138 sub->user_data_len = user_data->values[0].unknown.length;
2139 memcpy(sub->user_data, user_data->values[0].unknown.data,
07623986 2140 (size_t)sub->user_data_len);
52f6f666 2141 }
ef416fc2 2142
52f6f666
MS
2143 ippAddSeparator(con->response);
2144 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
2145 "notify-subscription-id", sub->id);
f8b3a85b
MS
2146
2147 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for job %d",
2148 sub->id, job->id);
ef416fc2 2149 }
2150
b423cd4c 2151 if (attr)
2152 attr = attr->next;
ef416fc2 2153 }
2154
3dfe78b3 2155 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
b423cd4c 2156
ef416fc2 2157 /*
b423cd4c 2158 * Remove all of the subscription attributes from the job request...
1f0275e3
MS
2159 *
2160 * TODO: Optimize this since subscription groups have to come at the
2161 * end of the request...
ef416fc2 2162 */
2163
b423cd4c 2164 for (attr = job->attrs->attrs, prev = NULL; attr; attr = next)
ef416fc2 2165 {
b423cd4c 2166 next = attr->next;
ef416fc2 2167
b423cd4c 2168 if (attr->group_tag == IPP_TAG_SUBSCRIPTION ||
2169 attr->group_tag == IPP_TAG_ZERO)
2170 {
2171 /*
2172 * Free and remove this attribute...
2173 */
ef416fc2 2174
a2326b5b 2175 ippDeleteAttribute(NULL, attr);
ef416fc2 2176
b423cd4c 2177 if (prev)
2178 prev->next = next;
2179 else
2180 job->attrs->attrs = next;
2181 }
2182 else
2183 prev = attr;
ef416fc2 2184 }
2185
b423cd4c 2186 job->attrs->last = prev;
2187 job->attrs->current = prev;
2188}
ef416fc2 2189
ef416fc2 2190
b423cd4c 2191/*
2192 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2193 *
2194 * See RFC 4122 for the definition of UUIDs and the format.
2195 */
2196
2197static void
321d8d57 2198add_job_uuid(cupsd_job_t *job) /* I - Job */
b423cd4c 2199{
82f97232 2200 char uuid[64]; /* job-uuid string */
ef416fc2 2201
ef416fc2 2202
2203 /*
82f97232 2204 * Add a job-uuid attribute if none exists...
ef416fc2 2205 */
2206
82f97232
MS
2207 if (!ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI))
2208 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL,
7855ab56
MS
2209 httpAssembleUUID(ServerName, RemotePort, job->dest, job->id,
2210 uuid, sizeof(uuid)));
ef416fc2 2211}
2212
2213
2214/*
b423cd4c 2215 * 'add_printer()' - Add a printer to the system.
ef416fc2 2216 */
2217
2218static void
b423cd4c 2219add_printer(cupsd_client_t *con, /* I - Client connection */
2220 ipp_attribute_t *uri) /* I - URI of printer */
ef416fc2 2221{
2222 http_status_t status; /* Policy status */
b423cd4c 2223 int i; /* Looping var */
09a101d6 2224 char scheme[HTTP_MAX_URI], /* Method portion of URI */
b423cd4c 2225 username[HTTP_MAX_URI], /* Username portion of URI */
ef416fc2 2226 host[HTTP_MAX_URI], /* Host portion of URI */
2227 resource[HTTP_MAX_URI]; /* Resource portion of URI */
2228 int port; /* Port portion of URI */
b423cd4c 2229 cupsd_printer_t *printer; /* Printer/class */
2230 ipp_attribute_t *attr; /* Printer attribute */
2231 cups_file_t *fp; /* Script/PPD file */
2232 char line[1024]; /* Line from file... */
2233 char srcfile[1024], /* Source Script/PPD file */
2234 dstfile[1024]; /* Destination Script/PPD file */
2235 int modify; /* Non-zero if we are modifying */
e67e2f9e 2236 int changed_driver, /* Changed the PPD? */
b226ab99
MS
2237 need_restart_job, /* Need to restart job? */
2238 set_device_uri, /* Did we set the device URI? */
09a101d6 2239 set_port_monitor; /* Did we set the port monitor? */
ef416fc2 2240
2241
b423cd4c 2242 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con,
996acce8 2243 con->number, uri->values[0].string.text);
ef416fc2 2244
2245 /*
b423cd4c 2246 * Do we have a valid URI?
ef416fc2 2247 */
2248
09a101d6 2249 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
2250 sizeof(scheme), username, sizeof(username), host,
b423cd4c 2251 sizeof(host), &port, resource, sizeof(resource));
2252
2253 if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10)
ef416fc2 2254 {
b423cd4c 2255 /*
2256 * No, return an error...
2257 */
2258
ef416fc2 2259 send_ipp_status(con, IPP_BAD_REQUEST,
b423cd4c 2260 _("The printer-uri must be of the form "
2261 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
ef416fc2 2262 return;
2263 }
2264
2265 /*
b423cd4c 2266 * Do we have a valid printer name?
ef416fc2 2267 */
2268
b423cd4c 2269 if (!validate_name(resource + 10))
ef416fc2 2270 {
b423cd4c 2271 /*
2272 * No, return an error...
2273 */
2274
2275 send_ipp_status(con, IPP_BAD_REQUEST,
2276 _("The printer-uri \"%s\" contains invalid characters."),
2277 uri->values[0].string.text);
2278 return;
ef416fc2 2279 }
ef416fc2 2280
ef416fc2 2281 /*
b423cd4c 2282 * See if the printer already exists; if not, create a new printer...
2283 */
ef416fc2 2284
b423cd4c 2285 if ((printer = cupsdFindPrinter(resource + 10)) == NULL)
ef416fc2 2286 {
2287 /*
b423cd4c 2288 * Printer doesn't exist; see if we have a class of the same name...
ef416fc2 2289 */
2290
a2326b5b 2291 if ((printer = cupsdFindClass(resource + 10)) != NULL)
ef416fc2 2292 {
b423cd4c 2293 /*
2294 * Yes, return an error...
2295 */
ef416fc2 2296
b423cd4c 2297 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 2298 _("A class named \"%s\" already exists."),
b423cd4c 2299 resource + 10);
ef416fc2 2300 return;
2301 }
2302
2303 /*
2e4ff8af 2304 * No, check the default policy then add the printer...
ef416fc2 2305 */
2306
2e4ff8af
MS
2307 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
2308 {
2309 send_http_error(con, status, NULL);
2310 return;
2311 }
2312
b423cd4c 2313 printer = cupsdAddPrinter(resource + 10);
2314 modify = 0;
ef416fc2 2315 }
2e4ff8af
MS
2316 else if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
2317 NULL)) != HTTP_OK)
2318 {
2fb76298 2319 send_http_error(con, status, printer);
2e4ff8af
MS
2320 return;
2321 }
b423cd4c 2322 else
2323 modify = 1;
ef416fc2 2324
b423cd4c 2325 /*
2326 * Look for attributes and copy them over as needed...
2327 */
ef416fc2 2328
b226ab99 2329 changed_driver = 0;
b423cd4c 2330 need_restart_job = 0;
ef416fc2 2331
7ae00c35
MS
2332 if ((attr = ippFindAttribute(con->request, "printer-is-temporary", IPP_TAG_BOOLEAN)) != NULL)
2333 printer->temporary = ippGetBoolean(attr, 0);
2334
b423cd4c 2335 if ((attr = ippFindAttribute(con->request, "printer-location",
2336 IPP_TAG_TEXT)) != NULL)
2337 cupsdSetString(&printer->location, attr->values[0].string.text);
ef416fc2 2338
9b4bd602
MS
2339 if ((attr = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI)) != NULL && !strncmp(attr->values[0].string.text, "geo:", 4))
2340 cupsdSetString(&printer->geo_location, attr->values[0].string.text);
2341
2342 if ((attr = ippFindAttribute(con->request, "printer-organization", IPP_TAG_TEXT)) != NULL)
2343 cupsdSetString(&printer->organization, attr->values[0].string.text);
2344
2345 if ((attr = ippFindAttribute(con->request, "printer-organizational-unit", IPP_TAG_TEXT)) != NULL)
2346 cupsdSetString(&printer->organizational_unit, attr->values[0].string.text);
2347
b423cd4c 2348 if ((attr = ippFindAttribute(con->request, "printer-info",
2349 IPP_TAG_TEXT)) != NULL)
2350 cupsdSetString(&printer->info, attr->values[0].string.text);
ef416fc2 2351
09a101d6 2352 set_device_uri = 0;
2353
b423cd4c 2354 if ((attr = ippFindAttribute(con->request, "device-uri",
2355 IPP_TAG_URI)) != NULL)
ef416fc2 2356 {
2357 /*
b423cd4c 2358 * Do we have a valid device URI?
ef416fc2 2359 */
2360
0af14961
MS
2361 http_uri_status_t uri_status; /* URI separation status */
2362 char old_device_uri[1024];
2363 /* Old device URI */
2fb76298 2364
b423cd4c 2365 need_restart_job = 1;
ef416fc2 2366
2fb76298
MS
2367 uri_status = httpSeparateURI(HTTP_URI_CODING_ALL,
2368 attr->values[0].string.text,
2369 scheme, sizeof(scheme),
2370 username, sizeof(username),
2371 host, sizeof(host), &port,
2372 resource, sizeof(resource));
2373
f0b589f7 2374 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s device-uri: %s", printer->name, httpURIStatusString(uri_status));
6961465f 2375
2fb76298
MS
2376 if (uri_status < HTTP_URI_OK)
2377 {
84315f46 2378 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"."),
2fb76298 2379 attr->values[0].string.text);
f0b589f7
MS
2380 if (!modify)
2381 cupsdDeletePrinter(printer, 0);
2382
2fb76298
MS
2383 return;
2384 }
b423cd4c 2385
09a101d6 2386 if (!strcmp(scheme, "file"))
ef416fc2 2387 {
2388 /*
b423cd4c 2389 * See if the administrator has enabled file devices...
ef416fc2 2390 */
2391
b423cd4c 2392 if (!FileDevice && strcmp(resource, "/dev/null"))
ef416fc2 2393 {
2394 /*
b423cd4c 2395 * File devices are disabled and the URL is not file:/dev/null...
ef416fc2 2396 */
2397
b423cd4c 2398 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 2399 _("File device URIs have been disabled. "
b423cd4c 2400 "To enable, see the FileDevice directive in "
cb7f98ee 2401 "\"%s/cups-files.conf\"."),
b423cd4c 2402 ServerRoot);
f0b589f7
MS
2403 if (!modify)
2404 cupsdDeletePrinter(printer, 0);
2405
ef416fc2 2406 return;
2407 }
b423cd4c 2408 }
2409 else
2410 {
ef416fc2 2411 /*
b423cd4c 2412 * See if the backend exists and is executable...
ef416fc2 2413 */
2414
09a101d6 2415 snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, scheme);
b423cd4c 2416 if (access(srcfile, X_OK))
ef416fc2 2417 {
2418 /*
b423cd4c 2419 * Could not find device in list!
ef416fc2 2420 */
ef416fc2 2421
84315f46
MS
2422 send_ipp_status(con, IPP_NOT_POSSIBLE,
2423 _("Bad device-uri scheme \"%s\"."), scheme);
f0b589f7
MS
2424 if (!modify)
2425 cupsdDeletePrinter(printer, 0);
2426
b423cd4c 2427 return;
ef416fc2 2428 }
2429 }
ef416fc2 2430
0af14961
MS
2431 if (printer->sanitized_device_uri)
2432 strlcpy(old_device_uri, printer->sanitized_device_uri,
2433 sizeof(old_device_uri));
2434 else
2435 old_device_uri[0] = '\0';
2436
2437 cupsdSetDeviceURI(printer, attr->values[0].string.text);
2438
b423cd4c 2439 cupsdLogMessage(CUPSD_LOG_INFO,
2440 "Setting %s device-uri to \"%s\" (was \"%s\".)",
0af14961
MS
2441 printer->name, printer->sanitized_device_uri,
2442 old_device_uri);
ef416fc2 2443
09a101d6 2444 set_device_uri = 1;
ef416fc2 2445 }
2446
09a101d6 2447 set_port_monitor = 0;
2448
b423cd4c 2449 if ((attr = ippFindAttribute(con->request, "port-monitor",
09a101d6 2450 IPP_TAG_NAME)) != NULL)
ef416fc2 2451 {
b423cd4c 2452 ipp_attribute_t *supported; /* port-monitor-supported attribute */
ef416fc2 2453
ef416fc2 2454
b423cd4c 2455 need_restart_job = 1;
ef416fc2 2456
e4572d57 2457 supported = ippFindAttribute(printer->ppd_attrs, "port-monitor-supported",
a41f09e2 2458 IPP_TAG_NAME);
080811b1
MS
2459 if (supported)
2460 {
2461 for (i = 0; i < supported->num_values; i ++)
2462 if (!strcmp(supported->values[i].string.text,
2463 attr->values[0].string.text))
2464 break;
2465 }
ef416fc2 2466
080811b1 2467 if (!supported || i >= supported->num_values)
b423cd4c 2468 {
84315f46 2469 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"."),
b423cd4c 2470 attr->values[0].string.text);
f0b589f7
MS
2471 if (!modify)
2472 cupsdDeletePrinter(printer, 0);
2473
b423cd4c 2474 return;
2475 }
ef416fc2 2476
b423cd4c 2477 cupsdLogMessage(CUPSD_LOG_INFO,
2478 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2479 printer->name, attr->values[0].string.text,
09a101d6 2480 printer->port_monitor ? printer->port_monitor : "none");
ef416fc2 2481
b423cd4c 2482 if (strcmp(attr->values[0].string.text, "none"))
2483 cupsdSetString(&printer->port_monitor, attr->values[0].string.text);
2484 else
2485 cupsdClearString(&printer->port_monitor);
09a101d6 2486
2487 set_port_monitor = 1;
b423cd4c 2488 }
ef416fc2 2489
b423cd4c 2490 if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs",
f8b3a85b
MS
2491 IPP_TAG_BOOLEAN)) != NULL &&
2492 attr->values[0].boolean != printer->accepting)
b423cd4c 2493 {
2494 cupsdLogMessage(CUPSD_LOG_INFO,
2495 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2496 printer->name, attr->values[0].boolean, printer->accepting);
ef416fc2 2497
b423cd4c 2498 printer->accepting = attr->values[0].boolean;
f8b3a85b
MS
2499
2500 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
2501 "%s accepting jobs.",
2502 printer->accepting ? "Now" : "No longer");
b423cd4c 2503 }
bd7854cb 2504
7ae00c35 2505 if ((attr = ippFindAttribute(con->request, "printer-is-shared", IPP_TAG_BOOLEAN)) != NULL)
b423cd4c 2506 {
7ae00c35 2507 if (ippGetBoolean(attr, 0) &&
07ed0e9a
MS
2508 printer->num_auth_info_required == 1 &&
2509 !strcmp(printer->auth_info_required[0], "negotiate"))
2510 {
2511 send_ipp_status(con, IPP_BAD_REQUEST,
2512 _("Cannot share a remote Kerberized printer."));
f0b589f7
MS
2513 if (!modify)
2514 cupsdDeletePrinter(printer, 0);
2515
07ed0e9a
MS
2516 return;
2517 }
2518
7dc6f91d
MS
2519 if (printer->type & CUPS_PRINTER_REMOTE)
2520 {
2521 /*
2522 * Cannot re-share remote printers.
2523 */
2524
2525 send_ipp_status(con, IPP_BAD_REQUEST, _("Cannot change printer-is-shared for remote queues."));
f0b589f7
MS
2526 if (!modify)
2527 cupsdDeletePrinter(printer, 0);
2528
7dc6f91d
MS
2529 return;
2530 }
2531
7ae00c35 2532 if (printer->shared && !ippGetBoolean(attr, 0))
f7deaa1a 2533 cupsdDeregisterPrinter(printer, 1);
bd7854cb 2534
b423cd4c 2535 cupsdLogMessage(CUPSD_LOG_INFO,
2536 "Setting %s printer-is-shared to %d (was %d.)",
2537 printer->name, attr->values[0].boolean, printer->shared);
bd7854cb 2538
7ae00c35
MS
2539 printer->shared = ippGetBoolean(attr, 0);
2540 if (printer->shared && printer->temporary)
2541 printer->temporary = 0;
b423cd4c 2542 }
bd7854cb 2543
b423cd4c 2544 if ((attr = ippFindAttribute(con->request, "printer-state",
2545 IPP_TAG_ENUM)) != NULL)
bd7854cb 2546 {
b423cd4c 2547 if (attr->values[0].integer != IPP_PRINTER_IDLE &&
2548 attr->values[0].integer != IPP_PRINTER_STOPPED)
2549 {
84315f46 2550 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d."),
b423cd4c 2551 attr->values[0].integer);
f0b589f7
MS
2552 if (!modify)
2553 cupsdDeletePrinter(printer, 0);
2554
b423cd4c 2555 return;
2556 }
bd7854cb 2557
bc44d920 2558 cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
2559 printer->name, attr->values[0].integer, printer->state);
b423cd4c 2560
2561 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
2562 cupsdStopPrinter(printer, 0);
2563 else
2564 {
2565 need_restart_job = 1;
2566 cupsdSetPrinterState(printer, (ipp_pstate_t)(attr->values[0].integer), 0);
2567 }
2568 }
745129be 2569
b423cd4c 2570 if ((attr = ippFindAttribute(con->request, "printer-state-message",
2571 IPP_TAG_TEXT)) != NULL)
2572 {
2573 strlcpy(printer->state_message, attr->values[0].string.text,
2574 sizeof(printer->state_message));
f8b3a85b
MS
2575
2576 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL, "%s",
2577 printer->state_message);
bd7854cb 2578 }
2579
1f6f3dbc
MS
2580 if ((attr = ippFindAttribute(con->request, "printer-state-reasons",
2581 IPP_TAG_KEYWORD)) != NULL)
2582 {
2583 if (attr->num_values >
2584 (int)(sizeof(printer->reasons) / sizeof(printer->reasons[0])))
2585 {
2586 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 2587 _("Too many printer-state-reasons values (%d > %d)."),
1f6f3dbc
MS
2588 attr->num_values,
2589 (int)(sizeof(printer->reasons) /
2590 sizeof(printer->reasons[0])));
f0b589f7
MS
2591 if (!modify)
2592 cupsdDeletePrinter(printer, 0);
2593
1f6f3dbc
MS
2594 return;
2595 }
2596
2597 for (i = 0; i < printer->num_reasons; i ++)
2598 _cupsStrFree(printer->reasons[i]);
2599
2600 printer->num_reasons = 0;
2601 for (i = 0; i < attr->num_values; i ++)
2602 {
2603 if (!strcmp(attr->values[i].string.text, "none"))
2604 continue;
2605
2606 printer->reasons[printer->num_reasons] =
426c6a59 2607 _cupsStrRetain(attr->values[i].string.text);
745129be 2608 printer->num_reasons ++;
1f6f3dbc 2609
745129be 2610 if (!strcmp(attr->values[i].string.text, "paused") &&
1f6f3dbc
MS
2611 printer->state != IPP_PRINTER_STOPPED)
2612 {
2613 cupsdLogMessage(CUPSD_LOG_INFO,
2614 "Setting %s printer-state to %d (was %d.)",
2615 printer->name, IPP_PRINTER_STOPPED, printer->state);
2616 cupsdStopPrinter(printer, 0);
2617 }
1f6f3dbc
MS
2618 }
2619
2620 if (PrintcapFormat == PRINTCAP_PLIST)
2621 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
f8b3a85b
MS
2622
2623 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
2624 "Printer \"%s\" state changed.", printer->name);
1f6f3dbc
MS
2625 }
2626
f0b589f7
MS
2627 if (!set_printer_defaults(con, printer))
2628 {
2629 if (!modify)
2630 cupsdDeletePrinter(printer, 0);
2631
2632 return;
2633 }
b423cd4c 2634
09a101d6 2635 if ((attr = ippFindAttribute(con->request, "auth-info-required",
2636 IPP_TAG_KEYWORD)) != NULL)
2637 cupsdSetAuthInfoRequired(printer, NULL, attr);
2638
bd7854cb 2639 /*
b423cd4c 2640 * See if we have all required attributes...
bd7854cb 2641 */
2642
b423cd4c 2643 if (!printer->device_uri)
2644 cupsdSetString(&printer->device_uri, "file:///dev/null");
bd7854cb 2645
2646 /*
e67e2f9e 2647 * See if we have a PPD file attached to the request...
bd7854cb 2648 */
2649
b423cd4c 2650 if (con->filename)
2651 {
2652 need_restart_job = 1;
b226ab99 2653 changed_driver = 1;
bd7854cb 2654
b423cd4c 2655 strlcpy(srcfile, con->filename, sizeof(srcfile));
ef416fc2 2656
b423cd4c 2657 if ((fp = cupsFileOpen(srcfile, "rb")))
2658 {
2659 /*
2660 * Yes; get the first line from it...
2661 */
ef416fc2 2662
b423cd4c 2663 line[0] = '\0';
2664 cupsFileGets(fp, line, sizeof(line));
2665 cupsFileClose(fp);
ef416fc2 2666
b423cd4c 2667 /*
2668 * Then see what kind of file it is...
2669 */
ef416fc2 2670
e67e2f9e 2671 if (strncmp(line, "*PPD-Adobe", 10))
b423cd4c 2672 {
e67e2f9e 2673 send_ipp_status(con, IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED, _("Bad PPD file."));
f0b589f7
MS
2674 if (!modify)
2675 cupsdDeletePrinter(printer, 0);
2676
e67e2f9e 2677 return;
b423cd4c 2678 }
ef416fc2 2679
b423cd4c 2680 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2681 printer->name);
ef416fc2 2682
e67e2f9e
MS
2683 /*
2684 * The new file is a PPD file, so move the file over to the ppd
2685 * directory...
2686 */
568fa3fa 2687
e67e2f9e 2688 if (copy_file(srcfile, dstfile, ConfigFilePerm))
b423cd4c 2689 {
e67e2f9e 2690 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file - %s"), strerror(errno));
f0b589f7
MS
2691 if (!modify)
2692 cupsdDeletePrinter(printer, 0);
2693
e67e2f9e 2694 return;
b423cd4c 2695 }
e67e2f9e
MS
2696
2697 cupsdLogMessage(CUPSD_LOG_DEBUG, "Copied PPD file successfully");
b423cd4c 2698 }
2699 }
e67e2f9e 2700 else if ((attr = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME)) != NULL)
ef416fc2 2701 {
e67e2f9e
MS
2702 const char *ppd_name = ippGetString(attr, 0, NULL);
2703 /* ppd-name value */
2704
b423cd4c 2705 need_restart_job = 1;
b226ab99 2706 changed_driver = 1;
ef416fc2 2707
e67e2f9e 2708 if (!strcmp(ppd_name, "raw"))
ef416fc2 2709 {
b423cd4c 2710 /*
e67e2f9e 2711 * Raw driver, remove any existing PPD file.
b423cd4c 2712 */
ef416fc2 2713
e67e2f9e 2714 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, printer->name);
b423cd4c 2715 unlink(dstfile);
2716 }
e67e2f9e
MS
2717 else if (strstr(ppd_name, "../"))
2718 {
2719 send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Invalid ppd-name value."));
f0b589f7
MS
2720 if (!modify)
2721 cupsdDeletePrinter(printer, 0);
2722
e67e2f9e
MS
2723 return;
2724 }
b423cd4c 2725 else
ef416fc2 2726 {
b423cd4c 2727 /*
2728 * PPD model file...
2729 */
2730
e67e2f9e 2731 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, printer->name);
b423cd4c 2732
e67e2f9e 2733 if (copy_model(con, ppd_name, dstfile))
b423cd4c 2734 {
84315f46 2735 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file."));
f0b589f7
MS
2736 if (!modify)
2737 cupsdDeletePrinter(printer, 0);
2738
b423cd4c 2739 return;
2740 }
568fa3fa 2741
e67e2f9e 2742 cupsdLogMessage(CUPSD_LOG_DEBUG, "Copied PPD file successfully");
b226ab99
MS
2743 }
2744 }
2745
2746 if (changed_driver)
2747 {
2748 /*
e67e2f9e
MS
2749 * If we changed the PPD, then remove the printer's cache file and clear the
2750 * printer-state-reasons...
b226ab99
MS
2751 */
2752
2753 char cache_name[1024]; /* Cache filename for printer attrs */
2754
f0b589f7 2755 snprintf(cache_name, sizeof(cache_name), "%s/%s.data", CacheDir, printer->name);
54afec33
MS
2756 unlink(cache_name);
2757
c7017ecc
MS
2758 cupsdSetPrinterReasons(printer, "none");
2759
b226ab99
MS
2760 /*
2761 * (Re)register color profiles...
2762 */
568fa3fa 2763
a29fd7dd 2764 cupsdRegisterColor(printer);
ef416fc2 2765 }
2766
09a101d6 2767 /*
2768 * If we set the device URI but not the port monitor, check which port
2769 * monitor to use by default...
2770 */
2771
2772 if (set_device_uri && !set_port_monitor)
2773 {
2774 ppd_file_t *ppd; /* PPD file */
2775 ppd_attr_t *ppdattr; /* cupsPortMonitor attribute */
2776
2777
2778 httpSeparateURI(HTTP_URI_CODING_ALL, printer->device_uri, scheme,
2779 sizeof(scheme), username, sizeof(username), host,
2780 sizeof(host), &port, resource, sizeof(resource));
2781
2782 snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot,
2783 printer->name);
9c80ffa2 2784 if ((ppd = _ppdOpenFile(srcfile, _PPD_LOCALIZATION_NONE)) != NULL)
09a101d6 2785 {
2786 for (ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2787 ppdattr;
2788 ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
2789 if (!strcmp(scheme, ppdattr->spec))
2790 {
2791 cupsdLogMessage(CUPSD_LOG_INFO,
2792 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2793 printer->name, ppdattr->value,
bc44d920 2794 printer->port_monitor ? printer->port_monitor
2795 : "none");
09a101d6 2796
2797 if (strcmp(ppdattr->value, "none"))
2798 cupsdSetString(&printer->port_monitor, ppdattr->value);
2799 else
2800 cupsdClearString(&printer->port_monitor);
2801
2802 break;
2803 }
2804
2805 ppdClose(ppd);
2806 }
2807 }
2808
9b4bd602
MS
2809 printer->config_time = time(NULL);
2810
ef416fc2 2811 /*
b423cd4c 2812 * Update the printer attributes and return...
ef416fc2 2813 */
2814
b423cd4c 2815 cupsdSetPrinterAttrs(printer);
7ae00c35
MS
2816 if (!printer->temporary)
2817 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
ef416fc2 2818
b423cd4c 2819 if (need_restart_job && printer->job)
ef416fc2 2820 {
b423cd4c 2821 /*
b9faaae1 2822 * Restart the current job...
b423cd4c 2823 */
ef416fc2 2824
b9faaae1
MS
2825 cupsdSetJobState(printer->job, IPP_JOB_PENDING, CUPSD_JOB_FORCE,
2826 "Job restarted because the printer was modified.");
ef416fc2 2827 }
2828
3dfe78b3 2829 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
b423cd4c 2830
2831 if (modify)
ef416fc2 2832 {
c7017ecc 2833 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED,
f8b3a85b
MS
2834 printer, NULL, "Printer \"%s\" modified by \"%s\".",
2835 printer->name, get_username(con));
ef416fc2 2836
b423cd4c 2837 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".",
2838 printer->name, get_username(con));
ef416fc2 2839 }
b423cd4c 2840 else
2841 {
c7017ecc 2842 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED,
f8b3a85b
MS
2843 printer, NULL, "New printer \"%s\" added by \"%s\".",
2844 printer->name, get_username(con));
ef416fc2 2845
b423cd4c 2846 cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".",
2847 printer->name, get_username(con));
2848 }
2849
2850 con->response->request.status.status_code = IPP_OK;
ef416fc2 2851}
2852
2853
2854/*
b423cd4c 2855 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2856 * based upon the printer state...
ef416fc2 2857 */
2858
b423cd4c 2859static void
2860add_printer_state_reasons(
2861 cupsd_client_t *con, /* I - Client connection */
2862 cupsd_printer_t *p) /* I - Printer info */
ef416fc2 2863{
b423cd4c 2864 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2865 "add_printer_state_reasons(%p[%d], %p[%s])",
996acce8 2866 con, con->number, p, p->name);
ef416fc2 2867
b423cd4c 2868 if (p->num_reasons == 0)
2869 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
745129be 2870 "printer-state-reasons", NULL, "none");
b423cd4c 2871 else
2872 ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2873 "printer-state-reasons", p->num_reasons, NULL,
2874 (const char * const *)p->reasons);
2875}
ef416fc2 2876
ef416fc2 2877
b423cd4c 2878/*
2879 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2880 * the specified printer or class.
2881 */
ef416fc2 2882
b423cd4c 2883static void
2884add_queued_job_count(
2885 cupsd_client_t *con, /* I - Client connection */
2886 cupsd_printer_t *p) /* I - Printer or class */
2887{
2888 int count; /* Number of jobs on destination */
ef416fc2 2889
ef416fc2 2890
b423cd4c 2891 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])",
996acce8 2892 con, con->number, p, p->name);
ef416fc2 2893
b423cd4c 2894 count = cupsdGetPrinterJobCount(p->name);
ef416fc2 2895
b423cd4c 2896 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2897 "queued-job-count", count);
2898}
ef416fc2 2899
ef416fc2 2900
b423cd4c 2901/*
2902 * 'apply_printer_defaults()' - Apply printer default options to a job.
2903 */
ef416fc2 2904
b423cd4c 2905static void
2906apply_printer_defaults(
2907 cupsd_printer_t *printer, /* I - Printer */
2908 cupsd_job_t *job) /* I - Job */
2909{
2910 int i, /* Looping var */
2911 num_options; /* Number of default options */
2912 cups_option_t *options, /* Default options */
f7deaa1a 2913 *option; /* Current option */
ef416fc2 2914
ef416fc2 2915
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")))
4812 ippAddString(con->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_NAME), "job-name", NULL, job->name);
4813
4814 if (job->username && (!ra || cupsArrayFind(ra, "job-originating-user-name")))
4815 ippAddString(con->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_NAME), "job-originating-user-name", NULL, job->username);
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)
f11a948a
MS
4960 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
4961 "printer-error-policy-supported", NULL, "retry-current-job");
4962 else
4963 ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
4964 "printer-error-policy-supported",
4965 sizeof(errors) / sizeof(errors[0]), NULL, errors);
4966 }
4967
7cf5915e
MS
4968 if (!ra || cupsArrayFind(ra, "printer-icons"))
4969 {
4970 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_icons, sizeof(printer_icons),
f2534050 4971 "http", NULL, con->clientname, con->clientport,
7cf5915e
MS
4972 "/icons/%s.png", printer->name);
4973 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-icons",
4974 NULL, printer_icons);
4975 cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-icons=\"%s\"", printer_icons);
4976 }
4977
b423cd4c 4978 if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs"))
7e86f2f6 4979 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", (char)printer->accepting);
ef416fc2 4980
b423cd4c 4981 if (!ra || cupsArrayFind(ra, "printer-is-shared"))
7e86f2f6 4982 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared", (char)printer->shared);
ef416fc2 4983
7ae00c35
MS
4984 if (!ra || cupsArrayFind(ra, "printer-is-temporary"))
4985 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-temporary", (char)printer->temporary);
4986
a2326b5b 4987 if (!ra || cupsArrayFind(ra, "printer-more-info"))
58dc1933
MS
4988 {
4989 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
f2534050 4990 "http", NULL, con->clientname, con->clientport,
58dc1933
MS
4991 (printer->type & CUPS_PRINTER_CLASS) ?
4992 "/classes/%s" : "/printers/%s", printer->name);
4993 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
4994 "printer-more-info", NULL, printer_uri);
4995 }
4996
b423cd4c 4997 if (!ra || cupsArrayFind(ra, "printer-op-policy"))
4998 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
4999 "printer-op-policy", NULL, printer->op_policy);
5000
5001 if (!ra || cupsArrayFind(ra, "printer-state"))
5002 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
5003 printer->state);
5004
9b4bd602
MS
5005 if (!ra || cupsArrayFind(ra, "printer-state-change-date-time"))
5006 ippAddDate(con->response, IPP_TAG_PRINTER, "printer-state-change-date-time", ippTimeToDate(printer->state_time));
5007
b423cd4c 5008 if (!ra || cupsArrayFind(ra, "printer-state-change-time"))
5009 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5010 "printer-state-change-time", printer->state_time);
f7deaa1a 5011
b423cd4c 5012 if (!ra || cupsArrayFind(ra, "printer-state-message"))
5013 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
5014 "printer-state-message", NULL, printer->state_message);
ef416fc2 5015
b423cd4c 5016 if (!ra || cupsArrayFind(ra, "printer-state-reasons"))
5017 add_printer_state_reasons(con, printer);
ef416fc2 5018
b423cd4c 5019 if (!ra || cupsArrayFind(ra, "printer-type"))
5020 {
7e86f2f6 5021 cups_ptype_t type; /* printer-type value */
ef416fc2 5022
ef416fc2 5023 /*
b423cd4c 5024 * Add the CUPS-specific printer-type attribute...
ef416fc2 5025 */
5026
09a101d6 5027 type = printer->type;
b423cd4c 5028
5029 if (printer == DefaultPrinter)
5030 type |= CUPS_PRINTER_DEFAULT;
5031
5032 if (!printer->accepting)
5033 type |= CUPS_PRINTER_REJECTING;
5034
5035 if (!printer->shared)
5036 type |= CUPS_PRINTER_NOT_SHARED;
5037
7e86f2f6 5038 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type", (int)type);
ef416fc2 5039 }
ef416fc2 5040
b423cd4c 5041 if (!ra || cupsArrayFind(ra, "printer-up-time"))
5042 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5043 "printer-up-time", curtime);
ef416fc2 5044
a2326b5b 5045 if (!ra || cupsArrayFind(ra, "printer-uri-supported"))
ef416fc2 5046 {
b423cd4c 5047 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
f2534050 5048 "ipp", NULL, con->clientname, con->clientport,
8ca02f3c 5049 (printer->type & CUPS_PRINTER_CLASS) ?
5050 "/classes/%s" : "/printers/%s", printer->name);
b423cd4c 5051 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
5052 "printer-uri-supported", NULL, printer_uri);
5053 cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"",
5054 printer_uri);
5055 }
ef416fc2 5056
b423cd4c 5057 if (!ra || cupsArrayFind(ra, "queued-job-count"))
5058 add_queued_job_count(con, printer);
ef416fc2 5059
10d09e33 5060 copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0, NULL);
61cf44e2 5061 if (printer->ppd_attrs)
10d09e33
MS
5062 copy_attrs(con->response, printer->ppd_attrs, ra, IPP_TAG_ZERO, 0, NULL);
5063 copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY, NULL);
8e47ac01
MS
5064
5065 _cupsRWUnlock(&printer->lock);
b423cd4c 5066}
ef416fc2 5067
ef416fc2 5068
b423cd4c 5069/*
5070 * 'copy_subscription_attrs()' - Copy subscription attributes.
5071 */
ef416fc2 5072
b423cd4c 5073static void
5074copy_subscription_attrs(
5075 cupsd_client_t *con, /* I - Client connection */
5076 cupsd_subscription_t *sub, /* I - Subscription */
10d09e33
MS
5077 cups_array_t *ra, /* I - Requested attributes array */
5078 cups_array_t *exclude) /* I - Private attributes array */
b423cd4c 5079{
5080 ipp_attribute_t *attr; /* Current attribute */
5081 char printer_uri[HTTP_MAX_URI];
5082 /* Printer URI */
5083 int count; /* Number of events */
5084 unsigned mask; /* Current event mask */
5085 const char *name; /* Current event name */
ef416fc2 5086
ef416fc2 5087
10d09e33
MS
5088 cupsdLogMessage(CUPSD_LOG_DEBUG2,
5089 "copy_subscription_attrs(con=%p, sub=%p, ra=%p, exclude=%p)",
5090 con, sub, ra, exclude);
5091
b423cd4c 5092 /*
5093 * Copy the subscription attributes to the response using the
5094 * requested-attributes attribute that may be provided by the client.
5095 */
ef416fc2 5096
10d09e33 5097 if (!exclude || !cupsArrayFind(exclude, "all"))
b423cd4c 5098 {
10d09e33
MS
5099 if ((!exclude || !cupsArrayFind(exclude, "notify-events")) &&
5100 (!ra || cupsArrayFind(ra, "notify-events")))
b423cd4c 5101 {
10d09e33 5102 cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_subscription_attrs: notify-events");
ef416fc2 5103
10d09e33
MS
5104 if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL)
5105 {
5106 /*
5107 * Simple event list...
5108 */
5109
5110 ippAddString(con->response, IPP_TAG_SUBSCRIPTION,
5111 (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
5112 "notify-events", NULL, name);
5113 }
5114 else
5115 {
5116 /*
5117 * Complex event list...
5118 */
ef416fc2 5119
10d09e33
MS
5120 for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
5121 if (sub->mask & mask)
5122 count ++;
ef416fc2 5123
10d09e33
MS
5124 attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION,
5125 (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
5126 "notify-events", count, NULL, NULL);
ef416fc2 5127
10d09e33
MS
5128 for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
5129 if (sub->mask & mask)
5130 {
5131 attr->values[count].string.text =
5132 (char *)cupsdEventName((cupsd_eventmask_t)mask);
ef416fc2 5133
10d09e33
MS
5134 count ++;
5135 }
5136 }
ef416fc2 5137 }
10d09e33
MS
5138
5139 if ((!exclude || !cupsArrayFind(exclude, "notify-lease-duration")) &&
5140 (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration"))))
5141 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5142 "notify-lease-duration", sub->lease);
5143
5144 if ((!exclude || !cupsArrayFind(exclude, "notify-recipient-uri")) &&
5145 (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri"))))
5146 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
5147 "notify-recipient-uri", NULL, sub->recipient);
5148 else if ((!exclude || !cupsArrayFind(exclude, "notify-pull-method")) &&
5149 (!ra || cupsArrayFind(ra, "notify-pull-method")))
5150 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
5151 "notify-pull-method", NULL, "ippget");
5152
5153 if ((!exclude || !cupsArrayFind(exclude, "notify-subscriber-user-name")) &&
5154 (!ra || cupsArrayFind(ra, "notify-subscriber-user-name")))
5155 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME,
5156 "notify-subscriber-user-name", NULL, sub->owner);
5157
5158 if ((!exclude || !cupsArrayFind(exclude, "notify-time-interval")) &&
5159 (!ra || cupsArrayFind(ra, "notify-time-interval")))
5160 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5161 "notify-time-interval", sub->interval);
5162
5163 if (sub->user_data_len > 0 &&
5164 (!exclude || !cupsArrayFind(exclude, "notify-user-data")) &&
5165 (!ra || cupsArrayFind(ra, "notify-user-data")))
5166 ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data",
5167 sub->user_data, sub->user_data_len);
b423cd4c 5168 }
ef416fc2 5169
b423cd4c 5170 if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id")))
5171 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5172 "notify-job-id", sub->job->id);
ef416fc2 5173
b423cd4c 5174 if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri")))
5175 {
5176 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
f2534050 5177 "ipp", NULL, con->clientname, con->clientport,
b423cd4c 5178 "/printers/%s", sub->dest->name);
5179 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
5180 "notify-printer-uri", NULL, printer_uri);
ef416fc2 5181 }
ef416fc2 5182
b423cd4c 5183 if (!ra || cupsArrayFind(ra, "notify-subscription-id"))
5184 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5185 "notify-subscription-id", sub->id);
b423cd4c 5186}
ef416fc2 5187
ef416fc2 5188
b423cd4c 5189/*
5190 * 'create_job()' - Print a file to a printer or class.
5191 */
5192
5193static void
5194create_job(cupsd_client_t *con, /* I - Client connection */
5195 ipp_attribute_t *uri) /* I - Printer URI */
5196{
a29fd7dd 5197 int i; /* Looping var */
f7deaa1a 5198 cupsd_printer_t *printer; /* Printer */
5199 cupsd_job_t *job; /* New job */
a29fd7dd
MS
5200 static const char * const forbidden_attrs[] =
5201 { /* List of forbidden attributes */
5202 "compression",
5203 "document-format",
5204 "document-name",
5205 "document-natural-language"
5206 };
b423cd4c 5207
ef416fc2 5208
b423cd4c 5209 cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con,
996acce8 5210 con->number, uri->values[0].string.text);
ef416fc2 5211
f7deaa1a 5212 /*
5213 * Is the destination valid?
5214 */
5215
5216 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
5217 {
5218 /*
5219 * Bad URI...
5220 */
5221
5222 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 5223 _("The printer or class does not exist."));
f7deaa1a 5224 return;
5225 }
5226
a29fd7dd
MS
5227 /*
5228 * Check for invalid Create-Job attributes and log a warning or error depending
5229 * on whether cupsd is running in "strict conformance" mode...
5230 */
5231
5232 for (i = 0;
5233 i < (int)(sizeof(forbidden_attrs) / sizeof(forbidden_attrs[0]));
5234 i ++)
5235 if (ippFindAttribute(con->request, forbidden_attrs[i], IPP_TAG_ZERO))
5236 {
5237 if (StrictConformance)
5238 {
5239 send_ipp_status(con, IPP_BAD_REQUEST,
5240 _("The '%s' operation attribute cannot be supplied in a "
5241 "Create-Job request."), forbidden_attrs[i]);
5242 return;
5243 }
5244
5245 cupsdLogMessage(CUPSD_LOG_WARN,
5246 "Unexpected '%s' operation attribute in a Create-Job "
5247 "request.", forbidden_attrs[i]);
5248 }
5249
ef416fc2 5250 /*
b423cd4c 5251 * Create the job object...
ef416fc2 5252 */
5253
f7deaa1a 5254 if ((job = add_job(con, printer, NULL)) == NULL)
b423cd4c 5255 return;
ef416fc2 5256
09a101d6 5257 job->pending_timeout = 1;
5258
ef416fc2 5259 /*
5260 * Save and log the job...
5261 */
f7deaa1a 5262
75bd9771
MS
5263 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
5264 job->dest, job->username);
ef416fc2 5265}
5266
5267
95ece0cb
MS
5268/*
5269 * 'create_local_bg_thread()' - Background thread for creating a local print queue.
5270 */
5271
5272static void * /* O - Exit status */
5273create_local_bg_thread(
5274 cupsd_printer_t *printer) /* I - Printer */
5275{
5276 cups_file_t *from, /* Source file */
5277 *to; /* Destination file */
5278 char fromppd[1024], /* Source PPD */
5279 toppd[1024], /* Destination PPD */
5280 scheme[32], /* URI scheme */
5281 userpass[256], /* User:pass */
5282 host[256], /* Hostname */
5283 resource[1024], /* Resource path */
5284 line[1024]; /* Line from PPD */
5285 int port; /* Port number */
5286 http_encryption_t encryption; /* Type of encryption to use */
5287 http_t *http; /* Connection to printer */
5288 ipp_t *request, /* Request to printer */
5289 *response; /* Response from printer */
5290 ipp_attribute_t *attr; /* Attribute in response */
aefefc35 5291 ipp_status_t status; /* Status code */
95ece0cb
MS
5292
5293
5294 /*
5295 * Try connecting to the printer...
5296 */
5297
c585706d
MS
5298 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Generating PPD file from \"%s\"...", printer->name, printer->device_uri);
5299
95ece0cb 5300 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
5301 {
5302 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Bad device URI \"%s\".", printer->name, printer->device_uri);
95ece0cb 5303 return (NULL);
c585706d 5304 }
95ece0cb
MS
5305
5306 if (!strcmp(scheme, "ipps") || port == 443)
5307 encryption = HTTP_ENCRYPTION_ALWAYS;
5308 else
5309 encryption = HTTP_ENCRYPTION_IF_REQUESTED;
5310
5311 if ((http = httpConnect2(host, port, NULL, AF_UNSPEC, encryption, 1, 30000, NULL)) == NULL)
c585706d
MS
5312 {
5313 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to connect to %s:%d: %s", printer->name, host, port, cupsLastErrorString());
95ece0cb 5314 return (NULL);
c585706d 5315 }
95ece0cb
MS
5316
5317 /*
5318 * Query the printer for its capabilities...
5319 */
5320
c585706d
MS
5321 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Connected to %s:%d, sending Get-Printer-Attributes request...", printer->name, host, port);
5322
95ece0cb 5323 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
aefefc35 5324 ippSetVersion(request, 2, 0);
95ece0cb
MS
5325 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printer->device_uri);
5326 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", NULL, "all");
5327
5328 response = cupsDoRequest(http, request, resource);
aefefc35
MS
5329 status = cupsLastError();
5330
5331 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Get-Printer-Attributes returned %s (%s)", printer->name, ippErrorString(cupsLastError()), cupsLastErrorString());
5332
5333 if (status == IPP_STATUS_ERROR_BAD_REQUEST || status == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED)
5334 {
5335 /*
5336 * Try request using IPP/1.1, in case we are talking to an old CUPS server or
5337 * printer...
5338 */
95ece0cb 5339
aefefc35
MS
5340 ippDelete(response);
5341
5342 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Re-sending Get-Printer-Attributes request using IPP/1.1...", printer->name);
5343
5344 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
5345 ippSetVersion(request, 1, 1);
5346 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printer->device_uri);
5347 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", NULL, "all");
5348
5349 response = cupsDoRequest(http, request, resource);
5350
5351 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: IPP/1.1 Get-Printer-Attributes returned %s (%s)", printer->name, ippErrorString(cupsLastError()), cupsLastErrorString());
5352 }
c585706d 5353
95ece0cb
MS
5354 // TODO: Grab printer icon file...
5355 httpClose(http);
5356
5357 /*
5358 * Write the PPD for the queue...
5359 */
5360
5361 if (_ppdCreateFromIPP(fromppd, sizeof(fromppd), response))
5362 {
f6d93318
MS
5363 _cupsRWLockWrite(&printer->lock);
5364
95ece0cb
MS
5365 if ((!printer->info || !*(printer->info)) && (attr = ippFindAttribute(response, "printer-info", IPP_TAG_TEXT)) != NULL)
5366 cupsdSetString(&printer->info, ippGetString(attr, 0, NULL));
5367
5368 if ((!printer->location || !*(printer->location)) && (attr = ippFindAttribute(response, "printer-location", IPP_TAG_TEXT)) != NULL)
5369 cupsdSetString(&printer->location, ippGetString(attr, 0, NULL));
5370
5371 if ((!printer->geo_location || !*(printer->geo_location)) && (attr = ippFindAttribute(response, "printer-geo-location", IPP_TAG_URI)) != NULL)
5372 cupsdSetString(&printer->geo_location, ippGetString(attr, 0, NULL));
5373
f6d93318
MS
5374 _cupsRWUnlock(&printer->lock);
5375
95ece0cb 5376 if ((from = cupsFileOpen(fromppd, "r")) == NULL)
c585706d
MS
5377 {
5378 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to read generated PPD: %s", printer->name, strerror(errno));
95ece0cb 5379 return (NULL);
c585706d 5380 }
95ece0cb
MS
5381
5382 snprintf(toppd, sizeof(toppd), "%s/ppd/%s.ppd", ServerRoot, printer->name);
5383 if ((to = cupsdCreateConfFile(toppd, ConfigFilePerm)) == NULL)
5384 {
c585706d 5385 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to create PPD for printer: %s", printer->name, strerror(errno));
95ece0cb
MS
5386 cupsFileClose(from);
5387 return (NULL);
5388 }
5389
5390 while (cupsFileGets(from, line, sizeof(line)))
5391 cupsFilePrintf(to, "%s\n", line);
5392
5393 cupsFileClose(from);
5394 if (!cupsdCloseCreatedConfFile(to, toppd))
5395 {
c585706d
MS
5396 printer->config_time = time(NULL);
5397 printer->state = IPP_PSTATE_IDLE;
5398 printer->accepting = 1;
95ece0cb
MS
5399
5400 cupsdSetPrinterAttrs(printer);
c585706d
MS
5401
5402 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL, "Printer \"%s\" is now available.", printer->name);
5403 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" is now available.", printer->name);
95ece0cb
MS
5404 }
5405 }
c585706d 5406 else
956bb127 5407 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: PPD creation failed: %s", printer->name, cupsLastErrorString());
95ece0cb
MS
5408
5409 return (NULL);
5410}
5411
5412
7ae00c35
MS
5413/*
5414 * 'create_local_printer()' - Create a local (temporary) print queue.
5415 */
5416
5417static void
5418create_local_printer(
5419 cupsd_client_t *con) /* I - Client connection */
5420{
95ece0cb
MS
5421 ipp_attribute_t *device_uri, /* device-uri attribute */
5422 *printer_geo_location, /* printer-geo-location attribute */
5423 *printer_info, /* printer-info attribute */
5424 *printer_location, /* printer-location attribute */
5425 *printer_name; /* printer-name attribute */
5426 cupsd_printer_t *printer; /* New printer */
5427 http_status_t status; /* Policy status */
5428 char name[128], /* Sanitized printer name */
5429 *nameptr, /* Pointer into name */
5430 uri[1024]; /* printer-uri-supported value */
5431 const char *ptr; /* Pointer into attribute value */
5432
5433
5434 /*
5435 * Require local access to create a local printer...
5436 */
5437
5438 if (!httpAddrLocalhost(httpGetAddress(con->http)))
5439 {
5440 send_ipp_status(con, IPP_STATUS_ERROR_FORBIDDEN, _("Only local users can create a local printer."));
5441 return;
5442 }
5443
5444 /*
5445 * Check any other policy limits...
5446 */
5447
5448 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5449 {
5450 send_http_error(con, status, NULL);
5451 return;
5452 }
5453
5454 /*
5455 * Grab needed attributes...
5456 */
5457
5458 if ((printer_name = ippFindAttribute(con->request, "printer-name", IPP_TAG_ZERO)) == NULL || ippGetGroupTag(printer_name) != IPP_TAG_PRINTER || ippGetValueTag(printer_name) != IPP_TAG_NAME)
5459 {
5460 if (!printer_name)
5461 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Missing required attribute \"%s\"."), "printer-name");
5462 else if (ippGetGroupTag(printer_name) != IPP_TAG_PRINTER)
5463 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is in the wrong group."), "printer-name");
5464 else
5465 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is the wrong value type."), "printer-name");
5466
5467 return;
5468 }
5469
5470 for (nameptr = name, ptr = ippGetString(printer_name, 0, NULL); *ptr && nameptr < (name + sizeof(name) - 1); ptr ++)
5471 {
5472 /*
5473 * Sanitize the printer name...
5474 */
5475
5476 if (_cups_isalnum(*ptr))
5477 *nameptr++ = *ptr;
5478 else if (nameptr == name || nameptr[-1] != '_')
5479 *nameptr++ = '_';
5480 }
5481
5482 *nameptr = '\0';
5483
5484 if ((device_uri = ippFindAttribute(con->request, "device-uri", IPP_TAG_ZERO)) == NULL || ippGetGroupTag(device_uri) != IPP_TAG_PRINTER || ippGetValueTag(device_uri) != IPP_TAG_URI)
5485 {
5486 if (!device_uri)
5487 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Missing required attribute \"%s\"."), "device-uri");
5488 else if (ippGetGroupTag(device_uri) != IPP_TAG_PRINTER)
5489 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is in the wrong group."), "device-uri");
5490 else
5491 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is the wrong value type."), "device-uri");
5492
5493 return;
5494 }
5495
5496 printer_geo_location = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI);
5497 printer_info = ippFindAttribute(con->request, "printer-info", IPP_TAG_TEXT);
5498 printer_location = ippFindAttribute(con->request, "printer-location", IPP_TAG_TEXT);
5499
46385a1a
MS
5500 /*
5501 * See if the printer already exists...
5502 */
5503
5504 if ((printer = cupsdFindDest(name)) != NULL)
5505 {
5506 send_ipp_status(con, IPP_STATUS_ERROR_NOT_POSSIBLE, _("Printer \"%s\" already exists."), name);
5507 goto add_printer_attributes;
5508 }
5509
95ece0cb
MS
5510 /*
5511 * Create the printer...
5512 */
5513
5514 if ((printer = cupsdAddPrinter(name)) == NULL)
5515 {
5516 send_ipp_status(con, IPP_STATUS_ERROR_INTERNAL, _("Unable to create printer."));
5517 return;
5518 }
5519
167e0655
MS
5520 printer->shared = 0;
5521 printer->temporary = 1;
42deee7e 5522
95ece0cb
MS
5523 cupsdSetDeviceURI(printer, ippGetString(device_uri, 0, NULL));
5524
5525 if (printer_geo_location)
5526 cupsdSetString(&printer->geo_location, ippGetString(printer_geo_location, 0, NULL));
5527 if (printer_info)
5528 cupsdSetString(&printer->info, ippGetString(printer_info, 0, NULL));
5529 if (printer_location)
5530 cupsdSetString(&printer->location, ippGetString(printer_location, 0, NULL));
5531
5532 cupsdSetPrinterAttrs(printer);
5533
5534 /*
5535 * Run a background thread to create the PPD...
5536 */
5537
5538 _cupsThreadCreate((_cups_thread_func_t)create_local_bg_thread, printer);
5539
5540 /*
5541 * Return printer attributes...
5542 */
5543
5544 send_ipp_status(con, IPP_STATUS_OK, _("Local printer created."));
5545
46385a1a
MS
5546 add_printer_attributes:
5547
95ece0cb
MS
5548 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", (char)printer->accepting);
5549 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", printer->state);
5550 add_printer_state_reasons(con, printer);
5551
5552 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), httpIsEncrypted(con->http) ? "ipps" : "ipp", NULL, con->clientname, con->clientport, "/printers/%s", printer->name);
5553 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-uri-supported", NULL, uri);
7ae00c35
MS
5554}
5555
5556
ef416fc2 5557/*
5558 * 'create_requested_array()' - Create an array for the requested-attributes.
5559 */
5560
5561static cups_array_t * /* O - Array of attributes or NULL */
5562create_requested_array(ipp_t *request) /* I - IPP request */
5563{
ef416fc2 5564 cups_array_t *ra; /* Requested attributes array */
ef416fc2 5565
5566
5567 /*
db8b865d 5568 * Create the array for standard attributes...
ef416fc2 5569 */
5570
db8b865d 5571 ra = ippCreateRequestedArray(request);
ef416fc2 5572
5573 /*
db8b865d 5574 * Add CUPS defaults as needed...
ef416fc2 5575 */
5576
db8b865d 5577 if (cupsArrayFind(ra, "printer-defaults"))
ef416fc2 5578 {
db8b865d
MS
5579 /*
5580 * Include user-set defaults...
5581 */
ef416fc2 5582
db8b865d 5583 char *name; /* Option name */
f7deaa1a 5584
db8b865d 5585 cupsArrayRemove(ra, "printer-defaults");
f7deaa1a 5586
db8b865d
MS
5587 for (name = (char *)cupsArrayFirst(CommonDefaults);
5588 name;
5589 name = (char *)cupsArrayNext(CommonDefaults))
5590 if (!cupsArrayFind(ra, name))
f7deaa1a 5591 cupsArrayAdd(ra, name);
ef416fc2 5592 }
5593
5594 return (ra);
5595}
5596
5597
5598/*
42404685 5599 * 'create_subscriptions()' - Create one or more notification subscriptions.
ef416fc2 5600 */
5601
5602static void
42404685 5603create_subscriptions(
ef416fc2 5604 cupsd_client_t *con, /* I - Client connection */
5605 ipp_attribute_t *uri) /* I - Printer URI */
5606{
5607 http_status_t status; /* Policy status */
5608 int i; /* Looping var */
5609 ipp_attribute_t *attr; /* Current attribute */
bc44d920 5610 cups_ptype_t dtype; /* Destination type (printer/class) */
ed486911 5611 char scheme[HTTP_MAX_URI],
5612 /* Scheme portion of URI */
ef416fc2 5613 userpass[HTTP_MAX_URI],
5614 /* Username portion of URI */
5615 host[HTTP_MAX_URI],
5616 /* Host portion of URI */
5617 resource[HTTP_MAX_URI];
5618 /* Resource portion of URI */
5619 int port; /* Port portion of URI */
5620 cupsd_printer_t *printer; /* Printer/class */
5621 cupsd_job_t *job; /* Job */
5622 int jobid; /* Job ID */
5623 cupsd_subscription_t *sub; /* Subscription object */
bc44d920 5624 const char *username, /* requesting-user-name or
5625 authenticated username */
ef416fc2 5626 *recipient, /* notify-recipient-uri */
5627 *pullmethod; /* notify-pull-method */
5628 ipp_attribute_t *user_data; /* notify-user-data */
5629 int interval, /* notify-time-interval */
5630 lease; /* notify-lease-duration */
5631 unsigned mask; /* notify-events */
f7deaa1a 5632 ipp_attribute_t *notify_events,/* notify-events(-default) */
5633 *notify_lease; /* notify-lease-duration(-default) */
ef416fc2 5634
5635
bd7854cb 5636#ifdef DEBUG
5637 for (attr = con->request->attrs; attr; attr = attr->next)
5638 {
5639 if (attr->group_tag != IPP_TAG_ZERO)
3dfe78b3 5640 cupsdLogMessage(CUPSD_LOG_DEBUG2, "g%04x v%04x %s", attr->group_tag,
bd7854cb 5641 attr->value_tag, attr->name);
5642 else
3dfe78b3 5643 cupsdLogMessage(CUPSD_LOG_DEBUG2, "----SEP----");
bd7854cb 5644 }
5645#endif /* DEBUG */
5646
ef416fc2 5647 /*
5648 * Is the destination valid?
5649 */
5650
42404685 5651 cupsdLogMessage(CUPSD_LOG_DEBUG, "create_subscriptions(con=%p(%d), uri=\"%s\")", con, con->number, uri->values[0].string.text);
fa73b229 5652
ed486911 5653 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
5654 sizeof(scheme), userpass, sizeof(userpass), host,
a4d04587 5655 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 5656
5657 if (!strcmp(resource, "/"))
5658 {
ef416fc2 5659 dtype = (cups_ptype_t)0;
5660 printer = NULL;
5661 }
5662 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
5663 {
ef416fc2 5664 dtype = (cups_ptype_t)0;
5665 printer = NULL;
5666 }
5667 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
5668 {
ef416fc2 5669 dtype = CUPS_PRINTER_CLASS;
5670 printer = NULL;
5671 }
f7deaa1a 5672 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 5673 {
5674 /*
5675 * Bad URI...
5676 */
5677
5678 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 5679 _("The printer or class does not exist."));
ef416fc2 5680 return;
5681 }
5682
5683 /*
5684 * Check policy...
5685 */
5686
5687 if (printer)
5688 {
f7deaa1a 5689 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
5690 NULL)) != HTTP_OK)
ef416fc2 5691 {
f899b121 5692 send_http_error(con, status, printer);
ef416fc2 5693 return;
5694 }
5695 }
5696 else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5697 {
f899b121 5698 send_http_error(con, status, NULL);
ef416fc2 5699 return;
5700 }
5701
5702 /*
5703 * Get the user that is requesting the subscription...
5704 */
5705
e00b005a 5706 username = get_username(con);
ef416fc2 5707
5708 /*
5709 * Find the first subscription group attribute; return if we have
5710 * none...
5711 */
5712
5713 for (attr = con->request->attrs; attr; attr = attr->next)
5714 if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
5715 break;
5716
5717 if (!attr)
5718 {
5719 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 5720 _("No subscription attributes in request."));
ef416fc2 5721 return;
5722 }
5723
5724 /*
5725 * Process the subscription attributes in the request...
5726 */
5727
fa73b229 5728 con->response->request.status.status_code = IPP_BAD_REQUEST;
5729
ef416fc2 5730 while (attr)
5731 {
5732 recipient = NULL;
5733 pullmethod = NULL;
5734 user_data = NULL;
5735 interval = 0;
5736 lease = DefaultLeaseDuration;
5737 jobid = 0;
5738 mask = CUPSD_EVENT_NONE;
5739
f7deaa1a 5740 if (printer)
5741 {
5742 notify_events = ippFindAttribute(printer->attrs, "notify-events-default",
5743 IPP_TAG_KEYWORD);
5744 notify_lease = ippFindAttribute(printer->attrs,
5745 "notify-lease-duration-default",
5746 IPP_TAG_INTEGER);
5747
5748 if (notify_lease)
5749 lease = notify_lease->values[0].integer;
5750 }
5751 else
5752 {
5753 notify_events = NULL;
5754 notify_lease = NULL;
5755 }
5756
ef416fc2 5757 while (attr && attr->group_tag != IPP_TAG_ZERO)
5758 {
7594b224 5759 if (!strcmp(attr->name, "notify-recipient-uri") &&
ef416fc2 5760 attr->value_tag == IPP_TAG_URI)
ed486911 5761 {
5762 /*
5763 * Validate the recipient scheme against the ServerBin/notifier
5764 * directory...
5765 */
5766
5767 char notifier[1024]; /* Notifier filename */
5768
5769
ef416fc2 5770 recipient = attr->values[0].string.text;
ed486911 5771
8ca02f3c 5772 if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient,
5773 scheme, sizeof(scheme), userpass, sizeof(userpass),
5774 host, sizeof(host), &port,
5775 resource, sizeof(resource)) < HTTP_URI_OK)
ed486911 5776 {
5777 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 5778 _("Bad notify-recipient-uri \"%s\"."), recipient);
ed486911 5779 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5780 "notify-status-code", IPP_URI_SCHEME);
5781 return;
5782 }
5783
5784 snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
5785 scheme);
5786 if (access(notifier, X_OK))
5787 {
5788 send_ipp_status(con, IPP_NOT_POSSIBLE,
bc44d920 5789 _("notify-recipient-uri URI \"%s\" uses unknown "
84315f46 5790 "scheme."), recipient);
ed486911 5791 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5792 "notify-status-code", IPP_URI_SCHEME);
5793 return;
5794 }
3dfe78b3
MS
5795
5796 if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient))
5797 {
5798 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 5799 _("notify-recipient-uri URI \"%s\" is already used."),
3dfe78b3
MS
5800 recipient);
5801 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5802 "notify-status-code", IPP_ATTRIBUTES);
5803 return;
5804 }
ed486911 5805 }
ef416fc2 5806 else if (!strcmp(attr->name, "notify-pull-method") &&
5807 attr->value_tag == IPP_TAG_KEYWORD)
ed486911 5808 {
ef416fc2 5809 pullmethod = attr->values[0].string.text;
ed486911 5810
5811 if (strcmp(pullmethod, "ippget"))
5812 {
5813 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 5814 _("Bad notify-pull-method \"%s\"."), pullmethod);
ed486911 5815 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5816 "notify-status-code", IPP_ATTRIBUTES);
5817 return;
5818 }
5819 }
ef416fc2 5820 else if (!strcmp(attr->name, "notify-charset") &&
5821 attr->value_tag == IPP_TAG_CHARSET &&
5822 strcmp(attr->values[0].string.text, "us-ascii") &&
5823 strcmp(attr->values[0].string.text, "utf-8"))
5824 {
5825 send_ipp_status(con, IPP_CHARSET,
84315f46 5826 _("Character set \"%s\" not supported."),
ef416fc2 5827 attr->values[0].string.text);
5828 return;
5829 }
5830 else if (!strcmp(attr->name, "notify-natural-language") &&
5831 (attr->value_tag != IPP_TAG_LANGUAGE ||
5832 strcmp(attr->values[0].string.text, DefaultLanguage)))
5833 {
5834 send_ipp_status(con, IPP_CHARSET,
84315f46 5835 _("Language \"%s\" not supported."),
ef416fc2 5836 attr->values[0].string.text);
5837 return;
5838 }
5839 else if (!strcmp(attr->name, "notify-user-data") &&
5840 attr->value_tag == IPP_TAG_STRING)
5841 {
5842 if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
5843 {
5844 send_ipp_status(con, IPP_REQUEST_VALUE,
5845 _("The notify-user-data value is too large "
84315f46 5846 "(%d > 63 octets)."),
ef416fc2 5847 attr->values[0].unknown.length);
5848 return;
5849 }
5850
5851 user_data = attr;
5852 }
5853 else if (!strcmp(attr->name, "notify-events") &&
5854 attr->value_tag == IPP_TAG_KEYWORD)
f7deaa1a 5855 notify_events = attr;
ef416fc2 5856 else if (!strcmp(attr->name, "notify-lease-duration") &&
5857 attr->value_tag == IPP_TAG_INTEGER)
5858 lease = attr->values[0].integer;
5859 else if (!strcmp(attr->name, "notify-time-interval") &&
5860 attr->value_tag == IPP_TAG_INTEGER)
5861 interval = attr->values[0].integer;
5862 else if (!strcmp(attr->name, "notify-job-id") &&
5863 attr->value_tag == IPP_TAG_INTEGER)
5864 jobid = attr->values[0].integer;
5865
5866 attr = attr->next;
5867 }
5868
f7deaa1a 5869 if (notify_events)
5870 {
5871 for (i = 0; i < notify_events->num_values; i ++)
5872 mask |= cupsdEventValue(notify_events->values[i].string.text);
5873 }
5874
bd7854cb 5875 if (recipient)
4c37eb9f 5876 {
bd7854cb 5877 cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient);
4c37eb9f
MS
5878
5879
5880 if (!strncmp(recipient, "mailto:", 7) && user_data)
5881 {
5882 char temp[64]; /* Temporary string */
5883
5884 memcpy(temp, user_data->values[0].unknown.data, user_data->values[0].unknown.length);
5885 temp[user_data->values[0].unknown.length] = '\0';
5886
5887 if (httpSeparateURI(HTTP_URI_CODING_ALL, temp, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_OK)
5888 {
5889 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad notify-user-data \"%s\"."), temp);
5890 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, "notify-status-code", IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES);
5891 return;
5892 }
5893 }
5894 }
5895
bd7854cb 5896 if (pullmethod)
5897 cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod);
5898 cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease);
5899 cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval);
fa73b229 5900
ef416fc2 5901 if (!recipient && !pullmethod)
5902 break;
5903
5904 if (mask == CUPSD_EVENT_NONE)
5905 {
5906 if (jobid)
5907 mask = CUPSD_EVENT_JOB_COMPLETED;
5908 else if (printer)
5909 mask = CUPSD_EVENT_PRINTER_STATE_CHANGED;
5910 else
5911 {
5912 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 5913 _("notify-events not specified."));
ef416fc2 5914 return;
5915 }
5916 }
5917
bd7854cb 5918 if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration))
ef416fc2 5919 {
5920 cupsdLogMessage(CUPSD_LOG_INFO,
42404685 5921 "create_subscriptions: Limiting notify-lease-duration to "
ef416fc2 5922 "%d seconds.",
5923 MaxLeaseDuration);
5924 lease = MaxLeaseDuration;
5925 }
5926
5927 if (jobid)
5928 {
5929 if ((job = cupsdFindJob(jobid)) == NULL)
5930 {
84315f46
MS
5931 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
5932 jobid);
ef416fc2 5933 return;
5934 }
5935 }
5936 else
5937 job = NULL;
5938
52f6f666
MS
5939 if ((sub = cupsdAddSubscription(mask, printer, job, recipient, 0)) == NULL)
5940 {
5941 send_ipp_status(con, IPP_TOO_MANY_SUBSCRIPTIONS,
5942 _("There are too many subscriptions."));
5943 return;
5944 }
ef416fc2 5945
fa73b229 5946 if (job)
84315f46 5947 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription #%d for job %d.",
fa73b229 5948 sub->id, job->id);
5949 else if (printer)
5950 cupsdLogMessage(CUPSD_LOG_DEBUG,
84315f46 5951 "Added subscription #%d for printer \"%s\".",
fa73b229 5952 sub->id, printer->name);
5953 else
84315f46 5954 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription #%d for server.",
fa73b229 5955 sub->id);
5956
ef416fc2 5957 sub->interval = interval;
5958 sub->lease = lease;
bd7854cb 5959 sub->expire = lease ? time(NULL) + lease : 0;
ef416fc2 5960
5961 cupsdSetString(&sub->owner, username);
5962
5963 if (user_data)
5964 {
5965 sub->user_data_len = user_data->values[0].unknown.length;
5966 memcpy(sub->user_data, user_data->values[0].unknown.data,
07623986 5967 (size_t)sub->user_data_len);
ef416fc2 5968 }
5969
5970 ippAddSeparator(con->response);
5971 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5972 "notify-subscription-id", sub->id);
5973
fa73b229 5974 con->response->request.status.status_code = IPP_OK;
5975
ef416fc2 5976 if (attr)
5977 attr = attr->next;
5978 }
5979
3dfe78b3 5980 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
ef416fc2 5981}
5982
5983
5984/*
5985 * 'delete_printer()' - Remove a printer or class from the system.
5986 */
5987
5988static void
5989delete_printer(cupsd_client_t *con, /* I - Client connection */
5990 ipp_attribute_t *uri) /* I - URI of printer or class */
5991{
5992 http_status_t status; /* Policy status */
bc44d920 5993 cups_ptype_t dtype; /* Destination type (printer/class) */
ef416fc2 5994 cupsd_printer_t *printer; /* Printer/class */
5995 char filename[1024]; /* Script/PPD filename */
7ae00c35 5996 int temporary; /* Temporary queue? */
ef416fc2 5997
5998
5999 cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con,
996acce8 6000 con->number, uri->values[0].string.text);
ef416fc2 6001
6002 /*
6003 * Do we have a valid URI?
6004 */
6005
f7deaa1a 6006 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 6007 {
6008 /*
6009 * Bad URI...
6010 */
6011
6012 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 6013 _("The printer or class does not exist."));
ef416fc2 6014 return;
6015 }
6016
6017 /*
6018 * Check policy...
6019 */
6020
6021 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6022 {
f899b121 6023 send_http_error(con, status, NULL);
ef416fc2 6024 return;
6025 }
6026
6027 /*
6028 * Remove old jobs...
6029 */
6030
f7deaa1a 6031 cupsdCancelJobs(printer->name, NULL, 1);
ef416fc2 6032
6033 /*
6034 * Remove old subscriptions and send a "deleted printer" event...
6035 */
6036
6037 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL,
6038 "%s \"%s\" deleted by \"%s\".",
6039 (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
f7deaa1a 6040 printer->name, get_username(con));
ef416fc2 6041
6042 cupsdExpireSubscriptions(printer, NULL);
f7deaa1a 6043
ef416fc2 6044 /*
6045 * Remove any old PPD or script files...
6046 */
6047
f7deaa1a 6048 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
6049 printer->name);
ef416fc2 6050 unlink(filename);
928b43f7
MS
6051 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd.O", ServerRoot,
6052 printer->name);
6053 unlink(filename);
ef416fc2 6054
7cf5915e 6055 snprintf(filename, sizeof(filename), "%s/%s.png", CacheDir, printer->name);
61cf44e2
MS
6056 unlink(filename);
6057
f14324a7 6058 snprintf(filename, sizeof(filename), "%s/%s.data", CacheDir, printer->name);
54afec33
MS
6059 unlink(filename);
6060
568fa3fa
MS
6061 /*
6062 * Unregister color profiles...
6063 */
6064
a29fd7dd 6065 cupsdUnregisterColor(printer);
568fa3fa 6066
7ae00c35
MS
6067 temporary = printer->temporary;
6068
ef416fc2 6069 if (dtype & CUPS_PRINTER_CLASS)
6070 {
f7deaa1a 6071 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".",
6072 printer->name, get_username(con));
ef416fc2 6073
6074 cupsdDeletePrinter(printer, 0);
7ae00c35
MS
6075 if (!temporary)
6076 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
ef416fc2 6077 }
6078 else
6079 {
f7deaa1a 6080 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".",
6081 printer->name, get_username(con));
ef416fc2 6082
7ae00c35 6083 if (cupsdDeletePrinter(printer, 0) && !temporary)
f8b3a85b
MS
6084 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
6085
7ae00c35
MS
6086 if (!temporary)
6087 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
ef416fc2 6088 }
6089
7ae00c35
MS
6090 if (!temporary)
6091 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
ef416fc2 6092
6093 /*
6094 * Return with no errors...
6095 */
6096
6097 con->response->request.status.status_code = IPP_OK;
6098}
6099
6100
6101/*
6102 * 'get_default()' - Get the default destination.
6103 */
6104
6105static void
6106get_default(cupsd_client_t *con) /* I - Client connection */
6107{
fa73b229 6108 http_status_t status; /* Policy status */
6109 cups_array_t *ra; /* Requested attributes array */
ef416fc2 6110
6111
996acce8 6112 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->number);
ef416fc2 6113
6114 /*
6115 * Check policy...
6116 */
6117
6118 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6119 {
f899b121 6120 send_http_error(con, status, NULL);
ef416fc2 6121 return;
6122 }
6123
fa73b229 6124 if (DefaultPrinter)
ef416fc2 6125 {
fa73b229 6126 ra = create_requested_array(con->request);
ef416fc2 6127
fa73b229 6128 copy_printer_attrs(con, DefaultPrinter, ra);
ef416fc2 6129
fa73b229 6130 cupsArrayDelete(ra);
ef416fc2 6131
fa73b229 6132 con->response->request.status.status_code = IPP_OK;
ef416fc2 6133 }
6134 else
84315f46 6135 send_ipp_status(con, IPP_NOT_FOUND, _("No default printer."));
ef416fc2 6136}
6137
6138
6139/*
6140 * 'get_devices()' - Get the list of available devices on the local system.
6141 */
6142
6143static void
6144get_devices(cupsd_client_t *con) /* I - Client connection */
6145{
6146 http_status_t status; /* Policy status */
ae71f5de
MS
6147 ipp_attribute_t *limit, /* limit attribute */
6148 *timeout, /* timeout attribute */
6149 *requested, /* requested-attributes attribute */
ed6e7faf
MS
6150 *exclude, /* exclude-schemes attribute */
6151 *include; /* include-schemes attribute */
ef416fc2 6152 char command[1024], /* cups-deviced command */
ed6e7faf 6153 options[2048], /* Options to pass to command */
ae71f5de 6154 requested_str[256],
89d46774 6155 /* String for requested attributes */
ed6e7faf
MS
6156 exclude_str[512],
6157 /* String for excluded schemes */
6158 include_str[512];
6159 /* String for included schemes */
ef416fc2 6160
6161
996acce8 6162 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->number);
ef416fc2 6163
6164 /*
6165 * Check policy...
6166 */
6167
6168 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6169 {
f899b121 6170 send_http_error(con, status, NULL);
ef416fc2 6171 return;
6172 }
6173
6174 /*
6175 * Run cups-deviced command with the given options...
6176 */
6177
ae71f5de
MS
6178 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
6179 timeout = ippFindAttribute(con->request, "timeout", IPP_TAG_INTEGER);
ef416fc2 6180 requested = ippFindAttribute(con->request, "requested-attributes",
6181 IPP_TAG_KEYWORD);
ae71f5de 6182 exclude = ippFindAttribute(con->request, "exclude-schemes", IPP_TAG_NAME);
ed6e7faf 6183 include = ippFindAttribute(con->request, "include-schemes", IPP_TAG_NAME);
ef416fc2 6184
6185 if (requested)
89d46774 6186 url_encode_attr(requested, requested_str, sizeof(requested_str));
ef416fc2 6187 else
89d46774 6188 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
ef416fc2 6189
ae71f5de
MS
6190 if (exclude)
6191 url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
6192 else
6193 exclude_str[0] = '\0';
6194
ed6e7faf
MS
6195 if (include)
6196 url_encode_attr(include, include_str, sizeof(include_str));
6197 else
6198 include_str[0] = '\0';
6199
ef416fc2 6200 snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin);
6201 snprintf(options, sizeof(options),
ed6e7faf 6202 "%d+%d+%d+%d+%s%s%s%s%s",
ef416fc2 6203 con->request->request.op.request_id,
ae71f5de 6204 limit ? limit->values[0].integer : 0,
7a0cbd5e 6205 timeout ? timeout->values[0].integer : 15,
ae71f5de
MS
6206 (int)User,
6207 requested_str,
ed6e7faf
MS
6208 exclude_str[0] ? "%20" : "", exclude_str,
6209 include_str[0] ? "%20" : "", include_str);
ef416fc2 6210
6211 if (cupsdSendCommand(con, command, options, 1))
6212 {
6213 /*
6214 * Command started successfully, don't send an IPP response here...
6215 */
6216
6217 ippDelete(con->response);
6218 con->response = NULL;
6219 }
6220 else
6221 {
6222 /*
6223 * Command failed, return "internal error" so the user knows something
6224 * went wrong...
6225 */
6226
6227 send_ipp_status(con, IPP_INTERNAL_ERROR,
6228 _("cups-deviced failed to execute."));
6229 }
6230}
6231
6232
2e4ff8af
MS
6233/*
6234 * 'get_document()' - Get a copy of a job file.
6235 */
6236
6237static void
6238get_document(cupsd_client_t *con, /* I - Client connection */
6239 ipp_attribute_t *uri) /* I - Job URI */
6240{
6241 http_status_t status; /* Policy status */
6242 ipp_attribute_t *attr; /* Current attribute */
6243 int jobid; /* Job ID */
6244 int docnum; /* Document number */
6245 cupsd_job_t *job; /* Current job */
61cf44e2 6246 char scheme[HTTP_MAX_URI], /* Method portion of URI */
2e4ff8af
MS
6247 username[HTTP_MAX_URI], /* Username portion of URI */
6248 host[HTTP_MAX_URI], /* Host portion of URI */
6249 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6250 int port; /* Port portion of URI */
6251 char filename[1024], /* Filename for document */
6252 format[1024]; /* Format for document */
6253
6254
6255 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_document(%p[%d], %s)", con,
996acce8 6256 con->number, uri->values[0].string.text);
2e4ff8af
MS
6257
6258 /*
6259 * See if we have a job URI or a printer URI...
6260 */
6261
6262 if (!strcmp(uri->name, "printer-uri"))
6263 {
6264 /*
6265 * Got a printer URI; see if we also have a job-id attribute...
6266 */
6267
6268 if ((attr = ippFindAttribute(con->request, "job-id",
6269 IPP_TAG_INTEGER)) == NULL)
6270 {
6271 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 6272 _("Got a printer-uri attribute but no job-id."));
2e4ff8af
MS
6273 return;
6274 }
6275
6276 jobid = attr->values[0].integer;
6277 }
6278 else
6279 {
6280 /*
6281 * Got a job URI; parse it to get the job ID...
6282 */
6283
61cf44e2
MS
6284 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6285 sizeof(scheme), username, sizeof(username), host,
2e4ff8af
MS
6286 sizeof(host), &port, resource, sizeof(resource));
6287
6288 if (strncmp(resource, "/jobs/", 6))
6289 {
6290 /*
6291 * Not a valid URI!
6292 */
6293
84315f46 6294 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
2e4ff8af
MS
6295 uri->values[0].string.text);
6296 return;
6297 }
6298
6299 jobid = atoi(resource + 6);
6300 }
6301
6302 /*
6303 * See if the job exists...
6304 */
6305
6306 if ((job = cupsdFindJob(jobid)) == NULL)
6307 {
6308 /*
6309 * Nope - return a "not found" error...
6310 */
6311
84315f46 6312 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
2e4ff8af
MS
6313 return;
6314 }
6315
6316 /*
6317 * Check policy...
6318 */
6319
e60ec91f
MS
6320 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con,
6321 job->username)) != HTTP_OK)
2e4ff8af
MS
6322 {
6323 send_http_error(con, status, NULL);
6324 return;
6325 }
6326
6327 /*
6328 * Get the document number...
6329 */
6330
6331 if ((attr = ippFindAttribute(con->request, "document-number",
6332 IPP_TAG_INTEGER)) == NULL)
6333 {
6334 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 6335 _("Missing document-number attribute."));
2e4ff8af
MS
6336 return;
6337 }
6338
6339 if ((docnum = attr->values[0].integer) < 1 || docnum > job->num_files ||
6340 attr->num_values > 1)
6341 {
84315f46
MS
6342 send_ipp_status(con, IPP_NOT_FOUND,
6343 _("Document #%d does not exist in job #%d."), docnum,
6344 jobid);
2e4ff8af
MS
6345 return;
6346 }
6347
6348 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, jobid,
6349 docnum);
6350 if ((con->file = open(filename, O_RDONLY)) == -1)
6351 {
6352 cupsdLogMessage(CUPSD_LOG_ERROR,
6353 "Unable to open document %d in job %d - %s", docnum, jobid,
6354 strerror(errno));
6355 send_ipp_status(con, IPP_NOT_FOUND,
84315f46
MS
6356 _("Unable to open document #%d in job #%d."), docnum,
6357 jobid);
2e4ff8af
MS
6358 return;
6359 }
6360
6361 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
6362
6363 cupsdLoadJob(job);
6364
6365 snprintf(format, sizeof(format), "%s/%s", job->filetypes[docnum - 1]->super,
6366 job->filetypes[docnum - 1]->type);
6367
6368 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
6369 NULL, format);
6370 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "document-number",
6371 docnum);
6372 if ((attr = ippFindAttribute(job->attrs, "document-name",
6373 IPP_TAG_NAME)) != NULL)
6374 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "document-name",
6375 NULL, attr->values[0].string.text);
6376}
6377
6378
ef416fc2 6379/*
fa73b229 6380 * 'get_job_attrs()' - Get job attributes.
ef416fc2 6381 */
6382
6383static void
fa73b229 6384get_job_attrs(cupsd_client_t *con, /* I - Client connection */
6385 ipp_attribute_t *uri) /* I - Job URI */
ef416fc2 6386{
6387 http_status_t status; /* Policy status */
fa73b229 6388 ipp_attribute_t *attr; /* Current attribute */
6389 int jobid; /* Job ID */
6390 cupsd_job_t *job; /* Current job */
ef55b745 6391 cupsd_printer_t *printer; /* Current printer */
10d09e33
MS
6392 cupsd_policy_t *policy; /* Current security policy */
6393 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
ef416fc2 6394 username[HTTP_MAX_URI], /* Username portion of URI */
6395 host[HTTP_MAX_URI], /* Host portion of URI */
6396 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6397 int port; /* Port portion of URI */
10d09e33
MS
6398 cups_array_t *ra, /* Requested attributes array */
6399 *exclude; /* Private attributes array */
ef416fc2 6400
6401
fa73b229 6402 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con,
996acce8 6403 con->number, uri->values[0].string.text);
ef416fc2 6404
6405 /*
fa73b229 6406 * See if we have a job URI or a printer URI...
6407 */
6408
6409 if (!strcmp(uri->name, "printer-uri"))
6410 {
6411 /*
6412 * Got a printer URI; see if we also have a job-id attribute...
6413 */
6414
6415 if ((attr = ippFindAttribute(con->request, "job-id",
6416 IPP_TAG_INTEGER)) == NULL)
6417 {
6418 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 6419 _("Got a printer-uri attribute but no job-id."));
fa73b229 6420 return;
6421 }
6422
6423 jobid = attr->values[0].integer;
6424 }
6425 else
6426 {
6427 /*
6428 * Got a job URI; parse it to get the job ID...
6429 */
6430
61cf44e2
MS
6431 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6432 sizeof(scheme), username, sizeof(username), host,
a4d04587 6433 sizeof(host), &port, resource, sizeof(resource));
fa73b229 6434
6435 if (strncmp(resource, "/jobs/", 6))
6436 {
6437 /*
6438 * Not a valid URI!
6439 */
6440
84315f46 6441 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
fa73b229 6442 uri->values[0].string.text);
6443 return;
6444 }
6445
6446 jobid = atoi(resource + 6);
6447 }
6448
6449 /*
6450 * See if the job exists...
6451 */
6452
6453 if ((job = cupsdFindJob(jobid)) == NULL)
6454 {
6455 /*
6456 * Nope - return a "not found" error...
6457 */
6458
84315f46 6459 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
fa73b229 6460 return;
6461 }
6462
6463 /*
6464 * Check policy...
6465 */
6466
ef55b745
MS
6467 if ((printer = job->printer) == NULL)
6468 printer = cupsdFindDest(job->dest);
6469
6470 if (printer)
10d09e33
MS
6471 policy = printer->op_policy_ptr;
6472 else
6473 policy = DefaultPolicyPtr;
6474
6475 if ((status = cupsdCheckPolicy(policy, con, job->username)) != HTTP_OK)
fa73b229 6476 {
f899b121 6477 send_http_error(con, status, NULL);
fa73b229 6478 return;
6479 }
6480
10d09e33
MS
6481 exclude = cupsdGetPrivateAttrs(policy, con, printer, job->username);
6482
fa73b229 6483 /*
6484 * Copy attributes...
6485 */
6486
bd7854cb 6487 cupsdLoadJob(job);
6488
fa73b229 6489 ra = create_requested_array(con->request);
10d09e33 6490 copy_job_attrs(con, job, ra, exclude);
fa73b229 6491 cupsArrayDelete(ra);
6492
6493 con->response->request.status.status_code = IPP_OK;
6494}
6495
6496
6497/*
6498 * 'get_jobs()' - Get a list of jobs for the specified printer.
6499 */
6500
6501static void
6502get_jobs(cupsd_client_t *con, /* I - Client connection */
6503 ipp_attribute_t *uri) /* I - Printer URI */
6504{
6505 http_status_t status; /* Policy status */
6506 ipp_attribute_t *attr; /* Current attribute */
6507 const char *dest; /* Destination */
bc44d920 6508 cups_ptype_t dtype; /* Destination type (printer/class) */
fa73b229 6509 cups_ptype_t dmask; /* Destination type mask */
f7deaa1a 6510 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
fa73b229 6511 username[HTTP_MAX_URI], /* Username portion of URI */
6512 host[HTTP_MAX_URI], /* Host portion of URI */
6513 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6514 int port; /* Port portion of URI */
aaf19ab0
MS
6515 int job_comparison; /* Job comparison */
6516 ipp_jstate_t job_state; /* job-state value */
f16ea703
MS
6517 int first_job_id = 1, /* First job ID */
6518 first_index = 1, /* First index */
9e6d7a0f
MS
6519 limit = 0, /* Maximum number of jobs to return */
6520 count, /* Number of jobs that match */
6521 need_load_job = 0; /* Do we need to load the job? */
f16ea703 6522 const char *job_attr; /* Job attribute requested */
aaf19ab0 6523 ipp_attribute_t *job_ids; /* job-ids attribute */
fa73b229 6524 cupsd_job_t *job; /* Current job pointer */
6525 cupsd_printer_t *printer; /* Printer */
6526 cups_array_t *list; /* Which job list... */
eec1fbc3 6527 int delete_list = 0; /* Delete the list afterwards? */
10d09e33
MS
6528 cups_array_t *ra, /* Requested attributes array */
6529 *exclude; /* Private attributes array */
6530 cupsd_policy_t *policy; /* Current policy */
fa73b229 6531
6532
996acce8 6533 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->number,
fa73b229 6534 uri->values[0].string.text);
6535
6536 /*
6537 * Is the destination valid?
ef416fc2 6538 */
6539
4b3f67ff
MS
6540 if (strcmp(uri->name, "printer-uri"))
6541 {
84315f46 6542 send_ipp_status(con, IPP_BAD_REQUEST, _("No printer-uri in request."));
4b3f67ff
MS
6543 return;
6544 }
6545
f7deaa1a 6546 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6547 sizeof(scheme), username, sizeof(username), host,
a4d04587 6548 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 6549
b9faaae1 6550 if (!strcmp(resource, "/") || !strcmp(resource, "/jobs"))
ef416fc2 6551 {
6552 dest = NULL;
6553 dtype = (cups_ptype_t)0;
6554 dmask = (cups_ptype_t)0;
6555 printer = NULL;
6556 }
fa73b229 6557 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
ef416fc2 6558 {
6559 dest = NULL;
6560 dtype = (cups_ptype_t)0;
6561 dmask = CUPS_PRINTER_CLASS;
6562 printer = NULL;
6563 }
fa73b229 6564 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
ef416fc2 6565 {
6566 dest = NULL;
6567 dtype = CUPS_PRINTER_CLASS;
6568 dmask = CUPS_PRINTER_CLASS;
6569 printer = NULL;
6570 }
f7deaa1a 6571 else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype,
6572 &printer)) == NULL)
ef416fc2 6573 {
6574 /*
6575 * Bad URI...
6576 */
6577
6578 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 6579 _("The printer or class does not exist."));
ef416fc2 6580 return;
6581 }
6582 else
b86bc4cf 6583 {
6584 dtype &= CUPS_PRINTER_CLASS;
ef416fc2 6585 dmask = CUPS_PRINTER_CLASS;
b86bc4cf 6586 }
ef416fc2 6587
6588 /*
6589 * Check policy...
6590 */
6591
6592 if (printer)
10d09e33
MS
6593 policy = printer->op_policy_ptr;
6594 else
6595 policy = DefaultPolicyPtr;
6596
6597 if ((status = cupsdCheckPolicy(policy, con, NULL)) != HTTP_OK)
ef416fc2 6598 {
f899b121 6599 send_http_error(con, status, NULL);
ef416fc2 6600 return;
6601 }
6602
aaf19ab0
MS
6603 job_ids = ippFindAttribute(con->request, "job-ids", IPP_TAG_INTEGER);
6604
ef416fc2 6605 /*
6606 * See if the "which-jobs" attribute have been specified...
6607 */
6608
fa73b229 6609 if ((attr = ippFindAttribute(con->request, "which-jobs",
aaf19ab0
MS
6610 IPP_TAG_KEYWORD)) != NULL && job_ids)
6611 {
6612 send_ipp_status(con, IPP_CONFLICT,
6613 _("The %s attribute cannot be provided with job-ids."),
6614 "which-jobs");
6615 return;
6616 }
6617 else if (!attr || !strcmp(attr->values[0].string.text, "not-completed"))
6618 {
6619 job_comparison = -1;
6620 job_state = IPP_JOB_STOPPED;
e584c068 6621 list = ActiveJobs;
aaf19ab0
MS
6622 }
6623 else if (!strcmp(attr->values[0].string.text, "completed"))
6624 {
6625 job_comparison = 1;
6626 job_state = IPP_JOB_CANCELED;
eec1fbc3
MS
6627 list = cupsdGetCompletedJobs(printer);
6628 delete_list = 1;
aaf19ab0
MS
6629 }
6630 else if (!strcmp(attr->values[0].string.text, "aborted"))
6631 {
6632 job_comparison = 0;
6633 job_state = IPP_JOB_ABORTED;
eec1fbc3
MS
6634 list = cupsdGetCompletedJobs(printer);
6635 delete_list = 1;
aaf19ab0
MS
6636 }
6637 else if (!strcmp(attr->values[0].string.text, "all"))
6638 {
6639 job_comparison = 1;
6640 job_state = IPP_JOB_PENDING;
6641 list = Jobs;
6642 }
6643 else if (!strcmp(attr->values[0].string.text, "canceled"))
6644 {
6645 job_comparison = 0;
6646 job_state = IPP_JOB_CANCELED;
eec1fbc3
MS
6647 list = cupsdGetCompletedJobs(printer);
6648 delete_list = 1;
aaf19ab0
MS
6649 }
6650 else if (!strcmp(attr->values[0].string.text, "pending"))
ef416fc2 6651 {
aaf19ab0
MS
6652 job_comparison = 0;
6653 job_state = IPP_JOB_PENDING;
6654 list = ActiveJobs;
ef416fc2 6655 }
aaf19ab0 6656 else if (!strcmp(attr->values[0].string.text, "pending-held"))
ef416fc2 6657 {
aaf19ab0
MS
6658 job_comparison = 0;
6659 job_state = IPP_JOB_HELD;
6660 list = ActiveJobs;
ef416fc2 6661 }
aaf19ab0 6662 else if (!strcmp(attr->values[0].string.text, "processing"))
3dfe78b3 6663 {
aaf19ab0
MS
6664 job_comparison = 0;
6665 job_state = IPP_JOB_PROCESSING;
6666 list = PrintingJobs;
6667 }
6668 else if (!strcmp(attr->values[0].string.text, "processing-stopped"))
6669 {
6670 job_comparison = 0;
6671 job_state = IPP_JOB_STOPPED;
6672 list = ActiveJobs;
3dfe78b3 6673 }
ef416fc2 6674 else
6675 {
aaf19ab0
MS
6676 send_ipp_status(con, IPP_ATTRIBUTES,
6677 _("The which-jobs value \"%s\" is not supported."),
6678 attr->values[0].string.text);
6679 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
6680 "which-jobs", NULL, attr->values[0].string.text);
6681 return;
ef416fc2 6682 }
6683
6684 /*
6685 * See if they want to limit the number of jobs reported...
6686 */
6687
f16ea703 6688 if ((attr = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER)) != NULL)
aaf19ab0
MS
6689 {
6690 if (job_ids)
6691 {
6692 send_ipp_status(con, IPP_CONFLICT,
6693 _("The %s attribute cannot be provided with job-ids."),
6694 "limit");
6695 return;
6696 }
6697
ef416fc2 6698 limit = attr->values[0].integer;
aaf19ab0 6699 }
ef416fc2 6700
f16ea703
MS
6701 if ((attr = ippFindAttribute(con->request, "first-index", IPP_TAG_INTEGER)) != NULL)
6702 {
6703 if (job_ids)
6704 {
6705 send_ipp_status(con, IPP_CONFLICT,
6706 _("The %s attribute cannot be provided with job-ids."),
6707 "first-index");
6708 return;
6709 }
6710
6711 first_index = attr->values[0].integer;
6712 }
6713 else if ((attr = ippFindAttribute(con->request, "first-job-id", IPP_TAG_INTEGER)) != NULL)
aaf19ab0
MS
6714 {
6715 if (job_ids)
6716 {
6717 send_ipp_status(con, IPP_CONFLICT,
6718 _("The %s attribute cannot be provided with job-ids."),
6719 "first-job-id");
6720 return;
6721 }
6722
ef416fc2 6723 first_job_id = attr->values[0].integer;
aaf19ab0 6724 }
ef416fc2 6725
6726 /*
6727 * See if we only want to see jobs for a specific user...
6728 */
6729
f16ea703 6730 if ((attr = ippFindAttribute(con->request, "my-jobs", IPP_TAG_BOOLEAN)) != NULL && job_ids)
aaf19ab0
MS
6731 {
6732 send_ipp_status(con, IPP_CONFLICT,
6733 _("The %s attribute cannot be provided with job-ids."),
6734 "my-jobs");
6735 return;
6736 }
6737 else if (attr && attr->values[0].boolean)
e00b005a 6738 strlcpy(username, get_username(con), sizeof(username));
ef416fc2 6739 else
6740 username[0] = '\0';
6741
db8b865d 6742 ra = create_requested_array(con->request);
f16ea703
MS
6743 for (job_attr = (char *)cupsArrayFirst(ra); job_attr; job_attr = (char *)cupsArrayNext(ra))
6744 if (strcmp(job_attr, "job-id") &&
6745 strcmp(job_attr, "job-k-octets") &&
6746 strcmp(job_attr, "job-media-progress") &&
6747 strcmp(job_attr, "job-more-info") &&
6748 strcmp(job_attr, "job-name") &&
6749 strcmp(job_attr, "job-originating-user-name") &&
6750 strcmp(job_attr, "job-preserved") &&
6751 strcmp(job_attr, "job-printer-up-time") &&
6752 strcmp(job_attr, "job-printer-uri") &&
6753 strcmp(job_attr, "job-state") &&
6754 strcmp(job_attr, "job-state-reasons") &&
6755 strcmp(job_attr, "job-uri") &&
6756 strcmp(job_attr, "time-at-completed") &&
6757 strcmp(job_attr, "time-at-creation") &&
6758 strcmp(job_attr, "number-of-documents"))
6759 {
6760 need_load_job = 1;
6761 break;
6762 }
6763
6764 if (need_load_job && (limit == 0 || limit > 500) && (list == Jobs || delete_list))
6765 {
6766 /*
6767 * Limit expensive Get-Jobs for job history to 500 jobs...
6768 */
6769
6770 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "limit", 500);
6771
6772 if (limit)
6773 ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER, "limit", limit);
6774
6775 limit = 500;
6776
6777 cupsdLogClient(con, CUPSD_LOG_INFO, "Limiting Get-Jobs response to %d jobs.", limit);
6778 }
ef416fc2 6779
6780 /*
6781 * OK, build a list of jobs for this printer...
6782 */
6783
aaf19ab0 6784 if (job_ids)
ef416fc2 6785 {
aaf19ab0 6786 int i; /* Looping var */
ef416fc2 6787
aaf19ab0
MS
6788 for (i = 0; i < job_ids->num_values; i ++)
6789 {
4220952d 6790 if (!cupsdFindJob(job_ids->values[i].integer))
aaf19ab0
MS
6791 break;
6792 }
ef416fc2 6793
aaf19ab0
MS
6794 if (i < job_ids->num_values)
6795 {
84315f46 6796 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
aaf19ab0
MS
6797 job_ids->values[i].integer);
6798 return;
6799 }
0a682745 6800
aaf19ab0
MS
6801 for (i = 0; i < job_ids->num_values; i ++)
6802 {
6803 job = cupsdFindJob(job_ids->values[i].integer);
0a682745 6804
f16ea703 6805 if (need_load_job && !job->attrs)
aaf19ab0 6806 {
f16ea703
MS
6807 cupsdLoadJob(job);
6808
6809 if (!job->attrs)
6810 {
6811 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d", job->id);
6812 continue;
6813 }
aaf19ab0 6814 }
ef416fc2 6815
aaf19ab0
MS
6816 if (i > 0)
6817 ippAddSeparator(con->response);
bd7854cb 6818
10d09e33
MS
6819 exclude = cupsdGetPrivateAttrs(job->printer ?
6820 job->printer->op_policy_ptr :
6821 policy, con, job->printer,
6822 job->username);
6823
6824 copy_job_attrs(con, job, ra, exclude);
d1c13e16 6825 }
aaf19ab0
MS
6826 }
6827 else
6828 {
9e6d7a0f
MS
6829 if (first_index > 1)
6830 job = (cupsd_job_t *)cupsArrayIndex(list, first_index - 1);
6831 else
6832 job = (cupsd_job_t *)cupsArrayFirst(list);
6833
6834 for (count = 0; (limit <= 0 || count < limit) && job; job = (cupsd_job_t *)cupsArrayNext(list))
aaf19ab0
MS
6835 {
6836 /*
6837 * Filter out jobs that don't match...
6838 */
bd7854cb 6839
aaf19ab0
MS
6840 cupsdLogMessage(CUPSD_LOG_DEBUG2,
6841 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
6842 "state_value=%d, attrs=%p", job->id, job->dest,
6843 job->username, job->state_value, job->attrs);
7594b224 6844
aaf19ab0
MS
6845 if (!job->dest || !job->username)
6846 cupsdLoadJob(job);
fa73b229 6847
aaf19ab0
MS
6848 if (!job->dest || !job->username)
6849 continue;
ef416fc2 6850
aaf19ab0
MS
6851 if ((dest && strcmp(job->dest, dest)) &&
6852 (!job->printer || !dest || strcmp(job->printer->name, dest)))
6853 continue;
6854 if ((job->dtype & dmask) != dtype &&
6855 (!job->printer || (job->printer->type & dmask) != dtype))
6856 continue;
ef416fc2 6857
aaf19ab0
MS
6858 if ((job_comparison < 0 && job->state_value > job_state) ||
6859 (job_comparison == 0 && job->state_value != job_state) ||
6860 (job_comparison > 0 && job->state_value < job_state))
6861 continue;
6862
6863 if (job->id < first_job_id)
6864 continue;
6865
f16ea703 6866 if (need_load_job && !job->attrs)
aaf19ab0 6867 {
f16ea703
MS
6868 cupsdLoadJob(job);
6869
6870 if (!job->attrs)
6871 {
6872 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d", job->id);
6873 continue;
6874 }
aaf19ab0
MS
6875 }
6876
88f9aafc 6877 if (username[0] && _cups_strcasecmp(username, job->username))
aaf19ab0
MS
6878 continue;
6879
6880 if (count > 0)
6881 ippAddSeparator(con->response);
6882
6883 count ++;
6884
10d09e33
MS
6885 exclude = cupsdGetPrivateAttrs(job->printer ?
6886 job->printer->op_policy_ptr :
6887 policy, con, job->printer,
6888 job->username);
6889
6890 copy_job_attrs(con, job, ra, exclude);
aaf19ab0
MS
6891 }
6892
6893 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count=%d", count);
6894 }
d1c13e16 6895
fa73b229 6896 cupsArrayDelete(ra);
ef416fc2 6897
eec1fbc3
MS
6898 if (delete_list)
6899 cupsArrayDelete(list);
6900
fa73b229 6901 con->response->request.status.status_code = IPP_OK;
ef416fc2 6902}
6903
6904
6905/*
6906 * 'get_notifications()' - Get events for a subscription.
6907 */
6908
6909static void
bd7854cb 6910get_notifications(cupsd_client_t *con) /* I - Client connection */
ef416fc2 6911{
bd7854cb 6912 int i, j; /* Looping vars */
6913 http_status_t status; /* Policy status */
6914 cupsd_subscription_t *sub; /* Subscription */
6915 ipp_attribute_t *ids, /* notify-subscription-ids */
6916 *sequences; /* notify-sequence-numbers */
6917 int min_seq; /* Minimum sequence number */
6918 int interval; /* Poll interval */
6919
6920
f7deaa1a 6921 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])",
996acce8 6922 con, con->number);
bd7854cb 6923
6924 /*
6925 * Get subscription attributes...
6926 */
6927
6928 ids = ippFindAttribute(con->request, "notify-subscription-ids",
6929 IPP_TAG_INTEGER);
6930 sequences = ippFindAttribute(con->request, "notify-sequence-numbers",
6931 IPP_TAG_INTEGER);
6932
6933 if (!ids)
6934 {
6935 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 6936 _("Missing notify-subscription-ids attribute."));
bd7854cb 6937 return;
6938 }
6939
6940 /*
6941 * Are the subscription IDs valid?
6942 */
6943
6944 for (i = 0, interval = 60; i < ids->num_values; i ++)
6945 {
6946 if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL)
6947 {
6948 /*
6949 * Bad subscription ID...
6950 */
6951
84315f46 6952 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
bd7854cb 6953 ids->values[i].integer);
6954 return;
6955 }
6956
6957 /*
6958 * Check policy...
6959 */
6960
6961 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
6962 DefaultPolicyPtr,
6963 con, sub->owner)) != HTTP_OK)
6964 {
f899b121 6965 send_http_error(con, status, sub->dest);
bd7854cb 6966 return;
6967 }
6968
6969 /*
6970 * Check the subscription type and update the interval accordingly.
6971 */
6972
6973 if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING &&
6974 interval > 10)
6975 interval = 10;
6976 else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED)
6977 interval = 0;
6978 else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING &&
6979 interval > 30)
6980 interval = 30;
6981 }
6982
6983 /*
6984 * Tell the client to poll again in N seconds...
6985 */
6986
6987 if (interval > 0)
6988 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
6989 "notify-get-interval", interval);
6990
6991 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
6992 "printer-up-time", time(NULL));
6993
6994 /*
6995 * Copy the subscription event attributes to the response.
6996 */
6997
6998 con->response->request.status.status_code =
6999 interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE;
7000
7001 for (i = 0; i < ids->num_values; i ++)
7002 {
7003 /*
7004 * Get the subscription and sequence number...
7005 */
7006
7007 sub = cupsdFindSubscription(ids->values[i].integer);
7008
7009 if (sequences && i < sequences->num_values)
7010 min_seq = sequences->values[i].integer;
7011 else
7012 min_seq = 1;
7013
7014 /*
7015 * If we don't have any new events, nothing to do here...
7016 */
7017
10d09e33 7018 if (min_seq > (sub->first_event_id + cupsArrayCount(sub->events)))
bd7854cb 7019 continue;
7020
7021 /*
7022 * Otherwise copy all of the new events...
7023 */
7024
7025 if (sub->first_event_id > min_seq)
7026 j = 0;
7027 else
7028 j = min_seq - sub->first_event_id;
7029
10d09e33 7030 for (; j < cupsArrayCount(sub->events); j ++)
b423cd4c 7031 {
7032 ippAddSeparator(con->response);
ef416fc2 7033
10d09e33
MS
7034 copy_attrs(con->response,
7035 ((cupsd_event_t *)cupsArrayIndex(sub->events, j))->attrs, NULL,
7036 IPP_TAG_EVENT_NOTIFICATION, 0, NULL);
b423cd4c 7037 }
7038 }
7039}
ef416fc2 7040
ef416fc2 7041
b94498cf 7042/*
7043 * 'get_ppd()' - Get a named PPD from the local system.
7044 */
7045
7046static void
7047get_ppd(cupsd_client_t *con, /* I - Client connection */
7048 ipp_attribute_t *uri) /* I - Printer URI or PPD name */
7049{
7050 http_status_t status; /* Policy status */
7051 cupsd_printer_t *dest; /* Destination */
7052 cups_ptype_t dtype; /* Destination type */
7053
7054
7055 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con,
996acce8 7056 con->number, uri, uri->name, uri->values[0].string.text);
b94498cf 7057
e67e2f9e 7058 if (!strcmp(ippGetName(uri), "ppd-name"))
b94498cf 7059 {
7060 /*
7061 * Return a PPD file from cups-driverd...
7062 */
7063
e67e2f9e
MS
7064 const char *ppd_name = ippGetString(uri, 0, NULL);
7065 /* ppd-name value */
7066 char command[1024], /* cups-driverd command */
7067 options[1024], /* Options to pass to command */
7068 oppd_name[1024]; /* Escaped ppd-name */
b94498cf 7069
7070 /*
7071 * Check policy...
7072 */
7073
7074 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7075 {
7076 send_http_error(con, status, NULL);
7077 return;
7078 }
7079
e67e2f9e
MS
7080 /*
7081 * Check ppd-name value...
7082 */
7083
7084 if (strstr(ppd_name, "../"))
7085 {
7086 send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Invalid ppd-name value."));
7087 return;
7088 }
7089
b94498cf 7090 /*
7091 * Run cups-driverd command with the given options...
7092 */
7093
7094 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
e67e2f9e
MS
7095 url_encode_string(ppd_name, oppd_name, sizeof(oppd_name));
7096 snprintf(options, sizeof(options), "get+%d+%s", ippGetRequestId(con->request), oppd_name);
b94498cf 7097
7098 if (cupsdSendCommand(con, command, options, 0))
7099 {
7100 /*
7101 * Command started successfully, don't send an IPP response here...
7102 */
7103
7104 ippDelete(con->response);
7105 con->response = NULL;
7106 }
7107 else
7108 {
7109 /*
7110 * Command failed, return "internal error" so the user knows something
7111 * went wrong...
7112 */
7113
e67e2f9e 7114 send_ipp_status(con, IPP_INTERNAL_ERROR, _("cups-driverd failed to execute."));
b94498cf 7115 }
7116 }
e67e2f9e 7117 else if (!strcmp(ippGetName(uri), "printer-uri") && cupsdValidateDest(ippGetString(uri, 0, NULL), &dtype, &dest))
b94498cf 7118 {
7119 int i; /* Looping var */
7120 char filename[1024]; /* PPD filename */
7121
b94498cf 7122 /*
7123 * Check policy...
7124 */
7125
7126 if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK)
7127 {
2fb76298 7128 send_http_error(con, status, dest);
b94498cf 7129 return;
7130 }
7131
7132 /*
7133 * See if we need the PPD for a class or remote printer...
7134 */
7135
e67e2f9e 7136 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, dest->name);
bc44d920 7137
7138 if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0))
b94498cf 7139 {
e67e2f9e
MS
7140 send_ipp_status(con, IPP_STATUS_CUPS_SEE_OTHER, _("See remote printer."));
7141 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, dest->uri);
b94498cf 7142 return;
7143 }
a2326b5b 7144 else if (dtype & CUPS_PRINTER_CLASS)
b94498cf 7145 {
7146 for (i = 0; i < dest->num_printers; i ++)
a2326b5b 7147 if (!(dest->printers[i]->type & CUPS_PRINTER_CLASS))
bc44d920 7148 {
e67e2f9e 7149 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, dest->printers[i]->name);
bc44d920 7150
7151 if (!access(filename, 0))
7152 break;
7153 }
b94498cf 7154
7155 if (i < dest->num_printers)
7156 dest = dest->printers[i];
7157 else
7158 {
e67e2f9e
MS
7159 send_ipp_status(con, IPP_STATUS_CUPS_SEE_OTHER, _("See remote printer."));
7160 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, dest->printers[0]->uri);
b94498cf 7161 return;
7162 }
7163 }
7164
7165 /*
7166 * Found the printer with the PPD file, now see if there is one...
7167 */
7168
b94498cf 7169 if ((con->file = open(filename, O_RDONLY)) < 0)
7170 {
e67e2f9e 7171 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 7172 return;
7173 }
7174
7175 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
7176
7177 con->pipe_pid = 0;
7178
e67e2f9e 7179 ippSetStatusCode(con->response, IPP_STATUS_OK);
b94498cf 7180 }
7181 else
e67e2f9e 7182 send_ipp_status(con, IPP_STATUS_ERROR_NOT_FOUND, _("The PPD file \"%s\" could not be found."), ippGetString(uri, 0, NULL));
b94498cf 7183}
7184
7185
b423cd4c 7186/*
7187 * 'get_ppds()' - Get the list of PPD files on the local system.
7188 */
ef416fc2 7189
b423cd4c 7190static void
7191get_ppds(cupsd_client_t *con) /* I - Client connection */
7192{
7193 http_status_t status; /* Policy status */
b423cd4c 7194 ipp_attribute_t *limit, /* Limit attribute */
b94498cf 7195 *device, /* ppd-device-id attribute */
7196 *language, /* ppd-natural-language attribute */
b423cd4c 7197 *make, /* ppd-make attribute */
b94498cf 7198 *model, /* ppd-make-and-model attribute */
3d8365b8 7199 *model_number, /* ppd-model-number attribute */
b94498cf 7200 *product, /* ppd-product attribute */
7201 *psversion, /* ppd-psverion attribute */
3d8365b8 7202 *type, /* ppd-type attribute */
ed6e7faf
MS
7203 *requested, /* requested-attributes attribute */
7204 *exclude, /* exclude-schemes attribute */
7205 *include; /* include-schemes attribute */
b94498cf 7206 char command[1024], /* cups-driverd command */
ed6e7faf 7207 options[4096], /* Options to pass to command */
b94498cf 7208 device_str[256],/* Escaped ppd-device-id string */
7209 language_str[256],
bc44d920 7210 /* Escaped ppd-natural-language */
b94498cf 7211 make_str[256], /* Escaped ppd-make string */
7212 model_str[256], /* Escaped ppd-make-and-model string */
3d8365b8 7213 model_number_str[256],
7214 /* ppd-model-number string */
b94498cf 7215 product_str[256],
7216 /* Escaped ppd-product string */
7217 psversion_str[256],
7218 /* Escaped ppd-psversion string */
3d8365b8 7219 type_str[256], /* Escaped ppd-type string */
ed6e7faf 7220 requested_str[256],
89d46774 7221 /* String for requested attributes */
ed6e7faf
MS
7222 exclude_str[512],
7223 /* String for excluded schemes */
7224 include_str[512];
7225 /* String for included schemes */
ef416fc2 7226
ef416fc2 7227
996acce8 7228 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->number);
ef416fc2 7229
7230 /*
b423cd4c 7231 * Check policy...
ef416fc2 7232 */
7233
b423cd4c 7234 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
ef416fc2 7235 {
f899b121 7236 send_http_error(con, status, NULL);
b423cd4c 7237 return;
ef416fc2 7238 }
ef416fc2 7239
b423cd4c 7240 /*
7241 * Run cups-driverd command with the given options...
7242 */
7243
3d8365b8 7244 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
7245 device = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT);
7246 language = ippFindAttribute(con->request, "ppd-natural-language",
7247 IPP_TAG_LANGUAGE);
7248 make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT);
7249 model = ippFindAttribute(con->request, "ppd-make-and-model",
7250 IPP_TAG_TEXT);
7251 model_number = ippFindAttribute(con->request, "ppd-model-number",
7252 IPP_TAG_INTEGER);
7253 product = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT);
7254 psversion = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT);
7255 type = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD);
7256 requested = ippFindAttribute(con->request, "requested-attributes",
7257 IPP_TAG_KEYWORD);
8b450588
MS
7258 exclude = ippFindAttribute(con->request, "exclude-schemes",
7259 IPP_TAG_NAME);
7260 include = ippFindAttribute(con->request, "include-schemes",
7261 IPP_TAG_NAME);
b423cd4c 7262
7263 if (requested)
89d46774 7264 url_encode_attr(requested, requested_str, sizeof(requested_str));
7265 else
7266 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
ef416fc2 7267
b94498cf 7268 if (device)
7269 url_encode_attr(device, device_str, sizeof(device_str));
7270 else
7271 device_str[0] = '\0';
7272
7273 if (language)
7274 url_encode_attr(language, language_str, sizeof(language_str));
7275 else
7276 language_str[0] = '\0';
7277
89d46774 7278 if (make)
7279 url_encode_attr(make, make_str, sizeof(make_str));
b423cd4c 7280 else
89d46774 7281 make_str[0] = '\0';
fa73b229 7282
b94498cf 7283 if (model)
7284 url_encode_attr(model, model_str, sizeof(model_str));
7285 else
7286 model_str[0] = '\0';
7287
3d8365b8 7288 if (model_number)
7289 snprintf(model_number_str, sizeof(model_number_str), "ppd-model-number=%d",
7290 model_number->values[0].integer);
7291 else
7292 model_number_str[0] = '\0';
7293
b94498cf 7294 if (product)
7295 url_encode_attr(product, product_str, sizeof(product_str));
7296 else
7297 product_str[0] = '\0';
7298
7299 if (psversion)
7300 url_encode_attr(psversion, psversion_str, sizeof(psversion_str));
7301 else
7302 psversion_str[0] = '\0';
7303
3d8365b8 7304 if (type)
7305 url_encode_attr(type, type_str, sizeof(type_str));
7306 else
7307 type_str[0] = '\0';
7308
ed6e7faf
MS
7309 if (exclude)
7310 url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
7311 else
7312 exclude_str[0] = '\0';
7313
7314 if (include)
7315 url_encode_attr(include, include_str, sizeof(include_str));
7316 else
7317 include_str[0] = '\0';
7318
b423cd4c 7319 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
3d8365b8 7320 snprintf(options, sizeof(options),
ed6e7faf 7321 "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 7322 con->request->request.op.request_id,
7323 limit ? limit->values[0].integer : 0,
b94498cf 7324 requested_str,
7325 device ? "%20" : "", device_str,
7326 language ? "%20" : "", language_str,
7327 make ? "%20" : "", make_str,
7328 model ? "%20" : "", model_str,
3d8365b8 7329 model_number ? "%20" : "", model_number_str,
b94498cf 7330 product ? "%20" : "", product_str,
3d8365b8 7331 psversion ? "%20" : "", psversion_str,
ed6e7faf
MS
7332 type ? "%20" : "", type_str,
7333 exclude_str[0] ? "%20" : "", exclude_str,
7334 include_str[0] ? "%20" : "", include_str);
ef416fc2 7335
b423cd4c 7336 if (cupsdSendCommand(con, command, options, 0))
7337 {
7338 /*
7339 * Command started successfully, don't send an IPP response here...
7340 */
7341
7342 ippDelete(con->response);
7343 con->response = NULL;
7344 }
7345 else
7346 {
7347 /*
7348 * Command failed, return "internal error" so the user knows something
7349 * went wrong...
7350 */
7351
7352 send_ipp_status(con, IPP_INTERNAL_ERROR,
7353 _("cups-driverd failed to execute."));
7354 }
ef416fc2 7355}
7356
7357
7358/*
b423cd4c 7359 * 'get_printer_attrs()' - Get printer attributes.
ef416fc2 7360 */
7361
7362static void
b423cd4c 7363get_printer_attrs(cupsd_client_t *con, /* I - Client connection */
7364 ipp_attribute_t *uri) /* I - Printer URI */
ef416fc2 7365{
7366 http_status_t status; /* Policy status */
bc44d920 7367 cups_ptype_t dtype; /* Destination type (printer/class) */
b423cd4c 7368 cupsd_printer_t *printer; /* Printer/class */
ef416fc2 7369 cups_array_t *ra; /* Requested attributes array */
7370
7371
b423cd4c 7372 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con,
996acce8 7373 con->number, uri->values[0].string.text);
ef416fc2 7374
7375 /*
b423cd4c 7376 * Is the destination valid?
ef416fc2 7377 */
7378
f7deaa1a 7379 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 7380 {
7381 /*
b423cd4c 7382 * Bad URI...
ef416fc2 7383 */
7384
7385 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 7386 _("The printer or class does not exist."));
ef416fc2 7387 return;
7388 }
7389
7390 /*
7391 * Check policy...
7392 */
7393
b423cd4c 7394 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
ef416fc2 7395 {
f899b121 7396 send_http_error(con, status, printer);
ef416fc2 7397 return;
7398 }
7399
7400 /*
b423cd4c 7401 * Send the attributes...
ef416fc2 7402 */
7403
7404 ra = create_requested_array(con->request);
7405
b423cd4c 7406 copy_printer_attrs(con, printer, ra);
ef416fc2 7407
7408 cupsArrayDelete(ra);
7409
7410 con->response->request.status.status_code = IPP_OK;
7411}
7412
7413
c168a833
MS
7414/*
7415 * 'get_printer_supported()' - Get printer supported values.
7416 */
7417
7418static void
7419get_printer_supported(
7420 cupsd_client_t *con, /* I - Client connection */
7421 ipp_attribute_t *uri) /* I - Printer URI */
7422{
7423 http_status_t status; /* Policy status */
7424 cups_ptype_t dtype; /* Destination type (printer/class) */
7425 cupsd_printer_t *printer; /* Printer/class */
7426
7427
7428 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_supported(%p[%d], %s)", con,
996acce8 7429 con->number, uri->values[0].string.text);
c168a833
MS
7430
7431 /*
7432 * Is the destination valid?
7433 */
7434
7435 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7436 {
7437 /*
7438 * Bad URI...
7439 */
7440
7441 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 7442 _("The printer or class does not exist."));
c168a833
MS
7443 return;
7444 }
7445
7446 /*
7447 * Check policy...
7448 */
7449
7450 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
7451 {
7452 send_http_error(con, status, printer);
7453 return;
7454 }
7455
7456 /*
7457 * Return a list of attributes that can be set via Set-Printer-Attributes.
7458 */
7459
9b4bd602
MS
7460 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7461 "printer-geo-location", 0);
c168a833
MS
7462 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7463 "printer-info", 0);
7464 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7465 "printer-location", 0);
9b4bd602
MS
7466 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7467 "printer-organization", 0);
7468 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7469 "printer-organizational-unit", 0);
c168a833
MS
7470
7471 con->response->request.status.status_code = IPP_OK;
7472}
7473
7474
ef416fc2 7475/*
b423cd4c 7476 * 'get_printers()' - Get a list of printers or classes.
ef416fc2 7477 */
7478
7479static void
b423cd4c 7480get_printers(cupsd_client_t *con, /* I - Client connection */
7481 int type) /* I - 0 or CUPS_PRINTER_CLASS */
ef416fc2 7482{
b423cd4c 7483 http_status_t status; /* Policy status */
7484 ipp_attribute_t *attr; /* Current attribute */
bc44d920 7485 int limit; /* Max number of printers to return */
b423cd4c 7486 int count; /* Number of printers that match */
7487 cupsd_printer_t *printer; /* Current printer pointer */
7e86f2f6 7488 cups_ptype_t printer_type, /* printer-type attribute */
b423cd4c 7489 printer_mask; /* printer-type-mask attribute */
7490 char *location; /* Location string */
7491 const char *username; /* Current user */
7492 char *first_printer_name; /* first-printer-name attribute */
7493 cups_array_t *ra; /* Requested attributes array */
e07d4801 7494 int local; /* Local connection? */
ef416fc2 7495
ef416fc2 7496
b423cd4c 7497 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con,
996acce8 7498 con->number, type);
ef416fc2 7499
7500 /*
7501 * Check policy...
7502 */
7503
b423cd4c 7504 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
ef416fc2 7505 {
f899b121 7506 send_http_error(con, status, NULL);
ef416fc2 7507 return;
7508 }
7509
7510 /*
b423cd4c 7511 * Check for printers...
ef416fc2 7512 */
7513
b423cd4c 7514 if (!Printers || !cupsArrayCount(Printers))
7515 {
7516 send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added."));
7517 return;
7518 }
7519
7520 /*
7521 * See if they want to limit the number of printers reported...
7522 */
ef416fc2 7523
fa73b229 7524 if ((attr = ippFindAttribute(con->request, "limit",
7525 IPP_TAG_INTEGER)) != NULL)
ef416fc2 7526 limit = attr->values[0].integer;
7527 else
b423cd4c 7528 limit = 10000000;
7529
7530 if ((attr = ippFindAttribute(con->request, "first-printer-name",
7531 IPP_TAG_NAME)) != NULL)
7532 first_printer_name = attr->values[0].string.text;
7533 else
7534 first_printer_name = NULL;
ef416fc2 7535
7536 /*
b423cd4c 7537 * Support filtering...
ef416fc2 7538 */
7539
b423cd4c 7540 if ((attr = ippFindAttribute(con->request, "printer-type",
7541 IPP_TAG_ENUM)) != NULL)
7e86f2f6 7542 printer_type = (cups_ptype_t)attr->values[0].integer;
ef416fc2 7543 else
7e86f2f6 7544 printer_type = (cups_ptype_t)0;
ef416fc2 7545
b423cd4c 7546 if ((attr = ippFindAttribute(con->request, "printer-type-mask",
7547 IPP_TAG_ENUM)) != NULL)
7e86f2f6 7548 printer_mask = (cups_ptype_t)attr->values[0].integer;
ef416fc2 7549 else
7e86f2f6 7550 printer_mask = (cups_ptype_t)0;
e00b005a 7551
e07d4801
MS
7552 local = httpAddrLocalhost(&(con->clientaddr));
7553
b423cd4c 7554 if ((attr = ippFindAttribute(con->request, "printer-location",
7555 IPP_TAG_TEXT)) != NULL)
7556 location = attr->values[0].string.text;
7557 else
7558 location = NULL;
e00b005a 7559
7560 if (con->username[0])
b423cd4c 7561 username = con->username;
e00b005a 7562 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7563 IPP_TAG_NAME)) != NULL)
b423cd4c 7564 username = attr->values[0].string.text;
e00b005a 7565 else
b423cd4c 7566 username = NULL;
ef416fc2 7567
b423cd4c 7568 ra = create_requested_array(con->request);
ef416fc2 7569
7570 /*
b423cd4c 7571 * OK, build a list of printers for this printer...
ef416fc2 7572 */
7573
b423cd4c 7574 if (first_printer_name)
ef416fc2 7575 {
b423cd4c 7576 if ((printer = cupsdFindDest(first_printer_name)) == NULL)
7577 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
ef416fc2 7578 }
7579 else
b423cd4c 7580 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
ef416fc2 7581
b423cd4c 7582 for (count = 0;
7583 count < limit && printer;
7584 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
7585 {
e07d4801
MS
7586 if (!local && !printer->shared)
7587 continue;
7588
b423cd4c 7589 if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) &&
7590 (printer->type & printer_mask) == printer_type &&
b19ccc9e 7591 (!location ||
88f9aafc 7592 (printer->location && !_cups_strcasecmp(printer->location, location))))
ef416fc2 7593 {
b423cd4c 7594 /*
7595 * If a username is specified, see if it is allowed or denied
7596 * access...
7597 */
ef416fc2 7598
10d09e33
MS
7599 if (cupsArrayCount(printer->users) && username &&
7600 !user_allowed(printer, username))
b423cd4c 7601 continue;
ef416fc2 7602
b423cd4c 7603 /*
7604 * Add the group separator as needed...
7605 */
ef416fc2 7606
b423cd4c 7607 if (count > 0)
7608 ippAddSeparator(con->response);
ef416fc2 7609
b423cd4c 7610 count ++;
ef416fc2 7611
b423cd4c 7612 /*
7613 * Send the attributes...
7614 */
ef416fc2 7615
b423cd4c 7616 copy_printer_attrs(con, printer, ra);
7617 }
ef416fc2 7618 }
7619
b423cd4c 7620 cupsArrayDelete(ra);
ef416fc2 7621
7622 con->response->request.status.status_code = IPP_OK;
7623}
7624
7625
7626/*
b423cd4c 7627 * 'get_subscription_attrs()' - Get subscription attributes.
ef416fc2 7628 */
7629
7630static void
b423cd4c 7631get_subscription_attrs(
7632 cupsd_client_t *con, /* I - Client connection */
7633 int sub_id) /* I - Subscription ID */
ef416fc2 7634{
b423cd4c 7635 http_status_t status; /* Policy status */
7636 cupsd_subscription_t *sub; /* Subscription */
10d09e33
MS
7637 cupsd_policy_t *policy; /* Current security policy */
7638 cups_array_t *ra, /* Requested attributes array */
7639 *exclude; /* Private attributes array */
ef416fc2 7640
7641
b423cd4c 7642 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7643 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
996acce8 7644 con, con->number, sub_id);
ef416fc2 7645
79d3cd17
MS
7646 /*
7647 * Expire subscriptions as needed...
7648 */
7649
7650 cupsdExpireSubscriptions(NULL, NULL);
7651
fa73b229 7652 /*
b423cd4c 7653 * Is the subscription ID valid?
fa73b229 7654 */
7655
b423cd4c 7656 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
fa73b229 7657 {
7658 /*
b423cd4c 7659 * Bad subscription ID...
fa73b229 7660 */
7661
84315f46
MS
7662 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
7663 sub_id);
fa73b229 7664 return;
7665 }
7666
7667 /*
7668 * Check policy...
7669 */
7670
10d09e33
MS
7671 if (sub->dest)
7672 policy = sub->dest->op_policy_ptr;
7673 else
7674 policy = DefaultPolicyPtr;
7675
7676 if ((status = cupsdCheckPolicy(policy, con, sub->owner)) != HTTP_OK)
fa73b229 7677 {
f899b121 7678 send_http_error(con, status, sub->dest);
fa73b229 7679 return;
7680 }
7681
10d09e33
MS
7682 exclude = cupsdGetPrivateAttrs(policy, con, sub->dest, sub->owner);
7683
ef416fc2 7684 /*
b423cd4c 7685 * Copy the subscription attributes to the response using the
7686 * requested-attributes attribute that may be provided by the client.
ef416fc2 7687 */
7688
b423cd4c 7689 ra = create_requested_array(con->request);
fa73b229 7690
10d09e33 7691 copy_subscription_attrs(con, sub, ra, exclude);
ef416fc2 7692
b423cd4c 7693 cupsArrayDelete(ra);
fa73b229 7694
b423cd4c 7695 con->response->request.status.status_code = IPP_OK;
7696}
fa73b229 7697
fa73b229 7698
b423cd4c 7699/*
7700 * 'get_subscriptions()' - Get subscriptions.
7701 */
ef416fc2 7702
b423cd4c 7703static void
7704get_subscriptions(cupsd_client_t *con, /* I - Client connection */
7705 ipp_attribute_t *uri) /* I - Printer/job URI */
7706{
7707 http_status_t status; /* Policy status */
7708 int count; /* Number of subscriptions */
7709 int limit; /* Limit */
7710 cupsd_subscription_t *sub; /* Subscription */
7711 cups_array_t *ra; /* Requested attributes array */
7712 ipp_attribute_t *attr; /* Attribute */
bc44d920 7713 cups_ptype_t dtype; /* Destination type (printer/class) */
f7deaa1a 7714 char scheme[HTTP_MAX_URI],
7715 /* Scheme portion of URI */
b423cd4c 7716 username[HTTP_MAX_URI],
7717 /* Username portion of URI */
7718 host[HTTP_MAX_URI],
7719 /* Host portion of URI */
7720 resource[HTTP_MAX_URI];
7721 /* Resource portion of URI */
7722 int port; /* Port portion of URI */
7723 cupsd_job_t *job; /* Job pointer */
7724 cupsd_printer_t *printer; /* Printer */
10d09e33
MS
7725 cupsd_policy_t *policy; /* Policy */
7726 cups_array_t *exclude; /* Private attributes array */
fa73b229 7727
fa73b229 7728
b423cd4c 7729 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7730 "get_subscriptions(con=%p[%d], uri=%s)",
996acce8 7731 con, con->number, uri->values[0].string.text);
b423cd4c 7732
7733 /*
7734 * Is the destination valid?
7735 */
7736
f7deaa1a 7737 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7738 sizeof(scheme), username, sizeof(username), host,
b423cd4c 7739 sizeof(host), &port, resource, sizeof(resource));
7740
7741 if (!strcmp(resource, "/") ||
7742 (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) ||
7743 (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) ||
7744 (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9))
7745 {
7746 printer = NULL;
7747 job = NULL;
ef416fc2 7748 }
b423cd4c 7749 else if (!strncmp(resource, "/jobs/", 6) && resource[6])
ef416fc2 7750 {
b423cd4c 7751 printer = NULL;
7752 job = cupsdFindJob(atoi(resource + 6));
ef416fc2 7753
b423cd4c 7754 if (!job)
ef416fc2 7755 {
84315f46
MS
7756 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
7757 atoi(resource + 6));
ef416fc2 7758 return;
7759 }
b423cd4c 7760 }
f7deaa1a 7761 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
b423cd4c 7762 {
fa73b229 7763 /*
b423cd4c 7764 * Bad URI...
fa73b229 7765 */
7766
b423cd4c 7767 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 7768 _("The printer or class does not exist."));
b423cd4c 7769 return;
7770 }
7771 else if ((attr = ippFindAttribute(con->request, "notify-job-id",
7772 IPP_TAG_INTEGER)) != NULL)
7773 {
7774 job = cupsdFindJob(attr->values[0].integer);
ef416fc2 7775
b423cd4c 7776 if (!job)
fa73b229 7777 {
84315f46 7778 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
b423cd4c 7779 attr->values[0].integer);
fa73b229 7780 return;
7781 }
ef416fc2 7782 }
b423cd4c 7783 else
7784 job = NULL;
ef416fc2 7785
7786 /*
b423cd4c 7787 * Check policy...
ef416fc2 7788 */
7789
10d09e33
MS
7790 if (printer)
7791 policy = printer->op_policy_ptr;
7792 else
7793 policy = DefaultPolicyPtr;
7794
7795 if ((status = cupsdCheckPolicy(policy, con, NULL)) != HTTP_OK)
ef416fc2 7796 {
f899b121 7797 send_http_error(con, status, printer);
b423cd4c 7798 return;
7799 }
ef416fc2 7800
79d3cd17
MS
7801 /*
7802 * Expire subscriptions as needed...
7803 */
7804
7805 cupsdExpireSubscriptions(NULL, NULL);
7806
b423cd4c 7807 /*
7808 * Copy the subscription attributes to the response using the
7809 * requested-attributes attribute that may be provided by the client.
7810 */
ef416fc2 7811
b423cd4c 7812 ra = create_requested_array(con->request);
ef416fc2 7813
b423cd4c 7814 if ((attr = ippFindAttribute(con->request, "limit",
7815 IPP_TAG_INTEGER)) != NULL)
7816 limit = attr->values[0].integer;
7817 else
7818 limit = 0;
ef416fc2 7819
b423cd4c 7820 /*
7821 * See if we only want to see subscriptions for a specific user...
7822 */
ef416fc2 7823
b423cd4c 7824 if ((attr = ippFindAttribute(con->request, "my-subscriptions",
7825 IPP_TAG_BOOLEAN)) != NULL &&
7826 attr->values[0].boolean)
7827 strlcpy(username, get_username(con), sizeof(username));
fa73b229 7828 else
b423cd4c 7829 username[0] = '\0';
ef416fc2 7830
b423cd4c 7831 for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0;
7832 sub;
7833 sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
7834 if ((!printer || sub->dest == printer) && (!job || sub->job == job) &&
88f9aafc 7835 (!username[0] || !_cups_strcasecmp(username, sub->owner)))
fa73b229 7836 {
b423cd4c 7837 ippAddSeparator(con->response);
10d09e33
MS
7838
7839 exclude = cupsdGetPrivateAttrs(sub->dest ? sub->dest->op_policy_ptr :
7840 policy, con, sub->dest,
7841 sub->owner);
7842
7843 copy_subscription_attrs(con, sub, ra, exclude);
ef416fc2 7844
b423cd4c 7845 count ++;
7846 if (limit && count >= limit)
7847 break;
7848 }
ef416fc2 7849
b423cd4c 7850 cupsArrayDelete(ra);
fa73b229 7851
b423cd4c 7852 if (count)
7853 con->response->request.status.status_code = IPP_OK;
7854 else
7855 send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found."));
7856}
ef416fc2 7857
ef416fc2 7858
b423cd4c 7859/*
7860 * 'get_username()' - Get the username associated with a request.
7861 */
ef416fc2 7862
b423cd4c 7863static const char * /* O - Username */
7864get_username(cupsd_client_t *con) /* I - Connection */
7865{
7866 ipp_attribute_t *attr; /* Attribute */
ef416fc2 7867
ef416fc2 7868
b423cd4c 7869 if (con->username[0])
7870 return (con->username);
7871 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7872 IPP_TAG_NAME)) != NULL)
7873 return (attr->values[0].string.text);
7874 else
7875 return ("anonymous");
ef416fc2 7876}
7877
7878
7879/*
b423cd4c 7880 * 'hold_job()' - Hold a print job.
ef416fc2 7881 */
7882
b423cd4c 7883static void
7884hold_job(cupsd_client_t *con, /* I - Client connection */
7885 ipp_attribute_t *uri) /* I - Job or Printer URI */
ef416fc2 7886{
b9faaae1
MS
7887 ipp_attribute_t *attr; /* Current job-hold-until */
7888 const char *when; /* New value */
b423cd4c 7889 int jobid; /* Job ID */
61cf44e2 7890 char scheme[HTTP_MAX_URI], /* Method portion of URI */
b423cd4c 7891 username[HTTP_MAX_URI], /* Username portion of URI */
7892 host[HTTP_MAX_URI], /* Host portion of URI */
7893 resource[HTTP_MAX_URI]; /* Resource portion of URI */
7894 int port; /* Port portion of URI */
7895 cupsd_job_t *job; /* Job information */
7896
ef416fc2 7897
996acce8 7898 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->number,
b423cd4c 7899 uri->values[0].string.text);
ef416fc2 7900
7901 /*
b423cd4c 7902 * See if we have a job URI or a printer URI...
ef416fc2 7903 */
7904
b423cd4c 7905 if (!strcmp(uri->name, "printer-uri"))
7906 {
7907 /*
7908 * Got a printer URI; see if we also have a job-id attribute...
7909 */
ef416fc2 7910
b423cd4c 7911 if ((attr = ippFindAttribute(con->request, "job-id",
7912 IPP_TAG_INTEGER)) == NULL)
7913 {
7914 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 7915 _("Got a printer-uri attribute but no job-id."));
b423cd4c 7916 return;
7917 }
ef416fc2 7918
b423cd4c 7919 jobid = attr->values[0].integer;
7920 }
ef416fc2 7921 else
ef416fc2 7922 {
b423cd4c 7923 /*
7924 * Got a job URI; parse it to get the job ID...
7925 */
ef416fc2 7926
61cf44e2
MS
7927 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7928 sizeof(scheme), username, sizeof(username), host,
b423cd4c 7929 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 7930
b423cd4c 7931 if (strncmp(resource, "/jobs/", 6))
7932 {
7933 /*
7934 * Not a valid URI!
7935 */
ef416fc2 7936
b423cd4c 7937 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 7938 _("Bad job-uri \"%s\"."),
b423cd4c 7939 uri->values[0].string.text);
7940 return;
7941 }
ef416fc2 7942
b423cd4c 7943 jobid = atoi(resource + 6);
7944 }
ef416fc2 7945
ef416fc2 7946 /*
b423cd4c 7947 * See if the job exists...
ef416fc2 7948 */
7949
b423cd4c 7950 if ((job = cupsdFindJob(jobid)) == NULL)
7951 {
7952 /*
7953 * Nope - return a "not found" error...
7954 */
7955
84315f46 7956 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
b423cd4c 7957 return;
7958 }
ef416fc2 7959
7960 /*
b423cd4c 7961 * See if the job is owned by the requesting user...
ef416fc2 7962 */
7963
b423cd4c 7964 if (!validate_user(job, con, job->username, username, sizeof(username)))
7965 {
b0f6947b
MS
7966 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
7967 cupsdFindDest(job->dest));
b423cd4c 7968 return;
7969 }
ef416fc2 7970
f99f3698
MS
7971 /*
7972 * See if the job is in a state that allows holding...
7973 */
7974
7975 if (job->state_value > IPP_JOB_STOPPED)
7976 {
7977 /*
7978 * Return a "not-possible" error...
7979 */
7980
7981 send_ipp_status(con, IPP_NOT_POSSIBLE,
7982 _("Job #%d is finished and cannot be altered."),
7983 job->id);
7984 return;
7985 }
7986
ef416fc2 7987 /*
b423cd4c 7988 * Hold the job and return...
ef416fc2 7989 */
7990
4c37eb9f 7991 if ((attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_ZERO)) != NULL)
b423cd4c 7992 {
4c37eb9f
MS
7993 if ((ippGetValueTag(attr) != IPP_TAG_KEYWORD && ippGetValueTag(attr) != IPP_TAG_NAME && ippGetValueTag(attr) != IPP_TAG_NAMELANG) || ippGetCount(attr) != 1 || !ippValidateAttribute(attr))
7994 {
7995 send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Unsupported 'job-hold-until' value."));
7996 ippCopyAttribute(con->response, attr, 0);
7997 return;
7998 }
7999
8000 when = ippGetString(attr, 0, NULL);
d09495fa 8001
01ce6322 8002 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
b9faaae1 8003 "Job job-hold-until value changed by user.");
b423cd4c 8004 }
b9faaae1
MS
8005 else
8006 when = "indefinite";
ef416fc2 8007
b9faaae1
MS
8008 cupsdSetJobHoldUntil(job, when, 1);
8009 cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT, "Job held by \"%s\".",
8010 username);
b423cd4c 8011
8012 con->response->request.status.status_code = IPP_OK;
ef416fc2 8013}
8014
8015
61cf44e2
MS
8016/*
8017 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
8018 */
8019
8020static void
8021hold_new_jobs(cupsd_client_t *con, /* I - Connection */
8022 ipp_attribute_t *uri) /* I - Printer URI */
8023{
8024 http_status_t status; /* Policy status */
8025 cups_ptype_t dtype; /* Destination type (printer/class) */
8026 cupsd_printer_t *printer; /* Printer data */
8027
8028
8029 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_new_jobs(%p[%d], %s)", con,
996acce8 8030 con->number, uri->values[0].string.text);
61cf44e2
MS
8031
8032 /*
8033 * Is the destination valid?
8034 */
8035
8036 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8037 {
8038 /*
8039 * Bad URI...
8040 */
8041
8042 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 8043 _("The printer or class does not exist."));
61cf44e2
MS
8044 return;
8045 }
8046
8047 /*
8048 * Check policy...
8049 */
8050
8051 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8052 {
8053 send_http_error(con, status, printer);
8054 return;
8055 }
8056
8057 /*
8058 * Hold pending/new jobs sent to the printer...
8059 */
8060
8061 printer->holding_new_jobs = 1;
8062
8063 cupsdSetPrinterReasons(printer, "+hold-new-jobs");
61cf44e2
MS
8064
8065 if (dtype & CUPS_PRINTER_CLASS)
8066 cupsdLogMessage(CUPSD_LOG_INFO,
8067 "Class \"%s\" now holding pending/new jobs (\"%s\").",
8068 printer->name, get_username(con));
8069 else
8070 cupsdLogMessage(CUPSD_LOG_INFO,
8071 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
8072 printer->name, get_username(con));
8073
8074 /*
8075 * Everything was ok, so return OK status...
8076 */
8077
8078 con->response->request.status.status_code = IPP_OK;
8079}
8080
8081
ef416fc2 8082/*
b423cd4c 8083 * 'move_job()' - Move a job to a new destination.
ef416fc2 8084 */
8085
8086static void
b423cd4c 8087move_job(cupsd_client_t *con, /* I - Client connection */
8088 ipp_attribute_t *uri) /* I - Job URI */
ef416fc2 8089{
8090 http_status_t status; /* Policy status */
8091 ipp_attribute_t *attr; /* Current attribute */
b423cd4c 8092 int jobid; /* Job ID */
ef416fc2 8093 cupsd_job_t *job; /* Current job */
d09495fa 8094 const char *src; /* Source printer/class */
b423cd4c 8095 cups_ptype_t stype, /* Source type (printer or class) */
bc44d920 8096 dtype; /* Destination type (printer/class) */
f7deaa1a 8097 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
ef416fc2 8098 username[HTTP_MAX_URI], /* Username portion of URI */
8099 host[HTTP_MAX_URI], /* Host portion of URI */
b423cd4c 8100 resource[HTTP_MAX_URI]; /* Resource portion of URI */
ef416fc2 8101 int port; /* Port portion of URI */
b423cd4c 8102 cupsd_printer_t *sprinter, /* Source printer */
8103 *dprinter; /* Destination printer */
ef416fc2 8104
8105
996acce8 8106 cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->number,
ef416fc2 8107 uri->values[0].string.text);
8108
8109 /*
b423cd4c 8110 * Get the new printer or class...
ef416fc2 8111 */
8112
b423cd4c 8113 if ((attr = ippFindAttribute(con->request, "job-printer-uri",
8114 IPP_TAG_URI)) == NULL)
ef416fc2 8115 {
b423cd4c 8116 /*
8117 * Need job-printer-uri...
8118 */
ef416fc2 8119
b423cd4c 8120 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 8121 _("job-printer-uri attribute missing."));
b423cd4c 8122 return;
ef416fc2 8123 }
f7faf1f5 8124
f7deaa1a 8125 if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter))
ef416fc2 8126 {
b423cd4c 8127 /*
8128 * Bad URI...
8129 */
ef416fc2 8130
b423cd4c 8131 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 8132 _("The printer or class does not exist."));
b423cd4c 8133 return;
ef416fc2 8134 }
8135
ef416fc2 8136 /*
b423cd4c 8137 * See if we have a job URI or a printer URI...
ef416fc2 8138 */
8139
f7deaa1a 8140 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
8141 sizeof(scheme), username, sizeof(username), host,
b423cd4c 8142 sizeof(host), &port, resource, sizeof(resource));
8143
8144 if (!strcmp(uri->name, "printer-uri"))
ef416fc2 8145 {
8146 /*
b423cd4c 8147 * Got a printer URI; see if we also have a job-id attribute...
ef416fc2 8148 */
8149
b423cd4c 8150 if ((attr = ippFindAttribute(con->request, "job-id",
8151 IPP_TAG_INTEGER)) == NULL)
ef416fc2 8152 {
b423cd4c 8153 /*
8154 * Move all jobs...
8155 */
8156
f7deaa1a 8157 if ((src = cupsdValidateDest(uri->values[0].string.text, &stype,
8158 &sprinter)) == NULL)
b423cd4c 8159 {
8160 /*
8161 * Bad URI...
8162 */
8163
8164 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 8165 _("The printer or class does not exist."));
b423cd4c 8166 return;
8167 }
8168
8169 job = NULL;
8170 }
8171 else
8172 {
8173 /*
8174 * Otherwise, just move a single job...
8175 */
8176
8177 if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
8178 {
8179 /*
8180 * Nope - return a "not found" error...
8181 */
8182
8183 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 8184 _("Job #%d does not exist."), attr->values[0].integer);
b423cd4c 8185 return;
8186 }
8187 else
8188 {
8189 /*
8190 * Job found, initialize source pointers...
8191 */
8192
8193 src = NULL;
8194 sprinter = NULL;
8195 }
ef416fc2 8196 }
8197 }
8198 else
8199 {
8200 /*
b423cd4c 8201 * Got a job URI; parse it to get the job ID...
ef416fc2 8202 */
8203
b423cd4c 8204 if (strncmp(resource, "/jobs/", 6))
8205 {
8206 /*
8207 * Not a valid URI!
8208 */
8209
84315f46 8210 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
b423cd4c 8211 uri->values[0].string.text);
8212 return;
8213 }
ef416fc2 8214
ef416fc2 8215 /*
b423cd4c 8216 * See if the job exists...
ef416fc2 8217 */
8218
b423cd4c 8219 jobid = atoi(resource + 6);
ef416fc2 8220
b423cd4c 8221 if ((job = cupsdFindJob(jobid)) == NULL)
ef416fc2 8222 {
8223 /*
b423cd4c 8224 * Nope - return a "not found" error...
ef416fc2 8225 */
8226
84315f46 8227 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
b423cd4c 8228 return;
ef416fc2 8229 }
8230 else
b423cd4c 8231 {
8232 /*
8233 * Job found, initialize source pointers...
8234 */
ef416fc2 8235
b423cd4c 8236 src = NULL;
8237 sprinter = NULL;
8238 }
ef416fc2 8239 }
8240
ac884b6a
MS
8241 /*
8242 * Check the policy of the destination printer...
8243 */
8244
8245 if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con,
8246 job ? job->username : NULL)) != HTTP_OK)
8247 {
8248 send_http_error(con, status, dprinter);
8249 return;
8250 }
8251
ef416fc2 8252 /*
b423cd4c 8253 * Now move the job or jobs...
ef416fc2 8254 */
8255
b423cd4c 8256 if (job)
8257 {
8258 /*
8259 * See if the job has been completed...
8260 */
8261
8262 if (job->state_value > IPP_JOB_STOPPED)
8263 {
8264 /*
8265 * Return a "not-possible" error...
8266 */
8267
8268 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 8269 _("Job #%d is finished and cannot be altered."),
b423cd4c 8270 job->id);
8271 return;
8272 }
ef416fc2 8273
b423cd4c 8274 /*
8275 * See if the job is owned by the requesting user...
8276 */
ef416fc2 8277
b423cd4c 8278 if (!validate_user(job, con, job->username, username, sizeof(username)))
8279 {
b0f6947b
MS
8280 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
8281 cupsdFindDest(job->dest));
b423cd4c 8282 return;
8283 }
ef416fc2 8284
ef416fc2 8285 /*
b423cd4c 8286 * Move the job to a different printer or class...
ef416fc2 8287 */
8288
e53920b9 8289 cupsdMoveJob(job, dprinter);
ef416fc2 8290 }
b423cd4c 8291 else
8292 {
8293 /*
8294 * Got the source printer, now look through the jobs...
8295 */
ef416fc2 8296
b423cd4c 8297 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
8298 job;
8299 job = (cupsd_job_t *)cupsArrayNext(Jobs))
8300 {
8301 /*
8302 * See if the job is pointing at the source printer or has not been
8303 * completed...
8304 */
ef416fc2 8305
88f9aafc 8306 if (_cups_strcasecmp(job->dest, src) ||
b423cd4c 8307 job->state_value > IPP_JOB_STOPPED)
8308 continue;
ef416fc2 8309
b423cd4c 8310 /*
8311 * See if the job can be moved by the requesting user...
8312 */
ef416fc2 8313
b423cd4c 8314 if (!validate_user(job, con, job->username, username, sizeof(username)))
8315 continue;
ef416fc2 8316
b423cd4c 8317 /*
8318 * Move the job to a different printer or class...
8319 */
ef416fc2 8320
e53920b9 8321 cupsdMoveJob(job, dprinter);
b423cd4c 8322 }
ef416fc2 8323 }
8324
8325 /*
b423cd4c 8326 * Start jobs if possible...
ef416fc2 8327 */
8328
b423cd4c 8329 cupsdCheckJobs();
ef416fc2 8330
8331 /*
b423cd4c 8332 * Return with "everything is OK" status...
ef416fc2 8333 */
8334
b423cd4c 8335 con->response->request.status.status_code = IPP_OK;
8336}
ef416fc2 8337
ef416fc2 8338
b423cd4c 8339/*
8340 * 'ppd_parse_line()' - Parse a PPD default line.
8341 */
ef416fc2 8342
b423cd4c 8343static int /* O - 0 on success, -1 on failure */
8344ppd_parse_line(const char *line, /* I - Line */
8345 char *option, /* O - Option name */
8346 int olen, /* I - Size of option name */
8347 char *choice, /* O - Choice name */
8348 int clen) /* I - Size of choice name */
8349{
8350 /*
8351 * Verify this is a default option line...
8352 */
ef416fc2 8353
b423cd4c 8354 if (strncmp(line, "*Default", 8))
8355 return (-1);
ef416fc2 8356
b423cd4c 8357 /*
8358 * Read the option name...
8359 */
8360
18ecb428
MS
8361 for (line += 8, olen --;
8362 *line > ' ' && *line < 0x7f && *line != ':' && *line != '/';
8363 line ++)
b423cd4c 8364 if (olen > 0)
8365 {
8366 *option++ = *line;
8367 olen --;
ef416fc2 8368 }
8369
b423cd4c 8370 *option = '\0';
ef416fc2 8371
b423cd4c 8372 /*
8373 * Skip everything else up to the colon (:)...
8374 */
ef416fc2 8375
b423cd4c 8376 while (*line && *line != ':')
8377 line ++;
ef416fc2 8378
b423cd4c 8379 if (!*line)
8380 return (-1);
ef416fc2 8381
b423cd4c 8382 line ++;
ef416fc2 8383
b423cd4c 8384 /*
8385 * Now grab the option choice, skipping leading whitespace...
8386 */
ef416fc2 8387
b423cd4c 8388 while (isspace(*line & 255))
8389 line ++;
ef416fc2 8390
18ecb428
MS
8391 for (clen --;
8392 *line > ' ' && *line < 0x7f && *line != ':' && *line != '/';
8393 line ++)
b423cd4c 8394 if (clen > 0)
8395 {
8396 *choice++ = *line;
8397 clen --;
8398 }
ef416fc2 8399
b423cd4c 8400 *choice = '\0';
ef416fc2 8401
b423cd4c 8402 /*
8403 * Return with no errors...
8404 */
ef416fc2 8405
b423cd4c 8406 return (0);
8407}
ef416fc2 8408
ef416fc2 8409
b423cd4c 8410/*
8411 * 'print_job()' - Print a file to a printer or class.
8412 */
ef416fc2 8413
b423cd4c 8414static void
8415print_job(cupsd_client_t *con, /* I - Client connection */
8416 ipp_attribute_t *uri) /* I - Printer URI */
8417{
8418 ipp_attribute_t *attr; /* Current attribute */
9514a192 8419 ipp_attribute_t *doc_name; /* document-name attribute */
b423cd4c 8420 ipp_attribute_t *format; /* Document-format attribute */
f7deaa1a 8421 const char *default_format; /* document-format-default value */
b423cd4c 8422 cupsd_job_t *job; /* New job */
8423 char filename[1024]; /* Job filename */
8424 mime_type_t *filetype; /* Type of file */
8425 char super[MIME_MAX_SUPER], /* Supertype of file */
8426 type[MIME_MAX_TYPE], /* Subtype of file */
8427 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
8428 /* Textual name of mime type */
8429 cupsd_printer_t *printer; /* Printer data */
8430 struct stat fileinfo; /* File information */
8431 int kbytes; /* Size of file */
8432 int compression; /* Document compression */
ef416fc2 8433
ef416fc2 8434
996acce8 8435 cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->number,
b423cd4c 8436 uri->values[0].string.text);
ef416fc2 8437
b423cd4c 8438 /*
8439 * Validate print file attributes, for now just document-format and
8440 * compression (CUPS only supports "none" and "gzip")...
8441 */
ef416fc2 8442
b423cd4c 8443 compression = CUPS_FILE_NONE;
ef416fc2 8444
b423cd4c 8445 if ((attr = ippFindAttribute(con->request, "compression",
8446 IPP_TAG_KEYWORD)) != NULL)
8447 {
8448 if (strcmp(attr->values[0].string.text, "none")
8449#ifdef HAVE_LIBZ
8450 && strcmp(attr->values[0].string.text, "gzip")
8451#endif /* HAVE_LIBZ */
8452 )
8453 {
8454 send_ipp_status(con, IPP_ATTRIBUTES,
84315f46 8455 _("Unsupported compression \"%s\"."),
b423cd4c 8456 attr->values[0].string.text);
8457 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
8458 "compression", NULL, attr->values[0].string.text);
8459 return;
8460 }
ef416fc2 8461
b423cd4c 8462#ifdef HAVE_LIBZ
8463 if (!strcmp(attr->values[0].string.text, "gzip"))
8464 compression = CUPS_FILE_GZIP;
8465#endif /* HAVE_LIBZ */
8466 }
ef416fc2 8467
b423cd4c 8468 /*
8469 * Do we have a file to print?
8470 */
ef416fc2 8471
b423cd4c 8472 if (!con->filename)
8473 {
84315f46 8474 send_ipp_status(con, IPP_BAD_REQUEST, _("No file in print request."));
b423cd4c 8475 return;
8476 }
ef416fc2 8477
f7deaa1a 8478 /*
8479 * Is the destination valid?
8480 */
8481
8482 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
8483 {
8484 /*
8485 * Bad URI...
8486 */
8487
8488 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 8489 _("The printer or class does not exist."));
f7deaa1a 8490 return;
8491 }
8492
b423cd4c 8493 /*
8494 * Is it a format we support?
8495 */
ef416fc2 8496
9514a192
MS
8497 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
8498 if (doc_name)
8499 ippSetName(con->request, &doc_name, "document-name-supplied");
8500
b423cd4c 8501 if ((format = ippFindAttribute(con->request, "document-format",
8502 IPP_TAG_MIMETYPE)) != NULL)
8503 {
8504 /*
8505 * Grab format from client...
8506 */
8507
c5b24bfa 8508 if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]", super,
f7deaa1a 8509 type) != 2)
b423cd4c 8510 {
8511 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 8512 _("Bad document-format \"%s\"."),
b423cd4c 8513 format->values[0].string.text);
8514 return;
ef416fc2 8515 }
9514a192
MS
8516
8517 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-supplied", NULL, ippGetString(format, 0, NULL));
b423cd4c 8518 }
f7deaa1a 8519 else if ((default_format = cupsGetOption("document-format",
8520 printer->num_options,
8521 printer->options)) != NULL)
8522 {
8523 /*
8524 * Use default document format...
8525 */
8526
c5b24bfa 8527 if (sscanf(default_format, "%15[^/]/%255[^;]", super, type) != 2)
f7deaa1a 8528 {
8529 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 8530 _("Bad document-format \"%s\"."),
f7deaa1a 8531 default_format);
8532 return;
8533 }
8534 }
b423cd4c 8535 else
8536 {
8537 /*
f7deaa1a 8538 * Auto-type it!
b423cd4c 8539 */
8540
5a9febac
MS
8541 strlcpy(super, "application", sizeof(super));
8542 strlcpy(type, "octet-stream", sizeof(type));
b423cd4c 8543 }
ef416fc2 8544
b423cd4c 8545 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
8546 {
ef416fc2 8547 /*
b423cd4c 8548 * Auto-type the file...
ef416fc2 8549 */
8550
a0f6818e 8551 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file...");
b423cd4c 8552
9514a192 8553
b423cd4c 8554 filetype = mimeFileType(MimeDatabase, con->filename,
8555 doc_name ? doc_name->values[0].string.text : NULL,
8556 &compression);
8557
f7deaa1a 8558 if (!filetype)
8559 filetype = mimeType(MimeDatabase, super, type);
a0f6818e
MS
8560
8561 cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.",
8562 filetype->super, filetype->type);
9514a192
MS
8563
8564 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, filetype->type);
8565 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-detected", NULL, mimetype);
f7deaa1a 8566 }
8567 else
8568 filetype = mimeType(MimeDatabase, super, type);
ef416fc2 8569
f7deaa1a 8570 if (filetype &&
8571 (!format ||
8572 (!strcmp(super, "application") && !strcmp(type, "octet-stream"))))
8573 {
8574 /*
8575 * Replace the document-format attribute value with the auto-typed or
8576 * default one.
8577 */
b423cd4c 8578
f7deaa1a 8579 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
8580 filetype->type);
ed486911 8581
f7deaa1a 8582 if (format)
5e6c3df7 8583 ippSetString(con->request, &format, 0, mimetype);
b423cd4c 8584 else
f7deaa1a 8585 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
8586 "document-format", NULL, mimetype);
ef416fc2 8587 }
f7deaa1a 8588 else if (!filetype)
b423cd4c 8589 {
8590 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
84315f46 8591 _("Unsupported document-format \"%s\"."),
c7017ecc
MS
8592 format ? format->values[0].string.text :
8593 "application/octet-stream");
b423cd4c 8594 cupsdLogMessage(CUPSD_LOG_INFO,
8595 "Hint: Do you have the raw file printing rules enabled?");
8596
8597 if (format)
8598 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
8599 "document-format", NULL, format->values[0].string.text);
8600
8601 return;
8602 }
8603
b423cd4c 8604 /*
8605 * Read any embedded job ticket info from PS files...
8606 */
8607
88f9aafc
MS
8608 if (!_cups_strcasecmp(filetype->super, "application") &&
8609 (!_cups_strcasecmp(filetype->type, "postscript") ||
8610 !_cups_strcasecmp(filetype->type, "pdf")))
c5571a1d 8611 read_job_ticket(con);
b423cd4c 8612
8613 /*
8614 * Create the job object...
8615 */
8616
f7deaa1a 8617 if ((job = add_job(con, printer, filetype)) == NULL)
b423cd4c 8618 return;
8619
8620 /*
8621 * Update quota data...
8622 */
8623
8624 if (stat(con->filename, &fileinfo))
8625 kbytes = 0;
8626 else
8627 kbytes = (fileinfo.st_size + 1023) / 1024;
8628
8629 cupsdUpdateQuota(printer, job->username, 0, kbytes);
8630
f16ea703
MS
8631 job->koctets += kbytes;
8632
8633 if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL)
b423cd4c 8634 attr->values[0].integer += kbytes;
8635
ef416fc2 8636 /*
8637 * Add the job file...
8638 */
8639
8640 if (add_file(con, job, filetype, compression))
8641 return;
8642
6d8021f4
MS
8643 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, job->num_files);
8644 if (rename(con->filename, filename))
8645 {
8646 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to rename job document file \"%s\": %s", filename, strerror(errno));
8647
8648 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to rename job document file."));
8649 return;
8650 }
8651
ef416fc2 8652 cupsdClearString(&con->filename);
8653
8654 /*
8655 * See if we need to add the ending sheet...
8656 */
8657
91c84a35
MS
8658 if (cupsdTimeoutJob(job))
8659 return;
ef416fc2 8660
ef416fc2 8661 /*
8662 * Log and save the job...
8663 */
8664
75bd9771
MS
8665 cupsdLogJob(job, CUPSD_LOG_INFO,
8666 "File of type %s/%s queued by \"%s\".",
8667 filetype->super, filetype->type, job->username);
8668 cupsdLogJob(job, CUPSD_LOG_DEBUG, "hold_until=%d", (int)job->hold_until);
b19ccc9e
MS
8669 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
8670 job->dest, job->username);
ef416fc2 8671
ef416fc2 8672 /*
8673 * Start the job if possible...
8674 */
8675
8676 cupsdCheckJobs();
8677}
8678
8679
8680/*
c5571a1d 8681 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
ef416fc2 8682 *
c5571a1d 8683 * This function only gets called when printing a single PDF or PostScript
ef416fc2 8684 * file using the Print-Job operation. It doesn't work for Create-Job +
8685 * Send-File, since the job attributes need to be set at job creation
c5571a1d
MS
8686 * time for banners to work. The embedded job ticket stuff is here
8687 * primarily to allow the Windows printer driver for CUPS to pass in JCL
ef416fc2 8688 * options and IPP attributes which otherwise would be lost.
8689 *
c5571a1d 8690 * The format of a job ticket is simple:
ef416fc2 8691 *
8692 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8693 *
8694 * %cupsJobTicket: attr1=value1
8695 * %cupsJobTicket: attr2=value2
8696 * ...
8697 * %cupsJobTicket: attrN=valueN
8698 *
8699 * Job ticket lines must appear immediately after the first line that
c5571a1d
MS
8700 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8701 * stops looking for job ticket info when it finds a line that does not begin
ef416fc2 8702 * with "%cupsJobTicket:".
8703 *
8704 * The maximum length of a job ticket line, including the prefix, is
8705 * 255 characters to conform with the Adobe DSC.
8706 *
8707 * Read-only attributes are rejected with a notice to the error log in
8708 * case a malicious user tries anything. Since the job ticket is read
8709 * prior to attribute validation in print_job(), job ticket attributes
8710 * will go through the same validation as IPP attributes...
8711 */
8712
8713static void
c5571a1d 8714read_job_ticket(cupsd_client_t *con) /* I - Client connection */
ef416fc2 8715{
8716 cups_file_t *fp; /* File to read from */
8717 char line[256]; /* Line data */
8718 int num_options; /* Number of options */
8719 cups_option_t *options; /* Options */
8720 ipp_t *ticket; /* New attributes */
8721 ipp_attribute_t *attr, /* Current attribute */
8722 *attr2, /* Job attribute */
8723 *prev2; /* Previous job attribute */
8724
8725
8726 /*
8727 * First open the print file...
8728 */
8729
8730 if ((fp = cupsFileOpen(con->filename, "rb")) == NULL)
8731 {
8732 cupsdLogMessage(CUPSD_LOG_ERROR,
c5571a1d 8733 "Unable to open print file for job ticket - %s",
ef416fc2 8734 strerror(errno));
8735 return;
8736 }
8737
8738 /*
8739 * Skip the first line...
8740 */
8741
8742 if (cupsFileGets(fp, line, sizeof(line)) == NULL)
8743 {
8744 cupsdLogMessage(CUPSD_LOG_ERROR,
c5571a1d 8745 "Unable to read from print file for job ticket - %s",
ef416fc2 8746 strerror(errno));
8747 cupsFileClose(fp);
8748 return;
8749 }
8750
c5571a1d 8751 if (strncmp(line, "%!PS-Adobe-", 11) && strncmp(line, "%PDF-", 5))
ef416fc2 8752 {
8753 /*
8754 * Not a DSC-compliant file, so no job ticket info will be available...
8755 */
8756
8757 cupsFileClose(fp);
8758 return;
8759 }
8760
8761 /*
8762 * Read job ticket info from the file...
8763 */
8764
8765 num_options = 0;
8766 options = NULL;
8767
fa73b229 8768 while (cupsFileGets(fp, line, sizeof(line)))
ef416fc2 8769 {
8770 /*
8771 * Stop at the first non-ticket line...
8772 */
8773
fa73b229 8774 if (strncmp(line, "%cupsJobTicket:", 15))
ef416fc2 8775 break;
8776
8777 /*
8778 * Add the options to the option array...
8779 */
8780
8781 num_options = cupsParseOptions(line + 15, num_options, &options);
8782 }
8783
8784 /*
8785 * Done with the file; see if we have any options...
8786 */
8787
8788 cupsFileClose(fp);
8789
8790 if (num_options == 0)
8791 return;
8792
8793 /*
8794 * OK, convert the options to an attribute list, and apply them to
8795 * the request...
8796 */
8797
8798 ticket = ippNew();
8799 cupsEncodeOptions(ticket, num_options, options);
8800
8801 /*
8802 * See what the user wants to change.
8803 */
8804
fa73b229 8805 for (attr = ticket->attrs; attr; attr = attr->next)
ef416fc2 8806 {
8807 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
8808 continue;
8809
9514a192
MS
8810 if (!strncmp(attr->name, "date-time-at-", 13) ||
8811 !strcmp(attr->name, "job-impressions-completed") ||
ef416fc2 8812 !strcmp(attr->name, "job-media-sheets-completed") ||
9514a192 8813 !strncmp(attr->name, "job-k-octets", 12) ||
ef416fc2 8814 !strcmp(attr->name, "job-id") ||
9514a192
MS
8815 !strcmp(attr->name, "job-originating-host-name") ||
8816 !strcmp(attr->name, "job-originating-user-name") ||
8817 !strcmp(attr->name, "job-pages-completed") ||
8818 !strcmp(attr->name, "job-printer-uri") ||
ef416fc2 8819 !strncmp(attr->name, "job-state", 9) ||
9514a192 8820 !strcmp(attr->name, "job-uri") ||
ef416fc2 8821 !strncmp(attr->name, "time-at-", 8))
8822 continue; /* Read-only attrs */
8823
fa73b229 8824 if ((attr2 = ippFindAttribute(con->request, attr->name,
8825 IPP_TAG_ZERO)) != NULL)
ef416fc2 8826 {
8827 /*
8828 * Some other value; first free the old value...
8829 */
8830
8831 if (con->request->attrs == attr2)
8832 {
8833 con->request->attrs = attr2->next;
8834 prev2 = NULL;
8835 }
8836 else
8837 {
fa73b229 8838 for (prev2 = con->request->attrs; prev2; prev2 = prev2->next)
ef416fc2 8839 if (prev2->next == attr2)
8840 {
8841 prev2->next = attr2->next;
8842 break;
8843 }
8844 }
8845
8846 if (con->request->last == attr2)
8847 con->request->last = prev2;
8848
a2326b5b 8849 ippDeleteAttribute(NULL, attr2);
ef416fc2 8850 }
8851
8852 /*
8853 * Add new option by copying it...
8854 */
8855
a2326b5b 8856 ippCopyAttribute(con->request, attr, 0);
ef416fc2 8857 }
8858
8859 /*
8860 * Then free the attribute list and option array...
8861 */
8862
8863 ippDelete(ticket);
8864 cupsFreeOptions(num_options, options);
8865}
8866
8867
8868/*
8869 * 'reject_jobs()' - Reject print jobs to a printer.
8870 */
8871
8872static void
8873reject_jobs(cupsd_client_t *con, /* I - Client connection */
8874 ipp_attribute_t *uri) /* I - Printer or class URI */
8875{
8876 http_status_t status; /* Policy status */
bc44d920 8877 cups_ptype_t dtype; /* Destination type (printer/class) */
ef416fc2 8878 cupsd_printer_t *printer; /* Printer data */
8879 ipp_attribute_t *attr; /* printer-state-message text */
8880
8881
8882 cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con,
996acce8 8883 con->number, uri->values[0].string.text);
ef416fc2 8884
8885 /*
8886 * Is the destination valid?
8887 */
8888
f7deaa1a 8889 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 8890 {
8891 /*
8892 * Bad URI...
8893 */
8894
8895 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 8896 _("The printer or class does not exist."));
ef416fc2 8897 return;
8898 }
8899
8900 /*
8901 * Check policy...
8902 */
8903
8904 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8905 {
f899b121 8906 send_http_error(con, status, printer);
ef416fc2 8907 return;
8908 }
8909
8910 /*
8911 * Reject jobs sent to the printer...
8912 */
8913
8914 printer->accepting = 0;
8915
8916 if ((attr = ippFindAttribute(con->request, "printer-state-message",
8917 IPP_TAG_TEXT)) == NULL)
5a9febac
MS
8918 strlcpy(printer->state_message, "Rejecting Jobs",
8919 sizeof(printer->state_message));
ef416fc2 8920 else
8921 strlcpy(printer->state_message, attr->values[0].string.text,
8922 sizeof(printer->state_message));
8923
f8b3a85b
MS
8924 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
8925 "No longer accepting jobs.");
8926
ef416fc2 8927 if (dtype & CUPS_PRINTER_CLASS)
8928 {
3dfe78b3 8929 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
ef416fc2 8930
8931 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").",
f7deaa1a 8932 printer->name, get_username(con));
ef416fc2 8933 }
8934 else
8935 {
3dfe78b3 8936 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
ef416fc2 8937
8938 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").",
f7deaa1a 8939 printer->name, get_username(con));
ef416fc2 8940 }
8941
8942 /*
8943 * Everything was ok, so return OK status...
8944 */
8945
8946 con->response->request.status.status_code = IPP_OK;
8947}
8948
8949
61cf44e2
MS
8950/*
8951 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8952 */
8953
8954static void
8955release_held_new_jobs(
8956 cupsd_client_t *con, /* I - Connection */
8957 ipp_attribute_t *uri) /* I - Printer URI */
8958{
8959 http_status_t status; /* Policy status */
8960 cups_ptype_t dtype; /* Destination type (printer/class) */
8961 cupsd_printer_t *printer; /* Printer data */
8962
8963
8964 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_held_new_jobs(%p[%d], %s)", con,
996acce8 8965 con->number, uri->values[0].string.text);
61cf44e2
MS
8966
8967 /*
8968 * Is the destination valid?
8969 */
8970
8971 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8972 {
8973 /*
8974 * Bad URI...
8975 */
8976
8977 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 8978 _("The printer or class does not exist."));
61cf44e2
MS
8979 return;
8980 }
8981
8982 /*
8983 * Check policy...
8984 */
8985
8986 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8987 {
8988 send_http_error(con, status, printer);
8989 return;
8990 }
8991
8992 /*
8993 * Hold pending/new jobs sent to the printer...
8994 */
8995
8996 printer->holding_new_jobs = 0;
8997
8998 cupsdSetPrinterReasons(printer, "-hold-new-jobs");
61cf44e2
MS
8999
9000 if (dtype & CUPS_PRINTER_CLASS)
9001 cupsdLogMessage(CUPSD_LOG_INFO,
9002 "Class \"%s\" now printing pending/new jobs (\"%s\").",
9003 printer->name, get_username(con));
9004 else
9005 cupsdLogMessage(CUPSD_LOG_INFO,
9006 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
9007 printer->name, get_username(con));
9008
e0f489cd
MS
9009 cupsdCheckJobs();
9010
61cf44e2
MS
9011 /*
9012 * Everything was ok, so return OK status...
9013 */
9014
9015 con->response->request.status.status_code = IPP_OK;
9016}
9017
9018
ef416fc2 9019/*
9020 * 'release_job()' - Release a held print job.
9021 */
9022
9023static void
9024release_job(cupsd_client_t *con, /* I - Client connection */
9025 ipp_attribute_t *uri) /* I - Job or Printer URI */
9026{
9027 ipp_attribute_t *attr; /* Current attribute */
9028 int jobid; /* Job ID */
61cf44e2 9029 char scheme[HTTP_MAX_URI], /* Method portion of URI */
ef416fc2 9030 username[HTTP_MAX_URI], /* Username portion of URI */
9031 host[HTTP_MAX_URI], /* Host portion of URI */
9032 resource[HTTP_MAX_URI]; /* Resource portion of URI */
9033 int port; /* Port portion of URI */
9034 cupsd_job_t *job; /* Job information */
9035
9036
9037 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con,
996acce8 9038 con->number, uri->values[0].string.text);
ef416fc2 9039
9040 /*
9041 * See if we have a job URI or a printer URI...
9042 */
9043
9044 if (!strcmp(uri->name, "printer-uri"))
9045 {
9046 /*
9047 * Got a printer URI; see if we also have a job-id attribute...
9048 */
9049
fa73b229 9050 if ((attr = ippFindAttribute(con->request, "job-id",
9051 IPP_TAG_INTEGER)) == NULL)
ef416fc2 9052 {
9053 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 9054 _("Got a printer-uri attribute but no job-id."));
ef416fc2 9055 return;
9056 }
9057
9058 jobid = attr->values[0].integer;
9059 }
9060 else
9061 {
9062 /*
9063 * Got a job URI; parse it to get the job ID...
9064 */
9065
61cf44e2
MS
9066 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9067 sizeof(scheme), username, sizeof(username), host,
a4d04587 9068 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 9069
9070 if (strncmp(resource, "/jobs/", 6))
9071 {
9072 /*
9073 * Not a valid URI!
9074 */
9075
84315f46 9076 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
ef416fc2 9077 uri->values[0].string.text);
9078 return;
9079 }
9080
9081 jobid = atoi(resource + 6);
9082 }
9083
9084 /*
9085 * See if the job exists...
9086 */
9087
9088 if ((job = cupsdFindJob(jobid)) == NULL)
9089 {
9090 /*
9091 * Nope - return a "not found" error...
9092 */
9093
84315f46 9094 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
ef416fc2 9095 return;
9096 }
9097
9098 /*
9099 * See if job is "held"...
9100 */
9101
bd7854cb 9102 if (job->state_value != IPP_JOB_HELD)
ef416fc2 9103 {
9104 /*
9105 * Nope - return a "not possible" error...
9106 */
9107
84315f46 9108 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held."), jobid);
ef416fc2 9109 return;
9110 }
9111
9112 /*
9113 * See if the job is owned by the requesting user...
9114 */
9115
9116 if (!validate_user(job, con, job->username, username, sizeof(username)))
9117 {
b0f6947b
MS
9118 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9119 cupsdFindDest(job->dest));
ef416fc2 9120 return;
9121 }
9122
9123 /*
9124 * Reset the job-hold-until value to "no-hold"...
9125 */
9126
fa73b229 9127 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9128 IPP_TAG_KEYWORD)) == NULL)
ef416fc2 9129 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9130
fa73b229 9131 if (attr)
ef416fc2 9132 {
5e6c3df7
MS
9133 ippSetValueTag(job->attrs, &attr, IPP_TAG_KEYWORD);
9134 ippSetString(job->attrs, &attr, 0, "no-hold");
d09495fa 9135
01ce6322 9136 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
d09495fa 9137 "Job job-hold-until value changed by user.");
3e7fe0ca 9138 ippSetString(job->attrs, &job->reasons, 0, "none");
ef416fc2 9139 }
9140
9141 /*
9142 * Release the job and return...
9143 */
9144
9145 cupsdReleaseJob(job);
9146
01ce6322 9147 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
d09495fa 9148 "Job released by user.");
9149
75bd9771 9150 cupsdLogJob(job, CUPSD_LOG_INFO, "Released by \"%s\".", username);
ef416fc2 9151
9152 con->response->request.status.status_code = IPP_OK;
005dd1eb
MS
9153
9154 cupsdCheckJobs();
ef416fc2 9155}
9156
9157
9158/*
9159 * 'renew_subscription()' - Renew an existing subscription...
9160 */
9161
9162static void
9163renew_subscription(
9164 cupsd_client_t *con, /* I - Client connection */
9165 int sub_id) /* I - Subscription ID */
9166{
bd7854cb 9167 http_status_t status; /* Policy status */
9168 cupsd_subscription_t *sub; /* Subscription */
9169 ipp_attribute_t *lease; /* notify-lease-duration */
9170
9171
9172 cupsdLogMessage(CUPSD_LOG_DEBUG2,
9173 "renew_subscription(con=%p[%d], sub_id=%d)",
996acce8 9174 con, con->number, sub_id);
bd7854cb 9175
9176 /*
9177 * Is the subscription ID valid?
9178 */
9179
9180 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
9181 {
9182 /*
9183 * Bad subscription ID...
9184 */
9185
84315f46
MS
9186 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
9187 sub_id);
bd7854cb 9188 return;
9189 }
9190
9191 if (sub->job)
9192 {
9193 /*
9194 * Job subscriptions cannot be renewed...
9195 */
9196
9197 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 9198 _("Job subscriptions cannot be renewed."));
bd7854cb 9199 return;
9200 }
9201
9202 /*
9203 * Check policy...
9204 */
9205
9206 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
9207 DefaultPolicyPtr,
9208 con, sub->owner)) != HTTP_OK)
9209 {
f899b121 9210 send_http_error(con, status, sub->dest);
bd7854cb 9211 return;
9212 }
9213
9214 /*
9215 * Renew the subscription...
9216 */
9217
9218 lease = ippFindAttribute(con->request, "notify-lease-duration",
9219 IPP_TAG_INTEGER);
9220
9221 sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration;
9222
9223 if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration))
9224 {
9225 cupsdLogMessage(CUPSD_LOG_INFO,
9226 "renew_subscription: Limiting notify-lease-duration to "
9227 "%d seconds.",
9228 MaxLeaseDuration);
9229 sub->lease = MaxLeaseDuration;
9230 }
9231
9232 sub->expire = sub->lease ? time(NULL) + sub->lease : 0;
9233
3dfe78b3 9234 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
bd7854cb 9235
9236 con->response->request.status.status_code = IPP_OK;
b94498cf 9237
9238 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
9239 "notify-lease-duration", sub->lease);
ef416fc2 9240}
9241
9242
9243/*
9244 * 'restart_job()' - Restart an old print job.
9245 */
9246
9247static void
9248restart_job(cupsd_client_t *con, /* I - Client connection */
9249 ipp_attribute_t *uri) /* I - Job or Printer URI */
9250{
9251 ipp_attribute_t *attr; /* Current attribute */
9252 int jobid; /* Job ID */
238c3832 9253 cupsd_job_t *job; /* Job information */
61cf44e2 9254 char scheme[HTTP_MAX_URI], /* Method portion of URI */
ef416fc2 9255 username[HTTP_MAX_URI], /* Username portion of URI */
9256 host[HTTP_MAX_URI], /* Host portion of URI */
9257 resource[HTTP_MAX_URI]; /* Resource portion of URI */
9258 int port; /* Port portion of URI */
ef416fc2 9259
9260
9261 cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con,
996acce8 9262 con->number, uri->values[0].string.text);
ef416fc2 9263
9264 /*
9265 * See if we have a job URI or a printer URI...
9266 */
9267
9268 if (!strcmp(uri->name, "printer-uri"))
9269 {
9270 /*
9271 * Got a printer URI; see if we also have a job-id attribute...
9272 */
9273
fa73b229 9274 if ((attr = ippFindAttribute(con->request, "job-id",
9275 IPP_TAG_INTEGER)) == NULL)
ef416fc2 9276 {
9277 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 9278 _("Got a printer-uri attribute but no job-id."));
ef416fc2 9279 return;
9280 }
9281
9282 jobid = attr->values[0].integer;
9283 }
9284 else
9285 {
9286 /*
9287 * Got a job URI; parse it to get the job ID...
9288 */
9289
61cf44e2
MS
9290 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9291 sizeof(scheme), username, sizeof(username), host,
a4d04587 9292 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 9293
fa73b229 9294 if (strncmp(resource, "/jobs/", 6))
ef416fc2 9295 {
9296 /*
9297 * Not a valid URI!
9298 */
9299
84315f46 9300 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
ef416fc2 9301 uri->values[0].string.text);
9302 return;
9303 }
9304
9305 jobid = atoi(resource + 6);
9306 }
9307
9308 /*
9309 * See if the job exists...
9310 */
9311
9312 if ((job = cupsdFindJob(jobid)) == NULL)
9313 {
9314 /*
9315 * Nope - return a "not found" error...
9316 */
9317
84315f46 9318 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
ef416fc2 9319 return;
9320 }
9321
9322 /*
9323 * See if job is in any of the "completed" states...
9324 */
9325
bd7854cb 9326 if (job->state_value <= IPP_JOB_PROCESSING)
ef416fc2 9327 {
9328 /*
9329 * Nope - return a "not possible" error...
9330 */
9331
84315f46 9332 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete."),
ef416fc2 9333 jobid);
9334 return;
9335 }
9336
9337 /*
9338 * See if we have retained the job files...
9339 */
9340
bd7854cb 9341 cupsdLoadJob(job);
9342
07725fee 9343 if (!job->attrs || job->num_files == 0)
ef416fc2 9344 {
9345 /*
9346 * Nope - return a "not possible" error...
9347 */
9348
9349 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 9350 _("Job #%d cannot be restarted - no files."), jobid);
ef416fc2 9351 return;
9352 }
9353
9354 /*
9355 * See if the job is owned by the requesting user...
9356 */
9357
9358 if (!validate_user(job, con, job->username, username, sizeof(username)))
9359 {
b0f6947b
MS
9360 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9361 cupsdFindDest(job->dest));
ef416fc2 9362 return;
9363 }
9364
9365 /*
238c3832 9366 * See if the job-hold-until attribute is specified...
ef416fc2 9367 */
9368
238c3832
MS
9369 if ((attr = ippFindAttribute(con->request, "job-hold-until",
9370 IPP_TAG_KEYWORD)) == NULL)
9371 attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
9372
9373 if (attr && strcmp(attr->values[0].string.text, "no-hold"))
9374 {
9375 /*
9376 * Return the job to a held state...
9377 */
9378
9379 cupsdLogJob(job, CUPSD_LOG_DEBUG,
9380 "Restarted by \"%s\" with job-hold-until=%s.",
9381 username, attr->values[0].string.text);
9382 cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
9383
9384 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE,
9385 NULL, job, "Job restarted by user with job-hold-until=%s",
9386 attr->values[0].string.text);
9387 }
9388 else
9389 {
9390 /*
9391 * Restart the job...
9392 */
9393
9394 cupsdRestartJob(job);
f11a948a 9395 cupsdCheckJobs();
238c3832 9396 }
ef416fc2 9397
75bd9771 9398 cupsdLogJob(job, CUPSD_LOG_INFO, "Restarted by \"%s\".", username);
ef416fc2 9399
9400 con->response->request.status.status_code = IPP_OK;
9401}
9402
9403
9404/*
9405 * 'save_auth_info()' - Save authentication information for a job.
9406 */
9407
9408static void
f7deaa1a 9409save_auth_info(
9410 cupsd_client_t *con, /* I - Client connection */
9411 cupsd_job_t *job, /* I - Job */
9412 ipp_attribute_t *auth_info) /* I - auth-info attribute, if any */
ef416fc2 9413{
09a101d6 9414 int i; /* Looping var */
9415 char filename[1024]; /* Job authentication filename */
9416 cups_file_t *fp; /* Job authentication file */
85dda01c 9417 char line[65536]; /* Line for file */
09a101d6 9418 cupsd_printer_t *dest; /* Destination printer/class */
ef416fc2 9419
9420
9421 /*
9422 * This function saves the in-memory authentication information for
9423 * a job so that it can be used to authenticate with a remote host.
9424 * The information is stored in a file that is readable only by the
f7deaa1a 9425 * root user. The fields are Base-64 encoded, each on a separate line,
9426 * followed by random number (up to 1024) of newlines to limit the
9427 * amount of information that is exposed.
ef416fc2 9428 *
9429 * Because of the potential for exposing of authentication information,
9430 * this functionality is only enabled when running cupsd as root.
9431 *
9432 * This caching only works for the Basic and BasicDigest authentication
9433 * types. Digest authentication cannot be cached this way, and in
9434 * the future Kerberos authentication may make all of this obsolete.
9435 *
9436 * Authentication information is saved whenever an authenticated
9437 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9438 * performed.
9439 *
9440 * This information is deleted after a job is completed or canceled,
9441 * so reprints may require subsequent re-authentication.
9442 */
9443
9444 if (RunUser)
9445 return;
9446
09a101d6 9447 if ((dest = cupsdFindDest(job->dest)) == NULL)
9448 return;
9449
ef416fc2 9450 /*
9451 * Create the authentication file and change permissions...
9452 */
9453
9454 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
9455 if ((fp = cupsFileOpen(filename, "w")) == NULL)
9456 {
9457 cupsdLogMessage(CUPSD_LOG_ERROR,
9458 "Unable to save authentication info to \"%s\" - %s",
9459 filename, strerror(errno));
9460 return;
9461 }
9462
9463 fchown(cupsFileNumber(fp), 0, 0);
9464 fchmod(cupsFileNumber(fp), 0400);
9465
94436c5a 9466 cupsFilePuts(fp, "CUPSD-AUTH-V3\n");
dcb445bc 9467
88f9aafc
MS
9468 for (i = 0;
9469 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
9470 i ++)
9471 cupsdClearString(job->auth_env + i);
9472
09a101d6 9473 if (auth_info && auth_info->num_values == dest->num_auth_info_required)
f7deaa1a 9474 {
9475 /*
09a101d6 9476 * Write 1 to 3 auth values...
f7deaa1a 9477 */
ef416fc2 9478
88f9aafc
MS
9479 for (i = 0;
9480 i < auth_info->num_values &&
9481 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
9482 i ++)
f7deaa1a 9483 {
94436c5a
MS
9484 if (strcmp(dest->auth_info_required[i], "negotiate"))
9485 {
7e86f2f6 9486 httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text, (int)strlen(auth_info->values[i].string.text));
94436c5a
MS
9487 cupsFilePutConf(fp, dest->auth_info_required[i], line);
9488 }
9489 else
9490 cupsFilePutConf(fp, dest->auth_info_required[i],
9491 auth_info->values[i].string.text);
09a101d6 9492
9493 if (!strcmp(dest->auth_info_required[i], "username"))
88f9aafc 9494 cupsdSetStringf(job->auth_env + i, "AUTH_USERNAME=%s",
09a101d6 9495 auth_info->values[i].string.text);
9496 else if (!strcmp(dest->auth_info_required[i], "domain"))
88f9aafc 9497 cupsdSetStringf(job->auth_env + i, "AUTH_DOMAIN=%s",
09a101d6 9498 auth_info->values[i].string.text);
9499 else if (!strcmp(dest->auth_info_required[i], "password"))
88f9aafc
MS
9500 cupsdSetStringf(job->auth_env + i, "AUTH_PASSWORD=%s",
9501 auth_info->values[i].string.text);
9502 else if (!strcmp(dest->auth_info_required[i], "negotiate"))
9503 cupsdSetStringf(job->auth_env + i, "AUTH_NEGOTIATE=%s",
9504 auth_info->values[i].string.text);
9505 else
dcb445bc 9506 i --;
f7deaa1a 9507 }
9508 }
dcb445bc
MS
9509 else if (auth_info && auth_info->num_values == 2 &&
9510 dest->num_auth_info_required == 1 &&
9511 !strcmp(dest->auth_info_required[0], "negotiate"))
9512 {
9513 /*
9514 * Allow fallback to username+password for Kerberized queues...
9515 */
9516
7e86f2f6 9517 httpEncode64_2(line, sizeof(line), auth_info->values[0].string.text, (int)strlen(auth_info->values[0].string.text));
dcb445bc
MS
9518 cupsFilePutConf(fp, "username", line);
9519
9520 cupsdSetStringf(job->auth_env + 0, "AUTH_USERNAME=%s",
9521 auth_info->values[0].string.text);
9522
7e86f2f6 9523 httpEncode64_2(line, sizeof(line), auth_info->values[1].string.text, (int)strlen(auth_info->values[1].string.text));
dcb445bc
MS
9524 cupsFilePutConf(fp, "password", line);
9525
9526 cupsdSetStringf(job->auth_env + 1, "AUTH_PASSWORD=%s",
9527 auth_info->values[1].string.text);
9528 }
09a101d6 9529 else if (con->username[0])
f7deaa1a 9530 {
9531 /*
9532 * Write the authenticated username...
9533 */
ef416fc2 9534
7e86f2f6 9535 httpEncode64_2(line, sizeof(line), con->username, (int)strlen(con->username));
dcb445bc 9536 cupsFilePutConf(fp, "username", line);
ef416fc2 9537
88f9aafc 9538 cupsdSetStringf(job->auth_env + 0, "AUTH_USERNAME=%s", con->username);
09a101d6 9539
f7deaa1a 9540 /*
9541 * Write the authenticated password...
9542 */
9543
7e86f2f6 9544 httpEncode64_2(line, sizeof(line), con->password, (int)strlen(con->password));
dcb445bc 9545 cupsFilePutConf(fp, "password", line);
09a101d6 9546
88f9aafc 9547 cupsdSetStringf(job->auth_env + 1, "AUTH_PASSWORD=%s", con->password);
f7deaa1a 9548 }
ef416fc2 9549
07ed0e9a 9550#ifdef HAVE_GSSAPI
eac3a0a0 9551 if (con->gss_uid > 0)
07ed0e9a 9552 {
dcb445bc 9553 cupsFilePrintf(fp, "uid %d\n", (int)con->gss_uid);
07ed0e9a
MS
9554 cupsdSetStringf(&job->auth_uid, "AUTH_UID=%d", (int)con->gss_uid);
9555 }
9556#endif /* HAVE_GSSAPI */
9557
ef416fc2 9558 /*
9559 * Write a random number of newlines to the end of the file...
9560 */
9561
41681883 9562 for (i = (CUPS_RAND() % 1024); i >= 0; i --)
ef416fc2 9563 cupsFilePutChar(fp, '\n');
9564
9565 /*
9566 * Close the file and return...
9567 */
9568
9569 cupsFileClose(fp);
9570}
9571
9572
9573/*
9574 * 'send_document()' - Send a file to a printer or class.
9575 */
9576
9577static void
9578send_document(cupsd_client_t *con, /* I - Client connection */
9579 ipp_attribute_t *uri) /* I - Printer URI */
9580{
9581 ipp_attribute_t *attr; /* Current attribute */
a0f6818e
MS
9582 ipp_attribute_t *format; /* Request's document-format attribute */
9583 ipp_attribute_t *jformat; /* Job's document-format attribute */
f7deaa1a 9584 const char *default_format;/* document-format-default value */
ef416fc2 9585 int jobid; /* Job ID number */
9586 cupsd_job_t *job; /* Current job */
9587 char job_uri[HTTP_MAX_URI],
9588 /* Job URI */
61cf44e2 9589 scheme[HTTP_MAX_URI],
ef416fc2 9590 /* Method portion of URI */
9591 username[HTTP_MAX_URI],
9592 /* Username portion of URI */
9593 host[HTTP_MAX_URI],
9594 /* Host portion of URI */
9595 resource[HTTP_MAX_URI];
9596 /* Resource portion of URI */
9597 int port; /* Port portion of URI */
9598 mime_type_t *filetype; /* Type of file */
9599 char super[MIME_MAX_SUPER],
9600 /* Supertype of file */
9601 type[MIME_MAX_TYPE],
9602 /* Subtype of file */
9603 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
9604 /* Textual name of mime type */
9605 char filename[1024]; /* Job filename */
9606 cupsd_printer_t *printer; /* Current printer */
9607 struct stat fileinfo; /* File information */
9608 int kbytes; /* Size of file */
9609 int compression; /* Type of compression */
75bd9771 9610 int start_job; /* Start the job? */
ef416fc2 9611
9612
9613 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con,
996acce8 9614 con->number, uri->values[0].string.text);
ef416fc2 9615
9616 /*
9617 * See if we have a job URI or a printer URI...
9618 */
9619
9620 if (!strcmp(uri->name, "printer-uri"))
9621 {
9622 /*
9623 * Got a printer URI; see if we also have a job-id attribute...
9624 */
9625
fa73b229 9626 if ((attr = ippFindAttribute(con->request, "job-id",
9627 IPP_TAG_INTEGER)) == NULL)
ef416fc2 9628 {
9629 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 9630 _("Got a printer-uri attribute but no job-id."));
ef416fc2 9631 return;
9632 }
9633
9634 jobid = attr->values[0].integer;
9635 }
9636 else
9637 {
9638 /*
9639 * Got a job URI; parse it to get the job ID...
9640 */
9641
61cf44e2
MS
9642 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9643 sizeof(scheme), username, sizeof(username), host,
a4d04587 9644 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 9645
9646 if (strncmp(resource, "/jobs/", 6))
9647 {
9648 /*
9649 * Not a valid URI!
9650 */
9651
84315f46 9652 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
ef416fc2 9653 uri->values[0].string.text);
9654 return;
9655 }
9656
9657 jobid = atoi(resource + 6);
9658 }
9659
9660 /*
9661 * See if the job exists...
9662 */
9663
9664 if ((job = cupsdFindJob(jobid)) == NULL)
9665 {
9666 /*
9667 * Nope - return a "not found" error...
9668 */
9669
84315f46 9670 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
ef416fc2 9671 return;
9672 }
9673
f301802f 9674 printer = cupsdFindDest(job->dest);
9675
ef416fc2 9676 /*
9677 * See if the job is owned by the requesting user...
9678 */
9679
9680 if (!validate_user(job, con, job->username, username, sizeof(username)))
9681 {
b0f6947b
MS
9682 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9683 cupsdFindDest(job->dest));
ef416fc2 9684 return;
9685 }
9686
9687 /*
9688 * OK, see if the client is sending the document compressed - CUPS
9689 * only supports "none" and "gzip".
9690 */
9691
9692 compression = CUPS_FILE_NONE;
9693
fa73b229 9694 if ((attr = ippFindAttribute(con->request, "compression",
9695 IPP_TAG_KEYWORD)) != NULL)
ef416fc2 9696 {
9697 if (strcmp(attr->values[0].string.text, "none")
9698#ifdef HAVE_LIBZ
9699 && strcmp(attr->values[0].string.text, "gzip")
9700#endif /* HAVE_LIBZ */
9701 )
9702 {
84315f46 9703 send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"."),
ef416fc2 9704 attr->values[0].string.text);
9705 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
9706 "compression", NULL, attr->values[0].string.text);
9707 return;
9708 }
9709
9710#ifdef HAVE_LIBZ
9711 if (!strcmp(attr->values[0].string.text, "gzip"))
9712 compression = CUPS_FILE_GZIP;
9713#endif /* HAVE_LIBZ */
9714 }
9715
9716 /*
9717 * Do we have a file to print?
9718 */
9719
83e08001
MS
9720 if ((attr = ippFindAttribute(con->request, "last-document",
9721 IPP_TAG_BOOLEAN)) == NULL)
9722 {
9723 send_ipp_status(con, IPP_BAD_REQUEST,
9724 _("Missing last-document attribute in request."));
9725 return;
9726 }
9727
ef416fc2 9728 if (!con->filename)
9729 {
6d2f911b
MS
9730 /*
9731 * Check for an empty request with "last-document" set to true, which is
9732 * used to close an "open" job by RFC 2911, section 3.3.2.
9733 */
9734
83e08001 9735 if (job->num_files > 0 && attr->values[0].boolean)
6d2f911b
MS
9736 goto last_document;
9737
84315f46 9738 send_ipp_status(con, IPP_BAD_REQUEST, _("No file in print request."));
ef416fc2 9739 return;
9740 }
9741
9742 /*
9743 * Is it a format we support?
9744 */
9745
9514a192
MS
9746 cupsdLoadJob(job);
9747
ef416fc2 9748 if ((format = ippFindAttribute(con->request, "document-format",
9749 IPP_TAG_MIMETYPE)) != NULL)
9750 {
9751 /*
9752 * Grab format from client...
9753 */
9754
c5b24bfa 9755 if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]",
bc44d920 9756 super, type) != 2)
ef416fc2 9757 {
84315f46 9758 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"."),
ef416fc2 9759 format->values[0].string.text);
9760 return;
9761 }
9514a192
MS
9762
9763 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-supplied", NULL, ippGetString(format, 0, NULL));
ef416fc2 9764 }
f7deaa1a 9765 else if ((default_format = cupsGetOption("document-format",
9766 printer->num_options,
9767 printer->options)) != NULL)
9768 {
9769 /*
9770 * Use default document format...
9771 */
9772
c5b24bfa 9773 if (sscanf(default_format, "%15[^/]/%255[^;]", super, type) != 2)
f7deaa1a 9774 {
9775 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 9776 _("Bad document-format-default \"%s\"."), default_format);
f7deaa1a 9777 return;
9778 }
9779 }
ef416fc2 9780 else
9781 {
9782 /*
9783 * No document format attribute? Auto-type it!
9784 */
9785
5a9febac
MS
9786 strlcpy(super, "application", sizeof(super));
9787 strlcpy(type, "octet-stream", sizeof(type));
ef416fc2 9788 }
9789
fa73b229 9790 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
ef416fc2 9791 {
9792 /*
9793 * Auto-type the file...
9794 */
9795
bd7854cb 9796 ipp_attribute_t *doc_name; /* document-name attribute */
9797
9798
75bd9771 9799 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Auto-typing file...");
ef416fc2 9800
bd7854cb 9801 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
9802 filetype = mimeFileType(MimeDatabase, con->filename,
9803 doc_name ? doc_name->values[0].string.text : NULL,
9804 &compression);
ef416fc2 9805
f7deaa1a 9806 if (!filetype)
f7faf1f5 9807 filetype = mimeType(MimeDatabase, super, type);
a0f6818e 9808
0268488e
MS
9809 if (filetype)
9810 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Request file type is %s/%s.",
9811 filetype->super, filetype->type);
9514a192
MS
9812
9813 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, filetype->type);
9814 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-detected", NULL, mimetype);
ed486911 9815 }
f7faf1f5 9816 else
9817 filetype = mimeType(MimeDatabase, super, type);
9818
49d87452 9819 if (filetype)
f7deaa1a 9820 {
9821 /*
9822 * Replace the document-format attribute value with the auto-typed or
9823 * default one.
9824 */
9825
9826 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9827 filetype->type);
9828
49d87452
MS
9829 if ((jformat = ippFindAttribute(job->attrs, "document-format",
9830 IPP_TAG_MIMETYPE)) != NULL)
5e6c3df7 9831 ippSetString(job->attrs, &jformat, 0, mimetype);
f7deaa1a 9832 else
a0f6818e 9833 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
f7deaa1a 9834 "document-format", NULL, mimetype);
9835 }
9836 else if (!filetype)
ef416fc2 9837 {
9838 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
84315f46 9839 _("Unsupported document-format \"%s/%s\"."), super, type);
ef416fc2 9840 cupsdLogMessage(CUPSD_LOG_INFO,
9841 "Hint: Do you have the raw file printing rules enabled?");
9842
9843 if (format)
9844 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9845 "document-format", NULL, format->values[0].string.text);
9846
9847 return;
9848 }
9849
80ca4592 9850 if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype))
9851 {
9852 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9853 filetype->type);
9854
9855 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
84315f46 9856 _("Unsupported document-format \"%s\"."), mimetype);
80ca4592 9857
9858 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9859 "document-format", NULL, mimetype);
9860
9861 return;
9862 }
9863
ef416fc2 9864 /*
9865 * Add the file to the job...
9866 */
9867
9868 if (add_file(con, job, filetype, compression))
9869 return;
9870
9514a192
MS
9871 if ((attr = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME)) != NULL)
9872 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "document-name-supplied", NULL, ippGetString(attr, 0, NULL));
9873
ef416fc2 9874 if (stat(con->filename, &fileinfo))
9875 kbytes = 0;
9876 else
9877 kbytes = (fileinfo.st_size + 1023) / 1024;
9878
9879 cupsdUpdateQuota(printer, job->username, 0, kbytes);
9880
f16ea703
MS
9881 job->koctets += kbytes;
9882
9883 if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL)
ef416fc2 9884 attr->values[0].integer += kbytes;
9885
6d8021f4
MS
9886 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, job->num_files);
9887 if (rename(con->filename, filename))
9888 {
9889 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to rename job document file \"%s\": %s", filename, strerror(errno));
9890
9891 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to rename job document file."));
9892 return;
9893 }
ef416fc2 9894
9895 cupsdClearString(&con->filename);
9896
75bd9771
MS
9897 cupsdLogJob(job, CUPSD_LOG_INFO, "File of type %s/%s queued by \"%s\".",
9898 filetype->super, filetype->type, job->username);
ef416fc2 9899
9900 /*
9901 * Start the job if this is the last document...
9902 */
9903
6d2f911b
MS
9904 last_document:
9905
fa73b229 9906 if ((attr = ippFindAttribute(con->request, "last-document",
9907 IPP_TAG_BOOLEAN)) != NULL &&
ef416fc2 9908 attr->values[0].boolean)
9909 {
9910 /*
9911 * See if we need to add the ending sheet...
9912 */
9913
91c84a35
MS
9914 if (cupsdTimeoutJob(job))
9915 return;
ef416fc2 9916
bd7854cb 9917 if (job->state_value == IPP_JOB_STOPPED)
9918 {
ef416fc2 9919 job->state->values[0].integer = IPP_JOB_PENDING;
bd7854cb 9920 job->state_value = IPP_JOB_PENDING;
12f89d24
MS
9921
9922 ippSetString(job->attrs, &job->reasons, 0, "none");
bd7854cb 9923 }
9924 else if (job->state_value == IPP_JOB_HELD)
ef416fc2 9925 {
fa73b229 9926 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9927 IPP_TAG_KEYWORD)) == NULL)
ef416fc2 9928 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9929
fa73b229 9930 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
bd7854cb 9931 {
ef416fc2 9932 job->state->values[0].integer = IPP_JOB_PENDING;
bd7854cb 9933 job->state_value = IPP_JOB_PENDING;
12f89d24
MS
9934
9935 ippSetString(job->attrs, &job->reasons, 0, "none");
bd7854cb 9936 }
12f89d24
MS
9937 else
9938 ippSetString(job->attrs, &job->reasons, 0, "job-hold-until-specified");
ef416fc2 9939 }
9940
3dfe78b3
MS
9941 job->dirty = 1;
9942 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
ef416fc2 9943
75bd9771 9944 start_job = 1;
ef416fc2 9945 }
9946 else
9947 {
fa73b229 9948 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9949 IPP_TAG_KEYWORD)) == NULL)
ef416fc2 9950 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9951
fa73b229 9952 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
ef416fc2 9953 {
9954 job->state->values[0].integer = IPP_JOB_HELD;
bd7854cb 9955 job->state_value = IPP_JOB_HELD;
dfd5680b 9956 job->hold_until = time(NULL) + MultipleOperationTimeout;
3dfe78b3 9957
12f89d24
MS
9958 ippSetString(job->attrs, &job->reasons, 0, "job-incoming");
9959
9960 job->dirty = 1;
3dfe78b3 9961 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
ef416fc2 9962 }
75bd9771
MS
9963
9964 start_job = 0;
ef416fc2 9965 }
9966
9967 /*
9968 * Fill in the response info...
9969 */
9970
83e08001 9971 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
f2534050 9972 con->clientname, con->clientport, "/jobs/%d", jobid);
ef416fc2 9973 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
9974 job_uri);
9975
9976 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid);
9977
9978 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
75bd9771 9979 job->state_value);
12f89d24
MS
9980 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons",
9981 NULL, job->reasons->values[0].string.text);
ef416fc2 9982
9983 con->response->request.status.status_code = IPP_OK;
75bd9771
MS
9984
9985 /*
9986 * Start the job if necessary...
9987 */
9988
9989 if (start_job)
9990 cupsdCheckJobs();
ef416fc2 9991}
9992
9993
9994/*
9995 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9996 */
9997
9998static void
f899b121 9999send_http_error(
10000 cupsd_client_t *con, /* I - Client connection */
10001 http_status_t status, /* I - HTTP status code */
10002 cupsd_printer_t *printer) /* I - Printer, if any */
ef416fc2 10003{
9f5eb9be
MS
10004 ipp_attribute_t *uri; /* Request URI, if any */
10005
10006
10007 if ((uri = ippFindAttribute(con->request, "printer-uri",
10008 IPP_TAG_URI)) == NULL)
10009 uri = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI);
10010
10011 cupsdLogMessage(status == HTTP_FORBIDDEN ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
21f36711 10012 "[Client %d] Returning HTTP %s for %s (%s) from %s",
996acce8 10013 con->number, httpStatus(status),
321d8d57
MS
10014 con->request ?
10015 ippOpString(con->request->request.op.operation_id) :
10016 "no operation-id",
9f5eb9be 10017 uri ? uri->values[0].string.text : "no URI",
996acce8 10018 con->http->hostname);
ef416fc2 10019
5f64df29
MS
10020 if (printer)
10021 {
db0bd74a 10022 int auth_type; /* Type of authentication required */
2fb76298
MS
10023
10024
5f64df29
MS
10025 auth_type = CUPSD_AUTH_NONE;
10026
10027 if (status == HTTP_UNAUTHORIZED &&
10028 printer->num_auth_info_required > 0 &&
10029 !strcmp(printer->auth_info_required[0], "negotiate") &&
10030 con->request &&
10031 (con->request->request.op.operation_id == IPP_PRINT_JOB ||
10032 con->request->request.op.operation_id == IPP_CREATE_JOB ||
10033 con->request->request.op.operation_id == CUPS_AUTHENTICATE_JOB))
10034 {
10035 /*
10036 * Creating and authenticating jobs requires Kerberos...
10037 */
10038
10039 auth_type = CUPSD_AUTH_NEGOTIATE;
10040 }
db0bd74a 10041 else
5f64df29
MS
10042 {
10043 /*
10044 * Use policy/location-defined authentication requirements...
10045 */
10046
10047 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
10048 cupsd_location_t *auth; /* Pointer to authentication element */
10049
10050
10051 if (printer->type & CUPS_PRINTER_CLASS)
10052 snprintf(resource, sizeof(resource), "/classes/%s", printer->name);
10053 else
10054 snprintf(resource, sizeof(resource), "/printers/%s", printer->name);
10055
10056 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
10057 auth->type == CUPSD_AUTH_NONE)
10058 auth = cupsdFindPolicyOp(printer->op_policy_ptr,
10059 con->request ?
10060 con->request->request.op.operation_id :
10061 IPP_PRINT_JOB);
10062
10063 if (auth)
10064 {
10065 if (auth->type == CUPSD_AUTH_DEFAULT)
dcb445bc 10066 auth_type = cupsdDefaultAuthType();
5f64df29
MS
10067 else
10068 auth_type = auth->type;
10069 }
10070 }
2fb76298 10071
db0bd74a 10072 cupsdSendError(con, status, auth_type);
2fb76298 10073 }
f899b121 10074 else
5bd77a73 10075 cupsdSendError(con, status, CUPSD_AUTH_NONE);
ef416fc2 10076
10077 ippDelete(con->response);
10078 con->response = NULL;
10079
10080 return;
10081}
10082
10083
10084/*
10085 * 'send_ipp_status()' - Send a status back to the IPP client.
10086 */
10087
10088static void
10089send_ipp_status(cupsd_client_t *con, /* I - Client connection */
1f0275e3
MS
10090 ipp_status_t status, /* I - IPP status code */
10091 const char *message,/* I - Status message */
10092 ...) /* I - Additional args as needed */
ef416fc2 10093{
10094 va_list ap; /* Pointer to additional args */
10095 char formatted[1024]; /* Formatted errror message */
10096
10097
3d8365b8 10098 va_start(ap, message);
10099 vsnprintf(formatted, sizeof(formatted),
10100 _cupsLangString(con->language, message), ap);
10101 va_end(ap);
ef416fc2 10102
3d8365b8 10103 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s",
10104 ippOpString(con->request->request.op.operation_id),
10105 ippErrorString(status), formatted);
ef416fc2 10106
10107 con->response->request.status.status_code = status;
10108
fa73b229 10109 if (ippFindAttribute(con->response, "attributes-charset",
10110 IPP_TAG_ZERO) == NULL)
ef416fc2 10111 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
f8b3a85b 10112 "attributes-charset", NULL, "utf-8");
ef416fc2 10113
10114 if (ippFindAttribute(con->response, "attributes-natural-language",
10115 IPP_TAG_ZERO) == NULL)
10116 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
10117 "attributes-natural-language", NULL, DefaultLanguage);
10118
3d8365b8 10119 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT,
10120 "status-message", NULL, formatted);
ef416fc2 10121}
10122
10123
10124/*
10125 * 'set_default()' - Set the default destination...
10126 */
10127
10128static void
10129set_default(cupsd_client_t *con, /* I - Client connection */
10130 ipp_attribute_t *uri) /* I - Printer URI */
10131{
10132 http_status_t status; /* Policy status */
bc44d920 10133 cups_ptype_t dtype; /* Destination type (printer/class) */
dd1abb6b
MS
10134 cupsd_printer_t *printer, /* Printer */
10135 *oldprinter; /* Old default printer */
ef416fc2 10136
10137
10138 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con,
996acce8 10139 con->number, uri->values[0].string.text);
ef416fc2 10140
10141 /*
10142 * Is the destination valid?
10143 */
10144
f7deaa1a 10145 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 10146 {
10147 /*
10148 * Bad URI...
10149 */
10150
10151 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 10152 _("The printer or class does not exist."));
ef416fc2 10153 return;
10154 }
10155
10156 /*
10157 * Check policy...
10158 */
10159
10160 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
10161 {
f899b121 10162 send_http_error(con, status, NULL);
ef416fc2 10163 return;
10164 }
10165
10166 /*
10167 * Set it as the default...
10168 */
10169
dd1abb6b 10170 oldprinter = DefaultPrinter;
ef416fc2 10171 DefaultPrinter = printer;
10172
dd1abb6b
MS
10173 if (oldprinter)
10174 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, oldprinter, NULL,
10175 "%s is no longer the default printer.", oldprinter->name);
10176
10177 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
10178 "%s is now the default printer.", printer->name);
10179
3dfe78b3 10180 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES |
a2326b5b 10181 CUPSD_DIRTY_PRINTCAP);
ef416fc2 10182
10183 cupsdLogMessage(CUPSD_LOG_INFO,
f7deaa1a 10184 "Default destination set to \"%s\" by \"%s\".",
10185 printer->name, get_username(con));
ef416fc2 10186
10187 /*
10188 * Everything was ok, so return OK status...
10189 */
10190
10191 con->response->request.status.status_code = IPP_OK;
10192}
10193
10194
10195/*
10196 * 'set_job_attrs()' - Set job attributes.
10197 */
10198
10199static void
10200set_job_attrs(cupsd_client_t *con, /* I - Client connection */
10201 ipp_attribute_t *uri) /* I - Job URI */
10202{
10203 ipp_attribute_t *attr, /* Current attribute */
10204 *attr2; /* Job attribute */
10205 int jobid; /* Job ID */
10206 cupsd_job_t *job; /* Current job */
61cf44e2 10207 char scheme[HTTP_MAX_URI],
ef416fc2 10208 /* Method portion of URI */
10209 username[HTTP_MAX_URI],
10210 /* Username portion of URI */
10211 host[HTTP_MAX_URI],
10212 /* Host portion of URI */
10213 resource[HTTP_MAX_URI];
10214 /* Resource portion of URI */
10215 int port; /* Port portion of URI */
d09495fa 10216 int event; /* Events? */
005dd1eb 10217 int check_jobs; /* Check jobs? */
ef416fc2 10218
10219
10220 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con,
996acce8 10221 con->number, uri->values[0].string.text);
ef416fc2 10222
10223 /*
10224 * Start with "everything is OK" status...
10225 */
10226
10227 con->response->request.status.status_code = IPP_OK;
10228
10229 /*
10230 * See if we have a job URI or a printer URI...
10231 */
10232
fa73b229 10233 if (!strcmp(uri->name, "printer-uri"))
ef416fc2 10234 {
10235 /*
10236 * Got a printer URI; see if we also have a job-id attribute...
10237 */
10238
fa73b229 10239 if ((attr = ippFindAttribute(con->request, "job-id",
10240 IPP_TAG_INTEGER)) == NULL)
ef416fc2 10241 {
10242 send_ipp_status(con, IPP_BAD_REQUEST,
84315f46 10243 _("Got a printer-uri attribute but no job-id."));
ef416fc2 10244 return;
10245 }
10246
10247 jobid = attr->values[0].integer;
10248 }
10249 else
10250 {
10251 /*
10252 * Got a job URI; parse it to get the job ID...
10253 */
10254
61cf44e2
MS
10255 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
10256 sizeof(scheme), username, sizeof(username), host,
a4d04587 10257 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 10258
fa73b229 10259 if (strncmp(resource, "/jobs/", 6))
ef416fc2 10260 {
10261 /*
10262 * Not a valid URI!
10263 */
10264
84315f46 10265 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
ef416fc2 10266 uri->values[0].string.text);
10267 return;
10268 }
10269
10270 jobid = atoi(resource + 6);
10271 }
10272
10273 /*
10274 * See if the job exists...
10275 */
10276
10277 if ((job = cupsdFindJob(jobid)) == NULL)
10278 {
10279 /*
10280 * Nope - return a "not found" error...
10281 */
10282
84315f46 10283 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
ef416fc2 10284 return;
10285 }
10286
10287 /*
10288 * See if the job has been completed...
10289 */
10290
bd7854cb 10291 if (job->state_value > IPP_JOB_STOPPED)
ef416fc2 10292 {
10293 /*
10294 * Return a "not-possible" error...
10295 */
10296
10297 send_ipp_status(con, IPP_NOT_POSSIBLE,
84315f46 10298 _("Job #%d is finished and cannot be altered."), jobid);
ef416fc2 10299 return;
10300 }
10301
10302 /*
10303 * See if the job is owned by the requesting user...
10304 */
10305
10306 if (!validate_user(job, con, job->username, username, sizeof(username)))
10307 {
b0f6947b
MS
10308 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
10309 cupsdFindDest(job->dest));
ef416fc2 10310 return;
10311 }
10312
10313 /*
10314 * See what the user wants to change.
10315 */
10316
bd7854cb 10317 cupsdLoadJob(job);
10318
005dd1eb
MS
10319 check_jobs = 0;
10320 event = 0;
d09495fa 10321
fa73b229 10322 for (attr = con->request->attrs; attr; attr = attr->next)
ef416fc2 10323 {
10324 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
10325 continue;
10326
10327 if (!strcmp(attr->name, "attributes-charset") ||
10328 !strcmp(attr->name, "attributes-natural-language") ||
9514a192
MS
10329 !strncmp(attr->name, "date-time-at-", 13) ||
10330 !strncmp(attr->name, "document-compression", 20) ||
10331 !strncmp(attr->name, "document-format", 15) ||
ef416fc2 10332 !strcmp(attr->name, "job-detailed-status-messages") ||
10333 !strcmp(attr->name, "job-document-access-errors") ||
10334 !strcmp(attr->name, "job-id") ||
5d6412a9 10335 !strcmp(attr->name, "job-impressions-completed") ||
9514a192
MS
10336 !strcmp(attr->name, "job-k-octets-completed") ||
10337 !strcmp(attr->name, "job-media-sheets-completed") ||
ef416fc2 10338 !strcmp(attr->name, "job-originating-host-name") ||
10339 !strcmp(attr->name, "job-originating-user-name") ||
9514a192 10340 !strcmp(attr->name, "job-pages-completed") ||
ef416fc2 10341 !strcmp(attr->name, "job-printer-up-time") ||
10342 !strcmp(attr->name, "job-printer-uri") ||
10343 !strcmp(attr->name, "job-sheets") ||
10344 !strcmp(attr->name, "job-state-message") ||
10345 !strcmp(attr->name, "job-state-reasons") ||
10346 !strcmp(attr->name, "job-uri") ||
10347 !strcmp(attr->name, "number-of-documents") ||
10348 !strcmp(attr->name, "number-of-intervening-jobs") ||
10349 !strcmp(attr->name, "output-device-assigned") ||
ef416fc2 10350 !strncmp(attr->name, "time-at-", 8))
10351 {
10352 /*
10353 * Read-only attrs!
10354 */
10355
10356 send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE,
10357 _("%s cannot be changed."), attr->name);
10358
a2326b5b
MS
10359 attr2 = ippCopyAttribute(con->response, attr, 0);
10360 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
ef416fc2 10361 continue;
10362 }
10363
4c37eb9f
MS
10364 if (!ippValidateAttribute(attr))
10365 {
10366 send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Bad '%s' value."), attr->name);
10367 ippCopyAttribute(con->response, attr, 0);
10368 return;
10369 }
10370
10371 if (!strcmp(attr->name, "job-hold-until"))
10372 {
10373 const char *when = ippGetString(attr, 0, NULL);
10374 /* job-hold-until value */
10375
10376 if ((ippGetValueTag(attr) != IPP_TAG_KEYWORD && ippGetValueTag(attr) != IPP_TAG_NAME && ippGetValueTag(attr) != IPP_TAG_NAMELANG) || ippGetCount(attr) != 1)
10377 {
10378 send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Unsupported 'job-hold-until' value."));
10379 ippCopyAttribute(con->response, attr, 0);
10380 return;
10381 }
10382
10383 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s", when);
10384 cupsdSetJobHoldUntil(job, when, 0);
10385
10386 if (!strcmp(when, "no-hold"))
10387 {
10388 cupsdReleaseJob(job);
10389 check_jobs = 1;
10390 }
10391 else
10392 cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT, "Job held by \"%s\".", username);
10393
10394 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
10395 }
10396 else if (!strcmp(attr->name, "job-priority"))
ef416fc2 10397 {
10398 /*
10399 * Change the job priority...
10400 */
10401
10402 if (attr->value_tag != IPP_TAG_INTEGER)
10403 {
84315f46 10404 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value."));
ef416fc2 10405
a2326b5b
MS
10406 attr2 = ippCopyAttribute(con->response, attr, 0);
10407 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
ef416fc2 10408 }
bd7854cb 10409 else if (job->state_value >= IPP_JOB_PROCESSING)
ef416fc2 10410 {
10411 send_ipp_status(con, IPP_NOT_POSSIBLE,
10412 _("Job is completed and cannot be changed."));
10413 return;
10414 }
10415 else if (con->response->request.status.status_code == IPP_OK)
d09495fa 10416 {
005dd1eb
MS
10417 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-priority to %d",
10418 attr->values[0].integer);
ef416fc2 10419 cupsdSetJobPriority(job, attr->values[0].integer);
005dd1eb
MS
10420
10421 check_jobs = 1;
10422 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED |
10423 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED;
d09495fa 10424 }
ef416fc2 10425 }
10426 else if (!strcmp(attr->name, "job-state"))
10427 {
10428 /*
10429 * Change the job state...
10430 */
10431
10432 if (attr->value_tag != IPP_TAG_ENUM)
10433 {
84315f46 10434 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value."));
ef416fc2 10435
a2326b5b
MS
10436 attr2 = ippCopyAttribute(con->response, attr, 0);
10437 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
ef416fc2 10438 }
10439 else
10440 {
10441 switch (attr->values[0].integer)
10442 {
10443 case IPP_JOB_PENDING :
10444 case IPP_JOB_HELD :
bd7854cb 10445 if (job->state_value > IPP_JOB_HELD)
ef416fc2 10446 {
10447 send_ipp_status(con, IPP_NOT_POSSIBLE,
10448 _("Job state cannot be changed."));
10449 return;
10450 }
10451 else if (con->response->request.status.status_code == IPP_OK)
bd7854cb 10452 {
005dd1eb
MS
10453 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10454 attr->values[0].integer);
7e86f2f6 10455 cupsdSetJobState(job, (ipp_jstate_t)attr->values[0].integer, CUPSD_JOB_DEFAULT, "Job state changed by \"%s\"", username);
005dd1eb 10456 check_jobs = 1;
bd7854cb 10457 }
ef416fc2 10458 break;
10459
10460 case IPP_JOB_PROCESSING :
10461 case IPP_JOB_STOPPED :
bd7854cb 10462 if (job->state_value != attr->values[0].integer)
ef416fc2 10463 {
10464 send_ipp_status(con, IPP_NOT_POSSIBLE,
10465 _("Job state cannot be changed."));
10466 return;
10467 }
10468 break;
10469
d09495fa 10470 case IPP_JOB_CANCELED :
ef416fc2 10471 case IPP_JOB_ABORTED :
10472 case IPP_JOB_COMPLETED :
bd7854cb 10473 if (job->state_value > IPP_JOB_PROCESSING)
ef416fc2 10474 {
10475 send_ipp_status(con, IPP_NOT_POSSIBLE,
10476 _("Job state cannot be changed."));
10477 return;
10478 }
10479 else if (con->response->request.status.status_code == IPP_OK)
005dd1eb
MS
10480 {
10481 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10482 attr->values[0].integer);
b9faaae1
MS
10483 cupsdSetJobState(job, (ipp_jstate_t)attr->values[0].integer,
10484 CUPSD_JOB_DEFAULT,
10485 "Job state changed by \"%s\"", username);
005dd1eb
MS
10486 check_jobs = 1;
10487 }
ef416fc2 10488 break;
10489 }
10490 }
10491 }
10492 else if (con->response->request.status.status_code != IPP_OK)
10493 continue;
fa73b229 10494 else if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10495 IPP_TAG_ZERO)) != NULL)
ef416fc2 10496 {
10497 /*
10498 * Some other value; first free the old value...
10499 */
10500
10501 if (job->attrs->prev)
10502 job->attrs->prev->next = attr2->next;
10503 else
10504 job->attrs->attrs = attr2->next;
10505
10506 if (job->attrs->last == attr2)
10507 job->attrs->last = job->attrs->prev;
10508
a2326b5b 10509 ippDeleteAttribute(NULL, attr2);
ef416fc2 10510
10511 /*
10512 * Then copy the attribute...
10513 */
10514
a2326b5b 10515 ippCopyAttribute(job->attrs, attr, 0);
ef416fc2 10516 }
10517 else if (attr->value_tag == IPP_TAG_DELETEATTR)
10518 {
10519 /*
10520 * Delete the attribute...
10521 */
10522
10523 if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10524 IPP_TAG_ZERO)) != NULL)
10525 {
10526 if (job->attrs->prev)
10527 job->attrs->prev->next = attr2->next;
10528 else
10529 job->attrs->attrs = attr2->next;
10530
10531 if (attr2 == job->attrs->last)
10532 job->attrs->last = job->attrs->prev;
10533
a2326b5b 10534 ippDeleteAttribute(NULL, attr2);
d09495fa 10535
10536 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
ef416fc2 10537 }
10538 }
10539 else
10540 {
10541 /*
10542 * Add new option by copying it...
10543 */
10544
a2326b5b 10545 ippCopyAttribute(job->attrs, attr, 0);
d09495fa 10546
10547 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
ef416fc2 10548 }
10549 }
10550
10551 /*
10552 * Save the job...
10553 */
10554
3dfe78b3
MS
10555 job->dirty = 1;
10556 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
ef416fc2 10557
d09495fa 10558 /*
10559 * Send events as needed...
10560 */
10561
d9bca400 10562 if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED)
01ce6322
MS
10563 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED,
10564 cupsdFindDest(job->dest), job,
d9bca400
MS
10565 "Job priority changed by user.");
10566
d09495fa 10567 if (event & CUPSD_EVENT_JOB_STATE)
01ce6322 10568 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
d09495fa 10569 job->state_value == IPP_JOB_HELD ?
10570 "Job held by user." : "Job restarted by user.");
10571
10572 if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED)
01ce6322 10573 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
d09495fa 10574 "Job options changed by user.");
10575
ef416fc2 10576 /*
10577 * Start jobs if possible...
10578 */
10579
005dd1eb
MS
10580 if (check_jobs)
10581 cupsdCheckJobs();
ef416fc2 10582}
10583
10584
c168a833
MS
10585/*
10586 * 'set_printer_attrs()' - Set printer attributes.
10587 */
10588
10589static void
10590set_printer_attrs(cupsd_client_t *con, /* I - Client connection */
10591 ipp_attribute_t *uri) /* I - Printer */
10592{
10593 http_status_t status; /* Policy status */
10594 cups_ptype_t dtype; /* Destination type (printer/class) */
10595 cupsd_printer_t *printer; /* Printer/class */
10596 ipp_attribute_t *attr; /* Printer attribute */
bf3816c7 10597 int changed = 0; /* Was anything changed? */
c168a833
MS
10598
10599
10600 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_attrs(%p[%d], %s)", con,
996acce8 10601 con->number, uri->values[0].string.text);
c168a833
MS
10602
10603 /*
10604 * Is the destination valid?
10605 */
10606
10607 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10608 {
10609 /*
10610 * Bad URI...
10611 */
10612
10613 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 10614 _("The printer or class does not exist."));
c168a833
MS
10615 return;
10616 }
10617
10618 /*
10619 * Check policy...
10620 */
10621
10622 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10623 {
10624 send_http_error(con, status, printer);
10625 return;
10626 }
10627
10628 /*
10629 * Return a list of attributes that can be set via Set-Printer-Attributes.
10630 */
10631
10632 if ((attr = ippFindAttribute(con->request, "printer-location",
10633 IPP_TAG_TEXT)) != NULL)
10634 {
10635 cupsdSetString(&printer->location, attr->values[0].string.text);
10636 changed = 1;
10637 }
10638
9b4bd602
MS
10639 if ((attr = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI)) != NULL && !strncmp(attr->values[0].string.text, "geo:", 4))
10640 {
10641 cupsdSetString(&printer->geo_location, attr->values[0].string.text);
10642 changed = 1;
10643 }
10644
10645 if ((attr = ippFindAttribute(con->request, "printer-organization", IPP_TAG_TEXT)) != NULL)
10646 {
10647 cupsdSetString(&printer->organization, attr->values[0].string.text);
10648 changed = 1;
10649 }
10650
10651 if ((attr = ippFindAttribute(con->request, "printer-organizational-unit", IPP_TAG_TEXT)) != NULL)
10652 {
10653 cupsdSetString(&printer->organizational_unit, attr->values[0].string.text);
10654 changed = 1;
10655 }
10656
c168a833
MS
10657 if ((attr = ippFindAttribute(con->request, "printer-info",
10658 IPP_TAG_TEXT)) != NULL)
10659 {
10660 cupsdSetString(&printer->info, attr->values[0].string.text);
10661 changed = 1;
10662 }
10663
10664 /*
10665 * Update the printer attributes and return...
10666 */
10667
10668 if (changed)
10669 {
9b4bd602
MS
10670 printer->config_time = time(NULL);
10671
c168a833
MS
10672 cupsdSetPrinterAttrs(printer);
10673 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
10674
10675 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL,
10676 "Printer \"%s\" description or location changed by \"%s\".",
10677 printer->name, get_username(con));
10678
10679 cupsdLogMessage(CUPSD_LOG_INFO,
10680 "Printer \"%s\" description or location changed by \"%s\".",
10681 printer->name, get_username(con));
10682 }
10683
10684 con->response->request.status.status_code = IPP_OK;
10685}
10686
10687
b423cd4c 10688/*
10689 * 'set_printer_defaults()' - Set printer default options from a request.
10690 */
10691
f0b589f7 10692static int /* O - 1 on success, 0 on failure */
b423cd4c 10693set_printer_defaults(
10694 cupsd_client_t *con, /* I - Client connection */
10695 cupsd_printer_t *printer) /* I - Printer */
10696{
10697 int i; /* Looping var */
10698 ipp_attribute_t *attr; /* Current attribute */
7e86f2f6 10699 size_t namelen; /* Length of attribute name */
b423cd4c 10700 char name[256], /* New attribute name */
10701 value[256]; /* String version of integer attrs */
10702
10703
10704 for (attr = con->request->attrs; attr; attr = attr->next)
10705 {
10706 /*
10707 * Skip non-printer attributes...
10708 */
10709
10710 if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
10711 continue;
10712
10713 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name);
10714
10715 if (!strcmp(attr->name, "job-sheets-default"))
10716 {
10717 /*
10718 * Only allow keywords and names...
10719 */
10720
10721 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10722 continue;
10723
10724 /*
10725 * Only allow job-sheets-default to be set when running without a
10726 * system high classification level...
10727 */
10728
10729 if (Classification)
10730 continue;
10731
10732 cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text);
10733
10734 if (attr->num_values > 1)
10735 cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text);
10736 else
10737 cupsdSetString(&printer->job_sheets[1], "none");
10738 }
10739 else if (!strcmp(attr->name, "requesting-user-name-allowed"))
10740 {
10d09e33 10741 cupsdFreeStrings(&(printer->users));
b423cd4c 10742
10743 printer->deny_users = 0;
10744
10745 if (attr->value_tag == IPP_TAG_NAME &&
10746 (attr->num_values > 1 ||
10747 strcmp(attr->values[0].string.text, "all")))
10748 {
10749 for (i = 0; i < attr->num_values; i ++)
10d09e33 10750 cupsdAddString(&(printer->users), attr->values[i].string.text);
b423cd4c 10751 }
10752 }
10753 else if (!strcmp(attr->name, "requesting-user-name-denied"))
10754 {
10d09e33 10755 cupsdFreeStrings(&(printer->users));
b423cd4c 10756
10757 printer->deny_users = 1;
10758
10759 if (attr->value_tag == IPP_TAG_NAME &&
10760 (attr->num_values > 1 ||
10761 strcmp(attr->values[0].string.text, "none")))
10762 {
10763 for (i = 0; i < attr->num_values; i ++)
10d09e33 10764 cupsdAddString(&(printer->users), attr->values[i].string.text);
b423cd4c 10765 }
10766 }
10767 else if (!strcmp(attr->name, "job-quota-period"))
10768 {
10769 if (attr->value_tag != IPP_TAG_INTEGER)
10770 continue;
10771
10772 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...",
10773 attr->values[0].integer);
10774 cupsdFreeQuotas(printer);
10775
10776 printer->quota_period = attr->values[0].integer;
10777 }
10778 else if (!strcmp(attr->name, "job-k-limit"))
10779 {
10780 if (attr->value_tag != IPP_TAG_INTEGER)
10781 continue;
10782
10783 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...",
10784 attr->values[0].integer);
10785 cupsdFreeQuotas(printer);
10786
10787 printer->k_limit = attr->values[0].integer;
10788 }
10789 else if (!strcmp(attr->name, "job-page-limit"))
10790 {
10791 if (attr->value_tag != IPP_TAG_INTEGER)
10792 continue;
10793
10794 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...",
10795 attr->values[0].integer);
10796 cupsdFreeQuotas(printer);
10797
10798 printer->page_limit = attr->values[0].integer;
10799 }
10800 else if (!strcmp(attr->name, "printer-op-policy"))
10801 {
10802 cupsd_policy_t *p; /* Policy */
10803
10804
10805 if (attr->value_tag != IPP_TAG_NAME)
10806 continue;
10807
10808 if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
10809 {
10810 cupsdLogMessage(CUPSD_LOG_DEBUG,
10811 "Setting printer-op-policy to \"%s\"...",
10812 attr->values[0].string.text);
10813 cupsdSetString(&printer->op_policy, attr->values[0].string.text);
10814 printer->op_policy_ptr = p;
10815 }
10816 else
10817 {
10818 send_ipp_status(con, IPP_NOT_POSSIBLE,
10819 _("Unknown printer-op-policy \"%s\"."),
10820 attr->values[0].string.text);
f0b589f7 10821 return (0);
b423cd4c 10822 }
10823 }
10824 else if (!strcmp(attr->name, "printer-error-policy"))
10825 {
10826 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10827 continue;
10828
f11a948a 10829 if (strcmp(attr->values[0].string.text, "retry-current-job") &&
a2326b5b 10830 ((printer->type & CUPS_PRINTER_CLASS) ||
f11a948a
MS
10831 (strcmp(attr->values[0].string.text, "abort-job") &&
10832 strcmp(attr->values[0].string.text, "retry-job") &&
10833 strcmp(attr->values[0].string.text, "stop-printer"))))
b423cd4c 10834 {
10835 send_ipp_status(con, IPP_NOT_POSSIBLE,
10836 _("Unknown printer-error-policy \"%s\"."),
10837 attr->values[0].string.text);
f0b589f7 10838 return (0);
b423cd4c 10839 }
10840
10841 cupsdLogMessage(CUPSD_LOG_DEBUG,
10842 "Setting printer-error-policy to \"%s\"...",
10843 attr->values[0].string.text);
10844 cupsdSetString(&printer->error_policy, attr->values[0].string.text);
10845 }
b423cd4c 10846
10847 /*
10848 * Skip any other non-default attributes...
10849 */
10850
10851 namelen = strlen(attr->name);
10852 if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") ||
10853 namelen > (sizeof(name) - 1) || attr->num_values != 1)
10854 continue;
10855
10856 /*
10857 * OK, anything else must be a user-defined default...
10858 */
10859
10860 strlcpy(name, attr->name, sizeof(name));
10861 name[namelen - 8] = '\0'; /* Strip "-default" */
10862
10863 switch (attr->value_tag)
10864 {
10865 case IPP_TAG_DELETEATTR :
10866 printer->num_options = cupsRemoveOption(name,
10867 printer->num_options,
10868 &(printer->options));
10869 cupsdLogMessage(CUPSD_LOG_DEBUG,
10870 "Deleting %s", attr->name);
10871 break;
10872
10873 case IPP_TAG_NAME :
5a9febac 10874 case IPP_TAG_TEXT :
b423cd4c 10875 case IPP_TAG_KEYWORD :
10876 case IPP_TAG_URI :
10877 printer->num_options = cupsAddOption(name,
10878 attr->values[0].string.text,
10879 printer->num_options,
10880 &(printer->options));
10881 cupsdLogMessage(CUPSD_LOG_DEBUG,
10882 "Setting %s to \"%s\"...", attr->name,
10883 attr->values[0].string.text);
10884 break;
10885
10886 case IPP_TAG_BOOLEAN :
10887 printer->num_options = cupsAddOption(name,
10888 attr->values[0].boolean ?
10889 "true" : "false",
10890 printer->num_options,
10891 &(printer->options));
10892 cupsdLogMessage(CUPSD_LOG_DEBUG,
10893 "Setting %s to %s...", attr->name,
10894 attr->values[0].boolean ? "true" : "false");
10895 break;
10896
10897 case IPP_TAG_INTEGER :
10898 case IPP_TAG_ENUM :
10899 sprintf(value, "%d", attr->values[0].integer);
10900 printer->num_options = cupsAddOption(name, value,
10901 printer->num_options,
10902 &(printer->options));
10903 cupsdLogMessage(CUPSD_LOG_DEBUG,
10904 "Setting %s to %s...", attr->name, value);
10905 break;
10906
10907 case IPP_TAG_RANGE :
10908 sprintf(value, "%d-%d", attr->values[0].range.lower,
10909 attr->values[0].range.upper);
10910 printer->num_options = cupsAddOption(name, value,
10911 printer->num_options,
10912 &(printer->options));
10913 cupsdLogMessage(CUPSD_LOG_DEBUG,
10914 "Setting %s to %s...", attr->name, value);
10915 break;
10916
10917 case IPP_TAG_RESOLUTION :
10918 sprintf(value, "%dx%d%s", attr->values[0].resolution.xres,
10919 attr->values[0].resolution.yres,
10920 attr->values[0].resolution.units == IPP_RES_PER_INCH ?
3e7fe0ca 10921 "dpi" : "dpcm");
b423cd4c 10922 printer->num_options = cupsAddOption(name, value,
10923 printer->num_options,
10924 &(printer->options));
10925 cupsdLogMessage(CUPSD_LOG_DEBUG,
10926 "Setting %s to %s...", attr->name, value);
10927 break;
10928
10929 default :
10930 /* Do nothing for other values */
10931 break;
10932 }
10933 }
f0b589f7
MS
10934
10935 return (1);
b423cd4c 10936}
10937
10938
ef416fc2 10939/*
10940 * 'start_printer()' - Start a printer.
10941 */
10942
10943static void
10944start_printer(cupsd_client_t *con, /* I - Client connection */
10945 ipp_attribute_t *uri) /* I - Printer URI */
10946{
10d09e33 10947 int i; /* Temporary variable */
ef416fc2 10948 http_status_t status; /* Policy status */
bc44d920 10949 cups_ptype_t dtype; /* Destination type (printer/class) */
ef416fc2 10950 cupsd_printer_t *printer; /* Printer data */
10951
10952
10953 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con,
996acce8 10954 con->number, uri->values[0].string.text);
ef416fc2 10955
10956 /*
10957 * Is the destination valid?
10958 */
10959
f7deaa1a 10960 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 10961 {
10962 /*
10963 * Bad URI...
10964 */
10965
10966 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 10967 _("The printer or class does not exist."));
ef416fc2 10968 return;
10969 }
10970
10971 /*
10972 * Check policy...
10973 */
10974
10975 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10976 {
f899b121 10977 send_http_error(con, status, printer);
ef416fc2 10978 return;
10979 }
10980
10981 /*
10982 * Start the printer...
10983 */
10984
10985 printer->state_message[0] = '\0';
10986
10987 cupsdStartPrinter(printer, 1);
10988
10989 if (dtype & CUPS_PRINTER_CLASS)
f7deaa1a 10990 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".",
10991 printer->name, get_username(con));
e00b005a 10992 else
f7deaa1a 10993 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".",
10994 printer->name, get_username(con));
ef416fc2 10995
10996 cupsdCheckJobs();
10997
10d09e33
MS
10998 /*
10999 * Check quotas...
11000 */
11001
11002 if ((i = check_quotas(con, printer)) < 0)
11003 {
11004 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
11005 return;
11006 }
11007 else if (i == 0)
11008 {
11009 send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print."));
11010 return;
11011 }
11012
ef416fc2 11013 /*
11014 * Everything was ok, so return OK status...
11015 */
11016
11017 con->response->request.status.status_code = IPP_OK;
11018}
11019
11020
11021/*
11022 * 'stop_printer()' - Stop a printer.
11023 */
11024
11025static void
11026stop_printer(cupsd_client_t *con, /* I - Client connection */
11027 ipp_attribute_t *uri) /* I - Printer URI */
11028{
11029 http_status_t status; /* Policy status */
bc44d920 11030 cups_ptype_t dtype; /* Destination type (printer/class) */
ef416fc2 11031 cupsd_printer_t *printer; /* Printer data */
11032 ipp_attribute_t *attr; /* printer-state-message attribute */
11033
11034
11035 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con,
996acce8 11036 con->number, uri->values[0].string.text);
ef416fc2 11037
11038 /*
11039 * Is the destination valid?
11040 */
11041
f7deaa1a 11042 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 11043 {
11044 /*
11045 * Bad URI...
11046 */
11047
11048 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 11049 _("The printer or class does not exist."));
ef416fc2 11050 return;
11051 }
11052
11053 /*
11054 * Check policy...
11055 */
11056
11057 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
11058 {
f899b121 11059 send_http_error(con, status, printer);
ef416fc2 11060 return;
11061 }
11062
11063 /*
11064 * Stop the printer...
11065 */
11066
11067 if ((attr = ippFindAttribute(con->request, "printer-state-message",
11068 IPP_TAG_TEXT)) == NULL)
5a9febac 11069 strlcpy(printer->state_message, "Paused", sizeof(printer->state_message));
ef416fc2 11070 else
11071 {
11072 strlcpy(printer->state_message, attr->values[0].string.text,
11073 sizeof(printer->state_message));
11074 }
11075
11076 cupsdStopPrinter(printer, 1);
11077
11078 if (dtype & CUPS_PRINTER_CLASS)
f7deaa1a 11079 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".",
11080 printer->name, get_username(con));
ef416fc2 11081 else
f7deaa1a 11082 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".",
11083 printer->name, get_username(con));
ef416fc2 11084
11085 /*
11086 * Everything was ok, so return OK status...
11087 */
11088
11089 con->response->request.status.status_code = IPP_OK;
11090}
11091
11092
89d46774 11093/*
11094 * 'url_encode_attr()' - URL-encode a string attribute.
11095 */
11096
11097static void
11098url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */
11099 char *buffer,/* I - String buffer */
07623986 11100 size_t bufsize)/* I - Size of buffer */
89d46774 11101{
11102 int i; /* Looping var */
11103 char *bufptr, /* Pointer into buffer */
b94498cf 11104 *bufend; /* End of buffer */
89d46774 11105
11106
11107 strlcpy(buffer, attr->name, bufsize);
11108 bufptr = buffer + strlen(buffer);
11109 bufend = buffer + bufsize - 1;
11110
11111 for (i = 0; i < attr->num_values; i ++)
11112 {
11113 if (bufptr >= bufend)
11114 break;
11115
11116 if (i)
11117 *bufptr++ = ',';
11118 else
11119 *bufptr++ = '=';
11120
11121 if (bufptr >= bufend)
11122 break;
11123
11124 *bufptr++ = '\'';
11125
07623986 11126 bufptr = url_encode_string(attr->values[i].string.text, bufptr, (size_t)(bufend - bufptr + 1));
89d46774 11127
11128 if (bufptr >= bufend)
11129 break;
11130
11131 *bufptr++ = '\'';
11132 }
11133
11134 *bufptr = '\0';
11135}
11136
11137
b94498cf 11138/*
11139 * 'url_encode_string()' - URL-encode a string.
11140 */
11141
11142static char * /* O - End of string */
11143url_encode_string(const char *s, /* I - String */
11144 char *buffer, /* I - String buffer */
07623986 11145 size_t bufsize) /* I - Size of buffer */
b94498cf 11146{
11147 char *bufptr, /* Pointer into buffer */
11148 *bufend; /* End of buffer */
11149 static const char *hex = "0123456789ABCDEF";
11150 /* Hex digits */
11151
11152
11153 bufptr = buffer;
11154 bufend = buffer + bufsize - 1;
11155
11156 while (*s && bufptr < bufend)
11157 {
f701418f 11158 if (*s == ' ' || *s == '%' || *s == '+')
b94498cf 11159 {
11160 if (bufptr >= (bufend - 2))
11161 break;
11162
11163 *bufptr++ = '%';
11164 *bufptr++ = hex[(*s >> 4) & 15];
11165 *bufptr++ = hex[*s & 15];
11166
11167 s ++;
11168 }
11169 else if (*s == '\'' || *s == '\\')
11170 {
11171 if (bufptr >= (bufend - 1))
11172 break;
11173
11174 *bufptr++ = '\\';
11175 *bufptr++ = *s++;
11176 }
11177 else
11178 *bufptr++ = *s++;
11179 }
11180
11181 *bufptr = '\0';
11182
11183 return (bufptr);
11184}
11185
11186
ef416fc2 11187/*
11188 * 'user_allowed()' - See if a user is allowed to print to a queue.
11189 */
11190
11191static int /* O - 0 if not allowed, 1 if allowed */
11192user_allowed(cupsd_printer_t *p, /* I - Printer or class */
11193 const char *username) /* I - Username */
11194{
ef416fc2 11195 struct passwd *pw; /* User password data */
5bd77a73 11196 char baseuser[256], /* Base username */
10d09e33
MS
11197 *baseptr, /* Pointer to "@" in base username */
11198 *name; /* Current user name */
ef416fc2 11199
11200
10d09e33 11201 if (cupsArrayCount(p->users) == 0)
ef416fc2 11202 return (1);
11203
11204 if (!strcmp(username, "root"))
11205 return (1);
11206
5bd77a73
MS
11207 if (strchr(username, '@'))
11208 {
11209 /*
11210 * Strip @REALM for username check...
11211 */
11212
11213 strlcpy(baseuser, username, sizeof(baseuser));
11214
11215 if ((baseptr = strchr(baseuser, '@')) != NULL)
11216 *baseptr = '\0';
11217
11218 username = baseuser;
11219 }
11220
ef416fc2 11221 pw = getpwnam(username);
11222 endpwent();
11223
10d09e33
MS
11224 for (name = (char *)cupsArrayFirst(p->users);
11225 name;
11226 name = (char *)cupsArrayNext(p->users))
ef416fc2 11227 {
10d09e33 11228 if (name[0] == '@')
ef416fc2 11229 {
11230 /*
11231 * Check group membership...
11232 */
11233
10d09e33 11234 if (cupsdCheckGroup(username, pw, name + 1))
ef416fc2 11235 break;
11236 }
10d09e33 11237 else if (name[0] == '#')
8922323b
MS
11238 {
11239 /*
11240 * Check UUID...
11241 */
11242
10d09e33 11243 if (cupsdCheckGroup(username, pw, name))
8922323b
MS
11244 break;
11245 }
88f9aafc 11246 else if (!_cups_strcasecmp(username, name))
ef416fc2 11247 break;
11248 }
11249
10d09e33 11250 return ((name != NULL) != p->deny_users);
ef416fc2 11251}
11252
11253
11254/*
11255 * 'validate_job()' - Validate printer options and destination.
11256 */
11257
11258static void
11259validate_job(cupsd_client_t *con, /* I - Client connection */
11260 ipp_attribute_t *uri) /* I - Printer URI */
11261{
11262 http_status_t status; /* Policy status */
7d5824d6
MS
11263 ipp_attribute_t *attr; /* Current attribute */
11264#ifdef HAVE_SSL
11265 ipp_attribute_t *auth_info; /* auth-info attribute */
11266#endif /* HAVE_SSL */
5a9febac
MS
11267 ipp_attribute_t *format, /* Document-format attribute */
11268 *name; /* Job-name attribute */
bc44d920 11269 cups_ptype_t dtype; /* Destination type (printer/class) */
ef416fc2 11270 char super[MIME_MAX_SUPER],
11271 /* Supertype of file */
11272 type[MIME_MAX_TYPE];
11273 /* Subtype of file */
11274 cupsd_printer_t *printer; /* Printer */
11275
11276
11277 cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con,
996acce8 11278 con->number, uri->values[0].string.text);
ef416fc2 11279
11280 /*
11281 * OK, see if the client is sending the document compressed - CUPS
11282 * doesn't support compression yet...
11283 */
11284
fa73b229 11285 if ((attr = ippFindAttribute(con->request, "compression",
c7017ecc 11286 IPP_TAG_KEYWORD)) != NULL)
ef416fc2 11287 {
c7017ecc
MS
11288 if (strcmp(attr->values[0].string.text, "none")
11289#ifdef HAVE_LIBZ
11290 && strcmp(attr->values[0].string.text, "gzip")
11291#endif /* HAVE_LIBZ */
11292 )
11293 {
11294 send_ipp_status(con, IPP_ATTRIBUTES,
5a9febac 11295 _("Unsupported 'compression' value \"%s\"."),
c7017ecc
MS
11296 attr->values[0].string.text);
11297 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
11298 "compression", NULL, attr->values[0].string.text);
11299 return;
11300 }
ef416fc2 11301 }
11302
11303 /*
11304 * Is it a format we support?
11305 */
11306
11307 if ((format = ippFindAttribute(con->request, "document-format",
11308 IPP_TAG_MIMETYPE)) != NULL)
11309 {
c5b24bfa 11310 if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]",
bc44d920 11311 super, type) != 2)
ef416fc2 11312 {
5a9febac
MS
11313 send_ipp_status(con, IPP_BAD_REQUEST,
11314 _("Bad 'document-format' value \"%s\"."),
ef416fc2 11315 format->values[0].string.text);
11316 return;
11317 }
11318
fa73b229 11319 if ((strcmp(super, "application") || strcmp(type, "octet-stream")) &&
11320 !mimeType(MimeDatabase, super, type))
ef416fc2 11321 {
11322 cupsdLogMessage(CUPSD_LOG_INFO,
11323 "Hint: Do you have the raw file printing rules enabled?");
11324 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
5a9febac 11325 _("Unsupported 'document-format' value \"%s\"."),
ef416fc2 11326 format->values[0].string.text);
11327 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
11328 "document-format", NULL, format->values[0].string.text);
11329 return;
11330 }
11331 }
11332
4c37eb9f
MS
11333 /*
11334 * Is the job-hold-until value valid?
11335 */
11336
11337 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)))
11338 {
11339 send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Unsupported 'job-hold-until' value."));
11340 ippCopyAttribute(con->response, attr, 0);
11341 return;
11342 }
11343
5a9febac
MS
11344 /*
11345 * Is the job-name valid?
11346 */
11347
11348 if ((name = ippFindAttribute(con->request, "job-name", IPP_TAG_ZERO)) != NULL)
11349 {
5a9febac 11350 if ((name->value_tag != IPP_TAG_NAME && name->value_tag != IPP_TAG_NAMELANG) ||
4c37eb9f 11351 name->num_values != 1 || !ippValidateAttribute(name))
5a9febac
MS
11352 {
11353 if (StrictConformance)
11354 {
4c37eb9f 11355 send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Unsupported 'job-name' value."));
5a9febac
MS
11356 ippCopyAttribute(con->response, name, 0);
11357 return;
11358 }
11359 else
11360 {
4c37eb9f 11361 cupsdLogMessage(CUPSD_LOG_WARN, "Unsupported 'job-name' value, deleting from request.");
5a9febac
MS
11362 ippDeleteAttribute(con->request, name);
11363 }
11364 }
11365 }
11366
ef416fc2 11367 /*
11368 * Is the destination valid?
11369 */
11370
f7deaa1a 11371 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
ef416fc2 11372 {
11373 /*
11374 * Bad URI...
11375 */
11376
11377 send_ipp_status(con, IPP_NOT_FOUND,
84315f46 11378 _("The printer or class does not exist."));
ef416fc2 11379 return;
11380 }
11381
11382 /*
11383 * Check policy...
11384 */
11385
7d5824d6 11386#ifdef HAVE_SSL
c7017ecc 11387 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
7d5824d6 11388#endif /* HAVE_SSL */
c7017ecc 11389
ef416fc2 11390 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
11391 {
f899b121 11392 send_http_error(con, status, printer);
ef416fc2 11393 return;
11394 }
c7017ecc
MS
11395 else if (printer->num_auth_info_required == 1 &&
11396 !strcmp(printer->auth_info_required[0], "negotiate") &&
11397 !con->username[0])
11398 {
11399 send_http_error(con, HTTP_UNAUTHORIZED, printer);
11400 return;
11401 }
11402#ifdef HAVE_SSL
996acce8
MS
11403 else if (auth_info && !con->http->tls &&
11404 !httpAddrLocalhost(con->http->hostaddr))
c7017ecc
MS
11405 {
11406 /*
11407 * Require encryption of auth-info over non-local connections...
11408 */
11409
11410 send_http_error(con, HTTP_UPGRADE_REQUIRED, printer);
11411 return;
11412 }
11413#endif /* HAVE_SSL */
ef416fc2 11414
11415 /*
11416 * Everything was ok, so return OK status...
11417 */
11418
11419 con->response->request.status.status_code = IPP_OK;
11420}
11421
11422
11423/*
11424 * 'validate_name()' - Make sure the printer name only contains valid chars.
11425 */
11426
bc44d920 11427static int /* O - 0 if name is no good, 1 if good */
ef416fc2 11428validate_name(const char *name) /* I - Name to check */
11429{
11430 const char *ptr; /* Pointer into name */
11431
11432
11433 /*
11434 * Scan the whole name...
11435 */
11436
11437 for (ptr = name; *ptr; ptr ++)
f7deaa1a 11438 if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
ef416fc2 11439 return (0);
11440
11441 /*
11442 * All the characters are good; validate the length, too...
11443 */
11444
11445 return ((ptr - name) < 128);
11446}
11447
11448
11449/*
11450 * 'validate_user()' - Validate the user for the request.
11451 */
11452
11453static int /* O - 1 if permitted, 0 otherwise */
11454validate_user(cupsd_job_t *job, /* I - Job */
11455 cupsd_client_t *con, /* I - Client connection */
11456 const char *owner, /* I - Owner of job/resource */
11457 char *username, /* O - Authenticated username */
07623986 11458 size_t userlen) /* I - Length of username */
ef416fc2 11459{
ef416fc2 11460 cupsd_printer_t *printer; /* Printer for job */
11461
11462
07623986 11463 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 11464
11465 /*
11466 * Validate input...
11467 */
11468
11469 if (!con || !owner || !username || userlen <= 0)
11470 return (0);
11471
11472 /*
11473 * Get the best authenticated username that is available.
11474 */
11475
e00b005a 11476 strlcpy(username, get_username(con), userlen);
ef416fc2 11477
11478 /*
11479 * Check the username against the owner...
11480 */
11481
80ca4592 11482 printer = cupsdFindDest(job->dest);
ef416fc2 11483
11484 return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr,
11485 con, owner) == HTTP_OK);
11486}